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 <hdf_log.h>
18 #include "../../../chip/hdi_service/iface_tool.h"
19 #include "../../../chip/hdi_service/wifi_chip.h"
20 #include "wifi_hal_fn.h"
21 
22 using namespace testing::ext;
23 using namespace OHOS::HDI::Wlan::Chip::V1_0;
24 
25 namespace WifiChipTest {
26 const std::string TEST_AP_IFNAME = "wlan1";
27 const std::string TEST_STA_IFNAME = "wlan0";
28 const std::string TEST_P2P_IFNAME = "p2p0";
29 
30 class WifiChipTest : public testing::Test {
31 public:
SetUpTestCase()32     static void SetUpTestCase() {}
TearDownTestCase()33     static void TearDownTestCase() {}
SetUp()34     void SetUp()
35     {
36         int32_t chipId = 0;
37         bool isPrimary = true;
38         ifaceTool = std::make_shared<IfaceTool>();
39         WifiHalFn fn;
40         InitWifiHalFuncTable(&fn);
41         wifiVendorHalTest = std::make_shared<WifiVendorHal>(ifaceTool, fn, true);
42         wifiChip = new WifiChip(chipId, isPrimary, wifiVendorHalTest,
43             std::make_shared<IfaceUtil>(ifaceTool), HandlerMock);
44     }
TearDown()45     void TearDown() {}
46 
HandlerMock(const std::string & ifName)47     static void HandlerMock(const std::string& ifName)
48     {
49         HDF_LOGI("HandlerMock enter");
50     }
51 
52 public:
53     std::shared_ptr<WifiVendorHal> wifiVendorHalTest;
54     std::shared_ptr<IfaceTool> ifaceTool;
55     sptr<WifiChip> wifiChip;
56 };
57 
58 /**
59  * @tc.name: GetCurrentModeTest
60  * @tc.desc: GetCurrentMode
61  * @tc.type: FUNC
62  * @tc.require:
63  */
64 HWTEST_F(WifiChipTest, GetCurrentModeTest, TestSize.Level1)
65 {
66     HDF_LOGI("GetCurrentModeTest started");
67     uint32_t modeId = -1;
68     if (wifiChip == nullptr) {
69         HDF_LOGE("wifiChip is null");
70         return;
71     }
72     EXPECT_TRUE(wifiChip->GetCurrentMode(modeId) == HDF_ERR_INVALID_PARAM);
73     modeId = 0;
74     wifiChip->GetCurrentMode(modeId);
75     wifiChip->RegisterChipEventCallback(nullptr);
76 }
77 
78 /**
79  * @tc.name: SetChipModeTest
80  * @tc.desc: SetChipMode
81  * @tc.type: FUNC
82  * @tc.require:
83  */
84 HWTEST_F(WifiChipTest, SetChipModeTest, TestSize.Level1)
85 {
86     HDF_LOGI("SetChipModeTest started");
87     uint32_t modeId = UINT32_MAX;
88     if (wifiChip == nullptr) {
89         HDF_LOGE("wifiChip is null");
90         return;
91     }
92     EXPECT_TRUE(wifiChip->SetChipMode(modeId) == HDF_FAILURE);
93 }
94 
95 /**
96  * @tc.name: CreateApServiceTest
97  * @tc.desc: CreateApService
98  * @tc.type: FUNC
99  * @tc.require:
100  */
101 HWTEST_F(WifiChipTest, CreateApServiceTest, TestSize.Level1)
102 {
103     HDF_LOGI("CreateApServiceTest started");
104     if (wifiChip == nullptr) {
105         HDF_LOGE("wifiChip is null");
106         return;
107     }
108     std::vector<std::string> instances;
109     std::weak_ptr<IfaceTool> ifaceTool = std::make_shared<IfaceTool>();
110     WifiHalFn fn;
111     std::weak_ptr<WifiVendorHal> vendorHal = std::make_shared<WifiVendorHal>(ifaceTool, fn, true);
112     sptr<IChipIface> apIface = new (std::nothrow) WifiApIface(TEST_AP_IFNAME, instances, vendorHal,
113         std::make_shared<IfaceUtil>(ifaceTool));
114     wifiChip->CreateApService(apIface);
115     std::vector<std::string> ifnames;
116     wifiChip->GetApServiceIfNames(ifnames);
117     std::string ifname1;
118     wifiChip->GetApService(ifname1, apIface);
119     wifiChip->RemoveApService(ifname1);
120     EXPECT_TRUE(wifiChip->GetApServiceIfNames(ifnames) == HDF_FAILURE);
121     EXPECT_TRUE(wifiChip->GetApService(ifname1, apIface) == HDF_FAILURE);
122 }
123 
124 /**
125  * @tc.name: CreateP2pIfaceTest
126  * @tc.desc: CreateP2pIface
127  * @tc.type: FUNC
128  * @tc.require:
129  */
130 HWTEST_F(WifiChipTest, CreateP2pIfaceTest, TestSize.Level1)
131 {
132     HDF_LOGI("CreateP2pIfaceTest started");
133     if (wifiChip == nullptr) {
134         HDF_LOGE("wifiChip is null");
135         return;
136     }
137     std::weak_ptr<IfaceTool> ifaceTool = std::make_shared<IfaceTool>();
138     WifiHalFn fn;
139     std::weak_ptr<WifiVendorHal> vendorHal = std::make_shared<WifiVendorHal>(ifaceTool, fn, true);
140     sptr<IChipIface> p2pIface = new (std::nothrow) WifiP2pIface(TEST_P2P_IFNAME, vendorHal,
141         std::make_shared<IfaceUtil>(ifaceTool));
142     wifiChip->CreateP2pService(p2pIface);
143     std::vector<std::string> ifnames;
144     wifiChip->GetP2pServiceIfNames(ifnames);
145     std::string ifname1;
146     wifiChip->GetP2pService(ifname1, p2pIface);
147     wifiChip->RemoveP2pService(ifname1);
148     EXPECT_TRUE(wifiChip->GetP2pServiceIfNames(ifnames) == HDF_FAILURE);
149     EXPECT_TRUE(wifiChip->GetP2pService(ifname1, p2pIface) == HDF_FAILURE);
150 }
151 
152 /**
153  * @tc.name: CreateStaIfaceTest
154  * @tc.desc: CreateStaIface
155  * @tc.type: FUNC
156  * @tc.require:
157  */
158 HWTEST_F(WifiChipTest, CreateStaIfaceTest, TestSize.Level1)
159 {
160     HDF_LOGI("CreateStaIfaceTest started");
161     if (wifiChip == nullptr) {
162         HDF_LOGE("wifiChip is null");
163         return;
164     }
165     std::weak_ptr<IfaceTool> ifaceTool = std::make_shared<IfaceTool>();
166     WifiHalFn fn;
167     std::weak_ptr<WifiVendorHal> vendorHal = std::make_shared<WifiVendorHal>(ifaceTool, fn, true);
168     sptr<IChipIface> staIface = new (std::nothrow) WifiStaIface(TEST_STA_IFNAME, vendorHal,
169         std::make_shared<IfaceUtil>(ifaceTool));
170     wifiChip->CreateStaService(staIface);
171 
172     std::vector<std::string> ifnames;
173     wifiChip->GetStaServiceIfNames(ifnames);
174     std::string ifname1;
175     wifiChip->GetStaService(ifname1, staIface);
176     wifiChip->RemoveStaService(ifname1);
177     EXPECT_TRUE(wifiChip->GetStaServiceIfNames(ifnames) == HDF_FAILURE);
178     EXPECT_TRUE(wifiChip->GetStaService(ifname1, staIface) == HDF_FAILURE);
179 }
180 }
181 
182