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 "vnic_manager.h"
26
27 namespace OHOS {
28 namespace NetManagerStandard {
29 namespace {
30 using namespace testing::ext;
31 } // namespace
32
33 class VnicManagerTest : public testing::Test {
34 public:
35 static void SetUpTestCase();
36 static void TearDownTestCase();
37 void SetUp();
38 void TearDown();
39 };
40
SetUpTestCase()41 void VnicManagerTest::SetUpTestCase() {}
42
TearDownTestCase()43 void VnicManagerTest::TearDownTestCase() {}
44
SetUp()45 void VnicManagerTest::SetUp() {}
46
TearDown()47 void VnicManagerTest::TearDown() {}
48
49 HWTEST_F(VnicManagerTest, CreateVnicInterface001, TestSize.Level1)
50 {
51 auto result = VnicManager::GetInstance().CreateVnicInterface();
52 EXPECT_EQ(result, NETMANAGER_SUCCESS);
53
54 VnicManager::GetInstance().DestroyVnicInterface();
55 }
56
57 HWTEST_F(VnicManagerTest, SetVnicMtu001, TestSize.Level1)
58 {
59 std::string ifName = "";
60 int32_t testNumber = 0;
61 auto result = VnicManager::GetInstance().SetVnicMtu(ifName, testNumber);
62 EXPECT_EQ(result, NETMANAGER_ERROR);
63
64 testNumber = 1;
65 result = VnicManager::GetInstance().SetVnicMtu(ifName, testNumber);
66 EXPECT_EQ(result, NETMANAGER_SUCCESS);
67 }
68
69 HWTEST_F(VnicManagerTest, SetVnicAddress001, TestSize.Level1)
70 {
71 std::string ifName = "";
72 std::string tunAddr = "";
73 int32_t testNumber = 0;
74 auto result = VnicManager::GetInstance().SetVnicAddress(ifName, tunAddr, testNumber);
75 EXPECT_EQ(result, NETMANAGER_ERROR);
76 }
77
78 HWTEST_F(VnicManagerTest, SetVnicUp001, TestSize.Level1)
79 {
80 auto result = VnicManager::GetInstance().SetVnicUp();
81 EXPECT_EQ(result, NETMANAGER_SUCCESS);
82 }
83
84 HWTEST_F(VnicManagerTest, SetVnicDown001, TestSize.Level1)
85 {
86 auto result = VnicManager::GetInstance().SetVnicDown();
87 EXPECT_EQ(result, NETMANAGER_SUCCESS);
88 }
89
90 HWTEST_F(VnicManagerTest, InitIfreq001, TestSize.Level1)
91 {
92 ifreq ifr;
93 std::string cardName = "";
94 auto result = VnicManager::GetInstance().InitIfreq(ifr, cardName);
95 EXPECT_EQ(result, NETMANAGER_SUCCESS);
96 }
97
98 HWTEST_F(VnicManagerTest, CreateWithDestroyVnic001, TestSize.Level1)
99 {
100 uint16_t mtu = 1500;
101 const std::string tunAddr = "192.168.1.100";
102 int32_t prefix = 24;
103 std::set<int32_t> uids = {5527};
104 auto result = VnicManager::GetInstance().CreateVnic(mtu, tunAddr, prefix, uids);
105 EXPECT_EQ(result, NETMANAGER_SUCCESS);
106
107 result = VnicManager::GetInstance().DelDefaultRoute();
108 EXPECT_NE(result, NETMANAGER_SUCCESS);
109
110 result = VnicManager::GetInstance().AddDefaultRoute();
111 EXPECT_NE(result, NETMANAGER_SUCCESS);
112
113 result = VnicManager::GetInstance().DestroyVnic();
114 EXPECT_EQ(result, NETMANAGER_SUCCESS);
115 }
116
117 HWTEST_F(VnicManagerTest, CreateWithDestroyVnic002, TestSize.Level1)
118 {
119 uint16_t mtu = 1500;
120 const std::string tunAddr = "192.168.1.100";
121 int32_t prefix = 24;
122 std::set<int32_t> uids;
123 for (int32_t i = 1; i <= 30; ++i) {
124 uids.insert(i);
125 }
126
127 auto result = VnicManager::GetInstance().CreateVnic(mtu, tunAddr, prefix, uids);
128 EXPECT_EQ(result, NETMANAGER_ERROR);
129 }
130
131 HWTEST_F(VnicManagerTest, CreateWithDestroyVnic003, TestSize.Level1)
132 {
133 uint16_t mtu = 0;
134 const std::string tunAddr = "192.168.1.100";
135 int32_t prefix = 24;
136 const std::set<int32_t> uids = {5527};
137 auto result = VnicManager::GetInstance().CreateVnic(mtu, tunAddr, prefix, uids);
138 EXPECT_EQ(result, NETMANAGER_ERROR);
139 }
140
141 } // namespace NetManagerStandard
142 } // namespace OHOS
143