Paul Stewart | 1d18e8c | 2011-07-15 11:00:31 -0700 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium OS Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include <gtest/gtest.h> |
| 6 | |
| 7 | #include <arpa/inet.h> |
| 8 | |
| 9 | #include "shill/byte_string.h" |
| 10 | #include "shill/ip_address.h" |
| 11 | |
| 12 | using testing::Test; |
| 13 | |
| 14 | namespace shill { |
| 15 | |
| 16 | namespace { |
| 17 | const char kV4String1[] = "192.168.10.1"; |
| 18 | const unsigned char kV4Address1[] = { 192, 168, 10, 1 }; |
| 19 | const char kV4String2[] = "192.168.10"; |
| 20 | const unsigned char kV4Address2[] = { 192, 168, 10 }; |
| 21 | const char kV6String1[] = "fe80::1aa9:5ff:7ebf:14c5"; |
| 22 | const unsigned char kV6Address1[] = { 0xfe, 0x80, 0x00, 0x00, |
| 23 | 0x00, 0x00, 0x00, 0x00, |
| 24 | 0x1a, 0xa9, 0x05, 0xff, |
| 25 | 0x7e, 0xbf, 0x14, 0xc5 }; |
| 26 | const char kV6String2[] = "1980:0:1000:1b02:1aa9:5ff:7ebf"; |
| 27 | const unsigned char kV6Address2[] = { 0x19, 0x80, 0x00, 0x00, |
| 28 | 0x10, 0x00, 0x1b, 0x02, |
| 29 | 0x1a, 0xa9, 0x05, 0xff, |
| 30 | 0x7e, 0xbf }; |
| 31 | } // namespace {} |
| 32 | |
| 33 | class IPAddressTest : public Test { |
| 34 | protected: |
| 35 | void TestAddress(IPAddress::Family family, |
| 36 | const std::string &good_string, |
| 37 | const ByteString &good_bytes, |
| 38 | const std::string &bad_string, |
| 39 | const ByteString &bad_bytes) { |
| 40 | IPAddress good_addr(family); |
| 41 | |
| 42 | EXPECT_TRUE(good_addr.SetAddressFromString(good_string)); |
| 43 | EXPECT_EQ(IPAddress::GetAddressLength(family), good_addr.GetLength()); |
| 44 | EXPECT_EQ(family, good_addr.family()); |
| 45 | EXPECT_FALSE(good_addr.IsDefault()); |
| 46 | EXPECT_EQ(0, memcmp(good_addr.GetConstData(), good_bytes.GetConstData(), |
| 47 | good_bytes.GetLength())); |
| 48 | EXPECT_TRUE(good_addr.address().Equals(good_bytes)); |
| 49 | |
| 50 | IPAddress good_addr_from_bytes(family, good_bytes); |
| 51 | EXPECT_TRUE(good_addr.Equals(good_addr_from_bytes)); |
| 52 | |
| 53 | IPAddress bad_addr(family); |
| 54 | EXPECT_FALSE(bad_addr.SetAddressFromString(bad_string)); |
| 55 | EXPECT_FALSE(good_addr.Equals(bad_addr)); |
| 56 | |
| 57 | EXPECT_FALSE(bad_addr.IsValid()); |
| 58 | |
| 59 | IPAddress bad_addr_from_bytes(family, bad_bytes); |
| 60 | EXPECT_EQ(family, bad_addr_from_bytes.family()); |
| 61 | EXPECT_FALSE(bad_addr_from_bytes.IsValid()); |
| 62 | |
| 63 | EXPECT_FALSE(bad_addr.Equals(bad_addr_from_bytes)); |
| 64 | } |
| 65 | }; |
| 66 | |
| 67 | TEST_F(IPAddressTest, Statics) { |
Paul Stewart | 7355ce1 | 2011-09-02 10:47:01 -0700 | [diff] [blame] | 68 | EXPECT_EQ(4, IPAddress::GetAddressLength(IPAddress::kFamilyIPv4)); |
| 69 | EXPECT_EQ(16, IPAddress::GetAddressLength(IPAddress::kFamilyIPv6)); |
Paul Stewart | 1d18e8c | 2011-07-15 11:00:31 -0700 | [diff] [blame] | 70 | |
Paul Stewart | 7355ce1 | 2011-09-02 10:47:01 -0700 | [diff] [blame] | 71 | IPAddress addr4(IPAddress::kFamilyIPv4); |
Paul Stewart | 1d18e8c | 2011-07-15 11:00:31 -0700 | [diff] [blame] | 72 | addr4.SetAddressToDefault(); |
| 73 | |
| 74 | EXPECT_EQ(4, addr4.GetLength()); |
Paul Stewart | 7355ce1 | 2011-09-02 10:47:01 -0700 | [diff] [blame] | 75 | EXPECT_EQ(IPAddress::kFamilyIPv4, addr4.family()); |
Paul Stewart | 1d18e8c | 2011-07-15 11:00:31 -0700 | [diff] [blame] | 76 | EXPECT_TRUE(addr4.IsDefault()); |
| 77 | EXPECT_TRUE(addr4.address().IsZero()); |
| 78 | EXPECT_TRUE(addr4.address().Equals(ByteString(4))); |
| 79 | |
| 80 | |
Paul Stewart | 7355ce1 | 2011-09-02 10:47:01 -0700 | [diff] [blame] | 81 | IPAddress addr6(IPAddress::kFamilyIPv6); |
Paul Stewart | 1d18e8c | 2011-07-15 11:00:31 -0700 | [diff] [blame] | 82 | addr6.SetAddressToDefault(); |
| 83 | |
| 84 | EXPECT_EQ(16, addr6.GetLength()); |
Paul Stewart | 7355ce1 | 2011-09-02 10:47:01 -0700 | [diff] [blame] | 85 | EXPECT_EQ(addr6.family(), IPAddress::kFamilyIPv6); |
Paul Stewart | 1d18e8c | 2011-07-15 11:00:31 -0700 | [diff] [blame] | 86 | EXPECT_TRUE(addr6.IsDefault()); |
| 87 | EXPECT_TRUE(addr6.address().IsZero()); |
| 88 | EXPECT_TRUE(addr6.address().Equals(ByteString(16))); |
| 89 | |
| 90 | EXPECT_FALSE(addr4.Equals(addr6)); |
| 91 | } |
| 92 | |
| 93 | TEST_F(IPAddressTest, IPv4) { |
Paul Stewart | 7355ce1 | 2011-09-02 10:47:01 -0700 | [diff] [blame] | 94 | TestAddress(IPAddress::kFamilyIPv4, |
Paul Stewart | 1d18e8c | 2011-07-15 11:00:31 -0700 | [diff] [blame] | 95 | kV4String1, ByteString(kV4Address1, sizeof(kV4Address1)), |
| 96 | kV4String2, ByteString(kV4Address2, sizeof(kV4Address2))); |
| 97 | } |
| 98 | |
| 99 | |
| 100 | TEST_F(IPAddressTest, IPv6) { |
Paul Stewart | 7355ce1 | 2011-09-02 10:47:01 -0700 | [diff] [blame] | 101 | TestAddress(IPAddress::kFamilyIPv6, |
Paul Stewart | 1d18e8c | 2011-07-15 11:00:31 -0700 | [diff] [blame] | 102 | kV6String1, ByteString(kV6Address1, sizeof(kV6Address1)), |
| 103 | kV6String2, ByteString(kV6Address2, sizeof(kV6Address2))); |
| 104 | } |
| 105 | |
| 106 | } // namespace shill |