Skip to content

Commit

Permalink
added initial ssl testing
Browse files Browse the repository at this point in the history
  • Loading branch information
The-EDev committed Apr 16, 2021
1 parent 301e535 commit 798f190
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 1 deletion.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ if (NOT MSVC AND BUILD_TESTING)
add_subdirectory(tests)
enable_testing()
add_test(NAME crow_test COMMAND ${CMAKE_CURRENT_BINARY_DIR}/tests/unittest)
add_test(NAME ssl_test COMMAND ${CMAKE_CURRENT_BINARY_DIR}/tests/ssl/ssltest)
add_test(NAME template_test COMMAND ${CMAKE_CURRENT_BINARY_DIR}/tests/template/test.py WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/tests/template)
endif()

Expand Down
1 change: 1 addition & 0 deletions include/crow/http_connection.h
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,7 @@ namespace crow
}
else
{
CROW_LOG_ERROR << "Could not start adaptor: " << ec.message();
check_destroy();
}
});
Expand Down
3 changes: 2 additions & 1 deletion tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ set(TEST_SRCS
)

add_executable(unittest ${TEST_SRCS})
target_link_libraries(unittest ${Boost_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT} z)
target_link_libraries(unittest ${Boost_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT} ${OPENSSL_LIBRARIES} z)
# set target compile options as defined in the cmake/compiler_options.cmake Module
target_compile_options(unittest PRIVATE ${compiler_options})

Expand All @@ -16,4 +16,5 @@ if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
endif()

add_subdirectory(template)
add_subdirectory(ssl)
add_subdirectory(img)
19 changes: 19 additions & 0 deletions tests/ssl/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
cmake_minimum_required(VERSION 3.15)
project (ssl_test)

set(PROJECT_INCLUDE_DIR
${PROJECT_SOURCE_DIR}/include
)

set(TEST_SRCS
ssltest.cpp
)

add_executable(ssltest ${TEST_SRCS})
target_link_libraries(ssltest ${Boost_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT} ${OPENSSL_LIBRARIES} z)
target_compile_options(ssltest PRIVATE "${compiler_options}")

if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
set_target_properties(ssltest PROPERTIES COMPILE_FLAGS "--coverage -fprofile-arcs -ftest-coverage")
target_link_libraries(ssltest gcov)
endif()
86 changes: 86 additions & 0 deletions tests/ssl/ssltest.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
#define CROW_ENABLE_SSL
#define CATCH_CONFIG_MAIN
#define CROW_LOG_LEVEL 0
#define CROW_MAIN

#include <thread>

#include "../catch.hpp"
#include "crow.h"

#define LOCALHOST_ADDRESS "127.0.0.1"

using namespace boost;

//TODO SSL test with .pem file
TEST_CASE("SSL")
{
static char buf[2048];
//static char buf2[2048];

std::system("openssl req -newkey rsa:4096 -x509 -sha256 -days 365 -nodes -out test.crt -keyout test.key -subj '/CN=127.0.0.1'");
//std::system("cat test.key > test.pem");
//std::system("cat test.crt >> test.pem");

crow::SimpleApp app;
//crow::SimpleApp app2;

CROW_ROUTE(app, "/")
([]() {
return "Hello world, I'm keycrt.";
});
/*
CROW_ROUTE(app2, "/")
([]() {
return "Hello world, I'm pem.";
});
*/
auto _ = async(std::launch::async, [&] { app.bindaddr(LOCALHOST_ADDRESS).port(45460).ssl_file("test.crt", "test.key").run(); });
//auto _1 = async(std::launch::async,[&] { app2.bindaddr(LOCALHOST_ADDRESS).port(45461).ssl_file("test.pem").run(); });

app.wait_for_server_start();
//app2.wait_for_server_start();

std::string sendmsg = "GET /\r\n\r\n";

asio::ssl::context ctx(asio::ssl::context::sslv23);

asio::io_service is;
{
std::cout << "started first one" << std::endl;

asio::ssl::stream<asio::ip::tcp::socket> c(is, ctx);
c.lowest_layer().connect(asio::ip::tcp::endpoint(
asio::ip::address::from_string(LOCALHOST_ADDRESS), 45460));

c.handshake(asio::ssl::stream_base::client);

c.write_some(asio::buffer(sendmsg));

size_t sz = c.read_some(asio::buffer(buf, 2048));

CHECK("Hello world, I'm keycrt." == std::string(buf).substr((sz - 24)));
}

/*
asio::io_service is2;
{
std::cout << "started second one" << std::endl;
asio::ssl::stream<asio::ip::tcp::socket> c(is2, ctx);
c.lowest_layer().connect(asio::ip::tcp::endpoint(
asio::ip::address::from_string(LOCALHOST_ADDRESS), 45461));
c.handshake(asio::ssl::stream_base::client);
c.write_some(asio::buffer(sendmsg));
size_t sz = c.read_some(asio::buffer(buf2, 2048));
CHECK("Hello world, I'm pem." == std::string(buf2).substr((sz - 21)));
}
*/
app.stop();
//app2.stop();

std::system("rm test.crt test.key" /*test.pem*/);
}

0 comments on commit 798f190

Please sign in to comment.