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