Added basic_tls_request_with_servefile unit test to try and reproduce #549 and #566...

parent 1c9d71ad
...@@ -26,6 +26,16 @@ struct HelloHandler : public Http::Handler { ...@@ -26,6 +26,16 @@ struct HelloHandler : public Http::Handler {
} }
}; };
struct ServeFileHandler : public Http::Handler {
HTTP_PROTOTYPE(ServeFileHandler)
void onRequest(const Http::Request&, Http::ResponseWriter writer) override {
Http::serveFile(writer, "./certs/rootCA.crt").then([](ssize_t bytes) {
std::cout << "Sent " << bytes << " bytes" << std::endl;
}, Async::NoExcept);
}
};
TEST(http_client_test, basic_tls_request) { TEST(http_client_test, basic_tls_request) {
Http::Endpoint server(ADDRESS "1"); Http::Endpoint server(ADDRESS "1");
auto flags = Tcp::Options::ReuseAddr; auto flags = Tcp::Options::ReuseAddr;
...@@ -63,11 +73,49 @@ TEST(http_client_test, basic_tls_request) { ...@@ -63,11 +73,49 @@ TEST(http_client_test, basic_tls_request) {
server.shutdown(); server.shutdown();
} }
TEST(http_client_test, basic_tls_request_with_auth) { TEST(http_client_test, basic_tls_request_with_servefile) {
Http::Endpoint server(ADDRESS "2"); Http::Endpoint server(ADDRESS "2");
auto flags = Tcp::Options::ReuseAddr; auto flags = Tcp::Options::ReuseAddr;
auto server_opts = Http::Endpoint::options().flags(flags); auto server_opts = Http::Endpoint::options().flags(flags);
server.init(server_opts);
server.setHandler(Http::make_handler<ServeFileHandler>());
server.useSSL("./certs/server.crt", "./certs/server.key");
server.serveThreaded();
CURL *curl;
CURLcode res;
std::string buffer;
curl_global_init(CURL_GLOBAL_DEFAULT);
curl = curl_easy_init();
ASSERT_NE(curl, nullptr);
curl_easy_setopt(curl, CURLOPT_URL, "https://" ADDRESS "2");
curl_easy_setopt(curl, CURLOPT_CAINFO, "./certs/rootCA.crt");
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 1);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &write_cb);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &buffer);
/* Skip hostname check */
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
res = curl_easy_perform(curl);
ASSERT_EQ(res, CURLE_OK);
std::cout << buffer << std::endl;
ASSERT_EQ(buffer.rfind("-----BEGIN CERTIFICATE-----", 0), 0);
curl_easy_cleanup(curl);
curl_global_cleanup();
server.shutdown();
}
TEST(http_client_test, basic_tls_request_with_auth) {
Http::Endpoint server(ADDRESS "3");
auto flags = Tcp::Options::ReuseAddr;
auto server_opts = Http::Endpoint::options().flags(flags);
server.init(server_opts); server.init(server_opts);
server.setHandler(Http::make_handler<HelloHandler>()); server.setHandler(Http::make_handler<HelloHandler>());
server.useSSL("./certs/server.crt", "./certs/server.key"); server.useSSL("./certs/server.crt", "./certs/server.key");
...@@ -82,7 +130,7 @@ TEST(http_client_test, basic_tls_request_with_auth) { ...@@ -82,7 +130,7 @@ TEST(http_client_test, basic_tls_request_with_auth) {
curl = curl_easy_init(); curl = curl_easy_init();
ASSERT_NE(curl, nullptr); ASSERT_NE(curl, nullptr);
curl_easy_setopt(curl, CURLOPT_URL, "https://" ADDRESS "2"); curl_easy_setopt(curl, CURLOPT_URL, "https://" ADDRESS "3");
curl_easy_setopt(curl, CURLOPT_SSLCERT, "./certs/client.crt"); curl_easy_setopt(curl, CURLOPT_SSLCERT, "./certs/client.crt");
curl_easy_setopt(curl, CURLOPT_SSLKEY, "./certs/client.key"); curl_easy_setopt(curl, CURLOPT_SSLKEY, "./certs/client.key");
curl_easy_setopt(curl, CURLOPT_CAINFO, "./certs/rootCA.crt"); curl_easy_setopt(curl, CURLOPT_CAINFO, "./certs/rootCA.crt");
...@@ -105,7 +153,7 @@ TEST(http_client_test, basic_tls_request_with_auth) { ...@@ -105,7 +153,7 @@ TEST(http_client_test, basic_tls_request_with_auth) {
} }
TEST(http_client_test, basic_tls_request_with_auth_no_client_cert) { TEST(http_client_test, basic_tls_request_with_auth_no_client_cert) {
Http::Endpoint server(ADDRESS "3"); Http::Endpoint server(ADDRESS "4");
auto flags = Tcp::Options::ReuseAddr; auto flags = Tcp::Options::ReuseAddr;
auto server_opts = Http::Endpoint::options().flags(flags); auto server_opts = Http::Endpoint::options().flags(flags);
...@@ -123,7 +171,7 @@ TEST(http_client_test, basic_tls_request_with_auth_no_client_cert) { ...@@ -123,7 +171,7 @@ TEST(http_client_test, basic_tls_request_with_auth_no_client_cert) {
curl = curl_easy_init(); curl = curl_easy_init();
ASSERT_NE(curl, nullptr); ASSERT_NE(curl, nullptr);
curl_easy_setopt(curl, CURLOPT_URL, "https://" ADDRESS "3"); curl_easy_setopt(curl, CURLOPT_URL, "https://" ADDRESS "4");
curl_easy_setopt(curl, CURLOPT_CAINFO, "./certs/rootCA.crt"); curl_easy_setopt(curl, CURLOPT_CAINFO, "./certs/rootCA.crt");
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 1); curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 1);
...@@ -143,7 +191,7 @@ TEST(http_client_test, basic_tls_request_with_auth_no_client_cert) { ...@@ -143,7 +191,7 @@ TEST(http_client_test, basic_tls_request_with_auth_no_client_cert) {
} }
TEST(http_client_test, basic_tls_request_with_auth_client_cert_not_signed) { TEST(http_client_test, basic_tls_request_with_auth_client_cert_not_signed) {
Http::Endpoint server(ADDRESS "4"); Http::Endpoint server(ADDRESS "5");
auto flags = Tcp::Options::ReuseAddr; auto flags = Tcp::Options::ReuseAddr;
auto server_opts = Http::Endpoint::options().flags(flags); auto server_opts = Http::Endpoint::options().flags(flags);
...@@ -161,7 +209,7 @@ TEST(http_client_test, basic_tls_request_with_auth_client_cert_not_signed) { ...@@ -161,7 +209,7 @@ TEST(http_client_test, basic_tls_request_with_auth_client_cert_not_signed) {
curl = curl_easy_init(); curl = curl_easy_init();
ASSERT_NE(curl, nullptr); ASSERT_NE(curl, nullptr);
curl_easy_setopt(curl, CURLOPT_URL, "https://" ADDRESS "4"); curl_easy_setopt(curl, CURLOPT_URL, "https://" ADDRESS "5");
curl_easy_setopt(curl, CURLOPT_SSLCERT, "./certs/client_not_signed.crt"); curl_easy_setopt(curl, CURLOPT_SSLCERT, "./certs/client_not_signed.crt");
curl_easy_setopt(curl, CURLOPT_SSLKEY, "./certs/client_not_signed.key"); curl_easy_setopt(curl, CURLOPT_SSLKEY, "./certs/client_not_signed.key");
curl_easy_setopt(curl, CURLOPT_CAINFO, "./certs/rootCA.crt"); curl_easy_setopt(curl, CURLOPT_CAINFO, "./certs/rootCA.crt");
...@@ -194,7 +242,7 @@ static int verify_callback(int verify, void *ctx) ...@@ -194,7 +242,7 @@ static int verify_callback(int verify, void *ctx)
TEST(http_client_test, basic_tls_request_with_auth_with_cb) { TEST(http_client_test, basic_tls_request_with_auth_with_cb) {
Http::Endpoint server(ADDRESS "5"); Http::Endpoint server(ADDRESS "6");
auto flags = Tcp::Options::ReuseAddr; auto flags = Tcp::Options::ReuseAddr;
auto server_opts = Http::Endpoint::options().flags(flags); auto server_opts = Http::Endpoint::options().flags(flags);
...@@ -212,7 +260,7 @@ TEST(http_client_test, basic_tls_request_with_auth_with_cb) { ...@@ -212,7 +260,7 @@ TEST(http_client_test, basic_tls_request_with_auth_with_cb) {
curl = curl_easy_init(); curl = curl_easy_init();
ASSERT_NE(curl, nullptr); ASSERT_NE(curl, nullptr);
curl_easy_setopt(curl, CURLOPT_URL, "https://" ADDRESS "5"); curl_easy_setopt(curl, CURLOPT_URL, "https://" ADDRESS "6");
curl_easy_setopt(curl, CURLOPT_SSLCERT, "./certs/client.crt"); curl_easy_setopt(curl, CURLOPT_SSLCERT, "./certs/client.crt");
curl_easy_setopt(curl, CURLOPT_SSLKEY, "./certs/client.key"); curl_easy_setopt(curl, CURLOPT_SSLKEY, "./certs/client.key");
curl_easy_setopt(curl, CURLOPT_CAINFO, "./certs/rootCA.crt"); curl_easy_setopt(curl, CURLOPT_CAINFO, "./certs/rootCA.crt");
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment