2019-01-03 01:08:32 +01:00
|
|
|
/*
|
2019-01-03 06:59:06 +01:00
|
|
|
* IXWebSocketServerTest.cpp
|
2019-01-03 01:08:32 +01:00
|
|
|
* Author: Benjamin Sergeant
|
|
|
|
* Copyright (c) 2019 Machine Zone. All rights reserved.
|
|
|
|
*/
|
|
|
|
|
2019-09-23 19:25:23 +02:00
|
|
|
#include "IXTest.h"
|
|
|
|
#include "catch.hpp"
|
2019-01-03 01:08:32 +01:00
|
|
|
#include <iostream>
|
|
|
|
#include <ixwebsocket/IXSocket.h>
|
2019-09-23 19:25:23 +02:00
|
|
|
#include <ixwebsocket/IXSocketFactory.h>
|
2019-01-03 01:08:32 +01:00
|
|
|
#include <ixwebsocket/IXWebSocket.h>
|
|
|
|
#include <ixwebsocket/IXWebSocketServer.h>
|
|
|
|
|
|
|
|
using namespace ix;
|
|
|
|
|
|
|
|
namespace ix
|
|
|
|
{
|
2019-03-21 02:34:24 +01:00
|
|
|
// Test that we can override the connectionState impl to provide our own
|
|
|
|
class ConnectionStateCustom : public ConnectionState
|
2019-01-03 01:08:32 +01:00
|
|
|
{
|
2019-03-21 02:34:24 +01:00
|
|
|
void computeId()
|
|
|
|
{
|
|
|
|
// a very boring invariant id that we can test against in the unittest
|
|
|
|
_id = "foobarConnectionId";
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-09-23 19:25:23 +02:00
|
|
|
bool startServer(ix::WebSocketServer& server, std::string& connectionId)
|
2019-03-21 02:34:24 +01:00
|
|
|
{
|
2019-09-23 19:25:23 +02:00
|
|
|
auto factory = []() -> std::shared_ptr<ConnectionState> {
|
2019-03-21 02:34:24 +01:00
|
|
|
return std::make_shared<ConnectionStateCustom>();
|
|
|
|
};
|
|
|
|
server.setConnectionStateFactory(factory);
|
|
|
|
|
2020-07-24 22:04:14 +02:00
|
|
|
server.setOnClientMessageCallback(
|
|
|
|
[&server, &connectionId](std::shared_ptr<ConnectionState> connectionState,
|
|
|
|
WebSocket& webSocket,
|
|
|
|
const ix::WebSocketMessagePtr& msg) {
|
2020-08-28 23:55:40 +02:00
|
|
|
auto remoteIp = connectionState->getRemoteIp();
|
2020-07-24 21:49:36 +02:00
|
|
|
|
2020-07-24 22:04:14 +02:00
|
|
|
if (msg->type == ix::WebSocketMessageType::Open)
|
|
|
|
{
|
|
|
|
TLogger() << "New connection";
|
|
|
|
connectionState->computeId();
|
|
|
|
TLogger() << "remote ip: " << remoteIp;
|
|
|
|
TLogger() << "id: " << connectionState->getId();
|
|
|
|
TLogger() << "Uri: " << msg->openInfo.uri;
|
|
|
|
TLogger() << "Headers:";
|
|
|
|
for (auto it : msg->openInfo.headers)
|
|
|
|
{
|
|
|
|
TLogger() << it.first << ": " << it.second;
|
|
|
|
}
|
2020-07-24 21:49:36 +02:00
|
|
|
|
2020-07-24 22:04:14 +02:00
|
|
|
connectionId = connectionState->getId();
|
|
|
|
}
|
|
|
|
else if (msg->type == ix::WebSocketMessageType::Close)
|
2020-07-24 21:49:36 +02:00
|
|
|
{
|
2020-07-24 22:04:14 +02:00
|
|
|
TLogger() << "Closed connection";
|
2020-07-24 21:49:36 +02:00
|
|
|
}
|
2020-07-24 22:04:14 +02:00
|
|
|
else if (msg->type == ix::WebSocketMessageType::Message)
|
2020-07-24 21:49:36 +02:00
|
|
|
{
|
2020-07-24 22:04:14 +02:00
|
|
|
for (auto&& client : server.getClients())
|
2020-07-24 21:49:36 +02:00
|
|
|
{
|
2020-07-24 22:04:14 +02:00
|
|
|
if (client.get() != &webSocket)
|
|
|
|
{
|
|
|
|
client->send(msg->str, msg->binary);
|
|
|
|
}
|
2020-07-24 21:49:36 +02:00
|
|
|
}
|
|
|
|
}
|
2020-07-24 22:04:14 +02:00
|
|
|
});
|
2019-01-03 01:08:32 +01:00
|
|
|
|
|
|
|
auto res = server.listen();
|
|
|
|
if (!res.first)
|
|
|
|
{
|
2020-03-12 19:15:54 +01:00
|
|
|
TLogger() << res.second;
|
2019-01-03 01:08:32 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
server.start();
|
|
|
|
return true;
|
|
|
|
}
|
2019-09-23 19:25:23 +02:00
|
|
|
} // namespace ix
|
2019-01-03 01:08:32 +01:00
|
|
|
|
|
|
|
TEST_CASE("Websocket_server", "[websocket_server]")
|
|
|
|
{
|
|
|
|
SECTION("Connect to the server, do not send anything. Should timeout and return 400")
|
|
|
|
{
|
2019-01-29 00:14:49 +01:00
|
|
|
int port = getFreePort();
|
2019-01-03 01:08:32 +01:00
|
|
|
ix::WebSocketServer server(port);
|
2019-03-21 02:34:24 +01:00
|
|
|
std::string connectionId;
|
|
|
|
REQUIRE(startServer(server, connectionId));
|
2019-01-03 01:08:32 +01:00
|
|
|
|
|
|
|
std::string errMsg;
|
2019-03-15 02:37:38 +01:00
|
|
|
bool tls = false;
|
2019-10-01 07:06:46 +02:00
|
|
|
SocketTLSOptions tlsOptions;
|
|
|
|
std::shared_ptr<Socket> socket = createSocket(tls, -1, errMsg, tlsOptions);
|
2019-05-06 21:22:57 +02:00
|
|
|
std::string host("127.0.0.1");
|
2019-09-23 19:25:23 +02:00
|
|
|
auto isCancellationRequested = []() -> bool { return false; };
|
2019-03-15 02:37:38 +01:00
|
|
|
bool success = socket->connect(host, port, errMsg, isCancellationRequested);
|
2019-01-03 01:08:32 +01:00
|
|
|
REQUIRE(success);
|
|
|
|
|
2019-03-15 02:37:38 +01:00
|
|
|
auto lineResult = socket->readLine(isCancellationRequested);
|
2019-01-03 01:08:32 +01:00
|
|
|
auto lineValid = lineResult.first;
|
2019-09-12 20:43:52 +02:00
|
|
|
REQUIRE(lineValid);
|
|
|
|
|
2019-01-03 01:08:32 +01:00
|
|
|
auto line = lineResult.second;
|
|
|
|
|
|
|
|
int status = -1;
|
|
|
|
REQUIRE(sscanf(line.c_str(), "HTTP/1.1 %d", &status) == 1);
|
|
|
|
REQUIRE(status == 400);
|
|
|
|
|
|
|
|
// FIXME: explicitely set a client timeout larger than the server one (3)
|
|
|
|
|
|
|
|
// Give us 500ms for the server to notice that clients went away
|
|
|
|
ix::msleep(500);
|
|
|
|
server.stop();
|
|
|
|
REQUIRE(server.getClients().size() == 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
SECTION("Connect to the server. Send GET request without header. Should return 400")
|
|
|
|
{
|
2019-01-29 00:14:49 +01:00
|
|
|
int port = getFreePort();
|
2019-01-03 01:08:32 +01:00
|
|
|
ix::WebSocketServer server(port);
|
2019-03-21 02:34:24 +01:00
|
|
|
std::string connectionId;
|
|
|
|
REQUIRE(startServer(server, connectionId));
|
2019-01-03 01:08:32 +01:00
|
|
|
|
|
|
|
std::string errMsg;
|
2019-03-15 02:37:38 +01:00
|
|
|
bool tls = false;
|
2019-10-01 07:06:46 +02:00
|
|
|
SocketTLSOptions tlsOptions;
|
|
|
|
std::shared_ptr<Socket> socket = createSocket(tls, -1, errMsg, tlsOptions);
|
2019-05-06 21:22:57 +02:00
|
|
|
std::string host("127.0.0.1");
|
2019-09-23 19:25:23 +02:00
|
|
|
auto isCancellationRequested = []() -> bool { return false; };
|
2019-03-15 02:37:38 +01:00
|
|
|
bool success = socket->connect(host, port, errMsg, isCancellationRequested);
|
2019-01-03 01:08:32 +01:00
|
|
|
REQUIRE(success);
|
|
|
|
|
2020-03-12 19:15:54 +01:00
|
|
|
TLogger() << "writeBytes";
|
2019-03-15 02:37:38 +01:00
|
|
|
socket->writeBytes("GET /\r\n", isCancellationRequested);
|
2019-01-03 01:08:32 +01:00
|
|
|
|
2019-03-15 02:37:38 +01:00
|
|
|
auto lineResult = socket->readLine(isCancellationRequested);
|
2019-01-03 01:08:32 +01:00
|
|
|
auto lineValid = lineResult.first;
|
2019-09-12 20:43:52 +02:00
|
|
|
REQUIRE(lineValid);
|
|
|
|
|
2019-01-03 01:08:32 +01:00
|
|
|
auto line = lineResult.second;
|
|
|
|
|
|
|
|
int status = -1;
|
|
|
|
REQUIRE(sscanf(line.c_str(), "HTTP/1.1 %d", &status) == 1);
|
|
|
|
REQUIRE(status == 400);
|
|
|
|
|
|
|
|
// FIXME: explicitely set a client timeout larger than the server one (3)
|
|
|
|
|
|
|
|
// Give us 500ms for the server to notice that clients went away
|
|
|
|
ix::msleep(500);
|
|
|
|
server.stop();
|
|
|
|
REQUIRE(server.getClients().size() == 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
SECTION("Connect to the server. Send GET request with correct header")
|
|
|
|
{
|
2019-01-29 00:14:49 +01:00
|
|
|
int port = getFreePort();
|
2019-01-03 01:08:32 +01:00
|
|
|
ix::WebSocketServer server(port);
|
2019-03-21 02:34:24 +01:00
|
|
|
std::string connectionId;
|
|
|
|
REQUIRE(startServer(server, connectionId));
|
2019-01-03 01:08:32 +01:00
|
|
|
|
|
|
|
std::string errMsg;
|
2019-03-15 02:37:38 +01:00
|
|
|
bool tls = false;
|
2019-10-01 07:06:46 +02:00
|
|
|
SocketTLSOptions tlsOptions;
|
|
|
|
std::shared_ptr<Socket> socket = createSocket(tls, -1, errMsg, tlsOptions);
|
2019-05-06 21:22:57 +02:00
|
|
|
std::string host("127.0.0.1");
|
2019-09-23 19:25:23 +02:00
|
|
|
auto isCancellationRequested = []() -> bool { return false; };
|
2019-03-15 02:37:38 +01:00
|
|
|
bool success = socket->connect(host, port, errMsg, isCancellationRequested);
|
2019-01-03 01:08:32 +01:00
|
|
|
REQUIRE(success);
|
|
|
|
|
2019-03-15 02:37:38 +01:00
|
|
|
socket->writeBytes("GET / HTTP/1.1\r\n"
|
|
|
|
"Upgrade: websocket\r\n"
|
|
|
|
"Sec-WebSocket-Version: 13\r\n"
|
|
|
|
"Sec-WebSocket-Key: foobar\r\n"
|
|
|
|
"\r\n",
|
|
|
|
isCancellationRequested);
|
2019-01-03 22:41:06 +01:00
|
|
|
|
2019-03-15 02:37:38 +01:00
|
|
|
auto lineResult = socket->readLine(isCancellationRequested);
|
2019-01-03 01:08:32 +01:00
|
|
|
auto lineValid = lineResult.first;
|
2019-09-12 20:43:52 +02:00
|
|
|
REQUIRE(lineValid);
|
|
|
|
|
2019-01-03 01:08:32 +01:00
|
|
|
auto line = lineResult.second;
|
|
|
|
|
|
|
|
int status = -1;
|
|
|
|
REQUIRE(sscanf(line.c_str(), "HTTP/1.1 %d", &status) == 1);
|
|
|
|
REQUIRE(status == 101);
|
|
|
|
|
|
|
|
// Give us 500ms for the server to notice that clients went away
|
|
|
|
ix::msleep(500);
|
|
|
|
|
|
|
|
server.stop();
|
2019-04-25 07:10:22 +02:00
|
|
|
REQUIRE(connectionId == "foobarConnectionId");
|
2019-01-03 01:08:32 +01:00
|
|
|
REQUIRE(server.getClients().size() == 0);
|
|
|
|
}
|
|
|
|
}
|