File: strict_consistent_store_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 (135 lines) | stat: -rw-r--r-- 3,347 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
#include "log/strict_consistent_store.h"

#include <gflags/gflags.h>
#include <gmock/gmock.h>
#include <gtest/gtest.h>

#include "log/logged_entry.h"
#include "log/mock_consistent_store.h"
#include "util/mock_masterelection.h"
#include "util/status.h"
#include "util/status_test_util.h"
#include "util/statusor.h"
#include "util/testing.h"
#include "util/util.h"

DECLARE_int32(node_state_ttl_seconds);

namespace cert_trans {

using ct::SequenceMapping;
using testing::_;
using testing::NiceMock;
using testing::Return;
using util::Status;
using util::StatusOr;
using util::testing::StatusIs;


class StrictConsistentStoreTest : public ::testing::TestWithParam<bool> {
 public:
  StrictConsistentStoreTest()
      : peer_(new NiceMock<MockConsistentStore>()),
        strict_store_(&election_, peer_) {
    ON_CALL(election_, IsMaster()).WillByDefault(Return(IsMaster()));
  }

 protected:
  bool IsMaster() const {
    return GetParam();
  }

  NiceMock<MockMasterElection> election_;
  // strict_store_ takes ownership of this:
  NiceMock<MockConsistentStore>* peer_;
  StrictConsistentStore strict_store_;
};


TEST_P(StrictConsistentStoreTest, TestNextAvailableSequenceNumber) {
  if (IsMaster()) {
    EXPECT_CALL(*peer_, NextAvailableSequenceNumber()).WillOnce(Return(123));
  } else {
    EXPECT_CALL(*peer_, NextAvailableSequenceNumber()).Times(0);
  }

  util::StatusOr<int64_t> seq(strict_store_.NextAvailableSequenceNumber());

  if (IsMaster()) {
    EXPECT_EQ(123, seq.ValueOrDie());
  } else {
    EXPECT_FALSE(seq.ok());
    EXPECT_THAT(seq.status(), StatusIs(util::error::PERMISSION_DENIED));
  }
}


TEST_P(StrictConsistentStoreTest, TestSetServingSTH) {
  if (IsMaster()) {
    EXPECT_CALL(*peer_, SetServingSTH(_)).WillOnce(Return(util::Status::OK));
  } else {
    EXPECT_CALL(*peer_, SetServingSTH(_)).Times(0);
  }

  ct::SignedTreeHead sth;
  sth.set_timestamp(234);
  util::Status status(strict_store_.SetServingSTH(sth));

  if (IsMaster()) {
    EXPECT_OK(status);
  } else {
    EXPECT_THAT(status, StatusIs(util::error::PERMISSION_DENIED));
  }
}


TEST_P(StrictConsistentStoreTest, TestUpdateSequenceMapping) {
  if (IsMaster()) {
    EXPECT_CALL(*peer_, UpdateSequenceMapping(_))
        .WillOnce(Return(util::Status::OK));
  } else {
    EXPECT_CALL(*peer_, UpdateSequenceMapping(_)).Times(0);
  }

  EntryHandle<SequenceMapping> mapping;
  util::Status status(strict_store_.UpdateSequenceMapping(&mapping));

  if (IsMaster()) {
    EXPECT_TRUE(status.ok());
  } else {
    EXPECT_FALSE(status.ok());
    EXPECT_THAT(status, StatusIs(util::error::PERMISSION_DENIED));
  }
}


TEST_P(StrictConsistentStoreTest, TestClusterConfig) {
  if (IsMaster()) {
    EXPECT_CALL(*peer_, SetClusterConfig(_))
        .WillOnce(Return(util::Status::OK));
  } else {
    EXPECT_CALL(*peer_, SetClusterConfig(_)).Times(0);
  }

  ct::ClusterConfig conf;
  util::Status status(strict_store_.SetClusterConfig(conf));

  if (IsMaster()) {
    EXPECT_TRUE(status.ok());
  } else {
    EXPECT_THAT(status, StatusIs(util::error::PERMISSION_DENIED));
  }
}


INSTANTIATE_TEST_CASE_P(MasterInstance, StrictConsistentStoreTest,
                        testing::Values(true, false));


}  // namespace cert_trans


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