oxen-storage-server/unit_test/onion_requests.cpp
Jason Rhinelander de7bf79854 Optimize/cleanup onion request parsing
- Do the json parsing as part of the payload parsing rather than
allocating a string and then making the caller do it (there's no case
where the caller *doesn't* want to do it).

- Modernize code to use structured bindings, allowing both cleaner code
and reduction in the number of moves/copies.
2021-04-18 14:50:40 -03:00

122 lines
3 KiB
C++

#include <boost/test/unit_test.hpp>
#include <iostream>
#include <ostream>
#include "onion_processing.h"
using namespace oxen;
BOOST_AUTO_TEST_SUITE(onion_requests)
constexpr const char* ciphertext = "ciphertext";
const auto prefix = "\x0a\0\0\0ciphertext"s;
// Provided "headers", so the request terminates
// at a service node.
BOOST_AUTO_TEST_CASE(final_destination) {
auto data = prefix + R"#({
"headers": "something"
})#";
auto res = process_inner_request(data);
auto expected = FinalDestinationInfo {
ciphertext
};
BOOST_REQUIRE(std::holds_alternative<FinalDestinationInfo>(res));
BOOST_CHECK_EQUAL(*std::get_if<FinalDestinationInfo>(&res), expected);
}
// Provided "host", so the request should go
// to an extrenal server. Default values will
// be used for port and protocol.
BOOST_AUTO_TEST_CASE(relay_to_server_legacy) {
auto data = prefix + R"#({
"host": "host",
"target": "target"
})#";
auto res = process_inner_request(data);
uint16_t port = 443;
std::string protocol = "https";
auto expected = RelayToServerInfo {
data,
"host",
port,
protocol,
"target"
};
BOOST_REQUIRE(std::holds_alternative<RelayToServerInfo>(res));
BOOST_CHECK_EQUAL(*std::get_if<RelayToServerInfo>(&res), expected);
}
// Provided "host", so the request should go
// to an extrenal server.
BOOST_AUTO_TEST_CASE(relay_to_server) {
auto data = prefix + R"#({
"host": "host",
"target": "target",
"port": 80,
"protocol": "http"
})#";
auto res = process_inner_request(data);
uint16_t port = 80;
std::string protocol = "http";
auto expected = RelayToServerInfo {
data,
"host",
port,
protocol,
"target"
};
BOOST_REQUIRE(std::holds_alternative<RelayToServerInfo>(res));
BOOST_CHECK_EQUAL(*std::get_if<RelayToServerInfo>(&res), expected);
}
/// No "host" or "headers", so we forward
/// the request to another node
BOOST_AUTO_TEST_CASE(relay_to_node) {
auto data = prefix + R"#({
"destination": "ffffeeeeddddccccbbbbaaaa9999888877776666555544443333222211110000",
"ephemeral_key": "ephemeral_key"
})#";
auto res = process_inner_request(data);
auto expected = RelayToNodeInfo {
ciphertext,
"ephemeral_key",
ed25519_pubkey::from_hex("ffffeeeeddddccccbbbbaaaa9999888877776666555544443333222211110000")
};
BOOST_REQUIRE(std::holds_alternative<RelayToNodeInfo>(res));
BOOST_CHECK_EQUAL(*std::get_if<RelayToNodeInfo>(&res), expected);
}
BOOST_AUTO_TEST_CASE(correctly_filters_urls) {
BOOST_CHECK(is_server_url_allowed("/loki/v3/lsrpc"));
BOOST_CHECK(is_server_url_allowed("/loki/oxen/v4/lsrpc"));
BOOST_CHECK(is_server_url_allowed("/oxen/v3/lsrpc"));
BOOST_CHECK(!is_server_url_allowed("/not_loki/v3/lsrpc"));
BOOST_CHECK(!is_server_url_allowed("/loki/v3"));
BOOST_CHECK(!is_server_url_allowed("/loki/v3/lsrpc?foo=bar"));
}
BOOST_AUTO_TEST_SUITE_END()