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 "location_without_permission_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 #ifdef FEATURE_GNSS_SUPPORT
24 #include "cached_locations_callback_host.h"
25 #endif
26 #include "constant_definition.h"
27 #ifdef FEATURE_GEOCODE_SUPPORT
28 #include "geo_address.h"
29 #endif
30 #ifdef FEATURE_GNSS_SUPPORT
31 #include "gnss_status_callback_host.h"
32 #include "i_cached_locations_callback.h"
33 #endif
34 #include "i_locator_callback.h"
35 #include "location_switch_callback_host.h"
36 #include "locator.h"
37 #include "location_log.h"
38 #include "locator_callback_proxy.h"
39 #include "locator_impl.h"
40 #ifdef FEATURE_GNSS_SUPPORT
41 #include "nmea_message_callback_host.h"
42 #endif
43 #include "request_config.h"
44 #include "permission_manager.h"
45 #include "geofence_request.h"
46
47 using namespace testing::ext;
48 namespace OHOS {
49 namespace Location {
50 const int32_t LOCATION_WITHOUT_PERM = 1;
SetUp()51 void LocationWithoutPermissionTest::SetUp()
52 {
53 MockNativePermission();
54 }
55
TearDown()56 void LocationWithoutPermissionTest::TearDown()
57 {
58 }
59
MockNativePermission()60 void LocationWithoutPermissionTest::MockNativePermission()
61 {
62 const char *perms[] = {
63 ACCESS_BACKGROUND_LOCATION.c_str(),
64 };
65 NativeTokenInfoParams infoInstance = {
66 .dcapsNum = 0,
67 .permsNum = LOCATION_WITHOUT_PERM,
68 .aclsNum = 0,
69 .dcaps = nullptr,
70 .perms = perms,
71 .acls = nullptr,
72 .processName = "LocatorPermissionTest",
73 .aplStr = "system_basic",
74 };
75 tokenId_ = GetAccessTokenId(&infoInstance);
76 SetSelfTokenID(tokenId_);
77 Security::AccessToken::AccessTokenKit::ReloadNativeTokenInfo();
78 }
79
80 HWTEST_F(LocationWithoutPermissionTest, LocatorWithoutSettingsPermission001, TestSize.Level1)
81 {
82 GTEST_LOG_(INFO)
83 << "LocationWithoutPermissionTest, LocatorWithoutSettingsPermission001, TestSize.Level1";
84 LBSLOGI(LOCATOR, "[LocationWithoutPermissionTest] LocatorWithoutSettingsPermission001 begin");
85 auto locatorImpl = Locator::GetInstance();
86 EXPECT_NE(nullptr, locatorImpl);
87 auto switchCallbackHost =
88 sptr<LocationSwitchCallbackHost>(new (std::nothrow) LocationSwitchCallbackHost());
89 EXPECT_NE(nullptr, switchCallbackHost);
90 EXPECT_EQ(true, locatorImpl->RegisterSwitchCallback(switchCallbackHost->AsObject(), 1000));
91 EXPECT_EQ(true, locatorImpl->UnregisterSwitchCallback(switchCallbackHost->AsObject()));
92 locatorImpl->EnableAbility(true);
93 LBSLOGI(LOCATOR, "[LocationWithoutPermissionTest] LocatorWithoutSettingsPermission001 end");
94 }
95
96 HWTEST_F(LocationWithoutPermissionTest, LocatorWithoutLocationPermission001, TestSize.Level1)
97 {
98 GTEST_LOG_(INFO)
99 << "LocationWithoutPermissionTest, LocatorWithoutLocationPermission001, TestSize.Level1";
100 LBSLOGI(LOCATOR, "[LocationWithoutPermissionTest] LocatorWithoutLocationPermission001 begin");
101 auto locatorImpl = Locator::GetInstance();
102 EXPECT_NE(nullptr, locatorImpl);
103
104 std::unique_ptr<RequestConfig> requestConfig = std::make_unique<RequestConfig>();
105 requestConfig->SetPriority(PRIORITY_ACCURACY);
106 sptr<ILocatorCallback> callbackStub = new (std::nothrow) LocatorCallbackStub();
107 locatorImpl->StartLocating(requestConfig, callbackStub);
108 locatorImpl->StopLocating(callbackStub);
109
110 EXPECT_EQ(nullptr, locatorImpl->GetCachedLocation());
111 #ifdef FEATURE_GEOCODE_SUPPORT
112 locatorImpl->IsGeoServiceAvailable();
113 MessageParcel request001;
114 std::list<std::shared_ptr<GeoAddress>> geoAddressList001;
115 locatorImpl->GetAddressByCoordinate(request001, geoAddressList001);
116 MessageParcel request002;
117 std::list<std::shared_ptr<GeoAddress>> geoAddressList002;
118 locatorImpl->GetAddressByLocationName(request002, geoAddressList002);
119 #endif
120 LBSLOGI(LOCATOR, "[LocationWithoutPermissionTest] LocatorWithoutLocationPermission001 end");
121 }
122
123 HWTEST_F(LocationWithoutPermissionTest, LocatorWithoutLocationPermission002, TestSize.Level1)
124 {
125 GTEST_LOG_(INFO)
126 << "LocationWithoutPermissionTest, LocatorWithoutLocationPermission002, TestSize.Level1";
127 LBSLOGI(LOCATOR, "[LocationWithoutPermissionTest] LocatorWithoutLocationPermission002 begin");
128 auto locatorImpl = Locator::GetInstance();
129 EXPECT_NE(nullptr, locatorImpl);
130 bool state = false;
131 EXPECT_EQ(ERRCODE_SUCCESS, locatorImpl->IsLocationEnabledV9(state));
132 #ifdef FEATURE_GNSS_SUPPORT
133 auto gnssCallbackHost =
134 sptr<GnssStatusCallbackHost>(new (std::nothrow) GnssStatusCallbackHost());
135 EXPECT_NE(nullptr, gnssCallbackHost);
136 EXPECT_EQ(true, locatorImpl->RegisterGnssStatusCallback(gnssCallbackHost->AsObject(), 1000));
137 EXPECT_EQ(true, locatorImpl->UnregisterGnssStatusCallback(gnssCallbackHost->AsObject()));
138
139 auto nmeaCallbackHost =
140 sptr<NmeaMessageCallbackHost>(new (std::nothrow) NmeaMessageCallbackHost());
141 EXPECT_NE(nullptr, nmeaCallbackHost);
142 EXPECT_EQ(true, locatorImpl->RegisterNmeaMessageCallback(nmeaCallbackHost->AsObject(), 1000));
143 EXPECT_EQ(true, locatorImpl->UnregisterNmeaMessageCallback(nmeaCallbackHost->AsObject()));
144 #endif
145 EXPECT_EQ(false, locatorImpl->IsLocationPrivacyConfirmed(1));
146 EXPECT_EQ(ERRCODE_PERMISSION_DENIED, locatorImpl->SetLocationPrivacyConfirmStatus(1, true));
147 #ifdef FEATURE_GNSS_SUPPORT
148 if (state) {
149 EXPECT_EQ(ERRCODE_PERMISSION_DENIED, locatorImpl->FlushCachedGnssLocations());
150 } else {
151 EXPECT_EQ(ERRCODE_SWITCH_OFF, locatorImpl->FlushCachedGnssLocations());
152 }
153 #endif
154 LBSLOGI(LOCATOR, "[LocationWithoutPermissionTest] LocatorWithoutLocationPermission002 end");
155 }
156
157 HWTEST_F(LocationWithoutPermissionTest, LocatorWithoutLocationPermission003, TestSize.Level1)
158 {
159 GTEST_LOG_(INFO)
160 << "LocationWithoutPermissionTest, LocatorWithoutLocationPermission003, TestSize.Level1";
161 LBSLOGI(LOCATOR, "[LocationWithoutPermissionTest] LocatorWithoutLocationPermission003 begin");
162 auto locatorImpl = Locator::GetInstance();
163 EXPECT_NE(nullptr, locatorImpl);
164 #ifdef FEATURE_GNSS_SUPPORT
165 locatorImpl->GetCachedGnssLocationsSize();
166
167 auto cachedLocationsCallbackHost =
168 sptr<CachedLocationsCallbackHost>(new (std::nothrow) CachedLocationsCallbackHost());
169 EXPECT_NE(nullptr, cachedLocationsCallbackHost);
170 auto cachedCallback = sptr<ICachedLocationsCallback>(cachedLocationsCallbackHost);
171 EXPECT_NE(nullptr, cachedCallback);
172 auto request = std::make_unique<CachedGnssLocationsRequest>();
173 EXPECT_NE(nullptr, request);
174 request->reportingPeriodSec = 10;
175 request->wakeUpCacheQueueFull = true;
176 locatorImpl->RegisterCachedLocationCallback(request, cachedCallback);
177 locatorImpl->UnregisterCachedLocationCallback(cachedCallback);
178 #endif
179 LBSLOGI(LOCATOR, "[LocationWithoutPermissionTest] LocatorWithoutLocationPermission003 end");
180 }
181
182 HWTEST_F(LocationWithoutPermissionTest, LocatorWithoutSettingsPermissionV9001, TestSize.Level1)
183 {
184 GTEST_LOG_(INFO)
185 << "LocationWithoutPermissionTest, LocatorWithoutSettingsPermissionV9001, TestSize.Level1";
186 LBSLOGI(LOCATOR, "[LocationWithoutPermissionTest] LocatorWithoutSettingsPermissionV9001 begin");
187 auto locatorImpl = Locator::GetInstance();
188 EXPECT_NE(nullptr, locatorImpl);
189 EXPECT_EQ(ERRCODE_PERMISSION_DENIED, locatorImpl->EnableAbilityV9(true));
190 EXPECT_EQ(ERRCODE_PERMISSION_DENIED, locatorImpl->SetLocationPrivacyConfirmStatusV9(1, true));
191 LBSLOGI(LOCATOR, "[LocationWithoutPermissionTest] LocatorWithoutSettingsPermissionV9001 end");
192 }
193
194 HWTEST_F(LocationWithoutPermissionTest, LocatorWithoutLocationPermissionV9001, TestSize.Level1)
195 {
196 GTEST_LOG_(INFO)
197 << "LocationWithoutPermissionTest, LocatorWithoutLocationPermissionV9001, TestSize.Level1";
198 LBSLOGI(LOCATOR, "[LocationWithoutPermissionTest] LocatorWithoutLocationPermissionV9001 begin");
199 auto locatorImpl = Locator::GetInstance();
200 EXPECT_NE(nullptr, locatorImpl);
201
202 bool state = false;
203 EXPECT_EQ(ERRCODE_SUCCESS, locatorImpl->IsLocationEnabledV9(state));
204
205 std::unique_ptr<RequestConfig> requestConfig = std::make_unique<RequestConfig>();
206 requestConfig->SetPriority(PRIORITY_ACCURACY);
207 sptr<ILocatorCallback> callbackStub = new (std::nothrow) LocatorCallbackStub();
208 if (state) {
209 EXPECT_EQ(ERRCODE_PERMISSION_DENIED, locatorImpl->StartLocatingV9(requestConfig, callbackStub));
210 } else {
211 EXPECT_EQ(ERRCODE_SWITCH_OFF, locatorImpl->StartLocatingV9(requestConfig, callbackStub));
212 }
213 EXPECT_EQ(ERRCODE_PERMISSION_DENIED, locatorImpl->StopLocatingV9(callbackStub));
214
215 std::unique_ptr loc = std::make_unique<Location>();
216 if (state) {
217 EXPECT_EQ(ERRCODE_PERMISSION_DENIED, locatorImpl->GetCachedLocationV9(loc));
218 } else {
219 EXPECT_EQ(ERRCODE_SWITCH_OFF, locatorImpl->GetCachedLocationV9(loc));
220 }
221 EXPECT_EQ(nullptr, loc);
222 LBSLOGI(LOCATOR, "[LocationWithoutPermissionTest] LocatorWithoutLocationPermissionV9001 end");
223 }
224
225 HWTEST_F(LocationWithoutPermissionTest, LocatorWithoutLocationPermissionV9002, TestSize.Level1)
226 {
227 GTEST_LOG_(INFO)
228 << "LocationWithoutPermissionTest, LocatorWithoutLocationPermissionV9002, TestSize.Level1";
229 LBSLOGI(LOCATOR, "[LocationWithoutPermissionTest] LocatorWithoutLocationPermissionV9002 begin");
230 auto locatorImpl = Locator::GetInstance();
231 EXPECT_NE(nullptr, locatorImpl);
232
233 bool state = false;
234 EXPECT_EQ(ERRCODE_SUCCESS, locatorImpl->IsLocationEnabledV9(state));
235 #ifdef FEATURE_GNSS_SUPPORT
236 auto gnssCallbackHost =
237 sptr<GnssStatusCallbackHost>(new (std::nothrow) GnssStatusCallbackHost());
238 EXPECT_NE(nullptr, gnssCallbackHost);
239 if (state) {
240 EXPECT_EQ(ERRCODE_PERMISSION_DENIED, locatorImpl->RegisterGnssStatusCallbackV9(gnssCallbackHost->AsObject()));
241 } else {
242 EXPECT_EQ(ERRCODE_SWITCH_OFF, locatorImpl->RegisterGnssStatusCallbackV9(gnssCallbackHost->AsObject()));
243 }
244 EXPECT_EQ(ERRCODE_PERMISSION_DENIED, locatorImpl->UnregisterGnssStatusCallbackV9(gnssCallbackHost->AsObject()));
245
246 auto nmeaCallbackHost =
247 sptr<NmeaMessageCallbackHost>(new (std::nothrow) NmeaMessageCallbackHost());
248 EXPECT_NE(nullptr, nmeaCallbackHost);
249 if (state) {
250 EXPECT_EQ(ERRCODE_PERMISSION_DENIED, locatorImpl->RegisterNmeaMessageCallbackV9(nmeaCallbackHost->AsObject()));
251 } else {
252 EXPECT_EQ(ERRCODE_SWITCH_OFF, locatorImpl->RegisterNmeaMessageCallbackV9(nmeaCallbackHost->AsObject()));
253 }
254 EXPECT_EQ(ERRCODE_PERMISSION_DENIED, locatorImpl->UnregisterNmeaMessageCallbackV9(nmeaCallbackHost->AsObject()));
255 if (state) {
256 EXPECT_EQ(ERRCODE_PERMISSION_DENIED, locatorImpl->FlushCachedGnssLocationsV9());
257 } else {
258 EXPECT_EQ(ERRCODE_SWITCH_OFF, locatorImpl->FlushCachedGnssLocationsV9());
259 }
260 #endif
261 std::set<int> pidList;
262 pidList.insert(1000);
263 EXPECT_EQ(ERRCODE_PERMISSION_DENIED, locatorImpl->ProxyForFreeze(pidList, false));
264 EXPECT_EQ(ERRCODE_PERMISSION_DENIED, locatorImpl->ResetAllProxy());
265 LBSLOGI(LOCATOR, "[LocationWithoutPermissionTest] LocatorWithoutLocationPermissionV9002 end");
266 }
267 } // namespace Location
268 } // namespace OHOS