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 "chromeos/network/network_connection_handler.h" | |
6 | |
7 #include "base/bind.h" | |
8 #include "base/json/json_reader.h" | |
9 #include "base/memory/scoped_ptr.h" | |
10 #include "base/message_loop.h" | |
11 #include "chromeos/dbus/dbus_thread_manager.h" | |
12 #include "chromeos/dbus/shill_manager_client.h" | |
13 #include "chromeos/dbus/shill_service_client.h" | |
14 #include "chromeos/network/network_configuration_handler.h" | |
15 #include "chromeos/network/network_state_handler.h" | |
16 #include "testing/gtest/include/gtest/gtest.h" | |
17 #include "third_party/cros_system_api/dbus/service_constants.h" | |
18 | |
19 namespace { | |
20 | |
21 const char* kSuccessResult = "success"; | |
22 | |
23 } // namespace | |
24 | |
25 namespace chromeos { | |
26 | |
27 class NetworkConnectionHandlerTest : public testing::Test { | |
28 public: | |
29 NetworkConnectionHandlerTest() { | |
30 } | |
31 virtual ~NetworkConnectionHandlerTest() { | |
32 } | |
33 | |
34 virtual void SetUp() OVERRIDE { | |
35 // Initialize DBusThreadManager with a stub implementation. | |
36 DBusThreadManager::InitializeWithStub(); | |
37 message_loop_.RunUntilIdle(); | |
38 DBusThreadManager::Get()->GetShillServiceClient()->GetTestInterface() | |
39 ->ClearServices(); | |
40 message_loop_.RunUntilIdle(); | |
41 NetworkStateHandler::Initialize(); | |
42 NetworkConfigurationHandler::Initialize(); | |
43 NetworkConnectionHandler::Initialize(); | |
44 } | |
45 | |
46 virtual void TearDown() OVERRIDE { | |
47 NetworkConnectionHandler::Shutdown(); | |
48 NetworkConfigurationHandler::Shutdown(); | |
49 NetworkStateHandler::Shutdown(); | |
50 DBusThreadManager::Shutdown(); | |
51 } | |
52 | |
53 protected: | |
54 bool Configure(const std::string& json_string) { | |
55 base::Value* json = base::JSONReader::Read(json_string); | |
pneubeck (no reviews)
2013/05/07 08:46:39
network/onc/onc_utils.h: ReadDictionaryFromJson
wh
stevenjb
2013/05/08 01:57:22
Done.
| |
56 if (!json) { | |
57 LOG(ERROR) << "Error parsing json: " << json_string; | |
58 return false; | |
59 } | |
60 base::DictionaryValue* json_dict; | |
61 if (!json->GetAsDictionary(&json_dict)) { | |
62 LOG(ERROR) << "json is not a dictionary: " << json; | |
63 return false; | |
64 } | |
65 DBusThreadManager::Get()->GetShillManagerClient()->ConfigureService( | |
66 *json_dict, | |
67 ObjectPathCallback(), ShillManagerClient::ErrorCallback()); | |
68 message_loop_.RunUntilIdle(); | |
69 return true; | |
70 } | |
71 | |
72 void Connect(const std::string& service_path) { | |
73 NetworkConnectionHandler::Get()->ConnectToNetwork( | |
74 service_path, | |
75 base::Bind(&NetworkConnectionHandlerTest::SuccessCallback, | |
76 base::Unretained(this)), | |
77 base::Bind(&NetworkConnectionHandlerTest::ErrorCallback, | |
78 base::Unretained(this))); | |
79 message_loop_.RunUntilIdle(); | |
80 } | |
81 | |
82 void Disconnect(const std::string& service_path) { | |
83 NetworkConnectionHandler::Get()->DisconnectNetwork( | |
84 service_path, | |
85 base::Bind(&NetworkConnectionHandlerTest::SuccessCallback, | |
86 base::Unretained(this)), | |
87 base::Bind(&NetworkConnectionHandlerTest::ErrorCallback, | |
88 base::Unretained(this))); | |
89 message_loop_.RunUntilIdle(); | |
90 } | |
91 | |
92 void SuccessCallback() { | |
93 result_ = kSuccessResult; | |
94 } | |
95 | |
96 void ErrorCallback(const std::string& error_name, | |
97 scoped_ptr<base::DictionaryValue> error_data) { | |
98 result_ = error_name; | |
99 } | |
100 | |
101 std::string GetResultAndReset() { | |
102 std::string result = result_; | |
pneubeck (no reviews)
2013/05/07 08:46:39
nit: swap maybe
stevenjb
2013/05/08 01:57:22
Done.
| |
103 result_.clear(); | |
104 return result; | |
105 } | |
106 | |
107 MessageLoopForUI message_loop_; | |
108 std::string result_; | |
109 | |
110 private: | |
111 DISALLOW_COPY_AND_ASSIGN(NetworkConnectionHandlerTest); | |
112 }; | |
113 | |
114 namespace { | |
115 | |
116 const char* kConfigConnectable = | |
117 "{ \"GUID\": \"wifi0\", \"Type\": \"wifi\", \"State\": \"idle\" }"; | |
118 const char* kConfigConnected = | |
119 "{ \"GUID\": \"wifi1\", \"Type\": \"wifi\", \"State\": \"online\" }"; | |
120 const char* kConfigConnecting = | |
121 "{ \"GUID\": \"wifi2\", \"Type\": \"wifi\", \"State\": \"association\" }"; | |
122 const char* kConfigRequiresPassphrase = | |
123 "{ \"GUID\": \"wifi3\", \"Type\": \"wifi\", " | |
124 "\"PassphraseRequired\": true }"; | |
125 const char* kConfigRequiresActivation = | |
126 "{ \"GUID\": \"cellular1\", \"Type\": \"cellular\"," | |
127 " \"Cellular.ActivationState\": \"not-activated\" }"; | |
128 | |
129 } // namespace | |
130 | |
131 TEST_F(NetworkConnectionHandlerTest, NetworkConnectionHandlerConnectSuccess) { | |
pneubeck (no reviews)
2013/05/07 08:46:39
We should also test that at least a connect reques
stevenjb
2013/05/08 01:57:22
Done
| |
132 EXPECT_TRUE(Configure(kConfigConnectable)); | |
133 Connect("wifi0"); | |
134 EXPECT_EQ(kSuccessResult, GetResultAndReset()); | |
135 } | |
136 | |
137 // Handles basic failure cases. | |
138 TEST_F(NetworkConnectionHandlerTest, NetworkConnectionHandlerConnectFailure) { | |
139 Connect("no-network"); | |
140 EXPECT_EQ(NetworkConnectionHandler::kNetworkNotFound, GetResultAndReset()); | |
141 | |
142 EXPECT_TRUE(Configure(kConfigConnected)); | |
143 Connect("wifi1"); | |
144 EXPECT_EQ(NetworkConnectionHandler::kConnected, GetResultAndReset()); | |
145 | |
146 EXPECT_TRUE(Configure(kConfigConnecting)); | |
147 Connect("wifi2"); | |
148 EXPECT_EQ(NetworkConnectionHandler::kConnecting, GetResultAndReset()); | |
149 | |
150 EXPECT_TRUE(Configure(kConfigRequiresPassphrase)); | |
151 Connect("wifi3"); | |
152 EXPECT_EQ(NetworkConnectionHandler::kPassphraseRequired, GetResultAndReset()); | |
153 | |
154 EXPECT_TRUE(Configure(kConfigRequiresActivation)); | |
155 Connect("cellular1"); | |
156 EXPECT_EQ(NetworkConnectionHandler::kActivationRequired, GetResultAndReset()); | |
157 } | |
158 | |
159 namespace { | |
160 | |
161 const char* kConfigRequiresCertificate = | |
162 "{ \"GUID\": \"wifi4\", \"Type\": \"wifi\", \"Connectable\": false," | |
163 " \"Security\": \"802_1x\"," | |
164 " \"UIData\": \"{" | |
165 " \\\"certificate_type\\\": \\\"pattern\\\"," | |
166 " \\\"certificate_pattern\\\": \\\"foobar\\\" }\" }"; | |
167 | |
168 } // namespace | |
169 | |
170 // Handle certificates. TODO(stevenjb): Add certificate stubs to improve | |
171 // test coverage. | |
172 TEST_F(NetworkConnectionHandlerTest, | |
173 NetworkConnectionHandlerConnectCertificate) { | |
174 EXPECT_TRUE(Configure(kConfigRequiresCertificate)); | |
175 Connect("wifi4"); | |
176 EXPECT_EQ(NetworkConnectionHandler::kConfigurationRequired, | |
177 GetResultAndReset()); | |
178 } | |
179 | |
180 TEST_F(NetworkConnectionHandlerTest, | |
181 NetworkConnectionHandlerDisconnectSuccess) { | |
182 EXPECT_TRUE(Configure(kConfigConnected)); | |
183 Disconnect("wifi1"); | |
184 EXPECT_EQ(kSuccessResult, GetResultAndReset()); | |
185 } | |
186 | |
187 TEST_F(NetworkConnectionHandlerTest, | |
188 NetworkConnectionHandlerDisconnectFailure) { | |
189 Connect("no-network"); | |
190 EXPECT_EQ(NetworkConnectionHandler::kNetworkNotFound, GetResultAndReset()); | |
191 | |
192 EXPECT_TRUE(Configure(kConfigConnectable)); | |
193 Disconnect("wifi0"); | |
194 EXPECT_EQ(NetworkConnectionHandler::kNotConnected, GetResultAndReset()); | |
195 } | |
196 | |
197 } // namespace chromeos | |
OLD | NEW |