def test_deliver_order(): server = Server(host=None) setup_server(server) server.users[2].logout() # first send from u2 to u3 conn = server.connections[1] server._process_send( conn, [server.users[1].username, server.users[2].username, 'test message!']) server._process_send(conn, [ server.users[1].username, server.users[2].username, 'another test message' ]) # then login as u3 and get all the messages conn = server.connections[2] server.users[2].login(conn) assert len(server.users[2].undelivered_messages) == 2 server._process_deliver(conn, None) wp = WireProtocol() wp.parse_incoming_bytes(conn.send_buffer) assert wp.command == CMD.SEND assert wp.data_buffer == b'u2|||u3|||test message!' remaining_bytes = wp.tmp_buffer wp.reset_buffers() wp.parse_incoming_bytes(remaining_bytes) assert wp.command == CMD.SEND assert wp.data_buffer == b'u2|||u3|||another test message'
def test_msg_data_parse(): wp = WireProtocol() msg_bytes = b'\x00\x02' + b'\x00' * 39 + b'\x20' + b'toname|||fromname|||hello world!' wp.parse_incoming_bytes(msg_bytes) data_out = wp.parse_data() assert len(data_out) == 3 assert data_out[0] == 'toname' assert data_out[1] == 'fromname' assert data_out[2] == 'hello world!'
def test_login_alreadylogged(): server = Server(host=None) setup_server(server) conn = Connection('newconn', None) server._process_login(conn, server.users[2].username) wp = WireProtocol() wp.parse_incoming_bytes(conn.send_buffer) assert wp.command == CMD.RESPONSE assert wp.data_len == len('user is already logged in') assert wp.data_buffer == b'user is already logged in'
def test_send_notexists(): server = Server(host=None) setup_server(server) conn = server.connections[1] server._process_send( conn, [server.users[1].username, 'asdfasdfadf', 'test message']) wp = WireProtocol() wp.parse_incoming_bytes(conn.send_buffer) assert wp.command == CMD.RESPONSE # indicates error
def test_send_notlogged(): server = Server(host=None) setup_server(server) conn = Connection('newconn', None) server._process_send( conn, [server.users[1].username, server.users[2].username, 'test message']) wp = WireProtocol() wp.parse_incoming_bytes(conn.send_buffer) assert wp.command == CMD.RESPONSE # indicates error
def test_login_noaccount(): server = Server(host=None) setup_server(server) server.users[2].logout() conn = Connection('newconn', None) server._process_login(conn, server.users[2].username + 'asdfasdf') wp = WireProtocol() wp.parse_incoming_bytes(conn.send_buffer) assert wp.command == CMD.RESPONSE assert wp.data_len == len('username does not exist') assert wp.data_buffer == b'username does not exist'
def test_parse_bytes_multiarg(): wp = WireProtocol() msg_bytes = b'\x00\x02' + b'\x00' * 39 + b'\x20' + b'toname|||fromname|||hello world!' output = wp.parse_incoming_bytes(msg_bytes) assert output == True assert wp.version == 0 assert wp.command == 2 assert wp.data_len == 32 assert wp.data_buffer == b'toname|||fromname|||hello world!'
def test_emptyedge(): wp = WireProtocol() msg_bytes = b'' output = wp.parse_incoming_bytes(msg_bytes) assert output == False assert wp.version == -1 assert wp.command == -1 assert wp.data_len == -1 assert wp.data_buffer == b'' assert wp.tmp_buffer == b''
def test_parse_bytes_multiarg_partial(): wp = WireProtocol() msg_bytes = b'\x00\x02' + b'\x00' * 39 + b'\x20' + b'toname|||fro' output = wp.parse_incoming_bytes(msg_bytes) assert output == False assert wp.version == 0 assert wp.command == 2 assert wp.data_len == 32 assert wp.data_buffer == b'' assert wp.tmp_buffer == b'toname|||fro'
def test_parse_bytes_singlearg(): wp = WireProtocol() msg_bytes = b'\x00\x00' + b'\x00' * 39 + b'\x08' + b'testname' output = wp.parse_incoming_bytes(msg_bytes) assert output == True assert wp.version == 0 assert wp.command == 0 assert wp.data_len == 8 print(wp.data_buffer) assert wp.data_buffer == b'testname'
def listen_for_messages(q, socket, buffer_size): # sets up wp = WireProtocol() while True: # indicates that the socket has been closed from the main process if socket.fileno() == -1: break data = socket.recv(buffer_size) if not data: q.put( [None, None] ) # None indicates server cut connection (uncontrolled failure mode) break # if parse_incoming_bytes is True, an entire message has been received while wp.parse_incoming_bytes(data): # print('received full message, command: %d, args: %s' % (wp.command, str(wp.parse_data()))) q.put([wp.command, wp.parse_data()]) data = wp.tmp_buffer # Save any leftover bytes wp.reset_buffers()
def test_malformed(): wp = WireProtocol() msg_bytes = b'this is very wrong!' wp.parse_incoming_bytes(msg_bytes) with pytest.raises(ValueError): data_out = wp.parse_data()