owner_key_util_impl_unittest.cc 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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
  3. // found in the LICENSE file.
  4. #include "components/ownership/owner_key_util_impl.h"
  5. #include <stdint.h>
  6. #include <string>
  7. #include <vector>
  8. #include "base/files/file_path.h"
  9. #include "base/files/file_util.h"
  10. #include "base/files/scoped_temp_dir.h"
  11. #include "base/memory/ref_counted.h"
  12. #include "testing/gtest/include/gtest/gtest.h"
  13. namespace ownership {
  14. // 2048-bit RSA public key for testing.
  15. const uint8_t kTestKeyData[] = {
  16. 0x30, 0x82, 0x01, 0x22, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86,
  17. 0xf7, 0x0d, 0x01, 0x01, 0x01, 0x05, 0x00, 0x03, 0x82, 0x01, 0x0f, 0x00,
  18. 0x30, 0x82, 0x01, 0x0a, 0x02, 0x82, 0x01, 0x01, 0x00, 0xe8, 0x39, 0x11,
  19. 0xd0, 0x98, 0x52, 0x4f, 0xf7, 0x18, 0xd1, 0xbf, 0x98, 0x06, 0xae, 0x7a,
  20. 0x7c, 0xd7, 0x6f, 0x02, 0x54, 0x37, 0x4e, 0xcd, 0xa6, 0x27, 0x8e, 0xf7,
  21. 0x82, 0x1d, 0xde, 0x3d, 0xf5, 0x6b, 0xa4, 0xe5, 0x6b, 0x0c, 0xf0, 0x39,
  22. 0xe5, 0xd9, 0x53, 0xe7, 0x6b, 0x6d, 0xa1, 0xc7, 0xdf, 0x92, 0xb7, 0xb0,
  23. 0x14, 0x4d, 0x4e, 0x7d, 0x27, 0xd4, 0xf7, 0x35, 0xae, 0xc7, 0x3d, 0x13,
  24. 0x74, 0x23, 0x8c, 0xda, 0xf1, 0x94, 0xbb, 0x2e, 0x06, 0x0d, 0x55, 0xe2,
  25. 0x29, 0xf1, 0xfb, 0x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0xda, 0x0f, 0xfa,
  40. 0x41, 0x02, 0x03, 0x01, 0x00, 0x01,
  41. };
  42. class OwnerKeyUtilImplTest : public testing::Test {
  43. public:
  44. OwnerKeyUtilImplTest(const OwnerKeyUtilImplTest&) = delete;
  45. OwnerKeyUtilImplTest& operator=(const OwnerKeyUtilImplTest&) = delete;
  46. protected:
  47. OwnerKeyUtilImplTest() {}
  48. ~OwnerKeyUtilImplTest() override {}
  49. void SetUp() override {
  50. ASSERT_TRUE(tmpdir_.CreateUniqueTempDir());
  51. key_file_ = tmpdir_.GetPath().Append(FILE_PATH_LITERAL("key"));
  52. util_ = new OwnerKeyUtilImpl(key_file_);
  53. }
  54. base::ScopedTempDir tmpdir_;
  55. base::FilePath key_file_;
  56. scoped_refptr<OwnerKeyUtil> util_;
  57. };
  58. TEST_F(OwnerKeyUtilImplTest, ImportPublicKey) {
  59. // Export public key, so that we can compare it to the one we get off disk.
  60. std::vector<uint8_t> public_key(kTestKeyData,
  61. kTestKeyData + sizeof(kTestKeyData));
  62. ASSERT_EQ(static_cast<int>(public_key.size()),
  63. base::WriteFile(key_file_,
  64. reinterpret_cast<const char*>(public_key.data()),
  65. public_key.size()));
  66. EXPECT_TRUE(util_->IsPublicKeyPresent());
  67. std::vector<uint8_t> from_disk;
  68. EXPECT_TRUE(util_->ImportPublicKey(&from_disk));
  69. EXPECT_EQ(public_key, from_disk);
  70. }
  71. TEST_F(OwnerKeyUtilImplTest, ImportPublicKeyFailed) {
  72. // First test the case where the file is missing which should fail.
  73. EXPECT_FALSE(util_->IsPublicKeyPresent());
  74. std::vector<uint8_t> from_disk;
  75. EXPECT_FALSE(util_->ImportPublicKey(&from_disk));
  76. // Next try empty file. This should fail and the array should be empty.
  77. from_disk.resize(10);
  78. ASSERT_EQ(0, base::WriteFile(key_file_, "", 0));
  79. EXPECT_TRUE(util_->IsPublicKeyPresent());
  80. EXPECT_FALSE(util_->ImportPublicKey(&from_disk));
  81. EXPECT_FALSE(from_disk.size());
  82. }
  83. } // namespace ownership