Exemple #1
0
    def testDuplicate(self):

        b = Buffer(1024)
        c = b.duplicate()
        
        self.assertEqual(b.position, c.position)
        self.assertEqual(b.limit, c.limit)
        self.assertEqual(b.capacity, c.capacity)

        b.write_bytes('test')

        #check that position is independend from c
        self.assertNotEqual(b.position, c.position)
        self.assertEqual(b.limit, c.limit)
        self.assertEqual(b.capacity, c.capacity)

        #but data buffer should be shared with c, so c should be able to read it
        self.assertEquals('test', c.read_bytes(4))

        #if we delete b, c should still be able to reference the buffer, (it will keep b around
        #until it itself is released
        del b
        c.clear()
        self.assertEquals('test', c.read_bytes(4))
        del c #this releases buffer b as well, we cannot test this, but this should not crash :-)
Exemple #2
0
    def testDuplicate(self):

        b = Buffer(1024)
        c = b.duplicate()

        self.assertEqual(b.position, c.position)
        self.assertEqual(b.limit, c.limit)
        self.assertEqual(b.capacity, c.capacity)

        b.write_bytes('test')

        #check that position is independend from c
        self.assertNotEqual(b.position, c.position)
        self.assertEqual(b.limit, c.limit)
        self.assertEqual(b.capacity, c.capacity)

        #but data buffer should be shared with c, so c should be able to read it
        self.assertEquals('test', c.read_bytes(4))

        #if we delete b, c should still be able to reference the buffer, (it will keep b around
        #until it itself is released
        del b
        c.clear()
        self.assertEquals('test', c.read_bytes(4))
        del c  #this releases buffer b as well, we cannot test this, but this should not crash :-)
Exemple #3
0
 def testString1(self):
     S = 'Henk\0Punt'
     b = Buffer(1024)
     self.assertEqual(0, b.position)
     self.assertEqual(1024, b.limit)                
     b.write_bytes(S)
     self.assertEqual(9, b.position)
     self.assertEqual(1024, b.limit)
     b.flip()                
     self.assertEqual(0, b.position)
     self.assertEqual(9, b.limit)
Exemple #4
0
    def testSlice(self):
        b = Buffer(1024)
        for i in range(1024):
            b[i] = i % 256

        self.assertEquals(0, b[0])
        self.assertEquals(255, b[1023])
        self.assertEquals(
            '\x00\x01\x02\x03\x04\x05\x06\x07\x08\t\n\x0b\x0c\r\x0e\x0f',
            b[0:16])
        self.assertEquals(
            '\xf0\xf1\xf2\xf3\xf4\xf5\xf6\xf7\xf8\xf9\xfa\xfb\xfc\xfd\xfe\xff',
            b[-16:])

        try:
            b[0:16] = '1234567890123456xxx'
            self.fail()
        except BufferInvalidArgumentError:
            pass

        b[0:16] = '1234567890123456'
        b[16:32] = '1234567890123456'
        b[-16:] = '1234567890123456'
        self.assertEquals('1234567890123456', b[0:16])
        self.assertEquals('1234567890123456', b[16:32])
        self.assertEquals('1234567890123456', b[-16:])
Exemple #5
0
    def testCompatibleReadLines(self):

        for chunk_size in [4, 8, 16, 32, 64, 128]:
            b = Buffer(1024)

            test_stream = TestStream(
                'hello world!\nTest\n\nPiet\nKlaas\nPietBlaat\n',
                chunk_size=chunk_size)

            f = BufferedReader(test_stream, b).file()

            lines = f.readlines()

            self.assertEquals('hello world!\n', lines.next())
            self.assertEquals('Test\n', lines.next())
            self.assertEquals('\n', lines.next())
            self.assertEquals('Piet\n', lines.next())
            self.assertEquals('Klaas\n', lines.next())
            self.assertEquals('PietBlaat\n', lines.next())
            self.assertEquals('', lines.next())

            #line without newline at end of buffer, should report without newline
            test_stream = TestStream('hello world!\nTest',
                                     chunk_size=chunk_size)
            f = BufferedReader(test_stream, b).file()
            lines = f.readlines()
            self.assertEquals('hello world!\n', lines.next())
            self.assertEquals('Test', lines.next())
            self.assertEquals('', lines.next())
Exemple #6
0
 def testString2(self):
     b = Buffer(1024)
     self.assertEqual(0, b.position)
     self.assertEqual(1024, b.limit)
     self.assertEqual('\0' * 10, b.read_bytes(10))
     b.clear()
     b.flip()
     #try read string past limit
     try:
         b.read_bytes(10)
         self.fail("expected buffer underflow")
     except BufferUnderflowError:
         pass  #expected
