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