From 7ca7fffe8af39ded8ef89a7b630d0bc9ffc88aed Mon Sep 17 00:00:00 2001 From: Zhang Jingqiang Date: Tue, 19 Nov 2024 12:51:34 +0800 Subject: [PATCH] g3proxy: add ftp over http test script via pycurl --- g3proxy/ci/python3+curl/test_ftp_over_http.py | 94 +++++++++++++++++++ .../g3proxy/0001_base_http_proxy/testcases.sh | 15 +++ .../0006_chain_http_proxy/testcases.sh | 3 + .../0007_chain_socks_proxy/testcases.sh | 2 + .../g3proxy/0008_base_user_auth/testcases.sh | 2 + .../g3proxy/0009_anonymous_user/testcases.sh | 4 + 6 files changed, 120 insertions(+) create mode 100644 g3proxy/ci/python3+curl/test_ftp_over_http.py diff --git a/g3proxy/ci/python3+curl/test_ftp_over_http.py b/g3proxy/ci/python3+curl/test_ftp_over_http.py new file mode 100644 index 000000000..dc6045cf9 --- /dev/null +++ b/g3proxy/ci/python3+curl/test_ftp_over_http.py @@ -0,0 +1,94 @@ +#!/usr/bin/env python3 + +import argparse +import sys +import unittest +from io import BytesIO + +import pycurl + +target_site = 'ftp://127.0.0.1' +target_proxy = None +proxy_ca_cert = None + + +class TestFtp(unittest.TestCase): + def setUp(self): + self.buffer = BytesIO() + + self.c = pycurl.Curl() + self.c.setopt(pycurl.WRITEFUNCTION, self.buffer.write) + if target_proxy is not None: + self.c.setopt(pycurl.PROXY, target_proxy) + if proxy_ca_cert is not None: + self.c.setopt(pycurl.PROXY_CAINFO, proxy_ca_cert) + + def tearDown(self): + self.c.close() + + def set_url_and_request_target(self, path: str): + self.c.setopt(pycurl.URL, f"{target_site}{path}") + + def test_001_list_root(self): + self.set_url_and_request_target('/') + self.c.perform() + self.assertEqual(self.c.getinfo(pycurl.RESPONSE_CODE), 200) + + def test_002_list_file(self): + self.set_url_and_request_target('/uploaded_file') + self.c.perform() + self.assertEqual(self.c.getinfo(pycurl.RESPONSE_CODE), 200) + self.assertEqual(self.buffer.getvalue(), b'') + + def test_003_upload_file(self): + self.set_url_and_request_target('/uploaded_file') + self.c.setopt(pycurl.UPLOAD, 1) + file = open(__file__) + self.c.setopt(pycurl.READDATA, file) + self.c.perform() + self.assertEqual(self.c.getinfo(pycurl.RESPONSE_CODE), 200) + file.close() + + def test_004_download_file(self): + self.set_url_and_request_target('/uploaded_file') + self.c.perform() + self.assertEqual(self.c.getinfo(pycurl.RESPONSE_CODE), 200) + file = open(__file__) + data = file.read() + self.assertEqual(self.buffer.getvalue().decode('utf-8'), data) + file.close() + + def test_005_delete_file(self): + self.set_url_and_request_target('/uploaded_file') + self.c.setopt(pycurl.CUSTOMREQUEST, 'DELETE') + self.c.perform() + self.assertEqual(self.c.getinfo(pycurl.RESPONSE_CODE), 200) + + def test_006_delete_file(self): + self.set_url_and_request_target('/uploaded_file') + self.c.setopt(pycurl.CUSTOMREQUEST, 'DELETE') + self.c.perform() + self.assertEqual(self.c.getinfo(pycurl.RESPONSE_CODE), 404) + + def test_007_list_root(self): + self.set_url_and_request_target('/') + self.c.perform() + self.assertEqual(self.c.getinfo(pycurl.RESPONSE_CODE), 200) + + +if __name__ == '__main__': + parser = argparse.ArgumentParser() + parser.add_argument('--proxy', '-x', nargs='?', help='Proxy URL', required=True) + parser.add_argument('--site', '-T', nargs='?', help='Target Site', default=target_site) + parser.add_argument('--proxy-ca-cert', nargs='?', help='Proxy CA Cert') + + (args, left_args) = parser.parse_known_args() + + if args.proxy_ca_cert is not None: + proxy_ca_cert = args.proxy_ca_cert + target_site = args.site + target_proxy = args.proxy + + left_args.insert(0, sys.argv[0]) + + unittest.main(argv=left_args) diff --git a/scripts/coverage/g3proxy/0001_base_http_proxy/testcases.sh b/scripts/coverage/g3proxy/0001_base_http_proxy/testcases.sh index a50a32bfc..7bb3cd519 100644 --- a/scripts/coverage/g3proxy/0001_base_http_proxy/testcases.sh +++ b/scripts/coverage/g3proxy/0001_base_http_proxy/testcases.sh @@ -9,6 +9,12 @@ test_http_proxy_http_forward() } +test_http_proxy_ftp_over_http() +{ + python3 "${PROJECT_DIR}/g3proxy/ci/python3+curl/test_ftp_over_http.py" -x ${HTTP_PROXY} -T ftp://ftpuser:ftppass@127.0.0.1 +} + + test_https_proxy_http_forward() { python3 "${PROJECT_DIR}/g3proxy/ci/python3+curl/test_httpbin.py" -x ${HTTPS_PROXY} -T http://httpbin.local --proxy-ca-cert ${SSL_CERT_FILE} @@ -17,13 +23,22 @@ test_https_proxy_http_forward() } +test_https_proxy_ftp_over_http() +{ + python3 "${PROJECT_DIR}/g3proxy/ci/python3+curl/test_ftp_over_http.py" -x ${HTTPS_PROXY} -T ftp://ftpuser:ftppass@127.0.0.1 --proxy-ca-cert ${SSL_CERT_FILE} +} + + HTTP_PROXY="http://127.0.0.1:8080" test_http_proxy_http_forward +test_http_proxy_ftp_over_http HTTPS_PROXY="https://g3proxy.local:8443" test_https_proxy_http_forward +test_https_proxy_ftp_over_http HTTPS_PROXY="https://g3proxy.local:9443" test_https_proxy_http_forward +test_https_proxy_ftp_over_http diff --git a/scripts/coverage/g3proxy/0006_chain_http_proxy/testcases.sh b/scripts/coverage/g3proxy/0006_chain_http_proxy/testcases.sh index d48d86610..e84b84db5 100644 --- a/scripts/coverage/g3proxy/0006_chain_http_proxy/testcases.sh +++ b/scripts/coverage/g3proxy/0006_chain_http_proxy/testcases.sh @@ -45,12 +45,15 @@ test_socks4_proxy_https() HTTP_PROXY="http://127.0.0.1:8080" test_http_proxy_http_forward +# FTP not supported in proxy escaper +#test_http_proxy_ftp_over_http test_http_proxy_https_connect test_http_proxy_https_forward HTTPS_PROXY="https://g3proxy.local:8443" test_https_proxy_http_forward +test_https_proxy_ftp_over_http test_https_proxy_https_connect test_https_proxy_https_forward diff --git a/scripts/coverage/g3proxy/0007_chain_socks_proxy/testcases.sh b/scripts/coverage/g3proxy/0007_chain_socks_proxy/testcases.sh index 66ac497a5..df4079f52 100644 --- a/scripts/coverage/g3proxy/0007_chain_socks_proxy/testcases.sh +++ b/scripts/coverage/g3proxy/0007_chain_socks_proxy/testcases.sh @@ -3,6 +3,8 @@ HTTP_PROXY="http://127.0.0.1:8080" test_http_proxy_http_forward +# FTP not supported in proxy escaper +#test_http_proxy_ftp_over_http SOCKS5_PROXY="socks5h://127.0.0.1:1080" diff --git a/scripts/coverage/g3proxy/0008_base_user_auth/testcases.sh b/scripts/coverage/g3proxy/0008_base_user_auth/testcases.sh index e7efcfda3..328fdb79c 100644 --- a/scripts/coverage/g3proxy/0008_base_user_auth/testcases.sh +++ b/scripts/coverage/g3proxy/0008_base_user_auth/testcases.sh @@ -6,11 +6,13 @@ do HTTP_PROXY="http://${user}@127.0.0.1:8080" test_http_proxy_http_forward + test_http_proxy_ftp_over_http test_http_proxy_https_connect test_http_proxy_https_forward HTTPS_PROXY="https://${user}@g3proxy.local:8443" test_https_proxy_http_forward + test_https_proxy_ftp_over_http test_https_proxy_https_connect test_https_proxy_https_forward diff --git a/scripts/coverage/g3proxy/0009_anonymous_user/testcases.sh b/scripts/coverage/g3proxy/0009_anonymous_user/testcases.sh index 5266bba6a..c7e29ddf3 100644 --- a/scripts/coverage/g3proxy/0009_anonymous_user/testcases.sh +++ b/scripts/coverage/g3proxy/0009_anonymous_user/testcases.sh @@ -6,11 +6,13 @@ do HTTP_PROXY="http://${user}@127.0.0.1:8080" test_http_proxy_http_forward + test_http_proxy_ftp_over_http test_http_proxy_https_connect test_http_proxy_https_forward HTTPS_PROXY="https://${user}@g3proxy.local:8443" test_https_proxy_http_forward + test_https_proxy_ftp_over_http test_https_proxy_https_connect test_https_proxy_https_forward @@ -23,12 +25,14 @@ done HTTP_PROXY="http://127.0.0.1:8080" test_http_proxy_http_forward +test_http_proxy_ftp_over_http test_http_proxy_https_connect test_http_proxy_https_forward HTTPS_PROXY="https://g3proxy.local:8443" test_https_proxy_http_forward +test_https_proxy_ftp_over_http test_https_proxy_https_connect test_https_proxy_https_forward