Debian Codegraph @

test/cpp/naming/address_sorting_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 <string.h>
20 #include <sys/types.h>
21
22 #include <vector>
23
24 #include <address_sorting/address_sorting.h>
25 #include <gmock/gmock.h>
26
27 #include <grpc/grpc.h>
28 #include <grpc/support/alloc.h>
29 #include <grpc/support/log.h>
30 #include <grpc/support/string_util.h>
31 #include <grpc/support/sync.h>
32 #include <grpc/support/time.h>
33
34 #include "src/core/ext/filters/client_channel/client_channel.h"
35 #include "src/core/ext/filters/client_channel/resolver/dns/c_ares/grpc_ares_wrapper.h"
36 #include "src/core/ext/filters/client_channel/resolver/dns/dns_resolver_selection.h"
37 #include "src/core/lib/address_utils/sockaddr_utils.h"
38 #include "src/core/lib/channel/channel_args.h"
39 #include "src/core/lib/gpr/string.h"
40 #include "src/core/lib/gprpp/host_port.h"
41 #include "src/core/lib/iomgr/combiner.h"
42 #include "src/core/lib/iomgr/executor.h"
43 #include "src/core/lib/iomgr/iomgr.h"
44 #include "src/core/lib/iomgr/resolve_address.h"
45 #include "src/core/lib/resolver/resolver.h"
46 #include "src/core/lib/resolver/resolver_registry.h"
47 #include "src/core/lib/resolver/server_address.h"
48 #include "test/core/util/port.h"
49 #include "test/core/util/test_config.h"
50 #include "test/cpp/util/subprocess.h"
51 #include "test/cpp/util/test_config.h"
52
53 #ifndef GPR_WINDOWS
54 #include <arpa/inet.h>
55 #include <netinet/in.h>
56 #include <sys/socket.h>
57 #endif
58
59 namespace {
60
61 struct TestAddress {
62 std::string dest_addr;
63 int family;
64 };
65
66 grpc_resolved_address TestAddressToGrpcResolvedAddress(TestAddress test_addr) {
67 std::string host;
68 std::string port;
69 grpc_resolved_address resolved_addr;
70 grpc_core::SplitHostPort(test_addr.dest_addr.c_str(), &host, &port);
71 if (test_addr.family == AF_INET) {
72 sockaddr_in in_dest;
73 memset(&in_dest, 0, sizeof(sockaddr_in));
74 in_dest.sin_port = htons(atoi(port.c_str()));
75 in_dest.sin_family = AF_INET;
76 GPR_ASSERT(inet_pton(AF_INET, host.c_str(), &in_dest.sin_addr) == 1);
77 memcpy(&resolved_addr.addr, &in_dest, sizeof(sockaddr_in));
78 resolved_addr.len = sizeof(sockaddr_in);
79 } else {
80 GPR_ASSERT(test_addr.family == AF_INET6);
81 sockaddr_in6 in6_dest;
82 memset(&in6_dest, 0, sizeof(sockaddr_in6));
83 in6_dest.sin6_port = htons(atoi(port.c_str()));
84 in6_dest.sin6_family = AF_INET6;
85 GPR_ASSERT(inet_pton(AF_INET6, host.c_str(), &in6_dest.sin6_addr) == 1);
86 memcpy(&resolved_addr.addr, &in6_dest, sizeof(sockaddr_in6));
87 resolved_addr.len = sizeof(sockaddr_in6);
88 }
89 return resolved_addr;
90 }
91
92 class MockSourceAddrFactory : public address_sorting_source_addr_factory {
93 public:
94 MockSourceAddrFactory(
95 bool ipv4_supported, bool ipv6_supported,
96 const std::map<std::string, TestAddress>& dest_addr_to_src_addr)
97 : ipv4_supported_(ipv4_supported),
98 ipv6_supported_(ipv6_supported),
99 dest_addr_to_src_addr_(dest_addr_to_src_addr) {}
100
101 bool GetSourceAddr(const address_sorting_address* dest_addr,
102 address_sorting_address* source_addr) {
103 if ((address_sorting_abstract_get_family(dest_addr) ==
104 ADDRESS_SORTING_AF_INET &&
105 !ipv4_supported_) ||
106 (address_sorting_abstract_get_family(dest_addr) ==
107 ADDRESS_SORTING_AF_INET6 &&
108 !ipv6_supported_)) {
109 return false;
110 }
111 grpc_resolved_address dest_addr_as_resolved_addr;
112 memcpy(&dest_addr_as_resolved_addr.addr, dest_addr, dest_addr->len);
113 dest_addr_as_resolved_addr.len = dest_addr->len;
114 std::string ip_addr_str =
115 grpc_sockaddr_to_string(&dest_addr_as_resolved_addr,
116 false /* normalize */)
117 .value();
118 auto it = dest_addr_to_src_addr_.find(ip_addr_str);
119 if (it == dest_addr_to_src_addr_.end()) {
120 gpr_log(GPR_DEBUG, "can't find |%s| in dest to src map",
121 ip_addr_str.c_str());
122 return false;
123 }
124 grpc_resolved_address source_addr_as_resolved_addr =
125 TestAddressToGrpcResolvedAddress(it->second);
126 memcpy(source_addr->addr, &source_addr_as_resolved_addr.addr,
127 source_addr_as_resolved_addr.len);
128 source_addr->len = source_addr_as_resolved_addr.len;
129 return true;
130 }
131
132 private:
133 // user provided test config
134 bool ipv4_supported_;
135 bool ipv6_supported_;
136 std::map<std::string, TestAddress> dest_addr_to_src_addr_;
137 };
138
139 bool mock_source_addr_factory_wrapper_get_source_addr(
140 address_sorting_source_addr_factory* factory,
141 const address_sorting_address* dest_addr,
142 address_sorting_address* source_addr) {
143 MockSourceAddrFactory* mock =
144 reinterpret_cast<MockSourceAddrFactory*>(factory);
145 return mock->GetSourceAddr(dest_addr, source_addr);
146 }
147
148 void mock_source_addr_factory_wrapper_destroy(
149 address_sorting_source_addr_factory* factory) {
150 MockSourceAddrFactory* mock =
151 reinterpret_cast<MockSourceAddrFactory*>(factory);
152 delete mock;
153 }
154
155 const address_sorting_source_addr_factory_vtable kMockSourceAddrFactoryVtable =
156 {
157 mock_source_addr_factory_wrapper_get_source_addr,
158 mock_source_addr_factory_wrapper_destroy,
159 };
160
161 void OverrideAddressSortingSourceAddrFactory(
162 bool ipv4_supported, bool ipv6_supported,
163 const std::map<std::string, TestAddress>& dest_addr_to_src_addr) {
164 address_sorting_source_addr_factory* factory = new MockSourceAddrFactory(
165 ipv4_supported, ipv6_supported, dest_addr_to_src_addr);
166 factory->vtable = &kMockSourceAddrFactoryVtable;
167 address_sorting_override_source_addr_factory_for_testing(factory);
168 }
169
170 grpc_core::ServerAddressList BuildLbAddrInputs(
171 const std::vector<TestAddress>& test_addrs) {
172 grpc_core::ServerAddressList addresses;
173 for (const auto& addr : test_addrs) {
174 addresses.emplace_back(TestAddressToGrpcResolvedAddress(addr),
175 grpc_core::ChannelArgs());
176 }
177 return addresses;
178 }
179
180 void VerifyLbAddrOutputs(const grpc_core::ServerAddressList& addresses,
181 std::vector<std::string> expected_addrs) {
182 EXPECT_EQ(addresses.size(), expected_addrs.size());
183 for (size_t i = 0; i < addresses.size(); ++i) {
184 std::string ip_addr_str =
185 grpc_sockaddr_to_string(&addresses[i].address(), false /* normalize */)
186 .value();
187 EXPECT_EQ(expected_addrs[i], ip_addr_str);
188 }
189 }
190
191 /* We need to run each test case inside of its own
192 * isolated grpc_init/grpc_shutdown pair, so that
193 * the "address sorting source addr factory" can be
194 * restored to its default for each test case. */
195 class AddressSortingTest : public ::testing::Test {
196 protected:
197 void SetUp() override { grpc_init(); }
198 void TearDown() override { grpc_shutdown(); }
199 };
200
201 /* Tests for rule 1 */
202 TEST_F(AddressSortingTest, TestDepriotizesUnreachableAddresses) {
203 bool ipv4_supported = true;
204 bool ipv6_supported = true;
205 OverrideAddressSortingSourceAddrFactory(
206 ipv4_supported, ipv6_supported,
207 {
208 {"1.2.3.4:443", {"4.3.2.1:443", AF_INET}},
209 });
210 auto lb_addrs = BuildLbAddrInputs({
211 {"1.2.3.4:443", AF_INET},
212 {"5.6.7.8:443", AF_INET},
213 });
214 grpc_cares_wrapper_address_sorting_sort(nullptr, &lb_addrs);
215 VerifyLbAddrOutputs(lb_addrs, {
216 "1.2.3.4:443",
217 "5.6.7.8:443",
218 });
219 }
220
221 TEST_F(AddressSortingTest, TestDepriotizesUnsupportedDomainIpv6) {
222 bool ipv4_supported = true;
223 bool ipv6_supported = false;
224 OverrideAddressSortingSourceAddrFactory(
225 ipv4_supported, ipv6_supported,
226 {
227 {"1.2.3.4:443", {"4.3.2.1:0", AF_INET}},
228 });
229 auto lb_addrs = BuildLbAddrInputs({
230 {"[2607:f8b0:400a:801::1002]:443", AF_INET6},
231 {"1.2.3.4:443", AF_INET},
232 });
233 grpc_cares_wrapper_address_sorting_sort(nullptr, &lb_addrs);
234 VerifyLbAddrOutputs(lb_addrs, {
235 "1.2.3.4:443",
236 "[2607:f8b0:400a:801::1002]:443",
237 });
238 }
239
240 TEST_F(AddressSortingTest, TestDepriotizesUnsupportedDomainIpv4) {
241 bool ipv4_supported = false;
242 bool ipv6_supported = true;
243 OverrideAddressSortingSourceAddrFactory(
244 ipv4_supported, ipv6_supported,
245 {
246 {"1.2.3.4:443", {"4.3.2.1:0", AF_INET}},
247 {"[2607:f8b0:400a:801::1002]:443", {"[fec0::1234]:0", AF_INET6}},
248 });
249 auto lb_addrs = BuildLbAddrInputs({
250 {"[2607:f8b0:400a:801::1002]:443", AF_INET6},
251 {"1.2.3.4:443", AF_INET},
252 });
253 grpc_cares_wrapper_address_sorting_sort(nullptr, &lb_addrs);
254 VerifyLbAddrOutputs(lb_addrs, {
255 "[2607:f8b0:400a:801::1002]:443",
256 "1.2.3.4:443",
257 });
258 }
259
260 /* Tests for rule 2 */
261
262 TEST_F(AddressSortingTest, TestDepriotizesNonMatchingScope) {
263 bool ipv4_supported = true;
264 bool ipv6_supported = true;
265 OverrideAddressSortingSourceAddrFactory(
266 ipv4_supported, ipv6_supported,
267 {
268 {"[2000:f8b0:400a:801::1002]:443",
269 {"[fec0::1000]:0", AF_INET6}}, // global and site-local scope
270 {"[fec0::5000]:443",
271 {"[fec0::5001]:0", AF_INET6}}, // site-local and site-local scope
272 });
273 auto lb_addrs = BuildLbAddrInputs({
274 {"[2000:f8b0:400a:801::1002]:443", AF_INET6},
275 {"[fec0::5000]:443", AF_INET6},
276 });
277 grpc_cares_wrapper_address_sorting_sort(nullptr, &lb_addrs);
278 VerifyLbAddrOutputs(lb_addrs, {
279 "[fec0::5000]:443",
280 "[2000:f8b0:400a:801::1002]:443",
281 });
282 }
283
284 /* Tests for rule 5 */
285
286 TEST_F(AddressSortingTest, TestUsesLabelFromDefaultTable) {
287 bool ipv4_supported = true;
288 bool ipv6_supported = true;
289 OverrideAddressSortingSourceAddrFactory(
290 ipv4_supported, ipv6_supported,
291 {
292 {"[2002::5001]:443", {"[2001::5002]:0", AF_INET6}},
293 {"[2001::5001]:443",
294 {"[2001::5002]:0", AF_INET6}}, // matching labels
295 });
296 auto lb_addrs = BuildLbAddrInputs({
297 {"[2002::5001]:443", AF_INET6},
298 {"[2001::5001]:443", AF_INET6},
299 });
300 grpc_cares_wrapper_address_sorting_sort(nullptr, &lb_addrs);
301 VerifyLbAddrOutputs(lb_addrs, {
302 "[2001::5001]:443",
303 "[2002::5001]:443",
304 });
305 }
306
307 /* Flip the input on the test above to reorder the sort function's
308 * comparator's inputs. */
309 TEST_F(AddressSortingTest, TestUsesLabelFromDefaultTableInputFlipped) {
310 bool ipv4_supported = true;
311 bool ipv6_supported = true;
312 OverrideAddressSortingSourceAddrFactory(
313 ipv4_supported, ipv6_supported,
314 {
315 {"[2002::5001]:443", {"[2001::5002]:0", AF_INET6}},
316 {"[2001::5001]:443",
317 {"[2001::5002]:0", AF_INET6}}, // matching labels
318 });
319 auto lb_addrs = BuildLbAddrInputs({
320 {"[2001::5001]:443", AF_INET6},
321 {"[2002::5001]:443", AF_INET6},
322 });
323 grpc_cares_wrapper_address_sorting_sort(nullptr, &lb_addrs);
324 VerifyLbAddrOutputs(lb_addrs, {
325 "[2001::5001]:443",
326 "[2002::5001]:443",
327 });
328 }
329
330 /* Tests for rule 6 */
331
332 TEST_F(AddressSortingTest,
333 TestUsesDestinationWithHigherPrecedenceWithAnIpv4Address) {
334 bool ipv4_supported = true;
335 bool ipv6_supported = true;
336 OverrideAddressSortingSourceAddrFactory(
337 ipv4_supported, ipv6_supported,
338 {
339 {"[3ffe::5001]:443", {"[3ffe::5002]:0", AF_INET6}},
340 {"1.2.3.4:443", {"5.6.7.8:0", AF_INET}},
341 });
342 auto lb_addrs = BuildLbAddrInputs({
343 {"[3ffe::5001]:443", AF_INET6},
344 {"1.2.3.4:443", AF_INET},
345 });
346 grpc_cares_wrapper_address_sorting_sort(nullptr, &lb_addrs);
347 VerifyLbAddrOutputs(
348 lb_addrs, {
349 // The AF_INET address should be IPv4-mapped by the sort,
350 // and IPv4-mapped
351 // addresses have higher precedence than 3ffe::/16 by spec.
352 "1.2.3.4:443",
353 "[3ffe::5001]:443",
354 });
355 }
356
357 TEST_F(AddressSortingTest,
358 TestUsesDestinationWithHigherPrecedenceWithV4CompatAndLocalhostAddress) {
359 bool ipv4_supported = true;
360 bool ipv6_supported = true;
361 const char* v4_compat_dest = "[::2]:443";
362 const char* v4_compat_src = "[::2]:0";
363 OverrideAddressSortingSourceAddrFactory(
364 ipv4_supported, ipv6_supported,
365 {
366 {"[::1]:443", {"[::1]:0", AF_INET6}},
367 {v4_compat_dest, {v4_compat_src, AF_INET6}},
368 });
369 auto lb_addrs = BuildLbAddrInputs({
370 {v4_compat_dest, AF_INET6},
371 {"[::1]:443", AF_INET6},
372 });
373 grpc_cares_wrapper_address_sorting_sort(nullptr, &lb_addrs);
374 ASSERT_EQ(lb_addrs.size(), 2);
375 EXPECT_EQ(
376 grpc_sockaddr_to_string(&lb_addrs[0].address(), false /* normalize */)
377 .value(),
378 "[::1]:443");
379 // We've observed some inet_ntop implementations have special representations
380 // of IPv4-compatible IPv6 addresses, and others represent them as normal
381 // IPv6 addresses. For the purposes of this test, we don't care which
382 // representation is used.
383 std::vector<std::string> acceptable_addresses = {
384 "[::0.0.0.2]:443",
385 "[::2]:443",
386 };
387 EXPECT_THAT(
388 acceptable_addresses,
389 ::testing::Contains(
390 grpc_sockaddr_to_string(&lb_addrs[1].address(), false /* normalize */)
391 .value()));
392 }
393
394 TEST_F(AddressSortingTest,
395 TestUsesDestinationWithHigherPrecedenceWithCatchAllAndLocalhostAddress) {
396 bool ipv4_supported = true;
397 bool ipv6_supported = true;
398 OverrideAddressSortingSourceAddrFactory(
399 ipv4_supported, ipv6_supported,
400 {
401 // 1234::2 for src and dest to make sure that prefix matching has no
402 // influence on this test.
403 {"[1234::2]:443", {"[1234::2]:0", AF_INET6}},
404 {"[::1]:443", {"[::1]:0", AF_INET6}},
405 });
406 auto lb_addrs = BuildLbAddrInputs({
407 {"[1234::2]:443", AF_INET6},
408 {"[::1]:443", AF_INET6},
409 });
410 grpc_cares_wrapper_address_sorting_sort(nullptr, &lb_addrs);
411 VerifyLbAddrOutputs(
412 lb_addrs,
413 {
414 // ::1 should match the localhost precedence entry and be prioritized
415 "[::1]:443",
416 "[1234::2]:443",
417 });
418 }
419
420 TEST_F(AddressSortingTest,
421 TestUsesDestinationWithHigherPrecedenceWith2000PrefixedAddress) {
422 bool ipv4_supported = true;
423 bool ipv6_supported = true;
424 OverrideAddressSortingSourceAddrFactory(
425 ipv4_supported, ipv6_supported,
426 {
427 {"[2001::1234]:443", {"[2001::5678]:0", AF_INET6}},
428 {"[2000::5001]:443", {"[2000::5002]:0", AF_INET6}},
429 });
430 auto lb_addrs = BuildLbAddrInputs({
431 {"[2001::1234]:443", AF_INET6},
432 {"[2000::5001]:443", AF_INET6},
433 });
434 grpc_cares_wrapper_address_sorting_sort(nullptr, &lb_addrs);
435 VerifyLbAddrOutputs(
436 lb_addrs, {
437 // The 2000::/16 address should match the ::/0 prefix rule
438 "[2000::5001]:443",
439 "[2001::1234]:443",
440 });
441 }
442
443 TEST_F(
444 AddressSortingTest,
445 TestUsesDestinationWithHigherPrecedenceWith2000PrefixedAddressEnsurePrefixMatchHasNoEffect) {
446 bool ipv4_supported = true;
447 bool ipv6_supported = true;
448 OverrideAddressSortingSourceAddrFactory(
449 ipv4_supported, ipv6_supported,
450 {
451 {"[2001::1231]:443", {"[2001::1232]:0", AF_INET6}},
452 {"[2000::5001]:443", {"[2000::5002]:0", AF_INET6}},
453 });
454 auto lb_addrs = BuildLbAddrInputs({
455 {"[2001::1231]:443", AF_INET6},
456 {"[2000::5001]:443", AF_INET6},
457 });
458 grpc_cares_wrapper_address_sorting_sort(nullptr, &lb_addrs);
459 VerifyLbAddrOutputs(lb_addrs, {
460 "[2000::5001]:443",
461 "[2001::1231]:443",
462 });
463 }
464
465 TEST_F(AddressSortingTest,
466 TestUsesDestinationWithHigherPrecedenceWithLinkAndSiteLocalAddresses) {
467 bool ipv4_supported = true;
468 bool ipv6_supported = true;
469 OverrideAddressSortingSourceAddrFactory(
470 ipv4_supported, ipv6_supported,
471 {
472 {"[fec0::1234]:443", {"[fec0::5678]:0", AF_INET6}},
473 {"[fc00::5001]:443", {"[fc00::5002]:0", AF_INET6}},
474 });
475 auto lb_addrs = BuildLbAddrInputs({
476 {"[fec0::1234]:443", AF_INET6},
477 {"[fc00::5001]:443", AF_INET6},
478 });
479 grpc_cares_wrapper_address_sorting_sort(nullptr, &lb_addrs);
480 VerifyLbAddrOutputs(lb_addrs, {
481 "[fc00::5001]:443",
482 "[fec0::1234]:443",
483 });
484 }
485
486 TEST_F(
487 AddressSortingTest,
488 TestUsesDestinationWithHigherPrecedenceWithCatchAllAndAndV4MappedAddresses) {
489 bool ipv4_supported = true;
490 bool ipv6_supported = true;
491 // Use embedded ipv4 addresses with leading 1's instead of zero's to be
492 // compatible with inet_ntop implementations that can display such
493 // addresses with leading zero's as e.g.: "::ffff:0:2", as on windows.
494 OverrideAddressSortingSourceAddrFactory(
495 ipv4_supported, ipv6_supported,
496 {
497 {"[::ffff:1.1.1.2]:443", {"[::ffff:1.1.1.3]:0", AF_INET6}},
498 {"[1234::2]:443", {"[1234::3]:0", AF_INET6}},
499 });
500 auto lb_addrs = BuildLbAddrInputs({
501 {"[::ffff:1.1.1.2]:443", AF_INET6},
502 {"[1234::2]:443", AF_INET6},
503 });
504 grpc_cares_wrapper_address_sorting_sort(nullptr, &lb_addrs);
505 VerifyLbAddrOutputs(lb_addrs, {
506 // ::ffff:0:2 should match the v4-mapped
507 // precedence entry and be deprioritized.
508 "[1234::2]:443",
509 "[::ffff:1.1.1.2]:443",
510 });
511 }
512
513 /* Tests for rule 8 */
514
515 TEST_F(AddressSortingTest, TestPrefersSmallerScope) {
516 bool ipv4_supported = true;
517 bool ipv6_supported = true;
518 OverrideAddressSortingSourceAddrFactory(
519 ipv4_supported, ipv6_supported,
520 {
521 // Both of these destinations have the same precedence in default
522 // policy
523 // table.
524 {"[fec0::1234]:443", {"[fec0::5678]:0", AF_INET6}},
525 {"[3ffe::5001]:443", {"[3ffe::5002]:0", AF_INET6}},
526 });
527 auto lb_addrs = BuildLbAddrInputs({
528 {"[3ffe::5001]:443", AF_INET6},
529 {"[fec0::1234]:443", AF_INET6},
530 });
531 grpc_cares_wrapper_address_sorting_sort(nullptr, &lb_addrs);
532 VerifyLbAddrOutputs(lb_addrs, {
533 "[fec0::1234]:443",
534 "[3ffe::5001]:443",
535 });
536 }
537
538 /* Tests for rule 9 */
539
540 TEST_F(AddressSortingTest, TestPrefersLongestMatchingSrcDstPrefix) {
541 bool ipv4_supported = true;
542 bool ipv6_supported = true;
543 OverrideAddressSortingSourceAddrFactory(
544 ipv4_supported, ipv6_supported,
545 {
546 // Both of these destinations have the same precedence in default
547 // policy
548 // table.
549 {"[3ffe:1234::]:443", {"[3ffe:1235::]:0", AF_INET6}},
550 {"[3ffe:5001::]:443", {"[3ffe:4321::]:0", AF_INET6}},
551 });
552 auto lb_addrs = BuildLbAddrInputs({
553 {"[3ffe:5001::]:443", AF_INET6},
554 {"[3ffe:1234::]:443", AF_INET6},
555 });
556 grpc_cares_wrapper_address_sorting_sort(nullptr, &lb_addrs);
557 VerifyLbAddrOutputs(lb_addrs, {
558 "[3ffe:1234::]:443",
559 "[3ffe:5001::]:443",
560 });
561 }
562
563 TEST_F(AddressSortingTest,
564 TestPrefersLongestMatchingSrcDstPrefixMatchesWholeAddress) {
565 bool ipv4_supported = true;
566 bool ipv6_supported = true;
567 OverrideAddressSortingSourceAddrFactory(
568 ipv4_supported, ipv6_supported,
569 {
570 {"[3ffe::1234]:443", {"[3ffe::1235]:0", AF_INET6}},
571 {"[3ffe::5001]:443", {"[3ffe::4321]:0", AF_INET6}},
572 });
573 auto lb_addrs = BuildLbAddrInputs({
574 {"[3ffe::5001]:443", AF_INET6},
575 {"[3ffe::1234]:443", AF_INET6},
576 });
577 grpc_cares_wrapper_address_sorting_sort(nullptr, &lb_addrs);
578 VerifyLbAddrOutputs(lb_addrs, {
579 "[3ffe::1234]:443",
580 "[3ffe::5001]:443",
581 });
582 }
583
584 TEST_F(AddressSortingTest, TestPrefersLongestPrefixStressInnerBytePrefix) {
585 bool ipv4_supported = true;
586 bool ipv6_supported = true;
587 OverrideAddressSortingSourceAddrFactory(
588 ipv4_supported, ipv6_supported,
589 {
590 {"[3ffe:8000::]:443", {"[3ffe:C000::]:0", AF_INET6}},
591 {"[3ffe:2000::]:443", {"[3ffe:3000::]:0", AF_INET6}},
592 });
593 auto lb_addrs = BuildLbAddrInputs({
594 {"[3ffe:8000::]:443", AF_INET6},
595 {"[3ffe:2000::]:443", AF_INET6},
596 });
597 grpc_cares_wrapper_address_sorting_sort(nullptr, &lb_addrs);
598 VerifyLbAddrOutputs(lb_addrs, {
599 "[3ffe:2000::]:443",
600 "[3ffe:8000::]:443",
601 });
602 }
603
604 TEST_F(AddressSortingTest, TestPrefersLongestPrefixDiffersOnHighestBitOfByte) {
605 bool ipv4_supported = true;
606 bool ipv6_supported = true;
607 OverrideAddressSortingSourceAddrFactory(
608 ipv4_supported, ipv6_supported,
609 {
610 {"[3ffe:6::]:443", {"[3ffe:8::]:0", AF_INET6}},
611 {"[3ffe:c::]:443", {"[3ffe:8::]:0", AF_INET6}},
612 });
613 auto lb_addrs = BuildLbAddrInputs({
614 {"[3ffe:6::]:443", AF_INET6},
615 {"[3ffe:c::]:443", AF_INET6},
616 });
617 grpc_cares_wrapper_address_sorting_sort(nullptr, &lb_addrs);
618 VerifyLbAddrOutputs(lb_addrs, {
619 "[3ffe:c::]:443",
620 "[3ffe:6::]:443",
621 });
622 }
623
624 TEST_F(AddressSortingTest, TestPrefersLongestPrefixDiffersByLastBit) {
625 bool ipv4_supported = true;
626 bool ipv6_supported = true;
627 OverrideAddressSortingSourceAddrFactory(
628 ipv4_supported, ipv6_supported,
629 {
630 {"[3ffe:1111:1111:1111::]:443",
631 {"[3ffe:1111:1111:1111::]:0", AF_INET6}},
632 {"[3ffe:1111:1111:1110::]:443",
633 {"[3ffe:1111:1111:1111::]:0", AF_INET6}},
634 });
635 auto lb_addrs = BuildLbAddrInputs({
636 {"[3ffe:1111:1111:1110::]:443", AF_INET6},
637 {"[3ffe:1111:1111:1111::]:443", AF_INET6},
638 });
639 grpc_cares_wrapper_address_sorting_sort(nullptr, &lb_addrs);
640 VerifyLbAddrOutputs(lb_addrs, {
641 "[3ffe:1111:1111:1111::]:443",
642 "[3ffe:1111:1111:1110::]:443",
643 });
644 }
645
646 /* Tests for rule 10 */
647
648 TEST_F(AddressSortingTest, TestStableSort) {
649 bool ipv4_supported = true;
650 bool ipv6_supported = true;
651 OverrideAddressSortingSourceAddrFactory(
652 ipv4_supported, ipv6_supported,
653 {
654 {"[3ffe::1234]:443", {"[3ffe::1236]:0", AF_INET6}},
655 {"[3ffe::1235]:443", {"[3ffe::1237]:0", AF_INET6}},
656 });
657 auto lb_addrs = BuildLbAddrInputs({
658 {"[3ffe::1234]:443", AF_INET6},
659 {"[3ffe::1235]:443", AF_INET6},
660 });
661 grpc_cares_wrapper_address_sorting_sort(nullptr, &lb_addrs);
662 VerifyLbAddrOutputs(lb_addrs, {
663 "[3ffe::1234]:443",
664 "[3ffe::1235]:443",
665 });
666 }
667
668 TEST_F(AddressSortingTest, TestStableSortFiveElements) {
669 bool ipv4_supported = true;
670 bool ipv6_supported = true;
671 OverrideAddressSortingSourceAddrFactory(
672 ipv4_supported, ipv6_supported,
673 {
674 {"[3ffe::1231]:443", {"[3ffe::1201]:0", AF_INET6}},
675 {"[3ffe::1232]:443", {"[3ffe::1202]:0", AF_INET6}},
676 {"[3ffe::1233]:443", {"[3ffe::1203]:0", AF_INET6}},
677 {"[3ffe::1234]:443", {"[3ffe::1204]:0", AF_INET6}},
678 {"[3ffe::1235]:443", {"[3ffe::1205]:0", AF_INET6}},
679 });
680 auto lb_addrs = BuildLbAddrInputs({
681 {"[3ffe::1231]:443", AF_INET6},
682 {"[3ffe::1232]:443", AF_INET6},
683 {"[3ffe::1233]:443", AF_INET6},
684 {"[3ffe::1234]:443", AF_INET6},
685 {"[3ffe::1235]:443", AF_INET6},
686 });
687 grpc_cares_wrapper_address_sorting_sort(nullptr, &lb_addrs);
688 VerifyLbAddrOutputs(lb_addrs, {
689 "[3ffe::1231]:443",
690 "[3ffe::1232]:443",
691 "[3ffe::1233]:443",
692 "[3ffe::1234]:443",
693 "[3ffe::1235]:443",
694 });
695 }
696
697 TEST_F(AddressSortingTest, TestStableSortNoSrcAddrsExist) {
698 bool ipv4_supported = true;
699 bool ipv6_supported = true;
700 OverrideAddressSortingSourceAddrFactory(ipv4_supported, ipv6_supported, {});
701 auto lb_addrs = BuildLbAddrInputs({
702 {"[3ffe::1231]:443", AF_INET6},
703 {"[3ffe::1232]:443", AF_INET6},
704 {"[3ffe::1233]:443", AF_INET6},
705 {"[3ffe::1234]:443", AF_INET6},
706 {"[3ffe::1235]:443", AF_INET6},
707 });
708 grpc_cares_wrapper_address_sorting_sort(nullptr, &lb_addrs);
709 VerifyLbAddrOutputs(lb_addrs, {
710 "[3ffe::1231]:443",
711 "[3ffe::1232]:443",
712 "[3ffe::1233]:443",
713 "[3ffe::1234]:443",
714 "[3ffe::1235]:443",
715 });
716 }
717
718 TEST_F(AddressSortingTest, TestStableSortNoSrcAddrsExistWithIpv4) {
719 bool ipv4_supported = true;
720 bool ipv6_supported = true;
721 OverrideAddressSortingSourceAddrFactory(ipv4_supported, ipv6_supported, {});
722 auto lb_addrs = BuildLbAddrInputs({
723 {"[::ffff:5.6.7.8]:443", AF_INET6},
724 {"1.2.3.4:443", AF_INET},
725 });
726 grpc_cares_wrapper_address_sorting_sort(nullptr, &lb_addrs);
727 VerifyLbAddrOutputs(lb_addrs, {
728 "[::ffff:5.6.7.8]:443",
729 "1.2.3.4:443",
730 });
731 }
732
733 TEST_F(AddressSortingTest, TestStableSortV4CompatAndSiteLocalAddresses) {
734 bool ipv4_supported = true;
735 bool ipv6_supported = true;
736 const char* v4_compat_dest = "[::2]:443";
737 const char* v4_compat_src = "[::3]:0";
738 OverrideAddressSortingSourceAddrFactory(
739 ipv4_supported, ipv6_supported,
740 {
741 {"[fec0::2000]:443", {"[fec0::2001]:0", AF_INET6}},
742 {v4_compat_dest, {v4_compat_src, AF_INET6}},
743 });
744 auto lb_addrs = BuildLbAddrInputs({
745 {"[fec0::2000]:443", AF_INET6},
746 {v4_compat_dest, AF_INET6},
747 });
748 grpc_cares_wrapper_address_sorting_sort(nullptr, &lb_addrs);
749 ASSERT_EQ(lb_addrs.size(), 2);
750 // The sort should be stable since
751 // v4-compatible has same precedence as site-local.
752 EXPECT_EQ(
753 grpc_sockaddr_to_string(&lb_addrs[0].address(), false /* normalize */)
754 .value(),
755 "[fec0::2000]:443");
756 // We've observed some inet_ntop implementations have special representations
757 // of IPv4-compatible IPv6 addresses, and others represent them as normal
758 // IPv6 addresses. For the purposes of this test, we don't care which
759 // representation is used.
760 std::vector<std::string> acceptable_addresses = {
761 "[::0.0.0.2]:443",
762 "[::2]:443",
763 };
764 EXPECT_THAT(
765 acceptable_addresses,
766 ::testing::Contains(
767 grpc_sockaddr_to_string(&lb_addrs[1].address(), false /* normalize */)
768 .value()));
769 }
770
771 /* TestPrefersIpv6Loopback tests the actual "address probing" code
772 * for the current platform, without any mocks.
773 * This test relies on the assumption that the ipv6 loopback address is
774 * available in the hosts/containers that grpc C/C++ tests run on
775 * (whether ipv4 loopback is available or not, an available ipv6
776 * loopback should be preferred). */
777 TEST_F(AddressSortingTest, TestPrefersIpv6Loopback) {
778 auto lb_addrs = BuildLbAddrInputs({
779 {"[::1]:443", AF_INET6},
780 {"127.0.0.1:443", AF_INET},
781 });
782 grpc_cares_wrapper_address_sorting_sort(nullptr, &lb_addrs);
783 VerifyLbAddrOutputs(lb_addrs, {
784 "[::1]:443",
785 "127.0.0.1:443",
786 });
787 }
788
789 /* Flip the order of the inputs above and expect the same output order
790 * (try to rule out influence of arbitrary qsort ordering) */
791 TEST_F(AddressSortingTest, TestPrefersIpv6LoopbackInputsFlipped) {
792 auto lb_addrs = BuildLbAddrInputs({
793 {"127.0.0.1:443", AF_INET},
794 {"[::1]:443", AF_INET6},
795 });
796 grpc_cares_wrapper_address_sorting_sort(nullptr, &lb_addrs);
797 VerifyLbAddrOutputs(lb_addrs, {
798 "[::1]:443",
799 "127.0.0.1:443",
800 });
801 }
802
803 /* Try to rule out false positives in the above two tests in which
804 * the sorter might think that neither ipv6 or ipv4 loopback is
805 * available, but ipv6 loopback is still preferred only due
806 * to precedence table lookups. */
807 TEST_F(AddressSortingTest, TestSorterKnowsIpv6LoopbackIsAvailable) {
808 sockaddr_in6 ipv6_loopback;
809 memset(&ipv6_loopback, 0, sizeof(ipv6_loopback));
810 ipv6_loopback.sin6_family = AF_INET6;
811 (reinterpret_cast<char*>(&ipv6_loopback.sin6_addr))[15] = 1;
812 ipv6_loopback.sin6_port = htons(443);
813 // Set up the source and destination parameters of
814 // address_sorting_get_source_addr
815 address_sorting_address sort_input_dest;
816 memcpy(&sort_input_dest.addr, &ipv6_loopback, sizeof(ipv6_loopback));
817 sort_input_dest.len = sizeof(ipv6_loopback);
818 address_sorting_address source_for_sort_input_dest;
819 memset(&source_for_sort_input_dest, 0, sizeof(source_for_sort_input_dest));
820 // address_sorting_get_source_addr returns true if a source address was found
821 // for the destination address, otherwise false.
822 EXPECT_TRUE(address_sorting_get_source_addr_for_testing(
823 &sort_input_dest, &source_for_sort_input_dest));
824 // Now also check that the source address was filled in correctly.
825 EXPECT_GT(source_for_sort_input_dest.len, 0u);
826 sockaddr_in6* source_addr_output =
827 reinterpret_cast<sockaddr_in6*>(source_for_sort_input_dest.addr);
828 EXPECT_EQ(source_addr_output->sin6_family, AF_INET6);
829 char* buf = static_cast<char*>(gpr_zalloc(100));
830 EXPECT_NE(inet_ntop(AF_INET6, &source_addr_output->sin6_addr, buf, 100),
831 nullptr)
832 << "inet_ntop failed. Errno: " + std::to_string(errno);
833 std::string source_addr_str(buf);
834 gpr_free(buf);
835 // This test
836 // assumes that the source address for any loopback destination is also the
837 // loopback address.
838 EXPECT_EQ(source_addr_str, "::1");
839 }
840
841 } // namespace
842
843 int main(int argc, char** argv) {
844 grpc_core::UniquePtr<char> resolver =
845 GPR_GLOBAL_CONFIG_GET(grpc_dns_resolver);
846 if (strlen(resolver.get()) == 0) {
847 GPR_GLOBAL_CONFIG_SET(grpc_dns_resolver, "ares");
848 } else if (strcmp("ares", resolver.get()) != 0) {
849 gpr_log(GPR_INFO, "GRPC_DNS_RESOLVER != ares: %s.", resolver.get());
850 }
851 grpc::testing::TestEnvironment env(&argc, argv);
852 ::testing::InitGoogleTest(&argc, argv);
853 auto result = RUN_ALL_TESTS();
854 // Test sequential and nested inits and shutdowns.
855 grpc_init();
856 grpc_init();
857 grpc_shutdown();
858 grpc_shutdown();
859 grpc_init();
860 grpc_shutdown();
861 return result;
862 }