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