xref: /aosp_15_r20/external/bsdiff/test_utils.h (revision a3a45f308bd90ef1a6e6a5e8fb92fe449b895909)
1*a3a45f30SXin Li // Copyright 2015 The Chromium OS Authors. All rights reserved.
2*a3a45f30SXin Li // Use of this source code is governed by a BSD-style license that can be
3*a3a45f30SXin Li // found in the LICENSE file.
4*a3a45f30SXin Li 
5*a3a45f30SXin Li #ifndef _BSDIFF_TEST_UTILS_H_
6*a3a45f30SXin Li #define _BSDIFF_TEST_UTILS_H_
7*a3a45f30SXin Li 
8*a3a45f30SXin Li #include <gtest/gtest.h>
9*a3a45f30SXin Li #include <string>
10*a3a45f30SXin Li #include <vector>
11*a3a45f30SXin Li 
12*a3a45f30SXin Li #include "bsdiff/logging.h"
13*a3a45f30SXin Li 
14*a3a45f30SXin Li #define TEST_AND_RETURN_FALSE(_x)   \
15*a3a45f30SXin Li   do {                              \
16*a3a45f30SXin Li     if (!static_cast<bool>(_x)) {   \
17*a3a45f30SXin Li       LOG(ERROR) << #_x " failed."; \
18*a3a45f30SXin Li       return false;                 \
19*a3a45f30SXin Li     }                               \
20*a3a45f30SXin Li   } while (0)
21*a3a45f30SXin Li 
22*a3a45f30SXin Li namespace test_utils {
23*a3a45f30SXin Li 
24*a3a45f30SXin Li class BsdiffTestEnvironment : public ::testing::Environment {
25*a3a45f30SXin Li   public:
26*a3a45f30SXin Li     virtual void SetUp();
27*a3a45f30SXin Li };
28*a3a45f30SXin Li 
29*a3a45f30SXin Li // Reads all the contents of the file |path| into |out|. Returns whether it
30*a3a45f30SXin Li // read up to the end of file.
31*a3a45f30SXin Li bool ReadFile(const std::string& path, std::vector<uint8_t>* out);
32*a3a45f30SXin Li 
33*a3a45f30SXin Li // Overrides the file |path| with the contents passed in |out|. Returns whether
34*a3a45f30SXin Li // the operation succeeded.
35*a3a45f30SXin Li bool WriteFile(const std::string& path, std::vector<uint8_t> contents);
36*a3a45f30SXin Li 
37*a3a45f30SXin Li // Utility class to create and delete a temp file.
38*a3a45f30SXin Li class ScopedTempFile {
39*a3a45f30SXin Li  public:
40*a3a45f30SXin Li   // Creates a temp file with the passed |pattern|. The pattern should end with
41*a3a45f30SXin Li   // "XXXXXX", that will be replaced with a random string. The file will be
42*a3a45f30SXin Li   // removed when this instance is destroyed.
43*a3a45f30SXin Li   explicit ScopedTempFile(const std::string& pattern);
44*a3a45f30SXin Li   ~ScopedTempFile();
45*a3a45f30SXin Li 
filename()46*a3a45f30SXin Li   std::string filename() const { return filename_; }
c_str()47*a3a45f30SXin Li   const char* c_str() const { return filename_.c_str(); }
48*a3a45f30SXin Li 
49*a3a45f30SXin Li   // Releases the temporary file. It will not be deleted when this instance is
50*a3a45f30SXin Li   // destroyed.
release()51*a3a45f30SXin Li   void release() { filename_.clear(); }
52*a3a45f30SXin Li 
53*a3a45f30SXin Li  private:
54*a3a45f30SXin Li   std::string filename_;
55*a3a45f30SXin Li };
56*a3a45f30SXin Li 
57*a3a45f30SXin Li // This struct representes a parsed BSDIFF40 file.
58*a3a45f30SXin Li struct BsdiffPatchFile {
59*a3a45f30SXin Li   static const size_t kHeaderSize = 32;
60*a3a45f30SXin Li 
61*a3a45f30SXin Li   // Parses a BSDIFF40 file and stores the contents in the local methods.
62*a3a45f30SXin Li   bool LoadFromFile(const std::string& filename);
63*a3a45f30SXin Li 
64*a3a45f30SXin Li   // Returns wheter the patch file is valid.
65*a3a45f30SXin Li   bool IsValid() const;
66*a3a45f30SXin Li 
67*a3a45f30SXin Li   // The magic string in the header file. Normally "BSDIFF40".
68*a3a45f30SXin Li   std::string magic;
69*a3a45f30SXin Li 
70*a3a45f30SXin Li   // The length of the first (ctrl) bzip2 stream. Negative values are invalid.
71*a3a45f30SXin Li   int64_t ctrl_len = -1;
72*a3a45f30SXin Li 
73*a3a45f30SXin Li   // The length of the first (diff) bzip2 stream. Negative values are invalid.
74*a3a45f30SXin Li   int64_t diff_len = -1;
75*a3a45f30SXin Li 
76*a3a45f30SXin Li   // The length of the first (diff) bzip2 stream. This value is not stored in
77*a3a45f30SXin Li   // the file, but generated based on the |file_size|.
78*a3a45f30SXin Li   uint64_t extra_len = 0;
79*a3a45f30SXin Li 
80*a3a45f30SXin Li   // The length of the new file after applying the patch. Negative values are
81*a3a45f30SXin Li   // invalid.
82*a3a45f30SXin Li   int64_t new_file_len = -1;
83*a3a45f30SXin Li 
84*a3a45f30SXin Li   // The three compressed streams.
85*a3a45f30SXin Li   std::vector<uint8_t> bz2_ctrl;
86*a3a45f30SXin Li   std::vector<uint8_t> bz2_diff;
87*a3a45f30SXin Li   std::vector<uint8_t> bz2_extra;
88*a3a45f30SXin Li 
89*a3a45f30SXin Li   uint64_t file_size = 0;
90*a3a45f30SXin Li };
91*a3a45f30SXin Li 
92*a3a45f30SXin Li 
93*a3a45f30SXin Li }  // namespace test_utils
94*a3a45f30SXin Li 
95*a3a45f30SXin Li 
96*a3a45f30SXin Li #endif  // _BSDIFF_TEST_UTILS_H_
97