1 //
2 // Copyright (C) 2013 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
17 #include "shill/ip_address_store.h"
18
19 #include <string>
20
21 #include <gtest/gtest.h>
22
23 using ::std::string;
24 using ::std::vector;
25 using ::testing::Test;
26
27 namespace shill {
28
29 namespace {
30 const char kIPAddress_0_0_0_0[] = "0.0.0.0";
31 const char kIPAddress_8_8_8_8[] = "8.8.8.8";
32 const char kIPAddress_7_7_7_7[] = "7.7.7.7";
33 } // namespace
34
35 class IPAddressStoreTest : public Test {
36 public:
IPAddressStoreTest()37 IPAddressStoreTest() {}
38
39 protected:
StringToIPv4Address(const string & address_string)40 IPAddress StringToIPv4Address(const string& address_string) {
41 IPAddress ip_address(IPAddress::kFamilyIPv4);
42 EXPECT_TRUE(ip_address.SetAddressFromString(address_string));
43 return ip_address;
44 }
45
PopulateIPAddressStore()46 void PopulateIPAddressStore() {
47 ip_address_store_.AddUnique(StringToIPv4Address(kIPAddress_0_0_0_0));
48 ip_address_store_.AddUnique(StringToIPv4Address(kIPAddress_8_8_8_8));
49 ip_address_store_.AddUnique(StringToIPv4Address(kIPAddress_7_7_7_7));
50 }
51
52 IPAddressStore ip_address_store_;
53 };
54
TEST_F(IPAddressStoreTest,AddUnique)55 TEST_F(IPAddressStoreTest, AddUnique) {
56 IPAddress ip_0_0_0_0 = StringToIPv4Address(kIPAddress_0_0_0_0);
57 IPAddress ip_8_8_8_8 = StringToIPv4Address(kIPAddress_8_8_8_8);
58 IPAddress ip_7_7_7_7 = StringToIPv4Address(kIPAddress_7_7_7_7);
59
60 EXPECT_EQ(0, ip_address_store_.Count());
61 ip_address_store_.AddUnique(ip_0_0_0_0);
62 EXPECT_TRUE(ip_address_store_.Contains(ip_0_0_0_0));
63 EXPECT_FALSE(ip_address_store_.Contains(ip_8_8_8_8));
64 EXPECT_FALSE(ip_address_store_.Contains(ip_7_7_7_7));
65 EXPECT_EQ(1, ip_address_store_.Count());
66 ip_address_store_.AddUnique(ip_8_8_8_8);
67 EXPECT_TRUE(ip_address_store_.Contains(ip_8_8_8_8));
68 EXPECT_FALSE(ip_address_store_.Contains(ip_7_7_7_7));
69 EXPECT_EQ(2, ip_address_store_.Count());
70 ip_address_store_.AddUnique(ip_8_8_8_8);
71 EXPECT_TRUE(ip_address_store_.Contains(ip_0_0_0_0));
72 EXPECT_TRUE(ip_address_store_.Contains(ip_8_8_8_8));
73 EXPECT_EQ(2, ip_address_store_.Count());
74 ip_address_store_.AddUnique(ip_0_0_0_0);
75 EXPECT_EQ(2, ip_address_store_.Count());
76 ip_address_store_.AddUnique(ip_7_7_7_7);
77 EXPECT_TRUE(ip_address_store_.Contains(ip_7_7_7_7));
78 EXPECT_EQ(3, ip_address_store_.Count());
79 }
80
81 } // namespace shill
82