示例#1
0
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
示例#2
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
示例#3
0
文件: socket_suite.py 项目: OS-Q/Y511
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
示例#4
0
文件: socket_suite.py 项目: OS-Q/Y511
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
示例#5
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
示例#6
0
文件: ssl_suite.py 项目: OS-Q/Y511
def test_server():
    with open('server.crt', 'w') as f:
        f.write('bar')
    with open('server.key', 'w') as f:
        f.write('fie')

    socket_stub.set_accept(0)

    context = ssl.SSLContext(ssl.PROTOCOL_TLS)
    context.load_cert_chain("server.crt", keyfile="server.key")

    listener_sock = socket.socket()
    listener_sock.bind(('192.168.0.1', 8080))
    listener_sock.listen(5)

    sock, _ = listener_sock.accept()
    ssl_sock = context.wrap_socket(sock, server_side=True)

    assert ssl_sock.get_server_hostname() is None
    assert ssl_sock.cipher() == ('TLS-RSA-WITH-AES-256-GCM-SHA384', 'TLSv1.1',
                                 -1)

    assert ssl_sock.recv(5) == b'hello'
    assert ssl_sock.send(b'goodbye') == 7