Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

g3proxy: add user coverage test CI #370

Merged
merged 2 commits into from
Nov 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/g3-io-ext/src/limit/datagram.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ use tokio::time::Instant;

use super::LocalDatagramLimiter;

#[derive(Debug, Eq, PartialEq)]
pub enum DatagramLimitAction {
Advance(usize),
DelayUntil(Instant),
Expand Down
65 changes: 59 additions & 6 deletions lib/g3-io-ext/src/limit/token_bucket/datagram.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,14 @@ impl GlobalDatagramLimiter {
}
let config = *self.config.load().as_ref();
tokio::time::sleep(config.replenish_interval()).await;
self.add_bytes(config.replenish_bytes(), config.max_burst_bytes());
self.add_packets(config.replenish_packets(), config.max_burst_packets());
let replenish_bytes = config.replenish_bytes();
if replenish_bytes > 0 {
self.add_bytes(replenish_bytes, config.max_burst_bytes());
}
let replenish_packets = config.replenish_packets();
if replenish_packets > 0 {
self.add_packets(replenish_packets, config.max_burst_packets());
}
self.last_updated.store(Arc::new(Instant::now()));
}
};
Expand Down Expand Up @@ -180,7 +186,10 @@ impl GlobalDatagramLimit for GlobalDatagramLimiter {
Ordering::AcqRel,
Ordering::Acquire,
) {
Ok(_) => to_advance = (cur_tokens - left_tokens) as usize,
Ok(_) => {
to_advance = (cur_tokens - left_tokens) as usize;
break;
}
Err(actual) => cur_tokens = actual,
}
}
Expand All @@ -204,7 +213,10 @@ impl GlobalDatagramLimit for GlobalDatagramLimiter {
Ordering::AcqRel,
Ordering::Acquire,
) {
Ok(_) => buf_size = (cur_tokens - left_tokens) as usize,
Ok(_) => {
buf_size = (cur_tokens - left_tokens) as usize;
break;
}
Err(actual) => cur_tokens = actual,
}
}
Expand Down Expand Up @@ -248,11 +260,52 @@ impl GlobalDatagramLimit for GlobalDatagramLimiter {

fn release_bytes(&self, size: usize) {
let max_burst = self.config.load().as_ref().max_burst_bytes();
self.add_bytes(size as u64, max_burst);
if max_burst > 0 {
self.add_bytes(size as u64, max_burst);
}
}

fn release_packets(&self, count: usize) {
let max_burst = self.config.load().as_ref().max_burst_packets();
self.add_packets(count as u64, max_burst);
if max_burst > 0 {
self.add_packets(count as u64, max_burst);
}
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn check_packet() {
let config = GlobalDatagramSpeedLimitConfig::per_second(1000);
let limiter = GlobalDatagramLimiter::new(config);
assert_eq!(limiter.check_packet(100), DatagramLimitAction::Advance(1));
assert_eq!(limiter.check_packet(900), DatagramLimitAction::Advance(1));
assert_ne!(limiter.check_packet(100), DatagramLimitAction::Advance(1));
}

#[test]
fn check_packets() {
let config = GlobalDatagramSpeedLimitConfig::per_second(1000);
let limiter = GlobalDatagramLimiter::new(config);
let total_len_v = [100, 200];
assert_eq!(
limiter.check_packets(&total_len_v),
DatagramLimitAction::Advance(2)
);

let total_len_v = [200, 700, 900];
assert_eq!(
limiter.check_packets(&total_len_v),
DatagramLimitAction::Advance(2)
);

let total_len_v = [50, 40, 10];
assert_eq!(
limiter.check_packets(&total_len_v),
DatagramLimitAction::Advance(3)
);
}
}
15 changes: 15 additions & 0 deletions lib/g3-io-ext/src/limit/token_bucket/stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,3 +128,18 @@ impl GlobalStreamLimit for GlobalStreamLimiter {
self.add_bytes(size as u64, max_burst);
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn check() {
let config = GlobalStreamSpeedLimitConfig::per_second(1000);
let limiter = GlobalStreamLimiter::new(GlobalLimitGroup::Server, config);
assert_eq!(limiter.check(100), StreamLimitAction::AdvanceBy(100));
assert_eq!(limiter.check(900), StreamLimitAction::AdvanceBy(900));
limiter.release(100);
assert_eq!(limiter.check(1000), StreamLimitAction::AdvanceBy(100));
}
}
26 changes: 17 additions & 9 deletions scripts/coverage/g3proxy/0001_base_http_proxy/testcases.sh
Original file line number Diff line number Diff line change
@@ -1,21 +1,29 @@
#!/bin/sh

HTTP_PROXY="http://127.0.0.1:8080"

python3 "${PROJECT_DIR}/g3proxy/ci/python3+curl/test_httpbin.py" -x ${HTTP_PROXY} -T http://httpbin.local
test_http_proxy_http_forward()
{
python3 "${PROJECT_DIR}/g3proxy/ci/python3+curl/test_httpbin.py" -x ${HTTP_PROXY} -T http://httpbin.local

python3 "${PROJECT_DIR}/g3proxy/ci/python3+requests/test_httpbin.py" -x ${HTTP_PROXY} -T http://httpbin.local
python3 "${PROJECT_DIR}/g3proxy/ci/python3+requests/test_httpbin.py" -x ${HTTP_PROXY} -T http://httpbin.local
}


HTTPS_PROXY="https://g3proxy.local:8443"
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}

