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 */
test()11 void test() {
12 
13     /* ws->getUserData returns one of these */
14     struct PerSocketData {
15         /* Fill with user data */
16         std::vector<std::string> topics;
17         int nr = 0;
18     };
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     uWS::SSLApp *app = new uWS::SSLApp({
23         /* There are example certificates in uWebSockets.js repo */
24 	    .key_file_name = "../misc/key.pem",
25 	    .cert_file_name = "../misc/cert.pem",
26 	    .passphrase = "1234"
27 	});
28 
29     app->ws<PerSocketData>("/*", {
30         /* Settings */
31         .compression = uWS::DISABLED,
32         .maxPayloadLength = 512, // also have a low value here for fuzzing
33         .idleTimeout = 60,
34         .maxBackpressure = 128, // we want a low number so that we can reach this in fuzzing
35         .closeOnBackpressureLimit = false, // this one could be tested as well
36         .resetIdleTimeoutOnSend = true, // and this
37         .sendPingsAutomatically = false, // and this
38         /* Handlers */
39         .upgrade = nullptr,
40         .open = [](auto *ws) {
41             /* Open event here, you may access ws->getUserData() which points to a PerSocketData struct */
42 
43             PerSocketData *perSocketData = (PerSocketData *) ws->getUserData();
44 
45             for (int i = 0; i < 100; i++) {
46                 std::string topic = std::to_string((uintptr_t)ws) + "-" + std::to_string(i);
47                 perSocketData->topics.push_back(topic);
48                 ws->subscribe(topic);
49             }
50         },
51         .message = [&app](auto *ws, std::string_view message, uWS::OpCode opCode) {
52             PerSocketData *perSocketData = (PerSocketData *) ws->getUserData();
53 
54             app->publish(perSocketData->topics[++perSocketData->nr % 100], message, opCode);
55         },
56         .drain = [](auto */*ws*/) {
57             /* Check ws->getBufferedAmount() here */
58             //std::cout << "drain" << std::endl;
59         },
60         .ping = [](auto */*ws*/, std::string_view ) {
61             /* Not implemented yet */
62         },
63         .pong = [](auto */*ws*/, std::string_view ) {
64             /* Not implemented yet */
65         },
66         .close = [](auto */*ws*/, int /*code*/, std::string_view /*message*/) {
67             /* You may access ws->getUserData() here */
68         }
69     }).listen(9001, [](auto *listen_s) {
70         if (listen_s) {
71             //std::cout << "Listening on port " << 9001 << std::endl;
72             listen_socket = listen_s;
73         }
74     });
75 
76     app->run();
77 
78     delete app;
79 
80     uWS::Loop::get()->free();
81 }
82 
83 /* Thus function should shutdown the event-loop and let the test fall through */
teardown()84 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 	if (!listen_socket) {
89 		exit(-1);
90 	}
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     if (listen_socket) {
95         us_listen_socket_close(0, listen_socket);
96         listen_socket = NULL;
97     }
98 }
99