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