1 /*
2 *
3 * Copyright 2017 gRPC authors.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 */
18
19 #include "src/core/ext/filters/client_channel/parse_address.h"
20 #include "src/core/lib/iomgr/sockaddr.h"
21 #include "src/core/lib/iomgr/socket_utils.h"
22
23 #include <string.h>
24 #ifdef GRPC_HAVE_UNIX_SOCKET
25 #include <sys/un.h>
26 #endif
27
28 #include <grpc/grpc.h>
29 #include <grpc/support/log.h>
30
31 #include "src/core/lib/iomgr/exec_ctx.h"
32 #include "src/core/lib/iomgr/socket_utils.h"
33 #include "test/core/util/test_config.h"
34
35 #ifdef GRPC_HAVE_UNIX_SOCKET
36
test_grpc_parse_unix(const char * uri_text,const char * pathname)37 static void test_grpc_parse_unix(const char* uri_text, const char* pathname) {
38 grpc_core::ExecCtx exec_ctx;
39 grpc_uri* uri = grpc_uri_parse(uri_text, 0);
40 grpc_resolved_address addr;
41
42 GPR_ASSERT(1 == grpc_parse_unix(uri, &addr));
43 struct sockaddr_un* addr_un =
44 reinterpret_cast<struct sockaddr_un*>(addr.addr);
45 GPR_ASSERT(AF_UNIX == addr_un->sun_family);
46 GPR_ASSERT(0 == strcmp(addr_un->sun_path, pathname));
47
48 grpc_uri_destroy(uri);
49 }
50
51 #else /* GRPC_HAVE_UNIX_SOCKET */
52
test_grpc_parse_unix(const char * uri_text,const char * pathname)53 static void test_grpc_parse_unix(const char* uri_text, const char* pathname) {}
54
55 #endif /* GRPC_HAVE_UNIX_SOCKET */
56
test_grpc_parse_ipv4(const char * uri_text,const char * host,unsigned short port)57 static void test_grpc_parse_ipv4(const char* uri_text, const char* host,
58 unsigned short port) {
59 grpc_core::ExecCtx exec_ctx;
60 grpc_uri* uri = grpc_uri_parse(uri_text, 0);
61 grpc_resolved_address addr;
62 char ntop_buf[GRPC_INET_ADDRSTRLEN];
63
64 GPR_ASSERT(1 == grpc_parse_ipv4(uri, &addr));
65 grpc_sockaddr_in* addr_in = reinterpret_cast<grpc_sockaddr_in*>(addr.addr);
66 GPR_ASSERT(GRPC_AF_INET == addr_in->sin_family);
67 GPR_ASSERT(nullptr != grpc_inet_ntop(GRPC_AF_INET, &addr_in->sin_addr,
68 ntop_buf, sizeof(ntop_buf)));
69 GPR_ASSERT(0 == strcmp(ntop_buf, host));
70 GPR_ASSERT(grpc_ntohs(addr_in->sin_port) == port);
71
72 grpc_uri_destroy(uri);
73 }
74
test_grpc_parse_ipv6(const char * uri_text,const char * host,unsigned short port,uint32_t scope_id)75 static void test_grpc_parse_ipv6(const char* uri_text, const char* host,
76 unsigned short port, uint32_t scope_id) {
77 grpc_core::ExecCtx exec_ctx;
78 grpc_uri* uri = grpc_uri_parse(uri_text, 0);
79 grpc_resolved_address addr;
80 char ntop_buf[GRPC_INET6_ADDRSTRLEN];
81
82 GPR_ASSERT(1 == grpc_parse_ipv6(uri, &addr));
83 grpc_sockaddr_in6* addr_in6 = reinterpret_cast<grpc_sockaddr_in6*>(addr.addr);
84 GPR_ASSERT(GRPC_AF_INET6 == addr_in6->sin6_family);
85 GPR_ASSERT(nullptr != grpc_inet_ntop(GRPC_AF_INET6, &addr_in6->sin6_addr,
86 ntop_buf, sizeof(ntop_buf)));
87 GPR_ASSERT(0 == strcmp(ntop_buf, host));
88 GPR_ASSERT(grpc_ntohs(addr_in6->sin6_port) == port);
89 GPR_ASSERT(addr_in6->sin6_scope_id == scope_id);
90
91 grpc_uri_destroy(uri);
92 }
93
94 /* Test parsing invalid ipv6 addresses (valid uri_text but invalid ipv6 addr) */
test_grpc_parse_ipv6_invalid(const char * uri_text)95 static void test_grpc_parse_ipv6_invalid(const char* uri_text) {
96 grpc_core::ExecCtx exec_ctx;
97 grpc_uri* uri = grpc_uri_parse(uri_text, 0);
98 grpc_resolved_address addr;
99 GPR_ASSERT(!grpc_parse_ipv6(uri, &addr));
100 grpc_uri_destroy(uri);
101 }
102
main(int argc,char ** argv)103 int main(int argc, char** argv) {
104 grpc_test_init(argc, argv);
105 grpc_init();
106
107 test_grpc_parse_unix("unix:/path/name", "/path/name");
108 test_grpc_parse_ipv4("ipv4:192.0.2.1:12345", "192.0.2.1", 12345);
109 test_grpc_parse_ipv6("ipv6:[2001:db8::1]:12345", "2001:db8::1", 12345, 0);
110 test_grpc_parse_ipv6("ipv6:[2001:db8::1%252]:12345", "2001:db8::1", 12345, 2);
111
112 /* Address length greater than GRPC_INET6_ADDRSTRLEN */
113 test_grpc_parse_ipv6_invalid(
114 "ipv6:WWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWW45%"
115 "v6:45%x$1*");
116
117 grpc_shutdown();
118 }
119