Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 2 additions & 10 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,29 +16,21 @@ 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 }}
EB_CLIENT_PRIVATE_KEY_DATA: ${{ secrets.EB_CLIENT_PRIVATE_KEY_DATA }}
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
Expand Down
13 changes: 10 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# ./CMakelists.txt

cmake_minimum_required(VERSION 3.8)
project(MyProject VERSION 1.0)

Expand All @@ -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)
6 changes: 4 additions & 2 deletions include/client.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -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_;
};


8 changes: 3 additions & 5 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```
Expand Down
15 changes: 9 additions & 6 deletions src/client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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());

Expand Down Expand Up @@ -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_)
{
Expand All @@ -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(
Expand Down
4 changes: 2 additions & 2 deletions src/client_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 2 additions & 0 deletions src/server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
13 changes: 8 additions & 5 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
4 changes: 2 additions & 2 deletions tests/tests_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down