puffin_patcher_unittest.cc 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. // Copyright 2022 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 "components/update_client/puffin_patcher.h"
  5. #include "base/base_paths.h"
  6. #include "base/bind.h"
  7. #include "base/files/file_path.h"
  8. #include "base/files/file_util.h"
  9. #include "base/files/scoped_temp_dir.h"
  10. #include "base/memory/scoped_refptr.h"
  11. #include "base/path_service.h"
  12. #include "base/run_loop.h"
  13. #include "base/sequence_checker.h"
  14. #include "base/test/bind.h"
  15. #include "base/test/task_environment.h"
  16. #include "base/values.h"
  17. #include "components/services/patch/in_process_file_patcher.h"
  18. #include "components/update_client/patch/patch_impl.h"
  19. #include "components/update_client/test_installer.h"
  20. #include "components/update_client/update_client_errors.h"
  21. #include "courgette/courgette.h"
  22. #include "courgette/third_party/bsdiff/bsdiff.h"
  23. #include "testing/gtest/include/gtest/gtest.h"
  24. namespace update_client {
  25. namespace {
  26. base::FilePath OutTestFile(const char* file) {
  27. base::FilePath path;
  28. base::PathService::Get(base::DIR_GEN_TEST_DATA_ROOT, &path);
  29. return path.AppendASCII(file);
  30. }
  31. } // namespace
  32. class PuffinPatcherTest : public testing::Test {
  33. public:
  34. PuffinPatcherTest() = default;
  35. ~PuffinPatcherTest() override = default;
  36. protected:
  37. base::test::TaskEnvironment env_;
  38. };
  39. TEST_F(PuffinPatcherTest, CheckPuffPatch) {
  40. // The operation needs a Patcher to access the PatchService.
  41. scoped_refptr<Patcher> patcher =
  42. base::MakeRefCounted<PatchChromiumFactory>(
  43. base::BindRepeating(&patch::LaunchInProcessFilePatcher))
  44. ->Create();
  45. base::FilePath out_file = OutTestFile("puffin_app_v1_to_v2.crx3");
  46. EXPECT_TRUE(base::DeleteFile(out_file));
  47. SEQUENCE_CHECKER(sequence_checker);
  48. {
  49. base::File input_file(OutTestFile("puffin_app_v1.crx3"),
  50. base::File::FLAG_OPEN | base::File::FLAG_READ);
  51. base::File patch_file(OutTestFile("puffin_app_v1_to_v2.puff"),
  52. base::File::FLAG_OPEN | base::File::FLAG_READ);
  53. base::File output_file(out_file, base::File::FLAG_CREATE |
  54. base::File::FLAG_WRITE |
  55. base::File::FLAG_WIN_EXCLUSIVE_WRITE);
  56. base::RunLoop loop;
  57. PuffinPatcher::Patch(
  58. std::move(input_file), std::move(patch_file), std::move(output_file),
  59. patcher,
  60. base::BindLambdaForTesting(
  61. [&loop, &sequence_checker](UnpackerError error, int extra_code) {
  62. DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker);
  63. EXPECT_EQ(error, UnpackerError::kNone);
  64. EXPECT_EQ(extra_code, 0);
  65. loop.Quit();
  66. }));
  67. loop.Run();
  68. }
  69. EXPECT_TRUE(base::ContentsEqual(OutTestFile("puffin_app_v2.crx3"), out_file));
  70. out_file = OutTestFile("puffin_app_v2_to_v1.crx3");
  71. EXPECT_TRUE(base::DeleteFile(out_file));
  72. {
  73. base::File input_file(OutTestFile("puffin_app_v2.crx3"),
  74. base::File::FLAG_OPEN | base::File::FLAG_READ);
  75. base::File patch_file(OutTestFile("puffin_app_v2_to_v1.puff"),
  76. base::File::FLAG_OPEN | base::File::FLAG_READ);
  77. base::File output_file(out_file, base::File::FLAG_CREATE |
  78. base::File::FLAG_WRITE |
  79. base::File::FLAG_WIN_EXCLUSIVE_WRITE);
  80. base::RunLoop loop;
  81. PuffinPatcher::Patch(
  82. std::move(input_file), std::move(patch_file), std::move(output_file),
  83. patcher,
  84. base::BindLambdaForTesting(
  85. [&loop, &sequence_checker](UnpackerError error, int extra_code) {
  86. DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker);
  87. EXPECT_EQ(error, UnpackerError::kNone);
  88. EXPECT_EQ(extra_code, 0);
  89. loop.Quit();
  90. }));
  91. loop.Run();
  92. }
  93. DETACH_FROM_SEQUENCE(sequence_checker);
  94. EXPECT_TRUE(base::ContentsEqual(OutTestFile("puffin_app_v1.crx3"), out_file));
  95. }
  96. } // namespace update_client