blob: 478da5c3f419a059a71edf1673fbe07f18f2c3c1 [file] [log] [blame]
Craig Tiller147c4f42015-12-11 12:32:39 -08001/*
2 *
3 * Copyright 2015, Google Inc.
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
8 * met:
9 *
10 * * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * * Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following disclaimer
14 * in the documentation and/or other materials provided with the
15 * distribution.
16 * * Neither the name of Google Inc. nor the names of its
17 * contributors may be used to endorse or promote products derived from
18 * this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 *
32 */
33
34#include "src/core/client_config/resolvers/dns_resolver.h"
35
36#include <string.h>
37
38#include <grpc/support/log.h>
39
40#include "src/core/client_config/resolver.h"
41#include "test/core/util/test_config.h"
42
43static void subchannel_factory_ref(grpc_subchannel_factory *scv) {}
44static void subchannel_factory_unref(grpc_exec_ctx *exec_ctx,
45 grpc_subchannel_factory *scv) {}
46static grpc_subchannel *subchannel_factory_create_subchannel(grpc_exec_ctx *exec_ctx,
47 grpc_subchannel_factory *factory,
48 grpc_subchannel_args *args) {
49 GPR_UNREACHABLE_CODE(return NULL);
50}
51
52static const grpc_subchannel_factory_vtable sc_vtable = {
53 subchannel_factory_ref,
54 subchannel_factory_unref,
55 subchannel_factory_create_subchannel
56};
57
58static grpc_subchannel_factory sc_factory = { &sc_vtable };
59
60static void test_succeeds(grpc_resolver_factory *factory, const char *string) {
61 grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
62 grpc_uri *uri = grpc_uri_parse(string, 0);
63 grpc_resolver_args args;
64 grpc_resolver *resolver;
65 gpr_log(GPR_DEBUG, "test: '%s' should be valid for '%s'", string, factory->vtable->scheme);
66 GPR_ASSERT(uri);
67 memset(&args, 0, sizeof(args));
68 args.uri = uri;
69 args.subchannel_factory = &sc_factory;
70 resolver = grpc_resolver_factory_create_resolver(factory, &args);
71 GPR_ASSERT(resolver != NULL);
72 GRPC_RESOLVER_UNREF(&exec_ctx, resolver, "test_succeeds");
73 grpc_uri_destroy(uri);
74 grpc_exec_ctx_finish(&exec_ctx);
75}
76
77static void test_fails(grpc_resolver_factory *factory, const char *string) {
78 grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
79 grpc_uri *uri = grpc_uri_parse(string, 0);
80 grpc_resolver_args args;
81 grpc_resolver *resolver;
82 gpr_log(GPR_DEBUG, "test: '%s' should be invalid for '%s'", string, factory->vtable->scheme);
83 GPR_ASSERT(uri);
84 memset(&args, 0, sizeof(args));
85 args.uri = uri;
86 resolver = grpc_resolver_factory_create_resolver(factory, &args);
87 GPR_ASSERT(resolver == NULL);
88 grpc_uri_destroy(uri);
89 grpc_exec_ctx_finish(&exec_ctx);
90}
91
92int main(int argc, char **argv) {
93 grpc_resolver_factory *dns;
94 grpc_test_init(argc, argv);
95
96 dns = grpc_dns_resolver_factory_create();
97
98 test_succeeds(dns, "dns:10.2.1.1");
99 test_succeeds(dns, "dns:10.2.1.1:1234");
100 test_succeeds(dns, "ipv4:www.google.com");
101 test_fails(dns, "ipv4://8.8.8.8/8.8.8.8:8888");
102
103 grpc_resolver_factory_unref(dns);
104
105 return 0;
106}