Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(829)

Side by Side Diff: chromeos/network/network_connection_handler_unittest.cc

Issue 14566009: Add NetworkConnectionHandler class (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Revert Associating Stub change for test Created 7 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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/memory/scoped_ptr.h"
9 #include "base/message_loop.h"
10 #include "chromeos/dbus/dbus_thread_manager.h"
11 #include "chromeos/dbus/shill_manager_client.h"
12 #include "chromeos/dbus/shill_service_client.h"
13 #include "chromeos/network/network_configuration_handler.h"
14 #include "chromeos/network/network_state_handler.h"
15 #include "chromeos/network/onc/onc_utils.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 scoped_ptr<base::DictionaryValue> json_dict =
56 onc::ReadDictionaryFromJson(json_string);
57 if (!json_dict) {
58 LOG(ERROR) << "Error parsing json: " << json_string;
59 return false;
60 }
61 DBusThreadManager::Get()->GetShillManagerClient()->ConfigureService(
62 *json_dict,
63 ObjectPathCallback(), ShillManagerClient::ErrorCallback());
64 message_loop_.RunUntilIdle();
65 return true;
66 }
67
68 void Connect(const std::string& service_path) {
69 NetworkConnectionHandler::Get()->ConnectToNetwork(
70 service_path,
71 base::Bind(&NetworkConnectionHandlerTest::SuccessCallback,
72 base::Unretained(this)),
73 base::Bind(&NetworkConnectionHandlerTest::ErrorCallback,
74 base::Unretained(this)));
75 message_loop_.RunUntilIdle();
76 }
77
78 void Disconnect(const std::string& service_path) {
79 NetworkConnectionHandler::Get()->DisconnectNetwork(
80 service_path,
81 base::Bind(&NetworkConnectionHandlerTest::SuccessCallback,
82 base::Unretained(this)),
83 base::Bind(&NetworkConnectionHandlerTest::ErrorCallback,
84 base::Unretained(this)));
85 message_loop_.RunUntilIdle();
86 }
87
88 void SuccessCallback() {
89 result_ = kSuccessResult;
90 }
91
92 void ErrorCallback(const std::string& error_name,
93 scoped_ptr<base::DictionaryValue> error_data) {
94 result_ = error_name;
95 }
96
97 std::string GetResultAndReset() {
98 std::string result;
99 result.swap(result_);
100 return result;
101 }
102
103 std::string GetServiceStringProperty(const std::string& service_path,
104 const std::string& key) {
105 std::string result;
106 const base::DictionaryValue* properties =
107 DBusThreadManager::Get()->GetShillServiceClient()->GetTestInterface()->
108 GetServiceProperties(service_path);
109 if (properties)
110 properties->GetStringWithoutPathExpansion(key, &result);
111 return result;
112 }
113
114 MessageLoopForUI message_loop_;
115 std::string result_;
116
117 private:
118 DISALLOW_COPY_AND_ASSIGN(NetworkConnectionHandlerTest);
119 };
120
121 namespace {
122
123 const char* kConfigConnectable =
124 "{ \"GUID\": \"wifi0\", \"Type\": \"wifi\", \"State\": \"idle\" }";
125 const char* kConfigConnected =
126 "{ \"GUID\": \"wifi1\", \"Type\": \"wifi\", \"State\": \"online\" }";
127 const char* kConfigConnecting =
128 "{ \"GUID\": \"wifi2\", \"Type\": \"wifi\", \"State\": \"association\" }";
129 const char* kConfigRequiresPassphrase =
130 "{ \"GUID\": \"wifi3\", \"Type\": \"wifi\", "
131 "\"PassphraseRequired\": true }";
132 const char* kConfigRequiresActivation =
133 "{ \"GUID\": \"cellular1\", \"Type\": \"cellular\","
134 " \"Cellular.ActivationState\": \"not-activated\" }";
135
136 } // namespace
137
138 TEST_F(NetworkConnectionHandlerTest, NetworkConnectionHandlerConnectSuccess) {
139 EXPECT_TRUE(Configure(kConfigConnectable));
140 Connect("wifi0");
141 EXPECT_EQ(kSuccessResult, GetResultAndReset());
142 EXPECT_EQ(flimflam::kStateOnline,
143 GetServiceStringProperty("wifi0", flimflam::kStateProperty));
144 }
145
146 // Handles basic failure cases.
147 TEST_F(NetworkConnectionHandlerTest, NetworkConnectionHandlerConnectFailure) {
148 Connect("no-network");
149 EXPECT_EQ(NetworkConnectionHandler::kErrorNotFound, GetResultAndReset());
150
151 EXPECT_TRUE(Configure(kConfigConnected));
152 Connect("wifi1");
153 EXPECT_EQ(NetworkConnectionHandler::kErrorConnected, GetResultAndReset());
154
155 EXPECT_TRUE(Configure(kConfigConnecting));
156 Connect("wifi2");
157 EXPECT_EQ(NetworkConnectionHandler::kErrorConnecting, GetResultAndReset());
158
159 EXPECT_TRUE(Configure(kConfigRequiresPassphrase));
160 Connect("wifi3");
161 EXPECT_EQ(NetworkConnectionHandler::kErrorPassphraseRequired,
162 GetResultAndReset());
163
164 EXPECT_TRUE(Configure(kConfigRequiresActivation));
165 Connect("cellular1");
166 EXPECT_EQ(NetworkConnectionHandler::kErrorActivationRequired,
167 GetResultAndReset());
168 }
169
170 namespace {
171
172 const char* kConfigRequiresCertificate =
173 "{ \"GUID\": \"wifi4\", \"Type\": \"wifi\", \"Connectable\": false,"
174 " \"Security\": \"802_1x\","
175 " \"UIData\": \"{"
176 " \\\"certificate_type\\\": \\\"pattern\\\","
177 " \\\"certificate_pattern\\\": {"
178 " \\\"Subject\\\": { \\\"CommonName\\\": \\\"Foo\\\" }"
179 " } }\" }";
180
181 } // namespace
182
183 // Handle certificates. TODO(stevenjb): Add certificate stubs to improve
184 // test coverage.
185 TEST_F(NetworkConnectionHandlerTest,
186 NetworkConnectionHandlerConnectCertificate) {
187 EXPECT_TRUE(Configure(kConfigRequiresCertificate));
188 Connect("wifi4");
189 EXPECT_EQ(NetworkConnectionHandler::kErrorCertificateRequired,
190 GetResultAndReset());
191 }
192
193 TEST_F(NetworkConnectionHandlerTest,
194 NetworkConnectionHandlerDisconnectSuccess) {
195 EXPECT_TRUE(Configure(kConfigConnected));
196 Disconnect("wifi1");
197 EXPECT_EQ(kSuccessResult, GetResultAndReset());
198 }
199
200 TEST_F(NetworkConnectionHandlerTest,
201 NetworkConnectionHandlerDisconnectFailure) {
202 Connect("no-network");
203 EXPECT_EQ(NetworkConnectionHandler::kErrorNotFound, GetResultAndReset());
204
205 EXPECT_TRUE(Configure(kConfigConnectable));
206 Disconnect("wifi0");
207 EXPECT_EQ(NetworkConnectionHandler::kErrorNotConnected, GetResultAndReset());
208 }
209
210 } // namespace chromeos
OLDNEW
« no previous file with comments | « chromeos/network/network_connection_handler.cc ('k') | chromeos/network/network_handler_callbacks.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698