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