[tor-commits] [stem/master] Merging test.network and test.util

atagar at torproject.org atagar at torproject.org
Sun Apr 14 04:33:47 UTC 2013


commit e28971e61ec41ec76be74d9b703cd728d7ec0d0f
Author: Damian Johnson <atagar at torproject.org>
Date:   Fri Apr 12 08:40:16 2013 -0700

    Merging test.network and test.util
    
    Both modules are to support tests for the ONLINE target. The main difference is
    that one module was made by Ravi and the other by Sean. At some point we should
    truely unify their functionality, but for now sticking them in the same place.
---
 test/integ/control/controller.py |    7 +--
 test/network.py                  |   75 +++++++++++++++++++++++++++++++++++
 test/util.py                     |   80 --------------------------------------
 3 files changed, 78 insertions(+), 84 deletions(-)

diff --git a/test/integ/control/controller.py b/test/integ/control/controller.py
index 3cf09d9..64cbaa4 100644
--- a/test/integ/control/controller.py
+++ b/test/integ/control/controller.py
@@ -19,7 +19,6 @@ import stem.socket
 import stem.version
 import test.network
 import test.runner
-import test.util
 
 from stem import Flag, Signal
 from stem.control import EventType, State
@@ -788,8 +787,8 @@ class TestController(unittest.TestCase):
 
       s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
       s.connect(('127.0.0.1', int(controller.get_conf('SocksListenAddress').rsplit(':', 1)[1])))
-      test.util.negotiate_socks(s, '1.2.1.2', 80)
-      s.sendall(test.util.ip_request)  # make the http request for the ip address
+      test.network.negotiate_socks(s, '1.2.1.2', 80)
+      s.sendall(test.network.ip_request)  # make the http request for the ip address
       response = s.recv(1000)
 
       # everything after the blank line is the 'data' in a HTTP response.
@@ -974,7 +973,7 @@ class TestController(unittest.TestCase):
         circuit_id = controller.new_circuit(await_build = True)
         socksport = controller.get_socks_listeners()[0][1]
 
-        ip = test.util.external_ip('127.0.0.1', socksport)
+        ip = test.network.external_ip('127.0.0.1', socksport)
         exit_circuit = controller.get_circuit(circuit_id)
         self.assertTrue(exit_circuit)
         exit_ip = controller.get_network_status(exit_circuit.path[2][0]).address
diff --git a/test/network.py b/test/network.py
index 2d4d9ba..df9f018 100644
--- a/test/network.py
+++ b/test/network.py
@@ -21,6 +21,7 @@ import socket
 import struct
 
 import stem.util.connection
+from stem import ProtocolError, SocketError
 
 # Store a reference to the original class so we can find it after
 # monkey patching.
@@ -32,6 +33,20 @@ SOCKS5_CONN_BY_IPV4 = (0x05, 0x01, 0x00, 0x01)
 SOCKS5_CONN_BY_NAME = (0x05, 0x01, 0x00, 0x03)
 
 
+error_msgs = {
+  0x5a: "SOCKS4A request granted",
+  0x5b: "SOCKS4A request rejected or failed",
+  0x5c: "SOCKS4A request failed because client is not running identd (or not reachable from the server)",
+  0x5d: "SOCKS4A request failed because client's identd could not confirm the user ID string in the request",
+}
+
+ip_request = """GET /ip HTTP/1.0
+Host: ifconfig.me
+Accept-Encoding: identity
+
+"""
+
+
 class ProxyError(Exception):
   "Base error for proxy issues."
 
@@ -253,3 +268,63 @@ class SocksPatch(object):
 
   def __exit__(self, exit_type, value, traceback):
     socket.socket = _socket_socket
