1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <stddef.h>
#include <map>
#include <set>
#include "base/bind.h"
#include "base/json/json_writer.h"
#include "base/location.h"
#include "base/macros.h"
#include "base/message_loop/message_loop.h"
#include "base/single_thread_task_runner.h"
#include "base/stl_util.h"
#include "base/strings/string_piece.h"
#include "base/values.h"
#include "chromeos/dbus/dbus_thread_manager.h"
#include "chromeos/dbus/mock_shill_manager_client.h"
#include "chromeos/dbus/mock_shill_profile_client.h"
#include "chromeos/dbus/mock_shill_service_client.h"
#include "chromeos/network/network_configuration_handler.h"
#include "chromeos/network/network_configuration_observer.h"
#include "chromeos/network/network_profile_handler.h"
#include "chromeos/network/network_state.h"
#include "chromeos/network/network_state_handler.h"
#include "chromeos/network/network_state_handler_observer.h"
#include "chromeos/network/shill_property_util.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/cros_system_api/dbus/service_constants.h"
using ::testing::_;
using ::testing::AnyNumber;
using ::testing::Invoke;
using ::testing::Pointee;
using ::testing::Return;
using ::testing::SaveArg;
using ::testing::StrEq;
// Matcher to match base::Value.
MATCHER_P(IsEqualTo, value, "") {
return arg.Equals(value);
}
namespace chromeos {
namespace {
static std::string PrettyJson(const base::DictionaryValue& value) {
std::string pretty;
base::JSONWriter::WriteWithOptions(
value, base::JSONWriter::OPTIONS_PRETTY_PRINT, &pretty);
return pretty;
}
void DictionaryValueCallback(const std::string& expected_id,
const std::string& expected_json,
const std::string& service_path,
const base::DictionaryValue& dictionary) {
std::string dict_str = PrettyJson(dictionary);
EXPECT_EQ(expected_json, dict_str);
EXPECT_EQ(expected_id, service_path);
}
void ErrorCallback(bool error_expected,
const std::string& expected_id,
const std::string& error_name,
scoped_ptr<base::DictionaryValue> error_data) {
EXPECT_TRUE(error_expected) << "Unexpected error: " << error_name
<< " with associated data: \n"
<< PrettyJson(*error_data);
}
void StringResultCallback(const std::string& expected_result,
const std::string& result) {
EXPECT_EQ(expected_result, result);
}
void DBusErrorCallback(const std::string& error_name,
const std::string& error_message) {
EXPECT_TRUE(false) << "DBus Error: " << error_name << "(" << error_message
<< ")";
}
class TestCallback {
public:
TestCallback() : run_count_(0) {}
void Run() { ++run_count_; }
int run_count() const { return run_count_; }
private:
int run_count_;
};
class TestNetworkConfigurationObserver : public NetworkConfigurationObserver {
public:
TestNetworkConfigurationObserver() {}
~TestNetworkConfigurationObserver() override {
STLDeleteContainerPairSecondPointers(configurations_.begin(),
configurations_.end());
}
// NetworkConfigurationObserver
void OnConfigurationCreated(
const std::string& service_path,
const std::string& profile_path,
const base::DictionaryValue& properties,
NetworkConfigurationObserver::Source source) override {
ASSERT_EQ(0u, configurations_.count(service_path));
configurations_[service_path] = properties.DeepCopy();
profiles_[profile_path].insert(service_path);
}
void OnConfigurationRemoved(
const std::string& service_path,
const std::string& guid,
NetworkConfigurationObserver::Source source) override {
ASSERT_EQ(1u, configurations_.count(service_path));
delete configurations_[service_path];
configurations_.erase(service_path);
for (auto& p : profiles_) {
p.second.erase(service_path);
}
}
void OnConfigurationProfileChanged(
const std::string& service_path,
const std::string& profile_path,
NetworkConfigurationObserver::Source source) override {
for (auto& p : profiles_) {
p.second.erase(service_path);
}
profiles_[profile_path].insert(service_path);
}
void OnPropertiesSet(const std::string& service_path,
const std::string& guid,
const base::DictionaryValue& set_properties,
NetworkConfigurationObserver::Source source) override {
configurations_[service_path]->MergeDictionary(&set_properties);
}
bool HasConfiguration(const std::string& service_path) {
return configurations_.count(service_path) == 1;
}
std::string GetStringProperty(const std::string& service_path,
const std::string& property) {
if (!HasConfiguration(service_path))
return "";
std::string result;
configurations_[service_path]->GetStringWithoutPathExpansion(property,
&result);
return result;
}
bool HasConfigurationInProfile(const std::string& service_path,
const std::string& profile_path) {
if (profiles_.count(profile_path) == 0)
return false;
return profiles_[profile_path].count(service_path) == 1;
}
private:
std::map<std::string, base::DictionaryValue*> configurations_;
std::map<std::string, std::set<std::string>> profiles_;
DISALLOW_COPY_AND_ASSIGN(TestNetworkConfigurationObserver);
};
} // namespace
class NetworkConfigurationHandlerTest : public testing::Test {
public:
NetworkConfigurationHandlerTest()
: mock_manager_client_(NULL),
mock_profile_client_(NULL),
mock_service_client_(NULL),
dictionary_value_result_(NULL) {}
~NetworkConfigurationHandlerTest() override {}
void SetUp() override {
scoped_ptr<DBusThreadManagerSetter> dbus_setter =
DBusThreadManager::GetSetterForTesting();
mock_manager_client_ = new MockShillManagerClient();
mock_profile_client_ = new MockShillProfileClient();
mock_service_client_ = new MockShillServiceClient();
dbus_setter->SetShillManagerClient(
scoped_ptr<ShillManagerClient>(mock_manager_client_));
dbus_setter->SetShillProfileClient(
scoped_ptr<ShillProfileClient>(mock_profile_client_));
dbus_setter->SetShillServiceClient(
scoped_ptr<ShillServiceClient>(mock_service_client_));
EXPECT_CALL(*mock_service_client_, GetProperties(_, _)).Times(AnyNumber());
EXPECT_CALL(*mock_manager_client_, GetProperties(_)).Times(AnyNumber());
EXPECT_CALL(*mock_manager_client_, AddPropertyChangedObserver(_))
.Times(AnyNumber());
EXPECT_CALL(*mock_manager_client_, RemovePropertyChangedObserver(_))
.Times(AnyNumber());
network_state_handler_.reset(NetworkStateHandler::InitializeForTest());
network_configuration_handler_.reset(new NetworkConfigurationHandler());
network_configuration_handler_->Init(network_state_handler_.get(),
NULL /* network_device_handler */);
message_loop_.RunUntilIdle();
}
void TearDown() override {
network_configuration_handler_.reset();
network_state_handler_.reset();
DBusThreadManager::Shutdown();
}
// Handles responses for GetProperties method calls.
void OnGetProperties(
const dbus::ObjectPath& path,
const ShillClientHelper::DictionaryValueCallback& callback) {
callback.Run(DBUS_METHOD_CALL_SUCCESS, *dictionary_value_result_);
}
// Handles responses for SetProperties method calls.
void OnSetProperties(const dbus::ObjectPath& service_path,
const base::DictionaryValue& properties,
const base::Closure& callback,
const ShillClientHelper::ErrorCallback& error_callback) {
callback.Run();
}
// Handles responses for ClearProperties method calls.
void OnClearProperties(
const dbus::ObjectPath& service_path,
const std::vector<std::string>& names,
const ShillClientHelper::ListValueCallback& callback,
const ShillClientHelper::ErrorCallback& error_callback) {
base::ListValue result;
result.AppendBoolean(true);
callback.Run(result);
}
// Handles responses for ClearProperties method calls, and simulates an error
// result.
void OnClearPropertiesError(
const dbus::ObjectPath& service_path,
const std::vector<std::string>& names,
const ShillClientHelper::ListValueCallback& callback,
const ShillClientHelper::ErrorCallback& error_callback) {
base::ListValue result;
result.AppendBoolean(false);
callback.Run(result);
}
void OnConfigureService(
const dbus::ObjectPath& profile_path,
const base::DictionaryValue& properties,
const ObjectPathCallback& callback,
const ShillClientHelper::ErrorCallback& error_callback) {
callback.Run(dbus::ObjectPath("/service/2"));
}
void OnGetLoadableProfileEntries(
const dbus::ObjectPath& service_path,
const ShillClientHelper::DictionaryValueCallback& callback) {
base::DictionaryValue entries;
entries.SetString("profile1", "entry1");
entries.SetString("profile2", "entry2");
callback.Run(DBUS_METHOD_CALL_SUCCESS, entries);
}
void OnDeleteEntry(const dbus::ObjectPath& profile_path,
const std::string& entry_path,
const base::Closure& callback,
const ShillClientHelper::ErrorCallback& error_callback) {
// Don't run the callback immediately to emulate actual behavior.
message_loop_.task_runner()->PostTask(FROM_HERE, callback);
}
bool PendingProfileEntryDeleterForTest(const std::string& service_path) {
return network_configuration_handler_->PendingProfileEntryDeleterForTest(
service_path);
}
void CreateConfiguration(const std::string& service_path,
const base::DictionaryValue& properties) {
network_configuration_handler_->CreateShillConfiguration(
properties, NetworkConfigurationObserver::SOURCE_USER_ACTION,
base::Bind(&StringResultCallback, service_path),
base::Bind(&ErrorCallback, false, std::string()));
}
protected:
MockShillManagerClient* mock_manager_client_;
MockShillProfileClient* mock_profile_client_;
MockShillServiceClient* mock_service_client_;
scoped_ptr<NetworkStateHandler> network_state_handler_;
scoped_ptr<NetworkConfigurationHandler> network_configuration_handler_;
base::MessageLoopForUI message_loop_;
base::DictionaryValue* dictionary_value_result_;
};
TEST_F(NetworkConfigurationHandlerTest, GetProperties) {
std::string service_path = "/service/1";
std::string expected_json = "{\n \"SSID\": \"MyNetwork\"\n}\n";
std::string networkName = "MyNetwork";
std::string key = "SSID";
scoped_ptr<base::StringValue> networkNameValue(
new base::StringValue(networkName));
base::DictionaryValue value;
value.Set(key, new base::StringValue(networkName));
dictionary_value_result_ = &value;
EXPECT_CALL(*mock_service_client_,
SetProperty(dbus::ObjectPath(service_path), key,
IsEqualTo(networkNameValue.get()), _, _)).Times(1);
mock_service_client_->SetProperty(
dbus::ObjectPath(service_path), key, *networkNameValue,
base::Bind(&base::DoNothing), base::Bind(&DBusErrorCallback));
message_loop_.RunUntilIdle();
ShillServiceClient::DictionaryValueCallback get_properties_callback;
EXPECT_CALL(*mock_service_client_, GetProperties(_, _))
.WillOnce(
Invoke(this, &NetworkConfigurationHandlerTest::OnGetProperties));
network_configuration_handler_->GetShillProperties(
service_path,
base::Bind(&DictionaryValueCallback, service_path, expected_json),
base::Bind(&ErrorCallback, false, service_path));
message_loop_.RunUntilIdle();
}
TEST_F(NetworkConfigurationHandlerTest, SetProperties) {
std::string service_path = "/service/1";
std::string networkName = "MyNetwork";
std::string key = "SSID";
scoped_ptr<base::StringValue> networkNameValue(
new base::StringValue(networkName));
base::DictionaryValue value;
value.Set(key, new base::StringValue(networkName));
dictionary_value_result_ = &value;
EXPECT_CALL(*mock_service_client_, SetProperties(_, _, _, _))
.WillOnce(
Invoke(this, &NetworkConfigurationHandlerTest::OnSetProperties));
network_configuration_handler_->SetShillProperties(
service_path, value, NetworkConfigurationObserver::SOURCE_USER_ACTION,
base::Bind(&base::DoNothing),
base::Bind(&ErrorCallback, false, service_path));
message_loop_.RunUntilIdle();
}
TEST_F(NetworkConfigurationHandlerTest, ClearProperties) {
std::string service_path = "/service/1";
std::string networkName = "MyNetwork";
std::string key = "SSID";
scoped_ptr<base::StringValue> networkNameValue(
new base::StringValue(networkName));
// First set up a value to clear.
base::DictionaryValue value;
value.Set(key, new base::StringValue(networkName));
dictionary_value_result_ = &value;
EXPECT_CALL(*mock_service_client_, SetProperties(_, _, _, _))
.WillOnce(
Invoke(this, &NetworkConfigurationHandlerTest::OnSetProperties));
network_configuration_handler_->SetShillProperties(
service_path, value, NetworkConfigurationObserver::SOURCE_USER_ACTION,
base::Bind(&base::DoNothing),
base::Bind(&ErrorCallback, false, service_path));
message_loop_.RunUntilIdle();
// Now clear it.
std::vector<std::string> values_to_clear;
values_to_clear.push_back(key);
EXPECT_CALL(*mock_service_client_, ClearProperties(_, _, _, _))
.WillOnce(
Invoke(this, &NetworkConfigurationHandlerTest::OnClearProperties));
network_configuration_handler_->ClearShillProperties(
service_path, values_to_clear, base::Bind(&base::DoNothing),
base::Bind(&ErrorCallback, false, service_path));
message_loop_.RunUntilIdle();
}
TEST_F(NetworkConfigurationHandlerTest, ClearPropertiesError) {
std::string service_path = "/service/1";
std::string networkName = "MyNetwork";
std::string key = "SSID";
scoped_ptr<base::StringValue> networkNameValue(
new base::StringValue(networkName));
// First set up a value to clear.
base::DictionaryValue value;
value.Set(key, new base::StringValue(networkName));
dictionary_value_result_ = &value;
EXPECT_CALL(*mock_service_client_, SetProperties(_, _, _, _))
.WillOnce(
Invoke(this, &NetworkConfigurationHandlerTest::OnSetProperties));
network_configuration_handler_->SetShillProperties(
service_path, value, NetworkConfigurationObserver::SOURCE_USER_ACTION,
base::Bind(&base::DoNothing),
base::Bind(&ErrorCallback, false, service_path));
message_loop_.RunUntilIdle();
// Now clear it.
std::vector<std::string> values_to_clear;
values_to_clear.push_back(key);
EXPECT_CALL(*mock_service_client_, ClearProperties(_, _, _, _))
.WillOnce(Invoke(
this, &NetworkConfigurationHandlerTest::OnClearPropertiesError));
network_configuration_handler_->ClearShillProperties(
service_path, values_to_clear, base::Bind(&base::DoNothing),
base::Bind(&ErrorCallback, true, service_path));
message_loop_.RunUntilIdle();
}
TEST_F(NetworkConfigurationHandlerTest, CreateConfiguration) {
std::string networkName = "MyNetwork";
std::string key = "SSID";
std::string type = "wifi";
std::string profile = "profile path";
base::DictionaryValue value;
shill_property_util::SetSSID(networkName, &value);
value.SetWithoutPathExpansion(shill::kTypeProperty,
new base::StringValue(type));
value.SetWithoutPathExpansion(shill::kProfileProperty,
new base::StringValue(profile));
EXPECT_CALL(*mock_manager_client_,
ConfigureServiceForProfile(dbus::ObjectPath(profile), _, _, _))
.WillOnce(
Invoke(this, &NetworkConfigurationHandlerTest::OnConfigureService));
CreateConfiguration("/service/2", value);
message_loop_.RunUntilIdle();
}
TEST_F(NetworkConfigurationHandlerTest, RemoveConfiguration) {
std::string service_path = "/service/1";
TestCallback test_callback;
EXPECT_CALL(*mock_service_client_, GetLoadableProfileEntries(_, _))
.WillOnce(Invoke(
this, &NetworkConfigurationHandlerTest::OnGetLoadableProfileEntries));
EXPECT_CALL(*mock_profile_client_, DeleteEntry(_, _, _, _))
.WillRepeatedly(
Invoke(this, &NetworkConfigurationHandlerTest::OnDeleteEntry));
network_configuration_handler_->RemoveConfiguration(
service_path, NetworkConfigurationObserver::SOURCE_USER_ACTION,
base::Bind(&TestCallback::Run, base::Unretained(&test_callback)),
base::Bind(&ErrorCallback, false, service_path));
message_loop_.RunUntilIdle();
EXPECT_EQ(1, test_callback.run_count());
EXPECT_FALSE(PendingProfileEntryDeleterForTest(service_path));
}
////////////////////////////////////////////////////////////////////////////////
// Stub based tests
namespace {
class TestObserver : public chromeos::NetworkStateHandlerObserver {
public:
TestObserver() : network_list_changed_count_(0) {}
~TestObserver() override {}
void NetworkListChanged() override { ++network_list_changed_count_; }
void NetworkPropertiesUpdated(const NetworkState* network) override {
property_updates_[network->path()]++;
}
size_t network_list_changed_count() { return network_list_changed_count_; }
int PropertyUpdatesForService(const std::string& service_path) {
return property_updates_[service_path];
}
void ClearPropertyUpdates() { property_updates_.clear(); }
private:
size_t network_list_changed_count_;
std::map<std::string, int> property_updates_;
DISALLOW_COPY_AND_ASSIGN(TestObserver);
};
} // namespace
class NetworkConfigurationHandlerStubTest : public testing::Test {
public:
NetworkConfigurationHandlerStubTest() {}
~NetworkConfigurationHandlerStubTest() override {}
void SetUp() override {
DBusThreadManager::Initialize();
network_state_handler_.reset(NetworkStateHandler::InitializeForTest());
test_observer_.reset(new TestObserver());
network_state_handler_->AddObserver(test_observer_.get(), FROM_HERE);
network_configuration_handler_.reset(new NetworkConfigurationHandler());
network_configuration_handler_->Init(network_state_handler_.get(),
NULL /* network_device_handler */);
message_loop_.RunUntilIdle();
test_observer_->ClearPropertyUpdates();
}
void TearDown() override {
network_configuration_handler_.reset();
network_state_handler_->RemoveObserver(test_observer_.get(), FROM_HERE);
network_state_handler_.reset();
DBusThreadManager::Shutdown();
}
void SuccessCallback(const std::string& callback_name) {
success_callback_name_ = callback_name;
}
void GetPropertiesCallback(const std::string& service_path,
const base::DictionaryValue& dictionary) {
get_properties_path_ = service_path;
get_properties_.reset(dictionary.DeepCopy());
}
void CreateConfigurationCallback(const std::string& service_path) {
create_service_path_ = service_path;
}
void CreateTestConfiguration(const std::string& service_path,
const std::string& type) {
base::DictionaryValue properties;
shill_property_util::SetSSID(service_path, &properties);
properties.SetStringWithoutPathExpansion(shill::kNameProperty,
service_path);
properties.SetStringWithoutPathExpansion(shill::kGuidProperty,
service_path);
properties.SetStringWithoutPathExpansion(shill::kTypeProperty, type);
properties.SetStringWithoutPathExpansion(shill::kStateProperty,
shill::kStateIdle);
properties.SetStringWithoutPathExpansion(
shill::kProfileProperty, NetworkProfileHandler::GetSharedProfilePath());
network_configuration_handler_->CreateShillConfiguration(
properties, NetworkConfigurationObserver::SOURCE_USER_ACTION,
base::Bind(
&NetworkConfigurationHandlerStubTest::CreateConfigurationCallback,
base::Unretained(this)),
base::Bind(&ErrorCallback, false, service_path));
message_loop_.RunUntilIdle();
}
protected:
bool GetServiceStringProperty(const std::string& service_path,
const std::string& key,
std::string* result) {
ShillServiceClient::TestInterface* service_test =
DBusThreadManager::Get()->GetShillServiceClient()->GetTestInterface();
const base::DictionaryValue* properties =
service_test->GetServiceProperties(service_path);
if (properties && properties->GetStringWithoutPathExpansion(key, result))
return true;
return false;
}
bool GetReceivedStringProperty(const std::string& service_path,
const std::string& key,
std::string* result) {
if (get_properties_path_ != service_path)
return false;
if (get_properties_ &&
get_properties_->GetStringWithoutPathExpansion(key, result))
return true;
return false;
}
scoped_ptr<NetworkStateHandler> network_state_handler_;
scoped_ptr<NetworkConfigurationHandler> network_configuration_handler_;
scoped_ptr<TestObserver> test_observer_;
base::MessageLoopForUI message_loop_;
std::string success_callback_name_;
std::string get_properties_path_;
scoped_ptr<base::DictionaryValue> get_properties_;
std::string create_service_path_;
};
TEST_F(NetworkConfigurationHandlerStubTest, StubSetAndClearProperties) {
// TODO(stevenjb): Remove dependency on default Stub service.
const std::string service_path("/service/wifi1");
const std::string test_identity("test_identity");
const std::string test_passphrase("test_passphrase");
// Set Properties
base::DictionaryValue properties_to_set;
properties_to_set.SetStringWithoutPathExpansion(shill::kIdentityProperty,
test_identity);
properties_to_set.SetStringWithoutPathExpansion(shill::kPassphraseProperty,
test_passphrase);
network_configuration_handler_->SetShillProperties(
service_path, properties_to_set,
NetworkConfigurationObserver::SOURCE_USER_ACTION,
base::Bind(&NetworkConfigurationHandlerStubTest::SuccessCallback,
base::Unretained(this), "SetProperties"),
base::Bind(&ErrorCallback, false, service_path));
message_loop_.RunUntilIdle();
EXPECT_EQ("SetProperties", success_callback_name_);
std::string identity, passphrase;
EXPECT_TRUE(GetServiceStringProperty(service_path, shill::kIdentityProperty,
&identity));
EXPECT_TRUE(GetServiceStringProperty(service_path, shill::kPassphraseProperty,
&passphrase));
EXPECT_EQ(test_identity, identity);
EXPECT_EQ(test_passphrase, passphrase);
EXPECT_EQ(1, test_observer_->PropertyUpdatesForService(service_path));
// Clear Properties
std::vector<std::string> properties_to_clear;
properties_to_clear.push_back(shill::kIdentityProperty);
properties_to_clear.push_back(shill::kPassphraseProperty);
network_configuration_handler_->ClearShillProperties(
service_path, properties_to_clear,
base::Bind(&NetworkConfigurationHandlerStubTest::SuccessCallback,
base::Unretained(this), "ClearProperties"),
base::Bind(&ErrorCallback, false, service_path));
message_loop_.RunUntilIdle();
EXPECT_EQ("ClearProperties", success_callback_name_);
EXPECT_FALSE(GetServiceStringProperty(service_path, shill::kIdentityProperty,
&identity));
EXPECT_FALSE(GetServiceStringProperty(service_path, shill::kIdentityProperty,
&passphrase));
EXPECT_EQ(2, test_observer_->PropertyUpdatesForService(service_path));
}
TEST_F(NetworkConfigurationHandlerStubTest, StubGetNameFromWifiHex) {
// TODO(stevenjb): Remove dependency on default Stub service.
const std::string service_path("/service/wifi1");
std::string wifi_hex = "5468697320697320484558205353494421";
std::string expected_name = "This is HEX SSID!";
// Set Properties
base::DictionaryValue properties_to_set;
properties_to_set.SetStringWithoutPathExpansion(shill::kWifiHexSsid,
wifi_hex);
network_configuration_handler_->SetShillProperties(
service_path, properties_to_set,
NetworkConfigurationObserver::SOURCE_USER_ACTION,
base::Bind(&base::DoNothing),
base::Bind(&ErrorCallback, false, service_path));
message_loop_.RunUntilIdle();
std::string wifi_hex_result;
EXPECT_TRUE(GetServiceStringProperty(service_path, shill::kWifiHexSsid,
&wifi_hex_result));
EXPECT_EQ(wifi_hex, wifi_hex_result);
// Get Properties
network_configuration_handler_->GetShillProperties(
service_path,
base::Bind(&NetworkConfigurationHandlerStubTest::GetPropertiesCallback,
base::Unretained(this)),
base::Bind(&ErrorCallback, false, service_path));
message_loop_.RunUntilIdle();
EXPECT_EQ(service_path, get_properties_path_);
std::string name_result;
EXPECT_TRUE(GetReceivedStringProperty(service_path, shill::kNameProperty,
&name_result));
EXPECT_EQ(expected_name, name_result);
}
TEST_F(NetworkConfigurationHandlerStubTest, StubCreateConfiguration) {
const std::string service_path("/service/test_wifi");
CreateTestConfiguration(service_path, shill::kTypeWifi);
EXPECT_FALSE(create_service_path_.empty());
std::string guid;
EXPECT_TRUE(GetServiceStringProperty(create_service_path_,
shill::kGuidProperty, &guid));
EXPECT_EQ(service_path, guid);
std::string actual_profile;
EXPECT_TRUE(GetServiceStringProperty(
create_service_path_, shill::kProfileProperty, &actual_profile));
EXPECT_EQ(NetworkProfileHandler::GetSharedProfilePath(), actual_profile);
}
TEST_F(NetworkConfigurationHandlerStubTest, NetworkConfigurationObserver) {
const std::string service_path("/service/test_wifi");
const std::string test_passphrase("test_passphrase");
scoped_ptr<TestNetworkConfigurationObserver> test_observer(
new TestNetworkConfigurationObserver);
network_configuration_handler_->AddObserver(test_observer.get());
CreateTestConfiguration(service_path, shill::kTypeWifi);
EXPECT_TRUE(test_observer->HasConfiguration(service_path));
EXPECT_TRUE(test_observer->HasConfigurationInProfile(
service_path, NetworkProfileHandler::GetSharedProfilePath()));
EXPECT_EQ(shill::kTypeWifi, test_observer->GetStringProperty(
service_path, shill::kTypeProperty));
base::DictionaryValue properties_to_set;
properties_to_set.SetStringWithoutPathExpansion(shill::kPassphraseProperty,
test_passphrase);
network_configuration_handler_->SetShillProperties(
service_path, properties_to_set,
NetworkConfigurationObserver::SOURCE_USER_ACTION,
base::Bind(&base::DoNothing),
base::Bind(&ErrorCallback, false, service_path));
message_loop_.RunUntilIdle();
EXPECT_EQ(test_passphrase, test_observer->GetStringProperty(
service_path, shill::kPassphraseProperty));
std::string user_profile = "/profiles/user1";
std::string userhash = "user1";
DBusThreadManager::Get()
->GetShillProfileClient()
->GetTestInterface()
->AddProfile(user_profile, userhash);
network_configuration_handler_->SetNetworkProfile(
service_path, user_profile,
NetworkConfigurationObserver::SOURCE_USER_ACTION,
base::Bind(&base::DoNothing),
base::Bind(&ErrorCallback, false, service_path));
message_loop_.RunUntilIdle();
EXPECT_TRUE(test_observer->HasConfiguration(service_path));
EXPECT_FALSE(test_observer->HasConfigurationInProfile(
service_path, NetworkProfileHandler::GetSharedProfilePath()));
EXPECT_TRUE(
test_observer->HasConfigurationInProfile(service_path, user_profile));
network_configuration_handler_->RemoveConfiguration(
service_path, NetworkConfigurationObserver::SOURCE_USER_ACTION,
base::Bind(&base::DoNothing),
base::Bind(&ErrorCallback, false, service_path));
message_loop_.RunUntilIdle();
EXPECT_FALSE(test_observer->HasConfiguration(service_path));
EXPECT_FALSE(test_observer->HasConfigurationInProfile(
service_path, NetworkProfileHandler::GetSharedProfilePath()));
EXPECT_FALSE(
test_observer->HasConfigurationInProfile(service_path, user_profile));
network_configuration_handler_->RemoveObserver(test_observer.get());
}
} // namespace chromeos
|