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 "domain_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 DomainChainRuleTest : public testing::Test {};
34 
35 /**
36  * @tc.name: TestToFilterRule
37  * @tc.desc: Test ToFilterRule func.
38  * @tc.type: FUNC
39  */
40 HWTEST_F(DomainChainRuleTest, TestToFilterRule, TestSize.Level1)
41 {
42     DomainFilterRule domainFilterRule{Action::DENY, "9696", "www.example.com"};
43     DomainChainRule domainChainRule{domainFilterRule};
44 
45     EXPECT_EQ(domainChainRule.ToFilterRule(), domainFilterRule);
46 
47     std::string rule =
48         "1        0     0 DROP       udp  --  *      *       0.0.0.0/0            0.0.0.0/0            "
49         "udp dpt:53 owner UID match 9696 STRING match  \"|03777777076578616d706c6503636f6d|\" ALGO name bm TO 65535";
50     DomainChainRule domainChainRule1{rule};
51     EXPECT_EQ(domainChainRule1.ToFilterRule(), domainFilterRule);
52 }
53 
54 /**
55  * @tc.name: TestParameter
56  * @tc.desc: Test Parameter func.
57  * @tc.type: FUNC
58  */
59 HWTEST_F(DomainChainRuleTest, TestParameter, TestSize.Level1)
60 {
61     DomainFilterRule domainFilterRule{Action::DENY, "9696", "www.example.com"};
62     std::string parameter =
63         " -p udp --dport 53 -m owner --uid-owner 9696 -m string --hex-string |03|www|07|example|03|com| --algo bm";
64 
65     DomainChainRule domainChainRule{domainFilterRule};
66     EXPECT_EQ(domainChainRule.Parameter(), parameter);
67 
68     DomainFilterRule domainFilterRule1{Action::ALLOW, "", "www.example.com"};
69     std::string parameter1 = " -p udp --dport 53 -m string --hex-string |03|www|07|example|03|com| --algo bm";
70 
71     DomainChainRule domainChainRule1{domainFilterRule1};
72     EXPECT_EQ(domainChainRule1.Parameter(), parameter1);
73 }
74 
75 /**
76  * @tc.name: TestDomainToFormatData
77  * @tc.desc: Test DomainToFormatData func.
78  * @tc.type: FUNC
79  */
80 HWTEST_F(DomainChainRuleTest, TestDomainToFormatData, TestSize.Level1)
81 {
82     std::string domainName{"www.example1.com"};
83     std::string result = DomainChainRule::DomainToFormatData(domainName);
84     EXPECT_EQ(result, "|03|www|08|example1|03|com|");
85 }
86 
87 /**
88  * @tc.name: TestFormatDataToDomain
89  * @tc.desc: Test FormatDataToDomain func.
90  * @tc.type: FUNC
91  */
92 HWTEST_F(DomainChainRuleTest, TestFormatDataToDomain, TestSize.Level1)
93 {
94     std::string domainName{"\"|03777777076578616d706c6503636f6d|\""};
95     std::string result = DomainChainRule::FormatDataToDomain(domainName);
96     EXPECT_EQ(result, "www.example.com");
97 }
98 
99 /**
100  * @tc.name: TestCharToHex
101  * @tc.desc: Test CharToHex func.
102  * @tc.type: FUNC
103  */
104 HWTEST_F(DomainChainRuleTest, TestCharToHex, TestSize.Level1)
105 {
106     uint8_t result = DomainChainRule::CharToHex('1', '0');
107     EXPECT_TRUE(result == 0x10);
108 
109     result = DomainChainRule::CharToHex('2', '2');
110     EXPECT_TRUE(result == 0x22);
111 
112     result = DomainChainRule::CharToHex('3', '9');
113     EXPECT_TRUE(result == 0x39);
114 }
115 
116 /**
117  * @tc.name: TestCharToHex1
118  * @tc.desc: Test CharToHex func.
119  * @tc.type: FUNC
120  */
121 HWTEST_F(DomainChainRuleTest, TestCharToHex1, TestSize.Level1)
122 {
123     uint8_t expectChar = 1;
124     uint8_t result = DomainChainRule::CharToHex('1');
125     EXPECT_TRUE(result == expectChar);
126 
127     expectChar = 10;
128     result = DomainChainRule::CharToHex('a');
129     EXPECT_TRUE(result == expectChar);
130 
131     expectChar = 15;
132     result = DomainChainRule::CharToHex('F');
133     EXPECT_TRUE(result == expectChar);
134 }
135 } // namespace TEST
136 } // namespace IPTABLES
137 } // namespace EDM
138 } // namespace OHOS