xref: /aosp_15_r20/external/zucchini/integration_test.cc (revision a03ca8b91e029cd15055c20c78c2e087c84792e4)
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 <stdint.h>
6 
7 #include <algorithm>
8 #include <optional>
9 #include <string>
10 #include <vector>
11 
12 #include "base/files/file_path.h"
13 #include "base/files/memory_mapped_file.h"
14 #include "base/path_service.h"
15 #include "components/zucchini/buffer_view.h"
16 #include "components/zucchini/patch_reader.h"
17 #include "components/zucchini/patch_writer.h"
18 #include "components/zucchini/zucchini.h"
19 #include "testing/gtest/include/gtest/gtest.h"
20 
21 namespace zucchini {
22 
MakeTestPath(const std::string & filename)23 base::FilePath MakeTestPath(const std::string& filename) {
24   base::FilePath path;
25   DCHECK(base::PathService::Get(base::DIR_SOURCE_ROOT, &path));
26   return path.AppendASCII("components")
27       .AppendASCII("zucchini")
28       .AppendASCII("testdata")
29       .AppendASCII(filename);
30 }
31 
TestGenApply(const std::string & old_filename,const std::string & new_filename,bool raw)32 void TestGenApply(const std::string& old_filename,
33                   const std::string& new_filename,
34                   bool raw) {
35   base::FilePath old_path = MakeTestPath(old_filename);
36   base::FilePath new_path = MakeTestPath(new_filename);
37 
38   base::MemoryMappedFile old_file;
39   ASSERT_TRUE(old_file.Initialize(old_path));
40 
41   base::MemoryMappedFile new_file;
42   ASSERT_TRUE(new_file.Initialize(new_path));
43 
44   ConstBufferView old_region(old_file.data(), old_file.length());
45   ConstBufferView new_region(new_file.data(), new_file.length());
46 
47   EnsemblePatchWriter patch_writer(old_region, new_region);
48 
49   // Generate patch from "old" to "new".
50   ASSERT_EQ(status::kStatusSuccess,
51             raw ? GenerateBufferRaw(old_region, new_region, &patch_writer)
52                 : GenerateBuffer(old_region, new_region, &patch_writer));
53 
54   size_t patch_size = patch_writer.SerializedSize();
55   EXPECT_GE(patch_size, 80U);  // Minimum size is empty patch.
56   // TODO(etiennep): Add check on maximum expected size.
57 
58   std::vector<uint8_t> patch_buffer(patch_writer.SerializedSize());
59   patch_writer.SerializeInto({patch_buffer.data(), patch_buffer.size()});
60 
61   // Read back generated patch.
62   std::optional<EnsemblePatchReader> patch_reader =
63       EnsemblePatchReader::Create({patch_buffer.data(), patch_buffer.size()});
64   ASSERT_TRUE(patch_reader.has_value());
65 
66   // Check basic properties.
67   EXPECT_TRUE(patch_reader->CheckOldFile(old_region));
68   EXPECT_TRUE(patch_reader->CheckNewFile(new_region));
69   EXPECT_EQ(old_file.length(), patch_reader->header().old_size);
70   // If new_size doesn't match expectation, the function is aborted.
71   ASSERT_EQ(new_file.length(), patch_reader->header().new_size);
72 
73   // Apply patch to "old" to get "patched new", ensure it's identical to "new".
74   std::vector<uint8_t> patched_new_buffer(new_region.size());
75   ASSERT_EQ(status::kStatusSuccess, ApplyBuffer(old_region, *patch_reader,
76                                                 {patched_new_buffer.data(),
77                                                  patched_new_buffer.size()}));
78 
79   // Note that |new_region| and |patched_new_buffer| are the same size.
80   EXPECT_TRUE(std::equal(new_region.begin(), new_region.end(),
81                          patched_new_buffer.begin()));
82 }
83 
TEST(EndToEndTest,GenApplyRaw)84 TEST(EndToEndTest, GenApplyRaw) {
85   TestGenApply("setup1.exe", "setup2.exe", true);
86   TestGenApply("chrome64_1.exe", "chrome64_2.exe", true);
87 }
88 
TEST(EndToEndTest,GenApplyIdentity)89 TEST(EndToEndTest, GenApplyIdentity) {
90   TestGenApply("setup1.exe", "setup1.exe", false);
91 }
92 
TEST(EndToEndTest,GenApplySimple)93 TEST(EndToEndTest, GenApplySimple) {
94   TestGenApply("setup1.exe", "setup2.exe", false);
95   TestGenApply("setup2.exe", "setup1.exe", false);
96   TestGenApply("chrome64_1.exe", "chrome64_2.exe", false);
97 }
98 
TEST(EndToEndTest,GenApplyCross)99 TEST(EndToEndTest, GenApplyCross) {
100   TestGenApply("setup1.exe", "chrome64_1.exe", false);
101 }
102 
103 }  // namespace zucchini
104