def verifyResults(self, transport, proto, parser):
        ByteGroupingsMixin.verifyResults(self, transport, proto, parser)

        for char in iterbytes(b'abc123ABC!@#'):
            result = self.assertCall(occurrences(proto).pop(0), "keystrokeReceived", (char, None))
            self.assertEqual(occurrences(result), [])

        for char in iterbytes(b'abc123'):
            result = self.assertCall(occurrences(proto).pop(0), "keystrokeReceived", (char, parser.ALT))
            self.assertEqual(occurrences(result), [])

        occs = occurrences(proto)
        self.assertFalse(occs, "%r should have been []" % (occs,))
    def testCharacterSet(self):
        self.parser.dataReceived(
            b''.join(
                [b''.join([b'\x1b' + g + n for n in iterbytes(b'AB012')])
                    for g in iterbytes(b'()')
                ]))
        occs = occurrences(self.proto)

        for which in (G0, G1):
            for charset in (CS_UK, CS_US, CS_DRAWING, CS_ALTERNATE, CS_ALTERNATE_SPECIAL):
                result = self.assertCall(occs.pop(0), "selectCharacterSet", (charset, which))
                self.assertFalse(occurrences(result))
        self.assertFalse(occs)
    def test_delete(self):
        """
        When L{HistoricRecvLine} receives a DELETE keystroke, it
        delets the character immediately after the cursor.
        """
        kR = lambda ch: self.p.keystrokeReceived(ch, None)

        for ch in iterbytes(b'xyz'):
            kR(ch)
        self.assertEqual(self.p.currentLineBuffer(), (b'xyz', b''))

        kR(self.pt.DELETE)
        self.assertEqual(self.p.currentLineBuffer(), (b'xyz', b''))

        kR(self.pt.LEFT_ARROW)
        kR(self.pt.DELETE)
        self.assertEqual(self.p.currentLineBuffer(), (b'xy', b''))

        kR(self.pt.LEFT_ARROW)
        kR(self.pt.DELETE)
        self.assertEqual(self.p.currentLineBuffer(), (b'x', b''))

        kR(self.pt.LEFT_ARROW)
        kR(self.pt.DELETE)
        self.assertEqual(self.p.currentLineBuffer(), (b'', b''))

        kR(self.pt.DELETE)
        self.assertEqual(self.p.currentLineBuffer(), (b'', b''))
Example #4
0
 def dataReceived(self, data):
     for ch in iterbytes(data):
         if self.state == b'data':
             if ch == b'\x1b':
                 self.state = b'escaped'
             else:
                 self.terminalProtocol.keystrokeReceived(ch, None)
         elif self.state == b'escaped':
             if ch == b'[':
                 self.state = b'bracket-escaped'
                 self.escBuf = []
             elif ch == b'O':
                 self.state = b'low-function-escaped'
             else:
                 self.state = b'data'
                 self._handleShortControlSequence(ch)
         elif self.state == b'bracket-escaped':
             if ch == b'O':
                 self.state = b'low-function-escaped'
             elif ch.isalpha() or ch == b'~':
                 self._handleControlSequence(b''.join(self.escBuf) + ch)
                 del self.escBuf
                 self.state = b'data'
             else:
                 self.escBuf.append(ch)
         elif self.state == b'low-function-escaped':
             self._handleLowFunctionControlSequence(ch)
             self.state = b'data'
         else:
             raise ValueError("Illegal state")
Example #5
0
    def test_socks4aFailedResolution(self):
        """
        Failed hostname resolution on a SOCKSv4a packet results in a 91 error
        response and the connection getting closed.
        """
        # send the domain name "failinghost" to be resolved
        clientRequest = (
            struct.pack('!BBH', 4, 2, 34)
            + socket.inet_aton('0.0.0.1')
            + b'fooBAZ\0'
            + b'failinghost\0')

        # Deliver the bytes one by one to exercise the protocol's buffering
        # logic. FakeResolverReactor's resolve method is invoked to "resolve"
        # the hostname.
        for byte in iterbytes(clientRequest):
            self.sock.dataReceived(byte)

        # Verify that the server responds with a 91 error.
        sent = self.sock.transport.value()
        self.assertEqual(
            sent,
            struct.pack('!BBH', 0, 91, 0) + socket.inet_aton('0.0.0.0'))

        # A failed resolution causes the transport to drop the connection.
        self.assertTrue(self.sock.transport.stringTCPTransport_closing)
        self.assertIsNone(self.sock.driver_outgoing)
Example #6
0
 def __repr__(self):
     """
     Return a pretty representation of this object.
     """
     lines = [
         '<%s %s (%s bits)' % (
             nativeString(self.type()),
             self.isPublic() and 'Public Key' or 'Private Key',
             self._keyObject.key_size)]
     for k, v in sorted(self.data().items()):
         if _PY3 and isinstance(k, bytes):
             k = k.decode('ascii')
         lines.append('attr %s:' % (k,))
         by = common.MP(v)[4:]
         while by:
             m = by[:15]
             by = by[15:]
             o = ''
             for c in iterbytes(m):
                 o = o + '%02x:' % (ord(c),)
             if len(m) < 15:
                 o = o[:-1]
             lines.append('\t' + o)
     lines[-1] = lines[-1] + '>'
     return '\n'.join(lines)
