blob: 597f9110fc708df1bead4bee423ae717086ff0ee [file] [log] [blame]
Ben Chan3e4bf162013-04-03 18:14:51 -07001// 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 Chan11c213f2014-09-05 08:21:06 -07007#include <base/files/file_util.h>
Ben Chan3e4bf162013-04-03 18:14:51 -07008#include <base/files/scoped_temp_dir.h>
Ben Chana0ddf462014-02-06 11:32:42 -08009#include <base/strings/stringprintf.h>
Ben Chan320d04a2013-04-09 13:10:53 -070010#include <gmock/gmock.h>
Ben Chan3e4bf162013-04-03 18:14:51 -070011#include <gtest/gtest.h>
12
13using base::FilePath;
14using base::ScopedTempDir;
15using std::string;
16using std::vector;
Ben Chan320d04a2013-04-09 13:10:53 -070017using testing::Return;
Ben Chan3e4bf162013-04-03 18:14:51 -070018
19namespace shill {
20
21namespace {
22
Ben Chan320d04a2013-04-09 13:10:53 -070023const char kIPv4AddressAllZeros[] = "0.0.0.0";
24const char kIPv4AddressAllOnes[] = "255.255.255.255";
25const char kIPv4Address_127_0_0_1[] = "127.0.0.1";
26const char kIPv4Address_192_168_1_10[] = "192.168.1.10";
27const char kIPv6AddressAllZeros[] = "0000:0000:0000:0000:0000:0000:0000:0000";
28const char kIPv6AddressAllOnes[] = "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff";
29const char kIPv6AddressPattern1[] = "0123:4567:89ab:cdef:ffee:ddcc:bbaa:9988";
30
31const char *kIPv4SocketInfoLines[] = {
Ben Chan3e4bf162013-04-03 18:14:51 -070032 " 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 Chan320d04a2013-04-09 13:10:53 -070039const 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 Chan3e4bf162013-04-03 18:14:51 -070050
51} // namespace
52
Ben Chan320d04a2013-04-09 13:10:53 -070053class 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 Vakulenko8a532292014-06-16 17:18:44 -070058 MOCK_CONST_METHOD0(GetTcpv4SocketInfoFilePath, FilePath());
59 MOCK_CONST_METHOD0(GetTcpv6SocketInfoFilePath, FilePath());
Ben Chan320d04a2013-04-09 13:10:53 -070060};
61
Ben Chan3e4bf162013-04-03 18:14:51 -070062class 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 Chan320d04a2013-04-09 13:10:53 -070070 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 Chan3e4bf162013-04-03 18:14:51 -070076 void CreateSocketInfoFile(const char **lines, size_t num_lines,
77 const FilePath &dir_path, FilePath *file_path) {
Ben Chana0ddf462014-02-06 11:32:42 -080078 ASSERT_TRUE(base::CreateTemporaryFileInDir(dir_path, file_path));
Ben Chan3e4bf162013-04-03 18:14:51 -070079 for (size_t i = 0; i < num_lines; ++i) {
80 string line = lines[i];
81 line += '\n';
82 ASSERT_EQ(line.size(),
Ben Chan6fbf64f2014-05-21 18:07:01 -070083 base::AppendToFile(*file_path, line.data(), line.size()));
Ben Chan3e4bf162013-04-03 18:14:51 -070084 }
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 Chan320d04a2013-04-09 13:10:53 -070098 SocketInfoReaderUnderTest reader_;
Ben Chan3e4bf162013-04-03 18:14:51 -070099};
100
Ben Chan320d04a2013-04-09 13:10:53 -0700101TEST_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
158TEST_F(SocketInfoReaderTest, AppendSocketInfo) {
Ben Chan3e4bf162013-04-03 18:14:51 -0700159 FilePath file_path("/non-existent-file");
160 vector<SocketInfo> info_list;
161
Ben Chan320d04a2013-04-09 13:10:53 -0700162 EXPECT_FALSE(reader_.AppendSocketInfo(file_path, &info_list));
Ben Chan3e4bf162013-04-03 18:14:51 -0700163 EXPECT_TRUE(info_list.empty());
164
165 ScopedTempDir temp_dir;
166 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
167
Ben Chan320d04a2013-04-09 13:10:53 -0700168 CreateSocketInfoFile(kIPv4SocketInfoLines, 1, temp_dir.path(), &file_path);
169 EXPECT_TRUE(reader_.AppendSocketInfo(file_path, &info_list));
Ben Chan3e4bf162013-04-03 18:14:51 -0700170 EXPECT_TRUE(info_list.empty());
171
Ben Chan320d04a2013-04-09 13:10:53 -0700172 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 Chan3e4bf162013-04-03 18:14:51 -0700206 temp_dir.path(), &file_path);
Ben Chan320d04a2013-04-09 13:10:53 -0700207 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 Chan3e4bf162013-04-03 18:14:51 -0700222}
223
224TEST_F(SocketInfoReaderTest, ParseSocketInfo) {
225 SocketInfo info;
226
227 EXPECT_FALSE(reader_.ParseSocketInfo("", &info));
Ben Chan320d04a2013-04-09 13:10:53 -0700228 EXPECT_FALSE(reader_.ParseSocketInfo(kIPv4SocketInfoLines[0], &info));
Ben Chan3e4bf162013-04-03 18:14:51 -0700229
Ben Chan320d04a2013-04-09 13:10:53 -0700230 EXPECT_TRUE(reader_.ParseSocketInfo(kIPv4SocketInfoLines[1], &info));
Ben Chan3e4bf162013-04-03 18:14:51 -0700231 ExpectSocketInfoEqual(SocketInfo(SocketInfo::kConnectionStateListen,
Ben Chan320d04a2013-04-09 13:10:53 -0700232 StringToIPv4Address(kIPv4Address_127_0_0_1),
Ben Chan3e4bf162013-04-03 18:14:51 -0700233 25,
Ben Chan320d04a2013-04-09 13:10:53 -0700234 StringToIPv4Address(kIPv4AddressAllZeros),
Ben Chan3e4bf162013-04-03 18:14:51 -0700235 0,
236 10,
237 5,
238 SocketInfo::kTimerStateNoTimerPending),
239 info);
240}
241
242TEST_F(SocketInfoReaderTest, ParseIPAddressAndPort) {
243 IPAddress ip_address(IPAddress::kFamilyUnknown);
Ben Chan7fab8972014-08-10 17:14:46 -0700244 uint16_t port = 0;
Ben Chan3e4bf162013-04-03 18:14:51 -0700245
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 Chan320d04a2013-04-09 13:10:53 -0700250 EXPECT_FALSE(reader_.ParseIPAddressAndPort(
251 "0000000Y:0000", &ip_address, &port));
252 EXPECT_FALSE(reader_.ParseIPAddressAndPort(
253 "00000000:000Y", &ip_address, &port));
Ben Chan3e4bf162013-04-03 18:14:51 -0700254
Ben Chan320d04a2013-04-09 13:10:53 -0700255 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 Chan3e4bf162013-04-03 18:14:51 -0700268 EXPECT_EQ(80, port);
Ben Chan320d04a2013-04-09 13:10:53 -0700269
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 Chan3e4bf162013-04-03 18:14:51 -0700274}
275
276TEST_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 Chan320d04a2013-04-09 13:10:53 -0700283 EXPECT_FALSE(reader_.ParseIPAddress("0000000000000000000000000000000Y",
284 &ip_address));
Ben Chan3e4bf162013-04-03 18:14:51 -0700285
286 EXPECT_TRUE(reader_.ParseIPAddress("00000000", &ip_address));
Ben Chan320d04a2013-04-09 13:10:53 -0700287 EXPECT_TRUE(ip_address.Equals(StringToIPv4Address(kIPv4AddressAllZeros)));
Ben Chan3e4bf162013-04-03 18:14:51 -0700288
289 EXPECT_TRUE(reader_.ParseIPAddress("0100007F", &ip_address));
Ben Chan320d04a2013-04-09 13:10:53 -0700290 EXPECT_TRUE(ip_address.Equals(StringToIPv4Address(kIPv4Address_127_0_0_1)));
Ben Chan3e4bf162013-04-03 18:14:51 -0700291
292 EXPECT_TRUE(reader_.ParseIPAddress("0a01A8c0", &ip_address));
Ben Chan320d04a2013-04-09 13:10:53 -0700293 EXPECT_TRUE(ip_address.Equals(
294 StringToIPv4Address(kIPv4Address_192_168_1_10)));
Ben Chan3e4bf162013-04-03 18:14:51 -0700295
296 EXPECT_TRUE(reader_.ParseIPAddress("ffffffff", &ip_address));
297 EXPECT_TRUE(ip_address.Equals(
Ben Chan320d04a2013-04-09 13:10:53 -0700298 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 Chan3e4bf162013-04-03 18:14:51 -0700311}
312
313TEST_F(SocketInfoReaderTest, ParsePort) {
Ben Chan7fab8972014-08-10 17:14:46 -0700314 uint16_t port = 0;
Ben Chan3e4bf162013-04-03 18:14:51 -0700315
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
335TEST_F(SocketInfoReaderTest, ParseTransimitAndReceiveQueueValues) {
Ben Chan7fab8972014-08-10 17:14:46 -0700336 uint64_t transmit_queue_value = 0, receive_queue_value = 0;
Ben Chan3e4bf162013-04-03 18:14:51 -0700337
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
357TEST_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
397TEST_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