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 <gtest/gtest.h>
17
18 #ifdef GTEST_API_
19 #define private public
20 #define protected public
21 #endif
22
23 #include "net_manager_constants.h"
24 #include "netnative_log_wrapper.h"
25 #include "distributed_manager.h"
26
27 namespace OHOS {
28 namespace NetManagerStandard {
29 namespace {
30 using namespace testing::ext;
31 constexpr const char *DISTRIBUTED_TUN_CARD_NAME = "virnic";
32 } // namespace
33
34 class DistributedManagerTest : public testing::Test {
35 public:
36 static void SetUpTestCase();
37 static void TearDownTestCase();
38 void SetUp();
39 void TearDown();
40 };
41
SetUpTestCase()42 void DistributedManagerTest::SetUpTestCase() {}
43
TearDownTestCase()44 void DistributedManagerTest::TearDownTestCase() {}
45
SetUp()46 void DistributedManagerTest::SetUp() {}
47
TearDown()48 void DistributedManagerTest::TearDown() {}
49
50 HWTEST_F(DistributedManagerTest, CreateDistributedInterface001, TestSize.Level1)
51 {
52 auto result = DistributedManager::GetInstance().CreateDistributedInterface(DISTRIBUTED_TUN_CARD_NAME);
53 EXPECT_EQ(result, NETMANAGER_SUCCESS);
54
55 DistributedManager::GetInstance().DestroyDistributedNic(DISTRIBUTED_TUN_CARD_NAME);
56 }
57
58 HWTEST_F(DistributedManagerTest, SetDistributedNicMtu001, TestSize.Level1)
59 {
60 std::string ifName = "";
61 int32_t testNumber = 0;
62 auto result = DistributedManager::GetInstance().SetDistributedNicMtu(ifName, testNumber);
63 EXPECT_EQ(result, NETMANAGER_ERROR);
64
65 ifName = DISTRIBUTED_TUN_CARD_NAME;
66 testNumber = 1;
67 result = DistributedManager::GetInstance().SetDistributedNicMtu(ifName, testNumber);
68 EXPECT_EQ(result, NETMANAGER_ERROR);
69 }
70
71 HWTEST_F(DistributedManagerTest, SetDistributedNicMtu002, TestSize.Level1)
72 {
73 DistributedManager::GetInstance().CreateDistributedInterface(DISTRIBUTED_TUN_CARD_NAME);
74 std::string ifName = DISTRIBUTED_TUN_CARD_NAME;
75 int32_t testNumber = 1; // func ioctl will be failed, if mtu is too small
76 auto result = DistributedManager::GetInstance().SetDistributedNicMtu(ifName, testNumber);
77 EXPECT_EQ(result, NETMANAGER_ERROR);
78
79 ifName = DISTRIBUTED_TUN_CARD_NAME;
80 testNumber = 1400;
81 result = DistributedManager::GetInstance().SetDistributedNicMtu(ifName, testNumber);
82 DistributedManager::GetInstance().DestroyDistributedNic(DISTRIBUTED_TUN_CARD_NAME);
83 EXPECT_EQ(result, NETMANAGER_SUCCESS);
84 }
85
86 HWTEST_F(DistributedManagerTest, SetDistributedNicAddress001, TestSize.Level1)
87 {
88 std::string ifName = "";
89 std::string tunAddr = "";
90 auto result = DistributedManager::GetInstance().SetDistributedNicAddress(ifName, tunAddr);
91 EXPECT_EQ(result, NETMANAGER_ERROR);
92
93 tunAddr = "1.23.45.6";
94 DistributedManager::GetInstance().CreateDistributedInterface(DISTRIBUTED_TUN_CARD_NAME);
95 result = DistributedManager::GetInstance().SetDistributedNicAddress(DISTRIBUTED_TUN_CARD_NAME, tunAddr);
96 DistributedManager::GetInstance().DestroyDistributedNic(DISTRIBUTED_TUN_CARD_NAME);
97 EXPECT_EQ(result, NETMANAGER_SUCCESS);
98 }
99
100 HWTEST_F(DistributedManagerTest, SetDistributedNicUp001, TestSize.Level1)
101 {
102 auto result = DistributedManager::GetInstance().SetDistributedNicUp(DISTRIBUTED_TUN_CARD_NAME);
103 EXPECT_EQ(result, NETMANAGER_SUCCESS);
104
105 result = DistributedManager::GetInstance().SetDistributedNicDown(DISTRIBUTED_TUN_CARD_NAME);
106 EXPECT_EQ(result, NETMANAGER_SUCCESS);
107 }
108
109 HWTEST_F(DistributedManagerTest, InitIfreq001, TestSize.Level1)
110 {
111 ifreq ifr;
112 std::string cardName = "";
113 auto result = DistributedManager::GetInstance().InitIfreq(ifr, cardName);
114 EXPECT_EQ(result, NETMANAGER_SUCCESS);
115 }
116
117 } // namespace NetManagerStandard
118 } // namespace OHOS
119