1 /*
2 * Copyright (c) 2021-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 <gtest/gtest.h>
17 #define TEST_ACCESS private: public; protected: public
18 #include "uri_ext.h"
19 #undef TEST_ACCESS
20 using namespace OHOS;
21 using namespace OHOS::FileAccessFwk;
22 using namespace testing::ext;
23 namespace OHOS {
24 namespace {
25 const string EMPTY = "";
26 const int PORT_NONE = -1;
27 }
28 class UriTest : public testing::Test {
29 public:
30 static void SetUpTestCase(void);
31 static void TearDownTestCase(void);
32 void SetUp();
33 void TearDown();
34 std::shared_ptr<Urie> urie_ = nullptr;
35 };
36
SetUpTestCase(void)37 void UriTest::SetUpTestCase(void)
38 {}
39
TearDownTestCase(void)40 void UriTest::TearDownTestCase(void)
41 {}
42
SetUp(void)43 void UriTest::SetUp(void)
44 {
45 urie_ = std::make_shared<Urie>(" ");
46 }
47
TearDown(void)48 void UriTest::TearDown(void)
49 {}
50
51 /**
52 * @tc.number: Uri_GetSchemeSpecificPart_0100
53 * @tc.name: GetSchemeSpecificPart
54 * @tc.desc: Verify the function when the input string get string specific part.
55 */
56 HWTEST_F(UriTest, Uri_GetSchemeSpecificPart_0100, Function | MediumTest | Level0)
57 {
58 GTEST_LOG_(INFO) << "Uri_GetSchemeSpecificPart_0100 start";
59 auto result = urie_->GetSchemeSpecificPart();
60 EXPECT_EQ(result, " ");
61 GTEST_LOG_(INFO) << "Uri_GetSchemeSpecificPart_0100 end";
62 }
63
64 /**
65 * @tc.number: Uri_GetSchemeSpecificPart_0200
66 * @tc.name: GetSchemeSpecificPart
67 * @tc.desc: Verify the function when the input string get string specific part.
68 */
69 HWTEST_F(UriTest, Uri_GetSchemeSpecificPart_0200, Function | MediumTest | Level1)
70 {
71 GTEST_LOG_(INFO) << "Uri_GetSchemeSpecificPart_0200 start";
72 urie_->uriString_ = "abc";
73 auto result = urie_->GetSchemeSpecificPart();
74 EXPECT_EQ(result, "abc");
75 GTEST_LOG_(INFO) << "Uri_GetSchemeSpecificPart_0200 end";
76 }
77
78 /**
79 * @tc.number: Uri_GetAuthority_0100
80 * @tc.name: GetAuthority
81 * @tc.desc: Verify the function to get authority.
82 */
83 HWTEST_F(UriTest, Uri_GetAuthority_0100, Function | MediumTest | Level1)
84 {
85 GTEST_LOG_(INFO) << "Uri_GetAuthority_0100 start";
86 urie_->uriString_ = "abc";
87 auto result = urie_->GetAuthority();
88 EXPECT_TRUE(result == "");
89 GTEST_LOG_(INFO) << "Uri_GetAuthority_0100 end";
90 }
91
92 /**
93 * @tc.number: Uri_GetAuthority_0200
94 * @tc.name: GetAuthority
95 * @tc.desc: Verify the function to get authority.
96 */
97 HWTEST_F(UriTest, Uri_GetAuthority_0200, Function | MediumTest | Level1)
98 {
99 GTEST_LOG_(INFO) << "Uri_GetAuthority_0200 start";
100 urie_->uriString_ = "abc";
101 urie_->cachedSsi_ = 1;
102 auto result = urie_->GetAuthority();
103 EXPECT_TRUE(result == "");
104 GTEST_LOG_(INFO) << "Uri_GetAuthority_0200 end";
105 }
106
107 /**
108 * @tc.number: Uri_GetAuthority_0300
109 * @tc.name: GetAuthority
110 * @tc.desc: Verify the function to get authority.
111 */
112 HWTEST_F(UriTest, Uri_GetAuthority_0300, Function | MediumTest | Level1)
113 {
114 GTEST_LOG_(INFO) << "Uri_GetAuthority_0200 start";
115 urie_->uriString_ = "://abc";
116 auto result = urie_->GetAuthority();
117 EXPECT_EQ(result, "abc");
118 GTEST_LOG_(INFO) << "Uri_GetAuthority_0200 end";
119 }
120
121 /**
122 * @tc.number: Uri_GetHost_0100
123 * @tc.name: WithAbilityName/GetAbilityName.
124 * @tc.desc: Verify the function when the input authority get host.
125 */
126 HWTEST_F(UriTest, Uri_GetHost_0100, Function | MediumTest | Level1)
127 {
128 GTEST_LOG_(INFO) << "Uri_GetHost_0100 start";
129 urie_->uriString_ = "abc";
130 urie_->authority_ = "abcd";
131 auto result = urie_->GetHost();
132 EXPECT_EQ("abcd", result);
133 GTEST_LOG_(INFO) << "Uri_GetHost_0100 end";
134 }
135
136 /**
137 * @tc.number: Uri_GetHost_0200
138 * @tc.name: WithAbilityName/GetAbilityName.
139 * @tc.desc: Verify the function when the input authority get host.
140 */
141 HWTEST_F(UriTest, Uri_GetHost_0200, Function | MediumTest | Level1)
142 {
143 GTEST_LOG_(INFO) << "Uri_GetHost_0200 start";
144 auto result = urie_->GetAuthority();
145 EXPECT_TRUE(result == "");
146 GTEST_LOG_(INFO) << "Uri_GetHost_0200 end";
147 }
148
149 /**
150 * @tc.number: Uri_GetPort_0100
151 * @tc.name: WithAbilityName/GetAbilityName.
152 * @tc.desc: Verify the function when the input port get port.
153 */
154 HWTEST_F(UriTest, Uri_GetPort_0100, Function | MediumTest | Level1)
155 {
156 GTEST_LOG_(INFO) << "Uri_GetPort_0100 start";
157 urie_->uriString_ = "abc";
158 urie_->port_ = -1;
159 auto result = urie_->GetPort();
160 EXPECT_EQ(urie_->port_, result);
161 GTEST_LOG_(INFO) << "Uri_GetPort_0100 end";
162 }
163
164 /**
165 * @tc.number: Uri_GetPort_0200
166 * @tc.name: WithAbilityName/GetAbilityName.
167 * @tc.desc: Verify the function when the input port get port..
168 */
169 HWTEST_F(UriTest, Uri_GetPort_0200, Function | MediumTest | Level1)
170 {
171 GTEST_LOG_(INFO) << "Uri_GetPort_0200 start";
172 auto result = urie_->GetPort();
173 EXPECT_TRUE(result == -1);
174 GTEST_LOG_(INFO) << "Uri_GetPort_0200 end";
175 }
176
177 /**
178 * @tc.number: Uri_GetPort_0300
179 * @tc.name: WithAbilityName/GetAbilityName.
180 * @tc.desc: Verify the function when the input port get port.
181 */
182 HWTEST_F(UriTest, Uri_GetPort_0300, Function | MediumTest | Level1)
183 {
184 GTEST_LOG_(INFO) << "Uri_GetPort_0300 start";
185 urie_->uriString_ = "://:567";
186 auto result = urie_->GetPort();
187 EXPECT_EQ(567, result);
188 GTEST_LOG_(INFO) << "Uri_GetPort_0300 end";
189 }
190
191 /**
192 * @tc.number: Uri_GetPort_0400
193 * @tc.name: WithAbilityName/GetAbilityName.
194 * @tc.desc: Verify the function when the input port get port.
195 */
196 HWTEST_F(UriTest, Uri_GetPort_0400, Function | MediumTest | Level1)
197 {
198 GTEST_LOG_(INFO) << "Uri_GetPort_0400 start";
199 urie_->uriString_ = ":abc";
200 urie_->port_ = 5;
201 auto result = urie_->GetPort();
202 EXPECT_EQ(5, result);
203 GTEST_LOG_(INFO) << "Uri_GetPort_0400 end";
204 }
205
206 /**
207 * @tc.number: Uri_GetUserInfo_0100
208 * @tc.name: GetUserInfo
209 * @tc.desc: Verify the function when the input userInfo get userInfo.
210 */
211 HWTEST_F(UriTest, Uri_GetUserInfo_0100, Function | MediumTest | Level1)
212 {
213 GTEST_LOG_(INFO) << "Uri_GetUserInfo_0100 start";
214 urie_->uriString_ = "abc";
215 auto result = urie_->GetUserInfo();
216 EXPECT_TRUE(result == "");
217 GTEST_LOG_(INFO) << "Uri_GetUserInfo_0100 end";
218 }
219
220 /**
221 * @tc.number: Uri_GetUserInfo_0200
222 * @tc.name: GetUserInfo
223 * @tc.desc: Verify the function when the input userInfo get userInfo.
224 */
225 HWTEST_F(UriTest, Uri_GetUserInfo_0200, Function | MediumTest | Level1)
226 {
227 GTEST_LOG_(INFO) << "Uri_GetUserInfo_0200 start";
228 urie_->uriString_ = "abcde@";
229 urie_->userInfo_ = "abc";
230 auto result = urie_->GetUserInfo();
231 EXPECT_EQ(result, "abc");
232 GTEST_LOG_(INFO) << "Uri_GetUserInfo_0200 end";
233 }
234
235 /**
236 * @tc.number: Uri_GetFragment_0100
237 * @tc.name: GetFragment
238 * @tc.desc: Verify the function to get fragment.
239 */
240 HWTEST_F(UriTest, Uri_GetFragment_0100, Function | MediumTest | Level1)
241 {
242 GTEST_LOG_(INFO) << "Uri_GetFragment_0100 start";
243 urie_->uriString_ = "abc";
244 auto result = urie_->GetFragment();
245 EXPECT_TRUE(result == "");
246 GTEST_LOG_(INFO) << "Uri_GetFragment_0100 end";
247 }
248
249 /**
250 * @tc.number: Uri_IsHierarchical_0100
251 * @tc.name: IsHierarchical
252 * @tc.desc: Verify the function is hierarchical.
253 */
254 HWTEST_F(UriTest, Uri_IsHierarchical_0100, Function | MediumTest | Level1)
255 {
256 GTEST_LOG_(INFO) << "Uri_IsHierarchical_0100 start";
257 urie_->uriString_ = "abc";
258 urie_->cachedSsi_ = std::string::npos;
259 auto result = urie_->IsHierarchical();
260 EXPECT_TRUE(result);
261 GTEST_LOG_(INFO) << "Uri_IsHierarchical_0100 end";
262 }
263
264 /**
265 * @tc.number: Uri_IsHierarchical_0200
266 * @tc.name: IsHierarchical
267 * @tc.desc: Verify the function is hierarchical.
268 */
269 HWTEST_F(UriTest, Uri_IsHierarchical_0200, Function | MediumTest | Level1)
270 {
271 GTEST_LOG_(INFO) << "Uri_IsHierarchical_0200 start";
272 urie_->uriString_ = ":abc";
273 auto result = urie_->IsHierarchical();
274 EXPECT_FALSE(result);
275 GTEST_LOG_(INFO) << "Uri_IsHierarchical_0200 end";
276 }
277
278 /**
279 * @tc.number: Uri_IsAbsolute_0100
280 * @tc.name: IsAbsolute
281 * @tc.desc: Verify the function is absolute.
282 */
283 HWTEST_F(UriTest, Uri_IsAbsolute_0100, Function | MediumTest | Level1)
284 {
285 GTEST_LOG_(INFO) << "Uri_IsAbsolute_0100 start";
286 urie_->uriString_ = "ab:c";
287 auto result = urie_->IsAbsolute();
288 EXPECT_TRUE(result);
289 GTEST_LOG_(INFO) << "Uri_IsAbsolute_0100 end";
290 }
291
292 /**
293 * @tc.number: Uri_IsAbsolute_0200
294 * @tc.name: IsAbsolute
295 * @tc.desc: Verify the function is absolute.
296 */
297 HWTEST_F(UriTest, Uri_IsAbsolute_0200, Function | MediumTest | Level1)
298 {
299 GTEST_LOG_(INFO) << "Uri_IsAbsolute_0200 start";
300 auto result = urie_->IsAbsolute();
301 EXPECT_TRUE(!result);
302 GTEST_LOG_(INFO) << "Uri_IsAbsolute_0200 end";
303 }
304
305 /**
306 * @tc.number: Uri_IsRelative_0100
307 * @tc.name: IsRelative
308 * @tc.desc: Verify the function is relative.
309 */
310 HWTEST_F(UriTest, Uri_IsRelative_0100, Function | MediumTest | Level1)
311 {
312 GTEST_LOG_(INFO) << "Uri_IsRelative_0100 start";
313 urie_->uriString_ = "abc";
314 urie_->cachedSsi_ = std::string::npos;
315 auto result = urie_->IsRelative();
316 EXPECT_TRUE(result);
317 GTEST_LOG_(INFO) << "Uri_IsRelative_0100 end";
318 }
319
320 /**
321 * @tc.number: Uri_IsRelative_0200
322 * @tc.name: IsRelative
323 * @tc.desc: Verify the function is relative.
324 */
325 HWTEST_F(UriTest, Uri_IsRelative_0200, Function | MediumTest | Level1)
326 {
327 GTEST_LOG_(INFO) << "Uri_IsRelative_0200 start";
328 urie_->uriString_ = "a:bc";
329 auto result = urie_->IsRelative();
330 EXPECT_TRUE(!result);
331 GTEST_LOG_(INFO) << "Uri_IsRelative_0200 end";
332 }
333
334 /**
335 * @tc.number: Uri_Equals_0100
336 * @tc.name: Equals
337 * @tc.desc: Verify the function when the input string contains special characters.
338 */
339 HWTEST_F(UriTest, Uri_Equals_0100, Function | MediumTest | Level1)
340 {
341 GTEST_LOG_(INFO) << "Uri_Equals_0100 start";
342 Parcel parcel;
343 auto other = urie_->Unmarshalling(parcel);
344 auto result = urie_->Equals(*other);
345 EXPECT_FALSE(result);
346 GTEST_LOG_(INFO) << "Uri_Equals_0100 end";
347 }
348
349 /**
350 * @tc.number: Uri_CompareTo_0100
351 * @tc.name: Equals
352 * @tc.desc: Verify the function when the input string contains special characters.
353 */
354 HWTEST_F(UriTest, Uri_CompareTo_0100, Function | MediumTest | Level1)
355 {
356 GTEST_LOG_(INFO) << "Uri_CompareTo_0100 start";
357 Parcel parcel;
358 auto other = urie_->Unmarshalling(parcel);
359 auto result = urie_->CompareTo(*other);
360 EXPECT_EQ(result, 1);
361 GTEST_LOG_(INFO) << "Uri_CompareTo_0100 end";
362 }
363
364 /**
365 * @tc.number: Uri_GetScheme_0100
366 * @tc.name: GetScheme
367 * @tc.desc: Verify the function when the uriString_ is not empty.
368 * @tc.require: issueI6415N
369 */
370 HWTEST_F(UriTest, Uri_GetScheme_0100, Function | MediumTest | Level1)
371 {
372 GTEST_LOG_(INFO) << "Uri_GetScheme_0100 start";
373 string uriString = "this is uriString";
374 urie_ = std::make_shared<Urie>(uriString);
375 string result = urie_->GetScheme();
376 string result1 = urie_->ParseScheme();
377 EXPECT_EQ(result, result1);
378 GTEST_LOG_(INFO) << "Uri_GetScheme_0100 end";
379 }
380
381 /**
382 * @tc.number: Uri_GetSchemeSpecificPart_0300
383 * @tc.name: GetSchemeSpecificPart
384 * @tc.desc: Verify the function when the uriString_ is empty.
385 * @tc.require: issueI6415N
386 */
387 HWTEST_F(UriTest, Uri_GetSchemeSpecificPart_0300, Function | MediumTest | Level1)
388 {
389 GTEST_LOG_(INFO) << "Uri_GetSchemeSpecificPart_0300 start";
390 auto result = urie_->GetSchemeSpecificPart();
391 auto result1 = urie_->GetAuthority();
392 auto result2 = urie_->GetUserInfo();
393 auto result5 = urie_->GetHost();
394 auto result6 = urie_->ParseHost();
395 auto result7 = urie_->GetPort();
396 auto result8 = urie_->GetQuery();
397 auto result9 = urie_->GetPath();
398 auto result10 = urie_->GetFragment();
399 auto result13 = urie_->IsAbsolute();
400 EXPECT_EQ(result, " ");
401 EXPECT_EQ(result1, EMPTY);
402 EXPECT_EQ(result2, EMPTY);
403 EXPECT_EQ(result5, EMPTY);
404 EXPECT_EQ(result6, EMPTY);
405 EXPECT_EQ(result7, PORT_NONE);
406 EXPECT_EQ(result8, EMPTY);
407 EXPECT_EQ(result9, " ");
408 EXPECT_EQ(result10, EMPTY);
409 EXPECT_EQ(result13, false);
410 GTEST_LOG_(INFO) << "Uri_GetSchemeSpecificPart_0300 end";
411 }
412
413 /**
414 * @tc.number: Uri_IsHierarchical_0300
415 * @tc.name: IsHierarchical
416 * @tc.desc: Verify the function when the uriString_ is empty.
417 * @tc.require: issueI6415N
418 */
419 HWTEST_F(UriTest, Uri_IsHierarchical_0300, Function | MediumTest | Level1)
420 {
421 GTEST_LOG_(INFO) << "Uri_IsHierarchical_0300 start";
422 urie_->uriString_ = "";
423 auto result1 = urie_->IsHierarchical();
424 auto result2 = urie_->IsRelative();
425 EXPECT_EQ(result1, false);
426 EXPECT_EQ(result2, false);
427 GTEST_LOG_(INFO) << "Uri_IsHierarchical_0300 end";
428 }
429
430 /**
431 * @tc.number: Uri_ParseUserInfo_0100
432 * @tc.name: ParseUserInfo
433 * @tc.desc: Verify the function when the authority is not empty.
434 * @tc.require: issueI6415N
435 */
436 HWTEST_F(UriTest, Uri_ParseUserInfo_0100, Function | MediumTest | Level1)
437 {
438 GTEST_LOG_(INFO) << "Uri_ParseUserInfo_0100 start";
439 string uriString = "this is uriString";
440 urie_ = std::make_shared<Urie>(uriString);
441 auto result = urie_->GetAuthority();
442 auto result1 = urie_->ParseUserInfo();
443 auto result2 = urie_->ParseAuthority();
444 auto result3 = urie_->ParsePort();
445 EXPECT_EQ(result, result2);
446 EXPECT_EQ(result3, PORT_NONE);
447 GTEST_LOG_(INFO) << "Uri_ParseUserInfo_0100 end";
448 }
449
450 /**
451 * @tc.number: Uri_ParseQuery_0100
452 * @tc.name: ParseQuery
453 * @tc.desc: Verify the function when the fsi < qsi.
454 * @tc.require: issueI6415N
455 */
456 HWTEST_F(UriTest, Uri_ParseQuery_0100, Function | MediumTest | Level1)
457 {
458 GTEST_LOG_(INFO) << "Uri_ParseQuery_0100 start";
459 string uriString = "this is uriString";
460 urie_ = std::make_shared<Urie>(uriString);
461 urie_->cachedSsi_ = 1;
462 urie_->cachedFsi_ = 2;
463 auto result = urie_->ParseQuery();
464 EXPECT_EQ(result, EMPTY);
465 GTEST_LOG_(INFO) << "Uri_ParseQuery_0100 end";
466 }
467
468 /**
469 * @tc.number: Uri_ParseQuery_0200
470 * @tc.name: ParseQuery
471 * @tc.desc: Verify the function when the fsi > qsi.
472 * @tc.require: issueI6415N
473 */
474 HWTEST_F(UriTest, Uri_ParseQuery_0200, Function | MediumTest | Level1)
475 {
476 GTEST_LOG_(INFO) << "Uri_ParseQuery_0200 start";
477 string uriString = "this is uriString";
478 urie_ = std::make_shared<Urie>(uriString);
479 urie_->cachedSsi_ = 3;
480 urie_->cachedFsi_ = 2;
481 auto result = urie_->ParseQuery();
482 EXPECT_EQ(result, EMPTY);
483 GTEST_LOG_(INFO) << "Uri_ParseQuery_0200 end";
484 }
485
486 /**
487 * @tc.number: Uri_ParsePath_0100
488 * @tc.name: ParsePath
489 * @tc.desc: Verify the function when the fsi = uriString.length.
490 * @tc.require: issueI6415N
491 */
492 HWTEST_F(UriTest, Uri_ParsePath_0100, Function | MediumTest | Level1)
493 {
494 GTEST_LOG_(INFO) << "Uri_ParsePath_0100 start";
495 string uriString = "uriString";
496 urie_ = std::make_shared<Urie>(uriString);
497 urie_->cachedSsi_ = 8;
498 auto result = urie_->ParsePath();
499 EXPECT_EQ(result, EMPTY);
500 GTEST_LOG_(INFO) << "Uri_ParsePath_0100 end";
501 }
502
503 /**
504 * @tc.number: Uri_ParsePath_0200
505 * @tc.name: ParsePath
506 * @tc.desc: Verify the function.
507 * @tc.require: issueI6415N
508 */
509 HWTEST_F(UriTest, Uri_ParsePath_0200, Function | MediumTest | Level1)
510 {
511 GTEST_LOG_(INFO) << "Uri_ParsePath_0200 start";
512 string uriString = "uriString";
513 urie_ = std::make_shared<Urie>(uriString);
514 urie_->cachedSsi_ = 2;
515 auto result = urie_->ParsePath();
516 EXPECT_EQ(result, EMPTY);
517 GTEST_LOG_(INFO) << "Uri_ParsePath_0200 end";
518 }
519 }