aboutsummaryrefslogtreecommitdiffstats
path: root/test/test_socks.py
diff options
context:
space:
mode:
authorMaximilian Hils <git@maximilianhils.com>2014-06-25 20:31:28 +0200
committerMaximilian Hils <git@maximilianhils.com>2014-06-25 20:31:28 +0200
commit6405595ae8593a52f6b81d7f311044f113476d82 (patch)
treed7c08efdfdbe46a8a7fcbd8791c454115440d19f /test/test_socks.py
parentdc3d3e5f0a8c4de734187c39888af5fbdb63d8a0 (diff)
downloadmitmproxy-6405595ae8593a52f6b81d7f311044f113476d82.tar.gz
mitmproxy-6405595ae8593a52f6b81d7f311044f113476d82.tar.bz2
mitmproxy-6405595ae8593a52f6b81d7f311044f113476d82.zip
socks module: polish, add tests
Diffstat (limited to 'test/test_socks.py')
-rw-r--r--test/test_socks.py55
1 files changed, 55 insertions, 0 deletions
diff --git a/test/test_socks.py b/test/test_socks.py
new file mode 100644
index 00000000..3771df62
--- /dev/null
+++ b/test/test_socks.py
@@ -0,0 +1,55 @@
+from cStringIO import StringIO
+import socket
+from netlib import socks, utils
+import tutils
+
+
+def test_client_greeting():
+ raw = StringIO("\x05\x02\x00\xBE\xEF")
+ out = StringIO()
+ msg = socks.ClientGreeting.from_file(raw)
+ msg.to_file(out)
+
+ assert out.getvalue() == raw.getvalue()[:-1]
+ assert msg.ver == 5
+ assert len(msg.methods) == 2
+ assert 0xBE in msg.methods
+ assert 0xEF not in msg.methods
+
+
+def test_server_greeting():
+ raw = StringIO("\x05\x02")
+ out = StringIO()
+ msg = socks.ServerGreeting.from_file(raw)
+ msg.to_file(out)
+
+ assert out.getvalue() == raw.getvalue()
+ assert msg.ver == 5
+ assert msg.method == 0x02
+
+
+def test_message():
+ raw = StringIO("\x05\x01\x00\x03\x0bexample.com\xDE\xAD\xBE\xEF")
+ out = StringIO()
+ msg = socks.Message.from_file(raw)
+ assert raw.read(2) == "\xBE\xEF"
+ msg.to_file(out)
+
+ assert out.getvalue() == raw.getvalue()[:-2]
+ assert msg.ver == 5
+ assert msg.msg == 0x01
+ assert msg.atyp == 0x03
+ assert msg.addr == ("example.com", 0xDEAD)
+
+ # Test ATYP=0x01 (IPV4)
+ raw = StringIO("\x05\x01\x00\x01\x7f\x00\x00\x01\xDE\xAD\xBE\xEF")
+ msg = socks.Message.from_file(raw)
+ assert raw.read(2) == "\xBE\xEF"
+ assert msg.addr == ("127.0.0.1", 0xDEAD)
+
+ # Test ATYP=0x04 (IPV6)
+ ipv6_addr = "2001:0db8:85a3:08d3:1319:8a2e:0370:7344"
+ raw = StringIO("\x05\x01\x00\x04" + utils.inet_pton(socket.AF_INET6, ipv6_addr) + "\xDE\xAD\xBE\xEF")
+ msg = socks.Message.from_file(raw)
+ assert raw.read(2) == "\xBE\xEF"
+ assert msg.addr.host == ipv6_addr \ No newline at end of file