Revert of Add nacl_loader_unittests to the chromium_builder_tests (https://codereview...
[chromium-blink-merge.git] / crypto / rsa_private_key_nss_unittest.cc
blob66d352a0bd25f2e3914239f5b901ecd270331e05
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.
5 #include "crypto/rsa_private_key.h"
7 #include <keyhi.h>
8 #include <pk11pub.h>
10 #include "base/memory/scoped_ptr.h"
11 #include "crypto/nss_util.h"
12 #include "testing/gtest/include/gtest/gtest.h"
14 namespace crypto {
16 class RSAPrivateKeyNSSTest : public testing::Test {
17 public:
18 RSAPrivateKeyNSSTest() {}
19 virtual ~RSAPrivateKeyNSSTest() {}
21 virtual void SetUp() {
22 #if defined(OS_CHROMEOS)
23 OpenPersistentNSSDB();
24 #endif
27 private:
28 ScopedTestNSSDB test_nssdb_;
30 DISALLOW_COPY_AND_ASSIGN(RSAPrivateKeyNSSTest);
33 TEST_F(RSAPrivateKeyNSSTest, CreateFromKeyTest) {
34 scoped_ptr<crypto::RSAPrivateKey> key_pair(RSAPrivateKey::Create(256));
36 scoped_ptr<crypto::RSAPrivateKey> key_copy(
37 RSAPrivateKey::CreateFromKey(key_pair->key()));
38 ASSERT_TRUE(key_copy.get());
40 std::vector<uint8> privkey;
41 std::vector<uint8> pubkey;
42 ASSERT_TRUE(key_pair->ExportPrivateKey(&privkey));
43 ASSERT_TRUE(key_pair->ExportPublicKey(&pubkey));
45 std::vector<uint8> privkey_copy;
46 std::vector<uint8> pubkey_copy;
47 ASSERT_TRUE(key_copy->ExportPrivateKey(&privkey_copy));
48 ASSERT_TRUE(key_copy->ExportPublicKey(&pubkey_copy));
50 ASSERT_EQ(privkey, privkey_copy);
51 ASSERT_EQ(pubkey, pubkey_copy);
54 TEST_F(RSAPrivateKeyNSSTest, FindFromPublicKey) {
55 // Create a keypair, which will put the keys in the user's NSSDB.
56 scoped_ptr<crypto::RSAPrivateKey> key_pair(RSAPrivateKey::Create(256));
58 std::vector<uint8> public_key;
59 ASSERT_TRUE(key_pair->ExportPublicKey(&public_key));
61 scoped_ptr<crypto::RSAPrivateKey> key_pair_2(
62 crypto::RSAPrivateKey::FindFromPublicKeyInfo(public_key));
64 EXPECT_EQ(key_pair->key_->pkcs11ID, key_pair_2->key_->pkcs11ID);
67 TEST_F(RSAPrivateKeyNSSTest, FailedFindFromPublicKey) {
68 // Create a keypair, which will put the keys in the user's NSSDB.
69 scoped_ptr<crypto::RSAPrivateKey> key_pair(RSAPrivateKey::Create(256));
71 std::vector<uint8> public_key;
72 ASSERT_TRUE(key_pair->ExportPublicKey(&public_key));
74 // Remove the keys from the DB, and make sure we can't find them again.
75 if (key_pair->key_) {
76 PK11_DestroyTokenObject(key_pair->key_->pkcs11Slot,
77 key_pair->key_->pkcs11ID);
79 if (key_pair->public_key_) {
80 PK11_DestroyTokenObject(key_pair->public_key_->pkcs11Slot,
81 key_pair->public_key_->pkcs11ID);
84 EXPECT_EQ(NULL, crypto::RSAPrivateKey::FindFromPublicKeyInfo(public_key));
87 } // namespace crypto