1 // Copyright (c) 2009 The Chromium 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 "net/base/address_list.h" 6 7 #include "base/string_util.h" 8 #include "net/base/host_resolver_proc.h" 9 #include "net/base/net_util.h" 10 #if defined(OS_WIN) 11 #include "net/base/winsock_init.h" 12 #endif 13 #include "testing/gtest/include/gtest/gtest.h" 14 15 namespace { 16 17 // Use getaddrinfo() to allocate an addrinfo structure. 18 void CreateAddressList(net::AddressList* addrlist, int port) { 19 #if defined(OS_WIN) 20 net::EnsureWinsockInit(); 21 #endif 22 int rv = SystemHostResolverProc("192.168.1.1", 23 net::ADDRESS_FAMILY_UNSPECIFIED, 24 addrlist); 25 EXPECT_EQ(0, rv); 26 addrlist->SetPort(port); 27 } 28 29 TEST(AddressListTest, GetPort) { 30 net::AddressList addrlist; 31 CreateAddressList(&addrlist, 81); 32 EXPECT_EQ(81, addrlist.GetPort()); 33 34 addrlist.SetPort(83); 35 EXPECT_EQ(83, addrlist.GetPort()); 36 } 37 38 TEST(AddressListTest, Assignment) { 39 net::AddressList addrlist1; 40 CreateAddressList(&addrlist1, 85); 41 EXPECT_EQ(85, addrlist1.GetPort()); 42 43 // Should reference the same data as addrlist1 -- so when we change addrlist1 44 // both are changed. 45 net::AddressList addrlist2 = addrlist1; 46 EXPECT_EQ(85, addrlist2.GetPort()); 47 48 addrlist1.SetPort(80); 49 EXPECT_EQ(80, addrlist1.GetPort()); 50 EXPECT_EQ(80, addrlist2.GetPort()); 51 } 52 53 TEST(AddressListTest, Copy) { 54 net::AddressList addrlist1; 55 CreateAddressList(&addrlist1, 85); 56 EXPECT_EQ(85, addrlist1.GetPort()); 57 58 net::AddressList addrlist2; 59 addrlist2.Copy(addrlist1.head()); 60 61 // addrlist1 is the same as addrlist2 at this point. 62 EXPECT_EQ(85, addrlist1.GetPort()); 63 EXPECT_EQ(85, addrlist2.GetPort()); 64 65 // Changes to addrlist1 are not reflected in addrlist2. 66 addrlist1.SetPort(70); 67 addrlist2.SetPort(90); 68 69 EXPECT_EQ(70, addrlist1.GetPort()); 70 EXPECT_EQ(90, addrlist2.GetPort()); 71 } 72 73 } // namespace 74