Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2013 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 <string> | |
| 6 | |
| 7 #include "base/command_line.h" | |
| 8 #include "base/memory/scoped_ptr.h" | |
| 9 #include "base/run_loop.h" | |
| 10 #include "chrome/browser/browser_process.h" | |
| 11 #include "chrome/browser/policy/browser_policy_connector.h" | |
| 12 #include "chrome/browser/policy/cloud_policy_client.h" | |
| 13 #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.
| |
| 14 #include "chrome/browser/policy/test_request_interceptor.h" | |
| 15 #include "chrome/browser/policy/test_utils.h" | |
| 16 #include "chrome/browser/profiles/profile.h" | |
| 17 #include "chrome/browser/ui/browser.h" | |
| 18 #include "chrome/common/chrome_switches.h" | |
| 19 #include "chrome/test/base/in_process_browser_test.h" | |
| 20 #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.
| |
| 21 #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.
| |
| 22 #include "net/base/net_errors.h" | |
| 23 #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.
| |
| 24 #include "testing/gmock/include/gmock/gmock.h" | |
| 25 #include "testing/gtest/include/gtest/gtest.h" | |
| 26 | |
| 27 #if defined(OS_CHROMEOS) | |
| 28 #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.
| |
| 29 #include "chrome/browser/policy/user_cloud_policy_manager_chromeos.h" | |
| 30 #else | |
| 31 #include "chrome/browser/policy/user_cloud_policy_manager.h" | |
| 32 #include "chrome/browser/policy/user_cloud_policy_manager_factory.h" | |
| 33 #include "chrome/browser/signin/signin_manager.h" | |
| 34 #include "chrome/browser/signin/signin_manager_factory.h" | |
| 35 #endif | |
| 36 | |
| 37 using testing::AnyNumber; | |
| 38 using testing::InvokeWithoutArgs; | |
| 39 using testing::Mock; | |
| 40 using testing::_; | |
| 41 | |
| 42 namespace policy { | |
| 43 | |
| 44 namespace { | |
| 45 | |
| 46 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
| |
| 47 public: | |
| 48 MockCloudPolicyClientObserver() {} | |
| 49 virtual ~MockCloudPolicyClientObserver() {} | |
| 50 | |
| 51 MOCK_METHOD1(OnPolicyFetched, void(CloudPolicyClient*)); | |
| 52 MOCK_METHOD1(OnRegistrationStateChanged, void(CloudPolicyClient*)); | |
| 53 MOCK_METHOD1(OnClientError, void(CloudPolicyClient*)); | |
| 54 }; | |
| 55 | |
| 56 } // namespace | |
| 57 | |
| 58 // Tests the cloud policy stack using a URLRequestJobFactory::ProtocolHandler | |
| 59 // to intercept requests and produce canned responses. | |
| 60 class CloudPolicyManagerTest : public InProcessBrowserTest { | |
| 61 protected: | |
| 62 CloudPolicyManagerTest() {} | |
| 63 virtual ~CloudPolicyManagerTest() {} | |
| 64 | |
| 65 virtual void SetUpInProcessBrowserTestFixture() OVERRIDE { | |
| 66 CommandLine* command_line = CommandLine::ForCurrentProcess(); | |
| 67 command_line->AppendSwitchASCII(switches::kDeviceManagementUrl, | |
| 68 "http://localhost"); | |
| 69 } | |
| 70 | |
| 71 virtual void SetUpOnMainThread() OVERRIDE { | |
| 72 ASSERT_TRUE(PolicyServiceIsEmpty(g_browser_process->policy_service())) | |
| 73 << "Pre-existing policies in this machine will make this test fail."; | |
| 74 | |
| 75 interceptor_.reset(new TestRequestInterceptor("localhost")); | |
| 76 | |
| 77 BrowserPolicyConnector* connector = | |
| 78 g_browser_process->browser_policy_connector(); | |
| 79 connector->ScheduleServiceInitialization(0); | |
| 80 | |
| 81 #if !defined(OS_CHROMEOS) | |
| 82 // Mock a signed-in user. This is used by the UserCloudPolicyStore to pass | |
| 83 // the username to the UserCloudPolicyValidator. | |
| 84 SigninManager* signin_manager = | |
| 85 SigninManagerFactory::GetForProfile(browser()->profile()); | |
| 86 ASSERT_TRUE(signin_manager); | |
| 87 signin_manager->SetAuthenticatedUsername("user@example.com"); | |
| 88 | |
| 89 UserCloudPolicyManager* policy_manager = | |
| 90 UserCloudPolicyManagerFactory::GetForProfile(browser()->profile()); | |
| 91 ASSERT_TRUE(policy_manager); | |
| 92 policy_manager->Connect(g_browser_process->local_state(), | |
| 93 UserCloudPolicyManager::CreateCloudPolicyClient( | |
| 94 connector->device_management_service()).Pass()); | |
| 95 #endif | |
| 96 } | |
| 97 | |
| 98 virtual void CleanUpOnMainThread() OVERRIDE { | |
| 99 // Verify that all the expected requests were handled. | |
| 100 EXPECT_EQ(0u, interceptor_->GetPendingSize()); | |
| 101 | |
| 102 interceptor_.reset(); | |
| 103 } | |
| 104 | |
| 105 #if defined(OS_CHROMEOS) | |
| 106 UserCloudPolicyManagerChromeOS* policy_manager() { | |
| 107 return g_browser_process->browser_policy_connector()-> | |
| 108 GetUserCloudPolicyManager(); | |
| 109 } | |
| 110 #else | |
| 111 UserCloudPolicyManager* policy_manager() { | |
| 112 return UserCloudPolicyManagerFactory::GetForProfile(browser()->profile()); | |
| 113 } | |
| 114 #endif // defined(OS_CHROMEOS) | |
| 115 | |
| 116 // Register the client of the policy_manager() using a bogus auth token, and | |
| 117 // returns once the registration gets a result back. | |
| 118 void Register() { | |
| 119 ASSERT_TRUE(policy_manager()); | |
| 120 ASSERT_TRUE(policy_manager()->core()->client()); | |
| 121 | |
| 122 base::RunLoop run_loop; | |
| 123 MockCloudPolicyClientObserver observer; | |
| 124 EXPECT_CALL(observer, OnRegistrationStateChanged(_)) | |
| 125 .Times(AnyNumber()) | |
| 126 .WillRepeatedly(InvokeWithoutArgs(&run_loop, &base::RunLoop::Quit)); | |
| 127 EXPECT_CALL(observer, OnClientError(_)) | |
| 128 .Times(AnyNumber()) | |
| 129 .WillRepeatedly(InvokeWithoutArgs(&run_loop, &base::RunLoop::Quit)); | |
| 130 policy_manager()->core()->client()->AddObserver(&observer); | |
| 131 | |
| 132 // Give a bogus OAuth token to the |policy_manager|. This should make its | |
| 133 // CloudPolicyClient fetch the DMToken. | |
| 134 policy_manager()->RegisterClient("bogus"); | |
| 135 run_loop.Run(); | |
| 136 Mock::VerifyAndClearExpectations(&observer); | |
| 137 policy_manager()->core()->client()->RemoveObserver(&observer); | |
| 138 } | |
| 139 | |
| 140 scoped_ptr<TestRequestInterceptor> interceptor_; | |
| 141 }; | |
| 142 | |
| 143 IN_PROC_BROWSER_TEST_F(CloudPolicyManagerTest, Register) { | |
| 144 // Accept one register request. The initial request should not include the | |
| 145 // reregister flag. | |
| 146 const bool expect_reregister = false; | |
| 147 interceptor_->PushJobCallback( | |
| 148 TestRequestInterceptor::RegisterJob(expect_reregister)); | |
| 149 | |
| 150 EXPECT_FALSE(policy_manager()->core()->client()->is_registered()); | |
| 151 ASSERT_NO_FATAL_FAILURE(Register()); | |
| 152 EXPECT_TRUE(policy_manager()->core()->client()->is_registered()); | |
| 153 } | |
| 154 | |
| 155 IN_PROC_BROWSER_TEST_F(CloudPolicyManagerTest, RegisterFails) { | |
| 156 // The interceptor makes all requests fail by default; this will trigger | |
| 157 // an OnClientError() call on the observer. | |
| 158 EXPECT_FALSE(policy_manager()->core()->client()->is_registered()); | |
| 159 ASSERT_NO_FATAL_FAILURE(Register()); | |
| 160 EXPECT_FALSE(policy_manager()->core()->client()->is_registered()); | |
| 161 } | |
| 162 | |
| 163 IN_PROC_BROWSER_TEST_F(CloudPolicyManagerTest, RegisterFailsWithRetries) { | |
| 164 // Fail 4 times with ERR_NETWORK_CHANGED; the first 3 will trigger a retry, | |
| 165 // the last one will forward the error to the client and unblock the | |
| 166 // register process. | |
| 167 for (int i = 0; i < 4; ++i) { | |
| 168 interceptor_->PushJobCallback( | |
| 169 TestRequestInterceptor::ErrorJob(net::ERR_NETWORK_CHANGED)); | |
| 170 } | |
| 171 | |
| 172 EXPECT_FALSE(policy_manager()->core()->client()->is_registered()); | |
| 173 ASSERT_NO_FATAL_FAILURE(Register()); | |
| 174 EXPECT_FALSE(policy_manager()->core()->client()->is_registered()); | |
| 175 } | |
| 176 | |
| 177 IN_PROC_BROWSER_TEST_F(CloudPolicyManagerTest, RegisterWithRetry) { | |
| 178 // Accept one register request after failing once. The retry request should | |
| 179 // set the reregister flag. | |
| 180 interceptor_->PushJobCallback( | |
| 181 TestRequestInterceptor::ErrorJob(net::ERR_NETWORK_CHANGED)); | |
| 182 const bool expect_reregister = true; | |
| 183 interceptor_->PushJobCallback( | |
| 184 TestRequestInterceptor::RegisterJob(expect_reregister)); | |
| 185 | |
| 186 EXPECT_FALSE(policy_manager()->core()->client()->is_registered()); | |
| 187 ASSERT_NO_FATAL_FAILURE(Register()); | |
| 188 EXPECT_TRUE(policy_manager()->core()->client()->is_registered()); | |
| 189 } | |
| 190 | |
| 191 } // namespace policy | |
| OLD | NEW |