Example #7
0
    def _toString_OPENSSH(self, extra):
        """
        Return a public or private OpenSSH string.  See
        _fromString_PUBLIC_OPENSSH and _fromString_PRIVATE_OPENSSH for the
        string formats.  If extra is present, it represents a comment for a
        public key, or a passphrase for a private key.

        @param extra: Comment for a public key or passphrase for a
            private key
        @type extra: L{bytes}

        @rtype: L{bytes}
        """
        data = self.data()
        if self.isPublic():
            b64Data = base64.encodestring(self.blob()).replace(b'\n', b'')
            if not extra:
                extra = b''
            return (self.sshType() + b' ' + b64Data + b' ' + extra).strip()
        else:
            lines = [b''.join((b'-----BEGIN ', self.type().encode('ascii'),
                               b' PRIVATE KEY-----'))]
            if self.type() == 'RSA':
                p, q = data['p'], data['q']
                objData = (0, data['n'], data['e'], data['d'], q, p,
                           data['d'] % (q - 1), data['d'] % (p - 1),
                           data['u'])
            else:
                objData = (0, data['p'], data['q'], data['g'], data['y'],
                           data['x'])
            asn1Sequence = univ.Sequence()
            for index, value in izip(itertools.count(), objData):
                asn1Sequence.setComponentByPosition(index, univ.Integer(value))
            asn1Data = berEncoder.encode(asn1Sequence)
            if extra:
                iv = randbytes.secureRandom(8)
                hexiv = ''.join(['%02X' % (ord(x),) for x in iterbytes(iv)])
                hexiv = hexiv.encode('ascii')
                lines.append(b'Proc-Type: 4,ENCRYPTED')
                lines.append(b'DEK-Info: DES-EDE3-CBC,' + hexiv + b'\n')
                ba = md5(extra + iv).digest()
                bb = md5(ba + extra + iv).digest()
                encKey = (ba + bb)[:24]
                padLen = 8 - (len(asn1Data) % 8)
                asn1Data += (chr(padLen) * padLen).encode('ascii')

                encryptor = Cipher(
                    algorithms.TripleDES(encKey),
                    modes.CBC(iv),
                    backend=default_backend()
                ).encryptor()

                asn1Data = encryptor.update(asn1Data) + encryptor.finalize()

            b64Data = base64.encodestring(asn1Data).replace(b'\n', b'')
            lines += [b64Data[i:i + 64] for i in range(0, len(b64Data), 64)]
            lines.append(b''.join((b'-----END ', self.type().encode('ascii'),
                                   b' PRIVATE KEY-----')))
            return b'\n'.join(lines)
Example #8
0
    def test_socks4a(self):
        """
        If the destination IP address has zeros for the first three octets and
        non-zero for the fourth octet, the client is attempting a v4a
        connection.  A hostname is specified after the user ID string and the
        server connects to the address that hostname resolves to.

        @see: U{http://en.wikipedia.org/wiki/SOCKS#SOCKS_4a_protocol}
        """
        # send the domain name "localhost" to be resolved
        clientRequest = (
            struct.pack('!BBH', 4, 2, 34)
            + socket.inet_aton('0.0.0.1')
            + b'fooBAZ\0'
            + b'localhost\0')

        # Deliver the bytes one by one to exercise the protocol's buffering
        # logic. FakeResolverReactor's resolve method is invoked to "resolve"
        # the hostname.
        for byte in iterbytes(clientRequest):
            self.sock.dataReceived(byte)

        sent = self.sock.transport.value()
        self.sock.transport.clear()

        # Verify that the server responded with the address which will be
        # connected to.
        self.assertEqual(
            sent,
            struct.pack('!BBH', 0, 90, 1234) + socket.inet_aton('6.7.8.9'))
        self.assertFalse(self.sock.transport.stringTCPTransport_closing)
        self.assertIsNotNone(self.sock.driver_listen)

        # connect
        incoming = self.sock.driver_listen.buildProtocol(('127.0.0.1', 5345))
        self.assertIsNotNone(incoming)
        incoming.transport = StringTCPTransport()
        incoming.connectionMade()

        # now we should have the second reply packet
        sent = self.sock.transport.value()
        self.sock.transport.clear()
        self.assertEqual(sent,
                         struct.pack('!BBH', 0, 90, 0)
                         + socket.inet_aton('0.0.0.0'))
        self.assertIsNot(
            self.sock.transport.stringTCPTransport_closing, None)

        # Deliver some data from the output connection and verify it is
        # passed along to the incoming side.
        self.sock.dataReceived(b'hi there')
        self.assertEqual(incoming.transport.value(), b'hi there')

        # the other way around
        incoming.dataReceived(b'hi there')
        self.assertEqual(self.sock.transport.value(), b'hi there')

        self.sock.connectionLost('fake reason')
Example #9
0
 def test_receive(self):
     """
     Test receiving data find the same data send.
     """
     r = self.getProtocol()
     for s in self.strings:
         for c in iterbytes(struct.pack(r.structFormat, len(s)) + s):
             r.dataReceived(c)
     self.assertEqual(r.received, self.strings)
Example #10
0
 def test_illegal(self):
     """
     Assert that illegal strings cause the transport to be closed.
     """
     for s in self.illegalStrings:
         r = self.getProtocol()
         for c in iterbytes(s):
             r.dataReceived(c)
         self.assertTrue(r.transport.disconnecting)
Example #11
0
 def test_partial(self):
     """
     Send partial data, nothing should be definitely received.
     """
     for s in self.partialStrings:
         r = self.getProtocol()
         for c in iterbytes(s):
             r.dataReceived(c)
         self.assertEqual(r.received, [])
Example #12
0
 def test_iteration(self):
     """
     When L{iterbytes} is called with a bytestring, the returned object
     can be iterated over, resulting in the individual bytes of the
     bytestring.
     """
     input = b"abcd"
     result = list(iterbytes(input))
     self.assertEqual(result, [b'a', b'b', b'c', b'd'])
 def test_iteration(self):
     """
     When L{iterbytes} is called with a bytestring, the returned object
     can be iterated over, resulting in the individual bytes of the
     bytestring.
     """
     input = b"abcd"
     result = list(iterbytes(input))
     self.assertEqual(result, [b'a', b'b', b'c', b'd'])
