Esempio n. 1
0
def test_drop_message(ws_testdata):
    tctx, playbook = ws_testdata
    flow = Placeholder(WebSocketFlow)
    assert (playbook << websocket.WebsocketStartHook(flow) >> reply() >>
            DataReceived(tctx.server, b"\x81\x03foo") <<
            websocket.WebsocketMessageHook(flow))
    flow().messages[-1].content = ""
    assert (playbook >> reply() << None)
Esempio n. 2
0
def test_modify_message(ws_testdata):
    tctx, playbook = ws_testdata
    flow = Placeholder(WebSocketFlow)
    assert (playbook << websocket.WebsocketStartHook(flow) >> reply() >>
            DataReceived(tctx.server, b"\x81\x03foo") <<
            websocket.WebsocketMessageHook(flow))
    flow().messages[-1].content = flow().messages[-1].content.replace(
        "foo", "foobar")
    assert (playbook >> reply() << SendData(tctx.client, b"\x81\x06foobar"))
Esempio n. 3
0
def test_fragmented(ws_testdata):
    tctx, playbook = ws_testdata
    flow = Placeholder(WebSocketFlow)
    assert (playbook << websocket.WebsocketStartHook(flow) >> reply() >>
            DataReceived(tctx.server, b"\x01\x03foo") >> DataReceived(
                tctx.server, b"\x80\x03bar") <<
            websocket.WebsocketMessageHook(flow) >> reply() << SendData(
                tctx.client, b"\x01\x03foo") << SendData(
                    tctx.client, b"\x80\x03bar"))
    assert flow().messages[-1].content == "foobar"
Esempio n. 4
0
def test_upgrade(tctx):
    """Test a HTTP -> WebSocket upgrade"""
    tctx.server.address = ("example.com", 80)
    tctx.server.state = ConnectionState.OPEN
    http_flow = Placeholder(HTTPFlow)
    flow = Placeholder(WebSocketFlow)
    assert (
        Playbook(http.HttpLayer(tctx, HTTPMode.transparent)) >> DataReceived(
            tctx.client, b"GET / HTTP/1.1\r\n"
            b"Connection: upgrade\r\n"
            b"Upgrade: websocket\r\n"
            b"Sec-WebSocket-Version: 13\r\n"
            b"\r\n") << http.HttpRequestHeadersHook(http_flow) >> reply() <<
        http.HttpRequestHook(http_flow) >> reply() << SendData(
            tctx.server, b"GET / HTTP/1.1\r\n"
            b"Connection: upgrade\r\n"
            b"Upgrade: websocket\r\n"
            b"Sec-WebSocket-Version: 13\r\n"
            b"\r\n") >> DataReceived(
                tctx.server, b"HTTP/1.1 101 Switching Protocols\r\n"
                b"Upgrade: websocket\r\n"
                b"Connection: Upgrade\r\n"
                b"\r\n") << http.HttpResponseHeadersHook(http_flow) >> reply()
        << http.HttpResponseHook(http_flow) >> reply() << SendData(
            tctx.client, b"HTTP/1.1 101 Switching Protocols\r\n"
            b"Upgrade: websocket\r\n"
            b"Connection: Upgrade\r\n"
            b"\r\n") << websocket.WebsocketStartHook(flow) >> reply() >>
        DataReceived(tctx.client, masked_bytes(b"\x81\x0bhello world")) <<
        websocket.WebsocketMessageHook(flow) >> reply() << SendData(
            tctx.server, masked(b"\x81\x0bhello world")) >> DataReceived(
                tctx.server, b"\x82\nhello back") <<
        websocket.WebsocketMessageHook(flow) >> reply() << SendData(
            tctx.client, b"\x82\nhello back"))
    assert flow().handshake_flow == http_flow()
    assert len(flow().messages) == 2
    assert flow().messages[0].content == "hello world"
    assert flow().messages[0].from_client
    assert flow().messages[1].content == b"hello back"
    assert flow().messages[1].from_client is False
Esempio n. 5
0
def test_deflate(ws_testdata):
    tctx, playbook = ws_testdata
    flow = Placeholder(WebSocketFlow)
    # noinspection PyUnresolvedReferences
    http_flow: HTTPFlow = playbook.layer.flow.handshake_flow
    http_flow.response.headers[
        "Sec-WebSocket-Extensions"] = "permessage-deflate; server_max_window_bits=10"
    assert (playbook << websocket.WebsocketStartHook(flow) >> reply()
            # https://tools.ietf.org/html/rfc7692#section-7.2.3.1
            >> DataReceived(tctx.server,
                            bytes.fromhex("c1 07 f2 48 cd c9 c9 07 00")) <<
            websocket.WebsocketMessageHook(flow) >> reply() << SendData(
                tctx.client, bytes.fromhex("c1 07 f2 48 cd c9 c9 07 00")))
    assert flow().messages[0].content == "Hello"