diff --git a/src/communication/v2/session.hpp b/src/communication/v2/session.hpp index 0add8244d..62bc4346f 100644 --- a/src/communication/v2/session.hpp +++ b/src/communication/v2/session.hpp @@ -246,11 +246,7 @@ class Session final : public std::enable_shared_from_this +#include +#include + +#include +#include +#include +#include +#include + +#include "utils/logging.hpp" + +inline void OnTimeoutExpiration(const boost::system::error_code &ec) { + // Timer was not cancelled, take necessary action. + MG_ASSERT(!!ec, "Connection timeout"); +} + +inline void EstablishConnection(const uint16_t bolt_port, const bool use_ssl) { + spdlog::info("Testing successfull connection from one client"); + mg::Client::Init(); + + boost::asio::io_context ioc; + boost::asio::steady_timer timer(ioc, std::chrono::seconds(5)); + timer.async_wait(std::bind_front(&OnTimeoutExpiration)); + std::jthread bg_thread([&ioc]() { ioc.run(); }); + + auto client = mg::Client::Connect({.host = "127.0.0.1", .port = bolt_port, .use_ssl = use_ssl}); + MG_ASSERT(client, "Failed to connect!"); + timer.cancel(); +} + +inline void EstablishMultipleConnections(const uint16_t bolt_port, const bool use_ssl) { + spdlog::info("Testing successfull connection from multiple clients"); + mg::Client::Init(); + + boost::asio::io_context ioc; + boost::asio::steady_timer timer(ioc, std::chrono::seconds(5)); + timer.async_wait(std::bind_front(&OnTimeoutExpiration)); + std::jthread bg_thread([&ioc]() { ioc.run(); }); + + auto client1 = mg::Client::Connect({.host = "127.0.0.1", .port = bolt_port, .use_ssl = use_ssl}); + auto client2 = mg::Client::Connect({.host = "127.0.0.1", .port = bolt_port, .use_ssl = use_ssl}); + auto client3 = mg::Client::Connect({.host = "127.0.0.1", .port = bolt_port, .use_ssl = use_ssl}); + + MG_ASSERT(client1, "Failed to connect!"); + MG_ASSERT(client2, "Failed to connect!"); + MG_ASSERT(client3, "Failed to connect!"); + timer.cancel(); +} diff --git a/tests/e2e/server/server_connection.cpp b/tests/e2e/server/server_connection.cpp new file mode 100644 index 000000000..7f3f31c4d --- /dev/null +++ b/tests/e2e/server/server_connection.cpp @@ -0,0 +1,56 @@ +// Copyright 2022 Memgraph Ltd. +// +// Use of this software is governed by the Business Source License +// included in the file licenses/BSL.txt; by using this file, you agree to be bound by the terms of the Business Source +// License, and you may not use this file except in compliance with the Business Source License. +// +// As of the Change Date specified in that file, in accordance with +// the Business Source License, use of this software will be governed +// by the Apache License, Version 2.0, included in the file +// licenses/APL.txt. + +#include +#include +#include + +#include +#include +#include +#include +#include +#include + +#include "common.hpp" +#include "utils/logging.hpp" + +DEFINE_uint64(bolt_port, 7687, "Bolt port"); + +void EstablishSSLConnectionToNonSSLServer(const auto bolt_port) { + spdlog::info("Testing that connection fails when connecting to non SSL server while using SSL"); + mg::Client::Init(); + + boost::asio::io_context ioc; + boost::asio::steady_timer timer(ioc, std::chrono::seconds(5)); + timer.async_wait(std::bind_front(&OnTimeoutExpiration)); + std::jthread bg_thread([&ioc]() { ioc.run(); }); + + auto client = mg::Client::Connect({.host = "127.0.0.1", .port = bolt_port, .use_ssl = true}); + + MG_ASSERT(client == nullptr, "Connection not refused when connecting with SSL turned on to a non SSL server!"); + timer.cancel(); +} + +int main(int argc, char **argv) { + google::SetUsageMessage("Memgraph E2E server connection!"); + gflags::ParseCommandLineFlags(&argc, &argv, true); + MG_ASSERT(FLAGS_bolt_port != 0); + memgraph::logging::RedirectToStderr(); + + const auto bolt_port = static_cast(FLAGS_bolt_port); + + EstablishConnection(bolt_port, false); + EstablishMultipleConnections(bolt_port, false); + EstablishSSLConnectionToNonSSLServer(bolt_port); + + return 0; +} diff --git a/tests/e2e/server/server_ssl_connection.cpp b/tests/e2e/server/server_ssl_connection.cpp new file mode 100644 index 000000000..5368b5e65 --- /dev/null +++ b/tests/e2e/server/server_ssl_connection.cpp @@ -0,0 +1,57 @@ +// Copyright 2022 Memgraph Ltd. +// +// Use of this software is governed by the Business Source License +// included in the file licenses/BSL.txt; by using this file, you agree to be bound by the terms of the Business Source +// License, and you may not use this file except in compliance with the Business Source License. +// +// As of the Change Date specified in that file, in accordance with +// the Business Source License, use of this software will be governed +// by the Apache License, Version 2.0, included in the file +// licenses/APL.txt. + +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include + +#include "common.hpp" +#include "utils/logging.hpp" + +DEFINE_uint64(bolt_port, 7687, "Bolt port"); + +void EstablishNonSSLConnectionToSSLServer(const auto bolt_port) { + spdlog::info("Testing that connection fails when connecting to SSL server without using SSL"); + mg::Client::Init(); + + boost::asio::io_context ioc; + boost::asio::steady_timer timer(ioc, std::chrono::seconds(5)); + timer.async_wait(std::bind_front(&OnTimeoutExpiration)); + std::jthread bg_thread([&ioc]() { ioc.run(); }); + + auto client = mg::Client::Connect({.host = "127.0.0.1", .port = bolt_port, .use_ssl = false}); + + MG_ASSERT(client == nullptr, "Connection not refused when conneting without SSL turned on to a SSL server!"); + timer.cancel(); +} + +int main(int argc, char **argv) { + google::SetUsageMessage("Memgraph E2E server SSL connection!"); + gflags::ParseCommandLineFlags(&argc, &argv, true); + MG_ASSERT(FLAGS_bolt_port != 0); + memgraph::logging::RedirectToStderr(); + + const auto bolt_port = static_cast(FLAGS_bolt_port); + + EstablishConnection(bolt_port, true); + EstablishMultipleConnections(bolt_port, true); + EstablishNonSSLConnectionToSSLServer(bolt_port); + + return 0; +} diff --git a/tests/e2e/server/workloads.yaml b/tests/e2e/server/workloads.yaml new file mode 100644 index 000000000..db956e701 --- /dev/null +++ b/tests/e2e/server/workloads.yaml @@ -0,0 +1,34 @@ +cert_file: &cert_file "$PROJECT_DIR/tests/e2e/memgraph-selfsigned.crt" +key_file: &key_file "$PROJECT_DIR/tests/e2e/memgraph-selfsigned.key" +bolt_port: &bolt_port "7687" +template_cluster: &template_cluster + cluster: + server: + args: ["--bolt-port=7687", "--log-level=TRACE", "--"] + log_file: "server-connection-e2e.log" +template_cluster_ssl: &template_cluster_ssl + cluster: + server: + args: + [ + "--bolt-port", + *bolt_port, + "--log-level=TRACE", + "--bolt-cert-file", + *cert_file, + "--bolt-key-file", + *key_file, + "--", + ] + log_file: "server-connection-ssl-e2e.log" + ssl: true + +workloads: + - name: "Server connection" + binary: "tests/e2e/server/memgraph__e2e__server_connection" + args: ["--bolt-port", *bolt_port] + <<: *template_cluster + - name: "Server SSL connection" + binary: "tests/e2e/server/memgraph__e2e__server_ssl_connection" + args: ["--bolt-port", *bolt_port] + <<: *template_cluster_ssl diff --git a/tests/e2e/websocket/CMakeLists.txt b/tests/e2e/websocket/CMakeLists.txt deleted file mode 100644 index 15c3a0f3a..000000000 --- a/tests/e2e/websocket/CMakeLists.txt +++ /dev/null @@ -1,10 +0,0 @@ -find_package(gflags REQUIRED) -find_package(Boost REQUIRED) - -add_executable(memgraph__e2e__websocket websocket.cpp) -target_link_libraries(memgraph__e2e__websocket mgclient mg-utils json gflags Boost::headers) - -add_executable(memgraph__e2e__websocket_ssl websocket_ssl.cpp) -target_link_libraries(memgraph__e2e__websocket_ssl mgclient mg-utils json gflags Boost::headers) -file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/memgraph-selfsigned.crt DESTINATION ${CMAKE_CURRENT_BINARY_DIR}) -file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/memgraph-selfsigned.key DESTINATION ${CMAKE_CURRENT_BINARY_DIR})