blob: b11546b6b1cb18d6eaf8d3cb262e48ac88ee48c5 [file] [log] [blame]
Craig Tillerb8bd62e2015-12-10 15:51:15 -08001/*
2 *
Craig Tiller6169d5f2016-03-31 07:46:18 -07003 * Copyright 2015, Google Inc.
Craig Tillerb8bd62e2015-12-10 15:51:15 -08004 * 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 Tillerb8bd62e2015-12-10 15:51:15 -080034#include <string.h>
35
36#include <grpc/support/log.h>
37
Craig Tillerd4c98332016-03-31 13:45:47 -070038#include "src/core/ext/client_config/resolver_registry.h"
Craig Tillerb8bd62e2015-12-10 15:51:15 -080039#include "test/core/util/test_config.h"
40
David Garcia Quintasfcf7ad62016-03-29 21:55:34 -070041static void client_channel_factory_ref(grpc_client_channel_factory *scv) {}
42static void client_channel_factory_unref(grpc_exec_ctx *exec_ctx,
43 grpc_client_channel_factory *scv) {}
44static grpc_subchannel *client_channel_factory_create_subchannel(
45 grpc_exec_ctx *exec_ctx, grpc_client_channel_factory *factory,
Craig Tiller620e9652015-12-14 12:02:50 -080046 grpc_subchannel_args *args) {
Craig Tillerb8bd62e2015-12-10 15:51:15 -080047 GPR_UNREACHABLE_CODE(return NULL);
48}
49
David Garcia Quintasfcf7ad62016-03-29 21:55:34 -070050static grpc_channel *client_channel_factory_create_channel(
51 grpc_exec_ctx *exec_ctx, grpc_client_channel_factory *cc_factory,
David Garcia Quintas0b868c72016-04-01 14:28:22 -070052 const char *target, grpc_client_channel_type type,
53 grpc_channel_args *args) {
David Garcia Quintasfcf7ad62016-03-29 21:55:34 -070054 GPR_UNREACHABLE_CODE(return NULL);
55}
Craig Tillerb8bd62e2015-12-10 15:51:15 -080056
David Garcia Quintasfcf7ad62016-03-29 21:55:34 -070057static const grpc_client_channel_factory_vtable sc_vtable = {
58 client_channel_factory_ref, client_channel_factory_unref,
59 client_channel_factory_create_subchannel,
60 client_channel_factory_create_channel};
61
62static grpc_client_channel_factory cc_factory = {&sc_vtable};
Craig Tillerb8bd62e2015-12-10 15:51:15 -080063
64static void test_succeeds(grpc_resolver_factory *factory, const char *string) {
65 grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
66 grpc_uri *uri = grpc_uri_parse(string, 0);
67 grpc_resolver_args args;
68 grpc_resolver *resolver;
Craig Tiller620e9652015-12-14 12:02:50 -080069 gpr_log(GPR_DEBUG, "test: '%s' should be valid for '%s'", string,
70 factory->vtable->scheme);
Craig Tillerb8bd62e2015-12-10 15:51:15 -080071 GPR_ASSERT(uri);
72 memset(&args, 0, sizeof(args));
73 args.uri = uri;
David Garcia Quintasfcf7ad62016-03-29 21:55:34 -070074 args.client_channel_factory = &cc_factory;
Craig Tillerb8bd62e2015-12-10 15:51:15 -080075 resolver = grpc_resolver_factory_create_resolver(factory, &args);
76 GPR_ASSERT(resolver != NULL);
77 GRPC_RESOLVER_UNREF(&exec_ctx, resolver, "test_succeeds");
78 grpc_uri_destroy(uri);
79 grpc_exec_ctx_finish(&exec_ctx);
80}
81
82static void test_fails(grpc_resolver_factory *factory, const char *string) {
83 grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
84 grpc_uri *uri = grpc_uri_parse(string, 0);
85 grpc_resolver_args args;
86 grpc_resolver *resolver;
Craig Tiller620e9652015-12-14 12:02:50 -080087 gpr_log(GPR_DEBUG, "test: '%s' should be invalid for '%s'", string,
88 factory->vtable->scheme);
Craig Tillerb8bd62e2015-12-10 15:51:15 -080089 GPR_ASSERT(uri);
90 memset(&args, 0, sizeof(args));
91 args.uri = uri;
92 resolver = grpc_resolver_factory_create_resolver(factory, &args);
93 GPR_ASSERT(resolver == NULL);
94 grpc_uri_destroy(uri);
95 grpc_exec_ctx_finish(&exec_ctx);
96}
97
98int main(int argc, char **argv) {
99 grpc_resolver_factory *ipv4, *ipv6;
100 grpc_test_init(argc, argv);
Craig Tiller65938df2016-03-31 13:08:49 -0700101 grpc_init();
Craig Tillerb8bd62e2015-12-10 15:51:15 -0800102
Craig Tiller65938df2016-03-31 13:08:49 -0700103 ipv4 = grpc_resolver_factory_lookup("ipv4");
104 ipv6 = grpc_resolver_factory_lookup("ipv6");
Craig Tillerb8bd62e2015-12-10 15:51:15 -0800105
106 test_fails(ipv4, "ipv4:10.2.1.1");
107 test_succeeds(ipv4, "ipv4:10.2.1.1:1234");
David Garcia Quintas2c73b222016-03-23 14:09:26 -0700108 test_succeeds(ipv4, "ipv4:10.2.1.1:1234,127.0.0.1:4321");
Craig Tillerb8bd62e2015-12-10 15:51:15 -0800109 test_fails(ipv4, "ipv4:10.2.1.1:123456");
110 test_fails(ipv4, "ipv4:www.google.com");
111 test_fails(ipv4, "ipv4:[");
112 test_fails(ipv4, "ipv4://8.8.8.8/8.8.8.8:8888");
113
114 test_fails(ipv6, "ipv6:[");
115 test_fails(ipv6, "ipv6:[::]");
116 test_succeeds(ipv6, "ipv6:[::]:1234");
117 test_fails(ipv6, "ipv6:[::]:123456");
Craig Tiller795764b2015-12-11 08:56:09 -0800118 test_fails(ipv6, "ipv6:www.google.com");
Craig Tillerb8bd62e2015-12-10 15:51:15 -0800119
120 grpc_resolver_factory_unref(ipv4);
121 grpc_resolver_factory_unref(ipv6);
Craig Tiller65938df2016-03-31 13:08:49 -0700122 grpc_shutdown();
Craig Tillerb8bd62e2015-12-10 15:51:15 -0800123
124 return 0;
125}