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