• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024-2025 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 <gmock/gmock.h>
17 #include <gtest/gtest.h>
18 #include <securec.h>
19 
20 #include "lnn_network_manager.h"
21 #include "lnn_physical_subnet_manager.h"
22 
23 namespace OHOS {
24 using namespace testing::ext;
25 using namespace testing;
26 static VisitNextChoice visitCb = CHOICE_VISIT_NEXT;
LnnVisitPhysicalSubnetCallback(const LnnPhysicalSubnet * subnet,void * priv)27 static VisitNextChoice LnnVisitPhysicalSubnetCallback(const LnnPhysicalSubnet *subnet, void *priv)
28 {
29     return visitCb;
30 }
31 
32 class LNNPhysicalSubnetManagerTest : public testing::Test {
33 public:
34     void SetUp();
35     void TearDown();
36 };
37 
SetUp()38 void LNNPhysicalSubnetManagerTest::SetUp()
39 {
40     int32_t ret = LnnInitPhysicalSubnetManager();
41     EXPECT_TRUE(ret == SOFTBUS_OK);
42 }
43 
TearDown()44 void LNNPhysicalSubnetManagerTest::TearDown()
45 {
46     LnnDeinitPhysicalSubnetManager();
47 }
48 
49 /*
50  * @tc.name: LNN_REGIST_PHYSICAL_SUBNET_001
51  * @tc.desc: test subnet null or subnet->protocol null
52  * @tc.type: FUNC
53  * @tc.require:
54  */
55 HWTEST_F(LNNPhysicalSubnetManagerTest, LNN_REGIST_PHYSICAL_SUBNET_001, TestSize.Level1)
56 {
57     int32_t ret = SOFTBUS_OK;
58     ret = LnnRegistPhysicalSubnet(nullptr);
59     EXPECT_NE(ret, SOFTBUS_OK);
60 
61     LnnPhysicalSubnet subnet = {
62         .protocol = nullptr,
63         .status = LNN_SUBNET_RUNNING,
64     };
65     ret = LnnRegistPhysicalSubnet(&subnet);
66     EXPECT_NE(ret, SOFTBUS_OK);
67 }
68 
69 /*
70  * @tc.name: LNN_REGIST_PHYSICAL_SUBNET_002
71  * @tc.desc: test subnet is full
72  * @tc.type: FUNC
73  * @tc.require:
74  */
75 HWTEST_F(LNNPhysicalSubnetManagerTest, LNN_REGIST_PHYSICAL_SUBNET_002, TestSize.Level1)
76 {
77     int32_t ret = SOFTBUS_OK;
78     for (int32_t i = 0; i <= 6; i++) {
79         LnnProtocolManager lnnProtocolManager = {
80             .id = LNN_PROTOCOL_IP,
81         };
82         LnnPhysicalSubnet subnet = {
83             .protocol = &lnnProtocolManager,
84             .status = LNN_SUBNET_RUNNING,
85         };
86         ret += LnnRegistPhysicalSubnet(&subnet);
87     }
88     EXPECT_NE(ret, SOFTBUS_OK);
89 }
90 
91 /*
92  * @tc.name: LNN_UNREGIST_PHYSICAL_SUBNET_BY_TYPE
93  * @tc.desc: test LnnUnregistPhysicalSubnetByType
94  * @tc.type: FUNC
95  * @tc.require:
96  */
97 HWTEST_F(LNNPhysicalSubnetManagerTest, LNN_UNREGIST_PHYSICAL_SUBNET_BY_TYPE, TestSize.Level1)
98 {
99     int32_t ret = SOFTBUS_OK;
100     LnnProtocolManager lnnProtocolManager = {
101         .id = LNN_PROTOCOL_IP,
102     };
103     LnnPhysicalSubnet subnet = {
104         .protocol = &lnnProtocolManager,
105         .status = LNN_SUBNET_RUNNING,
106     };
107     ret = LnnRegistPhysicalSubnet(&subnet);
108     EXPECT_EQ(ret, SOFTBUS_OK);
109 
110     ret = LnnUnregistPhysicalSubnetByType(LNN_PROTOCOL_IP);
111     EXPECT_EQ(ret, SOFTBUS_OK);
112 }
113 
114 /*
115  * @tc.name: LNN_VISIT_PHYSICAL_SUBNET
116  * @tc.desc: test LnnVisitPhysicalSubnet
117  * @tc.type: FUNC
118  * @tc.require:
119  */
120 HWTEST_F(LNNPhysicalSubnetManagerTest, LNN_VISIT_PHYSICAL_SUBNET, TestSize.Level1)
121 {
122     int32_t ret = SOFTBUS_OK;
123 
124     LnnProtocolManager lnnProtocolManager = {
125         .id = LNN_PROTOCOL_IP,
126     };
127     LnnPhysicalSubnet subnet = {
128         .protocol = &lnnProtocolManager,
129         .status = LNN_SUBNET_RUNNING,
130     };
131     ret = LnnRegistPhysicalSubnet(&subnet);
132     EXPECT_EQ(ret, SOFTBUS_OK);
133 
134     visitCb = CHOICE_FINISH_VISITING;
135     bool visit = LnnVisitPhysicalSubnet(LnnVisitPhysicalSubnetCallback, nullptr);
136     EXPECT_FALSE(visit);
137 
138     visitCb = CHOICE_VISIT_NEXT;
139     visit = LnnVisitPhysicalSubnet(LnnVisitPhysicalSubnetCallback, nullptr);
140     EXPECT_TRUE(visit);
141 
142     ret = LnnUnregistPhysicalSubnetByType(LNN_PROTOCOL_IP);
143     EXPECT_EQ(ret, SOFTBUS_OK);
144 }
145 } // namespace OHOS