Example #14
0
    def write(self, data):
        """
        Add the given printable bytes to the terminal.

        Line feeds in L{bytes} will be replaced with carriage return / line
        feed pairs.
        """
        for b in iterbytes(data.replace(b"\n", b"\r\n")):
            self.insertAtCursor(b)
Example #15
0
 def test_illegal(self):
     """
     Assert that illegal strings cause the transport to be closed.
     """
     for s in self.illegalStrings:
         r = self.getProtocol()
         for c in iterbytes(s):
             r.dataReceived(c)
         self.assertTrue(r.transport.disconnecting)
Example #16
0
 def test_receive(self):
     """
     Test receiving data find the same data send.
     """
     r = self.getProtocol()
     for s in self.strings:
         for c in iterbytes(struct.pack(r.structFormat, len(s)) + s):
             r.dataReceived(c)
     self.assertEqual(r.received, self.strings)
Example #17
0
    def feed(self, data):
        """
        Feed the data byte per byte to the receiver.

        @param data: The bytes to deliver.
        @type data: L{bytes}
        """
        for byte in iterbytes(data):
            self.enc.dataReceived(byte)
Example #18
0
    def feed(self, data):
        """
        Feed the data byte per byte to the receiver.

        @param data: The bytes to deliver.
        @type data: L{bytes}
        """
        for byte in iterbytes(data):
            self.enc.dataReceived(byte)
Example #19
0
 def test_partial(self):
     """
     Send partial data, nothing should be definitely received.
     """
     for s in self.partialStrings:
         r = self.getProtocol()
         for c in iterbytes(s):
             r.dataReceived(c)
         self.assertEqual(r.received, [])
    def write(self, data):
        """
        Add the given printable bytes to the terminal.

        Line feeds in L{bytes} will be replaced with carriage return / line
        feed pairs.
        """
        for b in iterbytes(data.replace(b'\n', b'\r\n')):
            self.insertAtCursor(b)
Example #21
0
 def test_buffer(self):
     """
     Test buffering over line protocol: data received should match buffer.
     """
     t = proto_helpers.StringTransport()
     a = LineOnlyTester()
     a.makeConnection(t)
     for c in iterbytes(self.buffer):
         a.dataReceived(c)
     self.assertEqual(a.received, self.buffer.split(b'\n')[:-1])
Example #22
0
    def testCharacterSet(self):
        self.parser.dataReceived(b"".join([
            b"".join([b"\x1b" + g + n for n in iterbytes(b"AB012")])
            for g in iterbytes(b"()")
        ]))
        occs = occurrences(self.proto)

        for which in (G0, G1):
            for charset in (
                    CS_UK,
                    CS_US,
                    CS_DRAWING,
                    CS_ALTERNATE,
                    CS_ALTERNATE_SPECIAL,
            ):
                result = self.assertCall(occs.pop(0), "selectCharacterSet",
                                         (charset, which))
                self.assertFalse(occurrences(result))
        self.assertFalse(occs)
Example #23
0
 def test_buffer(self):
     """
     Test buffering over line protocol: data received should match buffer.
     """
     t = proto_helpers.StringTransport()
     a = LineOnlyTester()
     a.makeConnection(t)
     for c in iterbytes(self.buffer):
         a.dataReceived(c)
     self.assertEqual(a.received, self.buffer.split(b"\n")[:-1])
    def test_socks4a(self):
        """
        If the destination IP address has zeros for the first three octets and
        non-zero for the fourth octet, the client is attempting a v4a
        connection.  A hostname is specified after the user ID string and the
        server connects to the address that hostname resolves to.

        @see: U{http://en.wikipedia.org/wiki/SOCKS#SOCKS_4a_protocol}
        """
        # send the domain name "localhost" to be resolved
        clientRequest = (struct.pack("!BBH", 4, 2, 34) +
                         socket.inet_aton("0.0.0.1") + b"fooBAZ\0" +
                         b"localhost\0")

        # Deliver the bytes one by one to exercise the protocol's buffering
        # logic. FakeResolverReactor's resolve method is invoked to "resolve"
        # the hostname.
        for byte in iterbytes(clientRequest):
            self.sock.dataReceived(byte)

        sent = self.sock.transport.value()
        self.sock.transport.clear()

        # Verify that the server responded with the address which will be
        # connected to.
        self.assertEqual(
            sent,
            struct.pack("!BBH", 0, 90, 1234) + socket.inet_aton("6.7.8.9"))
        self.assertFalse(self.sock.transport.stringTCPTransport_closing)
        self.assertIsNotNone(self.sock.driver_listen)

        # connect
        incoming = self.sock.driver_listen.buildProtocol(("127.0.0.1", 5345))
        self.assertIsNotNone(incoming)
        incoming.transport = StringTCPTransport()
        incoming.connectionMade()

        # now we should have the second reply packet
        sent = self.sock.transport.value()
        self.sock.transport.clear()
        self.assertEqual(
            sent,
            struct.pack("!BBH", 0, 90, 0) + socket.inet_aton("0.0.0.0"))
        self.assertIsNot(self.sock.transport.stringTCPTransport_closing, None)

        # Deliver some data from the output connection and verify it is
        # passed along to the incoming side.
        self.sock.dataReceived(b"hi there")
        self.assertEqual(incoming.transport.value(), b"hi there")

        # the other way around
        incoming.dataReceived(b"hi there")
        self.assertEqual(self.sock.transport.value(), b"hi there")

        self.sock.connectionLost("fake reason")
