File: file_storage_test.cc

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 (366 lines) | stat: -rw-r--r-- 11,300 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
#include <errno.h>
#include <gtest/gtest.h>
#include <stdio.h>
#include <sys/stat.h>
#include <unistd.h>
#include <iostream>
#include <set>
#include <string>

#include "log/file_storage.h"
#include "log/filesystem_ops.h"
#include "log/test_db.h"
#include "util/status_test_util.h"
#include "util/testing.h"
#include "util/util.h"

using cert_trans::FailingFilesystemOps;
using cert_trans::FileStorage;
using std::string;
using util::testing::StatusIs;

namespace {

const unsigned kStorageDepth = 3;

class BasicFileStorageTest : public ::testing::Test {
 protected:
  BasicFileStorageTest() : test_db_() {
  }

  FileStorage* fs() const {
    return test_db_.db();
  }

  TestDB<FileStorage> test_db_;
};

TEST_F(BasicFileStorageTest, Create) {
  string key0("1234xyzw", 8);
  string value0("unicorn", 7);

  string key1("1245abcd", 8);
  string value1("Alice", 5);

  EXPECT_THAT(fs()->LookupEntry(key0, NULL), StatusIs(util::error::NOT_FOUND));
  EXPECT_THAT(fs()->LookupEntry(key1, NULL), StatusIs(util::error::NOT_FOUND));

  EXPECT_OK(fs()->CreateEntry(key0, value0));
  string lookup_result;
  EXPECT_OK(fs()->LookupEntry(key0, &lookup_result));
  EXPECT_EQ(value0, lookup_result);

  EXPECT_OK(fs()->CreateEntry(key1, value1));
  EXPECT_OK(fs()->LookupEntry(key1, &lookup_result));
  EXPECT_EQ(value1, lookup_result);
}

TEST_F(BasicFileStorageTest, Scan) {
  string key0("1234xyzw", 8);
  string value0("unicorn", 7);

  string key1("1245abcd", 8);
  string value1("Alice", 5);

  EXPECT_OK(fs()->CreateEntry(key0, value0));
  EXPECT_OK(fs()->CreateEntry(key1, value1));

  std::set<string> keys;
  keys.insert(key0);
  keys.insert(key1);

  std::set<string> scan_keys = fs()->Scan();
  EXPECT_EQ(keys, scan_keys);
}

TEST_F(BasicFileStorageTest, CreateDuplicate) {
  string key("1234xyzw", 8);
  string value("unicorn", 7);

  EXPECT_THAT(fs()->LookupEntry(key, NULL), StatusIs(util::error::NOT_FOUND));
  EXPECT_OK(fs()->CreateEntry(key, value));
  string lookup_result;
  EXPECT_OK(fs()->LookupEntry(key, &lookup_result));
  EXPECT_EQ(value, lookup_result);

  // Try to log another entry with the same key.
  string new_value("alice", 5);
  EXPECT_THAT(fs()->CreateEntry(key, new_value),
              StatusIs(util::error::ALREADY_EXISTS));
  lookup_result.clear();
  EXPECT_OK(fs()->LookupEntry(key, &lookup_result));

  // Expect to receive the original entry on lookup.
  EXPECT_EQ(value, lookup_result);
}

TEST_F(BasicFileStorageTest, Update) {
  string key("1234xyzw", 8);
  string value("unicorn", 7);

  EXPECT_THAT(fs()->LookupEntry(key, NULL), StatusIs(util::error::NOT_FOUND));
  EXPECT_OK(fs()->CreateEntry(key, value));
  string lookup_result;
  EXPECT_OK(fs()->LookupEntry(key, &lookup_result));
  EXPECT_EQ(value, lookup_result);

  // Update.
  string new_value("alice", 5);
  EXPECT_OK(fs()->UpdateEntry(key, new_value));
  EXPECT_OK(fs()->LookupEntry(key, &lookup_result));

  // Expect to receive the new entry on lookup.
  EXPECT_EQ(new_value, lookup_result);
}

// Test for non-existing keys that are similar to  existing ones.
TEST_F(BasicFileStorageTest, LookupInvalidKey) {
  string key("1234xyzw", 8);
  string value("unicorn", 7);

  string similar_key0("1234xyz", 7);
  string similar_key1("1234xyzv", 8);
  string similar_key2("123", 3);
  string empty_key;

  EXPECT_OK(fs()->CreateEntry(key, value));
  EXPECT_OK(fs()->LookupEntry(key, NULL));
  EXPECT_THAT(fs()->LookupEntry(similar_key0, NULL),
              StatusIs(util::error::NOT_FOUND));
  EXPECT_THAT(fs()->LookupEntry(similar_key1, NULL),
              StatusIs(util::error::NOT_FOUND));
  EXPECT_THAT(fs()->LookupEntry(similar_key2, NULL),
              StatusIs(util::error::NOT_FOUND));
  EXPECT_THAT(fs()->LookupEntry(empty_key, NULL),
              StatusIs(util::error::NOT_FOUND));
}

TEST_F(BasicFileStorageTest, Resume) {
  string key0("1234xyzw", 8);
  string value0("unicorn", 7);

  string key1("1245abcd", 8);
  string value1("Alice", 5);

  EXPECT_OK(fs()->CreateEntry(key0, value0));
  EXPECT_OK(fs()->CreateEntry(key1, value1));

  // A second database.
  FileStorage* db2 = test_db_.SecondDB();

  // Look up and expect to find the entries.
  string lookup_result;
  EXPECT_OK(db2->LookupEntry(key0, &lookup_result));
  EXPECT_EQ(value0, lookup_result);

  EXPECT_OK(db2->LookupEntry(key1, &lookup_result));
  EXPECT_EQ(value1, lookup_result);

  delete db2;
};

TEST_F(BasicFileStorageTest, ScanOnResume) {
  string key0("1234xyzw", 8);
  string value0("unicorn", 7);

  string key1("1245abcd", 8);
  string value1("Alice", 5);

  EXPECT_OK(fs()->CreateEntry(key0, value0));
  EXPECT_OK(fs()->CreateEntry(key1, value1));

  // A second database.
  FileStorage* db2 = test_db_.SecondDB();

  std::set<string> keys;
  keys.insert(key0);
  keys.insert(key1);

  std::set<string> scan_keys = db2->Scan();
  EXPECT_EQ(keys, scan_keys);
  delete db2;
}

class FailingFileStorageDeathTest : public ::testing::Test {
 protected:
  string GetTemporaryDirectory() {
    return util::CreateTemporaryDirectory(tmp_.TmpStorageDir() +
                                          "/ctlogXXXXXX");
  }
  TmpStorage tmp_;
};

TEST(DeathTest, SupportDeath) {
#ifndef EXPECT_DEATH
  FAIL() << "Death tests not supported on this platform.";
#endif
};

// TODO(ekasper): death tests throw the following warning
// (at least on some platforms):
//
// [WARNING] ../src/gtest-death-test.cc:789:: Death tests use fork(),
// which is unsafe particularly in a threaded context.
// For this test, Google Test couldn't detect the number of threads.
//
// Find out why.

TEST_F(FailingFileStorageDeathTest, DieOnFailedCreate) {
  // Profiling run: count file operations.
  FailingFilesystemOps* failing_file_op = new FailingFilesystemOps(-1);
  FileStorage db(GetTemporaryDirectory(), kStorageDepth, failing_file_op);

  // Count ops for constructor.
  int op_count_init = failing_file_op->OpCount();
  ASSERT_GE(op_count_init, 0);

  string key0("1234xyzw", 8);
  string value0("unicorn", 7);

  EXPECT_OK(db.CreateEntry(key0, value0));
  int op_count0 = failing_file_op->OpCount();
  ASSERT_GT(op_count0, op_count_init);

  string key1("1245abcd", 8);
  string value1("Alice", 5);

  EXPECT_OK(db.CreateEntry(key1, value1));
  int op_count1 = failing_file_op->OpCount();
  ASSERT_GT(op_count1, op_count0);

  // Real run. Repeat for each file op individually.
  for (int i = op_count_init; i < op_count0; ++i) {
    FileStorage db(GetTemporaryDirectory(), kStorageDepth,
                   new FailingFilesystemOps(i));
    EXPECT_DEATH_IF_SUPPORTED(db.CreateEntry(key0, value0), "");
  }

  for (int i = op_count0; i < op_count1; ++i) {
    FileStorage db(GetTemporaryDirectory(), kStorageDepth,
                   new FailingFilesystemOps(i));
    EXPECT_OK(db.CreateEntry(key0, value0));
    EXPECT_DEATH_IF_SUPPORTED(db.CreateEntry(key1, value1), "");
  }
};

TEST_F(FailingFileStorageDeathTest, DieOnFailedUpdate) {
  // Profiling run: count file operations.
  FailingFilesystemOps* failing_file_op = new FailingFilesystemOps(-1);
  FileStorage db(GetTemporaryDirectory(), kStorageDepth, failing_file_op);

  string key("1234xyzw", 8);
  string value("unicorn", 7);

  EXPECT_OK(db.CreateEntry(key, value));
  int op_count0 = failing_file_op->OpCount();
  ASSERT_GT(op_count0, 0);

  string new_value("Alice", 5);

  EXPECT_OK(db.UpdateEntry(key, new_value));
  int op_count1 = failing_file_op->OpCount();
  ASSERT_GT(op_count1, op_count0);

  // Real run. Repeat for each file op individually.
  for (int i = op_count0; i < op_count1; ++i) {
    FileStorage db(GetTemporaryDirectory(), kStorageDepth,
                   new FailingFilesystemOps(i));
    EXPECT_OK(db.CreateEntry(key, value));
    EXPECT_DEATH_IF_SUPPORTED(db.UpdateEntry(key, new_value), "");
  }
};

TEST_F(FailingFileStorageDeathTest, ResumeOnFailedCreate) {
  // Profiling run: count file operations.
  FailingFilesystemOps* failing_file_op = new FailingFilesystemOps(-1);
  FileStorage db(GetTemporaryDirectory(), kStorageDepth, failing_file_op);

  string key0("1234xyzw", 8);
  string value0("unicorn", 7);

  int op_count_init = failing_file_op->OpCount();
  EXPECT_OK(db.CreateEntry(key0, value0));
  int op_count0 = failing_file_op->OpCount();
  ASSERT_GT(op_count0, 0);

  string key1("1245abcd", 8);
  string value1("Alice", 5);

  EXPECT_OK(db.CreateEntry(key1, value1));
  int op_count1 = failing_file_op->OpCount();
  ASSERT_GT(op_count1, op_count0);

  // Real run. Repeat for each file op individually.
  for (int i = op_count_init; i < op_count0; ++i) {
    string db_dir = GetTemporaryDirectory();
    FileStorage db(db_dir, kStorageDepth, new FailingFilesystemOps(i));
    EXPECT_DEATH_IF_SUPPORTED(db.CreateEntry(key0, value0), "");
    FileStorage db2(db_dir, kStorageDepth);
    // Entry should not be there, and we should be able to insert it.
    EXPECT_THAT(db2.LookupEntry(key0, NULL), StatusIs(util::error::NOT_FOUND));
    EXPECT_OK(db2.CreateEntry(key0, value0));
    // Look it up to double-check that everything works.
    string lookup_result;
    EXPECT_OK(db2.LookupEntry(key0, &lookup_result));
    EXPECT_EQ(value0, lookup_result);
  }

  for (int i = op_count0; i < op_count1; ++i) {
    string db_dir = GetTemporaryDirectory();
    FileStorage db(db_dir, kStorageDepth, new FailingFilesystemOps(i));
    EXPECT_OK(db.CreateEntry(key0, value0));
    EXPECT_DEATH_IF_SUPPORTED(db.CreateEntry(key1, value1), "");
    FileStorage db2(db_dir, kStorageDepth);
    // First entry should be there just fine.
    string lookup_result;
    EXPECT_OK(db2.LookupEntry(key0, &lookup_result));
    EXPECT_EQ(value0, lookup_result);

    // Second entry should not be there, and we should be able to insert it.
    EXPECT_THAT(db2.LookupEntry(key1, NULL), StatusIs(util::error::NOT_FOUND));
    EXPECT_OK(db2.CreateEntry(key1, value1));
    // Look it up to double-check that everything works.
    EXPECT_OK(db2.LookupEntry(key1, &lookup_result));
    EXPECT_EQ(value1, lookup_result);
  }
}

TEST_F(FailingFileStorageDeathTest, ResumeOnFailedUpdate) {
  // Profiling run: count file operations.
  FailingFilesystemOps* failing_file_op = new FailingFilesystemOps(-1);
  FileStorage db(GetTemporaryDirectory(), kStorageDepth, failing_file_op);

  string key("1234xyzw", 8);
  string value("unicorn", 7);

  EXPECT_OK(db.CreateEntry(key, value));
  int op_count0 = failing_file_op->OpCount();
  ASSERT_GT(op_count0, 0);

  string new_value("Alice", 5);

  EXPECT_OK(db.UpdateEntry(key, new_value));
  int op_count1 = failing_file_op->OpCount();
  ASSERT_GT(op_count1, op_count0);

  // Real run. Repeat for each file op individually.
  for (int i = op_count0; i < op_count1; ++i) {
    string db_dir = GetTemporaryDirectory();
    FileStorage db(db_dir, kStorageDepth, new FailingFilesystemOps(i));
    EXPECT_OK(db.CreateEntry(key, value));
    EXPECT_DEATH_IF_SUPPORTED(db.UpdateEntry(key, new_value), "");
    FileStorage db2(db_dir, kStorageDepth);
    // The entry should be there just fine...
    string lookup_result;
    EXPECT_OK(db2.LookupEntry(key, &lookup_result));
    // ... but it should still have its old value.
    EXPECT_EQ(value, lookup_result);
  }
}

}  // namespace

int main(int argc, char** argv) {
  cert_trans::test::InitTesting(argv[0], &argc, &argv, true);
  return RUN_ALL_TESTS();
}