blob: 3bbce87d561e93c92071dd231ab4ef4633753548 [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
7#include <vector>
8
9#include <gtest/gtest.h>
10
11#include "bsdiff/fake_patch_writer.h"
12#include "bsdiff/test_utils.h"
13
14namespace {
15
16// Generated with:
17// echo 'Hello World' | hexdump -v -e '" " 12/1 "0x%02x, " "\n"'
18const uint8_t kHelloWorld[] = {
19 0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x57, 0x6f, 0x72, 0x6c, 0x64, 0x0a,
20};
21
22} // namespace
23
24namespace bsdiff {
25
26class DiffEncoderTest : public testing::Test {
27 protected:
28 void SetUp() { EXPECT_TRUE(fake_patch_.Init()); }
29
30 FakePatchWriter fake_patch_;
31 std::unique_ptr<DiffEncoder> diff_encoder_;
32};
33
34TEST_F(DiffEncoderTest, CreateEmptyPatchTest) {
35 diff_encoder_.reset(new DiffEncoder(&fake_patch_, nullptr, 0, nullptr, 0));
36 EXPECT_TRUE(diff_encoder_->Close());
37
38 // Both diff and extra stream must be empty stream, and not control entries.
39 EXPECT_EQ(0U, fake_patch_.entries().size());
40 EXPECT_TRUE(fake_patch_.diff_stream().empty());
41 EXPECT_TRUE(fake_patch_.extra_stream().empty());
42}
43
44TEST_F(DiffEncoderTest, AllInExtraStreamTest) {
45 diff_encoder_.reset(new DiffEncoder(&fake_patch_, nullptr, 0, kHelloWorld,
46 sizeof(kHelloWorld)));
47
48 // Write to the extra stream in two parts: first 5 bytes, then the rest.
49 EXPECT_TRUE(diff_encoder_->AddControlEntry(ControlEntry(0, 5, 0)));
50 EXPECT_TRUE(diff_encoder_->AddControlEntry(
51 ControlEntry(0, sizeof(kHelloWorld) - 5, 0)));
52 EXPECT_TRUE(diff_encoder_->Close());
53
54 EXPECT_EQ(2U, fake_patch_.entries().size());
55 EXPECT_TRUE(fake_patch_.diff_stream().empty());
56 std::vector<uint8_t> hello_world(kHelloWorld,
57 kHelloWorld + sizeof(kHelloWorld));
58 EXPECT_EQ(hello_world, fake_patch_.extra_stream());
59}
60
61TEST_F(DiffEncoderTest, AllInDiffStreamTest) {
62 diff_encoder_.reset(new DiffEncoder(&fake_patch_, kHelloWorld,
63 sizeof(kHelloWorld), kHelloWorld,
64 sizeof(kHelloWorld)));
65 EXPECT_TRUE(
66 diff_encoder_->AddControlEntry(ControlEntry(sizeof(kHelloWorld), 0, 0)));
67 EXPECT_TRUE(diff_encoder_->Close());
68
69 EXPECT_EQ(std::vector<uint8_t>(sizeof(kHelloWorld), 0),
70 fake_patch_.diff_stream());
71 EXPECT_TRUE(fake_patch_.extra_stream().empty());
72}
73
74} // namespace bsdiff