Example #25
0
 def test_dataNotFullyReceived(self):
     """
     Since the initial rule inside the grammar is not matched, the receiver
     shouldn't receive any byte.
     """
     receiver = TrampolinedReceiver()
     trampolinedParser = TrampolinedParser(self.grammar, receiver, {})
     buf = b'foobarandnotreachdelimiter'
     for c in iterbytes(buf):
         trampolinedParser.receive(c)
     self.assertEqual(receiver.received, [])
Example #26
0
 def test_currentRuleWithArgs(self):
     """
     TrampolinedParser should be able to invoke curruent rule with args.
     """
     receiver = TrampolinedReceiver()
     receiver.currentRule = "witharg", "nice ", "day"
     trampolinedParser = TrampolinedParser(self.grammar, receiver, {})
     buf = b' oh yes\r\n'
     for c in iterbytes(buf):
         trampolinedParser.receive(c)
     self.assertEqual(receiver.received, ["nice day oh yes"])
Example #27
0
 def test_dataNotFullyReceived(self):
     """
     Since the initial rule inside the grammar is not matched, the receiver
     shouldn't receive any byte.
     """
     receiver = TrampolinedReceiver()
     trampolinedParser = TrampolinedParser(self.grammar, receiver, {})
     buf = b'foobarandnotreachdelimiter'
     for c in iterbytes(buf):
         trampolinedParser.receive(c)
     self.assertEqual(receiver.received, [])
Example #28
0
    def testSimpleCardinals(self):
        self.parser.dataReceived(b"".join(b"\x1b[" + n + ch
                                          for ch in iterbytes(b"BACD")
                                          for n in (b"", b"2", b"20", b"200")))
        occs = occurrences(self.proto)

        for meth in ("Down", "Up", "Forward", "Backward"):
            for count in (1, 2, 20, 200):
                result = self.assertCall(occs.pop(0), "cursor" + meth,
                                         (count, ))
                self.assertFalse(occurrences(result))
        self.assertFalse(occs)
Example #29
0
 def test_dataFullyReceived(self):
     """
     The receiver should receive the data according to the grammar.
     """
     receiver = TrampolinedReceiver()
     trampolinedParser = TrampolinedParser(self.grammar, receiver, {})
     buf = b'\r\n'.join((b'foo', b'bar', b'foo', b'bar'))
     for c in iterbytes(buf):
         trampolinedParser.receive(c)
     self.assertEqual(receiver.received, [b'foo', b'bar', b'foo'])
     trampolinedParser.receive('\r\n')
     self.assertEqual(receiver.received, [b'foo', b'bar', b'foo', b'bar'])
Example #30
0
    def _toString_OPENSSH(self, extra):
        """
        Return a public or private OpenSSH string.  See
        _fromString_PUBLIC_OPENSSH and _fromString_PRIVATE_OPENSSH for the
        string formats.  If extra is present, it represents a comment for a
        public key, or a passphrase for a private key.

        @param extra: Comment for a public key or passphrase for a
            private key
        @type extra: C{bytes}

        @rtype: C{bytes}
        """
        data = self.data()
        if self.isPublic():
            b64Data = base64.encodestring(self.blob()).replace(b'\n', b'')
            if not extra:
                extra = b''
            return (self.sshType() + b' ' + b64Data + b' ' + extra).strip()
        else:
            lines = [
                b''.join((b'-----BEGIN ', self.type().encode('ascii'),
                          b' PRIVATE KEY-----'))
            ]
            if self.type() == 'RSA':
                p, q = data['p'], data['q']
                objData = (0, data['n'], data['e'], data['d'], q, p,
                           data['d'] % (q - 1), data['d'] % (p - 1), data['u'])
            else:
                objData = (0, data['p'], data['q'], data['g'], data['y'],
                           data['x'])
            asn1Sequence = univ.Sequence()
            for index, value in izip(itertools.count(), objData):
                asn1Sequence.setComponentByPosition(index, univ.Integer(value))
            asn1Data = berEncoder.encode(asn1Sequence)
            if extra:
                iv = randbytes.secureRandom(8)
                hexiv = ''.join(['%02X' % ord(x) for x in iterbytes(iv)])
                hexiv = hexiv.encode('ascii')
                lines.append(b'Proc-Type: 4,ENCRYPTED')
                lines.append(b'DEK-Info: DES-EDE3-CBC,' + hexiv + b'\n')
                ba = md5(extra + iv).digest()
                bb = md5(ba + extra + iv).digest()
                encKey = (ba + bb)[:24]
                padLen = 8 - (len(asn1Data) % 8)
                asn1Data += (chr(padLen) * padLen).encode('ascii')
                asn1Data = DES3.new(encKey, DES3.MODE_CBC,
                                    iv).encrypt(asn1Data)
            b64Data = base64.encodestring(asn1Data).replace(b'\n', b'')
            lines += [b64Data[i:i + 64] for i in range(0, len(b64Data), 64)]
            lines.append(b''.join((b'-----END ', self.type().encode('ascii'),
                                   b' PRIVATE KEY-----')))
            return b'\n'.join(lines)
Example #31
0
 def test_dataFullyReceived(self):
     """
     The receiver should receive the data according to the grammar.
     """
     receiver = TrampolinedReceiver()
     trampolinedParser = TrampolinedParser(self.grammar, receiver, {})
     buf = b'\r\n'.join((b'foo', b'bar', b'foo', b'bar'))
     for c in iterbytes(buf):
         trampolinedParser.receive(c)
     self.assertEqual(receiver.received, [b'foo', b'bar', b'foo'])
     trampolinedParser.receive('\r\n')
     self.assertEqual(receiver.received, [b'foo', b'bar', b'foo', b'bar'])
