Skip to content

Commit

Permalink
g3proxy: add ftp over http test script via pycurl
Browse files Browse the repository at this point in the history
  • Loading branch information
zh-jq-b committed Nov 19, 2024
1 parent efc7d1e commit 7ca7fff
Show file tree
Hide file tree
Showing 6 changed files with 120 additions and 0 deletions.
94 changes: 94 additions & 0 deletions g3proxy/ci/python3+curl/test_ftp_over_http.py
Original file line number Diff line number Diff line change
@@ -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)
15 changes: 15 additions & 0 deletions scripts/coverage/g3proxy/0001_base_http_proxy/testcases.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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:[email protected]
}


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}
Expand All @@ -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:[email protected] --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
3 changes: 3 additions & 0 deletions scripts/coverage/g3proxy/0006_chain_http_proxy/testcases.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 2 additions & 0 deletions scripts/coverage/g3proxy/0007_chain_socks_proxy/testcases.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 2 additions & 0 deletions scripts/coverage/g3proxy/0008_base_user_auth/testcases.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 4 additions & 0 deletions scripts/coverage/g3proxy/0009_anonymous_user/testcases.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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

Expand Down

0 comments on commit 7ca7fff

Please sign in to comment.