blob: e3b3747fac8c44f06ccd4b8f19315b512c22dd99 [file] [log] [blame]
Alex Deymo68c0e7f2017-10-02 20:38:12 +02001// Copyright 2017 The Chromium OS 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 "bsdiff/diff_encoder.h"
6
Alex Deymoaefc2532017-10-11 17:50:12 +02007#include <memory>
Alex Deymo68c0e7f2017-10-02 20:38:12 +02008#include <vector>
9
10#include <gtest/gtest.h>
11
12#include "bsdiff/fake_patch_writer.h"
13#include "bsdiff/test_utils.h"
14
15namespace {
16
17// Generated with:
18// echo 'Hello World' | hexdump -v -e '" " 12/1 "0x%02x, " "\n"'
19const uint8_t kHelloWorld[] = {
20 0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x57, 0x6f, 0x72, 0x6c, 0x64, 0x0a,
21};
22
23} // namespace
24
25namespace bsdiff {
26
27class DiffEncoderTest : public testing::Test {
28 protected:
Alex Deymoaefc2532017-10-11 17:50:12 +020029 void SetUp() {
30 EXPECT_TRUE(fake_patch_.Init());
31 // By default, set the encoder to kHelloWorld to kHelloWorld.
32 diff_encoder_.reset(new DiffEncoder(&fake_patch_, kHelloWorld,
33 sizeof(kHelloWorld), kHelloWorld,
34 sizeof(kHelloWorld)));
35 }
Alex Deymo68c0e7f2017-10-02 20:38:12 +020036
37 FakePatchWriter fake_patch_;
38 std::unique_ptr<DiffEncoder> diff_encoder_;
39};
40
41TEST_F(DiffEncoderTest, CreateEmptyPatchTest) {
42 diff_encoder_.reset(new DiffEncoder(&fake_patch_, nullptr, 0, nullptr, 0));
43 EXPECT_TRUE(diff_encoder_->Close());
44
45 // Both diff and extra stream must be empty stream, and not control entries.
46 EXPECT_EQ(0U, fake_patch_.entries().size());
47 EXPECT_TRUE(fake_patch_.diff_stream().empty());
48 EXPECT_TRUE(fake_patch_.extra_stream().empty());
49}
50
51TEST_F(DiffEncoderTest, AllInExtraStreamTest) {
52 diff_encoder_.reset(new DiffEncoder(&fake_patch_, nullptr, 0, kHelloWorld,
53 sizeof(kHelloWorld)));
54
55 // Write to the extra stream in two parts: first 5 bytes, then the rest.
56 EXPECT_TRUE(diff_encoder_->AddControlEntry(ControlEntry(0, 5, 0)));
57 EXPECT_TRUE(diff_encoder_->AddControlEntry(
58 ControlEntry(0, sizeof(kHelloWorld) - 5, 0)));
59 EXPECT_TRUE(diff_encoder_->Close());
60
61 EXPECT_EQ(2U, fake_patch_.entries().size());
62 EXPECT_TRUE(fake_patch_.diff_stream().empty());
63 std::vector<uint8_t> hello_world(kHelloWorld,
64 kHelloWorld + sizeof(kHelloWorld));
65 EXPECT_EQ(hello_world, fake_patch_.extra_stream());
66}
67
68TEST_F(DiffEncoderTest, AllInDiffStreamTest) {
Alex Deymo68c0e7f2017-10-02 20:38:12 +020069 EXPECT_TRUE(
70 diff_encoder_->AddControlEntry(ControlEntry(sizeof(kHelloWorld), 0, 0)));
71 EXPECT_TRUE(diff_encoder_->Close());
72
73 EXPECT_EQ(std::vector<uint8_t>(sizeof(kHelloWorld), 0),
74 fake_patch_.diff_stream());
75 EXPECT_TRUE(fake_patch_.extra_stream().empty());
76}
77
Alex Deymoaefc2532017-10-11 17:50:12 +020078TEST_F(DiffEncoderTest, OldPosNegativeErrorTest) {
79 // Referencing negative values in oldpos is fine, until you use them.
80 EXPECT_TRUE(diff_encoder_->AddControlEntry(ControlEntry(0, 0, -5)));
81 EXPECT_TRUE(diff_encoder_->AddControlEntry(ControlEntry(0, 0, 2)));
82 EXPECT_FALSE(diff_encoder_->AddControlEntry(ControlEntry(1, 0, 0)));
83}
84
85// Test that using an oldpos past the end of the file fails.
86TEST_F(DiffEncoderTest, OldPosTooBigErrorTest) {
87 EXPECT_TRUE(
88 diff_encoder_->AddControlEntry(ControlEntry(0, 0, sizeof(kHelloWorld))));
89 EXPECT_FALSE(diff_encoder_->AddControlEntry(ControlEntry(1, 0, 0)));
90}
91
92// Test that diffing against a section of the old file past the end of the file
93// fails.
94TEST_F(DiffEncoderTest, OldPosPlusSizeTooBigErrorTest) {
95 // The oldpos is set to a range inside the word, the we try to copy past the
96 // end of it.
97 EXPECT_TRUE(diff_encoder_->AddControlEntry(
98 ControlEntry(0, 0, sizeof(kHelloWorld) - 3)));
99 EXPECT_FALSE(
100 diff_encoder_->AddControlEntry(ControlEntry(sizeof(kHelloWorld), 0, 0)));
101}
102
103TEST_F(DiffEncoderTest, ExtraStreamTooBigErrorTest) {
104 EXPECT_TRUE(diff_encoder_->AddControlEntry(ControlEntry(3, 0, 0)));
105 // This writes too many bytes in the stream because we already have 3 bytes.
106 EXPECT_FALSE(
107 diff_encoder_->AddControlEntry(ControlEntry(0, sizeof(kHelloWorld), 0)));
108}
109
Alex Deymo68c0e7f2017-10-02 20:38:12 +0200110} // namespace bsdiff