File: test_db.h

package info (click to toggle)
golang-github-google-certificate-transparency 0.0~git20160709.0.0f6e3d1~ds1-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, buster
  • size: 5,676 kB
  • sloc: cpp: 35,278; python: 11,838; java: 1,911; sh: 1,885; makefile: 950; xml: 520; ansic: 225
file content (70 lines) | stat: -rw-r--r-- 1,844 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#ifndef CERT_TRANS_UTIL_TEST_DB_H_
#define CERT_TRANS_UTIL_TEST_DB_H_

#include <gflags/gflags.h>
#include <glog/logging.h>
#include <stdlib.h>

#include "base/macros.h"
#include "util/util.h"

DEFINE_string(database_test_dir, "/tmp",
              "Test directory for databases that use the disk. We attempt to "
              "remove all created files and directories but data may be left "
              "behind if the program does not exit cleanly.");

class TmpStorage {
 public:
  TmpStorage() : tmp_dir_(FLAGS_database_test_dir) {
    file_base_ = util::CreateTemporaryDirectory(tmp_dir_ + "/ctlogXXXXXX");
    CHECK_EQ(tmp_dir_ + "/ctlog", file_base_.substr(0, tmp_dir_.size() + 6));
    CHECK_EQ(tmp_dir_.size() + 12, file_base_.length());
  }

  ~TmpStorage() {
    // Check again that it is safe to empty file_base_.
    CHECK_EQ(tmp_dir_ + "/ctlog", file_base_.substr(0, tmp_dir_.size() + 6));
    CHECK_EQ(tmp_dir_.size() + 12, file_base_.length());

    std::string command = "rm -r " + file_base_;
    CHECK_ERR(system(command.c_str()))
        << "Failed to delete temporary directory in " << file_base_;
  }

  std::string TmpStorageDir() const {
    return file_base_;
  }

 private:
  std::string tmp_dir_;
  std::string file_base_;
};

// Helper for generating test instances of the databases for typed tests.
template <class T>
class TestDB {
 public:
  TestDB() : tmp_() {
    Setup();
  }

  void Setup();

  T* db() const {
    return db_.get();
  }

  // Build a second database from the current disk state. Caller owns result.
  // Meant to be used for testing resumes from disk.
  // Concurrent behaviour is undefined (depends on the Database
  // implementation).
  T* SecondDB();

 private:
  TmpStorage tmp_;
  std::unique_ptr<T> db_;

  DISALLOW_COPY_AND_ASSIGN(TestDB);
};

#endif  // CERT_TRANS_UTIL_TEST_DB_H_