| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 "chrome/browser/policy/device_management_backend_impl.h" | 5 #include "chrome/browser/policy/device_management_service.h" |
| 6 | 6 |
| 7 #include "base/message_loop.h" | 7 #include "base/message_loop.h" |
| 8 #include "chrome/browser/browser.h" |
| 8 #include "chrome/browser/browser_thread.h" | 9 #include "chrome/browser/browser_thread.h" |
| 10 #include "chrome/browser/profile.h" |
| 9 #include "chrome/browser/policy/device_management_backend_mock.h" | 11 #include "chrome/browser/policy/device_management_backend_mock.h" |
| 10 #include "chrome/test/in_process_browser_test.h" | 12 #include "chrome/test/in_process_browser_test.h" |
| 11 #include "net/test/test_server.h" | 13 #include "net/test/test_server.h" |
| 12 #include "net/url_request/url_request.h" | 14 #include "net/url_request/url_request.h" |
| 13 #include "net/url_request/url_request_test_job.h" | 15 #include "net/url_request/url_request_test_job.h" |
| 14 #include "testing/gtest/include/gtest/gtest.h" | 16 #include "testing/gtest/include/gtest/gtest.h" |
| 15 #include "testing/gmock/include/gmock/gmock.h" | 17 #include "testing/gmock/include/gmock/gmock.h" |
| 16 | 18 |
| 17 using testing::_; | 19 using testing::_; |
| 18 using testing::DoAll; | 20 using testing::DoAll; |
| 19 using testing::Invoke; | 21 using testing::Invoke; |
| 20 using testing::InvokeWithoutArgs; | 22 using testing::InvokeWithoutArgs; |
| 21 | 23 |
| 22 namespace policy { | 24 namespace policy { |
| 23 | 25 |
| 24 namespace { | 26 namespace { |
| 25 | 27 |
| 26 const char kServiceUrl[] = "http://example.com/service"; | 28 // Dummy service URL for testing with request interception enabled. |
| 29 const char kServiceURL[] = "http://example.com/device_management"; |
| 27 | 30 |
| 28 // Binary representation of successful register response containing a token. | 31 // Binary representation of successful register response containing a token. |
| 29 const char kServiceResponseRegister[] = | 32 const char kServiceResponseRegister[] = |
| 30 "\x08\x00\x1a\x22\x0a\x20\x64\x64\x32\x63\x38\x63\x33\x65\x64\x61" | 33 "\x08\x00\x1a\x22\x0a\x20\x64\x64\x32\x63\x38\x63\x33\x65\x64\x61" |
| 31 "\x63\x63\x34\x61\x33\x32\x38\x31\x66\x33\x38\x62\x36\x35\x31\x31" | 34 "\x63\x63\x34\x61\x33\x32\x38\x31\x66\x33\x38\x62\x36\x35\x31\x31" |
| 32 "\x36\x64\x61\x62\x66\x63"; | 35 "\x36\x64\x61\x62\x66\x63"; |
| 33 // Contains a single policy setting, namely HomepageIsNewTabPage: false. | 36 // Contains a single policy setting, namely HomepageIsNewTabPage: false. |
| 34 const char kServiceResponsePolicy[] = | 37 const char kServiceResponsePolicy[] = |
| 35 "\x08\x00\x2a\x2a\x0a\x28\x0a\x06\x70\x6f\x6c\x69\x63\x79\x12\x1e" | 38 "\x08\x00\x2a\x2a\x0a\x28\x0a\x06\x70\x6f\x6c\x69\x63\x79\x12\x1e" |
| 36 "\x0a\x1c\x0a\x14\x48\x6f\x6d\x65\x70\x61\x67\x65\x49\x73\x4e\x65" | 39 "\x0a\x1c\x0a\x14\x48\x6f\x6d\x65\x70\x61\x67\x65\x49\x73\x4e\x65" |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 68 true); | 71 true); |
| 69 } | 72 } |
| 70 | 73 |
| 71 return NULL; | 74 return NULL; |
| 72 } | 75 } |
| 73 | 76 |
| 74 const GURL service_url_; | 77 const GURL service_url_; |
| 75 const std::string response_data_; | 78 const std::string response_data_; |
| 76 }; | 79 }; |
| 77 | 80 |
| 78 class DeviceManagementBackendImplIntegrationTest : public InProcessBrowserTest { | 81 class DeviceManagementServiceIntegrationTest : public InProcessBrowserTest { |
| 79 public: | 82 public: |
| 80 void CaptureToken(const em::DeviceRegisterResponse& response) { | 83 void CaptureToken(const em::DeviceRegisterResponse& response) { |
| 81 token_ = response.device_management_token(); | 84 token_ = response.device_management_token(); |
| 82 } | 85 } |
| 83 | 86 |
| 84 protected: | 87 protected: |
| 85 std::string token_; | 88 std::string token_; |
| 86 }; | 89 }; |
| 87 | 90 |
| 88 static void QuitMessageLoop() { | 91 static void QuitMessageLoop() { |
| 89 MessageLoop::current()->Quit(); | 92 MessageLoop::current()->Quit(); |
| 90 } | 93 } |
| 91 | 94 |
| 92 IN_PROC_BROWSER_TEST_F(DeviceManagementBackendImplIntegrationTest, | 95 IN_PROC_BROWSER_TEST_F(DeviceManagementServiceIntegrationTest, |
| 93 CannedResponses) { | 96 CannedResponses) { |
| 94 URLFetcher::enable_interception_for_tests(true); | 97 URLFetcher::enable_interception_for_tests(true); |
| 95 DeviceManagementBackendImpl service(kServiceUrl); | 98 DeviceManagementService backend(kServiceURL); |
| 99 backend.Initialize(browser()->profile()->GetRequestContext()); |
| 100 scoped_ptr<DeviceManagementBackend> service(backend.CreateBackend()); |
| 96 | 101 |
| 97 { | 102 { |
| 98 CannedResponseInterceptor interceptor( | 103 CannedResponseInterceptor interceptor( |
| 99 GURL(kServiceUrl), PROTO_STRING(kServiceResponseRegister)); | 104 GURL(kServiceURL), PROTO_STRING(kServiceResponseRegister)); |
| 100 DeviceRegisterResponseDelegateMock delegate; | 105 DeviceRegisterResponseDelegateMock delegate; |
| 101 EXPECT_CALL(delegate, HandleRegisterResponse(_)) | 106 EXPECT_CALL(delegate, HandleRegisterResponse(_)) |
| 102 .WillOnce(DoAll(Invoke(this, &DeviceManagementBackendImplIntegrationTest | 107 .WillOnce(DoAll(Invoke(this, &DeviceManagementServiceIntegrationTest |
| 103 ::CaptureToken), | 108 ::CaptureToken), |
| 104 InvokeWithoutArgs(QuitMessageLoop))); | 109 InvokeWithoutArgs(QuitMessageLoop))); |
| 105 em::DeviceRegisterRequest request; | 110 em::DeviceRegisterRequest request; |
| 106 service.ProcessRegisterRequest("token", "device id", request, &delegate); | 111 service->ProcessRegisterRequest("token", "device id", request, &delegate); |
| 107 MessageLoop::current()->Run(); | 112 MessageLoop::current()->Run(); |
| 108 } | 113 } |
| 109 | 114 |
| 110 { | 115 { |
| 111 CannedResponseInterceptor interceptor( | 116 CannedResponseInterceptor interceptor( |
| 112 GURL(kServiceUrl), PROTO_STRING(kServiceResponsePolicy)); | 117 GURL(kServiceURL), PROTO_STRING(kServiceResponsePolicy)); |
| 113 DevicePolicyResponseDelegateMock delegate; | 118 DevicePolicyResponseDelegateMock delegate; |
| 114 EXPECT_CALL(delegate, HandlePolicyResponse(_)) | 119 EXPECT_CALL(delegate, HandlePolicyResponse(_)) |
| 115 .WillOnce(InvokeWithoutArgs(QuitMessageLoop)); | 120 .WillOnce(InvokeWithoutArgs(QuitMessageLoop)); |
| 116 em::DevicePolicyRequest request; | 121 em::DevicePolicyRequest request; |
| 117 request.set_policy_scope("chrome"); | 122 request.set_policy_scope("chrome"); |
| 118 em::DevicePolicySettingRequest* setting_request = | 123 em::DevicePolicySettingRequest* setting_request = |
| 119 request.add_setting_request(); | 124 request.add_setting_request(); |
| 120 setting_request->set_key("policy"); | 125 setting_request->set_key("policy"); |
| 121 service.ProcessPolicyRequest(token_, request, &delegate); | 126 service->ProcessPolicyRequest(token_, request, &delegate); |
| 122 | 127 |
| 123 MessageLoop::current()->Run(); | 128 MessageLoop::current()->Run(); |
| 124 } | 129 } |
| 125 | 130 |
| 126 { | 131 { |
| 127 CannedResponseInterceptor interceptor( | 132 CannedResponseInterceptor interceptor( |
| 128 GURL(kServiceUrl), PROTO_STRING(kServiceResponseUnregister)); | 133 GURL(kServiceURL), PROTO_STRING(kServiceResponseUnregister)); |
| 129 DeviceUnregisterResponseDelegateMock delegate; | 134 DeviceUnregisterResponseDelegateMock delegate; |
| 130 EXPECT_CALL(delegate, HandleUnregisterResponse(_)) | 135 EXPECT_CALL(delegate, HandleUnregisterResponse(_)) |
| 131 .WillOnce(InvokeWithoutArgs(QuitMessageLoop)); | 136 .WillOnce(InvokeWithoutArgs(QuitMessageLoop)); |
| 132 em::DeviceUnregisterRequest request; | 137 em::DeviceUnregisterRequest request; |
| 133 service.ProcessUnregisterRequest(token_, request, &delegate); | 138 service->ProcessUnregisterRequest(token_, request, &delegate); |
| 134 | 139 |
| 135 MessageLoop::current()->Run(); | 140 MessageLoop::current()->Run(); |
| 136 } | 141 } |
| 137 } | 142 } |
| 138 | 143 |
| 139 IN_PROC_BROWSER_TEST_F(DeviceManagementBackendImplIntegrationTest, | 144 IN_PROC_BROWSER_TEST_F(DeviceManagementServiceIntegrationTest, |
| 140 WithTestServer) { | 145 WithTestServer) { |
| 141 net::TestServer test_server( | 146 net::TestServer test_server_( |
| 142 net::TestServer::TYPE_HTTP, | 147 net::TestServer::TYPE_HTTP, |
| 143 FilePath(FILE_PATH_LITERAL("chrome/test/data/policy"))); | 148 FilePath(FILE_PATH_LITERAL("chrome/test/data/policy"))); |
| 144 ASSERT_TRUE(test_server.Start()); | 149 ASSERT_TRUE(test_server_.Start()); |
| 145 DeviceManagementBackendImpl service( | 150 DeviceManagementService backend( |
| 146 test_server.GetURL("device_management").spec()); | 151 test_server_.GetURL("device_management").spec()); |
| 152 backend.Initialize(browser()->profile()->GetRequestContext()); |
| 153 scoped_ptr<DeviceManagementBackend> service(backend.CreateBackend()); |
| 147 | 154 |
| 148 { | 155 { |
| 149 DeviceRegisterResponseDelegateMock delegate; | 156 DeviceRegisterResponseDelegateMock delegate; |
| 150 EXPECT_CALL(delegate, HandleRegisterResponse(_)) | 157 EXPECT_CALL(delegate, HandleRegisterResponse(_)) |
| 151 .WillOnce(DoAll(Invoke(this, &DeviceManagementBackendImplIntegrationTest | 158 .WillOnce(DoAll(Invoke(this, &DeviceManagementServiceIntegrationTest |
| 152 ::CaptureToken), | 159 ::CaptureToken), |
| 153 InvokeWithoutArgs(QuitMessageLoop))); | 160 InvokeWithoutArgs(QuitMessageLoop))); |
| 154 em::DeviceRegisterRequest request; | 161 em::DeviceRegisterRequest request; |
| 155 service.ProcessRegisterRequest("token", "device id", request, &delegate); | 162 service->ProcessRegisterRequest("token", "device id", request, &delegate); |
| 156 MessageLoop::current()->Run(); | 163 MessageLoop::current()->Run(); |
| 157 } | 164 } |
| 158 | 165 |
| 159 { | 166 { |
| 160 em::DevicePolicyResponse expected_response; | 167 em::DevicePolicyResponse expected_response; |
| 161 | 168 |
| 162 DevicePolicyResponseDelegateMock delegate; | 169 DevicePolicyResponseDelegateMock delegate; |
| 163 EXPECT_CALL(delegate, HandlePolicyResponse(_)) | 170 EXPECT_CALL(delegate, HandlePolicyResponse(_)) |
| 164 .WillOnce(InvokeWithoutArgs(QuitMessageLoop)); | 171 .WillOnce(InvokeWithoutArgs(QuitMessageLoop)); |
| 165 em::DevicePolicyRequest request; | 172 em::DevicePolicyRequest request; |
| 166 request.set_policy_scope("chrome"); | 173 request.set_policy_scope("chrome"); |
| 167 em::DevicePolicySettingRequest* setting_request = | 174 em::DevicePolicySettingRequest* setting_request = |
| 168 request.add_setting_request(); | 175 request.add_setting_request(); |
| 169 setting_request->set_key("policy"); | 176 setting_request->set_key("policy"); |
| 170 service.ProcessPolicyRequest(token_, request, &delegate); | 177 service->ProcessPolicyRequest(token_, request, &delegate); |
| 171 | 178 |
| 172 MessageLoop::current()->Run(); | 179 MessageLoop::current()->Run(); |
| 173 } | 180 } |
| 174 | 181 |
| 175 { | 182 { |
| 176 DeviceUnregisterResponseDelegateMock delegate; | 183 DeviceUnregisterResponseDelegateMock delegate; |
| 177 EXPECT_CALL(delegate, HandleUnregisterResponse(_)) | 184 EXPECT_CALL(delegate, HandleUnregisterResponse(_)) |
| 178 .WillOnce(InvokeWithoutArgs(QuitMessageLoop)); | 185 .WillOnce(InvokeWithoutArgs(QuitMessageLoop)); |
| 179 em::DeviceUnregisterRequest request; | 186 em::DeviceUnregisterRequest request; |
| 180 service.ProcessUnregisterRequest(token_, request, &delegate); | 187 service->ProcessUnregisterRequest(token_, request, &delegate); |
| 181 | 188 |
| 182 MessageLoop::current()->Run(); | 189 MessageLoop::current()->Run(); |
| 183 } | 190 } |
| 184 } | 191 } |
| 185 | 192 |
| 186 } // namespace policy | 193 } // namespace policy |
| OLD | NEW |