Foundation / checks (deploy) (push) Successful in 5s
Foundation / checks (ecs) (push) Successful in 1m54s
Foundation / checks (client) (push) Failing after 4m23s
Foundation / checks (network) (push) Failing after 3m5s
Foundation / postgres-tools (push) Canceled after 0s
Foundation / server-image (push) Canceled after 0s
Foundation / image (push) Canceled after 0s
Foundation / checks (tsan) (push) Canceled after 11s
Foundation / checks (postgres) (push) Canceled after 57s
Foundation / checks (checks) (push) Canceled after 5m23s
80 lines
3.3 KiB
C++
80 lines
3.3 KiB
C++
#include "evo/network/session_protocol.hpp"
|
|
|
|
#include <cstdio>
|
|
#include <source_location>
|
|
#include <stdexcept>
|
|
#include <string>
|
|
|
|
namespace {
|
|
using namespace evo::network;
|
|
void check(bool value, std::source_location at = std::source_location::current()) {
|
|
if (!value)
|
|
throw std::runtime_error("cellular intent protocol line " + std::to_string(at.line()));
|
|
}
|
|
#define CHECK(...) check(static_cast<bool>(__VA_ARGS__))
|
|
|
|
void round_trip_and_version_gate() {
|
|
const CellularIntention targeted{17, 23, 29, 31, -707, 707, true, 4'000, 5'000};
|
|
const SessionMessage message{targeted};
|
|
const auto bytes = encode_session(message);
|
|
CHECK(!bytes.empty() && bytes.size() == session_header_bytes + 48);
|
|
std::uint16_t version{};
|
|
const auto decoded = decode_session(bytes, &version);
|
|
CHECK(decoded && version == session_protocol_v6);
|
|
CHECK(std::get<CellularIntention>(*decoded) == targeted);
|
|
CHECK(encode_session(*decoded) == bytes);
|
|
CHECK(encode_session(message, session_protocol_v5).empty());
|
|
|
|
const CellularIntention no_target{1, 2, 3, 4, 0, -1, false, 0, 0};
|
|
const auto no_target_bytes = encode_session(SessionMessage{no_target});
|
|
CHECK(std::get<CellularIntention>(*decode_session(no_target_bytes)) == no_target);
|
|
}
|
|
|
|
void malformed_frames_are_refused() {
|
|
const auto bytes =
|
|
encode_session(SessionMessage{CellularIntention{1, 2, 3, 4, 0, 0, true, 1'000, 2'000}});
|
|
for (std::size_t size = 0; size < bytes.size(); ++size) {
|
|
const auto truncated = decode_session(std::span{bytes}.first(size));
|
|
CHECK(!truncated);
|
|
CHECK(truncated.error() == (size < session_header_bytes ? SessionCodecError::truncated
|
|
: SessionCodecError::wrong_length));
|
|
}
|
|
auto bad = bytes;
|
|
bad.at(6) = 255; // Unknown message type.
|
|
const auto wrong_type = decode_session(bad);
|
|
CHECK(!wrong_type && wrong_type.error() == SessionCodecError::unknown_type);
|
|
|
|
bad = bytes;
|
|
bad.at(session_header_bytes + 36) = 2;
|
|
const auto bad_flag = decode_session(bad);
|
|
CHECK(!bad_flag && bad_flag.error() == SessionCodecError::invalid_value);
|
|
|
|
bad = bytes;
|
|
bad.at(session_header_bytes + 38) = 1;
|
|
const auto bad_reserved = decode_session(bad);
|
|
CHECK(!bad_reserved && bad_reserved.error() == SessionCodecError::invalid_value);
|
|
|
|
bad = encode_session(SessionMessage{CellularIntention{1, 2, 3, 4, 0, 0, false, 0, 0}});
|
|
bad.at(session_header_bytes + 40) = 1; // Coordinates must be zero when target is absent.
|
|
const auto noncanonical = decode_session(bad);
|
|
CHECK(!noncanonical && noncanonical.error() == SessionCodecError::invalid_value);
|
|
|
|
bad = bytes;
|
|
bad.at(4) = static_cast<std::uint8_t>(session_protocol_v5);
|
|
const auto old_version = decode_session(bad);
|
|
CHECK(!old_version && old_version.error() == SessionCodecError::unsupported_version);
|
|
CHECK(encode_session(SessionMessage{CellularIntention{1, 2, 3, 4}}, 7).empty());
|
|
}
|
|
} // namespace
|
|
|
|
int main() {
|
|
try {
|
|
round_trip_and_version_gate();
|
|
malformed_frames_are_refused();
|
|
std::puts("PASS cellular intention protocol: v6 round trip and strict framing");
|
|
} catch (const std::exception& error) {
|
|
std::fprintf(stderr, "%s\n", error.what());
|
|
return 1;
|
|
}
|
|
}
|