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