Files
bsdiff/test_utils.h
Amin Hassani 1106bf7d9c bsdiff: Change all stderr and perror to LOG(ERROR)
We need to be able to turn off the logging at runtime. When performing fuzz
testing, the input data is mostly invalid. But the invalid input data causes all
the error logs in the output which is noisy, inefficient, and not
necessary. More information can be found in:
https://chromium.googlesource.com/chromium/src/+/master/testing/libfuzzer/getting_started.md#disable-noisy-error-message-logging

One option to turn off the logging at runtime is:

std::cerr.setstate(std::ios_base::failbit);

which works but not when we are doing fprintf to stderr. Another option
is in the lines of the following:

freopen("/dev/null", "w", stderr);

But, this seem to disable everything even useful needed logs.

This patch, changes all codes (except in bsdiff_main.cc) that logs using
fprintf() and perror() to the new uniform form of logging (defined in
logging.h). This new logging alternative allows universal manipulation of the
logging if necessary.

Bug: none
Test: unittests pass

Change-Id: I42bb3e5856c5c215dd82925ccdca44556124655d
2017-12-04 14:41:49 -08:00

97 lines
2.7 KiB
C++

// Copyright 2015 The Chromium OS Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef _BSDIFF_TEST_UTILS_H_
#define _BSDIFF_TEST_UTILS_H_
#include <gtest/gtest.h>
#include <string>
#include <vector>
#include "bsdiff/logging.h"
#define TEST_AND_RETURN_FALSE(_x) \
do { \
if (!static_cast<bool>(_x)) { \
LOG(ERROR) << #_x " failed."; \
return false; \
} \
} while (0)
namespace test_utils {
class BsdiffTestEnvironment : public ::testing::Environment {
public:
virtual void SetUp();
};
// Reads all the contents of the file |path| into |out|. Returns whether it
// read up to the end of file.
bool ReadFile(const std::string& path, std::vector<uint8_t>* out);
// Overrides the file |path| with the contents passed in |out|. Returns whether
// the operation succeeded.
bool WriteFile(const std::string& path, std::vector<uint8_t> contents);
// Utility class to create and delete a temp file.
class ScopedTempFile {
public:
// Creates a temp file with the passed |pattern|. The pattern should end with
// "XXXXXX", that will be replaced with a random string. The file will be
// removed when this instance is destroyed.
explicit ScopedTempFile(const std::string& pattern);
~ScopedTempFile();
std::string filename() const { return filename_; }
const char* c_str() const { return filename_.c_str(); }
// Releases the temporary file. It will not be deleted when this instance is
// destroyed.
void release() { filename_.clear(); }
private:
std::string filename_;
};
// This struct representes a parsed BSDIFF40 file.
struct BsdiffPatchFile {
static const size_t kHeaderSize = 32;
// Parses a BSDIFF40 file and stores the contents in the local methods.
bool LoadFromFile(const std::string& filename);
// Returns wheter the patch file is valid.
bool IsValid() const;
// The magic string in the header file. Normally "BSDIFF40".
std::string magic;
// The length of the first (ctrl) bzip2 stream. Negative values are invalid.
int64_t ctrl_len = -1;
// The length of the first (diff) bzip2 stream. Negative values are invalid.
int64_t diff_len = -1;
// The length of the first (diff) bzip2 stream. This value is not stored in
// the file, but generated based on the |file_size|.
uint64_t extra_len = 0;
// The length of the new file after applying the patch. Negative values are
// invalid.
int64_t new_file_len = -1;
// The three compressed streams.
std::vector<uint8_t> bz2_ctrl;
std::vector<uint8_t> bz2_diff;
std::vector<uint8_t> bz2_extra;
uint64_t file_size = 0;
};
} // namespace test_utils
#endif // _BSDIFF_TEST_UTILS_H_