• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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 "UTTest_dm_softbus_cache.h"
17 
18 #include "accesstoken_kit.h"
19 #include "dm_constants.h"
20 #include "dm_device_info.h"
21 #include "dm_log.h"
22 #include "dm_softbus_cache.h"
23 #include "nativetoken_kit.h"
24 #include "token_setproc.h"
25 #include "softbus_common.h"
26 #include "softbus_error_code.h"
27 #include <utility>
28 
29 using namespace OHOS::Security::AccessToken;
30 using namespace testing;
31 using namespace testing::ext;
32 namespace OHOS {
33 namespace DistributedHardware {
SetUp()34 void DMSoftbusCacheTest::SetUp()
35 {
36     const int32_t permsNum = 4;
37     const int32_t indexZero = 0;
38     const int32_t indexOne = 1;
39     const int32_t indexTwo = 2;
40     const int32_t indexThree = 3;
41     uint64_t tokenId;
42     const char *perms[permsNum];
43     perms[indexZero] = "ohos.permission.DISTRIBUTED_SOFTBUS_CENTER";
44     perms[indexOne] = "ohos.permission.DISTRIBUTED_DATASYNC";
45     perms[indexTwo] = "ohos.permission.ACCESS_SERVICE_DM";
46     perms[indexThree] = "ohos.permission.MONITOR_DEVICE_NETWORK_STATE";
47     NativeTokenInfoParams infoInstance = {
48         .dcapsNum = 0,
49         .permsNum = permsNum,
50         .aclsNum = 0,
51         .dcaps = NULL,
52         .perms = perms,
53         .acls = NULL,
54         .processName = "dsoftbus_service",
55         .aplStr = "system_core",
56     };
57     tokenId = GetAccessTokenId(&infoInstance);
58     SetSelfTokenID(tokenId);
59     OHOS::Security::AccessToken::AccessTokenKit::ReloadNativeTokenInfo();
60 }
61 
TearDown()62 void DMSoftbusCacheTest::TearDown()
63 {
64 }
65 
SetUpTestCase()66 void DMSoftbusCacheTest::SetUpTestCase()
67 {
68     SoftbusCenterInterface::softbusCenterInterface_ = softbusCenterMock_;
69 }
70 
TearDownTestCase()71 void DMSoftbusCacheTest::TearDownTestCase()
72 {
73     SoftbusCenterInterface::softbusCenterInterface_ = nullptr;
74     softbusCenterMock_ = nullptr;
75 }
76 
77 namespace {
78 
79 HWTEST_F(DMSoftbusCacheTest, GetDeviceInfoFromCache_001, testing::ext::TestSize.Level1)
80 {
81     DmDeviceInfo deviceInfo = {
82         .deviceId = "deviceIdTest",
83         .deviceName = "deviceNameTest",
84         .deviceTypeId = 1
85     };
86     {
87         std::lock_guard<std::mutex> mutexLock(SoftbusCache::GetInstance().deviceInfosMutex_);
88         SoftbusCache::GetInstance().deviceInfo_.clear();
89         SoftbusCache::GetInstance().deviceInfo_["udid"] = std::pair<std::string, DmDeviceInfo>("uuid", deviceInfo);
90     }
91     DeviceManagerService::GetInstance().softbusListener_ = std::make_shared<SoftbusListener>();
92     DeviceManagerService::GetInstance().UninitSoftbusListener();
93     EXPECT_EQ(DeviceManagerService::GetInstance().softbusListener_, nullptr);
94     std::vector<DmDeviceInfo> deviceInfoList;
95     int32_t ret = SoftbusCache::GetInstance().GetDeviceInfoFromCache(deviceInfoList);
96     EXPECT_EQ(ret, DM_OK);
97     SoftbusCache::GetInstance().DeleteDeviceInfo();
98 }
99 
100 HWTEST_F(DMSoftbusCacheTest, CheckIsOnline_001, testing::ext::TestSize.Level1)
101 {
102     DmDeviceInfo deviceInfo = {
103         .deviceId = "deviceIdTest",
104         .deviceName = "deviceNameTest",
105         .deviceTypeId = 1,
106     };
107     {
108         std::lock_guard<std::mutex> mutexLock(SoftbusCache::GetInstance().deviceInfosMutex_);
109         SoftbusCache::GetInstance().deviceInfo_.clear();
110         SoftbusCache::GetInstance().deviceInfo_["udid"] = std::pair<std::string, DmDeviceInfo>("uuid", deviceInfo);
111     }
112     EXPECT_TRUE(SoftbusCache::GetInstance().CheckIsOnline("deviceIdTest"));
113     EXPECT_FALSE(SoftbusCache::GetInstance().CheckIsOnline("deviceIdTest1"));
114     SoftbusCache::GetInstance().DeleteDeviceInfo();
115 }
116 
117 HWTEST_F(DMSoftbusCacheTest, GetUuidByUdid_001, testing::ext::TestSize.Level1)
118 {
119     DmDeviceInfo deviceInfo = {
120         .deviceId = "deviceIdTest",
121         .deviceName = "deviceNameTest",
122         .deviceTypeId = 1,
123         .networkId = "networkid"
124     };
125     {
126         std::lock_guard<std::mutex> mutexLock(SoftbusCache::GetInstance().deviceInfosMutex_);
127         SoftbusCache::GetInstance().deviceInfo_.clear();
128         SoftbusCache::GetInstance().deviceInfo_["udid"] = std::pair<std::string, DmDeviceInfo>("uuid", deviceInfo);
129     }
130     std::string uuid = "";
131     EXPECT_EQ(SoftbusCache::GetInstance().GetUuidByUdid("udid", uuid), DM_OK);
132     EXPECT_EQ(uuid, "uuid");
133     uuid = "";
134     EXPECT_NE(SoftbusCache::GetInstance().GetUuidByUdid("test", uuid), DM_OK);
135     EXPECT_TRUE(uuid.empty());
136     SoftbusCache::GetInstance().DeleteDeviceInfo();
137 }
138 
139 HWTEST_F(DMSoftbusCacheTest, GetNetworkIdFromCache_001, testing::ext::TestSize.Level1)
140 {
141     DmDeviceInfo deviceInfo = {
142         .deviceId = "deviceIdTest",
143         .deviceName = "deviceNameTest",
144         .deviceTypeId = 1,
145         .networkId = "networkid"
146     };
147     {
148         std::lock_guard<std::mutex> mutexLock(SoftbusCache::GetInstance().deviceInfosMutex_);
149         SoftbusCache::GetInstance().deviceInfo_.clear();
150         SoftbusCache::GetInstance().deviceInfo_["udid"] = std::pair<std::string, DmDeviceInfo>("uuid", deviceInfo);
151     }
152     std::string networkId = "";
153     EXPECT_EQ(SoftbusCache::GetInstance().GetNetworkIdFromCache("udid", networkId), DM_OK);
154     EXPECT_EQ(networkId, deviceInfo.networkId);
155     networkId = "";
156     EXPECT_NE(SoftbusCache::GetInstance().GetNetworkIdFromCache("test", networkId), DM_OK);
157     EXPECT_TRUE(networkId.empty());
158     SoftbusCache::GetInstance().DeleteDeviceInfo();
159 }
160 
161 HWTEST_F(DMSoftbusCacheTest, GetUdidByUdidHash_001, testing::ext::TestSize.Level1)
162 {
163     DmDeviceInfo deviceInfo = {
164         .deviceId = "deviceIdTest",
165         .deviceName = "deviceNameTest",
166         .deviceTypeId = 1,
167         .networkId = "networkid"
168     };
169 
170     {
171         std::lock_guard<std::mutex> mutexLock(SoftbusCache::GetInstance().deviceInfosMutex_);
172         SoftbusCache::GetInstance().deviceInfo_.clear();
173         SoftbusCache::GetInstance().deviceInfo_["udid"] = std::pair<std::string, DmDeviceInfo>("uuid", deviceInfo);
174     }
175     std::string udid = "";
176     EXPECT_EQ(SoftbusCache::GetInstance().GetUdidByUdidHash("deviceIdTest", udid), DM_OK);
177     EXPECT_EQ(udid, "udid");
178     udid = "";
179     EXPECT_NE(SoftbusCache::GetInstance().GetUdidByUdidHash("test", udid), DM_OK);
180     EXPECT_TRUE(udid.empty());
181     SoftbusCache::GetInstance().DeleteDeviceInfo();
182 }
183 
184 HWTEST_F(DMSoftbusCacheTest, GetDevInfoByNetworkId_001, testing::ext::TestSize.Level1)
185 {
186     DmDeviceInfo deviceInfo = {
187         .deviceId = "deviceIdTest",
188         .deviceName = "deviceNameTest",
189         .deviceTypeId = 1,
190         .networkId = "networkid"
191     };
192     {
193         std::lock_guard<std::mutex> mutexLock(SoftbusCache::GetInstance().deviceInfosMutex_);
194         SoftbusCache::GetInstance().deviceInfo_.clear();
195         SoftbusCache::GetInstance().deviceInfo_["udid"] = std::pair<std::string, DmDeviceInfo>("uuid", deviceInfo);
196     }
197     DmDeviceInfo nodeInfo;
198     EXPECT_EQ(SoftbusCache::GetInstance().GetDevInfoByNetworkId("networkid", nodeInfo), DM_OK);
199     EXPECT_CALL(*softbusCenterMock_, GetAllNodeDeviceInfo(_, _, _)).Times(::testing::AtLeast(1))
200         .WillOnce(Return(ERR_DM_FAILED));
201     EXPECT_NE(SoftbusCache::GetInstance().GetDevInfoByNetworkId("test", nodeInfo), DM_OK);
202     SoftbusCache::GetInstance().DeleteDeviceInfo();
203 }
204 
205 HWTEST_F(DMSoftbusCacheTest, GetSecurityDeviceLevel_001, testing::ext::TestSize.Level1)
206 {
207     {
208         std::lock_guard<std::mutex> mutexLock(SoftbusCache::GetInstance().deviceSecurityLevelMutex_);
209         SoftbusCache::GetInstance().deviceSecurityLevel_.clear();
210         SoftbusCache::GetInstance().deviceSecurityLevel_["networkid"] = 1;
211     }
212     int32_t securityLevel = 0;
213     EXPECT_EQ(SoftbusCache::GetInstance().GetSecurityDeviceLevel("networkid", securityLevel), DM_OK);
214     EXPECT_EQ(securityLevel, 1);
215     EXPECT_CALL(*softbusCenterMock_, GetNodeKeyInfo(_, _, _, _, _)).Times(::testing::AtLeast(1))
216         .WillOnce(Return(ERR_DM_FAILED));
217     EXPECT_NE(SoftbusCache::GetInstance().GetSecurityDeviceLevel("test", securityLevel), DM_OK);
218     {
219         std::lock_guard<std::mutex> mutexLock(SoftbusCache::GetInstance().deviceSecurityLevelMutex_);
220         SoftbusCache::GetInstance().deviceSecurityLevel_.clear();
221     }
222 }
223 
224 HWTEST_F(DMSoftbusCacheTest, GetUuidFromCache_001, testing::ext::TestSize.Level1)
225 {
226     SoftbusCache::GetInstance().DeleteDeviceInfo();
227     DmDeviceInfo nodeInfo = {
228         .deviceId = "deviceIdTest",
229         .deviceName = "deviceNameTest",
230         .deviceTypeId = 1,
231         .networkId = ""
232     };
233     SoftbusCache::GetInstance().SaveDeviceInfo(nodeInfo);
234     SoftbusCache::GetInstance().UpdateDeviceInfoCache();
235     SoftbusCache::GetInstance().DeleteDeviceInfo();
236     DmDeviceInfo deviceInfo = {
237         .deviceId = "deviceIdTest",
238         .deviceName = "deviceNameTest",
239         .deviceTypeId = 1,
240         .networkId = "networkid"
241     };
242 
243     {
244         std::lock_guard<std::mutex> mutexLock(SoftbusCache::GetInstance().deviceInfosMutex_);
245         SoftbusCache::GetInstance().deviceInfo_["udid"] = std::pair<std::string, DmDeviceInfo>("uuid", deviceInfo);
246     }
247     std::string uuid = "";
248     EXPECT_EQ(SoftbusCache::GetInstance().GetUuidFromCache("networkid", uuid), DM_OK);
249     EXPECT_EQ(uuid, "uuid");
250     SoftbusCache::GetInstance().DeleteDeviceInfo();
251 }
252 
253 HWTEST_F(DMSoftbusCacheTest, GetUdidFromCache_001, testing::ext::TestSize.Level1)
254 {
255     DmDeviceInfo deviceInfo = {
256         .deviceId = "deviceIdTest",
257         .deviceName = "deviceNameTest",
258         .deviceTypeId = 1,
259         .networkId = "networkid"
260     };
261 
262     {
263         std::lock_guard<std::mutex> mutexLock(SoftbusCache::GetInstance().deviceInfosMutex_);
264         SoftbusCache::GetInstance().deviceInfo_.clear();
265         SoftbusCache::GetInstance().deviceInfo_["udid"] = std::pair<std::string, DmDeviceInfo>("uuid", deviceInfo);
266     }
267     std::string udid = "";
268     EXPECT_EQ(SoftbusCache::GetInstance().GetUdidFromCache("networkid", udid), DM_OK);
269     EXPECT_EQ(udid, "udid");
270     EXPECT_CALL(*softbusCenterMock_, GetNodeKeyInfo(_, _, _, _, _)).WillOnce(Return(ERR_DM_FAILED));
271     EXPECT_NE(SoftbusCache::GetInstance().GetUdidFromCache("test", udid), DM_OK);
272     SoftbusCache::GetInstance().DeleteDeviceInfo();
273 }
274 } // namespace
275 } // namespace DistributedHardware
276 } // namespace OHOS
277