python3 "${PROJECT_DIR}/g3proxy/ci/python3+curl/test_httpbin.py" -x ${HTTPS_PROXY} -T http://httpbin.local --proxy-ca-cert ${SSL_CERT_FILE}
python3 "${PROJECT_DIR}/g3proxy/ci/python3+requests/test_httpbin.py" -x ${HTTPS_PROXY} -T http://httpbin.local
}

python3 "${PROJECT_DIR}/g3proxy/ci/python3+requests/test_httpbin.py" -x ${HTTPS_PROXY} -T http://httpbin.local

HTTP_PROXY="http://127.0.0.1:8080"
test_http_proxy_http_forward

HTTPS_PROXY="https://g3proxy.local:9443"

python3 "${PROJECT_DIR}/g3proxy/ci/python3+curl/test_httpbin.py" -x ${HTTPS_PROXY} -T http://httpbin.local --proxy-ca-cert ${SSL_CERT_FILE}
HTTPS_PROXY="https://g3proxy.local:8443"
test_https_proxy_http_forward


python3 "${PROJECT_DIR}/g3proxy/ci/python3+requests/test_httpbin.py" -x ${HTTPS_PROXY} -T http://httpbin.local
HTTPS_PROXY="https://g3proxy.local:9443"
test_https_proxy_http_forward
40 changes: 28 additions & 12 deletions scripts/coverage/g3proxy/0002_base_socks_proxy/testcases.sh
Original file line number Diff line number Diff line change
@@ -1,22 +1,38 @@
#!/bin/sh

SOCKS5_PROXY="socks5h://127.0.0.1:1080"
SOCKS4_PROXY="socks4a://127.0.0.1:1080"

python3 "${PROJECT_DIR}/g3proxy/ci/python3+curl/test_httpbin.py" -x ${SOCKS5_PROXY} -T http://httpbin.local
python3 "${PROJECT_DIR}/g3proxy/ci/python3+curl/test_httpbin.py" -x ${SOCKS4_PROXY} -T http://httpbin.local
test_socks5_proxy_http()
{
python3 "${PROJECT_DIR}/g3proxy/ci/python3+curl/test_httpbin.py" -x ${SOCKS5_PROXY} -T http://httpbin.local
python3 "${PROJECT_DIR}/g3proxy/ci/python3+requests/test_httpbin.py" -x ${SOCKS5_PROXY} -T http://httpbin.local
}

