Paul Stewart | 188a84a | 2012-01-20 16:28:15 -0800 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
Paul Stewart | 1d18e8c | 2011-07-15 11:00:31 -0700 | [diff] [blame] | 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 | |
Paul Stewart | 188a84a | 2012-01-20 16:28:15 -0800 | [diff] [blame] | 12 | using std::string; |
Paul Stewart | 1d18e8c | 2011-07-15 11:00:31 -0700 | [diff] [blame] | 13 | using testing::Test; |
| 14 | |
| 15 | namespace shill { |
| 16 | |
| 17 | namespace { |
| 18 | const char kV4String1[] = "192.168.10.1"; |
| 19 | const unsigned char kV4Address1[] = { 192, 168, 10, 1 }; |
| 20 | const char kV4String2[] = "192.168.10"; |
| 21 | const unsigned char kV4Address2[] = { 192, 168, 10 }; |
| 22 | const char kV6String1[] = "fe80::1aa9:5ff:7ebf:14c5"; |
| 23 | const unsigned char kV6Address1[] = { 0xfe, 0x80, 0x00, 0x00, |
| 24 | 0x00, 0x00, 0x00, 0x00, |
| 25 | 0x1a, 0xa9, 0x05, 0xff, |
| 26 | 0x7e, 0xbf, 0x14, 0xc5 }; |
| 27 | const char kV6String2[] = "1980:0:1000:1b02:1aa9:5ff:7ebf"; |
| 28 | const unsigned char kV6Address2[] = { 0x19, 0x80, 0x00, 0x00, |
| 29 | 0x10, 0x00, 0x1b, 0x02, |
| 30 | 0x1a, 0xa9, 0x05, 0xff, |
| 31 | 0x7e, 0xbf }; |
| 32 | } // namespace {} |
| 33 | |
| 34 | class IPAddressTest : public Test { |
| 35 | protected: |
| 36 | void TestAddress(IPAddress::Family family, |
Paul Stewart | 188a84a | 2012-01-20 16:28:15 -0800 | [diff] [blame] | 37 | const string &good_string, |
Paul Stewart | 1d18e8c | 2011-07-15 11:00:31 -0700 | [diff] [blame] | 38 | const ByteString &good_bytes, |
Paul Stewart | 188a84a | 2012-01-20 16:28:15 -0800 | [diff] [blame] | 39 | const string &bad_string, |
Paul Stewart | 1d18e8c | 2011-07-15 11:00:31 -0700 | [diff] [blame] | 40 | const ByteString &bad_bytes) { |
| 41 | IPAddress good_addr(family); |
| 42 | |
| 43 | EXPECT_TRUE(good_addr.SetAddressFromString(good_string)); |
| 44 | EXPECT_EQ(IPAddress::GetAddressLength(family), good_addr.GetLength()); |
| 45 | EXPECT_EQ(family, good_addr.family()); |
| 46 | EXPECT_FALSE(good_addr.IsDefault()); |
| 47 | EXPECT_EQ(0, memcmp(good_addr.GetConstData(), good_bytes.GetConstData(), |
| 48 | good_bytes.GetLength())); |
| 49 | EXPECT_TRUE(good_addr.address().Equals(good_bytes)); |
Paul Stewart | 188a84a | 2012-01-20 16:28:15 -0800 | [diff] [blame] | 50 | string address_string; |
mukesh agrawal | 2c15d2c | 2012-02-21 16:09:21 -0800 | [diff] [blame] | 51 | EXPECT_TRUE(good_addr.IntoString(&address_string)); |
Paul Stewart | 188a84a | 2012-01-20 16:28:15 -0800 | [diff] [blame] | 52 | EXPECT_EQ(good_string, address_string); |
Paul Stewart | 1d18e8c | 2011-07-15 11:00:31 -0700 | [diff] [blame] | 53 | |
| 54 | IPAddress good_addr_from_bytes(family, good_bytes); |
| 55 | EXPECT_TRUE(good_addr.Equals(good_addr_from_bytes)); |
| 56 | |
| 57 | IPAddress bad_addr(family); |
| 58 | EXPECT_FALSE(bad_addr.SetAddressFromString(bad_string)); |
| 59 | EXPECT_FALSE(good_addr.Equals(bad_addr)); |
| 60 | |
| 61 | EXPECT_FALSE(bad_addr.IsValid()); |
| 62 | |
| 63 | IPAddress bad_addr_from_bytes(family, bad_bytes); |
| 64 | EXPECT_EQ(family, bad_addr_from_bytes.family()); |
| 65 | EXPECT_FALSE(bad_addr_from_bytes.IsValid()); |
| 66 | |
| 67 | EXPECT_FALSE(bad_addr.Equals(bad_addr_from_bytes)); |
mukesh agrawal | 2c15d2c | 2012-02-21 16:09:21 -0800 | [diff] [blame] | 68 | EXPECT_FALSE(bad_addr.IntoString(&address_string)); |
Paul Stewart | 1d18e8c | 2011-07-15 11:00:31 -0700 | [diff] [blame] | 69 | } |
| 70 | }; |
| 71 | |
| 72 | TEST_F(IPAddressTest, Statics) { |
Paul Stewart | 7355ce1 | 2011-09-02 10:47:01 -0700 | [diff] [blame] | 73 | EXPECT_EQ(4, IPAddress::GetAddressLength(IPAddress::kFamilyIPv4)); |
| 74 | EXPECT_EQ(16, IPAddress::GetAddressLength(IPAddress::kFamilyIPv6)); |
Paul Stewart | 1d18e8c | 2011-07-15 11:00:31 -0700 | [diff] [blame] | 75 | |
Darin Petkov | 14c29ec | 2012-03-02 11:34:19 +0100 | [diff] [blame] | 76 | EXPECT_EQ(0, IPAddress::GetPrefixLengthFromMask(IPAddress::kFamilyIPv4, |
| 77 | "0.0.0.0")); |
| 78 | EXPECT_EQ(20, IPAddress::GetPrefixLengthFromMask(IPAddress::kFamilyIPv4, |
| 79 | "255.255.240.0")); |
| 80 | EXPECT_EQ(32, IPAddress::GetPrefixLengthFromMask(IPAddress::kFamilyIPv4, |
| 81 | "255.255.255.255")); |
| 82 | EXPECT_EQ(32, IPAddress::GetPrefixLengthFromMask(IPAddress::kFamilyIPv4, |
| 83 | "")); |
| 84 | EXPECT_EQ(32, IPAddress::GetPrefixLengthFromMask(IPAddress::kFamilyIPv4, |
| 85 | "foo")); |
| 86 | |
Paul Stewart | 7355ce1 | 2011-09-02 10:47:01 -0700 | [diff] [blame] | 87 | IPAddress addr4(IPAddress::kFamilyIPv4); |
Paul Stewart | 1d18e8c | 2011-07-15 11:00:31 -0700 | [diff] [blame] | 88 | addr4.SetAddressToDefault(); |
| 89 | |
| 90 | EXPECT_EQ(4, addr4.GetLength()); |
Paul Stewart | 7355ce1 | 2011-09-02 10:47:01 -0700 | [diff] [blame] | 91 | EXPECT_EQ(IPAddress::kFamilyIPv4, addr4.family()); |
Paul Stewart | 1d18e8c | 2011-07-15 11:00:31 -0700 | [diff] [blame] | 92 | EXPECT_TRUE(addr4.IsDefault()); |
| 93 | EXPECT_TRUE(addr4.address().IsZero()); |
| 94 | EXPECT_TRUE(addr4.address().Equals(ByteString(4))); |
| 95 | |
| 96 | |
Paul Stewart | 7355ce1 | 2011-09-02 10:47:01 -0700 | [diff] [blame] | 97 | IPAddress addr6(IPAddress::kFamilyIPv6); |
Paul Stewart | 1d18e8c | 2011-07-15 11:00:31 -0700 | [diff] [blame] | 98 | addr6.SetAddressToDefault(); |
| 99 | |
| 100 | EXPECT_EQ(16, addr6.GetLength()); |
Paul Stewart | 7355ce1 | 2011-09-02 10:47:01 -0700 | [diff] [blame] | 101 | EXPECT_EQ(addr6.family(), IPAddress::kFamilyIPv6); |
Paul Stewart | 1d18e8c | 2011-07-15 11:00:31 -0700 | [diff] [blame] | 102 | EXPECT_TRUE(addr6.IsDefault()); |
| 103 | EXPECT_TRUE(addr6.address().IsZero()); |
| 104 | EXPECT_TRUE(addr6.address().Equals(ByteString(16))); |
| 105 | |
| 106 | EXPECT_FALSE(addr4.Equals(addr6)); |
| 107 | } |
| 108 | |
| 109 | TEST_F(IPAddressTest, IPv4) { |
Paul Stewart | 7355ce1 | 2011-09-02 10:47:01 -0700 | [diff] [blame] | 110 | TestAddress(IPAddress::kFamilyIPv4, |
Paul Stewart | 1d18e8c | 2011-07-15 11:00:31 -0700 | [diff] [blame] | 111 | kV4String1, ByteString(kV4Address1, sizeof(kV4Address1)), |
| 112 | kV4String2, ByteString(kV4Address2, sizeof(kV4Address2))); |
| 113 | } |
| 114 | |
| 115 | |
| 116 | TEST_F(IPAddressTest, IPv6) { |
Paul Stewart | 7355ce1 | 2011-09-02 10:47:01 -0700 | [diff] [blame] | 117 | TestAddress(IPAddress::kFamilyIPv6, |
Paul Stewart | 1d18e8c | 2011-07-15 11:00:31 -0700 | [diff] [blame] | 118 | kV6String1, ByteString(kV6Address1, sizeof(kV6Address1)), |
| 119 | kV6String2, ByteString(kV6Address2, sizeof(kV6Address2))); |
| 120 | } |
| 121 | |
| 122 | } // namespace shill |