OLD | NEW |
| (Empty) |
1 // Copyright (c) 2011 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/login/ownership_service.h" | |
6 | |
7 #include <string> | |
8 | |
9 #include "base/file_path.h" | |
10 #include "base/file_util.h" | |
11 #include "base/logging.h" | |
12 #include "base/memory/scoped_ptr.h" | |
13 #include "base/message_loop.h" | |
14 #include "base/scoped_temp_dir.h" | |
15 #include "chrome/browser/chromeos/login/mock_owner_key_utils.h" | |
16 #include "chrome/browser/chromeos/login/owner_manager_unittest.h" | |
17 #include "content/public/browser/notification_service.h" | |
18 #include "content/public/test/test_browser_thread.h" | |
19 #include "crypto/nss_util.h" | |
20 #include "crypto/rsa_private_key.h" | |
21 #include "testing/gmock/include/gmock/gmock.h" | |
22 #include "testing/gtest/include/gtest/gtest.h" | |
23 | |
24 using content::BrowserThread; | |
25 using ::crypto::RSAPrivateKey; | |
26 using ::testing::DoAll; | |
27 using ::testing::Eq; | |
28 using ::testing::Invoke; | |
29 using ::testing::Return; | |
30 using ::testing::SetArgumentPointee; | |
31 using ::testing::_; | |
32 | |
33 | |
34 namespace chromeos { | |
35 | |
36 class OwnershipServiceTest : public testing::Test { | |
37 public: | |
38 OwnershipServiceTest() | |
39 : message_loop_(MessageLoop::TYPE_UI), | |
40 ui_thread_(BrowserThread::UI, &message_loop_), | |
41 file_thread_(BrowserThread::FILE), | |
42 mock_(new MockKeyUtils), | |
43 injector_(mock_) /* injector_ takes ownership of mock_ */ { | |
44 } | |
45 virtual ~OwnershipServiceTest() {} | |
46 | |
47 virtual void SetUp() { | |
48 crypto::OpenPersistentNSSDB(); // TODO(cmasone): use test DB instead | |
49 fake_private_key_.reset(RSAPrivateKey::Create(256)); | |
50 ASSERT_TRUE(fake_private_key_->ExportPublicKey(&fake_public_key_)); | |
51 | |
52 // Mimic ownership. | |
53 ASSERT_TRUE(tmpdir_.CreateUniqueTempDir()); | |
54 ASSERT_TRUE(file_util::CreateTemporaryFileInDir(tmpdir_.path(), &tmpfile_)); | |
55 | |
56 file_thread_.Start(); | |
57 OwnerKeyUtils::set_factory(&injector_); | |
58 service_.reset(new OwnershipService); // must happen AFTER set_factory(). | |
59 service_->Prewarm(); | |
60 } | |
61 | |
62 virtual void TearDown() { | |
63 OwnerKeyUtils::set_factory(NULL); | |
64 service_.reset(NULL); | |
65 } | |
66 | |
67 void StartUnowned() { | |
68 file_util::Delete(tmpfile_, false); | |
69 } | |
70 | |
71 ScopedTempDir tmpdir_; | |
72 FilePath tmpfile_; | |
73 | |
74 MessageLoop message_loop_; | |
75 content::TestBrowserThread ui_thread_; | |
76 content::TestBrowserThread file_thread_; | |
77 | |
78 std::vector<uint8> fake_public_key_; | |
79 scoped_ptr<RSAPrivateKey> fake_private_key_; | |
80 | |
81 MockKeyUtils* mock_; | |
82 MockInjector injector_; | |
83 scoped_ptr<OwnershipService> service_; | |
84 }; | |
85 | |
86 TEST_F(OwnershipServiceTest, IsOwned) { | |
87 EXPECT_CALL(*mock_, GetOwnerKeyFilePath()) | |
88 .WillRepeatedly(Return(tmpfile_)); | |
89 EXPECT_TRUE(service_->IsAlreadyOwned()); | |
90 } | |
91 | |
92 TEST_F(OwnershipServiceTest, IsOwnershipTaken) { | |
93 EXPECT_CALL(*mock_, GetOwnerKeyFilePath()) | |
94 .WillRepeatedly(Return(tmpfile_)); | |
95 EXPECT_TRUE(service_->GetStatus(true) == OwnershipService::OWNERSHIP_TAKEN); | |
96 } | |
97 | |
98 TEST_F(OwnershipServiceTest, IsUnowned) { | |
99 StartUnowned(); | |
100 | |
101 EXPECT_CALL(*mock_, GetOwnerKeyFilePath()) | |
102 .WillRepeatedly(Return(tmpfile_)); | |
103 EXPECT_FALSE(service_->IsAlreadyOwned()); | |
104 } | |
105 | |
106 TEST_F(OwnershipServiceTest, IsOwnershipNone) { | |
107 StartUnowned(); | |
108 | |
109 EXPECT_CALL(*mock_, GetOwnerKeyFilePath()) | |
110 .WillRepeatedly(Return(tmpfile_)); | |
111 EXPECT_TRUE(service_->GetStatus(true) == OwnershipService::OWNERSHIP_NONE); | |
112 } | |
113 | |
114 TEST_F(OwnershipServiceTest, LoadOwnerKeyFail) { | |
115 base::WaitableEvent event(true, false); | |
116 MockKeyLoadObserver loader(&event); | |
117 EXPECT_CALL(*mock_, GetOwnerKeyFilePath()) | |
118 .WillRepeatedly(Return(tmpfile_)); | |
119 EXPECT_CALL(*mock_, ImportPublicKey(tmpfile_, _)) | |
120 .WillOnce(Return(false)) | |
121 .RetiresOnSaturation(); | |
122 | |
123 service_->StartLoadOwnerKeyAttempt(); | |
124 | |
125 // Run remaining events, until ExportPublicKeyViaDbus(). | |
126 while (!event.IsSignaled()) | |
127 message_loop_.RunAllPending(); | |
128 } | |
129 | |
130 TEST_F(OwnershipServiceTest, UpdateOwnerKey) { | |
131 base::WaitableEvent event(true, false); | |
132 MockKeyUpdateUser delegate(&event); | |
133 service_->StartUpdateOwnerKey(std::vector<uint8>(), &delegate); | |
134 | |
135 while (!event.IsSignaled()) | |
136 message_loop_.RunAllPending(); | |
137 } | |
138 | |
139 TEST_F(OwnershipServiceTest, LoadOwnerKey) { | |
140 base::WaitableEvent event(true, false); | |
141 MockKeyLoadObserver loader(&event); | |
142 loader.ExpectKeyFetchSuccess(true); | |
143 | |
144 EXPECT_CALL(*mock_, GetOwnerKeyFilePath()) | |
145 .WillRepeatedly(Return(tmpfile_)); | |
146 EXPECT_CALL(*mock_, ImportPublicKey(tmpfile_, _)) | |
147 .WillOnce(DoAll(SetArgumentPointee<1>(fake_public_key_), | |
148 Return(true))) | |
149 .RetiresOnSaturation(); | |
150 service_->StartLoadOwnerKeyAttempt(); | |
151 | |
152 while (!event.IsSignaled()) | |
153 message_loop_.RunAllPending(); | |
154 } | |
155 | |
156 TEST_F(OwnershipServiceTest, NotYetOwnedVerify) { | |
157 StartUnowned(); | |
158 | |
159 EXPECT_CALL(*mock_, GetOwnerKeyFilePath()) | |
160 .WillRepeatedly(Return(tmpfile_)); | |
161 | |
162 base::WaitableEvent event(true, false); | |
163 MockKeyUser delegate(OwnerManager::KEY_UNAVAILABLE, &event); | |
164 service_->StartVerifyAttempt("", std::vector<uint8>(), &delegate); | |
165 | |
166 while (!event.IsSignaled()) | |
167 message_loop_.RunAllPending(); | |
168 } | |
169 | |
170 TEST_F(OwnershipServiceTest, GetKeyFailDuringVerify) { | |
171 MockKeyLoadObserver loader(NULL); | |
172 loader.ExpectKeyFetchSuccess(false); | |
173 | |
174 EXPECT_CALL(*mock_, GetOwnerKeyFilePath()) | |
175 .WillRepeatedly(Return(tmpfile_)); | |
176 EXPECT_CALL(*mock_, ImportPublicKey(tmpfile_, _)) | |
177 .WillOnce(Return(false)) | |
178 .RetiresOnSaturation(); | |
179 | |
180 base::WaitableEvent event(true, false); | |
181 MockKeyUser delegate(OwnerManager::KEY_UNAVAILABLE, &event); | |
182 service_->StartVerifyAttempt("", std::vector<uint8>(), &delegate); | |
183 | |
184 while (!event.IsSignaled()) | |
185 message_loop_.RunAllPending(); | |
186 } | |
187 | |
188 TEST_F(OwnershipServiceTest, GetKeyAndVerify) { | |
189 MockKeyLoadObserver loader(NULL); | |
190 loader.ExpectKeyFetchSuccess(true); | |
191 | |
192 std::string data; | |
193 std::vector<uint8> sig(0, 2); | |
194 | |
195 EXPECT_CALL(*mock_, GetOwnerKeyFilePath()) | |
196 .WillRepeatedly(Return(tmpfile_)); | |
197 EXPECT_CALL(*mock_, ImportPublicKey(tmpfile_, _)) | |
198 .WillOnce(DoAll(SetArgumentPointee<1>(fake_public_key_), | |
199 Return(true))) | |
200 .RetiresOnSaturation(); | |
201 EXPECT_CALL(*mock_, Verify(Eq(data), Eq(sig), Eq(fake_public_key_))) | |
202 .WillOnce(Return(true)) | |
203 .RetiresOnSaturation(); | |
204 | |
205 base::WaitableEvent event(true, false); | |
206 MockKeyUser delegate(OwnerManager::SUCCESS, &event); | |
207 service_->StartVerifyAttempt(data, sig, &delegate); | |
208 | |
209 while (!event.IsSignaled()) | |
210 message_loop_.RunAllPending(); | |
211 } | |
212 | |
213 TEST_F(OwnershipServiceTest, GetKeyAndFailVerify) { | |
214 MockKeyLoadObserver loader(NULL); | |
215 loader.ExpectKeyFetchSuccess(true); | |
216 | |
217 std::string data; | |
218 std::vector<uint8> sig(0, 2); | |
219 | |
220 EXPECT_CALL(*mock_, GetOwnerKeyFilePath()) | |
221 .WillRepeatedly(Return(tmpfile_)); | |
222 EXPECT_CALL(*mock_, ImportPublicKey(tmpfile_, _)) | |
223 .WillOnce(DoAll(SetArgumentPointee<1>(fake_public_key_), | |
224 Return(true))) | |
225 .RetiresOnSaturation(); | |
226 EXPECT_CALL(*mock_, Verify(Eq(data), Eq(sig), Eq(fake_public_key_))) | |
227 .WillOnce(Return(false)) | |
228 .RetiresOnSaturation(); | |
229 | |
230 base::WaitableEvent event(true, false); | |
231 MockKeyUser delegate(OwnerManager::OPERATION_FAILED, &event); | |
232 service_->StartVerifyAttempt(data, sig, &delegate); | |
233 | |
234 while (!event.IsSignaled()) | |
235 message_loop_.RunAllPending(); | |
236 } | |
237 | |
238 } // namespace chromeos | |
OLD | NEW |