| OLD | NEW |
| (Empty) | |
| 1 // Copyright (c) 2012 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/owner_key_util.h" |
| 6 |
| 7 #include <string> |
| 8 #include <vector> |
| 9 |
| 10 #include "base/file_path.h" |
| 11 #include "base/file_util.h" |
| 12 #include "base/memory/ref_counted.h" |
| 13 #include "base/scoped_temp_dir.h" |
| 14 #include "crypto/nss_util.h" |
| 15 #include "crypto/nss_util_internal.h" |
| 16 #include "crypto/rsa_private_key.h" |
| 17 #include "testing/gmock/include/gmock/gmock.h" |
| 18 #include "testing/gtest/include/gtest/gtest.h" |
| 19 |
| 20 namespace chromeos { |
| 21 |
| 22 class OwnerKeyUtilTest : public ::testing::Test { |
| 23 public: |
| 24 OwnerKeyUtilTest() {} |
| 25 virtual ~OwnerKeyUtilTest() {} |
| 26 |
| 27 virtual void SetUp() { |
| 28 crypto::OpenPersistentNSSDB(); |
| 29 |
| 30 ASSERT_TRUE(tmpdir_.CreateUniqueTempDir()); |
| 31 key_file_ = tmpdir_.path().Append("key"); |
| 32 util_ = new OwnerKeyUtilImpl(key_file_); |
| 33 } |
| 34 |
| 35 // Key generation parameters. |
| 36 static const uint16 kKeySizeInBits; |
| 37 |
| 38 ScopedTempDir tmpdir_; |
| 39 FilePath key_file_; |
| 40 scoped_refptr<OwnerKeyUtil> util_; |
| 41 }; |
| 42 |
| 43 // We're generating and using 2048-bit RSA keys. |
| 44 // static |
| 45 const uint16 OwnerKeyUtilTest::kKeySizeInBits = 2048; |
| 46 |
| 47 TEST_F(OwnerKeyUtilTest, ExportImportPublicKey) { |
| 48 scoped_ptr<crypto::RSAPrivateKey> pair( |
| 49 crypto::RSAPrivateKey::CreateSensitive(kKeySizeInBits)); |
| 50 ASSERT_NE(pair.get(), reinterpret_cast<crypto::RSAPrivateKey*>(NULL)); |
| 51 |
| 52 // Export public key to file. |
| 53 ASSERT_TRUE(util_->ExportPublicKey(pair.get())); |
| 54 |
| 55 // Export public key, so that we can compare it to the one we get off disk. |
| 56 std::vector<uint8> public_key; |
| 57 ASSERT_TRUE(pair->ExportPublicKey(&public_key)); |
| 58 std::vector<uint8> from_disk; |
| 59 ASSERT_TRUE(util_->ImportPublicKey(&from_disk)); |
| 60 |
| 61 std::vector<uint8>::iterator pubkey_it; |
| 62 std::vector<uint8>::iterator disk_it; |
| 63 for (pubkey_it = public_key.begin(), disk_it = from_disk.begin(); |
| 64 pubkey_it < public_key.end(); |
| 65 pubkey_it++, disk_it++) { |
| 66 EXPECT_EQ(*pubkey_it, *disk_it); |
| 67 } |
| 68 } |
| 69 |
| 70 TEST_F(OwnerKeyUtilTest, ImportPublicKeyFailed) { |
| 71 // First test the case where the file is missing which should fail. |
| 72 std::vector<uint8> from_disk; |
| 73 ASSERT_FALSE(util_->ImportPublicKey(&from_disk)); |
| 74 |
| 75 // Next try empty file. This should fail and the array should be empty. |
| 76 from_disk.resize(10); |
| 77 ASSERT_EQ(0, file_util::WriteFile(key_file_, "", 0)); |
| 78 ASSERT_FALSE(util_->ImportPublicKey(&from_disk)); |
| 79 ASSERT_FALSE(from_disk.size()); |
| 80 } |
| 81 |
| 82 } // namespace chromeos |
| OLD | NEW |