blob: a150c1a7951769c69541f64a51c120bb37c82e14 [file] [log] [blame]
jorlow@chromium.org179be582011-03-18 22:37:00 +00001// 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
jorlow@chromium.orgfbd97aa2011-03-30 18:35:40 +00008#include "leveldb/env.h"
9#include "leveldb/slice.h"
jorlow@chromium.org179be582011-03-18 22:37:00 +000010#include "util/random.h"
11
12namespace leveldb {
13namespace test {
14
15// Store in *dst a random string of length "len" and return a Slice that
16// references the generated data.
17extern Slice RandomString(Random* rnd, int len, std::string* dst);
18
19// Return a random key with the specified length that may contain interesting
20// characters (e.g. \x00, \xff, etc.).
21extern std::string RandomKey(Random* rnd, int len);
22
23// Store in *dst a string of length "len" that will compress to
24// "N*compressed_fraction" bytes and return a Slice that references
25// the generated data.
26extern Slice CompressibleString(Random* rnd, double compressed_fraction,
27 int len, std::string* dst);
28
29// A wrapper that allows injection of errors.
30class ErrorEnv : public EnvWrapper {
31 public:
32 bool writable_file_error_;
33 int num_writable_file_errors_;
34
35 ErrorEnv() : EnvWrapper(Env::Default()),
36 writable_file_error_(false),
37 num_writable_file_errors_(0) { }
38
39 virtual Status NewWritableFile(const std::string& fname,
40 WritableFile** result) {
41 if (writable_file_error_) {
42 ++num_writable_file_errors_;
43 *result = NULL;
44 return Status::IOError(fname, "fake error");
45 }
46 return target()->NewWritableFile(fname, result);
47 }
48};
49
50}
51}
52
53#endif // STORAGE_LEVELDB_UTIL_TESTUTIL_H_