aboutsummaryrefslogtreecommitdiffhomepage
path: root/test/cpp/naming/address_sorting_test.cc
diff options
context:
space:
mode:
Diffstat (limited to 'test/cpp/naming/address_sorting_test.cc')
-rw-r--r--test/cpp/naming/address_sorting_test.cc160
1 files changed, 38 insertions, 122 deletions
diff --git a/test/cpp/naming/address_sorting_test.cc b/test/cpp/naming/address_sorting_test.cc
index 04c300876c..a92e9e3b3e 100644
--- a/test/cpp/naming/address_sorting_test.cc
+++ b/test/cpp/naming/address_sorting_test.cc
@@ -24,8 +24,10 @@
#include <grpc/support/time.h>
#include <string.h>
+#include <arpa/inet.h>
#include <gflags/gflags.h>
#include <gmock/gmock.h>
+#include <sys/socket.h>
#include <sys/types.h>
#include <vector>
@@ -49,11 +51,6 @@
#include "test/core/util/port.h"
#include "test/core/util/test_config.h"
-#ifndef GPR_WINDOWS
-#include <arpa/inet.h>
-#include <sys/socket.h>
-#endif
-
namespace {
struct TestAddress {
@@ -193,18 +190,10 @@ void VerifyLbAddrOutputs(grpc_lb_addresses* lb_addrs,
grpc_lb_addresses_destroy(lb_addrs);
}
-/* We need to run each test case inside of its own
- * isolated grpc_init/grpc_shutdown pair, so that
- * the "address sorting source addr factory" can be
- * restored to its default for each test case. */
-class AddressSortingTest : public ::testing::Test {
- protected:
- void SetUp() override { grpc_init(); }
- void TearDown() override { grpc_shutdown(); }
-};
+} // namespace
/* Tests for rule 1 */
-TEST_F(AddressSortingTest, TestDepriotizesUnreachableAddresses) {
+TEST(AddressSortingTest, TestDepriotizesUnreachableAddresses) {
bool ipv4_supported = true;
bool ipv6_supported = true;
OverrideAddressSortingSourceAddrFactory(
@@ -223,7 +212,7 @@ TEST_F(AddressSortingTest, TestDepriotizesUnreachableAddresses) {
});
}
-TEST_F(AddressSortingTest, TestDepriotizesUnsupportedDomainIpv6) {
+TEST(AddressSortingTest, TestDepriotizesUnsupportedDomainIpv6) {
bool ipv4_supported = true;
bool ipv6_supported = false;
OverrideAddressSortingSourceAddrFactory(
@@ -242,7 +231,7 @@ TEST_F(AddressSortingTest, TestDepriotizesUnsupportedDomainIpv6) {
});
}
-TEST_F(AddressSortingTest, TestDepriotizesUnsupportedDomainIpv4) {
+TEST(AddressSortingTest, TestDepriotizesUnsupportedDomainIpv4) {
bool ipv4_supported = false;
bool ipv6_supported = true;
OverrideAddressSortingSourceAddrFactory(
@@ -264,7 +253,7 @@ TEST_F(AddressSortingTest, TestDepriotizesUnsupportedDomainIpv4) {
/* Tests for rule 2 */
-TEST_F(AddressSortingTest, TestDepriotizesNonMatchingScope) {
+TEST(AddressSortingTest, TestDepriotizesNonMatchingScope) {
bool ipv4_supported = true;
bool ipv6_supported = true;
OverrideAddressSortingSourceAddrFactory(
@@ -288,7 +277,7 @@ TEST_F(AddressSortingTest, TestDepriotizesNonMatchingScope) {
/* Tests for rule 5 */
-TEST_F(AddressSortingTest, TestUsesLabelFromDefaultTable) {
+TEST(AddressSortingTest, TestUsesLabelFromDefaultTable) {
bool ipv4_supported = true;
bool ipv6_supported = true;
OverrideAddressSortingSourceAddrFactory(
@@ -311,7 +300,7 @@ TEST_F(AddressSortingTest, TestUsesLabelFromDefaultTable) {
/* Flip the input on the test above to reorder the sort function's
* comparator's inputs. */
-TEST_F(AddressSortingTest, TestUsesLabelFromDefaultTableInputFlipped) {
+TEST(AddressSortingTest, TestUsesLabelFromDefaultTableInputFlipped) {
bool ipv4_supported = true;
bool ipv6_supported = true;
OverrideAddressSortingSourceAddrFactory(
@@ -334,8 +323,8 @@ TEST_F(AddressSortingTest, TestUsesLabelFromDefaultTableInputFlipped) {
/* Tests for rule 6 */
-TEST_F(AddressSortingTest,
- TestUsesDestinationWithHigherPrecedenceWithAnIpv4Address) {
+TEST(AddressSortingTest,
+ TestUsesDestinationWithHigherPrecedenceWithAnIpv4Address) {
bool ipv4_supported = true;
bool ipv6_supported = true;
OverrideAddressSortingSourceAddrFactory(
@@ -359,8 +348,8 @@ TEST_F(AddressSortingTest,
});
}
-TEST_F(AddressSortingTest,
- TestUsesDestinationWithHigherPrecedenceWithV4CompatAndLocalhostAddress) {
+TEST(AddressSortingTest,
+ TestUsesDestinationWithHigherPrecedenceWithV4CompatAndLocalhostAddress) {
bool ipv4_supported = true;
bool ipv6_supported = true;
// Handle unique observed behavior of inet_ntop(v4-compatible-address) on OS X.
@@ -388,8 +377,8 @@ TEST_F(AddressSortingTest,
});
}
-TEST_F(AddressSortingTest,
- TestUsesDestinationWithHigherPrecedenceWithCatchAllAndLocalhostAddress) {
+TEST(AddressSortingTest,
+ TestUsesDestinationWithHigherPrecedenceWithCatchAllAndLocalhostAddress) {
bool ipv4_supported = true;
bool ipv6_supported = true;
OverrideAddressSortingSourceAddrFactory(
@@ -414,8 +403,8 @@ TEST_F(AddressSortingTest,
});
}
-TEST_F(AddressSortingTest,
- TestUsesDestinationWithHigherPrecedenceWith2000PrefixedAddress) {
+TEST(AddressSortingTest,
+ TestUsesDestinationWithHigherPrecedenceWith2000PrefixedAddress) {
bool ipv4_supported = true;
bool ipv6_supported = true;
OverrideAddressSortingSourceAddrFactory(
@@ -437,7 +426,7 @@ TEST_F(AddressSortingTest,
});
}
-TEST_F(
+TEST(
AddressSortingTest,
TestUsesDestinationWithHigherPrecedenceWith2000PrefixedAddressEnsurePrefixMatchHasNoEffect) {
bool ipv4_supported = true;
@@ -459,8 +448,8 @@ TEST_F(
});
}
-TEST_F(AddressSortingTest,
- TestUsesDestinationWithHigherPrecedenceWithLinkAndSiteLocalAddresses) {
+TEST(AddressSortingTest,
+ TestUsesDestinationWithHigherPrecedenceWithLinkAndSiteLocalAddresses) {
bool ipv4_supported = true;
bool ipv6_supported = true;
OverrideAddressSortingSourceAddrFactory(
@@ -480,22 +469,19 @@ TEST_F(AddressSortingTest,
});
}
-TEST_F(
+TEST(
AddressSortingTest,
TestUsesDestinationWithHigherPrecedenceWithCatchAllAndAndV4MappedAddresses) {
bool ipv4_supported = true;
bool ipv6_supported = true;
- // Use embedded ipv4 addresses with leading 1's instead of zero's to be
- // compatible with inet_ntop implementations that can display such
- // addresses with leading zero's as e.g.: "::ffff:0:2", as on windows.
OverrideAddressSortingSourceAddrFactory(
ipv4_supported, ipv6_supported,
{
- {"[::ffff:1.1.1.2]:443", {"[::ffff:1.1.1.3]:0", AF_INET6}},
+ {"[::ffff:0.0.0.2]:443", {"[::ffff:0.0.0.3]:0", AF_INET6}},
{"[1234::2]:443", {"[1234::3]:0", AF_INET6}},
});
grpc_lb_addresses* lb_addrs = BuildLbAddrInputs({
- {"[::ffff:1.1.1.2]:443", AF_INET6},
+ {"[::ffff:0.0.0.2]:443", AF_INET6},
{"[1234::2]:443", AF_INET6},
});
grpc_cares_wrapper_test_only_address_sorting_sort(lb_addrs);
@@ -503,13 +489,13 @@ TEST_F(
// ::ffff:0:2 should match the v4-mapped
// precedence entry and be deprioritized.
"[1234::2]:443",
- "[::ffff:1.1.1.2]:443",
+ "[::ffff:0.0.0.2]:443",
});
}
/* Tests for rule 8 */
-TEST_F(AddressSortingTest, TestPrefersSmallerScope) {
+TEST(AddressSortingTest, TestPrefersSmallerScope) {
bool ipv4_supported = true;
bool ipv6_supported = true;
OverrideAddressSortingSourceAddrFactory(
@@ -534,7 +520,7 @@ TEST_F(AddressSortingTest, TestPrefersSmallerScope) {
/* Tests for rule 9 */
-TEST_F(AddressSortingTest, TestPrefersLongestMatchingSrcDstPrefix) {
+TEST(AddressSortingTest, TestPrefersLongestMatchingSrcDstPrefix) {
bool ipv4_supported = true;
bool ipv6_supported = true;
OverrideAddressSortingSourceAddrFactory(
@@ -557,8 +543,8 @@ TEST_F(AddressSortingTest, TestPrefersLongestMatchingSrcDstPrefix) {
});
}
-TEST_F(AddressSortingTest,
- TestPrefersLongestMatchingSrcDstPrefixMatchesWholeAddress) {
+TEST(AddressSortingTest,
+ TestPrefersLongestMatchingSrcDstPrefixMatchesWholeAddress) {
bool ipv4_supported = true;
bool ipv6_supported = true;
OverrideAddressSortingSourceAddrFactory(
@@ -578,7 +564,7 @@ TEST_F(AddressSortingTest,
});
}
-TEST_F(AddressSortingTest, TestPrefersLongestPrefixStressInnerBytePrefix) {
+TEST(AddressSortingTest, TestPrefersLongestPrefixStressInnerBytePrefix) {
bool ipv4_supported = true;
bool ipv6_supported = true;
OverrideAddressSortingSourceAddrFactory(
@@ -598,7 +584,7 @@ TEST_F(AddressSortingTest, TestPrefersLongestPrefixStressInnerBytePrefix) {
});
}
-TEST_F(AddressSortingTest, TestPrefersLongestPrefixDiffersOnHighestBitOfByte) {
+TEST(AddressSortingTest, TestPrefersLongestPrefixDiffersOnHighestBitOfByte) {
bool ipv4_supported = true;
bool ipv6_supported = true;
OverrideAddressSortingSourceAddrFactory(
@@ -618,7 +604,7 @@ TEST_F(AddressSortingTest, TestPrefersLongestPrefixDiffersOnHighestBitOfByte) {
});
}
-TEST_F(AddressSortingTest, TestPrefersLongestPrefixDiffersByLastBit) {
+TEST(AddressSortingTest, TestPrefersLongestPrefixDiffersByLastBit) {
bool ipv4_supported = true;
bool ipv6_supported = true;
OverrideAddressSortingSourceAddrFactory(
@@ -642,7 +628,7 @@ TEST_F(AddressSortingTest, TestPrefersLongestPrefixDiffersByLastBit) {
/* Tests for rule 10 */
-TEST_F(AddressSortingTest, TestStableSort) {
+TEST(AddressSortingTest, TestStableSort) {
bool ipv4_supported = true;
bool ipv6_supported = true;
OverrideAddressSortingSourceAddrFactory(
@@ -662,7 +648,7 @@ TEST_F(AddressSortingTest, TestStableSort) {
});
}
-TEST_F(AddressSortingTest, TestStableSortFiveElements) {
+TEST(AddressSortingTest, TestStableSortFiveElements) {
bool ipv4_supported = true;
bool ipv6_supported = true;
OverrideAddressSortingSourceAddrFactory(
@@ -691,7 +677,7 @@ TEST_F(AddressSortingTest, TestStableSortFiveElements) {
});
}
-TEST_F(AddressSortingTest, TestStableSortNoSrcAddrsExist) {
+TEST(AddressSortingTest, TestStableSortNoSrcAddrsExist) {
bool ipv4_supported = true;
bool ipv6_supported = true;
OverrideAddressSortingSourceAddrFactory(ipv4_supported, ipv6_supported, {});
@@ -712,7 +698,7 @@ TEST_F(AddressSortingTest, TestStableSortNoSrcAddrsExist) {
});
}
-TEST_F(AddressSortingTest, TestStableSortNoSrcAddrsExistWithIpv4) {
+TEST(AddressSortingTest, TestStableSortNoSrcAddrsExistWithIpv4) {
bool ipv4_supported = true;
bool ipv6_supported = true;
OverrideAddressSortingSourceAddrFactory(ipv4_supported, ipv6_supported, {});
@@ -727,7 +713,7 @@ TEST_F(AddressSortingTest, TestStableSortNoSrcAddrsExistWithIpv4) {
});
}
-TEST_F(AddressSortingTest, TestStableSortV4CompatAndSiteLocalAddresses) {
+TEST(AddressSortingTest, TestStableSortV4CompatAndSiteLocalAddresses) {
bool ipv4_supported = true;
bool ipv6_supported = true;
// Handle unique observed behavior of inet_ntop(v4-compatible-address) on OS X.
@@ -758,78 +744,6 @@ TEST_F(AddressSortingTest, TestStableSortV4CompatAndSiteLocalAddresses) {
});
}
-/* TestPrefersIpv6Loopback tests the actual "address probing" code
- * for the current platform, without any mocks.
- * This test relies on the assumption that the ipv6 loopback address is
- * available in the hosts/containers that grpc C/C++ tests run on
- * (whether ipv4 loopback is available or not, an available ipv6
- * loopback should be preferred). */
-TEST_F(AddressSortingTest, TestPrefersIpv6Loopback) {
- grpc_lb_addresses* lb_addrs = BuildLbAddrInputs({
- {"[::1]:443", AF_INET6},
- {"127.0.0.1:443", AF_INET},
- });
- grpc_cares_wrapper_test_only_address_sorting_sort(lb_addrs);
- VerifyLbAddrOutputs(lb_addrs, {
- "[::1]:443",
- "127.0.0.1:443",
- });
-}
-
-/* Flip the order of the inputs above and expect the same output order
- * (try to rule out influence of arbitrary qsort ordering) */
-TEST_F(AddressSortingTest, TestPrefersIpv6LoopbackInputsFlipped) {
- grpc_lb_addresses* lb_addrs = BuildLbAddrInputs({
- {"127.0.0.1:443", AF_INET},
- {"[::1]:443", AF_INET6},
- });
- grpc_cares_wrapper_test_only_address_sorting_sort(lb_addrs);
- VerifyLbAddrOutputs(lb_addrs, {
- "[::1]:443",
- "127.0.0.1:443",
- });
-}
-
-/* Try to rule out false positives in the above two tests in which
- * the sorter might think that neither ipv6 or ipv4 loopback is
- * available, but ipv6 loopback is still preferred only due
- * to precedance table lookups. */
-TEST_F(AddressSortingTest, TestSorterKnowsIpv6LoopbackIsAvailable) {
- sockaddr_in6 ipv6_loopback;
- memset(&ipv6_loopback, 0, sizeof(ipv6_loopback));
- ipv6_loopback.sin6_family = AF_INET6;
- ((char*)&ipv6_loopback.sin6_addr)[15] = 1;
- ipv6_loopback.sin6_port = htons(443);
- // Set up the source and destination parameters of
- // address_sorting_get_source_addr
- address_sorting_address sort_input_dest;
- memcpy(&sort_input_dest.addr, &ipv6_loopback, sizeof(ipv6_loopback));
- sort_input_dest.len = sizeof(ipv6_loopback);
- address_sorting_address source_for_sort_input_dest;
- memset(&source_for_sort_input_dest, 0, sizeof(source_for_sort_input_dest));
- // address_sorting_get_source_addr returns true if a source address was found
- // for the destination address, otherwise false.
- EXPECT_TRUE(address_sorting_get_source_addr_for_testing(
- &sort_input_dest, &source_for_sort_input_dest));
- // Now also check that the source address was filled in correctly.
- EXPECT_GT(source_for_sort_input_dest.len, 0u);
- sockaddr_in6* source_addr_output =
- (sockaddr_in6*)source_for_sort_input_dest.addr;
- EXPECT_EQ(source_addr_output->sin6_family, AF_INET6);
- char* buf = static_cast<char*>(gpr_zalloc(100));
- EXPECT_NE(inet_ntop(AF_INET6, &source_addr_output->sin6_addr, buf, 100),
- nullptr)
- << "inet_ntop failed. Errno: " + std::to_string(errno);
- std::string source_addr_str(buf);
- gpr_free(buf);
- // This test
- // assumes that the source address for any loopback destination is also the
- // loopback address.
- EXPECT_EQ(source_addr_str, "::1");
-}
-
-} // namespace
-
int main(int argc, char** argv) {
char* resolver = gpr_getenv("GRPC_DNS_RESOLVER");
if (resolver == nullptr || strlen(resolver) == 0) {
@@ -840,7 +754,9 @@ int main(int argc, char** argv) {
gpr_free(resolver);
grpc_test_init(argc, argv);
::testing::InitGoogleTest(&argc, argv);
+ grpc_init();
auto result = RUN_ALL_TESTS();
+ grpc_shutdown();
// Test sequential and nested inits and shutdowns.
grpc_init();
grpc_init();