resources_integrity_unittest.cc 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. // Copyright 2021 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 "chrome/browser/resources_integrity.h"
  5. #include "base/bind.h"
  6. #include "base/path_service.h"
  7. #include "base/test/bind.h"
  8. #include "base/test/metrics/histogram_tester.h"
  9. #include "base/test/task_environment.h"
  10. #include "base/threading/sequenced_task_runner_handle.h"
  11. #include "build/build_config.h"
  12. #include "chrome/app/packed_resources_integrity.h"
  13. #include "chrome/browser/buildflags.h"
  14. #include "chrome/common/chrome_paths.h"
  15. #include "testing/gtest/include/gtest/gtest.h"
  16. class CheckResourceIntegrityTest : public testing::Test {
  17. protected:
  18. base::test::TaskEnvironment task_environment_;
  19. };
  20. TEST_F(CheckResourceIntegrityTest, Match) {
  21. base::FilePath test_data_path;
  22. ASSERT_TRUE(base::PathService::Get(chrome::DIR_TEST_DATA, &test_data_path));
  23. std::array<uint8_t, crypto::kSHA256Length> expected = {
  24. 0x1b, 0x3a, 0x5c, 0x9f, 0x92, 0x74, 0x48, 0xcc, 0x89, 0x1a, 0xe8,
  25. 0x3e, 0xcb, 0xfa, 0xc6, 0x6e, 0xb8, 0x73, 0x03, 0xf2, 0xb2, 0x25,
  26. 0xee, 0xf3, 0xba, 0x7f, 0xb6, 0x94, 0x85, 0x61, 0xe2, 0xe8};
  27. base::RunLoop loop;
  28. CheckResourceIntegrity(test_data_path.AppendASCII("circle.svg"), expected,
  29. base::SequencedTaskRunnerHandle::Get(),
  30. base::BindLambdaForTesting([&](bool matches) {
  31. EXPECT_TRUE(matches);
  32. loop.Quit();
  33. }));
  34. loop.Run();
  35. }
  36. TEST_F(CheckResourceIntegrityTest, Mismatch) {
  37. base::FilePath test_data_path;
  38. ASSERT_TRUE(base::PathService::Get(chrome::DIR_TEST_DATA, &test_data_path));
  39. std::vector<uint8_t> unexpected(crypto::kSHA256Length, 'a');
  40. base::RunLoop loop;
  41. CheckResourceIntegrity(test_data_path.AppendASCII("circle.svg"),
  42. base::make_span<32>(unexpected),
  43. base::SequencedTaskRunnerHandle::Get(),
  44. base::BindLambdaForTesting([&](bool matches) {
  45. EXPECT_FALSE(matches);
  46. loop.Quit();
  47. }));
  48. loop.Run();
  49. }
  50. TEST_F(CheckResourceIntegrityTest, NonExistentFile) {
  51. std::vector<uint8_t> unexpected(crypto::kSHA256Length, 'a');
  52. base::RunLoop loop;
  53. CheckResourceIntegrity(
  54. base::FilePath(FILE_PATH_LITERAL("this file does not exist.moo")),
  55. base::make_span<crypto::kSHA256Length>(unexpected),
  56. base::SequencedTaskRunnerHandle::Get(),
  57. base::BindLambdaForTesting([&](bool matches) {
  58. EXPECT_FALSE(matches);
  59. loop.Quit();
  60. }));
  61. loop.Quit();
  62. }
  63. #if BUILDFLAG(IS_WIN)
  64. // On Windows, CheckPakFileIntegrity() dynamically finds this symbol from its
  65. // main exe module (normally chrome.exe). In unit_tests.exe, provide the same
  66. // export.
  67. extern "C" __declspec(dllexport) __cdecl void GetPakFileHashes(
  68. const uint8_t** resources_pak,
  69. const uint8_t** chrome_100_pak,
  70. const uint8_t** chrome_200_pak) {
  71. *resources_pak = kSha256_resources_pak.data();
  72. *chrome_100_pak = kSha256_chrome_100_percent_pak.data();
  73. *chrome_200_pak = kSha256_chrome_200_percent_pak.data();
  74. }
  75. #endif // BUILDFLAG(IS_WIN)
  76. TEST_F(CheckResourceIntegrityTest, ChromePaks) {
  77. base::HistogramTester tester;
  78. CheckPakFileIntegrity();
  79. task_environment_.RunUntilIdle();
  80. tester.ExpectBucketCount("SafeBrowsing.PakIntegrity.Resources", 1, 1);
  81. tester.ExpectBucketCount("SafeBrowsing.PakIntegrity.Chrome100", 1, 1);
  82. tester.ExpectBucketCount("SafeBrowsing.PakIntegrity.Chrome200", 1, 1);
  83. }