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