ip_ut.cpp 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. #include "ip.h"
  2. #include <library/cpp/testing/unittest/registar.h>
  3. #include <util/generic/yexception.h>
  4. class TSysIpTest: public TTestBase {
  5. UNIT_TEST_SUITE(TSysIpTest);
  6. UNIT_TEST(TestIpFromString);
  7. UNIT_TEST_EXCEPTION(TestIpFromString2, yexception);
  8. UNIT_TEST_EXCEPTION(TestIpFromString3, yexception);
  9. UNIT_TEST_EXCEPTION(TestIpFromString4, yexception);
  10. UNIT_TEST_EXCEPTION(TestIpFromString5, yexception);
  11. UNIT_TEST(TestIpToString);
  12. UNIT_TEST_SUITE_END();
  13. private:
  14. void TestIpFromString();
  15. void TestIpFromString2();
  16. void TestIpFromString3();
  17. void TestIpFromString4();
  18. void TestIpFromString5();
  19. void TestIpToString();
  20. };
  21. UNIT_TEST_SUITE_REGISTRATION(TSysIpTest);
  22. void TSysIpTest::TestIpFromString() {
  23. const char* ipStr[] = {"192.168.0.1", "87.255.18.167", "255.255.0.31", "188.225.124.255"};
  24. ui8 ipArr[][4] = {{192, 168, 0, 1}, {87, 255, 18, 167}, {255, 255, 0, 31}, {188, 225, 124, 255}};
  25. for (size_t i = 0; i < Y_ARRAY_SIZE(ipStr); ++i) {
  26. const ui32 ip = IpFromString(ipStr[i]);
  27. UNIT_ASSERT(memcmp(&ip, ipArr[i], sizeof(ui32)) == 0);
  28. }
  29. }
  30. void TSysIpTest::TestIpFromString2() {
  31. IpFromString("XXXXXXWXW");
  32. }
  33. void TSysIpTest::TestIpFromString3() {
  34. IpFromString("986.0.37.255");
  35. }
  36. void TSysIpTest::TestIpFromString4() {
  37. IpFromString("256.0.22.365");
  38. }
  39. void TSysIpTest::TestIpFromString5() {
  40. IpFromString("245.12..0");
  41. }
  42. void TSysIpTest::TestIpToString() {
  43. ui8 ipArr[][4] = {{192, 168, 0, 1}, {87, 255, 18, 167}, {255, 255, 0, 31}, {188, 225, 124, 255}};
  44. const char* ipStr[] = {"192.168.0.1", "87.255.18.167", "255.255.0.31", "188.225.124.255"};
  45. for (size_t i = 0; i < Y_ARRAY_SIZE(ipStr); ++i) {
  46. UNIT_ASSERT(IpToString(*reinterpret_cast<TIpHost*>(&(ipArr[i]))) == ipStr[i]);
  47. }
  48. }