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 struct us_socket_t *client;
9 
10 /* This test is run by libEpollFuzzer */
test()11 void test() {
12 	/* ws->getUserData returns one of these */
13     struct PerSocketData {
14         /* Fill with user data */
15     };
16 
17     {
18         /* Keep in mind that uWS::SSLApp({options}) is the same as uWS::App() when compiled without SSL support.
19         * You may swap to using uWS:App() if you don't need SSL */
20         auto app = uWS::App({
21             /* There are example certificates in uWebSockets.js repo */
22             .key_file_name = "../misc/key.pem",
23             .cert_file_name = "../misc/cert.pem",
24             .passphrase = "1234"
25         }).ws<PerSocketData>("/empty", {
26         /* Having no handlers here should not crash */
27         }).get("/*", [](auto *res, auto *req) {
28             if (req->getHeader("write").length()) {
29                 res->writeStatus("200 OK")->writeHeader("write", "true")->write("Hello");
30                 res->write(" world!");
31                 res->end();
32             } else if (req->getQuery().length()) {
33                 res->close();
34             } else {
35                 res->end("Hello world!");
36             }
37         }).post("/*", [](auto *res, auto *req) {
38             res->onAborted([]() {
39                 /* We might as well use this opportunity to stress the loop a bit */
40                 uWS::Loop::get()->defer([]() {
41 
42                 });
43             });
44             res->onData([res](std::string_view chunk, bool isEnd) {
45                 if (isEnd) {
46                     res->cork([res, chunk]() {
47                         res->write("something ahead");
48                         res->end(chunk);
49                     });
50                 }
51             });
52         }).any("/:candy/*", [](auto *res, auto *req) {
53             if (req->getParameter(0).length() == 0) {
54                 free((void *) -1);
55             }
56             /* Some invalid queries */
57             req->getParameter(30000);
58             req->getParameter((unsigned short) -34234);
59             req->getHeader("yhello");
60             req->getQuery();
61             req->getQuery("assd");
62 
63             res->end("done");
64         }).ws<PerSocketData>("/*", {
65             /* Settings */
66             .compression = uWS::SHARED_COMPRESSOR,
67             .maxPayloadLength = 16 * 1024,
68             .idleTimeout = 12,
69             .maxBackpressure = 1024,
70             /* Handlers */
71             .open = [](auto *ws) {
72                 /* Open event here, you may access ws->getUserData() which points to a PerSocketData struct */
73                 ws->getNativeHandle();
74                 ws->getRemoteAddressAsText();
75                 us_poll_ext((struct us_poll_t *) ws);
76             },
77             .message = [](auto *ws, std::string_view message, uWS::OpCode opCode) {
78                 ws->send(message, opCode, true);
79             },
80             .drain = [](auto *ws) {
81                 /* Check ws->getBufferedAmount() here */
82             },
83             .ping = [](auto *ws, std::string_view) {
84                 /* We use this to trigger the async/wakeup feature */
85                 uWS::Loop::get()->defer([]() {
86                     /* Do nothing */
87                 });
88             },
89             .pong = [](auto *ws, std::string_view) {
90                 /* Not implemented yet */
91             },
92             .close = [](auto *ws, int code, std::string_view message) {
93                 /* You may access ws->getUserData() here */
94             }
95         }).listen(9001, [](auto *listenSocket) {
96             listen_socket = listenSocket;
97         });
98 
99         /* Here we want to stress the connect feature, since nothing else stresses it */
100         struct us_loop_t *loop = (struct us_loop_t *) uWS::Loop::get();
101         /* This function is stupid */
102         us_loop_iteration_number(loop);
103         struct us_socket_context_t *client_context = us_create_socket_context(0, loop, 0, {});
104         us_socket_context_timestamp(0, client_context);
105         client = us_socket_context_connect(0, client_context, "hostname", 5000, "localhost", 0, 0);
106 
107         if (client) {
108             us_socket_is_established(0, client);
109             us_socket_local_port(0, client);
110         }
111 
112         us_socket_context_on_connect_error(0, client_context, [](struct us_socket_t *s, int code) {
113             client = nullptr;
114             return s;
115         });
116 
117         us_socket_context_on_open(0, client_context, [](struct us_socket_t *s, int is_client, char *ip, int ip_length) {
118             us_socket_flush(0, s);
119             return s;
120         });
121 
122         us_socket_context_on_end(0, client_context, [](struct us_socket_t *s) {
123             /* Someone sent is a FIN, but we can still send data */
124             us_socket_write(0, s, "asdadasdasdasdaddfgdfhdfgdfg", 28, false);
125             return s;
126         });
127 
128         us_socket_context_on_data(0, client_context, [](struct us_socket_t *s, char *data, int length) {
129             return s;
130         });
131 
132         us_socket_context_on_writable(0, client_context, [](struct us_socket_t *s) {
133             /* Let's defer a close here */
134             us_socket_shutdown_read(0, s);
135             return s;
136         });
137 
138         us_socket_context_on_close(0, client_context, [](struct us_socket_t *s, int code, void *reason) {
139             client = NULL;
140             return s;
141         });
142 
143         /* Trigger some context functions */
144         app.addServerName("", {});
145         app.removeServerName("");
146         app.missingServerName(nullptr);
147         app.getNativeHandle();
148 
149         app.run();
150 
151         /* After done we also free the client context */
152         us_socket_context_free(0, client_context);
153     }
154     uWS::Loop::get()->setSilent(true);
155     uWS::Loop::get()->free();
156 }
157 
158 /* Thus function should shutdown the event-loop and let the test fall through */
teardown()159 void teardown() {
160 	/* If we are called twice there's a bug (it potentially could if
161 	 * all open sockets cannot be error-closed in one epoll_wait call).
162 	 * But we only allow 1k FDs and we have a buffer of 1024 from epoll_wait */
163 	if (!listen_socket && !client) {
164 		exit(-1);
165 	}
166 
167     if (client) {
168         us_socket_close(0, client, 0, 0);
169         client = NULL;
170     }
171 
172 	/* We might have open sockets still, and these will be error-closed by epoll_wait */
173 	// us_socket_context_close - close all open sockets created with this socket context
174     if (listen_socket) {
175         us_listen_socket_close(0, listen_socket);
176         listen_socket = NULL;
177     }
178 }
179