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 "leveldb/env.h" 9 #include "leveldb/slice.h" 10 #include "util/random.h" 11 12 namespace leveldb { 13 namespace test { 14 15 // Store in *dst a random string of length "len" and return a Slice that 16 // references the generated data. 17 extern 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.). 21 extern 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. 26 extern Slice CompressibleString(Random* rnd, double compressed_fraction, 27 size_t len, std::string* dst); 28 29 // A wrapper that allows injection of errors. 30 class 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 } // namespace test 51 } // namespace leveldb 52 53 #endif // STORAGE_LEVELDB_UTIL_TESTUTIL_H_ 54