def _versionTest(self, serverVersionResponse): """ Test L{NotificationClient} version negotiation. """ self.client.factory.userHandle = "foo" transport = StringTransport() self.client.makeConnection(transport) self.assertEquals( transport.value(), "VER 1 MSNP8 CVR0\r\n") transport.clear() self.client.dataReceived(serverVersionResponse) self.assertEquals( transport.value(), "CVR 2 0x0409 win 4.10 i386 MSNMSGR 5.0.0544 MSMSGS foo\r\n")
def connect(reactor, cc): d = cc.connectUNIX('/foo/bar') address, factory, timeout, bindAddress = reactor.unixClients.pop() protocol = factory.buildProtocol(None) transport = StringTransport() protocol.makeConnection(transport) return d
def testListSync(self): # currently this test does not take into account the fact # that BPRs sent as part of the SYN reply may not be interpreted # as such if they are for the last LST -- maybe I should # factor this in later. self.client.makeConnection(StringTransport()) msn.NotificationClient.loggedIn(self.client, '*****@*****.**', 'foobar', 1) lines = [ "SYN %s 100 1 1" % self.client.currentID, "GTC A", "BLP AL", "LSG 0 Other%20Contacts 0", "LST [email protected] Some%20Name 11 0" ] map(self.client.lineReceived, lines) contacts = self.client.factory.contacts contact = contacts.getContact('*****@*****.**') self.failUnless(contacts.version == 100, "Invalid contact list version") self.failUnless(contact.screenName == 'Some Name', "Invalid screen-name for user") self.failUnless(contacts.groups == {0: 'Other Contacts'}, "Did not get proper group list") self.failUnless(contact.groups == [0] and contact.lists == 11, "Invalid contact list/group info") self.failUnless(self.client.state == 'GOTLIST', "Failed to call list sync handler")
def connect(reactor, cc): d = cc.connectSSL('example.com', 1234, object()) host, port, factory, contextFactory, timeout, bindADdress = reactor.sslClients.pop( ) protocol = factory.buildProtocol(None) transport = StringTransport() protocol.makeConnection(transport) return d
def connect(reactor, cc): d = cc.connectTCP('example.com', 1234) host, port, factory, timeout, bindAddress = reactor.tcpClients.pop( ) protocol = factory.buildProtocol(None) transport = StringTransport() protocol.makeConnection(transport) return d
def _doLoginTest(self, response, headers): protocol = msn.PassportLogin(self.deferred, '*****@*****.**', 'testpass', 'https://foo.com/', 'a') protocol.makeConnection(StringTransport()) protocol.dataReceived(response) for (h, v) in headers.items(): protocol.dataReceived('%s: %s\r\n' % (h, v)) protocol.dataReceived('\r\n')
def test_challenge(self): """ L{NotificationClient} responds to a I{CHL} message by sending a I{QRY} back which included a hash based on the parameters of the I{CHL}. """ transport = StringTransport() self.client.makeConnection(transport) transport.clear() challenge = "15570131571988941333" self.client.dataReceived('CHL 0 ' + challenge + '\r\n') # md5 of the challenge and a magic string defined by the protocol response = "8f2f5a91b72102cd28355e9fc9000d6e" # Sanity check - the response is what the comment above says it is. self.assertEquals(response, md5(challenge + "Q1P7W2E4J9R8U3S5").hexdigest()) self.assertEquals( transport.value(), # 2 is the next transaction identifier. 32 is the length of the # response. "QRY 2 [email protected] 32\r\n" + response)
def test_challenge(self): """ L{NotificationClient} responds to a I{CHL} message by sending a I{QRY} back which included a hash based on the parameters of the I{CHL}. """ transport = StringTransport() self.client.makeConnection(transport) transport.clear() challenge = "15570131571988941333" self.client.dataReceived('CHL 0 ' + challenge + '\r\n') # md5 of the challenge and a magic string defined by the protocol response = "8f2f5a91b72102cd28355e9fc9000d6e" # Sanity check - the response is what the comment above says it is. self.assertEquals( response, md5(challenge + "Q1P7W2E4J9R8U3S5").hexdigest()) self.assertEquals( transport.value(), # 2 is the next transaction identifier. 32 is the length of the # response. "QRY 2 [email protected] 32\r\n" + response)
def testAsyncPhoneChange(self): c = msn.MSNContact(userHandle='*****@*****.**') self.client.factory.contacts = msn.MSNContactList() self.client.factory.contacts.addContact(c) self.client.makeConnection(StringTransport()) self.client.lineReceived("BPR 101 [email protected] PHH 123%20456") c = self.client.factory.contacts.getContact('*****@*****.**') self.failUnless(self.client.state == 'GOTPHONE', "Did not fire phone change callback") self.failUnless(c.homePhone == '123 456', "Did not update the contact's phone number") self.failUnless(self.client.factory.contacts.version == 101, "Did not update list version")
def testLateBPR(self): """ This test makes sure that if a BPR response that was meant to be part of a SYN response (but came after the last LST) is received, the correct contact is updated and all is well """ self.client.makeConnection(StringTransport()) msn.NotificationClient.loggedIn(self.client, '*****@*****.**', 'foo', 1) lines = [ "SYN %s 100 1 1" % self.client.currentID, "GTC A", "BLP AL", "LSG 0 Other%20Contacts 0", "LST [email protected] Some%20Name 11 0", "BPR PHH 123%20456" ] map(self.client.lineReceived, lines) contact = self.client.factory.contacts.getContact( '*****@*****.**') self.failUnless(contact.homePhone == '123 456', "Did not update contact's phone number")
def test_makeConnection(self): """ When L{TLSMemoryBIOProtocol} is connected to a transport, it connects the protocol it wraps to a transport. """ clientProtocol = Protocol() clientFactory = ClientFactory() clientFactory.protocol = lambda: clientProtocol contextFactory = ClientContextFactory() wrapperFactory = TLSMemoryBIOFactory(contextFactory, True, clientFactory) sslProtocol = wrapperFactory.buildProtocol(None) transport = StringTransport() sslProtocol.makeConnection(transport) self.assertNotIdentical(clientProtocol.transport, None) self.assertNotIdentical(clientProtocol.transport, transport)
def _versionTest(self, serverVersionResponse): """ Test L{NotificationClient} version negotiation. """ self.client.factory.userHandle = "foo" transport = StringTransport() self.client.makeConnection(transport) self.assertEquals(transport.value(), "VER 1 MSNP8 CVR0\r\n") transport.clear() self.client.dataReceived(serverVersionResponse) self.assertEquals( transport.value(), "CVR 2 0x0409 win 4.10 i386 MSNMSGR 5.0.0544 MSMSGS foo\r\n")
def test_getHandle(self): """ L{TLSMemoryBIOProtocol.getHandle} returns the L{OpenSSL.SSL.Connection} instance it uses to actually implement TLS. This may seem odd. In fact, it is. The L{OpenSSL.SSL.Connection} is not actually the "system handle" here, nor even an object the reactor knows about directly. However, L{twisted.internet.ssl.Certificate}'s C{peerFromTransport} and C{hostFromTransport} methods depend on being able to get an L{OpenSSL.SSL.Connection} object in order to work properly. Implementing L{ISystemHandle.getHandle} like this is the easiest way for those APIs to be made to work. If they are changed, then it may make sense to get rid of this implementation of L{ISystemHandle} and return the underlying socket instead. """ factory = ClientFactory() contextFactory = ClientContextFactory() wrapperFactory = TLSMemoryBIOFactory(contextFactory, True, factory) proto = TLSMemoryBIOProtocol(wrapperFactory, Protocol()) transport = StringTransport() proto.makeConnection(transport) self.assertIsInstance(proto.getHandle(), ConnectionType)
def test_nexus(self): """ When L{msn.PassportNexus} receives enough information to identify the address of the login server, it fires the L{Deferred} passed to its initializer with that address. """ protocol = msn.PassportNexus(self.deferred, 'https://foobar.com/somepage.quux') headers = { 'Content-Length': '0', 'Content-Type': 'text/html', 'PassportURLs': 'DARealm=Passport.Net,DALogin=login.myserver.com/,DAReg=reg.myserver.com' } transport = StringTransport() protocol.makeConnection(transport) protocol.dataReceived('HTTP/1.0 200 OK\r\n') for (h, v) in headers.items(): protocol.dataReceived('%s: %s\r\n' % (h, v)) protocol.dataReceived('\r\n') self.assertEquals(self.result[0], "https://login.myserver.com/")