Exemple #7
0
 def testString2(self):
     b = Buffer(1024)
     self.assertEqual(0, b.position)
     self.assertEqual(1024, b.limit)                
     self.assertEqual('\0' * 10, b.read_bytes(10))
     b.clear()
     b.flip()
     #try read string past limit
     try:
         b.read_bytes(10)
         self.fail("expected buffer underflow")
     except BufferUnderflowError:
         pass #expected
Exemple #8
0
    def testGetSetItem(self):
        b = Buffer(1024)

        try:
            x = b[-1]
            self.fail()
        except BufferInvalidArgumentError:
            pass
        x = b[0]
        x = b[1023]
        try:
            x = b[1024]
            self.fail()
        except BufferInvalidArgumentError:
            pass
        try:
            x = b[1025]
            self.fail()
        except BufferInvalidArgumentError:
            pass

        try:
            b[-1] = 1
            self.fail()
        except BufferInvalidArgumentError:
            pass
        b[0] = 1
        b[1023] = 1
        try:
            b[1024] = 1
            self.fail()
        except BufferInvalidArgumentError:
            pass
        try:
            b[1025] = 1
            self.fail()
        except BufferInvalidArgumentError:
            pass

        try:
            b[0] = -1
            self.fail()
        except BufferInvalidArgumentError:
            pass

        try:
            b[0] = 256
            self.fail()
        except BufferInvalidArgumentError:
            pass
Exemple #9
0
 def testString1(self):
     S = 'Henk\0Punt'
     b = Buffer(1024)
     self.assertEqual(0, b.position)
     self.assertEqual(1024, b.limit)
     b.write_bytes(S)
     self.assertEqual(9, b.position)
     self.assertEqual(1024, b.limit)
     b.flip()
     self.assertEqual(0, b.position)
     self.assertEqual(9, b.limit)
Exemple #10
0
    def testBytes(self):
        #test that we can write all bytes into buffer
        #and get them back without any encoding/decoding stuff going on

        #a string with all possible bytes
        B = ''.join([chr(i) for i in range(256)])

        b = Buffer(1024)
        b.write_bytes(B)
        b.flip()

        self.assertEqual(256, b.limit)
        x = b.read_bytes(256)

        self.assertEquals(B, x)
Exemple #11
0
    def testBytes(self):
        #test that we can write all bytes into buffer
        #and get them back without any encoding/decoding stuff going on

        #a string with all possible bytes
        B = ''.join([chr(i) for i in range(256)])
        
        b = Buffer(1024)
        b.write_bytes(B)
        b.flip()
        
        self.assertEqual(256, b.limit)
        x = b.read_bytes(256)

        self.assertEquals(B, x)
Exemple #12
0
    def testCopy(self):
        b = Buffer(1024)
        c = Buffer(1024)
        
        try:
            c.copy(b, 0, 0, -1)
            self.fail()
        except BufferInvalidArgumentError:
            pass

        try:
            c.copy(b, 0, 0, 1025)
            self.fail()
        except BufferInvalidArgumentError:
            pass

        try:
            c.copy(b, 1, 0, 1024)
            self.fail()
        except BufferInvalidArgumentError:
            pass

        try:
            c.copy(b, 0, 1, 1024)
            self.fail()
        except BufferInvalidArgumentError:
            pass

        b[0] = 1
        b[10] = 2
        b[1023] = 3
        c.copy(b, 0, 0, 1024)
        self.assertEquals(1, c[0])
        self.assertEquals(2, c[10])
        self.assertEquals(3, c[1023])
        
        c.copy(b, 0, 10, 20)
        self.assertEquals(1, c[10])
        self.assertEquals(2, c[20])
        self.assertEquals(3, c[1023])
