File: log_lookup_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 (239 lines) | stat: -rw-r--r-- 7,045 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
/* -*- indent-tabs-mode: nil -*- */
#include <gtest/gtest.h>
#include <memory>
#include <string>

#include "log/etcd_consistent_store.h"
#include "log/file_db.h"
#include "log/file_storage.h"
#include "log/log_lookup.h"
#include "log/log_signer.h"
#include "log/log_verifier.h"
#include "log/logged_entry.h"
#include "log/sqlite_db.h"
#include "log/test_db.h"
#include "log/test_signer.h"
#include "log/tree_signer.h"
#include "merkletree/merkle_verifier.h"
#include "merkletree/serial_hasher.h"
#include "proto/cert_serializer.h"
#include "util/fake_etcd.h"
#include "util/mock_masterelection.h"
#include "util/sync_task.h"
#include "util/testing.h"
#include "util/thread_pool.h"
#include "util/util.h"

namespace {

namespace libevent = cert_trans::libevent;

using cert_trans::Database;
using cert_trans::EntryHandle;
using cert_trans::EtcdClient;
using cert_trans::FakeEtcdClient;
using cert_trans::FileDB;
using cert_trans::LogLookup;
using cert_trans::LoggedEntry;
using cert_trans::MockMasterElection;
using cert_trans::SQLiteDB;
using cert_trans::ThreadPool;
using cert_trans::TreeSigner;
using ct::MerkleAuditProof;
using ct::SequenceMapping;
using std::make_shared;
using std::shared_ptr;
using std::string;
using std::unique_ptr;
using testing::NiceMock;


template <class T>
class LogLookupTest : public ::testing::Test {
 protected:
  LogLookupTest()
      : test_db_(),
        base_(make_shared<libevent::Base>()),
        event_pump_(base_),
        etcd_client_(base_.get()),
        pool_(2),
        store_(base_.get(), &pool_, &etcd_client_, &election_, "/root", "id"),
        test_signer_(),
        log_signer_(TestSigner::DefaultLogSigner()),
        tree_signer_(std::chrono::duration<double>(0), db(),
                     unique_ptr<CompactMerkleTree>(new CompactMerkleTree(
                         unique_ptr<Sha256Hasher>(new Sha256Hasher))),
                     &store_, log_signer_.get()),
        verifier_(TestSigner::DefaultLogSigVerifier(),
                  new MerkleVerifier(
                      unique_ptr<Sha256Hasher>(new Sha256Hasher))) {
    // Set some noddy STH so that we can call UpdateTree on the Tree Signer.
    store_.SetServingSTH(ct::SignedTreeHead());
    // Force an empty sequence mapping file:
    {
      util::SyncTask task(&pool_);
      EtcdClient::Response r;
      etcd_client_.ForceSet("/root/sequence_mapping", "", &r, task.task());
      task.Wait();
    }
  }


  void CreateSequencedEntry(LoggedEntry* logged_cert, int64_t seq) {
    CHECK_NOTNULL(logged_cert);
    CHECK_GE(seq, 0);
    logged_cert->clear_sequence_number();

    CHECK(this->store_.AddPendingEntry(logged_cert).ok());

    EntryHandle<SequenceMapping> mapping;
    CHECK(this->store_.GetSequenceMapping(&mapping).ok());
    SequenceMapping::Mapping* m(mapping.MutableEntry()->add_mapping());
    m->set_sequence_number(seq);
    m->set_entry_hash(logged_cert->Hash());
    CHECK(this->store_.UpdateSequenceMapping(&mapping).ok());
  }

  void UpdateTree() {
    // first need to populate the local DB with the sequenced entries in etcd:
    EntryHandle<SequenceMapping> mapping;
    CHECK(this->store_.GetSequenceMapping(&mapping).ok());

    for (const auto& m : mapping.Entry().mapping()) {
      EntryHandle<LoggedEntry> entry;
      CHECK_EQ(util::Status::OK,
               this->store_.GetPendingEntryForHash(m.entry_hash(), &entry));
      entry.MutableEntry()->set_sequence_number(m.sequence_number());
      CHECK_EQ(this->db()->OK,
               this->db()->CreateSequencedEntry(entry.Entry()));
    }

    // then do the actual update.
    EXPECT_EQ(TreeSigner::OK, this->tree_signer_.UpdateTree());
    this->db()->WriteTreeHead(this->tree_signer_.LatestSTH());
  }