python3 "${PROJECT_DIR}/g3proxy/ci/python3+requests/test_httpbin.py" -x ${SOCKS5_PROXY} -T http://httpbin.local

python3 "${PROJECT_DIR}/scripts/test/socks5_dns_query.py" -x ${SOCKS5_PROXY} --dns-server 127.0.0.1 g3proxy.local httpbin.local -v
test_socks5_proxy_dns()
{
python3 "${PROJECT_DIR}/scripts/test/socks5_dns_query.py" -x ${SOCKS5_PROXY} --dns-server 127.0.0.1 g3proxy.local httpbin.local -v
}


SOCKS5_PROXY="socks5h://127.0.0.1:1081"
SOCKS4_PROXY="socks4a://127.0.0.1:1081"
test_socks4_proxy_http()
{
python3 "${PROJECT_DIR}/g3proxy/ci/python3+curl/test_httpbin.py" -x ${SOCKS4_PROXY} -T http://httpbin.local
}

python3 "${PROJECT_DIR}/g3proxy/ci/python3+curl/test_httpbin.py" -x ${SOCKS5_PROXY} -T http://httpbin.local
python3 "${PROJECT_DIR}/g3proxy/ci/python3+curl/test_httpbin.py" -x ${SOCKS4_PROXY} -T http://httpbin.local

python3 "${PROJECT_DIR}/g3proxy/ci/python3+requests/test_httpbin.py" -x ${SOCKS5_PROXY} -T http://httpbin.local
SOCKS5_PROXY="socks5h://127.0.0.1:1080"
test_socks5_proxy_http
test_socks5_proxy_dns


python3 "${PROJECT_DIR}/scripts/test/socks5_dns_query.py" -x ${SOCKS5_PROXY} --dns-server 127.0.0.1 g3proxy.local httpbin.local -v
SOCKS4_PROXY="socks4a://127.0.0.1:1080"
test_socks4_proxy_http


SOCKS5_PROXY="socks5h://127.0.0.1:1081"
test_socks5_proxy_http
test_socks5_proxy_dns


SOCKS4_PROXY="socks4a://127.0.0.1:1081"
test_socks4_proxy_http
73 changes: 55 additions & 18 deletions scripts/coverage/g3proxy/0006_chain_http_proxy/testcases.sh
Original file line number Diff line number Diff line change
@@ -1,28 +1,65 @@
#!/bin/sh


test_http_proxy_https_connect()
{
python3 "${PROJECT_DIR}/g3proxy/ci/python3+curl/test_httpbin.py" -x ${HTTP_PROXY} -T https://httpbin.local:9443 --no-auth --ca-cert ${SSL_CERT_FILE}

python3 "${PROJECT_DIR}/g3proxy/ci/python3+requests/test_httpbin.py" -x ${HTTP_PROXY} -T https://httpbin.local:9443 --no-auth --ca-cert ${SSL_CERT_FILE}
}


test_http_proxy_https_forward()
{
python3 "${PROJECT_DIR}/g3proxy/ci/python3+curl/test_httpbin.py" -x ${HTTP_PROXY} -T http://httpbin.local --no-auth --request-target-prefix https://httpbin.local:9443
}


test_https_proxy_https_connect()
{
python3 "${PROJECT_DIR}/g3proxy/ci/python3+curl/test_httpbin.py" -x ${HTTPS_PROXY} -T https://httpbin.local:9443 --no-auth --proxy-ca-cert ${SSL_CERT_FILE} --ca-cert ${SSL_CERT_FILE}

python3 "${PROJECT_DIR}/g3proxy/ci/python3+requests/test_httpbin.py" -x ${HTTPS_PROXY} -T https://httpbin.local:9443 --no-auth --ca-cert ${SSL_CERT_FILE}
}


test_https_proxy_https_forward()
{
python3 "${PROJECT_DIR}/g3proxy/ci/python3+curl/test_httpbin.py" -x ${HTTPS_PROXY} -T http://httpbin.local --no-auth --proxy-ca-cert ${SSL_CERT_FILE} --request-target-prefix https://httpbin.local:9443
}


