| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2012 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/values.h" | |
| 10 #include "chrome/browser/browser_process.h" | |
| 11 #include "chrome/browser/policy/mock_device_management_service.h" | |
| 12 #include "chrome/browser/prefs/pref_service.h" | |
| 13 #include "chrome/common/pref_names.h" | |
| 14 #include "chrome/test/base/testing_browser_process.h" | |
| 15 #include "chrome/test/base/testing_pref_service.h" | |
| 16 #include "crypto/sha2.h" | |
| 17 #include "testing/gmock/include/gmock/gmock.h" | |
| 18 #include "testing/gtest/include/gtest/gtest.h" | |
| 19 | |
| 20 namespace em = enterprise_management; | |
| 21 | |
| 22 namespace policy { | |
| 23 | |
| 24 namespace { | |
| 25 | |
| 26 const char* kSerial = "serial"; | |
| 27 const char* kSerialHash = | |
| 28 "\x01\x44\xb1\xde\xfc\xf7\x56\x10\x87\x01\x5f\x8d\x83\x0d\x65\xb1" | |
| 29 "\x6f\x02\x4a\xd7\xeb\x92\x45\xfc\xd4\xe4\x37\xa1\x55\x2b\x13\x8a"; | |
| 30 | |
| 31 using ::testing::InSequence; | |
| 32 using ::testing::SaveArg; | |
| 33 using ::testing::_; | |
| 34 | |
| 35 class AutoEnrollmentClientTest : public testing::Test { | |
| 36 protected: | |
| 37 AutoEnrollmentClientTest() | |
| 38 : scoped_testing_local_state_( | |
| 39 TestingBrowserProcess::GetGlobal()), | |
| 40 local_state_(scoped_testing_local_state_.Get()), | |
| 41 service_(NULL), | |
| 42 completion_callback_count_(0) {} | |
| 43 | |
| 44 virtual void SetUp() OVERRIDE { | |
| 45 CreateClient(kSerial, 4, 8); | |
| 46 ASSERT_FALSE(local_state_->GetUserPref(prefs::kShouldAutoEnroll)); | |
| 47 ASSERT_FALSE(local_state_->GetUserPref(prefs::kAutoEnrollmentPowerLimit)); | |
| 48 } | |
| 49 | |
| 50 void CreateClient(const std::string& serial, | |
| 51 int power_initial, | |
| 52 int power_limit) { | |
| 53 service_ = new MockDeviceManagementService(); | |
| 54 EXPECT_CALL(*service_, StartJob(_, _, _, _, _, _, _)) | |
| 55 .WillRepeatedly(SaveArg<6>(&last_request_)); | |
| 56 base::Closure callback = | |
| 57 base::Bind(&AutoEnrollmentClientTest::CompletionCallback, | |
| 58 base::Unretained(this)); | |
| 59 client_.reset(new AutoEnrollmentClient(callback, | |
| 60 service_, | |
| 61 local_state_, | |
| 62 serial, | |
| 63 power_initial, | |
| 64 power_limit)); | |
| 65 } | |
| 66 | |
| 67 void CompletionCallback() { | |
| 68 completion_callback_count_++; | |
| 69 } | |
| 70 | |
| 71 void ServerWillFail(DeviceManagementStatus error) { | |
| 72 em::DeviceManagementResponse dummy_response; | |
| 73 EXPECT_CALL(*service_, | |
| 74 CreateJob(DeviceManagementRequestJob::TYPE_AUTO_ENROLLMENT)) | |
| 75 .WillOnce(service_->FailJob(error)); | |
| 76 } | |
| 77 | |
| 78 void ServerWillReply(int64 modulus, bool with_hashes, bool with_serial_hash) { | |
| 79 em::DeviceManagementResponse response; | |
| 80 em::DeviceAutoEnrollmentResponse* enrollment_response = | |
| 81 response.mutable_auto_enrollment_response(); | |
| 82 if (modulus >= 0) | |
| 83 enrollment_response->set_expected_modulus(modulus); | |
| 84 if (with_hashes) { | |
| 85 for (size_t i = 0; i < 10; ++i) { | |
| 86 std::string serial = "serial X"; | |
| 87 serial[7] = '0' + i; | |
| 88 std::string hash = crypto::SHA256HashString(serial); | |
| 89 enrollment_response->mutable_hash()->Add()->assign(hash); | |
| 90 } | |
| 91 } | |
| 92 if (with_serial_hash) { | |
| 93 enrollment_response->mutable_hash()->Add()->assign(kSerialHash, | |
| 94 crypto::kSHA256Length); | |
| 95 } | |
| 96 EXPECT_CALL(*service_, | |
| 97 CreateJob(DeviceManagementRequestJob::TYPE_AUTO_ENROLLMENT)) | |
| 98 .WillOnce(service_->SucceedJob(response)); | |
| 99 } | |
| 100 | |
| 101 void VerifyCachedResult(bool should_enroll, int power_limit) { | |
| 102 base::FundamentalValue value_should_enroll(should_enroll); | |
| 103 base::FundamentalValue value_power_limit(power_limit); | |
| 104 EXPECT_TRUE(Value::Equals( | |
| 105 &value_should_enroll, | |
| 106 local_state_->GetUserPref(prefs::kShouldAutoEnroll))); | |
| 107 EXPECT_TRUE(Value::Equals( | |
| 108 &value_power_limit, | |
| 109 local_state_->GetUserPref(prefs::kAutoEnrollmentPowerLimit))); | |
| 110 } | |
| 111 | |
| 112 const em::DeviceAutoEnrollmentRequest& auto_enrollment_request() { | |
| 113 return last_request_.auto_enrollment_request(); | |
| 114 } | |
| 115 | |
| 116 ScopedTestingLocalState scoped_testing_local_state_; | |
| 117 TestingPrefServiceSimple* local_state_; | |
| 118 MockDeviceManagementService* service_; | |
| 119 scoped_ptr<AutoEnrollmentClient> client_; | |
| 120 em::DeviceManagementRequest last_request_; | |
| 121 int completion_callback_count_; | |
| 122 | |
| 123 private: | |
| 124 DISALLOW_COPY_AND_ASSIGN(AutoEnrollmentClientTest); | |
| 125 }; | |
| 126 | |
| 127 TEST_F(AutoEnrollmentClientTest, NetworkFailure) { | |
| 128 ServerWillFail(DM_STATUS_TEMPORARY_UNAVAILABLE); | |
| 129 client_->Start(); | |
| 130 EXPECT_FALSE(client_->should_auto_enroll()); | |
| 131 EXPECT_EQ(1, completion_callback_count_); | |
| 132 VerifyCachedResult(false, 8); | |
| 133 } | |
| 134 | |
| 135 TEST_F(AutoEnrollmentClientTest, EmptyReply) { | |
| 136 ServerWillReply(-1, false, false); | |
| 137 client_->Start(); | |
| 138 EXPECT_FALSE(client_->should_auto_enroll()); | |
| 139 EXPECT_EQ(1, completion_callback_count_); | |
| 140 VerifyCachedResult(false, 8); | |
| 141 } | |
| 142 | |
| 143 TEST_F(AutoEnrollmentClientTest, ClientUploadsRightBits) { | |
| 144 ServerWillReply(-1, false, false); | |
| 145 client_->Start(); | |
| 146 EXPECT_FALSE(client_->should_auto_enroll()); | |
| 147 EXPECT_EQ(1, completion_callback_count_); | |
| 148 | |
| 149 EXPECT_TRUE(auto_enrollment_request().has_remainder()); | |
| 150 EXPECT_TRUE(auto_enrollment_request().has_modulus()); | |
| 151 EXPECT_EQ(16, auto_enrollment_request().modulus()); | |
| 152 EXPECT_EQ(kSerialHash[31] & 0xf, auto_enrollment_request().remainder()); | |
| 153 VerifyCachedResult(false, 8); | |
| 154 } | |
| 155 | |
| 156 TEST_F(AutoEnrollmentClientTest, AskForMoreThenFail) { | |
| 157 InSequence sequence; | |
| 158 ServerWillReply(32, false, false); | |
| 159 ServerWillFail(DM_STATUS_TEMPORARY_UNAVAILABLE); | |
| 160 client_->Start(); | |
| 161 EXPECT_FALSE(client_->should_auto_enroll()); | |
| 162 EXPECT_EQ(1, completion_callback_count_); | |
| 163 VerifyCachedResult(false, 8); | |
| 164 } | |
| 165 | |
| 166 TEST_F(AutoEnrollmentClientTest, AskForMoreThenEvenMore) { | |
| 167 InSequence sequence; | |
| 168 ServerWillReply(32, false, false); | |
| 169 ServerWillReply(64, false, false); | |
| 170 client_->Start(); | |
| 171 EXPECT_FALSE(client_->should_auto_enroll()); | |
| 172 EXPECT_EQ(1, completion_callback_count_); | |
| 173 VerifyCachedResult(false, 8); | |
| 174 } | |
| 175 | |
| 176 TEST_F(AutoEnrollmentClientTest, AskForLess) { | |
| 177 InSequence sequence; | |
| 178 ServerWillReply(8, false, false); | |
| 179 ServerWillReply(-1, true, true); | |
| 180 client_->Start(); | |
| 181 EXPECT_TRUE(client_->should_auto_enroll()); | |
| 182 EXPECT_EQ(1, completion_callback_count_); | |
| 183 VerifyCachedResult(true, 8); | |
| 184 } | |
| 185 | |
| 186 TEST_F(AutoEnrollmentClientTest, AskForSame) { | |
| 187 InSequence sequence; | |
| 188 ServerWillReply(16, false, false); | |
| 189 ServerWillReply(-1, true, true); | |
| 190 client_->Start(); | |
| 191 EXPECT_TRUE(client_->should_auto_enroll()); | |
| 192 EXPECT_EQ(1, completion_callback_count_); | |
| 193 VerifyCachedResult(true, 8); | |
| 194 } | |
| 195 | |
| 196 TEST_F(AutoEnrollmentClientTest, AskForSameTwice) { | |
| 197 InSequence sequence; | |
| 198 ServerWillReply(16, false, false); | |
| 199 ServerWillReply(16, false, false); | |
| 200 client_->Start(); | |
| 201 EXPECT_FALSE(client_->should_auto_enroll()); | |
| 202 EXPECT_EQ(1, completion_callback_count_); | |
| 203 VerifyCachedResult(false, 8); | |
| 204 } | |
| 205 | |
| 206 TEST_F(AutoEnrollmentClientTest, AskForTooMuch) { | |
| 207 ServerWillReply(512, false, false); | |
| 208 client_->Start(); | |
| 209 EXPECT_FALSE(client_->should_auto_enroll()); | |
| 210 EXPECT_EQ(1, completion_callback_count_); | |
| 211 VerifyCachedResult(false, 8); | |
| 212 } | |
| 213 | |
| 214 TEST_F(AutoEnrollmentClientTest, AskNonPowerOf2) { | |
| 215 InSequence sequence; | |
| 216 ServerWillReply(100, false, false); | |
| 217 ServerWillReply(-1, false, false); | |
| 218 client_->Start(); | |
| 219 EXPECT_FALSE(client_->should_auto_enroll()); | |
| 220 EXPECT_EQ(1, completion_callback_count_); | |
| 221 EXPECT_TRUE(auto_enrollment_request().has_remainder()); | |
| 222 EXPECT_TRUE(auto_enrollment_request().has_modulus()); | |
| 223 EXPECT_EQ(128, auto_enrollment_request().modulus()); | |
| 224 EXPECT_EQ(kSerialHash[31] & 0x7f, auto_enrollment_request().remainder()); | |
| 225 VerifyCachedResult(false, 8); | |
| 226 } | |
| 227 | |
| 228 TEST_F(AutoEnrollmentClientTest, ConsumerDevice) { | |
| 229 ServerWillReply(-1, true, false); | |
| 230 client_->Start(); | |
| 231 EXPECT_FALSE(client_->should_auto_enroll()); | |
| 232 EXPECT_EQ(1, completion_callback_count_); | |
| 233 VerifyCachedResult(false, 8); | |
| 234 } | |
| 235 | |
| 236 TEST_F(AutoEnrollmentClientTest, EnterpriseDevice) { | |
| 237 ServerWillReply(-1, true, true); | |
| 238 client_->Start(); | |
| 239 EXPECT_TRUE(client_->should_auto_enroll()); | |
| 240 EXPECT_EQ(1, completion_callback_count_); | |
| 241 VerifyCachedResult(true, 8); | |
| 242 } | |
| 243 | |
| 244 TEST_F(AutoEnrollmentClientTest, NoSerial) { | |
| 245 CreateClient("", 4, 8); | |
| 246 client_->Start(); | |
| 247 EXPECT_FALSE(client_->should_auto_enroll()); | |
| 248 EXPECT_EQ(1, completion_callback_count_); | |
| 249 EXPECT_FALSE(local_state_->GetUserPref(prefs::kShouldAutoEnroll)); | |
| 250 EXPECT_FALSE(local_state_->GetUserPref(prefs::kAutoEnrollmentPowerLimit)); | |
| 251 } | |
| 252 | |
| 253 TEST_F(AutoEnrollmentClientTest, NoBitsUploaded) { | |
| 254 CreateClient(kSerial, 0, 0); | |
| 255 ServerWillReply(-1, false, false); | |
| 256 client_->Start(); | |
| 257 EXPECT_FALSE(client_->should_auto_enroll()); | |
| 258 EXPECT_EQ(1, completion_callback_count_); | |
| 259 EXPECT_TRUE(auto_enrollment_request().has_remainder()); | |
| 260 EXPECT_TRUE(auto_enrollment_request().has_modulus()); | |
| 261 EXPECT_EQ(1, auto_enrollment_request().modulus()); | |
| 262 EXPECT_EQ(0, auto_enrollment_request().remainder()); | |
| 263 VerifyCachedResult(false, 0); | |
| 264 } | |
| 265 | |
| 266 TEST_F(AutoEnrollmentClientTest, ManyBitsUploaded) { | |
| 267 int64 bottom62 = GG_INT64_C(0x14e437a1552b138a); | |
| 268 for (int i = 0; i <= 62; ++i) { | |
| 269 completion_callback_count_ = 0; | |
| 270 CreateClient(kSerial, i, i); | |
| 271 ServerWillReply(-1, false, false); | |
| 272 client_->Start(); | |
| 273 EXPECT_FALSE(client_->should_auto_enroll()); | |
| 274 EXPECT_EQ(1, completion_callback_count_); | |
| 275 EXPECT_TRUE(auto_enrollment_request().has_remainder()); | |
| 276 EXPECT_TRUE(auto_enrollment_request().has_modulus()); | |
| 277 EXPECT_EQ(GG_INT64_C(1) << i, auto_enrollment_request().modulus()); | |
| 278 EXPECT_EQ(bottom62 % (GG_INT64_C(1) << i), | |
| 279 auto_enrollment_request().remainder()); | |
| 280 VerifyCachedResult(false, i); | |
| 281 } | |
| 282 } | |
| 283 | |
| 284 TEST_F(AutoEnrollmentClientTest, MoreThan32BitsUploaded) { | |
| 285 CreateClient(kSerial, 10, 37); | |
| 286 InSequence sequence; | |
| 287 ServerWillReply(GG_INT64_C(1) << 37, false, false); | |
| 288 ServerWillReply(-1, true, true); | |
| 289 client_->Start(); | |
| 290 EXPECT_TRUE(client_->should_auto_enroll()); | |
| 291 EXPECT_EQ(1, completion_callback_count_); | |
| 292 VerifyCachedResult(true, 37); | |
| 293 } | |
| 294 | |
| 295 TEST_F(AutoEnrollmentClientTest, ReuseCachedDecision) { | |
| 296 EXPECT_CALL(*service_, CreateJob(_)).Times(0); | |
| 297 local_state_->SetUserPref(prefs::kShouldAutoEnroll, | |
| 298 Value::CreateBooleanValue(true)); | |
| 299 local_state_->SetUserPref(prefs::kAutoEnrollmentPowerLimit, | |
| 300 Value::CreateIntegerValue(8)); | |
| 301 client_->Start(); | |
| 302 EXPECT_TRUE(client_->should_auto_enroll()); | |
| 303 EXPECT_EQ(1, completion_callback_count_); | |
| 304 AutoEnrollmentClient::CancelAutoEnrollment(); | |
| 305 client_->Start(); | |
| 306 EXPECT_FALSE(client_->should_auto_enroll()); | |
| 307 EXPECT_EQ(2, completion_callback_count_); | |
| 308 } | |
| 309 | |
| 310 TEST_F(AutoEnrollmentClientTest, RetryIfPowerLargerThanCached) { | |
| 311 local_state_->SetUserPref(prefs::kShouldAutoEnroll, | |
| 312 Value::CreateBooleanValue(false)); | |
| 313 local_state_->SetUserPref(prefs::kAutoEnrollmentPowerLimit, | |
| 314 Value::CreateIntegerValue(8)); | |
| 315 CreateClient(kSerial, 5, 10); | |
| 316 ServerWillReply(-1, true, true); | |
| 317 client_->Start(); | |
| 318 EXPECT_TRUE(client_->should_auto_enroll()); | |
| 319 EXPECT_EQ(1, completion_callback_count_); | |
| 320 } | |
| 321 | |
| 322 } // namespace | |
| 323 | |
| 324 } // namespace policy | |
| OLD | NEW |