Tesing and fix for disconnected socket reads

Adding a unit test and fix for when the input file is derived from a socket
that's never been connected. I'm suspicious that this won't catch disconnects
that accur a little while into the socket's use, but I'll need to implement
integration tests for that. Guess that's next...
This commit is contained in:
Damian Johnson 2011-10-12 09:37:49 -07:00
parent 05b106bb57
commit c74ac0f7f0
2 changed files with 18 additions and 2 deletions

View File

@ -19,6 +19,7 @@ Version - Tor versioning information.
"""
import re
import socket
from stem.util import log
@ -51,7 +52,9 @@ def read_message(control_file):
parsed_content, raw_content = [], ""
while True:
line = control_file.readline()
try: line = control_file.readline()
except socket.error, exc: raise ControlSocketClosed(exc)
raw_content += line
# Parses the tor control lines. These are of the form...
@ -85,7 +88,9 @@ def read_message(control_file):
# get a line with just a period
while True:
line = control_file.readline()
try: line = control_file.readline()
except socket.error, exc: raise ControlSocketClosed(exc)
raw_content += line
if not line.endswith("\r\n"):

View File

@ -2,6 +2,7 @@
Unit tests for the types.ControlMessage parsing and class.
"""
import socket
import StringIO
import unittest
import stem.types
@ -147,6 +148,16 @@ class TestMessageFunctions(unittest.TestCase):
self.assert_message_parses(removal_test_input)
self.assert_message_parses(replacement_test_input)
def test_disconnected_socket(self):
"""
Tests when the read function is given a file derived from a disconnected
socket.
"""
control_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
control_socket_file = control_socket.makefile()
self.assertRaises(stem.types.ControlSocketClosed, stem.types.read_message, control_socket_file)
def assert_message_parses(self, controller_reply):
"""
Performs some basic sanity checks that a reply mirrors its parsed result.