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
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"
14
15 namespace {
16
TestFile(const std::string & file)17 base::FilePath TestFile(const std::string& file) {
18 base::FilePath path;
19 base::PathService::Get(base::DIR_SOURCE_ROOT, &path);
20 return path.AppendASCII("components")
21 .AppendASCII("test")
22 .AppendASCII("data")
23 .AppendASCII("crx_file")
24 .AppendASCII(file);
25 }
26
27 } // namespace
28
29 namespace crx_file {
30
31 using CrxCreatorTest = testing::Test;
32
TEST_F(CrxCreatorTest,Create)33 TEST_F(CrxCreatorTest, Create) {
34 // Set up a signing key.
35 auto signing_key = crypto::RSAPrivateKey::Create(4096);
36 std::vector<uint8_t> public_key;
37 signing_key->ExportPublicKey(&public_key);
38 std::string expected_public_key;
39 base::Base64Encode(std::string(public_key.begin(), public_key.end()),
40 &expected_public_key);
41
42 // Create a CRX File.
43 base::FilePath temp_file;
44 EXPECT_TRUE(base::CreateTemporaryFile(&temp_file));
45 EXPECT_EQ(CreatorResult::OK,
46 Create(temp_file, TestFile("sample.zip"), signing_key.get()));
47
48 // Test that the created file can be verified.
49 const std::vector<std::vector<uint8_t>> keys;
50 const std::vector<uint8_t> hash;
51 std::string public_key_in_crx;
52 EXPECT_EQ(VerifierResult::OK_FULL,
53 Verify(temp_file, VerifierFormat::CRX3, keys, hash,
54 &public_key_in_crx, nullptr));
55 EXPECT_EQ(expected_public_key, public_key_in_crx);
56
57 // Delete the file.
58 EXPECT_TRUE(base::DeleteFile(temp_file));
59 }
60
61 } // namespace crx_file
62