ipv6_address_ut.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. #include <library/cpp/testing/unittest/registar.h>
  2. #include <library/cpp/ipv6_address/ipv6_address.h>
  3. #include <unordered_set>
  4. class TIpv6AddressTest: public TTestBase {
  5. UNIT_TEST_SUITE(TIpv6AddressTest);
  6. UNIT_TEST(ParseHostAndMayBePortFromString_data);
  7. UNIT_TEST(CheckAddressValidity)
  8. UNIT_TEST(CheckToStringConversion)
  9. UNIT_TEST_SUITE_END();
  10. private:
  11. void ParseHostAndMayBePortFromString_data();
  12. void CheckAddressValidity();
  13. void HashCompileTest();
  14. void CheckToStringConversion();
  15. };
  16. UNIT_TEST_SUITE_REGISTRATION(TIpv6AddressTest);
  17. using TResult = std::tuple<THostAddressAndPort, TString, TIpPort>;
  18. TResult IpRes(TString Ip, TIpPort Port) {
  19. bool Ok;
  20. THostAddressAndPort HostAddressAndPort;
  21. HostAddressAndPort.Ip = TIpv6Address::FromString(Ip, Ok);
  22. Y_ABORT_UNLESS(Ok);
  23. HostAddressAndPort.Port = Port;
  24. return TResult(HostAddressAndPort, {}, {});
  25. }
  26. TResult HostRes(TString HostName, TIpPort Port) {
  27. return TResult({}, HostName, Port);
  28. }
  29. void ParseHostAndMayBePortFromString(TString RawStr,
  30. TIpPort DefaultPort,
  31. const TResult ExpectedResult,
  32. const bool ExpectedOk) {
  33. bool Ok = false;
  34. const TResult ActualResult = ParseHostAndMayBePortFromString(RawStr, DefaultPort, Ok);
  35. UNIT_ASSERT(Ok == ExpectedOk);
  36. if (Ok == false)
  37. return;
  38. UNIT_ASSERT(ActualResult == ExpectedResult);
  39. }
  40. void CheckIpDefPortAgainstIpPortDefPort_v4OrHost(TString Ip,
  41. TIpPort Port,
  42. const TResult ExpectedResult,
  43. const bool ExpectedOk) {
  44. ParseHostAndMayBePortFromString(Ip, Port, ExpectedResult, ExpectedOk);
  45. TString New = Ip + ":" + ToString(Port);
  46. ParseHostAndMayBePortFromString(New, Port + 12, ExpectedResult, ExpectedOk);
  47. }
  48. void CheckIpDefPortAgainstIpPortDefPort_v6(TString Ip, TIpPort Port, const TResult ExpectedResult, const bool ExpectedOk) {
  49. ParseHostAndMayBePortFromString(Ip, Port, ExpectedResult, ExpectedOk);
  50. TString New = "[" + Ip + "]" + ":" + ToString(Port);
  51. ParseHostAndMayBePortFromString(New, Port + 12, ExpectedResult, ExpectedOk);
  52. }
  53. void CheckIpDefPortAgainstIpPortDefPort(TString Ip, TIpPort Port, const TResult ExpectedResult, const bool ExpectedOk) {
  54. if (Ip.find(':') == TString::npos) {
  55. CheckIpDefPortAgainstIpPortDefPort_v4OrHost(Ip, Port, ExpectedResult, ExpectedOk);
  56. } else {
  57. CheckIpDefPortAgainstIpPortDefPort_v6(Ip, Port, ExpectedResult, ExpectedOk);
  58. }
  59. }
  60. void TIpv6AddressTest::ParseHostAndMayBePortFromString_data() {
  61. CheckIpDefPortAgainstIpPortDefPort("1.2.3.4", 123, IpRes("1.2.3.4", 123), true);
  62. ParseHostAndMayBePortFromString("[1.2.3.4]", 123, {}, false);
  63. ParseHostAndMayBePortFromString("[2001::7348]", 123, IpRes("2001::7348", 123), true);
  64. CheckIpDefPortAgainstIpPortDefPort("2001::7348", 123, IpRes("2001::7348", 123), true);
  65. CheckIpDefPortAgainstIpPortDefPort("ya.ru", 123, HostRes("ya.ru", 123), true);
  66. }
  67. void TIpv6AddressTest::CheckAddressValidity() {
  68. bool Ok;
  69. constexpr TIpv6Address partsV4 {12, 34, 56, 78};
  70. static_assert(partsV4.Type() == TIpv6Address::Ipv4);
  71. constexpr TIpv6Address intV4 {0x0C22384E, TIpv6Address::Ipv4};
  72. static_assert(partsV4 == intV4);
  73. const auto parsedV4 = TIpv6Address::FromString("12.34.56.78", Ok);
  74. UNIT_ASSERT(Ok);
  75. UNIT_ASSERT_EQUAL(parsedV4, partsV4);
  76. constexpr TIpv6Address partsV6 {0xFB, 0x1634, 0x19, 0xABED, 0, 0x8001, 0x1670, 0x742};
  77. static_assert(partsV6.Type() == TIpv6Address::Ipv6);
  78. constexpr TIpv6Address intV6 {{0x00FB16340019ABED, 0x0000800116700742}, TIpv6Address::Ipv6};
  79. static_assert(partsV6 == intV6);
  80. const auto parsedV6 = TIpv6Address::FromString("FB:1634:19:ABED:0:8001:1670:742", Ok);
  81. UNIT_ASSERT(Ok);
  82. UNIT_ASSERT_EQUAL(parsedV6, partsV6);
  83. static_assert(Get127001() == TIpv6Address(0x7F000001, TIpv6Address::Ipv4));
  84. static_assert(Get1() == TIpv6Address(0, 0, 0, 0, 0, 0, 0, 1));
  85. }
  86. void TIpv6AddressTest::CheckToStringConversion() {
  87. {
  88. TString ipPort = "[2aa2::786b]:789";
  89. bool ok;
  90. auto result = ParseHostAndMayBePortFromString(ipPort, 80, ok);
  91. auto hostAddressAndPort = std::get<THostAddressAndPort>(result);
  92. UNIT_ASSERT_EQUAL(hostAddressAndPort.ToString({}), ipPort);
  93. UNIT_ASSERT_EQUAL(hostAddressAndPort.ToString(), ipPort);
  94. }
  95. {
  96. TString ipPort = "[2aa2::786b%25]:789";
  97. bool ok;
  98. auto result = ParseHostAndMayBePortFromString(ipPort, 80, ok);
  99. auto hostAddressAndPort = std::get<THostAddressAndPort>(result);
  100. UNIT_ASSERT_EQUAL(hostAddressAndPort.ToString({.PrintScopeId = true}), ipPort);
  101. }
  102. }
  103. void TIpv6AddressTest::HashCompileTest() {
  104. std::unordered_set<TIpv6Address> test;
  105. Y_UNUSED(test);
  106. }