summaryrefslogtreecommitdiffstats
path: root/chrome/browser/chromeos/login/owner_manager_unittest.cc
blob: f28f029f190a98c9e7d9ed7bdd8311ee479a0d1e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
// 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/chromeos/login/owner_manager.h"
#include "chrome/browser/chromeos/login/owner_manager_unittest.h"

#include <string>

#include "base/bind.h"
#include "base/file_path.h"
#include "base/file_util.h"
#include "base/logging.h"
#include "base/message_loop.h"
#include "base/scoped_temp_dir.h"
#include "chrome/browser/chromeos/login/mock_owner_key_utils.h"
#include "chrome/common/chrome_notification_types.h"
#include "content/public/browser/notification_service.h"
#include "content/public/test/test_browser_thread.h"
#include "crypto/nss_util.h"
#include "crypto/rsa_private_key.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"

using content::BrowserThread;
using ::crypto::RSAPrivateKey;
using ::testing::DoAll;
using ::testing::Eq;
using ::testing::Invoke;
using ::testing::Return;
using ::testing::SetArgumentPointee;
using ::testing::_;

namespace chromeos {

////////////////////////////////////////////////////////////////////////////////
// MockKeyLoadObserver
MockKeyLoadObserver::MockKeyLoadObserver(base::WaitableEvent* e)
    : success_expected_(false),
      event_(e),
      observed_(false) {
  registrar_.Add(
      this,
      chrome::NOTIFICATION_OWNER_KEY_FETCH_ATTEMPT_FAILED,
      content::NotificationService::AllSources());
  registrar_.Add(
      this,
      chrome::NOTIFICATION_OWNER_KEY_FETCH_ATTEMPT_SUCCEEDED,
      content::NotificationService::AllSources());
}

MockKeyLoadObserver::~MockKeyLoadObserver() {
  DCHECK(observed_);
}

void MockKeyLoadObserver::Observe(int type,
                                  const content::NotificationSource& source,
                                  const content::NotificationDetails& details) {
  LOG(INFO) << "Observed key fetch event";
  if (type == chrome::NOTIFICATION_OWNER_KEY_FETCH_ATTEMPT_SUCCEEDED) {
    DCHECK(success_expected_);
    observed_ = true;
    if (event_)
      event_->Signal();
  } else if (type == chrome::NOTIFICATION_OWNER_KEY_FETCH_ATTEMPT_FAILED) {
    DCHECK(!success_expected_);
    observed_ = true;
    if (event_)
      event_->Signal();
  }
}

void MockKeyLoadObserver::ExpectKeyFetchSuccess(bool should_succeed) {
  success_expected_ = should_succeed;
}

////////////////////////////////////////////////////////////////////////////////
// MockKeyUser
MockKeyUser::MockKeyUser(const OwnerManager::KeyOpCode expected,
                         base::WaitableEvent* e)
    : expected_(expected),
      event_(e) {
}

void MockKeyUser::OnKeyOpComplete(const OwnerManager::KeyOpCode return_code,
                                  const std::vector<uint8>& payload) {
  DCHECK_EQ(expected_, return_code);
  if (event_)
    event_->Signal();
}

////////////////////////////////////////////////////////////////////////////////
// MockKeyUpdateUser

void MockKeyUpdateUser::OnKeyUpdated() {
  if (event_)
    event_->Signal();
}

////////////////////////////////////////////////////////////////////////////////
// MockSigner

MockSigner::MockSigner(const OwnerManager::KeyOpCode expected,
                       const std::vector<uint8>& sig,
                       base::WaitableEvent* e)
    : expected_code_(expected),
      expected_sig_(sig),
      event_(e) {
}

MockSigner::~MockSigner() {}

void MockSigner::OnKeyOpComplete(const OwnerManager::KeyOpCode return_code,
                                 const std::vector<uint8>& payload) {
  DCHECK_EQ(expected_code_, return_code);
  for (uint32 i = 0; i < payload.size(); ++i)
    DCHECK_EQ(expected_sig_[i], payload[i]);
  if (event_)
    event_->Signal();
}

////////////////////////////////////////////////////////////////////////////////
// OwnerManagerTest

class OwnerManagerTest : public testing::Test {
 public:
  OwnerManagerTest()
      : message_loop_(MessageLoop::TYPE_UI),
        ui_thread_(BrowserThread::UI, &message_loop_),
        file_thread_(BrowserThread::FILE),
        mock_(new MockKeyUtils),
        injector_(mock_) /* injector_ takes ownership of mock_ */ {
  }
  virtual ~OwnerManagerTest() {}

