示例#1
0
    def test_no_response_conflict(self):
        data = []

        def data_received(blob):
            data.append(blob)

        def connection_made(transport):
            transport.write(b"foo")
            self.assertFalse(data)
            transport.write(b"bar")

        self.protocol.connection_made = connection_made
        self.protocol.data_received = data_received

        self._run_test(
            self.t,
            [
                TransportMock.Write(
                    b"foo",
                    response=TransportMock.Receive(b"baz"),
                ),
                TransportMock.Write(
                    b"bar",
                    response=TransportMock.Receive(b"baz")
                )
            ])
示例#2
0
    def test_iq_results_are_not_replied_to(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")),
                    ]),
            ],
                       partial=True))

        self.assertEqual(p.state, aioxmpp.protocol.State.OPEN)

        s.start(p)

        run_coroutine(
            t.run_test(
                [],
                stimulus=[
                    TransportMock.Receive(b'<iq type="result" id="foo">'
                                          b'<payload xmlns="fnord"/>'
                                          b'</iq>')
                ],
                partial=True,
            ))

        s.flush_incoming()

        run_coroutine(asyncio.sleep(0))

        run_coroutine(t.run_test([], ))

        s.stop()
示例#3
0
    def test_request_multiresponse(self):
        def data_received(data):
            assert data in {b"foo", b"bar", b"baz"}
            if data == b"foo":
                self.t.write(b"bar")
            elif data == b"bar":
                self.t.write(b"baric")
            elif data == b"baz":
                self.t.close()

        self.protocol.data_received = data_received
        self._run_test(self.t, [
            TransportMock.Write(b"bar",
                                response=[
                                    TransportMock.Receive(b"bar"),
                                    TransportMock.Receive(b"baz")
                                ]),
            TransportMock.Write(b"baric"),
            TransportMock.Close()
        ],
                       stimulus=b"foo")
示例#4
0
    def test_exception_from_stimulus_bubbles_up(self):
        exc = ConnectionError("foobar")

        def data_received(data):
            raise exc

        self.protocol.data_received = data_received

        with self.assertRaises(ConnectionError) as ctx:
            run_coroutine(
                self.t.run_test([], stimulus=TransportMock.Receive(b"foobar")))

        self.assertIs(exc, ctx.exception)
示例#5
0
    def test_asynchronous_request_response(self):
        def data_received(data):
            self.assertIn(data, {b"foo", b"baz"})
            if data == b"foo":
                self.loop.call_soon(self.t.write, b"bar")
            elif data == b"baz":
                self.loop.call_soon(self.t.close)

        self.protocol.data_received = data_received
        self._run_test(self.t, [
            TransportMock.Write(b"bar",
                                response=TransportMock.Receive(b"baz")),
            TransportMock.Close()
        ],
                       stimulus=b"foo")
示例#6
0
    def test_response_sequence(self):
        def connection_made(transport):
            transport.write(b"foo")

        self.protocol.connection_made = connection_made

        self._run_test(self.t, [
            TransportMock.Write(b"foo",
                                response=[
                                    TransportMock.Receive(b"foo"),
                                    TransportMock.ReceiveEof()
                                ])
        ])

        self.assertSequenceEqual(self.protocol.mock_calls, [
            unittest.mock.call.data_received(b"foo"),
            unittest.mock.call.eof_received(),
            unittest.mock.call.connection_lost(None),
        ])
示例#7
0
    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))
示例#8
0
    def test_hard_deadtime_kills_stream(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.1)
        s.round_trip_time = timedelta(seconds=0.1)

        failure_fut = s.on_failure.future()

        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")),
                    ]),
            ],
                       partial=True))

        self.assertEqual(p.state, aioxmpp.protocol.State.OPEN)

        s.start(p)

        IQ_bak = aioxmpp.IQ

        def fake_iq_constructor(*args, **kwargs):
            iq = IQ_bak(*args, **kwargs)
            iq.id_ = "ping"
            return iq

        with unittest.mock.patch("aioxmpp.stanza.IQ") as IQ:
            IQ.side_effect = fake_iq_constructor

            run_coroutine(
                t.run_test([
                    TransportMock.Write(b'<iq id="ping" type="get">'
                                        b'<ping xmlns="urn:xmpp:ping"/></iq>'),
                ],
                           partial=True))

        run_coroutine(t.run_test([
            TransportMock.Abort(),
        ], ))

        run_coroutine(asyncio.sleep(0))

        self.assertFalse(s.running)

        self.assertTrue(failure_fut.done())
        self.assertIsInstance(failure_fut.exception(), ConnectionError)
        self.assertIn("timeout", str(failure_fut.exception()))
示例#9
0
    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))