def test_unique_integers(self):
     results = set()
     for i in range(2048):
         res = utils.to_nmtoken(i)
         if res in results:
             self.fail("generated tokens not unique")
         results.add(res)
示例#2
0
    async def open_session(self,
                           protocol_factory,
                           peer_jid,
                           *,
                           stanza_type=ibb_xso.IBBStanzaType.IQ,
                           block_size=4096,
                           sid=None):
        """
        Establish an in-band bytestream session with `peer_jid` and
        return the transport and protocol.

        :param protocol_factory: the protocol factory
        :type protocol_factory: a nullary callable returning an
                                :class:`asyncio.Protocol` instance
        :param peer_jid: the JID with which to establish the byte-stream.
        :type peer_jid: :class:`aioxmpp.JID`
        :param stanza_type: the stanza type to use
        :type stanza_type: class:`~aioxmpp.ibb.IBBStanzaType`
        :param block_size: the maximal size of blocks to transfer
        :type block_size: :class:`int`
        :param sid: the session id to use
        :type sid: :class:`str` (must be a valid NMTOKEN)

        :returns: the transport and protocol
        :rtype: a tuple of :class:`aioxmpp.ibb.service.IBBTransport`
                and :class:`asyncio.Protocol`
        """
        if block_size > MAX_BLOCK_SIZE:
            raise ValueError("block_size too large")

        if sid is None:
            sid = utils.to_nmtoken(random.getrandbits(8 * 8))
        open_ = ibb_xso.Open()
        open_.stanza = stanza_type
        open_.sid = sid
        open_.block_size = block_size

        # XXX: retry on XMPPModifyError with RESOURCE_CONSTRAINT
        await self.client.send(
            aioxmpp.IQ(
                aioxmpp.IQType.SET,
                to=peer_jid,
                payload=open_,
            ))

        handle = self._sessions[sid, peer_jid] = IBBTransport(
            self,
            peer_jid,
            sid,
            stanza_type,
            block_size,
        )

        protocol = protocol_factory()
        handle.set_protocol(protocol)
        return handle, protocol
 def test_zero_extension(self):
     a = utils.to_nmtoken(b"\xff\x00")
     b = utils.to_nmtoken(b"\xff")
     self.assertNotEqual(a, b)
 def test_integers_and_bytes_do_not_collide(self):
     a = utils.to_nmtoken(255)
     b = utils.to_nmtoken(b"\xff")
     self.assertNotEqual(a, b)
     self.assertTrue(a.startswith(":"))
     self.assertFalse(b.startswith(":"))