Coverage Report

Created: 2024-04-25 06:10

/src/uWebSockets/fuzzing/AsyncEpollHelloWorld.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
6
/* We keep this one for teardown later on */
7
struct us_listen_socket_t *listen_socket;
8
9
/* This test is run by libEpollFuzzer */
10
2.25k
void test() {
11
12
2.25k
    {
13
        /* Keep in mind that uWS::SSLApp({options}) is the same as uWS::App() when compiled without SSL support.
14
        * You may swap to using uWS:App() if you don't need SSL */
15
2.25k
        auto app = uWS::App({
16
            /* There are example certificates in uWebSockets.js repo */
17
2.25k
            .key_file_name = "../misc/key.pem",
18
2.25k
            .cert_file_name = "../misc/cert.pem",
19
2.25k
            .passphrase = "1234"
20
34.2k
        }).get("/*", [](auto *res, auto *req) {
21
34.2k
            auto aborted = std::make_shared<bool>();
22
34.2k
            *aborted = false;
23
34.2k
            res->onAborted([aborted]() {
24
16.8k
                *aborted = true;
25
16.8k
            });
26
27
34.2k
            uWS::Loop::get()->defer([res, aborted]() {
28
33.5k
                if (!*aborted) {
29
17.3k
                    res->cork([res, aborted]() {
30
                        // Todo: also test upgrade to websocket here
31
17.3k
                        res->end("Hello async!");
32
17.3k
                    });
33
17.3k
                }
34
33.5k
            });
35
34.2k
        }).listen(9001, [](auto *listenSocket) {
36
2.25k
            listen_socket = listenSocket;
37
2.25k
        });
38
39
2.25k
        app.run();
40
2.25k
    }
41
2.25k
    uWS::Loop::get()->free();
42
2.25k
}
43
44
/* Thus function should shutdown the event-loop and let the test fall through */
45
2.24k
void teardown() {
46
  /* If we are called twice there's a bug (it potentially could if
47
   * all open sockets cannot be error-closed in one epoll_wait call).
48
   * But we only allow 1k FDs and we have a buffer of 1024 from epoll_wait */
49
2.24k
  if (!listen_socket) {
50
0
    exit(-1);
51
0
  }
52
53
  /* We might have open sockets still, and these will be error-closed by epoll_wait */
54
  // us_socket_context_close - close all open sockets created with this socket context
55
2.24k
    if (listen_socket) {
56
2.24k
        us_listen_socket_close(0, listen_socket);
57
2.24k
        listen_socket = NULL;
58
2.24k
    }
59
2.24k
}