Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(80)

Side by Side Diff: crypto/rsa_private_key_unittest.cc

Issue 1870233002: Convert crypto to std::unique_ptr (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: comment Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « crypto/rsa_private_key_openssl.cc ('k') | crypto/scoped_nss_types.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "crypto/rsa_private_key.h" 5 #include "crypto/rsa_private_key.h"
6 6
7 #include <stdint.h> 7 #include <stdint.h>
8 8
9 #include "base/memory/scoped_ptr.h" 9 #include <memory>
10
10 #include "testing/gtest/include/gtest/gtest.h" 11 #include "testing/gtest/include/gtest/gtest.h"
11 12
12 namespace { 13 namespace {
13 14
14 const uint8_t kTestPrivateKeyInfo[] = { 15 const uint8_t kTestPrivateKeyInfo[] = {
15 0x30, 0x82, 0x02, 0x78, 0x02, 0x01, 0x00, 0x30, 0x0d, 0x06, 0x09, 0x2a, 16 0x30, 0x82, 0x02, 0x78, 0x02, 0x01, 0x00, 0x30, 0x0d, 0x06, 0x09, 0x2a,
16 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01, 0x05, 0x00, 0x04, 0x82, 17 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01, 0x05, 0x00, 0x04, 0x82,
17 0x02, 0x62, 0x30, 0x82, 0x02, 0x5e, 0x02, 0x01, 0x00, 0x02, 0x81, 0x81, 18 0x02, 0x62, 0x30, 0x82, 0x02, 0x5e, 0x02, 0x01, 0x00, 0x02, 0x81, 0x81,
18 0x00, 0xb8, 0x7f, 0x2b, 0x20, 0xdc, 0x7c, 0x9b, 0x0c, 0xdc, 0x51, 0x61, 19 0x00, 0xb8, 0x7f, 0x2b, 0x20, 0xdc, 0x7c, 0x9b, 0x0c, 0xdc, 0x51, 0x61,
19 0x99, 0x0d, 0x36, 0x0f, 0xd4, 0x66, 0x88, 0x08, 0x55, 0x84, 0xd5, 0x3a, 20 0x99, 0x0d, 0x36, 0x0f, 0xd4, 0x66, 0x88, 0x08, 0x55, 0x84, 0xd5, 0x3a,
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
64 0x27, 0x49, 0xb0, 0x99, 0x63, 0xcb, 0x77, 0x5f, 0xa8, 0x82, 0x65, 0xf6, 65 0x27, 0x49, 0xb0, 0x99, 0x63, 0xcb, 0x77, 0x5f, 0xa8, 0x82, 0x65, 0xf6,
65 0x03, 0x52, 0x51, 0xf1, 0xae, 0x2e, 0x05, 0xb3, 0xc6, 0xa4, 0x92, 0xd1, 66 0x03, 0x52, 0x51, 0xf1, 0xae, 0x2e, 0x05, 0xb3, 0xc6, 0xa4, 0x92, 0xd1,
66 0xce, 0x6c, 0x72, 0xfb, 0x21, 0xb3, 0x02, 0x87, 0xe4, 0xfd, 0x61, 0xca, 67 0xce, 0x6c, 0x72, 0xfb, 0x21, 0xb3, 0x02, 0x87, 0xe4, 0xfd, 0x61, 0xca,
67 0x00, 0x42, 0x19, 0xf0, 0xda, 0x5a, 0x53, 0xe3, 0xb1, 0xc5, 0x15, 0xf3}; 68 0x00, 0x42, 0x19, 0xf0, 0xda, 0x5a, 0x53, 0xe3, 0xb1, 0xc5, 0x15, 0xf3};
68 69
69 } // namespace 70 } // namespace
70 71
71 // Generate random private keys with two different sizes. Reimport, then 72 // Generate random private keys with two different sizes. Reimport, then
72 // export them again. We should get back the same exact bytes. 73 // export them again. We should get back the same exact bytes.
73 TEST(RSAPrivateKeyUnitTest, InitRandomTest) { 74 TEST(RSAPrivateKeyUnitTest, InitRandomTest) {
74 scoped_ptr<crypto::RSAPrivateKey> keypair1( 75 std::unique_ptr<crypto::RSAPrivateKey> keypair1(
75 crypto::RSAPrivateKey::Create(1024)); 76 crypto::RSAPrivateKey::Create(1024));
76 scoped_ptr<crypto::RSAPrivateKey> keypair2( 77 std::unique_ptr<crypto::RSAPrivateKey> keypair2(
77 crypto::RSAPrivateKey::Create(2048)); 78 crypto::RSAPrivateKey::Create(2048));
78 ASSERT_TRUE(keypair1.get()); 79 ASSERT_TRUE(keypair1.get());
79 ASSERT_TRUE(keypair2.get()); 80 ASSERT_TRUE(keypair2.get());
80 81
81 std::vector<uint8_t> privkey1; 82 std::vector<uint8_t> privkey1;
82 std::vector<uint8_t> privkey2; 83 std::vector<uint8_t> privkey2;
83 std::vector<uint8_t> pubkey1; 84 std::vector<uint8_t> pubkey1;
84 std::vector<uint8_t> pubkey2; 85 std::vector<uint8_t> pubkey2;
85 86
86 ASSERT_TRUE(keypair1->ExportPrivateKey(&privkey1)); 87 ASSERT_TRUE(keypair1->ExportPrivateKey(&privkey1));
87 ASSERT_TRUE(keypair2->ExportPrivateKey(&privkey2)); 88 ASSERT_TRUE(keypair2->ExportPrivateKey(&privkey2));
88 ASSERT_TRUE(keypair1->ExportPublicKey(&pubkey1)); 89 ASSERT_TRUE(keypair1->ExportPublicKey(&pubkey1));
89 ASSERT_TRUE(keypair2->ExportPublicKey(&pubkey2)); 90 ASSERT_TRUE(keypair2->ExportPublicKey(&pubkey2));
90 91
91 scoped_ptr<crypto::RSAPrivateKey> keypair3( 92 std::unique_ptr<crypto::RSAPrivateKey> keypair3(
92 crypto::RSAPrivateKey::CreateFromPrivateKeyInfo(privkey1)); 93 crypto::RSAPrivateKey::CreateFromPrivateKeyInfo(privkey1));
93 scoped_ptr<crypto::RSAPrivateKey> keypair4( 94 std::unique_ptr<crypto::RSAPrivateKey> keypair4(
94 crypto::RSAPrivateKey::CreateFromPrivateKeyInfo(privkey2)); 95 crypto::RSAPrivateKey::CreateFromPrivateKeyInfo(privkey2));
95 ASSERT_TRUE(keypair3.get()); 96 ASSERT_TRUE(keypair3.get());
96 ASSERT_TRUE(keypair4.get()); 97 ASSERT_TRUE(keypair4.get());
97 98
98 std::vector<uint8_t> privkey3; 99 std::vector<uint8_t> privkey3;
99 std::vector<uint8_t> privkey4; 100 std::vector<uint8_t> privkey4;
100 ASSERT_TRUE(keypair3->ExportPrivateKey(&privkey3)); 101 ASSERT_TRUE(keypair3->ExportPrivateKey(&privkey3));
101 ASSERT_TRUE(keypair4->ExportPrivateKey(&privkey4)); 102 ASSERT_TRUE(keypair4->ExportPrivateKey(&privkey4));
102 103
103 ASSERT_EQ(privkey1.size(), privkey3.size()); 104 ASSERT_EQ(privkey1.size(), privkey3.size());
104 ASSERT_EQ(privkey2.size(), privkey4.size()); 105 ASSERT_EQ(privkey2.size(), privkey4.size());
105 ASSERT_TRUE(0 == memcmp(&privkey1.front(), &privkey3.front(), 106 ASSERT_TRUE(0 == memcmp(&privkey1.front(), &privkey3.front(),
106 privkey1.size())); 107 privkey1.size()));
107 ASSERT_TRUE(0 == memcmp(&privkey2.front(), &privkey4.front(), 108 ASSERT_TRUE(0 == memcmp(&privkey2.front(), &privkey4.front(),
108 privkey2.size())); 109 privkey2.size()));
109 } 110 }
110 111
111 // Test Copy() method. 112 // Test Copy() method.
112 TEST(RSAPrivateKeyUnitTest, CopyTest) { 113 TEST(RSAPrivateKeyUnitTest, CopyTest) {
113 std::vector<uint8_t> input(kTestPrivateKeyInfo, 114 std::vector<uint8_t> input(kTestPrivateKeyInfo,
114 kTestPrivateKeyInfo + sizeof(kTestPrivateKeyInfo)); 115 kTestPrivateKeyInfo + sizeof(kTestPrivateKeyInfo));
115 116
116 scoped_ptr<crypto::RSAPrivateKey> key( 117 std::unique_ptr<crypto::RSAPrivateKey> key(
117 crypto::RSAPrivateKey::CreateFromPrivateKeyInfo(input)); 118 crypto::RSAPrivateKey::CreateFromPrivateKeyInfo(input));
118 119
119 scoped_ptr<crypto::RSAPrivateKey> key_copy(key->Copy()); 120 std::unique_ptr<crypto::RSAPrivateKey> key_copy(key->Copy());
120 ASSERT_TRUE(key_copy.get()); 121 ASSERT_TRUE(key_copy.get());
121 122
122 std::vector<uint8_t> privkey_copy; 123 std::vector<uint8_t> privkey_copy;
123 ASSERT_TRUE(key_copy->ExportPrivateKey(&privkey_copy)); 124 ASSERT_TRUE(key_copy->ExportPrivateKey(&privkey_copy));
124 ASSERT_EQ(input, privkey_copy); 125 ASSERT_EQ(input, privkey_copy);
125 } 126 }
126 127
127 // Test that CreateFromPrivateKeyInfo fails if there is extra data after the RSA 128 // Test that CreateFromPrivateKeyInfo fails if there is extra data after the RSA
128 // key. 129 // key.
129 TEST(RSAPrivateKeyUnitTest, ExtraData) { 130 TEST(RSAPrivateKeyUnitTest, ExtraData) {
130 std::vector<uint8_t> input(kTestPrivateKeyInfo, 131 std::vector<uint8_t> input(kTestPrivateKeyInfo,
131 kTestPrivateKeyInfo + sizeof(kTestPrivateKeyInfo)); 132 kTestPrivateKeyInfo + sizeof(kTestPrivateKeyInfo));
132 input.push_back(0); 133 input.push_back(0);
133 134
134 scoped_ptr<crypto::RSAPrivateKey> key( 135 std::unique_ptr<crypto::RSAPrivateKey> key(
135 crypto::RSAPrivateKey::CreateFromPrivateKeyInfo(input)); 136 crypto::RSAPrivateKey::CreateFromPrivateKeyInfo(input));
136 137
137 // Import should fail. 138 // Import should fail.
138 EXPECT_FALSE(key); 139 EXPECT_FALSE(key);
139 } 140 }
140 141
141 TEST(RSAPrivateKeyUnitTest, NotRsaKey) { 142 TEST(RSAPrivateKeyUnitTest, NotRsaKey) {
142 // Defines a valid P-256 private key. 143 // Defines a valid P-256 private key.
143 const uint8_t kTestEcPrivateKeyInfo[] = { 144 const uint8_t kTestEcPrivateKeyInfo[] = {
144 0x30, 0x81, 0x87, 0x02, 0x01, 0x00, 0x30, 0x13, 0x06, 0x07, 0x2A, 0x86, 145 0x30, 0x81, 0x87, 0x02, 0x01, 0x00, 0x30, 0x13, 0x06, 0x07, 0x2A, 0x86,
145 0x48, 0xCE, 0x3D, 0x02, 0x01, 0x06, 0x08, 0x2A, 0x86, 0x48, 0xCE, 0x3D, 146 0x48, 0xCE, 0x3D, 0x02, 0x01, 0x06, 0x08, 0x2A, 0x86, 0x48, 0xCE, 0x3D,
146 0x03, 0x01, 0x07, 0x04, 0x6D, 0x30, 0x6B, 0x02, 0x01, 0x01, 0x04, 0x20, 147 0x03, 0x01, 0x07, 0x04, 0x6D, 0x30, 0x6B, 0x02, 0x01, 0x01, 0x04, 0x20,
147 0x1F, 0xE3, 0x39, 0x50, 0xC5, 0xF4, 0x61, 0x12, 0x4A, 0xE9, 0x92, 0xC2, 148 0x1F, 0xE3, 0x39, 0x50, 0xC5, 0xF4, 0x61, 0x12, 0x4A, 0xE9, 0x92, 0xC2,
148 0xBD, 0xFD, 0xF1, 0xC7, 0x3B, 0x16, 0x15, 0xF5, 0x71, 0xBD, 0x56, 0x7E, 149 0xBD, 0xFD, 0xF1, 0xC7, 0x3B, 0x16, 0x15, 0xF5, 0x71, 0xBD, 0x56, 0x7E,
149 0x60, 0xD1, 0x9A, 0xA1, 0xF4, 0x8C, 0xDF, 0x42, 0xA1, 0x44, 0x03, 0x42, 150 0x60, 0xD1, 0x9A, 0xA1, 0xF4, 0x8C, 0xDF, 0x42, 0xA1, 0x44, 0x03, 0x42,
150 0x00, 0x04, 0x7C, 0x11, 0x0C, 0x66, 0xDC, 0xFD, 0xA8, 0x07, 0xF6, 0xE6, 151 0x00, 0x04, 0x7C, 0x11, 0x0C, 0x66, 0xDC, 0xFD, 0xA8, 0x07, 0xF6, 0xE6,
151 0x9E, 0x45, 0xDD, 0xB3, 0xC7, 0x4F, 0x69, 0xA1, 0x48, 0x4D, 0x20, 0x3E, 152 0x9E, 0x45, 0xDD, 0xB3, 0xC7, 0x4F, 0x69, 0xA1, 0x48, 0x4D, 0x20, 0x3E,
152 0x8D, 0xC5, 0xAD, 0xA8, 0xE9, 0xA9, 0xDD, 0x7C, 0xB3, 0xC7, 0x0D, 0xF4, 153 0x8D, 0xC5, 0xAD, 0xA8, 0xE9, 0xA9, 0xDD, 0x7C, 0xB3, 0xC7, 0x0D, 0xF4,
153 0x48, 0x98, 0x6E, 0x51, 0xBD, 0xE5, 0xD1, 0x57, 0x6F, 0x99, 0x90, 0x1F, 154 0x48, 0x98, 0x6E, 0x51, 0xBD, 0xE5, 0xD1, 0x57, 0x6F, 0x99, 0x90, 0x1F,
154 0x9C, 0x2C, 0x6A, 0x80, 0x6A, 0x47, 0xFD, 0x90, 0x76, 0x43, 0xA7, 0x2B, 155 0x9C, 0x2C, 0x6A, 0x80, 0x6A, 0x47, 0xFD, 0x90, 0x76, 0x43, 0xA7, 0x2B,
155 0x83, 0x55, 0x97, 0xEF, 0xC8, 0xC6}; 156 0x83, 0x55, 0x97, 0xEF, 0xC8, 0xC6};
156 157
157 std::vector<uint8_t> input( 158 std::vector<uint8_t> input(
158 kTestEcPrivateKeyInfo, 159 kTestEcPrivateKeyInfo,
159 kTestEcPrivateKeyInfo + sizeof(kTestEcPrivateKeyInfo)); 160 kTestEcPrivateKeyInfo + sizeof(kTestEcPrivateKeyInfo));
160 161
161 scoped_ptr<crypto::RSAPrivateKey> key( 162 std::unique_ptr<crypto::RSAPrivateKey> key(
162 crypto::RSAPrivateKey::CreateFromPrivateKeyInfo(input)); 163 crypto::RSAPrivateKey::CreateFromPrivateKeyInfo(input));
163 164
164 // Import should fail as the given PKCS8 bytes were for an EC key not RSA key. 165 // Import should fail as the given PKCS8 bytes were for an EC key not RSA key.
165 EXPECT_FALSE(key); 166 EXPECT_FALSE(key);
166 } 167 }
167 168
168 // Verify that generated public keys look good. This test data was generated 169 // Verify that generated public keys look good. This test data was generated
169 // with the openssl command line tool. 170 // with the openssl command line tool.
170 TEST(RSAPrivateKeyUnitTest, PublicKeyTest) { 171 TEST(RSAPrivateKeyUnitTest, PublicKeyTest) {
171 const uint8_t expected_public_key_info[] = { 172 const uint8_t expected_public_key_info[] = {
172 0x30, 0x81, 0x9f, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 173 0x30, 0x81, 0x9f, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7,
173 0x0d, 0x01, 0x01, 0x01, 0x05, 0x00, 0x03, 0x81, 0x8d, 0x00, 0x30, 0x81, 174 0x0d, 0x01, 0x01, 0x01, 0x05, 0x00, 0x03, 0x81, 0x8d, 0x00, 0x30, 0x81,
174 0x89, 0x02, 0x81, 0x81, 0x00, 0xb8, 0x7f, 0x2b, 0x20, 0xdc, 0x7c, 0x9b, 175 0x89, 0x02, 0x81, 0x81, 0x00, 0xb8, 0x7f, 0x2b, 0x20, 0xdc, 0x7c, 0x9b,
175 0x0c, 0xdc, 0x51, 0x61, 0x99, 0x0d, 0x36, 0x0f, 0xd4, 0x66, 0x88, 0x08, 176 0x0c, 0xdc, 0x51, 0x61, 0x99, 0x0d, 0x36, 0x0f, 0xd4, 0x66, 0x88, 0x08,
176 0x55, 0x84, 0xd5, 0x3a, 0xbf, 0x2b, 0xa4, 0x64, 0x85, 0x7b, 0x0c, 0x04, 177 0x55, 0x84, 0xd5, 0x3a, 0xbf, 0x2b, 0xa4, 0x64, 0x85, 0x7b, 0x0c, 0x04,
177 0x13, 0x3f, 0x8d, 0xf4, 0xbc, 0x38, 0x0d, 0x49, 0xfe, 0x6b, 0xc4, 0x5a, 178 0x13, 0x3f, 0x8d, 0xf4, 0xbc, 0x38, 0x0d, 0x49, 0xfe, 0x6b, 0xc4, 0x5a,
178 0xb0, 0x40, 0x53, 0x3a, 0xd7, 0x66, 0x09, 0x0f, 0x9e, 0x36, 0x74, 0x30, 179 0xb0, 0x40, 0x53, 0x3a, 0xd7, 0x66, 0x09, 0x0f, 0x9e, 0x36, 0x74, 0x30,
179 0xda, 0x8a, 0x31, 0x4f, 0x1f, 0x14, 0x50, 0xd7, 0xc7, 0x20, 0x94, 0x17, 180 0xda, 0x8a, 0x31, 0x4f, 0x1f, 0x14, 0x50, 0xd7, 0xc7, 0x20, 0x94, 0x17,
180 0xde, 0x4e, 0xb9, 0x57, 0x5e, 0x7e, 0x0a, 0xe5, 0xb2, 0x65, 0x7a, 0x89, 181 0xde, 0x4e, 0xb9, 0x57, 0x5e, 0x7e, 0x0a, 0xe5, 0xb2, 0x65, 0x7a, 0x89,
181 0x4e, 0xb6, 0x47, 0xff, 0x1c, 0xbd, 0xb7, 0x38, 0x13, 0xaf, 0x47, 0x85, 182 0x4e, 0xb6, 0x47, 0xff, 0x1c, 0xbd, 0xb7, 0x38, 0x13, 0xaf, 0x47, 0x85,
182 0x84, 0x32, 0x33, 0xf3, 0x17, 0x49, 0xbf, 0xe9, 0x96, 0xd0, 0xd6, 0x14, 183 0x84, 0x32, 0x33, 0xf3, 0x17, 0x49, 0xbf, 0xe9, 0x96, 0xd0, 0xd6, 0x14,
183 0x6f, 0x13, 0x8d, 0xc5, 0xfc, 0x2c, 0x72, 0xba, 0xac, 0xea, 0x7e, 0x18, 184 0x6f, 0x13, 0x8d, 0xc5, 0xfc, 0x2c, 0x72, 0xba, 0xac, 0xea, 0x7e, 0x18,
184 0x53, 0x56, 0xa6, 0x83, 0xa2, 0xce, 0x93, 0x93, 0xe7, 0x1f, 0x0f, 0xe6, 185 0x53, 0x56, 0xa6, 0x83, 0xa2, 0xce, 0x93, 0x93, 0xe7, 0x1f, 0x0f, 0xe6,
185 0x0f, 0x02, 0x03, 0x01, 0x00, 0x01}; 186 0x0f, 0x02, 0x03, 0x01, 0x00, 0x01};
186 187
187 std::vector<uint8_t> input(kTestPrivateKeyInfo, 188 std::vector<uint8_t> input(kTestPrivateKeyInfo,
188 kTestPrivateKeyInfo + sizeof(kTestPrivateKeyInfo)); 189 kTestPrivateKeyInfo + sizeof(kTestPrivateKeyInfo));
189 190
190 scoped_ptr<crypto::RSAPrivateKey> key( 191 std::unique_ptr<crypto::RSAPrivateKey> key(
191 crypto::RSAPrivateKey::CreateFromPrivateKeyInfo(input)); 192 crypto::RSAPrivateKey::CreateFromPrivateKeyInfo(input));
192 ASSERT_TRUE(key.get()); 193 ASSERT_TRUE(key.get());
193 194
194 std::vector<uint8_t> output; 195 std::vector<uint8_t> output;
195 ASSERT_TRUE(key->ExportPublicKey(&output)); 196 ASSERT_TRUE(key->ExportPublicKey(&output));
196 197
197 ASSERT_TRUE( 198 ASSERT_TRUE(
198 memcmp(expected_public_key_info, &output.front(), output.size()) == 0); 199 memcmp(expected_public_key_info, &output.front(), output.size()) == 0);
199 } 200 }
200 201
(...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after
327 std::vector<uint8_t> input2; 328 std::vector<uint8_t> input2;
328 329
329 input1.resize(sizeof(short_integer_with_high_bit)); 330 input1.resize(sizeof(short_integer_with_high_bit));
330 input2.resize(sizeof(short_integer_without_high_bit)); 331 input2.resize(sizeof(short_integer_without_high_bit));
331 332
332 memcpy(&input1.front(), short_integer_with_high_bit, 333 memcpy(&input1.front(), short_integer_with_high_bit,
333 sizeof(short_integer_with_high_bit)); 334 sizeof(short_integer_with_high_bit));
334 memcpy(&input2.front(), short_integer_without_high_bit, 335 memcpy(&input2.front(), short_integer_without_high_bit,
335 sizeof(short_integer_without_high_bit)); 336 sizeof(short_integer_without_high_bit));
336 337
337 scoped_ptr<crypto::RSAPrivateKey> keypair1( 338 std::unique_ptr<crypto::RSAPrivateKey> keypair1(
338 crypto::RSAPrivateKey::CreateFromPrivateKeyInfo(input1)); 339 crypto::RSAPrivateKey::CreateFromPrivateKeyInfo(input1));
339 scoped_ptr<crypto::RSAPrivateKey> keypair2( 340 std::unique_ptr<crypto::RSAPrivateKey> keypair2(
340 crypto::RSAPrivateKey::CreateFromPrivateKeyInfo(input2)); 341 crypto::RSAPrivateKey::CreateFromPrivateKeyInfo(input2));
341 ASSERT_TRUE(keypair1.get()); 342 ASSERT_TRUE(keypair1.get());
342 ASSERT_TRUE(keypair2.get()); 343 ASSERT_TRUE(keypair2.get());
343 344
344 std::vector<uint8_t> output1; 345 std::vector<uint8_t> output1;
345 std::vector<uint8_t> output2; 346 std::vector<uint8_t> output2;
346 ASSERT_TRUE(keypair1->ExportPrivateKey(&output1)); 347 ASSERT_TRUE(keypair1->ExportPrivateKey(&output1));
347 ASSERT_TRUE(keypair2->ExportPrivateKey(&output2)); 348 ASSERT_TRUE(keypair2->ExportPrivateKey(&output2));
348 349
349 ASSERT_EQ(input1.size(), output1.size()); 350 ASSERT_EQ(input1.size(), output1.size());
350 ASSERT_EQ(input2.size(), output2.size()); 351 ASSERT_EQ(input2.size(), output2.size());
351 ASSERT_TRUE(0 == memcmp(&output1.front(), &input1.front(), 352 ASSERT_TRUE(0 == memcmp(&output1.front(), &input1.front(),
352 input1.size())); 353 input1.size()));
353 ASSERT_TRUE(0 == memcmp(&output2.front(), &input2.front(), 354 ASSERT_TRUE(0 == memcmp(&output2.front(), &input2.front(),
354 input2.size())); 355 input2.size()));
355 } 356 }
356 357
357 TEST(RSAPrivateKeyUnitTest, CreateFromKeyTest) { 358 TEST(RSAPrivateKeyUnitTest, CreateFromKeyTest) {
358 scoped_ptr<crypto::RSAPrivateKey> key_pair( 359 std::unique_ptr<crypto::RSAPrivateKey> key_pair(
359 crypto::RSAPrivateKey::Create(512)); 360 crypto::RSAPrivateKey::Create(512));
360 ASSERT_TRUE(key_pair.get()); 361 ASSERT_TRUE(key_pair.get());
361 362
362 scoped_ptr<crypto::RSAPrivateKey> key_copy( 363 std::unique_ptr<crypto::RSAPrivateKey> key_copy(
363 crypto::RSAPrivateKey::CreateFromKey(key_pair->key())); 364 crypto::RSAPrivateKey::CreateFromKey(key_pair->key()));
364 ASSERT_TRUE(key_copy.get()); 365 ASSERT_TRUE(key_copy.get());
365 366
366 std::vector<uint8_t> privkey; 367 std::vector<uint8_t> privkey;
367 std::vector<uint8_t> pubkey; 368 std::vector<uint8_t> pubkey;
368 ASSERT_TRUE(key_pair->ExportPrivateKey(&privkey)); 369 ASSERT_TRUE(key_pair->ExportPrivateKey(&privkey));
369 ASSERT_TRUE(key_pair->ExportPublicKey(&pubkey)); 370 ASSERT_TRUE(key_pair->ExportPublicKey(&pubkey));
370 371
371 std::vector<uint8_t> privkey_copy; 372 std::vector<uint8_t> privkey_copy;
372 std::vector<uint8_t> pubkey_copy; 373 std::vector<uint8_t> pubkey_copy;
373 ASSERT_TRUE(key_copy->ExportPrivateKey(&privkey_copy)); 374 ASSERT_TRUE(key_copy->ExportPrivateKey(&privkey_copy));
374 ASSERT_TRUE(key_copy->ExportPublicKey(&pubkey_copy)); 375 ASSERT_TRUE(key_copy->ExportPublicKey(&pubkey_copy));
375 376
376 ASSERT_EQ(privkey, privkey_copy); 377 ASSERT_EQ(privkey, privkey_copy);
377 ASSERT_EQ(pubkey, pubkey_copy); 378 ASSERT_EQ(pubkey, pubkey_copy);
378 } 379 }
379 380
OLDNEW
« no previous file with comments | « crypto/rsa_private_key_openssl.cc ('k') | crypto/scoped_nss_types.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698