1 /*
2 * Copyright (c) 2023 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #define private public
17 #define protected public
18 #include "executer_utils.h"
19 #include "iexecuter.h"
20 #undef protected
21 #undef private
22
23 #include <gmock/gmock.h>
24 #include <gtest/gtest.h>
25
26 #include "domain_chain_rule.h"
27 #include "executer_utils_mock.h"
28
29 using namespace testing::ext;
30 using namespace testing;
31 using namespace OHOS::EDM::IPTABLES;
32
33 using ::testing::Return;
34 using ::testing::DoAll;
35 using ::testing::SetArgPointee;
36 using ::testing::SetArgReferee;
37 using ::testing::SaveArg;
38 using ::testing::Invoke;
39
40 namespace OHOS {
41 namespace EDM {
42 namespace IPTABLES {
43 namespace TEST {
44
45 class IExecuterMock final: public IExecuter {
46 public:
Init()47 ErrCode Init() override
48 {
49 return ERR_OK;
50 }
51 };
52
53 class IExecuterTest : public testing::Test {
54 public:
55 std::shared_ptr<ExecuterUtilsMock> executerUtilsMock;
56
57 protected:
58 void SetUp() override;
59 };
60
SetUp()61 void IExecuterTest::SetUp()
62 {
63 executerUtilsMock = std::make_shared<ExecuterUtilsMock>();
64 ExecuterUtils::instance_ = executerUtilsMock;
65 }
66
67 /**
68 * @tc.name: TestInit
69 * @tc.desc: Test Init func.
70 * @tc.type: FUNC
71 */
72 HWTEST_F(IExecuterTest, TestInit, TestSize.Level1)
73 {
74 std::shared_ptr<IExecuter> executer = std::make_shared<IExecuterMock>();
75 EXPECT_TRUE(executer->Init() == ERR_OK);
76 }
77
78 /**
79 * @tc.name: TestInit
80 * @tc.desc: Test CreateChain func.
81 * @tc.type: FUNC
82 */
83 HWTEST_F(IExecuterTest, TestCreateChain, TestSize.Level1)
84 {
85 std::shared_ptr<IExecuter> executer = std::make_shared<IExecuterMock>();
86
87 EXPECT_CALL(*executerUtilsMock, Execute).WillRepeatedly(DoAll(Invoke(PrintExecRule), Return(ERR_OK)));
88 EXPECT_TRUE(executer->CreateChain() == ERR_OK);
89
90 EXPECT_CALL(*executerUtilsMock, Execute).WillRepeatedly(DoAll(Invoke(PrintExecRule), Return(-1)));
91 EXPECT_FALSE(executer->CreateChain() == ERR_OK);
92 }
93
94 /**
95 * @tc.name: TestAdd
96 * @tc.desc: Test Add func.
97 * @tc.type: FUNC
98 */
99 HWTEST_F(IExecuterTest, TestAdd, TestSize.Level1)
100 {
101 std::shared_ptr<IExecuter> executer = std::make_shared<IExecuterMock>();
102
103 EXPECT_CALL(*executerUtilsMock, Execute).WillRepeatedly(DoAll(Invoke(PrintExecRule), Return(ERR_OK)));
104 EXPECT_TRUE(executer->Add(nullptr) != ERR_OK);
105
106 EXPECT_CALL(*executerUtilsMock, Execute).WillRepeatedly(DoAll(Invoke(PrintExecRule), Return(-1)));
107 EXPECT_TRUE(executer->Add(nullptr) != ERR_OK);
108
109 DomainFilterRule domainFilterRule{Action::ALLOW, "9999", "www.example.com"};
110 std::shared_ptr<ChainRule> rule = std::make_shared<DomainChainRule>(domainFilterRule);
111 EXPECT_CALL(*executerUtilsMock, Execute).WillRepeatedly(DoAll(Invoke(PrintExecRule), Return(ERR_OK)));
112 EXPECT_TRUE(executer->Add(rule) == ERR_OK);
113
114 EXPECT_CALL(*executerUtilsMock, Execute).WillRepeatedly(DoAll(Invoke(PrintExecRule), Return(-1)));
115 EXPECT_FALSE(executer->Add(rule) == ERR_OK);
116
117 domainFilterRule = {Action::DENY, "9999", "www.example.com"};
118 rule = std::make_shared<DomainChainRule>(domainFilterRule);
119 EXPECT_CALL(*executerUtilsMock, Execute)
120 .Times(2)
121 .WillOnce(DoAll(Invoke(PrintExecRule), Return(ERR_OK)))
122 .WillOnce(DoAll(Invoke(PrintExecRule), Return(ERR_OK)));
123 EXPECT_TRUE(executer->Add(rule) == ERR_OK);
124 }
125
126 /**
127 * @tc.name: TestRemove
128 * @tc.desc: Test Remove func.
129 * @tc.type: FUNC
130 */
131 HWTEST_F(IExecuterTest, TestRemove, TestSize.Level1)
132 {
133 std::shared_ptr<IExecuter> executer = std::make_shared<IExecuterMock>();
134
135 EXPECT_CALL(*executerUtilsMock, Execute).WillRepeatedly(DoAll(Invoke(PrintExecRule), Return(ERR_OK)));
136 EXPECT_TRUE(executer->Remove(nullptr) == ERR_OK);
137
138 EXPECT_CALL(*executerUtilsMock, Execute).WillRepeatedly(DoAll(Invoke(PrintExecRule), Return(-1)));
139 EXPECT_TRUE(executer->Remove(nullptr) != ERR_OK);
140
141 DomainFilterRule domainFilterRule{Action::ALLOW, "9999", "www.example.com"};
142 std::shared_ptr<ChainRule> rule = std::make_shared<DomainChainRule>(domainFilterRule);
143 EXPECT_CALL(*executerUtilsMock, Execute).WillRepeatedly(DoAll(Invoke(PrintExecRule), Return(ERR_OK)));
144 EXPECT_TRUE(executer->Remove(rule) == ERR_OK);
145
146 EXPECT_CALL(*executerUtilsMock, Execute).WillRepeatedly(DoAll(Invoke(PrintExecRule), Return(-1)));
147 EXPECT_FALSE(executer->Remove(rule) == ERR_OK);
148
149 domainFilterRule = {Action::DENY, "9999", "www.example.com"};
150 rule = std::make_shared<DomainChainRule>(domainFilterRule);
151 EXPECT_CALL(*executerUtilsMock, Execute)
152 .Times(2)
153 .WillOnce(DoAll(Invoke(PrintExecRule), Return(ERR_OK)))
154 .WillOnce(DoAll(Invoke(PrintExecRule), Return(ERR_OK)));
155 EXPECT_TRUE(executer->Remove(rule) == ERR_OK);
156 }
157
158 /**
159 * @tc.name: TestGetAll
160 * @tc.desc: Test GetAll func.
161 * @tc.type: FUNC
162 */
163 HWTEST_F(IExecuterTest, TestGetAll, TestSize.Level1)
164 {
165 std::shared_ptr<IExecuter> executer = std::make_shared<IExecuterMock>();
166 std::vector<std::string> list;
167
168 EXPECT_CALL(*executerUtilsMock, Execute).WillRepeatedly(DoAll(Invoke(PrintExecRule), Return(ERR_OK)));
169 EXPECT_TRUE(executer->GetAll(list) == ERR_OK);
170
171 EXPECT_CALL(*executerUtilsMock, Execute).WillRepeatedly(DoAll(Invoke(PrintExecRule), Return(-1)));
172 EXPECT_TRUE(executer->GetAll(list) != ERR_OK);
173
174 EXPECT_CALL(*executerUtilsMock, Execute).WillRepeatedly(DoAll(Invoke(PrintExecRule), Return(ERR_OK)));
175 std::string result =
176 "Chain edm_deny_output (1 references)\n"
177 "num pkts bytes target prot opt in out source destination\n"
178 "1 0 0 DROP udp -- * * 0.0.0.0/0 10.1.1.1 "
179 "source IP range 192.168.1.1-192.188.22.66 udp spt:8080 dpt:8080";
180 EXPECT_CALL(*executerUtilsMock, Execute)
181 .WillRepeatedly(DoAll(Invoke(PrintExecRule), SetArgReferee<1>(result), Return(ERR_OK)));
182 EXPECT_TRUE(executer->GetAll(list) == ERR_OK);
183 EXPECT_TRUE(list.size() == 1);
184 }
185
186 /**
187 * @tc.name: TestExecWithOption
188 * @tc.desc: Test ExecWithOption func.
189 * @tc.type: FUNC
190 */
191 HWTEST_F(IExecuterTest, TestExecWithOption, TestSize.Level1)
192 {
193 std::shared_ptr<IExecuter> executer = std::make_shared<IExecuterMock>();
194 std::ostringstream oss{};
195 DomainFilterRule domainFilterRule{Action::ALLOW, "9999", "www.example.com"};
196 std::shared_ptr<ChainRule> rule = std::make_shared<DomainChainRule>(domainFilterRule);
197
198 EXPECT_CALL(*executerUtilsMock, Execute).WillRepeatedly(DoAll(Invoke(PrintExecRule), Return(ERR_OK)));
199 EXPECT_TRUE(executer->ExecWithOption(oss, rule) == ERR_OK);
200
201 EXPECT_CALL(*executerUtilsMock, Execute).WillRepeatedly(DoAll(Invoke(PrintExecRule), Return(-1)));
202 EXPECT_TRUE(executer->ExecWithOption(oss, rule) != ERR_OK);
203
204 domainFilterRule = {Action::DENY, "9999", "www.example.com"};
205 rule = std::make_shared<DomainChainRule>(domainFilterRule);
206 EXPECT_CALL(*executerUtilsMock, Execute)
207 .Times(2)
208 .WillOnce(DoAll(Invoke(PrintExecRule), Return(ERR_OK)))
209 .WillOnce(DoAll(Invoke(PrintExecRule), Return(ERR_OK)));
210 EXPECT_TRUE(executer->ExecWithOption(oss, rule) == ERR_OK);
211 }
212 } // namespace TEST
213 } // namespace IPTABLES
214 } // namespace EDM
215 } // namespace OHOS