sanitizer cmake stuff

This commit is contained in:
Benjamin Sergeant 2019-01-06 18:54:16 -08:00
parent bf3e8195f7
commit 76f196206b
5 changed files with 155 additions and 1 deletions

3
.gitmodules vendored Normal file
View File

@ -0,0 +1,3 @@
[submodule "third_party/sanitizers-cmake"]
path = third_party/sanitizers-cmake
url = git://github.com/arsenm/sanitizers-cmake.git

View File

@ -3,6 +3,9 @@
# Copyright (c) 2018 Machine Zone, Inc. All rights reserved. # Copyright (c) 2018 Machine Zone, Inc. All rights reserved.
# #
set(CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/../third_party/sanitizers-cmake/cmake" ${CMAKE_MODULE_PATH})
find_package(Sanitizers)
cmake_minimum_required (VERSION 3.4.1) cmake_minimum_required (VERSION 3.4.1)
project (ixwebsocket_unittest) project (ixwebsocket_unittest)
@ -38,6 +41,7 @@ if (NOT WIN32)
endif() endif()
add_executable(ixwebsocket_unittest ${SOURCES}) add_executable(ixwebsocket_unittest ${SOURCES})
add_sanitizers(ixwebsocket_unittest)
if (APPLE AND USE_TLS) if (APPLE AND USE_TLS)
target_link_libraries(ixwebsocket_unittest "-framework foundation" "-framework security") target_link_libraries(ixwebsocket_unittest "-framework foundation" "-framework security")

View File

@ -0,0 +1,133 @@
/*
* IXWebSocketTestConnectionDisconnection.cpp
* Author: Benjamin Sergeant
* Copyright (c) 2017 Machine Zone. All rights reserved.
*/
#include <iostream>
#include <sstream>
#include <set>
#include <ixwebsocket/IXWebSocket.h>
#include "IXTest.h"
#include "catch.hpp"
using namespace ix;
namespace
{
const std::string WEBSOCKET_DOT_ORG_URL("wss://echo.websocket.org");
const std::string GOOGLE_URL("wss://google.com");
const std::string UNKNOWN_URL("wss://asdcasdcaasdcasdcasdcasdcasdcasdcasassdd.com");
void log(const std::string& msg)
{
std::cout << msg << std::endl;
}
}
namespace
{
class IXWebSocketTestConnectionDisconnection
{
public:
IXWebSocketTestConnectionDisconnection();
void start(const std::string& url);
void stop();
private:
ix::WebSocket _webSocket;
};
IXWebSocketTestConnectionDisconnection::IXWebSocketTestConnectionDisconnection()
{
;
}
void IXWebSocketTestConnectionDisconnection::stop()
{
_webSocket.stop();
}
void IXWebSocketTestConnectionDisconnection::start(const std::string& url)
{
_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::WebSocketOpenInfo& openInfo,
const ix::WebSocketCloseInfo& closeInfo)
{
std::stringstream ss;
if (messageType == ix::WebSocket_MessageType_Open)
{
log("cmd_websocket_satori_chat: connected !");
}
else if (messageType == ix::WebSocket_MessageType_Close)
{
log("cmd_websocket_satori_chat: disconnected !");
}
else if (messageType == ix::WebSocket_MessageType_Error)
{
log("cmd_websocket_satori_chat: Error!");
}
else if (messageType == ix::WebSocket_MessageType_Message)
{
log("cmd_websocket_satori_chat: received message.!");
}
else if (messageType == ix::WebSocket_MessageType_Ping)
{
log("cmd_websocket_satori_chat: received ping message.!");
}
else if (messageType == ix::WebSocket_MessageType_Pong)
{
log("cmd_websocket_satori_chat: received pong message.!");
}
else
{
log("Invalid ix::WebSocketMessageType");
}
});
// Start the connection
_webSocket.start();
}
}
//
// We try to connect to different servers, and make sure there are no crashes.
// FIXME: We could do more checks (make sure that we were not able to connect to unknown servers, etc...)
//
TEST_CASE("websocket_connections", "[websocket]")
{
SECTION("Try to connect to invalid servers.")
{
IXWebSocketTestConnectionDisconnection chatA;
chatA.start(GOOGLE_URL);
ix::msleep(1000);
chatA.stop();
chatA.start(UNKNOWN_URL);
ix::msleep(1000);
chatA.stop();
}
SECTION("Try to connect and disconnect with different timing.")
{
IXWebSocketTestConnectionDisconnection chatA;
for (int i = 0; i < 50; ++i)
{
log(std::string("Run: ") + std::to_string(i));
chatA.start(WEBSOCKET_DOT_ORG_URL);
ix::msleep(i);
chatA.stop();
}
}
}

View File

@ -22,7 +22,20 @@ else:
make = 'make' make = 'make'
testBinary ='./ixwebsocket_unittest' testBinary ='./ixwebsocket_unittest'
os.system('cmake -DCMAKE_BUILD_TYPE=Debug {} ..'.format(generator)) sanitizersFlags = {
'asan': '-DSANITIZE_ADDRESS=On',
'ubsan': '-DSANITIZE_UNDEFINED=On',
'tsan': '-DSANITIZE_THREAD=On',
'none': ''
}
sanitizer = 'tsan'
sanitizerFlags = sanitizersFlags[sanitizer]
cmakeCmd = 'cmake -DCMAKE_BUILD_TYPE=Debug {} {} ..'.format(generator, sanitizerFlags)
print(cmakeCmd)
ret = os.system(cmakeCmd)
assert ret == 0, 'CMake failed, exiting'
ret = os.system(make) ret = os.system(make)
assert ret == 0, 'Make failed, exiting' assert ret == 0, 'Make failed, exiting'

1
third_party/sanitizers-cmake vendored Submodule

@ -0,0 +1 @@
Subproject commit 99e159ec9bc8dd362b08d18436bd40ff0648417b