22Unit tests for TCP connection matcher helpers.
33"""
44
5- import pytest
65from localstack_extensions .utils .tcp_protocol_detector import (
76 create_prefix_matcher ,
87 create_signature_matcher ,
98 create_custom_matcher ,
109 combine_matchers ,
1110)
11+ from localstack_extensions .utils .docker import ProxiedDockerContainerExtension
12+ from werkzeug .datastructures import Headers
1213
1314
1415class TestMatcherFactories :
@@ -26,13 +27,13 @@ def test_create_prefix_matcher(self):
2627 def test_create_signature_matcher (self ):
2728 """Test creating a signature matcher with offset."""
2829 # Match signature at offset 4
29- matcher = create_signature_matcher (b"\xAA \xBB " , offset = 4 )
30+ matcher = create_signature_matcher (b"\xaa \xbb " , offset = 4 )
3031
31- assert matcher (b"\x00 \x00 \x00 \x00 \xAA \xBB \xCC " )
32- assert matcher (b"\x00 \x00 \x00 \x00 \xAA \xBB " )
33- assert not matcher (b"\xAA \xBB \xCC " ) # Wrong offset
34- assert not matcher (b"\x00 \x00 \x00 \x00 \xCC \xDD " ) # Wrong signature
35- assert not matcher (b"\x00 \x00 \x00 \x00 \xAA " ) # Incomplete
32+ assert matcher (b"\x00 \x00 \x00 \x00 \xaa \xbb \xcc " )
33+ assert matcher (b"\x00 \x00 \x00 \x00 \xaa \xbb " )
34+ assert not matcher (b"\xaa \xbb \xcc " ) # Wrong offset
35+ assert not matcher (b"\x00 \x00 \x00 \x00 \xcc \xdd " ) # Wrong signature
36+ assert not matcher (b"\x00 \x00 \x00 \x00 \xaa " ) # Incomplete
3637
3738 def test_create_custom_matcher (self ):
3839 """Test creating a custom matcher."""
@@ -42,8 +43,8 @@ def my_check(data):
4243
4344 matcher = create_custom_matcher (my_check )
4445
45- assert matcher (b"\x00 \x00 \x00 \x00 \x00 \xFF " )
46- assert matcher (b"\x00 \x00 \x00 \x00 \x00 \xFF \xFF " )
46+ assert matcher (b"\x00 \x00 \x00 \x00 \x00 \xff " )
47+ assert matcher (b"\x00 \x00 \x00 \x00 \x00 \xff \xff " )
4748 assert not matcher (b"\x00 \x00 \x00 \x00 \x00 \x00 " )
4849 assert not matcher (b"\x00 \x00 \x00 \x00 \x00 " ) # Too short
4950
@@ -88,16 +89,14 @@ def test_matcher_with_extra_data(self):
8889 matcher = create_prefix_matcher (b"PREFIX" )
8990
9091 # Should match even with lots of extra data
91- assert matcher (b"PREFIX" + b"\xFF " * 1000 )
92+ assert matcher (b"PREFIX" + b"\xff " * 1000 )
9293
9394
9495class TestRealWorldUsage :
9596 """Tests for real-world usage patterns."""
9697
9798 def test_extension_with_custom_protocol_matcher (self ):
9899 """Test using custom matchers in an extension context."""
99- from localstack_extensions .utils .docker import ProxiedDockerContainerExtension
100- from werkzeug .datastructures import Headers
101100
102101 class CustomProtocolExtension (ProxiedDockerContainerExtension ):
103102 name = "custom"
@@ -111,7 +110,7 @@ def __init__(self):
111110
112111 def tcp_connection_matcher (self , data : bytes ) -> bool :
113112 # Match custom protocol with magic bytes at offset 4
114- matcher = create_signature_matcher (b"\xDE \xAD \xBE \xEF " , offset = 4 )
113+ matcher = create_signature_matcher (b"\xde \xad \xbe \xef " , offset = 4 )
115114 return matcher (data )
116115
117116 def should_proxy_request (self , headers : Headers ) -> bool :
@@ -121,16 +120,14 @@ def should_proxy_request(self, headers: Headers) -> bool:
121120 assert hasattr (extension , "tcp_connection_matcher" )
122121
123122 # Test the matcher
124- valid_data = b"\x00 \x00 \x00 \x00 \xDE \xAD \xBE \xEF \xFF "
123+ valid_data = b"\x00 \x00 \x00 \x00 \xde \xad \xbe \xef \xff "
125124 assert extension .tcp_connection_matcher (valid_data )
126125
127- invalid_data = b"\x00 \x00 \x00 \x00 \xFF \xFF \xFF \xFF "
126+ invalid_data = b"\x00 \x00 \x00 \x00 \xff \xff \xff \xff "
128127 assert not extension .tcp_connection_matcher (invalid_data )
129128
130129 def test_extension_with_combined_matchers (self ):
131130 """Test using combined matchers in an extension."""
132- from localstack_extensions .utils .docker import ProxiedDockerContainerExtension
133- from werkzeug .datastructures import Headers
134131
135132 class MultiProtocolExtension (ProxiedDockerContainerExtension ):
136133 name = "multi-protocol"
@@ -160,8 +157,6 @@ def should_proxy_request(self, headers: Headers) -> bool:
160157
161158 def test_extension_with_inline_matcher (self ):
162159 """Test using an inline matcher function."""
163- from localstack_extensions .utils .docker import ProxiedDockerContainerExtension
164- from werkzeug .datastructures import Headers
165160
166161 class InlineMatcherExtension (ProxiedDockerContainerExtension ):
167162 name = "inline"
@@ -175,11 +170,7 @@ def __init__(self):
175170
176171 def tcp_connection_matcher (self , data : bytes ) -> bool :
177172 # Inline custom logic without helper functions
178- return (
179- len (data ) >= 8
180- and data .startswith (b"MAGIC" )
181- and data [7 ] == 0x42
182- )
173+ return len (data ) >= 8 and data .startswith (b"MAGIC" ) and data [7 ] == 0x42
183174
184175 def should_proxy_request (self , headers : Headers ) -> bool :
185176 return False
0 commit comments