Example #32
0
    def _bytesToIPv4(bytestring):
        """
        Convert packed 32-bit IPv4 address bytes into a dotted-quad ASCII bytes
        representation of that address.

        @param bytestring: 4 octets representing an IPv4 address.
        @type bytestring: L{bytes}

        @return: a dotted-quad notation IPv4 address.
        @rtype: L{bytes}
        """
        return b'.'.join(('%i' % (ord(b), )).encode('ascii')
                         for b in compat.iterbytes(bytestring))
Example #33
0
    def testSubnegotiation(self):
        # Send a subnegotiation command and make sure it gets
        # parsed and that the correct method is called.
        h = self.p.protocol

        cmd = telnet.IAC + telnet.SB + b"\x12hello world" + telnet.IAC + telnet.SE
        L = [b"These are some bytes but soon" + cmd, b"there will be some more"]

        for b in L:
            self.p.dataReceived(b)

        self.assertEqual(h.bytes, b"".join(L).replace(cmd, b""))
        self.assertEqual(h.subcmd, list(iterbytes(b"hello world")))
Example #34
0
    def testSubnegotiation(self):
        # Send a subnegotiation command and make sure it gets
        # parsed and that the correct method is called.
        h = self.p.protocol

        cmd = telnet.IAC + telnet.SB + b"\x12hello world" + telnet.IAC + telnet.SE
        L = [b"These are some bytes but soon" + cmd, b"there will be some more"]

        for b in L:
            self.p.dataReceived(b)

        self.assertEqual(h.data, b"".join(L).replace(cmd, b""))
        self.assertEqual(h.subcmd, list(iterbytes(b"hello world")))
Example #35
0
    def test_home(self):
        """
        When L{HistoricRecvLine} receives a HOME keystroke it moves the
        cursor to the beginning of the current line buffer.
        """
        kR = lambda ch: self.p.keystrokeReceived(ch, None)

        for ch in iterbytes(b'hello, world'):
            kR(ch)
        self.assertEqual(self.p.currentLineBuffer(), (b'hello, world', b''))

        kR(self.pt.HOME)
        self.assertEqual(self.p.currentLineBuffer(), (b'', b'hello, world'))
    def test_home(self):
        """
        When L{HistoricRecvLine} receives a HOME keystroke it moves the
        cursor to the beginning of the current line buffer.
        """
        kR = lambda ch: self.p.keystrokeReceived(ch, None)

        for ch in iterbytes(b'hello, world'):
            kR(ch)
        self.assertEqual(self.p.currentLineBuffer(), (b'hello, world', b''))

        kR(self.pt.HOME)
        self.assertEqual(self.p.currentLineBuffer(), (b'', b'hello, world'))
Example #37
0
    def test_end(self):
        """
        When L{HistoricRecvLine} receives an END keystroke it moves the cursor
        to the end of the current line buffer.
        """
        kR = lambda ch: self.p.keystrokeReceived(ch, None)

        for ch in iterbytes(b"hello, world"):
            kR(ch)
        self.assertEqual(self.p.currentLineBuffer(), (b"hello, world", b""))

        kR(self.pt.HOME)
        kR(self.pt.END)
        self.assertEqual(self.p.currentLineBuffer(), (b"hello, world", b""))
Example #38
0
    def testSimpleCardinals(self):
        self.parser.dataReceived(
            b''.join(
                    [b''.join([b'\x1b[' + n + ch
                             for n in (b'', intToBytes(2), intToBytes(20), intToBytes(200))]
                           ) for ch in iterbytes(b'BACD')
                    ]))
        occs = occurrences(self.proto)

        for meth in ("Down", "Up", "Forward", "Backward"):
            for count in (1, 2, 20, 200):
                result = self.assertCall(occs.pop(0), "cursor" + meth, (count,))
                self.assertFalse(occurrences(result))
        self.assertFalse(occs)
Example #39
0
 def deliver_data(self, protocol, data):
     """
     Deliver bytes one by one, to ensure parser can deal with unchunked
     data.
     """
     for byte in iterbytes(data):
         protocol.dataReceived(byte)
         if protocol.transport.disconnecting:
             # Don't deliver any more data if the protocol lost its
             # connection.  This happens in some error cases where the
             # server can't parse the input.  Continuing to deliver data
             # diverges from real transport behavior and generates tons of
             # garbage.
             break
Example #40
0
    def testSimpleCardinals(self):
        self.parser.dataReceived(b''.join([
            b''.join([
                b'\x1b[' + n + ch
                for n in (b'', intToBytes(2), intToBytes(20), intToBytes(200))
            ]) for ch in iterbytes(b'BACD')
        ]))
        occs = occurrences(self.proto)

        for meth in ("Down", "Up", "Forward", "Backward"):
            for count in (1, 2, 20, 200):
                result = self.assertCall(occs.pop(0), "cursor" + meth,
                                         (count, ))
                self.assertFalse(occurrences(result))
        self.assertFalse(occs)
Example #41
0
    def _bytesToIPv4(bytestring):
        """
        Convert packed 32-bit IPv4 address bytes into a dotted-quad ASCII bytes
        representation of that address.

        @param bytestring: 4 octets representing an IPv4 address.
        @type bytestring: L{bytes}

        @return: a dotted-quad notation IPv4 address.
        @rtype: L{bytes}
        """
        return b'.'.join(
            ('%i' % (ord(b),)).encode('ascii')
            for b in compat.iterbytes(bytestring)
        )
