OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 "components/ownership/owner_key_util_impl.h" | 5 #include "components/ownership/owner_key_util_impl.h" |
6 | 6 |
| 7 #include <stdint.h> |
| 8 |
7 #include <string> | 9 #include <string> |
8 #include <vector> | 10 #include <vector> |
9 | 11 |
10 #include "base/basictypes.h" | |
11 #include "base/files/file_path.h" | 12 #include "base/files/file_path.h" |
12 #include "base/files/file_util.h" | 13 #include "base/files/file_util.h" |
13 #include "base/files/scoped_temp_dir.h" | 14 #include "base/files/scoped_temp_dir.h" |
| 15 #include "base/macros.h" |
14 #include "base/memory/ref_counted.h" | 16 #include "base/memory/ref_counted.h" |
15 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
16 | 18 |
17 namespace ownership { | 19 namespace ownership { |
18 | 20 |
19 // 2048-bit RSA public key for testing. | 21 // 2048-bit RSA public key for testing. |
20 const uint8 kTestKeyData[] = { | 22 const uint8_t kTestKeyData[] = { |
21 0x30, 0x82, 0x01, 0x22, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, | 23 0x30, 0x82, 0x01, 0x22, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, |
22 0xf7, 0x0d, 0x01, 0x01, 0x01, 0x05, 0x00, 0x03, 0x82, 0x01, 0x0f, 0x00, | 24 0xf7, 0x0d, 0x01, 0x01, 0x01, 0x05, 0x00, 0x03, 0x82, 0x01, 0x0f, 0x00, |
23 0x30, 0x82, 0x01, 0x0a, 0x02, 0x82, 0x01, 0x01, 0x00, 0xe8, 0x39, 0x11, | 25 0x30, 0x82, 0x01, 0x0a, 0x02, 0x82, 0x01, 0x01, 0x00, 0xe8, 0x39, 0x11, |
24 0xd0, 0x98, 0x52, 0x4f, 0xf7, 0x18, 0xd1, 0xbf, 0x98, 0x06, 0xae, 0x7a, | 26 0xd0, 0x98, 0x52, 0x4f, 0xf7, 0x18, 0xd1, 0xbf, 0x98, 0x06, 0xae, 0x7a, |
25 0x7c, 0xd7, 0x6f, 0x02, 0x54, 0x37, 0x4e, 0xcd, 0xa6, 0x27, 0x8e, 0xf7, | 27 0x7c, 0xd7, 0x6f, 0x02, 0x54, 0x37, 0x4e, 0xcd, 0xa6, 0x27, 0x8e, 0xf7, |
26 0x82, 0x1d, 0xde, 0x3d, 0xf5, 0x6b, 0xa4, 0xe5, 0x6b, 0x0c, 0xf0, 0x39, | 28 0x82, 0x1d, 0xde, 0x3d, 0xf5, 0x6b, 0xa4, 0xe5, 0x6b, 0x0c, 0xf0, 0x39, |
27 0xe5, 0xd9, 0x53, 0xe7, 0x6b, 0x6d, 0xa1, 0xc7, 0xdf, 0x92, 0xb7, 0xb0, | 29 0xe5, 0xd9, 0x53, 0xe7, 0x6b, 0x6d, 0xa1, 0xc7, 0xdf, 0x92, 0xb7, 0xb0, |
28 0x14, 0x4d, 0x4e, 0x7d, 0x27, 0xd4, 0xf7, 0x35, 0xae, 0xc7, 0x3d, 0x13, | 30 0x14, 0x4d, 0x4e, 0x7d, 0x27, 0xd4, 0xf7, 0x35, 0xae, 0xc7, 0x3d, 0x13, |
29 0x74, 0x23, 0x8c, 0xda, 0xf1, 0x94, 0xbb, 0x2e, 0x06, 0x0d, 0x55, 0xe2, | 31 0x74, 0x23, 0x8c, 0xda, 0xf1, 0x94, 0xbb, 0x2e, 0x06, 0x0d, 0x55, 0xe2, |
30 0x29, 0xf1, 0xfb, 0x4a, 0x2a, 0xb2, 0x40, 0x62, 0x59, 0x21, 0x39, 0xf9, | 32 0x29, 0xf1, 0xfb, 0x4a, 0x2a, 0xb2, 0x40, 0x62, 0x59, 0x21, 0x39, 0xf9, |
(...skipping 28 matching lines...) Expand all Loading... |
59 base::ScopedTempDir tmpdir_; | 61 base::ScopedTempDir tmpdir_; |
60 base::FilePath key_file_; | 62 base::FilePath key_file_; |
61 scoped_refptr<OwnerKeyUtil> util_; | 63 scoped_refptr<OwnerKeyUtil> util_; |
62 | 64 |
63 private: | 65 private: |
64 DISALLOW_COPY_AND_ASSIGN(OwnerKeyUtilImplTest); | 66 DISALLOW_COPY_AND_ASSIGN(OwnerKeyUtilImplTest); |
65 }; | 67 }; |
66 | 68 |
67 TEST_F(OwnerKeyUtilImplTest, ImportPublicKey) { | 69 TEST_F(OwnerKeyUtilImplTest, ImportPublicKey) { |
68 // Export public key, so that we can compare it to the one we get off disk. | 70 // Export public key, so that we can compare it to the one we get off disk. |
69 std::vector<uint8> public_key(kTestKeyData, | 71 std::vector<uint8_t> public_key(kTestKeyData, |
70 kTestKeyData + sizeof(kTestKeyData)); | 72 kTestKeyData + sizeof(kTestKeyData)); |
71 ASSERT_EQ(static_cast<int>(public_key.size()), | 73 ASSERT_EQ(static_cast<int>(public_key.size()), |
72 base::WriteFile(key_file_, | 74 base::WriteFile(key_file_, |
73 reinterpret_cast<const char*>(public_key.data()), | 75 reinterpret_cast<const char*>(public_key.data()), |
74 public_key.size())); | 76 public_key.size())); |
75 EXPECT_TRUE(util_->IsPublicKeyPresent()); | 77 EXPECT_TRUE(util_->IsPublicKeyPresent()); |
76 | 78 |
77 std::vector<uint8> from_disk; | 79 std::vector<uint8_t> from_disk; |
78 EXPECT_TRUE(util_->ImportPublicKey(&from_disk)); | 80 EXPECT_TRUE(util_->ImportPublicKey(&from_disk)); |
79 | 81 |
80 EXPECT_EQ(public_key, from_disk); | 82 EXPECT_EQ(public_key, from_disk); |
81 } | 83 } |
82 | 84 |
83 TEST_F(OwnerKeyUtilImplTest, ImportPublicKeyFailed) { | 85 TEST_F(OwnerKeyUtilImplTest, ImportPublicKeyFailed) { |
84 // First test the case where the file is missing which should fail. | 86 // First test the case where the file is missing which should fail. |
85 EXPECT_FALSE(util_->IsPublicKeyPresent()); | 87 EXPECT_FALSE(util_->IsPublicKeyPresent()); |
86 std::vector<uint8> from_disk; | 88 std::vector<uint8_t> from_disk; |
87 EXPECT_FALSE(util_->ImportPublicKey(&from_disk)); | 89 EXPECT_FALSE(util_->ImportPublicKey(&from_disk)); |
88 | 90 |
89 // Next try empty file. This should fail and the array should be empty. | 91 // Next try empty file. This should fail and the array should be empty. |
90 from_disk.resize(10); | 92 from_disk.resize(10); |
91 ASSERT_EQ(0, base::WriteFile(key_file_, "", 0)); | 93 ASSERT_EQ(0, base::WriteFile(key_file_, "", 0)); |
92 EXPECT_TRUE(util_->IsPublicKeyPresent()); | 94 EXPECT_TRUE(util_->IsPublicKeyPresent()); |
93 EXPECT_FALSE(util_->ImportPublicKey(&from_disk)); | 95 EXPECT_FALSE(util_->ImportPublicKey(&from_disk)); |
94 EXPECT_FALSE(from_disk.size()); | 96 EXPECT_FALSE(from_disk.size()); |
95 } | 97 } |
96 | 98 |
97 } // namespace ownership | 99 } // namespace ownership |
OLD | NEW |