1 // Copyright 2017 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "components/crx_file/crx_creator.h"
6 #include "base/base64.h"
7 #include "base/base_paths.h"
8 #include "base/files/file_path.h"
9 #include "base/files/file_util.h"
10 #include "base/path_service.h"
11 #include "components/crx_file/crx_verifier.h"
12 #include "crypto/rsa_private_key.h"
13 #include "testing/gtest/include/gtest/gtest.h"
17 base::FilePath TestFile(const std::string& file) {
19 base::PathService::Get(base::DIR_SRC_TEST_DATA_ROOT, &path);
20 return path.AppendASCII("components")
23 .AppendASCII("crx_file")
27 // Gzip compression of UTF-8 encoded string `sample_verified_contents`.
28 constexpr char kTestCompressedVerifiedContents[] =
29 "\x1f\x8b\x08\x00\xbb\xf7\x1a`\x02\xff+N\xcc-\xc8I\x8d/"
30 "K-\xcaL\xcbLM\x89O\xce\xcf+I\xcd+)"
31 "\x06\x00\x8a\x10\xc9\x01\x18\x00\x00\x00";
37 using CrxCreatorTest = testing::Test;
39 TEST_F(CrxCreatorTest, Create) {
40 // Set up a signing key.
41 auto signing_key = crypto::RSAPrivateKey::Create(4096);
42 std::vector<uint8_t> public_key;
43 signing_key->ExportPublicKey(&public_key);
44 std::string expected_public_key;
45 base::Base64Encode(std::string(public_key.begin(), public_key.end()),
46 &expected_public_key);
49 base::FilePath temp_file;
50 EXPECT_TRUE(base::CreateTemporaryFile(&temp_file));
51 EXPECT_EQ(CreatorResult::OK,
52 Create(temp_file, TestFile("sample.zip"), signing_key.get()));
54 // Test that the created file can be verified.
55 const std::vector<std::vector<uint8_t>> keys;
56 const std::vector<uint8_t> hash;
57 std::string public_key_in_crx;
59 VerifierResult::OK_FULL,
60 Verify(temp_file, VerifierFormat::CRX3, keys, hash, &public_key_in_crx,
61 nullptr, /*compressed_verified_contents=*/nullptr));
62 EXPECT_EQ(expected_public_key, public_key_in_crx);
65 EXPECT_TRUE(base::DeleteFile(temp_file));
68 TEST_F(CrxCreatorTest, VerifyCrxWithVerifiedContents) {
69 // Set up a signing key.
70 auto signing_key = crypto::RSAPrivateKey::Create(4096);
71 std::vector<uint8_t> public_key;
72 signing_key->ExportPublicKey(&public_key);
73 std::string expected_public_key;
74 base::Base64Encode(std::string(public_key.begin(), public_key.end()),
75 &expected_public_key);
78 base::FilePath temp_file;
79 EXPECT_TRUE(base::CreateTemporaryFile(&temp_file));
80 std::string test_compressed_verified_contents(
81 kTestCompressedVerifiedContents);
82 EXPECT_EQ(CreatorResult::OK,
83 CreateCrxWithVerifiedContentsInHeader(
84 temp_file, TestFile("sample.zip"), signing_key.get(),
85 test_compressed_verified_contents));
87 // Test that the created file can be verified.
88 const std::vector<std::vector<uint8_t>> keys;
89 const std::vector<uint8_t> hash;
90 std::string public_key_in_crx;
91 std::vector<uint8_t> compressed_verified_contents;
92 EXPECT_EQ(VerifierResult::OK_FULL,
93 Verify(temp_file, VerifierFormat::CRX3, keys, hash,
94 &public_key_in_crx, nullptr, &compressed_verified_contents));
95 EXPECT_EQ(expected_public_key, public_key_in_crx);
96 std::vector<uint8_t> expected_verified_contents;
97 expected_verified_contents.assign(test_compressed_verified_contents.begin(),
98 test_compressed_verified_contents.end());
99 EXPECT_EQ(compressed_verified_contents, expected_verified_contents);
102 EXPECT_TRUE(base::DeleteFile(temp_file));
105 } // namespace crx_file