OLD | NEW |
(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(Invoke(this, &AutoEnrollmentClientTest::CaptureRequest), |
| 74 MockDeviceManagementBackendFailAutoEnrollment(error))); |
| 75 } |
| 76 |
| 77 void ServerWillReply(int64 modulus, bool with_hashes, bool with_serial_hash) { |
| 78 em::DeviceAutoEnrollmentResponse response; |
| 79 if (modulus >= 0) |
| 80 response.set_modulus(modulus); |
| 81 if (with_hashes) { |
| 82 for (size_t i = 0; i < 10; ++i) { |
| 83 std::string serial = "serial X"; |
| 84 serial[7] = '0' + i; |
| 85 std::string hash = crypto::SHA256HashString(serial); |
| 86 response.mutable_hashes()->Add()->assign(hash); |
| 87 } |
| 88 } |
| 89 if (with_serial_hash) { |
| 90 response.mutable_hashes()->Add()->assign(kSerialHash, |
| 91 crypto::kSHA256Length); |
| 92 } |
| 93 EXPECT_CALL(backend_, ProcessAutoEnrollmentRequest(_, _, _)) |
| 94 .WillOnce( |
| 95 DoAll(Invoke(this, &AutoEnrollmentClientTest::CaptureRequest), |
| 96 MockDeviceManagementBackendSucceedAutoEnrollment(response))); |
| 97 } |
| 98 |
| 99 MockDeviceManagementBackend backend_; |
| 100 scoped_ptr<AutoEnrollmentClient> client_; |
| 101 em::DeviceAutoEnrollmentRequest last_request_; |
| 102 int completion_callback_count_; |
| 103 |
| 104 private: |
| 105 void CaptureRequest( |
| 106 Unused, |
| 107 const em::DeviceAutoEnrollmentRequest& request, |
| 108 Unused) { |
| 109 last_request_ = request; |
| 110 } |
| 111 |
| 112 DISALLOW_COPY_AND_ASSIGN(AutoEnrollmentClientTest); |
| 113 }; |
| 114 |
| 115 TEST_F(AutoEnrollmentClientTest, NetworkFailure) { |
| 116 ServerWillFail(DeviceManagementBackend::kErrorTemporaryUnavailable); |
| 117 client_->Start(); |
| 118 EXPECT_FALSE(client_->should_auto_enroll()); |
| 119 EXPECT_EQ(1, completion_callback_count_); |
| 120 } |
| 121 |
| 122 TEST_F(AutoEnrollmentClientTest, EmptyReply) { |
| 123 ServerWillReply(-1, false, false); |
| 124 client_->Start(); |
| 125 EXPECT_FALSE(client_->should_auto_enroll()); |
| 126 EXPECT_EQ(1, completion_callback_count_); |
| 127 } |
| 128 |
| 129 TEST_F(AutoEnrollmentClientTest, ClientUploadsRightBits) { |
| 130 ServerWillReply(-1, false, false); |
| 131 client_->Start(); |
| 132 EXPECT_FALSE(client_->should_auto_enroll()); |
| 133 EXPECT_EQ(1, completion_callback_count_); |
| 134 EXPECT_TRUE(last_request_.has_remainder()); |
| 135 EXPECT_TRUE(last_request_.has_modulus()); |
| 136 EXPECT_EQ(16, last_request_.modulus()); |
| 137 EXPECT_EQ(kSerialHash[31] & 0xf, last_request_.remainder()); |
| 138 } |
| 139 |
| 140 TEST_F(AutoEnrollmentClientTest, AskForMoreThenFail) { |
| 141 InSequence sequence; |
| 142 ServerWillReply(32, false, false); |
| 143 ServerWillFail(DeviceManagementBackend::kErrorTemporaryUnavailable); |
| 144 client_->Start(); |
| 145 EXPECT_FALSE(client_->should_auto_enroll()); |
| 146 EXPECT_EQ(1, completion_callback_count_); |
| 147 } |
| 148 |
| 149 TEST_F(AutoEnrollmentClientTest, AskForMoreThenEvenMore) { |
| 150 InSequence sequence; |
| 151 ServerWillReply(32, false, false); |
| 152 ServerWillReply(64, false, false); |
| 153 client_->Start(); |
| 154 EXPECT_FALSE(client_->should_auto_enroll()); |
| 155 EXPECT_EQ(1, completion_callback_count_); |
| 156 } |
| 157 |
| 158 TEST_F(AutoEnrollmentClientTest, AskForLess) { |
| 159 ServerWillReply(8, false, false); |
| 160 client_->Start(); |
| 161 EXPECT_FALSE(client_->should_auto_enroll()); |
| 162 EXPECT_EQ(1, completion_callback_count_); |
| 163 } |
| 164 |
| 165 TEST_F(AutoEnrollmentClientTest, AskForSame) { |
| 166 ServerWillReply(16, false, false); |
| 167 client_->Start(); |
| 168 EXPECT_FALSE(client_->should_auto_enroll()); |
| 169 EXPECT_EQ(1, completion_callback_count_); |
| 170 } |
| 171 |
| 172 TEST_F(AutoEnrollmentClientTest, AskForTooMuch) { |
| 173 ServerWillReply(512, false, false); |
| 174 client_->Start(); |
| 175 EXPECT_FALSE(client_->should_auto_enroll()); |
| 176 EXPECT_EQ(1, completion_callback_count_); |
| 177 } |
| 178 |
| 179 TEST_F(AutoEnrollmentClientTest, AskNonPowerOf2) { |
| 180 InSequence sequence; |
| 181 ServerWillReply(100, false, false); |
| 182 ServerWillReply(-1, false, false); |
| 183 client_->Start(); |
| 184 EXPECT_FALSE(client_->should_auto_enroll()); |
| 185 EXPECT_EQ(1, completion_callback_count_); |
| 186 EXPECT_TRUE(last_request_.has_remainder()); |
| 187 EXPECT_TRUE(last_request_.has_modulus()); |
| 188 EXPECT_EQ(128, last_request_.modulus()); |
| 189 EXPECT_EQ(kSerialHash[31] & 0x7f, last_request_.remainder()); |
| 190 } |
| 191 |
| 192 TEST_F(AutoEnrollmentClientTest, ConsumerDevice) { |
| 193 ServerWillReply(-1, true, false); |
| 194 client_->Start(); |
| 195 EXPECT_FALSE(client_->should_auto_enroll()); |
| 196 EXPECT_EQ(1, completion_callback_count_); |
| 197 } |
| 198 |
| 199 TEST_F(AutoEnrollmentClientTest, EnterpriseDevice) { |
| 200 ServerWillReply(-1, true, true); |
| 201 client_->Start(); |
| 202 EXPECT_TRUE(client_->should_auto_enroll()); |
| 203 EXPECT_EQ(1, completion_callback_count_); |
| 204 } |
| 205 |
| 206 TEST_F(AutoEnrollmentClientTest, NoSerial) { |
| 207 CreateClient("", 4, 8); |
| 208 client_->Start(); |
| 209 EXPECT_FALSE(client_->should_auto_enroll()); |
| 210 EXPECT_EQ(1, completion_callback_count_); |
| 211 } |
| 212 |
| 213 TEST_F(AutoEnrollmentClientTest, NoBitsUploaded) { |
| 214 CreateClient(kSerial, 0, 0); |
| 215 ServerWillReply(-1, false, false); |
| 216 client_->Start(); |
| 217 EXPECT_FALSE(client_->should_auto_enroll()); |
| 218 EXPECT_EQ(1, completion_callback_count_); |
| 219 EXPECT_TRUE(last_request_.has_remainder()); |
| 220 EXPECT_TRUE(last_request_.has_modulus()); |
| 221 EXPECT_EQ(1, last_request_.modulus()); |
| 222 EXPECT_EQ(0, last_request_.remainder()); |
| 223 } |
| 224 |
| 225 TEST_F(AutoEnrollmentClientTest, ManyBitsUploaded) { |
| 226 int64 bottom62 = GG_LONGLONG(0x14e437a1552b138a); |
| 227 for (int i = 0; i <= 62; ++i) { |
| 228 completion_callback_count_ = 0; |
| 229 CreateClient(kSerial, i, i); |
| 230 ServerWillReply(-1, false, false); |
| 231 client_->Start(); |
| 232 EXPECT_FALSE(client_->should_auto_enroll()); |
| 233 EXPECT_EQ(1, completion_callback_count_); |
| 234 EXPECT_TRUE(last_request_.has_remainder()); |
| 235 EXPECT_TRUE(last_request_.has_modulus()); |
| 236 EXPECT_EQ((int64) 1 << i, last_request_.modulus()); |
| 237 EXPECT_EQ(bottom62 % ((int64) 1 << i), last_request_.remainder()); |
| 238 } |
| 239 } |
| 240 |
| 241 } // namespace |
| 242 |
| 243 } // namespace policy |
OLD | NEW |