+
+
+def external_ip(host, port):
+  """
+  Returns the externally visible IP address when using a SOCKS4a proxy.
+  Negotiates the socks connection, connects to ipconfig.me and requests
+  http://ifconfig.me/ip to find out the externally visible IP.
+
+  Supports only SOCKS4a proxies.
+
+  :param str host: hostname/IP of the proxy server
+  :param int port: port on which the proxy server is listening
+
+  :returns: externally visible IP address, or None if it isn't able to
+
+  :raises: :class:`stem.socket.SocketError`: unable to connect a socket to the socks server
+  """
+
+  try:
+    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
+    sock.connect((host, int(port)))
+  except Exception, exc:
+    raise SocketError("Failed to connect to the socks server: " + str(exc))
+
+  try:
+    negotiate_socks(sock, "ifconfig.me", 80)
+    sock.sendall(ip_request)
+    response = sock.recv(1000)
+
+    # everything after the blank line is the 'data' in a HTTP response
+    # The response data for our request for request should be an IP address + '\n'
+    return response[response.find("\r\n\r\n"):].strip()
+  except Exception, exc:
+    return None
+
+
+def negotiate_socks(sock, host, port):
+  """
+  Negotiate with a socks4a server. Closes the socket and raises an exception on
+  failure.
+
+  :param socket sock: socket connected to socks4a server
+  :param str host: hostname/IP to connect to
+  :param int port: port to connect to
+
+  :raises: :class:`stem.ProtocolError` if the socks server doesn't grant our request
+
+  :returns: a list with the IP address and the port that the proxy connected to
+  """
+
+  # SOCKS4a request here - http://en.wikipedia.org/wiki/SOCKS#Protocol
+  request = "\x04\x01" + struct.pack("!H", port) + "\x00\x00\x00\x01" + "\x00" + host + "\x00"
+  sock.sendall(request)
+  response = sock.recv(8)
+
+  if len(response) != 8 or response[0] != "\x00" or response[1] != "\x5a":
+    sock.close()
+    raise ProtocolError(error_msgs.get(response[1], "SOCKS server returned unrecognized error code"))
+
+  return [socket.inet_ntoa(response[4:]), struct.unpack("!H", response[2:4])[0]]
diff --git a/test/util.py b/test/util.py
deleted file mode 100644
index 467ca91..0000000
--- a/test/util.py
+++ /dev/null
@@ -1,80 +0,0 @@
-# Copyright 2012-2013, Damian Johnson
-# See LICENSE for licensing information
-
-import socket
-import struct
-
-from stem import ProtocolError, SocketError
-
-error_msgs = {
-  0x5a: "SOCKS4A request granted",
-  0x5b: "SOCKS4A request rejected or failed",
-  0x5c: "SOCKS4A request failed because client is not running identd (or not reachable from the server)",
-  0x5d: "SOCKS4A request failed because client's identd could not confirm the user ID string in the request",
-}
-
-ip_request = """GET /ip HTTP/1.0
-Host: ifconfig.me
-Accept-Encoding: identity
-
-"""
-
-
-def external_ip(host, port):
-  """
-  Returns the externally visible IP address when using a SOCKS4a proxy.
-  Negotiates the socks connection, connects to ipconfig.me and requests
-  http://ifconfig.me/ip to find out the externally visible IP.
-
-  Supports only SOCKS4a proxies.
-
-  :param str host: hostname/IP of the proxy server
-  :param int port: port on which the proxy server is listening
-
-  :returns: externally visible IP address, or None if it isn't able to
-
-  :raises: :class:`stem.socket.SocketError`: unable to connect a socket to the socks server
-  """
-
-  try:
-    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
-    sock.connect((host, int(port)))
-  except Exception, exc:
-    raise SocketError("Failed to connect to the socks server: " + str(exc))
-
-  try:
-    negotiate_socks(sock, "ifconfig.me", 80)
-    sock.sendall(ip_request)
-    response = sock.recv(1000)
-
-    # everything after the blank line is the 'data' in a HTTP response
-    # The response data for our request for request should be an IP address + '\n'
-    return response[response.find("\r\n\r\n"):].strip()
-  except Exception, exc:
-    return None
-
-
-def negotiate_socks(sock, host, port):
-  """
-  Negotiate with a socks4a server. Closes the socket and raises an exception on
-  failure.
-
-  :param socket sock: socket connected to socks4a server
-  :param str host: hostname/IP to connect to
-  :param int port: port to connect to
-
-  :raises: :class:`stem.ProtocolError` if the socks server doesn't grant our request
-
-  :returns: a list with the IP address and the port that the proxy connected to
-  """
-
-  # SOCKS4a request here - http://en.wikipedia.org/wiki/SOCKS#Protocol
-  request = "\x04\x01" + struct.pack("!H", port) + "\x00\x00\x00\x01" + "\x00" + host + "\x00"
-  sock.sendall(request)
-  response = sock.recv(8)
-
-  if len(response) != 8 or response[0] != "\x00" or response[1] != "\x5a":
-    sock.close()
-    raise ProtocolError(error_msgs.get(response[1], "SOCKS server returned unrecognized error code"))
-
-  return [socket.inet_ntoa(response[4:]), struct.unpack("!H", response[2:4])[0]]





More information about the tor-commits mailing list