Example #42
0
 def dataReceived(self, data):
     chunk = BytesIO()
     # silly little IAC state-machine
     for char in iterbytes(data):
         if self.gotIAC:
             # working on an IAC request state
             if self.iacByte:
                 # we're in SB mode, getting a chunk
                 if self.iacByte == SB:
                     if char == SE:
                         self.iacSBchunk(chunk.getvalue())
                         chunk = BytesIO()
                         del self.iacByte
                         del self.gotIAC
                     else:
                         chunk.write(char)
                 else:
                     # got all I need to know state
                     try:
                         getattr(self,
                                 'iac_%s' % iacBytes[self.iacByte])(char)
                     except KeyError:
                         pass
                     del self.iacByte
                     del self.gotIAC
             else:
                 # got IAC, this is my W/W/D/D (or perhaps sb)
                 self.iacByte = char
         elif char == IAC:
             # Process what I've got so far before going into
             # the IAC state; don't want to process characters
             # in an inconsistent state with what they were
             # received in.
             c = chunk.getvalue()
             if c:
                 why = self.processChunk(c)
                 if why:
                     return why
                 chunk = BytesIO()
             self.gotIAC = 1
         else:
             chunk.write(char)
     # chunks are of a relatively indeterminate size.
     c = chunk.getvalue()
     if c:
         why = self.processChunk(c)
         if why:
             return why
Example #43
0
 def dataReceived(self, data):
     chunk = BytesIO()
     # silly little IAC state-machine
     for char in iterbytes(data):
         if self.gotIAC:
             # working on an IAC request state
             if self.iacByte:
                 # we're in SB mode, getting a chunk
                 if self.iacByte == SB:
                     if char == SE:
                         self.iacSBchunk(chunk.getvalue())
                         chunk = BytesIO()
                         del self.iacByte
                         del self.gotIAC
                     else:
                         chunk.write(char)
                 else:
                     # got all I need to know state
                     try:
                         getattr(self, 'iac_%s' % iacBytes[self.iacByte])(char)
                     except KeyError:
                         pass
                     del self.iacByte
                     del self.gotIAC
             else:
                 # got IAC, this is my W/W/D/D (or perhaps sb)
                 self.iacByte = char
         elif char == IAC:
             # Process what I've got so far before going into
             # the IAC state; don't want to process characters
             # in an inconsistent state with what they were
             # received in.
             c = chunk.getvalue()
             if c:
                 why = self.processChunk(c)
                 if why:
                     return why
                 chunk = BytesIO()
             self.gotIAC = 1
         else:
             chunk.write(char)
     # chunks are of a relatively indeterminate size.
     c = chunk.getvalue()
     if c:
         why = self.processChunk(c)
         if why:
             return why
    def test_insert(self):
        """
        When not in INSERT mode, L{HistoricRecvLine} inserts the typed
        character at the cursor before the next character.
        """
        kR = lambda ch: self.p.keystrokeReceived(ch, None)

        for ch in iterbytes(b'xyz'):
            kR(ch)

        kR(self.pt.LEFT_ARROW)
        kR(b'A')
        self.assertEqual(self.p.currentLineBuffer(), (b'xyA', b'z'))

        kR(self.pt.LEFT_ARROW)
        kR(b'B')
        self.assertEqual(self.p.currentLineBuffer(), (b'xyB', b'Az'))
Example #45
0
def _validateChecksum(sentence):
    """
    Validates the checksum of an NMEA sentence.

    @param sentence: The NMEA sentence to check the checksum of.
    @type sentence: C{bytes}

    @raise ValueError: If the sentence has an invalid checksum.

    Simply returns on sentences that either don't have a checksum,
    or have a valid checksum.
    """
    if sentence[-3:-2] == b"*":  # Sentence has a checksum
        reference, source = int(sentence[-2:], 16), sentence[1:-3]
        computed = reduce(operator.xor, [ord(x) for x in iterbytes(source)])
        if computed != reference:
            raise base.InvalidChecksum(f"{computed:02x} != {reference:02x}")
Example #46
0
    def test_insert(self):
        """
        When not in INSERT mode, L{HistoricRecvLine} inserts the typed
        character at the cursor before the next character.
        """
        kR = lambda ch: self.p.keystrokeReceived(ch, None)

        for ch in iterbytes(b'xyz'):
            kR(ch)

        kR(self.pt.LEFT_ARROW)
        kR(b'A')
        self.assertEqual(self.p.currentLineBuffer(), (b'xyA', b'z'))

        kR(self.pt.LEFT_ARROW)
        kR(b'B')
        self.assertEqual(self.p.currentLineBuffer(), (b'xyB', b'Az'))
Example #47
0
def _validateChecksum(sentence):
    """
    Validates the checksum of an NMEA sentence.

    @param sentence: The NMEA sentence to check the checksum of.
    @type sentence: C{bytes}

    @raise ValueError: If the sentence has an invalid checksum.

    Simply returns on sentences that either don't have a checksum,
    or have a valid checksum.
    """
    if sentence[-3:-2] == b'*':  # Sentence has a checksum
        reference, source = int(sentence[-2:], 16), sentence[1:-3]
        computed = reduce(operator.xor, [ord(x) for x in iterbytes(source)])
        if computed != reference:
            raise base.InvalidChecksum("%02x != %02x" % (computed, reference))
Example #48
0
def b1282int(st):
    """
    Convert an integer represented as a base 128 string into an L{int}.

    @param st: The integer encoded in a byte string.
    @type st: L{bytes}

    @return: The integer value extracted from the byte string.
    @rtype: L{int}
    """
    e = 1
    i = 0
    for char in iterbytes(st):
        n = ord(char)
        i += n * e
        e <<= 7
    return i