  virtual void SetUp() {
    crypto::OpenPersistentNSSDB();  // TODO(cmasone): use test DB instead
    fake_private_key_.reset(RSAPrivateKey::Create(256));
    ASSERT_TRUE(fake_private_key_->ExportPublicKey(&fake_public_key_));

    // Mimic ownership.
    ASSERT_TRUE(tmpdir_.CreateUniqueTempDir());
    ASSERT_TRUE(file_util::CreateTemporaryFileInDir(tmpdir_.path(), &tmpfile_));

    file_thread_.Start();
    OwnerKeyUtils::set_factory(&injector_);
  }

  virtual void TearDown() {
    OwnerKeyUtils::set_factory(NULL);
  }

  void StartUnowned() {
    file_util::Delete(tmpfile_, false);
  }

  void InjectKeys(OwnerManager* manager) {
    manager->public_key_ = fake_public_key_;
    manager->private_key_.reset(fake_private_key_.release());
  }

  ScopedTempDir tmpdir_;
  FilePath tmpfile_;

  MessageLoop message_loop_;
  content::TestBrowserThread ui_thread_;
  content::TestBrowserThread file_thread_;

  std::vector<uint8> fake_public_key_;
  scoped_ptr<RSAPrivateKey> fake_private_key_;

  MockKeyUtils* mock_;
  MockInjector injector_;
};

TEST_F(OwnerManagerTest, UpdateOwnerKey) {
  scoped_refptr<OwnerManager> manager(new OwnerManager);

  base::WaitableEvent event(true, false);
  MockKeyUpdateUser delegate(&event);
  BrowserThread::PostTask(
      BrowserThread::FILE, FROM_HERE,
      base::Bind(&OwnerManager::UpdateOwnerKey, manager.get(),
                 BrowserThread::UI, std::vector<uint8>(), &delegate));
  while (!event.IsSignaled())
    message_loop_.RunAllPending();
}

TEST_F(OwnerManagerTest, LoadOwnerKeyFail) {
  StartUnowned();
  base::WaitableEvent event(true, false);
  MockKeyLoadObserver loader(&event);
  scoped_refptr<OwnerManager> manager(new OwnerManager);

  EXPECT_CALL(*mock_, GetOwnerKeyFilePath())
      .WillRepeatedly(Return(tmpfile_));
  EXPECT_CALL(*mock_, ImportPublicKey(tmpfile_, _))
      .WillOnce(Return(false))
      .RetiresOnSaturation();

  BrowserThread::PostTask(
      BrowserThread::FILE, FROM_HERE,
      base::Bind(&OwnerManager::LoadOwnerKey, manager.get()));
  while (!event.IsSignaled())
    message_loop_.RunAllPending();
}

TEST_F(OwnerManagerTest, AlreadyLoadedOwnerKey) {
  base::WaitableEvent event(true, false);
  MockKeyLoadObserver loader(&event);
  loader.ExpectKeyFetchSuccess(true);
  scoped_refptr<OwnerManager> manager(new OwnerManager);

  EXPECT_CALL(*mock_, GetOwnerKeyFilePath())
      .WillRepeatedly(Return(tmpfile_));

  InjectKeys(manager.get());

  BrowserThread::PostTask(
      BrowserThread::FILE, FROM_HERE,
      base::Bind(&OwnerManager::LoadOwnerKey, manager.get()));
  while (!event.IsSignaled())
    message_loop_.RunAllPending();
}

TEST_F(OwnerManagerTest, LoadOwnerKey) {
  base::WaitableEvent event(true, false);
  MockKeyLoadObserver loader(&event);
  loader.ExpectKeyFetchSuccess(true);
  scoped_refptr<OwnerManager> manager(new OwnerManager);

  EXPECT_CALL(*mock_, GetOwnerKeyFilePath())
      .WillRepeatedly(Return(tmpfile_));
  EXPECT_CALL(*mock_, ImportPublicKey(tmpfile_, _))
      .WillOnce(DoAll(SetArgumentPointee<1>(fake_public_key_),
                      Return(true)))
      .RetiresOnSaturation();

  BrowserThread::PostTask(
      BrowserThread::FILE, FROM_HERE,
      base::Bind(&OwnerManager::LoadOwnerKey, manager.get()));
  while (!event.IsSignaled())
    message_loop_.RunAllPending();
}

TEST_F(OwnerManagerTest, GetKeyFailDuringVerify) {
  StartUnowned();
  MockKeyLoadObserver loader(NULL);
  loader.ExpectKeyFetchSuccess(false);
  scoped_refptr<OwnerManager> manager(new OwnerManager);

  EXPECT_CALL(*mock_, GetOwnerKeyFilePath())
      .WillRepeatedly(Return(tmpfile_));
  EXPECT_CALL(*mock_, ImportPublicKey(tmpfile_, _))
      .WillOnce(Return(false))
      .RetiresOnSaturation();

  base::WaitableEvent event(true, false);
  MockKeyUser delegate(OwnerManager::KEY_UNAVAILABLE, &event);

  BrowserThread::PostTask(
      BrowserThread::FILE, FROM_HERE,
      base::Bind(&OwnerManager::Verify, manager.get(), BrowserThread::UI,
                 std::string(), std::vector<uint8>(), &delegate));
  while (!event.IsSignaled())
    message_loop_.RunAllPending();
}

TEST_F(OwnerManagerTest, AlreadyHaveKeysVerify) {
  scoped_refptr<OwnerManager> manager(new OwnerManager);

  std::string data;
  std::vector<uint8> sig(0, 2);

  EXPECT_CALL(*mock_, GetOwnerKeyFilePath())
      .WillRepeatedly(Return(tmpfile_));
  EXPECT_CALL(*mock_, Verify(Eq(data), Eq(sig), Eq(fake_public_key_)))
      .WillOnce(Return(true))
      .RetiresOnSaturation();

  InjectKeys(manager.get());
  base::WaitableEvent event(true, false);
  MockKeyUser delegate(OwnerManager::SUCCESS, &event);

  BrowserThread::PostTask(
      BrowserThread::FILE, FROM_HERE,
      base::Bind(&OwnerManager::Verify, manager.get(), BrowserThread::UI, data,
                 sig, &delegate));
  while (!event.IsSignaled())
    message_loop_.RunAllPending();
}

TEST_F(OwnerManagerTest, GetKeyAndVerify) {
  MockKeyLoadObserver loader(NULL);
  loader.ExpectKeyFetchSuccess(true);
  scoped_refptr<OwnerManager> manager(new OwnerManager);

  std::string data;
  std::vector<uint8> sig(0, 2);

  EXPECT_CALL(*mock_, GetOwnerKeyFilePath())
      .WillRepeatedly(Return(tmpfile_));
  EXPECT_CALL(*mock_, ImportPublicKey(tmpfile_, _))
      .WillOnce(DoAll(SetArgumentPointee<1>(fake_public_key_),
                      Return(true)))
      .RetiresOnSaturation();
  EXPECT_CALL(*mock_, Verify(Eq(data), Eq(sig), Eq(fake_public_key_)))
      .WillOnce(Return(true))
      .RetiresOnSaturation();

  base::WaitableEvent event(true, false);
  MockKeyUser delegate(OwnerManager::SUCCESS, &event);

  BrowserThread::PostTask(
      BrowserThread::FILE, FROM_HERE,
      base::Bind(&OwnerManager::Verify, manager.get(), BrowserThread::UI, data,
                 sig, &delegate));
  while (!event.IsSignaled())
    message_loop_.RunAllPending();
}

TEST_F(OwnerManagerTest, AlreadyHaveKeysSign) {
  scoped_refptr<OwnerManager> manager(new OwnerManager);

  std::string data;
  std::vector<uint8> sig(0, 2);

  EXPECT_CALL(*mock_, GetOwnerKeyFilePath())
      .WillRepeatedly(Return(tmpfile_));
  EXPECT_CALL(*mock_, Sign(Eq(data), _, Eq(fake_private_key_.get())))
      .WillOnce(DoAll(SetArgumentPointee<1>(sig),
                      Return(true)))
      .RetiresOnSaturation();

  InjectKeys(manager.get());
  base::WaitableEvent event(true, false);
  MockSigner delegate(OwnerManager::SUCCESS, sig, &event);

  BrowserThread::PostTask(
      BrowserThread::FILE, FROM_HERE,
      base::Bind(&OwnerManager::Sign, manager.get(), BrowserThread::UI, data,
                 &delegate));
  while (!event.IsSignaled())
    message_loop_.RunAllPending();
}

}  // namespace chromeos