Index: chrome/browser/policy/auto_enrollment_client_unittest.cc |
diff --git a/chrome/browser/policy/auto_enrollment_client_unittest.cc b/chrome/browser/policy/auto_enrollment_client_unittest.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..5096edd88a12bcf5a9a3930f4407a179df631c18 |
--- /dev/null |
+++ b/chrome/browser/policy/auto_enrollment_client_unittest.cc |
@@ -0,0 +1,249 @@ |
+// Copyright (c) 2011 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "chrome/browser/policy/auto_enrollment_client.h" |
+ |
+#include "base/rand_util.h" |
+#include "chrome/browser/policy/mock_device_management_backend.h" |
+#include "chrome/browser/policy/mock_device_management_service.h" |
+#include "crypto/sha2.h" |
+#include "testing/gmock/include/gmock/gmock.h" |
+#include "testing/gtest/include/gtest/gtest.h" |
+ |
+namespace policy { |
+ |
+namespace { |
+ |
+const char* kSerial = "serial"; |
+const char* kSerialHash = |
+ "\x01\x44\xb1\xde\xfc\xf7\x56\x10\x87\x01\x5f\x8d\x83\x0d\x65\xb1" |
+ "\x6f\x02\x4a\xd7\xeb\x92\x45\xfc\xd4\xe4\x37\xa1\x55\x2b\x13\x8a"; |
+ |
+using ::testing::AnyNumber; |
+using ::testing::DoAll; |
+using ::testing::Invoke; |
+using ::testing::Unused; |
+using ::testing::_; |
+ |
+ACTION_P(MockDeviceManagementBackendFailAutoEnrollment, error) { |
+ arg2->OnError(error); |
+} |
+ |
+ACTION_P(MockDeviceManagementBackendSucceedAutoEnrollment, response) { |
+ arg2->HandleAutoEnrollmentResponse(response); |
+} |
+ |
+class MockAutoEnrollmentClientDelegate : public AutoEnrollmentClient::Delegate { |
+ public: |
+ MockAutoEnrollmentClientDelegate() {} |
+ virtual ~MockAutoEnrollmentClientDelegate() {} |
+ |
+ MOCK_METHOD1(OnAutoEnrollmentComplete, void(AutoEnrollmentClient*)); |
+ |
+ private: |
+ DISALLOW_COPY_AND_ASSIGN(MockAutoEnrollmentClientDelegate); |
+}; |
+ |
+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
|
+ 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.
|
+ AutoEnrollmentClientTest() { |
+ } |
+ |
+ virtual void SetUp() OVERRIDE { |
+ CreateClient(kSerial, 4, 8); |
+ } |
+ |
+ void CreateClient(const std::string& serial, |
+ int power_initial, |
+ int power_limit) { |
+ MockDeviceManagementService* service = new MockDeviceManagementService; |
+ EXPECT_CALL(*service, CreateBackend()) |
+ .Times(AnyNumber()) |
Mattias Nissler (ping if slow)
2011/12/09 15:40:52
indentation.
Joao da Silva
2011/12/09 17:26:06
Done.
|
+ .WillRepeatedly(MockDeviceManagementServiceProxyBackend(&backend_)); |
+ client_.reset(new AutoEnrollmentClient(&delegate_, |
+ service, |
+ serial, |
+ power_initial, |
+ power_limit)); |
+ } |
+ |
+ void ExpectNotification() { |
+ EXPECT_CALL(delegate_, OnAutoEnrollmentComplete(client_.get())).Times(1); |
+ } |
+ |
+ void ServerWillFail(DeviceManagementBackend::ErrorCode error) { |
+ EXPECT_CALL(backend_, ProcessAutoEnrollmentRequest(_, _, _)) |
+ .WillOnce( |
+ DoAll( |
+ Invoke(this, |
+ &AutoEnrollmentClientTest::OnProcessAutoEnrollmentRequest), |
+ MockDeviceManagementBackendFailAutoEnrollment(error))) |
+ .RetiresOnSaturation(); |
+ } |
+ |
+ void ServerWillReply(int64 modulus, bool with_hashes, bool with_serial_hash) { |
+ em::DeviceAutoEnrollmentResponse response; |
+ if (modulus >= 0) |
+ response.set_modulus(modulus); |
+ if (with_hashes) { |
+ for (size_t i = 0; i < 10; ++i) { |
+ response.mutable_hashes()->Add()->assign( |
+ 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! :-)
|
+ } |
+ } |
+ if (with_serial_hash) { |
+ response.mutable_hashes()->Add()->assign(kSerialHash, |
+ crypto::kSHA256Length); |
+ } |
+ EXPECT_CALL(backend_, ProcessAutoEnrollmentRequest(_, _, _)) |
+ .WillOnce( |
+ DoAll( |
+ Invoke(this, |
+ &AutoEnrollmentClientTest::OnProcessAutoEnrollmentRequest), |
+ MockDeviceManagementBackendSucceedAutoEnrollment(response))) |
+ .RetiresOnSaturation(); |
+ } |
+ |
+ protected: |
+ MockDeviceManagementBackend backend_; |
+ MockAutoEnrollmentClientDelegate delegate_; |
+ scoped_ptr<AutoEnrollmentClient> client_; |
+ em::DeviceAutoEnrollmentRequest last_request_; |
+ |
+ private: |
+ 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.
|
+ Unused, |
+ const em::DeviceAutoEnrollmentRequest& request, |
+ Unused) { |
+ last_request_ = request; |
+ } |
+ |
+ DISALLOW_COPY_AND_ASSIGN(AutoEnrollmentClientTest); |
+}; |
+ |
+TEST_F(AutoEnrollmentClientTest, NetworkFailure) { |
+ ServerWillFail(DeviceManagementBackend::kErrorTemporaryUnavailable); |
+ ExpectNotification(); |
+ client_->Start(); |
+ EXPECT_FALSE(client_->should_auto_enroll()); |
+} |
+ |
+TEST_F(AutoEnrollmentClientTest, EmptyReply) { |
+ ServerWillReply(-1, false, false); |
+ ExpectNotification(); |
+ client_->Start(); |
+ EXPECT_FALSE(client_->should_auto_enroll()); |
+} |
+ |
+TEST_F(AutoEnrollmentClientTest, ClientUploadsRightBits) { |
+ ServerWillReply(-1, false, false); |
+ ExpectNotification(); |
+ client_->Start(); |
+ EXPECT_FALSE(client_->should_auto_enroll()); |
+ EXPECT_TRUE(last_request_.has_remainder()); |
+ EXPECT_TRUE(last_request_.has_modulus()); |
+ EXPECT_EQ(16, last_request_.modulus()); |
+ EXPECT_EQ(kSerialHash[31] & 0xf, last_request_.remainder()); |
+} |
+ |
+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
|
+ ServerWillFail(DeviceManagementBackend::kErrorTemporaryUnavailable); |
+ ServerWillReply(32, false, false); |
+ ExpectNotification(); |
+ client_->Start(); |
+ EXPECT_FALSE(client_->should_auto_enroll()); |
+} |
+ |
+TEST_F(AutoEnrollmentClientTest, AskForMoreThenEvenMore) { |
+ ServerWillReply(64, false, false); |
+ ServerWillReply(32, false, false); |
+ ExpectNotification(); |
+ client_->Start(); |
+ EXPECT_FALSE(client_->should_auto_enroll()); |
+} |
+ |
+TEST_F(AutoEnrollmentClientTest, AskForLess) { |
+ ServerWillReply(8, false, false); |
+ ExpectNotification(); |
+ client_->Start(); |
+ EXPECT_FALSE(client_->should_auto_enroll()); |
+} |
+ |
+TEST_F(AutoEnrollmentClientTest, AskForSame) { |
+ ServerWillReply(16, false, false); |
+ ExpectNotification(); |
+ client_->Start(); |
+ EXPECT_FALSE(client_->should_auto_enroll()); |
+} |
+ |
+TEST_F(AutoEnrollmentClientTest, AskForTooMuch) { |
+ ServerWillReply(512, false, false); |
+ ExpectNotification(); |
+ client_->Start(); |
+ EXPECT_FALSE(client_->should_auto_enroll()); |
+} |
+ |
+TEST_F(AutoEnrollmentClientTest, AskNonPowerOf2) { |
+ ServerWillReply(-1, false, false); |
+ ServerWillReply(100, false, false); |
+ ExpectNotification(); |
+ client_->Start(); |
+ EXPECT_FALSE(client_->should_auto_enroll()); |
+ EXPECT_TRUE(last_request_.has_remainder()); |
+ EXPECT_TRUE(last_request_.has_modulus()); |
+ EXPECT_EQ(128, last_request_.modulus()); |
+ EXPECT_EQ(kSerialHash[31] & 0x7f, last_request_.remainder()); |
+} |
+ |
+TEST_F(AutoEnrollmentClientTest, ConsumerDevice) { |
+ ServerWillReply(-1, true, false); |
+ ExpectNotification(); |
+ client_->Start(); |
+ EXPECT_FALSE(client_->should_auto_enroll()); |
+} |
+ |
+TEST_F(AutoEnrollmentClientTest, EnterpriseDevice) { |
+ ServerWillReply(-1, true, true); |
+ ExpectNotification(); |
+ client_->Start(); |
+ EXPECT_TRUE(client_->should_auto_enroll()); |
+} |
+ |
+TEST_F(AutoEnrollmentClientTest, NoSerial) { |
+ CreateClient("", 4, 8); |
+ ExpectNotification(); |
+ client_->Start(); |
+ EXPECT_FALSE(client_->should_auto_enroll()); |
+} |
+ |
+TEST_F(AutoEnrollmentClientTest, NoBitsUploaded) { |
+ CreateClient(kSerial, 0, 0); |
+ ServerWillReply(-1, false, false); |
+ ExpectNotification(); |
+ client_->Start(); |
+ EXPECT_FALSE(client_->should_auto_enroll()); |
+ EXPECT_TRUE(last_request_.has_remainder()); |
+ EXPECT_TRUE(last_request_.has_modulus()); |
+ EXPECT_EQ(1, last_request_.modulus()); |
+ EXPECT_EQ(0, last_request_.remainder()); |
+} |
+ |
+TEST_F(AutoEnrollmentClientTest, ManyBitsUploaded) { |
+ int64 bottom62 = GG_LONGLONG(0x14e437a1552b138a); |
+ for (int i = 0; i <= 62; ++i) { |
+ CreateClient(kSerial, i, i); |
+ ServerWillReply(-1, false, false); |
+ ExpectNotification(); |
+ client_->Start(); |
+ EXPECT_FALSE(client_->should_auto_enroll()); |
+ EXPECT_TRUE(last_request_.has_remainder()); |
+ EXPECT_TRUE(last_request_.has_modulus()); |
+ EXPECT_EQ((int64) 1 << i, last_request_.modulus()); |
+ EXPECT_EQ(bottom62 % ((int64) 1 << i), last_request_.remainder()); |
+ } |
+} |
+ |
+} // namespace |
+ |
+} // namespace policy |