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 #include "executer_utils.h"
17
18 #include <gtest/gtest.h>
19 #include <memory>
20
21 #include "utils.h"
22
23 using namespace testing::ext;
24 using namespace testing;
25 using namespace OHOS::EDM::IPTABLES;
26 using namespace OHOS::EDM::TEST;
27
28 namespace OHOS {
29 namespace EDM {
30 namespace IPTABLES {
31 namespace TEST {
32
33 class ExecuterUtilsTest : public testing::Test {
34 protected:
35 static void SetUpTestSuite(void);
36
37 static void TearDownTestSuite(void);
38 };
39
SetUpTestSuite(void)40 void ExecuterUtilsTest::SetUpTestSuite(void)
41 {
42 Utils::SetEdmInitialEnv();
43 }
44
TearDownTestSuite(void)45 void ExecuterUtilsTest::TearDownTestSuite(void)
46 {
47 Utils::ResetTokenTypeAndUid();
48 ASSERT_TRUE(Utils::IsOriginalUTEnv());
49 }
50
51 /**
52 * @tc.name: TestExecute
53 * @tc.desc: Test Execute func.
54 * @tc.type: FUNC
55 */
56 HWTEST_F(ExecuterUtilsTest, TestExecute, TestSize.Level1)
57 {
58 std::shared_ptr<ExecuterUtils> executerUtils = std::make_shared<ExecuterUtils>();
59
60 std::string rule = "-t filter -N edm_unit_test_output";
61 std::string result;
62 ErrCode ret = executerUtils->Execute(rule, result);
63 ASSERT_TRUE(ret == ERR_OK);
64
65 rule = "-t filter -I OUTPUT -j edm_unit_test_output";
66 ret = executerUtils->Execute(rule, result);
67 ASSERT_TRUE(ret == ERR_OK);
68
69 rule = "-t filter -F edm_unit_test_output";
70 ret = executerUtils->Execute(rule, result);
71 ASSERT_TRUE(ret == ERR_OK);
72
73 rule = "-t filter -n -v -L edm_unit_test_output --line-number";
74 ret = executerUtils->Execute(rule, result);
75 ASSERT_TRUE(ret == ERR_OK);
76
77 std::istringstream iss(result);
78 std::vector<std::string> ruleLines;
79 std::string line;
80 while (getline(iss, line)) {
81 ruleLines.emplace_back(line);
82 }
83 ASSERT_TRUE(ruleLines.size() == 2);
84
85 rule = "-t filter -D OUTPUT -j edm_unit_test_output";
86 ret = executerUtils->Execute(rule, result);
87 ASSERT_TRUE(ret == ERR_OK);
88
89 rule = "-t filter -X edm_unit_test_output";
90 ret = executerUtils->Execute(rule, result);
91 ASSERT_TRUE(ret == ERR_OK);
92
93 rule = "-t filter -n -v -L edm_unit_test_output --line-number";
94 ret = executerUtils->Execute(rule, result);
95 ASSERT_TRUE(result.empty());
96 }
97 } // namespace TEST
98 } // namespace IPTABLES
99 } // namespace EDM
100 } // namespace OHOS