Debian Codegraph @

test/core/address_utils/parse_address_test.cc

raw
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/lib/address_utils/parse_address.h"
20 #ifdef GRPC_HAVE_UNIX_SOCKET
21 #include <sys/un.h>
22 #endif
23
24 #include <string>
25
26 #include "absl/status/status.h"
27 #include "absl/strings/match.h"
28 #include "gtest/gtest.h"
29
30 #include <grpc/grpc.h>
31 #include <grpc/support/log.h>
32
33 #include "src/core/lib/iomgr/exec_ctx.h"
34 #include "src/core/lib/iomgr/port.h"
35 #include "src/core/lib/iomgr/sockaddr.h"
36 #include "src/core/lib/iomgr/socket_utils.h"
37 #include "test/core/util/test_config.h"
38
39 #ifdef GRPC_HAVE_UNIX_SOCKET
40
41 static void test_grpc_parse_unix(const char* uri_text, const char* pathname) {
42 grpc_core::ExecCtx exec_ctx;
43 absl::StatusOr<grpc_core::URI> uri = grpc_core::URI::Parse(uri_text);
44 if (!uri.ok()) {
45 gpr_log(GPR_ERROR, "%s", uri.status().ToString().c_str());
46 ASSERT_TRUE(uri.ok());
47 }
48 grpc_resolved_address addr;
49
50 ASSERT_TRUE(grpc_parse_uri(*uri, &addr));
51 struct sockaddr_un* addr_un =
52 reinterpret_cast<struct sockaddr_un*>(addr.addr);
53 ASSERT_EQ(AF_UNIX, addr_un->sun_family);
54 ASSERT_STREQ(addr_un->sun_path, pathname);
55 }
56
57 static void test_grpc_parse_unix_abstract(const char* uri_text,
58 const char* pathname) {
59 grpc_core::ExecCtx exec_ctx;
60 absl::StatusOr<grpc_core::URI> uri = grpc_core::URI::Parse(uri_text);
61 if (!uri.ok()) {
62 gpr_log(GPR_ERROR, "%s", uri.status().ToString().c_str());
63 ASSERT_TRUE(uri.ok());
64 }
65 grpc_resolved_address addr;
66
67 ASSERT_TRUE(grpc_parse_uri(*uri, &addr));
68 struct sockaddr_un* addr_un =
69 reinterpret_cast<struct sockaddr_un*>(addr.addr);
70 ASSERT_EQ(AF_UNIX, addr_un->sun_family);
71 ASSERT_EQ('\0', addr_un->sun_path[0]);
72 ASSERT_TRUE(absl::StartsWith(addr_un->sun_path + 1, pathname));
73 }
74
75 #else /* GRPC_HAVE_UNIX_SOCKET */
76
77 static void test_grpc_parse_unix(const char* uri_text, const char* pathname) {}
78 static void test_grpc_parse_unix_abstract(const char* uri_text,
79 const char* pathname) {}
80
81 #endif /* GRPC_HAVE_UNIX_SOCKET */
82
83 static void test_grpc_parse_ipv4(const char* uri_text, const char* host,
84 unsigned short port) {
85 grpc_core::ExecCtx exec_ctx;
86 absl::StatusOr<grpc_core::URI> uri = grpc_core::URI::Parse(uri_text);
87 if (!uri.ok()) {
88 gpr_log(GPR_ERROR, "%s", uri.status().ToString().c_str());
89 ASSERT_TRUE(uri.ok());
90 }
91 grpc_resolved_address addr;
92 char ntop_buf[GRPC_INET_ADDRSTRLEN];
93
94 ASSERT_TRUE(grpc_parse_ipv4(*uri, &addr));
95 grpc_sockaddr_in* addr_in = reinterpret_cast<grpc_sockaddr_in*>(addr.addr);
96 ASSERT_EQ(GRPC_AF_INET, addr_in->sin_family);
97 ASSERT_NE(nullptr, grpc_inet_ntop(GRPC_AF_INET, &addr_in->sin_addr, ntop_buf,
98 sizeof(ntop_buf)));
99 ASSERT_STREQ(ntop_buf, host);
100 ASSERT_EQ(grpc_ntohs(addr_in->sin_port), port);
101 }
102
103 static void test_grpc_parse_ipv6(const char* uri_text, const char* host,
104 unsigned short port, uint32_t scope_id) {
105 grpc_core::ExecCtx exec_ctx;
106 absl::StatusOr<grpc_core::URI> uri = grpc_core::URI::Parse(uri_text);
107 if (!uri.ok()) {
108 gpr_log(GPR_ERROR, "%s", uri.status().ToString().c_str());
109 ASSERT_TRUE(uri.ok());
110 }
111 grpc_resolved_address addr;
112 char ntop_buf[GRPC_INET6_ADDRSTRLEN];
113 ASSERT_TRUE(grpc_parse_ipv6(*uri, &addr));
114 grpc_sockaddr_in6* addr_in6 = reinterpret_cast<grpc_sockaddr_in6*>(addr.addr);
115 ASSERT_EQ(GRPC_AF_INET6, addr_in6->sin6_family);
116 ASSERT_NE(nullptr, grpc_inet_ntop(GRPC_AF_INET6, &addr_in6->sin6_addr,
117 ntop_buf, sizeof(ntop_buf)));
118 ASSERT_STREQ(ntop_buf, host);
119 ASSERT_EQ(grpc_ntohs(addr_in6->sin6_port), port);
120 ASSERT_EQ(addr_in6->sin6_scope_id, scope_id);
121 }
122
123 /* Test parsing invalid ipv6 addresses (valid uri_text but invalid ipv6 addr) */
124 static void test_grpc_parse_ipv6_invalid(const char* uri_text) {
125 grpc_core::ExecCtx exec_ctx;
126 absl::StatusOr<grpc_core::URI> uri = grpc_core::URI::Parse(uri_text);
127 if (!uri.ok()) {
128 gpr_log(GPR_ERROR, "%s", uri.status().ToString().c_str());
129 ASSERT_TRUE(uri.ok());
130 }
131 grpc_resolved_address addr;
132 ASSERT_FALSE(grpc_parse_ipv6(*uri, &addr));
133 }
134
135 TEST(ParseAddressTest, MainTest) {
136 grpc_init();
137
138 test_grpc_parse_unix("unix:/path/name", "/path/name");
139 test_grpc_parse_unix_abstract("unix-abstract:foobar", "foobar");
140 test_grpc_parse_ipv4("ipv4:192.0.2.1:12345", "192.0.2.1", 12345);
141 test_grpc_parse_ipv6("ipv6:[2001:db8::1]:12345", "2001:db8::1", 12345, 0);
142 test_grpc_parse_ipv6("ipv6:[2001:db8::1%252]:12345", "2001:db8::1", 12345, 2);
143
144 /* Address length greater than GRPC_INET6_ADDRSTRLEN */
145 test_grpc_parse_ipv6_invalid(
146 "ipv6:WWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWW45%"
147 "25v6:45%25x$1*");
148
149 grpc_shutdown();
150 }
151
152 int main(int argc, char** argv) {
153 grpc::testing::TestEnvironment env(&argc, argv);
154 ::testing::InitGoogleTest(&argc, argv);
155 return RUN_ALL_TESTS();
156 }