Ben Chan | 3e4bf16 | 2013-04-03 18:14:51 -0700 | [diff] [blame] | 1 | // Copyright (c) 2013 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 "shill/socket_info_reader.h" |
| 6 | |
Ben Chan | 11c213f | 2014-09-05 08:21:06 -0700 | [diff] [blame] | 7 | #include <base/files/file_util.h> |
Ben Chan | 3e4bf16 | 2013-04-03 18:14:51 -0700 | [diff] [blame] | 8 | #include <base/files/scoped_temp_dir.h> |
Ben Chan | a0ddf46 | 2014-02-06 11:32:42 -0800 | [diff] [blame] | 9 | #include <base/strings/stringprintf.h> |
Ben Chan | 320d04a | 2013-04-09 13:10:53 -0700 | [diff] [blame] | 10 | #include <gmock/gmock.h> |
Ben Chan | 3e4bf16 | 2013-04-03 18:14:51 -0700 | [diff] [blame] | 11 | #include <gtest/gtest.h> |
| 12 | |
| 13 | using base::FilePath; |
| 14 | using base::ScopedTempDir; |
| 15 | using std::string; |
| 16 | using std::vector; |
Ben Chan | 320d04a | 2013-04-09 13:10:53 -0700 | [diff] [blame] | 17 | using testing::Return; |
Ben Chan | 3e4bf16 | 2013-04-03 18:14:51 -0700 | [diff] [blame] | 18 | |
| 19 | namespace shill { |
| 20 | |
| 21 | namespace { |
| 22 | |
Ben Chan | 320d04a | 2013-04-09 13:10:53 -0700 | [diff] [blame] | 23 | const char kIPv4AddressAllZeros[] = "0.0.0.0"; |
| 24 | const char kIPv4AddressAllOnes[] = "255.255.255.255"; |
| 25 | const char kIPv4Address_127_0_0_1[] = "127.0.0.1"; |
| 26 | const char kIPv4Address_192_168_1_10[] = "192.168.1.10"; |
| 27 | const char kIPv6AddressAllZeros[] = "0000:0000:0000:0000:0000:0000:0000:0000"; |
| 28 | const char kIPv6AddressAllOnes[] = "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"; |
| 29 | const char kIPv6AddressPattern1[] = "0123:4567:89ab:cdef:ffee:ddcc:bbaa:9988"; |
| 30 | |
| 31 | const char *kIPv4SocketInfoLines[] = { |
Ben Chan | 3e4bf16 | 2013-04-03 18:14:51 -0700 | [diff] [blame] | 32 | " sl local_address rem_address st tx_queue rx_queue tr tm->when " |
| 33 | "retrnsmt uid timeout inode ", |
| 34 | " 0: 0100007F:0019 00000000:0000 0A 0000000A:00000005 00:00000000 " |
| 35 | "00000000 0 0 36948 1 0000000000000000 100 0 0 10 -1 ", |
| 36 | " 1: 0A01A8C0:0050 0100007F:03FC 01 00000000:00000000 00:00000000 " |
| 37 | "00000000 65534 0 2787034 1 0000000000000000 100 0 0 10 -1 ", |
| 38 | }; |
Ben Chan | 320d04a | 2013-04-09 13:10:53 -0700 | [diff] [blame] | 39 | const char *kIPv6SocketInfoLines[] = { |
| 40 | " sl local_address " |
| 41 | "remote_address st tx_queue rx_queue tr tm->when " |
| 42 | "retrnsmt uid timeout inode", |
| 43 | " 0: 67452301EFCDAB89CCDDEEFF8899AABB:0019 " |
| 44 | "00000000000000000000000000000000:0000 0A 0000000A:00000005 00:00000000 " |
| 45 | "00000000 0 0 36412 1 0000000000000000 100 0 0 2 -1", |
| 46 | " 1: 00000000000000000000000000000000:0050 " |
| 47 | "67452301EFCDAB89CCDDEEFF8899AABB:03FC 01 00000000:00000000 00:00000000 " |
| 48 | "00000000 0 0 36412 1 0000000000000000 100 0 0 2 -1", |
| 49 | }; |
Ben Chan | 3e4bf16 | 2013-04-03 18:14:51 -0700 | [diff] [blame] | 50 | |
| 51 | } // namespace |
| 52 | |
Ben Chan | 320d04a | 2013-04-09 13:10:53 -0700 | [diff] [blame] | 53 | class SocketInfoReaderUnderTest : public SocketInfoReader { |
| 54 | public: |
| 55 | // Mock out GetTcpv4SocketInfoFilePath and GetTcpv6SocketInfoFilePath to |
| 56 | // use a temporary created socket info file instead of the actual path |
| 57 | // in procfs (i.e. /proc/net/tcp and /proc/net/tcp6). |
Alex Vakulenko | 8a53229 | 2014-06-16 17:18:44 -0700 | [diff] [blame] | 58 | MOCK_CONST_METHOD0(GetTcpv4SocketInfoFilePath, FilePath()); |
| 59 | MOCK_CONST_METHOD0(GetTcpv6SocketInfoFilePath, FilePath()); |
Ben Chan | 320d04a | 2013-04-09 13:10:53 -0700 | [diff] [blame] | 60 | }; |
| 61 | |
Ben Chan | 3e4bf16 | 2013-04-03 18:14:51 -0700 | [diff] [blame] | 62 | class SocketInfoReaderTest : public testing::Test { |
| 63 | protected: |
| 64 | IPAddress StringToIPv4Address(const string &address_string) { |
| 65 | IPAddress ip_address(IPAddress::kFamilyIPv4); |
| 66 | EXPECT_TRUE(ip_address.SetAddressFromString(address_string)); |
| 67 | return ip_address; |
| 68 | } |
| 69 | |
Ben Chan | 320d04a | 2013-04-09 13:10:53 -0700 | [diff] [blame] | 70 | IPAddress StringToIPv6Address(const string &address_string) { |
| 71 | IPAddress ip_address(IPAddress::kFamilyIPv6); |
| 72 | EXPECT_TRUE(ip_address.SetAddressFromString(address_string)); |
| 73 | return ip_address; |
| 74 | } |
| 75 | |
Ben Chan | 3e4bf16 | 2013-04-03 18:14:51 -0700 | [diff] [blame] | 76 | void CreateSocketInfoFile(const char **lines, size_t num_lines, |
| 77 | const FilePath &dir_path, FilePath *file_path) { |
Ben Chan | a0ddf46 | 2014-02-06 11:32:42 -0800 | [diff] [blame] | 78 | ASSERT_TRUE(base::CreateTemporaryFileInDir(dir_path, file_path)); |
Ben Chan | 3e4bf16 | 2013-04-03 18:14:51 -0700 | [diff] [blame] | 79 | for (size_t i = 0; i < num_lines; ++i) { |
| 80 | string line = lines[i]; |
| 81 | line += '\n'; |
| 82 | ASSERT_EQ(line.size(), |
Ben Chan | 6fbf64f | 2014-05-21 18:07:01 -0700 | [diff] [blame] | 83 | base::AppendToFile(*file_path, line.data(), line.size())); |
Ben Chan | 3e4bf16 | 2013-04-03 18:14:51 -0700 | [diff] [blame] | 84 | } |
| 85 | } |
| 86 | |
| 87 | void ExpectSocketInfoEqual(const SocketInfo &info1, const SocketInfo &info2) { |
| 88 | EXPECT_EQ(info1.connection_state(), info2.connection_state()); |
| 89 | EXPECT_TRUE(info1.local_ip_address().Equals(info2.local_ip_address())); |
| 90 | EXPECT_EQ(info1.local_port(), info2.local_port()); |
| 91 | EXPECT_TRUE(info1.remote_ip_address().Equals(info2.remote_ip_address())); |
| 92 | EXPECT_EQ(info1.remote_port(), info2.remote_port()); |
| 93 | EXPECT_EQ(info1.transmit_queue_value(), info2.transmit_queue_value()); |
| 94 | EXPECT_EQ(info1.receive_queue_value(), info2.receive_queue_value()); |
| 95 | EXPECT_EQ(info1.timer_state(), info2.timer_state()); |
| 96 | } |
| 97 | |
Ben Chan | 320d04a | 2013-04-09 13:10:53 -0700 | [diff] [blame] | 98 | SocketInfoReaderUnderTest reader_; |
Ben Chan | 3e4bf16 | 2013-04-03 18:14:51 -0700 | [diff] [blame] | 99 | }; |
| 100 | |
Ben Chan | 320d04a | 2013-04-09 13:10:53 -0700 | [diff] [blame] | 101 | TEST_F(SocketInfoReaderTest, LoadTcpSocketInfo) { |
| 102 | FilePath invalid_path("/non-existent-file"), v4_path, v6_path; |
| 103 | ScopedTempDir temp_dir; |
| 104 | ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 105 | CreateSocketInfoFile(kIPv4SocketInfoLines, 2, temp_dir.path(), &v4_path); |
| 106 | CreateSocketInfoFile(kIPv6SocketInfoLines, 2, temp_dir.path(), &v6_path); |
| 107 | |
| 108 | SocketInfo v4_info(SocketInfo::kConnectionStateListen, |
| 109 | StringToIPv4Address(kIPv4Address_127_0_0_1), |
| 110 | 25, |
| 111 | StringToIPv4Address(kIPv4AddressAllZeros), |
| 112 | 0, |
| 113 | 10, |
| 114 | 5, |
| 115 | SocketInfo::kTimerStateNoTimerPending); |
| 116 | SocketInfo v6_info(SocketInfo::kConnectionStateListen, |
| 117 | StringToIPv6Address(kIPv6AddressPattern1), |
| 118 | 25, |
| 119 | StringToIPv6Address(kIPv6AddressAllZeros), |
| 120 | 0, |
| 121 | 10, |
| 122 | 5, |
| 123 | SocketInfo::kTimerStateNoTimerPending); |
| 124 | |
| 125 | vector<SocketInfo> info_list; |
| 126 | EXPECT_CALL(reader_, GetTcpv4SocketInfoFilePath()) |
| 127 | .WillOnce(Return(invalid_path)); |
| 128 | EXPECT_CALL(reader_, GetTcpv6SocketInfoFilePath()) |
| 129 | .WillOnce(Return(invalid_path)); |
| 130 | EXPECT_FALSE(reader_.LoadTcpSocketInfo(&info_list)); |
| 131 | |
| 132 | EXPECT_CALL(reader_, GetTcpv4SocketInfoFilePath()) |
| 133 | .WillOnce(Return(v4_path)); |
| 134 | EXPECT_CALL(reader_, GetTcpv6SocketInfoFilePath()) |
| 135 | .WillOnce(Return(invalid_path)); |
| 136 | EXPECT_TRUE(reader_.LoadTcpSocketInfo(&info_list)); |
| 137 | EXPECT_EQ(1, info_list.size()); |
| 138 | ExpectSocketInfoEqual(v4_info, info_list[0]); |
| 139 | |
| 140 | EXPECT_CALL(reader_, GetTcpv4SocketInfoFilePath()) |
| 141 | .WillOnce(Return(invalid_path)); |
| 142 | EXPECT_CALL(reader_, GetTcpv6SocketInfoFilePath()) |
| 143 | .WillOnce(Return(v6_path)); |
| 144 | EXPECT_TRUE(reader_.LoadTcpSocketInfo(&info_list)); |
| 145 | EXPECT_EQ(1, info_list.size()); |
| 146 | ExpectSocketInfoEqual(v6_info, info_list[0]); |
| 147 | |
| 148 | EXPECT_CALL(reader_, GetTcpv4SocketInfoFilePath()) |
| 149 | .WillOnce(Return(v4_path)); |
| 150 | EXPECT_CALL(reader_, GetTcpv6SocketInfoFilePath()) |
| 151 | .WillOnce(Return(v6_path)); |
| 152 | EXPECT_TRUE(reader_.LoadTcpSocketInfo(&info_list)); |
| 153 | EXPECT_EQ(2, info_list.size()); |
| 154 | ExpectSocketInfoEqual(v4_info, info_list[0]); |
| 155 | ExpectSocketInfoEqual(v6_info, info_list[1]); |
| 156 | } |
| 157 | |
| 158 | TEST_F(SocketInfoReaderTest, AppendSocketInfo) { |
Ben Chan | 3e4bf16 | 2013-04-03 18:14:51 -0700 | [diff] [blame] | 159 | FilePath file_path("/non-existent-file"); |
| 160 | vector<SocketInfo> info_list; |
| 161 | |
Ben Chan | 320d04a | 2013-04-09 13:10:53 -0700 | [diff] [blame] | 162 | EXPECT_FALSE(reader_.AppendSocketInfo(file_path, &info_list)); |
Ben Chan | 3e4bf16 | 2013-04-03 18:14:51 -0700 | [diff] [blame] | 163 | EXPECT_TRUE(info_list.empty()); |
| 164 | |
| 165 | ScopedTempDir temp_dir; |
| 166 | ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 167 | |
Ben Chan | 320d04a | 2013-04-09 13:10:53 -0700 | [diff] [blame] | 168 | CreateSocketInfoFile(kIPv4SocketInfoLines, 1, temp_dir.path(), &file_path); |
| 169 | EXPECT_TRUE(reader_.AppendSocketInfo(file_path, &info_list)); |
Ben Chan | 3e4bf16 | 2013-04-03 18:14:51 -0700 | [diff] [blame] | 170 | EXPECT_TRUE(info_list.empty()); |
| 171 | |
Ben Chan | 320d04a | 2013-04-09 13:10:53 -0700 | [diff] [blame] | 172 | SocketInfo v4_info1(SocketInfo::kConnectionStateListen, |
| 173 | StringToIPv4Address(kIPv4Address_127_0_0_1), |
| 174 | 25, |
| 175 | StringToIPv4Address(kIPv4AddressAllZeros), |
| 176 | 0, |
| 177 | 10, |
| 178 | 5, |
| 179 | SocketInfo::kTimerStateNoTimerPending); |
| 180 | SocketInfo v4_info2(SocketInfo::kConnectionStateEstablished, |
| 181 | StringToIPv4Address(kIPv4Address_192_168_1_10), |
| 182 | 80, |
| 183 | StringToIPv4Address(kIPv4Address_127_0_0_1), |
| 184 | 1020, |
| 185 | 0, |
| 186 | 0, |
| 187 | SocketInfo::kTimerStateNoTimerPending); |
| 188 | SocketInfo v6_info1(SocketInfo::kConnectionStateListen, |
| 189 | StringToIPv6Address(kIPv6AddressPattern1), |
| 190 | 25, |
| 191 | StringToIPv6Address(kIPv6AddressAllZeros), |
| 192 | 0, |
| 193 | 10, |
| 194 | 5, |
| 195 | SocketInfo::kTimerStateNoTimerPending); |
| 196 | SocketInfo v6_info2(SocketInfo::kConnectionStateEstablished, |
| 197 | StringToIPv6Address(kIPv6AddressAllZeros), |
| 198 | 80, |
| 199 | StringToIPv6Address(kIPv6AddressPattern1), |
| 200 | 1020, |
| 201 | 0, |
| 202 | 0, |
| 203 | SocketInfo::kTimerStateNoTimerPending); |
| 204 | |
| 205 | CreateSocketInfoFile(kIPv4SocketInfoLines, arraysize(kIPv4SocketInfoLines), |
Ben Chan | 3e4bf16 | 2013-04-03 18:14:51 -0700 | [diff] [blame] | 206 | temp_dir.path(), &file_path); |
Ben Chan | 320d04a | 2013-04-09 13:10:53 -0700 | [diff] [blame] | 207 | EXPECT_TRUE(reader_.AppendSocketInfo(file_path, &info_list)); |
| 208 | EXPECT_EQ(arraysize(kIPv4SocketInfoLines) - 1, info_list.size()); |
| 209 | ExpectSocketInfoEqual(v4_info1, info_list[0]); |
| 210 | ExpectSocketInfoEqual(v4_info2, info_list[1]); |
| 211 | |
| 212 | CreateSocketInfoFile(kIPv6SocketInfoLines, arraysize(kIPv6SocketInfoLines), |
| 213 | temp_dir.path(), &file_path); |
| 214 | EXPECT_TRUE(reader_.AppendSocketInfo(file_path, &info_list)); |
| 215 | EXPECT_EQ( |
| 216 | arraysize(kIPv4SocketInfoLines) + arraysize(kIPv6SocketInfoLines) - 2, |
| 217 | info_list.size()); |
| 218 | ExpectSocketInfoEqual(v4_info1, info_list[0]); |
| 219 | ExpectSocketInfoEqual(v4_info2, info_list[1]); |
| 220 | ExpectSocketInfoEqual(v6_info1, info_list[2]); |
| 221 | ExpectSocketInfoEqual(v6_info2, info_list[3]); |
Ben Chan | 3e4bf16 | 2013-04-03 18:14:51 -0700 | [diff] [blame] | 222 | } |
| 223 | |
| 224 | TEST_F(SocketInfoReaderTest, ParseSocketInfo) { |
| 225 | SocketInfo info; |
| 226 | |
| 227 | EXPECT_FALSE(reader_.ParseSocketInfo("", &info)); |
Ben Chan | 320d04a | 2013-04-09 13:10:53 -0700 | [diff] [blame] | 228 | EXPECT_FALSE(reader_.ParseSocketInfo(kIPv4SocketInfoLines[0], &info)); |
Ben Chan | 3e4bf16 | 2013-04-03 18:14:51 -0700 | [diff] [blame] | 229 | |
Ben Chan | 320d04a | 2013-04-09 13:10:53 -0700 | [diff] [blame] | 230 | EXPECT_TRUE(reader_.ParseSocketInfo(kIPv4SocketInfoLines[1], &info)); |
Ben Chan | 3e4bf16 | 2013-04-03 18:14:51 -0700 | [diff] [blame] | 231 | ExpectSocketInfoEqual(SocketInfo(SocketInfo::kConnectionStateListen, |
Ben Chan | 320d04a | 2013-04-09 13:10:53 -0700 | [diff] [blame] | 232 | StringToIPv4Address(kIPv4Address_127_0_0_1), |
Ben Chan | 3e4bf16 | 2013-04-03 18:14:51 -0700 | [diff] [blame] | 233 | 25, |
Ben Chan | 320d04a | 2013-04-09 13:10:53 -0700 | [diff] [blame] | 234 | StringToIPv4Address(kIPv4AddressAllZeros), |
Ben Chan | 3e4bf16 | 2013-04-03 18:14:51 -0700 | [diff] [blame] | 235 | 0, |
| 236 | 10, |
| 237 | 5, |
| 238 | SocketInfo::kTimerStateNoTimerPending), |
| 239 | info); |
| 240 | } |
| 241 | |
| 242 | TEST_F(SocketInfoReaderTest, ParseIPAddressAndPort) { |
| 243 | IPAddress ip_address(IPAddress::kFamilyUnknown); |
Ben Chan | 7fab897 | 2014-08-10 17:14:46 -0700 | [diff] [blame] | 244 | uint16_t port = 0; |
Ben Chan | 3e4bf16 | 2013-04-03 18:14:51 -0700 | [diff] [blame] | 245 | |
| 246 | EXPECT_FALSE(reader_.ParseIPAddressAndPort("", &ip_address, &port)); |
| 247 | EXPECT_FALSE(reader_.ParseIPAddressAndPort("00000000", &ip_address, &port)); |
| 248 | EXPECT_FALSE(reader_.ParseIPAddressAndPort("00000000:", &ip_address, &port)); |
| 249 | EXPECT_FALSE(reader_.ParseIPAddressAndPort(":0000", &ip_address, &port)); |
Ben Chan | 320d04a | 2013-04-09 13:10:53 -0700 | [diff] [blame] | 250 | EXPECT_FALSE(reader_.ParseIPAddressAndPort( |
| 251 | "0000000Y:0000", &ip_address, &port)); |
| 252 | EXPECT_FALSE(reader_.ParseIPAddressAndPort( |
| 253 | "00000000:000Y", &ip_address, &port)); |
Ben Chan | 3e4bf16 | 2013-04-03 18:14:51 -0700 | [diff] [blame] | 254 | |
Ben Chan | 320d04a | 2013-04-09 13:10:53 -0700 | [diff] [blame] | 255 | EXPECT_FALSE(reader_.ParseIPAddressAndPort( |
| 256 | "00000000000000000000000000000000", &ip_address, &port)); |
| 257 | EXPECT_FALSE(reader_.ParseIPAddressAndPort( |
| 258 | "00000000000000000000000000000000:", &ip_address, &port)); |
| 259 | EXPECT_FALSE(reader_.ParseIPAddressAndPort( |
| 260 | "00000000000000000000000000000000Y:0000", &ip_address, &port)); |
| 261 | EXPECT_FALSE(reader_.ParseIPAddressAndPort( |
| 262 | "000000000000000000000000000000000:000Y", &ip_address, &port)); |
| 263 | |
| 264 | EXPECT_TRUE(reader_.ParseIPAddressAndPort( |
| 265 | "0a01A8c0:0050", &ip_address, &port)); |
| 266 | EXPECT_TRUE(ip_address.Equals( |
| 267 | StringToIPv4Address(kIPv4Address_192_168_1_10))); |
Ben Chan | 3e4bf16 | 2013-04-03 18:14:51 -0700 | [diff] [blame] | 268 | EXPECT_EQ(80, port); |
Ben Chan | 320d04a | 2013-04-09 13:10:53 -0700 | [diff] [blame] | 269 | |
| 270 | EXPECT_TRUE(reader_.ParseIPAddressAndPort( |
| 271 | "67452301efcdab89CCDDEEFF8899AABB:1F90", &ip_address, &port)); |
| 272 | EXPECT_TRUE(ip_address.Equals(StringToIPv6Address(kIPv6AddressPattern1))); |
| 273 | EXPECT_EQ(8080, port); |
Ben Chan | 3e4bf16 | 2013-04-03 18:14:51 -0700 | [diff] [blame] | 274 | } |
| 275 | |
| 276 | TEST_F(SocketInfoReaderTest, ParseIPAddress) { |
| 277 | IPAddress ip_address(IPAddress::kFamilyUnknown); |
| 278 | |
| 279 | EXPECT_FALSE(reader_.ParseIPAddress("", &ip_address)); |
| 280 | EXPECT_FALSE(reader_.ParseIPAddress("0", &ip_address)); |
| 281 | EXPECT_FALSE(reader_.ParseIPAddress("00", &ip_address)); |
| 282 | EXPECT_FALSE(reader_.ParseIPAddress("0000000Y", &ip_address)); |
Ben Chan | 320d04a | 2013-04-09 13:10:53 -0700 | [diff] [blame] | 283 | EXPECT_FALSE(reader_.ParseIPAddress("0000000000000000000000000000000Y", |
| 284 | &ip_address)); |
Ben Chan | 3e4bf16 | 2013-04-03 18:14:51 -0700 | [diff] [blame] | 285 | |
| 286 | EXPECT_TRUE(reader_.ParseIPAddress("00000000", &ip_address)); |
Ben Chan | 320d04a | 2013-04-09 13:10:53 -0700 | [diff] [blame] | 287 | EXPECT_TRUE(ip_address.Equals(StringToIPv4Address(kIPv4AddressAllZeros))); |
Ben Chan | 3e4bf16 | 2013-04-03 18:14:51 -0700 | [diff] [blame] | 288 | |
| 289 | EXPECT_TRUE(reader_.ParseIPAddress("0100007F", &ip_address)); |
Ben Chan | 320d04a | 2013-04-09 13:10:53 -0700 | [diff] [blame] | 290 | EXPECT_TRUE(ip_address.Equals(StringToIPv4Address(kIPv4Address_127_0_0_1))); |
Ben Chan | 3e4bf16 | 2013-04-03 18:14:51 -0700 | [diff] [blame] | 291 | |
| 292 | EXPECT_TRUE(reader_.ParseIPAddress("0a01A8c0", &ip_address)); |
Ben Chan | 320d04a | 2013-04-09 13:10:53 -0700 | [diff] [blame] | 293 | EXPECT_TRUE(ip_address.Equals( |
| 294 | StringToIPv4Address(kIPv4Address_192_168_1_10))); |
Ben Chan | 3e4bf16 | 2013-04-03 18:14:51 -0700 | [diff] [blame] | 295 | |
| 296 | EXPECT_TRUE(reader_.ParseIPAddress("ffffffff", &ip_address)); |
| 297 | EXPECT_TRUE(ip_address.Equals( |
Ben Chan | 320d04a | 2013-04-09 13:10:53 -0700 | [diff] [blame] | 298 | StringToIPv4Address(kIPv4AddressAllOnes))); |
| 299 | |
| 300 | EXPECT_TRUE(reader_.ParseIPAddress("00000000000000000000000000000000", |
| 301 | &ip_address)); |
| 302 | EXPECT_TRUE(ip_address.Equals(StringToIPv6Address(kIPv6AddressAllZeros))); |
| 303 | |
| 304 | EXPECT_TRUE(reader_.ParseIPAddress("67452301efcdab89CCDDEEFF8899AABB", |
| 305 | &ip_address)); |
| 306 | EXPECT_TRUE(ip_address.Equals(StringToIPv6Address(kIPv6AddressPattern1))); |
| 307 | |
| 308 | EXPECT_TRUE(reader_.ParseIPAddress("ffffffffffffffffffffffffffffffff", |
| 309 | &ip_address)); |
| 310 | EXPECT_TRUE(ip_address.Equals(StringToIPv6Address(kIPv6AddressAllOnes))); |
Ben Chan | 3e4bf16 | 2013-04-03 18:14:51 -0700 | [diff] [blame] | 311 | } |
| 312 | |
| 313 | TEST_F(SocketInfoReaderTest, ParsePort) { |
Ben Chan | 7fab897 | 2014-08-10 17:14:46 -0700 | [diff] [blame] | 314 | uint16_t port = 0; |
Ben Chan | 3e4bf16 | 2013-04-03 18:14:51 -0700 | [diff] [blame] | 315 | |
| 316 | EXPECT_FALSE(reader_.ParsePort("", &port)); |
| 317 | EXPECT_FALSE(reader_.ParsePort("0", &port)); |
| 318 | EXPECT_FALSE(reader_.ParsePort("00", &port)); |
| 319 | EXPECT_FALSE(reader_.ParsePort("000", &port)); |
| 320 | EXPECT_FALSE(reader_.ParsePort("000Y", &port)); |
| 321 | |
| 322 | EXPECT_TRUE(reader_.ParsePort("0000", &port)); |
| 323 | EXPECT_EQ(0, port); |
| 324 | |
| 325 | EXPECT_TRUE(reader_.ParsePort("0050", &port)); |
| 326 | EXPECT_EQ(80, port); |
| 327 | |
| 328 | EXPECT_TRUE(reader_.ParsePort("abCD", &port)); |
| 329 | EXPECT_EQ(43981, port); |
| 330 | |
| 331 | EXPECT_TRUE(reader_.ParsePort("ffff", &port)); |
| 332 | EXPECT_EQ(65535, port); |
| 333 | } |
| 334 | |
| 335 | TEST_F(SocketInfoReaderTest, ParseTransimitAndReceiveQueueValues) { |
Ben Chan | 7fab897 | 2014-08-10 17:14:46 -0700 | [diff] [blame] | 336 | uint64_t transmit_queue_value = 0, receive_queue_value = 0; |
Ben Chan | 3e4bf16 | 2013-04-03 18:14:51 -0700 | [diff] [blame] | 337 | |
| 338 | EXPECT_FALSE(reader_.ParseTransimitAndReceiveQueueValues( |
| 339 | "", &transmit_queue_value, &receive_queue_value)); |
| 340 | EXPECT_FALSE(reader_.ParseTransimitAndReceiveQueueValues( |
| 341 | "00000000", &transmit_queue_value, &receive_queue_value)); |
| 342 | EXPECT_FALSE(reader_.ParseTransimitAndReceiveQueueValues( |
| 343 | "00000000:", &transmit_queue_value, &receive_queue_value)); |
| 344 | EXPECT_FALSE(reader_.ParseTransimitAndReceiveQueueValues( |
| 345 | ":00000000", &transmit_queue_value, &receive_queue_value)); |
| 346 | EXPECT_FALSE(reader_.ParseTransimitAndReceiveQueueValues( |
| 347 | "0000000Y:00000000", &transmit_queue_value, &receive_queue_value)); |
| 348 | EXPECT_FALSE(reader_.ParseTransimitAndReceiveQueueValues( |
| 349 | "00000000:0000000Y", &transmit_queue_value, &receive_queue_value)); |
| 350 | |
| 351 | EXPECT_TRUE(reader_.ParseTransimitAndReceiveQueueValues( |
| 352 | "00000001:FFFFFFFF", &transmit_queue_value, &receive_queue_value)); |
| 353 | EXPECT_EQ(1, transmit_queue_value); |
| 354 | EXPECT_EQ(0xffffffff, receive_queue_value); |
| 355 | } |
| 356 | |
| 357 | TEST_F(SocketInfoReaderTest, ParseConnectionState) { |
| 358 | SocketInfo::ConnectionState connection_state = |
| 359 | SocketInfo::kConnectionStateUnknown; |
| 360 | |
| 361 | EXPECT_FALSE(reader_.ParseConnectionState("", &connection_state)); |
| 362 | EXPECT_FALSE(reader_.ParseConnectionState("0", &connection_state)); |
| 363 | EXPECT_FALSE(reader_.ParseConnectionState("X", &connection_state)); |
| 364 | |
| 365 | EXPECT_TRUE(reader_.ParseConnectionState("00", &connection_state)); |
| 366 | EXPECT_EQ(SocketInfo::kConnectionStateUnknown, connection_state); |
| 367 | EXPECT_TRUE(reader_.ParseConnectionState("01", &connection_state)); |
| 368 | EXPECT_EQ(SocketInfo::kConnectionStateEstablished, connection_state); |
| 369 | EXPECT_TRUE(reader_.ParseConnectionState("02", &connection_state)); |
| 370 | EXPECT_EQ(SocketInfo::kConnectionStateSynSent, connection_state); |
| 371 | EXPECT_TRUE(reader_.ParseConnectionState("03", &connection_state)); |
| 372 | EXPECT_EQ(SocketInfo::kConnectionStateSynRecv, connection_state); |
| 373 | EXPECT_TRUE(reader_.ParseConnectionState("04", &connection_state)); |
| 374 | EXPECT_EQ(SocketInfo::kConnectionStateFinWait1, connection_state); |
| 375 | EXPECT_TRUE(reader_.ParseConnectionState("05", &connection_state)); |
| 376 | EXPECT_EQ(SocketInfo::kConnectionStateFinWait2, connection_state); |
| 377 | EXPECT_TRUE(reader_.ParseConnectionState("06", &connection_state)); |
| 378 | EXPECT_EQ(SocketInfo::kConnectionStateTimeWait, connection_state); |
| 379 | EXPECT_TRUE(reader_.ParseConnectionState("07", &connection_state)); |
| 380 | EXPECT_EQ(SocketInfo::kConnectionStateClose, connection_state); |
| 381 | EXPECT_TRUE(reader_.ParseConnectionState("08", &connection_state)); |
| 382 | EXPECT_EQ(SocketInfo::kConnectionStateCloseWait, connection_state); |
| 383 | EXPECT_TRUE(reader_.ParseConnectionState("09", &connection_state)); |
| 384 | EXPECT_EQ(SocketInfo::kConnectionStateLastAck, connection_state); |
| 385 | EXPECT_TRUE(reader_.ParseConnectionState("0A", &connection_state)); |
| 386 | EXPECT_EQ(SocketInfo::kConnectionStateListen, connection_state); |
| 387 | EXPECT_TRUE(reader_.ParseConnectionState("0B", &connection_state)); |
| 388 | EXPECT_EQ(SocketInfo::kConnectionStateClosing, connection_state); |
| 389 | |
| 390 | for (int i = SocketInfo::kConnectionStateMax; i < 256; ++i) { |
| 391 | EXPECT_TRUE(reader_.ParseConnectionState( |
| 392 | base::StringPrintf("%02X", i), &connection_state)); |
| 393 | EXPECT_EQ(SocketInfo::kConnectionStateUnknown, connection_state); |
| 394 | } |
| 395 | } |
| 396 | |
| 397 | TEST_F(SocketInfoReaderTest, ParseTimerState) { |
| 398 | SocketInfo::TimerState timer_state = SocketInfo::kTimerStateUnknown; |
| 399 | |
| 400 | EXPECT_FALSE(reader_.ParseTimerState("", &timer_state)); |
| 401 | EXPECT_FALSE(reader_.ParseTimerState("0", &timer_state)); |
| 402 | EXPECT_FALSE(reader_.ParseTimerState("X", &timer_state)); |
| 403 | EXPECT_FALSE(reader_.ParseTimerState("00", &timer_state)); |
| 404 | |
| 405 | EXPECT_TRUE(reader_.ParseTimerState("00:00000000", &timer_state)); |
| 406 | EXPECT_EQ(SocketInfo::kTimerStateNoTimerPending, timer_state); |
| 407 | EXPECT_TRUE(reader_.ParseTimerState("01:00000000", &timer_state)); |
| 408 | EXPECT_EQ(SocketInfo::kTimerStateRetransmitTimerPending, timer_state); |
| 409 | EXPECT_TRUE(reader_.ParseTimerState("02:00000000", &timer_state)); |
| 410 | EXPECT_EQ(SocketInfo::kTimerStateAnotherTimerPending, timer_state); |
| 411 | EXPECT_TRUE(reader_.ParseTimerState("03:00000000", &timer_state)); |
| 412 | EXPECT_EQ(SocketInfo::kTimerStateInTimeWaitState, timer_state); |
| 413 | EXPECT_TRUE(reader_.ParseTimerState("04:00000000", &timer_state)); |
| 414 | EXPECT_EQ(SocketInfo::kTimerStateZeroWindowProbeTimerPending, timer_state); |
| 415 | |
| 416 | for (int i = SocketInfo::kTimerStateMax; i < 256; ++i) { |
| 417 | EXPECT_TRUE(reader_.ParseTimerState( |
| 418 | base::StringPrintf("%02X:00000000", i), &timer_state)); |
| 419 | EXPECT_EQ(SocketInfo::kTimerStateUnknown, timer_state); |
| 420 | } |
| 421 | } |
| 422 | |
| 423 | } // namespace shill |