Exemplo n.º 1
0
 def test_reader_read_error(self):
     s = BytesIO(b"foobar\nfoobar")
     s = tcp.Reader(s)
     o = mock.MagicMock()
     o.read = mock.MagicMock(side_effect=socket.error)
     s.o = o
     tutils.raises(exceptions.TcpDisconnect, s.read, 10)
Exemplo n.º 2
0
 def test_first_byte_timestamp_updated_on_readline(self):
     s = BytesIO(b"foobar\nfoobar\nfoobar")
     s = tcp.Reader(s)
     s.readline()
     assert s.first_byte_timestamp
     expected = s.first_byte_timestamp
     s.readline()
     assert s.first_byte_timestamp == expected
Exemplo n.º 3
0
 def test_wrap(self):
     s = BytesIO(b"foobar\nfoobar")
     s.flush()
     s = tcp.Reader(s)
     assert s.readline() == b"foobar\n"
     assert s.readline() == b"foobar"
     # Test __getattr__
     assert s.isatty
Exemplo n.º 4
0
 def test_blocksize(self):
     s = BytesIO(b"1234567890abcdefghijklmnopqrstuvwxyz")
     s = tcp.Reader(s)
     s.BLOCKSIZE = 2
     assert s.read(1) == b"1"
     assert s.read(2) == b"23"
     assert s.read(3) == b"456"
     assert s.read(4) == b"7890"
     d = s.read(-1)
     assert d.startswith(b"abc") and d.endswith(b"xyz")
Exemplo n.º 5
0
 def test_readlog(self):
     s = BytesIO(b"foobar\nfoobar")
     s = tcp.Reader(s)
     assert not s.is_logging()
     s.start_log()
     assert s.is_logging()
     s.readline()
     assert s.get_log() == b"foobar\n"
     s.read(1)
     assert s.get_log() == b"foobar\nf"
     s.start_log()
     assert s.get_log() == b""
     s.read(1)
     assert s.get_log() == b"o"
     s.stop_log()
     tutils.raises(ValueError, s.get_log)
Exemplo n.º 6
0
 def test_reader_incomplete_error(self):
     s = BytesIO(b"foobar")
     s = tcp.Reader(s)
     with pytest.raises(exceptions.TcpReadIncomplete):
         s.safe_read(10)
Exemplo n.º 7
0
 def test_reader_readline_disconnect(self):
     o = mock.MagicMock()
     o.read = mock.MagicMock(side_effect=socket.error)
     s = tcp.Reader(o)
     with pytest.raises(exceptions.TcpDisconnect):
         s.readline(10)
Exemplo n.º 8
0
 def test_read_syscall_ssl_error(self):
     s = mock.MagicMock()
     s.read = mock.MagicMock(side_effect=SSL.SysCallError())
     s = tcp.Reader(s)
     with pytest.raises(exceptions.TlsException):
         s.read(1)
Exemplo n.º 9
0
 def test_reset_timestamps(self):
     s = BytesIO(b"foobar\nfoobar")
     s = tcp.Reader(s)
     s.first_byte_timestamp = 500
     s.reset_timestamps()
     assert not s.first_byte_timestamp
Exemplo n.º 10
0
 def test_limitless(self):
     s = BytesIO(b"f" * (50 * 1024))
     s = tcp.Reader(s)
     ret = s.read(-1)
     assert len(ret) == 50 * 1024
Exemplo n.º 11
0
 def test_limit(self):
     s = BytesIO(b"foobar\nfoobar")
     s = tcp.Reader(s)
     assert s.readline(3) == b"foo"
Exemplo n.º 12
0
def treader(bytes):
    """
        Construct a tcp.Read object from bytes.
    """
    fp = io.BytesIO(bytes)
    return tcp.Reader(fp)
Exemplo n.º 13
0
 def test_reader_incomplete_error(self):
     s = BytesIO(b"foobar")
     s = tcp.Reader(s)
     tutils.raises(exceptions.TcpReadIncomplete, s.safe_read, 10)
Exemplo n.º 14
0
 def test_reader_readline_disconnect(self):
     o = mock.MagicMock()
     o.read = mock.MagicMock(side_effect=socket.error)
     s = tcp.Reader(o)
     tutils.raises(exceptions.TcpDisconnect, s.readline, 10)
Exemplo n.º 15
0
 def test_read_ssl_error(self):
     s = mock.MagicMock()
     s.read = mock.MagicMock(side_effect=SSL.Error())
     s = tcp.Reader(s)
     tutils.raises(exceptions.TlsException, s.read, 1)
Exemplo n.º 16
0
 def from_bytes(cls, bytestring):
     """
       Construct a websocket frame from an in-memory bytestring
       to construct a frame from a stream of bytes, use from_file() directly
     """
     return cls.from_file(tcp.Reader(io.BytesIO(bytestring)))