OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #include "base/bind.h" | |
6 #include "base/memory/scoped_ptr.h" | |
7 #include "base/values.h" | |
8 #include "chrome/browser/chromeos/cros/cros_network_functions.h" | |
9 #include "chrome/browser/chromeos/cros/sms_watcher.h" | |
10 #include "chromeos/dbus/mock_dbus_thread_manager.h" | |
11 #include "chromeos/dbus/mock_shill_device_client.h" | |
12 #include "chromeos/dbus/mock_shill_ipconfig_client.h" | |
13 #include "chromeos/dbus/mock_shill_manager_client.h" | |
14 #include "chromeos/dbus/mock_shill_network_client.h" | |
15 #include "chromeos/dbus/mock_shill_profile_client.h" | |
16 #include "chromeos/dbus/mock_shill_service_client.h" | |
17 #include "chromeos/dbus/mock_gsm_sms_client.h" | |
18 #include "testing/gtest/include/gtest/gtest.h" | |
19 #include "third_party/cros_system_api/dbus/service_constants.h" | |
20 | |
21 using ::testing::_; | |
22 using ::testing::Invoke; | |
23 using ::testing::Pointee; | |
24 using ::testing::Return; | |
25 using ::testing::SaveArg; | |
26 using ::testing::StrEq; | |
27 | |
28 // Matcher to match base::Value. | |
29 MATCHER_P(IsEqualTo, value, "") { return arg.Equals(value); } | |
30 | |
31 // Matcher to match SMS. | |
32 MATCHER_P(IsSMSEqualTo, sms, "") { | |
33 return sms.timestamp == arg.timestamp && | |
34 std::string(sms.number) == arg.number && | |
35 std::string(sms.text) == arg.text && | |
36 sms.validity == arg.validity && | |
37 sms.msgclass == arg.msgclass; | |
38 } | |
39 | |
40 // Matcher to match IPConfig::path | |
41 MATCHER_P(IsIPConfigPathEqualTo, str, "") { return str == arg.path; } | |
42 | |
43 namespace chromeos { | |
44 | |
45 namespace { | |
46 | |
47 const char kExamplePath[] = "/foo/bar/baz"; | |
48 | |
49 // A mock to check arguments of NetworkPropertiesCallback and ensure that the | |
50 // callback is called exactly once. | |
51 class MockNetworkPropertiesCallback { | |
52 public: | |
53 // Creates a NetworkPropertiesCallback with expectations. | |
54 static NetworkPropertiesCallback CreateCallback( | |
55 const std::string& expected_path, | |
56 const base::DictionaryValue& expected_result) { | |
57 MockNetworkPropertiesCallback* mock_callback = | |
58 new MockNetworkPropertiesCallback; | |
59 | |
60 EXPECT_CALL(*mock_callback, | |
61 Run(expected_path, Pointee(IsEqualTo(&expected_result)))) | |
62 .Times(1); | |
63 | |
64 return base::Bind(&MockNetworkPropertiesCallback::Run, | |
65 base::Owned(mock_callback)); | |
66 } | |
67 | |
68 MOCK_METHOD2(Run, void(const std::string& path, | |
69 const base::DictionaryValue* result)); | |
70 }; | |
71 | |
72 // A mock to check arguments of NetworkPropertiesWatcherCallback and ensure that | |
73 // the callback is called exactly once. | |
74 class MockNetworkPropertiesWatcherCallback { | |
75 public: | |
76 // Creates a NetworkPropertiesWatcherCallback with expectations. | |
77 static NetworkPropertiesWatcherCallback CreateCallback( | |
78 const std::string& expected_path, | |
79 const std::string& expected_key, | |
80 const base::Value& expected_value) { | |
81 MockNetworkPropertiesWatcherCallback* mock_callback = | |
82 new MockNetworkPropertiesWatcherCallback; | |
83 | |
84 EXPECT_CALL(*mock_callback, | |
85 Run(expected_path, expected_key, IsEqualTo(&expected_value))) | |
86 .Times(1); | |
87 | |
88 return base::Bind(&MockNetworkPropertiesWatcherCallback::Run, | |
89 base::Owned(mock_callback)); | |
90 } | |
91 | |
92 MOCK_METHOD3(Run, void(const std::string& expected_path, | |
93 const std::string& expected_key, | |
94 const base::Value& value)); | |
95 }; | |
96 | |
97 } // namespace | |
98 | |
99 // Test for cros_network_functions.cc without Libcros. | |
100 class CrosNetworkFunctionsTest : public testing::Test { | |
101 public: | |
102 CrosNetworkFunctionsTest() : mock_profile_client_(NULL), | |
103 dictionary_value_result_(NULL) {} | |
104 | |
105 virtual void SetUp() { | |
106 MockDBusThreadManager* mock_dbus_thread_manager = new MockDBusThreadManager; | |
107 EXPECT_CALL(*mock_dbus_thread_manager, GetSystemBus()) | |
108 .WillRepeatedly(Return(reinterpret_cast<dbus::Bus*>(NULL))); | |
109 DBusThreadManager::InitializeForTesting(mock_dbus_thread_manager); | |
110 mock_device_client_ = | |
111 mock_dbus_thread_manager->mock_shill_device_client(); | |
112 mock_ipconfig_client_ = | |
113 mock_dbus_thread_manager->mock_shill_ipconfig_client(); | |
114 mock_manager_client_ = | |
115 mock_dbus_thread_manager->mock_shill_manager_client(); | |
116 mock_network_client_ = | |
117 mock_dbus_thread_manager->mock_shill_network_client(); | |
118 mock_profile_client_ = | |
119 mock_dbus_thread_manager->mock_shill_profile_client(); | |
120 mock_service_client_ = | |
121 mock_dbus_thread_manager->mock_shill_service_client(); | |
122 mock_gsm_sms_client_ = mock_dbus_thread_manager->mock_gsm_sms_client(); | |
123 } | |
124 | |
125 virtual void TearDown() { | |
126 DBusThreadManager::Shutdown(); | |
127 mock_profile_client_ = NULL; | |
128 } | |
129 | |
130 // Handles responses for GetProperties method calls for ShillManagerClient. | |
131 void OnGetManagerProperties( | |
132 const ShillClientHelper::DictionaryValueCallback& callback) { | |
133 callback.Run(DBUS_METHOD_CALL_SUCCESS, *dictionary_value_result_); | |
134 } | |
135 | |
136 // Handles responses for GetProperties method calls. | |
137 void OnGetProperties( | |
138 const dbus::ObjectPath& path, | |
139 const ShillClientHelper::DictionaryValueCallback& callback) { | |
140 callback.Run(DBUS_METHOD_CALL_SUCCESS, *dictionary_value_result_); | |
141 } | |
142 | |
143 // Handles responses for GetProperties method calls that return | |
144 // errors in an error callback. | |
145 void OnGetPropertiesWithoutStatus( | |
146 const dbus::ObjectPath& path, | |
147 const ShillClientHelper::DictionaryValueCallbackWithoutStatus& callback, | |
148 const ShillClientHelper::ErrorCallback& error_callback) { | |
149 callback.Run(*dictionary_value_result_); | |
150 } | |
151 | |
152 // Handles responses for GetEntry method calls. | |
153 void OnGetEntry( | |
154 const dbus::ObjectPath& profile_path, | |
155 const std::string& entry_path, | |
156 const ShillClientHelper::DictionaryValueCallbackWithoutStatus& callback, | |
157 const ShillClientHelper::ErrorCallback& error_callback) { | |
158 callback.Run(*dictionary_value_result_); | |
159 } | |
160 | |
161 // Mock NetworkOperationCallback. | |
162 MOCK_METHOD3(MockNetworkOperationCallback, | |
163 void(const std::string& path, | |
164 NetworkMethodErrorType error, | |
165 const std::string& error_message)); | |
166 | |
167 // Mock MonitorSMSCallback. | |
168 MOCK_METHOD2(MockMonitorSMSCallback, | |
169 void(const std::string& modem_device_path, const SMS& message)); | |
170 | |
171 protected: | |
172 MockShillDeviceClient* mock_device_client_; | |
173 MockShillIPConfigClient* mock_ipconfig_client_; | |
174 MockShillManagerClient* mock_manager_client_; | |
175 MockShillNetworkClient* mock_network_client_; | |
176 MockShillProfileClient* mock_profile_client_; | |
177 MockShillServiceClient* mock_service_client_; | |
178 MockGsmSMSClient* mock_gsm_sms_client_; | |
179 const base::DictionaryValue* dictionary_value_result_; | |
180 }; | |
181 | |
182 TEST_F(CrosNetworkFunctionsTest, CrosActivateCellularModem) { | |
183 const std::string service_path = "/"; | |
184 const std::string carrier = "carrier"; | |
185 EXPECT_CALL(*mock_service_client_, | |
186 CallActivateCellularModemAndBlock(dbus::ObjectPath(service_path), | |
187 carrier)) | |
188 .WillOnce(Return(true)); | |
189 EXPECT_TRUE(CrosActivateCellularModem(service_path, carrier)); | |
190 } | |
191 | |
192 TEST_F(CrosNetworkFunctionsTest, CrosSetNetworkServiceProperty) { | |
193 const std::string service_path = "/"; | |
194 const std::string property = "property"; | |
195 const std::string key1 = "key1"; | |
196 const std::string string1 = "string1"; | |
197 const std::string key2 = "key2"; | |
198 const std::string string2 = "string2"; | |
199 base::DictionaryValue value; | |
200 value.SetString(key1, string1); | |
201 value.SetString(key2, string2); | |
202 EXPECT_CALL(*mock_service_client_, | |
203 SetProperty(dbus::ObjectPath(service_path), property, | |
204 IsEqualTo(&value), _, _)).Times(1); | |
205 | |
206 CrosSetNetworkServiceProperty(service_path, property, value); | |
207 } | |
208 | |
209 TEST_F(CrosNetworkFunctionsTest, CrosClearNetworkServiceProperty) { | |
210 const std::string service_path = "/"; | |
211 const std::string property = "property"; | |
212 EXPECT_CALL(*mock_service_client_, | |
213 ClearProperty(dbus::ObjectPath(service_path), property, _, _)) | |
214 .Times(1); | |
215 | |
216 CrosClearNetworkServiceProperty(service_path, property); | |
217 } | |
218 | |
219 TEST_F(CrosNetworkFunctionsTest, CrosSetNetworkDeviceProperty) { | |
220 const std::string device_path = "/"; | |
221 const std::string property = "property"; | |
222 const bool kBool = true; | |
223 const base::FundamentalValue value(kBool); | |
224 EXPECT_CALL(*mock_device_client_, | |
225 SetProperty(dbus::ObjectPath(device_path), StrEq(property), | |
226 IsEqualTo(&value), _, _)).Times(1); | |
227 | |
228 CrosSetNetworkDeviceProperty(device_path, property, value); | |
229 } | |
230 | |
231 TEST_F(CrosNetworkFunctionsTest, CrosSetNetworkIPConfigProperty) { | |
232 const std::string ipconfig_path = "/"; | |
233 const std::string property = "property"; | |
234 const int kInt = 1234; | |
235 const base::FundamentalValue value(kInt); | |
236 EXPECT_CALL(*mock_ipconfig_client_, | |
237 SetProperty(dbus::ObjectPath(ipconfig_path), property, | |
238 IsEqualTo(&value), _)).Times(1); | |
239 CrosSetNetworkIPConfigProperty(ipconfig_path, property, value); | |
240 } | |
241 | |
242 TEST_F(CrosNetworkFunctionsTest, CrosSetNetworkManagerProperty) { | |
243 const std::string property = "property"; | |
244 const base::StringValue value("string"); | |
245 EXPECT_CALL(*mock_manager_client_, | |
246 SetProperty(property, IsEqualTo(&value), _, _)).Times(1); | |
247 | |
248 CrosSetNetworkManagerProperty(property, value); | |
249 } | |
250 | |
251 TEST_F(CrosNetworkFunctionsTest, CrosDeleteServiceFromProfile) { | |
252 const std::string profile_path("/profile/path"); | |
253 const std::string service_path("/service/path"); | |
254 EXPECT_CALL(*mock_profile_client_, | |
255 DeleteEntry(dbus::ObjectPath(profile_path), service_path, _, _)) | |
256 .Times(1); | |
257 CrosDeleteServiceFromProfile(profile_path, service_path); | |
258 } | |
259 | |
260 TEST_F(CrosNetworkFunctionsTest, CrosMonitorNetworkManagerProperties) { | |
261 const std::string key = "key"; | |
262 const int kValue = 42; | |
263 const base::FundamentalValue value(kValue); | |
264 | |
265 // Start monitoring. | |
266 ShillPropertyChangedObserver* observer = NULL; | |
267 EXPECT_CALL(*mock_manager_client_, AddPropertyChangedObserver(_)) | |
268 .WillOnce(SaveArg<0>(&observer)); | |
269 CrosNetworkWatcher* watcher = CrosMonitorNetworkManagerProperties( | |
270 MockNetworkPropertiesWatcherCallback::CreateCallback( | |
271 flimflam::kFlimflamServicePath, key, value)); | |
272 // Call callback. | |
273 observer->OnPropertyChanged(key, value); | |
274 // Stop monitoring. | |
275 EXPECT_CALL(*mock_manager_client_, | |
276 RemovePropertyChangedObserver(_)).Times(1); | |
277 delete watcher; | |
278 } | |
279 | |
280 TEST_F(CrosNetworkFunctionsTest, CrosMonitorNetworkServiceProperties) { | |
281 const dbus::ObjectPath path("/path"); | |
282 const std::string key = "key"; | |
283 const int kValue = 42; | |
284 const base::FundamentalValue value(kValue); | |
285 // Start monitoring. | |
286 ShillPropertyChangedObserver* observer = NULL; | |
287 EXPECT_CALL(*mock_service_client_, AddPropertyChangedObserver(path, _)) | |
288 .WillOnce(SaveArg<1>(&observer)); | |
289 NetworkPropertiesWatcherCallback callback = | |
290 MockNetworkPropertiesWatcherCallback::CreateCallback(path.value(), | |
291 key, value); | |
292 CrosNetworkWatcher* watcher = CrosMonitorNetworkServiceProperties( | |
293 callback, path.value()); | |
294 // Call callback. | |
295 observer->OnPropertyChanged(key, value); | |
296 // Stop monitoring. | |
297 EXPECT_CALL(*mock_service_client_, | |
298 RemovePropertyChangedObserver(path, _)).Times(1); | |
299 delete watcher; | |
300 } | |
301 | |
302 TEST_F(CrosNetworkFunctionsTest, CrosMonitorNetworkDeviceProperties) { | |
303 const dbus::ObjectPath path("/path"); | |
304 const std::string key = "key"; | |
305 const int kValue = 42; | |
306 const base::FundamentalValue value(kValue); | |
307 // Start monitoring. | |
308 ShillPropertyChangedObserver* observer = NULL; | |
309 EXPECT_CALL(*mock_device_client_, AddPropertyChangedObserver(path, _)) | |
310 .WillOnce(SaveArg<1>(&observer)); | |
311 NetworkPropertiesWatcherCallback callback = | |
312 MockNetworkPropertiesWatcherCallback::CreateCallback(path.value(), | |
313 key, value); | |
314 CrosNetworkWatcher* watcher = CrosMonitorNetworkDeviceProperties( | |
315 callback, path.value()); | |
316 // Call callback. | |
317 observer->OnPropertyChanged(key, value); | |
318 // Stop monitoring. | |
319 EXPECT_CALL(*mock_device_client_, | |
320 RemovePropertyChangedObserver(path, _)).Times(1); | |
321 delete watcher; | |
322 } | |
323 | |
324 TEST_F(CrosNetworkFunctionsTest, CrosMonitorSMS) { | |
325 const std::string dbus_connection = ":1.1"; | |
326 const dbus::ObjectPath object_path("/object/path"); | |
327 base::DictionaryValue device_properties; | |
328 device_properties.SetWithoutPathExpansion( | |
329 flimflam::kDBusConnectionProperty, | |
330 new base::StringValue(dbus_connection)); | |
331 device_properties.SetWithoutPathExpansion( | |
332 flimflam::kDBusObjectProperty, | |
333 new base::StringValue(object_path.value())); | |
334 | |
335 const std::string number = "0123456789"; | |
336 const std::string text = "Hello."; | |
337 const std::string timestamp_string = | |
338 "120424123456+00"; // 2012-04-24 12:34:56 | |
339 base::Time::Exploded timestamp_exploded = {}; | |
340 timestamp_exploded.year = 2012; | |
341 timestamp_exploded.month = 4; | |
342 timestamp_exploded.day_of_month = 24; | |
343 timestamp_exploded.hour = 12; | |
344 timestamp_exploded.minute = 34; | |
345 timestamp_exploded.second = 56; | |
346 const base::Time timestamp = base::Time::FromUTCExploded(timestamp_exploded); | |
347 const std::string smsc = "9876543210"; | |
348 const uint32 kValidity = 1; | |
349 const uint32 kMsgclass = 2; | |
350 const uint32 kIndex = 0; | |
351 const bool kComplete = true; | |
352 base::DictionaryValue* sms_dictionary = new base::DictionaryValue; | |
353 sms_dictionary->SetWithoutPathExpansion( | |
354 SMSWatcher::kNumberKey, new base::StringValue(number)); | |
355 sms_dictionary->SetWithoutPathExpansion( | |
356 SMSWatcher::kTextKey, new base::StringValue(text)); | |
357 sms_dictionary->SetWithoutPathExpansion( | |
358 SMSWatcher::kTimestampKey, | |
359 new base::StringValue(timestamp_string)); | |
360 sms_dictionary->SetWithoutPathExpansion(SMSWatcher::kSmscKey, | |
361 new base::StringValue(smsc)); | |
362 sms_dictionary->SetWithoutPathExpansion( | |
363 SMSWatcher::kValidityKey, base::Value::CreateDoubleValue(kValidity)); | |
364 sms_dictionary->SetWithoutPathExpansion( | |
365 SMSWatcher::kClassKey, base::Value::CreateDoubleValue(kMsgclass)); | |
366 sms_dictionary->SetWithoutPathExpansion( | |
367 SMSWatcher::kIndexKey, base::Value::CreateDoubleValue(kIndex)); | |
368 | |
369 base::ListValue sms_list; | |
370 sms_list.Append(sms_dictionary); | |
371 | |
372 SMS sms; | |
373 sms.timestamp = timestamp; | |
374 sms.number = number.c_str(); | |
375 sms.text = text.c_str(); | |
376 sms.smsc = smsc.c_str(); | |
377 sms.validity = kValidity; | |
378 sms.msgclass = kMsgclass; | |
379 | |
380 const std::string modem_device_path = "/modem/device/path"; | |
381 | |
382 // Set expectations. | |
383 ShillDeviceClient::DictionaryValueCallback get_properties_callback; | |
384 EXPECT_CALL(*mock_device_client_, | |
385 GetProperties(dbus::ObjectPath(modem_device_path), _)) | |
386 .WillOnce(SaveArg<1>(&get_properties_callback)); | |
387 GsmSMSClient::SmsReceivedHandler sms_received_handler; | |
388 EXPECT_CALL(*mock_gsm_sms_client_, | |
389 SetSmsReceivedHandler(dbus_connection, object_path, _)) | |
390 .WillOnce(SaveArg<2>(&sms_received_handler)); | |
391 | |
392 GsmSMSClient::ListCallback list_callback; | |
393 EXPECT_CALL(*mock_gsm_sms_client_, List(dbus_connection, object_path, _)) | |
394 .WillOnce(SaveArg<2>(&list_callback)); | |
395 | |
396 EXPECT_CALL(*this, MockMonitorSMSCallback( | |
397 modem_device_path, IsSMSEqualTo(sms))).Times(2); | |
398 | |
399 GsmSMSClient::DeleteCallback delete_callback; | |
400 EXPECT_CALL(*mock_gsm_sms_client_, | |
401 Delete(dbus_connection, object_path, kIndex, _)) | |
402 .WillRepeatedly(SaveArg<3>(&delete_callback)); | |
403 | |
404 GsmSMSClient::GetCallback get_callback; | |
405 EXPECT_CALL(*mock_gsm_sms_client_, | |
406 Get(dbus_connection, object_path, kIndex, _)) | |
407 .WillOnce(SaveArg<3>(&get_callback)); | |
408 | |
409 // Start monitoring. | |
410 CrosNetworkWatcher* watcher = CrosMonitorSMS( | |
411 modem_device_path, | |
412 base::Bind(&CrosNetworkFunctionsTest::MockMonitorSMSCallback, | |
413 base::Unretained(this))); | |
414 // Return GetProperties() result. | |
415 get_properties_callback.Run(DBUS_METHOD_CALL_SUCCESS, device_properties); | |
416 // Return List() result. | |
417 ASSERT_FALSE(list_callback.is_null()); | |
418 list_callback.Run(sms_list); | |
419 // Return Delete() result. | |
420 ASSERT_FALSE(delete_callback.is_null()); | |
421 delete_callback.Run(); | |
422 // Send fake signal. | |
423 ASSERT_FALSE(sms_received_handler.is_null()); | |
424 sms_received_handler.Run(kIndex, kComplete); | |
425 // Return Get() result. | |
426 ASSERT_FALSE(get_callback.is_null()); | |
427 get_callback.Run(*sms_dictionary); | |
428 // Return Delete() result. | |
429 ASSERT_FALSE(delete_callback.is_null()); | |
430 delete_callback.Run(); | |
431 // Stop monitoring. | |
432 EXPECT_CALL(*mock_gsm_sms_client_, | |
433 ResetSmsReceivedHandler(dbus_connection, object_path)).Times(1); | |
434 delete watcher; | |
435 } | |
436 | |
437 TEST_F(CrosNetworkFunctionsTest, CrosRequestNetworkManagerProperties) { | |
438 const std::string key1 = "key1"; | |
439 const std::string value1 = "value1"; | |
440 const std::string key2 = "key.2."; | |
441 const std::string value2 = "value2"; | |
442 // Create result value. | |
443 base::DictionaryValue result; | |
444 result.SetWithoutPathExpansion(key1, new base::StringValue(value1)); | |
445 result.SetWithoutPathExpansion(key2, new base::StringValue(value2)); | |
446 // Set expectations. | |
447 dictionary_value_result_ = &result; | |
448 EXPECT_CALL(*mock_manager_client_, | |
449 GetProperties(_)).WillOnce( | |
450 Invoke(this, | |
451 &CrosNetworkFunctionsTest::OnGetManagerProperties)); | |
452 | |
453 CrosRequestNetworkManagerProperties( | |
454 MockNetworkPropertiesCallback::CreateCallback( | |
455 flimflam::kFlimflamServicePath, result)); | |
456 } | |
457 | |
458 TEST_F(CrosNetworkFunctionsTest, CrosRequestNetworkServiceProperties) { | |
459 const std::string service_path = "/service/path"; | |
460 const std::string key1 = "key1"; | |
461 const std::string value1 = "value1"; | |
462 const std::string key2 = "key.2."; | |
463 const std::string value2 = "value2"; | |
464 // Create result value. | |
465 base::DictionaryValue result; | |
466 result.SetWithoutPathExpansion(key1, new base::StringValue(value1)); | |
467 result.SetWithoutPathExpansion(key2, new base::StringValue(value2)); | |
468 // Set expectations. | |
469 dictionary_value_result_ = &result; | |
470 EXPECT_CALL(*mock_service_client_, | |
471 GetProperties(dbus::ObjectPath(service_path), _)).WillOnce( | |
472 Invoke(this, &CrosNetworkFunctionsTest::OnGetProperties)); | |
473 | |
474 CrosRequestNetworkServiceProperties( | |
475 service_path, | |
476 MockNetworkPropertiesCallback::CreateCallback(service_path, result)); | |
477 } | |
478 | |
479 TEST_F(CrosNetworkFunctionsTest, CrosRequestNetworkDeviceProperties) { | |
480 const std::string device_path = "/device/path"; | |
481 const std::string key1 = "key1"; | |
482 const std::string value1 = "value1"; | |
483 const std::string key2 = "key.2."; | |
484 const std::string value2 = "value2"; | |
485 // Create result value. | |
486 base::DictionaryValue result; | |
487 result.SetWithoutPathExpansion(key1, new base::StringValue(value1)); | |
488 result.SetWithoutPathExpansion(key2, new base::StringValue(value2)); | |
489 // Set expectations. | |
490 dictionary_value_result_ = &result; | |
491 EXPECT_CALL(*mock_device_client_, | |
492 GetProperties(dbus::ObjectPath(device_path), _)).WillOnce( | |
493 Invoke(this, &CrosNetworkFunctionsTest::OnGetProperties)); | |
494 | |
495 CrosRequestNetworkDeviceProperties( | |
496 device_path, | |
497 MockNetworkPropertiesCallback::CreateCallback(device_path, result)); | |
498 } | |
499 | |
500 TEST_F(CrosNetworkFunctionsTest, CrosRequestNetworkProfileProperties) { | |
501 const std::string profile_path = "/profile/path"; | |
502 const std::string key1 = "key1"; | |
503 const std::string value1 = "value1"; | |
504 const std::string key2 = "key.2."; | |
505 const std::string value2 = "value2"; | |
506 // Create result value. | |
507 base::DictionaryValue result; | |
508 result.SetWithoutPathExpansion(key1, new base::StringValue(value1)); | |
509 result.SetWithoutPathExpansion(key2, new base::StringValue(value2)); | |
510 // Set expectations. | |
511 dictionary_value_result_ = &result; | |
512 EXPECT_CALL( | |
513 *mock_profile_client_, | |
514 GetProperties(dbus::ObjectPath(profile_path), _, _)).WillOnce( | |
515 Invoke(this, | |
516 &CrosNetworkFunctionsTest::OnGetPropertiesWithoutStatus)); | |
517 | |
518 CrosRequestNetworkProfileProperties( | |
519 profile_path, | |
520 MockNetworkPropertiesCallback::CreateCallback(profile_path, result)); | |
521 } | |
522 | |
523 TEST_F(CrosNetworkFunctionsTest, CrosRequestNetworkProfileEntryProperties) { | |
524 const std::string profile_path = "profile path"; | |
525 const std::string profile_entry_path = "profile entry path"; | |
526 const std::string key1 = "key1"; | |
527 const std::string value1 = "value1"; | |
528 const std::string key2 = "key.2."; | |
529 const std::string value2 = "value2"; | |
530 // Create result value. | |
531 base::DictionaryValue result; | |
532 result.SetWithoutPathExpansion(key1, new base::StringValue(value1)); | |
533 result.SetWithoutPathExpansion(key2, new base::StringValue(value2)); | |
534 // Set expectations. | |
535 dictionary_value_result_ = &result; | |
536 EXPECT_CALL(*mock_profile_client_, | |
537 GetEntry(dbus::ObjectPath(profile_path), | |
538 profile_entry_path, _, _)) | |
539 .WillOnce(Invoke(this, &CrosNetworkFunctionsTest::OnGetEntry)); | |
540 | |
541 CrosRequestNetworkProfileEntryProperties( | |
542 profile_path, profile_entry_path, | |
543 MockNetworkPropertiesCallback::CreateCallback(profile_entry_path, | |
544 result)); | |
545 } | |
546 | |
547 TEST_F(CrosNetworkFunctionsTest, CrosRequestHiddenWifiNetworkProperties) { | |
548 const std::string ssid = "ssid"; | |
549 const std::string security = "security"; | |
550 const std::string key1 = "key1"; | |
551 const std::string value1 = "value1"; | |
552 const std::string key2 = "key.2."; | |
553 const std::string value2 = "value2"; | |
554 // Create result value. | |
555 base::DictionaryValue result; | |
556 result.SetWithoutPathExpansion(key1, new base::StringValue(value1)); | |
557 result.SetWithoutPathExpansion(key2, new base::StringValue(value2)); | |
558 dictionary_value_result_ = &result; | |
559 // Create expected argument to ShillManagerClient::GetService. | |
560 base::DictionaryValue properties; | |
561 properties.SetWithoutPathExpansion( | |
562 flimflam::kModeProperty, | |
563 new base::StringValue(flimflam::kModeManaged)); | |
564 properties.SetWithoutPathExpansion( | |
565 flimflam::kTypeProperty, | |
566 new base::StringValue(flimflam::kTypeWifi)); | |
567 properties.SetWithoutPathExpansion( | |
568 flimflam::kSSIDProperty, | |
569 new base::StringValue(ssid)); | |
570 properties.SetWithoutPathExpansion( | |
571 flimflam::kSecurityProperty, | |
572 new base::StringValue(security)); | |
573 // Set expectations. | |
574 const dbus::ObjectPath service_path("/service/path"); | |
575 ObjectPathCallback callback; | |
576 EXPECT_CALL(*mock_manager_client_, GetService(IsEqualTo(&properties), _, _)) | |
577 .WillOnce(SaveArg<1>(&callback)); | |
578 EXPECT_CALL(*mock_service_client_, | |
579 GetProperties(service_path, _)).WillOnce( | |
580 Invoke(this, &CrosNetworkFunctionsTest::OnGetProperties)); | |
581 | |
582 // Call function. | |
583 CrosRequestHiddenWifiNetworkProperties( | |
584 ssid, security, | |
585 MockNetworkPropertiesCallback::CreateCallback(service_path.value(), | |
586 result)); | |
587 // Run callback to invoke GetProperties. | |
588 callback.Run(service_path); | |
589 } | |
590 | |
591 TEST_F(CrosNetworkFunctionsTest, CrosRequestVirtualNetworkProperties) { | |
592 const std::string service_name = "service name"; | |
593 const std::string server_hostname = "server hostname"; | |
594 const std::string provider_type = "provider type"; | |
595 const std::string key1 = "key1"; | |
596 const std::string value1 = "value1"; | |
597 const std::string key2 = "key.2."; | |
598 const std::string value2 = "value2"; | |
599 // Create result value. | |
600 base::DictionaryValue result; | |
601 result.SetWithoutPathExpansion(key1, new base::StringValue(value1)); | |
602 result.SetWithoutPathExpansion(key2, new base::StringValue(value2)); | |
603 dictionary_value_result_ = &result; | |
604 // Create expected argument to ShillManagerClient::GetService. | |
605 base::DictionaryValue properties; | |
606 properties.SetWithoutPathExpansion( | |
607 flimflam::kTypeProperty, new base::StringValue("vpn")); | |
608 properties.SetWithoutPathExpansion( | |
609 flimflam::kProviderNameProperty, | |
610 new base::StringValue(service_name)); | |
611 properties.SetWithoutPathExpansion( | |
612 flimflam::kProviderHostProperty, | |
613 new base::StringValue(server_hostname)); | |
614 properties.SetWithoutPathExpansion( | |
615 flimflam::kProviderTypeProperty, | |
616 new base::StringValue(provider_type)); | |
617 properties.SetWithoutPathExpansion( | |
618 flimflam::kVPNDomainProperty, | |
619 new base::StringValue(service_name)); | |
620 | |
621 // Set expectations. | |
622 const dbus::ObjectPath service_path("/service/path"); | |
623 ObjectPathCallback callback; | |
624 EXPECT_CALL(*mock_manager_client_, GetService(IsEqualTo(&properties), _, _)) | |
625 .WillOnce(SaveArg<1>(&callback)); | |
626 EXPECT_CALL(*mock_service_client_, | |
627 GetProperties(service_path, _)).WillOnce( | |
628 Invoke(this, &CrosNetworkFunctionsTest::OnGetProperties)); | |
629 | |
630 // Call function. | |
631 CrosRequestVirtualNetworkProperties( | |
632 service_name, server_hostname, provider_type, | |
633 MockNetworkPropertiesCallback::CreateCallback(service_path.value(), | |
634 result)); | |
635 // Run callback to invoke GetProperties. | |
636 callback.Run(service_path); | |
637 } | |
638 | |
639 TEST_F(CrosNetworkFunctionsTest, CrosRequestNetworkServiceDisconnect) { | |
640 const std::string service_path = "/service/path"; | |
641 EXPECT_CALL(*mock_service_client_, | |
642 Disconnect(dbus::ObjectPath(service_path), _, _)).Times(1); | |
643 CrosRequestNetworkServiceDisconnect(service_path); | |
644 } | |
645 | |
646 TEST_F(CrosNetworkFunctionsTest, CrosRequestRemoveNetworkService) { | |
647 const std::string service_path = "/service/path"; | |
648 EXPECT_CALL(*mock_service_client_, | |
649 Remove(dbus::ObjectPath(service_path), _, _)).Times(1); | |
650 CrosRequestRemoveNetworkService(service_path); | |
651 } | |
652 | |
653 TEST_F(CrosNetworkFunctionsTest, CrosRequestNetworkScan) { | |
654 EXPECT_CALL(*mock_manager_client_, | |
655 RequestScan(flimflam::kTypeWifi, _, _)).Times(1); | |
656 CrosRequestNetworkScan(flimflam::kTypeWifi); | |
657 } | |
658 | |
659 TEST_F(CrosNetworkFunctionsTest, CrosRequestNetworkDeviceEnable) { | |
660 const bool kEnable = true; | |
661 EXPECT_CALL(*mock_manager_client_, | |
662 EnableTechnology(flimflam::kTypeWifi, _, _)).Times(1); | |
663 CrosRequestNetworkDeviceEnable(flimflam::kTypeWifi, kEnable); | |
664 | |
665 const bool kDisable = false; | |
666 EXPECT_CALL(*mock_manager_client_, | |
667 DisableTechnology(flimflam::kTypeWifi, _, _)).Times(1); | |
668 CrosRequestNetworkDeviceEnable(flimflam::kTypeWifi, kDisable); | |
669 } | |
670 | |
671 TEST_F(CrosNetworkFunctionsTest, CrosRequestRequirePin) { | |
672 const std::string device_path = "/device/path"; | |
673 const std::string pin = "123456"; | |
674 const bool kRequire = true; | |
675 | |
676 // Set expectations. | |
677 base::Closure callback; | |
678 EXPECT_CALL(*mock_device_client_, | |
679 RequirePin(dbus::ObjectPath(device_path), pin, kRequire, _, _)) | |
680 .WillOnce(SaveArg<3>(&callback)); | |
681 EXPECT_CALL(*this, MockNetworkOperationCallback( | |
682 device_path, NETWORK_METHOD_ERROR_NONE, _)).Times(1); | |
683 CrosRequestRequirePin( | |
684 device_path, pin, kRequire, | |
685 base::Bind(&CrosNetworkFunctionsTest::MockNetworkOperationCallback, | |
686 base::Unretained(this))); | |
687 // Run saved callback. | |
688 callback.Run(); | |
689 } | |
690 | |
691 TEST_F(CrosNetworkFunctionsTest, CrosRequestEnterPin) { | |
692 const std::string device_path = "/device/path"; | |
693 const std::string pin = "123456"; | |
694 | |
695 // Set expectations. | |
696 base::Closure callback; | |
697 EXPECT_CALL(*mock_device_client_, | |
698 EnterPin(dbus::ObjectPath(device_path), pin, _, _)) | |
699 .WillOnce(SaveArg<2>(&callback)); | |
700 EXPECT_CALL(*this, MockNetworkOperationCallback( | |
701 device_path, NETWORK_METHOD_ERROR_NONE, _)).Times(1); | |
702 CrosRequestEnterPin( | |
703 device_path, pin, | |
704 base::Bind(&CrosNetworkFunctionsTest::MockNetworkOperationCallback, | |
705 base::Unretained(this))); | |
706 // Run saved callback. | |
707 callback.Run(); | |
708 } | |
709 | |
710 TEST_F(CrosNetworkFunctionsTest, CrosRequestUnblockPin) { | |
711 const std::string device_path = "/device/path"; | |
712 const std::string unblock_code = "987654"; | |
713 const std::string pin = "123456"; | |
714 | |
715 // Set expectations. | |
716 base::Closure callback; | |
717 EXPECT_CALL( | |
718 *mock_device_client_, | |
719 UnblockPin(dbus::ObjectPath(device_path), unblock_code, pin, _, _)) | |
720 .WillOnce(SaveArg<3>(&callback)); | |
721 EXPECT_CALL(*this, MockNetworkOperationCallback( | |
722 device_path, NETWORK_METHOD_ERROR_NONE, _)).Times(1); | |
723 CrosRequestUnblockPin(device_path, unblock_code, pin, | |
724 base::Bind(&CrosNetworkFunctionsTest::MockNetworkOperationCallback, | |
725 base::Unretained(this))); | |
726 // Run saved callback. | |
727 callback.Run(); | |
728 } | |
729 | |
730 TEST_F(CrosNetworkFunctionsTest, CrosRequestChangePin) { | |
731 const std::string device_path = "/device/path"; | |
732 const std::string old_pin = "123456"; | |
733 const std::string new_pin = "234567"; | |
734 | |
735 // Set expectations. | |
736 base::Closure callback; | |
737 EXPECT_CALL(*mock_device_client_, | |
738 ChangePin(dbus::ObjectPath(device_path), old_pin, new_pin, _, _)) | |
739 .WillOnce(SaveArg<3>(&callback)); | |
740 EXPECT_CALL(*this, MockNetworkOperationCallback( | |
741 device_path, NETWORK_METHOD_ERROR_NONE, _)).Times(1); | |
742 CrosRequestChangePin(device_path, old_pin, new_pin, | |
743 base::Bind(&CrosNetworkFunctionsTest::MockNetworkOperationCallback, | |
744 base::Unretained(this))); | |
745 // Run saved callback. | |
746 callback.Run(); | |
747 } | |
748 | |
749 TEST_F(CrosNetworkFunctionsTest, CrosProposeScan) { | |
750 const std::string device_path = "/device/path"; | |
751 EXPECT_CALL(*mock_device_client_, | |
752 ProposeScan(dbus::ObjectPath(device_path), _)).Times(1); | |
753 CrosProposeScan(device_path); | |
754 } | |
755 | |
756 TEST_F(CrosNetworkFunctionsTest, CrosRequestCellularRegister) { | |
757 const std::string device_path = "/device/path"; | |
758 const std::string network_id = "networkid"; | |
759 | |
760 // Set expectations. | |
761 base::Closure callback; | |
762 EXPECT_CALL(*mock_device_client_, | |
763 Register(dbus::ObjectPath(device_path), network_id, _, _)) | |
764 .WillOnce(SaveArg<2>(&callback)); | |
765 EXPECT_CALL(*this, MockNetworkOperationCallback( | |
766 device_path, NETWORK_METHOD_ERROR_NONE, _)).Times(1); | |
767 CrosRequestCellularRegister(device_path, network_id, | |
768 base::Bind(&CrosNetworkFunctionsTest::MockNetworkOperationCallback, | |
769 base::Unretained(this))); | |
770 // Run saved callback. | |
771 callback.Run(); | |
772 } | |
773 | |
774 TEST_F(CrosNetworkFunctionsTest, CrosSetOfflineMode) { | |
775 const bool kOffline = true; | |
776 const base::FundamentalValue value(kOffline); | |
777 EXPECT_CALL(*mock_manager_client_, SetProperty( | |
778 flimflam::kOfflineModeProperty, IsEqualTo(&value), _, _)).Times(1); | |
779 CrosSetOfflineMode(kOffline); | |
780 } | |
781 | |
782 TEST_F(CrosNetworkFunctionsTest, CrosListIPConfigsAndBlock) { | |
783 const std::string device_path = "/device/path"; | |
784 std::string ipconfig_path = "/ipconfig/path"; | |
785 | |
786 const IPConfigType kType = IPCONFIG_TYPE_DHCP; | |
787 const std::string address = "address"; | |
788 const int kMtu = 123; | |
789 const int kPrefixlen = 24; | |
790 const std::string netmask = "255.255.255.0"; | |
791 const std::string broadcast = "broadcast"; | |
792 const std::string peer_address = "peer address"; | |
793 const std::string gateway = "gateway"; | |
794 const std::string domainname = "domainname"; | |
795 const std::string name_server1 = "nameserver1"; | |
796 const std::string name_server2 = "nameserver2"; | |
797 const std::string name_servers = name_server1 + "," + name_server2; | |
798 const std::string hardware_address = "hardware address"; | |
799 | |
800 base::ListValue* ipconfigs = new base::ListValue; | |
801 ipconfigs->Append(new base::StringValue(ipconfig_path)); | |
802 base::DictionaryValue* device_properties = new base::DictionaryValue; | |
803 device_properties->SetWithoutPathExpansion( | |
804 flimflam::kIPConfigsProperty, ipconfigs); | |
805 device_properties->SetWithoutPathExpansion( | |
806 flimflam::kAddressProperty, | |
807 new base::StringValue(hardware_address)); | |
808 | |
809 base::ListValue* name_servers_list = new base::ListValue; | |
810 name_servers_list->Append(new base::StringValue(name_server1)); | |
811 name_servers_list->Append(new base::StringValue(name_server2)); | |
812 base::DictionaryValue* ipconfig_properties = new base::DictionaryValue; | |
813 ipconfig_properties->SetWithoutPathExpansion( | |
814 flimflam::kMethodProperty, | |
815 new base::StringValue(flimflam::kTypeDHCP)); | |
816 ipconfig_properties->SetWithoutPathExpansion( | |
817 flimflam::kAddressProperty, | |
818 new base::StringValue(address)); | |
819 ipconfig_properties->SetWithoutPathExpansion( | |
820 flimflam::kMtuProperty, | |
821 base::Value::CreateIntegerValue(kMtu)); | |
822 ipconfig_properties->SetWithoutPathExpansion( | |
823 flimflam::kPrefixlenProperty, | |
824 base::Value::CreateIntegerValue(kPrefixlen)); | |
825 ipconfig_properties->SetWithoutPathExpansion( | |
826 flimflam::kBroadcastProperty, | |
827 new base::StringValue(broadcast)); | |
828 ipconfig_properties->SetWithoutPathExpansion( | |
829 flimflam::kPeerAddressProperty, | |
830 new base::StringValue(peer_address)); | |
831 ipconfig_properties->SetWithoutPathExpansion( | |
832 flimflam::kGatewayProperty, | |
833 new base::StringValue(gateway)); | |
834 ipconfig_properties->SetWithoutPathExpansion( | |
835 flimflam::kDomainNameProperty, | |
836 new base::StringValue(domainname)); | |
837 ipconfig_properties->SetWithoutPathExpansion( | |
838 flimflam::kNameServersProperty, name_servers_list); | |
839 | |
840 EXPECT_CALL(*mock_device_client_, | |
841 CallGetPropertiesAndBlock(dbus::ObjectPath(device_path))) | |
842 .WillOnce(Return(device_properties)); | |
843 EXPECT_CALL(*mock_ipconfig_client_, | |
844 CallGetPropertiesAndBlock(dbus::ObjectPath(ipconfig_path))) | |
845 .WillOnce(Return(ipconfig_properties)); | |
846 | |
847 NetworkIPConfigVector result_ipconfigs; | |
848 std::vector<std::string> result_ipconfig_paths; | |
849 std::string result_hardware_address; | |
850 EXPECT_TRUE( | |
851 CrosListIPConfigsAndBlock(device_path, | |
852 &result_ipconfigs, | |
853 &result_ipconfig_paths, | |
854 &result_hardware_address)); | |
855 | |
856 EXPECT_EQ(hardware_address, result_hardware_address); | |
857 ASSERT_EQ(1U, result_ipconfigs.size()); | |
858 EXPECT_EQ(device_path, result_ipconfigs[0].device_path); | |
859 EXPECT_EQ(kType, result_ipconfigs[0].type); | |
860 EXPECT_EQ(address, result_ipconfigs[0].address); | |
861 EXPECT_EQ(netmask, result_ipconfigs[0].netmask); | |
862 EXPECT_EQ(gateway, result_ipconfigs[0].gateway); | |
863 EXPECT_EQ(name_servers, result_ipconfigs[0].name_servers); | |
864 ASSERT_EQ(1U, result_ipconfig_paths.size()); | |
865 EXPECT_EQ(ipconfig_path, result_ipconfig_paths[0]); | |
866 } | |
867 | |
868 TEST_F(CrosNetworkFunctionsTest, CrosGetWifiAccessPoints) { | |
869 const std::string device_path = "/device/path"; | |
870 base::ListValue* devices = new base::ListValue; | |
871 devices->Append(new base::StringValue(device_path)); | |
872 base::DictionaryValue* manager_properties = new base::DictionaryValue; | |
873 manager_properties->SetWithoutPathExpansion( | |
874 flimflam::kDevicesProperty, devices); | |
875 | |
876 const int kScanInterval = 42; | |
877 const std::string network_path = "/network/path"; | |
878 base::ListValue* networks = new base::ListValue; | |
879 networks->Append(new base::StringValue(network_path)); | |
880 base::DictionaryValue* device_properties = new base::DictionaryValue; | |
881 device_properties->SetWithoutPathExpansion( | |
882 flimflam::kNetworksProperty, networks); | |
883 device_properties->SetWithoutPathExpansion( | |
884 flimflam::kPoweredProperty, base::Value::CreateBooleanValue(true)); | |
885 device_properties->SetWithoutPathExpansion( | |
886 flimflam::kScanIntervalProperty, | |
887 base::Value::CreateIntegerValue(kScanInterval)); | |
888 | |
889 const int kSignalStrength = 10; | |
890 const int kWifiChannel = 3; | |
891 const std::string address = "address"; | |
892 const std::string name = "name"; | |
893 const base::Time expected_timestamp = | |
894 base::Time::Now() - base::TimeDelta::FromSeconds(kScanInterval); | |
895 const base::TimeDelta acceptable_timestamp_range = | |
896 base::TimeDelta::FromSeconds(1); | |
897 | |
898 base::DictionaryValue* network_properties = new base::DictionaryValue; | |
899 network_properties->SetWithoutPathExpansion( | |
900 flimflam::kAddressProperty, new base::StringValue(address)); | |
901 network_properties->SetWithoutPathExpansion( | |
902 flimflam::kNameProperty, new base::StringValue(name)); | |
903 network_properties->SetWithoutPathExpansion( | |
904 flimflam::kSignalStrengthProperty, | |
905 base::Value::CreateIntegerValue(kSignalStrength)); | |
906 network_properties->SetWithoutPathExpansion( | |
907 flimflam::kWifiChannelProperty, | |
908 base::Value::CreateIntegerValue(kWifiChannel)); | |
909 | |
910 // Set expectations. | |
911 EXPECT_CALL(*mock_manager_client_, CallGetPropertiesAndBlock()) | |
912 .WillOnce(Return(manager_properties)); | |
913 EXPECT_CALL(*mock_device_client_, | |
914 CallGetPropertiesAndBlock(dbus::ObjectPath(device_path))) | |
915 .WillOnce(Return(device_properties)); | |
916 EXPECT_CALL(*mock_network_client_, | |
917 CallGetPropertiesAndBlock(dbus::ObjectPath(network_path))) | |
918 .WillOnce(Return(network_properties)); | |
919 | |
920 // Call function. | |
921 WifiAccessPointVector aps; | |
922 ASSERT_TRUE(CrosGetWifiAccessPoints(&aps)); | |
923 ASSERT_EQ(1U, aps.size()); | |
924 EXPECT_EQ(address, aps[0].mac_address); | |
925 EXPECT_EQ(name, aps[0].name); | |
926 EXPECT_LE(expected_timestamp - acceptable_timestamp_range, aps[0].timestamp); | |
927 EXPECT_GE(expected_timestamp + acceptable_timestamp_range, aps[0].timestamp); | |
928 EXPECT_EQ(kSignalStrength, aps[0].signal_strength); | |
929 EXPECT_EQ(kWifiChannel, aps[0].channel); | |
930 } | |
931 | |
932 TEST_F(CrosNetworkFunctionsTest, CrosConfigureService) { | |
933 const std::string key1 = "key1"; | |
934 const std::string string1 = "string1"; | |
935 const std::string key2 = "key2"; | |
936 const std::string string2 = "string2"; | |
937 base::DictionaryValue value; | |
938 value.SetString(key1, string1); | |
939 value.SetString(key2, string2); | |
940 EXPECT_CALL(*mock_manager_client_, ConfigureService(IsEqualTo(&value), _, _)) | |
941 .Times(1); | |
942 CrosConfigureService(value); | |
943 } | |
944 | |
945 TEST_F(CrosNetworkFunctionsTest, NetmaskToPrefixLength) { | |
946 // Valid netmasks | |
947 EXPECT_EQ(32, CrosNetmaskToPrefixLength("255.255.255.255")); | |
948 EXPECT_EQ(31, CrosNetmaskToPrefixLength("255.255.255.254")); | |
949 EXPECT_EQ(30, CrosNetmaskToPrefixLength("255.255.255.252")); | |
950 EXPECT_EQ(29, CrosNetmaskToPrefixLength("255.255.255.248")); | |
951 EXPECT_EQ(28, CrosNetmaskToPrefixLength("255.255.255.240")); | |
952 EXPECT_EQ(27, CrosNetmaskToPrefixLength("255.255.255.224")); | |
953 EXPECT_EQ(26, CrosNetmaskToPrefixLength("255.255.255.192")); | |
954 EXPECT_EQ(25, CrosNetmaskToPrefixLength("255.255.255.128")); | |
955 EXPECT_EQ(24, CrosNetmaskToPrefixLength("255.255.255.0")); | |
956 EXPECT_EQ(23, CrosNetmaskToPrefixLength("255.255.254.0")); | |
957 EXPECT_EQ(22, CrosNetmaskToPrefixLength("255.255.252.0")); | |
958 EXPECT_EQ(21, CrosNetmaskToPrefixLength("255.255.248.0")); | |
959 EXPECT_EQ(20, CrosNetmaskToPrefixLength("255.255.240.0")); | |
960 EXPECT_EQ(19, CrosNetmaskToPrefixLength("255.255.224.0")); | |
961 EXPECT_EQ(18, CrosNetmaskToPrefixLength("255.255.192.0")); | |
962 EXPECT_EQ(17, CrosNetmaskToPrefixLength("255.255.128.0")); | |
963 EXPECT_EQ(16, CrosNetmaskToPrefixLength("255.255.0.0")); | |
964 EXPECT_EQ(15, CrosNetmaskToPrefixLength("255.254.0.0")); | |
965 EXPECT_EQ(14, CrosNetmaskToPrefixLength("255.252.0.0")); | |
966 EXPECT_EQ(13, CrosNetmaskToPrefixLength("255.248.0.0")); | |
967 EXPECT_EQ(12, CrosNetmaskToPrefixLength("255.240.0.0")); | |
968 EXPECT_EQ(11, CrosNetmaskToPrefixLength("255.224.0.0")); | |
969 EXPECT_EQ(10, CrosNetmaskToPrefixLength("255.192.0.0")); | |
970 EXPECT_EQ(9, CrosNetmaskToPrefixLength("255.128.0.0")); | |
971 EXPECT_EQ(8, CrosNetmaskToPrefixLength("255.0.0.0")); | |
972 EXPECT_EQ(7, CrosNetmaskToPrefixLength("254.0.0.0")); | |
973 EXPECT_EQ(6, CrosNetmaskToPrefixLength("252.0.0.0")); | |
974 EXPECT_EQ(5, CrosNetmaskToPrefixLength("248.0.0.0")); | |
975 EXPECT_EQ(4, CrosNetmaskToPrefixLength("240.0.0.0")); | |
976 EXPECT_EQ(3, CrosNetmaskToPrefixLength("224.0.0.0")); | |
977 EXPECT_EQ(2, CrosNetmaskToPrefixLength("192.0.0.0")); | |
978 EXPECT_EQ(1, CrosNetmaskToPrefixLength("128.0.0.0")); | |
979 EXPECT_EQ(0, CrosNetmaskToPrefixLength("0.0.0.0")); | |
980 // Invalid netmasks | |
981 EXPECT_EQ(-1, CrosNetmaskToPrefixLength("255.255.255")); | |
982 EXPECT_EQ(-1, CrosNetmaskToPrefixLength("255.255.255.255.255")); | |
983 EXPECT_EQ(-1, CrosNetmaskToPrefixLength("255.255.255.255.0")); | |
984 EXPECT_EQ(-1, CrosNetmaskToPrefixLength("255.255.255.256")); | |
985 EXPECT_EQ(-1, CrosNetmaskToPrefixLength("255.255.255.1")); | |
986 EXPECT_EQ(-1, CrosNetmaskToPrefixLength("255.255.240.255")); | |
987 EXPECT_EQ(-1, CrosNetmaskToPrefixLength("255.0.0.255")); | |
988 EXPECT_EQ(-1, CrosNetmaskToPrefixLength("255.255.255.FF")); | |
989 EXPECT_EQ(-1, CrosNetmaskToPrefixLength("255,255,255,255")); | |
990 EXPECT_EQ(-1, CrosNetmaskToPrefixLength("255 255 255 255")); | |
991 } | |
992 | |
993 TEST_F(CrosNetworkFunctionsTest, PrefixLengthToNetmask) { | |
994 // Valid Prefix Lengths | |
995 EXPECT_EQ("255.255.255.255", CrosPrefixLengthToNetmask(32)); | |
996 EXPECT_EQ("255.255.255.254", CrosPrefixLengthToNetmask(31)); | |
997 EXPECT_EQ("255.255.255.252", CrosPrefixLengthToNetmask(30)); | |
998 EXPECT_EQ("255.255.255.248", CrosPrefixLengthToNetmask(29)); | |
999 EXPECT_EQ("255.255.255.240", CrosPrefixLengthToNetmask(28)); | |
1000 EXPECT_EQ("255.255.255.224", CrosPrefixLengthToNetmask(27)); | |
1001 EXPECT_EQ("255.255.255.192", CrosPrefixLengthToNetmask(26)); | |
1002 EXPECT_EQ("255.255.255.128", CrosPrefixLengthToNetmask(25)); | |
1003 EXPECT_EQ("255.255.255.0", CrosPrefixLengthToNetmask(24)); | |
1004 EXPECT_EQ("255.255.254.0", CrosPrefixLengthToNetmask(23)); | |
1005 EXPECT_EQ("255.255.252.0", CrosPrefixLengthToNetmask(22)); | |
1006 EXPECT_EQ("255.255.248.0", CrosPrefixLengthToNetmask(21)); | |
1007 EXPECT_EQ("255.255.240.0", CrosPrefixLengthToNetmask(20)); | |
1008 EXPECT_EQ("255.255.224.0", CrosPrefixLengthToNetmask(19)); | |
1009 EXPECT_EQ("255.255.192.0", CrosPrefixLengthToNetmask(18)); | |
1010 EXPECT_EQ("255.255.128.0", CrosPrefixLengthToNetmask(17)); | |
1011 EXPECT_EQ("255.255.0.0", CrosPrefixLengthToNetmask(16)); | |
1012 EXPECT_EQ("255.254.0.0", CrosPrefixLengthToNetmask(15)); | |
1013 EXPECT_EQ("255.252.0.0", CrosPrefixLengthToNetmask(14)); | |
1014 EXPECT_EQ("255.248.0.0", CrosPrefixLengthToNetmask(13)); | |
1015 EXPECT_EQ("255.240.0.0", CrosPrefixLengthToNetmask(12)); | |
1016 EXPECT_EQ("255.224.0.0", CrosPrefixLengthToNetmask(11)); | |
1017 EXPECT_EQ("255.192.0.0", CrosPrefixLengthToNetmask(10)); | |
1018 EXPECT_EQ("255.128.0.0", CrosPrefixLengthToNetmask(9)); | |
1019 EXPECT_EQ("255.0.0.0", CrosPrefixLengthToNetmask(8)); | |
1020 EXPECT_EQ("254.0.0.0", CrosPrefixLengthToNetmask(7)); | |
1021 EXPECT_EQ("252.0.0.0", CrosPrefixLengthToNetmask(6)); | |
1022 EXPECT_EQ("248.0.0.0", CrosPrefixLengthToNetmask(5)); | |
1023 EXPECT_EQ("240.0.0.0", CrosPrefixLengthToNetmask(4)); | |
1024 EXPECT_EQ("224.0.0.0", CrosPrefixLengthToNetmask(3)); | |
1025 EXPECT_EQ("192.0.0.0", CrosPrefixLengthToNetmask(2)); | |
1026 EXPECT_EQ("128.0.0.0", CrosPrefixLengthToNetmask(1)); | |
1027 EXPECT_EQ("0.0.0.0", CrosPrefixLengthToNetmask(0)); | |
1028 // Invalid Prefix Lengths | |
1029 EXPECT_EQ("", CrosPrefixLengthToNetmask(-1)); | |
1030 EXPECT_EQ("", CrosPrefixLengthToNetmask(33)); | |
1031 EXPECT_EQ("", CrosPrefixLengthToNetmask(255)); | |
1032 } | |
1033 | |
1034 } // namespace chromeos | |
OLD | NEW |