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 "chain_rule.h"
19 #undef protected
20 #undef private
21 
22 #include <gtest/gtest.h>
23 
24 using namespace testing::ext;
25 using namespace testing;
26 using namespace OHOS::EDM::IPTABLES;
27 
28 namespace OHOS {
29 namespace EDM {
30 namespace IPTABLES {
31 namespace TEST {
32 
33 class ChainRuleMock final : public ChainRule {
34 public:
ChainRuleMock(const std::string & rule)35     explicit ChainRuleMock(const std::string& rule) : ChainRule(rule) {}
36 
Parameter() const37     [[nodiscard]] std::string Parameter() const override
38     {
39         return "";
40     }
41 };
42 
43 class ChainRuleTest : public testing::Test { };
44 
45 /**
46  * @tc.name: TestChainRule
47  * @tc.desc: Test ChainRule func.
48  * @tc.type: FUNC
49  */
50 HWTEST_F(ChainRuleTest, TestChainRule, TestSize.Level1)
51 {
52     std::string rule =
53         "3        0     0 ACCEPT     udp  --  *      *       0.0.0.0/0            "
54         "0.0.0.0/0            source IP range 192.168.6.1-192.168.6.254 destination IP range "
55         "192.168.5.1-192.168.5.254 udp spts:55:66 dpts:55:77 ";
56     std::shared_ptr<ChainRule> chainRule = std::make_shared<ChainRuleMock>(rule);
57     EXPECT_EQ(chainRule->RuleNum(), 3);
58     EXPECT_EQ(chainRule->Target(), "ACCEPT");
59 
60     rule =
61         "1        0     0 ACCEPT     udp  --  *      *       192.168.1.1          192.168.2.2          "
62         "udp spt:9090 dpt:9091 owner UID match 1234567";
63     chainRule = std::make_shared<ChainRuleMock>(rule);
64     EXPECT_EQ(chainRule->srcAddr_, "192.168.1.1");
65     EXPECT_EQ(chainRule->destAddr_, "192.168.2.2");
66     EXPECT_EQ(chainRule->prot_, "udp");
67 }
68 
69 /**
70  * @tc.name: TestGetOption
71  * @tc.desc: Test GetOption func.
72  * @tc.type: FUNC
73  */
74 HWTEST_F(ChainRuleTest, TestGetOption, TestSize.Level1)
75 {
76     std::string rule;
77     std::shared_ptr<ChainRule> chainRule = std::make_shared<ChainRuleMock>(rule);
78     std::string options = "source IP range 192.168.6.1-192.168.6.254";
79     std::string key = "source IP range";
80     std::string expectValue = "192.168.6.1-192.168.6.254";
81     std::string value;
82     chainRule->GetOption(options, key, value);
83     EXPECT_EQ(expectValue, value);
84 
85     options = "destination IP range 192.168.5.1-192.168.5.254 udp spts:55:66 dpts:55:77";
86     key = "owner UID match";
87     expectValue = "";
88     value = {};
89     chainRule->GetOption(options, key, value);
90     EXPECT_EQ(expectValue, value);
91 
92     options = "spt:55 dpt:55 owner UID match 6667";
93     key = "owner UID match";
94     uint32_t expectIntValue = 6667;
95     uint32_t intValue = 0;
96     chainRule->GetOption(options, key, intValue);
97     EXPECT_EQ(expectIntValue, intValue);
98 }
99 
100 /**
101  * @tc.name: TestTruncateOption
102  * @tc.desc: Test TruncateOption func.
103  * @tc.type: FUNC
104  */
105 HWTEST_F(ChainRuleTest, TestTruncateOption, TestSize.Level1)
106 {
107     std::string rule;
108     std::shared_ptr<ChainRule> chainRule = std::make_shared<ChainRuleMock>(rule);
109     std::string options = "source IP range 192.168.6.1-192.168.6.254";
110     std::string key = "source IP range";
111     std::string expectValue = " 192.168.6.1-192.168.6.254";
112     std::string value;
113     chainRule->TruncateOption(options, key, value);
114     EXPECT_EQ(expectValue, value);
115 
116     options = "destination IP range 192.168.5.1-192.168.5.254 udp spts:55:66 dpts:55:77";
117     key = "owner UID match";
118     expectValue = "";
119     value = {};
120     chainRule->TruncateOption(options, key, value);
121     EXPECT_EQ(expectValue, value);
122 }
123 } // namespace TEST
124 } // namespace IPTABLES
125 } // namespace EDM
126 } // namespace OHOS