Exemple #13
0
    def testReadLine(self):
        b = Buffer(1024)
        
        b.clear()
        b.write_bytes('hello world!\n')
        b.flip()
        self.assertEquals('hello world!', b.read_line())
        self.assertEquals(b.position, b.limit)

        b.clear()
        b.write_bytes('hello world!\r\n')
        b.flip()
        self.assertEquals('hello world!', b.read_line())
        self.assertEquals(b.position, b.limit)

        b.clear()
        b.write_bytes('hello world!\r')
        b.flip()
        try:
            b.read_line()
            self.fail('expected BufferUnderFlow')
        except BufferUnderflowError:
            pass
        

        b.clear()
        b.write_bytes('hello world!\n')
        b.flip()
        self.assertEquals('hello world!\n', b.read_line(True))
        self.assertEquals(b.position, b.limit)

        b.clear()
        b.write_bytes('hello world!\r\n')
        b.flip()
        self.assertEquals('hello world!\r\n', b.read_line(True))
        self.assertEquals(b.position, b.limit)

        b.clear()
        b.write_bytes('hello world!\r')
        b.flip()
        try:
            b.read_line(True)
            self.fail('expected BufferUnderFlow')
        except BufferUnderflowError:
            pass
        
        b.clear()
        b.write_bytes('line1\nline2\r\nline3\nline4\r\n')
        b.flip()
        self.assertEquals('line1', b.read_line())
        self.assertEquals('line2', b.read_line())
        self.assertEquals('line3', b.read_line())
        self.assertEquals('line4', b.read_line())
        self.assertEquals(b.position, b.limit)
        
        b.clear()
        b.write_bytes('line1\nline2\r\nline3\nline4\r\n')
        b.flip()
        self.assertEquals('line1\n', b.read_line(True))
        self.assertEquals('line2\r\n', b.read_line(True))
        self.assertEquals('line3\n', b.read_line(True))
        self.assertEquals('line4\r\n', b.read_line(True))
        self.assertEquals(b.position, b.limit)
Exemple #14
0
    def testReadBytesUntil(self):
        b = Buffer(1024)
        b.write_bytes('hello world!')
        b.flip()
        self.assertEquals('hello', b.read_bytes_until(ord(' ')))
        self.assertEquals('world', b.read_bytes_until(ord('!')))

        b = Buffer(1024)
        b.write_bytes('hello world!\nTest\n\nPiet\nKlaas\n')
        b.flip()
        self.assertEquals('hello world!', b.read_bytes_until(10))
        self.assertEquals('Test', b.read_bytes_until(10))
        self.assertEquals('', b.read_bytes_until(10))
        self.assertEquals('Piet', b.read_bytes_until(10))
        self.assertEquals('Klaas', b.read_bytes_until(10))
        try:
            b.read_bytes_until(10)
            self.fail("expected buffer underflow")
        except BufferUnderflowError:
            pass
Exemple #15
0
 def testInt(self):
     b = Buffer(1024)
     b.write_int(0x4A3B2C1D)
     #write_int is little-endian
     self.assertEquals(['0x1d', '0x2c', '0x3b', '0x4a'], [hex(b[i]) for i in range(4)])
Exemple #16
0
 def test_stream(s):
     b = Buffer(buffer_size)
     f = BufferedReader(TestStream(s, chunk_size=chunk_size),
                        b).file()
     i = cStringIO.StringIO(s)
     return (i, f)
Exemple #17
0
 def testInt(self):
     b = Buffer(1024)
     b.write_int(0x4A3B2C1D)
     #write_int is little-endian
     self.assertEquals(['0x1d', '0x2c', '0x3b', '0x4a'],
                       [hex(b[i]) for i in range(4)])
Exemple #18
0
    def testReadBytesUntil(self):
        b = Buffer(1024)
        b.write_bytes('hello world!')
        b.flip()
        self.assertEquals('hello', b.read_bytes_until(ord(' ')))
        self.assertEquals('world', b.read_bytes_until(ord('!')))

        b = Buffer(1024)
        b.write_bytes('hello world!\nTest\n\nPiet\nKlaas\n')
        b.flip()
        self.assertEquals('hello world!', b.read_bytes_until(10))
        self.assertEquals('Test', b.read_bytes_until(10))
        self.assertEquals('', b.read_bytes_until(10))
        self.assertEquals('Piet', b.read_bytes_until(10))
        self.assertEquals('Klaas', b.read_bytes_until(10))
        try:
            b.read_bytes_until(10)
            self.fail("expected buffer underflow")
        except BufferUnderflowError:
            pass
Exemple #19
0
    def testCopy(self):
        b = Buffer(1024)
        c = Buffer(1024)

        try:
            c.copy(b, 0, 0, -1)
            self.fail()
        except BufferInvalidArgumentError:
            pass

        try:
            c.copy(b, 0, 0, 1025)
            self.fail()
        except BufferInvalidArgumentError:
            pass

        try:
            c.copy(b, 1, 0, 1024)
            self.fail()
        except BufferInvalidArgumentError:
            pass

        try:
            c.copy(b, 0, 1, 1024)
            self.fail()
        except BufferInvalidArgumentError:
            pass

        b[0] = 1
        b[10] = 2
        b[1023] = 3
        c.copy(b, 0, 0, 1024)
        self.assertEquals(1, c[0])
        self.assertEquals(2, c[10])
        self.assertEquals(3, c[1023])

        c.copy(b, 0, 10, 20)
        self.assertEquals(1, c[10])
        self.assertEquals(2, c[20])
        self.assertEquals(3, c[1023])
