crx_creator_unittest.cc 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. // Copyright 2017 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/crx_file/crx_creator.h"
  5. #include "base/base64.h"
  6. #include "base/base_paths.h"
  7. #include "base/files/file_path.h"
  8. #include "base/files/file_util.h"
  9. #include "base/path_service.h"
  10. #include "components/crx_file/crx_verifier.h"
  11. #include "crypto/rsa_private_key.h"
  12. #include "testing/gtest/include/gtest/gtest.h"
  13. namespace {
  14. base::FilePath TestFile(const std::string& file) {
  15. base::FilePath path;
  16. base::PathService::Get(base::DIR_SOURCE_ROOT, &path);
  17. return path.AppendASCII("components")
  18. .AppendASCII("test")
  19. .AppendASCII("data")
  20. .AppendASCII("crx_file")
  21. .AppendASCII(file);
  22. }
  23. // Gzip compression of UTF-8 encoded string `sample_verified_contents`.
  24. constexpr char kTestCompressedVerifiedContents[] =
  25. "\x1f\x8b\x08\x00\xbb\xf7\x1a`\x02\xff+N\xcc-\xc8I\x8d/"
  26. "K-\xcaL\xcbLM\x89O\xce\xcf+I\xcd+)"
  27. "\x06\x00\x8a\x10\xc9\x01\x18\x00\x00\x00";
  28. } // namespace
  29. namespace crx_file {
  30. using CrxCreatorTest = testing::Test;
  31. TEST_F(CrxCreatorTest, Create) {
  32. // Set up a signing key.
  33. auto signing_key = crypto::RSAPrivateKey::Create(4096);
  34. std::vector<uint8_t> public_key;
  35. signing_key->ExportPublicKey(&public_key);
  36. std::string expected_public_key;
  37. base::Base64Encode(std::string(public_key.begin(), public_key.end()),
  38. &expected_public_key);
  39. // Create a CRX File.
  40. base::FilePath temp_file;
  41. EXPECT_TRUE(base::CreateTemporaryFile(&temp_file));
  42. EXPECT_EQ(CreatorResult::OK,
  43. Create(temp_file, TestFile("sample.zip"), signing_key.get()));
  44. // Test that the created file can be verified.
  45. const std::vector<std::vector<uint8_t>> keys;
  46. const std::vector<uint8_t> hash;
  47. std::string public_key_in_crx;
  48. EXPECT_EQ(
  49. VerifierResult::OK_FULL,
  50. Verify(temp_file, VerifierFormat::CRX3, keys, hash, &public_key_in_crx,
  51. nullptr, /*compressed_verified_contents=*/nullptr));
  52. EXPECT_EQ(expected_public_key, public_key_in_crx);
  53. // Delete the file.
  54. EXPECT_TRUE(base::DeleteFile(temp_file));
  55. }
  56. TEST_F(CrxCreatorTest, VerifyCrxWithVerifiedContents) {
  57. // Set up a signing key.
  58. auto signing_key = crypto::RSAPrivateKey::Create(4096);
  59. std::vector<uint8_t> public_key;
  60. signing_key->ExportPublicKey(&public_key);
  61. std::string expected_public_key;
  62. base::Base64Encode(std::string(public_key.begin(), public_key.end()),
  63. &expected_public_key);
  64. // Create a CRX File.
  65. base::FilePath temp_file;
  66. EXPECT_TRUE(base::CreateTemporaryFile(&temp_file));
  67. std::string test_compressed_verified_contents(
  68. kTestCompressedVerifiedContents);
  69. EXPECT_EQ(CreatorResult::OK,
  70. CreateCrxWithVerifiedContentsInHeader(
  71. temp_file, TestFile("sample.zip"), signing_key.get(),
  72. test_compressed_verified_contents));
  73. // Test that the created file can be verified.
  74. const std::vector<std::vector<uint8_t>> keys;
  75. const std::vector<uint8_t> hash;
  76. std::string public_key_in_crx;
  77. std::vector<uint8_t> compressed_verified_contents;
  78. EXPECT_EQ(VerifierResult::OK_FULL,
  79. Verify(temp_file, VerifierFormat::CRX3, keys, hash,
  80. &public_key_in_crx, nullptr, &compressed_verified_contents));
  81. EXPECT_EQ(expected_public_key, public_key_in_crx);
  82. std::vector<uint8_t> expected_verified_contents;
  83. expected_verified_contents.assign(test_compressed_verified_contents.begin(),
  84. test_compressed_verified_contents.end());
  85. EXPECT_EQ(compressed_verified_contents, expected_verified_contents);
  86. // Delete the file.
  87. EXPECT_TRUE(base::DeleteFile(temp_file));
  88. }
  89. } // namespace crx_file