blob: 9b4cd530207d70e0f49b388a9667cb5711d1f12e [file] [log] [blame]
Alex Deymoa5cff222015-04-08 14:10:30 -07001// Copyright 2015 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.h"
6
7#include <gtest/gtest.h>
8#include <string>
9#include <vector>
10
11#include "test_utils.h"
12
13using std::string;
14using std::vector;
15using test_utils::BsdiffPatchFile;
16
17class BsdiffTest : public testing::Test {
18 protected:
19 BsdiffTest()
20 : old_file_("bsdiff_oldfile.XXXXXX"),
21 new_file_("bsdiff_newfile.XXXXXX"),
22 patch_file_("bsdiff_patchfile.XXXXXX") {
23 }
24 ~BsdiffTest() override {}
25
26 test_utils::ScopedTempFile old_file_;
27 test_utils::ScopedTempFile new_file_;
28 test_utils::ScopedTempFile patch_file_;
29};
30
31// Check that a file with no changes has a very small patch (no extra data).
32TEST_F(BsdiffTest, EqualEmptyFiles) {
33 // Empty old and new files.
34 EXPECT_EQ(0, bsdiff(old_file_.filename().c_str(),
35 new_file_.filename().c_str(),
36 patch_file_.filename().c_str()));
37 BsdiffPatchFile patch;
38 EXPECT_TRUE(patch.LoadFromFile(patch_file_.filename()));
39 EXPECT_TRUE(patch.IsValid());
40
41 // An empty bz2 file will have 14 bytes.
42 EXPECT_EQ(14, patch.diff_len);
43 EXPECT_EQ(14, patch.extra_len);
44}
45
46TEST_F(BsdiffTest, EqualSmallFiles) {
47 string some_text = "Hello world!";
48 vector<uint8_t> vec_some_text(some_text.begin(), some_text.end());
49 test_utils::WriteFile(old_file_.filename(), vec_some_text);
50 EXPECT_EQ(0, bsdiff(old_file_.filename().c_str(),
51 new_file_.filename().c_str(),
52 patch_file_.filename().c_str()));
53 BsdiffPatchFile patch;
54 EXPECT_TRUE(patch.LoadFromFile(patch_file_.filename()));
55 EXPECT_TRUE(patch.IsValid());
56
57 // An empty bz2 file will have 14 bytes.
58 EXPECT_EQ(14, patch.diff_len);
59 EXPECT_EQ(14, patch.extra_len);
60}