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