• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_IsAbsolute_0100
281  * @tc.name: IsAbsolute
282  * @tc.desc: Verify the function is absolute.
283  */
284 HWTEST_F(UriTest, Uri_IsAbsolute_0100, Function | MediumTest | Level1)
285 {
286     GTEST_LOG_(INFO) << "Uri_IsAbsolute_0100 start";
287     uri_->uriString_ = "ab:c";
288     auto result = uri_->IsAbsolute();
289     EXPECT_TRUE(result);
290     GTEST_LOG_(INFO) << "Uri_IsAbsolute_0100 end";
291 }
292 
293 /**
294  * @tc.number: Uri_IsAbsolute_0200
295  * @tc.name: IsAbsolute
296  * @tc.desc: Verify the function is absolute.
297  */
298 HWTEST_F(UriTest, Uri_IsAbsolute_0200, Function | MediumTest | Level1)
299 {
300     GTEST_LOG_(INFO) << "Uri_IsAbsolute_0200 start";
301     auto result = uri_->IsAbsolute();
302     EXPECT_TRUE(!result);
303     GTEST_LOG_(INFO) << "Uri_IsAbsolute_0200 end";
304 }
305 
306 /**
307  * @tc.number: Uri_IsRelative_0100
308  * @tc.name: IsRelative
309  * @tc.desc: Verify the function is relative.
310  */
311 HWTEST_F(UriTest, Uri_IsRelative_0100, Function | MediumTest | Level1)
312 {
313     GTEST_LOG_(INFO) << "Uri_IsRelative_0100 start";
314     uri_->uriString_ = "abc";
315     uri_->cachedSsi_ = std::string::npos;
316     auto result = uri_->IsRelative();
317     EXPECT_TRUE(result);
318     GTEST_LOG_(INFO) << "Uri_IsRelative_0100 end";
319 }
320 
321 /**
322  * @tc.number: Uri_IsRelative_0200
323  * @tc.name: IsRelative
324  * @tc.desc: Verify the function is relative.
325  */
326 HWTEST_F(UriTest, Uri_IsRelative_0200, Function | MediumTest | Level1)
327 {
328     GTEST_LOG_(INFO) << "Uri_IsRelative_0200 start";
329     uri_->uriString_ = "a:bc";
330     auto result = uri_->IsRelative();
331     EXPECT_TRUE(!result);
332     GTEST_LOG_(INFO) << "Uri_IsRelative_0200 end";
333 }
334 
335 /**
336  * @tc.number: Uri_Equals_0100
337  * @tc.name: Equals
338  * @tc.desc: Verify the function when the input string contains special characters.
339  */
340 HWTEST_F(UriTest, Uri_Equals_0100, Function | MediumTest | Level1)
341 {
342     GTEST_LOG_(INFO) << "Uri_Equals_0100 start";
343     Parcel parcel;
344     auto other = uri_->Unmarshalling(parcel);
345     auto result = uri_->Equals(*other);
346     EXPECT_FALSE(result);
347     GTEST_LOG_(INFO) << "Uri_Equals_0100 end";
348 }
349 
350 /**
351  * @tc.number: Uri_CompareTo_0100
352  * @tc.name: Equals
353  * @tc.desc: Verify the function when the input string contains special characters.
354  */
355 HWTEST_F(UriTest, Uri_CompareTo_0100, Function | MediumTest | Level1)
356 {
357     GTEST_LOG_(INFO) << "Uri_CompareTo_0100 start";
358     Parcel parcel;
359     auto other = uri_->Unmarshalling(parcel);
360     auto result = uri_->CompareTo(*other);
361     EXPECT_EQ(result, 1);
362     GTEST_LOG_(INFO) << "Uri_CompareTo_0100 end";
363 }
364 
365 /**
366  * @tc.number: Uri_GetScheme_0100
367  * @tc.name: GetScheme
368  * @tc.desc: Verify the function when the uriString_ is not empty.
369  * @tc.require: issueI6415N
370  */
371 HWTEST_F(UriTest, Uri_GetScheme_0100, Function | MediumTest | Level1)
372 {
373     GTEST_LOG_(INFO) << "Uri_GetScheme_0100 start";
374     string uriString = "this is uriString";
375     uri_ = std::make_shared<Uri>(uriString);
376     string result = uri_->GetScheme();
377     string result1 = uri_->ParseScheme();
378     EXPECT_EQ(result, result1);
379     GTEST_LOG_(INFO) << "Uri_GetScheme_0100 end";
380 }
381 
382 /**
383  * @tc.number: Uri_GetSchemeSpecificPart_0300
384  * @tc.name: GetSchemeSpecificPart
385  * @tc.desc: Verify the function when the uriString_ is empty.
386  * @tc.require: issueI6415N
387  */
388 HWTEST_F(UriTest, Uri_GetSchemeSpecificPart_0300, Function | MediumTest | Level1)
389 {
390     GTEST_LOG_(INFO) << "Uri_GetSchemeSpecificPart_0300 start";
391     auto result = uri_->GetSchemeSpecificPart();
392     auto result1 = uri_->GetAuthority();
393     auto result2 = uri_->GetUserInfo();
394     auto result5 = uri_->GetHost();
395     auto result6 = uri_->ParseHost();
396     auto result7 = uri_->GetPort();
397     auto result8 = uri_->GetQuery();
398     auto result9 = uri_->GetPath();
399     auto result10 = uri_->GetFragment();
400     auto result13 = uri_->IsAbsolute();
401     EXPECT_EQ(result, " ");
402     EXPECT_EQ(result1, EMPTY);
403     EXPECT_EQ(result2, EMPTY);
404     EXPECT_EQ(result5, EMPTY);
405     EXPECT_EQ(result6, EMPTY);
406     EXPECT_EQ(result7, PORT_NONE);
407     EXPECT_EQ(result8, EMPTY);
408     EXPECT_EQ(result9, " ");
409     EXPECT_EQ(result10, EMPTY);
410     EXPECT_EQ(result13, false);
411     GTEST_LOG_(INFO) << "Uri_GetSchemeSpecificPart_0300 end";
412 }
413 
414 /**
415  * @tc.number: Uri_IsHierarchical_0300
416  * @tc.name: IsHierarchical
417  * @tc.desc: Verify the function when the uriString_ is empty.
418  * @tc.require: issueI6415N
419  */
420 HWTEST_F(UriTest, Uri_IsHierarchical_0300, Function | MediumTest | Level1)
421 {
422     GTEST_LOG_(INFO) << "Uri_IsHierarchical_0300 start";
423     uri_->uriString_ = "";
424     auto result1 = uri_->IsHierarchical();
425     auto result2 = uri_->IsRelative();
426     EXPECT_EQ(result1, false);
427     EXPECT_EQ(result2, false);
428     GTEST_LOG_(INFO) << "Uri_IsHierarchical_0300 end";
429 }
430 
431 /**
432  * @tc.number: Uri_ParseUserInfo_0100
433  * @tc.name: ParseUserInfo
434  * @tc.desc: Verify the function when the authority is not empty.
435  * @tc.require: issueI6415N
436  */
437 HWTEST_F(UriTest, Uri_ParseUserInfo_0100, Function | MediumTest | Level1)
438 {
439     GTEST_LOG_(INFO) << "Uri_ParseUserInfo_0100 start";
440     string uriString = "this is uriString";
441     uri_ = std::make_shared<Uri>(uriString);
442     auto result = uri_->GetAuthority();
443     auto result1 = uri_->ParseUserInfo();
444     auto result2 = uri_->ParseAuthority();
445     auto result3 = uri_->ParsePort();
446     EXPECT_EQ(result, result2);
447     EXPECT_EQ(result3, PORT_NONE);
448     GTEST_LOG_(INFO) << "Uri_ParseUserInfo_0100 end";
449 }
450 
451 /**
452  * @tc.number: Uri_ParseQuery_0100
453  * @tc.name: ParseQuery
454  * @tc.desc: Verify the function when the fsi < qsi.
455  * @tc.require: issueI6415N
456  */
457 HWTEST_F(UriTest, Uri_ParseQuery_0100, Function | MediumTest | Level1)
458 {
459     GTEST_LOG_(INFO) << "Uri_ParseQuery_0100 start";
460     string uriString = "this is uriString";
461     uri_ = std::make_shared<Uri>(uriString);
462     uri_->cachedSsi_ = 1;
463     uri_->cachedFsi_ = 2;
464     auto result = uri_->ParseQuery();
465     EXPECT_EQ(result, EMPTY);
466     GTEST_LOG_(INFO) << "Uri_ParseQuery_0100 end";
467 }
468 
469 /**
470  * @tc.number: Uri_ParseQuery_0200
471  * @tc.name: ParseQuery
472  * @tc.desc: Verify the function when the fsi > qsi.
473  * @tc.require: issueI6415N
474  */
475 HWTEST_F(UriTest, Uri_ParseQuery_0200, Function | MediumTest | Level1)
476 {
477     GTEST_LOG_(INFO) << "Uri_ParseQuery_0200 start";
478     string uriString = "this is uriString";
479     uri_ = std::make_shared<Uri>(uriString);
480     uri_->cachedSsi_ = 3;
481     uri_->cachedFsi_ = 2;
482     auto result = uri_->ParseQuery();
483     EXPECT_EQ(result, EMPTY);
484     GTEST_LOG_(INFO) << "Uri_ParseQuery_0200 end";
485 }
486 
487 /**
488  * @tc.number: Uri_ParsePath_0100
489  * @tc.name: ParsePath
490  * @tc.desc: Verify the function when the fsi = uriString.length.
491  * @tc.require: issueI6415N
492  */
493 HWTEST_F(UriTest, Uri_ParsePath_0100, Function | MediumTest | Level1)
494 {
495     GTEST_LOG_(INFO) << "Uri_ParsePath_0100 start";
496     string uriString = "uriString";
497     uri_ = std::make_shared<Uri>(uriString);
498     uri_->cachedSsi_ = 8;
499     auto result = uri_->ParsePath();
500     EXPECT_EQ(result, EMPTY);
501     GTEST_LOG_(INFO) << "Uri_ParsePath_0100 end";
502 }
503 
504 /**
505  * @tc.number: Uri_ParsePath_0200
506  * @tc.name: ParsePath
507  * @tc.desc: Verify the function.
508  * @tc.require: issueI6415N
509  */
510 HWTEST_F(UriTest, Uri_ParsePath_0200, Function | MediumTest | Level1)
511 {
512     GTEST_LOG_(INFO) << "Uri_ParsePath_0200 start";
513     string uriString = "uriString";
514     uri_ = std::make_shared<Uri>(uriString);
515     uri_->cachedSsi_ = 2;
516     auto result = uri_->ParsePath();
517     EXPECT_EQ(result, EMPTY);
518     GTEST_LOG_(INFO) << "Uri_ParsePath_0200 end";
519 }
520 }
521