Chromium Code Reviews| 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/rand_util.h" | |
| 8 #include "chrome/browser/policy/mock_device_management_backend.h" | |
| 9 #include "chrome/browser/policy/mock_device_management_service.h" | |
| 10 #include "crypto/sha2.h" | |
| 11 #include "testing/gmock/include/gmock/gmock.h" | |
| 12 #include "testing/gtest/include/gtest/gtest.h" | |
| 13 | |
| 14 namespace policy { | |
| 15 | |
| 16 namespace { | |
| 17 | |
| 18 const char* kSerial = "serial"; | |
| 19 const char* kSerialHash = | |
| 20 "\x01\x44\xb1\xde\xfc\xf7\x56\x10\x87\x01\x5f\x8d\x83\x0d\x65\xb1" | |
| 21 "\x6f\x02\x4a\xd7\xeb\x92\x45\xfc\xd4\xe4\x37\xa1\x55\x2b\x13\x8a"; | |
| 22 | |
| 23 using ::testing::AnyNumber; | |
| 24 using ::testing::DoAll; | |
| 25 using ::testing::Invoke; | |
| 26 using ::testing::Unused; | |
| 27 using ::testing::_; | |
| 28 | |
| 29 ACTION_P(MockDeviceManagementBackendFailAutoEnrollment, error) { | |
| 30 arg2->OnError(error); | |
| 31 } | |
| 32 | |
| 33 ACTION_P(MockDeviceManagementBackendSucceedAutoEnrollment, response) { | |
| 34 arg2->HandleAutoEnrollmentResponse(response); | |
| 35 } | |
| 36 | |
| 37 class MockAutoEnrollmentClientDelegate : public AutoEnrollmentClient::Delegate { | |
| 38 public: | |
| 39 MockAutoEnrollmentClientDelegate() {} | |
| 40 virtual ~MockAutoEnrollmentClientDelegate() {} | |
| 41 | |
| 42 MOCK_METHOD1(OnAutoEnrollmentComplete, void(AutoEnrollmentClient*)); | |
| 43 | |
| 44 private: | |
| 45 DISALLOW_COPY_AND_ASSIGN(MockAutoEnrollmentClientDelegate); | |
| 46 }; | |
| 47 | |
| 48 class AutoEnrollmentClientTest : public testing::Test { | |
|
Mattias Nissler (ping if slow)
2011/12/09 15:40:52
I think the test class itself usually doesn't go i
Joao da Silva
2011/12/09 17:26:06
I usually do this (see url_blacklist_manager_unitt
| |
| 49 public: | |
|
Mattias Nissler (ping if slow)
2011/12/09 15:40:52
can be protected.
Joao da Silva
2011/12/09 17:26:06
Done.
| |
| 50 AutoEnrollmentClientTest() { | |
| 51 } | |
| 52 | |
| 53 virtual void SetUp() OVERRIDE { | |
| 54 CreateClient(kSerial, 4, 8); | |
| 55 } | |
| 56 | |
| 57 void CreateClient(const std::string& serial, | |
| 58 int power_initial, | |
| 59 int power_limit) { | |
| 60 MockDeviceManagementService* service = new MockDeviceManagementService; | |
| 61 EXPECT_CALL(*service, CreateBackend()) | |
| 62 .Times(AnyNumber()) | |
|
Mattias Nissler (ping if slow)
2011/12/09 15:40:52
indentation.
Joao da Silva
2011/12/09 17:26:06
Done.
| |
| 63 .WillRepeatedly(MockDeviceManagementServiceProxyBackend(&backend_)); | |
| 64 client_.reset(new AutoEnrollmentClient(&delegate_, | |
| 65 service, | |
| 66 serial, | |
| 67 power_initial, | |
| 68 power_limit)); | |
| 69 } | |
| 70 | |
| 71 void ExpectNotification() { | |
| 72 EXPECT_CALL(delegate_, OnAutoEnrollmentComplete(client_.get())).Times(1); | |
| 73 } | |
| 74 | |
| 75 void ServerWillFail(DeviceManagementBackend::ErrorCode error) { | |
| 76 EXPECT_CALL(backend_, ProcessAutoEnrollmentRequest(_, _, _)) | |
| 77 .WillOnce( | |
| 78 DoAll( | |
| 79 Invoke(this, | |
| 80 &AutoEnrollmentClientTest::OnProcessAutoEnrollmentRequest), | |
| 81 MockDeviceManagementBackendFailAutoEnrollment(error))) | |
| 82 .RetiresOnSaturation(); | |
| 83 } | |
| 84 | |
| 85 void ServerWillReply(int64 modulus, bool with_hashes, bool with_serial_hash) { | |
| 86 em::DeviceAutoEnrollmentResponse response; | |
| 87 if (modulus >= 0) | |
| 88 response.set_modulus(modulus); | |
| 89 if (with_hashes) { | |
| 90 for (size_t i = 0; i < 10; ++i) { | |
| 91 response.mutable_hashes()->Add()->assign( | |
| 92 base::RandBytesAsString(crypto::kSHA256Length)); | |
|
Mattias Nissler (ping if slow)
2011/12/09 15:40:52
Might this cause indeterminism? Are we sure the ra
Joao da Silva
2011/12/09 17:26:06
Done! :-)
| |
| 93 } | |
| 94 } | |
| 95 if (with_serial_hash) { | |
| 96 response.mutable_hashes()->Add()->assign(kSerialHash, | |
| 97 crypto::kSHA256Length); | |
| 98 } | |
| 99 EXPECT_CALL(backend_, ProcessAutoEnrollmentRequest(_, _, _)) | |
| 100 .WillOnce( | |
| 101 DoAll( | |
| 102 Invoke(this, | |
| 103 &AutoEnrollmentClientTest::OnProcessAutoEnrollmentRequest), | |
| 104 MockDeviceManagementBackendSucceedAutoEnrollment(response))) | |
| 105 .RetiresOnSaturation(); | |
| 106 } | |
| 107 | |
| 108 protected: | |
| 109 MockDeviceManagementBackend backend_; | |
| 110 MockAutoEnrollmentClientDelegate delegate_; | |
| 111 scoped_ptr<AutoEnrollmentClient> client_; | |
| 112 em::DeviceAutoEnrollmentRequest last_request_; | |
| 113 | |
| 114 private: | |
| 115 void OnProcessAutoEnrollmentRequest( | |
|
Mattias Nissler (ping if slow)
2011/12/09 15:40:52
Maybe call this CaptureRequest()?
Joao da Silva
2011/12/09 17:26:06
Done.
| |
| 116 Unused, | |
| 117 const em::DeviceAutoEnrollmentRequest& request, | |
| 118 Unused) { | |
| 119 last_request_ = request; | |
| 120 } | |
| 121 | |
| 122 DISALLOW_COPY_AND_ASSIGN(AutoEnrollmentClientTest); | |
| 123 }; | |
| 124 | |
| 125 TEST_F(AutoEnrollmentClientTest, NetworkFailure) { | |
| 126 ServerWillFail(DeviceManagementBackend::kErrorTemporaryUnavailable); | |
| 127 ExpectNotification(); | |
| 128 client_->Start(); | |
| 129 EXPECT_FALSE(client_->should_auto_enroll()); | |
| 130 } | |
| 131 | |
| 132 TEST_F(AutoEnrollmentClientTest, EmptyReply) { | |
| 133 ServerWillReply(-1, false, false); | |
| 134 ExpectNotification(); | |
| 135 client_->Start(); | |
| 136 EXPECT_FALSE(client_->should_auto_enroll()); | |
| 137 } | |
| 138 | |
| 139 TEST_F(AutoEnrollmentClientTest, ClientUploadsRightBits) { | |
| 140 ServerWillReply(-1, false, false); | |
| 141 ExpectNotification(); | |
| 142 client_->Start(); | |
| 143 EXPECT_FALSE(client_->should_auto_enroll()); | |
| 144 EXPECT_TRUE(last_request_.has_remainder()); | |
| 145 EXPECT_TRUE(last_request_.has_modulus()); | |
| 146 EXPECT_EQ(16, last_request_.modulus()); | |
| 147 EXPECT_EQ(kSerialHash[31] & 0xf, last_request_.remainder()); | |
| 148 } | |
| 149 | |
| 150 TEST_F(AutoEnrollmentClientTest, AskForMoreThenFail) { | |
|
Mattias Nissler (ping if slow)
2011/12/09 15:40:52
Could use a comment that the expectations need to
Joao da Silva
2011/12/09 17:26:06
Indeed, InSequence is cooler. Also removed the "Re
| |
| 151 ServerWillFail(DeviceManagementBackend::kErrorTemporaryUnavailable); | |
| 152 ServerWillReply(32, false, false); | |
| 153 ExpectNotification(); | |
| 154 client_->Start(); | |
| 155 EXPECT_FALSE(client_->should_auto_enroll()); | |
| 156 } | |
| 157 | |
| 158 TEST_F(AutoEnrollmentClientTest, AskForMoreThenEvenMore) { | |
| 159 ServerWillReply(64, false, false); | |
| 160 ServerWillReply(32, false, false); | |
| 161 ExpectNotification(); | |
| 162 client_->Start(); | |
| 163 EXPECT_FALSE(client_->should_auto_enroll()); | |
| 164 } | |
| 165 | |
| 166 TEST_F(AutoEnrollmentClientTest, AskForLess) { | |
| 167 ServerWillReply(8, false, false); | |
| 168 ExpectNotification(); | |
| 169 client_->Start(); | |
| 170 EXPECT_FALSE(client_->should_auto_enroll()); | |
| 171 } | |
| 172 | |
| 173 TEST_F(AutoEnrollmentClientTest, AskForSame) { | |
| 174 ServerWillReply(16, false, false); | |
| 175 ExpectNotification(); | |
| 176 client_->Start(); | |
| 177 EXPECT_FALSE(client_->should_auto_enroll()); | |
| 178 } | |
| 179 | |
| 180 TEST_F(AutoEnrollmentClientTest, AskForTooMuch) { | |
| 181 ServerWillReply(512, false, false); | |
| 182 ExpectNotification(); | |
| 183 client_->Start(); | |
| 184 EXPECT_FALSE(client_->should_auto_enroll()); | |
| 185 } | |
| 186 | |
| 187 TEST_F(AutoEnrollmentClientTest, AskNonPowerOf2) { | |
| 188 ServerWillReply(-1, false, false); | |
| 189 ServerWillReply(100, false, false); | |
| 190 ExpectNotification(); | |
| 191 client_->Start(); | |
| 192 EXPECT_FALSE(client_->should_auto_enroll()); | |
| 193 EXPECT_TRUE(last_request_.has_remainder()); | |
| 194 EXPECT_TRUE(last_request_.has_modulus()); | |
| 195 EXPECT_EQ(128, last_request_.modulus()); | |
| 196 EXPECT_EQ(kSerialHash[31] & 0x7f, last_request_.remainder()); | |
| 197 } | |
| 198 | |
| 199 TEST_F(AutoEnrollmentClientTest, ConsumerDevice) { | |
| 200 ServerWillReply(-1, true, false); | |
| 201 ExpectNotification(); | |
| 202 client_->Start(); | |
| 203 EXPECT_FALSE(client_->should_auto_enroll()); | |
| 204 } | |
| 205 | |
| 206 TEST_F(AutoEnrollmentClientTest, EnterpriseDevice) { | |
| 207 ServerWillReply(-1, true, true); | |
| 208 ExpectNotification(); | |
| 209 client_->Start(); | |
| 210 EXPECT_TRUE(client_->should_auto_enroll()); | |
| 211 } | |
| 212 | |
| 213 TEST_F(AutoEnrollmentClientTest, NoSerial) { | |
| 214 CreateClient("", 4, 8); | |
| 215 ExpectNotification(); | |
| 216 client_->Start(); | |
| 217 EXPECT_FALSE(client_->should_auto_enroll()); | |
| 218 } | |
| 219 | |
| 220 TEST_F(AutoEnrollmentClientTest, NoBitsUploaded) { | |
| 221 CreateClient(kSerial, 0, 0); | |
| 222 ServerWillReply(-1, false, false); | |
| 223 ExpectNotification(); | |
| 224 client_->Start(); | |
| 225 EXPECT_FALSE(client_->should_auto_enroll()); | |
| 226 EXPECT_TRUE(last_request_.has_remainder()); | |
| 227 EXPECT_TRUE(last_request_.has_modulus()); | |
| 228 EXPECT_EQ(1, last_request_.modulus()); | |
| 229 EXPECT_EQ(0, last_request_.remainder()); | |
| 230 } | |
| 231 | |
| 232 TEST_F(AutoEnrollmentClientTest, ManyBitsUploaded) { | |
| 233 int64 bottom62 = GG_LONGLONG(0x14e437a1552b138a); | |
| 234 for (int i = 0; i <= 62; ++i) { | |
| 235 CreateClient(kSerial, i, i); | |
| 236 ServerWillReply(-1, false, false); | |
| 237 ExpectNotification(); | |
| 238 client_->Start(); | |
| 239 EXPECT_FALSE(client_->should_auto_enroll()); | |
| 240 EXPECT_TRUE(last_request_.has_remainder()); | |
| 241 EXPECT_TRUE(last_request_.has_modulus()); | |
| 242 EXPECT_EQ((int64) 1 << i, last_request_.modulus()); | |
| 243 EXPECT_EQ(bottom62 % ((int64) 1 << i), last_request_.remainder()); | |
| 244 } | |
| 245 } | |
| 246 | |
| 247 } // namespace | |
| 248 | |
| 249 } // namespace policy | |
| OLD | NEW |