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

Side by Side Diff: chrome/browser/policy/remote_commands/remote_commands_browsertest.cc

Issue 879233003: Initial RemoteCommandService (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@remote-commands
Patch Set: WIP Created 5 years, 10 months 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
OLDNEW
(Empty)
1 // Copyright 2015 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 <string>
6
7 #include "base/command_line.h"
8 #include "base/logging.h"
9 #include "base/macros.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "base/run_loop.h"
12 #include "base/time/time.h"
13 #include "chrome/browser/browser_process.h"
14 #include "chrome/browser/policy/cloud/test_request_interceptor.h"
15 #include "chrome/browser/policy/remote_commands/testing_remote_commands_server.h "
16 #include "chrome/browser/profiles/profile.h"
17 #include "chrome/browser/ui/browser.h"
18 #include "chrome/test/base/in_process_browser_test.h"
19 #include "components/policy/core/browser/browser_policy_connector.h"
20 #include "components/policy/core/common/policy_switches.h"
21 #include "components/policy/core/common/remote_commands/remote_command_job.h"
22 #include "components/policy/core/common/remote_commands/remote_commands_service. h"
23 #include "components/policy/core/common/remote_commands/test_remote_command_job. h"
24 #include "content/public/browser/browser_thread.h"
25 #include "net/url_request/url_request_context_getter.h"
26 #include "policy/proto/device_management_backend.pb.h"
27 #include "testing/gmock/include/gmock/gmock.h"
28 #include "testing/gtest/include/gtest/gtest.h"
29
30 #if defined(OS_CHROMEOS)
31 #include "chrome/browser/chromeos/policy/user_cloud_policy_manager_chromeos.h"
32 #include "chrome/browser/chromeos/policy/user_cloud_policy_manager_factory_chrom eos.h"
33 #else
34 #include "chrome/browser/policy/cloud/user_cloud_policy_manager_factory.h"
35 #include "components/policy/core/common/cloud/user_cloud_policy_manager.h"
36 #endif
37
38 using testing::AnyNumber;
39 using testing::InvokeWithoutArgs;
40 using testing::ReturnNew;
41 using testing::_;
42
43 namespace policy {
44
45 namespace {
46 const char kTestToken[] = "secret_token";
47 const char kTestClientID[] = "testing_client_id";
48 const char kTestPayload[] = "_testing_payload_";
49 } // namespace
50
51 namespace em = enterprise_management;
52
53 class MockTestRemoteCommandFactory : public RemoteCommandJob::Factory {
54 public:
55 MockTestRemoteCommandFactory() {}
56 ~MockTestRemoteCommandFactory() override {}
57
58 MOCK_METHOD0(BuildTestCommand, TestRemoteCommandJob*());
59
60 private:
61 // RemoteCommandJob::Factory:
62 scoped_ptr<RemoteCommandJob> BuildJobForType(
63 em::RemoteCommand_Type type) override {
64 if (type != em::RemoteCommand_Type_COMMAND_ECHO_TEST)
65 return nullptr;
bartfab (slow) 2015/02/12 14:29:18 Nit: Add NOTREACHED().
binjin 2015/02/16 22:46:22 Done.
66 return make_scoped_ptr<RemoteCommandJob>(BuildTestCommand());
67 }
68
69 DISALLOW_COPY_AND_ASSIGN(MockTestRemoteCommandFactory);
70 };
71
72 class MockRemoteCommandsServer : public TestingRemoteCommandsServer {
73 public:
74 MockRemoteCommandsServer() {}
75 ~MockRemoteCommandsServer() override {}
76
77 MOCK_CONST_METHOD2(SucceededJobReported,
78 void(const std::string&, base::Time));
79 MOCK_CONST_METHOD1(FailedJobReported, void(base::Time));
80 MOCK_CONST_METHOD1(IgnoredJobReported, void(base::Time));
81
82 private:
83 // TestingRemoteCommandsServer:
84 void JobResultReported(const enterprise_management::RemoteCommandResult&
85 job_result) const override {
86 base::Time timestamp;
87 if (job_result.has_timestamp()) {
bartfab (slow) 2015/02/12 14:29:18 It would be an error for the job not to have a tim
binjin 2015/02/16 22:46:22 Done.
88 timestamp = base::TimeDelta::FromMilliseconds(job_result.timestamp()) +
89 base::Time::UnixEpoch();
90 }
91 switch (job_result.result()) {
92 case em::RemoteCommandResult_ResultType_RESULT_SUCCESS:
93 SucceededJobReported(job_result.payload(), timestamp);
94 break;
95 case em::RemoteCommandResult_ResultType_RESULT_FAILURE:
96 FailedJobReported(timestamp);
97 break;
98 case em::RemoteCommandResult_ResultType_RESULT_IGNORED:
99 IgnoredJobReported(timestamp);
100 break;
101 default:
102 NOTREACHED();
103 }
104 }
105
106 DISALLOW_COPY_AND_ASSIGN(MockRemoteCommandsServer);
107 };
108
109 // XXX
110 class RemoteCommandsBrowserTest : public InProcessBrowserTest {
bartfab (slow) 2015/02/12 14:29:18 Rather than being a browser test, I think this sho
binjin 2015/02/16 22:46:22 I will change this into a unit test
111 protected:
112 RemoteCommandsBrowserTest() {}
113 ~RemoteCommandsBrowserTest() override {}
114
115 // XXX
116 void Register() {
117 ASSERT_TRUE(policy_manager());
118 ASSERT_TRUE(policy_manager()->core()->client());
119
120 EXPECT_FALSE(policy_manager()->core()->client()->is_registered());
121 policy_manager()->core()->client()->SetupRegistration(kTestToken,
122 kTestClientID);
123 EXPECT_TRUE(policy_manager()->core()->client()->is_registered());
124 }
125
126 void SetUpInProcessBrowserTestFixture() override {
127 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
128 command_line->AppendSwitchASCII(switches::kDeviceManagementUrl,
129 "http://localhost");
130 }
131
132 void SetUpOnMainThread() override {
133 interceptor_.reset(new TestRequestInterceptor(
134 "localhost", content::BrowserThread::GetMessageLoopProxyForThread(
135 content::BrowserThread::IO)));
136 server_.reset(new MockRemoteCommandsServer());
137
138 BrowserPolicyConnector* connector =
bartfab (slow) 2015/02/12 14:29:18 Nit: s/BrowserPolicyConnector*/BrowserPolicyConnec
binjin 2015/02/16 22:46:22 Done.
139 g_browser_process->browser_policy_connector();
140 connector->ScheduleServiceInitialization(0);
141
142 ASSERT_TRUE(policy_manager());
143
144 #if !defined(OS_CHROMEOS)
bartfab (slow) 2015/02/12 14:29:18 I think it is OK to limit ourselves to Chrome OS n
binjin 2015/02/16 22:46:22 Acknowledged.
145 policy_manager()->Connect(
146 g_browser_process->local_state(),
147 g_browser_process->system_request_context(),
148 UserCloudPolicyManager::CreateCloudPolicyClient(
149 connector->device_management_service(),
150 g_browser_process->system_request_context()).Pass());
151 #endif
152
153 scoped_ptr<MockTestRemoteCommandFactory> factory(
154 new MockTestRemoteCommandFactory());
155 mock_factory_ = factory.get();
156 policy_manager()->core()->StartRemoteCommandsService(factory.Pass());
157 }
158
159 void TearDownOnMainThread() override {
160 EXPECT_EQ(0u, interceptor_->GetPendingSize());
161 mock_factory_ = nullptr;
bartfab (slow) 2015/02/12 14:29:18 No need for this, really. It is a non-owner pointe
binjin 2015/02/16 22:46:22 Done.
162 server_.reset();
163 interceptor_.reset();
164 }
165
166 #if defined(OS_CHROMEOS)
167 UserCloudPolicyManagerChromeOS* policy_manager() {
168 return UserCloudPolicyManagerFactoryChromeOS::GetForProfile(
bartfab (slow) 2015/02/12 14:29:18 We are only implementing device commands for now.
binjin 2015/02/16 22:46:22 Acknowledged.
169 browser()->profile());
170 }
171 #else
172 UserCloudPolicyManager* policy_manager() {
173 return UserCloudPolicyManagerFactory::GetForBrowserContext(
174 browser()->profile());
175 }
176 #endif // defined(OS_CHROMEOS)
177
178 scoped_ptr<TestRequestInterceptor> interceptor_;
179 scoped_ptr<MockRemoteCommandsServer> server_;
180 MockTestRemoteCommandFactory* mock_factory_;
bartfab (slow) 2015/02/12 14:29:18 Nit: Initialize with = nullptr.
binjin 2015/02/16 22:46:22 Done.
181
182 private:
183 DISALLOW_COPY_AND_ASSIGN(RemoteCommandsBrowserTest);
184 };
185
186 IN_PROC_BROWSER_TEST_F(RemoteCommandsBrowserTest, SingleCommand) {
187 Register();
188
189 interceptor_->PushJobCallback(
190 TestRequestInterceptor::FetchRemoteCommandsJob(server_.get(), true));
191 interceptor_->PushJobCallback(
192 TestRequestInterceptor::FetchRemoteCommandsJob(server_.get(), true));
bartfab (slow) 2015/02/12 14:29:18 Why do we expect two callbacks?
binjin 2015/02/16 22:46:22 First one is for commands fetching, second one is
193
194 base::RunLoop run_loop;
195 EXPECT_CALL(*mock_factory_, BuildTestCommand())
196 .Times(1)
197 .WillOnce(ReturnNew<TestRemoteCommandJob>(
198 true, base::TimeDelta::FromSeconds(1)));
199 EXPECT_CALL(*server_, SucceededJobReported(kTestPayload, _))
200 .Times(1)
201 .WillOnce(InvokeWithoutArgs(&run_loop, &base::RunLoop::Quit));
202 server_->IssueCommand(em::RemoteCommand_Type_COMMAND_ECHO_TEST, kTestPayload);
203
204 policy_manager()->core()->remote_commands_service()->FetchRemoteCommands();
bartfab (slow) 2015/02/12 14:29:18 Will connecting the client not issue a remote comm
binjin 2015/02/16 22:46:22 Acknowledged.
205 run_loop.Run();
206
207 EXPECT_EQ(0u, server_->GetUnreportedCommandCount());
208 }
209
210 } // namespace policy
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698