network_ut.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. #include <library/cpp/testing/common/network.h>
  2. #include <library/cpp/testing/common/scope.h>
  3. #include <util/generic/hash_set.h>
  4. #include <util/folder/dirut.h>
  5. #include <util/folder/path.h>
  6. #include <util/folder/tempdir.h>
  7. #include <util/network/sock.h>
  8. #include <util/system/fs.h>
  9. #include <library/cpp/testing/gtest/gtest.h>
  10. static TTempDir TmpDir;
  11. TEST(NetworkTest, FreePort) {
  12. NTesting::TScopedEnvironment envGuard("PORT_SYNC_PATH", TmpDir.Name());
  13. NTesting::InitPortManagerFromEnv();
  14. TVector<NTesting::TPortHolder> ports(Reserve(100));
  15. for (size_t i = 0; i < 100; ++i) {
  16. ports.push_back(NTesting::GetFreePort());
  17. }
  18. THashSet<ui16> uniqPorts;
  19. for (auto& port : ports) {
  20. const TString guardPath = TmpDir.Path() / ToString(static_cast<ui16>(port));
  21. EXPECT_TRUE(NFs::Exists(guardPath));
  22. EXPECT_TRUE(uniqPorts.emplace(port).second);
  23. TInetStreamSocket sock;
  24. TSockAddrInet addr(TIpHost{INADDR_ANY}, port);
  25. ASSERT_EQ(0, SetSockOpt(sock, SOL_SOCKET, SO_REUSEADDR, 1));
  26. EXPECT_EQ(0, sock.Bind(&addr));
  27. }
  28. ports.clear();
  29. for (ui16 port : uniqPorts) {
  30. const TString guardPath = TmpDir.Path() / ToString(port);
  31. EXPECT_FALSE(NFs::Exists(guardPath));
  32. }
  33. }
  34. TEST(NetworkTest, FreePortWithinRanges) {
  35. NTesting::TScopedEnvironment envGuard{{
  36. {"PORT_SYNC_PATH", TmpDir.Name()},
  37. {"VALID_PORT_RANGE", "3456:7654"},
  38. }};
  39. NTesting::InitPortManagerFromEnv();
  40. for (size_t i = 0; i < 100; ++i) {
  41. auto holder = NTesting::GetFreePort();
  42. ui16 port = holder;
  43. ASSERT_GE(port, 3456u);
  44. ASSERT_LE(port, 7654u);
  45. }
  46. }
  47. TEST(NetworkTest, GetPortRandom) {
  48. NTesting::TScopedEnvironment envGuard{{
  49. {"PORT_SYNC_PATH", TmpDir.Name()},
  50. {"NO_RANDOM_PORTS", ""},
  51. }};
  52. NTesting::InitPortManagerFromEnv();
  53. ui16 testPort = 80; // value just must be outside the assignable range
  54. for (size_t i = 0; i < 10; ++i) {
  55. NTesting::TPortHolder assigned = NTesting::NLegacy::GetPort(testPort);
  56. ui16 assignedInt = assigned;
  57. ASSERT_NE(testPort, assignedInt);
  58. }
  59. }
  60. TEST(NetworkTest, GetPortNonRandom) {
  61. NTesting::TScopedEnvironment envGuard{{
  62. {"PORT_SYNC_PATH", TmpDir.Name()},
  63. {"NO_RANDOM_PORTS", "1"},
  64. }};
  65. NTesting::InitPortManagerFromEnv();
  66. TVector<ui16> ports(Reserve(100)); // keep integers, we don't need the ports to remain allocated
  67. for (size_t i = 0; i < 10; ++i) {
  68. auto portHolder = NTesting::GetFreePort();
  69. ports.push_back(portHolder);
  70. }
  71. for (auto& testPort : ports) {
  72. NTesting::TPortHolder assigned = NTesting::NLegacy::GetPort(testPort);
  73. ui16 assignedInt = assigned;
  74. ASSERT_EQ(testPort, assignedInt);
  75. }
  76. }
  77. TEST(FreePortTest, FreePortsRange) {
  78. NTesting::TScopedEnvironment envGuard("PORT_SYNC_PATH", TmpDir.Name());
  79. NTesting::InitPortManagerFromEnv();
  80. for (ui16 i = 2; i < 10; ++i) {
  81. TVector<NTesting::TPortHolder> ports = NTesting::NLegacy::GetFreePortsRange(i);
  82. ASSERT_EQ(i, ports.size());
  83. for (ui16 j = 1; j < i; ++j) {
  84. EXPECT_EQ(static_cast<ui16>(ports[j]), static_cast<ui16>(ports[0]) + j);
  85. }
  86. }
  87. }