integration_test.cc 3.7 KB

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