Coverage Report

Created: 2025-07-04 06:41

/src/uWebSockets/fuzzing/EpollEchoServerPubSub.cpp
Line
Count
Source (jump to first uncovered line)
1
/* We rely on wrapped syscalls */
2
#include "libEpollFuzzer/epoll_fuzzer.h"
3
4
#include "App.h"
5
#include <vector>
6
7
/* We keep this one for teardown later on */
8
struct us_listen_socket_t *listen_socket;
9
10
/* This test is run by libEpollFuzzer */
11
8.38k
void test() {
12
13
    /* ws->getUserData returns one of these */
14
8.38k
    struct PerSocketData {
15
        /* Fill with user data */
16
8.38k
        std::vector<std::string> topics;
17
8.38k
        int nr = 0;
18
8.38k
    };
19
20
    /* Keep in mind that uWS::SSLApp({options}) is the same as uWS::App() when compiled without SSL support.
21
     * You may swap to using uWS:App() if you don't need SSL */
22
8.38k
    uWS::SSLApp *app = new uWS::SSLApp({
23
        /* There are example certificates in uWebSockets.js repo */
24
8.38k
      .key_file_name = "../misc/key.pem",
25
8.38k
      .cert_file_name = "../misc/cert.pem",
26
8.38k
      .passphrase = "1234"
27
8.38k
  });
28
    
29
8.38k
    app->ws<PerSocketData>("/*", {
30
        /* Settings */
31
8.38k
        .compression = uWS::DISABLED,
32
8.38k
        .maxPayloadLength = 512, // also have a low value here for fuzzing
33
8.38k
        .idleTimeout = 60,
34
8.38k
        .maxBackpressure = 128, // we want a low number so that we can reach this in fuzzing
35
8.38k
        .closeOnBackpressureLimit = false, // this one could be tested as well
36
8.38k
        .resetIdleTimeoutOnSend = true, // and this
37
8.38k
        .sendPingsAutomatically = false, // and this
38
        /* Handlers */
39
8.38k
        .upgrade = nullptr,
40
86.4k
        .open = [](auto *ws) {
41
            /* Open event here, you may access ws->getUserData() which points to a PerSocketData struct */
42
43
86.4k
            PerSocketData *perSocketData = (PerSocketData *) ws->getUserData();
44
45
8.72M
            for (int i = 0; i < 100; i++) {
46
8.64M
                std::string topic = std::to_string((uintptr_t)ws) + "-" + std::to_string(i);
47
8.64M
                perSocketData->topics.push_back(topic);
48
8.64M
                ws->subscribe(topic);
49
8.64M
            }
50
86.4k
        },
51
952k
        .message = [&app](auto *ws, std::string_view message, uWS::OpCode opCode) {
52
952k
            PerSocketData *perSocketData = (PerSocketData *) ws->getUserData();
53
54
952k
            app->publish(perSocketData->topics[++perSocketData->nr % 100], message, opCode);
55
952k
        },
56
9.25k
        .drain = [](auto */*ws*/) {
57
            /* Check ws->getBufferedAmount() here */
58
            //std::cout << "drain" << std::endl;
59
9.25k
        },
60
8.38k
        .ping = [](auto */*ws*/, std::string_view ) {
61
            /* Not implemented yet */
62
4.92k
        },
63
8.38k
        .pong = [](auto */*ws*/, std::string_view ) {
64
            /* Not implemented yet */
65
746
        },
66
86.4k
        .close = [](auto */*ws*/, int /*code*/, std::string_view /*message*/) {
67
            /* You may access ws->getUserData() here */
68
86.4k
        }
69
8.38k
    }).listen(9001, [](auto *listen_s) {
70
5.25k
        if (listen_s) {
71
            //std::cout << "Listening on port " << 9001 << std::endl;
72
5.23k
            listen_socket = listen_s;
73
5.23k
        }
74
5.25k
    });
75
    
76
8.38k
    app->run();
77
78
8.38k
    delete app;
79
80
8.38k
    uWS::Loop::get()->free();
81
8.38k
}
82
83
/* Thus function should shutdown the event-loop and let the test fall through */
84
14.6k
void teardown() {
85
  /* If we are called twice there's a bug (it potentially could if
86
   * all open sockets cannot be error-closed in one epoll_wait call).
87
   * But we only allow 1k FDs and we have a buffer of 1024 from epoll_wait */
88
14.6k
  if (!listen_socket) {
89
0
    exit(-1);
90
0
  }
91
92
  /* We might have open sockets still, and these will be error-closed by epoll_wait */
93
  // us_socket_context_close - close all open sockets created with this socket context
94
14.6k
    if (listen_socket) {
95
14.6k
        us_listen_socket_close(0, listen_socket);
96
14.6k
        listen_socket = NULL;
97
14.6k
    }
98
14.6k
}