• 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 "report_manager_test.h"
17 
18 #include "accesstoken_kit.h"
19 #include "message_parcel.h"
20 #include "nativetoken_kit.h"
21 #include "token_setproc.h"
22 
23 #include "i_locator_callback.h"
24 #include "location.h"
25 #include "locator.h"
26 #define private public
27 #include "locator_ability.h"
28 #undef private
29 #include "locator_callback_host.h"
30 #include "locator_callback_proxy.h"
31 #include "request.h"
32 #include "request_manager.h"
33 
34 using namespace testing::ext;
35 namespace OHOS {
36 namespace Location {
37 const int32_t LOCATION_PERM_NUM = 4;
38 const std::string UNKNOWN_ABILITY = "unknown_ability";
SetUp()39 void ReportManagerTest::SetUp()
40 {
41     MockNativePermission();
42     reportManager_ = DelayedSingleton<ReportManager>::GetInstance();
43     EXPECT_NE(nullptr, reportManager_);
44 }
45 
TearDown()46 void ReportManagerTest::TearDown()
47 {
48     reportManager_ = nullptr;
49     DelayedSingleton<ReportManager>::DestroyInstance();
50 }
51 
MockNativePermission()52 void ReportManagerTest::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     };
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 = "ReportManagerTest",
66         .aplStr = "system_basic",
67     };
68     tokenId_ = GetAccessTokenId(&infoInstance);
69     SetSelfTokenID(tokenId_);
70     Security::AccessToken::AccessTokenKit::ReloadNativeTokenInfo();
71 }
72 
73 HWTEST_F(ReportManagerTest, ReportRemoteCallbackTest001, TestSize.Level1)
74 {
75     GTEST_LOG_(INFO)
76         << "ReportManagerTest, ReportRemoteCallbackTest001, TestSize.Level1";
77     LBSLOGI(REPORT_MANAGER, "[ReportManagerTest] ReportRemoteCallbackTest001 begin");
78     std::unique_ptr<Location> location = std::make_unique<Location>();
79     auto locatorCallbackHostForTest =
80         sptr<LocatorCallbackHost>(new (std::nothrow) LocatorCallbackHost());
81     sptr<ILocatorCallback> locatorCallback =
82         sptr<ILocatorCallback>(locatorCallbackHostForTest);
83     EXPECT_EQ(true, reportManager_->
84         ReportRemoteCallback(locatorCallback, ILocatorCallback::RECEIVE_LOCATION_STATUS_EVENT, 1));
85     EXPECT_EQ(true, reportManager_->
86         ReportRemoteCallback(locatorCallback, ILocatorCallback::RECEIVE_ERROR_INFO_EVENT, 1));
87     EXPECT_EQ(false, reportManager_->
88         ReportRemoteCallback(locatorCallback, ILocatorCallback::RECEIVE_LOCATION_INFO_EVENT, 1));
89     LBSLOGI(REPORT_MANAGER, "[ReportManagerTest] ReportRemoteCallbackTest001 end");
90 }
91 
92 HWTEST_F(ReportManagerTest, ResultCheckTest001, TestSize.Level1)
93 {
94     GTEST_LOG_(INFO)
95         << "ReportManagerTest, ResultCheckTest001, TestSize.Level1";
96     LBSLOGI(REPORT_MANAGER, "[ReportManagerTest] ResultCheckTest001 begin");
97     std::shared_ptr<Request> request = std::make_shared<Request>();
98     request->SetUid(1000);
99     request->SetPid(0);
100     request->SetTokenId(tokenId_);
101     request->SetFirstTokenId(0);
102     request->SetPackageName("ReportManagerTest");
103     std::unique_ptr<Location> location = std::make_unique<Location>();
104     MessageParcel parcel;
105     parcel.WriteDouble(12.0); // latitude
106     parcel.WriteDouble(13.0); // longitude
107     parcel.WriteDouble(14.0); // altitude
108     parcel.WriteDouble(1000.0); // accuracy
109     parcel.WriteDouble(10.0); // speed
110     parcel.WriteDouble(90.0); // direction
111     parcel.WriteInt64(1000000000); // timeStamp
112     parcel.WriteInt64(1000000100); // timeSinceBoot
113     parcel.WriteString16(u"additions"); // additions
114     parcel.WriteInt64(1); // additionSize
115     parcel.WriteBool(true); // isFromMock
116     parcel.WriteInt32(1); // source type
117     parcel.WriteInt32(0); // floor no.
118     parcel.WriteDouble(1000.0); // floor acc
119     location->ReadFromParcel(parcel);
120     EXPECT_EQ(true, reportManager_->ResultCheck(location, request));
121     EXPECT_EQ(false, reportManager_->ResultCheck(nullptr, request)); // no location
122     EXPECT_EQ(false, reportManager_->ResultCheck(location, nullptr)); // no request
123     EXPECT_EQ(false, reportManager_->ResultCheck(nullptr, nullptr)); // no location & no request
124 
125     auto requestConfig = std::make_unique<RequestConfig>();
126     EXPECT_NE(nullptr, requestConfig);
127     requestConfig->SetPriority(PRIORITY_FAST_FIRST_FIX);
128     requestConfig->SetMaxAccuracy(1000.0);
129     requestConfig->SetFixNumber(1);
130     request->SetRequestConfig(*requestConfig);
131     EXPECT_EQ(true, reportManager_->ResultCheck(location, request)); // no last location
132 
133     std::unique_ptr<Location> lastLocation1 = std::make_unique<Location>(*location);
134     lastLocation1->SetLatitude(-91.0);
135     request->SetLastLocation(lastLocation1);
136     EXPECT_EQ(true, reportManager_->ResultCheck(location, request)); // no need to check
137 
138     std::unique_ptr<Location> lastLocation2 = std::make_unique<Location>(*location);
139     request->SetLastLocation(lastLocation2);
140     EXPECT_EQ(false, reportManager_->ResultCheck(location, request)); // time interval check failed
141 
142     std::unique_ptr<Location> lastLocation3 = std::make_unique<Location>(*location);
143     lastLocation3->SetTimeSinceBoot(1000000000);
144     requestConfig->SetDistanceInterval(1.0);
145     request->SetRequestConfig(*requestConfig);
146     request->SetLastLocation(lastLocation3);
147     EXPECT_EQ(false, reportManager_->ResultCheck(location, request)); // distance interval check failed
148 
149     std::unique_ptr<Location> lastLocation4 = std::make_unique<Location>(*location);
150     lastLocation4->SetTimeSinceBoot(1000000000);
151     requestConfig->SetDistanceInterval(0.0);
152     requestConfig->SetMaxAccuracy(10.0);
153     request->SetRequestConfig(*requestConfig);
154     request->SetLastLocation(lastLocation4);
155     EXPECT_EQ(false, reportManager_->ResultCheck(location, request)); // acc check failed
156 
157     std::unique_ptr<Location> lastLocation5 = std::make_unique<Location>(*location);
158     lastLocation5->SetTimeSinceBoot(1000000000);
159     requestConfig->SetDistanceInterval(0.0);
160     requestConfig->SetMaxAccuracy(0.0);
161     request->SetRequestConfig(*requestConfig);
162     request->SetLastLocation(lastLocation5);
163     EXPECT_EQ(false, reportManager_->ResultCheck(location, request)); // check pass
164     LBSLOGI(REPORT_MANAGER, "[ReportManagerTest] ResultCheckTest001 end");
165 }
166 
167 HWTEST_F(ReportManagerTest, SetLastLocationTest001, TestSize.Level1)
168 {
169     GTEST_LOG_(INFO)
170         << "ReportManagerTest, SetLastLocationTest001, TestSize.Level1";
171     LBSLOGI(REPORT_MANAGER, "[ReportManagerTest] SetLastLocationTest001 begin");
172     EXPECT_EQ(nullptr, reportManager_->GetLastLocation());
173     MessageParcel parcel;
174     parcel.WriteDouble(12.0); // latitude
175     parcel.WriteDouble(13.0); // longitude
176     parcel.WriteDouble(14.0); // altitude
177     parcel.WriteDouble(1000.0); // accuracy
178     parcel.WriteDouble(10.0); // speed
179     parcel.WriteDouble(90.0); // direction
180     parcel.WriteInt64(1000000000); // timeStamp
181     parcel.WriteInt64(1000000000); // timeSinceBoot
182     parcel.WriteString16(u"additions"); // additions
183     parcel.WriteInt64(1); // additionSize
184     parcel.WriteBool(true); // isFromMock
185     parcel.WriteInt32(1); // source type
186     parcel.WriteInt32(0); // floor no.
187     parcel.WriteDouble(1000.0); // floor acc
188     std::unique_ptr<Location> location = std::make_unique<Location>();
189     location->ReadFromParcel(parcel);
190     reportManager_->SetLastLocation(location);
191     EXPECT_NE(nullptr, reportManager_->GetLastLocation());
192     LBSLOGI(REPORT_MANAGER, "[ReportManagerTest] SetLastLocationTest001 end");
193 }
194 
195 HWTEST_F(ReportManagerTest, GetPermittedLocationTest001, TestSize.Level1)
196 {
197     GTEST_LOG_(INFO)
198         << "ReportManagerTest, GetPermittedLocationTest001, TestSize.Level1";
199     LBSLOGI(REPORT_MANAGER, "[ReportManagerTest] GetPermittedLocationTest001 begin");
200     EXPECT_EQ(nullptr, reportManager_->GetPermittedLocation(0, tokenId_, 0, nullptr));
201     MessageParcel parcel;
202     parcel.WriteDouble(12.0); // latitude
203     parcel.WriteDouble(13.0); // longitude
204     parcel.WriteDouble(14.0); // altitude
205     parcel.WriteDouble(1000.0); // accuracy
206     parcel.WriteDouble(10.0); // speed
207     parcel.WriteDouble(90.0); // direction
208     parcel.WriteInt64(1000000000); // timeStamp
209     parcel.WriteInt64(1000000000); // timeSinceBoot
210     parcel.WriteString16(u"additions"); // additions
211     parcel.WriteInt64(1); // additionSize
212     parcel.WriteBool(true); // isFromMock
213     parcel.WriteInt32(1); // source type
214     parcel.WriteInt32(0); // floor no.
215     parcel.WriteDouble(1000.0); // floor acc
216     std::unique_ptr<Location> location = std::make_unique<Location>();
217     location->ReadFromParcel(parcel);
218     auto newLocation = reportManager_->GetPermittedLocation(0, tokenId_, 0, location);
219     EXPECT_NE(nullptr, newLocation);
220     EXPECT_EQ(12.0, newLocation->GetLatitude());
221     EXPECT_EQ(13.0, newLocation->GetLongitude());
222     EXPECT_EQ(1000.0, newLocation->GetAccuracy());
223     LBSLOGI(REPORT_MANAGER, "[ReportManagerTest] GetPermittedLocationTest001 end");
224 }
225 
226 HWTEST_F(ReportManagerTest, OnReportLocationTest001, TestSize.Level1)
227 {
228     GTEST_LOG_(INFO)
229         << "ReportManagerTest, OnReportLocationTest001, TestSize.Level1";
230     LBSLOGI(REPORT_MANAGER, "[ReportManagerTest] OnReportLocationTest001 begin");
231     MessageParcel parcel;
232     parcel.WriteDouble(12.0); // latitude
233     parcel.WriteDouble(13.0); // longitude
234     parcel.WriteDouble(14.0); // altitude
235     parcel.WriteDouble(1000.0); // accuracy
236     parcel.WriteDouble(10.0); // speed
237     parcel.WriteDouble(90.0); // direction
238     parcel.WriteInt64(1000000000); // timeStamp
239     parcel.WriteInt64(1000000000); // timeSinceBoot
240     parcel.WriteString16(u"additions"); // additions
241     parcel.WriteInt64(1); // additionSize
242     parcel.WriteBool(false); // isFromMock
243     parcel.WriteInt32(1); // source type
244     parcel.WriteInt32(0); // floor no.
245     parcel.WriteDouble(1000.0); // floor acc
246     std::unique_ptr<Location> location = std::make_unique<Location>();
247     location->ReadFromParcel(parcel);
248 
249     EXPECT_EQ(false, reportManager_->OnReportLocation(location, UNKNOWN_ABILITY));
250     LBSLOGI(REPORT_MANAGER, "[ReportManagerTest] OnReportLocationTest001 end");
251 }
252 
253 HWTEST_F(ReportManagerTest, OnReportLocationTest002, TestSize.Level1)
254 {
255     GTEST_LOG_(INFO)
256         << "ReportManagerTest, OnReportLocationTest002, TestSize.Level1";
257     LBSLOGI(REPORT_MANAGER, "[ReportManagerTest] OnReportLocationTest002 begin");
258     MessageParcel parcel;
259     parcel.WriteDouble(12.0); // latitude
260     parcel.WriteDouble(13.0); // longitude
261     parcel.WriteDouble(14.0); // altitude
262     parcel.WriteDouble(1000.0); // accuracy
263     parcel.WriteDouble(10.0); // speed
264     parcel.WriteDouble(90.0); // direction
265     parcel.WriteInt64(1000000000); // timeStamp
266     parcel.WriteInt64(1000000000); // timeSinceBoot
267     parcel.WriteString16(u"additions"); // additions
268     parcel.WriteInt64(1); // additionSize
269     parcel.WriteBool(false); // isFromMock
270     parcel.WriteInt32(1); // source type
271     parcel.WriteInt32(0); // floor no.
272     parcel.WriteDouble(1000.0); // floor acc
273     std::unique_ptr<Location> location = std::make_unique<Location>();
274     location->ReadFromParcel(parcel);
275     EXPECT_EQ(true, reportManager_->OnReportLocation(location, GNSS_ABILITY)); // is not requesting
276     LBSLOGI(REPORT_MANAGER, "[ReportManagerTest] OnReportLocationTest002 end");
277 }
278 
279 HWTEST_F(ReportManagerTest, OnReportLocationTest003, TestSize.Level1)
280 {
281     GTEST_LOG_(INFO)
282         << "ReportManagerTest, OnReportLocationTest003, TestSize.Level1";
283     LBSLOGI(REPORT_MANAGER, "[ReportManagerTest] OnReportLocationTest003 begin");
284     MessageParcel parcel;
285     parcel.WriteDouble(12.0);         // latitude
286     parcel.WriteDouble(13.0);         // longitude
287     parcel.WriteDouble(14.0);         // altitude
288     parcel.WriteDouble(1000.0);       // accuracy
289     parcel.WriteDouble(10.0);         // speed
290     parcel.WriteDouble(90.0);         // direction
291     parcel.WriteInt64(1000000000);    // timeStamp
292     parcel.WriteInt64(1000000000);    // timeSinceBoot
293     parcel.WriteString16(u"additions"); // additions
294     parcel.WriteInt64(1);             // additionSize
295     parcel.WriteBool(false);          // isFromMock
296     parcel.WriteInt32(1); // source type
297     parcel.WriteInt32(0); // floor no.
298     parcel.WriteDouble(1000.0); // floor acc
299     std::unique_ptr<Location> location = std::make_unique<Location>();
300     location->ReadFromParcel(parcel);
301 
302     std::unique_ptr<RequestConfig> requestConfig = std::make_unique<RequestConfig>();
303     requestConfig->SetPriority(PRIORITY_ACCURACY);
304     requestConfig->SetFixNumber(0);
305     requestConfig->SetTimeInterval(1);
306     auto locatorImpl = Locator::GetInstance();
307     sptr<ILocatorCallback> callbackStub = new (std::nothrow) LocatorCallbackStub();
308     locatorImpl->EnableAbility(true);
309     locatorImpl->StartLocating(requestConfig, callbackStub); // start locating
310     sleep(1);
311     EXPECT_EQ(true, reportManager_->OnReportLocation(location, GNSS_ABILITY)); // report location successfully
312     EXPECT_EQ(true,
313         reportManager_->OnReportLocation(location, GNSS_ABILITY)); // report the same location, result check is false
314     locatorImpl->StopLocating(callbackStub);
315     LBSLOGI(REPORT_MANAGER, "[ReportManagerTest] OnReportLocationTest003 end");
316 }
317 
318 HWTEST_F(ReportManagerTest, OnReportLocationTest004, TestSize.Level1)
319 {
320     GTEST_LOG_(INFO)
321         << "ReportManagerTest, OnReportLocationTest004, TestSize.Level1";
322     LBSLOGI(REPORT_MANAGER, "[ReportManagerTest] OnReportLocationTest004 begin");
323     MessageParcel parcel;
324     parcel.WriteDouble(12.0);         // latitude
325     parcel.WriteDouble(13.0);         // longitude
326     parcel.WriteDouble(14.0);         // altitude
327     parcel.WriteDouble(1000.0);       // accuracy
328     parcel.WriteDouble(10.0);         // speed
329     parcel.WriteDouble(90.0);         // direction
330     parcel.WriteInt64(1000000000);    // timeStamp
331     parcel.WriteInt64(1000000000);    // timeSinceBoot
332     parcel.WriteString16(u"additions"); // additions
333     parcel.WriteInt64(1);             // additionSize
334     parcel.WriteBool(false);          // isFromMock
335     parcel.WriteInt32(1); // source type
336     parcel.WriteInt32(0); // floor no.
337     parcel.WriteDouble(1000.0); // floor acc
338     std::unique_ptr<Location> location = std::make_unique<Location>();
339     location->ReadFromParcel(parcel);
340 
341     std::unique_ptr<RequestConfig> requestConfig = std::make_unique<RequestConfig>();
342     requestConfig->SetPriority(PRIORITY_ACCURACY);
343     requestConfig->SetFixNumber(1); // locating once
344     requestConfig->SetTimeOut(120000);
345     auto locatorImpl = Locator::GetInstance();
346     sptr<ILocatorCallback> callbackStub = new (std::nothrow) LocatorCallbackStub();
347     locatorImpl->EnableAbility(true);
348     locatorImpl->StartLocating(requestConfig, callbackStub); // start locating
349     sleep(1);
350     EXPECT_EQ(true, reportManager_->OnReportLocation(location, GNSS_ABILITY)); // will resolve deadRequests
351     locatorImpl->StopLocating(callbackStub);
352     LBSLOGI(REPORT_MANAGER, "[ReportManagerTest] OnReportLocationTest004 end");
353 }
354 
355 HWTEST_F(ReportManagerTest, UpdateRandomTest004, TestSize.Level1)
356 {
357     GTEST_LOG_(INFO)
358         << "ReportManagerTest, UpdateRandomTest004, TestSize.Level1";
359     LBSLOGI(REPORT_MANAGER, "[ReportManagerTest] UpdateRandomTest004 begin");
360     std::list<std::shared_ptr<Request>> gnssList;
361     auto locatorAbility = sptr<LocatorAbility>(new (std::nothrow) LocatorAbility());
362     locatorAbility->requests_->insert(make_pair(GNSS_ABILITY, gnssList));
363     reportManager_->UpdateRandom();
364 
365     struct timespec now;
366     clock_gettime(CLOCK_REALTIME, &now);
367     reportManager_->lastUpdateTime_.tv_sec = now.tv_sec + LONG_TIME_INTERVAL +1;
368     locatorAbility->requests_->clear();
369     reportManager_->UpdateRandom();
370     LBSLOGI(REPORT_MANAGER, "[ReportManagerTest] UpdateRandomTest004 end");
371 }
372 }  // namespace Location
373 }  // namespace OHOS