From ba6bcb97ae632272370a789ae769aa3e6c2c0a44 Mon Sep 17 00:00:00 2001 From: Paul Easter Date: Mon, 31 Jul 2023 21:19:24 +1000 Subject: [PATCH] IP and port now added to client args, test build moved to main Cmake file --- .github/workflows/ci.yml | 12 ++---------- CMakeLists.txt | 13 ++++++++++--- include/client.hpp | 6 ++++-- readme.md | 8 +++----- src/client.cpp | 15 +++++++++------ src/client_main.cpp | 4 ++-- src/server.cpp | 2 ++ tests/CMakeLists.txt | 13 ++++++++----- tests/tests_main.cpp | 4 ++-- 9 files changed, 42 insertions(+), 35 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 27e5b18..1f8a157 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -16,21 +16,13 @@ jobs: - name: Checkout submodules run: git submodule update --init --recursive - - name: Build application + - name: Build application and tests run: | mkdir build cd build cmake .. make - - name: Build tests - run: | - cd tests - mkdir build - cd build - cmake .. - make - - name: Run tests env: EB_CLIENT_CERTIFICATE_DATA: ${{ secrets.EB_CLIENT_CERTIFICATE_DATA }} @@ -38,7 +30,7 @@ jobs: EB_SERVER_CERTIFICATE_DATA: ${{ secrets.EB_SERVER_CERTIFICATE_DATA }} EB_SERVER_PRIVATE_KEY_DATA: ${{ secrets.EB_SERVER_PRIVATE_KEY_DATA }} run: | - cd tests/build + cd build/tests ./tests - name: Generate Doxygen Documentation diff --git a/CMakeLists.txt b/CMakeLists.txt index 44babcf..50d1d2e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,3 +1,5 @@ +# ./CMakelists.txt + cmake_minimum_required(VERSION 3.8) project(MyProject VERSION 1.0) @@ -7,17 +9,22 @@ set(CMAKE_CXX_STANDARD_REQUIRED True) # To avoid repeating these options for each target add_compile_options(-ftrack-macro-expansion=0) +find_package(Boost REQUIRED) +include_directories(${Boost_INCLUDE_DIRS}) + # include directories -include_directories(${CMAKE_SOURCE_DIR}/include /usr/local/include/boost) +include_directories(${CMAKE_SOURCE_DIR}/include) # link directories link_directories(/usr/local/lib) # Specify the executable targets and their dependencies -add_executable(client src/client_main.cpp src/client.cpp) -add_executable(server src/server_main.cpp src/server.cpp) +add_executable(client ${CMAKE_SOURCE_DIR}/src/client_main.cpp src/client.cpp) +add_executable(server ${CMAKE_SOURCE_DIR}/src/server_main.cpp src/server.cpp) # Link the necessary libraries target_link_libraries(client boost_system ssl crypto) target_link_libraries(server boost_system ssl crypto) +# include the tests directory +add_subdirectory(tests) \ No newline at end of file diff --git a/include/client.hpp b/include/client.hpp index 4e4e33c..0ed579a 100644 --- a/include/client.hpp +++ b/include/client.hpp @@ -12,8 +12,8 @@ using tcp = asio::ip::tcp; class Client { public: - Client(const std::string &certificateEnvVar, const std::string &privateKeyEnvVar); - void connect(const std::string &serverIP, unsigned short port); + Client(const std::string &certificateEnvVar, const std::string &privateKeyEnvVar, const std::string serverIP, const unsigned short port); + void connect(); void send(const std::string &message); void disconnect(); @@ -30,6 +30,8 @@ class Client boost::system::error_code certificateError; boost::system::error_code privateKeyError; boost::system::error_code handshakeError; + std::string serverIP_; + unsigned short port_; }; diff --git a/readme.md b/readme.md index bd36948..082d555 100644 --- a/readme.md +++ b/readme.md @@ -24,13 +24,11 @@ make ## Test instructions +The above build instructions also build the tests. To run the tests do this following: + ```bash -cd tests -mkdir build -cd build -cmake .. -make +cd build/tests/ # Then you can run the tests: ./tests ``` diff --git a/src/client.cpp b/src/client.cpp index 89dafb8..fdd38f0 100644 --- a/src/client.cpp +++ b/src/client.cpp @@ -3,17 +3,22 @@ /** - * \brief Constructs a new Client. + * \brief Constructs a new Server. * * \param certificateEnvVar The environment variable containing the certificate. * \param privateKeyEnvVar The environment variable containing the private key. + * \param serverIP The IP address for the Client to connect to. + * \param port The The port for the Client to connect to. */ -Client::Client(const std::string &certificateEnvVar, const std::string &privateKeyEnvVar) +Client::Client(const std::string &certificateEnvVar, const std::string &privateKeyEnvVar, const std::string serverIP, const unsigned short port) + : io_context_(), ctx_(ssl::context::tlsv13_client), socket_(io_context_, ctx_), connected_(false) { + serverIP_ = serverIP; + port_ = port; const char *certificateData = std::getenv(certificateEnvVar.c_str()); const char *privateKeyData = std::getenv(privateKeyEnvVar.c_str()); @@ -51,10 +56,8 @@ Client::Client(const std::string &certificateEnvVar, const std::string &privateK /** * \brief Connects to the server. * - * \param serverIP The IP address of the server. - * \param port The port to connect to. */ -void Client::connect(const std::string &serverIP, unsigned short port) +void Client::connect() { if (connected_) { @@ -63,7 +66,7 @@ void Client::connect(const std::string &serverIP, unsigned short port) } tcp::resolver resolver(io_context_); - tcp::resolver::query query(serverIP, std::to_string(port)); + tcp::resolver::query query(serverIP_, std::to_string(port_)); tcp::resolver::iterator endpoint_iterator = resolver.resolve(query); asio::async_connect( diff --git a/src/client_main.cpp b/src/client_main.cpp index 703a76f..b186bf6 100644 --- a/src/client_main.cpp +++ b/src/client_main.cpp @@ -6,8 +6,8 @@ int main() { std::string privateKeyEnvVar = "EB_CLIENT_PRIVATE_KEY_DATA"; try { - Client client(certificateEnvVar, privateKeyEnvVar); - client.connect("127.0.0.1", 4321); + Client client(certificateEnvVar, privateKeyEnvVar, "127.0.0.1", 4321); + client.connect(); client.send(message); client.disconnect(); } catch (const std::exception& e) { diff --git a/src/server.cpp b/src/server.cpp index 4bad421..e7e4295 100644 --- a/src/server.cpp +++ b/src/server.cpp @@ -6,6 +6,8 @@ * * \param certificateEnvVar The environment variable containing the certificate. * \param privateKeyEnvVar The environment variable containing the private key. + * \param serverIP The IP address for the Client to connect to. + * \param port The The port for the Client to connect to. */ Server::Server(const std::string &certificateEnvVar, const std::string &privateKeyEnvVar, const std::string serverIP, const unsigned short port) : io_context(), diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index aa63814..a7783bb 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -8,20 +8,23 @@ set(CMAKE_CXX_STANDARD_REQUIRED True) # To avoid repeating these options for each target add_compile_options(-ftrack-macro-expansion=0) -# add the Google Test directory -add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/../googletest googletest) +find_package(Boost REQUIRED) +include_directories(${Boost_INCLUDE_DIRS}) +# add the Google Test directory +add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/../googletest ${CMAKE_CURRENT_SOURCE_DIR}/../googletest) -# ${CMAKE_SOURCE_DIR} is ./tests, source in ./src, include in ./include +# ${CMAKE_CURRENT_SOURCE_DIR} is ./tests, source in ./src, include in ./include # include directories -include_directories(${CMAKE_SOURCE_DIR}/../include /usr/local/include/boost) +include_directories(${CMAKE_CURRENT_SOURCE_DIR}/../include) + # link directories link_directories(/usr/local/lib) # Specify the executable target for tests -add_executable(tests tests_main.cpp ${CMAKE_SOURCE_DIR}/../src/client.cpp ${CMAKE_SOURCE_DIR}/../src/server.cpp) +add_executable(tests tests_main.cpp ${CMAKE_CURRENT_SOURCE_DIR}/../src/client.cpp ${CMAKE_CURRENT_SOURCE_DIR}/../src/server.cpp) # Link the necessary libraries target_link_libraries(tests gtest gtest_main boost_system ssl crypto) diff --git a/tests/tests_main.cpp b/tests/tests_main.cpp index e134d17..73c48c5 100644 --- a/tests/tests_main.cpp +++ b/tests/tests_main.cpp @@ -27,8 +27,8 @@ void client_thread_function() { } try { - Client client(certificateEnvVar, privateKeyEnvVar); - client.connect("127.0.0.1", 4321); + Client client(certificateEnvVar, privateKeyEnvVar, "127.0.0.1", 4321); + client.connect(); client.send(client_message); client.disconnect(); } catch (const std::exception& e) {