async def test_read_pbmsg_safe_valid(msg_bytes): s = MockReaderWriter() write_unsigned_varint(s, len(msg_bytes)) s.write(msg_bytes) # reset the offset back to the beginning s.seek(0, 0) pb_msg = p2pd_pb.Response() await read_pbmsg_safe(s, pb_msg) assert pb_msg.SerializeToString() == msg_bytes
def test_raise_if_failed_raises(): resp = p2pd_pb.Response() resp.type = p2pd_pb.Response.ERROR with pytest.raises(ControlFailure): raise_if_failed(resp)
def test_raise_if_failed_not_raises(): resp = p2pd_pb.Response() resp.type = p2pd_pb.Response.OK raise_if_failed(resp)
async def handler_stream(reader, _): pb_msg = p2pd_pb.Response() try: await read_pbmsg_safe(reader, pb_msg) except asyncio.IncompleteReadError: event.set()
writer.close() try: await asyncio.wait_for(event.wait(), timeout=5) except asyncio.TimeoutError: assert False, "timeout before `handler_stream` is called" assert event.is_set() sock.close() await sock.wait_closed() @pytest.mark.parametrize( "pb_msg, msg_bytes", ( (p2pd_pb.Response(), b'Z\x08\x00*V\x08\x01\x12R\n"\x12 \x03\x8d\xf5\xd4(/#\xd6\xed\xa5\x1bU\xb8s\x8c\xfa\xad\xfc{\x04\xe3\xecw\xdeK\xc9,\xfe\x9c\x00:\xc8\x12\x02\xa2\x02\x12\x08\x04\x7f\x00\x00\x01\x06\xdea\x12\x08\x04\xc0\xa8\n\x87\x06\xdea\x12\x14)\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01\x06\xdeb' ), # noqa: E501 (p2pd_pb.Request(), b'\x02\x08\x05'), (p2pd_pb.DHTRequest(), b'&\x08\x00\x12"\x12 \xd5\x0b\x18/\x9e\xa5G\x06.\xdd\xebW\xf0N\xf5\x0eW\xd3\xec\xdf\x06\x02\xe2\x89\x1e\xf0\xbb.\xc0\xbdE\xb8' ), # noqa: E501 (p2pd_pb.DHTResponse(), b'V\x08\x01\x12R\n"\x12 wy\xe2\xfa\x11\x9e\xe2\x84X]\x84\xf8\x98\xba\x8c\x8cQ\xd7,\xb59\x1e!G\x92\x86G{\x141\xe9\x1b\x12\x02\xa2\x02\x12\x08\x04\x7f\x00\x00\x01\x06\xdeA\x12\x08\x04\xc0\xa8\n\x87\x06\xdeA\x12\x14)\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01\x06\xdeB' ), # noqa: E501 (p2pd_pb.StreamInfo(), b';\n"\x12 \xf6\x9e=\x9f\xc1J\xfe\x02\x93k!S\x80\xa0\xcc(s\xea&\xbe\xed\x9274qTI\xc1\xf7\xb6\xbd7\x12\x08\x04\x7f\x00\x00\x01\x06\xde\xc5\x1a\x0bprotocol123' ), # noqa: E501 ), ids=( 'pb example Response',