| 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 <string> | 7 #include <string> |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
| 11 #include "base/files/file_path.h" | 11 #include "base/files/file_path.h" |
| 12 #include "base/files/file_util.h" | 12 #include "base/files/file_util.h" |
| 13 #include "base/files/scoped_temp_dir.h" | 13 #include "base/files/scoped_temp_dir.h" |
| 14 #include "base/memory/ref_counted.h" | 14 #include "base/memory/ref_counted.h" |
| 15 #include "base/stl_util.h" | |
| 16 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
| 17 | 16 |
| 18 namespace ownership { | 17 namespace ownership { |
| 19 | 18 |
| 20 // 2048-bit RSA public key for testing. | 19 // 2048-bit RSA public key for testing. |
| 21 const uint8 kTestKeyData[] = { | 20 const uint8 kTestKeyData[] = { |
| 22 0x30, 0x82, 0x01, 0x22, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, | 21 0x30, 0x82, 0x01, 0x22, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, |
| 23 0xf7, 0x0d, 0x01, 0x01, 0x01, 0x05, 0x00, 0x03, 0x82, 0x01, 0x0f, 0x00, | 22 0xf7, 0x0d, 0x01, 0x01, 0x01, 0x05, 0x00, 0x03, 0x82, 0x01, 0x0f, 0x00, |
| 24 0x30, 0x82, 0x01, 0x0a, 0x02, 0x82, 0x01, 0x01, 0x00, 0xe8, 0x39, 0x11, | 23 0x30, 0x82, 0x01, 0x0a, 0x02, 0x82, 0x01, 0x01, 0x00, 0xe8, 0x39, 0x11, |
| 25 0xd0, 0x98, 0x52, 0x4f, 0xf7, 0x18, 0xd1, 0xbf, 0x98, 0x06, 0xae, 0x7a, | 24 0xd0, 0x98, 0x52, 0x4f, 0xf7, 0x18, 0xd1, 0xbf, 0x98, 0x06, 0xae, 0x7a, |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 63 | 62 |
| 64 private: | 63 private: |
| 65 DISALLOW_COPY_AND_ASSIGN(OwnerKeyUtilImplTest); | 64 DISALLOW_COPY_AND_ASSIGN(OwnerKeyUtilImplTest); |
| 66 }; | 65 }; |
| 67 | 66 |
| 68 TEST_F(OwnerKeyUtilImplTest, ImportPublicKey) { | 67 TEST_F(OwnerKeyUtilImplTest, ImportPublicKey) { |
| 69 // Export public key, so that we can compare it to the one we get off disk. | 68 // Export public key, so that we can compare it to the one we get off disk. |
| 70 std::vector<uint8> public_key(kTestKeyData, | 69 std::vector<uint8> public_key(kTestKeyData, |
| 71 kTestKeyData + sizeof(kTestKeyData)); | 70 kTestKeyData + sizeof(kTestKeyData)); |
| 72 ASSERT_EQ(static_cast<int>(public_key.size()), | 71 ASSERT_EQ(static_cast<int>(public_key.size()), |
| 73 base::WriteFile( | 72 base::WriteFile(key_file_, |
| 74 key_file_, | 73 reinterpret_cast<const char*>(public_key.data()), |
| 75 reinterpret_cast<const char*>(vector_as_array(&public_key)), | 74 public_key.size())); |
| 76 public_key.size())); | |
| 77 EXPECT_TRUE(util_->IsPublicKeyPresent()); | 75 EXPECT_TRUE(util_->IsPublicKeyPresent()); |
| 78 | 76 |
| 79 std::vector<uint8> from_disk; | 77 std::vector<uint8> from_disk; |
| 80 EXPECT_TRUE(util_->ImportPublicKey(&from_disk)); | 78 EXPECT_TRUE(util_->ImportPublicKey(&from_disk)); |
| 81 | 79 |
| 82 EXPECT_EQ(public_key, from_disk); | 80 EXPECT_EQ(public_key, from_disk); |
| 83 } | 81 } |
| 84 | 82 |
| 85 TEST_F(OwnerKeyUtilImplTest, ImportPublicKeyFailed) { | 83 TEST_F(OwnerKeyUtilImplTest, ImportPublicKeyFailed) { |
| 86 // First test the case where the file is missing which should fail. | 84 // First test the case where the file is missing which should fail. |
| 87 EXPECT_FALSE(util_->IsPublicKeyPresent()); | 85 EXPECT_FALSE(util_->IsPublicKeyPresent()); |
| 88 std::vector<uint8> from_disk; | 86 std::vector<uint8> from_disk; |
| 89 EXPECT_FALSE(util_->ImportPublicKey(&from_disk)); | 87 EXPECT_FALSE(util_->ImportPublicKey(&from_disk)); |
| 90 | 88 |
| 91 // Next try empty file. This should fail and the array should be empty. | 89 // Next try empty file. This should fail and the array should be empty. |
| 92 from_disk.resize(10); | 90 from_disk.resize(10); |
| 93 ASSERT_EQ(0, base::WriteFile(key_file_, "", 0)); | 91 ASSERT_EQ(0, base::WriteFile(key_file_, "", 0)); |
| 94 EXPECT_TRUE(util_->IsPublicKeyPresent()); | 92 EXPECT_TRUE(util_->IsPublicKeyPresent()); |
| 95 EXPECT_FALSE(util_->ImportPublicKey(&from_disk)); | 93 EXPECT_FALSE(util_->ImportPublicKey(&from_disk)); |
| 96 EXPECT_FALSE(from_disk.size()); | 94 EXPECT_FALSE(from_disk.size()); |
| 97 } | 95 } |
| 98 | 96 |
| 99 } // namespace ownership | 97 } // namespace ownership |
| OLD | NEW |