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