parsenetaddress_test.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /*
  2. * Copyright (C) 2016 The Android Open Source Project
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include "android-base/parsenetaddress.h"
  17. #include <gtest/gtest.h>
  18. using android::base::ParseNetAddress;
  19. TEST(ParseNetAddressTest, TestUrl) {
  20. std::string canonical, host, error;
  21. int port = 123;
  22. EXPECT_TRUE(
  23. ParseNetAddress("www.google.com", &host, &port, &canonical, &error));
  24. EXPECT_EQ("www.google.com:123", canonical);
  25. EXPECT_EQ("www.google.com", host);
  26. EXPECT_EQ(123, port);
  27. EXPECT_TRUE(
  28. ParseNetAddress("www.google.com:666", &host, &port, &canonical, &error));
  29. EXPECT_EQ("www.google.com:666", canonical);
  30. EXPECT_EQ("www.google.com", host);
  31. EXPECT_EQ(666, port);
  32. }
  33. TEST(ParseNetAddressTest, TestIpv4) {
  34. std::string canonical, host, error;
  35. int port = 123;
  36. EXPECT_TRUE(ParseNetAddress("1.2.3.4", &host, &port, &canonical, &error));
  37. EXPECT_EQ("1.2.3.4:123", canonical);
  38. EXPECT_EQ("1.2.3.4", host);
  39. EXPECT_EQ(123, port);
  40. EXPECT_TRUE(ParseNetAddress("1.2.3.4:666", &host, &port, &canonical, &error));
  41. EXPECT_EQ("1.2.3.4:666", canonical);
  42. EXPECT_EQ("1.2.3.4", host);
  43. EXPECT_EQ(666, port);
  44. }
  45. TEST(ParseNetAddressTest, TestIpv6) {
  46. std::string canonical, host, error;
  47. int port = 123;
  48. EXPECT_TRUE(ParseNetAddress("::1", &host, &port, &canonical, &error));
  49. EXPECT_EQ("[::1]:123", canonical);
  50. EXPECT_EQ("::1", host);
  51. EXPECT_EQ(123, port);
  52. EXPECT_TRUE(ParseNetAddress("fe80::200:5aee:feaa:20a2", &host, &port,
  53. &canonical, &error));
  54. EXPECT_EQ("[fe80::200:5aee:feaa:20a2]:123", canonical);
  55. EXPECT_EQ("fe80::200:5aee:feaa:20a2", host);
  56. EXPECT_EQ(123, port);
  57. EXPECT_TRUE(ParseNetAddress("[::1]:666", &host, &port, &canonical, &error));
  58. EXPECT_EQ("[::1]:666", canonical);
  59. EXPECT_EQ("::1", host);
  60. EXPECT_EQ(666, port);
  61. EXPECT_TRUE(ParseNetAddress("[fe80::200:5aee:feaa:20a2]:666", &host, &port,
  62. &canonical, &error));
  63. EXPECT_EQ("[fe80::200:5aee:feaa:20a2]:666", canonical);
  64. EXPECT_EQ("fe80::200:5aee:feaa:20a2", host);
  65. EXPECT_EQ(666, port);
  66. }
  67. TEST(ParseNetAddressTest, TestInvalidAddress) {
  68. std::string canonical, host;
  69. int port;
  70. std::string failure_cases[] = {
  71. // Invalid IPv4.
  72. "1.2.3.4:",
  73. "1.2.3.4::",
  74. ":123",
  75. // Invalid IPv6.
  76. ":1",
  77. "::::::::1",
  78. "[::1",
  79. "[::1]",
  80. "[::1]:",
  81. "[::1]::",
  82. // Invalid port.
  83. "1.2.3.4:-1",
  84. "1.2.3.4:0",
  85. "1.2.3.4:65536"
  86. "1.2.3.4:hello",
  87. "[::1]:-1",
  88. "[::1]:0",
  89. "[::1]:65536",
  90. "[::1]:hello",
  91. };
  92. for (const auto& address : failure_cases) {
  93. // Failure should give some non-empty error string.
  94. std::string error;
  95. EXPECT_FALSE(ParseNetAddress(address, &host, &port, &canonical, &error));
  96. EXPECT_NE("", error);
  97. }
  98. }
  99. // Null canonical address argument.
  100. TEST(ParseNetAddressTest, TestNullCanonicalAddress) {
  101. std::string host, error;
  102. int port = 42;
  103. EXPECT_TRUE(ParseNetAddress("www.google.com", &host, &port, nullptr, &error));
  104. EXPECT_TRUE(ParseNetAddress("1.2.3.4", &host, &port, nullptr, &error));
  105. EXPECT_TRUE(ParseNetAddress("::1", &host, &port, nullptr, &error));
  106. }