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
109 result = VnicManager::GetInstance().AddDefaultRoute();
110
111 result = VnicManager::GetInstance().DestroyVnic();
112 EXPECT_EQ(result, NETMANAGER_SUCCESS);
113 }
114
115 HWTEST_F(VnicManagerTest, CreateWithDestroyVnic002, TestSize.Level1)
116 {
117 uint16_t mtu = 1500;
118 const std::string tunAddr = "192.168.1.100";
119 int32_t prefix = 24;
120 std::set<int32_t> uids;
121 for (int32_t i = 1; i <= 30; ++i) {
122 uids.insert(i);
123 }
124
125 auto result = VnicManager::GetInstance().CreateVnic(mtu, tunAddr, prefix, uids);
126 EXPECT_EQ(result, NETMANAGER_ERROR);
127 }
128
129 HWTEST_F(VnicManagerTest, CreateWithDestroyVnic003, TestSize.Level1)
130 {
131 uint16_t mtu = 0;
132 const std::string tunAddr = "192.168.1.100";
133 int32_t prefix = 24;
134 const std::set<int32_t> uids = {5527};
135 auto result = VnicManager::GetInstance().CreateVnic(mtu, tunAddr, prefix, uids);
136 EXPECT_EQ(result, NETMANAGER_ERROR);
137 }
138
139 HWTEST_F(VnicManagerTest, DestroyVnicInterface001, TestSize.Level1)
140 {
141 VnicManager vnicmanager;
142 vnicmanager.net4Sock_ = 0;
143 vnicmanager.net6Sock_ = 0;
144 vnicmanager.tunFd_ = 0;
145 vnicmanager.DestroyVnicInterface();
146 EXPECT_EQ(vnicmanager.net4Sock_, 0);
147 EXPECT_EQ(vnicmanager.net6Sock_, 0);
148 EXPECT_EQ(vnicmanager.tunFd_, 0);
149 }
150
151 HWTEST_F(VnicManagerTest, SetVnicMtu002, TestSize.Level1)
152 {
153 std::string ifName = "12345678901234567890";
154 int32_t mtu = 1;
155 auto result = VnicManager::GetInstance().SetVnicMtu(ifName, mtu);
156 EXPECT_EQ(result, NETMANAGER_ERROR);
157 }
158
159 HWTEST_F(VnicManagerTest, SetVnicAddress002, TestSize.Level1)
160 {
161 std::string ifName = "12345678901234567890";
162 std::string tunAddr = "2001:db8:85a3::8a2e:370:7334";
163 int32_t testNumber = 0;
164 auto result = VnicManager::GetInstance().SetVnicAddress(ifName, tunAddr, testNumber);
165 EXPECT_EQ(result, NETMANAGER_ERROR);
166 ifName = "";
167 result = VnicManager::GetInstance().SetVnicAddress(ifName, tunAddr, testNumber);
168 EXPECT_EQ(result, NETMANAGER_ERROR);
169 tunAddr = "192.168.1.1";
170 result = VnicManager::GetInstance().SetVnicAddress(ifName, tunAddr, testNumber);
171 EXPECT_EQ(result, NETMANAGER_ERROR);
172 testNumber = 32;
173 result = VnicManager::GetInstance().SetVnicAddress(ifName, tunAddr, testNumber);
174 EXPECT_EQ(result, NETMANAGER_ERROR);
175 }
176
177 HWTEST_F(VnicManagerTest, InitIfreq002, TestSize.Level1)
178 {
179 ifreq ifr;
180 std::string cardName = "12345678901234567890";
181 auto result = VnicManager::GetInstance().InitIfreq(ifr, cardName);
182 EXPECT_EQ(result, NETMANAGER_ERROR);
183 }
184 } // namespace NetManagerStandard
185 } // namespace OHOS
186