1 /*
2 * Copyright (c) 2022-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 <gtest/gtest.h>
17 #include <thread>
18
19 #ifdef GTEST_API_
20 #define private public
21 #define protected public
22 #endif
23
24 #include "iptables_wrapper.h"
25 #include "net_manager_constants.h"
26 #include "netnative_log_wrapper.h"
27
28 namespace OHOS {
29 namespace NetsysNative {
30 using namespace testing::ext;
31 using namespace OHOS::nmd;
32 class IptablesWrapperTest : public testing::Test {
33 public:
34 static void SetUpTestCase();
35 static void TearDownTestCase();
36 void SetUp();
37 void TearDown();
38 };
39
SetUpTestCase()40 void IptablesWrapperTest::SetUpTestCase() {}
41
TearDownTestCase()42 void IptablesWrapperTest::TearDownTestCase() {}
43
SetUp()44 void IptablesWrapperTest::SetUp() {}
45
TearDown()46 void IptablesWrapperTest::TearDown() {}
47
48 HWTEST_F(IptablesWrapperTest, RunCommandTest001, TestSize.Level1)
49 {
50 NETNATIVE_LOGI("RunCommandTest001 enter");
51 std::shared_ptr<IptablesWrapper> wrapper = DelayedSingleton<IptablesWrapper>::GetInstance();
52 std::string comdLine = "-L -n";
53 std::string str = wrapper->RunCommandForRes(IpType::IPTYPE_IPV4, comdLine);
54 const uint32_t waiteMS1 = 500;
55 std::this_thread::sleep_for(std::chrono::milliseconds(waiteMS1));
56 int32_t ret = wrapper->RunCommand(IpType::IPTYPE_IPV4, comdLine);
57 EXPECT_EQ(ret, NetManagerStandard::NETMANAGER_SUCCESS);
58 const uint32_t waiteMS2 = 100;
59 std::this_thread::sleep_for(std::chrono::milliseconds(waiteMS2));
60 }
61
62 HWTEST_F(IptablesWrapperTest, RunCommandTest002, TestSize.Level1)
63 {
64 std::shared_ptr<IptablesWrapper> wrapper = DelayedSingleton<IptablesWrapper>::GetInstance();
65 IpType ipType = IpType::IPTYPE_IPV4;
66 std::string comdLine = "-A INPUT -j LOG";
67 auto ret = wrapper->RunCommand(ipType, comdLine);
68 EXPECT_EQ(ret, NetManagerStandard::NETMANAGER_SUCCESS);
69 }
70
71 HWTEST_F(IptablesWrapperTest, RunCommandForResTest001, TestSize.Level1)
72 {
73 std::shared_ptr<IptablesWrapper> wrapper = DelayedSingleton<IptablesWrapper>::GetInstance();
74 IpType ipType = IpType::IPTYPE_IPV4;
75 std::string comdLine = "-A INPUT -j LOG";
76 auto ret = wrapper->RunCommandForRes(ipType, comdLine);
77 EXPECT_EQ(ret, wrapper->result_);
78 }
79 } // namespace NetsysNative
80 } // namespace OHOS
81