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