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 "passive_ability_test.h"
17
18 #include "accesstoken_kit.h"
19 #include "if_system_ability_manager.h"
20 #include "ipc_skeleton.h"
21 #include "iservice_registry.h"
22 #include "nativetoken_kit.h"
23 #include "system_ability_definition.h"
24 #include "token_setproc.h"
25
26 #include "common_utils.h"
27 #include "constant_definition.h"
28 #include "location_dumper.h"
29
30 using namespace testing::ext;
31
32 namespace OHOS {
33 namespace Location {
34 const int32_t LOCATION_PERM_NUM = 4;
35 const std::string ARGS_HELP = "-h";
SetUp()36 void PassiveAbilityTest::SetUp()
37 {
38 /*
39 * @tc.setup: Get system ability's pointer and get sa proxy object.
40 */
41 MockNativePermission();
42 ability_ = new (std::nothrow) PassiveAbility();
43 EXPECT_NE(nullptr, ability_);
44 proxy_ = new (std::nothrow) PassiveAbilityProxy(ability_);
45 EXPECT_NE(nullptr, proxy_);
46 }
47
TearDown()48 void PassiveAbilityTest::TearDown()
49 {
50 /*
51 * @tc.teardown: release memory.
52 */
53 proxy_ = nullptr;
54 }
55
MockNativePermission()56 void PassiveAbilityTest::MockNativePermission()
57 {
58 const char *perms[] = {
59 ACCESS_LOCATION.c_str(), ACCESS_APPROXIMATELY_LOCATION.c_str(),
60 ACCESS_BACKGROUND_LOCATION.c_str(), MANAGE_SECURE_SETTINGS.c_str(),
61 };
62 NativeTokenInfoParams infoInstance = {
63 .dcapsNum = 0,
64 .permsNum = LOCATION_PERM_NUM,
65 .aclsNum = 0,
66 .dcaps = nullptr,
67 .perms = perms,
68 .acls = nullptr,
69 .processName = "PassiveAbilityTest",
70 .aplStr = "system_basic",
71 };
72 uint64_t tokenId = GetAccessTokenId(&infoInstance);
73 SetSelfTokenID(tokenId);
74 Security::AccessToken::AccessTokenKit::ReloadNativeTokenInfo();
75 }
76
77 /*
78 * @tc.name: SendLocationRequest001
79 * @tc.desc: Build Request, marshall and unmarshall data Then Send it
80 * @tc.type: FUNC
81 */
82 HWTEST_F(PassiveAbilityTest, SendLocationRequest001, TestSize.Level1)
83 {
84 GTEST_LOG_(INFO)
85 << "PassiveAbilityStubTest, SendLocationRequest001, TestSize.Level1";
86 LBSLOGI(PASSIVE_TEST, "[PassiveAbilityStubTest] SendLocationRequest001 begin");
87 /*
88 * @tc.steps: step1. build location request data.
89 */
90 std::unique_ptr<WorkRecord> workRecord = std::make_unique<WorkRecord>();
91 int num = 2;
92 for (int i = 0; i < num; i++) {
93 int uid = i + 1;
94 int pid = i + 2;
95 int timeInterval = i;
96 std::string name = "nameForTest";
97 std::string uuid = std::to_string(CommonUtils::IntRandom(MIN_INT_RANDOM, MAX_INT_RANDOM));
98 workRecord->Add(uid, pid, name, timeInterval, uuid);
99 }
100 /*
101 * @tc.steps: step2. send location request
102 * @tc.expected: step2. no exception happens.
103 */
104 EXPECT_EQ(ERRCODE_SUCCESS, proxy_->SendLocationRequest(*workRecord));
105 LBSLOGI(PASSIVE_TEST, "[PassiveAbilityStubTest] SendLocationRequest001 begin");
106 }
107
108 /*
109 * @tc.name: SetEnableAndDisable001
110 * @tc.desc: Test disable and enable system ability
111 * @tc.type: FUNC
112 */
113 HWTEST_F(PassiveAbilityTest, SetEnableAndDisable001, TestSize.Level1)
114 {
115 GTEST_LOG_(INFO)
116 << "PassiveAbilityStubTest, SetEnableAndDisable001, TestSize.Level1";
117 LBSLOGI(PASSIVE_TEST, "[PassiveAbilityStubTest] SetEnableAndDisable001 begin");
118 /*
119 * @tc.steps: step1.remove SA
120 * @tc.expected: step1. object1 is null.
121 */
122 EXPECT_EQ(ERRCODE_SUCCESS, proxy_->SetEnable(false)); // if the state is false
123 sptr<ISystemAbilityManager> systemAbilityManager1 =
124 SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
125 sptr<IRemoteObject> object1 = systemAbilityManager1->GetSystemAbility(LOCATION_NOPOWER_LOCATING_SA_ID);
126 EXPECT_EQ(nullptr, object1); // no SA can be given
127
128 /*
129 * @tc.steps: step2. test enable SA
130 * @tc.expected: step2. object2 is not null.
131 */
132 EXPECT_EQ(ERRCODE_SUCCESS, proxy_->SetEnable(true)); // if the state is true
133 sptr<ISystemAbilityManager> systemAbilityManager2 =
134 SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
135 sptr<IRemoteObject> object2 = systemAbilityManager2->GetSystemAbility(LOCATION_NOPOWER_LOCATING_SA_ID);
136 EXPECT_NE(nullptr, object2); // SA can be given
137 LBSLOGI(PASSIVE_TEST, "[PassiveAbilityStubTest] SetEnableAndDisable001 end");
138 }
139
140 HWTEST_F(PassiveAbilityTest, PassiveLocationMock001, TestSize.Level1)
141 {
142 GTEST_LOG_(INFO)
143 << "PassiveAbilityStubTest, PassiveLocationMock001, TestSize.Level1";
144 LBSLOGI(PASSIVE_TEST, "[PassiveAbilityStubTest] PassiveLocationMock001 begin");
145 int timeInterval = 1;
146 std::vector<std::shared_ptr<Location>> locations;
147 EXPECT_EQ(ERRCODE_SUCCESS, proxy_->EnableMock());
148 EXPECT_EQ(ERRCODE_SUCCESS, proxy_->SetMocked(timeInterval, locations));
149
150 EXPECT_EQ(ERRCODE_SUCCESS, proxy_->DisableMock());
151 EXPECT_EQ(ERRCODE_NOT_SUPPORTED, proxy_->SetMocked(timeInterval, locations));
152 LBSLOGI(PASSIVE_TEST, "[PassiveAbilityStubTest] PassiveLocationMock001 end");
153 }
154
155 HWTEST_F(PassiveAbilityTest, PassiveOnStartAndOnStop001, TestSize.Level1)
156 {
157 GTEST_LOG_(INFO)
158 << "PassiveAbilityStubTest, PassiveOnStartAndOnStop001, TestSize.Level1";
159 LBSLOGI(PASSIVE_TEST, "[PassiveAbilityStubTest] PassiveOnStartAndOnStop001 begin");
160 ability_->OnStart(); // start ability
161 EXPECT_EQ(ServiceRunningState::STATE_RUNNING, ability_->QueryServiceState());
162 ability_->OnStart(); // start ability again
163 EXPECT_EQ(ServiceRunningState::STATE_RUNNING, ability_->QueryServiceState());
164
165 ability_->OnStop(); // stop ability
166 EXPECT_EQ(ServiceRunningState::STATE_NOT_START, ability_->QueryServiceState());
167 ability_->OnStart(); // restart ability
168 EXPECT_EQ(ServiceRunningState::STATE_RUNNING, ability_->QueryServiceState());
169 LBSLOGI(PASSIVE_TEST, "[PassiveAbilityStubTest] PassiveOnStartAndOnStop001 end");
170 }
171
172 HWTEST_F(PassiveAbilityTest, PassiveDump001, TestSize.Level1)
173 {
174 GTEST_LOG_(INFO)
175 << "PassiveAbilityStubTest, PassiveDump001, TestSize.Level1";
176 LBSLOGI(PASSIVE_TEST, "[PassiveAbilityStubTest] PassiveDump001 begin");
177 int32_t fd = 0;
178 std::vector<std::u16string> args;
179 std::u16string arg1 = Str8ToStr16("arg1");
180 args.emplace_back(arg1);
181 std::u16string arg2 = Str8ToStr16("arg2");
182 args.emplace_back(arg2);
183 std::u16string arg3 = Str8ToStr16("arg3");
184 args.emplace_back(arg3);
185 std::u16string arg4 = Str8ToStr16("arg4");
186 args.emplace_back(arg4);
187 EXPECT_EQ(ERR_OK, ability_->Dump(fd, args));
188
189 std::vector<std::u16string> emptyArgs;
190 EXPECT_EQ(ERR_OK, ability_->Dump(fd, emptyArgs));
191
192 std::vector<std::u16string> helpArgs;
193 std::u16string helpArg1 = Str8ToStr16(ARGS_HELP);
194 helpArgs.emplace_back(helpArg1);
195 EXPECT_EQ(ERR_OK, ability_->Dump(fd, helpArgs));
196 LBSLOGI(PASSIVE_TEST, "[PassiveAbilityStubTest] PassiveDump001 end");
197 }
198
199 HWTEST_F(PassiveAbilityTest, PassiveSendReportMockLocationEvent001, TestSize.Level1)
200 {
201 GTEST_LOG_(INFO)
202 << "PassiveAbilityTest, PassiveSendReportMockLocationEvent001, TestSize.Level1";
203 LBSLOGI(PASSIVE_TEST, "[PassiveAbilityTest] PassiveSendReportMockLocationEvent001 begin");
204 ability_->SendReportMockLocationEvent(); // clear location mock
205
206 int timeInterval = 0;
207 std::vector<std::shared_ptr<Location>> locations;
208 Parcel parcel;
209 parcel.WriteDouble(10.6); // latitude
210 parcel.WriteDouble(10.5); // longitude
211 parcel.WriteDouble(10.4); // altitude
212 parcel.WriteDouble(1.0); // accuracy
213 parcel.WriteDouble(5.0); // speed
214 parcel.WriteDouble(10); // direction
215 parcel.WriteInt64(1611000000); // timestamp
216 parcel.WriteInt64(1611000000); // time since boot
217 parcel.WriteString16(u"additions"); // additions
218 parcel.WriteInt64(1); // additionSize
219 parcel.WriteBool(true); // isFromMock is true
220 parcel.WriteInt32(1); // source type
221 parcel.WriteInt32(0); // floor no.
222 parcel.WriteDouble(1000.0); // floor acc
223 locations.push_back(Location::UnmarshallingShared(parcel));
224 EXPECT_EQ(ERRCODE_SUCCESS, ability_->EnableMock());
225 EXPECT_EQ(ERRCODE_SUCCESS, ability_->SetMocked(timeInterval, locations));
226 sleep(2);
227 LBSLOGI(PASSIVE_TEST, "[PassiveAbilityTest] PassiveSendReportMockLocationEvent001 end");
228 }
229
230 HWTEST_F(PassiveAbilityTest, PassiveSendReportMockLocationEvent002, TestSize.Level1)
231 {
232 GTEST_LOG_(INFO)
233 << "PassiveAbilityTest, PassiveSendReportMockLocationEvent002, TestSize.Level1";
234 LBSLOGI(PASSIVE_TEST, "[PassiveAbilityTest] PassiveSendReportMockLocationEvent002 begin");
235 ability_->SendReportMockLocationEvent(); // clear location mock
236
237 int timeInterval = 0;
238 std::vector<std::shared_ptr<Location>> locations;
239 Parcel parcel;
240 parcel.WriteDouble(10.6); // latitude
241 parcel.WriteDouble(10.5); // longitude
242 parcel.WriteDouble(10.4); // altitude
243 parcel.WriteDouble(1.0); // accuracy
244 parcel.WriteDouble(5.0); // speed
245 parcel.WriteDouble(10); // direction
246 parcel.WriteInt64(1611000000); // timestamp
247 parcel.WriteInt64(1611000000); // time since boot
248 parcel.WriteString16(u"additions"); // additions
249 parcel.WriteInt64(1); // additionSize
250 parcel.WriteBool(false); // isFromMock is true
251 parcel.WriteInt32(1); // source type
252 parcel.WriteInt32(0); // floor no.
253 parcel.WriteDouble(1000.0); // floor acc
254 locations.push_back(Location::UnmarshallingShared(parcel));
255 EXPECT_EQ(ERRCODE_SUCCESS, ability_->EnableMock());
256 EXPECT_EQ(ERRCODE_SUCCESS, ability_->SetMocked(timeInterval, locations));
257 sleep(2);
258 LBSLOGI(PASSIVE_TEST, "[PassiveAbilityTest] PassiveSendReportMockLocationEvent002 end");
259 }
260 } // namespace Location
261 } // namespace OHOS