blob: 043b8821840ab1f1d316303064499ffcd1c7f8d1 [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
Craig Tiller9533d042016-03-25 17:11:06 -070034#include "src/core/lib/client_config/resolvers/dns_resolver.h"
Craig Tiller147c4f42015-12-11 12:32:39 -080035
36#include <string.h>
37
38#include <grpc/support/log.h>
39
Craig Tiller9533d042016-03-25 17:11:06 -070040#include "src/core/lib/client_config/resolver.h"
Craig Tiller147c4f42015-12-11 12:32:39 -080041#include "test/core/util/test_config.h"
42
43static void subchannel_factory_ref(grpc_subchannel_factory *scv) {}
Craig Tiller620e9652015-12-14 12:02:50 -080044static void subchannel_factory_unref(grpc_exec_ctx *exec_ctx,
Craig Tiller147c4f42015-12-11 12:32:39 -080045 grpc_subchannel_factory *scv) {}
Craig Tiller620e9652015-12-14 12:02:50 -080046static grpc_subchannel *subchannel_factory_create_subchannel(
47 grpc_exec_ctx *exec_ctx, grpc_subchannel_factory *factory,
48 grpc_subchannel_args *args) {
Craig Tiller147c4f42015-12-11 12:32:39 -080049 GPR_UNREACHABLE_CODE(return NULL);
50}
51
52static const grpc_subchannel_factory_vtable sc_vtable = {
Craig Tiller620e9652015-12-14 12:02:50 -080053 subchannel_factory_ref, subchannel_factory_unref,
54 subchannel_factory_create_subchannel};
Craig Tiller147c4f42015-12-11 12:32:39 -080055
Craig Tiller620e9652015-12-14 12:02:50 -080056static grpc_subchannel_factory sc_factory = {&sc_vtable};
Craig Tiller147c4f42015-12-11 12:32:39 -080057
58static void test_succeeds(grpc_resolver_factory *factory, const char *string) {
59 grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
60 grpc_uri *uri = grpc_uri_parse(string, 0);
61 grpc_resolver_args args;
62 grpc_resolver *resolver;
Craig Tiller620e9652015-12-14 12:02:50 -080063 gpr_log(GPR_DEBUG, "test: '%s' should be valid for '%s'", string,
64 factory->vtable->scheme);
Craig Tiller147c4f42015-12-11 12:32:39 -080065 GPR_ASSERT(uri);
66 memset(&args, 0, sizeof(args));
67 args.uri = uri;
68 args.subchannel_factory = &sc_factory;
69 resolver = grpc_resolver_factory_create_resolver(factory, &args);
70 GPR_ASSERT(resolver != NULL);
71 GRPC_RESOLVER_UNREF(&exec_ctx, resolver, "test_succeeds");
72 grpc_uri_destroy(uri);
73 grpc_exec_ctx_finish(&exec_ctx);
74}
75
76static void test_fails(grpc_resolver_factory *factory, const char *string) {
77 grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
78 grpc_uri *uri = grpc_uri_parse(string, 0);
79 grpc_resolver_args args;
80 grpc_resolver *resolver;
Craig Tiller620e9652015-12-14 12:02:50 -080081 gpr_log(GPR_DEBUG, "test: '%s' should be invalid for '%s'", string,
82 factory->vtable->scheme);
Craig Tiller147c4f42015-12-11 12:32:39 -080083 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}