blob: 5603a57b5fc5e3bb07e7f57fac1ee99172ef53d7 [file] [log] [blame]
Craig Tiller147c4f42015-12-11 12:32:39 -08001/*
2 *
Craig Tiller6169d5f2016-03-31 07:46:18 -07003 * Copyright 2015, Google Inc.
Craig Tiller147c4f42015-12-11 12:32:39 -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 Tiller147c4f42015-12-11 12:32:39 -080034#include <string.h>
35
36#include <grpc/support/log.h>
37
Mark D. Roth2137cd82016-09-14 09:04:00 -070038#include "src/core/ext/client_channel/resolver_registry.h"
Craig Tiller147c4f42015-12-11 12:32:39 -080039#include "test/core/util/test_config.h"
40
Craig Tiller147c4f42015-12-11 12:32:39 -080041static void test_succeeds(grpc_resolver_factory *factory, const char *string) {
42 grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
43 grpc_uri *uri = grpc_uri_parse(string, 0);
44 grpc_resolver_args args;
45 grpc_resolver *resolver;
Craig Tiller620e9652015-12-14 12:02:50 -080046 gpr_log(GPR_DEBUG, "test: '%s' should be valid for '%s'", string,
47 factory->vtable->scheme);
Craig Tiller147c4f42015-12-11 12:32:39 -080048 GPR_ASSERT(uri);
49 memset(&args, 0, sizeof(args));
50 args.uri = uri;
Yuchen Zeng63e3e3b2016-12-15 12:06:33 -080051 resolver = grpc_resolver_factory_create_resolver(&exec_ctx, factory, &args);
Craig Tiller147c4f42015-12-11 12:32:39 -080052 GPR_ASSERT(resolver != NULL);
53 GRPC_RESOLVER_UNREF(&exec_ctx, resolver, "test_succeeds");
54 grpc_uri_destroy(uri);
55 grpc_exec_ctx_finish(&exec_ctx);
56}
57
58static void test_fails(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 invalid 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;
Yuchen Zeng63e3e3b2016-12-15 12:06:33 -080068 resolver = grpc_resolver_factory_create_resolver(&exec_ctx, factory, &args);
Craig Tiller147c4f42015-12-11 12:32:39 -080069 GPR_ASSERT(resolver == NULL);
70 grpc_uri_destroy(uri);
71 grpc_exec_ctx_finish(&exec_ctx);
72}
73
74int main(int argc, char **argv) {
75 grpc_resolver_factory *dns;
76 grpc_test_init(argc, argv);
Craig Tiller65938df2016-03-31 13:08:49 -070077 grpc_init();
Craig Tiller147c4f42015-12-11 12:32:39 -080078
Craig Tiller65938df2016-03-31 13:08:49 -070079 dns = grpc_resolver_factory_lookup("dns");
Craig Tiller147c4f42015-12-11 12:32:39 -080080
81 test_succeeds(dns, "dns:10.2.1.1");
82 test_succeeds(dns, "dns:10.2.1.1:1234");
83 test_succeeds(dns, "ipv4:www.google.com");
84 test_fails(dns, "ipv4://8.8.8.8/8.8.8.8:8888");
85
86 grpc_resolver_factory_unref(dns);
Craig Tiller65938df2016-03-31 13:08:49 -070087 grpc_shutdown();
Craig Tiller147c4f42015-12-11 12:32:39 -080088
89 return 0;
90}