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