Coverage Report

Created: 2025-07-04 06:41

/src/uWebSockets/fuzzing/WebSocket.cpp
Line
Count
Source
1
/* This is a fuzz test of the websocket parser */
2
3
#define WIN32_EXPORT
4
5
#include "helpers.h"
6
7
/* We test the websocket parser */
8
#include "../src/WebSocketProtocol.h"
9
10
struct Impl {
11
22.9k
    static bool refusePayloadLength(uint64_t length, uWS::WebSocketState<true> *wState, void *s) {
12
13
        /* We need a limit */
14
22.9k
        if (length > 16000) {
15
596
            return true;
16
596
        }
17
18
        /* Return ok */
19
22.3k
        return false;
20
22.9k
    }
21
22
30.2k
    static bool setCompressed(uWS::WebSocketState<true> *wState, void *s) {
23
        /* We support it */
24
30.2k
        return true;
25
30.2k
    }
26
27
127k
    static void forceClose(uWS::WebSocketState<true> *wState, void *s, std::string_view reason = {}) {
28
29
127k
    }
30
31
32.0k
    static bool handleFragment(char *data, size_t length, unsigned int remainingBytes, int opCode, bool fin, uWS::WebSocketState<true> *webSocketState, void *s) {
32
33
32.0k
        if (opCode == uWS::TEXT) {
34
15.2k
            if (!uWS::protocol::isValidUtf8((unsigned char *)data, length)) {
35
                /* Return break */
36
7.82k
                return true;
37
7.82k
            }
38
16.7k
        } else if (opCode == uWS::CLOSE) {
39
10.2k
            uWS::protocol::parseClosePayload((char *)data, length);
40
10.2k
        }
41
42
        /* Return ok */
43
24.1k
        return false;
44
32.0k
    }
45
};
46
47
8.19k
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
48
49
    /* Create the parser state */
50
8.19k
    uWS::WebSocketState<true> state;
51
52
142k
    makeChunked(makePadded(data, size), size, [&state](const uint8_t *data, size_t size) {
53
        /* Parse it */
54
142k
        uWS::WebSocketProtocol<true, Impl>::consume((char *) data, size, &state, nullptr);
55
142k
    });
56
57
8.19k
    return 0;
58
8.19k
}
59