Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compile-time check for listen() having a tls credentials argument #45

Merged
merged 2 commits into from
Sep 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions include/quic/endpoint.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,11 @@ namespace oxen::quic
template <typename... Opt>
bool listen(Opt&&... opts)
{

static_assert(
(0 + ... + std::is_convertible_v<remove_cvref_t<Opt>, std::shared_ptr<TLSCreds>>) == 1,
"Endpoint listen requires exactly one std::shared_ptr<TLSCreds> argument");

std::promise<bool> p;
auto f = p.get_future();

Expand Down
6 changes: 6 additions & 0 deletions include/quic/utils.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#pragma once

#include <type_traits>

#include "format.hpp"

extern "C"
Expand Down Expand Up @@ -134,6 +136,10 @@ namespace oxen::quic
template <template <typename...> class Class, typename... Us>
inline constexpr bool is_instantiation<Class, Class<Us...>> = true;

// Backport of c++20 std::remove_cvref_t
template <typename T>
using remove_cvref_t = std::remove_cv_t<std::remove_reference_t<T>>;

// Application error code we close with if the stream data handle throws
inline constexpr uint64_t STREAM_ERROR_EXCEPTION = (1ULL << 62) - 2;
// Application error code we close with if the datagram data handle throws
Expand Down
37 changes: 11 additions & 26 deletions tests/001-handshake.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ namespace oxen::quic::test
auto ep_notls = test_net.endpoint(default_addr);
auto ep_tls = test_net.endpoint(local_addr);

REQUIRE_THROWS(ep_notls->listen());
// ep_notls->listen(); // Shouldn't compile if uncommented!
// ep_notls->listen(local_tls, local_tls); // Nor this
REQUIRE_NOTHROW(ep_tls->listen(local_tls));
};

Expand Down Expand Up @@ -152,33 +153,17 @@ namespace oxen::quic::test
opt::local_addr server_local{};
opt::local_addr client_local{};

SECTION("Unsuccessful TLS handshake - No server TLS credentials")
{
auto server_endpoint = test_net.endpoint(server_local, server_established);
REQUIRE_THROWS(server_endpoint->listen());

opt::remote_addr client_remote{"127.0.0.1"s, server_endpoint->local().port()};

auto client_endpoint = test_net.endpoint(client_local, client_established);
auto conn_interface = client_endpoint->connect(client_remote, client_tls);

REQUIRE_FALSE(client_established.is_ready());
};

SECTION("Successful TLS handshake")
{
auto server_endpoint = test_net.endpoint(server_local, server_established);
REQUIRE(server_endpoint->listen(server_tls));
auto server_endpoint = test_net.endpoint(server_local, server_established);
REQUIRE(server_endpoint->listen(server_tls));

opt::remote_addr client_remote{"127.0.0.1"s, server_endpoint->local().port()};
opt::remote_addr client_remote{"127.0.0.1"s, server_endpoint->local().port()};

auto client_endpoint = test_net.endpoint(client_local, client_established);
auto conn_interface = client_endpoint->connect(client_remote, client_tls);
auto client_endpoint = test_net.endpoint(client_local, client_established);
auto conn_interface = client_endpoint->connect(client_remote, client_tls);

REQUIRE(client_established.wait_ready());
REQUIRE(server_established.wait_ready());
REQUIRE(client_established.get());
REQUIRE(server_established.get());
};
REQUIRE(client_established.wait_ready());
REQUIRE(server_established.wait_ready());
REQUIRE(client_established.get());
REQUIRE(server_established.get());
};
} // namespace oxen::quic::test