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 #include <securec.h>
18
19 #include "lnn_event_monitor_impl.h"
20 #include "lnn_netmanager_monitor.h"
21 #include "lnn_netlink_monitor.c"
22 #include "net_conn_client.h"
23 #include "network_mock.h"
24 #include "refbase.h"
25 #include "softbus_adapter_errcode.h"
26 #include "softbus_error_code.h"
27
28 #define NCM_LINK_NAME "ncm0"
29 #define LOCAL_IP_LINK "192.168.66.1"
30 #define DEFAULT_GATEWAY_POSTFIX "99"
31
32 namespace OHOS {
33
34 using namespace testing::ext;
35 using namespace testing;
36 class AdapterNetManagerMonitorTest : public testing::Test {
37 public:
38 static void SetUpTestCase();
39 static void TearDownTestCase();
40 void SetUp();
41 void TearDown();
42 };
SetUpTestCase()43 void AdapterNetManagerMonitorTest::SetUpTestCase() { }
TearDownTestCase()44 void AdapterNetManagerMonitorTest::TearDownTestCase() { }
SetUp()45 void AdapterNetManagerMonitorTest::SetUp() { }
TearDown()46 void AdapterNetManagerMonitorTest::TearDown() { }
47
48 /*
49 * @tc.name: ConfigNetLinkUp
50 * @tc.desc: config net link up test
51 * @tc.type: FUNC
52 * @tc.require:
53 */
54 HWTEST_F(AdapterNetManagerMonitorTest, ConfigNetLinkUpTest001, TestSize.Level1)
55 {
56 int32_t ret = ConfigNetLinkUp(nullptr);
57 EXPECT_TRUE(ret == SOFTBUS_INVALID_PARAM);
58 char ifName[] = NCM_LINK_NAME;
59 ret = ConfigNetLinkUp(ifName);
60 EXPECT_TRUE(ret == SOFTBUS_NETWORK_CONFIG_NETLINK_UP_FAIL);
61 }
62
63 /*
64 * @tc.name: ConfigLocalIp
65 * @tc.desc: config net link local ip test
66 * @tc.type: FUNC
67 * @tc.require:
68 */
69 HWTEST_F(AdapterNetManagerMonitorTest, ConfigLocalIpTest001, TestSize.Level1)
70 {
71 char ifName[] = NCM_LINK_NAME;
72 int32_t ret = ConfigLocalIp(ifName, nullptr);
73 EXPECT_TRUE(ret == SOFTBUS_INVALID_PARAM);
74 char ip[] = LOCAL_IP_LINK;
75 ret = ConfigLocalIp(ifName, ip);
76 EXPECT_TRUE(ret == SOFTBUS_NETWORK_CONFIG_NETLINK_IP_FAIL);
77 }
78
79 /*
80 * @tc.name: ConfigRoute
81 * @tc.desc: config net link route test
82 * @tc.type: FUNC
83 * @tc.require:
84 */
85 HWTEST_F(AdapterNetManagerMonitorTest, ConfigRouteTest001, TestSize.Level1)
86 {
87 int32_t id = 0;
88 char ifName[] = NCM_LINK_NAME;
89 char destination[] = DEFAULT_GATEWAY_POSTFIX;
90 char gateway[] = "";
91 int32_t ret = ConfigRoute(id, ifName, destination, nullptr);
92 EXPECT_TRUE(ret == SOFTBUS_INVALID_PARAM);
93 ret = ConfigRoute(id, ifName, destination, gateway);
94 EXPECT_TRUE(ret == SOFTBUS_NETWORK_CONFIG_NETLINK_ROUTE_FAIL);
95 }
96
97 /**
98 * @tc.name:LnnNetmanagerMonitorTest_001
99 * @tc.desc: Verify the SetSoftBusWifiConnState function return value equal SOFTBUS_WIFI_UNKNOWN.
100 * @tc.type: FUNC
101 * @tc.require: 1
102 */
103 HWTEST_F(AdapterNetManagerMonitorTest, LnnInitNetManagerMonitorImpl_001, TestSize.Level1)
104 {
105 NiceMock<NetworkInterfaceMock> NetworkInterfaceMock;
106 EXPECT_CALL(NetworkInterfaceMock, LnnAsyncCallbackDelayHelper).WillRepeatedly(Return(SOFTBUS_INVALID_PARAM));
107 int32_t ret = LnnInitNetManagerMonitorImpl();
108 EXPECT_EQ(ret, SOFTBUS_NETWORK_MONITOR_INIT_FAIL);
109 }
110
111 /**
112 * @tc.name:LnnNetmanagerMonitorTest_001
113 * @tc.desc: Verify the LnnInitNetlinkMonitorImpl function return value equal SOFTBUS_LOCK_ERR.
114 * @tc.type: FUNC
115 * @tc.require: 1
116 */
117 HWTEST_F(AdapterNetManagerMonitorTest, LnnInitNetlinkMonitorImpl_001, TestSize.Level1)
118 {
119 NiceMock<NetworkInterfaceMock> NetworkInterfaceMock;
120 EXPECT_CALL(NetworkInterfaceMock, StartBaseClient).WillRepeatedly(Return(SOFTBUS_LOCK_ERR));
121 int32_t ret = LnnInitNetlinkMonitorImpl();
122 EXPECT_EQ(ret, SOFTBUS_LOCK_ERR);
123 }
124
125 /**
126 * @tc.name:LnnInitNetlinkMonitorImpl_002
127 * @tc.desc: Verify the LnnInitNetlinkMonitorImpl function return value SOFTBUS_NETWORK_CREATE_SOCKET_FAILED.
128 * @tc.type: FUNC
129 * @tc.require: 1
130 */
131 HWTEST_F(AdapterNetManagerMonitorTest, LnnInitNetlinkMonitorImpl_002, TestSize.Level1)
132 {
133 NiceMock<NetworkInterfaceMock> NetworkInterfaceMock;
134 EXPECT_CALL(NetworkInterfaceMock, StartBaseClient).WillRepeatedly(Return(SOFTBUS_OK));
135 EXPECT_CALL(NetworkInterfaceMock, SoftBusSocketCreate)
136 .WillRepeatedly(Return(SOFTBUS_NETWORK_CREATE_SOCKET_FAILED));
137 int32_t ret = LnnInitNetlinkMonitorImpl();
138 EXPECT_EQ(ret, SOFTBUS_NETWORK_CREATE_SOCKET_FAILED);
139 }
140
141 /**
142 * @tc.name:LnnInitNetlinkMonitorImpl_003
143 * @tc.desc: Verify the LnnInitNetlinkMonitorImpl function return value equal SOFTBUS_LOCK_ERR.
144 * @tc.type: FUNC
145 * @tc.require: 1
146 */
147 HWTEST_F(AdapterNetManagerMonitorTest, LnnInitNetlinkMonitorImpl_003, TestSize.Level1)
148 {
149 NiceMock<NetworkInterfaceMock> NetworkInterfaceMock;
150 EXPECT_CALL(NetworkInterfaceMock, StartBaseClient).WillRepeatedly(Return(SOFTBUS_OK));
151 ON_CALL(NetworkInterfaceMock, SoftBusSocketCreate).WillByDefault(Return(SOFTBUS_OK));
152 ON_CALL(NetworkInterfaceMock, SoftBusSocketSetOpt).WillByDefault(Return(SOFTBUS_OK));
153 ON_CALL(NetworkInterfaceMock, SoftBusSocketClose).WillByDefault(Return(SOFTBUS_OK));
154 ON_CALL(NetworkInterfaceMock, SoftBusSocketBind).WillByDefault(Return(SOFTBUS_OK));
155 EXPECT_CALL(NetworkInterfaceMock, AddTrigger).WillRepeatedly(Return(SOFTBUS_LOCK_ERR));
156 int32_t ret = LnnInitNetlinkMonitorImpl();
157 EXPECT_EQ(ret, SOFTBUS_LOCK_ERR);
158 }
159 } // namespace OHOS
160