add simple unittest
This commit is contained in:
parent
3b67032adb
commit
43cd6d34ca
@ -14,7 +14,6 @@ set( IXWEBSOCKET_SOURCES
|
|||||||
ixwebsocket/IXEventFd.cpp
|
ixwebsocket/IXEventFd.cpp
|
||||||
ixwebsocket/IXSocket.cpp
|
ixwebsocket/IXSocket.cpp
|
||||||
ixwebsocket/IXSocketConnect.cpp
|
ixwebsocket/IXSocketConnect.cpp
|
||||||
ixwebsocket/IXSetThreadName.cpp
|
|
||||||
ixwebsocket/IXDNSLookup.cpp
|
ixwebsocket/IXDNSLookup.cpp
|
||||||
ixwebsocket/IXWebSocket.cpp
|
ixwebsocket/IXWebSocket.cpp
|
||||||
ixwebsocket/IXWebSocketTransport.cpp
|
ixwebsocket/IXWebSocketTransport.cpp
|
||||||
@ -28,20 +27,33 @@ set( IXWEBSOCKET_HEADERS
|
|||||||
ixwebsocket/IXSocketConnect.h
|
ixwebsocket/IXSocketConnect.h
|
||||||
ixwebsocket/IXSetThreadName.h
|
ixwebsocket/IXSetThreadName.h
|
||||||
ixwebsocket/IXDNSLookup.h
|
ixwebsocket/IXDNSLookup.h
|
||||||
|
ixwebsocket/IXCancellationRequest.h
|
||||||
ixwebsocket/IXWebSocket.h
|
ixwebsocket/IXWebSocket.h
|
||||||
ixwebsocket/IXWebSocketTransport.h
|
ixwebsocket/IXWebSocketTransport.h
|
||||||
ixwebsocket/IXWebSocketSendInfo.h
|
ixwebsocket/IXWebSocketSendInfo.h
|
||||||
|
ixwebsocket/IXWebSocketErrorInfo.h
|
||||||
ixwebsocket/IXWebSocketPerMessageDeflate.h
|
ixwebsocket/IXWebSocketPerMessageDeflate.h
|
||||||
ixwebsocket/IXWebSocketPerMessageDeflateOptions.h
|
ixwebsocket/IXWebSocketPerMessageDeflateOptions.h
|
||||||
ixwebsocket/IXWebSocketHttpHeaders.h
|
ixwebsocket/IXWebSocketHttpHeaders.h
|
||||||
)
|
)
|
||||||
|
|
||||||
|
# Platform specific code
|
||||||
|
if (APPLE)
|
||||||
|
list( APPEND IXWEBSOCKET_SOURCES ixwebsocket/apple/IXSetThreadName_apple.cpp)
|
||||||
|
else()
|
||||||
|
list( APPEND IXWEBSOCKET_SOURCES ixwebsocket/linux/IXSetThreadName_linux.cpp)
|
||||||
|
endif()
|
||||||
|
|
||||||
|
|
||||||
if (USE_TLS)
|
if (USE_TLS)
|
||||||
add_definitions(-DIXWEBSOCKET_USE_TLS)
|
add_definitions(-DIXWEBSOCKET_USE_TLS)
|
||||||
|
|
||||||
if (APPLE)
|
if (APPLE)
|
||||||
list( APPEND IXWEBSOCKET_HEADERS ixwebsocket/IXSocketAppleSSL.h)
|
list( APPEND IXWEBSOCKET_HEADERS ixwebsocket/IXSocketAppleSSL.h)
|
||||||
list( APPEND IXWEBSOCKET_SOURCES ixwebsocket/IXSocketAppleSSL.cpp)
|
list( APPEND IXWEBSOCKET_SOURCES ixwebsocket/IXSocketAppleSSL.cpp)
|
||||||
|
elseif (WIN32)
|
||||||
|
list( APPEND IXWEBSOCKET_HEADERS ixwebsocket/IXSocketSChannel.h)
|
||||||
|
list( APPEND IXWEBSOCKET_SOURCES ixwebsocket/IXSocketSChannel.cpp)
|
||||||
else()
|
else()
|
||||||
list( APPEND IXWEBSOCKET_HEADERS ixwebsocket/IXSocketOpenSSL.h)
|
list( APPEND IXWEBSOCKET_HEADERS ixwebsocket/IXSocketOpenSSL.h)
|
||||||
list( APPEND IXWEBSOCKET_SOURCES ixwebsocket/IXSocketOpenSSL.cpp)
|
list( APPEND IXWEBSOCKET_SOURCES ixwebsocket/IXSocketOpenSSL.cpp)
|
||||||
|
108
ixwebsocket/IXSocketSChannel.cpp
Normal file
108
ixwebsocket/IXSocketSChannel.cpp
Normal file
@ -0,0 +1,108 @@
|
|||||||
|
/*
|
||||||
|
* IXSocketSChannel.cpp
|
||||||
|
* Author: Benjamin Sergeant
|
||||||
|
* Copyright (c) 2018 Machine Zone, Inc. All rights reserved.
|
||||||
|
*
|
||||||
|
* See https://docs.microsoft.com/en-us/windows/desktop/WinSock/using-secure-socket-extensions
|
||||||
|
*
|
||||||
|
* https://github.com/pauldotknopf/WindowsSDK7-Samples/blob/master/netds/winsock/securesocket/stcpclient/tcpclient.c
|
||||||
|
*
|
||||||
|
* This is the right example to look at:
|
||||||
|
* https://www.codeproject.com/Articles/1000189/A-Working-TCP-Client-and-Server-With-SSL
|
||||||
|
*/
|
||||||
|
#include "IXSocketSChannel.h"
|
||||||
|
|
||||||
|
#ifdef _WIN32
|
||||||
|
# include <basetsd.h>
|
||||||
|
# include <WinSock2.h>
|
||||||
|
# include <ws2def.h>
|
||||||
|
# include <WS2tcpip.h>
|
||||||
|
# include <schannel.h>
|
||||||
|
# include <sslsock.h>
|
||||||
|
# include <io.h>
|
||||||
|
|
||||||
|
#define WIN32_LEAN_AND_MEAN
|
||||||
|
|
||||||
|
#ifndef UNICODE
|
||||||
|
#define UNICODE
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#include <windows.h>
|
||||||
|
#include <winsock2.h>
|
||||||
|
#include <mstcpip.h>
|
||||||
|
#include <ws2tcpip.h>
|
||||||
|
#include <rpc.h>
|
||||||
|
#include <ntdsapi.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <tchar.h>
|
||||||
|
|
||||||
|
#define RECV_DATA_BUF_SIZE 256
|
||||||
|
|
||||||
|
// Link with ws2_32.lib
|
||||||
|
#pragma comment(lib, "Ws2_32.lib")
|
||||||
|
|
||||||
|
// link with fwpuclnt.lib for Winsock secure socket extensions
|
||||||
|
#pragma comment(lib, "fwpuclnt.lib")
|
||||||
|
|
||||||
|
// link with ntdsapi.lib for DsMakeSpn function
|
||||||
|
#pragma comment(lib, "ntdsapi.lib")
|
||||||
|
|
||||||
|
// The following function assumes that Winsock
|
||||||
|
// has already been initialized
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
#else
|
||||||
|
# error("This file should only be built on Windows")
|
||||||
|
#endif
|
||||||
|
|
||||||
|
namespace ix
|
||||||
|
{
|
||||||
|
SocketSChannel::SocketSChannel()
|
||||||
|
{
|
||||||
|
;
|
||||||
|
}
|
||||||
|
|
||||||
|
SocketSChannel::~SocketSChannel()
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
bool SocketSChannel::connect(const std::string& host,
|
||||||
|
int port,
|
||||||
|
std::string& errMsg)
|
||||||
|
{
|
||||||
|
return Socket::connect(host, port, errMsg);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void SocketSChannel::secureSocket()
|
||||||
|
{
|
||||||
|
// there will be a lot to do here ...
|
||||||
|
// FIXME do something with sockerror
|
||||||
|
}
|
||||||
|
|
||||||
|
void SocketSChannel::close()
|
||||||
|
{
|
||||||
|
Socket::close();
|
||||||
|
}
|
||||||
|
|
||||||
|
int SocketSChannel::send(char* buf, size_t nbyte)
|
||||||
|
{
|
||||||
|
return Socket::send(buf, nbyte);
|
||||||
|
}
|
||||||
|
|
||||||
|
int SocketSChannel::send(const std::string& buffer)
|
||||||
|
{
|
||||||
|
return Socket::send(buffer);
|
||||||
|
}
|
||||||
|
|
||||||
|
int SocketSChannel::recv(void* buf, size_t nbyte)
|
||||||
|
{
|
||||||
|
return Socket::recv(buf, nbyte);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
34
ixwebsocket/IXSocketSChannel.h
Normal file
34
ixwebsocket/IXSocketSChannel.h
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
/*
|
||||||
|
* IXSocketSChannel.h
|
||||||
|
* Author: Benjamin Sergeant
|
||||||
|
* Copyright (c) 2017-2018 Machine Zone, Inc. All rights reserved.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "IXSocket.h"
|
||||||
|
|
||||||
|
namespace ix
|
||||||
|
{
|
||||||
|
class SocketSChannel : public Socket
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
SocketSChannel();
|
||||||
|
~SocketSChannel();
|
||||||
|
|
||||||
|
virtual bool connect(const std::string& host,
|
||||||
|
int port,
|
||||||
|
std::string& errMsg) final;
|
||||||
|
virtual void close() final;
|
||||||
|
|
||||||
|
// The important override
|
||||||
|
virtual void secureSocket() final;
|
||||||
|
|
||||||
|
virtual int send(char* buffer, size_t length) final;
|
||||||
|
virtual int send(const std::string& buffer) final;
|
||||||
|
virtual int recv(void* buffer, size_t length) final;
|
||||||
|
|
||||||
|
private:
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
7
makefile
7
makefile
@ -15,3 +15,10 @@ build:
|
|||||||
(cd examples/chat ; mkdir -p build ; cd build ; cmake .. ; make)
|
(cd examples/chat ; mkdir -p build ; cd build ; cmake .. ; make)
|
||||||
(cd examples/ping_pong ; mkdir -p build ; cd build ; cmake .. ; make)
|
(cd examples/ping_pong ; mkdir -p build ; cd build ; cmake .. ; make)
|
||||||
(cd examples/ws_connect ; mkdir -p build ; cd build ; cmake .. ; make)
|
(cd examples/ws_connect ; mkdir -p build ; cd build ; cmake .. ; make)
|
||||||
|
|
||||||
|
test_server:
|
||||||
|
(cd test && npm i ws && node broadcast-server.js)
|
||||||
|
test:
|
||||||
|
(cd test && cmake . && make && ./ixwebsocket_unittest)
|
||||||
|
|
||||||
|
.PHONY: test
|
||||||
|
8
test/.gitignore
vendored
Normal file
8
test/.gitignore
vendored
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
CMakeCache.txt
|
||||||
|
package-lock.json
|
||||||
|
CMakeFiles
|
||||||
|
ixwebsocket_unittest
|
||||||
|
cmake_install.cmake
|
||||||
|
node_modules
|
||||||
|
ixwebsocket
|
||||||
|
Makefile
|
31
test/CMakeLists.txt
Normal file
31
test/CMakeLists.txt
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
#
|
||||||
|
# Author: Benjamin Sergeant
|
||||||
|
# Copyright (c) 2018 Machine Zone, Inc. All rights reserved.
|
||||||
|
#
|
||||||
|
|
||||||
|
cmake_minimum_required (VERSION 3.4.1)
|
||||||
|
project (ixwebsocket_unittest)
|
||||||
|
|
||||||
|
set (CMAKE_CXX_STANDARD 11)
|
||||||
|
|
||||||
|
option(USE_TLS "Add TLS support" ON)
|
||||||
|
|
||||||
|
add_subdirectory(${PROJECT_SOURCE_DIR}/.. ixwebsocket)
|
||||||
|
|
||||||
|
include_directories(
|
||||||
|
${PROJECT_SOURCE_DIR}/Catch2/single_include
|
||||||
|
)
|
||||||
|
|
||||||
|
add_executable(ixwebsocket_unittest
|
||||||
|
test_runner.cpp
|
||||||
|
cmd_websocket_chat.cpp
|
||||||
|
IXTest.cpp
|
||||||
|
msgpack11.cpp
|
||||||
|
)
|
||||||
|
|
||||||
|
if (APPLE AND USE_TLS)
|
||||||
|
target_link_libraries(ixwebsocket_unittest "-framework foundation" "-framework security")
|
||||||
|
endif()
|
||||||
|
|
||||||
|
target_link_libraries(ixwebsocket_unittest ixwebsocket)
|
||||||
|
install(TARGETS ixwebsocket_unittest DESTINATION bin)
|
12852
test/Catch2/single_include/catch.hpp
Normal file
12852
test/Catch2/single_include/catch.hpp
Normal file
File diff suppressed because it is too large
Load Diff
62
test/Catch2/single_include/catch_reporter_automake.hpp
Normal file
62
test/Catch2/single_include/catch_reporter_automake.hpp
Normal file
@ -0,0 +1,62 @@
|
|||||||
|
/*
|
||||||
|
* Created by Justin R. Wilson on 2/19/2017.
|
||||||
|
* Copyright 2017 Justin R. Wilson. All rights reserved.
|
||||||
|
*
|
||||||
|
* Distributed under the Boost Software License, Version 1.0. (See accompanying
|
||||||
|
* file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||||
|
*/
|
||||||
|
#ifndef TWOBLUECUBES_CATCH_REPORTER_AUTOMAKE_HPP_INCLUDED
|
||||||
|
#define TWOBLUECUBES_CATCH_REPORTER_AUTOMAKE_HPP_INCLUDED
|
||||||
|
|
||||||
|
// Don't #include any Catch headers here - we can assume they are already
|
||||||
|
// included before this header.
|
||||||
|
// This is not good practice in general but is necessary in this case so this
|
||||||
|
// file can be distributed as a single header that works with the main
|
||||||
|
// Catch single header.
|
||||||
|
|
||||||
|
namespace Catch {
|
||||||
|
|
||||||
|
struct AutomakeReporter : StreamingReporterBase<AutomakeReporter> {
|
||||||
|
AutomakeReporter( ReporterConfig const& _config )
|
||||||
|
: StreamingReporterBase( _config )
|
||||||
|
{}
|
||||||
|
|
||||||
|
~AutomakeReporter() override;
|
||||||
|
|
||||||
|
static std::string getDescription() {
|
||||||
|
return "Reports test results in the format of Automake .trs files";
|
||||||
|
}
|
||||||
|
|
||||||
|
void assertionStarting( AssertionInfo const& ) override {}
|
||||||
|
|
||||||
|
bool assertionEnded( AssertionStats const& /*_assertionStats*/ ) override { return true; }
|
||||||
|
|
||||||
|
void testCaseEnded( TestCaseStats const& _testCaseStats ) override {
|
||||||
|
// Possible values to emit are PASS, XFAIL, SKIP, FAIL, XPASS and ERROR.
|
||||||
|
stream << ":test-result: ";
|
||||||
|
if (_testCaseStats.totals.assertions.allPassed()) {
|
||||||
|
stream << "PASS";
|
||||||
|
} else if (_testCaseStats.totals.assertions.allOk()) {
|
||||||
|
stream << "XFAIL";
|
||||||
|
} else {
|
||||||
|
stream << "FAIL";
|
||||||
|
}
|
||||||
|
stream << ' ' << _testCaseStats.testInfo.name << '\n';
|
||||||
|
StreamingReporterBase::testCaseEnded( _testCaseStats );
|
||||||
|
}
|
||||||
|
|
||||||
|
void skipTest( TestCaseInfo const& testInfo ) override {
|
||||||
|
stream << ":test-result: SKIP " << testInfo.name << '\n';
|
||||||
|
}
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
#ifdef CATCH_IMPL
|
||||||
|
AutomakeReporter::~AutomakeReporter() {}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
CATCH_REGISTER_REPORTER( "automake", AutomakeReporter)
|
||||||
|
|
||||||
|
} // end namespace Catch
|
||||||
|
|
||||||
|
#endif // TWOBLUECUBES_CATCH_REPORTER_AUTOMAKE_HPP_INCLUDED
|
255
test/Catch2/single_include/catch_reporter_tap.hpp
Normal file
255
test/Catch2/single_include/catch_reporter_tap.hpp
Normal file
@ -0,0 +1,255 @@
|
|||||||
|
/*
|
||||||
|
* Created by Colton Wolkins on 2015-08-15.
|
||||||
|
* Copyright 2015 Martin Moene. All rights reserved.
|
||||||
|
*
|
||||||
|
* Distributed under the Boost Software License, Version 1.0. (See accompanying
|
||||||
|
* file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||||
|
*/
|
||||||
|
#ifndef TWOBLUECUBES_CATCH_REPORTER_TAP_HPP_INCLUDED
|
||||||
|
#define TWOBLUECUBES_CATCH_REPORTER_TAP_HPP_INCLUDED
|
||||||
|
|
||||||
|
|
||||||
|
// Don't #include any Catch headers here - we can assume they are already
|
||||||
|
// included before this header.
|
||||||
|
// This is not good practice in general but is necessary in this case so this
|
||||||
|
// file can be distributed as a single header that works with the main
|
||||||
|
// Catch single header.
|
||||||
|
|
||||||
|
#include <algorithm>
|
||||||
|
|
||||||
|
namespace Catch {
|
||||||
|
|
||||||
|
struct TAPReporter : StreamingReporterBase<TAPReporter> {
|
||||||
|
|
||||||
|
using StreamingReporterBase::StreamingReporterBase;
|
||||||
|
|
||||||
|
~TAPReporter() override;
|
||||||
|
|
||||||
|
static std::string getDescription() {
|
||||||
|
return "Reports test results in TAP format, suitable for test harnesses";
|
||||||
|
}
|
||||||
|
|
||||||
|
ReporterPreferences getPreferences() const override {
|
||||||
|
ReporterPreferences prefs;
|
||||||
|
prefs.shouldRedirectStdOut = false;
|
||||||
|
return prefs;
|
||||||
|
}
|
||||||
|
|
||||||
|
void noMatchingTestCases( std::string const& spec ) override {
|
||||||
|
stream << "# No test cases matched '" << spec << "'" << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
void assertionStarting( AssertionInfo const& ) override {}
|
||||||
|
|
||||||
|
bool assertionEnded( AssertionStats const& _assertionStats ) override {
|
||||||
|
++counter;
|
||||||
|
|
||||||
|
AssertionPrinter printer( stream, _assertionStats, counter );
|
||||||
|
printer.print();
|
||||||
|
stream << " # " << currentTestCaseInfo->name ;
|
||||||
|
|
||||||
|
stream << std::endl;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
void testRunEnded( TestRunStats const& _testRunStats ) override {
|
||||||
|
printTotals( _testRunStats.totals );
|
||||||
|
stream << "\n" << std::endl;
|
||||||
|
StreamingReporterBase::testRunEnded( _testRunStats );
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
std::size_t counter = 0;
|
||||||
|
class AssertionPrinter {
|
||||||
|
public:
|
||||||
|
AssertionPrinter& operator= ( AssertionPrinter const& ) = delete;
|
||||||
|
AssertionPrinter( AssertionPrinter const& ) = delete;
|
||||||
|
AssertionPrinter( std::ostream& _stream, AssertionStats const& _stats, std::size_t _counter )
|
||||||
|
: stream( _stream )
|
||||||
|
, result( _stats.assertionResult )
|
||||||
|
, messages( _stats.infoMessages )
|
||||||
|
, itMessage( _stats.infoMessages.begin() )
|
||||||
|
, printInfoMessages( true )
|
||||||
|
, counter(_counter)
|
||||||
|
{}
|
||||||
|
|
||||||
|
void print() {
|
||||||
|
itMessage = messages.begin();
|
||||||
|
|
||||||
|
switch( result.getResultType() ) {
|
||||||
|
case ResultWas::Ok:
|
||||||
|
printResultType( passedString() );
|
||||||
|
printOriginalExpression();
|
||||||
|
printReconstructedExpression();
|
||||||
|
if ( ! result.hasExpression() )
|
||||||
|
printRemainingMessages( Colour::None );
|
||||||
|
else
|
||||||
|
printRemainingMessages();
|
||||||
|
break;
|
||||||
|
case ResultWas::ExpressionFailed:
|
||||||
|
if (result.isOk()) {
|
||||||
|
printResultType(passedString());
|
||||||
|
} else {
|
||||||
|
printResultType(failedString());
|
||||||
|
}
|
||||||
|
printOriginalExpression();
|
||||||
|
printReconstructedExpression();
|
||||||
|
if (result.isOk()) {
|
||||||
|
printIssue(" # TODO");
|
||||||
|
}
|
||||||
|
printRemainingMessages();
|
||||||
|
break;
|
||||||
|
case ResultWas::ThrewException:
|
||||||
|
printResultType( failedString() );
|
||||||
|
printIssue( "unexpected exception with message:" );
|
||||||
|
printMessage();
|
||||||
|
printExpressionWas();
|
||||||
|
printRemainingMessages();
|
||||||
|
break;
|
||||||
|
case ResultWas::FatalErrorCondition:
|
||||||
|
printResultType( failedString() );
|
||||||
|
printIssue( "fatal error condition with message:" );
|
||||||
|
printMessage();
|
||||||
|
printExpressionWas();
|
||||||
|
printRemainingMessages();
|
||||||
|
break;
|
||||||
|
case ResultWas::DidntThrowException:
|
||||||
|
printResultType( failedString() );
|
||||||
|
printIssue( "expected exception, got none" );
|
||||||
|
printExpressionWas();
|
||||||
|
printRemainingMessages();
|
||||||
|
break;
|
||||||
|
case ResultWas::Info:
|
||||||
|
printResultType( "info" );
|
||||||
|
printMessage();
|
||||||
|
printRemainingMessages();
|
||||||
|
break;
|
||||||
|
case ResultWas::Warning:
|
||||||
|
printResultType( "warning" );
|
||||||
|
printMessage();
|
||||||
|
printRemainingMessages();
|
||||||
|
break;
|
||||||
|
case ResultWas::ExplicitFailure:
|
||||||
|
printResultType( failedString() );
|
||||||
|
printIssue( "explicitly" );
|
||||||
|
printRemainingMessages( Colour::None );
|
||||||
|
break;
|
||||||
|
// These cases are here to prevent compiler warnings
|
||||||
|
case ResultWas::Unknown:
|
||||||
|
case ResultWas::FailureBit:
|
||||||
|
case ResultWas::Exception:
|
||||||
|
printResultType( "** internal error **" );
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
static Colour::Code dimColour() { return Colour::FileName; }
|
||||||
|
|
||||||
|
static const char* failedString() { return "not ok"; }
|
||||||
|
static const char* passedString() { return "ok"; }
|
||||||
|
|
||||||
|
void printSourceInfo() const {
|
||||||
|
Colour colourGuard( dimColour() );
|
||||||
|
stream << result.getSourceInfo() << ":";
|
||||||
|
}
|
||||||
|
|
||||||
|
void printResultType( std::string const& passOrFail ) const {
|
||||||
|
if( !passOrFail.empty() ) {
|
||||||
|
stream << passOrFail << ' ' << counter << " -";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void printIssue( std::string const& issue ) const {
|
||||||
|
stream << " " << issue;
|
||||||
|
}
|
||||||
|
|
||||||
|
void printExpressionWas() {
|
||||||
|
if( result.hasExpression() ) {
|
||||||
|
stream << ";";
|
||||||
|
{
|
||||||
|
Colour colour( dimColour() );
|
||||||
|
stream << " expression was:";
|
||||||
|
}
|
||||||
|
printOriginalExpression();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void printOriginalExpression() const {
|
||||||
|
if( result.hasExpression() ) {
|
||||||
|
stream << " " << result.getExpression();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void printReconstructedExpression() const {
|
||||||
|
if( result.hasExpandedExpression() ) {
|
||||||
|
{
|
||||||
|
Colour colour( dimColour() );
|
||||||
|
stream << " for: ";
|
||||||
|
}
|
||||||
|
std::string expr = result.getExpandedExpression();
|
||||||
|
std::replace( expr.begin(), expr.end(), '\n', ' ');
|
||||||
|
stream << expr;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void printMessage() {
|
||||||
|
if ( itMessage != messages.end() ) {
|
||||||
|
stream << " '" << itMessage->message << "'";
|
||||||
|
++itMessage;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void printRemainingMessages( Colour::Code colour = dimColour() ) {
|
||||||
|
if (itMessage == messages.end()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// using messages.end() directly (or auto) yields compilation error:
|
||||||
|
std::vector<MessageInfo>::const_iterator itEnd = messages.end();
|
||||||
|
const std::size_t N = static_cast<std::size_t>( std::distance( itMessage, itEnd ) );
|
||||||
|
|
||||||
|
{
|
||||||
|
Colour colourGuard( colour );
|
||||||
|
stream << " with " << pluralise( N, "message" ) << ":";
|
||||||
|
}
|
||||||
|
|
||||||
|
for(; itMessage != itEnd; ) {
|
||||||
|
// If this assertion is a warning ignore any INFO messages
|
||||||
|
if( printInfoMessages || itMessage->type != ResultWas::Info ) {
|
||||||
|
stream << " '" << itMessage->message << "'";
|
||||||
|
if ( ++itMessage != itEnd ) {
|
||||||
|
Colour colourGuard( dimColour() );
|
||||||
|
stream << " and";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
std::ostream& stream;
|
||||||
|
AssertionResult const& result;
|
||||||
|
std::vector<MessageInfo> messages;
|
||||||
|
std::vector<MessageInfo>::const_iterator itMessage;
|
||||||
|
bool printInfoMessages;
|
||||||
|
std::size_t counter;
|
||||||
|
};
|
||||||
|
|
||||||
|
void printTotals( const Totals& totals ) const {
|
||||||
|
if( totals.testCases.total() == 0 ) {
|
||||||
|
stream << "1..0 # Skipped: No tests ran.";
|
||||||
|
} else {
|
||||||
|
stream << "1.." << counter;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
#ifdef CATCH_IMPL
|
||||||
|
TAPReporter::~TAPReporter() {}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
CATCH_REGISTER_REPORTER( "tap", TAPReporter )
|
||||||
|
|
||||||
|
} // end namespace Catch
|
||||||
|
|
||||||
|
#endif // TWOBLUECUBES_CATCH_REPORTER_TAP_HPP_INCLUDED
|
220
test/Catch2/single_include/catch_reporter_teamcity.hpp
Normal file
220
test/Catch2/single_include/catch_reporter_teamcity.hpp
Normal file
@ -0,0 +1,220 @@
|
|||||||
|
/*
|
||||||
|
* Created by Phil Nash on 19th December 2014
|
||||||
|
* Copyright 2014 Two Blue Cubes Ltd. All rights reserved.
|
||||||
|
*
|
||||||
|
* Distributed under the Boost Software License, Version 1.0. (See accompanying
|
||||||
|
* file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||||
|
*/
|
||||||
|
#ifndef TWOBLUECUBES_CATCH_REPORTER_TEAMCITY_HPP_INCLUDED
|
||||||
|
#define TWOBLUECUBES_CATCH_REPORTER_TEAMCITY_HPP_INCLUDED
|
||||||
|
|
||||||
|
// Don't #include any Catch headers here - we can assume they are already
|
||||||
|
// included before this header.
|
||||||
|
// This is not good practice in general but is necessary in this case so this
|
||||||
|
// file can be distributed as a single header that works with the main
|
||||||
|
// Catch single header.
|
||||||
|
|
||||||
|
#include <cstring>
|
||||||
|
|
||||||
|
#ifdef __clang__
|
||||||
|
# pragma clang diagnostic push
|
||||||
|
# pragma clang diagnostic ignored "-Wpadded"
|
||||||
|
#endif
|
||||||
|
|
||||||
|
namespace Catch {
|
||||||
|
|
||||||
|
struct TeamCityReporter : StreamingReporterBase<TeamCityReporter> {
|
||||||
|
TeamCityReporter( ReporterConfig const& _config )
|
||||||
|
: StreamingReporterBase( _config )
|
||||||
|
{
|
||||||
|
m_reporterPrefs.shouldRedirectStdOut = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
static std::string escape( std::string const& str ) {
|
||||||
|
std::string escaped = str;
|
||||||
|
replaceInPlace( escaped, "|", "||" );
|
||||||
|
replaceInPlace( escaped, "'", "|'" );
|
||||||
|
replaceInPlace( escaped, "\n", "|n" );
|
||||||
|
replaceInPlace( escaped, "\r", "|r" );
|
||||||
|
replaceInPlace( escaped, "[", "|[" );
|
||||||
|
replaceInPlace( escaped, "]", "|]" );
|
||||||
|
return escaped;
|
||||||
|
}
|
||||||
|
~TeamCityReporter() override;
|
||||||
|
|
||||||
|
static std::string getDescription() {
|
||||||
|
return "Reports test results as TeamCity service messages";
|
||||||
|
}
|
||||||
|
|
||||||
|
void skipTest( TestCaseInfo const& /* testInfo */ ) override {
|
||||||
|
}
|
||||||
|
|
||||||
|
void noMatchingTestCases( std::string const& /* spec */ ) override {}
|
||||||
|
|
||||||
|
void testGroupStarting( GroupInfo const& groupInfo ) override {
|
||||||
|
StreamingReporterBase::testGroupStarting( groupInfo );
|
||||||
|
stream << "##teamcity[testSuiteStarted name='"
|
||||||
|
<< escape( groupInfo.name ) << "']\n";
|
||||||
|
}
|
||||||
|
void testGroupEnded( TestGroupStats const& testGroupStats ) override {
|
||||||
|
StreamingReporterBase::testGroupEnded( testGroupStats );
|
||||||
|
stream << "##teamcity[testSuiteFinished name='"
|
||||||
|
<< escape( testGroupStats.groupInfo.name ) << "']\n";
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void assertionStarting( AssertionInfo const& ) override {}
|
||||||
|
|
||||||
|
bool assertionEnded( AssertionStats const& assertionStats ) override {
|
||||||
|
AssertionResult const& result = assertionStats.assertionResult;
|
||||||
|
if( !result.isOk() ) {
|
||||||
|
|
||||||
|
ReusableStringStream msg;
|
||||||
|
if( !m_headerPrintedForThisSection )
|
||||||
|
printSectionHeader( msg.get() );
|
||||||
|
m_headerPrintedForThisSection = true;
|
||||||
|
|
||||||
|
msg << result.getSourceInfo() << "\n";
|
||||||
|
|
||||||
|
switch( result.getResultType() ) {
|
||||||
|
case ResultWas::ExpressionFailed:
|
||||||
|
msg << "expression failed";
|
||||||
|
break;
|
||||||
|
case ResultWas::ThrewException:
|
||||||
|
msg << "unexpected exception";
|
||||||
|
break;
|
||||||
|
case ResultWas::FatalErrorCondition:
|
||||||
|
msg << "fatal error condition";
|
||||||
|
break;
|
||||||
|
case ResultWas::DidntThrowException:
|
||||||
|
msg << "no exception was thrown where one was expected";
|
||||||
|
break;
|
||||||
|
case ResultWas::ExplicitFailure:
|
||||||
|
msg << "explicit failure";
|
||||||
|
break;
|
||||||
|
|
||||||
|
// We shouldn't get here because of the isOk() test
|
||||||
|
case ResultWas::Ok:
|
||||||
|
case ResultWas::Info:
|
||||||
|
case ResultWas::Warning:
|
||||||
|
throw std::domain_error( "Internal error in TeamCity reporter" );
|
||||||
|
// These cases are here to prevent compiler warnings
|
||||||
|
case ResultWas::Unknown:
|
||||||
|
case ResultWas::FailureBit:
|
||||||
|
case ResultWas::Exception:
|
||||||
|
throw std::domain_error( "Not implemented" );
|
||||||
|
}
|
||||||
|
if( assertionStats.infoMessages.size() == 1 )
|
||||||
|
msg << " with message:";
|
||||||
|
if( assertionStats.infoMessages.size() > 1 )
|
||||||
|
msg << " with messages:";
|
||||||
|
for( auto const& messageInfo : assertionStats.infoMessages )
|
||||||
|
msg << "\n \"" << messageInfo.message << "\"";
|
||||||
|
|
||||||
|
|
||||||
|
if( result.hasExpression() ) {
|
||||||
|
msg <<
|
||||||
|
"\n " << result.getExpressionInMacro() << "\n"
|
||||||
|
"with expansion:\n" <<
|
||||||
|
" " << result.getExpandedExpression() << "\n";
|
||||||
|
}
|
||||||
|
|
||||||
|
if( currentTestCaseInfo->okToFail() ) {
|
||||||
|
msg << "- failure ignore as test marked as 'ok to fail'\n";
|
||||||
|
stream << "##teamcity[testIgnored"
|
||||||
|
<< " name='" << escape( currentTestCaseInfo->name )<< "'"
|
||||||
|
<< " message='" << escape( msg.str() ) << "'"
|
||||||
|
<< "]\n";
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
stream << "##teamcity[testFailed"
|
||||||
|
<< " name='" << escape( currentTestCaseInfo->name )<< "'"
|
||||||
|
<< " message='" << escape( msg.str() ) << "'"
|
||||||
|
<< "]\n";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
stream.flush();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
void sectionStarting( SectionInfo const& sectionInfo ) override {
|
||||||
|
m_headerPrintedForThisSection = false;
|
||||||
|
StreamingReporterBase::sectionStarting( sectionInfo );
|
||||||
|
}
|
||||||
|
|
||||||
|
void testCaseStarting( TestCaseInfo const& testInfo ) override {
|
||||||
|
m_testTimer.start();
|
||||||
|
StreamingReporterBase::testCaseStarting( testInfo );
|
||||||
|
stream << "##teamcity[testStarted name='"
|
||||||
|
<< escape( testInfo.name ) << "']\n";
|
||||||
|
stream.flush();
|
||||||
|
}
|
||||||
|
|
||||||
|
void testCaseEnded( TestCaseStats const& testCaseStats ) override {
|
||||||
|
StreamingReporterBase::testCaseEnded( testCaseStats );
|
||||||
|
if( !testCaseStats.stdOut.empty() )
|
||||||
|
stream << "##teamcity[testStdOut name='"
|
||||||
|
<< escape( testCaseStats.testInfo.name )
|
||||||
|
<< "' out='" << escape( testCaseStats.stdOut ) << "']\n";
|
||||||
|
if( !testCaseStats.stdErr.empty() )
|
||||||
|
stream << "##teamcity[testStdErr name='"
|
||||||
|
<< escape( testCaseStats.testInfo.name )
|
||||||
|
<< "' out='" << escape( testCaseStats.stdErr ) << "']\n";
|
||||||
|
stream << "##teamcity[testFinished name='"
|
||||||
|
<< escape( testCaseStats.testInfo.name ) << "' duration='"
|
||||||
|
<< m_testTimer.getElapsedMilliseconds() << "']\n";
|
||||||
|
stream.flush();
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
void printSectionHeader( std::ostream& os ) {
|
||||||
|
assert( !m_sectionStack.empty() );
|
||||||
|
|
||||||
|
if( m_sectionStack.size() > 1 ) {
|
||||||
|
os << getLineOfChars<'-'>() << "\n";
|
||||||
|
|
||||||
|
std::vector<SectionInfo>::const_iterator
|
||||||
|
it = m_sectionStack.begin()+1, // Skip first section (test case)
|
||||||
|
itEnd = m_sectionStack.end();
|
||||||
|
for( ; it != itEnd; ++it )
|
||||||
|
printHeaderString( os, it->name );
|
||||||
|
os << getLineOfChars<'-'>() << "\n";
|
||||||
|
}
|
||||||
|
|
||||||
|
SourceLineInfo lineInfo = m_sectionStack.front().lineInfo;
|
||||||
|
|
||||||
|
if( !lineInfo.empty() )
|
||||||
|
os << lineInfo << "\n";
|
||||||
|
os << getLineOfChars<'.'>() << "\n\n";
|
||||||
|
}
|
||||||
|
|
||||||
|
// if string has a : in first line will set indent to follow it on
|
||||||
|
// subsequent lines
|
||||||
|
static void printHeaderString( std::ostream& os, std::string const& _string, std::size_t indent = 0 ) {
|
||||||
|
std::size_t i = _string.find( ": " );
|
||||||
|
if( i != std::string::npos )
|
||||||
|
i+=2;
|
||||||
|
else
|
||||||
|
i = 0;
|
||||||
|
os << Column( _string )
|
||||||
|
.indent( indent+i)
|
||||||
|
.initialIndent( indent ) << "\n";
|
||||||
|
}
|
||||||
|
private:
|
||||||
|
bool m_headerPrintedForThisSection = false;
|
||||||
|
Timer m_testTimer;
|
||||||
|
};
|
||||||
|
|
||||||
|
#ifdef CATCH_IMPL
|
||||||
|
TeamCityReporter::~TeamCityReporter() {}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
CATCH_REGISTER_REPORTER( "teamcity", TeamCityReporter )
|
||||||
|
|
||||||
|
} // end namespace Catch
|
||||||
|
|
||||||
|
#ifdef __clang__
|
||||||
|
# pragma clang diagnostic pop
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#endif // TWOBLUECUBES_CATCH_REPORTER_TEAMCITY_HPP_INCLUDED
|
61
test/IXTest.cpp
Normal file
61
test/IXTest.cpp
Normal file
@ -0,0 +1,61 @@
|
|||||||
|
/*
|
||||||
|
* IXTest.cpp
|
||||||
|
* Author: Benjamin Sergeant
|
||||||
|
* Copyright (c) 2018 Machine Zone. All rights reserved.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "IXTest.h"
|
||||||
|
#include <ixwebsocket/IXWebSocket.h>
|
||||||
|
|
||||||
|
#include <chrono>
|
||||||
|
#include <thread>
|
||||||
|
#include <string>
|
||||||
|
#include <fstream>
|
||||||
|
#include <iostream>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
namespace ix
|
||||||
|
{
|
||||||
|
std::atomic<size_t> incomingBytes(0);
|
||||||
|
std::atomic<size_t> outgoingBytes(0);
|
||||||
|
|
||||||
|
void setupWebSocketTrafficTrackerCallback()
|
||||||
|
{
|
||||||
|
ix::WebSocket::setTrafficTrackerCallback(
|
||||||
|
[](size_t size, bool incoming)
|
||||||
|
{
|
||||||
|
if (incoming)
|
||||||
|
{
|
||||||
|
incomingBytes += size;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
outgoingBytes += size;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
void reportWebSocketTraffic()
|
||||||
|
{
|
||||||
|
std::cout << incomingBytes << std::endl;
|
||||||
|
std::cout << "Incoming bytes: " << incomingBytes << std::endl;
|
||||||
|
std::cout << "Outgoing bytes: " << outgoingBytes << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
void msleep(int ms)
|
||||||
|
{
|
||||||
|
std::chrono::duration<double, std::milli> duration(ms);
|
||||||
|
std::this_thread::sleep_for(duration);
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string generateSessionId()
|
||||||
|
{
|
||||||
|
auto now = std::chrono::system_clock::now();
|
||||||
|
auto seconds =
|
||||||
|
std::chrono::duration_cast<std::chrono::seconds>(
|
||||||
|
now.time_since_epoch()).count();
|
||||||
|
|
||||||
|
return std::to_string(seconds);
|
||||||
|
}
|
||||||
|
}
|
24
test/IXTest.h
Normal file
24
test/IXTest.h
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
/*
|
||||||
|
* IXTest.h
|
||||||
|
* Author: Benjamin Sergeant
|
||||||
|
* Copyright (c) 2018 Machine Zone. All rights reserved.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
#include <vector>
|
||||||
|
#include <sstream>
|
||||||
|
|
||||||
|
namespace ix
|
||||||
|
{
|
||||||
|
// Sleep for ms milliseconds.
|
||||||
|
void msleep(int ms);
|
||||||
|
|
||||||
|
// Generate a relatively random string
|
||||||
|
std::string generateSessionId();
|
||||||
|
|
||||||
|
// Record and report websocket traffic
|
||||||
|
void setupWebSocketTrafficTrackerCallback();
|
||||||
|
void reportWebSocketTraffic();
|
||||||
|
}
|
23
test/broadcast-server.js
Normal file
23
test/broadcast-server.js
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
const WebSocket = require('ws');
|
||||||
|
|
||||||
|
const wss = new WebSocket.Server({ port: 8080 });
|
||||||
|
|
||||||
|
// Broadcast to all.
|
||||||
|
wss.broadcast = function broadcast(data) {
|
||||||
|
wss.clients.forEach(function each(client) {
|
||||||
|
if (client.readyState === WebSocket.OPEN) {
|
||||||
|
client.send(data);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
wss.on('connection', function connection(ws) {
|
||||||
|
ws.on('message', function incoming(data) {
|
||||||
|
// Broadcast to everyone else.
|
||||||
|
wss.clients.forEach(function each(client) {
|
||||||
|
if (client !== ws && client.readyState === WebSocket.OPEN) {
|
||||||
|
client.send(data);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
217
test/cmd_websocket_chat.cpp
Normal file
217
test/cmd_websocket_chat.cpp
Normal file
@ -0,0 +1,217 @@
|
|||||||
|
/*
|
||||||
|
* cmd_websocket_chat.cpp
|
||||||
|
* Author: Benjamin Sergeant
|
||||||
|
* Copyright (c) 2017 Machine Zone. All rights reserved.
|
||||||
|
*/
|
||||||
|
|
||||||
|
//
|
||||||
|
// Simple chat program that talks to the node.js server at
|
||||||
|
// websocket_chat_server/broacast-server.js
|
||||||
|
//
|
||||||
|
|
||||||
|
#include <iostream>
|
||||||
|
#include <sstream>
|
||||||
|
#include <queue>
|
||||||
|
#include <ixwebsocket/IXWebSocket.h>
|
||||||
|
#include "msgpack11.hpp"
|
||||||
|
|
||||||
|
#include "IXTest.h"
|
||||||
|
|
||||||
|
#include "catch.hpp"
|
||||||
|
|
||||||
|
using msgpack11::MsgPack;
|
||||||
|
using namespace ix;
|
||||||
|
|
||||||
|
namespace
|
||||||
|
{
|
||||||
|
class WebSocketChat
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
WebSocketChat(const std::string& user,
|
||||||
|
const std::string& session);
|
||||||
|
|
||||||
|
void subscribe(const std::string& channel);
|
||||||
|
void start();
|
||||||
|
void stop();
|
||||||
|
bool isReady() const;
|
||||||
|
|
||||||
|
void sendMessage(const std::string& text);
|
||||||
|
size_t getReceivedMessagesCount() const;
|
||||||
|
|
||||||
|
std::string encodeMessage(const std::string& text);
|
||||||
|
std::pair<std::string, std::string> decodeMessage(const std::string& str);
|
||||||
|
|
||||||
|
private:
|
||||||
|
void log(const std::string& msg);
|
||||||
|
|
||||||
|
std::string _user;
|
||||||
|
std::string _session;
|
||||||
|
|
||||||
|
ix::WebSocket _webSocket;
|
||||||
|
|
||||||
|
std::queue<std::string> _receivedQueue;
|
||||||
|
};
|
||||||
|
|
||||||
|
WebSocketChat::WebSocketChat(const std::string& user,
|
||||||
|
const std::string& session) :
|
||||||
|
_user(user),
|
||||||
|
_session(session)
|
||||||
|
{
|
||||||
|
;
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t WebSocketChat::getReceivedMessagesCount() const
|
||||||
|
{
|
||||||
|
return _receivedQueue.size();
|
||||||
|
}
|
||||||
|
|
||||||
|
bool WebSocketChat::isReady() const
|
||||||
|
{
|
||||||
|
return _webSocket.getReadyState() == ix::WebSocket_ReadyState_Open;
|
||||||
|
}
|
||||||
|
|
||||||
|
void WebSocketChat::stop()
|
||||||
|
{
|
||||||
|
_webSocket.stop();
|
||||||
|
}
|
||||||
|
|
||||||
|
void WebSocketChat::log(const std::string& msg)
|
||||||
|
{
|
||||||
|
std::cerr << msg << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
void WebSocketChat::start()
|
||||||
|
{
|
||||||
|
std::string url("ws://localhost:8080/");
|
||||||
|
_webSocket.setUrl(url);
|
||||||
|
|
||||||
|
std::stringstream ss;
|
||||||
|
log(std::string("Connecting to url: ") + url);
|
||||||
|
|
||||||
|
_webSocket.setOnMessageCallback(
|
||||||
|
[this](ix::WebSocketMessageType messageType,
|
||||||
|
const std::string& str,
|
||||||
|
size_t wireSize,
|
||||||
|
const ix::WebSocketErrorInfo& error,
|
||||||
|
const ix::WebSocketCloseInfo& closeInfo,
|
||||||
|
const ix::WebSocketHttpHeaders& headers)
|
||||||
|
{
|
||||||
|
std::stringstream ss;
|
||||||
|
if (messageType == ix::WebSocket_MessageType_Open)
|
||||||
|
{
|
||||||
|
ss << "cmd_websocket_chat: user "
|
||||||
|
<< _user
|
||||||
|
<< " Connected !";
|
||||||
|
log(ss.str());
|
||||||
|
}
|
||||||
|
else if (messageType == ix::WebSocket_MessageType_Close)
|
||||||
|
{
|
||||||
|
ss << "cmd_websocket_chat: user "
|
||||||
|
<< _user
|
||||||
|
<< " disconnected !";
|
||||||
|
log(ss.str());
|
||||||
|
}
|
||||||
|
else if (messageType == ix::WebSocket_MessageType_Message)
|
||||||
|
{
|
||||||
|
auto result = decodeMessage(str);
|
||||||
|
|
||||||
|
// Our "chat" / "broacast" node.js server does not send us
|
||||||
|
// the messages we send, so we don't need to have a msg_user != user
|
||||||
|
// as we do for the satori chat example.
|
||||||
|
|
||||||
|
// store text
|
||||||
|
_receivedQueue.push(result.second);
|
||||||
|
|
||||||
|
ss << std::endl
|
||||||
|
<< result.first << " > " << result.second
|
||||||
|
<< std::endl
|
||||||
|
<< _user << " > ";
|
||||||
|
log(ss.str());
|
||||||
|
}
|
||||||
|
else if (messageType == ix::WebSocket_MessageType_Error)
|
||||||
|
{
|
||||||
|
ss << "cmd_websocket_chat: Error ! " << error.reason;
|
||||||
|
log(ss.str());
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// FIXME: missing ping/pong messages
|
||||||
|
ss << "Invalid ix::WebSocketMessageType";
|
||||||
|
log(ss.str());
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
_webSocket.start();
|
||||||
|
}
|
||||||
|
|
||||||
|
std::pair<std::string, std::string> WebSocketChat::decodeMessage(const std::string& str)
|
||||||
|
{
|
||||||
|
std::string errMsg;
|
||||||
|
MsgPack msg = MsgPack::parse(str, errMsg);
|
||||||
|
|
||||||
|
std::string msg_user = msg["user"].string_value();
|
||||||
|
std::string msg_text = msg["text"].string_value();
|
||||||
|
|
||||||
|
return std::pair<std::string, std::string>(msg_user, msg_text);
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string WebSocketChat::encodeMessage(const std::string& text)
|
||||||
|
{
|
||||||
|
std::map<MsgPack, MsgPack> obj;
|
||||||
|
obj["user"] = _user;
|
||||||
|
obj["text"] = text;
|
||||||
|
|
||||||
|
MsgPack msg(obj);
|
||||||
|
|
||||||
|
std::string output = msg.dump();
|
||||||
|
return output;
|
||||||
|
}
|
||||||
|
|
||||||
|
void WebSocketChat::sendMessage(const std::string& text)
|
||||||
|
{
|
||||||
|
_webSocket.send(encodeMessage(text));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_CASE("Websocket chat", "[websocket_chat]")
|
||||||
|
{
|
||||||
|
SECTION("Exchange and count sent/received messages.")
|
||||||
|
{
|
||||||
|
ix::setupWebSocketTrafficTrackerCallback();
|
||||||
|
|
||||||
|
std::string session = ix::generateSessionId();
|
||||||
|
WebSocketChat chatA("jean", session);
|
||||||
|
WebSocketChat chatB("paul", session);
|
||||||
|
|
||||||
|
chatA.start();
|
||||||
|
chatB.start();
|
||||||
|
|
||||||
|
// Wait for all chat instance to be ready
|
||||||
|
while (true)
|
||||||
|
{
|
||||||
|
if (chatA.isReady() && chatB.isReady()) break;
|
||||||
|
ix::msleep(10);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Add a bit of extra time, for the subscription to be active
|
||||||
|
ix::msleep(200);
|
||||||
|
|
||||||
|
chatA.sendMessage("from A1");
|
||||||
|
chatA.sendMessage("from A2");
|
||||||
|
chatA.sendMessage("from A3");
|
||||||
|
|
||||||
|
chatB.sendMessage("from B1");
|
||||||
|
chatB.sendMessage("from B2");
|
||||||
|
|
||||||
|
// Give us 1s for all messages to be received
|
||||||
|
ix::msleep(1000);
|
||||||
|
|
||||||
|
chatA.stop();
|
||||||
|
chatB.stop();
|
||||||
|
|
||||||
|
REQUIRE(chatA.getReceivedMessagesCount() == 2);
|
||||||
|
REQUIRE(chatB.getReceivedMessagesCount() == 3);
|
||||||
|
|
||||||
|
ix::reportWebSocketTraffic();
|
||||||
|
}
|
||||||
|
}
|
1050
test/msgpack11.cpp
Normal file
1050
test/msgpack11.cpp
Normal file
File diff suppressed because it is too large
Load Diff
217
test/msgpack11.hpp
Normal file
217
test/msgpack11.hpp
Normal file
@ -0,0 +1,217 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
#include <vector>
|
||||||
|
#include <map>
|
||||||
|
#include <memory>
|
||||||
|
#include <initializer_list>
|
||||||
|
|
||||||
|
#ifdef _MSC_VER
|
||||||
|
#if _MSC_VER <= 1800 // VS 2013
|
||||||
|
#ifndef noexcept
|
||||||
|
#define noexcept throw()
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef snprintf
|
||||||
|
#define snprintf _snprintf_s
|
||||||
|
#endif
|
||||||
|
#endif
|
||||||
|
#endif
|
||||||
|
|
||||||
|
namespace msgpack11 {
|
||||||
|
|
||||||
|
class MsgPackValue;
|
||||||
|
|
||||||
|
class MsgPack final {
|
||||||
|
public:
|
||||||
|
// Types
|
||||||
|
enum Type {
|
||||||
|
NUL,
|
||||||
|
FLOAT32,
|
||||||
|
FLOAT64,
|
||||||
|
INT8,
|
||||||
|
INT16,
|
||||||
|
INT32,
|
||||||
|
INT64,
|
||||||
|
UINT8,
|
||||||
|
UINT16,
|
||||||
|
UINT32,
|
||||||
|
UINT64,
|
||||||
|
BOOL,
|
||||||
|
STRING,
|
||||||
|
BINARY,
|
||||||
|
ARRAY,
|
||||||
|
OBJECT,
|
||||||
|
EXTENSION
|
||||||
|
};
|
||||||
|
|
||||||
|
// Array and object typedefs
|
||||||
|
typedef std::vector<MsgPack> array;
|
||||||
|
typedef std::map<MsgPack, MsgPack> object;
|
||||||
|
|
||||||
|
// Binary and extension typedefs
|
||||||
|
typedef std::vector<uint8_t> binary;
|
||||||
|
typedef std::tuple<int8_t, binary> extension;
|
||||||
|
|
||||||
|
// Constructors for the various types of JSON value.
|
||||||
|
MsgPack() noexcept; // NUL
|
||||||
|
MsgPack(std::nullptr_t) noexcept; // NUL
|
||||||
|
MsgPack(float value); // FLOAT32
|
||||||
|
MsgPack(double value); // FLOAT64
|
||||||
|
MsgPack(int8_t value); // INT8
|
||||||
|
MsgPack(int16_t value); // INT16
|
||||||
|
MsgPack(int32_t value); // INT32
|
||||||
|
MsgPack(int64_t value); // INT64
|
||||||
|
MsgPack(uint8_t value); // UINT8
|
||||||
|
MsgPack(uint16_t value); // UINT16
|
||||||
|
MsgPack(uint32_t value); // UINT32
|
||||||
|
MsgPack(uint64_t value); // UINT64
|
||||||
|
MsgPack(bool value); // BOOL
|
||||||
|
MsgPack(const std::string &value); // STRING
|
||||||
|
MsgPack(std::string &&value); // STRING
|
||||||
|
MsgPack(const char * value); // STRING
|
||||||
|
MsgPack(const array &values); // ARRAY
|
||||||
|
MsgPack(array &&values); // ARRAY
|
||||||
|
MsgPack(const object &values); // OBJECT
|
||||||
|
MsgPack(object &&values); // OBJECT
|
||||||
|
MsgPack(const binary &values); // BINARY
|
||||||
|
MsgPack(binary &&values); // BINARY
|
||||||
|
MsgPack(const extension &values); // EXTENSION
|
||||||
|
MsgPack(extension &&values); // EXTENSION
|
||||||
|
|
||||||
|
// Implicit constructor: anything with a to_msgpack() function.
|
||||||
|
template <class T, class = decltype(&T::to_msgpack)>
|
||||||
|
MsgPack(const T & t) : MsgPack(t.to_msgpack()) {}
|
||||||
|
|
||||||
|
// Implicit constructor: map-like objects (std::map, std::unordered_map, etc)
|
||||||
|
template <class M, typename std::enable_if<
|
||||||
|
std::is_constructible<MsgPack, typename M::key_type>::value
|
||||||
|
&& std::is_constructible<MsgPack, typename M::mapped_type>::value,
|
||||||
|
int>::type = 0>
|
||||||
|
MsgPack(const M & m) : MsgPack(object(m.begin(), m.end())) {}
|
||||||
|
|
||||||
|
// Implicit constructor: vector-like objects (std::list, std::vector, std::set, etc)
|
||||||
|
template <class V, typename std::enable_if<
|
||||||
|
std::is_constructible<MsgPack, typename V::value_type>::value &&
|
||||||
|
!std::is_same<typename binary::value_type, typename V::value_type>::value,
|
||||||
|
int>::type = 0>
|
||||||
|
MsgPack(const V & v) : MsgPack(array(v.begin(), v.end())) {}
|
||||||
|
|
||||||
|
template <class V, typename std::enable_if<
|
||||||
|
std::is_constructible<MsgPack, typename V::value_type>::value &&
|
||||||
|
std::is_same<typename binary::value_type, typename V::value_type>::value,
|
||||||
|
int>::type = 0>
|
||||||
|
MsgPack(const V & v) : MsgPack(binary(v.begin(), v.end())) {}
|
||||||
|
|
||||||
|
// This prevents MsgPack(some_pointer) from accidentally producing a bool. Use
|
||||||
|
// MsgPack(bool(some_pointer)) if that behavior is desired.
|
||||||
|
MsgPack(void *) = delete;
|
||||||
|
|
||||||
|
// Accessors
|
||||||
|
Type type() const;
|
||||||
|
|
||||||
|
bool is_null() const { return type() == NUL; }
|
||||||
|
bool is_bool() const { return type() == BOOL; }
|
||||||
|
bool is_number() const { return type() == FLOAT64; }
|
||||||
|
bool is_float32() const { return type() == FLOAT32; }
|
||||||
|
bool is_float64() const { return type() == FLOAT64; }
|
||||||
|
bool is_int() const { return type() == INT32; }
|
||||||
|
bool is_int8() const { return type() == INT8; }
|
||||||
|
bool is_int16() const { return type() == INT16; }
|
||||||
|
bool is_int32() const { return type() == INT32; }
|
||||||
|
bool is_int64() const { return type() == INT64; }
|
||||||
|
bool is_uint8() const { return type() == UINT8; }
|
||||||
|
bool is_uint16() const { return type() == UINT16; }
|
||||||
|
bool is_uint32() const { return type() == UINT32; }
|
||||||
|
bool is_uint64() const { return type() == UINT64; }
|
||||||
|
bool is_string() const { return type() == STRING; }
|
||||||
|
bool is_array() const { return type() == ARRAY; }
|
||||||
|
bool is_binary() const { return type() == BINARY; }
|
||||||
|
bool is_object() const { return type() == OBJECT; }
|
||||||
|
bool is_extension() const { return type() == EXTENSION; }
|
||||||
|
|
||||||
|
// Return the enclosed value if this is a number, 0 otherwise. Note that msgpack11 does not
|
||||||
|
// distinguish between integer and non-integer numbers - number_value() and int_value()
|
||||||
|
// can both be applied to a NUMBER-typed object.
|
||||||
|
double number_value() const;
|
||||||
|
float float32_value() const;
|
||||||
|
double float64_value() const;
|
||||||
|
int32_t int_value() const;
|
||||||
|
int8_t int8_value() const;
|
||||||
|
int16_t int16_value() const;
|
||||||
|
int32_t int32_value() const;
|
||||||
|
int64_t int64_value() const;
|
||||||
|
uint8_t uint8_value() const;
|
||||||
|
uint16_t uint16_value() const;
|
||||||
|
uint32_t uint32_value() const;
|
||||||
|
uint64_t uint64_value() const;
|
||||||
|
|
||||||
|
// Return the enclosed value if this is a boolean, false otherwise.
|
||||||
|
bool bool_value() const;
|
||||||
|
// Return the enclosed string if this is a string, "" otherwise.
|
||||||
|
const std::string &string_value() const;
|
||||||
|
// Return the enclosed std::vector if this is an array, or an empty vector otherwise.
|
||||||
|
const array &array_items() const;
|
||||||
|
// Return the enclosed std::map if this is an object, or an empty map otherwise.
|
||||||
|
const object &object_items() const;
|
||||||
|
// Return the enclosed std::vector if this is an binary, or an empty map otherwise.
|
||||||
|
const binary &binary_items() const;
|
||||||
|
// Return the enclosed std::tuple if this is an extension, or an empty map otherwise.
|
||||||
|
const extension &extension_items() const;
|
||||||
|
|
||||||
|
// Return a reference to arr[i] if this is an array, MsgPack() otherwise.
|
||||||
|
const MsgPack & operator[](size_t i) const;
|
||||||
|
// Return a reference to obj[key] if this is an object, MsgPack() otherwise.
|
||||||
|
const MsgPack & operator[](const std::string &key) const;
|
||||||
|
|
||||||
|
// Serialize.
|
||||||
|
void dump(std::string &out) const;
|
||||||
|
std::string dump() const {
|
||||||
|
std::string out;
|
||||||
|
dump(out);
|
||||||
|
return out;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Parse. If parse fails, return MsgPack() and assign an error message to err.
|
||||||
|
static MsgPack parse(const std::string & in, std::string & err);
|
||||||
|
static MsgPack parse(const char * in, size_t len, std::string & err) {
|
||||||
|
if (in) {
|
||||||
|
return parse(std::string(in,in+len), err);
|
||||||
|
} else {
|
||||||
|
err = "null input";
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Parse multiple objects, concatenated or separated by whitespace
|
||||||
|
static std::vector<MsgPack> parse_multi(
|
||||||
|
const std::string & in,
|
||||||
|
std::string::size_type & parser_stop_pos,
|
||||||
|
std::string & err);
|
||||||
|
|
||||||
|
static inline std::vector<MsgPack> parse_multi(
|
||||||
|
const std::string & in,
|
||||||
|
std::string & err) {
|
||||||
|
std::string::size_type parser_stop_pos;
|
||||||
|
return parse_multi(in, parser_stop_pos, err);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool operator== (const MsgPack &rhs) const;
|
||||||
|
bool operator< (const MsgPack &rhs) const;
|
||||||
|
bool operator!= (const MsgPack &rhs) const { return !(*this == rhs); }
|
||||||
|
bool operator<= (const MsgPack &rhs) const { return !(rhs < *this); }
|
||||||
|
bool operator> (const MsgPack &rhs) const { return (rhs < *this); }
|
||||||
|
bool operator>= (const MsgPack &rhs) const { return !(*this < rhs); }
|
||||||
|
|
||||||
|
/* has_shape(types, err)
|
||||||
|
*
|
||||||
|
* Return true if this is a JSON object and, for each item in types, has a field of
|
||||||
|
* the given type. If not, return false and set err to a descriptive message.
|
||||||
|
*/
|
||||||
|
typedef std::initializer_list<std::pair<std::string, Type>> shape;
|
||||||
|
bool has_shape(const shape & types, std::string & err) const;
|
||||||
|
|
||||||
|
private:
|
||||||
|
std::shared_ptr<MsgPackValue> m_ptr;
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace msgpack11
|
14
test/test_runner.cpp
Normal file
14
test/test_runner.cpp
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
/*
|
||||||
|
* test_runner.cpp
|
||||||
|
* Author: Benjamin Sergeant
|
||||||
|
* Copyright (c) 2018 Machine Zone. All rights reserved.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#define CATCH_CONFIG_RUNNER
|
||||||
|
#include "catch.hpp"
|
||||||
|
|
||||||
|
int main(int argc, char* argv[])
|
||||||
|
{
|
||||||
|
int result = Catch::Session().run(argc, argv);
|
||||||
|
return result;
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user