cc: Split out BeginFrame needed verse proactive for commits.
[chromium-blink-merge.git] / chromeos / network / network_util_unittest.cc
blob9ccf8e623fda3e0037bff40769844970a928661d
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
3 // found in the LICENSE file.
5 #include "chromeos/network/network_util.h"
7 #include "testing/gtest/include/gtest/gtest.h"
8 #include "third_party/cros_system_api/dbus/service_constants.h"
10 using chromeos::CellularScanResult;
11 using chromeos::network_util::NetmaskToPrefixLength;
12 using chromeos::network_util::PrefixLengthToNetmask;
13 using chromeos::network_util::ParseCellularScanResults;
15 typedef testing::Test NetworkUtilTest;
17 TEST_F(NetworkUtilTest, NetmaskToPrefixLength) {
18 // Valid netmasks
19 EXPECT_EQ(32, NetmaskToPrefixLength("255.255.255.255"));
20 EXPECT_EQ(31, NetmaskToPrefixLength("255.255.255.254"));
21 EXPECT_EQ(30, NetmaskToPrefixLength("255.255.255.252"));
22 EXPECT_EQ(29, NetmaskToPrefixLength("255.255.255.248"));
23 EXPECT_EQ(28, NetmaskToPrefixLength("255.255.255.240"));
24 EXPECT_EQ(27, NetmaskToPrefixLength("255.255.255.224"));
25 EXPECT_EQ(26, NetmaskToPrefixLength("255.255.255.192"));
26 EXPECT_EQ(25, NetmaskToPrefixLength("255.255.255.128"));
27 EXPECT_EQ(24, NetmaskToPrefixLength("255.255.255.0"));
28 EXPECT_EQ(23, NetmaskToPrefixLength("255.255.254.0"));
29 EXPECT_EQ(22, NetmaskToPrefixLength("255.255.252.0"));
30 EXPECT_EQ(21, NetmaskToPrefixLength("255.255.248.0"));
31 EXPECT_EQ(20, NetmaskToPrefixLength("255.255.240.0"));
32 EXPECT_EQ(19, NetmaskToPrefixLength("255.255.224.0"));
33 EXPECT_EQ(18, NetmaskToPrefixLength("255.255.192.0"));
34 EXPECT_EQ(17, NetmaskToPrefixLength("255.255.128.0"));
35 EXPECT_EQ(16, NetmaskToPrefixLength("255.255.0.0"));
36 EXPECT_EQ(15, NetmaskToPrefixLength("255.254.0.0"));
37 EXPECT_EQ(14, NetmaskToPrefixLength("255.252.0.0"));
38 EXPECT_EQ(13, NetmaskToPrefixLength("255.248.0.0"));
39 EXPECT_EQ(12, NetmaskToPrefixLength("255.240.0.0"));
40 EXPECT_EQ(11, NetmaskToPrefixLength("255.224.0.0"));
41 EXPECT_EQ(10, NetmaskToPrefixLength("255.192.0.0"));
42 EXPECT_EQ(9, NetmaskToPrefixLength("255.128.0.0"));
43 EXPECT_EQ(8, NetmaskToPrefixLength("255.0.0.0"));
44 EXPECT_EQ(7, NetmaskToPrefixLength("254.0.0.0"));
45 EXPECT_EQ(6, NetmaskToPrefixLength("252.0.0.0"));
46 EXPECT_EQ(5, NetmaskToPrefixLength("248.0.0.0"));
47 EXPECT_EQ(4, NetmaskToPrefixLength("240.0.0.0"));
48 EXPECT_EQ(3, NetmaskToPrefixLength("224.0.0.0"));
49 EXPECT_EQ(2, NetmaskToPrefixLength("192.0.0.0"));
50 EXPECT_EQ(1, NetmaskToPrefixLength("128.0.0.0"));
51 EXPECT_EQ(0, NetmaskToPrefixLength("0.0.0.0"));
52 // Invalid netmasks
53 EXPECT_EQ(-1, NetmaskToPrefixLength("255.255.255"));
54 EXPECT_EQ(-1, NetmaskToPrefixLength("255.255.255.255.255"));
55 EXPECT_EQ(-1, NetmaskToPrefixLength("255.255.255.255.0"));
56 EXPECT_EQ(-1, NetmaskToPrefixLength("255.255.255.256"));
57 EXPECT_EQ(-1, NetmaskToPrefixLength("255.255.255.1"));
58 EXPECT_EQ(-1, NetmaskToPrefixLength("255.255.240.255"));
59 EXPECT_EQ(-1, NetmaskToPrefixLength("255.0.0.255"));
60 EXPECT_EQ(-1, NetmaskToPrefixLength("255.255.255.FF"));
61 EXPECT_EQ(-1, NetmaskToPrefixLength("255,255,255,255"));
62 EXPECT_EQ(-1, NetmaskToPrefixLength("255 255 255 255"));
65 TEST_F(NetworkUtilTest, PrefixLengthToNetmask) {
66 // Valid Prefix Lengths
67 EXPECT_EQ("255.255.255.255", PrefixLengthToNetmask(32));
68 EXPECT_EQ("255.255.255.254", PrefixLengthToNetmask(31));
69 EXPECT_EQ("255.255.255.252", PrefixLengthToNetmask(30));
70 EXPECT_EQ("255.255.255.248", PrefixLengthToNetmask(29));
71 EXPECT_EQ("255.255.255.240", PrefixLengthToNetmask(28));
72 EXPECT_EQ("255.255.255.224", PrefixLengthToNetmask(27));
73 EXPECT_EQ("255.255.255.192", PrefixLengthToNetmask(26));
74 EXPECT_EQ("255.255.255.128", PrefixLengthToNetmask(25));
75 EXPECT_EQ("255.255.255.0", PrefixLengthToNetmask(24));
76 EXPECT_EQ("255.255.254.0", PrefixLengthToNetmask(23));
77 EXPECT_EQ("255.255.252.0", PrefixLengthToNetmask(22));
78 EXPECT_EQ("255.255.248.0", PrefixLengthToNetmask(21));
79 EXPECT_EQ("255.255.240.0", PrefixLengthToNetmask(20));
80 EXPECT_EQ("255.255.224.0", PrefixLengthToNetmask(19));
81 EXPECT_EQ("255.255.192.0", PrefixLengthToNetmask(18));
82 EXPECT_EQ("255.255.128.0", PrefixLengthToNetmask(17));
83 EXPECT_EQ("255.255.0.0", PrefixLengthToNetmask(16));
84 EXPECT_EQ("255.254.0.0", PrefixLengthToNetmask(15));
85 EXPECT_EQ("255.252.0.0", PrefixLengthToNetmask(14));
86 EXPECT_EQ("255.248.0.0", PrefixLengthToNetmask(13));
87 EXPECT_EQ("255.240.0.0", PrefixLengthToNetmask(12));
88 EXPECT_EQ("255.224.0.0", PrefixLengthToNetmask(11));
89 EXPECT_EQ("255.192.0.0", PrefixLengthToNetmask(10));
90 EXPECT_EQ("255.128.0.0", PrefixLengthToNetmask(9));
91 EXPECT_EQ("255.0.0.0", PrefixLengthToNetmask(8));
92 EXPECT_EQ("254.0.0.0", PrefixLengthToNetmask(7));
93 EXPECT_EQ("252.0.0.0", PrefixLengthToNetmask(6));
94 EXPECT_EQ("248.0.0.0", PrefixLengthToNetmask(5));
95 EXPECT_EQ("240.0.0.0", PrefixLengthToNetmask(4));
96 EXPECT_EQ("224.0.0.0", PrefixLengthToNetmask(3));
97 EXPECT_EQ("192.0.0.0", PrefixLengthToNetmask(2));
98 EXPECT_EQ("128.0.0.0", PrefixLengthToNetmask(1));
99 EXPECT_EQ("0.0.0.0", PrefixLengthToNetmask(0));
100 // Invalid Prefix Lengths
101 EXPECT_EQ("", PrefixLengthToNetmask(-1));
102 EXPECT_EQ("", PrefixLengthToNetmask(33));
103 EXPECT_EQ("", PrefixLengthToNetmask(255));
106 TEST_F(NetworkUtilTest, ParseScanResults) {
107 base::ListValue list;
108 std::vector<CellularScanResult> scan_results;
110 // Empty list value.
111 EXPECT_TRUE(ParseCellularScanResults(list, &scan_results));
113 // List contains invalid item.
114 list.AppendInteger(0);
115 EXPECT_FALSE(ParseCellularScanResults(list, &scan_results));
117 // Scan result has no network id.
118 list.Clear();
119 base::DictionaryValue* dict_value = new base::DictionaryValue();
120 dict_value->SetString(shill::kStatusProperty, "available");
121 list.Append(dict_value);
122 EXPECT_TRUE(ParseCellularScanResults(list, &scan_results));
123 EXPECT_TRUE(scan_results.empty());
125 // Mixed parse results.
126 dict_value = new base::DictionaryValue();
127 dict_value->SetString(shill::kNetworkIdProperty, "000001");
128 dict_value->SetString(shill::kStatusProperty, "unknown");
129 dict_value->SetString(shill::kTechnologyProperty, "GSM");
130 list.Append(dict_value);
132 dict_value = new base::DictionaryValue();
133 dict_value->SetString(shill::kNetworkIdProperty, "000002");
134 dict_value->SetString(shill::kStatusProperty, "available");
135 dict_value->SetString(shill::kLongNameProperty, "Long Name");
136 list.Append(dict_value);
138 EXPECT_TRUE(ParseCellularScanResults(list, &scan_results));
139 EXPECT_EQ(static_cast<size_t>(2), scan_results.size());
140 EXPECT_EQ("000001", scan_results[0].network_id);
141 EXPECT_EQ("unknown", scan_results[0].status);
142 EXPECT_EQ("GSM", scan_results[0].technology);
143 EXPECT_TRUE(scan_results[0].long_name.empty());
144 EXPECT_TRUE(scan_results[0].short_name.empty());
146 EXPECT_EQ("000002", scan_results[1].network_id);
147 EXPECT_EQ("available", scan_results[1].status);
148 EXPECT_EQ("Long Name", scan_results[1].long_name);
149 EXPECT_TRUE(scan_results[1].short_name.empty());
150 EXPECT_TRUE(scan_results[1].technology.empty());