endpoint_ut.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. #include "endpoint.h"
  2. #include <library/cpp/testing/unittest/registar.h>
  3. #include <util/generic/hash_set.h>
  4. #include <util/generic/strbuf.h>
  5. Y_UNIT_TEST_SUITE(TEndpointTest) {
  6. Y_UNIT_TEST(TestSimple) {
  7. TVector<TNetworkAddress> addrs;
  8. TEndpoint ep0;
  9. UNIT_ASSERT(ep0.IsIpV4());
  10. UNIT_ASSERT_VALUES_EQUAL(0, ep0.Port());
  11. UNIT_ASSERT_VALUES_EQUAL("0.0.0.0", ep0.IpToString());
  12. TEndpoint ep1;
  13. try {
  14. TNetworkAddress na1("25.26.27.28", 24242);
  15. addrs.push_back(na1);
  16. ep1 = TEndpoint(new NAddr::TAddrInfo(&*na1.Begin()));
  17. UNIT_ASSERT(ep1.IsIpV4());
  18. UNIT_ASSERT_VALUES_EQUAL("25.26.27.28", ep1.IpToString());
  19. UNIT_ASSERT_VALUES_EQUAL(24242, ep1.Port());
  20. } catch (const TNetworkResolutionError&) {
  21. TNetworkAddress n("2a02:6b8:0:1420:0::5f6c:f3c2", 11111);
  22. addrs.push_back(n);
  23. ep1 = TEndpoint(new NAddr::TAddrInfo(&*n.Begin()));
  24. }
  25. ep0.SetPort(12345);
  26. TEndpoint ep2(ep0);
  27. ep0.SetPort(0);
  28. UNIT_ASSERT_VALUES_EQUAL(12345, ep2.Port());
  29. TEndpoint ep2_;
  30. ep2_.SetPort(12345);
  31. UNIT_ASSERT(ep2 == ep2_);
  32. TNetworkAddress na3("2a02:6b8:0:1410::5f6c:f3c2", 54321);
  33. TEndpoint ep3(new NAddr::TAddrInfo(&*na3.Begin()));
  34. UNIT_ASSERT(ep3.IsIpV6());
  35. UNIT_ASSERT(ep3.IpToString().StartsWith(TStringBuf("2a02:6b8:0:1410:")));
  36. UNIT_ASSERT(ep3.IpToString().EndsWith(TStringBuf(":5f6c:f3c2")));
  37. UNIT_ASSERT_VALUES_EQUAL(54321, ep3.Port());
  38. TNetworkAddress na4("2a02:6b8:0:1410:0::5f6c:f3c2", 1);
  39. TEndpoint ep4(new NAddr::TAddrInfo(&*na4.Begin()));
  40. TEndpoint ep3_ = ep4;
  41. ep3_.SetPort(54321);
  42. THashSet<TEndpoint> he;
  43. he.insert(ep0);
  44. he.insert(ep1);
  45. he.insert(ep2);
  46. UNIT_ASSERT_VALUES_EQUAL(3u, he.size());
  47. he.insert(ep2_);
  48. UNIT_ASSERT_VALUES_EQUAL(3u, he.size());
  49. he.insert(ep3);
  50. he.insert(ep3_);
  51. UNIT_ASSERT_VALUES_EQUAL(4u, he.size());
  52. he.insert(ep4);
  53. UNIT_ASSERT_VALUES_EQUAL(5u, he.size());
  54. }
  55. Y_UNIT_TEST(TestEqual) {
  56. const TString ip1 = "2a02:6b8:0:1410::5f6c:f3c2";
  57. const TString ip2 = "2a02:6b8:0:1410::5f6c:f3c3";
  58. TNetworkAddress na1(ip1, 24242);
  59. TEndpoint ep1(new NAddr::TAddrInfo(&*na1.Begin()));
  60. TNetworkAddress na2(ip1, 24242);
  61. TEndpoint ep2(new NAddr::TAddrInfo(&*na2.Begin()));
  62. TNetworkAddress na3(ip2, 24242);
  63. TEndpoint ep3(new NAddr::TAddrInfo(&*na3.Begin()));
  64. TNetworkAddress na4(ip2, 24243);
  65. TEndpoint ep4(new NAddr::TAddrInfo(&*na4.Begin()));
  66. UNIT_ASSERT(ep1 == ep2);
  67. UNIT_ASSERT(!(ep1 == ep3));
  68. UNIT_ASSERT(!(ep1 == ep4));
  69. }
  70. Y_UNIT_TEST(TestIsUnixSocket) {
  71. TNetworkAddress na1(TUnixSocketPath("/tmp/unixsocket"));
  72. TEndpoint ep1(new NAddr::TAddrInfo(&*na1.Begin()));
  73. TNetworkAddress na2("2a02:6b8:0:1410::5f6c:f3c2", 24242);
  74. TEndpoint ep2(new NAddr::TAddrInfo(&*na2.Begin()));
  75. UNIT_ASSERT(ep1.IsUnix());
  76. UNIT_ASSERT(ep1.SockAddr()->sa_family == AF_UNIX);
  77. UNIT_ASSERT(!ep2.IsUnix());
  78. UNIT_ASSERT(ep2.SockAddr()->sa_family != AF_UNIX);
  79. }
  80. } // Y_UNIT_TEST_SUITE(TEndpointTest)