2019-09-01 01:46:44 +02:00
|
|
|
/*
|
|
|
|
* ws_autobahn.cpp
|
|
|
|
* Author: Benjamin Sergeant
|
|
|
|
* Copyright (c) 2019 Machine Zone, Inc. All rights reserved.
|
|
|
|
*/
|
|
|
|
|
2019-09-01 20:28:27 +02:00
|
|
|
//
|
|
|
|
// 1. First you need to generate a config file in a config folder,
|
|
|
|
// which can use a white list of test to execute (with globbing),
|
|
|
|
// or a black list of tests to ignore
|
|
|
|
//
|
|
|
|
// config/fuzzingserver.json
|
|
|
|
// {
|
|
|
|
// "url": "ws://127.0.0.1:9001",
|
|
|
|
// "outdir": "./reports/clients",
|
|
|
|
// "cases": ["2.*"],
|
|
|
|
// "exclude-cases": [
|
|
|
|
// ],
|
|
|
|
// "exclude-agent-cases": {}
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
//
|
|
|
|
// 2 Run the test server (using docker)
|
|
|
|
// docker run -it --rm \
|
|
|
|
// -v "${PWD}/config:/config" \
|
|
|
|
// -v "${PWD}/reports:/reports" \
|
|
|
|
// -p 9001:9001 \
|
|
|
|
// --name fuzzingserver \
|
|
|
|
// crossbario/autobahn-testsuite
|
|
|
|
//
|
|
|
|
// 3. Run this command
|
|
|
|
// ws autobahn -q --url ws://localhost:9001
|
|
|
|
//
|
|
|
|
// 4. A HTML report will be generated, you can inspect it to see if you are compliant or not
|
|
|
|
//
|
|
|
|
|
2019-09-01 01:46:44 +02:00
|
|
|
#include <iostream>
|
|
|
|
#include <sstream>
|
2019-09-01 20:10:27 +02:00
|
|
|
#include <atomic>
|
2019-09-01 19:50:16 +02:00
|
|
|
#include <mutex>
|
|
|
|
#include <condition_variable>
|
2019-09-01 01:46:44 +02:00
|
|
|
#include <ixwebsocket/IXWebSocket.h>
|
|
|
|
#include <ixwebsocket/IXSocket.h>
|
|
|
|
|
|
|
|
|
2019-09-04 05:14:35 +02:00
|
|
|
namespace
|
|
|
|
{
|
|
|
|
std::string truncate(const std::string& str, size_t n)
|
|
|
|
{
|
|
|
|
if (str.size() < n)
|
|
|
|
{
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return str.substr(0, n) + "...";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-01 01:46:44 +02:00
|
|
|
namespace ix
|
|
|
|
{
|
|
|
|
class AutobahnTestCase
|
|
|
|
{
|
|
|
|
public:
|
2019-09-01 19:45:51 +02:00
|
|
|
AutobahnTestCase(const std::string& _url, bool quiet);
|
2019-09-01 01:46:44 +02:00
|
|
|
void run();
|
|
|
|
|
|
|
|
private:
|
|
|
|
void log(const std::string& msg);
|
|
|
|
|
|
|
|
std::string _url;
|
|
|
|
ix::WebSocket _webSocket;
|
|
|
|
|
2019-09-01 19:45:51 +02:00
|
|
|
bool _quiet;
|
2019-09-01 19:50:16 +02:00
|
|
|
|
|
|
|
std::mutex _mutex;
|
|
|
|
std::condition_variable _condition;
|
2019-09-01 01:46:44 +02:00
|
|
|
};
|
|
|
|
|
2019-09-01 19:45:51 +02:00
|
|
|
AutobahnTestCase::AutobahnTestCase(const std::string& url, bool quiet) :
|
2019-09-01 01:46:44 +02:00
|
|
|
_url(url),
|
2019-09-01 19:45:51 +02:00
|
|
|
_quiet(quiet)
|
2019-09-01 01:46:44 +02:00
|
|
|
{
|
|
|
|
_webSocket.disableAutomaticReconnection();
|
|
|
|
|
2019-09-01 19:45:51 +02:00
|
|
|
// FIXME: this should be on by default
|
2019-09-01 01:46:44 +02:00
|
|
|
ix::WebSocketPerMessageDeflateOptions webSocketPerMessageDeflateOptions(
|
|
|
|
true, false, false, 15, 15);
|
|
|
|
_webSocket.setPerMessageDeflateOptions(webSocketPerMessageDeflateOptions);
|
|
|
|
}
|
|
|
|
|
|
|
|
void AutobahnTestCase::log(const std::string& msg)
|
|
|
|
{
|
2019-09-01 19:45:51 +02:00
|
|
|
if (!_quiet)
|
|
|
|
{
|
|
|
|
std::cerr << msg;
|
|
|
|
}
|
2019-09-01 01:46:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void AutobahnTestCase::run()
|
|
|
|
{
|
|
|
|
_webSocket.setUrl(_url);
|
|
|
|
|
|
|
|
std::stringstream ss;
|
|
|
|
log(std::string("Connecting to url: ") + _url);
|
|
|
|
|
|
|
|
_webSocket.setOnMessageCallback(
|
|
|
|
[this](const ix::WebSocketMessagePtr& msg)
|
|
|
|
{
|
|
|
|
std::stringstream ss;
|
|
|
|
if (msg->type == ix::WebSocketMessageType::Open)
|
|
|
|
{
|
|
|
|
log("autobahn: connected");
|
2019-09-01 19:45:51 +02:00
|
|
|
ss << "Uri: " << msg->openInfo.uri << std::endl;
|
|
|
|
ss << "Handshake Headers:" << std::endl;
|
2019-09-01 01:46:44 +02:00
|
|
|
for (auto it : msg->openInfo.headers)
|
|
|
|
{
|
2019-09-01 19:45:51 +02:00
|
|
|
ss << it.first << ": " << it.second << std::endl;
|
2019-09-01 01:46:44 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (msg->type == ix::WebSocketMessageType::Close)
|
|
|
|
{
|
|
|
|
ss << "autobahn: connection closed:";
|
|
|
|
ss << " code " << msg->closeInfo.code;
|
|
|
|
ss << " reason " << msg->closeInfo.reason << std::endl;
|
|
|
|
|
2019-09-01 19:50:16 +02:00
|
|
|
_condition.notify_one();
|
2019-09-01 01:46:44 +02:00
|
|
|
}
|
|
|
|
else if (msg->type == ix::WebSocketMessageType::Message)
|
|
|
|
{
|
2019-09-01 19:45:51 +02:00
|
|
|
ss << "Received " << msg->wireSize << " bytes" << std::endl;
|
2019-09-01 01:46:44 +02:00
|
|
|
|
2019-09-01 19:45:51 +02:00
|
|
|
ss << "autobahn: received message: "
|
2019-09-04 05:14:35 +02:00
|
|
|
<< truncate(msg->str, 40)
|
2019-09-01 19:45:51 +02:00
|
|
|
<< std::endl;
|
2019-09-01 01:46:44 +02:00
|
|
|
|
|
|
|
_webSocket.send(msg->str, msg->binary);
|
|
|
|
}
|
|
|
|
else if (msg->type == ix::WebSocketMessageType::Error)
|
|
|
|
{
|
|
|
|
ss << "Connection error: " << msg->errorInfo.reason << std::endl;
|
|
|
|
ss << "#retries: " << msg->errorInfo.retries << std::endl;
|
|
|
|
ss << "Wait time(ms): " << msg->errorInfo.wait_time << std::endl;
|
|
|
|
ss << "HTTP Status: " << msg->errorInfo.http_status << std::endl;
|
|
|
|
|
|
|
|
// And error can happen, in which case the test-case is marked done
|
2019-09-01 19:50:16 +02:00
|
|
|
_condition.notify_one();
|
2019-09-01 01:46:44 +02:00
|
|
|
}
|
|
|
|
else if (msg->type == ix::WebSocketMessageType::Fragment)
|
|
|
|
{
|
2019-09-01 19:45:51 +02:00
|
|
|
ss << "Received message fragment" << std::endl;
|
2019-09-01 01:46:44 +02:00
|
|
|
}
|
|
|
|
else if (msg->type == ix::WebSocketMessageType::Ping)
|
|
|
|
{
|
2019-09-01 19:45:51 +02:00
|
|
|
ss << "Received ping" << std::endl;
|
2019-09-01 01:46:44 +02:00
|
|
|
}
|
|
|
|
else if (msg->type == ix::WebSocketMessageType::Pong)
|
|
|
|
{
|
2019-09-01 19:45:51 +02:00
|
|
|
ss << "Received pong" << std::endl;
|
2019-09-01 01:46:44 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-09-01 19:45:51 +02:00
|
|
|
ss << "Invalid ix::WebSocketMessageType" << std::endl;
|
2019-09-01 01:46:44 +02:00
|
|
|
}
|
2019-09-01 19:45:51 +02:00
|
|
|
|
|
|
|
log(ss.str());
|
2019-09-01 01:46:44 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
_webSocket.start();
|
|
|
|
|
2019-09-01 19:50:16 +02:00
|
|
|
log("Waiting for test completion ...");
|
|
|
|
std::unique_lock<std::mutex> lock(_mutex);
|
|
|
|
_condition.wait(lock);
|
2019-09-01 01:46:44 +02:00
|
|
|
|
|
|
|
_webSocket.stop();
|
|
|
|
}
|
|
|
|
|
2019-09-01 20:10:27 +02:00
|
|
|
void generateReport(const std::string& url)
|
|
|
|
{
|
|
|
|
ix::WebSocket webSocket;
|
|
|
|
std::string reportUrl(url);
|
|
|
|
reportUrl += "/updateReports?agent=ixwebsocket";
|
|
|
|
webSocket.setUrl(reportUrl);
|
|
|
|
webSocket.disableAutomaticReconnection();
|
|
|
|
|
|
|
|
std::atomic<bool> done(false);
|
|
|
|
webSocket.setOnMessageCallback(
|
|
|
|
[&done](const ix::WebSocketMessagePtr& msg)
|
|
|
|
{
|
|
|
|
if (msg->type == ix::WebSocketMessageType::Close)
|
|
|
|
{
|
|
|
|
std::cerr << "Report generated" << std::endl;
|
|
|
|
done = true;
|
|
|
|
}
|
|
|
|
else if (msg->type == ix::WebSocketMessageType::Error)
|
|
|
|
{
|
|
|
|
std::stringstream ss;
|
|
|
|
ss << "Connection error: " << msg->errorInfo.reason << std::endl;
|
|
|
|
ss << "#retries: " << msg->errorInfo.retries << std::endl;
|
|
|
|
ss << "Wait time(ms): " << msg->errorInfo.wait_time << std::endl;
|
|
|
|
ss << "HTTP Status: " << msg->errorInfo.http_status << std::endl;
|
|
|
|
std::cerr << ss.str() << std::endl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
webSocket.start();
|
|
|
|
|
|
|
|
while (!done)
|
|
|
|
{
|
|
|
|
std::chrono::duration<double, std::milli> duration(10);
|
|
|
|
std::this_thread::sleep_for(duration);
|
|
|
|
}
|
|
|
|
|
|
|
|
webSocket.stop();
|
|
|
|
}
|
|
|
|
|
2019-09-01 20:17:28 +02:00
|
|
|
int getTestCaseCount(const std::string& url)
|
|
|
|
{
|
|
|
|
ix::WebSocket webSocket;
|
|
|
|
std::string caseCountUrl(url);
|
|
|
|
caseCountUrl += "/getCaseCount";
|
|
|
|
webSocket.setUrl(caseCountUrl);
|
|
|
|
webSocket.disableAutomaticReconnection();
|
|
|
|
|
|
|
|
int count = 0;
|
|
|
|
|
|
|
|
std::atomic<bool> done(false);
|
|
|
|
webSocket.setOnMessageCallback(
|
|
|
|
[&done, &count](const ix::WebSocketMessagePtr& msg)
|
|
|
|
{
|
|
|
|
if (msg->type == ix::WebSocketMessageType::Close)
|
|
|
|
{
|
|
|
|
done = true;
|
|
|
|
}
|
|
|
|
else if (msg->type == ix::WebSocketMessageType::Error)
|
|
|
|
{
|
|
|
|
std::stringstream ss;
|
|
|
|
ss << "Connection error: " << msg->errorInfo.reason << std::endl;
|
|
|
|
ss << "#retries: " << msg->errorInfo.retries << std::endl;
|
|
|
|
ss << "Wait time(ms): " << msg->errorInfo.wait_time << std::endl;
|
|
|
|
ss << "HTTP Status: " << msg->errorInfo.http_status << std::endl;
|
|
|
|
std::cerr << ss.str() << std::endl;
|
|
|
|
}
|
|
|
|
else if (msg->type == ix::WebSocketMessageType::Message)
|
|
|
|
{
|
|
|
|
// response is a string
|
|
|
|
std::stringstream ss;
|
|
|
|
ss << msg->str;
|
|
|
|
ss >> count;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
webSocket.start();
|
|
|
|
|
|
|
|
while (!done)
|
|
|
|
{
|
|
|
|
std::chrono::duration<double, std::milli> duration(10);
|
|
|
|
std::this_thread::sleep_for(duration);
|
|
|
|
}
|
|
|
|
|
|
|
|
webSocket.stop();
|
|
|
|
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
2019-09-01 01:46:44 +02:00
|
|
|
//
|
2019-09-01 20:10:27 +02:00
|
|
|
// make && bench ws autobahn --url ws://localhost:9001
|
2019-09-01 01:46:44 +02:00
|
|
|
//
|
2019-09-01 19:45:51 +02:00
|
|
|
int ws_autobahn_main(const std::string& url, bool quiet)
|
2019-09-01 01:46:44 +02:00
|
|
|
{
|
2019-09-01 20:17:28 +02:00
|
|
|
int N = getTestCaseCount(url);
|
2019-09-01 20:28:27 +02:00
|
|
|
std::cerr << "Test cases count: " << N << std::endl;
|
|
|
|
|
2019-09-01 01:46:44 +02:00
|
|
|
N++;
|
2019-09-01 20:17:28 +02:00
|
|
|
|
2019-09-01 01:46:44 +02:00
|
|
|
for (int i = 1 ; i < N; ++i)
|
|
|
|
{
|
2019-09-01 20:10:27 +02:00
|
|
|
std::cerr << "Execute test case " << i << std::endl;
|
|
|
|
|
2019-09-01 01:46:44 +02:00
|
|
|
int caseNumber = i;
|
|
|
|
|
|
|
|
std::stringstream ss;
|
2019-09-01 20:10:27 +02:00
|
|
|
ss << url
|
|
|
|
<< "/runCase?case="
|
2019-09-01 01:46:44 +02:00
|
|
|
<< caseNumber
|
|
|
|
<< "&agent=ixwebsocket";
|
|
|
|
|
|
|
|
std::string url(ss.str());
|
|
|
|
|
2019-09-01 19:45:51 +02:00
|
|
|
AutobahnTestCase testCase(url, quiet);
|
2019-09-01 01:46:44 +02:00
|
|
|
testCase.run();
|
|
|
|
}
|
|
|
|
|
2019-09-01 20:10:27 +02:00
|
|
|
generateReport(url);
|
|
|
|
|
2019-09-01 01:46:44 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|