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 #ifdef FEATURE_PASSIVE_SUPPORT
17 #define private public
18 #include "passive_ability_test.h"
19 #include "passive_ability.h"
20 #undef private
21 
22 #include "accesstoken_kit.h"
23 #include "if_system_ability_manager.h"
24 #include "ipc_skeleton.h"
25 #include "iservice_registry.h"
26 #include "nativetoken_kit.h"
27 #include "system_ability_definition.h"
28 #include "token_setproc.h"
29 
30 #include "common_utils.h"
31 #include "constant_definition.h"
32 #include "location_dumper.h"
33 #include "permission_manager.h"
34 #include "locationhub_ipc_interface_code.h"
35 
36 using namespace testing::ext;
37 
38 namespace OHOS {
39 namespace Location {
40 const int32_t LOCATION_PERM_NUM = 5;
41 const std::string ARGS_HELP = "-h";
42 const std::string UNLOAD_PASSIVE_TASK = "passive_sa_unload";
43 const int32_t WAIT_EVENT_TIME = 1;
44 
45 sptr<PassiveAbility> PassiveAbilityTest::ability_;
46 sptr<PassiveAbilityProxy> PassiveAbilityTest::proxy_;
47 
SetUp()48 void PassiveAbilityTest::SetUp()
49 {
50 }
51 
TearDown()52 void PassiveAbilityTest::TearDown()
53 {
54 }
55 
SetUpTestCase()56 void PassiveAbilityTest::SetUpTestCase()
57 {
58     MockNativePermission();
59     ability_ = new (std::nothrow) PassiveAbility();
60     proxy_ = new (std::nothrow) PassiveAbilityProxy(ability_);
61     EXPECT_NE(nullptr, proxy_);
62 }
63 
TearDownTestCase()64 void PassiveAbilityTest::TearDownTestCase()
65 {
66     ability_->passiveHandler_->RemoveAllEvents();
67     sleep(WAIT_EVENT_TIME);
68     proxy_ = nullptr;
69     ability_ = nullptr;
70 }
71 
MockNativePermission()72 void PassiveAbilityTest::MockNativePermission()
73 {
74     const char *perms[] = {
75         ACCESS_LOCATION.c_str(), ACCESS_APPROXIMATELY_LOCATION.c_str(),
76         ACCESS_BACKGROUND_LOCATION.c_str(), MANAGE_SECURE_SETTINGS.c_str(),
77         ACCESS_CONTROL_LOCATION_SWITCH.c_str(),
78     };
79     NativeTokenInfoParams infoInstance = {
80         .dcapsNum = 0,
81         .permsNum = LOCATION_PERM_NUM,
82         .aclsNum = 0,
83         .dcaps = nullptr,
84         .perms = perms,
85         .acls = nullptr,
86         .processName = "PassiveAbilityTest",
87         .aplStr = "system_basic",
88     };
89     uint64_t tokenId = GetAccessTokenId(&infoInstance);
90     SetSelfTokenID(tokenId);
91     Security::AccessToken::AccessTokenKit::ReloadNativeTokenInfo();
92 }
93 
94 /*
95  * @tc.name: SendLocationRequest001
96  * @tc.desc: Build Request, marshall and unmarshall data Then Send it
97  * @tc.type: FUNC
98  */
99 HWTEST_F(PassiveAbilityTest, SendLocationRequest001, TestSize.Level1)
100 {
101     GTEST_LOG_(INFO)
102         << "PassiveAbilityStubTest, SendLocationRequest001, TestSize.Level1";
103     LBSLOGI(PASSIVE_TEST, "[PassiveAbilityStubTest] SendLocationRequest001 begin");
104     /*
105      * @tc.steps: step1. build location request data.
106      */
107     std::unique_ptr<WorkRecord> workRecord = std::make_unique<WorkRecord>();
108     int num = 2;
109     for (int i = 0; i < num; i++) {
110         std::shared_ptr<Request> request = std::make_shared<Request>();
111         std::unique_ptr<RequestConfig> requestConfig = std::make_unique<RequestConfig>();
112         requestConfig->SetTimeInterval(i);
113         request->SetUid(i + 1);
114         request->SetPid(i + 2);
115         request->SetPackageName("nameForTest");
116         request->SetRequestConfig(*requestConfig);
117         request->SetUuid(std::to_string(CommonUtils::IntRandom(MIN_INT_RANDOM, MAX_INT_RANDOM)));
118         request->SetNlpRequestType(0);
119         workRecord->Add(request);
120     }
121     /*
122      * @tc.steps: step2. send location request
123      * @tc.expected: step2. no exception happens.
124      */
125     EXPECT_EQ(ERRCODE_SUCCESS, proxy_->SendLocationRequest(*workRecord));
126     LBSLOGI(PASSIVE_TEST, "[PassiveAbilityStubTest] SendLocationRequest001 begin");
127 }
128 
129 /*
130  * @tc.name: SetEnableAndDisable001
131  * @tc.desc: Test disable and enable system ability
132  * @tc.type: FUNC
133  */
134 HWTEST_F(PassiveAbilityTest, SetEnableAndDisable001, TestSize.Level1)
135 {
136     GTEST_LOG_(INFO)
137         << "PassiveAbilityStubTest, SetEnableAndDisable001, TestSize.Level1";
138     LBSLOGI(PASSIVE_TEST, "[PassiveAbilityStubTest] SetEnableAndDisable001 begin");
139     /*
140      * @tc.steps: step1.remove SA
141      * @tc.expected: step1. object1 is null.
142      */
143     EXPECT_EQ(ERRCODE_SUCCESS, proxy_->SetEnable(false)); // after mock, sa obj is nullptr
144 
145     /*
146      * @tc.steps: step2. test enable SA
147      * @tc.expected: step2. object2 is not null.
148      */
149     EXPECT_EQ(ERRCODE_SUCCESS, proxy_->SetEnable(true)); // after mock, sa obj is nullptr
150     LBSLOGI(PASSIVE_TEST, "[PassiveAbilityStubTest] SetEnableAndDisable001 end");
151 }
152 
153 HWTEST_F(PassiveAbilityTest, PassiveLocationMock001, TestSize.Level1)
154 {
155     GTEST_LOG_(INFO)
156         << "PassiveAbilityStubTest, PassiveLocationMock001, TestSize.Level1";
157     LBSLOGI(PASSIVE_TEST, "[PassiveAbilityStubTest] PassiveLocationMock001 begin");
158     int timeInterval = 1;
159     std::vector<std::shared_ptr<Location>> locations;
160     Parcel parcel;
161     for (int i = 0; i < 2; i++) {
162         parcel.WriteDouble(10.6); // latitude
163         parcel.WriteDouble(10.5); // longitude
164         parcel.WriteDouble(10.4); // altitude
165         parcel.WriteDouble(1.0); // accuracy
166         parcel.WriteDouble(5.0); // speed
167         parcel.WriteDouble(10); // direction
168         parcel.WriteInt64(1611000000); // timestamp
169         parcel.WriteInt64(1611000000); // time since boot
170         parcel.WriteString16(u"additions"); // additions
171         parcel.WriteInt64(1); // additionSize
172         parcel.WriteInt32(1); // isFromMock
173         locations.push_back(Location::UnmarshallingShared(parcel));
174     }
175     EXPECT_EQ(ERRCODE_SUCCESS, proxy_->EnableMock());
176     EXPECT_EQ(ERRCODE_SUCCESS, proxy_->SetMocked(timeInterval, locations));
177     ability_->passiveHandler_->RemoveEvent(static_cast<uint32_t>(PassiveInterfaceCode::SET_MOCKED_LOCATIONS));
178     sleep(WAIT_EVENT_TIME);
179 
180     EXPECT_EQ(ERRCODE_SUCCESS, proxy_->DisableMock());
181     EXPECT_EQ(ERRCODE_NOT_SUPPORTED, proxy_->SetMocked(timeInterval, locations));
182     ability_->passiveHandler_->RemoveEvent(static_cast<uint32_t>(PassiveInterfaceCode::SET_MOCKED_LOCATIONS));
183     sleep(WAIT_EVENT_TIME);
184     LBSLOGI(PASSIVE_TEST, "[PassiveAbilityStubTest] PassiveLocationMock001 end");
185 }
186 
187 HWTEST_F(PassiveAbilityTest, PassiveOnStartAndOnStop001, TestSize.Level1)
188 {
189     GTEST_LOG_(INFO)
190         << "PassiveAbilityStubTest, PassiveOnStartAndOnStop001, TestSize.Level1";
191     LBSLOGI(PASSIVE_TEST, "[PassiveAbilityStubTest] PassiveOnStartAndOnStop001 begin");
192     ability_->OnStart(); // start ability
193     ability_->OnStart(); // start ability again
194     ability_->state_ = ServiceRunningState::STATE_RUNNING;
195     ability_->OnStart(); // start ability again
196     ability_->OnStop(); // stop ability
197     ability_->OnStart(); // restart ability
198     LBSLOGI(PASSIVE_TEST, "[PassiveAbilityStubTest] PassiveOnStartAndOnStop001 end");
199 }
200 
201 HWTEST_F(PassiveAbilityTest, PassiveDump001, TestSize.Level1)
202 {
203     GTEST_LOG_(INFO)
204         << "PassiveAbilityStubTest, PassiveDump001, TestSize.Level1";
205     LBSLOGI(PASSIVE_TEST, "[PassiveAbilityStubTest] PassiveDump001 begin");
206     int32_t fd = 0;
207     std::vector<std::u16string> args;
208     std::u16string arg1 = Str8ToStr16("arg1");
209     args.emplace_back(arg1);
210     std::u16string arg2 = Str8ToStr16("arg2");
211     args.emplace_back(arg2);
212     std::u16string arg3 = Str8ToStr16("arg3");
213     args.emplace_back(arg3);
214     std::u16string arg4 = Str8ToStr16("arg4");
215     args.emplace_back(arg4);
216     EXPECT_EQ(ERR_OK, ability_->Dump(fd, args));
217 
218     std::vector<std::u16string> emptyArgs;
219     EXPECT_EQ(ERR_OK, ability_->Dump(fd, emptyArgs));
220 
221     std::vector<std::u16string> helpArgs;
222     std::u16string helpArg1 = Str8ToStr16(ARGS_HELP);
223     helpArgs.emplace_back(helpArg1);
224     EXPECT_EQ(ERR_OK, ability_->Dump(fd, helpArgs));
225     LBSLOGI(PASSIVE_TEST, "[PassiveAbilityStubTest] PassiveDump001 end");
226 }
227 
228 HWTEST_F(PassiveAbilityTest, PassiveSendReportMockLocationEvent001, TestSize.Level1)
229 {
230     GTEST_LOG_(INFO)
231         << "PassiveAbilityTest, PassiveSendReportMockLocationEvent001, TestSize.Level1";
232     LBSLOGI(PASSIVE_TEST, "[PassiveAbilityTest] PassiveSendReportMockLocationEvent001 begin");
233     ability_->SendReportMockLocationEvent(); // clear location mock
234 
235     int timeInterval = 0;
236     std::vector<std::shared_ptr<Location>> locations;
237     Parcel parcel;
238     parcel.WriteDouble(10.6); // latitude
239     parcel.WriteDouble(10.5); // longitude
240     parcel.WriteDouble(10.4); // altitude
241     parcel.WriteDouble(1.0); // accuracy
242     parcel.WriteDouble(5.0); // speed
243     parcel.WriteDouble(10); // direction
244     parcel.WriteInt64(1611000000); // timestamp
245     parcel.WriteInt64(1611000000); // time since boot
246     parcel.WriteString16(u"additions"); // additions
247     parcel.WriteInt64(1); // additionSize
248     parcel.WriteInt32(1); // isFromMock is true
249     locations.push_back(Location::UnmarshallingShared(parcel));
250     EXPECT_EQ(ERRCODE_SUCCESS, ability_->EnableMock());
251     EXPECT_EQ(ERRCODE_SUCCESS, ability_->SetMocked(timeInterval, locations));
252     sleep(2);
253     LBSLOGI(PASSIVE_TEST, "[PassiveAbilityTest] PassiveSendReportMockLocationEvent001 end");
254 }
255 
256 HWTEST_F(PassiveAbilityTest, PassiveSendReportMockLocationEvent002, TestSize.Level1)
257 {
258     GTEST_LOG_(INFO)
259         << "PassiveAbilityTest, PassiveSendReportMockLocationEvent002, TestSize.Level1";
260     LBSLOGI(PASSIVE_TEST, "[PassiveAbilityTest] PassiveSendReportMockLocationEvent002 begin");
261     ability_->SendReportMockLocationEvent(); // clear location mock
262 
263     int timeInterval = 0;
264     std::vector<std::shared_ptr<Location>> locations;
265     Parcel parcel;
266     parcel.WriteDouble(10.6); // latitude
267     parcel.WriteDouble(10.5); // longitude
268     parcel.WriteDouble(10.4); // altitude
269     parcel.WriteDouble(1.0); // accuracy
270     parcel.WriteDouble(5.0); // speed
271     parcel.WriteDouble(10); // direction
272     parcel.WriteInt64(1611000000); // timestamp
273     parcel.WriteInt64(1611000000); // time since boot
274     parcel.WriteString16(u"additions"); // additions
275     parcel.WriteInt64(1); // additionSize
276     parcel.WriteInt32(1); // isFromMock is true
277     locations.push_back(Location::UnmarshallingShared(parcel));
278     EXPECT_EQ(ERRCODE_SUCCESS, ability_->EnableMock());
279     EXPECT_EQ(ERRCODE_SUCCESS, ability_->SetMocked(timeInterval, locations));
280     sleep(2);
281     LBSLOGI(PASSIVE_TEST, "[PassiveAbilityTest] PassiveSendReportMockLocationEvent002 end");
282 }
283 
284 HWTEST_F(PassiveAbilityTest, PassiveSendReportUnloadPassiveSystemAbility001, TestSize.Level1)
285 {
286     GTEST_LOG_(INFO)
287         << "PassiveAbilityTest, PassiveSendReportUnloadPassiveSystemAbility001, TestSize.Level1";
288     LBSLOGI(PASSIVE_TEST, "[PassiveAbilityTest] PassiveSendReportUnloadPassiveSystemAbility001 begin");
289     ability_->UnloadPassiveSystemAbility();
290     LBSLOGI(PASSIVE_TEST, "[PassiveAbilityTest] PassiveSendReportUnloadPassiveSystemAbility001 end");
291 }
292 
293 HWTEST_F(PassiveAbilityTest, PassiveSendReportSendMessage001, TestSize.Level1)
294 {
295     GTEST_LOG_(INFO)
296         << "PassiveAbilityTest, PassiveSendReportUnloadSendMessage001, TestSize.Level1";
297     LBSLOGI(PASSIVE_TEST, "[PassiveAbilityTest] PassiveSendReportUnloadSendMessage001 begin");
298     MessageParcel parcel;
299     parcel.WriteDouble(10.6); // latitude
300     parcel.WriteDouble(10.5); // longitude
301     parcel.WriteDouble(10.4); // altitude
302     parcel.WriteDouble(1.0); // accuracy
303     parcel.WriteDouble(5.0); // speed
304     parcel.WriteDouble(10); // direction
305     parcel.WriteInt64(1611000000); // timestamp
306     parcel.WriteInt64(1611000000); // time since boot
307     parcel.WriteString16(u"additions"); // additions
308     parcel.WriteInt64(1); // additionSize
309     parcel.WriteInt32(1); // isFromMock is true
310     MessageParcel reply;
311     ability_->SendMessage(ERRCODE_NOT_SUPPORTED, parcel, reply);
312     LBSLOGI(PASSIVE_TEST, "[PassiveAbilityTest] PassiveSendReportSendMessage001 end");
313 }
314 
315 HWTEST_F(PassiveAbilityTest, PassiveOnStartAndOnStop002, TestSize.Level1)
316 {
317     GTEST_LOG_(INFO)
318         << "PassiveAbilityStubTest, PassiveOnStartAndOnStop002, TestSize.Level1";
319     LBSLOGI(PASSIVE_TEST, "[PassiveAbilityStubTest] PassiveOnStartAndOnStop002 begin");
320     ability_->state_ = ServiceRunningState::STATE_RUNNING;
321     ability_->OnStart(); // start ability again
322     LBSLOGI(PASSIVE_TEST, "[PassiveAbilityStubTest] PassiveOnStartAndOnStop002 end");
323 }
324 
325 HWTEST_F(PassiveAbilityTest, PassiveInit002, TestSize.Level1)
326 {
327     GTEST_LOG_(INFO)
328         << "PassiveAbilityStubTest, PassiveInit002, TestSize.Level1";
329     LBSLOGI(PASSIVE_TEST, "[PassiveAbilityStubTest] PassiveInit002 begin");
330     ability_->registerToAbility_ = true;
331     ability_->Init(); // start ability again
332     LBSLOGI(PASSIVE_TEST, "[PassiveAbilityStubTest] PassiveInit002 end");
333 }
334 } // namespace Location
335 } // namespace OHOS
336 #endif // FEATURE_PASSIVE_SUPPORT
337