Skip to content

Commit

Permalink
Add MockedBroadcastingEchoServer fuzz target
Browse files Browse the repository at this point in the history
  • Loading branch information
uNetworkingAB committed Jan 8, 2020
1 parent a23d3fd commit 7d2c1b4
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 0 deletions.
1 change: 1 addition & 0 deletions fuzzing/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ oss-fuzz:
$(CC) $(CFLAGS) -DLIBUS_NO_SSL -c -O3 uSocketsMock.c
$(CXX) $(CXXFLAGS) -std=c++17 -O3 -DLIBUS_NO_SSL -I../src -I../uSockets/src MockedHelloWorld.cpp uSocketsMock.o -lz -o $(OUT)/MockedHelloWorld $(LIB_FUZZING_ENGINE)
$(CXX) $(CXXFLAGS) -std=c++17 -O3 -DLIBUS_NO_SSL -I../src -I../uSockets/src MockedEchoServer.cpp uSocketsMock.o -lz -o $(OUT)/MockedEchoServer $(LIB_FUZZING_ENGINE)
$(CXX) $(CXXFLAGS) -std=c++17 -O3 -DLIBUS_NO_SSL -I../src -I../uSockets/src MockedBroadcastingEchoServer.cpp uSocketsMock.o -lz -o $(OUT)/MockedBroadcastingEchoServer $(LIB_FUZZING_ENGINE)

broken:
# Too small tests, failing coverage test
Expand Down
71 changes: 71 additions & 0 deletions fuzzing/MockedBroadcastingEchoServer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
#include "App.h"

#include "helpers.h"

/* This function pushes data to the uSockets mock */
extern "C" void us_loop_read_mocked_data(struct us_loop *loop, char *data, unsigned int size);

uWS::TemplatedApp<false> *app;
us_listen_socket_t *listenSocket;

extern "C" int LLVMFuzzerInitialize(int *argc, char ***argv) {

/* ws->getUserData returns one of these */
struct PerSocketData {
int nothing;
};

/* Very simple WebSocket echo server */
app = new uWS::TemplatedApp<false>(uWS::App().ws<PerSocketData>("/*", {
/* Settings */
.compression = uWS::SHARED_COMPRESSOR,
/* We want this to be low so that we can hit it, yet bigger than 256 */
.maxPayloadLength = 300,
.idleTimeout = 10,
/* Handlers */
.open = [](auto *ws, auto *req) {
/* Subscribe to anything */
ws->subscribe(req->getHeader("topic"));
},
.message = [](auto *ws, std::string_view message, uWS::OpCode opCode) {
if (message.length() && message[0] == 'C') {
ws->close();
} else if (message.length() && message[0] == 'E') {
ws->end(1006);
} else {
/* Publish to topic sent by message */
ws->publish(message, message, opCode, true);

if (message.length() && message[0] == 'U') {
ws->unsubscribe(message);
}
}
},
.drain = [](auto *ws) {
/* Check getBufferedAmount here */
},
.ping = [](auto *ws) {

},
.pong = [](auto *ws) {

},
.close = [](auto *ws, int code, std::string_view message) {

}
}).listen(9001, [](us_listen_socket_t *listenSocket) {
if (listenSocket) {
std::cout << "Listening on port " << 9001 << std::endl;
::listenSocket = listenSocket;
}
}));

return 0;
}

extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {

us_loop_read_mocked_data((struct us_loop *) uWS::Loop::get(), (char *) makePadded(data, size), size);

return 0;
}

0 comments on commit 7d2c1b4

Please sign in to comment.