| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include <stddef.h> | 5 #include <stddef.h> |
| 6 | 6 |
| 7 #include <map> | 7 #include <map> |
| 8 #include <memory> | 8 #include <memory> |
| 9 #include <utility> | 9 #include <utility> |
| 10 #include <vector> | 10 #include <vector> |
| 11 | 11 |
| 12 #include "base/bind.h" |
| 13 #include "base/callback.h" |
| 12 #include "base/command_line.h" | 14 #include "base/command_line.h" |
| 13 #include "base/logging.h" | 15 #include "base/logging.h" |
| 14 #include "base/macros.h" | 16 #include "base/macros.h" |
| 15 #include "base/memory/ptr_util.h" | 17 #include "base/memory/ptr_util.h" |
| 16 #include "build/build_config.h" | 18 #include "build/build_config.h" |
| 19 #include "chrome/browser/extensions/api/networking_cast_private/chrome_networkin
g_cast_private_delegate.h" |
| 17 #include "chrome/browser/extensions/extension_apitest.h" | 20 #include "chrome/browser/extensions/extension_apitest.h" |
| 18 #include "components/keyed_service/core/keyed_service.h" | 21 #include "components/keyed_service/core/keyed_service.h" |
| 19 #include "components/onc/onc_constants.h" | 22 #include "components/onc/onc_constants.h" |
| 20 #include "content/public/test/test_utils.h" | 23 #include "content/public/test/test_utils.h" |
| 21 #include "extensions/browser/api/networking_private/networking_private_delegate.
h" | 24 #include "extensions/browser/api/networking_private/networking_private_delegate.
h" |
| 22 #include "extensions/browser/api/networking_private/networking_private_delegate_
factory.h" | 25 #include "extensions/browser/api/networking_private/networking_private_delegate_
factory.h" |
| 23 #include "extensions/common/switches.h" | 26 #include "extensions/common/switches.h" |
| 24 | 27 |
| 25 namespace extensions { | 28 namespace extensions { |
| 26 | 29 |
| 27 // This tests just the interface for the networkingPrivate API, i.e. it ensures | 30 // This tests just the interface for the networkingPrivate API, i.e. it ensures |
| 28 // that the delegate methods are called as expected. | 31 // that the delegate methods are called as expected. |
| 29 | 32 |
| 30 // The implementations (which differ significantly between chromeos and | 33 // The implementations (which differ significantly between chromeos and |
| 31 // windows/mac) are tested independently in | 34 // windows/mac) are tested independently in |
| 32 // networking_private_[chromeos|service_client]_apitest.cc. | 35 // networking_private_[chromeos|service_client]_apitest.cc. |
| 33 // See also crbug.com/460119. | 36 // See also crbug.com/460119. |
| 34 | 37 |
| 35 namespace { | 38 namespace { |
| 36 | 39 |
| 37 const char kFailure[] = "Failure"; | 40 const char kFailure[] = "Failure"; |
| 38 const char kSuccess[] = "Success"; | 41 const char kSuccess[] = "Success"; |
| 39 const char kGuid[] = "SOME_GUID"; | 42 const char kGuid[] = "SOME_GUID"; |
| 40 | 43 |
| 41 class TestDelegate : public NetworkingPrivateDelegate { | 44 class TestNetworkingPrivateDelegate : public NetworkingPrivateDelegate { |
| 42 public: | 45 public: |
| 43 explicit TestDelegate(std::unique_ptr<VerifyDelegate> verify_delegate) | 46 explicit TestNetworkingPrivateDelegate(bool test_failure) |
| 44 : NetworkingPrivateDelegate(std::move(verify_delegate)), fail_(false) {} | 47 : fail_(test_failure) {} |
| 45 | 48 |
| 46 ~TestDelegate() override {} | 49 ~TestNetworkingPrivateDelegate() override {} |
| 47 | 50 |
| 48 // Asynchronous methods | 51 // Asynchronous methods |
| 49 void GetProperties(const std::string& guid, | 52 void GetProperties(const std::string& guid, |
| 50 const DictionaryCallback& success_callback, | 53 const DictionaryCallback& success_callback, |
| 51 const FailureCallback& failure_callback) override { | 54 const FailureCallback& failure_callback) override { |
| 52 DictionaryResult(guid, success_callback, failure_callback); | 55 DictionaryResult(guid, success_callback, failure_callback); |
| 53 } | 56 } |
| 54 | 57 |
| 55 void GetManagedProperties(const std::string& guid, | 58 void GetManagedProperties(const std::string& guid, |
| 56 const DictionaryCallback& success_callback, | 59 const DictionaryCallback& success_callback, |
| (...skipping 191 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 248 success_callback.Run(); | 251 success_callback.Run(); |
| 249 } | 252 } |
| 250 } | 253 } |
| 251 | 254 |
| 252 private: | 255 private: |
| 253 bool fail_; | 256 bool fail_; |
| 254 std::map<std::string, bool> enabled_; | 257 std::map<std::string, bool> enabled_; |
| 255 std::map<std::string, bool> disabled_; | 258 std::map<std::string, bool> disabled_; |
| 256 std::vector<bool> scan_requested_; | 259 std::vector<bool> scan_requested_; |
| 257 | 260 |
| 258 DISALLOW_COPY_AND_ASSIGN(TestDelegate); | 261 DISALLOW_COPY_AND_ASSIGN(TestNetworkingPrivateDelegate); |
| 259 }; | 262 }; |
| 260 | 263 |
| 261 class TestVerifyDelegate : public NetworkingPrivateDelegate::VerifyDelegate { | 264 class TestNetworkingCastPrivateDelegate |
| 265 : public ChromeNetworkingCastPrivateDelegate { |
| 262 public: | 266 public: |
| 263 TestVerifyDelegate() : owner_(NULL) {} | 267 explicit TestNetworkingCastPrivateDelegate(bool test_failure) |
| 268 : fail_(test_failure) {} |
| 264 | 269 |
| 265 ~TestVerifyDelegate() override {} | 270 ~TestNetworkingCastPrivateDelegate() override {} |
| 266 | 271 |
| 267 void VerifyDestination( | 272 void VerifyDestination(std::unique_ptr<Credentials> credentials, |
| 268 const VerificationProperties& verification_properties, | 273 const VerifiedCallback& success_callback, |
| 269 const BoolCallback& success_callback, | 274 const FailureCallback& failure_callback) override { |
| 270 const FailureCallback& failure_callback) override { | 275 if (fail_) { |
| 271 owner_->BoolResult(success_callback, failure_callback); | 276 failure_callback.Run(kFailure); |
| 277 } else { |
| 278 success_callback.Run(true); |
| 279 } |
| 272 } | 280 } |
| 281 |
| 273 void VerifyAndEncryptCredentials( | 282 void VerifyAndEncryptCredentials( |
| 274 const std::string& guid, | 283 const std::string& guid, |
| 275 const VerificationProperties& verification_properties, | 284 std::unique_ptr<Credentials> credentials, |
| 276 const StringCallback& success_callback, | 285 const DataCallback& success_callback, |
| 277 const FailureCallback& failure_callback) override { | 286 const FailureCallback& failure_callback) override { |
| 278 owner_->StringResult(success_callback, failure_callback); | 287 if (fail_) { |
| 288 failure_callback.Run(kFailure); |
| 289 } else { |
| 290 success_callback.Run("encrypted_credentials"); |
| 291 } |
| 279 } | 292 } |
| 280 void VerifyAndEncryptData( | 293 void VerifyAndEncryptData(const std::string& data, |
| 281 const VerificationProperties& verification_properties, | 294 std::unique_ptr<Credentials> credentials, |
| 282 const std::string& data, | 295 const DataCallback& success_callback, |
| 283 const StringCallback& success_callback, | 296 const FailureCallback& failure_callback) override { |
| 284 const FailureCallback& failure_callback) override { | 297 if (fail_) { |
| 285 owner_->StringResult(success_callback, failure_callback); | 298 failure_callback.Run(kFailure); |
| 299 } else { |
| 300 success_callback.Run("encrypted_data"); |
| 301 } |
| 286 } | 302 } |
| 287 | 303 |
| 288 void set_owner(TestDelegate* owner) { owner_ = owner; } | 304 private: |
| 305 bool fail_; |
| 289 | 306 |
| 290 private: | 307 DISALLOW_COPY_AND_ASSIGN(TestNetworkingCastPrivateDelegate); |
| 291 TestDelegate* owner_; | |
| 292 | |
| 293 DISALLOW_COPY_AND_ASSIGN(TestVerifyDelegate); | |
| 294 }; | 308 }; |
| 295 | 309 |
| 296 class NetworkingPrivateApiTest : public ExtensionApiTest { | 310 class NetworkingPrivateApiTest : public ExtensionApiTest { |
| 297 public: | 311 public: |
| 298 NetworkingPrivateApiTest() { | 312 using TestNetworkingPrivateDelegateFactory = |
| 299 if (!s_test_delegate_) { | 313 base::Callback<std::unique_ptr<KeyedService>()>; |
| 300 TestVerifyDelegate* verify_delegate = new TestVerifyDelegate; | |
| 301 std::unique_ptr<NetworkingPrivateDelegate::VerifyDelegate> | |
| 302 verify_delegate_ptr(verify_delegate); | |
| 303 s_test_delegate_ = new TestDelegate(std::move(verify_delegate_ptr)); | |
| 304 verify_delegate->set_owner(s_test_delegate_); | |
| 305 } | |
| 306 } | |
| 307 | 314 |
| 308 static std::unique_ptr<KeyedService> GetNetworkingPrivateDelegate( | 315 static std::unique_ptr<KeyedService> GetNetworkingPrivateDelegate( |
| 309 content::BrowserContext* profile) { | 316 content::BrowserContext* profile) { |
| 310 CHECK(s_test_delegate_); | 317 CHECK(s_networking_private_delegate_factory_ptr); |
| 311 return base::WrapUnique(s_test_delegate_); | 318 return s_networking_private_delegate_factory_ptr->Run(); |
| 319 } |
| 320 |
| 321 NetworkingPrivateApiTest() = default; |
| 322 ~NetworkingPrivateApiTest() override = default; |
| 323 |
| 324 void SetUp() override { |
| 325 networking_cast_delegate_factory_ = base::Bind( |
| 326 &NetworkingPrivateApiTest::CreateTestNetworkingCastPrivateDelegate, |
| 327 base::Unretained(this), test_failure_); |
| 328 ChromeNetworkingCastPrivateDelegate::SetFactoryCallbackForTest( |
| 329 &networking_cast_delegate_factory_); |
| 330 |
| 331 networking_private_delegate_factory_ = base::Bind( |
| 332 &NetworkingPrivateApiTest::CreateTestNetworkingPrivateDelegate, |
| 333 base::Unretained(this), test_failure_); |
| 334 s_networking_private_delegate_factory_ptr = |
| 335 &networking_private_delegate_factory_; |
| 336 |
| 337 ExtensionApiTest::SetUp(); |
| 312 } | 338 } |
| 313 | 339 |
| 314 void SetUpCommandLine(base::CommandLine* command_line) override { | 340 void SetUpCommandLine(base::CommandLine* command_line) override { |
| 315 ExtensionApiTest::SetUpCommandLine(command_line); | 341 ExtensionApiTest::SetUpCommandLine(command_line); |
| 316 // Whitelist the extension ID of the test extension. | 342 // Whitelist the extension ID of the test extension. |
| 317 command_line->AppendSwitchASCII( | 343 command_line->AppendSwitchASCII( |
| 318 extensions::switches::kWhitelistedExtensionID, | 344 extensions::switches::kWhitelistedExtensionID, |
| 319 "epcifkihnkjgphfkloaaleeakhpmgdmn"); | 345 "epcifkihnkjgphfkloaaleeakhpmgdmn"); |
| 320 } | 346 } |
| 321 | 347 |
| 322 void SetUpOnMainThread() override { | 348 void SetUpOnMainThread() override { |
| 323 ExtensionApiTest::SetUpOnMainThread(); | 349 ExtensionApiTest::SetUpOnMainThread(); |
| 324 NetworkingPrivateDelegateFactory::GetInstance()->SetTestingFactory( | 350 NetworkingPrivateDelegateFactory::GetInstance()->SetTestingFactory( |
| 325 profile(), &NetworkingPrivateApiTest::GetNetworkingPrivateDelegate); | 351 profile(), &NetworkingPrivateApiTest::GetNetworkingPrivateDelegate); |
| 326 content::RunAllPendingInMessageLoop(); | 352 } |
| 353 |
| 354 void TearDown() override { |
| 355 ExtensionApiTest::TearDown(); |
| 356 |
| 357 s_networking_private_delegate_factory_ptr = nullptr; |
| 358 ChromeNetworkingCastPrivateDelegate::SetFactoryCallbackForTest(nullptr); |
| 359 |
| 360 networking_private_delegate_ = nullptr; |
| 327 } | 361 } |
| 328 | 362 |
| 329 bool GetEnabled(const std::string& type) { | 363 bool GetEnabled(const std::string& type) { |
| 330 return s_test_delegate_->GetEnabled(type); | 364 return networking_private_delegate_->GetEnabled(type); |
| 331 } | 365 } |
| 332 | 366 |
| 333 bool GetDisabled(const std::string& type) { | 367 bool GetDisabled(const std::string& type) { |
| 334 return s_test_delegate_->GetDisabled(type); | 368 return networking_private_delegate_->GetDisabled(type); |
| 335 } | 369 } |
| 336 | 370 |
| 337 size_t GetScanRequested() { | 371 size_t GetScanRequested() { |
| 338 return s_test_delegate_->GetScanRequested(); | 372 return networking_private_delegate_->GetScanRequested(); |
| 339 } | 373 } |
| 340 | 374 |
| 341 protected: | 375 protected: |
| 342 bool RunNetworkingSubtest(const std::string& subtest) { | 376 bool RunNetworkingSubtest(const std::string& subtest) { |
| 343 return RunExtensionSubtest("networking_private", | 377 return RunExtensionSubtest("networking_private", |
| 344 "main.html?" + subtest, | 378 "main.html?" + subtest, |
| 345 kFlagEnableFileAccess | kFlagLoadAsComponent); | 379 kFlagEnableFileAccess | kFlagLoadAsComponent); |
| 346 } | 380 } |
| 347 | 381 |
| 348 // Static pointer to the TestDelegate so that it can be accessed in | 382 private: |
| 349 // GetNetworkingPrivateDelegate() passed to SetTestingFactory(). | 383 std::unique_ptr<ChromeNetworkingCastPrivateDelegate> |
| 350 static TestDelegate* s_test_delegate_; | 384 CreateTestNetworkingCastPrivateDelegate(bool test_failure) { |
| 385 return base::MakeUnique<TestNetworkingCastPrivateDelegate>(test_failure); |
| 386 } |
| 387 |
| 388 std::unique_ptr<KeyedService> CreateTestNetworkingPrivateDelegate( |
| 389 bool test_failure) { |
| 390 CHECK(!networking_private_delegate_); |
| 391 auto delegate = |
| 392 base::MakeUnique<TestNetworkingPrivateDelegate>(test_failure); |
| 393 networking_private_delegate_ = delegate.get(); |
| 394 return delegate; |
| 395 } |
| 396 |
| 397 protected: |
| 398 bool test_failure_ = false; |
| 399 |
| 400 private: |
| 401 // Pointer to a networking private delegate created by the test factory |
| 402 // callback. |
| 403 TestNetworkingPrivateDelegate* networking_private_delegate_ = nullptr; |
| 404 |
| 405 TestNetworkingPrivateDelegateFactory networking_private_delegate_factory_; |
| 406 // Static pointer to |test_delegate_factory_|, so it can be used from |
| 407 // |CreateNetwokringPrivateDelegate|. |
| 408 static TestNetworkingPrivateDelegateFactory* |
| 409 s_networking_private_delegate_factory_ptr; |
| 410 |
| 411 ChromeNetworkingCastPrivateDelegate::FactoryCallback |
| 412 networking_cast_delegate_factory_; |
| 351 | 413 |
| 352 DISALLOW_COPY_AND_ASSIGN(NetworkingPrivateApiTest); | 414 DISALLOW_COPY_AND_ASSIGN(NetworkingPrivateApiTest); |
| 353 }; | 415 }; |
| 354 | 416 |
| 355 // static | 417 NetworkingPrivateApiTest::TestNetworkingPrivateDelegateFactory* |
| 356 TestDelegate* NetworkingPrivateApiTest::s_test_delegate_ = NULL; | 418 NetworkingPrivateApiTest::s_networking_private_delegate_factory_ptr = |
| 419 nullptr; |
| 357 | 420 |
| 358 } // namespace | 421 } // namespace |
| 359 | 422 |
| 360 // Place each subtest into a separate browser test so that the stub networking | 423 // Place each subtest into a separate browser test so that the stub networking |
| 361 // library state is reset for each subtest run. This way they won't affect each | 424 // library state is reset for each subtest run. This way they won't affect each |
| 362 // other. TODO(stevenjb): Use extensions::ApiUnitTest once moved to | 425 // other. TODO(stevenjb): Use extensions::ApiUnitTest once moved to |
| 363 // src/extensions. | 426 // src/extensions. |
| 364 | 427 |
| 365 // These fail on Windows due to crbug.com/177163. Note: we still have partial | 428 // These fail on Windows due to crbug.com/177163. Note: we still have partial |
| 366 // coverage in NetworkingPrivateServiceClientApiTest. TODO(stevenjb): Enable | 429 // coverage in NetworkingPrivateServiceClientApiTest. TODO(stevenjb): Enable |
| (...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 468 } | 531 } |
| 469 | 532 |
| 470 IN_PROC_BROWSER_TEST_F(NetworkingPrivateApiTest, GetGlobalPolicy) { | 533 IN_PROC_BROWSER_TEST_F(NetworkingPrivateApiTest, GetGlobalPolicy) { |
| 471 EXPECT_TRUE(RunNetworkingSubtest("getGlobalPolicy")) << message_; | 534 EXPECT_TRUE(RunNetworkingSubtest("getGlobalPolicy")) << message_; |
| 472 } | 535 } |
| 473 | 536 |
| 474 // Test failure case | 537 // Test failure case |
| 475 | 538 |
| 476 class NetworkingPrivateApiTestFail : public NetworkingPrivateApiTest { | 539 class NetworkingPrivateApiTestFail : public NetworkingPrivateApiTest { |
| 477 public: | 540 public: |
| 478 NetworkingPrivateApiTestFail() { s_test_delegate_->set_fail(true); } | 541 NetworkingPrivateApiTestFail() { test_failure_ = true; } |
| 542 |
| 543 ~NetworkingPrivateApiTestFail() override = default; |
| 479 | 544 |
| 480 protected: | 545 protected: |
| 481 DISALLOW_COPY_AND_ASSIGN(NetworkingPrivateApiTestFail); | 546 DISALLOW_COPY_AND_ASSIGN(NetworkingPrivateApiTestFail); |
| 482 }; | 547 }; |
| 483 | 548 |
| 484 IN_PROC_BROWSER_TEST_F(NetworkingPrivateApiTestFail, GetProperties) { | 549 IN_PROC_BROWSER_TEST_F(NetworkingPrivateApiTestFail, GetProperties) { |
| 485 EXPECT_FALSE(RunNetworkingSubtest("getProperties")) << message_; | 550 EXPECT_FALSE(RunNetworkingSubtest("getProperties")) << message_; |
| 486 } | 551 } |
| 487 | 552 |
| 488 IN_PROC_BROWSER_TEST_F(NetworkingPrivateApiTestFail, GetManagedProperties) { | 553 IN_PROC_BROWSER_TEST_F(NetworkingPrivateApiTestFail, GetManagedProperties) { |
| (...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 568 EXPECT_FALSE(RunNetworkingSubtest("unlockCellularSim")) << message_; | 633 EXPECT_FALSE(RunNetworkingSubtest("unlockCellularSim")) << message_; |
| 569 } | 634 } |
| 570 | 635 |
| 571 IN_PROC_BROWSER_TEST_F(NetworkingPrivateApiTestFail, SetCellularSimState) { | 636 IN_PROC_BROWSER_TEST_F(NetworkingPrivateApiTestFail, SetCellularSimState) { |
| 572 EXPECT_FALSE(RunNetworkingSubtest("setCellularSimState")) << message_; | 637 EXPECT_FALSE(RunNetworkingSubtest("setCellularSimState")) << message_; |
| 573 } | 638 } |
| 574 | 639 |
| 575 #endif // defined(OS_WIN) | 640 #endif // defined(OS_WIN) |
| 576 | 641 |
| 577 } // namespace extensions | 642 } // namespace extensions |
| OLD | NEW |