1 /*
2  * Copyright (c) 2023 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 "download/downloader.h"
17 #include "download/network_client/http_curl_client.h"
18 #include "gtest/gtest.h"
19 
20 using namespace std;
21 using namespace testing::ext;
22 
23 namespace OHOS {
24 namespace Media {
25 namespace Plugins {
26 namespace HttpPlugin {
27 class DownloaderUnitTest : public testing::Test {
28 public:
29     static void SetUpTestCase(void);
30     static void TearDownTestCase(void);
31     void SetUp(void);
32     void TearDown(void);
33 protected:
34     std::shared_ptr<Downloader> downloader;
35 };
36 
SetUpTestCase(void)37 void DownloaderUnitTest::SetUpTestCase(void)
38 {
39 }
40 
TearDownTestCase(void)41 void DownloaderUnitTest::TearDownTestCase(void)
42 {
43 }
44 
SetUp(void)45 void DownloaderUnitTest::SetUp(void)
46 {
47     downloader = std::make_shared<Downloader>("test");
48 }
49 
TearDown(void)50 void DownloaderUnitTest::TearDown(void)
51 {
52     downloader.reset();
53 }
54 
55 HWTEST_F(DownloaderUnitTest, Downloader_Construct_nullptr, TestSize.Level1)
56 {
57     EXPECT_NE(downloader->client_, nullptr);
58 }
59 
60 HWTEST_F(DownloaderUnitTest, ToString_EmptyList, TestSize.Level1)
61 {
62     list<string> lists;
63     string result = ToString(lists);
64     EXPECT_EQ(result, "");
65 }
66 
67 HWTEST_F(DownloaderUnitTest, ToString_NoEmptyList, TestSize.Level1)
68 {
69     list<string> lists = {"Hello", "World"};
70     string result = ToString(lists);
71     EXPECT_EQ(result, "Hello,World");
72 }
73 
74 HWTEST_F(DownloaderUnitTest, InsertCharBefore_Test1, TestSize.Level1)
75 {
76     string input = "hello world";
77     char from = 'x';
78     char preChar = 'y';
79     char nextChar = 'z';
80     string expected = "hello world";
81     string actual = InsertCharBefore(input, from, preChar, nextChar);
82     EXPECT_EQ(expected, actual);
83 }
84 
85 HWTEST_F(DownloaderUnitTest, InsertCharBefore_Test2, TestSize.Level1)
86 {
87     string input = "hello world";
88     char from = 'l';
89     char preChar = 'y';
90     char nextChar = 'o';
91     string expected = "heyllo woryld";
92     string actual = InsertCharBefore(input, from, preChar, nextChar);
93     EXPECT_EQ(expected, actual);
94 }
95 
96 HWTEST_F(DownloaderUnitTest, InsertCharBefore_Test3, TestSize.Level1)
97 {
98     string input = "hello world";
99     char from = 'l';
100     char preChar = 'y';
101     char nextChar = 'l';
102     string expected = "hello woryld";
103     string actual = InsertCharBefore(input, from, preChar, nextChar);
104     EXPECT_EQ(expected, actual);
105 }
106 
107 HWTEST_F(DownloaderUnitTest, Trim_EmptyString, TestSize.Level1)
108 {
109     string input = "";
110     string expected = "";
111     EXPECT_EQ(Trim(input), expected);
112 }
113 
114 HWTEST_F(DownloaderUnitTest, Trim_LeadingSpaces, TestSize.Level1)
115 {
116     string input = "   Hello";
117     string expected = "Hello";
118     EXPECT_EQ(Trim(input), expected);
119 }
120 
121 HWTEST_F(DownloaderUnitTest, Trim_TrailingSpaces, TestSize.Level1)
122 {
123     string input = "Hello   ";
124     string expected = "Hello";
125     EXPECT_EQ(Trim(input), expected);
126 }
127 
128 HWTEST_F(DownloaderUnitTest, Trim_LeadingAndTrailingSpaces, TestSize.Level1)
129 {
130     string input = "  Hello   ";
131     string expected = "Hello";
132     EXPECT_EQ(Trim(input), expected);
133 }
134 
135 HWTEST_F(DownloaderUnitTest, Trim_NoSpaces, TestSize.Level1)
136 {
137     string input = "Hello";
138     string expected = "Hello";
139     EXPECT_EQ(Trim(input), expected);
140 }
141 
142 HWTEST_F(DownloaderUnitTest, IsRegexValid_Test1, TestSize.Level1)
143 {
144     string regex = "";
145     bool result = IsRegexValid(regex);
146     EXPECT_EQ(result, false);
147 }
148 
149 HWTEST_F(DownloaderUnitTest, IsRegexValid_Test2, TestSize.Level1)
150 {
151     string regex = "!@#$%^&*()";
152     bool result = IsRegexValid(regex);
153     EXPECT_EQ(result, false);
154 }
155 
156 HWTEST_F(DownloaderUnitTest, IsRegexValid_Test3, TestSize.Level1)
157 {
158     string regex = "abc123";
159     bool result = IsRegexValid(regex);
160     EXPECT_EQ(result, true);
161 }
162 
163 HWTEST_F(DownloaderUnitTest, ReplaceCharacters_01, TestSize.Level1)
164 {
165     string input = "";
166     string output = ReplaceCharacters(input);
167     EXPECT_EQ(output, "");
168 }
169 
170 HWTEST_F(DownloaderUnitTest, ReplaceCharacters_02, TestSize.Level1)
171 {
172     string input = "abc";
173     string output = ReplaceCharacters(input);
174     EXPECT_EQ(output, "abc");
175 }
176 
177 HWTEST_F(DownloaderUnitTest, ReplaceCharacters_03, TestSize.Level1)
178 {
179     string input = "a.b.c";
180     string output = ReplaceCharacters(input);
181     EXPECT_EQ(output, "a\\.b\\.c");
182 }
183 
184 HWTEST_F(DownloaderUnitTest, ReplaceCharacters_04, TestSize.Level1)
185 {
186     string input = "a\\b\\c";
187     string output = ReplaceCharacters(input);
188     EXPECT_EQ(output, "a\\b\\c");
189 }
190 
191 HWTEST_F(DownloaderUnitTest, ReplaceCharacters_05, TestSize.Level1)
192 {
193     string input = "a\\b.c";
194     string output = ReplaceCharacters(input);
195     EXPECT_EQ(output, "a\\b\\.c");
196 }
197 
198 HWTEST_F(DownloaderUnitTest, IsMatch_01, TestSize.Level1)
199 {
200     string str = "test";
201     string patternStr = "";
202     bool result = IsMatch(str, patternStr);
203     EXPECT_FALSE(result);
204 }
205 
206 HWTEST_F(DownloaderUnitTest, IsMatch_02, TestSize.Level1)
207 {
208     string str = "test";
209     string patternStr = "*";
210     bool result = IsMatch(str, patternStr);
211     EXPECT_TRUE(result);
212 }
213 
214 HWTEST_F(DownloaderUnitTest, IsMatch_03, TestSize.Level1)
215 {
216     string str = "test";
217     string patternStr = "test";
218     bool result = IsMatch(str, patternStr);
219     EXPECT_TRUE(result);
220 }
221 
222 HWTEST_F(DownloaderUnitTest, IsMatch_04, TestSize.Level1)
223 {
224     string str = "test";
225     string patternStr = "t.*";
226     bool result = IsMatch(str, patternStr);
227     EXPECT_FALSE(result);
228 }
229 
230 HWTEST_F(DownloaderUnitTest, IsMatch_05, TestSize.Level1)
231 {
232     string str = "test";
233     string patternStr = "e.*";
234     bool result = IsMatch(str, patternStr);
235     EXPECT_FALSE(result);
236 }
237 
238 HWTEST_F(DownloaderUnitTest, IsExcluded_01, TestSize.Level1)
239 {
240     string str = "test";
241     string exclusions = "";
242     string split = ",";
243     EXPECT_FALSE(IsExcluded(str, exclusions, split));
244 }
245 
246 HWTEST_F(DownloaderUnitTest, IsExcluded_02, TestSize.Level1)
247 {
248     string str = "test";
249     string exclusions = "test,example";
250     string split = ",";
251     EXPECT_TRUE(IsExcluded(str, exclusions, split));
252 }
253 
254 HWTEST_F(DownloaderUnitTest, IsExcluded_03, TestSize.Level1)
255 {
256     string str = "test";
257     string exclusions = "example,sample";
258     string split = ",";
259     EXPECT_FALSE(IsExcluded(str, exclusions, split));
260 }
261 
262 HWTEST_F(DownloaderUnitTest, IsExcluded_04, TestSize.Level1)
263 {
264     string str = "test";
265     string exclusions = "example";
266     string split = ",";
267     EXPECT_FALSE(IsExcluded(str, exclusions, split));
268 }
269 
270 }
271 }
272 }
273 }