1// Copyright (c) 2011 The LevelDB 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. See the AUTHORS file for names of contributors.
4
5#ifndef STORAGE_LEVELDB_UTIL_TESTUTIL_H_
6#define STORAGE_LEVELDB_UTIL_TESTUTIL_H_
7
8#include "gmock/gmock.h"
9#include "gtest/gtest.h"
10#include "helpers/memenv/memenv.h"
11#include "leveldb/env.h"
12#include "leveldb/slice.h"
13#include "util/random.h"
14
15namespace leveldb {
16namespace test {
17
18MATCHER(IsOK, "") { return arg.ok(); }
19
20// Macros for testing the results of functions that return leveldb::Status or
21// absl::StatusOr<T> (for any type T).
22#define EXPECT_LEVELDB_OK(expression) \
23 EXPECT_THAT(expression, leveldb::test::IsOK())
24#define ASSERT_LEVELDB_OK(expression) \
25 ASSERT_THAT(expression, leveldb::test::IsOK())
26
27// Returns the random seed used at the start of the current test run.
28inline int RandomSeed() {
29 return testing::UnitTest::GetInstance()->random_seed();
30}
31
32// Store in *dst a random string of length "len" and return a Slice that
33// references the generated data.
34Slice RandomString(Random* rnd, int len, std::string* dst);
35
36// Return a random key with the specified length that may contain interesting
37// characters (e.g. \x00, \xff, etc.).
38std::string RandomKey(Random* rnd, int len);
39
40// Store in *dst a string of length "len" that will compress to
41// "N*compressed_fraction" bytes and return a Slice that references
42// the generated data.
43Slice CompressibleString(Random* rnd, double compressed_fraction, size_t len,
44 std::string* dst);
45
46// A wrapper that allows injection of errors.
47class ErrorEnv : public EnvWrapper {
48 public:
49 bool writable_file_error_;
50 int num_writable_file_errors_;
51
52 ErrorEnv()
53 : EnvWrapper(NewMemEnv(Env::Default())),
54 writable_file_error_(false),
55 num_writable_file_errors_(0) {}
56 ~ErrorEnv() override { delete target(); }
57
58 Status NewWritableFile(const std::string& fname,
59 WritableFile** result) override {
60 if (writable_file_error_) {
61 ++num_writable_file_errors_;
62 *result = nullptr;
63 return Status::IOError(fname, "fake error");
64 }
65 return target()->NewWritableFile(fname, result);
66 }
67
68 Status NewAppendableFile(const std::string& fname,
69 WritableFile** result) override {
70 if (writable_file_error_) {
71 ++num_writable_file_errors_;
72 *result = nullptr;
73 return Status::IOError(fname, "fake error");
74 }
75 return target()->NewAppendableFile(fname, result);
76 }
77};
78
79} // namespace test
80} // namespace leveldb
81
82#endif // STORAGE_LEVELDB_UTIL_TESTUTIL_H_
83