From 76f196206becf5907a024b57f5cfdec6eb05f8cf Mon Sep 17 00:00:00 2001 From: Benjamin Sergeant Date: Sun, 6 Jan 2019 18:54:16 -0800 Subject: [PATCH] sanitizer cmake stuff --- .gitmodules | 3 + test/CMakeLists.txt | 4 + ...IXWebSocketTestConnectionDisconnection.cpp | 133 ++++++++++++++++++ test/run.py | 15 +- third_party/sanitizers-cmake | 1 + 5 files changed, 155 insertions(+), 1 deletion(-) create mode 100644 .gitmodules create mode 100644 test/IXWebSocketTestConnectionDisconnection.cpp create mode 160000 third_party/sanitizers-cmake diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 00000000..d63c764c --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "third_party/sanitizers-cmake"] + path = third_party/sanitizers-cmake + url = git://github.com/arsenm/sanitizers-cmake.git diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 6fd41092..2d296440 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -3,6 +3,9 @@ # 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) project (ixwebsocket_unittest) @@ -38,6 +41,7 @@ if (NOT WIN32) endif() add_executable(ixwebsocket_unittest ${SOURCES}) +add_sanitizers(ixwebsocket_unittest) if (APPLE AND USE_TLS) target_link_libraries(ixwebsocket_unittest "-framework foundation" "-framework security") diff --git a/test/IXWebSocketTestConnectionDisconnection.cpp b/test/IXWebSocketTestConnectionDisconnection.cpp new file mode 100644 index 00000000..7bd5e09f --- /dev/null +++ b/test/IXWebSocketTestConnectionDisconnection.cpp @@ -0,0 +1,133 @@ +/* + * IXWebSocketTestConnectionDisconnection.cpp + * Author: Benjamin Sergeant + * Copyright (c) 2017 Machine Zone. All rights reserved. + */ + +#include +#include +#include +#include +#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(); + } + } +} diff --git a/test/run.py b/test/run.py index 5475ba12..ac06cac6 100644 --- a/test/run.py +++ b/test/run.py @@ -22,7 +22,20 @@ else: make = 'make' 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) assert ret == 0, 'Make failed, exiting' diff --git a/third_party/sanitizers-cmake b/third_party/sanitizers-cmake new file mode 160000 index 00000000..99e159ec --- /dev/null +++ b/third_party/sanitizers-cmake @@ -0,0 +1 @@ +Subproject commit 99e159ec9bc8dd362b08d18436bd40ff0648417b