Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(301)

Side by Side Diff: chrome/browser/policy/device_management_backend_impl_browsertest.cc

Issue 4098004: Implement device management backend. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fix nits. Created 10 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "chrome/browser/policy/device_management_backend_impl.h"
6
7 #include "base/message_loop.h"
8 #include "chrome/browser/browser_thread.h"
9 #include "chrome/browser/policy/device_management_backend_mock.h"
10 #include "chrome/test/in_process_browser_test.h"
11 #include "net/url_request/url_request.h"
12 #include "net/url_request/url_request_test_job.h"
13 #include "testing/gtest/include/gtest/gtest.h"
14 #include "testing/gmock/include/gmock/gmock.h"
15
16 using testing::_;
17 using testing::DoAll;
18 using testing::Invoke;
19 using testing::InvokeWithoutArgs;
20
21 namespace policy {
22
23 namespace {
24
25 const char kServiceUrl[] = "http://example.com/service";
26
27 // Binary representation of successful register response containing a token.
28 const char kServiceResponseRegister[] =
29 "\x08\x00\x1a\x22\x0a\x20\x64\x64\x32\x63\x38\x63\x33\x65\x64\x61"
30 "\x63\x63\x34\x61\x33\x32\x38\x31\x66\x33\x38\x62\x36\x35\x31\x31"
31 "\x36\x64\x61\x62\x66\x63";
32 // Contains a single policy setting, namely HomepageIsNewTabPage: false.
33 const char kServiceResponsePolicy[] =
34 "\x08\x00\x2a\x2a\x0a\x28\x0a\x06\x70\x6f\x6c\x69\x63\x79\x12\x1e"
35 "\x0a\x1c\x0a\x14\x48\x6f\x6d\x65\x70\x61\x67\x65\x49\x73\x4e\x65"
36 "\x77\x54\x61\x62\x50\x61\x67\x65\x12\x04\x08\x01\x10\x00";
37 // Successful unregister response.
38 const char kServiceResponseUnregister[] =
39 "\x08\x00\x22\x00";
40
41 #define PROTO_STRING(name) (std::string(name, arraysize(name) - 1))
42
43 } // namespace
44
45 // Interceptor implementation that returns test data back to the service.
46 class CannedResponseInterceptor : public URLRequest::Interceptor {
47 public:
48 CannedResponseInterceptor(const GURL& service_url,
49 const std::string& response_data)
50 : service_url_(service_url),
51 response_data_(response_data) {
52 URLRequest::RegisterRequestInterceptor(this);
53 }
54
55 virtual ~CannedResponseInterceptor() {
56 URLRequest::UnregisterRequestInterceptor(this);
57 }
58
59 private:
60 // URLRequest::Interceptor overrides.
61 virtual URLRequestJob* MaybeIntercept(URLRequest* request) {
62 if (request->url().GetOrigin() == service_url_.GetOrigin() &&
63 request->url().path() == service_url_.path()) {
64 return new URLRequestTestJob(request,
65 URLRequestTestJob::test_headers(),
66 response_data_,
67 true);
68 }
69
70 return NULL;
71 }
72
73 const GURL service_url_;
74 const std::string response_data_;
75 };
76
77 class DeviceManagementBackendImplIntegrationTest : public InProcessBrowserTest {
78 public:
79 void CaptureToken(const em::DeviceRegisterResponse& response) {
80 token_ = response.device_management_token();
81 }
82
83 protected:
84 DeviceManagementBackendImplIntegrationTest() {
85 URLFetcher::enable_interception_for_tests(true);
86 }
87
88 std::string token_;
89 };
90
91 static void QuitMessageLoop() {
92 MessageLoop::current()->Quit();
93 }
94
95 IN_PROC_BROWSER_TEST_F(DeviceManagementBackendImplIntegrationTest,
96 RegisterAndFetchPolicy) {
97 DeviceManagementBackendImpl service(kServiceUrl);
98
99 {
100 CannedResponseInterceptor interceptor(
101 GURL(kServiceUrl), PROTO_STRING(kServiceResponseRegister));
102 DeviceRegisterResponseDelegateMock delegate;
103 EXPECT_CALL(delegate, HandleRegisterResponse(_))
104 .WillOnce(DoAll(Invoke(this, &DeviceManagementBackendImplIntegrationTest
105 ::CaptureToken),
106 InvokeWithoutArgs(QuitMessageLoop)));
107 em::DeviceRegisterRequest request;
108 service.ProcessRegisterRequest("token", "device id", request, &delegate);
109 MessageLoop::current()->Run();
110 }
111
112 {
113 CannedResponseInterceptor interceptor(
114 GURL(kServiceUrl), PROTO_STRING(kServiceResponsePolicy));
115 DevicePolicyResponseDelegateMock delegate;
116 EXPECT_CALL(delegate, HandlePolicyResponse(_))
117 .WillOnce(InvokeWithoutArgs(QuitMessageLoop));
118 em::DevicePolicyRequest request;
119 request.set_policy_scope("chrome");
120 em::DevicePolicySettingRequest* setting_request =
121 request.add_setting_request();
122 setting_request->set_key("policy");
123 service.ProcessPolicyRequest(token_, request, &delegate);
124
125 MessageLoop::current()->Run();
126 }
127
128 {
129 CannedResponseInterceptor interceptor(
130 GURL(kServiceUrl), PROTO_STRING(kServiceResponseUnregister));
131 DeviceUnregisterResponseDelegateMock delegate;
132 EXPECT_CALL(delegate, HandleUnregisterResponse(_))
133 .WillOnce(InvokeWithoutArgs(QuitMessageLoop));
134 em::DeviceUnregisterRequest request;
135 service.ProcessUnregisterRequest(token_, request, &delegate);
136
137 MessageLoop::current()->Run();
138 }
139 }
140
141 } // namespace policy
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698