1 /*
2 * Copyright (C) 2015-2022 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include <android-base/file.h>
18 #include <gtest/gtest.h>
19 #include <fstream>
20 #include "host/libs/location/GpsFix.h"
21 #include "host/libs/location/GpxParser.h"
22 #include "host/libs/location/StringParse.h"
23
24 namespace cuttlefish {
25
26 namespace {
ParseGpxFile(GpsFixArray * locations,char * text,std::string * error)27 bool ParseGpxFile(GpsFixArray* locations, char* text, std::string* error) {
28 bool result;
29 TemporaryDir myDir;
30 std::string path = std::string(myDir.path) + "/" + "test.gpx";
31
32 std::ofstream myfile;
33 myfile.open(path.c_str());
34 myfile << text;
35 myfile.close();
36 result = GpxParser::parseFile(path.c_str(), locations, error);
37 return result;
38 }
39
ParseGpxString(GpsFixArray * locations,char * text,std::string * error)40 bool ParseGpxString(GpsFixArray* locations, char* text, std::string* error) {
41 bool result;
42 result = GpxParser::parseString(text, strlen(text), locations, error);
43 return result;
44 }
45
46 } // namespace
47
TEST(GpxParser,ParseFileNotFound)48 TEST(GpxParser, ParseFileNotFound) {
49 GpsFixArray locations;
50 std::string error;
51 bool isOk = GpxParser::parseFile("i_dont_exist.gpx", &locations, &error);
52 EXPECT_FALSE(isOk);
53 }
54
55 char kEmptyText[] =
56 "<?xml version=\"1.0\"?>"
57 "<gpx>"
58 "</gpx>";
TEST(GpxParser,ParseEmptyFile)59 TEST(GpxParser, ParseEmptyFile) {
60 std::string error;
61 bool isOk;
62 GpsFixArray locations;
63 isOk = ParseGpxFile(&locations, kEmptyText, &error);
64 EXPECT_TRUE(isOk);
65 EXPECT_EQ(0U, locations.size());
66 }
67
TEST(GpxParser,ParseEmptyString)68 TEST(GpxParser, ParseEmptyString) {
69 std::string error;
70 bool isOk;
71 GpsFixArray locations;
72 isOk = ParseGpxString(&locations, kEmptyText, &error);
73 EXPECT_TRUE(isOk);
74 EXPECT_EQ(0U, locations.size());
75 }
76
77 char kEmptyRteTrkText[] =
78 "<?xml version=\"1.0\"?>"
79 "<gpx>"
80 "<rte>"
81 "</rte>"
82 "<trk>"
83 "<trkseg>"
84 "</trkseg>"
85 "</trk>"
86 "</gpx>";
TEST(GpxParser,ParseEmptyRteTrkFile)87 TEST(GpxParser, ParseEmptyRteTrkFile) {
88 std::string error;
89 bool isOk;
90 GpsFixArray locations;
91 isOk = ParseGpxFile(&locations, kEmptyRteTrkText, &error);
92 EXPECT_TRUE(isOk);
93 EXPECT_EQ(0U, locations.size());
94 }
95
TEST(GpxParser,ParseEmptyRteTrkString)96 TEST(GpxParser, ParseEmptyRteTrkString) {
97 std::string error;
98 bool isOk;
99 GpsFixArray locations;
100 isOk = ParseGpxString(&locations, kEmptyRteTrkText, &error);
101 EXPECT_TRUE(isOk);
102 EXPECT_EQ(0U, locations.size());
103 }
104
105 char kValidText[] =
106 "<?xml version=\"1.0\"?>"
107 "<gpx>"
108 "<wpt lon=\"0\" lat=\"0\">"
109 "<name>Wpt 1</name>"
110 "</wpt>"
111 "<wpt lon=\"0\" lat=\"0\">"
112 "<name>Wpt 2</name>"
113 "</wpt>"
114 "<rte>"
115 "<rtept lon=\"0\" lat=\"0\">"
116 "<name>Rtept 1</name>"
117 "</rtept>"
118 "<rtept lon=\"0\" lat=\"0\">"
119 "<name>Rtept 2</name>"
120 "</rtept>"
121 "</rte>"
122 "<trk>"
123 "<trkseg>"
124 "<trkpt lon=\"0\" lat=\"0\">"
125 "<name>Trkpt 1-1</name>"
126 "</trkpt>"
127 "<trkpt lon=\"0\" lat=\"0\">"
128 "<name>Trkpt 1-2</name>"
129 "</trkpt>"
130 "</trkseg>"
131 "<trkseg>"
132 "<trkpt lon=\"0\" lat=\"0\">"
133 "<name>Trkpt 2-1</name>"
134 "</trkpt>"
135 "<trkpt lon=\"0\" lat=\"0\">"
136 "<name>Trkpt 2-2</name>"
137 "</trkpt>"
138 "</trkseg>"
139 "</trk>"
140 "</gpx>";
TEST(GpxParser,ParseValidFile)141 TEST(GpxParser, ParseValidFile) {
142 std::string error;
143 bool isOk;
144
145 GpsFixArray locations;
146 isOk = ParseGpxFile(&locations, kValidText, &error);
147 EXPECT_TRUE(isOk);
148 ASSERT_EQ(8U, locations.size());
149 EXPECT_EQ("Wpt 1", locations[0].name);
150 EXPECT_EQ("Wpt 2", locations[1].name);
151 EXPECT_EQ("Rtept 1", locations[2].name);
152 EXPECT_EQ("Rtept 2", locations[3].name);
153 EXPECT_EQ("Trkpt 1-1", locations[4].name);
154 EXPECT_EQ("Trkpt 1-2", locations[5].name);
155 EXPECT_EQ("Trkpt 2-1", locations[6].name);
156 EXPECT_EQ("Trkpt 2-2", locations[7].name);
157 }
158
TEST(GpxParser,ParseValidString)159 TEST(GpxParser, ParseValidString) {
160 std::string error;
161 bool isOk;
162
163 GpsFixArray locations;
164 isOk = ParseGpxString(&locations, kValidText, &error);
165 EXPECT_TRUE(isOk);
166 ASSERT_EQ(8U, locations.size());
167 EXPECT_EQ("Wpt 1", locations[0].name);
168 EXPECT_EQ("Wpt 2", locations[1].name);
169 EXPECT_EQ("Rtept 1", locations[2].name);
170 EXPECT_EQ("Rtept 2", locations[3].name);
171 EXPECT_EQ("Trkpt 1-1", locations[4].name);
172 EXPECT_EQ("Trkpt 1-2", locations[5].name);
173 EXPECT_EQ("Trkpt 2-1", locations[6].name);
174 EXPECT_EQ("Trkpt 2-2", locations[7].name);
175 }
176
177 char kNullAttributeText[] =
178 "<?xml version=\"1.0\"?>"
179 "<gpx>"
180 "<wpt lon=\"0\" lat=\"0\">"
181 "<name/>"
182 "</wpt>"
183 "</gpx>";
TEST(GpxParser,ParseFileNullAttributeFile)184 TEST(GpxParser, ParseFileNullAttributeFile) {
185 std::string error;
186 bool isOk;
187
188 GpsFixArray locations;
189 isOk = ParseGpxFile(&locations, kNullAttributeText, &error);
190 // This test only checks if GpxParser doesn't crash on null attributes
191 // So if we're here it's already Ok - these tests aren't that relevant.
192 EXPECT_TRUE(isOk);
193 EXPECT_EQ(1U, locations.size());
194 EXPECT_STREQ("", locations[0].name.c_str());
195 EXPECT_TRUE(error.empty());
196 }
197
TEST(GpxParser,ParseFileNullAttributeString)198 TEST(GpxParser, ParseFileNullAttributeString) {
199 std::string error;
200 bool isOk;
201
202 GpsFixArray locations;
203 isOk = ParseGpxString(&locations, kNullAttributeText, &error);
204 // This test only checks if GpxParser doesn't crash on null attributes
205 // So if we're here it's already Ok - these tests aren't that relevant.
206 EXPECT_TRUE(isOk);
207 EXPECT_EQ(1U, locations.size());
208 EXPECT_STREQ("", locations[0].name.c_str());
209 EXPECT_TRUE(error.empty());
210 }
211
212 char kLocationMissingLongitudeLatitudeText[] =
213 "<?xml version=\"1.0\"?>"
214 "<gpx>"
215 "<wpt lon=\"9.81\">"
216 "<ele>6.02</ele>"
217 "<name>Name</name>"
218 "<desc>Desc</desc>"
219 "</wpt>"
220 "</gpx>";
TEST(GpxParser,ParseLocationMissingLatitudeFile)221 TEST(GpxParser, ParseLocationMissingLatitudeFile) {
222 std::string error;
223 bool isOk;
224
225 GpsFixArray locations;
226 isOk =
227 ParseGpxFile(&locations, kLocationMissingLongitudeLatitudeText, &error);
228 EXPECT_FALSE(isOk);
229 }
230
TEST(GpxParser,ParseLocationMissingLatitudeString)231 TEST(GpxParser, ParseLocationMissingLatitudeString) {
232 std::string error;
233 bool isOk;
234
235 GpsFixArray locations;
236 isOk =
237 ParseGpxString(&locations, kLocationMissingLongitudeLatitudeText, &error);
238 EXPECT_FALSE(isOk);
239 }
240
241 char kLocationMissingLongitudeText[] =
242 "<?xml version=\"1.0\"?>"
243 "<gpx>"
244 "<wpt lat=\"3.1415\">"
245 "<ele>6.02</ele>"
246 "<name>Name</name>"
247 "<desc>Desc</desc>"
248 "</wpt>"
249 "</gpx>";
TEST(GpxParser,ParseLocationMissingLongitudeFile)250 TEST(GpxParser, ParseLocationMissingLongitudeFile) {
251 std::string error;
252 bool isOk;
253
254 GpsFixArray locations;
255 isOk = ParseGpxFile(&locations, kLocationMissingLongitudeText, &error);
256 EXPECT_FALSE(isOk);
257 }
258
TEST(GpxParser,ParseLocationMissingLongitudeString)259 TEST(GpxParser, ParseLocationMissingLongitudeString) {
260 std::string error;
261 bool isOk;
262
263 GpsFixArray locations;
264 isOk = ParseGpxString(&locations, kLocationMissingLongitudeText, &error);
265 EXPECT_FALSE(isOk);
266 }
267
268 char kValidLocationText[] =
269 "<?xml version=\"1.0\"?>"
270 "<gpx>"
271 "<wpt lon=\"9.81\" lat=\"3.1415\">"
272 "<ele>6.02</ele>"
273 "<name>Name</name>"
274 "<desc>Desc</desc>"
275 "</wpt>"
276 "</gpx>";
TEST(GpxParser,ParseValidLocationFile)277 TEST(GpxParser, ParseValidLocationFile) {
278 std::string error;
279 bool isOk;
280
281 GpsFixArray locations;
282 isOk = ParseGpxFile(&locations, kValidLocationText, &error);
283 EXPECT_TRUE(isOk);
284 EXPECT_EQ(1U, locations.size());
285 const GpsFix& wpt = locations[0];
286 EXPECT_EQ("Desc", wpt.description);
287 EXPECT_FLOAT_EQ(6.02, wpt.elevation);
288 EXPECT_FLOAT_EQ(3.1415, wpt.latitude);
289 EXPECT_FLOAT_EQ(9.81, wpt.longitude);
290 EXPECT_EQ("Name", wpt.name);
291 }
292
TEST(GpxParser,ParseValidLocationString)293 TEST(GpxParser, ParseValidLocationString) {
294 std::string error;
295 bool isOk;
296
297 GpsFixArray locations;
298 isOk = ParseGpxString(&locations, kValidLocationText, &error);
299 EXPECT_TRUE(isOk);
300 EXPECT_EQ(1U, locations.size());
301 const GpsFix& wpt = locations[0];
302 EXPECT_EQ("Desc", wpt.description);
303 EXPECT_FLOAT_EQ(6.02, wpt.elevation);
304 EXPECT_FLOAT_EQ(3.1415, wpt.latitude);
305 EXPECT_FLOAT_EQ(9.81, wpt.longitude);
306 EXPECT_EQ("Name", wpt.name);
307 }
308
309 char kValidDocumentText[] =
310 "<?xml version=\"1.0\"?>"
311 "<gpx>"
312 "<wpt lon=\"0\" lat=\"0\">"
313 "<name>Wpt 1</name>"
314 "</wpt>"
315 "<wpt lon=\"0\" lat=\"0\">"
316 "<name>Wpt 2</name>"
317 "</wpt>"
318 "<rte>"
319 "<rtept lon=\"0\" lat=\"0\">"
320 "<name>Rtept 1</name>"
321 "</rtept>"
322 "<rtept lon=\"0\" lat=\"0\">"
323 "<name>Rtept 2</name>"
324 "</rtept>"
325 "</rte>"
326 "<trk>"
327 "<trkseg>"
328 "<trkpt lon=\"0\" lat=\"0\">"
329 "<name>Trkpt 1-1</name>"
330 "</trkpt>"
331 "<trkpt lon=\"0\" lat=\"0\">"
332 "<name>Trkpt 1-2</name>"
333 "</trkpt>"
334 "</trkseg>"
335 "<trkseg>"
336 "<trkpt lon=\"0\" lat=\"0\">"
337 "<name>Trkpt 2-1</name>"
338 "</trkpt>"
339 "<trkpt lon=\"0\" lat=\"0\">"
340 "<name>Trkpt 2-2</name>"
341 "</trkpt>"
342 "</trkseg>"
343 "</trk>"
344 "</gpx>";
TEST(GpxParser,ParseValidDocumentFile)345 TEST(GpxParser, ParseValidDocumentFile) {
346 std::string error;
347 bool isOk;
348
349 GpsFixArray locations;
350 isOk = ParseGpxFile(&locations, kValidDocumentText, &error);
351 EXPECT_TRUE(isOk);
352 EXPECT_EQ(8U, locations.size());
353 EXPECT_EQ("Wpt 1", locations[0].name);
354 EXPECT_EQ("Wpt 2", locations[1].name);
355 EXPECT_EQ("Rtept 1", locations[2].name);
356 EXPECT_EQ("Rtept 2", locations[3].name);
357 EXPECT_EQ("Trkpt 1-1", locations[4].name);
358 EXPECT_EQ("Trkpt 1-2", locations[5].name);
359 EXPECT_EQ("Trkpt 2-1", locations[6].name);
360 EXPECT_EQ("Trkpt 2-2", locations[7].name);
361 }
362
TEST(GpxParser,ParseValidDocumentString)363 TEST(GpxParser, ParseValidDocumentString) {
364 std::string error;
365 bool isOk;
366
367 GpsFixArray locations;
368 isOk = ParseGpxString(&locations, kValidDocumentText, &error);
369 EXPECT_TRUE(isOk);
370 EXPECT_EQ(8U, locations.size());
371 EXPECT_EQ("Wpt 1", locations[0].name);
372 EXPECT_EQ("Wpt 2", locations[1].name);
373 EXPECT_EQ("Rtept 1", locations[2].name);
374 EXPECT_EQ("Rtept 2", locations[3].name);
375 EXPECT_EQ("Trkpt 1-1", locations[4].name);
376 EXPECT_EQ("Trkpt 1-2", locations[5].name);
377 EXPECT_EQ("Trkpt 2-1", locations[6].name);
378 EXPECT_EQ("Trkpt 2-2", locations[7].name);
379 }
380
381 } // namespace cuttlefish