1 /*
2  * Copyright (c) 2022 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 "common_utils_test.h"
17 
18 #include "string_ex.h"
19 
20 #include "accesstoken_kit.h"
21 #include "hilog/log.h"
22 #include "nativetoken_kit.h"
23 #include "ipc_skeleton.h"
24 #include "system_ability_definition.h"
25 #include "token_setproc.h"
26 
27 #include "common_utils.h"
28 #include "location_log.h"
29 #include "location_sa_load_manager.h"
30 #include "permission_manager.h"
31 
32 using namespace testing::ext;
33 namespace OHOS {
34 namespace Location {
35 const int32_t LOCATION_PERM_NUM = 5;
36 const int32_t APPOXI_LOCATION_PERM_NUM = 3;
37 const int32_t ACC_LOCATION_PERM_NUM = 3;
38 const int UNKNOWN_SA_ID = -1;
39 const uint32_t CAPABILITY = 0x102;
40 const double NUM_ACC_E6 = 1.000001;
41 const double NUM_ACC_E7 = 1.0000001;
SetUp()42 void CommonUtilsTest::SetUp()
43 {
44     LoadSystemAbility();
45     MockNativeAccurateLocation();
46 }
47 
TearDown()48 void CommonUtilsTest::TearDown()
49 {
50 }
51 
MockNativePermission()52 void CommonUtilsTest::MockNativePermission()
53 {
54     const char *perms[] = {
55         ACCESS_LOCATION.c_str(), ACCESS_APPROXIMATELY_LOCATION.c_str(),
56         ACCESS_BACKGROUND_LOCATION.c_str(), MANAGE_SECURE_SETTINGS.c_str(),
57         ACCESS_CONTROL_LOCATION_SWITCH.c_str(),
58     };
59     NativeTokenInfoParams infoInstance = {
60         .dcapsNum = 0,
61         .permsNum = LOCATION_PERM_NUM,
62         .aclsNum = 0,
63         .dcaps = nullptr,
64         .perms = perms,
65         .acls = nullptr,
66         .processName = "CommonTest1",
67         .aplStr = "system_basic",
68     };
69     tokenId_ = GetAccessTokenId(&infoInstance);
70     SetSelfTokenID(tokenId_);
71     Security::AccessToken::AccessTokenKit::ReloadNativeTokenInfo();
72 }
73 
MockNativeApproxiPermission()74 void CommonUtilsTest::MockNativeApproxiPermission()
75 {
76     const char *perms[] = {
77         ACCESS_APPROXIMATELY_LOCATION.c_str(), ACCESS_BACKGROUND_LOCATION.c_str(),
78         MANAGE_SECURE_SETTINGS.c_str(),
79     };
80     NativeTokenInfoParams infoInstance = {
81         .dcapsNum = 0,
82         .permsNum = APPOXI_LOCATION_PERM_NUM,
83         .aclsNum = 0,
84         .dcaps = nullptr,
85         .perms = perms,
86         .acls = nullptr,
87         .processName = "CommonTest2",
88         .aplStr = "system_basic",
89     };
90     tokenIdForApproxi_ = GetAccessTokenId(&infoInstance);
91     SetSelfTokenID(tokenIdForApproxi_);
92     Security::AccessToken::AccessTokenKit::ReloadNativeTokenInfo();
93 }
94 
MockNativeAccurateLocation()95 void CommonUtilsTest::MockNativeAccurateLocation()
96 {
97     const char *perms[] = {
98         ACCESS_LOCATION.c_str(), ACCESS_BACKGROUND_LOCATION.c_str(),
99         MANAGE_SECURE_SETTINGS.c_str(),
100     };
101     NativeTokenInfoParams infoInstance = {
102         .dcapsNum = 0,
103         .permsNum = ACC_LOCATION_PERM_NUM,
104         .aclsNum = 0,
105         .dcaps = nullptr,
106         .perms = perms,
107         .acls = nullptr,
108         .processName = "CommonTest3",
109         .aplStr = "system_basic",
110     };
111     tokenIdForAcc_ = GetAccessTokenId(&infoInstance);
112     SetSelfTokenID(tokenIdForAcc_);
113     Security::AccessToken::AccessTokenKit::ReloadNativeTokenInfo();
114 }
115 
LoadSystemAbility()116 void CommonUtilsTest::LoadSystemAbility()
117 {
118     auto locationSaLoadManager = LocationSaLoadManager::GetInstance();
119     if (locationSaLoadManager == nullptr) {
120         return;
121     }
122     locationSaLoadManager->LoadLocationSa(LOCATION_LOCATOR_SA_ID);
123 #ifdef FEATURE_GNSS_SUPPORT
124     locationSaLoadManager->LoadLocationSa(LOCATION_GNSS_SA_ID);
125 #endif
126 #ifdef FEATURE_PASSIVE_SUPPORT
127     locationSaLoadManager->LoadLocationSa(LOCATION_NOPOWER_LOCATING_SA_ID);
128 #endif
129 #ifdef FEATURE_NETWORK_SUPPORT
130     locationSaLoadManager->LoadLocationSa(LOCATION_NETWORK_LOCATING_SA_ID);
131 #endif
132 #ifdef FEATURE_GEOCODE_SUPPORT
133     locationSaLoadManager->LoadLocationSa(LOCATION_GEO_CONVERT_SA_ID);
134 #endif
135 }
136 
137 HWTEST_F(CommonUtilsTest, AbilityConvertToIdTest001, TestSize.Level1)
138 {
139     GTEST_LOG_(INFO)
140         << "CommonUtilsTest, AbilityConvertToIdTest001, TestSize.Level1";
141     LBSLOGI(COMMON_UTILS, "[CommonUtilsTest] AbilityConvertToIdTest001 begin");
142     EXPECT_EQ(LOCATION_GNSS_SA_ID, CommonUtils::AbilityConvertToId(GNSS_ABILITY));
143     EXPECT_EQ(LOCATION_NETWORK_LOCATING_SA_ID, CommonUtils::AbilityConvertToId(NETWORK_ABILITY));
144     EXPECT_EQ(LOCATION_NOPOWER_LOCATING_SA_ID, CommonUtils::AbilityConvertToId(PASSIVE_ABILITY));
145     EXPECT_EQ(LOCATION_GEO_CONVERT_SA_ID, CommonUtils::AbilityConvertToId(GEO_ABILITY));
146     EXPECT_EQ(UNKNOWN_SA_ID, CommonUtils::AbilityConvertToId("unknown_ability"));
147     LBSLOGI(COMMON_UTILS, "[CommonUtilsTest] AbilityConvertToIdTest001 end");
148 }
149 
150 HWTEST_F(CommonUtilsTest, GetCapabilityTest001, TestSize.Level1)
151 {
152     GTEST_LOG_(INFO)
153         << "CommonUtilsTest, GetCapabilityTest001, TestSize.Level1";
154     LBSLOGI(COMMON_UTILS, "[CommonUtilsTest] GetCapabilityTest001 begin");
155     EXPECT_NE(Str8ToStr16(""), CommonUtils::GetCapabilityToString("unknown_ability", CAPABILITY));
156     EXPECT_NE(Str8ToStr16(""), CommonUtils::GetCapabilityToString(GNSS_ABILITY, CAPABILITY));
157     EXPECT_NE(Str8ToStr16(""), CommonUtils::GetCapabilityToString(NETWORK_ABILITY, CAPABILITY));
158     EXPECT_NE(Str8ToStr16(""), CommonUtils::GetCapabilityToString(PASSIVE_ABILITY, CAPABILITY));
159     EXPECT_NE(Str8ToStr16(""), CommonUtils::GetCapabilityToString(GEO_ABILITY, CAPABILITY));
160 
161     EXPECT_NE(Str8ToStr16(""), CommonUtils::GetCapability("unknown_ability"));
162     EXPECT_NE(Str8ToStr16(""), CommonUtils::GetCapability(GNSS_ABILITY));
163     EXPECT_NE(Str8ToStr16(""), CommonUtils::GetCapability(NETWORK_ABILITY));
164     EXPECT_NE(Str8ToStr16(""), CommonUtils::GetCapability(PASSIVE_ABILITY));
165     EXPECT_NE(Str8ToStr16(""), CommonUtils::GetCapability(GEO_ABILITY));
166     LBSLOGI(COMMON_UTILS, "[CommonUtilsTest] GetCapabilityTest001 end");
167 }
168 
169 HWTEST_F(CommonUtilsTest, GetLabelTest001, TestSize.Level1)
170 {
171     GTEST_LOG_(INFO)
172         << "CommonUtilsTest, GetLabelTest001, TestSize.Level1";
173     LBSLOGI(COMMON_UTILS, "[CommonUtilsTest] GetLabelTest001 begin");
174     EXPECT_NE("", CommonUtils::GetLabel(GNSS_ABILITY).tag);
175     EXPECT_NE("", CommonUtils::GetLabel(NETWORK_ABILITY).tag);
176     EXPECT_NE("", CommonUtils::GetLabel(PASSIVE_ABILITY).tag);
177     EXPECT_NE("", CommonUtils::GetLabel(GEO_ABILITY).tag);
178     EXPECT_NE("", CommonUtils::GetLabel("unknown_ability").tag);
179     LBSLOGI(COMMON_UTILS, "[CommonUtilsTest] GetLabelTest001 end");
180 }
181 
182 HWTEST_F(CommonUtilsTest, GetRemoteObjectTest001, TestSize.Level1)
183 {
184     GTEST_LOG_(INFO)
185         << "CommonUtilsTest, GetRemoteObjectTest001, TestSize.Level1";
186     LBSLOGI(COMMON_UTILS, "[CommonUtilsTest] GetRemoteObjectTest001 begin");
187     // inert to map
188     EXPECT_NE(nullptr, CommonUtils::GetRemoteObject(LOCATION_GNSS_SA_ID));
189     EXPECT_NE(nullptr, CommonUtils::GetRemoteObject(LOCATION_NETWORK_LOCATING_SA_ID));
190     EXPECT_NE(nullptr, CommonUtils::GetRemoteObject(LOCATION_NOPOWER_LOCATING_SA_ID));
191     CommonUtils::GetRemoteObject(LOCATION_GEO_CONVERT_SA_ID);
192     EXPECT_EQ(nullptr, CommonUtils::GetRemoteObject(UNKNOWN_SA_ID));
193 
194     // read from map
195     EXPECT_NE(nullptr, CommonUtils::GetRemoteObject(LOCATION_GNSS_SA_ID));
196     EXPECT_NE(nullptr, CommonUtils::GetRemoteObject(LOCATION_NETWORK_LOCATING_SA_ID));
197     EXPECT_NE(nullptr, CommonUtils::GetRemoteObject(LOCATION_NOPOWER_LOCATING_SA_ID));
198     CommonUtils::GetRemoteObject(LOCATION_GEO_CONVERT_SA_ID);
199     EXPECT_EQ(nullptr, CommonUtils::GetRemoteObject(UNKNOWN_SA_ID));
200     LBSLOGI(COMMON_UTILS, "[CommonUtilsTest] GetRemoteObjectTest001 end");
201 }
202 
203 HWTEST_F(CommonUtilsTest, GetCurrentUserIdTest001, TestSize.Level1)
204 {
205     GTEST_LOG_(INFO)
206         << "CommonUtilsTest, GetCurrentUserIdTest001, TestSize.Level1";
207     LBSLOGI(COMMON_UTILS, "[CommonUtilsTest] GetCurrentUserIdTest001 begin");
208     int32_t userId = 0;
209     EXPECT_EQ(true, CommonUtils::GetCurrentUserId(userId));
210     LBSLOGI(COMMON_UTILS, "[CommonUtilsTest] GetCurrentUserIdTest001 end");
211 }
212 
213 HWTEST_F(CommonUtilsTest, Str16ToStr8Test001, TestSize.Level1)
214 {
215     GTEST_LOG_(INFO)
216         << "CommonUtilsTest, Str16ToStr8Test001, TestSize.Level1";
217     LBSLOGI(COMMON_UTILS, "[CommonUtilsTest] Str16ToStr8Test001 begin");
218     EXPECT_EQ(DEFAULT_STRING, CommonUtils::Str16ToStr8(DEFAULT_USTRING));
219     EXPECT_EQ("string16", CommonUtils::Str16ToStr8(u"string16"));
220     LBSLOGI(COMMON_UTILS, "[CommonUtilsTest] Str16ToStr8Test001 end");
221 }
222 
223 HWTEST_F(CommonUtilsTest, DoubleEqualTest001, TestSize.Level1)
224 {
225     GTEST_LOG_(INFO)
226         << "CommonUtilsTest, DoubleEqualTest001, TestSize.Level1";
227     LBSLOGI(COMMON_UTILS, "[CommonUtilsTest] DoubleEqualTest001 begin");
228     EXPECT_EQ(true, CommonUtils::DoubleEqual(1.0, 1.0));
229     EXPECT_EQ(true, CommonUtils::DoubleEqual(1.0, NUM_ACC_E7));
230     EXPECT_EQ(true, CommonUtils::DoubleEqual(1.0, NUM_ACC_E6));
231     EXPECT_EQ(false, CommonUtils::DoubleEqual(1.0, 2.0));
232     LBSLOGI(COMMON_UTILS, "[CommonUtilsTest] DoubleEqualTest001 end");
233 }
234 
235 HWTEST_F(CommonUtilsTest, CalculationTest001, TestSize.Level1)
236 {
237     GTEST_LOG_(INFO)
238         << "CommonUtilsTest, CalculationTest001, TestSize.Level1";
239     LBSLOGI(COMMON_UTILS, "[CommonUtilsTest] CalculationTest001 begin");
240     EXPECT_EQ(0, CommonUtils::CalDistance(1.0, 1.0, 1.0, 1.0));
241     EXPECT_NE(0, CommonUtils::CalDistance(1.0, 1.0, 1.0, NUM_ACC_E6));
242     EXPECT_NE(0, CommonUtils::CalDistance(1.0, 1.0, NUM_ACC_E6, 1.0));
243     EXPECT_NE(-1, CommonUtils::DoubleRandom(0.0, 1.0));
244     LBSLOGI(COMMON_UTILS, "[CommonUtilsTest] CalculationTest001 end");
245 }
246 
247 HWTEST_F(CommonUtilsTest, GetBundleNameByUidTest001, TestSize.Level1)
248 {
249     GTEST_LOG_(INFO)
250         << "CommonUtilsTest, GetBundleNameByUidTest001, TestSize.Level1";
251     LBSLOGI(COMMON_UTILS, "[CommonUtilsTest] GetBundleNameByUidTest001 begin");
252     std::string bundleName;
253     EXPECT_EQ(false, CommonUtils::GetBundleNameByUid(SYSTEM_UID, bundleName));
254     LBSLOGI(COMMON_UTILS, "[CommonUtilsTest] GetBundleNameByUidTest001 end");
255 }
256 
257 HWTEST_F(CommonUtilsTest, CountDownLatchWaitTest001, TestSize.Level1)
258 {
259     auto latch = std::make_shared<CountDownLatch>();
260     latch->SetCount(0);
261     latch->Wait(0); // count is 0
262     EXPECT_EQ(0, latch->GetCount());
263 }
264 
265 HWTEST_F(CommonUtilsTest, CountDownLatchWaitTest002, TestSize.Level1)
266 {
267     auto latch = std::make_shared<CountDownLatch>();
268     latch->SetCount(1);
269     latch->Wait(0); // wait 0ms
270     EXPECT_EQ(1, latch->GetCount());
271 }
272 
273 HWTEST_F(CommonUtilsTest, CountDownLatchCountDownTest001, TestSize.Level1)
274 {
275     auto latch = std::make_shared<CountDownLatch>();
276     latch->SetCount(1);
277     latch->CountDown();
278     EXPECT_EQ(0, latch->GetCount());
279 }
280 
281 HWTEST_F(CommonUtilsTest, CountDownLatchCountDownTest002, TestSize.Level1)
282 {
283     auto latch = std::make_shared<CountDownLatch>();
284     latch->CountDown();
285     EXPECT_EQ(0, latch->GetCount());
286 }
287 
288 HWTEST_F(CommonUtilsTest, CountDownLatchCountDownTest003, TestSize.Level1)
289 {
290     auto latch = std::make_shared<CountDownLatch>();
291     latch->SetCount(5);
292     latch->CountDown();
293     EXPECT_EQ(4, latch->GetCount());
294 }
295 
296 HWTEST_F(CommonUtilsTest, GetMacArrayTest001, TestSize.Level1)
297 {
298     LBSLOGI(COMMON_UTILS, "[CommonUtilsTest] GetMacArrayTest001 begin");
299     uint8_t macArray[MAC_LEN];
300     if (CommonUtils::GetMacArray("20:28:3e:74:34:70", macArray) != EOK) {
301         LBSLOGE(COMMON_UTILS, "GetMacArray failed.");
302     }
303     EXPECT_EQ(32, macArray[0]);
304     LBSLOGI(COMMON_UTILS, "[CommonUtilsTest] GetMacArrayTest001 end");
305 }
306 
307 HWTEST_F(CommonUtilsTest, SplitTest001, TestSize.Level1)
308 {
309     LBSLOGI(COMMON_UTILS, "[CommonUtilsTest] SplitTest001 begin");
310     std::vector<std::string> strVec = CommonUtils::Split("aa:bb:cc:dd:ee:ff", ":");
311     EXPECT_EQ(6, strVec.size());
312 }
313 
314 HWTEST_F(CommonUtilsTest, GetStringParameter001, TestSize.Level1)
315 {
316     std::string name = "";
317     LBSLOGI(COMMON_UTILS, "[CommonUtilsTest] GetStringParameter001 begin");
318     bool ret = CommonUtils::GetStringParameter("test", name);
319     EXPECT_EQ(false, ret);
320     LBSLOGI(COMMON_UTILS, "[CommonUtilsTest] GetStringParameter001 rnd");
321 }
322 
323 HWTEST_F(CommonUtilsTest, GetStringParameter002, TestSize.Level1)
324 {
325     std::string name = "";
326     LBSLOGI(COMMON_UTILS, "[CommonUtilsTest] GetStringParameter002 begin");
327     CommonUtils::GetStringParameter(SUPL_MODE_NAME, name);
328     LBSLOGI(COMMON_UTILS, "[CommonUtilsTest] GetStringParameter002 end");
329 }
330 
331 HWTEST_F(CommonUtilsTest, GetCurrentTime001, TestSize.Level1)
332 {
333     LBSLOGI(COMMON_UTILS, "[CommonUtilsTest] GetCurrentTime001 begin");
334     int64_t timeStamp = 0;
335     timeStamp = CommonUtils::GetCurrentTime();
336     EXPECT_NE(0, timeStamp);
337     LBSLOGI(COMMON_UTILS, "[CommonUtilsTest] GetCurrentTime001 end");
338 }
339 
340 HWTEST_F(CommonUtilsTest, GenerateUuid001, TestSize.Level1)
341 {
342     LBSLOGI(COMMON_UTILS, "[CommonUtilsTest] GenerateUuid001 begin");
343     std::string uuid = CommonUtils::GenerateUuid();
344     EXPECT_LT(0, uuid.size());
345     LBSLOGI(COMMON_UTILS, "[CommonUtilsTest] GenerateUuid001 end");
346 }
347 } // namespace Location
348 } // namespace OHOS
349