Skip to content

Commit a7f6b30

Browse files
committed
Add more advanced test to test in2out / out2in handlers and
adjust class name to match.
1 parent 3c7d470 commit a7f6b30

2 files changed

Lines changed: 74 additions & 2 deletions

File tree

tests/AsyncProxy2FD_test.py

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
import socket
2+
import unittest
3+
from ctypes import string_at, memmove
4+
from libasyncproxy.AsyncProxy import AsyncProxy2FD
5+
6+
class NosyProxy(AsyncProxy2FD):
7+
def in2out(self, ptr, length):
8+
# Read original data from the pointer
9+
original = string_at(ptr, length)
10+
# Transform data to uppercase (note: transformation must retain length)
11+
transformed = original.upper()
12+
memmove(ptr, transformed, length)
13+
print("in2out hook: transformed", original, "to", transformed)
14+
15+
def out2in(self, ptr, length):
16+
# Read original data from the pointer
17+
original = string_at(ptr, length)
18+
# Reverse the data bytes (again, ensuring the length remains unchanged)
19+
transformed = original[::-1]
20+
memmove(ptr, transformed, length)
21+
print("out2in hook: transformed", original, "to", transformed)
22+
23+
class AsyncProxy2FDTest(unittest.TestCase):
24+
def test_AsyncProxy(self):
25+
# Create first socket pair:
26+
# - client_socket: acts as the client sending data.
27+
# - proxy_in: endpoint for the proxy from the client side.
28+
client_socket, proxy_in = socket.socketpair()
29+
30+
# Create second socket pair:
31+
# - proxy_out: endpoint for the proxy on the server side.
32+
# - server_socket: acts as the server receiving data.
33+
proxy_out, server_socket = socket.socketpair()
34+
35+
# Initialize the async proxy to connect the two endpoints
36+
proxy_fd = NosyProxy(proxy_in.fileno(), proxy_out.fileno())
37+
38+
# Start the asynchronous proxy worker.
39+
proxy_fd.start()
40+
41+
# Send a message from client to server.
42+
client_message = b"Hello from Client!"
43+
client_socket.sendall(client_message)
44+
print("Client sent:", client_message.decode())
45+
46+
server_received = server_socket.recv(1024)
47+
print("Server received:", server_received.decode())
48+
self.assertEqual(client_message.upper(), server_received)
49+
50+
# Now send a message from server back to client.
51+
server_message = b"Hello from Server!"
52+
server_socket.sendall(server_message)
53+
print("Server sent:", server_message.decode())
54+
55+
client_received = client_socket.recv(1024)
56+
print("Client received:", client_received.decode())
57+
self.assertEqual(server_message[::-1], client_received)
58+
59+
# Shutdown the proxy worker and cleanup.
60+
proxy_fd.join(shutdown=True)
61+
client_socket.close()
62+
proxy_in.close()
63+
proxy_out.close()
64+
server_socket.close()
65+
66+
def runme():
67+
unittest.main(module = __name__)
68+
69+
if __name__ == '__main__':
70+
runme()

tests/AsyncProxy_test.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,13 @@
2727

2828
from libasyncproxy.AsyncProxy import AsyncProxy, AsyncProxy2FD, setdebug
2929

30-
class PipelineTest(unittest.TestCase):
30+
class AsyncProxyTest(unittest.TestCase):
31+
debug = False
3132
def test_AsyncProxy(self):
3233
getnull = lambda: (open('/dev/null', 'r+'), open('/dev/null', 'r+'))
3334
getrandom = lambda: (open('/dev/urandom', 'r'), open('/dev/urandom', 'r'))
3435

35-
setdebug(2)
36+
if self.debug: setdebug(2)
3637

3738
dn = socketpair()
3839
a = AsyncProxy(dn[0].fileno(), 'gmail-smtp-in.l.google.com', 25, AF_INET, None)
@@ -73,4 +74,5 @@ def runme():
7374
unittest.main(module = __name__)
7475

7576
if __name__ == '__main__':
77+
AsyncProxyTest.debug = True
7678
runme()

0 commit comments

Comments
 (0)