diff options
author | Brad King <brad.king@kitware.com> | 2017-09-01 14:34:07 (GMT) |
---|---|---|
committer | Kitware Robot <kwrobot@kitware.com> | 2017-09-01 14:34:16 (GMT) |
commit | 671cc7490ca1dd732cbb3671d4732eb1ee1e180e (patch) | |
tree | 24e8572c5f21df085808119cb94928807d5ac5db /Tests | |
parent | 900f75868ce41620947ab4ca376d77e12d32830f (diff) | |
parent | 0e4d503384a254c843a065c26f77bc19dfccff16 (diff) | |
download | CMake-671cc7490ca1dd732cbb3671d4732eb1ee1e180e.zip CMake-671cc7490ca1dd732cbb3671d4732eb1ee1e180e.tar.gz CMake-671cc7490ca1dd732cbb3671d4732eb1ee1e180e.tar.bz2 |
Merge topic 'server-refactor'
0e4d5033 server: Added thread check to protect writedata
124424e9 server: Protect several fields from potentially pointing to bad memory
693fa0a9 server: Added assert to monitor uv_run status
882dcef8 server: Made connections in a server have a mutex to avoid use after frees
7ef28843 server: Moved buffer formatting into bufferstrategy
dc7a18d8 server: test buffer parsing
f8fd5a97 server: Made stdio connection accept different types of streams
6afc7f88 server: Remove unused fields / functions
...
Acked-by: Kitware Robot <kwrobot@kitware.com>
Merge-request: !1111
Diffstat (limited to 'Tests')
-rw-r--r-- | Tests/CMakeLists.txt | 4 | ||||
-rw-r--r-- | Tests/CMakeServerLib/CMakeLists.txt | 17 | ||||
-rw-r--r-- | Tests/CMakeServerLib/testServerBuffering.cpp | 86 |
3 files changed, 107 insertions, 0 deletions
diff --git a/Tests/CMakeLists.txt b/Tests/CMakeLists.txt index f0e58ee..516bc89 100644 --- a/Tests/CMakeLists.txt +++ b/Tests/CMakeLists.txt @@ -146,6 +146,10 @@ if(BUILD_TESTING) if(NOT CMake_TEST_EXTERNAL_CMAKE) add_subdirectory(CMakeLib) + + if(CMake_TEST_SERVER_MODE) + add_subdirectory(CMakeServerLib) + endif() endif() add_subdirectory(CMakeOnly) add_subdirectory(RunCMake) diff --git a/Tests/CMakeServerLib/CMakeLists.txt b/Tests/CMakeServerLib/CMakeLists.txt new file mode 100644 index 0000000..f5351fd --- /dev/null +++ b/Tests/CMakeServerLib/CMakeLists.txt @@ -0,0 +1,17 @@ +include_directories( + ${CMAKE_CURRENT_BINARY_DIR} + ${CMake_BINARY_DIR}/Source + ${CMake_SOURCE_DIR}/Source + ) + +set(CMakeServerLib_TESTS + testServerBuffering + ) + +create_test_sourcelist(CMakeLib_TEST_SRCS CMakeServerLibTests.cxx ${CMakeServerLib_TESTS}) +add_executable(CMakeServerLibTests ${CMakeLib_TEST_SRCS}) +target_link_libraries(CMakeServerLibTests CMakeLib CMakeServerLib) + +foreach(test ${CMakeServerLib_TESTS}) + add_test(CMakeServerLib.${test} CMakeServerLibTests ${test} ${${test}_ARGS}) +endforeach() diff --git a/Tests/CMakeServerLib/testServerBuffering.cpp b/Tests/CMakeServerLib/testServerBuffering.cpp new file mode 100644 index 0000000..97be891 --- /dev/null +++ b/Tests/CMakeServerLib/testServerBuffering.cpp @@ -0,0 +1,86 @@ +#include "cmConnection.h" +#include "cmServerConnection.h" +#include <iostream> +#include <stddef.h> +#include <string> +#include <vector> + +void print_error(const std::vector<std::string>& input, + const std::vector<std::string>& output) +{ + std::cerr << "Responses don't equal input messages input." << std::endl; + std::cerr << "Responses: " << std::endl; + + for (auto& msg : output) { + std::cerr << "'" << msg << "'" << std::endl; + } + + std::cerr << "Input messages" << std::endl; + for (auto& msg : input) { + std::cerr << "'" << msg << "'" << std::endl; + } +} + +std::string trim_newline(const std::string& _buffer) +{ + auto buffer = _buffer; + while (!buffer.empty() && (buffer.back() == '\n' || buffer.back() == '\r')) { + buffer.pop_back(); + } + return buffer; +} + +int testServerBuffering(int, char** const) +{ + std::vector<std::string> messages = { + "{ \"test\": 10}", "{ \"test\": { \"test2\": false} }", + "{ \"test\": [1, 2, 3] }", + "{ \"a\": { \"1\": {}, \n\n\n \"2\":[] \t\t\t\t}}" + }; + + std::string fullMessage; + for (auto& msg : messages) { + fullMessage += "[== \"CMake Server\" ==[\n"; + fullMessage += msg; + fullMessage += "\n]== \"CMake Server\" ==]\n"; + } + + // The buffering strategy should cope with any fragmentation, including + // just getting the characters one at a time. + auto bufferingStrategy = + std::unique_ptr<cmConnectionBufferStrategy>(new cmServerBufferStrategy); + std::vector<std::string> response; + std::string rawBuffer; + for (size_t i = 0; i < fullMessage.size(); i++) { + rawBuffer += fullMessage[i]; + std::string packet = bufferingStrategy->BufferMessage(rawBuffer); + do { + if (!packet.empty() && packet != "\r\n") { + response.push_back(trim_newline(packet)); + } + packet = bufferingStrategy->BufferMessage(rawBuffer); + } while (!packet.empty()); + } + + if (response != messages) { + print_error(messages, response); + return 1; + } + + // We should also be able to deal with getting a bunch at once + response.clear(); + std::string packet = bufferingStrategy->BufferMessage(fullMessage); + do { + if (!packet.empty() && packet != "\r\n") { + response.push_back(trim_newline(packet)); + } + packet = bufferingStrategy->BufferMessage(fullMessage); + } while (!packet.empty()); + + if (response != messages) { + print_error(messages, response); + return 1; + } + + return 0; +} |