1 // Copyright 2016 The Weave Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "src/access_black_list_manager_impl.h"
6
7 #include <gmock/gmock.h>
8 #include <gtest/gtest.h>
9 #include <weave/provider/test/mock_config_store.h>
10 #include <weave/test/unittest_utils.h>
11
12 #include "src/test/mock_clock.h"
13 #include "src/bind_lambda.h"
14
15 using testing::_;
16 using testing::Return;
17 using testing::StrictMock;
18
19 namespace weave {
20
21 class AccessBlackListManagerImplTest : public testing::Test {
22 protected:
SetUp()23 void SetUp() {
24 std::string to_load = R"([{
25 "user": "BQID",
26 "app": "BwQF",
27 "expiration": 1410000000
28 }, {
29 "user": "AQID",
30 "app": "AwQF",
31 "expiration": 1419999999
32 }])";
33
34 EXPECT_CALL(config_store_, LoadSettings("black_list"))
35 .WillOnce(Return(to_load));
36
37 EXPECT_CALL(config_store_, SaveSettings("black_list", _, _))
38 .WillOnce(testing::WithArgs<1, 2>(testing::Invoke(
39 [](const std::string& json, const DoneCallback& callback) {
40 std::string to_save = R"([{
41 "user": "AQID",
42 "app": "AwQF",
43 "expiration": 1419999999
44 }])";
45 EXPECT_JSON_EQ(to_save, *test::CreateValue(json));
46 if (!callback.is_null())
47 callback.Run(nullptr);
48 })));
49
50 EXPECT_CALL(clock_, Now())
51 .WillRepeatedly(Return(base::Time::FromTimeT(1412121212)));
52 manager_.reset(new AccessBlackListManagerImpl{&config_store_, 10, &clock_});
53 }
54 StrictMock<test::MockClock> clock_;
55 StrictMock<provider::test::MockConfigStore> config_store_{false};
56 std::unique_ptr<AccessBlackListManagerImpl> manager_;
57 };
58
TEST_F(AccessBlackListManagerImplTest,Init)59 TEST_F(AccessBlackListManagerImplTest, Init) {
60 EXPECT_EQ(1u, manager_->GetSize());
61 EXPECT_EQ(10u, manager_->GetCapacity());
62 EXPECT_EQ((std::vector<AccessBlackListManagerImpl::Entry>{{
63 {1, 2, 3}, {3, 4, 5}, base::Time::FromTimeT(1419999999),
64 }}),
65 manager_->GetEntries());
66 }
67
TEST_F(AccessBlackListManagerImplTest,Block)68 TEST_F(AccessBlackListManagerImplTest, Block) {
69 EXPECT_CALL(config_store_, SaveSettings("black_list", _, _))
70 .WillOnce(testing::WithArgs<1, 2>(testing::Invoke(
71 [](const std::string& json, const DoneCallback& callback) {
72 std::string to_save = R"([{
73 "user": "AQID",
74 "app": "AwQF",
75 "expiration": 1419999999
76 }, {
77 "app": "CAgI",
78 "user": "BwcH",
79 "expiration": 1419990000
80 }])";
81 EXPECT_JSON_EQ(to_save, *test::CreateValue(json));
82 if (!callback.is_null())
83 callback.Run(nullptr);
84 })));
85 manager_->Block({7, 7, 7}, {8, 8, 8}, base::Time::FromTimeT(1419990000), {});
86 }
87
TEST_F(AccessBlackListManagerImplTest,BlockExpired)88 TEST_F(AccessBlackListManagerImplTest, BlockExpired) {
89 manager_->Block({}, {}, base::Time::FromTimeT(1400000000),
90 base::Bind([](ErrorPtr error) {
91 EXPECT_TRUE(error->HasError("aleady_expired"));
92 }));
93 }
94
TEST_F(AccessBlackListManagerImplTest,BlockListIsFull)95 TEST_F(AccessBlackListManagerImplTest, BlockListIsFull) {
96 EXPECT_CALL(config_store_, SaveSettings("black_list", _, _))
97 .WillRepeatedly(testing::WithArgs<1, 2>(testing::Invoke(
98 [](const std::string& json, const DoneCallback& callback) {
99 if (!callback.is_null())
100 callback.Run(nullptr);
101 })));
102 for (size_t i = manager_->GetSize(); i < manager_->GetCapacity(); ++i) {
103 manager_->Block(
104 {99, static_cast<uint8_t>(i / 256), static_cast<uint8_t>(i % 256)},
105 {8, 8, 8}, base::Time::FromTimeT(1419990000), {});
106 EXPECT_EQ(i + 1, manager_->GetSize());
107 }
108 manager_->Block({99}, {8, 8, 8}, base::Time::FromTimeT(1419990000),
109 base::Bind([](ErrorPtr error) {
110 EXPECT_TRUE(error->HasError("blacklist_is_full"));
111 }));
112 }
113
TEST_F(AccessBlackListManagerImplTest,Unblock)114 TEST_F(AccessBlackListManagerImplTest, Unblock) {
115 EXPECT_CALL(config_store_, SaveSettings("black_list", _, _))
116 .WillOnce(testing::WithArgs<1, 2>(testing::Invoke(
117 [](const std::string& json, const DoneCallback& callback) {
118 EXPECT_JSON_EQ("[]", *test::CreateValue(json));
119 if (!callback.is_null())
120 callback.Run(nullptr);
121 })));
122 manager_->Unblock({1, 2, 3}, {3, 4, 5}, {});
123 }
124
TEST_F(AccessBlackListManagerImplTest,UnblockNotFound)125 TEST_F(AccessBlackListManagerImplTest, UnblockNotFound) {
126 manager_->Unblock({5, 2, 3}, {5, 4, 5}, base::Bind([](ErrorPtr error) {
127 EXPECT_TRUE(error->HasError("entry_not_found"));
128 }));
129 }
130
TEST_F(AccessBlackListManagerImplTest,IsBlockedFalse)131 TEST_F(AccessBlackListManagerImplTest, IsBlockedFalse) {
132 EXPECT_FALSE(manager_->IsBlocked({7, 7, 7}, {8, 8, 8}));
133 }
134
135 class AccessBlackListManagerImplIsBlockedTest
136 : public AccessBlackListManagerImplTest,
137 public testing::WithParamInterface<
138 std::tuple<std::vector<uint8_t>, std::vector<uint8_t>>> {
139 public:
SetUp()140 void SetUp() override {
141 AccessBlackListManagerImplTest::SetUp();
142 EXPECT_CALL(config_store_, SaveSettings("black_list", _, _))
143 .WillOnce(testing::WithArgs<2>(
144 testing::Invoke([](const DoneCallback& callback) {
145 if (!callback.is_null())
146 callback.Run(nullptr);
147 })));
148 manager_->Block(std::get<0>(GetParam()), std::get<1>(GetParam()),
149 base::Time::FromTimeT(1419990000), {});
150 }
151 };
152
TEST_P(AccessBlackListManagerImplIsBlockedTest,IsBlocked)153 TEST_P(AccessBlackListManagerImplIsBlockedTest, IsBlocked) {
154 EXPECT_TRUE(manager_->IsBlocked({7, 7, 7}, {8, 8, 8}));
155 }
156
157 INSTANTIATE_TEST_CASE_P(
158 Filters,
159 AccessBlackListManagerImplIsBlockedTest,
160 testing::Combine(testing::Values(std::vector<uint8_t>{},
161 std::vector<uint8_t>{7, 7, 7}),
162 testing::Values(std::vector<uint8_t>{},
163 std::vector<uint8_t>{8, 8, 8})));
164
165 } // namespace weave
166