Add new certificateProvider extension API.
[chromium-blink-merge.git] / chrome / browser / extensions / api / networking_private / networking_private_apitest.cc
blobc0805cad92cca10c7b7f0fd715671aaadff094ad
1 // Copyright 2014 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.
5 #include <map>
6 #include <vector>
8 #include "base/command_line.h"
9 #include "base/logging.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "chrome/browser/extensions/extension_apitest.h"
12 #include "components/keyed_service/core/keyed_service.h"
13 #include "components/onc/onc_constants.h"
14 #include "content/public/test/test_utils.h"
15 #include "extensions/browser/api/networking_private/networking_private_delegate.h"
16 #include "extensions/browser/api/networking_private/networking_private_delegate_factory.h"
17 #include "extensions/common/switches.h"
19 namespace extensions {
21 // This tests just the interface for the networkingPrivate API, i.e. it ensures
22 // that the delegate methods are called as expected.
24 // The implementations (which differ significantly between chromeos and
25 // windows/mac) are tested independently in
26 // networking_private_[chromeos|service_client]_apitest.cc.
27 // See also crbug.com/460119.
29 namespace {
31 const char kFailure[] = "Failure";
32 const char kSuccess[] = "Success";
33 const char kGuid[] = "SOME_GUID";
35 class TestDelegate : public NetworkingPrivateDelegate {
36 public:
37 explicit TestDelegate(scoped_ptr<VerifyDelegate> verify_delegate)
38 : NetworkingPrivateDelegate(verify_delegate.Pass()), fail_(false) {}
40 ~TestDelegate() override {}
42 // Asynchronous methods
43 void GetProperties(const std::string& guid,
44 const DictionaryCallback& success_callback,
45 const FailureCallback& failure_callback) override {
46 DictionaryResult(guid, success_callback, failure_callback);
49 void GetManagedProperties(const std::string& guid,
50 const DictionaryCallback& success_callback,
51 const FailureCallback& failure_callback) override {
52 DictionaryResult(guid, success_callback, failure_callback);
55 void GetState(const std::string& guid,
56 const DictionaryCallback& success_callback,
57 const FailureCallback& failure_callback) override {
58 DictionaryResult(guid, success_callback, failure_callback);
61 void SetProperties(const std::string& guid,
62 scoped_ptr<base::DictionaryValue> properties,
63 const VoidCallback& success_callback,
64 const FailureCallback& failure_callback) override {
65 VoidResult(success_callback, failure_callback);
68 void CreateNetwork(bool shared,
69 scoped_ptr<base::DictionaryValue> properties,
70 const StringCallback& success_callback,
71 const FailureCallback& failure_callback) override {
72 StringResult(success_callback, failure_callback);
75 void ForgetNetwork(const std::string& guid,
76 const VoidCallback& success_callback,
77 const FailureCallback& failure_callback) override {
78 VoidResult(success_callback, failure_callback);
81 void GetNetworks(const std::string& network_type,
82 bool configured_only,
83 bool visible_only,
84 int limit,
85 const NetworkListCallback& success_callback,
86 const FailureCallback& failure_callback) override {
87 if (fail_) {
88 failure_callback.Run(kFailure);
89 } else {
90 scoped_ptr<base::ListValue> result(new base::ListValue);
91 scoped_ptr<base::DictionaryValue> network(new base::DictionaryValue);
92 network->SetString(::onc::network_config::kType,
93 ::onc::network_config::kEthernet);
94 network->SetString(::onc::network_config::kGUID, kGuid);
95 result->Append(network.release());
96 success_callback.Run(result.Pass());
100 void StartConnect(const std::string& guid,
101 const VoidCallback& success_callback,
102 const FailureCallback& failure_callback) override {
103 VoidResult(success_callback, failure_callback);
106 void StartDisconnect(const std::string& guid,
107 const VoidCallback& success_callback,
108 const FailureCallback& failure_callback) override {
109 VoidResult(success_callback, failure_callback);
112 void StartActivate(const std::string& guid,
113 const std::string& carrier,
114 const VoidCallback& success_callback,
115 const FailureCallback& failure_callback) override {
116 VoidResult(success_callback, failure_callback);
119 void SetWifiTDLSEnabledState(
120 const std::string& ip_or_mac_address,
121 bool enabled,
122 const StringCallback& success_callback,
123 const FailureCallback& failure_callback) override {
124 StringResult(success_callback, failure_callback);
127 void GetWifiTDLSStatus(const std::string& ip_or_mac_address,
128 const StringCallback& success_callback,
129 const FailureCallback& failure_callback) override {
130 StringResult(success_callback, failure_callback);
133 void GetCaptivePortalStatus(
134 const std::string& guid,
135 const StringCallback& success_callback,
136 const FailureCallback& failure_callback) override {
137 StringResult(success_callback, failure_callback);
140 void UnlockCellularSim(const std::string& guid,
141 const std::string& pin,
142 const std::string& puk,
143 const VoidCallback& success_callback,
144 const FailureCallback& failure_callback) override {
145 VoidResult(success_callback, failure_callback);
148 void SetCellularSimState(const std::string& guid,
149 bool require_pin,
150 const std::string& current_pin,
151 const std::string& new_pin,
152 const VoidCallback& success_callback,
153 const FailureCallback& failure_callback) override {
154 VoidResult(success_callback, failure_callback);
157 // Synchronous methods
158 scoped_ptr<base::ListValue> GetEnabledNetworkTypes() override {
159 scoped_ptr<base::ListValue> result;
160 if (!fail_) {
161 result.reset(new base::ListValue);
162 result->AppendString(::onc::network_config::kEthernet);
164 return result.Pass();
167 scoped_ptr<DeviceStateList> GetDeviceStateList() override {
168 scoped_ptr<DeviceStateList> result;
169 if (fail_)
170 return result.Pass();
171 result.reset(new DeviceStateList);
172 scoped_ptr<api::networking_private::DeviceStateProperties> properties(
173 new api::networking_private::DeviceStateProperties);
174 properties->type = api::networking_private::NETWORK_TYPE_ETHERNET;
175 properties->state = api::networking_private::DEVICE_STATE_TYPE_ENABLED;
176 result->push_back(properties.Pass());
177 return result.Pass();
180 bool EnableNetworkType(const std::string& type) override {
181 enabled_[type] = true;
182 return !fail_;
185 bool DisableNetworkType(const std::string& type) override {
186 disabled_[type] = true;
187 return !fail_;
190 bool RequestScan() override {
191 scan_requested_.push_back(true);
192 return !fail_;
195 void set_fail(bool fail) { fail_ = fail; }
196 bool GetEnabled(const std::string& type) { return enabled_[type]; }
197 bool GetDisabled(const std::string& type) { return disabled_[type]; }
198 size_t GetScanRequested() { return scan_requested_.size(); }
200 void DictionaryResult(const std::string& guid,
201 const DictionaryCallback& success_callback,
202 const FailureCallback& failure_callback) {
203 if (fail_) {
204 failure_callback.Run(kFailure);
205 } else {
206 scoped_ptr<base::DictionaryValue> result(new base::DictionaryValue);
207 result->SetString(::onc::network_config::kGUID, guid);
208 result->SetString(::onc::network_config::kType,
209 ::onc::network_config::kWiFi);
210 success_callback.Run(result.Pass());
214 void StringResult(const StringCallback& success_callback,
215 const FailureCallback& failure_callback) {
216 if (fail_) {
217 failure_callback.Run(kFailure);
218 } else {
219 success_callback.Run(kSuccess);
223 void BoolResult(const BoolCallback& success_callback,
224 const FailureCallback& failure_callback) {
225 if (fail_) {
226 failure_callback.Run(kFailure);
227 } else {
228 success_callback.Run(true);
232 void VoidResult(const VoidCallback& success_callback,
233 const FailureCallback& failure_callback) {
234 if (fail_) {
235 failure_callback.Run(kFailure);
236 } else {
237 success_callback.Run();
241 private:
242 bool fail_;
243 std::map<std::string, bool> enabled_;
244 std::map<std::string, bool> disabled_;
245 std::vector<bool> scan_requested_;
247 DISALLOW_COPY_AND_ASSIGN(TestDelegate);
250 class TestVerifyDelegate : public NetworkingPrivateDelegate::VerifyDelegate {
251 public:
252 TestVerifyDelegate() : owner_(NULL) {}
254 ~TestVerifyDelegate() override {}
256 void VerifyDestination(
257 const VerificationProperties& verification_properties,
258 const BoolCallback& success_callback,
259 const FailureCallback& failure_callback) override {
260 owner_->BoolResult(success_callback, failure_callback);
262 void VerifyAndEncryptCredentials(
263 const std::string& guid,
264 const VerificationProperties& verification_properties,
265 const StringCallback& success_callback,
266 const FailureCallback& failure_callback) override {
267 owner_->StringResult(success_callback, failure_callback);
269 void VerifyAndEncryptData(
270 const VerificationProperties& verification_properties,
271 const std::string& data,
272 const StringCallback& success_callback,
273 const FailureCallback& failure_callback) override {
274 owner_->StringResult(success_callback, failure_callback);
277 void set_owner(TestDelegate* owner) { owner_ = owner; }
279 private:
280 TestDelegate* owner_;
282 DISALLOW_COPY_AND_ASSIGN(TestVerifyDelegate);
285 class NetworkingPrivateApiTest : public ExtensionApiTest {
286 public:
287 NetworkingPrivateApiTest() {
288 if (!s_test_delegate_) {
289 TestVerifyDelegate* verify_delegate = new TestVerifyDelegate;
290 scoped_ptr<NetworkingPrivateDelegate::VerifyDelegate> verify_delegate_ptr(
291 verify_delegate);
292 s_test_delegate_ = new TestDelegate(verify_delegate_ptr.Pass());
293 verify_delegate->set_owner(s_test_delegate_);
297 static scoped_ptr<KeyedService> GetNetworkingPrivateDelegate(
298 content::BrowserContext* profile) {
299 CHECK(s_test_delegate_);
300 return make_scoped_ptr(s_test_delegate_);
303 void SetUpCommandLine(base::CommandLine* command_line) override {
304 ExtensionApiTest::SetUpCommandLine(command_line);
305 // Whitelist the extension ID of the test extension.
306 command_line->AppendSwitchASCII(
307 extensions::switches::kWhitelistedExtensionID,
308 "epcifkihnkjgphfkloaaleeakhpmgdmn");
311 void SetUpOnMainThread() override {
312 ExtensionApiTest::SetUpOnMainThread();
313 NetworkingPrivateDelegateFactory::GetInstance()->SetTestingFactory(
314 profile(), &NetworkingPrivateApiTest::GetNetworkingPrivateDelegate);
315 content::RunAllPendingInMessageLoop();
318 bool GetEnabled(const std::string& type) {
319 return s_test_delegate_->GetEnabled(type);
322 bool GetDisabled(const std::string& type) {
323 return s_test_delegate_->GetDisabled(type);
326 size_t GetScanRequested() {
327 return s_test_delegate_->GetScanRequested();
330 protected:
331 bool RunNetworkingSubtest(const std::string& subtest) {
332 return RunExtensionSubtest("networking_private",
333 "main.html?" + subtest,
334 kFlagEnableFileAccess | kFlagLoadAsComponent);
337 // Static pointer to the TestDelegate so that it can be accessed in
338 // GetNetworkingPrivateDelegate() passed to SetTestingFactory().
339 static TestDelegate* s_test_delegate_;
341 DISALLOW_COPY_AND_ASSIGN(NetworkingPrivateApiTest);
344 // static
345 TestDelegate* NetworkingPrivateApiTest::s_test_delegate_ = NULL;
347 } // namespace
349 // Place each subtest into a separate browser test so that the stub networking
350 // library state is reset for each subtest run. This way they won't affect each
351 // other. TODO(stevenjb): Use extensions::ApiUnitTest once moved to
352 // src/extensions.
354 // These fail on Windows due to crbug.com/177163. Note: we still have partial
355 // coverage in NetworkingPrivateServiceClientApiTest. TODO(stevenjb): Enable
356 // these on Windows once we switch to extensions::ApiUnitTest.
358 #if !defined(OS_WIN)
360 IN_PROC_BROWSER_TEST_F(NetworkingPrivateApiTest, GetProperties) {
361 EXPECT_TRUE(RunNetworkingSubtest("getProperties")) << message_;
364 IN_PROC_BROWSER_TEST_F(NetworkingPrivateApiTest, GetManagedProperties) {
365 EXPECT_TRUE(RunNetworkingSubtest("getManagedProperties")) << message_;
368 IN_PROC_BROWSER_TEST_F(NetworkingPrivateApiTest, GetState) {
369 EXPECT_TRUE(RunNetworkingSubtest("getState")) << message_;
372 IN_PROC_BROWSER_TEST_F(NetworkingPrivateApiTest, SetProperties) {
373 EXPECT_TRUE(RunNetworkingSubtest("setProperties")) << message_;
376 IN_PROC_BROWSER_TEST_F(NetworkingPrivateApiTest, CreateNetwork) {
377 EXPECT_TRUE(RunNetworkingSubtest("createNetwork")) << message_;
380 IN_PROC_BROWSER_TEST_F(NetworkingPrivateApiTest, ForgetNetwork) {
381 EXPECT_TRUE(RunNetworkingSubtest("forgetNetwork")) << message_;
384 IN_PROC_BROWSER_TEST_F(NetworkingPrivateApiTest, GetNetworks) {
385 EXPECT_TRUE(RunNetworkingSubtest("getNetworks")) << message_;
388 IN_PROC_BROWSER_TEST_F(NetworkingPrivateApiTest, GetVisibleNetworks) {
389 EXPECT_TRUE(RunNetworkingSubtest("getVisibleNetworks")) << message_;
392 IN_PROC_BROWSER_TEST_F(NetworkingPrivateApiTest, GetEnabledNetworkTypes) {
393 EXPECT_TRUE(RunNetworkingSubtest("getEnabledNetworkTypes")) << message_;
396 IN_PROC_BROWSER_TEST_F(NetworkingPrivateApiTest, GetDeviceStates) {
397 EXPECT_TRUE(RunNetworkingSubtest("getDeviceStates")) << message_;
400 IN_PROC_BROWSER_TEST_F(NetworkingPrivateApiTest, EnableNetworkType) {
401 EXPECT_TRUE(RunNetworkingSubtest("enableNetworkType")) << message_;
402 EXPECT_TRUE(GetEnabled(::onc::network_config::kEthernet));
405 IN_PROC_BROWSER_TEST_F(NetworkingPrivateApiTest, DisableNetworkType) {
406 EXPECT_TRUE(RunNetworkingSubtest("disableNetworkType")) << message_;
407 EXPECT_TRUE(GetDisabled(::onc::network_config::kEthernet));
410 IN_PROC_BROWSER_TEST_F(NetworkingPrivateApiTest, RequestNetworkScan) {
411 EXPECT_TRUE(RunNetworkingSubtest("requestNetworkScan")) << message_;
412 EXPECT_EQ(1u, GetScanRequested());
415 IN_PROC_BROWSER_TEST_F(NetworkingPrivateApiTest, StartConnect) {
416 EXPECT_TRUE(RunNetworkingSubtest("startConnect")) << message_;
419 IN_PROC_BROWSER_TEST_F(NetworkingPrivateApiTest, StartDisconnect) {
420 EXPECT_TRUE(RunNetworkingSubtest("startDisconnect")) << message_;
423 IN_PROC_BROWSER_TEST_F(NetworkingPrivateApiTest, StartActivate) {
424 EXPECT_TRUE(RunNetworkingSubtest("startActivate")) << message_;
427 IN_PROC_BROWSER_TEST_F(NetworkingPrivateApiTest, VerifyDestination) {
428 EXPECT_TRUE(RunNetworkingSubtest("verifyDestination")) << message_;
431 IN_PROC_BROWSER_TEST_F(NetworkingPrivateApiTest, VerifyAndEncryptCredentials) {
432 EXPECT_TRUE(RunNetworkingSubtest("verifyAndEncryptCredentials")) << message_;
435 IN_PROC_BROWSER_TEST_F(NetworkingPrivateApiTest, VerifyAndEncryptData) {
436 EXPECT_TRUE(RunNetworkingSubtest("verifyAndEncryptData")) << message_;
439 IN_PROC_BROWSER_TEST_F(NetworkingPrivateApiTest, SetWifiTDLSEnabledState) {
440 EXPECT_TRUE(RunNetworkingSubtest("setWifiTDLSEnabledState")) << message_;
443 IN_PROC_BROWSER_TEST_F(NetworkingPrivateApiTest, GetWifiTDLSStatus) {
444 EXPECT_TRUE(RunNetworkingSubtest("getWifiTDLSStatus")) << message_;
447 IN_PROC_BROWSER_TEST_F(NetworkingPrivateApiTest, GetCaptivePortalStatus) {
448 EXPECT_TRUE(RunNetworkingSubtest("getCaptivePortalStatus")) << message_;
451 IN_PROC_BROWSER_TEST_F(NetworkingPrivateApiTest, UnlockCellularSim) {
452 EXPECT_TRUE(RunNetworkingSubtest("unlockCellularSim")) << message_;
455 IN_PROC_BROWSER_TEST_F(NetworkingPrivateApiTest, SetCellularSimState) {
456 EXPECT_TRUE(RunNetworkingSubtest("setCellularSimState")) << message_;
459 // Test failure case
461 class NetworkingPrivateApiTestFail : public NetworkingPrivateApiTest {
462 public:
463 NetworkingPrivateApiTestFail() { s_test_delegate_->set_fail(true); }
465 protected:
466 DISALLOW_COPY_AND_ASSIGN(NetworkingPrivateApiTestFail);
469 IN_PROC_BROWSER_TEST_F(NetworkingPrivateApiTestFail, GetProperties) {
470 EXPECT_FALSE(RunNetworkingSubtest("getProperties")) << message_;
473 IN_PROC_BROWSER_TEST_F(NetworkingPrivateApiTestFail, GetManagedProperties) {
474 EXPECT_FALSE(RunNetworkingSubtest("getManagedProperties")) << message_;
477 IN_PROC_BROWSER_TEST_F(NetworkingPrivateApiTestFail, GetState) {
478 EXPECT_FALSE(RunNetworkingSubtest("getState")) << message_;
481 IN_PROC_BROWSER_TEST_F(NetworkingPrivateApiTestFail, SetProperties) {
482 EXPECT_FALSE(RunNetworkingSubtest("setProperties")) << message_;
485 IN_PROC_BROWSER_TEST_F(NetworkingPrivateApiTestFail, CreateNetwork) {
486 EXPECT_FALSE(RunNetworkingSubtest("createNetwork")) << message_;
489 IN_PROC_BROWSER_TEST_F(NetworkingPrivateApiTestFail, ForgetNetwork) {
490 EXPECT_FALSE(RunNetworkingSubtest("forgetNetwork")) << message_;
493 IN_PROC_BROWSER_TEST_F(NetworkingPrivateApiTestFail, GetNetworks) {
494 EXPECT_FALSE(RunNetworkingSubtest("getNetworks")) << message_;
497 IN_PROC_BROWSER_TEST_F(NetworkingPrivateApiTestFail, GetVisibleNetworks) {
498 EXPECT_FALSE(RunNetworkingSubtest("getVisibleNetworks")) << message_;
501 IN_PROC_BROWSER_TEST_F(NetworkingPrivateApiTestFail, GetEnabledNetworkTypes) {
502 EXPECT_FALSE(RunNetworkingSubtest("getEnabledNetworkTypes")) << message_;
505 IN_PROC_BROWSER_TEST_F(NetworkingPrivateApiTestFail, GetDeviceStates) {
506 EXPECT_FALSE(RunNetworkingSubtest("getDeviceStates")) << message_;
509 // Note: Synchronous methods never fail:
510 // * disableNetworkType
511 // * enableNetworkType
512 // * requestNetworkScan
514 IN_PROC_BROWSER_TEST_F(NetworkingPrivateApiTestFail, StartConnect) {
515 EXPECT_FALSE(RunNetworkingSubtest("startConnect")) << message_;
518 IN_PROC_BROWSER_TEST_F(NetworkingPrivateApiTestFail, StartDisconnect) {
519 EXPECT_FALSE(RunNetworkingSubtest("startDisconnect")) << message_;
522 IN_PROC_BROWSER_TEST_F(NetworkingPrivateApiTestFail, StartActivate) {
523 EXPECT_FALSE(RunNetworkingSubtest("startActivate")) << message_;
526 IN_PROC_BROWSER_TEST_F(NetworkingPrivateApiTestFail, VerifyDestination) {
527 EXPECT_FALSE(RunNetworkingSubtest("verifyDestination")) << message_;
530 IN_PROC_BROWSER_TEST_F(NetworkingPrivateApiTestFail,
531 VerifyAndEncryptCredentials) {
532 EXPECT_FALSE(RunNetworkingSubtest("verifyAndEncryptCredentials")) << message_;
535 IN_PROC_BROWSER_TEST_F(NetworkingPrivateApiTestFail, VerifyAndEncryptData) {
536 EXPECT_FALSE(RunNetworkingSubtest("verifyAndEncryptData")) << message_;
539 IN_PROC_BROWSER_TEST_F(NetworkingPrivateApiTestFail, SetWifiTDLSEnabledState) {
540 EXPECT_FALSE(RunNetworkingSubtest("setWifiTDLSEnabledState")) << message_;
543 IN_PROC_BROWSER_TEST_F(NetworkingPrivateApiTestFail, GetWifiTDLSStatus) {
544 EXPECT_FALSE(RunNetworkingSubtest("getWifiTDLSStatus")) << message_;
547 IN_PROC_BROWSER_TEST_F(NetworkingPrivateApiTestFail, GetCaptivePortalStatus) {
548 EXPECT_FALSE(RunNetworkingSubtest("getCaptivePortalStatus")) << message_;
551 IN_PROC_BROWSER_TEST_F(NetworkingPrivateApiTestFail, UnlockCellularSim) {
552 EXPECT_FALSE(RunNetworkingSubtest("unlockCellularSim")) << message_;
555 IN_PROC_BROWSER_TEST_F(NetworkingPrivateApiTestFail, SetCellularSimState) {
556 EXPECT_FALSE(RunNetworkingSubtest("setCellularSimState")) << message_;
559 #endif // defined(OS_WIN)
561 } // namespace extensions