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