Skip to content

Commit 544bc39

Browse files
committed
feat: Add unittest coverage
1 parent b841419 commit 544bc39

6 files changed

Lines changed: 262 additions & 0 deletions

File tree

.github/workflows/ci.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,3 +51,7 @@ jobs:
5151
run: |
5252
python main.py --help
5353
python main.py --version
54+
55+
- name: Unit tests
56+
run: |
57+
python -m unittest discover -s tests -v

tests/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
"""Test package for MasterHttpRelayVPN."""

tests/test_adblock.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import pathlib
2+
import sys
3+
import unittest
4+
5+
6+
ROOT = pathlib.Path(__file__).resolve().parents[1]
7+
SRC = ROOT / "src"
8+
if str(SRC) not in sys.path:
9+
sys.path.insert(0, str(SRC))
10+
11+
from core.adblock import parse_hosts_text
12+
13+
14+
class ParseHostsTextTests(unittest.TestCase):
15+
def test_parses_hosts_and_bare_domains_and_deduplicates(self):
16+
text = """
17+
# comment
18+
0.0.0.0 ads.example.com
19+
127.0.0.1 tracker.example.com # inline comment
20+
plain.example.org
21+
ads.example.com
22+
EXAMPLE.NET.
23+
"""
24+
25+
self.assertEqual(
26+
parse_hosts_text(text),
27+
[
28+
"ads.example.com",
29+
"tracker.example.com",
30+
"plain.example.org",
31+
"example.net",
32+
],
33+
)
34+
35+
def test_skips_invalid_reserved_and_wildcard_entries(self):
36+
text = """
37+
localhost
38+
localhost.localdomain
39+
192.168.1.1
40+
::1
41+
analytics-*.example.com
42+
invalid
43+
bad_domain.example
44+
host name.example.com
45+
"""
46+
47+
self.assertEqual(parse_hosts_text(text), [])
48+
49+
50+
if __name__ == "__main__":
51+
unittest.main()

tests/test_codec.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import gzip
2+
import pathlib
3+
import sys
4+
import unittest
5+
import zlib
6+
7+
8+
ROOT = pathlib.Path(__file__).resolve().parents[1]
9+
SRC = ROOT / "src"
10+
if str(SRC) not in sys.path:
11+
sys.path.insert(0, str(SRC))
12+
13+
from core import codec
14+
15+
16+
class CodecTests(unittest.TestCase):
17+
def test_supported_encodings_always_include_gzip_and_deflate(self):
18+
encodings = codec.supported_encodings()
19+
self.assertIn("gzip", encodings)
20+
self.assertIn("deflate", encodings)
21+
22+
def test_decode_passes_through_unknown_empty_and_invalid_payloads(self):
23+
raw = b"plain-body"
24+
self.assertIs(codec.decode(raw, ""), raw)
25+
self.assertIs(codec.decode(raw, "identity"), raw)
26+
self.assertIs(codec.decode(raw, "unknown"), raw)
27+
self.assertEqual(codec.decode(raw, "gzip"), raw)
28+
29+
def test_decode_gzip_and_deflate(self):
30+
raw = b"hello through compression"
31+
gzip_body = gzip.compress(raw)
32+
zlib_body = zlib.compress(raw)
33+
raw_deflate_body = zlib.compress(raw, wbits=-zlib.MAX_WBITS)
34+
35+
self.assertEqual(codec.decode(gzip_body, "gzip"), raw)
36+
self.assertEqual(codec.decode(zlib_body, "deflate"), raw)
37+
self.assertEqual(codec.decode(raw_deflate_body, "deflate"), raw)
38+
39+
40+
if __name__ == "__main__":
41+
unittest.main()

tests/test_fronting_support.py

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import pathlib
2+
import sys
3+
import unittest
4+
5+
6+
ROOT = pathlib.Path(__file__).resolve().parents[1]
7+
SRC = ROOT / "src"
8+
if str(SRC) not in sys.path:
9+
sys.path.insert(0, str(SRC))
10+
11+
from core.constants import FRONT_SNI_POOL_GOOGLE
12+
from relay.fronting_support import (
13+
build_sni_pool,
14+
parse_content_range,
15+
validate_range_response,
16+
)
17+
18+
19+
class BuildSniPoolTests(unittest.TestCase):
20+
def test_uses_explicit_overrides_with_normalization_and_deduplication(self):
21+
result = build_sni_pool(
22+
"www.google.com",
23+
[" Mail.Google.com ", "mail.google.com", "accounts.google.com."],
24+
)
25+
self.assertEqual(result, ["mail.google.com", "accounts.google.com"])
26+
27+
def test_google_front_domain_uses_google_pool(self):
28+
result = build_sni_pool("www.google.com", None)
29+
self.assertEqual(result, list(FRONT_SNI_POOL_GOOGLE))
30+
31+
def test_non_google_front_domain_falls_back_to_single_host(self):
32+
self.assertEqual(build_sni_pool("cdn.example.com", None), ["cdn.example.com"])
33+
self.assertEqual(build_sni_pool("", None), ["www.google.com"])
34+
35+
36+
class RangeParsingTests(unittest.TestCase):
37+
def test_parse_content_range_accepts_valid_values(self):
38+
self.assertEqual(parse_content_range("bytes 10-19/20"), (10, 19, 20))
39+
40+
def test_parse_content_range_rejects_invalid_values(self):
41+
self.assertIsNone(parse_content_range(""))
42+
self.assertIsNone(parse_content_range("bytes 10-19/*"))
43+
self.assertIsNone(parse_content_range("bytes 10-9/20"))
44+
self.assertIsNone(parse_content_range("bytes 10-19/19"))
45+
46+
def test_validate_range_response_success_and_failures(self):
47+
headers = {"content-range": "bytes 10-13/20"}
48+
body = b"data"
49+
50+
self.assertIsNone(validate_range_response(206, headers, body, 10, 13, 20))
51+
self.assertEqual(
52+
validate_range_response(200, headers, body, 10, 13, 20),
53+
"status 200",
54+
)
55+
self.assertEqual(
56+
validate_range_response(206, {"content-range": "bytes 11-13/20"}, body, 10, 13, 20),
57+
"Content-Range mismatch 11-13",
58+
)
59+
self.assertEqual(
60+
validate_range_response(206, headers, b"abc", 10, 13, 20),
61+
"short chunk 3/4 B",
62+
)
63+
64+
65+
if __name__ == "__main__":
66+
unittest.main()

