• 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_GEOCODE_SUPPORT
17 #define private public
18 #include "geo_convert_service_test.h"
19 #undef private
20 
21 #include "parameters.h"
22 #include <string>
23 #include "string_ex.h"
24 
25 #include "accesstoken_kit.h"
26 #include "if_system_ability_manager.h"
27 #include "ipc_skeleton.h"
28 #include "iservice_registry.h"
29 #include "nativetoken_kit.h"
30 #include "system_ability_definition.h"
31 #include "token_setproc.h"
32 
33 #include "common_utils.h"
34 #include "constant_definition.h"
35 #include "geo_coding_mock_info.h"
36 #include "geo_convert_service.h"
37 #include "geo_convert_skeleton.h"
38 #include "location_dumper.h"
39 #include "location_log.h"
40 
41 using namespace testing::ext;
42 
43 namespace OHOS {
44 namespace Location {
45 const int32_t LOCATION_PERM_NUM = 4;
46 const std::string ARGS_HELP = "-h";
SetUp()47 void GeoConvertServiceTest::SetUp()
48 {
49     /*
50      * @tc.setup: Get system ability's pointer and get sa proxy object.
51      */
52     MockNativePermission();
53     service_ = new (std::nothrow) GeoConvertService();
54     EXPECT_NE(nullptr, service_);
55     proxy_ = new (std::nothrow) GeoConvertProxy(service_);
56     EXPECT_NE(nullptr, proxy_);
57 }
58 
TearDown()59 void GeoConvertServiceTest::TearDown()
60 {
61     /*
62      * @tc.teardown: release memory.
63      */
64     proxy_ = nullptr;
65 }
66 
MockNativePermission()67 void GeoConvertServiceTest::MockNativePermission()
68 {
69     const char *perms[] = {
70         ACCESS_LOCATION.c_str(), ACCESS_APPROXIMATELY_LOCATION.c_str(),
71         ACCESS_BACKGROUND_LOCATION.c_str(), MANAGE_SECURE_SETTINGS.c_str(),
72     };
73     NativeTokenInfoParams infoInstance = {
74         .dcapsNum = 0,
75         .permsNum = LOCATION_PERM_NUM,
76         .aclsNum = 0,
77         .dcaps = nullptr,
78         .perms = perms,
79         .acls = nullptr,
80         .processName = "GeoCodeServiceTest",
81         .aplStr = "system_basic",
82     };
83     uint64_t tokenId = GetAccessTokenId(&infoInstance);
84     SetSelfTokenID(tokenId);
85     Security::AccessToken::AccessTokenKit::ReloadNativeTokenInfo();
86 }
87 
Available()88 bool GeoConvertServiceTest::Available()
89 {
90     MessageParcel replyParcel;
91     proxy_->IsGeoConvertAvailable(replyParcel);
92     return replyParcel.ReadInt32() == ERRCODE_SUCCESS;
93 }
94 
95 HWTEST_F(GeoConvertServiceTest, GeoConvertAvailable001, TestSize.Level1)
96 {
97     GTEST_LOG_(INFO)
98         << "GeoConvertServiceTest, GeoConvertAvailable002, TestSize.Level1";
99     LBSLOGI(GEO_CONVERT, "[GeoConvertServiceTest] GeoConvertAvailable002 begin");
100     EXPECT_EQ(true, proxy_->EnableReverseGeocodingMock());
101 
102     /*
103      * @tc.steps: step1. Call system ability and check whether available.
104      * @tc.expected: step1. system ability is available.
105      */
106     Available();
107     LBSLOGI(GEO_CONVERT, "[GeoConvertServiceTest] GeoConvertAvailable002 end");
108 }
109 
110 /*
111  * @tc.name: GeoAddressByCoordinate001
112  * @tc.desc: Test get address from system ability by coordinate.
113  * @tc.type: FUNC
114  */
115 HWTEST_F(GeoConvertServiceTest, GetAddressByCoordinate001, TestSize.Level1)
116 {
117     GTEST_LOG_(INFO)
118         << "GeoConvertServiceTest, GetAddressByCoordinate001, TestSize.Level1";
119     LBSLOGI(GEO_CONVERT, "[GeoConvertServiceTest] GetAddressByCoordinate001 begin");
120     /*
121      * @tc.steps: step1.read test data.
122      */
123     MessageParcel dataParcel;
124     MessageParcel replyParcel;
125     dataParcel.WriteInterfaceToken(GeoConvertProxy::GetDescriptor());
126     dataParcel.WriteDouble(39.92879); // latitude
127     dataParcel.WriteDouble(116.3709); // longitude
128     dataParcel.WriteInt32(5); // maxItem
129     dataParcel.WriteInt32(1); // geocoder param object tag
130     dataParcel.WriteString16(Str8ToStr16("ZH")); // language
131     dataParcel.WriteString16(Str8ToStr16("cn")); // country
132     dataParcel.WriteString16(Str8ToStr16("")); // description
133     dataParcel.WriteString16(Str8ToStr16("test")); // package name
134 
135     /*
136      * @tc.steps: step2. test get address by coordinate.
137      * @tc.expected: step2. no exception head info.
138      */
139     proxy_->GetAddressByCoordinate(dataParcel, replyParcel);
140     EXPECT_EQ(ERRCODE_REVERSE_GEOCODING_FAIL, replyParcel.ReadInt32());
141     LBSLOGI(GEO_CONVERT, "[GeoConvertServiceTest] GetAddressByCoordinate001 end");
142 }
143 
144 /*
145  * @tc.name: GetAddressByLocationName001
146  * @tc.desc: Test get address from system ability by location name.
147  * @tc.type: FUNC
148  */
149 HWTEST_F(GeoConvertServiceTest, GetAddressByLocationName001, TestSize.Level1)
150 {
151     GTEST_LOG_(INFO)
152         << "GeoConvertServiceTest, GetAddressByLocationName001, TestSize.Level1";
153     LBSLOGI(GEO_CONVERT, "[GeoConvertServiceTest] GetAddressByLocationName001 begin");
154 
155     /*
156      * @tc.steps: step1.read test data.
157      */
158     MessageParcel dataParcel;
159     MessageParcel replyParcel;
160     dataParcel.WriteInterfaceToken(GeoConvertProxy::GetDescriptor());
161     dataParcel.WriteString16(Str8ToStr16("北京")); // input description of a location
162     dataParcel.WriteDouble(0.0); // minLatitude
163     dataParcel.WriteDouble(0.0); // minLongitude
164     dataParcel.WriteDouble(0.0); // maxLatitude
165     dataParcel.WriteDouble(0.0); // maxLongitude
166     dataParcel.WriteInt32(5); // maxItem
167     dataParcel.WriteInt32(1); // description
168     dataParcel.WriteString16(Str8ToStr16("ZH")); // language
169     dataParcel.WriteString16(Str8ToStr16("cn")); // country
170     dataParcel.WriteString16(Str8ToStr16("")); // description
171     dataParcel.WriteString16(u"ohos"); // package name
172 
173     /*
174      * @tc.steps: step2. test get address by location's name.
175      * @tc.expected: step2. no exception head info.
176      */
177     proxy_->GetAddressByLocationName(dataParcel, replyParcel);
178     EXPECT_EQ(ERRCODE_GEOCODING_FAIL, replyParcel.ReadInt32());
179     LBSLOGI(GEO_CONVERT, "[GeoConvertServiceTest] GetAddressByLocationName001 end");
180 }
181 
182 HWTEST_F(GeoConvertServiceTest, ReverseGeocodingMock001, TestSize.Level1)
183 {
184     GTEST_LOG_(INFO)
185         << "GeoConvertServiceTest, ReverseGeocodingMock001, TestSize.Level1";
186     LBSLOGI(GEO_CONVERT, "[GeoConvertServiceTest] ReverseGeocodingMock001 begin");
187     EXPECT_EQ(true, proxy_->EnableReverseGeocodingMock());
188     std::vector<std::shared_ptr<GeocodingMockInfo>> mockInfo;
189     EXPECT_EQ(ERRCODE_SUCCESS, proxy_->SetReverseGeocodingMockInfo(mockInfo));
190 
191     EXPECT_EQ(true, proxy_->DisableReverseGeocodingMock());
192     EXPECT_EQ(ERRCODE_SUCCESS, proxy_->SetReverseGeocodingMockInfo(mockInfo));
193     LBSLOGI(GEO_CONVERT, "[GeoConvertServiceTest] ReverseGeocodingMock001 end");
194 }
195 
196 HWTEST_F(GeoConvertServiceTest, GeoConvertServiceDump001, TestSize.Level1)
197 {
198     GTEST_LOG_(INFO)
199         << "GeoConvertServiceTest, GeoConvertServiceDump001, TestSize.Level1";
200     LBSLOGI(GEO_CONVERT, "[GeoConvertServiceTest] GeoConvertServiceDump001 begin");
201     int32_t fd = 0;
202     std::vector<std::u16string> args;
203     std::u16string arg1 = Str8ToStr16("arg1");
204     args.emplace_back(arg1);
205     std::u16string arg2 = Str8ToStr16("arg2");
206     args.emplace_back(arg2);
207     std::u16string arg3 = Str8ToStr16("arg3");
208     args.emplace_back(arg3);
209     std::u16string arg4 = Str8ToStr16("arg4");
210     args.emplace_back(arg4);
211     EXPECT_EQ(ERR_OK, service_->Dump(fd, args));
212 
213     std::vector<std::u16string> emptyArgs;
214     EXPECT_EQ(ERR_OK, service_->Dump(fd, emptyArgs));
215 
216     std::vector<std::u16string> helpArgs;
217     std::u16string helpArg1 = Str8ToStr16(ARGS_HELP);
218     helpArgs.emplace_back(helpArg1);
219     EXPECT_EQ(ERR_OK, service_->Dump(fd, helpArgs));
220     LBSLOGI(GEO_CONVERT, "[GeoConvertServiceTest] GeoConvertServiceDump001 end");
221 }
222 
223 HWTEST_F(GeoConvertServiceTest, GeoConvertProxyGetAddressByCoordinate001, TestSize.Level1)
224 {
225     GTEST_LOG_(INFO)
226         << "GeoConvertServiceTest, GeoConvertProxyGetAddressByCoordinate001, TestSize.Level1";
227     LBSLOGI(GEO_CONVERT, "[GeoConvertServiceTest] GeoConvertProxyGetAddressByCoordinate001 begin");
228     MessageParcel parcel1;
229     MessageParcel reply1;
230     EXPECT_EQ(true, proxy_->EnableReverseGeocodingMock());
231     proxy_->GetAddressByCoordinate(parcel1, reply1);
232     EXPECT_EQ(ERRCODE_SUCCESS, reply1.ReadInt32());
233 
234     MessageParcel parcel2;
235     MessageParcel reply2;
236     EXPECT_EQ(true, proxy_->DisableReverseGeocodingMock());
237     proxy_->GetAddressByCoordinate(parcel2, reply2);
238     EXPECT_EQ(ERRCODE_REVERSE_GEOCODING_FAIL, reply2.ReadInt32());
239     LBSLOGI(GEO_CONVERT, "[GeoConvertServiceTest] GeoConvertProxyGetAddressByCoordinate001 end");
240 }
241 HWTEST_F(GeoConvertServiceTest, GeoConvertOnStart001, TestSize.Level1)
242 {
243     GTEST_LOG_(INFO)
244         << "GeoConvertServiceTest, GeoConvertOnStart001, TestSize.Level1";
245     LBSLOGI(GEO_CONVERT, "[GeoConvertServiceTest] GeoConvertOnStart001 begin");
246     service_->state_  = ServiceRunningState::STATE_NOT_START;
247     service_->OnStart();
248     LBSLOGI(GEO_CONVERT, "[GeoConvertServiceTest] GeoConvertOnStart001 end");
249 }
250 
251 HWTEST_F(GeoConvertServiceTest, GeoConvertInit001, TestSize.Level1)
252 {
253     GTEST_LOG_(INFO)
254         << "GeoConvertServiceTest, GeoConvertInit001, TestSize.Level1";
255     LBSLOGI(GEO_CONVERT, "[GeoConvertServiceTest] GeoConvertInit001 begin");
256     service_->registerToService_  = true;
257     service_->Init();
258     LBSLOGI(GEO_CONVERT, "[GeoConvertServiceTest] GeoConvertOnInit001 end");
259 }
260 
261 HWTEST_F(GeoConvertServiceTest, GeoConvertOnRemoteRequest001, TestSize.Level1)
262 {
263     GTEST_LOG_(INFO)
264         << "GeoConvertServiceTest, GeoConvertOnRemoteRequest001, TestSize.Level1";
265     LBSLOGI(GEO_CONVERT, "[GeoConvertServiceTest] GeoConvertOnRemoteRequest001 begin");
266     MessageParcel requestParcel;
267     requestParcel.WriteInterfaceToken(u"location.IGeoConvert");
268     requestParcel.WriteBuffer("data", 4);
269     requestParcel.RewindRead(0);
270 
271     MessageParcel reply;
272     MessageOption option;
273     service_->OnRemoteRequest(0, requestParcel, reply, option);
274     LBSLOGI(GEO_CONVERT, "[GeoConvertServiceTest] GeoConvertOnRemoteRequest001 end");
275 }
276 
277 HWTEST_F(GeoConvertServiceTest, GeoConvertOnStop001, TestSize.Level1)
278 {
279     GTEST_LOG_(INFO)
280         << "GeoConvertServiceTest, GeoConvertOnStop001, TestSize.Level1";
281     LBSLOGI(GEO_CONVERT, "[GeoConvertServiceTest] GeoConvertOnStop001 begin");
282     service_->OnStop();
283     LBSLOGI(GEO_CONVERT, "[GeoConvertServiceTest] GeoConvertOnStop001 end");
284 }
285 
286 HWTEST_F(GeoConvertServiceTest, GeoConvertNotifyDisConnected001, TestSize.Level1)
287 {
288     GTEST_LOG_(INFO)
289         << "GeoConvertServiceTest, GeoConvertNotifyDisConnected001, TestSize.Level1";
290     LBSLOGI(GEO_CONVERT, "[GeoConvertServiceTest] GeoConvertNotifyDisConnected001 begin");
291     service_->NotifyDisConnected();
292     LBSLOGI(GEO_CONVERT, "[GeoConvertServiceTest] GeoConvertNotifyDisConnected001 end");
293 }
294 
295 HWTEST_F(GeoConvertServiceTest, GeoConvertSaDumpInfo001, TestSize.Level1)
296 {
297     GTEST_LOG_(INFO)
298         << "GeoConvertServiceTest, GeoConvertSaDumpInfo001, TestSize.Level1";
299     LBSLOGI(GEO_CONVERT, "[GeoConvertServiceTest] GeoConvertSaDumpInfo001 begin");
300     string result = "";
301     service_->SaDumpInfo(result);
302     LBSLOGI(GEO_CONVERT, "[GeoConvertServiceTest] GeoConvertSaDumpInfo001 end");
303 }
304 }  // namespace Location
305 } // namespace OHOS
306 #endif // FEATURE_GEOCODE_SUPPORT
307