Chromium Code Reviews| Index: chrome/browser/chromeos/login/owner_key_util_unittest.cc |
| diff --git a/chrome/browser/chromeos/login/owner_key_util_unittest.cc b/chrome/browser/chromeos/login/owner_key_util_unittest.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..ba5288c8de8293d052ef301f5b37e0e248ea90f0 |
| --- /dev/null |
| +++ b/chrome/browser/chromeos/login/owner_key_util_unittest.cc |
| @@ -0,0 +1,82 @@ |
| +// Copyright (c) 2012 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_key_util.h" |
| + |
| +#include <string> |
| +#include <vector> |
| + |
| +#include "base/file_path.h" |
| +#include "base/file_util.h" |
| +#include "base/memory/ref_counted.h" |
| +#include "base/scoped_temp_dir.h" |
| +#include "crypto/nss_util.h" |
| +#include "crypto/nss_util_internal.h" |
| +#include "crypto/rsa_private_key.h" |
| +#include "testing/gmock/include/gmock/gmock.h" |
| +#include "testing/gtest/include/gtest/gtest.h" |
| + |
| +namespace chromeos { |
| + |
| +class OwnerKeyUtilTest : public ::testing::Test { |
| + public: |
| + OwnerKeyUtilTest() {} |
| + virtual ~OwnerKeyUtilTest() {} |
| + |
| + virtual void SetUp() { |
| + crypto::OpenPersistentNSSDB(); |
| + |
| + ASSERT_TRUE(tmpdir_.CreateUniqueTempDir()); |
| + key_file_ = tmpdir_.path().Append("key"); |
| + util_ = new OwnerKeyUtilImpl(key_file_); |
| + } |
| + |
| + // Key generation parameters. |
| + static const uint16 kKeySizeInBits; |
| + |
| + ScopedTempDir tmpdir_; |
| + FilePath key_file_; |
| + scoped_refptr<OwnerKeyUtil> util_; |
| +}; |
| + |
| +// We're generating and using 2048-bit RSA keys. |
| +// static |
| +const uint16 OwnerKeyUtilTest::kKeySizeInBits = 2048; |
| + |
| +TEST_F(OwnerKeyUtilTest, ExportImportPublicKey) { |
| + scoped_ptr<crypto::RSAPrivateKey> pair( |
| + crypto::RSAPrivateKey::CreateSensitive(kKeySizeInBits)); |
|
pastarmovj
2012/07/30 12:19:19
Just a thought here: generating a new key every ti
Mattias Nissler (ping if slow)
2012/07/31 13:02:15
This is copied from OwnerKeyUtilsTest and has been
|
| + ASSERT_NE(pair.get(), reinterpret_cast<crypto::RSAPrivateKey*>(NULL)); |
| + |
| + // Export public key to file. |
| + ASSERT_TRUE(util_->ExportPublicKey(pair.get())); |
| + |
| + // Export public key, so that we can compare it to the one we get off disk. |
| + std::vector<uint8> public_key; |
| + ASSERT_TRUE(pair->ExportPublicKey(&public_key)); |
| + std::vector<uint8> from_disk; |
| + ASSERT_TRUE(util_->ImportPublicKey(&from_disk)); |
| + |
| + std::vector<uint8>::iterator pubkey_it; |
| + std::vector<uint8>::iterator disk_it; |
| + for (pubkey_it = public_key.begin(), disk_it = from_disk.begin(); |
| + pubkey_it < public_key.end(); |
| + pubkey_it++, disk_it++) { |
| + EXPECT_EQ(*pubkey_it, *disk_it); |
| + } |
|
pastarmovj
2012/07/30 12:19:19
==size + memcmp?
Mattias Nissler (ping if slow)
2012/07/31 13:02:15
Actually, using EXPECT_EQ on the vectors is even b
pastarmovj
2012/08/01 08:46:52
Agreed :)
|
| +} |
| + |
| +TEST_F(OwnerKeyUtilTest, ImportPublicKeyFailed) { |
| + // First test the case where the file is missing which should fail. |
| + std::vector<uint8> from_disk; |
| + ASSERT_FALSE(util_->ImportPublicKey(&from_disk)); |
| + |
| + // Next try empty file. This should fail and the array should be empty. |
| + from_disk.resize(10); |
| + ASSERT_EQ(0, file_util::WriteFile(key_file_, "", 0)); |
| + ASSERT_FALSE(util_->ImportPublicKey(&from_disk)); |
| + ASSERT_FALSE(from_disk.size()); |
| +} |
| + |
| +} // namespace chromeos |