/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  | 9.25k  | void test() { | 
11  |  |  | 
12  | 9.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  | 9.25k  |         auto app = uWS::App({ | 
16  |  |             /* There are example certificates in uWebSockets.js repo */  | 
17  | 9.25k  |             .key_file_name = "../misc/key.pem",  | 
18  | 9.25k  |             .cert_file_name = "../misc/cert.pem",  | 
19  | 9.25k  |             .passphrase = "1234"  | 
20  | 24.6k  |         }).get("/*", [](auto *res, auto *req) { | 
21  | 24.6k  |             auto aborted = std::make_shared<bool>();  | 
22  | 24.6k  |             *aborted = false;  | 
23  | 24.6k  |             res->onAborted([aborted]() { | 
24  | 7.27k  |                 *aborted = true;  | 
25  | 7.27k  |             });  | 
26  |  |  | 
27  | 24.6k  |             uWS::Loop::get()->defer([res, aborted]() { | 
28  | 22.9k  |                 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  | 22.9k  |             });  | 
35  | 24.6k  |         }).listen(9001, [](auto *listenSocket) { | 
36  | 3.30k  |             listen_socket = listenSocket;  | 
37  | 3.30k  |         });  | 
38  |  |  | 
39  | 9.25k  |         app.run();  | 
40  | 9.25k  |     }  | 
41  | 9.25k  |     uWS::Loop::get()->free();  | 
42  | 9.25k  | }  | 
43  |  |  | 
44  |  | /* Thus function should shutdown the event-loop and let the test fall through */  | 
45  | 16.4k  | 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  | 16.4k  |   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  | 16.4k  |     if (listen_socket) { | 
56  | 16.4k  |         us_listen_socket_close(0, listen_socket);  | 
57  | 16.4k  |         listen_socket = NULL;  | 
58  | 16.4k  |     }  | 
59  | 16.4k  | }  |