Example #49
0
    def test_socks4aSuccessfulResolution(self):
        """
        If the destination IP address has zeros for the first three octets and
        non-zero for the fourth octet, the client is attempting a v4a
        connection.  A hostname is specified after the user ID string and the
        server connects to the address that hostname resolves to.

        @see: U{http://en.wikipedia.org/wiki/SOCKS#SOCKS_4a_protocol}
        """
        # send the domain name "localhost" to be resolved
        clientRequest = (
            struct.pack('!BBH', 4, 1, 34)
            + socket.inet_aton('0.0.0.1')
            + b'fooBAZ\0'
            + b'localhost\0')

        # Deliver the bytes one by one to exercise the protocol's buffering
        # logic. FakeResolverReactor's resolve method is invoked to "resolve"
        # the hostname.
        for byte in iterbytes(clientRequest):
            self.sock.dataReceived(byte)

        sent = self.sock.transport.value()
        self.sock.transport.clear()

        # Verify that the server responded with the address which will be
        # connected to.
        self.assertEqual(
            sent,
            struct.pack('!BBH', 0, 90, 34) + socket.inet_aton('127.0.0.1'))
        self.assertFalse(self.sock.transport.stringTCPTransport_closing)
        self.assertIsNotNone(self.sock.driver_outgoing)

        # Pass some data through and verify it is forwarded to the outgoing
        # connection.
        self.sock.dataReceived(b'hello, world')
        self.assertEqual(
            self.sock.driver_outgoing.transport.value(), b'hello, world')

        # Deliver some data from the output connection and verify it is
        # passed along to the incoming side.
        self.sock.driver_outgoing.dataReceived(b'hi there')
        self.assertEqual(self.sock.transport.value(), b'hi there')

        self.sock.connectionLost('fake reason')
Example #50
0
    def testBoundarySubnegotiation(self):
        # Send a subnegotiation command.  Split it at every possible byte boundary
        # and make sure it always gets parsed and that it is passed to the correct
        # method.
        cmd = telnet.IAC + telnet.SB + b"\x12" + telnet.SE + b"hello" + telnet.IAC + telnet.SE

        for i in range(len(cmd)):
            h = self.p.protocol = TestProtocol()
            h.makeConnection(self.p)

            a, b = cmd[:i], cmd[i:]
            L = [b"first part" + a, b + b"last part"]

            for bytes in L:
                self.p.dataReceived(bytes)

            self.assertEqual(h.bytes, b"".join(L).replace(cmd, b""))
            self.assertEqual(h.subcmd, [telnet.SE] + list(iterbytes(b"hello")))
Example #51
0
def b1282int(st):
    """
    Convert an integer represented as a base 128 string into an L{int} or
    L{long}.

    @param st: The integer encoded in a byte string.
    @type st: L{bytes}

    @return: The integer value extracted from the byte string.
    @rtype: L{int} or L{long}
    """
    e = 1
    i = 0
    for char in iterbytes(st):
        n = ord(char)
        i += (n * e)
        e <<= 7
    return i
Example #52
0
    def test_doReadSeveralDatagrams(self):
        """
        L{TuntapPort.doRead} reads several datagrams, of up to
        C{TuntapPort.maxThroughput} bytes total, before returning.
        """
        values = cycle(iterbytes(b'abcdefghijklmnopqrstuvwxyz'))
        total = 0
        datagrams = []
        while total < self.port.maxThroughput:
            datagrams.append(next(values) * self.port.maxPacketSize)
            total += self.port.maxPacketSize

        self.port.startListening()
        tunnel = self.system.getTunnel(self.port)
        tunnel.readBuffer.extend(datagrams)
        tunnel.readBuffer.append(b'excessive datagram, not to be read')

        self.port.doRead()
        self.assertEqual(datagrams, self.protocol.received)
Example #53
0
    def testBoundarySubnegotiation(self):
        # Send a subnegotiation command.  Split it at every possible byte boundary
        # and make sure it always gets parsed and that it is passed to the correct
        # method.
        cmd = (telnet.IAC + telnet.SB + b'\x12' + telnet.SE + b'hello' +
               telnet.IAC + telnet.SE)

        for i in range(len(cmd)):
            h = self.p.protocol = TestProtocol()
            h.makeConnection(self.p)

            a, b = cmd[:i], cmd[i:]
            L = [b"first part" + a, b + b"last part"]

            for bytes in L:
                self.p.dataReceived(bytes)

            self.assertEqual(h.bytes, b''.join(L).replace(cmd, b''))
            self.assertEqual(h.subcmd, [telnet.SE] + list(iterbytes(b'hello')))
Example #54
0
    def test_doReadSeveralDatagrams(self):
        """
        L{TuntapPort.doRead} reads several datagrams, of up to
        C{TuntapPort.maxThroughput} bytes total, before returning.
        """
        values = cycle(iterbytes(b'abcdefghijklmnopqrstuvwxyz'))
        total = 0
        datagrams = []
        while total < self.port.maxThroughput:
            datagrams.append(next(values) * self.port.maxPacketSize)
            total += self.port.maxPacketSize

        self.port.startListening()
        tunnel = self.system.getTunnel(self.port)
        tunnel.readBuffer.extend(datagrams)
        tunnel.readBuffer.append(b'excessive datagram, not to be read')

        self.port.doRead()
        self.assertEqual(datagrams, self.protocol.received)
