1 //
2 // Copyright (C) 2012 The Android Open Source Project
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 //      http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16 
17 #include "shill/wimax/wimax_provider.h"
18 
19 #include <string>
20 #include <vector>
21 
22 #include <base/strings/stringprintf.h>
23 #if defined(__ANDROID__)
24 #include <dbus/service_constants.h>
25 #else
26 #include <chromeos/dbus/service_constants.h>
27 #endif  // __ANDROID__
28 
29 #include "shill/eap_credentials.h"
30 #include "shill/fake_store.h"
31 #include "shill/mock_device_info.h"
32 #include "shill/mock_manager.h"
33 #include "shill/mock_metrics.h"
34 #include "shill/mock_profile.h"
35 #include "shill/nice_mock_control.h"
36 #include "shill/testing.h"
37 #include "shill/wimax/mock_wimax.h"
38 #include "shill/wimax/mock_wimax_manager_proxy.h"
39 #include "shill/wimax/mock_wimax_network_proxy.h"
40 #include "shill/wimax/mock_wimax_service.h"
41 #include "shill/wimax/wimax_service.h"
42 
43 using std::string;
44 using std::vector;
45 using testing::Return;
46 using testing::ReturnNull;
47 using testing::SaveArg;
48 using testing::StartsWith;
49 using testing::_;
50 
51 namespace shill {
52 
53 namespace {
54 
GetTestLinkName(int index)55 string GetTestLinkName(int index) {
56   return base::StringPrintf("wm%d", index);
57 }
58 
GetTestPath(int index)59 string GetTestPath(int index) {
60   return wimax_manager::kDeviceObjectPathPrefix + GetTestLinkName(index);
61 }
62 
GetTestNetworkPath(uint32_t identifier)63 string GetTestNetworkPath(uint32_t identifier) {
64   return base::StringPrintf("%s%08x",
65                             wimax_manager::kNetworkObjectPathPrefix,
66                             identifier);
67 }
68 
69 }  // namespace
70 
71 class WiMaxProviderTest : public testing::Test {
72  public:
WiMaxProviderTest()73   WiMaxProviderTest()
74       : network_proxy_(new MockWiMaxNetworkProxy()),
75         metrics_(nullptr),
76         manager_(&control_, nullptr, &metrics_),
77         device_info_(&control_, nullptr, &metrics_, &manager_),
78         provider_(&control_, nullptr, &metrics_, &manager_) {}
79 
~WiMaxProviderTest()80   virtual ~WiMaxProviderTest() {}
81 
82  protected:
GetServiceFriendlyName(const ServiceRefPtr & service)83   string GetServiceFriendlyName(const ServiceRefPtr& service) {
84     return service->friendly_name();
85   }
86 
87   std::unique_ptr<MockWiMaxNetworkProxy> network_proxy_;
88   NiceMockControl control_;
89   MockMetrics metrics_;
90   MockManager manager_;
91   MockDeviceInfo device_info_;
92   WiMaxProvider provider_;
93 };
94 
TEST_F(WiMaxProviderTest,StartStop)95 TEST_F(WiMaxProviderTest, StartStop) {
96   MockWiMaxManagerProxy* wimax_manager_proxy = new MockWiMaxManagerProxy();
97 
98   base::Closure service_appeared_callback;
99   EXPECT_FALSE(provider_.wimax_manager_proxy_.get());
100   EXPECT_CALL(control_, CreateWiMaxManagerProxy(_, _))
101       .WillOnce(DoAll(SaveArg<0>(&service_appeared_callback),
102                       Return(wimax_manager_proxy)));
103   EXPECT_CALL(*wimax_manager_proxy, set_devices_changed_callback(_)).Times(1);
104   provider_.Start();
105   EXPECT_TRUE(provider_.wimax_manager_proxy_.get());
106 
107   EXPECT_CALL(*wimax_manager_proxy, Devices(_))
108       .WillOnce(Return(RpcIdentifiers()));
109   service_appeared_callback.Run();
110 
111   provider_.pending_devices_[GetTestLinkName(2)] = GetTestPath(2);
112   provider_.Stop();
113   EXPECT_FALSE(provider_.wimax_manager_proxy_.get());
114   EXPECT_TRUE(provider_.pending_devices_.empty());
115 }
116 
TEST_F(WiMaxProviderTest,ConnectDisconnectWiMaxManager)117 TEST_F(WiMaxProviderTest, ConnectDisconnectWiMaxManager) {
118   MockWiMaxManagerProxy* wimax_manager_proxy = new MockWiMaxManagerProxy();
119   provider_.wimax_manager_proxy_.reset(wimax_manager_proxy);
120 
121   EXPECT_CALL(*wimax_manager_proxy, Devices(_))
122       .WillOnce(Return(RpcIdentifiers()));
123   provider_.ConnectToWiMaxManager();
124 
125   provider_.pending_devices_[GetTestLinkName(2)] = GetTestPath(2);
126   provider_.DisconnectFromWiMaxManager();
127   EXPECT_TRUE(provider_.pending_devices_.empty());
128 }
129 
TEST_F(WiMaxProviderTest,OnDevicesChanged)130 TEST_F(WiMaxProviderTest, OnDevicesChanged) {
131   EXPECT_CALL(manager_, device_info()).WillRepeatedly(Return(&device_info_));
132 
133   provider_.pending_devices_[GetTestLinkName(1)] = GetTestPath(1);
134   RpcIdentifiers live_devices;
135   live_devices.push_back(GetTestPath(2));
136   live_devices.push_back(GetTestPath(3));
137   EXPECT_CALL(device_info_, GetIndex(GetTestLinkName(2))).WillOnce(Return(-1));
138   EXPECT_CALL(device_info_, GetIndex(GetTestLinkName(3))).WillOnce(Return(-1));
139   provider_.OnDevicesChanged(live_devices);
140   ASSERT_EQ(2, provider_.pending_devices_.size());
141   EXPECT_EQ(GetTestPath(2), provider_.pending_devices_[GetTestLinkName(2)]);
142   EXPECT_EQ(GetTestPath(3), provider_.pending_devices_[GetTestLinkName(3)]);
143 }
144 
TEST_F(WiMaxProviderTest,OnDeviceInfoAvailable)145 TEST_F(WiMaxProviderTest, OnDeviceInfoAvailable) {
146   EXPECT_CALL(manager_, device_info()).WillRepeatedly(Return(&device_info_));
147 
148   provider_.pending_devices_[GetTestLinkName(1)] = GetTestPath(1);
149   EXPECT_CALL(device_info_, GetIndex(GetTestLinkName(1))).WillOnce(Return(1));
150   EXPECT_CALL(device_info_, GetMACAddress(1, _)).WillOnce(Return(true));
151   EXPECT_CALL(device_info_, RegisterDevice(_));
152   provider_.OnDeviceInfoAvailable(GetTestLinkName(1));
153   EXPECT_TRUE(provider_.pending_devices_.empty());
154   ASSERT_EQ(1, provider_.devices_.size());
155   ASSERT_TRUE(ContainsKey(provider_.devices_, GetTestLinkName(1)));
156   EXPECT_EQ(GetTestLinkName(1),
157             provider_.devices_[GetTestLinkName(1)]->link_name());
158 }
159 
TEST_F(WiMaxProviderTest,CreateDevice)160 TEST_F(WiMaxProviderTest, CreateDevice) {
161   EXPECT_CALL(manager_, device_info()).WillRepeatedly(Return(&device_info_));
162 
163   EXPECT_CALL(device_info_, GetIndex(GetTestLinkName(1))).WillOnce(Return(-1));
164   provider_.CreateDevice(GetTestLinkName(1), GetTestPath(1));
165   EXPECT_TRUE(provider_.devices_.empty());
166   ASSERT_EQ(1, provider_.pending_devices_.size());
167   EXPECT_EQ(GetTestPath(1), provider_.pending_devices_[GetTestLinkName(1)]);
168 
169   EXPECT_CALL(device_info_, GetIndex(GetTestLinkName(1))).WillOnce(Return(1));
170   EXPECT_CALL(device_info_, GetMACAddress(1, _)).WillOnce(Return(true));
171   EXPECT_CALL(device_info_, RegisterDevice(_));
172   provider_.CreateDevice(GetTestLinkName(1), GetTestPath(1));
173   EXPECT_TRUE(provider_.pending_devices_.empty());
174   ASSERT_EQ(1, provider_.devices_.size());
175   ASSERT_TRUE(ContainsKey(provider_.devices_, GetTestLinkName(1)));
176   EXPECT_EQ(GetTestLinkName(1),
177             provider_.devices_[GetTestLinkName(1)]->link_name());
178 
179   WiMax* device = provider_.devices_[GetTestLinkName(1)].get();
180   provider_.CreateDevice(GetTestLinkName(1), GetTestPath(1));
181   EXPECT_EQ(device, provider_.devices_[GetTestLinkName(1)].get());
182 }
183 
TEST_F(WiMaxProviderTest,DestroyDeadDevices)184 TEST_F(WiMaxProviderTest, DestroyDeadDevices) {
185   for (int i = 0; i < 4; i++) {
186     scoped_refptr<MockWiMax> device(
187         new MockWiMax(&control_, nullptr, &metrics_, &manager_,
188                       GetTestLinkName(i), "", i, GetTestPath(i)));
189     EXPECT_CALL(*device, OnDeviceVanished()).Times((i == 0 || i == 3) ? 0 : 1);
190     provider_.devices_[GetTestLinkName(i)] = device;
191   }
192   for (int i = 4; i < 8; i++) {
193     provider_.pending_devices_[GetTestLinkName(i)] = GetTestPath(i);
194   }
195   RpcIdentifiers live_devices;
196   live_devices.push_back(GetTestPath(0));
197   live_devices.push_back(GetTestPath(3));
198   live_devices.push_back(GetTestPath(4));
199   live_devices.push_back(GetTestPath(7));
200   live_devices.push_back(GetTestPath(123));
201   EXPECT_CALL(manager_, device_info())
202       .Times(2)
203       .WillRepeatedly(Return(&device_info_));
204   EXPECT_CALL(device_info_, DeregisterDevice(_)).Times(2);
205   provider_.DestroyDeadDevices(live_devices);
206   ASSERT_EQ(2, provider_.devices_.size());
207   EXPECT_TRUE(ContainsKey(provider_.devices_, GetTestLinkName(0)));
208   EXPECT_TRUE(ContainsKey(provider_.devices_, GetTestLinkName(3)));
209   EXPECT_EQ(2, provider_.pending_devices_.size());
210   EXPECT_TRUE(ContainsKey(provider_.pending_devices_, GetTestLinkName(4)));
211   EXPECT_TRUE(ContainsKey(provider_.pending_devices_, GetTestLinkName(7)));
212 }
213 
TEST_F(WiMaxProviderTest,GetLinkName)214 TEST_F(WiMaxProviderTest, GetLinkName) {
215   EXPECT_EQ("", provider_.GetLinkName("/random/path"));
216   EXPECT_EQ(GetTestLinkName(1), provider_.GetLinkName(GetTestPath(1)));
217 }
218 
TEST_F(WiMaxProviderTest,RetrieveNetworkInfo)219 TEST_F(WiMaxProviderTest, RetrieveNetworkInfo) {
220   static const char kName[] = "Default Network";
221   const uint32_t kIdentifier = 0xabcdef;
222   static const char kNetworkId[] = "00abcdef";
223   string network_path = GetTestNetworkPath(kIdentifier);
224   EXPECT_CALL(control_, CreateWiMaxNetworkProxy(network_path))
225       .WillOnce(ReturnAndReleasePointee(&network_proxy_));
226   EXPECT_CALL(*network_proxy_, Name(_)).WillOnce(Return(kName));
227   EXPECT_CALL(*network_proxy_, Identifier(_)).WillOnce(Return(kIdentifier));
228   provider_.RetrieveNetworkInfo(network_path);
229   EXPECT_EQ(1, provider_.networks_.size());
230   EXPECT_TRUE(ContainsKey(provider_.networks_, network_path));
231   EXPECT_EQ(kName, provider_.networks_[network_path].name);
232   EXPECT_EQ(kNetworkId, provider_.networks_[network_path].id);
233   provider_.RetrieveNetworkInfo(network_path);
234   EXPECT_EQ(1, provider_.networks_.size());
235 }
236 
TEST_F(WiMaxProviderTest,FindService)237 TEST_F(WiMaxProviderTest, FindService) {
238   EXPECT_FALSE(provider_.FindService("some_storage_id"));
239   scoped_refptr<MockWiMaxService> service(
240       new MockWiMaxService(&control_, nullptr, &metrics_, &manager_));
241   static const char kName[] = "WiMAX Network";
242   static const char kNetworkId[] = "76543210";
243   service->set_friendly_name(kName);
244   service->set_network_id(kNetworkId);
245   service->InitStorageIdentifier();
246   provider_.services_[service->GetStorageIdentifier()] = service;
247   EXPECT_EQ(service.get(),
248             provider_.FindService(
249                 WiMaxService::CreateStorageIdentifier(kNetworkId,
250                                                       kName)).get());
251   EXPECT_FALSE(provider_.FindService("some_storage_id"));
252 }
253 
TEST_F(WiMaxProviderTest,StartLiveServices)254 TEST_F(WiMaxProviderTest, StartLiveServices) {
255   const uint32_t kIdentifier = 0x1234567;
256   static const char kNetworkId[] = "01234567";
257   static const char kName[] = "Some WiMAX Provider";
258   vector<scoped_refptr<MockWiMaxService>> services(4);
259   for (size_t i = 0; i < services.size(); i++) {
260     services[i] =
261         new MockWiMaxService(&control_, nullptr, &metrics_, &manager_);
262     if (i == 0) {
263       services[0]->set_network_id("deadbeef");
264     } else {
265       services[i]->set_network_id(kNetworkId);
266     }
267     // Make services[3] the default service.
268     if (i == 3) {
269       services[i]->set_friendly_name(kName);
270     } else {
271       services[i]->set_friendly_name(
272           base::StringPrintf("Configured %d", static_cast<int>(i)));
273     }
274     services[i]->InitStorageIdentifier();
275     provider_.services_[services[i]->GetStorageIdentifier()] = services[i];
276   }
277   WiMaxProvider::NetworkInfo info;
278   info.id = kNetworkId;
279   info.name = kName;
280   provider_.networks_[GetTestNetworkPath(kIdentifier)] = info;
281   network_proxy_.reset();
282   EXPECT_CALL(*services[0], IsStarted()).Times(0);
283   EXPECT_CALL(*services[1], IsStarted()).WillOnce(Return(true));
284   EXPECT_CALL(*services[1], Start(_)).Times(0);
285   EXPECT_CALL(*services[2], IsStarted()).WillOnce(Return(false));
286   EXPECT_CALL(*services[2], Start(_)).WillOnce(Return(true));
287   EXPECT_CALL(*services[3], IsStarted()).WillOnce(Return(false));
288   EXPECT_CALL(*services[3], Start(_)).WillOnce(Return(false));
289   EXPECT_CALL(manager_, RegisterService(_)).Times(0);
290   provider_.StartLiveServices();
291   EXPECT_FALSE(services[0]->is_default());
292   EXPECT_FALSE(services[1]->is_default());
293   EXPECT_FALSE(services[2]->is_default());
294   EXPECT_TRUE(services[3]->is_default());
295 }
296 
TEST_F(WiMaxProviderTest,DestroyAllServices)297 TEST_F(WiMaxProviderTest, DestroyAllServices) {
298   vector<scoped_refptr<MockWiMaxService>> services(2);
299   for (size_t i = 0; i < services.size(); i++) {
300     services[i] =
301         new MockWiMaxService(&control_, nullptr, &metrics_, &manager_);
302     provider_.services_[services[i]->GetStorageIdentifier()] = services[i];
303     EXPECT_CALL(*services[i], Stop());
304   }
305   EXPECT_CALL(manager_, DeregisterService(_)).Times(services.size());
306   provider_.DestroyAllServices();
307   EXPECT_TRUE(provider_.services_.empty());
308 }
309 
TEST_F(WiMaxProviderTest,StopDeadServices)310 TEST_F(WiMaxProviderTest, StopDeadServices) {
311   vector<scoped_refptr<MockWiMaxService>> services(4);
312   for (size_t i = 0; i < services.size(); i++) {
313     services[i] =
314         new MockWiMaxService(&control_, nullptr, &metrics_, &manager_);
315     if (i == 0) {
316       EXPECT_CALL(*services[i], IsStarted()).WillOnce(Return(false));
317       EXPECT_CALL(*services[i], GetNetworkObjectPath()).Times(0);
318       EXPECT_CALL(*services[i], Stop()).Times(0);
319     } else {
320       EXPECT_CALL(*services[i], IsStarted()).WillOnce(Return(true));
321       EXPECT_CALL(*services[i], GetNetworkObjectPath())
322           .WillOnce(Return(GetTestNetworkPath(100 + i)));
323     }
324     provider_.services_[services[i]->GetStorageIdentifier()] = services[i];
325   }
326   services[3]->set_is_default(true);
327   EXPECT_CALL(*services[1], Stop()).Times(0);
328   EXPECT_CALL(*services[2], Stop());
329   EXPECT_CALL(*services[3], Stop());
330   EXPECT_CALL(manager_, DeregisterService(_));
331   provider_.networks_[GetTestNetworkPath(777)].id = "01234567";
332   provider_.networks_[GetTestNetworkPath(101)].id = "12345678";
333   provider_.StopDeadServices();
334   EXPECT_EQ(3, provider_.services_.size());
335   EXPECT_FALSE(ContainsKey(provider_.services_,
336                            services[3]->GetStorageIdentifier()));
337 }
338 
TEST_F(WiMaxProviderTest,OnNetworksChanged)339 TEST_F(WiMaxProviderTest, OnNetworksChanged) {
340   static const char kName[] = "Default Network";
341   const uint32_t kIdentifier = 0xabcdef;
342   static const char kNetworkId[] = "00abcdef";
343 
344   // Started service to be stopped.
345   scoped_refptr<MockWiMaxService> service0(
346       new MockWiMaxService(&control_, nullptr, &metrics_, &manager_));
347   EXPECT_CALL(*service0, IsStarted()).WillOnce(Return(true));
348   EXPECT_CALL(*service0, GetNetworkObjectPath())
349       .WillOnce(Return(GetTestNetworkPath(100)));
350   EXPECT_CALL(*service0, Start(_)).Times(0);
351   EXPECT_CALL(*service0, Stop()).Times(1);
352   service0->set_network_id("1234");
353   service0->InitStorageIdentifier();
354 
355   // Stopped service to be started.
356   scoped_refptr<MockWiMaxService> service1(
357       new MockWiMaxService(&control_, nullptr, &metrics_, &manager_));
358   EXPECT_CALL(*service1, IsStarted()).Times(2).WillRepeatedly(Return(false));
359   EXPECT_CALL(*service1, Start(_)).WillOnce(Return(true));
360   EXPECT_CALL(*service1, Stop()).Times(0);
361   service1->set_network_id(kNetworkId);
362   service1->set_friendly_name(kName);
363   service1->InitStorageIdentifier();
364   EXPECT_CALL(control_, CreateWiMaxNetworkProxy(GetTestNetworkPath(101)))
365       .Times(2)
366       .WillOnce(ReturnAndReleasePointee(&network_proxy_))
367       .WillOnce(ReturnNull());
368   EXPECT_CALL(*network_proxy_, Name(_)).WillOnce(Return(kName));
369   EXPECT_CALL(*network_proxy_, Identifier(_)).WillOnce(Return(kIdentifier));
370 
371   provider_.services_[service0->GetStorageIdentifier()] = service0;
372   provider_.services_[service1->GetStorageIdentifier()] = service1;
373 
374   for (int i = 0; i < 3; i++) {
375     scoped_refptr<MockWiMax> device(
376         new MockWiMax(&control_, nullptr, &metrics_, &manager_,
377                       GetTestLinkName(i), "", i, GetTestPath(i)));
378     provider_.devices_[GetTestLinkName(i)] = device;
379     if (i > 0) {
380       device->networks_.insert(GetTestNetworkPath(101));
381     }
382   }
383   EXPECT_CALL(manager_, RegisterService(_)).Times(0);
384   EXPECT_CALL(manager_, DeregisterService(_)).Times(0);
385 
386   provider_.networks_["/org/chromium/foo"].id = "foo";
387   provider_.OnNetworksChanged();
388   EXPECT_EQ(1, provider_.networks_.size());
389   EXPECT_TRUE(ContainsKey(provider_.networks_, GetTestNetworkPath(101)));
390 }
391 
TEST_F(WiMaxProviderTest,GetUniqueService)392 TEST_F(WiMaxProviderTest, GetUniqueService) {
393   EXPECT_TRUE(provider_.services_.empty());
394 
395   static const char kName0[] = "Test WiMAX Network";
396   static const char kName1[] = "Unknown Network";
397   static const char kNetworkId[] = "12340000";
398 
399   // Service already exists.
400   scoped_refptr<MockWiMaxService> service0(
401       new MockWiMaxService(&control_, nullptr, &metrics_, &manager_));
402   service0->set_network_id(kNetworkId);
403   service0->set_friendly_name(kName0);
404   service0->InitStorageIdentifier();
405   provider_.services_[service0->GetStorageIdentifier()] = service0;
406   EXPECT_CALL(manager_, RegisterService(_)).Times(0);
407   WiMaxServiceRefPtr service = provider_.GetUniqueService(kNetworkId, kName0);
408   ASSERT_TRUE(service);
409   EXPECT_EQ(service0.get(), service.get());
410   EXPECT_EQ(1, provider_.services_.size());
411 
412   // Create a new service.
413   EXPECT_CALL(manager_, RegisterService(_));
414   service = provider_.GetUniqueService(kNetworkId, kName1);
415   ASSERT_TRUE(service);
416   EXPECT_NE(service0.get(), service.get());
417   EXPECT_EQ(2, provider_.services_.size());
418   EXPECT_EQ(WiMaxService::CreateStorageIdentifier(kNetworkId, kName1),
419             service->GetStorageIdentifier());
420   EXPECT_FALSE(service->is_default());
421 
422   // Service already exists -- it was just created.
423   EXPECT_CALL(manager_, RegisterService(_)).Times(0);
424   WiMaxServiceRefPtr service1 = provider_.GetUniqueService(kNetworkId, kName1);
425   ASSERT_TRUE(service1);
426   EXPECT_EQ(service.get(), service1.get());
427   EXPECT_EQ(2, provider_.services_.size());
428   EXPECT_FALSE(service->is_default());
429 }
430 
TEST_F(WiMaxProviderTest,CreateServicesFromProfile)431 TEST_F(WiMaxProviderTest, CreateServicesFromProfile) {
432   FakeStore store;
433   store.SetString("no_type", "Name", "No Type Entry");
434   store.SetString("no_wimax", "Type", "vpn");
435   store.SetString("wimax_network_01234567", "Name", "network");
436   store.SetString("wimax_network_01234567", "Type", "wimax");
437   store.SetString("wimax_network_01234567", "NetworkId", "01234567");
438   store.SetString("no_network_id", "Type", "wimax");
439   store.SetString("no_name", "Type", "wimax");
440   store.SetString("no_name", "NetworkId", "76543210");
441 
442   scoped_refptr<MockProfile> profile(
443       new MockProfile(&control_, &metrics_, &manager_));
444   EXPECT_CALL(*profile, GetConstStorage())
445       .Times(2)
446       .WillRepeatedly(Return(&store));
447   EXPECT_CALL(manager_, RegisterService(_));
448   EXPECT_CALL(*profile, ConfigureService(_)).WillOnce(Return(true));
449   provider_.CreateServicesFromProfile(profile);
450   ASSERT_EQ(1, provider_.services_.size());
451 
452   WiMaxServiceRefPtr service = provider_.services_.begin()->second;
453   EXPECT_EQ("wimax_network_01234567", service->GetStorageIdentifier());
454   provider_.CreateServicesFromProfile(profile);
455   ASSERT_EQ(1, provider_.services_.size());
456   EXPECT_EQ(service.get(), provider_.services_.begin()->second);
457 }
458 
TEST_F(WiMaxProviderTest,CreateTemporaryServiceFromProfile)459 TEST_F(WiMaxProviderTest, CreateTemporaryServiceFromProfile) {
460   FakeStore store;
461   store.SetString("no_type", "Name", "No Type Entry");
462   store.SetString("no_wimax", "Type", "vpn");
463   store.SetString("wimax_network_01234567", "Name", "network");
464   store.SetString("wimax_network_01234567", "Type", "wimax");
465   store.SetString("wimax_network_01234567", "NetworkId", "01234567");
466   store.SetString("no_network_id", "Type", "wimax");
467   store.SetString("no_name", "Type", "wimax");
468   store.SetString("no_name", "NetworkId", "76543210");
469   scoped_refptr<MockProfile> profile(
470       new MockProfile(&control_, &metrics_, &manager_));
471   EXPECT_CALL(*profile, GetConstStorage())
472       .WillRepeatedly(Return(&store));
473   Error error;
474 
475   // Network type not specified.
476   EXPECT_EQ(nullptr,
477             provider_.CreateTemporaryServiceFromProfile(profile,
478                                                         "no_type",
479                                                         &error));
480   EXPECT_FALSE(error.IsSuccess());
481   EXPECT_THAT(error.message(),
482               StartsWith("Unspecified or invalid network type"));
483 
484   // Not a WiMAX network.
485   error.Reset();
486   EXPECT_EQ(nullptr,
487             provider_.CreateTemporaryServiceFromProfile(profile,
488                                                         "no_wimax",
489                                                         &error));
490   EXPECT_FALSE(error.IsSuccess());
491   EXPECT_THAT(error.message(),
492               StartsWith("Unspecified or invalid network type"));
493 
494   // WiMAX network with required properties.
495   error.Reset();
496   EXPECT_TRUE(
497       provider_.CreateTemporaryServiceFromProfile(profile,
498                                                   "wimax_network_01234567",
499                                                   &error));
500   EXPECT_TRUE(error.IsSuccess());
501 
502   // Network ID not specified.
503   error.Reset();
504   EXPECT_EQ(nullptr,
505             provider_.CreateTemporaryServiceFromProfile(profile,
506                                                         "no_network_id",
507                                                         &error));
508   EXPECT_FALSE(error.IsSuccess());
509   EXPECT_THAT(error.message(),
510               StartsWith("Network ID not specified"));
511 
512   // Network name not specified.
513   error.Reset();
514   EXPECT_EQ(nullptr,
515             provider_.CreateTemporaryServiceFromProfile(profile,
516                                                         "no_name",
517                                                         &error));
518   EXPECT_FALSE(error.IsSuccess());
519   EXPECT_THAT(error.message(),
520               StartsWith("Network name not specified"));
521 }
522 
TEST_F(WiMaxProviderTest,GetService)523 TEST_F(WiMaxProviderTest, GetService) {
524   KeyValueStore args;
525   Error e;
526 
527   args.SetString(kTypeProperty, kTypeWimax);
528 
529   // No network id property.
530   ServiceRefPtr service = provider_.GetService(args, &e);
531   EXPECT_EQ(Error::kInvalidArguments, e.type());
532   EXPECT_FALSE(service);
533 
534   // No name property.
535   static const char kNetworkId[] = "1234abcd";
536   args.SetString(WiMaxService::kNetworkIdProperty, kNetworkId);
537   e.Reset();
538   service = provider_.GetService(args, &e);
539   EXPECT_EQ(Error::kInvalidArguments, e.type());
540   EXPECT_FALSE(service);
541 
542   // Service created and configured.
543   static const char kName[] = "Test WiMAX Network";
544   args.SetString(kNameProperty, kName);
545   static const char kIdentity[] = "joe";
546   args.SetString(kEapIdentityProperty, kIdentity);
547 
548   e.Reset();
549   service = provider_.FindSimilarService(args, &e);
550   EXPECT_EQ(ServiceRefPtr(), service);
551   EXPECT_EQ(Error::kNotFound, e.type());
552 
553   e.Reset();
554   EXPECT_CALL(manager_, RegisterService(_));
555   service = provider_.GetService(args, &e);
556   EXPECT_TRUE(e.IsSuccess());
557   ASSERT_TRUE(service);
558   testing::Mock::VerifyAndClearExpectations(&manager_);
559 
560   // GetService should create a service with only identifying parameters set.
561   EXPECT_EQ(kName, GetServiceFriendlyName(service));
562   EXPECT_EQ("", service->eap()->identity());
563 
564   e.Reset();
565   ServiceRefPtr similar_service = provider_.FindSimilarService(args, &e);
566   EXPECT_EQ(service, similar_service);
567   EXPECT_TRUE(e.IsSuccess());
568 
569   // After configuring the service, other parameters should be set.
570   service->Configure(args, &e);
571   EXPECT_TRUE(e.IsSuccess());
572   EXPECT_EQ(kIdentity, service->eap()->identity());
573 
574   e.Reset();
575   EXPECT_CALL(manager_, RegisterService(_)).Times(0);
576   ServiceRefPtr temporary_service = provider_.CreateTemporaryService(args, &e);
577   EXPECT_NE(ServiceRefPtr(), temporary_service);
578   EXPECT_NE(service, temporary_service);
579   EXPECT_TRUE(e.IsSuccess());
580 }
581 
TEST_F(WiMaxProviderTest,SelectCarrier)582 TEST_F(WiMaxProviderTest, SelectCarrier) {
583   scoped_refptr<MockWiMaxService> service(
584       new MockWiMaxService(&control_, nullptr, &metrics_, &manager_));
585   EXPECT_FALSE(provider_.SelectCarrier(service));
586   scoped_refptr<MockWiMax> device(
587       new MockWiMax(&control_, nullptr, &metrics_, &manager_,
588                     GetTestLinkName(1), "", 1, GetTestPath(1)));
589   provider_.devices_[GetTestLinkName(1)] = device;
590   WiMaxRefPtr carrier = provider_.SelectCarrier(service);
591   EXPECT_EQ(device.get(), carrier.get());
592 }
593 
TEST_F(WiMaxProviderTest,OnServiceUnloaded)594 TEST_F(WiMaxProviderTest, OnServiceUnloaded) {
595   scoped_refptr<MockWiMaxService> service(
596       new MockWiMaxService(&control_, nullptr, &metrics_, &manager_));
597   EXPECT_FALSE(service->is_default());
598   scoped_refptr<MockWiMaxService> service_default(
599       new MockWiMaxService(&control_, nullptr, &metrics_, &manager_));
600   service_default->set_is_default(true);
601   provider_.services_[service->GetStorageIdentifier()] = service;
602   provider_.services_[service_default->GetStorageIdentifier()] =
603       service_default;
604   EXPECT_CALL(manager_, DeregisterService(_)).Times(0);
605   EXPECT_FALSE(provider_.OnServiceUnloaded(service_default));
606   EXPECT_EQ(2, provider_.services_.size());
607   EXPECT_TRUE(provider_.OnServiceUnloaded(service));
608   EXPECT_EQ(1, provider_.services_.size());
609   EXPECT_EQ(service_default.get(), provider_.services_.begin()->second.get());
610 }
611 
612 }  // namespace shill
613