Exemple #20
0
    def testReadLine(self):
        b = Buffer(1024)

        b.clear()
        b.write_bytes('hello world!\n')
        b.flip()
        self.assertEquals('hello world!', b.read_line())
        self.assertEquals(b.position, b.limit)

        b.clear()
        b.write_bytes('hello world!\r\n')
        b.flip()
        self.assertEquals('hello world!', b.read_line())
        self.assertEquals(b.position, b.limit)

        b.clear()
        b.write_bytes('hello world!\r')
        b.flip()
        try:
            b.read_line()
            self.fail('expected BufferUnderFlow')
        except BufferUnderflowError:
            pass

        b.clear()
        b.write_bytes('hello world!\n')
        b.flip()
        self.assertEquals('hello world!\n', b.read_line(True))
        self.assertEquals(b.position, b.limit)

        b.clear()
        b.write_bytes('hello world!\r\n')
        b.flip()
        self.assertEquals('hello world!\r\n', b.read_line(True))
        self.assertEquals(b.position, b.limit)

        b.clear()
        b.write_bytes('hello world!\r')
        b.flip()
        try:
            b.read_line(True)
            self.fail('expected BufferUnderFlow')
        except BufferUnderflowError:
            pass

        b.clear()
        b.write_bytes('line1\nline2\r\nline3\nline4\r\n')
        b.flip()
        self.assertEquals('line1', b.read_line())
        self.assertEquals('line2', b.read_line())
        self.assertEquals('line3', b.read_line())
        self.assertEquals('line4', b.read_line())
        self.assertEquals(b.position, b.limit)

        b.clear()
        b.write_bytes('line1\nline2\r\nline3\nline4\r\n')
        b.flip()
        self.assertEquals('line1\n', b.read_line(True))
        self.assertEquals('line2\r\n', b.read_line(True))
        self.assertEquals('line3\n', b.read_line(True))
        self.assertEquals('line4\r\n', b.read_line(True))
        self.assertEquals(b.position, b.limit)
Exemple #21
0
 def _get_socket(self, host, port, timeout):
     new_socket = Socket.connect((host, port))
     self._reader = BufferedReader(new_socket, Buffer(1024))
     self._writer = BufferedWriter(new_socket, Buffer(1024))
     self.file = self._reader.file()
     return new_socket
Exemple #22
0
 def _send(self, identifier, body, timeout=30):
     try:
         conn = Socket.connect((self.host, self.port), timeout)
         try:
             req = u"POST / HTTP/1.1\r\nHost: %s\r\nContent-length: %d\r\nX-Realplexor: %s=%s%s\r\n\r\n%s" % (
                 self.host, len(body), (self.identifier or ""),
                 ("%s:%s@" %
                  (self.login, self.password) if self.login else ""),
                 identifier or "", body)
             if type(req) == unicode:
                 req = req.encode("utf-8")
             reader = BufferedReader(conn, Buffer(1024))
             writer = BufferedWriter(conn, Buffer(1024))
             writer.write_bytes(req)
             writer.flush()
             conn.socket.shutdown(socket.SHUT_WR)
             response = ""
             while True:
                 try:
                     chunk = reader.read_bytes_available()
                 except EOFError:
                     chunk = None
                 if not chunk:
                     break
                 response += chunk
             if response:
                 m = re_split_headers.split(response, 1)
                 if not m:
                     raise RealplexorError(
                         "Non-HTTP response received:\n%s" % response)
                 headers = m[0]
                 body = m[1]
                 m = re_http_status_line.match(headers)
                 if not m:
                     raise RealplexorError(
                         "Non-HTTP response received:\n%s" % response)
                 status, code = m.group(1, 2)
                 if code != "200":
                     raise RealplexorError("Request failed: %s\n%s" %
                                           (status, body))
                 m = re_content_length.search(headers)
                 if not m:
                     raise RealplexorError(
                         "No Content-Length header in response headers:\n%s"
                         % headers)
                 expected_len = int(m.group(1))
                 if len(body) != expected_len:
                     raise RealplexorError(
                         "Response length (%d) is different from one specified in the Content-Length header (%d): possibly broken response"
                         % (len(body), expected_len))
                 return body
             return response
         finally:
             conn.close()
     except socket.error as e:
         raise RealplexorSocketError("Socket error: %s" % e)
     except socket.gaierror as e:
         raise RealplexorError("DNS error: %s" % e)
     except socket.timeout as e:
         raise RealplexorError("Timeout error: %s" % e)
     except IOError as e:
         raise RealplexorSocketError("IOError: %s" % e)