1 /*
2 * Copyright (c) 2024 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 <arpa/inet.h>
17 #include <gtest/gtest.h>
18 #include "gtest/hwext/gtest-tag.h"
19 #include "netmanager_ext_test_security.h"
20 #include "net_manager_constants.h"
21
22 #define private public
23 #define protected public
24
25 #include "netfirewall_rule_native_helper.h"
26
27 namespace OHOS {
28 namespace NetManagerStandard {
29 namespace {
30 using namespace testing::ext;
31 }
32 class NetFirewallRuleNativeHelperTest : public testing::Test {
33 public:
34 static void SetUpTestCase();
35 static void TearDownTestCase();
36
37 void SetUp();
38 void TearDown();
39 };
40
SetUpTestCase()41 void NetFirewallRuleNativeHelperTest::SetUpTestCase() {}
42
TearDownTestCase()43 void NetFirewallRuleNativeHelperTest::TearDownTestCase() {}
44
SetUp()45 void NetFirewallRuleNativeHelperTest::SetUp() {}
46
TearDown()47 void NetFirewallRuleNativeHelperTest::TearDown() {}
48
49 /**
50 * @tc.name: SetFirewallRulesInner001
51 * @tc.desc: Test NetFirewallRuleNativeHelperTest SetFirewallRulesInner.
52 * @tc.type: FUNC
53 */
54 HWTEST_F(NetFirewallRuleNativeHelperTest, SetFirewallRulesInner001, TestSize.Level1)
55 {
56 NetManagerExtAccessToken token;
57 std::vector<sptr<NetFirewallBaseRule>> rules;
58 const int32_t userId = 100;
59 const int32_t ruleNum = 301;
60 const int32_t maxIp = 256;
61 NetFirewallIpParam param;
62 param.family = FAMILY_IPV4;
63 param.type = SINGLE_IP;
64 const std::string tmp = "192.168.";
65 for (int32_t i = 0; i < ruleNum; i++) {
66 sptr<NetFirewallIpRule> rule = new (std::nothrow) NetFirewallIpRule();
67 ASSERT_NE(rule, nullptr);
68 rule->userId = userId;
69 rule->ruleDirection = NetFirewallRuleDirection::RULE_OUT;
70 rule->ruleAction = FirewallRuleAction::RULE_DENY;
71 rule->protocol = NetworkProtocol::ICMP;
72
73 inet_pton(AF_INET, (tmp + std::to_string(i / maxIp) + "." + std::to_string(i % maxIp)).c_str(),
74 ¶m.ipv4.startIp);
75 rule->remoteIps.emplace_back(param);
76 rules.emplace_back(rule);
77 }
78 int32_t ret =
79 NetFirewallRuleNativeHelper::GetInstance().SetFirewallRulesInner(NetFirewallRuleType::RULE_IP, rules, true);
80 EXPECT_EQ(ret, FIREWALL_SUCCESS);
81 }
82 } // namespace NetManagerStandard
83 } // namespace OHOS
84