consumer_management_service_unittest.cc revision 6e8cce623b6e4fe0c9e4af605d675dd9d0338c38
1// Copyright 2014 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/chromeos/policy/consumer_management_service.h" 6 7#include "base/bind.h" 8#include "base/bind_helpers.h" 9#include "base/callback.h" 10#include "base/prefs/pref_registry_simple.h" 11#include "base/prefs/testing_pref_service.h" 12#include "chrome/common/pref_names.h" 13#include "chrome/test/base/scoped_testing_local_state.h" 14#include "chrome/test/base/testing_browser_process.h" 15#include "chromeos/dbus/cryptohome/rpc.pb.h" 16#include "chromeos/dbus/cryptohome_client.h" 17#include "chromeos/dbus/mock_cryptohome_client.h" 18#include "testing/gmock/include/gmock/gmock.h" 19#include "testing/gtest/include/gtest/gtest.h" 20 21using testing::Invoke; 22using testing::NiceMock; 23using testing::_; 24 25namespace { 26const char* kAttributeOwnerId = "consumer_management.owner_id"; 27const char* kTestOwner = "test@chromium.org.test"; 28} 29 30namespace policy { 31 32class ConsumerManagementServiceTest : public testing::Test { 33 public: 34 ConsumerManagementServiceTest() 35 : testing_local_state_(TestingBrowserProcess::GetGlobal()), 36 service_(new ConsumerManagementService(&mock_cryptohome_client_)), 37 cryptohome_result_(false), 38 set_owner_status_(false) { 39 ON_CALL(mock_cryptohome_client_, GetBootAttribute(_, _)) 40 .WillByDefault( 41 Invoke(this, &ConsumerManagementServiceTest::MockGetBootAttribute)); 42 ON_CALL(mock_cryptohome_client_, SetBootAttribute(_, _)) 43 .WillByDefault( 44 Invoke(this, &ConsumerManagementServiceTest::MockSetBootAttribute)); 45 ON_CALL(mock_cryptohome_client_, FlushAndSignBootAttributes(_, _)) 46 .WillByDefault( 47 Invoke(this, 48 &ConsumerManagementServiceTest:: 49 MockFlushAndSignBootAttributes)); 50 } 51 52 void MockGetBootAttribute( 53 const cryptohome::GetBootAttributeRequest& request, 54 const chromeos::CryptohomeClient::ProtobufMethodCallback& callback) { 55 get_boot_attribute_request_ = request; 56 callback.Run(cryptohome_status_, cryptohome_result_, cryptohome_reply_); 57 } 58 59 void MockSetBootAttribute( 60 const cryptohome::SetBootAttributeRequest& request, 61 const chromeos::CryptohomeClient::ProtobufMethodCallback& callback) { 62 set_boot_attribute_request_ = request; 63 callback.Run(cryptohome_status_, cryptohome_result_, cryptohome_reply_); 64 } 65 66 void MockFlushAndSignBootAttributes( 67 const cryptohome::FlushAndSignBootAttributesRequest& request, 68 const chromeos::CryptohomeClient::ProtobufMethodCallback& callback) { 69 callback.Run(cryptohome_status_, cryptohome_result_, cryptohome_reply_); 70 } 71 72 void OnGetOwnerDone(const std::string& owner) { 73 owner_ = owner; 74 } 75 76 void OnSetOwnerDone(bool status) { 77 set_owner_status_ = status; 78 } 79 80 ScopedTestingLocalState testing_local_state_; 81 NiceMock<chromeos::MockCryptohomeClient> mock_cryptohome_client_; 82 scoped_ptr<ConsumerManagementService> service_; 83 84 chromeos::DBusMethodCallStatus cryptohome_status_; 85 bool cryptohome_result_; 86 cryptohome::BaseReply cryptohome_reply_; 87 cryptohome::GetBootAttributeRequest get_boot_attribute_request_; 88 cryptohome::SetBootAttributeRequest set_boot_attribute_request_; 89 90 std::string owner_; 91 bool set_owner_status_; 92}; 93 94TEST_F(ConsumerManagementServiceTest, CanGetEnrollmentState) { 95 EXPECT_EQ(ConsumerManagementService::ENROLLMENT_NONE, 96 service_->GetEnrollmentState()); 97 98 testing_local_state_.Get()->SetInteger( 99 prefs::kConsumerManagementEnrollmentState, 100 ConsumerManagementService::ENROLLMENT_ENROLLING); 101 102 EXPECT_EQ(ConsumerManagementService::ENROLLMENT_ENROLLING, 103 service_->GetEnrollmentState()); 104} 105 106TEST_F(ConsumerManagementServiceTest, CanSetEnrollmentState) { 107 EXPECT_EQ(ConsumerManagementService::ENROLLMENT_NONE, 108 testing_local_state_.Get()->GetInteger( 109 prefs::kConsumerManagementEnrollmentState)); 110 111 service_->SetEnrollmentState(ConsumerManagementService::ENROLLMENT_ENROLLING); 112 113 EXPECT_EQ(ConsumerManagementService::ENROLLMENT_ENROLLING, 114 testing_local_state_.Get()->GetInteger( 115 prefs::kConsumerManagementEnrollmentState)); 116} 117 118TEST_F(ConsumerManagementServiceTest, CanGetOwner) { 119 cryptohome_status_ = chromeos::DBUS_METHOD_CALL_SUCCESS; 120 cryptohome_result_ = true; 121 cryptohome_reply_.MutableExtension(cryptohome::GetBootAttributeReply::reply)-> 122 set_value(kTestOwner); 123 124 service_->GetOwner(base::Bind(&ConsumerManagementServiceTest::OnGetOwnerDone, 125 base::Unretained(this))); 126 127 EXPECT_EQ(kAttributeOwnerId, get_boot_attribute_request_.name()); 128 EXPECT_EQ(kTestOwner, owner_); 129} 130 131TEST_F(ConsumerManagementServiceTest, GetOwnerReturnsAnEmptyStringWhenItFails) { 132 cryptohome_status_ = chromeos::DBUS_METHOD_CALL_FAILURE; 133 cryptohome_result_ = false; 134 cryptohome_reply_.MutableExtension(cryptohome::GetBootAttributeReply::reply)-> 135 set_value(kTestOwner); 136 137 service_->GetOwner(base::Bind(&ConsumerManagementServiceTest::OnGetOwnerDone, 138 base::Unretained(this))); 139 140 EXPECT_EQ("", owner_); 141} 142 143TEST_F(ConsumerManagementServiceTest, CanSetOwner) { 144 cryptohome_status_ = chromeos::DBUS_METHOD_CALL_SUCCESS; 145 cryptohome_result_ = true; 146 147 service_->SetOwner(kTestOwner, 148 base::Bind(&ConsumerManagementServiceTest::OnSetOwnerDone, 149 base::Unretained(this))); 150 151 EXPECT_EQ(kAttributeOwnerId, set_boot_attribute_request_.name()); 152 EXPECT_EQ(kTestOwner, set_boot_attribute_request_.value()); 153 EXPECT_TRUE(set_owner_status_); 154} 155 156TEST_F(ConsumerManagementServiceTest, SetOwnerReturnsFalseWhenItFails) { 157 cryptohome_status_ = chromeos::DBUS_METHOD_CALL_FAILURE; 158 cryptohome_result_ = false; 159 160 service_->SetOwner(kTestOwner, 161 base::Bind(&ConsumerManagementServiceTest::OnSetOwnerDone, 162 base::Unretained(this))); 163 164 EXPECT_FALSE(set_owner_status_); 165} 166 167} // namespace policy 168