File: crx_creator_unittest.cc

package info (click to toggle)
chromium 138.0.7204.157-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 6,071,864 kB
  • sloc: cpp: 34,936,859; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,967; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (101 lines) | stat: -rw-r--r-- 3,614 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
// Copyright 2017 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/crx_file/crx_creator.h"
#include "base/base64.h"
#include "base/base_paths.h"
#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/path_service.h"
#include "components/crx_file/crx_verifier.h"
#include "crypto/rsa_private_key.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace {

base::FilePath TestFile(const std::string& file) {
  base::FilePath path;
  base::PathService::Get(base::DIR_SRC_TEST_DATA_ROOT, &path);
  return path.AppendASCII("components")
      .AppendASCII("test")
      .AppendASCII("data")
      .AppendASCII("crx_file")
      .AppendASCII(file);
}

// Gzip compression of UTF-8 encoded string `sample_verified_contents`.
constexpr char kTestCompressedVerifiedContents[] =
    "\x1f\x8b\x08\x00\xbb\xf7\x1a`\x02\xff+N\xcc-\xc8I\x8d/"
    "K-\xcaL\xcbLM\x89O\xce\xcf+I\xcd+)"
    "\x06\x00\x8a\x10\xc9\x01\x18\x00\x00\x00";

}  // namespace

namespace crx_file {

using CrxCreatorTest = testing::Test;

TEST_F(CrxCreatorTest, Create) {
  // Set up a signing key.
  auto signing_key = crypto::RSAPrivateKey::Create(4096);
  std::vector<uint8_t> public_key;
  signing_key->ExportPublicKey(&public_key);
  const std::string expected_public_key = base::Base64Encode(public_key);

  // Create a CRX File.
  base::FilePath temp_file;
  EXPECT_TRUE(base::CreateTemporaryFile(&temp_file));
  EXPECT_EQ(CreatorResult::OK,
            Create(temp_file, TestFile("sample.zip"), signing_key.get()));

  // Test that the created file can be verified.
  const std::vector<std::vector<uint8_t>> keys;
  const std::vector<uint8_t> hash;
  std::string public_key_in_crx;
  EXPECT_EQ(
      VerifierResult::OK_FULL,
      Verify(temp_file, VerifierFormat::CRX3, keys, hash, &public_key_in_crx,
             nullptr, /*compressed_verified_contents=*/nullptr));
  EXPECT_EQ(expected_public_key, public_key_in_crx);

  // Delete the file.
  EXPECT_TRUE(base::DeleteFile(temp_file));
}

TEST_F(CrxCreatorTest, VerifyCrxWithVerifiedContents) {
  // Set up a signing key.
  auto signing_key = crypto::RSAPrivateKey::Create(4096);
  std::vector<uint8_t> public_key;
  signing_key->ExportPublicKey(&public_key);
  const std::string expected_public_key = base::Base64Encode(public_key);

  // Create a CRX File.
  base::FilePath temp_file;
  EXPECT_TRUE(base::CreateTemporaryFile(&temp_file));
  std::string test_compressed_verified_contents(
      kTestCompressedVerifiedContents);
  EXPECT_EQ(CreatorResult::OK,
            CreateCrxWithVerifiedContentsInHeader(
                temp_file, TestFile("sample.zip"), signing_key.get(),
                test_compressed_verified_contents));

  // Test that the created file can be verified.
  const std::vector<std::vector<uint8_t>> keys;
  const std::vector<uint8_t> hash;
  std::string public_key_in_crx;
  std::vector<uint8_t> compressed_verified_contents;
  EXPECT_EQ(VerifierResult::OK_FULL,
            Verify(temp_file, VerifierFormat::CRX3, keys, hash,
                   &public_key_in_crx, nullptr, &compressed_verified_contents));
  EXPECT_EQ(expected_public_key, public_key_in_crx);
  std::vector<uint8_t> expected_verified_contents;
  expected_verified_contents.assign(test_compressed_verified_contents.begin(),
                                    test_compressed_verified_contents.end());
  EXPECT_EQ(compressed_verified_contents, expected_verified_contents);

  // Delete the file.
  EXPECT_TRUE(base::DeleteFile(temp_file));
}

}  // namespace crx_file