1 /*
2 * Copyright (c) 2025 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 "curl/curl.h"
17 #include "net_pac_local_proxy_server.h"
18 #include "securec.h"
19 #include "gtest/gtest.h"
20 #include <arpa/inet.h>
21 #include <cerrno>
22 #include <cstdio>
23 #include <cstring>
24 #include <iostream>
25 #include <netdb.h>
26 #include <sys/resource.h>
27 #include <sys/socket.h>
28 #include <unistd.h>
29
30 using namespace OHOS::NetManagerStandard;
31
32 #define HEADER_LARGE 10240
33
34 struct RequestOptions {
35 bool largeHeader = false;
36 bool ccr = false;
37 int32_t timeout = 30;
38 };
39
WriteCallback(void * contents,size_t size,size_t nmemb,std::string * userp)40 static size_t WriteCallback(void *contents, size_t size, size_t nmemb, std::string *userp)
41 {
42 size_t totalSize = size * nmemb;
43 userp->append(static_cast<char *>(contents), totalSize);
44 return totalSize;
45 }
46
SetupHeaders(const std::string & ip,uint16_t port,bool largeHeader)47 struct curl_slist *SetupHeaders(const std::string &ip, uint16_t port, bool largeHeader)
48 {
49 struct curl_slist *headers = nullptr;
50 std::string proxyStr = "GlobalProxyIp: " + ip;
51 std::string proxyPortStr = "GlobalProxyPort: " + std::to_string(port);
52 headers = curl_slist_append(headers, proxyStr.c_str());
53 headers = curl_slist_append(headers, proxyPortStr.c_str());
54 headers = curl_slist_append(headers, "X-Custom-Header: CustomValue");
55 if (largeHeader) {
56 for (int32_t i = 0; i < HEADER_LARGE; i++) {
57 std::string testHeader = "testHeader";
58 testHeader.append(std::to_string(i)).append(": ").append(std::to_string(i));
59 headers = curl_slist_append(headers, testHeader.c_str());
60 }
61 }
62 return headers;
63 }
64
SetupProxyAndUrl(CURL * curl,const std::string & url,const std::string & ip,uint16_t port,bool ccr)65 static void SetupProxyAndUrl(CURL *curl, const std::string &url, const std::string &ip, uint16_t port, bool ccr)
66 {
67 curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
68
69 if (ccr) {
70 curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "CONNECT");
71 }
72 if (!ip.empty()) {
73 std::string proxy = "http://" + ip + ":" + std::to_string(port);
74 printf(
75 "\033[32m"
76 "curl %s use proxy %s \n"
77 "\033[0m",
78 url.c_str(), proxy.c_str());
79 curl_easy_setopt(curl, CURLOPT_PROXY, proxy.c_str());
80 }
81 }
82
SetupCommonOptions(CURL * curl,std::string & readBuffer,int32_t timeout)83 static void SetupCommonOptions(CURL *curl, std::string &readBuffer, int32_t timeout)
84 {
85 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
86 curl_easy_setopt(curl, CURLOPT_WRITEDATA, &readBuffer);
87 curl_easy_setopt(curl, CURLOPT_TIMEOUT, timeout);
88 curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
89 }
90
PerformRequest(CURL * curl,std::string & readBuffer)91 static CURLcode PerformRequest(CURL *curl, std::string &readBuffer)
92 {
93 CURLcode res = curl_easy_perform(curl);
94 if (res != CURLE_OK) {
95 std::cerr << "curl_easy_perform() failed: " << curl_easy_strerror(res) << std::endl;
96 } else {
97 long httpCode = 0;
98 curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &httpCode);
99 }
100 return res;
101 }
102
Request(std::string url,std::string ip,uint16_t port,RequestOptions options=RequestOptions ())103 static std::string Request(std::string url, std::string ip, uint16_t port, RequestOptions options = RequestOptions())
104 {
105 CURL *curl = nullptr;
106 CURLcode res;
107 std::string readBuffer;
108 curl_global_init(CURL_GLOBAL_DEFAULT);
109 curl = curl_easy_init();
110 if (curl) {
111 struct curl_slist *headers = SetupHeaders(ip, port, options.largeHeader);
112 curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
113 SetupProxyAndUrl(curl, url, ip, port, options.ccr);
114 SetupCommonOptions(curl, readBuffer, options.timeout);
115 res = PerformRequest(curl, readBuffer);
116 curl_slist_free_all(headers);
117 curl_easy_cleanup(curl);
118 }
119 curl_global_cleanup();
120 return readBuffer;
121 }
122
123 #if 1
TEST(ProxyServerTest,CreateProxyTest)124 TEST(ProxyServerTest, CreateProxyTest)
125 {
126 int32_t portStart = 1024;
127 int32_t portEnd = 65535;
128 int32_t port = ProxyServer::FindAvailablePort(portStart, portEnd);
129 auto proxy = std::make_shared<ProxyServer>(port, 0);
130 proxy->Start();
131 int32_t port1 = ProxyServer::FindAvailablePort(portStart, portEnd);
132 auto proxy1 = std::make_shared<ProxyServer>(port1, 0);
133 proxy1->Start();
134 EXPECT_EQ(proxy->IsRunning(), true);
135 }
136
TEST(ProxyServerTest,CreateProxyTest1)137 TEST(ProxyServerTest, CreateProxyTest1)
138 {
139 int32_t port = 8888;
140 auto proxy = std::make_shared<ProxyServer>(port, 0);
141 proxy->Start();
142 auto proxy1 = std::make_shared<ProxyServer>(port, 0);
143 EXPECT_EQ(proxy1->Start(), false);
144 }
145
TEST(ProxyServerTest,CreateProxyTest3)146 TEST(ProxyServerTest, CreateProxyTest3)
147 {
148 int32_t port = 8888;
149 auto proxy = std::make_shared<ProxyServer>(port, 0);
150 proxy->Start();
151 proxy->SetFindPacProxyFunction([](std::string, std::string) { return "DIRECT"; });
152 std::string res = Request("http://sssssssssssss.com", "127.0.0.1", 8888);
153 EXPECT_EQ(res.empty(), true);
154
155 res = Request("http://icanhazip.com/", "127.0.0.1", 8888);
156 EXPECT_NE(res.empty(), false);
157 }
158
TEST(ProxyServerTest,CreateProxyTest4)159 TEST(ProxyServerTest, CreateProxyTest4)
160 {
161 int32_t port = 8888;
162 auto proxy = std::make_shared<ProxyServer>(port, 0);
163 proxy->Start();
164 proxy->SetFindPacProxyFunction([](std::string, std::string) { return "SOCKS4 127.0.0.1:1234"; });
165 std::string res = Request("https://www.example.com/", "127.0.0.1", 8888);
166 EXPECT_EQ(res.empty(), true);
167 proxy->SetFindPacProxyFunction([](std::string, std::string) { return "DIRECT"; });
168 res = Request("https://www.example.com/", "127.0.0.1", 8888);
169 EXPECT_NE(res.empty(), false);
170 res = Request("https://www.example.com/", "127.0.0.1", 8888, RequestOptions{false});
171 EXPECT_EQ(res.empty(), false);
172 res = Request("https://www.example.com/", "127.0.0.1", 8888, RequestOptions{true});
173 EXPECT_EQ(res.empty(), true);
174 RequestOptions opts;
175 opts.ccr = true;
176 res = Request("https://www.example.com", "127.0.0.1", 8888, opts);
177 EXPECT_EQ(res.empty(), false);
178 }
179
TEST(ProxyServerTest,CreateProxyTest10)180 TEST(ProxyServerTest, CreateProxyTest10)
181 {
182 auto proxy = std::make_shared<ProxyServer>(8000, 1);
183 EXPECT_EQ(proxy->Start(), true);
184 proxy->SetFindPacProxyFunction([](std::string, std::string) { return "DIRECT"; });
185 std::vector<std::thread> threads;
186 #define THREAD_COUNT 10
187 for (int32_t i = 0; i < THREAD_COUNT; ++i) {
188 RequestOptions opts;
189 opts.timeout = 2;
190 threads.emplace_back([]() {
191 Request("https://www.example.com/", "127.0.0.1", 8000, RequestOptions{false, false, 2});
192 });
193 }
194 proxy->Stop();
195 for (auto &thread : threads) {
196 thread.join();
197 }
198 }
199
TEST(ProxyServerTest,CreateProxyTest6)200 TEST(ProxyServerTest, CreateProxyTest6)
201 {
202 auto proxy8000 = std::make_shared<ProxyServer>(8000, -1);
203 EXPECT_EQ(proxy8000->Start(), true);
204 EXPECT_EQ(proxy8000->Start(), false);
205 }
206
TEST(ProxyServerTest,CreateProxyTest9)207 TEST(ProxyServerTest, CreateProxyTest9)
208 {
209 auto proxy0 = std::make_shared<ProxyServer>(8000, 0);
210 auto proxy1 = std::make_shared<ProxyServer>(8000, 0);
211 EXPECT_EQ(proxy1->Start(), true);
212 EXPECT_EQ(proxy0->Start(), false);
213 }
214
TEST(ProxyServerTest,CreateProxyTest8)215 TEST(ProxyServerTest, CreateProxyTest8)
216 {
217 std::vector<int32_t> fds;
218 struct rlimit limit;
219 long maxFd = sysconf(_SC_OPEN_MAX);
220 for (int32_t i = 0; i < maxFd + 3; i++) {
221 int32_t fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
222 if (fd == -1) {
223 EXPECT_EQ(ProxyServer::FindAvailablePort(9000, 9000), -1);
224 auto proxy8000 = std::make_shared<ProxyServer>(8000, -1);
225 EXPECT_EQ(proxy8000->Start(), false);
226 break;
227 } else {
228 fds.push_back(fd);
229 }
230 }
231 for (auto fd : fds) {
232 close(fd);
233 }
234 }
235
TEST(ProxyServerTest,CreateProxyTest7)236 TEST(ProxyServerTest, CreateProxyTest7)
237 {
238 auto proxy8000 = std::make_shared<ProxyServer>(8000, -1);
239 EXPECT_EQ(proxy8000->Start(), true);
240 int32_t port = ProxyServer::FindAvailablePort(8000, 8000);
241 EXPECT_EQ(port, -1);
242 }
243
TEST(ProxyServerTest,CreateProxyTest5)244 TEST(ProxyServerTest, CreateProxyTest5)
245 {
246 auto proxy8000 = std::make_shared<ProxyServer>(8000, 0);
247 EXPECT_EQ(proxy8000->Start(), true);
248 auto proxy8001 = std::make_shared<ProxyServer>(8001, 0);
249 proxy8001->SetFindPacProxyFunction([](auto p1, auto p2) { return "DIRECT"; });
250 EXPECT_EQ(proxy8001->Start(), true);
251 auto proxy8002 = std::make_shared<ProxyServer>(8002, 0);
252 proxy8002->SetFindPacProxyFunction([](auto p1, auto p2) { return "DIRECT"; });
253 EXPECT_EQ(proxy8002->Start(), true);
254 auto proxy8003 = std::make_shared<ProxyServer>(8003, 0);
255 proxy8003->SetFindPacProxyFunction([](auto p1, auto p2) { return "DIRECT"; });
256 EXPECT_EQ(proxy8003->Start(), true);
257
258 proxy8000->SetFindPacProxyFunction(
259 [](auto p1, auto p2) { return "PROXY 127.0.0.1:8001;PROXY 127.0.0.1:8002; PROXY 127.0.0.1:8003"; });
260
261 std::string res = Request("http://127.0.0.1/", "127.0.0.1", 8000);
262 EXPECT_EQ(res.empty(), true);
263 res = Request("https://127.0.0.1/", "127.0.0.1", 8000);
264 EXPECT_EQ(res.empty(), true);
265 res = Request("http://icanhazip.com/", "127.0.0.1", 8000);
266 EXPECT_NE(res.empty(), false);
267 res = Request("https://www.example.com/", "127.0.0.1", 8000, RequestOptions{false, false, 30});
268 EXPECT_EQ(res.empty(), false);
269 proxy8001->Stop();
270 res = Request("https://www.example.com/", "127.0.0.1", 8000);
271 EXPECT_EQ(res.empty(), false);
272 proxy8002->Stop();
273 res = Request("http://icanhazip.com/", "127.0.0.1", 8000);
274 EXPECT_EQ(res.empty(), false);
275 res = Request("https://www.example.com/", "127.0.0.1", 8000);
276 EXPECT_EQ(res.empty(), false);
277 proxy8003->Stop();
278 res = Request("https://www.example.com/", "127.0.0.1", 8000);
279 EXPECT_EQ(res.empty(), true);
280 res = Request("http://icanhazip.com/", "127.0.0.1", 8000);
281 EXPECT_EQ(res.empty(), true);
282 }
283
TEST(ProxyServerTest,CreateProxyTest11)284 TEST(ProxyServerTest, CreateProxyTest11)
285 {
286 auto proxy = std::make_shared<ProxyServer>(8000, 1);
287 EXPECT_EQ(proxy->Start(), true);
288 proxy->SetFindPacProxyFunction([](auto s, auto s1) { return "DIRECT"; });
289 int32_t sockfd = -1;
290 struct sockaddr_in serverAddr;
291 struct hostent *server;
292 std::string host = "127.0.0.1";
293 #define LARGET_SIZE (1024 * 50)
294 std::string path(LARGET_SIZE, 'A');
295 int32_t port = 8000;
296 sockfd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
297 if (sockfd < 0) {
298 std::cerr << "Socket creation failed: " << strerror(errno) << std::endl;
299 }
300 server = gethostbyname(host.c_str());
301 if (!server) {
302 std::cerr << "Failed to resolve host: " << host << std::endl;
303 close(sockfd);
304 }
305 memset_s(&serverAddr, sizeof(serverAddr), 0, sizeof(serverAddr));
306 serverAddr.sin_family = AF_INET;
307 serverAddr.sin_port = htons(port);
308 memcpy_s(&serverAddr.sin_addr, sizeof(serverAddr.sin_addr), server->h_addr_list[0], server->h_length);
309 if (connect(sockfd, reinterpret_cast<sockaddr *>(&serverAddr), sizeof(serverAddr)) < 0) {
310 std::cerr << "Connection failed: " << strerror(errno) << std::endl;
311 close(sockfd);
312 }
313
314 std::string response;
315 std::string request =
316 "GET /" + path + " HTTP/1.1\r\n" + "Host: " + host + "\r\n" + "Connection: close\r\n" + "\r\n";
317 if (send(sockfd, request.c_str(), request.length(), 0) < 0) {
318 std::cerr << "Send request failed: " << strerror(errno) << std::endl;
319 close(sockfd);
320 }
321 response.clear();
322 std::vector<char> buffer(4096);
323 ssize_t bytesReceived;
324 while ((bytesReceived = recv(sockfd, buffer.data(), buffer.size(), 0)) > 0) {
325 response.append(buffer.data(), bytesReceived);
326 }
327 if (bytesReceived < 0) {
328 std::cerr << "Receive response failed: " << strerror(errno) << std::endl;
329 close(sockfd);
330 }
331 EXPECT_EQ(bytesReceived, -1);
332 close(sockfd);
333 }
334
TEST(ProxyServerTest,CreateProxyTest12)335 TEST(ProxyServerTest, CreateProxyTest12)
336 {
337 auto proxy = std::make_shared<ProxyServer>(8000, 1);
338 EXPECT_EQ(proxy->Start(), true);
339 proxy->SetFindPacProxyFunction([](auto s, auto s1) { return "DIRECT"; });
340 int32_t sockfd = -1;
341 struct sockaddr_in serverAddr;
342 struct hostent *server;
343 std::string host = "127.0.0.1";
344 std::string path;
345 int32_t port = 8000;
346 sockfd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
347 if (sockfd < 0) {
348 std::cerr << "Socket creation failed: " << strerror(errno) << std::endl;
349 }
350 server = gethostbyname(host.c_str());
351 if (!server) {
352 std::cerr << "Failed to resolve host: " << host << std::endl;
353 close(sockfd);
354 }
355 memset_s(&serverAddr, sizeof(serverAddr), 0, sizeof(serverAddr));
356 serverAddr.sin_family = AF_INET;
357 serverAddr.sin_port = htons(port);
358 memcpy_s(&serverAddr.sin_addr, sizeof(serverAddr.sin_addr), server->h_addr_list[0], server->h_length);
359 if (connect(sockfd, reinterpret_cast<sockaddr *>(&serverAddr), sizeof(serverAddr)) < 0) {
360 std::cerr << "Connection failed: " << strerror(errno) << std::endl;
361 close(sockfd);
362 }
363
364 std::string response;
365 std::string request = "\r\n\r\n";
366 if (send(sockfd, request.c_str(), request.length(), 0) < 0) {
367 std::cerr << "Send request failed: " << strerror(errno) << std::endl;
368 close(sockfd);
369 }
370 response.clear();
371 std::vector<char> buffer(4096);
372 ssize_t bytesReceived;
373 while ((bytesReceived = recv(sockfd, buffer.data(), buffer.size(), 0)) > 0) {
374 response.append(buffer.data(), bytesReceived);
375 }
376 if (bytesReceived < 0) {
377 std::cerr << "Receive response failed: " << strerror(errno) << std::endl;
378 close(sockfd);
379 }
380 EXPECT_EQ(bytesReceived, 0);
381 close(sockfd);
382 }
383
TEST(ProxyServerTest,CreateProxyTest13)384 TEST(ProxyServerTest, CreateProxyTest13)
385 {
386 auto proxy = std::make_shared<ProxyServer>(8000, 1);
387 EXPECT_EQ(proxy->Start(), true);
388 proxy->SetFindPacProxyFunction([](auto s, auto s1) { return "DIRECT"; });
389 int32_t sockfd = -1;
390 struct sockaddr_in serverAddr;
391 struct hostent *server;
392 std::string host = "127.0.0.1";
393 std::string path;
394 int32_t port = 8000;
395 sockfd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
396 if (sockfd < 0) {
397 std::cerr << "Socket creation failed: " << strerror(errno) << std::endl;
398 }
399 server = gethostbyname(host.c_str());
400 if (!server) {
401 std::cerr << "Failed to resolve host: " << host << std::endl;
402 close(sockfd);
403 }
404 memset_s(&serverAddr, sizeof(serverAddr), 0, sizeof(serverAddr));
405 serverAddr.sin_family = AF_INET;
406 serverAddr.sin_port = htons(port);
407 memcpy_s(&serverAddr.sin_addr, sizeof(serverAddr.sin_addr), server->h_addr_list[0], server->h_length);
408 if (connect(sockfd, reinterpret_cast<sockaddr *>(&serverAddr), sizeof(serverAddr)) < 0) {
409 std::cerr << "Connection failed: " << strerror(errno) << std::endl;
410 close(sockfd);
411 }
412
413 std::string response;
414 std::string request = "CONNECT \r\n\r\n";
415 if (send(sockfd, request.c_str(), request.length(), 0) < 0) {
416 std::cerr << "Send request failed: " << strerror(errno) << std::endl;
417 close(sockfd);
418 }
419 response.clear();
420 std::vector<char> buffer(4096);
421 ssize_t bytesReceived;
422 while ((bytesReceived = recv(sockfd, buffer.data(), buffer.size(), 0)) > 0) {
423 response.append(buffer.data(), bytesReceived);
424 }
425 if (bytesReceived < 0) {
426 std::cerr << "Receive response failed: " << strerror(errno) << std::endl;
427 close(sockfd);
428 }
429 EXPECT_EQ(bytesReceived, 0);
430 close(sockfd);
431 }
432
TEST(ProxyServerTest,CreateProxyTest14)433 TEST(ProxyServerTest, CreateProxyTest14)
434 {
435 long maxFd = sysconf(_SC_OPEN_MAX);
436 auto proxy = std::make_shared<ProxyServer>(8000, 1);
437 EXPECT_EQ(proxy->Start(), true);
438 proxy->SetFindPacProxyFunction([](auto s, auto s1) { return "DIRECT"; });
439 std::vector<int32_t> fds;
440 struct rlimit limit;
441 for (int32_t i = 0; i < maxFd; i++) {
442 int32_t fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
443 if (fd == -1) {
444 RequestOptions opts;
445 opts.largeHeader = false;
446 std::string res = Request("https://www.example.com/", "127.0.0.1", 8000, opts);
447 EXPECT_EQ(res.empty(), true);
448 break;
449 } else {
450 fds.push_back(fd);
451 }
452 }
453 for (auto fd : fds) {
454 close(fd);
455 }
456 }
457
TEST(ProxyServerTest,CreateProxyTest15)458 TEST(ProxyServerTest, CreateProxyTest15)
459 {
460 long maxFd = sysconf(_SC_OPEN_MAX);
461 auto proxy = std::make_shared<ProxyServer>(8000, 1);
462 EXPECT_EQ(proxy->Start(), true);
463 std::string res = Request("http://icanhazip.com/", "127.0.0.1", 8000);
464 EXPECT_EQ(res.empty(), true);
465 }
466
TEST(ProxyServerTest,CreateProxyTest16)467 TEST(ProxyServerTest, CreateProxyTest16)
468 {
469 auto proxy = std::make_shared<ProxyServer>(8000, 1);
470 EXPECT_EQ(proxy->Start(), true);
471 proxy->SetFindPacProxyFunction([](auto s, auto s1) { return "DIRECT"; });
472 int32_t sockfd = -1;
473 struct sockaddr_in serverAddr;
474 struct hostent *server;
475 std::string host = "127.0.0.1";
476 std::string path;
477 int32_t port = 8000;
478 sockfd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
479 if (sockfd < 0) {
480 std::cerr << "Socket creation failed: " << strerror(errno) << std::endl;
481 }
482 server = gethostbyname(host.c_str());
483 if (!server) {
484 std::cerr << "Failed to resolve host: " << host << std::endl;
485 close(sockfd);
486 }
487 memset_s(&serverAddr, sizeof(serverAddr), 0, sizeof(serverAddr));
488 serverAddr.sin_family = AF_INET;
489 serverAddr.sin_port = htons(port);
490 memcpy_s(&serverAddr.sin_addr, sizeof(serverAddr.sin_addr), server->h_addr_list[0], server->h_length);
491 if (connect(sockfd, reinterpret_cast<sockaddr *>(&serverAddr), sizeof(serverAddr)) < 0) {
492 std::cerr << "Connection failed: " << strerror(errno) << std::endl;
493 close(sockfd);
494 }
495
496 std::string response;
497 std::string request = "GET \r\n\r\n";
498 if (send(sockfd, request.c_str(), request.length(), 0) < 0) {
499 std::cerr << "Send request failed: " << strerror(errno) << std::endl;
500 close(sockfd);
501 }
502 response.clear();
503 std::vector<char> buffer(4096);
504 ssize_t bytesReceived;
505 while ((bytesReceived = recv(sockfd, buffer.data(), buffer.size(), 0)) > 0) {
506 response.append(buffer.data(), bytesReceived);
507 }
508 if (bytesReceived < 0) {
509 std::cerr << "Receive response failed: " << strerror(errno) << std::endl;
510 close(sockfd);
511 }
512 EXPECT_EQ(bytesReceived, 0);
513 close(sockfd);
514 }
515
TEST(ProxyServerTest,CreateProxyTest17)516 TEST(ProxyServerTest, CreateProxyTest17)
517 {
518 auto proxy = std::make_shared<ProxyServer>(8000, 1);
519 EXPECT_EQ(proxy->Start(), true);
520 proxy->SetFindPacProxyFunction([](auto s, auto s1) { return "DIRECT"; });
521 int32_t sockfd = -1;
522 struct sockaddr_in serverAddr;
523 struct hostent *server;
524 std::string host = "127.0.0.1";
525 std::string path;
526 int32_t port = 8000;
527 sockfd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
528 if (sockfd < 0) {
529 std::cerr << "Socket creation failed: " << strerror(errno) << std::endl;
530 }
531 server = gethostbyname(host.c_str());
532 if (!server) {
533 std::cerr << "Failed to resolve host: " << host << std::endl;
534 close(sockfd);
535 }
536 memset_s(&serverAddr, sizeof(serverAddr), 0, sizeof(serverAddr));
537 serverAddr.sin_family = AF_INET;
538 serverAddr.sin_port = htons(port);
539 memcpy_s(&serverAddr.sin_addr, sizeof(serverAddr.sin_addr), server->h_addr_list[0], server->h_length);
540 if (connect(sockfd, reinterpret_cast<sockaddr *>(&serverAddr), sizeof(serverAddr)) < 0) {
541 std::cerr << "Connection failed: " << strerror(errno) << std::endl;
542 close(sockfd);
543 }
544
545 std::string response;
546 std::string request = "GET \r\n \r\n\r\n";
547 if (send(sockfd, request.c_str(), request.length(), 0) < 0) {
548 std::cerr << "Send request failed: " << strerror(errno) << std::endl;
549 close(sockfd);
550 }
551 response.clear();
552 std::vector<char> buffer(4096);
553 ssize_t bytesReceived;
554 while ((bytesReceived = recv(sockfd, buffer.data(), buffer.size(), 0)) > 0) {
555 response.append(buffer.data(), bytesReceived);
556 }
557 if (bytesReceived < 0) {
558 std::cerr << "Receive response failed: " << strerror(errno) << std::endl;
559 close(sockfd);
560 }
561 EXPECT_EQ(bytesReceived, 0);
562 close(sockfd);
563 }
564
TEST(ProxyServerTest,CreateProxyTest18)565 TEST(ProxyServerTest, CreateProxyTest18)
566 {
567 auto proxy = std::make_shared<ProxyServer>(8000, 1);
568 EXPECT_EQ(proxy->Start(), true);
569 proxy->SetFindPacProxyFunction([](auto s, auto s1) { return "DIRECT"; });
570 int32_t sockfd = -1;
571 struct sockaddr_in serverAddr;
572 struct hostent *server;
573 std::string host = "127.0.0.1";
574 std::string path;
575 int32_t port = 8000;
576 sockfd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
577 if (sockfd < 0) {
578 std::cerr << "Socket creation failed: " << strerror(errno) << std::endl;
579 }
580 server = gethostbyname(host.c_str());
581 if (!server) {
582 std::cerr << "Failed to resolve host: " << host << std::endl;
583 close(sockfd);
584 }
585 memset_s(&serverAddr, sizeof(serverAddr), 0, sizeof(serverAddr));
586 serverAddr.sin_family = AF_INET;
587 serverAddr.sin_port = htons(port);
588 memcpy_s(&serverAddr.sin_addr, sizeof(serverAddr.sin_addr), server->h_addr_list[0], server->h_length);
589 if (connect(sockfd, reinterpret_cast<sockaddr *>(&serverAddr), sizeof(serverAddr)) < 0) {
590 std::cerr << "Connection failed: " << strerror(errno) << std::endl;
591 close(sockfd);
592 }
593
594 std::string response;
595 std::string request = "GET \r\n Host: 127.0.0.1 \r\n\r\n";
596 if (send(sockfd, request.c_str(), request.length(), 0) < 0) {
597 std::cerr << "Send request failed: " << strerror(errno) << std::endl;
598 close(sockfd);
599 }
600 response.clear();
601 std::vector<char> buffer(4096);
602 ssize_t bytesReceived;
603 while ((bytesReceived = recv(sockfd, buffer.data(), buffer.size(), 0)) > 0) {
604 response.append(buffer.data(), bytesReceived);
605 }
606 if (bytesReceived < 0) {
607 std::cerr << "Receive response failed: " << strerror(errno) << std::endl;
608 close(sockfd);
609 }
610 EXPECT_EQ(bytesReceived, 0);
611 close(sockfd);
612 }
613
TEST(ProxyServerTest,CreateProxyTest19)614 TEST(ProxyServerTest, CreateProxyTest19)
615 {
616 auto proxy = std::make_shared<ProxyServer>(8000, 1);
617 EXPECT_EQ(proxy->Start(), true);
618 proxy->SetFindPacProxyFunction([](auto s, auto s1) { return "DIRECT"; });
619 int32_t sockfd = -1;
620 struct sockaddr_in serverAddr;
621 struct hostent *server;
622 std::string host = "127.0.0.1";
623 std::string path;
624 int32_t port = 8000;
625 sockfd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
626 if (sockfd < 0) {
627 std::cerr << "Socket creation failed: " << strerror(errno) << std::endl;
628 }
629 server = gethostbyname(host.c_str());
630 if (!server) {
631 std::cerr << "Failed to resolve host: " << host << std::endl;
632 close(sockfd);
633 }
634 memset_s(&serverAddr, sizeof(serverAddr), 0, sizeof(serverAddr));
635 serverAddr.sin_family = AF_INET;
636 serverAddr.sin_port = htons(port);
637 memcpy_s(&serverAddr.sin_addr, sizeof(serverAddr.sin_addr), server->h_addr_list[0], server->h_length);
638 if (connect(sockfd, reinterpret_cast<sockaddr *>(&serverAddr), sizeof(serverAddr)) < 0) {
639 std::cerr << "Connection failed: " << strerror(errno) << std::endl;
640 close(sockfd);
641 }
642
643 std::string response;
644 std::string request = "GET \r\n Host: 127.0.0.1:9999 \r\n\r\n";
645 if (send(sockfd, request.c_str(), request.length(), 0) < 0) {
646 std::cerr << "Send request failed: " << strerror(errno) << std::endl;
647 close(sockfd);
648 }
649 response.clear();
650 std::vector<char> buffer(4096);
651 ssize_t bytesReceived;
652 while ((bytesReceived = recv(sockfd, buffer.data(), buffer.size(), 0)) > 0) {
653 response.append(buffer.data(), bytesReceived);
654 }
655 if (bytesReceived < 0) {
656 std::cerr << "Receive response failed: " << strerror(errno) << std::endl;
657 close(sockfd);
658 }
659 EXPECT_EQ(bytesReceived, 0);
660 close(sockfd);
661 }
662
TEST(ProxyServerTest,CreateProxyTest20)663 TEST(ProxyServerTest, CreateProxyTest20)
664 {
665 auto proxy = std::make_shared<ProxyServer>(8888, 1);
666 EXPECT_EQ(proxy->Start(), true);
667 proxy->SetFindPacProxyFunction(
668 [](auto s, auto s1) { return "DIRECT 127.0.0.1:abc;PROXY 127.0.0.1:abc;DIRECT;PROXY 127.0.0.1"; });
669 std::string res = Request("http://icanhazip.com/", "127.0.0.1", 8888);
670 EXPECT_NE(res.empty(), false);
671 }
672
673 TEST(ProxyServerTest, 21)
674 {
675 auto proxy = std::make_shared<ProxyServer>(8888, 1);
676 EXPECT_EQ(proxy->Start(), true);
__anone14daa511302(auto s, auto s1) 677 proxy->SetFindPacProxyFunction([](auto s, auto s1) { return "PROXY 127.0.0.1:111111111111 ;"; });
678 std::string res = Request("http://icanhazip.com/", "127.0.0.1", 8888);
679 EXPECT_EQ(res.empty(), true);
680 }
681 #endif