diff options
Diffstat (limited to 'chrome/browser/chromeos/settings/owner_key_utils_unittest.cc')
-rw-r--r-- | chrome/browser/chromeos/settings/owner_key_utils_unittest.cc | 84 |
1 files changed, 84 insertions, 0 deletions
diff --git a/chrome/browser/chromeos/settings/owner_key_utils_unittest.cc b/chrome/browser/chromeos/settings/owner_key_utils_unittest.cc new file mode 100644 index 0000000..0632b6b --- /dev/null +++ b/chrome/browser/chromeos/settings/owner_key_utils_unittest.cc @@ -0,0 +1,84 @@ +// 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/settings/owner_key_utils.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 OwnerKeyUtilsTest : public ::testing::Test { + public: + OwnerKeyUtilsTest() : utils_(OwnerKeyUtils::Create()) {} + virtual ~OwnerKeyUtilsTest() {} + + virtual void SetUp() { + crypto::OpenPersistentNSSDB(); + } + + // Key generation parameters. + static const uint16 kKeySizeInBits; + + scoped_refptr<OwnerKeyUtils> utils_; +}; + +// We're generating and using 2048-bit RSA keys. +// static +const uint16 OwnerKeyUtilsTest::kKeySizeInBits = 2048; + +TEST_F(OwnerKeyUtilsTest, ExportImportPublicKey) { + scoped_ptr<crypto::RSAPrivateKey> pair( + crypto::RSAPrivateKey::CreateSensitive(kKeySizeInBits)); + ASSERT_NE(pair.get(), reinterpret_cast<crypto::RSAPrivateKey*>(NULL)); + + // Export public key to file. + ScopedTempDir tmpdir; + FilePath tmpfile; + ASSERT_TRUE(tmpdir.CreateUniqueTempDir()); + ASSERT_TRUE(file_util::CreateTemporaryFileInDir(tmpdir.path(), &tmpfile)); + ASSERT_TRUE(utils_->ExportPublicKeyToFile(pair.get(), tmpfile)); + + // 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(utils_->ImportPublicKey(tmpfile, &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); + } +} + +TEST_F(OwnerKeyUtilsTest, ImportPublicKeyFailed) { + ScopedTempDir tmpdir; + FilePath tmpfile; + ASSERT_TRUE(tmpdir.CreateUniqueTempDir()); + + // First test the case where the file is missing which should fail. + std::vector<uint8> from_disk; + ASSERT_FALSE(utils_->ImportPublicKey(tmpfile, &from_disk)); + + // Next try empty file. This should fail and the array should be empty. + from_disk.resize(10); + ASSERT_TRUE(file_util::CreateTemporaryFileInDir(tmpdir.path(), &tmpfile)); + ASSERT_FALSE(utils_->ImportPublicKey(tmpfile, &from_disk)); + ASSERT_FALSE(from_disk.size()); +} + +} // namespace chromeos |