diff --git a/fuzzing/Makefile b/fuzzing/Makefile index c10d2f8..1e01aad 100644 --- a/fuzzing/Makefile +++ b/fuzzing/Makefile @@ -10,6 +10,7 @@ oss-fuzz: $(CXX) $(CXXFLAGS) -std=c++17 -O3 WebSocket.cpp -o $(OUT)/WebSocket $(LIB_FUZZING_ENGINE) $(CXX) $(CXXFLAGS) -std=c++17 -O3 Http.cpp -o $(OUT)/Http $(LIB_FUZZING_ENGINE) $(CXX) $(CXXFLAGS) -std=c++17 -O3 PerMessageDeflate.cpp -o $(OUT)/PerMessageDeflate $(LIB_FUZZING_ENGINE) -lz + $(CXX) $(CXXFLAGS) -std=c++17 -O3 TopicTree.cpp -o $(OUT)/TopicTree $(LIB_FUZZING_ENGINE) # "Integration tests" $(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) diff --git a/fuzzing/TopicTree.cpp b/fuzzing/TopicTree.cpp new file mode 100644 index 0000000..cbe4e59 --- /dev/null +++ b/fuzzing/TopicTree.cpp @@ -0,0 +1,67 @@ +#define WIN32_EXPORT + +#include "helpers.h" + +/* Test for the topic tree */ +#include "../src/TopicTree.h" + +#include + +extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { + /* Create topic tree */ + uWS::TopicTree topicTree([](uWS::Subscriber *s, std::string_view message) { + /* We assume sane output */ + if (!s || !message.length()) { + free((void *) -1); + } + + return 0; + }); + + /* Holder for all manually allocated subscribers */ + std::map> subscribers; + + /* Iterate the padded fuzz as chunks */ + makeChunked(makePadded(data, size), size, [&topicTree, &subscribers](const uint8_t *data, size_t size) { + /* We need at least 5 bytes */ + if (size > 4) { + /* Last of all is a string */ + std::string_view lastString((char *) data + 5, size - 5); + + /* First 4 bytes is the subscriber id */ + uint32_t id; + memcpy(&id, data, 4); + + /* Then one byte action */ + if (data[4] == 'S') { + /* Subscribe */ + if (subscribers.find(id) != subscribers.end()) { + uWS::Subscriber *subscriber = new uWS::Subscriber(nullptr); + subscribers[id] = std::unique_ptr(subscriber); + topicTree.subscribe(lastString, subscriber); + } + } else if (data[4] == 'U') { + /* Unsubscribe */ + auto it = subscribers.find(id); + if (it != subscribers.end()) { + topicTree.unsubscribe(lastString, it->second.get()); + } + } else if (data[4] == 'A') { + /* Unsubscribe from all */ + auto it = subscribers.find(id); + if (it != subscribers.end()) { + topicTree.unsubscribeAll(it->second.get()); + } + } else if (data[4] == 'P') { + /* Publish */ + topicTree.publish(lastString, lastString); + } else if (data[4] == 'D') { + /* Drain */ + topicTree.drain(); + } + } + }); + + return 0; +} +