File: integration_test.cc

package info (click to toggle)
chromium 139.0.7258.127-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,122,156 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (102 lines) | stat: -rw-r--r-- 3,652 bytes parent folder | download | duplicates (6)
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
97
98
99
100
101
102
// Copyright 2017 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <stdint.h>

#include <algorithm>
#include <optional>
#include <string>
#include <vector>

#include "base/files/file_path.h"
#include "base/files/memory_mapped_file.h"
#include "base/path_service.h"
#include "components/zucchini/buffer_view.h"
#include "components/zucchini/patch_reader.h"
#include "components/zucchini/patch_writer.h"
#include "components/zucchini/zucchini.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace zucchini {

base::FilePath MakeTestPath(const std::string& filename) {
  base::FilePath path;
  DCHECK(base::PathService::Get(base::DIR_SRC_TEST_DATA_ROOT, &path));
  return path.AppendASCII("components")
      .AppendASCII("zucchini")
      .AppendASCII("testdata")
      .AppendASCII(filename);
}

void TestGenApply(const std::string& old_filename,
                  const std::string& new_filename,
                  bool raw) {
  base::FilePath old_path = MakeTestPath(old_filename);
  base::FilePath new_path = MakeTestPath(new_filename);

  base::MemoryMappedFile old_file;
  ASSERT_TRUE(old_file.Initialize(old_path));

  base::MemoryMappedFile new_file;
  ASSERT_TRUE(new_file.Initialize(new_path));

  ConstBufferView old_region(old_file.data(), old_file.length());
  ConstBufferView new_region(new_file.data(), new_file.length());

  EnsemblePatchWriter patch_writer(old_region, new_region);

  // Generate patch from "old" to "new".
  ASSERT_EQ(status::kStatusSuccess,
            raw ? GenerateBufferRaw(old_region, new_region, &patch_writer)
                : GenerateBuffer(old_region, new_region, &patch_writer));

  size_t patch_size = patch_writer.SerializedSize();
  EXPECT_GE(patch_size, 80U);  // Minimum size is empty patch.
  // TODO(etiennep): Add check on maximum expected size.

  std::vector<uint8_t> patch_buffer(patch_writer.SerializedSize());
  patch_writer.SerializeInto({patch_buffer.data(), patch_buffer.size()});

  // Read back generated patch.
  std::optional<EnsemblePatchReader> patch_reader =
      EnsemblePatchReader::Create({patch_buffer.data(), patch_buffer.size()});
  ASSERT_TRUE(patch_reader.has_value());

  // Check basic properties.
  EXPECT_TRUE(patch_reader->CheckOldFile(old_region));
  EXPECT_TRUE(patch_reader->CheckNewFile(new_region));
  EXPECT_EQ(old_file.length(), patch_reader->header().old_size);
  // If new_size doesn't match expectation, the function is aborted.
  ASSERT_EQ(new_file.length(), patch_reader->header().new_size);

  // Apply patch to "old" to get "patched new", ensure it's identical to "new".
  std::vector<uint8_t> patched_new_buffer(new_region.size());
  ASSERT_EQ(status::kStatusSuccess, ApplyBuffer(old_region, *patch_reader,
                                                {patched_new_buffer.data(),
                                                 patched_new_buffer.size()}));

  // Note that |new_region| and |patched_new_buffer| are the same size.
  EXPECT_TRUE(std::ranges::equal(new_region, patched_new_buffer));
}

TEST(EndToEndTest, GenApplyRaw) {
  TestGenApply("setup1.exe", "setup2.exe", true);
  TestGenApply("chrome64_1.exe", "chrome64_2.exe", true);
}

TEST(EndToEndTest, GenApplyIdentity) {
  TestGenApply("setup1.exe", "setup1.exe", false);
}

TEST(EndToEndTest, GenApplySimple) {
  TestGenApply("setup1.exe", "setup2.exe", false);
  TestGenApply("setup2.exe", "setup1.exe", false);
  TestGenApply("chrome64_1.exe", "chrome64_2.exe", false);
}

TEST(EndToEndTest, GenApplyCross) {
  TestGenApply("setup1.exe", "chrome64_1.exe", false);
}

}  // namespace zucchini