Index: chrome/browser/policy/cloud_policy_manager_browsertest.cc |
diff --git a/chrome/browser/policy/cloud_policy_manager_browsertest.cc b/chrome/browser/policy/cloud_policy_manager_browsertest.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..14c8f3595b373e82de8784ec563178f6ac96cfcd |
--- /dev/null |
+++ b/chrome/browser/policy/cloud_policy_manager_browsertest.cc |
@@ -0,0 +1,191 @@ |
+// Copyright (c) 2013 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include <string> |
+ |
+#include "base/command_line.h" |
+#include "base/memory/scoped_ptr.h" |
+#include "base/run_loop.h" |
+#include "chrome/browser/browser_process.h" |
+#include "chrome/browser/policy/browser_policy_connector.h" |
+#include "chrome/browser/policy/cloud_policy_client.h" |
+#include "chrome/browser/policy/cloud_policy_constants.h" |
Mattias Nissler (ping if slow)
2013/02/15 08:07:36
needed?
Joao da Silva
2013/02/15 12:30:19
Done.
|
+#include "chrome/browser/policy/test_request_interceptor.h" |
+#include "chrome/browser/policy/test_utils.h" |
+#include "chrome/browser/profiles/profile.h" |
+#include "chrome/browser/ui/browser.h" |
+#include "chrome/common/chrome_switches.h" |
+#include "chrome/test/base/in_process_browser_test.h" |
+#include "content/public/browser/browser_thread.h" |
Mattias Nissler (ping if slow)
2013/02/15 08:07:36
needed?
Joao da Silva
2013/02/15 12:30:19
Done.
|
+#include "googleurl/src/gurl.h" |
Mattias Nissler (ping if slow)
2013/02/15 08:07:36
needed?
Joao da Silva
2013/02/15 12:30:19
Done.
|
+#include "net/base/net_errors.h" |
+#include "policy/policy_constants.h" |
Mattias Nissler (ping if slow)
2013/02/15 08:07:36
needed?
Joao da Silva
2013/02/15 12:30:19
Done.
|
+#include "testing/gmock/include/gmock/gmock.h" |
+#include "testing/gtest/include/gtest/gtest.h" |
+ |
+#if defined(OS_CHROMEOS) |
+#include "chrome/browser/chromeos/login/user_manager.h" |
Mattias Nissler (ping if slow)
2013/02/15 08:07:36
needed?
Joao da Silva
2013/02/15 12:30:19
Done.
|
+#include "chrome/browser/policy/user_cloud_policy_manager_chromeos.h" |
+#else |
+#include "chrome/browser/policy/user_cloud_policy_manager.h" |
+#include "chrome/browser/policy/user_cloud_policy_manager_factory.h" |
+#include "chrome/browser/signin/signin_manager.h" |
+#include "chrome/browser/signin/signin_manager_factory.h" |
+#endif |
+ |
+using testing::AnyNumber; |
+using testing::InvokeWithoutArgs; |
+using testing::Mock; |
+using testing::_; |
+ |
+namespace policy { |
+ |
+namespace { |
+ |
+class MockCloudPolicyClientObserver : public CloudPolicyClient::Observer { |
Mattias Nissler (ping if slow)
2013/02/15 08:07:36
FWIW, we have this already in could_policy_client_
Joao da Silva
2013/02/15 12:30:19
And also in cloud_policy_browsertest.cc! Moved to
|
+ public: |
+ MockCloudPolicyClientObserver() {} |
+ virtual ~MockCloudPolicyClientObserver() {} |
+ |
+ MOCK_METHOD1(OnPolicyFetched, void(CloudPolicyClient*)); |
+ MOCK_METHOD1(OnRegistrationStateChanged, void(CloudPolicyClient*)); |
+ MOCK_METHOD1(OnClientError, void(CloudPolicyClient*)); |
+}; |
+ |
+} // namespace |
+ |
+// Tests the cloud policy stack using a URLRequestJobFactory::ProtocolHandler |
+// to intercept requests and produce canned responses. |
+class CloudPolicyManagerTest : public InProcessBrowserTest { |
+ protected: |
+ CloudPolicyManagerTest() {} |
+ virtual ~CloudPolicyManagerTest() {} |
+ |
+ virtual void SetUpInProcessBrowserTestFixture() OVERRIDE { |
+ CommandLine* command_line = CommandLine::ForCurrentProcess(); |
+ command_line->AppendSwitchASCII(switches::kDeviceManagementUrl, |
+ "http://localhost"); |
+ } |
+ |
+ virtual void SetUpOnMainThread() OVERRIDE { |
+ ASSERT_TRUE(PolicyServiceIsEmpty(g_browser_process->policy_service())) |
+ << "Pre-existing policies in this machine will make this test fail."; |
+ |
+ interceptor_.reset(new TestRequestInterceptor("localhost")); |
+ |
+ BrowserPolicyConnector* connector = |
+ g_browser_process->browser_policy_connector(); |
+ connector->ScheduleServiceInitialization(0); |
+ |
+#if !defined(OS_CHROMEOS) |
+ // Mock a signed-in user. This is used by the UserCloudPolicyStore to pass |
+ // the username to the UserCloudPolicyValidator. |
+ SigninManager* signin_manager = |
+ SigninManagerFactory::GetForProfile(browser()->profile()); |
+ ASSERT_TRUE(signin_manager); |
+ signin_manager->SetAuthenticatedUsername("user@example.com"); |
+ |
+ UserCloudPolicyManager* policy_manager = |
+ UserCloudPolicyManagerFactory::GetForProfile(browser()->profile()); |
+ ASSERT_TRUE(policy_manager); |
+ policy_manager->Connect(g_browser_process->local_state(), |
+ UserCloudPolicyManager::CreateCloudPolicyClient( |
+ connector->device_management_service()).Pass()); |
+#endif |
+ } |
+ |
+ virtual void CleanUpOnMainThread() OVERRIDE { |
+ // Verify that all the expected requests were handled. |
+ EXPECT_EQ(0u, interceptor_->GetPendingSize()); |
+ |
+ interceptor_.reset(); |
+ } |
+ |
+#if defined(OS_CHROMEOS) |
+ UserCloudPolicyManagerChromeOS* policy_manager() { |
+ return g_browser_process->browser_policy_connector()-> |
+ GetUserCloudPolicyManager(); |
+ } |
+#else |
+ UserCloudPolicyManager* policy_manager() { |
+ return UserCloudPolicyManagerFactory::GetForProfile(browser()->profile()); |
+ } |
+#endif // defined(OS_CHROMEOS) |
+ |
+ // Register the client of the policy_manager() using a bogus auth token, and |
+ // returns once the registration gets a result back. |
+ void Register() { |
+ ASSERT_TRUE(policy_manager()); |
+ ASSERT_TRUE(policy_manager()->core()->client()); |
+ |
+ base::RunLoop run_loop; |
+ MockCloudPolicyClientObserver observer; |
+ EXPECT_CALL(observer, OnRegistrationStateChanged(_)) |
+ .Times(AnyNumber()) |
+ .WillRepeatedly(InvokeWithoutArgs(&run_loop, &base::RunLoop::Quit)); |
+ EXPECT_CALL(observer, OnClientError(_)) |
+ .Times(AnyNumber()) |
+ .WillRepeatedly(InvokeWithoutArgs(&run_loop, &base::RunLoop::Quit)); |
+ policy_manager()->core()->client()->AddObserver(&observer); |
+ |
+ // Give a bogus OAuth token to the |policy_manager|. This should make its |
+ // CloudPolicyClient fetch the DMToken. |
+ policy_manager()->RegisterClient("bogus"); |
+ run_loop.Run(); |
+ Mock::VerifyAndClearExpectations(&observer); |
+ policy_manager()->core()->client()->RemoveObserver(&observer); |
+ } |
+ |
+ scoped_ptr<TestRequestInterceptor> interceptor_; |
+}; |
+ |
+IN_PROC_BROWSER_TEST_F(CloudPolicyManagerTest, Register) { |
+ // Accept one register request. The initial request should not include the |
+ // reregister flag. |
+ const bool expect_reregister = false; |
+ interceptor_->PushJobCallback( |
+ TestRequestInterceptor::RegisterJob(expect_reregister)); |
+ |
+ EXPECT_FALSE(policy_manager()->core()->client()->is_registered()); |
+ ASSERT_NO_FATAL_FAILURE(Register()); |
+ EXPECT_TRUE(policy_manager()->core()->client()->is_registered()); |
+} |
+ |
+IN_PROC_BROWSER_TEST_F(CloudPolicyManagerTest, RegisterFails) { |
+ // The interceptor makes all requests fail by default; this will trigger |
+ // an OnClientError() call on the observer. |
+ EXPECT_FALSE(policy_manager()->core()->client()->is_registered()); |
+ ASSERT_NO_FATAL_FAILURE(Register()); |
+ EXPECT_FALSE(policy_manager()->core()->client()->is_registered()); |
+} |
+ |
+IN_PROC_BROWSER_TEST_F(CloudPolicyManagerTest, RegisterFailsWithRetries) { |
+ // Fail 4 times with ERR_NETWORK_CHANGED; the first 3 will trigger a retry, |
+ // the last one will forward the error to the client and unblock the |
+ // register process. |
+ for (int i = 0; i < 4; ++i) { |
+ interceptor_->PushJobCallback( |
+ TestRequestInterceptor::ErrorJob(net::ERR_NETWORK_CHANGED)); |
+ } |
+ |
+ EXPECT_FALSE(policy_manager()->core()->client()->is_registered()); |
+ ASSERT_NO_FATAL_FAILURE(Register()); |
+ EXPECT_FALSE(policy_manager()->core()->client()->is_registered()); |
+} |
+ |
+IN_PROC_BROWSER_TEST_F(CloudPolicyManagerTest, RegisterWithRetry) { |
+ // Accept one register request after failing once. The retry request should |
+ // set the reregister flag. |
+ interceptor_->PushJobCallback( |
+ TestRequestInterceptor::ErrorJob(net::ERR_NETWORK_CHANGED)); |
+ const bool expect_reregister = true; |
+ interceptor_->PushJobCallback( |
+ TestRequestInterceptor::RegisterJob(expect_reregister)); |
+ |
+ EXPECT_FALSE(policy_manager()->core()->client()->is_registered()); |
+ ASSERT_NO_FATAL_FAILURE(Register()); |
+ EXPECT_TRUE(policy_manager()->core()->client()->is_registered()); |
+} |
+ |
+} // namespace policy |