Example #55
0
    def test_backspace(self):
        """
        When L{HistoricRecvLine} receives a BACKSPACE keystroke it deletes
        the character immediately before the cursor.
        """
        kR = lambda ch: self.p.keystrokeReceived(ch, None)

        for ch in iterbytes(b'xyz'):
            kR(ch)
        self.assertEqual(self.p.currentLineBuffer(), (b'xyz', b''))

        kR(self.pt.BACKSPACE)
        self.assertEqual(self.p.currentLineBuffer(), (b'xy', b''))

        kR(self.pt.LEFT_ARROW)
        kR(self.pt.BACKSPACE)
        self.assertEqual(self.p.currentLineBuffer(), (b'', b'y'))

        kR(self.pt.BACKSPACE)
        self.assertEqual(self.p.currentLineBuffer(), (b'', b'y'))
    def test_backspace(self):
        """
        When L{HistoricRecvLine} receives a BACKSPACE keystroke it deletes
        the character immediately before the cursor.
        """
        kR = lambda ch: self.p.keystrokeReceived(ch, None)

        for ch in iterbytes(b'xyz'):
            kR(ch)
        self.assertEqual(self.p.currentLineBuffer(), (b'xyz', b''))

        kR(self.pt.BACKSPACE)
        self.assertEqual(self.p.currentLineBuffer(), (b'xy', b''))

        kR(self.pt.LEFT_ARROW)
        kR(self.pt.BACKSPACE)
        self.assertEqual(self.p.currentLineBuffer(), (b'', b'y'))

        kR(self.pt.BACKSPACE)
        self.assertEqual(self.p.currentLineBuffer(), (b'', b'y'))
Example #57
0
    def test_typeover(self):
        """
        When in INSERT mode and upon receiving a keystroke with a printable
        character, L{HistoricRecvLine} replaces the character at
        the cursor with the typed character rather than inserting before.
        Ah, the ironies of INSERT mode.
        """
        kR = lambda ch: self.p.keystrokeReceived(ch, None)

        for ch in iterbytes(b'xyz'):
            kR(ch)

        kR(self.pt.INSERT)

        kR(self.pt.LEFT_ARROW)
        kR(b'A')
        self.assertEqual(self.p.currentLineBuffer(), (b'xyA', b''))

        kR(self.pt.LEFT_ARROW)
        kR(b'B')
        self.assertEqual(self.p.currentLineBuffer(), (b'xyB', b''))
    def test_typeover(self):
        """
        When in INSERT mode and upon receiving a keystroke with a printable
        character, L{HistoricRecvLine} replaces the character at
        the cursor with the typed character rather than inserting before.
        Ah, the ironies of INSERT mode.
        """
        kR = lambda ch: self.p.keystrokeReceived(ch, None)

        for ch in iterbytes(b'xyz'):
            kR(ch)

        kR(self.pt.INSERT)

        kR(self.pt.LEFT_ARROW)
        kR(b'A')
        self.assertEqual(self.p.currentLineBuffer(), (b'xyA', b''))

        kR(self.pt.LEFT_ARROW)
        kR(b'B')
        self.assertEqual(self.p.currentLineBuffer(), (b'xyB', b''))
Example #59
0
    def test_verticalArrows(self):
        """
        When L{HistoricRecvLine} receives UP_ARROW or DOWN_ARROW
        keystrokes it move the current index in the current history
        buffer up or down, and resets the current line buffer to the
        previous or next line in history, respectively for each.
        """
        kR = lambda ch: self.p.keystrokeReceived(ch, None)

        for ch in iterbytes(b'xyz\nabc\n123\n'):
            kR(ch)

        self.assertEqual(self.p.currentHistoryBuffer(),
                         ((b'xyz', b'abc', b'123'), ()))
        self.assertEqual(self.p.currentLineBuffer(), (b'', b''))

        kR(self.pt.UP_ARROW)
        self.assertEqual(self.p.currentHistoryBuffer(),
                         ((b'xyz', b'abc'), (b'123', )))
        self.assertEqual(self.p.currentLineBuffer(), (b'123', b''))

        kR(self.pt.UP_ARROW)
        self.assertEqual(self.p.currentHistoryBuffer(),
                         ((b'xyz', ), (b'abc', b'123')))
        self.assertEqual(self.p.currentLineBuffer(), (b'abc', b''))

        kR(self.pt.UP_ARROW)
        self.assertEqual(self.p.currentHistoryBuffer(),
                         ((), (b'xyz', b'abc', b'123')))
        self.assertEqual(self.p.currentLineBuffer(), (b'xyz', b''))

        kR(self.pt.UP_ARROW)
        self.assertEqual(self.p.currentHistoryBuffer(),
                         ((), (b'xyz', b'abc', b'123')))
        self.assertEqual(self.p.currentLineBuffer(), (b'xyz', b''))

        for i in range(4):
            kR(self.pt.DOWN_ARROW)
        self.assertEqual(self.p.currentHistoryBuffer(),
                         ((b'xyz', b'abc', b'123'), ()))
Example #60
0
    def __repr__(self):
        """
        Return a pretty representation of this object.
        """
        if self.type() == 'EC':
            data = self.data()
            name = data['curve'].decode('utf-8')

            if self.isPublic():
                out = '<Elliptic Curve Public Key (%s bits)' % (name[-3:],)
            else:
                out = '<Elliptic Curve Private Key (%s bits)' % (name[-3:],)

            for k, v in sorted(data.items()):
                if _PY3 and k == 'curve':
                    out += "\ncurve:\n\t%s" % (name,)
                else:
                    out += "\n%s:\n\t%s" % (k, v)

            return out + ">\n"
        else:
            lines = [
                '<%s %s (%s bits)' % (
                    nativeString(self.type()),
                    self.isPublic() and 'Public Key' or 'Private Key',
                    self._keyObject.key_size)]
            for k, v in sorted(self.data().items()):
                lines.append('attr %s:' % (k,))
                by = common.MP(v)[4:]
                while by:
                    m = by[:15]
                    by = by[15:]
                    o = ''
                    for c in iterbytes(m):
                        o = o + '%02x:' % (ord(c),)
                    if len(m) < 15:
                        o = o[:-1]
                    lines.append('\t' + o)
            lines[-1] = lines[-1] + '>'
            return '\n'.join(lines)