tests/test_proxy_support.py

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
import pathlib
2+
import sys
3+
import unittest
4+
5+
6+
ROOT = pathlib.Path(__file__).resolve().parents[1]
7+
SRC = ROOT / "src"
8+
if str(SRC) not in sys.path:
9+
sys.path.insert(0, str(SRC))
10+
11+
from core.constants import CACHE_TTL_STATIC_LONG
12+
from proxy.proxy_support import (
13+
ResponseCache,
14+
has_unsupported_transfer_encoding,
15+
host_matches_rules,
16+
is_ip_literal,
17+
load_host_rules,
18+
parse_content_length,
19+
)
20+
21+
22+
class ProxySupportTests(unittest.TestCase):
23+
def test_is_ip_literal_handles_ipv4_ipv6_and_hostnames(self):
24+
self.assertTrue(is_ip_literal("127.0.0.1"))
25+
self.assertTrue(is_ip_literal("[2001:db8::1]"))
26+
self.assertFalse(is_ip_literal("example.com"))
27+
28+
def test_load_host_rules_and_match_suffixes(self):
29+
rules = load_host_rules(["Example.com", ".example.org", "api.example.net."])
30+
self.assertTrue(host_matches_rules("example.com", rules))
31+
self.assertTrue(host_matches_rules("sub.example.org", rules))
32+
self.assertTrue(host_matches_rules("api.example.net", rules))
33+
self.assertFalse(host_matches_rules("example.org", rules))
34+
self.assertFalse(host_matches_rules("other.test", rules))
35+
36+
def test_parse_content_length_uses_exact_header_name(self):
37+
headers = (
38+
b"HTTP/1.1 200 OK\r\n"
39+
b"X-Content-Length: 999\r\n"
40+
b"Content-Length: 42\r\n\r\n"
41+
)
42+
self.assertEqual(parse_content_length(headers), 42)
43+
self.assertEqual(parse_content_length(b"HTTP/1.1 200 OK\r\n\r\n"), 0)
44+
45+
def test_has_unsupported_transfer_encoding(self):
46+
self.assertFalse(
47+
has_unsupported_transfer_encoding(
48+
b"Transfer-Encoding: identity\r\n\r\n"
49+
)
50+
)
51+
self.assertTrue(
52+
has_unsupported_transfer_encoding(
53+
b"Transfer-Encoding: chunked\r\n\r\n"
54+
)
55+
)
56+
self.assertTrue(
57+
has_unsupported_transfer_encoding(
58+
b"Transfer-Encoding: gzip, chunked\r\n\r\n"
59+
)
60+
)
61+
62+
63+
class ResponseCacheParseTtlTests(unittest.TestCase):
64+
def test_static_asset_uses_long_ttl(self):
65+
raw = (
66+
b"HTTP/1.1 200 OK\r\n"
67+
b"Content-Type: image/png\r\n\r\n"
68+
b"body"
69+
)
70+
self.assertEqual(
71+
ResponseCache.parse_ttl(raw, "https://example.com/logo.png"),
72+
CACHE_TTL_STATIC_LONG,
73+
)
74+
75+
def test_private_no_store_and_set_cookie_disable_caching(self):
76+
private_resp = (
77+
b"HTTP/1.1 200 OK\r\n"
78+
b"Cache-Control: private, max-age=600\r\n\r\n"
79+
b"body"
80+
)
81+
no_store_resp = (
82+
b"HTTP/1.1 200 OK\r\n"
83+
b"Cache-Control: no-store\r\n\r\n"
84+
b"body"
85+
)
86+
cookie_resp = (
87+
b"HTTP/1.1 200 OK\r\n"
88+
b"Set-Cookie: sid=1\r\n"
89+
b"Content-Type: image/png\r\n\r\n"
90+
b"body"
91+
)
92+
93+
self.assertEqual(ResponseCache.parse_ttl(private_resp, "https://example.com/app.js"), 0)
94+
self.assertEqual(ResponseCache.parse_ttl(no_store_resp, "https://example.com/app.js"), 0)
95+
self.assertEqual(ResponseCache.parse_ttl(cookie_resp, "https://example.com/logo.png"), 0)
96+
97+
98+
if __name__ == "__main__":
99+
unittest.main()

0 commit comments

Comments
 (0)