1 /*
2 * Copyright (c) 2023 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 "locator_agent_test.h"
17 #include "accesstoken_kit.h"
18 #include "nativetoken_kit.h"
19 #include "token_setproc.h"
20 #include "locator.h"
21 #include "permission_manager.h"
22
23 using namespace testing::ext;
24 namespace OHOS {
25 namespace Location {
26 const int32_t LOCATION_PERM_NUM = 4;
SetUp()27 void LocatorAgentManagerTest::SetUp()
28 {
29 MockNativePermission();
30 }
31
TearDown()32 void LocatorAgentManagerTest::TearDown()
33 {
34 }
35
MockNativePermission()36 void LocatorAgentManagerTest::MockNativePermission()
37 {
38 const char *perms[] = {
39 ACCESS_LOCATION.c_str(), ACCESS_APPROXIMATELY_LOCATION.c_str(),
40 ACCESS_BACKGROUND_LOCATION.c_str(), MANAGE_SECURE_SETTINGS.c_str(),
41 };
42 NativeTokenInfoParams infoInstance = {
43 .dcapsNum = 0,
44 .permsNum = LOCATION_PERM_NUM,
45 .aclsNum = 0,
46 .dcaps = nullptr,
47 .perms = perms,
48 .acls = nullptr,
49 .processName = "LocatorAgentManagerTest",
50 .aplStr = "system_basic",
51 };
52 tokenId_ = GetAccessTokenId(&infoInstance);
53 SetSelfTokenID(tokenId_);
54 Security::AccessToken::AccessTokenKit::ReloadNativeTokenInfo();
55 }
56
57 HWTEST_F(LocatorAgentManagerTest, StartGnssLocatingTest001, TestSize.Level1)
58 {
59 GTEST_LOG_(INFO)
60 << "LocatorAgentManagerTest, StartGnssLocatingTest001, TestSize.Level1";
61 LBSLOGI(LOCATOR_STANDARD, "[LocatorAgentManagerTest] StartGnssLocatingTest001 begin");
62 auto locatorAgentManager = std::make_shared<LocatorAgentManager>();
63 static OHOS::Location::LocationCallbackIfaces locationCallback;
64 locatorAgentManager->StartGnssLocating(locationCallback);
65 LBSLOGI(LOCATOR_STANDARD, "[LocatorAgentManagerTest] StartGnssLocatingTest001 end");
66 }
67
68 HWTEST_F(LocatorAgentManagerTest, StopGnssLocatingTest001, TestSize.Level1)
69 {
70 GTEST_LOG_(INFO)
71 << "LocatorAgentManagerTest, StopGnssLocatingTest001, TestSize.Level1";
72 LBSLOGI(LOCATOR_STANDARD, "[LocatorAgentManagerTest] StopGnssLocatingTest001 begin");
73 auto locatorAgentManager = std::make_shared<LocatorAgentManager>();
74 locatorAgentManager->StopGnssLocating();
75 LBSLOGI(LOCATOR_STANDARD, "[LocatorAgentManagerTest] StopGnssLocatingTest001 end");
76 }
77
78 HWTEST_F(LocatorAgentManagerTest, RegisterGnssStatusCallbackTest001, TestSize.Level1)
79 {
80 GTEST_LOG_(INFO)
81 << "LocatorAgentManagerTest, RegisterGnssStatusCallbackTest001, TestSize.Level1";
82 LBSLOGI(LOCATOR_STANDARD, "[LocatorAgentManagerTest] RegisterGnssStatusCallbackTest001 begin");
83 auto locatorAgentManager = std::make_shared<LocatorAgentManager>();
84 static OHOS::Location::SvStatusCallbackIfaces svCallback;
85 locatorAgentManager->RegisterGnssStatusCallback(svCallback);
86 LBSLOGI(LOCATOR_STANDARD, "[LocatorAgentManagerTest] RegisterGnssStatusCallbackTest001 end");
87 }
88
89 HWTEST_F(LocatorAgentManagerTest, UnregisterGnssStatusCallbackTest001, TestSize.Level1)
90 {
91 GTEST_LOG_(INFO)
92 << "LocatorAgentManagerTest, UnregisterGnssStatusCallbackTest001, TestSize.Level1";
93 LBSLOGI(LOCATOR_STANDARD, "[LocatorAgentManagerTest] UnregisterGnssStatusCallbackTest001 begin");
94 auto locatorAgentManager = std::make_shared<LocatorAgentManager>();
95 locatorAgentManager->UnregisterGnssStatusCallback();
96 LBSLOGI(LOCATOR_STANDARD, "[LocatorAgentManagerTest] UnregisterGnssStatusCallbackTest001 end");
97 }
98
99 HWTEST_F(LocatorAgentManagerTest, RegisterNmeaMessageCallbackTest001, TestSize.Level1)
100 {
101 GTEST_LOG_(INFO)
102 << "LocatorAgentTest, RegisterNmeaMessageCallbackTest001, TestSize.Level1";
103 LBSLOGI(LOCATOR_STANDARD, "[LocatorAgentManagerTest] RegisterNmeaMessageCallbackTest001 begin");
104 auto locatorAgentManager = std::make_shared<LocatorAgentManager>();
105 static OHOS::Location::GnssNmeaCallbackIfaces nmeaCallback;
106 locatorAgentManager->RegisterNmeaMessageCallback(nmeaCallback);
107 LBSLOGI(LOCATOR_STANDARD, "[LocatorAgentManagerTest] RegisterNmeaMessageCallbackTest001 end");
108 }
109
110 HWTEST_F(LocatorAgentManagerTest, UnregisterNmeaMessageCallbackTest001, TestSize.Level1)
111 {
112 GTEST_LOG_(INFO)
113 << "LocatorAgentManagerTest, UnregisterNmeaMessageCallbackTest001, TestSize.Level1";
114 LBSLOGI(LOCATOR_STANDARD, "[LocatorAgentManagerTest] UnregisterNmeaMessageCallbackTest001 begin");
115 auto locatorAgentManager = std::make_shared<LocatorAgentManager>();
116 locatorAgentManager->UnregisterNmeaMessageCallback();
117 LBSLOGI(LOCATOR_STANDARD, "[LocatorAgentManagerTest] UnregisterNmeaMessageCallbackTest001 end");
118 }
119
120 HWTEST_F(LocatorAgentManagerTest, GetLocatorAgentTest001, TestSize.Level1)
121 {
122 GTEST_LOG_(INFO)
123 << "LocatorAgentManagerTest, GetLocatorAgentTest001, TestSize.Level1";
124 LBSLOGI(LOCATOR_STANDARD, "[LocatorAgentManagerTest] GetLocatorAgentTest001 begin");
125 auto locatorAgentManager = std::make_shared<LocatorAgentManager>();
126 locatorAgentManager->GetLocatorAgent();
127 LBSLOGI(LOCATOR_STANDARD, "[LocatorAgentManagerTest] GetLocatorAgentTest001 end");
128 }
129
130 HWTEST_F(LocatorAgentManagerTest, TryLoadLocatorSystemAbilityTest001, TestSize.Level1)
131 {
132 GTEST_LOG_(INFO)
133 << "LocatorAgentManagerTest, TryLoadLocatorSystemAbilityTest001, TestSize.Level1";
134 LBSLOGI(LOCATOR_STANDARD, "[LocatorAgentManagerTest] TryLoadLocatorSystemAbilityTest001 begin");
135 auto locatorAgentManager = std::make_shared<LocatorAgentManager>();
136 locatorAgentManager->TryLoadLocatorSystemAbility();
137 LBSLOGI(LOCATOR_STANDARD, "[LocatorAgentManagerTest] TryLoadLocatorSystemAbilityTest001 end");
138 }
139
140 HWTEST_F(LocatorAgentManagerTest, InitLocatorAgentTest001, TestSize.Level1)
141 {
142 GTEST_LOG_(INFO)
143 << "LocatorAgentManagerTest, InitLocatorAgentTest001, TestSize.Level1";
144 LBSLOGI(LOCATOR_STANDARD, "[LocatorAgentManagerTest] InitLocatorAgentTest001 begin");
145 auto locatorAgentManager = std::make_shared<LocatorAgentManager>();
146 sptr<IRemoteObject> saObject = locatorAgentManager->CheckLocatorSystemAbilityLoaded();
147 locatorAgentManager->InitLocatorAgent(saObject);
148 LBSLOGI(LOCATOR_STANDARD, "[LocatorAgentManagerTest] InitLocatorAgentTest001 end");
149 }
150
151 HWTEST_F(LocatorAgentManagerTest, ResetLocatorAgentTest001, TestSize.Level1)
152 {
153 GTEST_LOG_(INFO)
154 << "LocatorAgentManagerTest, ResetLocatorAgentTest001, TestSize.Level1";
155 LBSLOGI(LOCATOR_STANDARD, "[LocatorAgentManagerTest] ResetLocatorAgentTest001 begin");
156 auto locatorAgentManager = std::make_shared<LocatorAgentManager>();
157 sptr<IRemoteObject> saObject = locatorAgentManager->CheckLocatorSystemAbilityLoaded();
158 locatorAgentManager->ResetLocatorAgent(saObject);
159 LBSLOGI(LOCATOR_STANDARD, "[LocatorAgentManagerTest] ResetLocatorAgentTest001 end");
160 }
161
SetUp()162 void LocatorAgentTest::SetUp()
163 {
164 MockNativePermission();
165 }
166
TearDown()167 void LocatorAgentTest::TearDown()
168 {
169 }
170
MockNativePermission()171 void LocatorAgentTest::MockNativePermission()
172 {
173 const char *perms[] = {
174 ACCESS_LOCATION.c_str(), ACCESS_APPROXIMATELY_LOCATION.c_str(),
175 ACCESS_BACKGROUND_LOCATION.c_str(), MANAGE_SECURE_SETTINGS.c_str(),
176 };
177 NativeTokenInfoParams infoInstance = {
178 .dcapsNum = 0,
179 .permsNum = LOCATION_PERM_NUM,
180 .aclsNum = 0,
181 .dcaps = nullptr,
182 .perms = perms,
183 .acls = nullptr,
184 .processName = "LocatorAgentTest",
185 .aplStr = "system_basic",
186 };
187 tokenId_ = GetAccessTokenId(&infoInstance);
188 SetSelfTokenID(tokenId_);
189 Security::AccessToken::AccessTokenKit::ReloadNativeTokenInfo();
190 }
191
192 HWTEST_F(LocatorAgentTest, StartGnssLocatingTest001, TestSize.Level1)
193 {
194 GTEST_LOG_(INFO)
195 << "LocatorAgentTest, StartGnssLocatingTest001, TestSize.Level1";
196 LBSLOGI(LOCATOR_STANDARD, "[LocatorAgentTest] StartGnssLocatingTest001 begin");
197 auto locatorImpl = Locator::GetInstance();
198 EXPECT_NE(nullptr, locatorImpl);
199 locatorImpl->EnableAbility(true);
200 auto locatorAgentManager = std::make_shared<LocatorAgentManager>();
201 sptr<IRemoteObject> saObject = locatorAgentManager->CheckLocatorSystemAbilityLoaded();
202 auto locatorAgent =
203 sptr<LocatorAgent>(new (std::nothrow) LocatorAgent(saObject));
204 auto locationCallbackHost =
205 sptr<NativeLocationCallbackHost>(new (std::nothrow) NativeLocationCallbackHost());
206 auto locatorCallback = sptr<ILocatorCallback>(locationCallbackHost);
207 locatorAgent->StartGnssLocating(locatorCallback);
208 LBSLOGI(LOCATOR_STANDARD, "[LocatorAgentTest] StartGnssLocatingTest001 end");
209 }
210
211 HWTEST_F(LocatorAgentTest, StartGnssLocatingTest002, TestSize.Level1)
212 {
213 GTEST_LOG_(INFO)
214 << "LocatorAgentTest, StartGnssLocatingTest002, TestSize.Level1";
215 LBSLOGI(LOCATOR_STANDARD, "[LocatorAgentTest] StartGnssLocatingTest002 begin");
216 auto locatorAgentManager = std::make_shared<LocatorAgentManager>();
217 sptr<IRemoteObject> saObject = locatorAgentManager->CheckLocatorSystemAbilityLoaded();
218 auto locatorAgent =
219 sptr<LocatorAgent>(new (std::nothrow) LocatorAgent(saObject));
220 sptr<ILocatorCallback> locatorCallback;
221 auto errCode = locatorAgent->StartGnssLocating(locatorCallback);
222 EXPECT_EQ(ERRCODE_INVALID_PARAM, errCode);
223 LBSLOGI(LOCATOR_STANDARD, "[LocatorAgentTest] StartGnssLocatingTest002 end");
224 }
225
226 HWTEST_F(LocatorAgentTest, StopGnssLocatingTest001, TestSize.Level1)
227 {
228 GTEST_LOG_(INFO)
229 << "LocatorAgentTest, StopGnssLocatingTest001, TestSize.Level1";
230 LBSLOGI(LOCATOR_STANDARD, "[LocatorAgentTest] StopGnssLocatingTest001 begin");
231 auto locatorAgentManager = std::make_shared<LocatorAgentManager>();
232 sptr<IRemoteObject> saObject = locatorAgentManager->CheckLocatorSystemAbilityLoaded();
233 auto locatorAgent =
234 sptr<LocatorAgent>(new (std::nothrow) LocatorAgent(saObject));
235 auto locationCallbackHost =
236 sptr<NativeLocationCallbackHost>(new (std::nothrow) NativeLocationCallbackHost());
237 auto locatorCallback = sptr<ILocatorCallback>(locationCallbackHost);
238 locatorAgent->StopGnssLocating(locatorCallback);
239 LBSLOGI(LOCATOR_STANDARD, "[LocatorAgentTest] StopGnssLocatingTest001 end");
240 }
241
242 HWTEST_F(LocatorAgentTest, StopGnssLocatingTest002, TestSize.Level1)
243 {
244 GTEST_LOG_(INFO)
245 << "LocatorAgentTest, StopGnssLocatingTest002, TestSize.Level1";
246 LBSLOGI(LOCATOR_STANDARD, "[LocatorAgentTest] StopGnssLocatingTest002 begin");
247 auto locatorAgentManager = std::make_shared<LocatorAgentManager>();
248 sptr<IRemoteObject> saObject = locatorAgentManager->CheckLocatorSystemAbilityLoaded();
249 auto locatorAgent =
250 sptr<LocatorAgent>(new (std::nothrow) LocatorAgent(saObject));
251 sptr<ILocatorCallback> locatorCallback;
252 auto errCode = locatorAgent->StopGnssLocating(locatorCallback);
253 EXPECT_EQ(ERRCODE_INVALID_PARAM, errCode);
254 LBSLOGI(LOCATOR_STANDARD, "[LocatorAgentTest] StopGnssLocatingTest002 end");
255 }
256
257 HWTEST_F(LocatorAgentTest, RegisterNmeaMessageCallbackTest001, TestSize.Level1)
258 {
259 GTEST_LOG_(INFO)
260 << "LocatorAgentTest, RegisterNmeaMessageCallbackTest001, TestSize.Level1";
261 LBSLOGI(LOCATOR_STANDARD, "[LocatorAgentTest] RegisterNmeaMessageCallbackTest001 begin");
262 auto locatorAgentManager = std::make_shared<LocatorAgentManager>();
263 sptr<IRemoteObject> saObject = locatorAgentManager->CheckLocatorSystemAbilityLoaded();
264 auto nmeaCallbackHost =
265 sptr<NativeNmeaCallbackHost>(new (std::nothrow) NativeNmeaCallbackHost());
266 auto nmeaCallback = sptr<INmeaMessageCallback>(nmeaCallbackHost);
267 auto locatorAgent =
268 sptr<LocatorAgent>(new (std::nothrow) LocatorAgent(saObject));
269 auto errCode = locatorAgent->RegisterNmeaMessageCallback(nmeaCallback);
270 EXPECT_EQ(ERRCODE_SUCCESS, errCode);
271 LBSLOGI(LOCATOR_STANDARD, "[LocatorAgentTest] RegisterNmeaMessageCallbackTest001 end");
272 }
273
274 HWTEST_F(LocatorAgentTest, RegisterNmeaMessageCallbackTest002, TestSize.Level1)
275 {
276 GTEST_LOG_(INFO)
277 << "LocatorAgentTest, RegisterNmeaMessageCallbackTest002, TestSize.Level1";
278 LBSLOGI(LOCATOR_STANDARD, "[LocatorAgentTest] RegisterNmeaMessageCallbackTest002 begin");
279 auto locatorAgentManager = std::make_shared<LocatorAgentManager>();
280 sptr<IRemoteObject> saObject = locatorAgentManager->CheckLocatorSystemAbilityLoaded();
281 auto locatorAgent =
282 sptr<LocatorAgent>(new (std::nothrow) LocatorAgent(saObject));
283 auto errCode = locatorAgent->RegisterNmeaMessageCallback(nullptr);
284 EXPECT_EQ(ERRCODE_INVALID_PARAM, errCode);
285 LBSLOGI(LOCATOR_STANDARD, "[LocatorAgentTest] RegisterNmeaMessageCallbackTest002 end");
286 }
287
288 HWTEST_F(LocatorAgentTest, UnregisterNmeaMessageCallbackTest001, TestSize.Level1)
289 {
290 GTEST_LOG_(INFO)
291 << "LocatorAgentTest, UnregisterNmeaMessageCallbackTest001, TestSize.Level1";
292 LBSLOGI(LOCATOR_STANDARD, "[LocatorAgentTest] UnregisterNmeaMessageCallbackTest001 begin");
293 auto locatorAgentManager = std::make_shared<LocatorAgentManager>();
294 sptr<IRemoteObject> saObject = locatorAgentManager->CheckLocatorSystemAbilityLoaded();
295 auto locatorAgent =
296 sptr<LocatorAgent>(new (std::nothrow) LocatorAgent(saObject));
297 auto nmeaCallbackHost =
298 sptr<NativeNmeaCallbackHost>(new (std::nothrow) NativeNmeaCallbackHost());
299 auto nmeaCallback = sptr<INmeaMessageCallback>(nmeaCallbackHost);
300 auto errCode = locatorAgent->UnregisterNmeaMessageCallback(nmeaCallback);
301 EXPECT_EQ(ERRCODE_SUCCESS, errCode);
302 LBSLOGI(LOCATOR_STANDARD, "[LocatorAgentTest] UnregisterNmeaMessageCallbackTest001 end");
303 }
304
305 HWTEST_F(LocatorAgentTest, UnregisterNmeaMessageCallbackTest002, TestSize.Level1)
306 {
307 GTEST_LOG_(INFO)
308 << "LocatorAgentTest, UnregisterNmeaMessageCallbackTest002, TestSize.Level1";
309 LBSLOGI(LOCATOR_STANDARD, "[LocatorAgentTest] UnregisterNmeaMessageCallbackTest002 begin");
310 auto locatorAgentManager = std::make_shared<LocatorAgentManager>();
311 sptr<IRemoteObject> saObject = locatorAgentManager->CheckLocatorSystemAbilityLoaded();
312 auto locatorAgent =
313 sptr<LocatorAgent>(new (std::nothrow) LocatorAgent(saObject));
314 auto errCode = locatorAgent->UnregisterNmeaMessageCallback(nullptr);
315 EXPECT_EQ(ERRCODE_INVALID_PARAM, errCode);
316 LBSLOGI(LOCATOR_STANDARD, "[LocatorAgentTest] UnregisterNmeaMessageCallbackTest002 end");
317 }
318
319 HWTEST_F(LocatorAgentTest, RegisterGnssStatusCallbackTest001, TestSize.Level1)
320 {
321 GTEST_LOG_(INFO)
322 << "LocatorAgentTest, RegisterGnssStatusCallbackTest001, TestSize.Level1";
323 LBSLOGI(LOCATOR_STANDARD, "[LocatorAgentTest] RegisterGnssStatusCallbackTest001 begin");
324 auto locatorAgentManager = std::make_shared<LocatorAgentManager>();
325 auto gnssCallbackHost =
326 sptr<NativeSvCallbackHost>(new (std::nothrow) NativeSvCallbackHost());
327 auto gnssCallback = sptr<IGnssStatusCallback>(gnssCallbackHost);
328 sptr<IRemoteObject> saObject = locatorAgentManager->CheckLocatorSystemAbilityLoaded();
329 auto locatorAgent =
330 sptr<LocatorAgent>(new (std::nothrow) LocatorAgent(saObject));
331 auto errCode = locatorAgent->RegisterGnssStatusCallback(gnssCallback);
332 EXPECT_EQ(ERRCODE_SUCCESS, errCode);
333 LBSLOGI(LOCATOR_STANDARD, "[LocatorAgentTest] RegisterGnssStatusCallbackTest001 end");
334 }
335
336 HWTEST_F(LocatorAgentTest, RegisterGnssStatusCallbackTest002, TestSize.Level1)
337 {
338 GTEST_LOG_(INFO)
339 << "LocatorAgentTest, RegisterGnssStatusCallbackTest002, TestSize.Level1";
340 LBSLOGI(LOCATOR_STANDARD, "[LocatorAgentTest] RegisterGnssStatusCallbackTest002 begin");
341 auto locatorAgentManager = std::make_shared<LocatorAgentManager>();
342 sptr<IRemoteObject> saObject = locatorAgentManager->CheckLocatorSystemAbilityLoaded();
343 auto locatorAgent =
344 sptr<LocatorAgent>(new (std::nothrow) LocatorAgent(saObject));
345 auto errCode = locatorAgent->RegisterGnssStatusCallback(nullptr);
346 EXPECT_EQ(ERRCODE_INVALID_PARAM, errCode);
347 LBSLOGI(LOCATOR_STANDARD, "[LocatorAgentTest] RegisterGnssStatusCallbackTest002 end");
348 }
349
350
351 HWTEST_F(LocatorAgentTest, UnregisterGnssStatusCallbackTest001, TestSize.Level1)
352 {
353 GTEST_LOG_(INFO)
354 << "LocatorAgentTest, UnregisterGnssStatusCallbackTest001, TestSize.Level1";
355 LBSLOGI(LOCATOR_STANDARD, "[LocatorAgentTest] UnregisterGnssStatusCallbackTest001 begin");
356 auto locatorAgentManager = std::make_shared<LocatorAgentManager>();
357 sptr<IRemoteObject> saObject = locatorAgentManager->CheckLocatorSystemAbilityLoaded();
358 auto locatorAgent =
359 sptr<LocatorAgent>(new (std::nothrow) LocatorAgent(saObject));
360 auto gnssCallbackHost =
361 sptr<NativeSvCallbackHost>(new (std::nothrow) NativeSvCallbackHost());
362 auto gnssCallback = sptr<IGnssStatusCallback>(gnssCallbackHost);
363 auto errCode = locatorAgent->UnregisterGnssStatusCallback(gnssCallback);
364 EXPECT_EQ(ERRCODE_SUCCESS, errCode);
365 LBSLOGI(LOCATOR_STANDARD, "[LocatorAgentTest] UnregisterGnssStatusCallbackTest001 end");
366 }
367
368 HWTEST_F(LocatorAgentTest, UnregisterGnssStatusCallbackTest002, TestSize.Level1)
369 {
370 GTEST_LOG_(INFO)
371 << "LocatorAgentTest, UnregisterGnssStatusCallbackTest002, TestSize.Level1";
372 LBSLOGI(LOCATOR_STANDARD, "[LocatorAgentTest] UnregisterGnssStatusCallbackTest002 begin");
373 auto locatorAgentManager = std::make_shared<LocatorAgentManager>();
374 sptr<IRemoteObject> saObject = locatorAgentManager->CheckLocatorSystemAbilityLoaded();
375 auto locatorAgent =
376 sptr<LocatorAgent>(new (std::nothrow) LocatorAgent(saObject));
377 auto errCode = locatorAgent->UnregisterGnssStatusCallback(nullptr);
378 EXPECT_EQ(ERRCODE_INVALID_PARAM, errCode);
379 LBSLOGI(LOCATOR_STANDARD, "[LocatorAgentTest] UnregisterGnssStatusCallbackTest002 end");
380 }
381 } // namespace Location
382 } // namespace OHOS