aboutsummaryrefslogtreecommitdiff
path: root/src/access_revocation_manager_impl_unittest.cc
blob: cd5887f684dd33b64686403fdd39aeded6ce0883 (plain)
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
// Copyright 2016 The Weave Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "src/access_revocation_manager_impl.h"

#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include <weave/provider/test/mock_config_store.h>
#include <weave/test/unittest_utils.h>

#include "src/test/mock_clock.h"
#include "src/bind_lambda.h"

using testing::_;
using testing::Return;
using testing::StrictMock;

namespace weave {

class AccessRevocationManagerImplTest : public testing::Test {
 protected:
  void SetUp() {
    std::string to_load = R"([{
      "user": "BQID",
      "app": "BwQF",
      "expiration": 463315200,
      "revocation": 463314200
    }, {
      "user": "AQID",
      "app": "AwQF",
      "expiration": 473315199,
      "revocation": 473313199
    }])";

    EXPECT_CALL(config_store_, LoadSettings("black_list"))
        .WillOnce(Return(to_load));

    EXPECT_CALL(config_store_, SaveSettings("black_list", _, _))
        .WillOnce(testing::WithArgs<1, 2>(testing::Invoke(
            [](const std::string& json, const DoneCallback& callback) {
              std::string to_save = R"([{
                "user": "AQID",
                "app": "AwQF",
                "expiration": 473315199,
                "revocation": 473313199
              }])";
              EXPECT_JSON_EQ(to_save, *test::CreateValue(json));
              if (!callback.is_null())
                callback.Run(nullptr);
            })));

    EXPECT_CALL(clock_, Now())
        .WillRepeatedly(Return(base::Time::FromTimeT(1412121212)));
    manager_.reset(
        new AccessRevocationManagerImpl{&config_store_, 10, &clock_});
  }
  StrictMock<test::MockClock> clock_;
  StrictMock<provider::test::MockConfigStore> config_store_{false};
  std::unique_ptr<AccessRevocationManagerImpl> manager_;
};

TEST_F(AccessRevocationManagerImplTest, Init) {
  EXPECT_EQ(1u, manager_->GetSize());
  EXPECT_EQ(10u, manager_->GetCapacity());
  EXPECT_EQ((std::vector<AccessRevocationManagerImpl::Entry>{{
                {1, 2, 3},
                {3, 4, 5},
                base::Time::FromTimeT(1419997999),
                base::Time::FromTimeT(1419999999),
            }}),
            manager_->GetEntries());
}

TEST_F(AccessRevocationManagerImplTest, Block) {
  bool callback_called = false;
  manager_->AddEntryAddedCallback(
      base::Bind([](bool* callback_called) { *callback_called = true; },
                 base::Unretained(&callback_called)));
  EXPECT_CALL(config_store_, SaveSettings("black_list", _, _))
      .WillOnce(testing::WithArgs<1, 2>(testing::Invoke(
          [](const std::string& json, const DoneCallback& callback) {
            std::string to_save = R"([{
                "user": "AQID",
                "app": "AwQF",
                "expiration": 473315199,
                "revocation": 473313199
              }, {
                "app": "CAgI",
                "user": "BwcH",
                "expiration": 473305200,
                "revocation": 473295200
              }])";
            EXPECT_JSON_EQ(to_save, *test::CreateValue(json));
            if (!callback.is_null())
              callback.Run(nullptr);
          })));
  manager_->Block({{7, 7, 7},
                   {8, 8, 8},
                   base::Time::FromTimeT(1419980000),
                   base::Time::FromTimeT(1419990000)},
                  {});
  EXPECT_TRUE(callback_called);
}

TEST_F(AccessRevocationManagerImplTest, BlockExpired) {
  manager_->Block({{},
                   {},
                   base::Time::FromTimeT(1300000000),
                   base::Time::FromTimeT(1400000000)},
                  base::Bind([](ErrorPtr error) {
                    EXPECT_TRUE(error->HasError("aleady_expired"));
                  }));
}

