def test_websocket_echo(): # Handshake. request = "GET /websocket/echo HTTP/1.1\r\n" \ "User-Agent: TestcaseRequestIndex\r\n" \ "Upgrade: WebSocket\r\n" \ "Connection: Upgrade\r\n" \ "Sec-WebSocket-Key: x3JJHMbDL1EzLkh9GBhXDw==\r\n" \ "Sec-WebSocket-Version: 13\r\n" \ "\r\n" response = "HTTP/1.1 101 Switching Protocols\r\n" \ "Upgrade: websocket\r\n" \ "Connection: Upgrade\r\n" \ "Sec-WebSocket-Accept: HSmrc0sMlYUkAGmm5OPpG2HaGWk=\r\n" \ "\r\n" socket_stub.set_recv([bytes(char, 'ascii') for char in request] + [b'\x81\x84', b'\x00\x00\x00\x00', b'123\x00'] + [b'\x81\x84', b'\x00\x00\x00\x00', b'123\x00']) socket_stub.set_send([ bytes(response, 'ascii'), b'\x81\x84\x00\x00\x00\x00', b'123\x00', b'\x81\x84\x00\x00\x00\x00', b'123\x00' ]) socket_stub.set_close(0) # Accept. socket_stub.set_accept(0) # Wait for the connection to be colsed by the HTTP server. socket_stub.wait_closed() assert socket_stub.reset_failed() == 0
def test_form(): # Test data. request_header = "POST /form.html HTTP/1.1\r\n" \ "User-Agent: TestcaseRequestIndex\r\n" \ "Connection: keep-alive\r\n" \ "Content-Type: application/x-www-form-urlencoded\r\n" \ "Content-Length: 9\r\n" \ "\r\n" response_header = "HTTP/1.1 200 OK\r\n" \ "Content-Type: text/html\r\n" \ "Content-Length: 5\r\n" \ "\r\n" response_body = "Form!" socket_stub.set_recv([bytes(char, 'ascii') for char in request_header] + [b'key=value'] + [-1]) socket_stub.set_send( [bytes(response_header, 'ascii'), bytes(response_body, 'ascii')]) socket_stub.set_close(0) # Accept. socket_stub.set_accept(0) # Wait for the connection to be colsed by the HTTP server. socket_stub.wait_closed() assert socket_stub.reset_failed() == 0
def test_tcp_client(): sock = socket.socket() sock.connect(("192.168.0.1", 8080)) assert sock.send(b'send()') == 6 assert sock.sendall(b'send()') == 6 assert sock.recv(6) == b'recv()' sock.close() assert socket_stub.reset_failed() == 0
def test_errors(): # Failed accept. socket_stub.set_accept(-1) listener = socket.socket() listener.bind(("192.168.0.1", 8080)) listener.listen(1) with assert_raises(OSError, 'socket accept failed'): listener.accept() listener.close() # Failed bind and close. socket_stub.set_bind(-1) socket_stub.set_close(-1) listener = socket.socket() with assert_raises(OSError, 'socket bind failed'): listener.bind(("192.168.0.1", 8080)) with assert_raises(OSError, 'socket close failed'): listener.close() # Failed listen. socket_stub.set_listen(-1) listener = socket.socket() listener.bind(("192.168.0.1", 8080)) with assert_raises(OSError, 'socket listen failed'): listener.listen(1) listener.close() # Failed connect. socket_stub.set_connect(-1) sock = socket.socket() with assert_raises(OSError, 'socket connect failed'): sock.connect(("192.168.0.1", 8080)) sock.close() # Failed send and recv. socket_stub.set_send([-1, 0]) socket_stub.set_recv([-1, 0]) sock = socket.socket() sock.connect(("192.168.0.1", 8080)) assert sock.send(b'bar') == 0 assert sock.recv(5) == b'' assert sock.send(b'bar') == 0 assert sock.recv(5) == b'' sock.close() assert socket_stub.reset_failed() == 0
def test_bad_arguments(): # Bad socket family. with assert_raises(OSError): socket.socket(-1) # Bad socket type. with assert_raises(OSError): socket.socket(socket.AF_INET, -1) assert socket_stub.reset_failed() == 0
def test_tcp_server(): socket_stub.set_accept(0) listener = socket.socket() listener.bind(("192.168.0.1", 8080)) listener.listen(1) client, fromaddr = listener.accept() assert client is not None assert fromaddr is None listener.close() assert socket_stub.reset_failed() == 0
def test_select(): poll = select.poll() tcp = socket.socket() # Register both event channels. poll.register(tcp) # Timeout waiting for data on the socket. assert poll.poll(0.01) == [] tcp.close() assert socket_stub.reset_failed() == 0
def simple_http_request(request, response_header=None, response_body=None): socket_stub.set_recv([bytes(char, 'ascii') for char in request]) if response_header is not None: socket_stub.set_send( [bytes(response_header, 'ascii'), bytes(response_body, 'ascii')]) socket_stub.set_close(0) # Accept. socket_stub.set_accept(0) # Wait for the connection to be closed by the HTTP server. socket_stub.wait_closed() assert socket_stub.reset_failed() == 0
def test_udp(): # Prepare the socket stub. socket_stub.set_sendto([ b'123', (b'456', -1), (b'789', 0), ]) sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) buf, fromaddr = sock.recvfrom(1024) assert buf == b'recvfrom()' assert fromaddr == (b'192.168.0.1', 8080) assert sock.sendto(b'123', fromaddr) == 3 assert sock.sendto(b'456', fromaddr) == 0 assert sock.sendto(b'789', fromaddr) == 0 assert socket_stub.reset_failed() == 0