def testBuffer(self): b = StringIOWithoutClosing() a = http.HTTPChannel() a.requestFactory = DummyHTTPHandler a.makeConnection(protocol.FileWrapper(b)) # one byte at a time, to stress it. for byte in self.requests: a.dataReceived(byte) a.connectionLost(IOError("all one")) value = b.getvalue() if value != self.expected_response: for i in range(len(value)): if len(self.expected_response) <= i: print ` value[i - 5:i + 10] `, ` self.expected_response[i - 5:i + 10] ` elif value[i] != self.expected_response[i]: print ` value[i - 5:i + 10] `, ` self.expected_response[i - 5:i + 10] ` break print '---VALUE---' print repr(value) print '---EXPECTED---' print repr(self.expected_response) raise AssertionError
def test_lineReceived(self): req = "0123456789abcdefghijk" ans = "dummypacketmaker012345678Dummy_abcde\r\n" s = SIOWOC() self.p.makeConnection(protocol.FileWrapper(s)) self.p.lineReceived(req) self.assertEquals(ans, s.getvalue())
class SMTPClientTestCase(unittest.TestCase): expected_output='''\ HELO foo.baz\r MAIL FROM:<*****@*****.**>\r RCPT TO:<*****@*****.**>\r DATA\r Subject: hello\r \r Goodbye\r .\r QUIT\r ''' def setUp(self): self.output = StringIOWithoutClosing() self.transport = protocols.protocol.FileWrapper(self.output) def testMessages(self): protocol = MySMTPClient() protocol.makeConnection(self.transport) protocol.lineReceived('220 hello') protocol.lineReceived('250 nice to meet you') protocol.lineReceived('250 great') protocol.lineReceived('250 great') protocol.lineReceived('354 go on, lad') protocol.lineReceived('250 gotcha') protocol.lineReceived('221 see ya around') if self.output.getvalue() != self.expected_output: raise AssertionError(`self.output.getvalue()`)
def testEmptyLineSyntaxError(self): proto = smtp.SMTP() output = StringIOWithoutClosing() transport = internet.protocol.FileWrapper(output) proto.makeConnection(transport) proto.lineReceived('') proto.setTimeout(None) out = output.getvalue().splitlines() self.assertEquals(len(out), 2) self.failUnless(out[0].startswith('220')) self.assertEquals(out[1], "500 Error: bad syntax")
def testNoop(self): a = StringIOWithoutClosing() dummy = DummyPOP3() dummy.makeConnection(protocol.FileWrapper(a)) p = pop3.POP3() lines = ['APOP spiv dummy', 'NOOP', 'QUIT'] expected_output = '+OK <moshez>\r\n+OK Authentication succeeded\r\n+OK \r\n+OK \r\n' for line in lines: dummy.lineReceived(line) self.failUnlessEqual(expected_output, a.getvalue(), "\nExpected:\n%s\nResults:\n%s\n" % (expected_output, a.getvalue()))
class POP3TestCase(unittest.TestCase): message = '''\ Subject: urgent Someone set up us the bomb! ''' expectedOutput = '''\ +OK <moshez>\015 +OK Authentication succeeded\015 +OK \015 1 0\015 .\015 +OK %d\015 Subject: urgent\015 \015 Someone set up us the bomb!\015 .\015 +OK \015 ''' % len(message) def setUp(self): self.factory = internet.protocol.Factory() self.factory.domains = {} self.factory.domains['baz.com'] = DummyDomain() self.factory.domains['baz.com'].addUser('hello') self.factory.domains['baz.com'].addMessage('hello', self.message) def testMessages(self): self.output = StringIOWithoutClosing() self.transport = internet.protocol.FileWrapper(self.output) protocol = MyVirtualPOP3() protocol.makeConnection(self.transport) protocol.service = self.factory protocol.lineReceived('APOP [email protected] world') protocol.lineReceived('UIDL') protocol.lineReceived('RETR 1') protocol.lineReceived('QUIT') if self.output.getvalue() != self.expectedOutput: #print `self.output.getvalue()` #print `self.expectedOutput` raise AssertionError(self.output.getvalue(), self.expectedOutput) def testLoopback(self): protocol = MyVirtualPOP3() protocol.service = self.factory clientProtocol = MyPOP3Downloader() loopback.loopback(protocol, clientProtocol) self.failUnlessEqual(clientProtocol.message, self.message)
def test_buffer(self): """ Send requests over a channel and check responses match what is expected. """ b = StringIOWithoutClosing() a = http.HTTPChannel() a.requestFactory = DummyHTTPHandler a.makeConnection(protocol.FileWrapper(b)) # one byte at a time, to stress it. for byte in self.requests: a.dataReceived(byte) a.connectionLost(IOError("all one")) value = b.getvalue() self.assertEquals(value, self.expected_response)
def testMessages(self): self.output = StringIOWithoutClosing() self.transport = protocols.protocol.FileWrapper(self.output) protocol = MyVirtualPOP3() protocol.makeConnection(self.transport) protocol.factory = self.factory protocol.lineReceived('APOP [email protected] world') protocol.lineReceived('UIDL') protocol.lineReceived('RETR 1') protocol.lineReceived('QUIT') if self.output.getvalue() != self.expectedOutput: print `self.output.getvalue()` print `self.expectedOutput` raise AssertionError(self.output.getvalue(), self.expectedOutput)
def testBuffer(self): output = StringIOWithoutClosing() a = self.serverClass() class fooFactory: domain = 'foo.com' a.factory = fooFactory() a.makeConnection(protocol.FileWrapper(output)) for (send, expect, msg, msgexpect) in self.data: if send: a.dataReceived(send) data = output.getvalue() output.truncate(0) if not re.match(expect, data): raise AssertionError, (send, expect, data) if data[:3] == '354': for line in msg.splitlines(): if line and line[0] == '.': line = '.' + line a.dataReceived(line + '\r\n') a.dataReceived('.\r\n') # Special case for DATA. Now we want a 250, and then # we compare the messages data = output.getvalue() output.truncate() resp, msgdata = msgexpect if not re.match(resp, data): raise AssertionError, (resp, data) for recip in msgdata[2]: expected = list(msgdata[:]) expected[2] = [recip] self.assertEquals(a.message[(recip, )], tuple(expected)) a.setTimeout(None)
def testDeferredChat(self): factory = postfix.PostfixTCPMapDeferringDictServerFactory(self.data) output = StringIOWithoutClosing() transport = internet.protocol.FileWrapper(output) protocol = postfix.PostfixTCPMapServer() protocol.service = factory protocol.factory = factory protocol.makeConnection(transport) for input, expected_output in self.chat: protocol.lineReceived(input) self.assertEquals(output.getvalue(), expected_output, 'For %r, expected %r but got %r' % ( input, expected_output, output.getvalue() )) output.truncate(0) protocol.setTimeout(None)
def testBuffer(self): output = StringIOWithoutClosing() a = self.serverClass() class fooFactory: domain = 'foo.com' a.factory = fooFactory() a.makeConnection(protocol.FileWrapper(output)) for (send, expect, msg, msgexpect) in self.data: if send: a.dataReceived(send) data = output.getvalue() output.truncate(0) if not re.match(expect, data): raise AssertionError, (send, expect, data) if data[:3] == '354': for line in msg.splitlines(): if line and line[0] == '.': line = '.' + line a.dataReceived(line + '\r\n') a.dataReceived('.\r\n') # Special case for DATA. Now we want a 250, and then # we compare the messages data = output.getvalue() output.truncate() resp, msgdata = msgexpect if not re.match(resp, data): raise AssertionError, (resp, data) self.assertEquals(a.message, msgdata) # a.transport.loseConnection() # reactor.iterate() a.timeoutID.cancel()
def testBuffer(self): a = StringIOWithoutClosing() dummy = DummyPOP3() dummy.makeConnection(protocol.FileWrapper(a)) lines = string.split('''\ APOP moshez dummy LIST UIDL RETR 1 RETR 2 DELE 1 RETR 1 QUIT''', '\n') expected_output = '+OK <moshez>\r\n+OK Authentication succeeded\r\n+OK 1\r\n1 44\r\n.\r\n+OK \r\n1 0\r\n.\r\n+OK 44\r\nFrom: moshe\r\nTo: moshe\r\n\r\nHow are you, friend?\r\n.\r\n-ERR index out of range\r\n+OK \r\n-ERR message deleted\r\n+OK \r\n' for line in lines: dummy.lineReceived(line) self.failUnlessEqual(expected_output, a.getvalue(), "\nExpected:\n%s\nResults:\n%s\n" % (expected_output, a.getvalue()))
class POP3TestCase(unittest.TestCase): message = '''\ Subject: urgent Someone set up us the bomb! ''' expectedOutput = '''\ +OK <moshez>\015 +OK \015 +OK \015 1 0\015 .\015 +OK %d\015 Subject: urgent\015 \015 Someone set up us the bomb!\015 .\015 +OK \015 ''' % len(message) def setUp(self): self.factory = protocols.protocol.Factory() self.factory.domains = {} self.factory.domains['baz.com'] = DummyDomain() self.factory.domains['baz.com'].addUser('hello') self.factory.domains['baz.com'].addMessage('hello', self.message) def testMessages(self): self.output = StringIOWithoutClosing() self.transport = protocols.protocol.FileWrapper(self.output) protocol = MyVirtualPOP3() protocol.makeConnection(self.transport) protocol.factory = self.factory protocol.lineReceived('APOP [email protected] world') protocol.lineReceived('UIDL') protocol.lineReceived('RETR 1') protocol.lineReceived('QUIT') if self.output.getvalue() != self.expectedOutput: print `self.output.getvalue()` print `self.expectedOutput` raise AssertionError(self.output.getvalue(), self.expectedOutput)
def testBuffer(self): b = StringIOWithoutClosing() a = http.HTTPChannel() a.requestFactory = DummyHTTPHandler a.makeConnection(protocol.FileWrapper(b)) # one byte at a time, to stress it. for byte in self.requests: a.dataReceived(byte) a.connectionLost(IOError("all one")) value = b.getvalue() if value != self.expected_response: for i in range(len(value)): if len(self.expected_response) <= i: print `value[i-5:i+10]`, `self.expected_response[i-5:i+10]` elif value[i] != self.expected_response[i]: print `value[i-5:i+10]`, `self.expected_response[i-5:i+10]` break print '---VALUE---' print repr(value) print '---EXPECTED---' print repr(self.expected_response) raise AssertionError
def testMessages(self): self.output = StringIOWithoutClosing() self.transport = internet.protocol.FileWrapper(self.output) protocol = MyVirtualPOP3() protocol.makeConnection(self.transport) protocol.service = self.factory protocol.lineReceived('APOP [email protected] world') protocol.lineReceived('UIDL') protocol.lineReceived('RETR 1') protocol.lineReceived('QUIT') if self.output.getvalue() != self.expectedOutput: #print `self.output.getvalue()` #print `self.expectedOutput` raise AssertionError(self.output.getvalue(), self.expectedOutput)
def runRequest(self, httpRequest, requestClass, success=1): httpRequest = httpRequest.replace("\n", "\r\n") b = StringIOWithoutClosing() a = http.HTTPChannel() a.requestFactory = requestClass a.makeConnection(protocol.FileWrapper(b)) # one byte at a time, to stress it. for byte in httpRequest: if a.transport.closed: break a.dataReceived(byte) a.connectionLost(IOError("all done")) if success: self.assertEquals(self.didRequest, 1) del self.didRequest else: self.assert_(not hasattr(self, "didRequest"))
def testDeferredChat(self): factory = postfix.PostfixTCPMapDeferringDictServerFactory(self.data) output = StringIOWithoutClosing() transport = internet.protocol.FileWrapper(output) protocol = postfix.PostfixTCPMapServer() protocol.service = factory protocol.factory = factory protocol.makeConnection(transport) for input, expected_output in self.chat: protocol.lineReceived(input) # self.runReactor(1) self.assertEquals(output.getvalue(), expected_output, 'For %r, expected %r but got %r' % ( input, expected_output, output.getvalue() )) output.truncate(0) protocol.setTimeout(None)
def testBuffer(self): output = StringIOWithoutClosing() a = self.serverClass() class fooFactory: domain = 'foo.com' a.factory = fooFactory() a.makeConnection(protocol.FileWrapper(output)) for (send, expect, msg, msgexpect) in self.data: if send: a.dataReceived(send) data = output.getvalue() output.truncate(0) if not re.match(expect, data): raise AssertionError, (send, expect, data) if data[:3] == '354': for line in msg.splitlines(): if line and line[0] == '.': line = '.' + line a.dataReceived(line + '\r\n') a.dataReceived('.\r\n') # Special case for DATA. Now we want a 250, and then # we compare the messages data = output.getvalue() output.truncate() resp, msgdata = msgexpect if not re.match(resp, data): raise AssertionError, (resp, data) for recip in msgdata[2]: expected = list(msgdata[:]) expected[2] = [recip] self.assertEquals( a.message[(recip,)], tuple(expected) ) a.setTimeout(None)
def test_sendAnswerUnicode(self): s = SIOWOC() self.p.makeConnection(protocol.FileWrapper(s)) self.p.sendAnswer(u"test") self.assertEquals("dummypacketmaker012345678test\r\n", s.getvalue())
def test_sendLineStr(self): s = SIOWOC() self.p.makeConnection(protocol.FileWrapper(s)) self.p.sendLine("test") self.assertEquals("test\r\n", s.getvalue())
def test_sendAnswerUnicode(self): s = SIOWOC() self.p.makeConnection(protocol.FileWrapper(s)) self.p.sendAnswer(u"test") self.assertEquals("\xd4\xc5\xd3\xd4test\r\n", s.getvalue())
def setUp(self): self.factory = smtp.SMTPFactory() self.factory.domains = {} self.factory.domains['baz.com'] = DummyDomain(['foo']) self.output = StringIOWithoutClosing() self.transport = internet.protocol.FileWrapper(self.output)
def setUp(self): self.output = StringIOWithoutClosing() self.transport = protocols.protocol.FileWrapper(self.output)