def test_starttls(self): self.t = TransportMock(self, self.protocol, with_starttls=True, loop=self.loop) self.assertTrue(self.t.can_starttls()) fut = asyncio.Future() def connection_made(transport): fut.set_result(None) self.protocol.connection_made = connection_made ssl_context = unittest.mock.Mock() post_handshake_callback = CoroutineMock() post_handshake_callback.return_value = None async def late_starttls(): await fut await self.t.starttls(ssl_context, post_handshake_callback) run_coroutine_with_peer( late_starttls(), self.t.run_test( [TransportMock.STARTTLS(ssl_context, post_handshake_callback)])) post_handshake_callback.assert_called_once_with(self.t)
def test_starttls_without_callback(self): self.t = TransportMock(self, self.protocol, with_starttls=True, loop=self.loop) self.assertTrue(self.t.can_starttls()) fut = asyncio.Future() def connection_made(transport): fut.set_result(None) self.protocol.connection_made = connection_made ssl_context = unittest.mock.Mock() @asyncio.coroutine def late_starttls(): yield from fut yield from self.t.starttls(ssl_context) run_coroutine_with_peer( late_starttls(), self.t.run_test( [ TransportMock.STARTTLS(ssl_context, None) ] ) )
def _run_test(self, coro, actions=[], stimulus=None): return run_coroutine_with_peer( coro, self.xmlstream.run_test(actions, stimulus=stimulus), loop=self.loop)
def test_sm_works_correctly_with_invalid_payload(self): import aioxmpp.protocol import aioxmpp.stream version = (1, 0) fut = asyncio.Future() p = aioxmpp.protocol.XMLStream(to=TEST_PEER, sorted_attributes=True, features_future=fut) t = TransportMock(self, p) s = aioxmpp.stream.StanzaStream(TEST_FROM.bare()) s.soft_timeout = timedelta(seconds=0.25) run_coroutine( t.run_test([ TransportMock.Write( STREAM_HEADER, response=[ TransportMock.Receive( PEER_STREAM_HEADER_TEMPLATE.format( minor=version[1], major=version[0]).encode("utf-8")), TransportMock.Receive( b"<stream:features><sm xmlns='urn:xmpp:sm:3'/>" b"</stream:features>") ]), ], partial=True)) self.assertEqual(p.state, aioxmpp.protocol.State.OPEN) self.assertTrue(fut.done()) s.start(p) run_coroutine_with_peer( s.start_sm(), t.run_test([ TransportMock.Write( b'<enable xmlns="urn:xmpp:sm:3" resume="true"/>', response=[ TransportMock.Receive( b'<enabled xmlns="urn:xmpp:sm:3" ' b'resume="true" id="foo"/>') ]) ], partial=True)) self.assertTrue(s.sm_enabled) self.assertEqual(s.sm_id, "foo") self.assertTrue(s.sm_resumable) run_coroutine( t.run_test([ TransportMock.Write( b'<r xmlns="urn:xmpp:sm:3"/>', response=[ TransportMock.Receive( b'<a xmlns="urn:xmpp:sm:3" h="0"/>', ), TransportMock.Receive(b'<r xmlns="urn:xmpp:sm:3"/>', ) ]), TransportMock.Write(b'<a xmlns="urn:xmpp:sm:3" h="0"/>') ], partial=True)) run_coroutine( t.run_test([ TransportMock.Write( b'<iq id="foo" type="error"><error type="cancel">' b'<service-unavailable' b' xmlns="urn:ietf:params:xml:ns:xmpp-stanzas"/>' b'</error></iq>'), TransportMock.Write( b'<r xmlns="urn:xmpp:sm:3"/>', response=[ TransportMock.Receive( b'<a xmlns="urn:xmpp:sm:3" h="1"/>', ), TransportMock.Receive(b'<r xmlns="urn:xmpp:sm:3"/>', ) ]), TransportMock.Write(b'<a xmlns="urn:xmpp:sm:3" h="1"/>') ], stimulus=[ TransportMock.Receive(b'<iq type="get" id="foo">' b'<payload xmlns="fnord"/>' b'</iq>') ], partial=True))
def test_sm_bootstrap_race(self): import aioxmpp.protocol import aioxmpp.stream version = (1, 0) fut = asyncio.Future() p = aioxmpp.protocol.XMLStream(to=TEST_PEER, sorted_attributes=True, features_future=fut) t = TransportMock(self, p) s = aioxmpp.stream.StanzaStream(TEST_FROM.bare()) s.soft_timeout = timedelta(seconds=0.25) run_coroutine( t.run_test([ TransportMock.Write( STREAM_HEADER, response=[ TransportMock.Receive( PEER_STREAM_HEADER_TEMPLATE.format( minor=version[1], major=version[0]).encode("utf-8")), TransportMock.Receive( b"<stream:features><sm xmlns='urn:xmpp:sm:3'/>" b"</stream:features>") ]), ], partial=True)) self.assertEqual(p.state, aioxmpp.protocol.State.OPEN) self.assertTrue(fut.done()) s.start(p) run_coroutine_with_peer( s.start_sm(), t.run_test([ TransportMock.Write( b'<enable xmlns="urn:xmpp:sm:3" resume="true"/>', response=[ TransportMock.Receive( b'<enabled xmlns="urn:xmpp:sm:3" ' b'resume="true" id="foo"/>' b'<r xmlns="urn:xmpp:sm:3"/>') ]) ], partial=True)) self.assertTrue(s.sm_enabled) self.assertEqual(s.sm_id, "foo") self.assertTrue(s.sm_resumable) run_coroutine( t.run_test([ TransportMock.Write( b'<a xmlns="urn:xmpp:sm:3" h="0"/>' b'<r xmlns="urn:xmpp:sm:3"/>', response=[ TransportMock.Receive( b'<a xmlns="urn:xmpp:sm:3" h="0"/>', ), TransportMock.Receive(b'<r xmlns="urn:xmpp:sm:3"/>', ) ]), TransportMock.Write(b'<a xmlns="urn:xmpp:sm:3" h="0"/>') ], partial=True))
def _run_test(self, coro, actions=[], stimulus=None): return run_coroutine_with_peer(coro, self.xmlstream.run_test( actions, stimulus=stimulus), loop=self.loop)
def test_sm_works_correctly_with_invalid_payload(self): import aioxmpp.protocol import aioxmpp.stream version = (1, 0) fut = asyncio.Future() p = aioxmpp.protocol.XMLStream( to=TEST_PEER, sorted_attributes=True, features_future=fut) t = TransportMock(self, p) s = aioxmpp.stream.StanzaStream(TEST_FROM.bare()) run_coroutine(t.run_test( [ TransportMock.Write( STREAM_HEADER, response=[ TransportMock.Receive( PEER_STREAM_HEADER_TEMPLATE.format( minor=version[1], major=version[0]).encode("utf-8")), TransportMock.Receive( b"<stream:features><sm xmlns='urn:xmpp:sm:3'/>" b"</stream:features>" ) ] ), ], partial=True )) self.assertEqual(p.state, aioxmpp.protocol.State.OPEN) self.assertTrue(fut.done()) s.ping_interval = timedelta(seconds=0.25) s.ping_opportunistic_interval = timedelta(seconds=0.25) s.start(p) run_coroutine_with_peer( s.start_sm(), t.run_test( [ TransportMock.Write( b'<enable xmlns="urn:xmpp:sm:3" resume="true"/>', response=[ TransportMock.Receive( b'<enabled xmlns="urn:xmpp:sm:3" ' b'resume="true" id="foo"/>' ) ] ) ], partial=True ) ) self.assertTrue(s.sm_enabled) self.assertEqual(s.sm_id, "foo") self.assertTrue(s.sm_resumable) run_coroutine( t.run_test( [ TransportMock.Write( b'<r xmlns="urn:xmpp:sm:3"/>', response=[ TransportMock.Receive( b'<a xmlns="urn:xmpp:sm:3" h="0"/>', ), TransportMock.Receive( b'<r xmlns="urn:xmpp:sm:3"/>', ) ] ), TransportMock.Write( b'<a xmlns="urn:xmpp:sm:3" h="0"/>' ) ], partial=True ) ) run_coroutine( t.run_test( [ TransportMock.Write( b'<iq id="foo" type="error"><error type="cancel">' b'<feature-not-implemented' b' xmlns="urn:ietf:params:xml:ns:xmpp-stanzas"/>' b'</error></iq>' ), TransportMock.Write( b'<r xmlns="urn:xmpp:sm:3"/>', response=[ TransportMock.Receive( b'<a xmlns="urn:xmpp:sm:3" h="1"/>', ), TransportMock.Receive( b'<r xmlns="urn:xmpp:sm:3"/>', ) ] ), TransportMock.Write( b'<a xmlns="urn:xmpp:sm:3" h="1"/>' ) ], stimulus=[ TransportMock.Receive( b'<iq type="get" id="foo">' b'<payload xmlns="fnord"/>' b'</iq>' ) ], partial=True ) )