-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathautobahn.cpp
More file actions
164 lines (135 loc) · 5.04 KB
/
Copy pathautobahn.cpp
File metadata and controls
164 lines (135 loc) · 5.04 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#include <iostream>
#include <thread>
#include <chrono>
#include <mutex>
#include <condition_variable>
#include <atomic>
#include <memory>
#include <string>
#include "WebSocketClient.h"
// IMPORTANT!
// Make sure to match the number of cases as per fuzzingserver
constexpr int totalTestCases = 516;
// Small helper to wait for a predicate with timeout
static bool wait_for_pred(std::condition_variable& cv,
std::unique_lock<std::mutex>& lk,
std::chrono::milliseconds dur,
const std::function<bool()>& pred)
{
return cv.wait_for(lk, dur, pred);
}
int main() {
for (int i = 1; i <= totalTestCases; ++i) {
std::cout << "\n--- Running test case " << i << " ---\n";
auto client = std::make_shared<WebSocketClient>();
std::weak_ptr<WebSocketClient> wclient = client;
std::mutex mtx;
std::condition_variable cv;
bool done = false;
// Once closing begins, stop echoing to avoid sending after close handshake starts.
std::atomic_bool closing{false};
auto finish = [&]() {
{
std::lock_guard<std::mutex> lk(mtx);
done = true;
}
cv.notify_one();
};
// Echo text exactly as received
client->setMessageCallback([wclient, &closing](const std::string& message) {
if (closing.load(std::memory_order_acquire)) return;
if (auto c = wclient.lock()) {
c->sendMessage(message);
}
});
// Echo binary exactly as received
client->setBinaryCallback([wclient, &closing](const void* data, size_t length) {
if (closing.load(std::memory_order_acquire)) return;
if (auto c = wclient.lock()) {
c->sendBinary(data, length);
}
});
client->setOpenCallback([i]() {
std::cout << "Connected (case " << i << ")\n";
});
client->setCloseCallback([&](int code, const std::string& reason) {
closing.store(true, std::memory_order_release);
std::cout << "Closed by server: \"" << reason
<< "\" (code=" << code << ")\n";
finish();
});
client->setErrorCallback([&](int error_code, const std::string& error_message) {
closing.store(true, std::memory_order_release);
std::cout << "Error (" << error_code << "): " << error_message << "\n";
finish();
});
// Start the test case
std::string url =
"ws://192.168.0.27:9001/runCase?case=" +
std::to_string(i) + "&agent=libwsc";
client->setUrl(url);
client->connect();
// Wait until the server closes the case (or we time out)
{
std::unique_lock<std::mutex> lk(mtx);
const bool ok = wait_for_pred(
cv, lk,
std::chrono::seconds(20),
[&]() { return done; }
);
if (!ok) {
// Timeout: force full shutdown.
closing.store(true, std::memory_order_release);
std::cout << "⏳ Timeout waiting for case close; disconnecting\n";
}
}
// Ensure the client is fully shut down before starting next case.
// Call exactly once per case.
client->disconnect();
// Drop the strong ref explicitly before next iteration.
client.reset();
}
// Final report
std::cout << "\n--- Reporting results ---\n";
{
auto reportClient = std::make_shared<WebSocketClient>();
std::weak_ptr<WebSocketClient> w = reportClient;
std::mutex mtx;
std::condition_variable cv;
bool done = false;
auto finish = [&]() {
{
std::lock_guard<std::mutex> lk(mtx);
done = true;
}
cv.notify_one();
};
reportClient->setOpenCallback([]() {
std::cout << "Connected to report endpoint\n";
});
reportClient->setCloseCallback([&](int code, const std::string& reason) {
std::cout << "Report closed: " << code << " \"" << reason << "\"\n";
finish();
});
reportClient->setErrorCallback([&](int error_code, const std::string& err) {
std::cout << "Report error (" << error_code << "): " << err << "\n";
finish();
});
reportClient->setUrl("ws://192.168.0.27:9001/updateReports?agent=libwsc");
reportClient->connect();
// Wait for the report endpoint to close, or time out.
{
std::unique_lock<std::mutex> lk(mtx);
wait_for_pred(
cv, lk,
std::chrono::seconds(5),
[&]() { return done; }
);
}
// Full shutdown once.
reportClient->disconnect();
reportClient.reset();
}
std::cout << "All tests + report complete.\n";
return 0;
}