diff options
author | Elliott Hughes <enh@google.com> | 2015-07-18 12:21:30 -0700 |
---|---|---|
committer | Elliott Hughes <enh@google.com> | 2015-07-21 15:25:14 -0700 |
commit | a5268097f605452a362ec0d49f9b3dd9174c1e92 (patch) | |
tree | 2be312cbca0b49375c67d451a2648042d3efa370 /adb_utils_test.cpp | |
parent | ebf3aaf3477c75c6bbc4b2c49542e6fe7a91dce7 (diff) | |
download | adb-a5268097f605452a362ec0d49f9b3dd9174c1e92.tar.gz |
Recognize IPv6 addresses for "adb connect".
Bug: http://b/22559299
Change-Id: I32891d706b5010c38db84a056e76dd279b780f75
Diffstat (limited to 'adb_utils_test.cpp')
-rw-r--r-- | adb_utils_test.cpp | 82 |
1 files changed, 82 insertions, 0 deletions
diff --git a/adb_utils_test.cpp b/adb_utils_test.cpp index 052aea5..7aa610a 100644 --- a/adb_utils_test.cpp +++ b/adb_utils_test.cpp @@ -50,3 +50,85 @@ TEST(adb_utils, escape_arg) { ASSERT_EQ(R"('abc(')", escape_arg("abc(")); ASSERT_EQ(R"('abc)')", escape_arg("abc)")); } + +TEST(adb_utils, parse_host_and_port) { + std::string canonical_address; + std::string host; + int port; + std::string error; + + // Name, default port. + port = 123; + ASSERT_TRUE(parse_host_and_port("www.google.com", &canonical_address, &host, &port, &error)); + ASSERT_EQ("www.google.com:123", canonical_address); + ASSERT_EQ("www.google.com", host); + ASSERT_EQ(123, port); + + // Name, explicit port. + ASSERT_TRUE(parse_host_and_port("www.google.com:666", &canonical_address, &host, &port, &error)); + ASSERT_EQ("www.google.com:666", canonical_address); + ASSERT_EQ("www.google.com", host); + ASSERT_EQ(666, port); + + // IPv4, default port. + port = 123; + ASSERT_TRUE(parse_host_and_port("1.2.3.4", &canonical_address, &host, &port, &error)); + ASSERT_EQ("1.2.3.4:123", canonical_address); + ASSERT_EQ("1.2.3.4", host); + ASSERT_EQ(123, port); + + // IPv4, explicit port. + ASSERT_TRUE(parse_host_and_port("1.2.3.4:666", &canonical_address, &host, &port, &error)); + ASSERT_EQ("1.2.3.4:666", canonical_address); + ASSERT_EQ("1.2.3.4", host); + ASSERT_EQ(666, port); + + // Simple IPv6, default port. + port = 123; + ASSERT_TRUE(parse_host_and_port("::1", &canonical_address, &host, &port, &error)); + ASSERT_EQ("[::1]:123", canonical_address); + ASSERT_EQ("::1", host); + ASSERT_EQ(123, port); + + // Simple IPv6, explicit port. + ASSERT_TRUE(parse_host_and_port("[::1]:666", &canonical_address, &host, &port, &error)); + ASSERT_EQ("[::1]:666", canonical_address); + ASSERT_EQ("::1", host); + ASSERT_EQ(666, port); + + // Hairy IPv6, default port. + port = 123; + ASSERT_TRUE(parse_host_and_port("fe80::200:5aee:feaa:20a2", &canonical_address, &host, &port, &error)); + ASSERT_EQ("[fe80::200:5aee:feaa:20a2]:123", canonical_address); + ASSERT_EQ("fe80::200:5aee:feaa:20a2", host); + ASSERT_EQ(123, port); + + // Simple IPv6, explicit port. + ASSERT_TRUE(parse_host_and_port("[fe80::200:5aee:feaa:20a2]:666", &canonical_address, &host, &port, &error)); + ASSERT_EQ("[fe80::200:5aee:feaa:20a2]:666", canonical_address); + ASSERT_EQ("fe80::200:5aee:feaa:20a2", host); + ASSERT_EQ(666, port); + + // Invalid IPv4. + EXPECT_FALSE(parse_host_and_port("1.2.3.4:", &canonical_address, &host, &port, &error)); + EXPECT_FALSE(parse_host_and_port("1.2.3.4::", &canonical_address, &host, &port, &error)); + EXPECT_FALSE(parse_host_and_port("1.2.3.4:hello", &canonical_address, &host, &port, &error)); + EXPECT_FALSE(parse_host_and_port(":123", &canonical_address, &host, &port, &error)); + + // Invalid IPv6. + EXPECT_FALSE(parse_host_and_port(":1", &canonical_address, &host, &port, &error)); + EXPECT_FALSE(parse_host_and_port("::::::::1", &canonical_address, &host, &port, &error)); + EXPECT_FALSE(parse_host_and_port("[::1", &canonical_address, &host, &port, &error)); + EXPECT_FALSE(parse_host_and_port("[::1]", &canonical_address, &host, &port, &error)); + EXPECT_FALSE(parse_host_and_port("[::1]:", &canonical_address, &host, &port, &error)); + EXPECT_FALSE(parse_host_and_port("[::1]::", &canonical_address, &host, &port, &error)); + EXPECT_FALSE(parse_host_and_port("[::1]:hello", &canonical_address, &host, &port, &error)); + + // Invalid ports. + EXPECT_FALSE(parse_host_and_port("[::1]:-1", &canonical_address, &host, &port, &error)); + EXPECT_FALSE(parse_host_and_port("[::1]:0", &canonical_address, &host, &port, &error)); + EXPECT_FALSE(parse_host_and_port("[::1]:65536", &canonical_address, &host, &port, &error)); + EXPECT_FALSE(parse_host_and_port("1.2.3.4:-1", &canonical_address, &host, &port, &error)); + EXPECT_FALSE(parse_host_and_port("1.2.3.4:0", &canonical_address, &host, &port, &error)); + EXPECT_FALSE(parse_host_and_port("1.2.3.4:65536", &canonical_address, &host, &port, &error)); +} |