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

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

Issue 8872032: Added the AutoEnrollmentClient and unit tests for it. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Use callback instead of Delegate, using InSequence, renamed switches, cleanups Created 9 years 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) 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 "chrome/browser/policy/auto_enrollment_client.h"
6
7 #include "base/bind.h"
8 #include "base/bind_helpers.h"
9 #include "base/rand_util.h"
10 #include "chrome/browser/policy/mock_device_management_backend.h"
11 #include "chrome/browser/policy/mock_device_management_service.h"
12 #include "crypto/sha2.h"
13 #include "testing/gmock/include/gmock/gmock.h"
14 #include "testing/gtest/include/gtest/gtest.h"
15
16 namespace policy {
17
18 namespace {
19
20 const char* kSerial = "serial";
21 const char* kSerialHash =
22 "\x01\x44\xb1\xde\xfc\xf7\x56\x10\x87\x01\x5f\x8d\x83\x0d\x65\xb1"
23 "\x6f\x02\x4a\xd7\xeb\x92\x45\xfc\xd4\xe4\x37\xa1\x55\x2b\x13\x8a";
24
25 using ::testing::AnyNumber;
26 using ::testing::DoAll;
27 using ::testing::InSequence;
28 using ::testing::Invoke;
29 using ::testing::Unused;
30 using ::testing::_;
31
32 ACTION_P(MockDeviceManagementBackendFailAutoEnrollment, error) {
33 arg2->OnError(error);
34 }
35
36 ACTION_P(MockDeviceManagementBackendSucceedAutoEnrollment, response) {
37 arg2->HandleAutoEnrollmentResponse(response);
38 }
39
40 class AutoEnrollmentClientTest : public testing::Test {
41 protected:
42 AutoEnrollmentClientTest()
43 : completion_callback_count_(0) {}
44
45 virtual void SetUp() OVERRIDE {
46 CreateClient(kSerial, 4, 8);
47 }
48
49 void CreateClient(const std::string& serial,
50 int power_initial,
51 int power_limit) {
52 MockDeviceManagementService* service = new MockDeviceManagementService;
53 EXPECT_CALL(*service, CreateBackend())
54 .Times(AnyNumber())
55 .WillRepeatedly(MockDeviceManagementServiceProxyBackend(&backend_));
56 base::Closure callback =
57 base::Bind(&AutoEnrollmentClientTest::CompletionCallback,
58 base::Unretained(this));
59 client_.reset(new AutoEnrollmentClient(callback,
60 service,
61 serial,
62 power_initial,
63 power_limit));
64 }
65
66 void CompletionCallback() {
67 completion_callback_count_++;
68 }
69
70 void ServerWillFail(DeviceManagementBackend::ErrorCode error) {
71 EXPECT_CALL(backend_, ProcessAutoEnrollmentRequest(_, _, _))
72 .WillOnce(
73 DoAll(
74 Invoke(this,
75 &AutoEnrollmentClientTest::CaptureRequest),
Mattias Nissler (ping if slow) 2011/12/12 10:20:31 nit: seems like there is more line breaking than r
Joao da Silva 2011/12/12 11:54:13 Done.
76 MockDeviceManagementBackendFailAutoEnrollment(error)));
77 }
78
79 void ServerWillReply(int64 modulus, bool with_hashes, bool with_serial_hash) {
80 em::DeviceAutoEnrollmentResponse response;
81 if (modulus >= 0)
82 response.set_modulus(modulus);
83 if (with_hashes) {
84 for (size_t i = 0; i < 10; ++i) {
85 std::string serial = "serial X";
86 serial[7] = '0' + i;
87 std::string hash = crypto::SHA256HashString(serial);
88 response.mutable_hashes()->Add()->assign(hash);
89 }
90 }
91 if (with_serial_hash) {
92 response.mutable_hashes()->Add()->assign(kSerialHash,
93 crypto::kSHA256Length);
94 }
95 EXPECT_CALL(backend_, ProcessAutoEnrollmentRequest(_, _, _))
96 .WillOnce(
97 DoAll(
98 Invoke(this,
99 &AutoEnrollmentClientTest::CaptureRequest),
Mattias Nissler (ping if slow) 2011/12/12 10:20:31 ditto.
Joao da Silva 2011/12/12 11:54:13 Done.
100 MockDeviceManagementBackendSucceedAutoEnrollment(response)));
101 }
102
103 MockDeviceManagementBackend backend_;
104 scoped_ptr<AutoEnrollmentClient> client_;
105 em::DeviceAutoEnrollmentRequest last_request_;
106 int completion_callback_count_;
107
108 private:
109 void CaptureRequest(
110 Unused,
111 const em::DeviceAutoEnrollmentRequest& request,
112 Unused) {
113 last_request_ = request;
114 }
115
116 DISALLOW_COPY_AND_ASSIGN(AutoEnrollmentClientTest);
117 };
118
119 TEST_F(AutoEnrollmentClientTest, NetworkFailure) {
120 ServerWillFail(DeviceManagementBackend::kErrorTemporaryUnavailable);
121 client_->Start();
122 EXPECT_FALSE(client_->should_auto_enroll());
123 EXPECT_EQ(1, completion_callback_count_);
124 }
125
126 TEST_F(AutoEnrollmentClientTest, EmptyReply) {
127 ServerWillReply(-1, false, false);
128 client_->Start();
129 EXPECT_FALSE(client_->should_auto_enroll());
130 EXPECT_EQ(1, completion_callback_count_);
131 }
132
133 TEST_F(AutoEnrollmentClientTest, ClientUploadsRightBits) {
134 ServerWillReply(-1, false, false);
135 client_->Start();
136 EXPECT_FALSE(client_->should_auto_enroll());
137 EXPECT_EQ(1, completion_callback_count_);
138 EXPECT_TRUE(last_request_.has_remainder());
139 EXPECT_TRUE(last_request_.has_modulus());
140 EXPECT_EQ(16, last_request_.modulus());
141 EXPECT_EQ(kSerialHash[31] & 0xf, last_request_.remainder());
142 }
143
144 TEST_F(AutoEnrollmentClientTest, AskForMoreThenFail) {
145 InSequence sequence;
146 ServerWillReply(32, false, false);
147 ServerWillFail(DeviceManagementBackend::kErrorTemporaryUnavailable);
148 client_->Start();
149 EXPECT_FALSE(client_->should_auto_enroll());
150 EXPECT_EQ(1, completion_callback_count_);
151 }
152
153 TEST_F(AutoEnrollmentClientTest, AskForMoreThenEvenMore) {
154 InSequence sequence;
155 ServerWillReply(32, false, false);
156 ServerWillReply(64, false, false);
157 client_->Start();
158 EXPECT_FALSE(client_->should_auto_enroll());
159 EXPECT_EQ(1, completion_callback_count_);
160 }
161
162 TEST_F(AutoEnrollmentClientTest, AskForLess) {
163 ServerWillReply(8, false, false);
164 client_->Start();
165 EXPECT_FALSE(client_->should_auto_enroll());
166 EXPECT_EQ(1, completion_callback_count_);
167 }
168
169 TEST_F(AutoEnrollmentClientTest, AskForSame) {
170 ServerWillReply(16, false, false);
171 client_->Start();
172 EXPECT_FALSE(client_->should_auto_enroll());
173 EXPECT_EQ(1, completion_callback_count_);
174 }
175
176 TEST_F(AutoEnrollmentClientTest, AskForTooMuch) {
177 ServerWillReply(512, false, false);
178 client_->Start();
179 EXPECT_FALSE(client_->should_auto_enroll());
180 EXPECT_EQ(1, completion_callback_count_);
181 }
182
183 TEST_F(AutoEnrollmentClientTest, AskNonPowerOf2) {
184 InSequence sequence;
185 ServerWillReply(100, false, false);
186 ServerWillReply(-1, false, false);
187 client_->Start();
188 EXPECT_FALSE(client_->should_auto_enroll());
189 EXPECT_EQ(1, completion_callback_count_);
190 EXPECT_TRUE(last_request_.has_remainder());
191 EXPECT_TRUE(last_request_.has_modulus());
192 EXPECT_EQ(128, last_request_.modulus());
193 EXPECT_EQ(kSerialHash[31] & 0x7f, last_request_.remainder());
194 }
195
196 TEST_F(AutoEnrollmentClientTest, ConsumerDevice) {
197 ServerWillReply(-1, true, false);
198 client_->Start();
199 EXPECT_FALSE(client_->should_auto_enroll());
200 EXPECT_EQ(1, completion_callback_count_);
201 }
202
203 TEST_F(AutoEnrollmentClientTest, EnterpriseDevice) {
204 ServerWillReply(-1, true, true);
205 client_->Start();
206 EXPECT_TRUE(client_->should_auto_enroll());
207 EXPECT_EQ(1, completion_callback_count_);
208 }
209
210 TEST_F(AutoEnrollmentClientTest, NoSerial) {
211 CreateClient("", 4, 8);
212 client_->Start();
213 EXPECT_FALSE(client_->should_auto_enroll());
214 EXPECT_EQ(1, completion_callback_count_);
215 }
216
217 TEST_F(AutoEnrollmentClientTest, NoBitsUploaded) {
218 CreateClient(kSerial, 0, 0);
219 ServerWillReply(-1, false, false);
220 client_->Start();
221 EXPECT_FALSE(client_->should_auto_enroll());
222 EXPECT_EQ(1, completion_callback_count_);
223 EXPECT_TRUE(last_request_.has_remainder());
224 EXPECT_TRUE(last_request_.has_modulus());
225 EXPECT_EQ(1, last_request_.modulus());
226 EXPECT_EQ(0, last_request_.remainder());
227 }
228
229 TEST_F(AutoEnrollmentClientTest, ManyBitsUploaded) {
230 int64 bottom62 = GG_LONGLONG(0x14e437a1552b138a);
231 for (int i = 0; i <= 62; ++i) {
232 completion_callback_count_ = 0;
233 CreateClient(kSerial, i, i);
234 ServerWillReply(-1, false, false);
235 client_->Start();
236 EXPECT_FALSE(client_->should_auto_enroll());
237 EXPECT_EQ(1, completion_callback_count_);
238 EXPECT_TRUE(last_request_.has_remainder());
239 EXPECT_TRUE(last_request_.has_modulus());
240 EXPECT_EQ((int64) 1 << i, last_request_.modulus());
241 EXPECT_EQ(bottom62 % ((int64) 1 << i), last_request_.remainder());
242 }
243 }
244
245 } // namespace
246
247 } // namespace policy
OLDNEW
« no previous file with comments | « chrome/browser/policy/auto_enrollment_client.cc ('k') | chrome/browser/policy/browser_policy_connector.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698