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

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

Powered by Google App Engine
This is Rietveld 408576698