You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

89 lines
2.8 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * Copyright 2010 The WebRTC Project Authors. All rights reserved.
  3. *
  4. * Use of this source code is governed by a BSD-style license
  5. * that can be found in the LICENSE file in the root of the source
  6. * tree. An additional intellectual property rights grant can be found
  7. * in the file PATENTS. All contributing project authors may
  8. * be found in the AUTHORS file in the root of the source tree.
  9. */
  10. #include "rtc_base/win32.h"
  11. #include <string>
  12. #include "rtc_base/gunit.h"
  13. #include "rtc_base/net_helpers.h"
  14. #if !defined(WEBRTC_WIN)
  15. #error Only for Windows
  16. #endif
  17. namespace rtc {
  18. class Win32Test : public ::testing::Test {
  19. public:
  20. Win32Test() {}
  21. };
  22. TEST_F(Win32Test, IPv6AddressCompression) {
  23. IPAddress ipv6;
  24. // Zero compression should be done on the leftmost 0s when there are
  25. // multiple longest series.
  26. ASSERT_TRUE(IPFromString("2a00:8a00:a000:1190:0000:0001:000:252", &ipv6));
  27. EXPECT_EQ("2a00:8a00:a000:1190::1:0:252", ipv6.ToString());
  28. // Ensure the zero compression could handle multiple octects.
  29. ASSERT_TRUE(IPFromString("0:0:0:0:0:0:0:1", &ipv6));
  30. EXPECT_EQ("::1", ipv6.ToString());
  31. // Make sure multiple 0 octects compressed.
  32. ASSERT_TRUE(IPFromString("fe80:0:0:0:2aa:ff:fe9a:4ca2", &ipv6));
  33. EXPECT_EQ("fe80::2aa:ff:fe9a:4ca2", ipv6.ToString());
  34. // Test zero compression at the end of string.
  35. ASSERT_TRUE(IPFromString("2a00:8a00:a000:1190:0000:0001:000:00", &ipv6));
  36. EXPECT_EQ("2a00:8a00:a000:1190:0:1::", ipv6.ToString());
  37. // Test zero compression at the beginning of string.
  38. ASSERT_TRUE(IPFromString("0:0:000:1190:0000:0001:000:00", &ipv6));
  39. EXPECT_EQ("::1190:0:1:0:0", ipv6.ToString());
  40. // Test zero compression only done once.
  41. ASSERT_TRUE(IPFromString("0:1:000:1190:0000:0001:000:01", &ipv6));
  42. EXPECT_EQ("::1:0:1190:0:1:0:1", ipv6.ToString());
  43. // Make sure noncompressable IPv6 is the same.
  44. ASSERT_TRUE(IPFromString("1234:5678:abcd:1234:5678:abcd:1234:5678", &ipv6));
  45. EXPECT_EQ("1234:5678:abcd:1234:5678:abcd:1234:5678", ipv6.ToString());
  46. }
  47. // Test that invalid IPv6 addresses are recognized and false is returned.
  48. TEST_F(Win32Test, InvalidIPv6AddressParsing) {
  49. IPAddress ipv6;
  50. // More than 1 run of "::"s.
  51. EXPECT_FALSE(IPFromString("1::2::3", &ipv6));
  52. // More than 1 run of "::"s in a longer address.
  53. // See: https://bugs.chromium.org/p/webrtc/issues/detail?id=7592
  54. EXPECT_FALSE(IPFromString("1::2::3::4::5::6::7::8", &ipv6));
  55. // Three ':'s in a row.
  56. EXPECT_FALSE(IPFromString("1:::2", &ipv6));
  57. // Non-hex character.
  58. EXPECT_FALSE(IPFromString("test::1", &ipv6));
  59. // More than 4 hex digits per group.
  60. EXPECT_FALSE(IPFromString("abcde::1", &ipv6));
  61. // More than 8 groups.
  62. EXPECT_FALSE(IPFromString("1:2:3:4:5:6:7:8:9", &ipv6));
  63. // Less than 8 groups.
  64. EXPECT_FALSE(IPFromString("1:2:3:4:5:6:7", &ipv6));
  65. }
  66. } // namespace rtc