/src/uWebSockets/fuzzing/AsyncEpollHelloWorld.cpp
Line | Count | Source |
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 | 4.08k | void test() { |
11 | | |
12 | 4.08k | { |
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 | 4.08k | auto app = uWS::App({ |
16 | | /* There are example certificates in uWebSockets.js repo */ |
17 | 4.08k | .key_file_name = "../misc/key.pem", |
18 | 4.08k | .cert_file_name = "../misc/cert.pem", |
19 | 4.08k | .passphrase = "1234" |
20 | 8.96k | }).get("/*", [](auto *res, auto *req) { |
21 | 8.96k | auto aborted = std::make_shared<bool>(); |
22 | 8.96k | *aborted = false; |
23 | 8.96k | res->onAborted([aborted]() { |
24 | 3.05k | *aborted = true; |
25 | 3.05k | }); |
26 | | |
27 | 8.96k | uWS::Loop::get()->defer([res, aborted]() { |
28 | 8.57k | if (!*aborted) { |
29 | 5.91k | res->cork([res, aborted]() { |
30 | | // Todo: also test upgrade to websocket here |
31 | 5.91k | res->end("Hello async!"); |
32 | 5.91k | }); |
33 | 5.91k | } |
34 | 8.57k | }); |
35 | 8.96k | }).listen(9001, [](auto *listenSocket) { |
36 | 4.08k | listen_socket = listenSocket; |
37 | 4.08k | }); |
38 | | |
39 | | /* Publishing without WebSocket contexts must be a no-op */ |
40 | 4.08k | uWS::PreparedMessage preparedMessage = {}; |
41 | 4.08k | if (app.publish("unused", "hi", uWS::TEXT) || |
42 | 4.08k | app.publishPrepared("unused", preparedMessage) || |
43 | 4.08k | app.numSubscribers("unused")) { |
44 | 0 | exit(-1); |
45 | 0 | } |
46 | | |
47 | 4.08k | app.run(); |
48 | 4.08k | } |
49 | 0 | uWS::Loop::get()->free(); |
50 | 4.08k | } |
51 | | |
52 | | /* Thus function should shutdown the event-loop and let the test fall through */ |
53 | 17.6k | void teardown() { |
54 | | /* If we are called twice there's a bug (it potentially could if |
55 | | * all open sockets cannot be error-closed in one epoll_wait call). |
56 | | * But we only allow 1k FDs and we have a buffer of 1024 from epoll_wait */ |
57 | 17.6k | if (!listen_socket) { |
58 | 0 | exit(-1); |
59 | 0 | } |
60 | | |
61 | | /* We might have open sockets still, and these will be error-closed by epoll_wait */ |
62 | | // us_socket_context_close - close all open sockets created with this socket context |
63 | 17.6k | if (listen_socket) { |
64 | 17.6k | us_listen_socket_close(0, listen_socket); |
65 | | listen_socket = NULL; |
66 | 17.6k | } |
67 | 17.6k | } |