-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Expand file tree
/
Copy pathTopicTree.cpp
More file actions
67 lines (56 loc) · 2.19 KB
/
TopicTree.cpp
File metadata and controls
67 lines (56 loc) · 2.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#define WIN32_EXPORT
#include "helpers.h"
/* Test for the topic tree */
#include "../src/TopicTree.h"
#include <memory>
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<uint32_t, std::unique_ptr<uWS::Subscriber>> 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<uWS::Subscriber>(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;
}