// Copyright 2017 The Chromium OS Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. #include "bsdiff/patch_writer.h" #include #include "bsdiff/test_utils.h" namespace { // Generated with: // echo 'Hello World' | hexdump -v -e '" " 12/1 "0x%02x, " "\n"' const uint8_t kHelloWorld[] = { 0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x57, 0x6f, 0x72, 0x6c, 0x64, 0x0a, }; // Compressed empty file. // bzip2 -9 (patch.diff_len)); EXPECT_EQ(sizeof(kCompressedEmpty), patch.extra_len); } TEST_F(BsdiffPatchWriterTest, AllInExtraStreamTest) { // Write to the extra stream in two parts: first 5 bytes, then the rest. EXPECT_TRUE(patch_writer_.AddControlEntry(ControlEntry(0, 5, 0))); EXPECT_TRUE(patch_writer_.AddControlEntry( ControlEntry(0, sizeof(kHelloWorld) - 5, 0))); EXPECT_TRUE(patch_writer_.WriteExtraStream(kHelloWorld, sizeof(kHelloWorld))); EXPECT_TRUE(patch_writer_.Close()); test_utils::BsdiffPatchFile patch; EXPECT_TRUE(patch.LoadFromFile(patch_file_.filename())); EXPECT_TRUE(patch.IsValid()); EXPECT_EQ(patch.bz2_diff, std::vector(kCompressedEmpty, kCompressedEmpty + sizeof(kCompressedEmpty))); EXPECT_EQ(patch.bz2_extra, std::vector( kCompressedHelloWorld, kCompressedHelloWorld + sizeof(kCompressedHelloWorld))); EXPECT_EQ(static_cast(sizeof(kHelloWorld)), patch.new_file_len); } TEST_F(BsdiffPatchWriterTest, AllInDiffStreamTest) { // Write to the extra stream in two parts: first 5 bytes, then the rest. EXPECT_TRUE( patch_writer_.AddControlEntry(ControlEntry(sizeof(kHelloWorld), 0, 0))); std::vector zeros(sizeof(kHelloWorld), 0); EXPECT_TRUE(patch_writer_.WriteDiffStream(zeros.data(), zeros.size())); EXPECT_TRUE(patch_writer_.Close()); test_utils::BsdiffPatchFile patch; EXPECT_TRUE(patch.LoadFromFile(patch_file_.filename())); EXPECT_TRUE(patch.IsValid()); EXPECT_EQ(patch.bz2_extra, std::vector(kCompressedEmpty, kCompressedEmpty + sizeof(kCompressedEmpty))); EXPECT_EQ(patch.bz2_diff, std::vector(kCompressedZeros, kCompressedZeros + sizeof(kCompressedZeros))); EXPECT_EQ(static_cast(sizeof(kHelloWorld)), patch.new_file_len); } } // namespace bsdiff