test_socks5_proxy_https()
{
python3 "${PROJECT_DIR}/g3proxy/ci/python3+curl/test_httpbin.py" -x ${SOCKS5_PROXY} -T https://httpbin.local:9443 --no-auth --ca-cert ${SSL_CERT_FILE}

python3 "${PROJECT_DIR}/g3proxy/ci/python3+requests/test_httpbin.py" -x ${SOCKS5_PROXY} -T https://httpbin.local:9443 --no-auth --ca-cert ${SSL_CERT_FILE}
}


test_socks4_proxy_https()
{
python3 "${PROJECT_DIR}/g3proxy/ci/python3+curl/test_httpbin.py" -x ${SOCKS4_PROXY} -T https://httpbin.local:9443 --no-auth --ca-cert ${SSL_CERT_FILE}
}


HTTP_PROXY="http://127.0.0.1:8080"
HTTPS_PROXY="https://g3proxy.local:8443"
test_http_proxy_http_forward
test_http_proxy_https_connect
test_http_proxy_https_forward

python3 "${PROJECT_DIR}/g3proxy/ci/python3+curl/test_httpbin.py" -x ${HTTP_PROXY} -T http://httpbin.local
python3 "${PROJECT_DIR}/g3proxy/ci/python3+curl/test_httpbin.py" -x ${HTTP_PROXY} -T https://httpbin.local:9443 --no-auth --ca-cert ${SSL_CERT_FILE}
python3 "${PROJECT_DIR}/g3proxy/ci/python3+curl/test_httpbin.py" -x ${HTTP_PROXY} -T http://httpbin.local --no-auth --request-target-prefix https://httpbin.local:9443
python3 "${PROJECT_DIR}/g3proxy/ci/python3+curl/test_httpbin.py" -x ${HTTPS_PROXY} -T http://httpbin.local --proxy-ca-cert ${SSL_CERT_FILE}
python3 "${PROJECT_DIR}/g3proxy/ci/python3+curl/test_httpbin.py" -x ${HTTPS_PROXY} -T https://httpbin.local:9443 --no-auth --proxy-ca-cert ${SSL_CERT_FILE} --ca-cert ${SSL_CERT_FILE}
python3 "${PROJECT_DIR}/g3proxy/ci/python3+curl/test_httpbin.py" -x ${HTTPS_PROXY} -T http://httpbin.local --no-auth --proxy-ca-cert ${SSL_CERT_FILE} --request-target-prefix https://httpbin.local:9443

python3 "${PROJECT_DIR}/g3proxy/ci/python3+requests/test_httpbin.py" -x ${HTTP_PROXY} -T http://httpbin.local
python3 "${PROJECT_DIR}/g3proxy/ci/python3+requests/test_httpbin.py" -x ${HTTP_PROXY} -T https://httpbin.local:9443 --no-auth --ca-cert ${SSL_CERT_FILE}
python3 "${PROJECT_DIR}/g3proxy/ci/python3+requests/test_httpbin.py" -x ${HTTPS_PROXY} -T http://httpbin.local
python3 "${PROJECT_DIR}/g3proxy/ci/python3+requests/test_httpbin.py" -x ${HTTPS_PROXY} -T https://httpbin.local:9443 --no-auth --ca-cert ${SSL_CERT_FILE}
HTTPS_PROXY="https://g3proxy.local:8443"
test_https_proxy_http_forward
test_https_proxy_https_connect
test_https_proxy_https_forward


SOCKS5_PROXY="socks5h://127.0.0.1:1080"
SOCKS4_PROXY="socks4a://127.0.0.1:1080"
test_socks5_proxy_http
test_socks5_proxy_https

