OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "net/base/address_list.h" | 5 #include "net/base/address_list.h" |
6 | 6 |
7 #include "base/strings/string_util.h" | 7 #include "base/strings/string_util.h" |
8 #include "base/sys_byteorder.h" | 8 #include "base/sys_byteorder.h" |
9 #include "net/base/net_util.h" | 9 #include "net/base/net_util.h" |
10 #include "net/base/sys_addrinfo.h" | 10 #include "net/base/sys_addrinfo.h" |
(...skipping 11 matching lines...) Expand all Loading... |
22 // so just zero-ing them out for consistency. | 22 // so just zero-ing them out for consistency. |
23 memset(&address, 0x0, sizeof(address)); | 23 memset(&address, 0x0, sizeof(address)); |
24 // But we need to set the family. | 24 // But we need to set the family. |
25 address.sin_family = AF_INET; | 25 address.sin_family = AF_INET; |
26 struct addrinfo ai; | 26 struct addrinfo ai; |
27 memset(&ai, 0x0, sizeof(ai)); | 27 memset(&ai, 0x0, sizeof(ai)); |
28 ai.ai_family = AF_INET; | 28 ai.ai_family = AF_INET; |
29 ai.ai_socktype = SOCK_STREAM; | 29 ai.ai_socktype = SOCK_STREAM; |
30 ai.ai_addrlen = sizeof(address); | 30 ai.ai_addrlen = sizeof(address); |
31 ai.ai_addr = reinterpret_cast<sockaddr*>(&address); | 31 ai.ai_addr = reinterpret_cast<sockaddr*>(&address); |
32 ai.ai_canonname = const_cast<char *>(kCanonicalHostname); | 32 ai.ai_canonname = const_cast<char*>(kCanonicalHostname); |
33 | 33 |
34 // Copy the addrinfo struct into an AddressList object and | 34 // Copy the addrinfo struct into an AddressList object and |
35 // make sure it seems correct. | 35 // make sure it seems correct. |
36 AddressList addrlist1 = AddressList::CreateFromAddrinfo(&ai); | 36 AddressList addrlist1 = AddressList::CreateFromAddrinfo(&ai); |
37 EXPECT_EQ("canonical.bar.com", addrlist1.canonical_name()); | 37 EXPECT_EQ("canonical.bar.com", addrlist1.canonical_name()); |
38 | 38 |
39 // Copy the AddressList to another one. | 39 // Copy the AddressList to another one. |
40 AddressList addrlist2 = addrlist1; | 40 AddressList addrlist2 = addrlist1; |
41 EXPECT_EQ("canonical.bar.com", addrlist2.canonical_name()); | 41 EXPECT_EQ("canonical.bar.com", addrlist2.canonical_name()); |
42 } | 42 } |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
88 | 88 |
89 TEST(AddressListTest, CreateFromIPAddressList) { | 89 TEST(AddressListTest, CreateFromIPAddressList) { |
90 struct TestData { | 90 struct TestData { |
91 std::string ip_address; | 91 std::string ip_address; |
92 const char* in_addr; | 92 const char* in_addr; |
93 int ai_family; | 93 int ai_family; |
94 size_t ai_addrlen; | 94 size_t ai_addrlen; |
95 size_t in_addr_offset; | 95 size_t in_addr_offset; |
96 size_t in_addr_size; | 96 size_t in_addr_size; |
97 } tests[] = { | 97 } tests[] = { |
98 { "127.0.0.1", | 98 { |
99 "\x7f\x00\x00\x01", | 99 "127.0.0.1", "\x7f\x00\x00\x01", AF_INET, sizeof(struct sockaddr_in), |
100 AF_INET, | 100 offsetof(struct sockaddr_in, sin_addr), sizeof(struct in_addr), |
101 sizeof(struct sockaddr_in), | 101 }, |
102 offsetof(struct sockaddr_in, sin_addr), | 102 { |
103 sizeof(struct in_addr), | 103 "2001:db8:0::42", |
104 }, | 104 "\x20\x01\x0d\xb8\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x42", |
105 { "2001:db8:0::42", | 105 AF_INET6, sizeof(struct sockaddr_in6), |
106 "\x20\x01\x0d\xb8\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x42", | 106 offsetof(struct sockaddr_in6, sin6_addr), sizeof(struct in6_addr), |
107 AF_INET6, | 107 }, |
108 sizeof(struct sockaddr_in6), | 108 { |
109 offsetof(struct sockaddr_in6, sin6_addr), | 109 "192.168.1.1", "\xc0\xa8\x01\x01", AF_INET, sizeof(struct sockaddr_in), |
110 sizeof(struct in6_addr), | 110 offsetof(struct sockaddr_in, sin_addr), sizeof(struct in_addr), |
111 }, | 111 }, |
112 { "192.168.1.1", | 112 }; |
113 "\xc0\xa8\x01\x01", | |
114 AF_INET, | |
115 sizeof(struct sockaddr_in), | |
116 offsetof(struct sockaddr_in, sin_addr), | |
117 sizeof(struct in_addr), | |
118 }, | |
119 }; | |
120 const std::string kCanonicalName = "canonical.example.com"; | 113 const std::string kCanonicalName = "canonical.example.com"; |
121 | 114 |
122 // Construct a list of ip addresses. | 115 // Construct a list of ip addresses. |
123 IPAddressList ip_list; | 116 IPAddressList ip_list; |
124 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) { | 117 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) { |
125 IPAddressNumber ip_number; | 118 IPAddressNumber ip_number; |
126 ASSERT_TRUE(ParseIPLiteralToNumber(tests[i].ip_address, &ip_number)); | 119 ASSERT_TRUE(ParseIPLiteralToNumber(tests[i].ip_address, &ip_number)); |
127 ip_list.push_back(ip_number); | 120 ip_list.push_back(ip_number); |
128 } | 121 } |
129 | 122 |
130 AddressList test_list = AddressList::CreateFromIPAddressList(ip_list, | 123 AddressList test_list = |
131 kCanonicalName); | 124 AddressList::CreateFromIPAddressList(ip_list, kCanonicalName); |
132 std::string canonical_name; | 125 std::string canonical_name; |
133 EXPECT_EQ(kCanonicalName, test_list.canonical_name()); | 126 EXPECT_EQ(kCanonicalName, test_list.canonical_name()); |
134 EXPECT_EQ(ARRAYSIZE_UNSAFE(tests), test_list.size()); | 127 EXPECT_EQ(ARRAYSIZE_UNSAFE(tests), test_list.size()); |
135 } | 128 } |
136 | 129 |
137 } // namespace | 130 } // namespace |
138 } // namespace net | 131 } // namespace net |
OLD | NEW |