  T* db() const {
    return test_db_.db();
  }


  TestDB<T> test_db_;
  shared_ptr<libevent::Base> base_;
  libevent::EventPumpThread event_pump_;
  FakeEtcdClient etcd_client_;
  ThreadPool pool_;
  NiceMock<MockMasterElection> election_;
  cert_trans::EtcdConsistentStore store_;
  TestSigner test_signer_;
  unique_ptr<LogSigner> log_signer_;
  TreeSigner tree_signer_;
  LogVerifier verifier_;
};


typedef testing::Types<FileDB, SQLiteDB> Databases;

TYPED_TEST_CASE(LogLookupTest, Databases);


TYPED_TEST(LogLookupTest, Lookup) {
  LoggedEntry logged_cert;
  this->test_signer_.CreateUnique(&logged_cert);
  this->CreateSequencedEntry(&logged_cert, 0);

  MerkleAuditProof proof;
  this->UpdateTree();

  LogLookup lookup(this->db());
  // Look the new entry up.
  EXPECT_EQ(LogLookup::OK,
            lookup.AuditProof(logged_cert.merkle_leaf_hash(), &proof));
}


TYPED_TEST(LogLookupTest, NotFound) {
  LoggedEntry logged_cert;
  this->test_signer_.CreateUnique(&logged_cert);
  this->CreateSequencedEntry(&logged_cert, 0);

  MerkleAuditProof proof;
  this->UpdateTree();

  LogLookup lookup(this->db());

  // Look up using a wrong hash.
  string hash = this->test_signer_.UniqueHash();
  EXPECT_EQ(LogLookup::NOT_FOUND, lookup.AuditProof(hash, &proof));
}


TYPED_TEST(LogLookupTest, Update) {
  LogLookup lookup(this->db());
  LoggedEntry logged_cert;
  this->test_signer_.CreateUnique(&logged_cert);
  this->CreateSequencedEntry(&logged_cert, 0);

  MerkleAuditProof proof;
  this->UpdateTree();

  // Look the new entry up.
  EXPECT_EQ(LogLookup::OK,
            lookup.AuditProof(logged_cert.merkle_leaf_hash(), &proof));
}


// Verify that the audit proof constructed is correct (assuming the signer
// operates correctly). TODO(ekasper): KAT tests.
TYPED_TEST(LogLookupTest, Verify) {
  LoggedEntry logged_cert;
  this->test_signer_.CreateUnique(&logged_cert);
  this->CreateSequencedEntry(&logged_cert, 0);

  MerkleAuditProof proof;
  this->UpdateTree();

  LogLookup lookup(this->db());
  // Look the new entry up.
  EXPECT_EQ(LogLookup::OK,
            lookup.AuditProof(logged_cert.merkle_leaf_hash(), &proof));
  EXPECT_EQ(LogVerifier::VERIFY_OK,
            this->verifier_.VerifyMerkleAuditProof(logged_cert.entry(),
                                                   logged_cert.sct(), proof));
}


// Build a bigger tree so that we actually verify a non-empty path.
TYPED_TEST(LogLookupTest, VerifyWithPath) {
  LoggedEntry logged_certs[13];

  // Make the tree not balanced for extra fun.
  for (int i = 0; i < 13; ++i) {
    this->test_signer_.CreateUnique(&logged_certs[i]);
    this->CreateSequencedEntry(&logged_certs[i], i);
  }

  this->UpdateTree();

  LogLookup lookup(this->db());
  MerkleAuditProof proof;

  for (int i = 0; i < 13; ++i) {
    EXPECT_EQ(LogLookup::OK,
              lookup.AuditProof(logged_certs[i].merkle_leaf_hash(), &proof));
    EXPECT_EQ(LogVerifier::VERIFY_OK,
              this->verifier_.VerifyMerkleAuditProof(logged_certs[i].entry(),
                                                     logged_certs[i].sct(),
                                                     proof));
  }
}


}  // namespace


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