blob: e6f3475f22f60b062bfc431841c1d37c69880e77 [file] [log] [blame]
[email protected]28ae8fe2009-06-05 18:25:061// Copyright (c) 2009 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 "base/file_util.h"
6#include "base/crypto/rsa_private_key.h"
7#include "base/scoped_ptr.h"
8#include "testing/gtest/include/gtest/gtest.h"
9
10TEST(RSAPrivateKeyUnitTest, InitRandomTest) {
11 // Generate random private keys with two different sizes. Reimport, then
12 // export them again. We should get back the same exact bytes.
13 scoped_ptr<base::RSAPrivateKey> keypair1(base::RSAPrivateKey::Create(1024));
14 scoped_ptr<base::RSAPrivateKey> keypair2(base::RSAPrivateKey::Create(2048));
15 ASSERT_TRUE(keypair1.get());
16 ASSERT_TRUE(keypair2.get());
17
18 std::vector<uint8> privkey1;
19 std::vector<uint8> privkey2;
20 std::vector<uint8> pubkey1;
21 std::vector<uint8> pubkey2;
22
23 ASSERT_TRUE(keypair1->ExportPrivateKey(&privkey1));
24 ASSERT_TRUE(keypair2->ExportPrivateKey(&privkey2));
25 ASSERT_TRUE(keypair1->ExportPublicKey(&pubkey1));
26 ASSERT_TRUE(keypair2->ExportPublicKey(&pubkey2));
27
28 scoped_ptr<base::RSAPrivateKey> keypair3(
29 base::RSAPrivateKey::CreateFromPrivateKeyInfo(privkey1));
30 scoped_ptr<base::RSAPrivateKey> keypair4(
31 base::RSAPrivateKey::CreateFromPrivateKeyInfo(privkey2));
32 ASSERT_TRUE(keypair3.get());
33 ASSERT_TRUE(keypair4.get());
34
35 std::vector<uint8> privkey3;
36 std::vector<uint8> privkey4;
37 ASSERT_TRUE(keypair3->ExportPrivateKey(&privkey3));
38 ASSERT_TRUE(keypair4->ExportPrivateKey(&privkey4));
39
40 ASSERT_EQ(privkey1.size(), privkey3.size());
41 ASSERT_EQ(privkey2.size(), privkey4.size());
42 ASSERT_TRUE(0 == memcmp(&privkey1.front(), &privkey3.front(),
43 privkey1.size()));
44 ASSERT_TRUE(0 == memcmp(&privkey2.front(), &privkey4.front(),
45 privkey2.size()));
46}
47
48// TODO(aa): Consider importing some private keys from other software.