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.
5 #include "chrome/browser/chromeos/policy/consumer_management_service.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"
21 using testing::Invoke;
22 using testing::NiceMock;
26 const char* kAttributeOwnerId = "consumer_management.owner_id";
27 const char* kTestOwner = "test@chromium.org.test";
32 class ConsumerManagementServiceTest : public testing::Test {
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(_, _))
41 Invoke(this, &ConsumerManagementServiceTest::MockGetBootAttribute));
42 ON_CALL(mock_cryptohome_client_, SetBootAttribute(_, _))
44 Invoke(this, &ConsumerManagementServiceTest::MockSetBootAttribute));
45 ON_CALL(mock_cryptohome_client_, FlushAndSignBootAttributes(_, _))
48 &ConsumerManagementServiceTest::
49 MockFlushAndSignBootAttributes));
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_);
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_);
66 void MockFlushAndSignBootAttributes(
67 const cryptohome::FlushAndSignBootAttributesRequest& request,
68 const chromeos::CryptohomeClient::ProtobufMethodCallback& callback) {
69 callback.Run(cryptohome_status_, cryptohome_result_, cryptohome_reply_);
72 void OnGetOwnerDone(const std::string& owner) {
76 void OnSetOwnerDone(bool status) {
77 set_owner_status_ = status;
80 ScopedTestingLocalState testing_local_state_;
81 NiceMock<chromeos::MockCryptohomeClient> mock_cryptohome_client_;
82 scoped_ptr<ConsumerManagementService> service_;
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_;
91 bool set_owner_status_;
94 TEST_F(ConsumerManagementServiceTest, CanGetEnrollmentState) {
95 EXPECT_EQ(ConsumerManagementService::ENROLLMENT_NONE,
96 service_->GetEnrollmentState());
98 testing_local_state_.Get()->SetInteger(
99 prefs::kConsumerManagementEnrollmentState,
100 ConsumerManagementService::ENROLLMENT_ENROLLING);
102 EXPECT_EQ(ConsumerManagementService::ENROLLMENT_ENROLLING,
103 service_->GetEnrollmentState());
106 TEST_F(ConsumerManagementServiceTest, CanSetEnrollmentState) {
107 EXPECT_EQ(ConsumerManagementService::ENROLLMENT_NONE,
108 testing_local_state_.Get()->GetInteger(
109 prefs::kConsumerManagementEnrollmentState));
111 service_->SetEnrollmentState(ConsumerManagementService::ENROLLMENT_ENROLLING);
113 EXPECT_EQ(ConsumerManagementService::ENROLLMENT_ENROLLING,
114 testing_local_state_.Get()->GetInteger(
115 prefs::kConsumerManagementEnrollmentState));
118 TEST_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);
124 service_->GetOwner(base::Bind(&ConsumerManagementServiceTest::OnGetOwnerDone,
125 base::Unretained(this)));
127 EXPECT_EQ(kAttributeOwnerId, get_boot_attribute_request_.name());
128 EXPECT_EQ(kTestOwner, owner_);
131 TEST_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);
137 service_->GetOwner(base::Bind(&ConsumerManagementServiceTest::OnGetOwnerDone,
138 base::Unretained(this)));
140 EXPECT_EQ("", owner_);
143 TEST_F(ConsumerManagementServiceTest, CanSetOwner) {
144 cryptohome_status_ = chromeos::DBUS_METHOD_CALL_SUCCESS;
145 cryptohome_result_ = true;
147 service_->SetOwner(kTestOwner,
148 base::Bind(&ConsumerManagementServiceTest::OnSetOwnerDone,
149 base::Unretained(this)));
151 EXPECT_EQ(kAttributeOwnerId, set_boot_attribute_request_.name());
152 EXPECT_EQ(kTestOwner, set_boot_attribute_request_.value());
153 EXPECT_TRUE(set_owner_status_);
156 TEST_F(ConsumerManagementServiceTest, SetOwnerReturnsFalseWhenItFails) {
157 cryptohome_status_ = chromeos::DBUS_METHOD_CALL_FAILURE;
158 cryptohome_result_ = false;
160 service_->SetOwner(kTestOwner,
161 base::Bind(&ConsumerManagementServiceTest::OnSetOwnerDone,
162 base::Unretained(this)));
164 EXPECT_FALSE(set_owner_status_);
167 } // namespace policy