File: resources_integrity_unittest.cc

package info (click to toggle)
chromium 138.0.7204.157-1
  • links: PTS, VCS
  • area: main
  • in suites: 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 (96 lines) | stat: -rw-r--r-- 3,591 bytes parent folder | download | duplicates (4)
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
// Copyright 2021 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/resources_integrity.h"

#include <algorithm>

#include "base/functional/bind.h"
#include "base/path_service.h"
#include "base/task/sequenced_task_runner.h"
#include "base/test/bind.h"
#include "base/test/metrics/histogram_tester.h"
#include "base/test/task_environment.h"
#include "build/build_config.h"
#include "chrome/app/packed_resources_integrity.h"
#include "chrome/browser/buildflags.h"
#include "chrome/common/chrome_paths.h"
#include "testing/gtest/include/gtest/gtest.h"

class CheckResourceIntegrityTest : public testing::Test {
 protected:
  base::test::TaskEnvironment task_environment_;
};

TEST_F(CheckResourceIntegrityTest, Match) {
  base::FilePath test_data_path;
  ASSERT_TRUE(base::PathService::Get(chrome::DIR_TEST_DATA, &test_data_path));

  std::array<uint8_t, crypto::kSHA256Length> expected = {
      0x1b, 0x3a, 0x5c, 0x9f, 0x92, 0x74, 0x48, 0xcc, 0x89, 0x1a, 0xe8,
      0x3e, 0xcb, 0xfa, 0xc6, 0x6e, 0xb8, 0x73, 0x03, 0xf2, 0xb2, 0x25,
      0xee, 0xf3, 0xba, 0x7f, 0xb6, 0x94, 0x85, 0x61, 0xe2, 0xe8};

  base::RunLoop loop;
  CheckResourceIntegrity(test_data_path.AppendASCII("circle.svg"), expected,
                         base::SequencedTaskRunner::GetCurrentDefault(),
                         base::BindLambdaForTesting([&](bool matches) {
                           EXPECT_TRUE(matches);
                           loop.Quit();
                         }));
  loop.Run();
}

TEST_F(CheckResourceIntegrityTest, Mismatch) {
  base::FilePath test_data_path;
  ASSERT_TRUE(base::PathService::Get(chrome::DIR_TEST_DATA, &test_data_path));

  uint8_t unexpected[crypto::kSHA256Length];
  std::ranges::fill(unexpected, 'a');
  base::RunLoop loop;
  CheckResourceIntegrity(test_data_path.AppendASCII("circle.svg"), unexpected,
                         base::SequencedTaskRunner::GetCurrentDefault(),
                         base::BindLambdaForTesting([&](bool matches) {
                           EXPECT_FALSE(matches);
                           loop.Quit();
                         }));
  loop.Run();
}

TEST_F(CheckResourceIntegrityTest, NonExistentFile) {
  uint8_t unexpected[crypto::kSHA256Length];
  std::ranges::fill(unexpected, 'a');
  base::RunLoop loop;
  CheckResourceIntegrity(
      base::FilePath(FILE_PATH_LITERAL("this file does not exist.moo")),
      unexpected, base::SequencedTaskRunner::GetCurrentDefault(),
      base::BindLambdaForTesting([&](bool matches) {
        EXPECT_FALSE(matches);
        loop.Quit();
      }));
  loop.Quit();
}

#if BUILDFLAG(IS_WIN)
// On Windows, CheckPakFileIntegrity() dynamically finds this symbol from its
// main exe module (normally chrome.exe). In unit_tests.exe, provide the same
// export.
extern "C" __declspec(dllexport) __cdecl void GetPakFileHashes(
    const uint8_t** resources_pak,
    const uint8_t** chrome_100_pak,
    const uint8_t** chrome_200_pak) {
  *resources_pak = kSha256_resources_pak.data();
  *chrome_100_pak = kSha256_chrome_100_percent_pak.data();
  *chrome_200_pak = kSha256_chrome_200_percent_pak.data();
}
#endif  // BUILDFLAG(IS_WIN)

TEST_F(CheckResourceIntegrityTest, ChromePaks) {
  base::HistogramTester tester;
  CheckPakFileIntegrity();
  task_environment_.RunUntilIdle();
  tester.ExpectBucketCount("SafeBrowsing.PakIntegrity.Resources", 1, 1);
  tester.ExpectBucketCount("SafeBrowsing.PakIntegrity.Chrome100", 1, 1);
  tester.ExpectBucketCount("SafeBrowsing.PakIntegrity.Chrome200", 1, 1);
}