python3 "${PROJECT_DIR}/g3proxy/ci/python3+curl/test_httpbin.py" -x ${SOCKS5_PROXY} -T http://httpbin.local
python3 "${PROJECT_DIR}/g3proxy/ci/python3+curl/test_httpbin.py" -x ${SOCKS5_PROXY} -T https://httpbin.local:9443 --no-auth --ca-cert ${SSL_CERT_FILE}
python3 "${PROJECT_DIR}/g3proxy/ci/python3+curl/test_httpbin.py" -x ${SOCKS4_PROXY} -T http://httpbin.local
python3 "${PROJECT_DIR}/g3proxy/ci/python3+curl/test_httpbin.py" -x ${SOCKS4_PROXY} -T https://httpbin.local:9443 --no-auth --ca-cert ${SSL_CERT_FILE}

python3 "${PROJECT_DIR}/g3proxy/ci/python3+requests/test_httpbin.py" -x ${SOCKS5_PROXY} -T http://httpbin.local
python3 "${PROJECT_DIR}/g3proxy/ci/python3+requests/test_httpbin.py" -x ${SOCKS5_PROXY} -T https://httpbin.local:9443 --no-auth --ca-cert ${SSL_CERT_FILE}
SOCKS4_PROXY="socks4a://127.0.0.1:1080"
test_socks4_proxy_http
test_socks4_proxy_https
26 changes: 10 additions & 16 deletions scripts/coverage/g3proxy/0007_chain_socks_proxy/testcases.sh
Original file line number Diff line number Diff line change
@@ -1,29 +1,23 @@
#!/bin/sh

HTTP_PROXY="http://127.0.0.1:8080"

python3 "${PROJECT_DIR}/g3proxy/ci/python3+curl/test_httpbin.py" -x ${HTTP_PROXY} -T http://httpbin.local

python3 "${PROJECT_DIR}/g3proxy/ci/python3+requests/test_httpbin.py" -x ${HTTP_PROXY} -T http://httpbin.local
HTTP_PROXY="http://127.0.0.1:8080"
test_http_proxy_http_forward


SOCKS5_PROXY="socks5h://127.0.0.1:1080"
SOCKS4_PROXY="socks4a://127.0.0.1:1080"

python3 "${PROJECT_DIR}/g3proxy/ci/python3+curl/test_httpbin.py" -x ${SOCKS5_PROXY} -T http://httpbin.local
python3 "${PROJECT_DIR}/g3proxy/ci/python3+curl/test_httpbin.py" -x ${SOCKS4_PROXY} -T http://httpbin.local
test_socks5_proxy_http
test_socks5_proxy_dns

python3 "${PROJECT_DIR}/g3proxy/ci/python3+requests/test_httpbin.py" -x ${SOCKS5_PROXY} -T http://httpbin.local

python3 "${PROJECT_DIR}/scripts/test/socks5_dns_query.py" -x ${SOCKS5_PROXY} --dns-server 127.0.0.1 g3proxy.local httpbin.local -v
SOCKS4_PROXY="socks4a://127.0.0.1:1080"
test_socks4_proxy_http


SOCKS5_PROXY="socks5h://127.0.0.1:1081"
SOCKS4_PROXY="socks4a://127.0.0.1:1081"
test_socks5_proxy_http
test_socks5_proxy_dns

python3 "${PROJECT_DIR}/g3proxy/ci/python3+curl/test_httpbin.py" -x ${SOCKS5_PROXY} -T http://httpbin.local
python3 "${PROJECT_DIR}/g3proxy/ci/python3+curl/test_httpbin.py" -x ${SOCKS4_PROXY} -T http://httpbin.local

python3 "${PROJECT_DIR}/g3proxy/ci/python3+requests/test_httpbin.py" -x ${SOCKS5_PROXY} -T http://httpbin.local

python3 "${PROJECT_DIR}/scripts/test/socks5_dns_query.py" -x ${SOCKS5_PROXY} --dns-server 127.0.0.1 g3proxy.local httpbin.local -v
SOCKS4_PROXY="socks4a://127.0.0.1:1081"
test_socks4_proxy_http
Loading
Loading