TEST_F(AccessRevocationManagerImplTest, BlockListOverflow) {
  EXPECT_CALL(config_store_, LoadSettings("black_list")).WillOnce(Return(""));
  manager_.reset(new AccessRevocationManagerImpl{&config_store_, 10, &clock_});

  EXPECT_CALL(config_store_, SaveSettings("black_list", _, _))
      .WillRepeatedly(testing::WithArgs<1, 2>(testing::Invoke(
          [](const std::string& json, const DoneCallback& callback) {
            if (!callback.is_null())
              callback.Run(nullptr);
          })));

  EXPECT_EQ(0u, manager_->GetSize());

  // Trigger overflow several times.
  for (size_t i = 0; i < manager_->GetCapacity() + 3; ++i) {
    bool callback_called = false;
    manager_->Block({{99, static_cast<uint8_t>(i), static_cast<uint8_t>(i)},
                     {8, 8, 8},
                     base::Time::FromTimeT(1419970000 + i),
                     base::Time::FromTimeT(1419990000)},
                    base::Bind([](bool* callback_called, ErrorPtr error) {
                      *callback_called = true;
                      EXPECT_FALSE(error);
                    }, base::Unretained(&callback_called)));
    EXPECT_TRUE(callback_called);
  }
  EXPECT_EQ(manager_->GetCapacity(), manager_->GetSize());

  // We didn't block these ids, so we can use this to check if all_blocking
  // issue is set for correct revocation time.
  EXPECT_TRUE(manager_->IsBlocked({1}, {2}, base::Time::FromTimeT(1419970003)));
  EXPECT_FALSE(
      manager_->IsBlocked({1}, {2}, base::Time::FromTimeT(1419970004)));

  // Check if all blocking rules still work.
  for (size_t i = 0; i < manager_->GetCapacity() + 3; ++i) {
    EXPECT_TRUE(manager_->IsBlocked(
        {99, static_cast<uint8_t>(i), static_cast<uint8_t>(i)}, {8, 8, 8},
        base::Time::FromTimeT(1419970000 + i)));
  }
}

TEST_F(AccessRevocationManagerImplTest, IsBlockedIdsNotMacth) {
  EXPECT_FALSE(manager_->IsBlocked({7, 7, 7}, {8, 8, 8}, {}));
}

TEST_F(AccessRevocationManagerImplTest, IsBlockedRevocationIsOld) {
  // Ids match but delegation time is newer than revocation time.
  EXPECT_FALSE(manager_->IsBlocked({1, 2, 3}, {3, 4, 5},
                                   base::Time::FromTimeT(1429997999)));
}

class AccessRevocationManagerImplIsBlockedTest
    : public AccessRevocationManagerImplTest,
      public testing::WithParamInterface<
          std::tuple<std::vector<uint8_t>, std::vector<uint8_t>>> {
 public:
  void SetUp() override {
    AccessRevocationManagerImplTest::SetUp();
    EXPECT_CALL(config_store_, SaveSettings("black_list", _, _))
        .WillOnce(testing::WithArgs<2>(
            testing::Invoke([](const DoneCallback& callback) {
              if (!callback.is_null())
                callback.Run(nullptr);
            })));
    manager_->Block({std::get<0>(GetParam()),
                     std::get<1>(GetParam()),
                     {},
                     base::Time::FromTimeT(1419990000)},
                    {});
  }
};

TEST_P(AccessRevocationManagerImplIsBlockedTest, IsBlocked) {
  EXPECT_TRUE(manager_->IsBlocked({7, 7, 7}, {8, 8, 8}, {}));
}

INSTANTIATE_TEST_CASE_P(
    Filters,
    AccessRevocationManagerImplIsBlockedTest,
    testing::Combine(testing::Values(std::vector<uint8_t>{},
                                     std::vector<uint8_t>{7, 7, 7}),
                     testing::Values(std::vector<uint8_t>{},
                                     std::vector<uint8_t>{8, 8, 8})));

}  // namespace weave