• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <cstring>
17 
18 #include "gtest/gtest.h"
19 #include "http_request_options.h"
20 #include "netstack_log.h"
21 #include "constant.h"
22 #include "secure_char.h"
23 #include "curl/curl.h"
24 
25 using namespace OHOS::NetStack;
26 using namespace OHOS::NetStack::Http;
27 
28 class HttpRequestOptionsTest : public testing::Test {
29 public:
SetUpTestCase()30     static void SetUpTestCase() {}
31 
TearDownTestCase()32     static void TearDownTestCase() {}
33 
SetUp()34     virtual void SetUp() {}
35 
TearDown()36     virtual void TearDown() {}
37 };
38 
39 namespace {
40 using namespace std;
41 using namespace testing::ext;
42 constexpr char OTHER_CA_PATH[] = "/etc/ssl/certs/other.pem";
43 
44 HWTEST_F(HttpRequestOptionsTest, CaPathTest001, TestSize.Level1)
45 {
46     HttpRequestOptions requestOptions;
47 
48     string path = requestOptions.GetCaPath();
49     EXPECT_EQ(path, HttpConstant::HTTP_DEFAULT_CA_PATH);
50 }
51 
52 HWTEST_F(HttpRequestOptionsTest, CaPathTest002, TestSize.Level1)
53 {
54     HttpRequestOptions requestOptions;
55 
56     requestOptions.SetCaPath("");
57     string path = requestOptions.GetCaPath();
58     EXPECT_EQ(path, HttpConstant::HTTP_DEFAULT_CA_PATH);
59 }
60 
61 HWTEST_F(HttpRequestOptionsTest, CaPathTest003, TestSize.Level1)
62 {
63     HttpRequestOptions requestOptions;
64 
65     requestOptions.SetCaPath(OTHER_CA_PATH);
66     string path = requestOptions.GetCaPath();
67     EXPECT_EQ(path, OTHER_CA_PATH);
68 }
69 
70 HWTEST_F(HttpRequestOptionsTest, SetDnsServersTest, TestSize.Level1)
71 {
72     HttpRequestOptions requestOptions;
73 
74     std::vector<std::string> dnsServers = { "8.8.8.8", "8.8.4.4" };
75     requestOptions.SetDnsServers(dnsServers);
76 
77     const std::vector<std::string>& retrievedDnsServers = requestOptions.GetDnsServers();
78     EXPECT_EQ(retrievedDnsServers, dnsServers);
79 }
80 
81 HWTEST_F(HttpRequestOptionsTest, SetDohUrlTest, TestSize.Level1)
82 {
83     HttpRequestOptions requestOptions;
84 
85     std::string dohUrl = "https://example.com/dns-query";
86     requestOptions.SetDohUrl(dohUrl);
87 
88     const std::string& retrievedDohUrl = requestOptions.GetDohUrl();
89     EXPECT_EQ(retrievedDohUrl, dohUrl);
90 }
91 
92 HWTEST_F(HttpRequestOptionsTest, SetRangeNumberTest001, TestSize.Level1)
93 {
94     HttpRequestOptions requestOptions;
95 
96     uint32_t resumeFromNumber = 10;
97     uint32_t resumeToNumber = 20;
98     requestOptions.SetRangeNumber(resumeFromNumber, resumeToNumber);
99 
100     const std::string& rangeString = requestOptions.GetRangeString();
101     std::string expectedRangeString = "10-20";
102     EXPECT_EQ(rangeString, expectedRangeString);
103 }
104 
105 HWTEST_F(HttpRequestOptionsTest, SetRangeNumberTest002, TestSize.Level1)
106 {
107     HttpRequestOptions requestOptions;
108 
109     uint32_t resumeFromNumber = 0;
110     uint32_t resumeToNumber = 20;
111     requestOptions.SetRangeNumber(resumeFromNumber, resumeToNumber);
112 
113     const std::string& rangeString = requestOptions.GetRangeString();
114     std::string expectedRangeString = "-20";
115     EXPECT_EQ(rangeString, expectedRangeString);
116 }
117 
118 HWTEST_F(HttpRequestOptionsTest, SetRangeNumberTest003, TestSize.Level1)
119 {
120     HttpRequestOptions requestOptions;
121 
122     uint32_t resumeFromNumber = 10;
123     uint32_t resumeToNumber = 0;
124     requestOptions.SetRangeNumber(resumeFromNumber, resumeToNumber);
125 
126     const std::string& rangeString = requestOptions.GetRangeString();
127     std::string expectedRangeString = "10-";
128     EXPECT_EQ(rangeString, expectedRangeString);
129 }
130 
131 HWTEST_F(HttpRequestOptionsTest, SetRangeNumberTest004, TestSize.Level1)
132 {
133     HttpRequestOptions requestOptions;
134 
135     uint32_t resumeFromNumber = 0;
136     uint32_t resumeToNumber = 0;
137     requestOptions.SetRangeNumber(resumeFromNumber, resumeToNumber);
138 
139     const std::string& rangeString = requestOptions.GetRangeString();
140     std::string expectedRangeString = "";
141     EXPECT_EQ(rangeString, expectedRangeString);
142 }
143 
144 HWTEST_F(HttpRequestOptionsTest, SetClientCertTest, TestSize.Level1)
145 {
146     HttpRequestOptions requestOptions;
147 
148     std::string cert = "path_to_cert.pem";
149     std::string key = "path_to_key.pem";
150     std::string certType = "PEM";
151     Secure::SecureChar keyPasswd("password");
152 
153     requestOptions.SetClientCert(cert, certType, key, keyPasswd);
154 
155     std::string retrievedCert;
156     std::string retrievedKey;
157     std::string retrievedCertType;
158     Secure::SecureChar retrievedKeyPasswd;
159 
160     requestOptions.GetClientCert(retrievedCert, retrievedCertType, retrievedKey, retrievedKeyPasswd);
161 
162     EXPECT_EQ(retrievedCert, cert);
163     EXPECT_EQ(retrievedKey, key);
164     EXPECT_EQ(retrievedCertType, certType);
165     EXPECT_EQ(strcmp(retrievedKeyPasswd.Data(), keyPasswd.Data()), 0);
166 }
167 
168 HWTEST_F(HttpRequestOptionsTest, AddMultiFormDataTest, TestSize.Level1)
169 {
170     HttpRequestOptions requestOptions;
171     for (int i = 0; i < 10; i++) {
172         MultiFormData multiFormData;
173         multiFormData.name = "name" + std::to_string(i);
174         multiFormData.data = "data" + std::to_string(i);
175         multiFormData.contentType = "contentType" + std::to_string(i);
176         multiFormData.remoteFileName = "remoteFileName" + std::to_string(i);
177         multiFormData.filePath = "filePath" + std::to_string(i);
178         requestOptions.AddMultiFormData(multiFormData);
179     }
180     std::vector<MultiFormData> dataList = requestOptions.GetMultiPartDataList();
181     EXPECT_EQ(dataList.size(), 10);
182     for (int i = 0; i < 10; i++) {
183         MultiFormData data = dataList[i];
184         EXPECT_EQ(data.name, "name" + std::to_string(i));
185         EXPECT_EQ(data.data, "data" + std::to_string(i));
186         EXPECT_EQ(data.contentType, "contentType" + std::to_string(i));
187         EXPECT_EQ(data.filePath, "filePath" + std::to_string(i));
188         EXPECT_EQ(data.remoteFileName, "remoteFileName" + std::to_string(i));
189     }
190 }
191 
192 HWTEST_F(HttpRequestOptionsTest, SetUrlTest, TestSize.Level1)
193 {
194     HttpRequestOptions requestOptions;
195 
196     std::string testValue = "Example";
197     requestOptions.SetUrl(testValue);
198     std::string url = requestOptions.GetUrl();
199     EXPECT_EQ(url, testValue);
200 }
201 
202 HWTEST_F(HttpRequestOptionsTest, SetPrioritylTest, TestSize.Level1)
203 {
204     HttpRequestOptions requestOptions;
205 
206     uint32_t testValue = 2;
207     requestOptions.SetPriority(testValue);
208     uint32_t resultValue = requestOptions.GetPriority();
209     EXPECT_EQ(resultValue, testValue);
210 }
211 
212 HWTEST_F(HttpRequestOptionsTest, SetHttpDataTypeTest, TestSize.Level1)
213 {
214     HttpRequestOptions requestOptions;
215 
216     HttpDataType testValue = HttpDataType::ARRAY_BUFFER;
217     requestOptions.SetHttpDataType(testValue);
218     HttpDataType resultValue = requestOptions.GetHttpDataType();
219     EXPECT_EQ(resultValue, testValue);
220 }
221 
222 HWTEST_F(HttpRequestOptionsTest, SetUsingProtocolTest001, TestSize.Level1)
223 {
224     HttpRequestOptions requestOptions;
225 
226     HttpProtocol testValue = HttpProtocol::HTTP1_1;
227     requestOptions.SetUsingProtocol(testValue);
228     uint32_t resultValue = requestOptions.GetHttpVersion();
229     EXPECT_EQ(resultValue, CURL_HTTP_VERSION_1_1);
230 }
231 
232 HWTEST_F(HttpRequestOptionsTest, SetUsingProtocolTest002, TestSize.Level1)
233 {
234     HttpRequestOptions requestOptions;
235 
236     HttpProtocol testValue = HttpProtocol::HTTP2;
237     requestOptions.SetUsingProtocol(testValue);
238     uint32_t resultValue = requestOptions.GetHttpVersion();
239     EXPECT_EQ(resultValue, CURL_HTTP_VERSION_2_0);
240 }
241 
242 HWTEST_F(HttpRequestOptionsTest, SetUsingProtocolTest003, TestSize.Level1)
243 {
244     HttpRequestOptions requestOptions;
245 
246     HttpProtocol testValue = HttpProtocol::HTTP3;
247     requestOptions.SetUsingProtocol(testValue);
248     uint32_t resultValue = requestOptions.GetHttpVersion();
249     EXPECT_EQ(resultValue, CURL_HTTP_VERSION_3);
250 }
251 
252 HWTEST_F(HttpRequestOptionsTest, SetUsingProtocolTest004, TestSize.Level1)
253 {
254     HttpRequestOptions requestOptions;
255 
256     HttpProtocol testValue = HttpProtocol::HTTP_NONE;
257     requestOptions.SetUsingProtocol(testValue);
258     uint32_t resultValue = requestOptions.GetHttpVersion();
259     EXPECT_EQ(resultValue, CURL_HTTP_VERSION_NONE);
260 }
261 
262 HWTEST_F(HttpRequestOptionsTest, SetMaxLimitTest, TestSize.Level1)
263 {
264     HttpRequestOptions requestOptions;
265 
266     uint32_t testValue = 50;
267     requestOptions.SetMaxLimit(testValue);
268     uint32_t resultValue = requestOptions.GetMaxLimit();
269     EXPECT_EQ(resultValue, testValue);
270 }
271 
272 HWTEST_F(HttpRequestOptionsTest, SetConnectTimeoutTest, TestSize.Level1)
273 {
274     HttpRequestOptions requestOptions;
275 
276     uint32_t testValue = 5000;
277     requestOptions.SetConnectTimeout(testValue);
278     uint32_t resultValue = requestOptions.GetConnectTimeout();
279     EXPECT_EQ(resultValue, testValue);
280 }
281 
282 HWTEST_F(HttpRequestOptionsTest, SetReadTimeoutTest, TestSize.Level1)
283 {
284     HttpRequestOptions requestOptions;
285 
286     uint32_t testValue = 5000;
287     requestOptions.SetReadTimeout(testValue);
288     uint32_t resultValue = requestOptions.GetReadTimeout();
289     EXPECT_EQ(resultValue, testValue);
290 }
291 
292 HWTEST_F(HttpRequestOptionsTest, SetHeaderTest, TestSize.Level1)
293 {
294     HttpRequestOptions requestOptions;
295 
296     int testSize = 10;
297     for (int i = 0; i < testSize; i++) {
298         requestOptions.SetHeader("key" + std::to_string(i), "value" + std::to_string(i));
299     }
300     const std::map<std::string, std::string> header = requestOptions.GetHeader();
301     for (int i = 0; i < testSize; i++) {
302         const std::string& value = header.at("key" + std::to_string(i));
303         EXPECT_EQ(value, "value" + std::to_string(i));
304     }
305 }
306 
307 HWTEST_F(HttpRequestOptionsTest, SetBodyTest, TestSize.Level1)
308 {
309     HttpRequestOptions requestOptions;
310 
311     std::string testValue = "TestBody";
312     requestOptions.SetBody(testValue.data(), testValue.size());
313     std::string resultValue = requestOptions.GetBody();
314     EXPECT_EQ(resultValue, testValue);
315 }
316 } // namespace