test: Add tests for control message serialization/deserialization

This commit is contained in:
Joshua Scott 2018-04-16 19:02:03 +01:00
parent 0eb81803c7
commit d7f5196a36
1 changed files with 188 additions and 0 deletions

188
test/src/unit-protocol.cpp Normal file
View File

@ -0,0 +1,188 @@
#define CATCH_CONFIG_MAIN
#include <catch.hpp>
#include <fstream>
#include <ki/protocol/control/SessionOffer.h>
#include <ki/protocol/control/SessionAccept.h>
#include <ki/protocol/control/ClientKeepAlive.h>
#include <ki/protocol/control/ServerKeepAlive.h>
using namespace ki::protocol;
TEST_CASE("Control Message Serialization", "[control]")
{
std::ostringstream oss;
SECTION("SessionOffer")
{
control::SessionOffer offer(0xABCD, 0xAABBCCDD, 0xAABBCCDD);
offer.write_to(oss);
const char expected_bytes[] = {
// Session ID
0xCD, 0xAB,
// Unknown
0x00, 0x00, 0x00, 0x00,
// Timestamp
0xDD, 0xCC, 0xBB, 0xAA,
// Milliseconds
0xDD, 0xCC, 0xBB, 0xAA
};
REQUIRE(oss.str() == std::string(expected_bytes, sizeof(expected_bytes)));
}
SECTION("SessionAccept")
{
control::SessionAccept accept(0xABCD, 0xAABBCCDD, 0xAABBCCDD);
accept.write_to(oss);
const char expected_bytes[] = {
// Unknown
0x00, 0x00,
// Unknown
0x00, 0x00, 0x00, 0x00,
// Timestamp
0xDD, 0xCC, 0xBB, 0xAA,
// Milliseconds
0xDD, 0xCC, 0xBB, 0xAA,
// Session ID
0xCD, 0xAB
};
REQUIRE(oss.str() == std::string(expected_bytes, sizeof(expected_bytes)));
}
SECTION("ClientKeepAlive")
{
control::ClientKeepAlive keep_alive(0xABCD, 0xABCD, 0xABCD);
keep_alive.write_to(oss);
const char expected_bytes[] = {
// Session ID
0xCD, 0xAB,
// Milliseconds
0xCD, 0xAB,
// Minutes
0xCD, 0xAB
};
REQUIRE(oss.str() == std::string(expected_bytes, sizeof(expected_bytes)));
}
SECTION("ServerKeepAlive")
{
control::ServerKeepAlive keep_alive(0xAABBCCDD);
keep_alive.write_to(oss);
const char expected_bytes[] = {
// Unknown
0x00, 0x00,
// Timestamp
0xDD, 0xCC, 0xBB, 0xAA
};
REQUIRE(oss.str() == std::string(expected_bytes, sizeof(expected_bytes)));
}
}
TEST_CASE("Control Message Deserialization", "[control]")
{
SECTION("SessionOffer")
{
const char bytes[] = {
// Session ID
0xCD, 0xAB,
// Unknown
0x00, 0x00, 0x00, 0x00,
// Timestamp
0xDD, 0xCC, 0xBB, 0xAA,
// Milliseconds
0xDD, 0xCC, 0xBB, 0xAA
};
std::istringstream iss(std::string(bytes, sizeof(bytes)));
control::SessionOffer offer;
offer.read_from(iss);
REQUIRE(offer.get_session_id() == 0xABCD);
REQUIRE(offer.get_timestamp() == 0xAABBCCDD);
REQUIRE(offer.get_milliseconds() == 0xAABBCCDD);
}
SECTION("SessionAccept")
{
const char bytes[] = {
// Unknown
0x00, 0x00,
// Unknown
0x00, 0x00, 0x00, 0x00,
// Timestamp
0xDD, 0xCC, 0xBB, 0xAA,
// Milliseconds
0xDD, 0xCC, 0xBB, 0xAA,
// Session ID
0xCD, 0xAB
};
std::istringstream iss(std::string(bytes, sizeof(bytes)));
control::SessionAccept accept;
accept.read_from(iss);
REQUIRE(accept.get_session_id() == 0xABCD);
REQUIRE(accept.get_timestamp() == 0xAABBCCDD);
REQUIRE(accept.get_milliseconds() == 0xAABBCCDD);
}
SECTION("ClientKeepAlive")
{
const char bytes[] = {
// Session ID
0xCD, 0xAB,
// Milliseconds
0xCD, 0xAB,
// Minutes
0xCD, 0xAB
};
std::istringstream iss(std::string(bytes, sizeof(bytes)));
control::ClientKeepAlive keep_alive;
keep_alive.read_from(iss);
REQUIRE(keep_alive.get_session_id() == 0xABCD);
REQUIRE(keep_alive.get_milliseconds() == 0xABCD);
REQUIRE(keep_alive.get_minutes() == 0xABCD);
}
SECTION("ServerKeepAlive")
{
const char bytes[] = {
// Unknown
0x00, 0x00,
// Timestamp
0xDD, 0xCC, 0xBB, 0xAA
};
std::istringstream iss(std::string(bytes, sizeof(bytes)));
control::ServerKeepAlive keep_alive;
keep_alive.read_from(iss);
REQUIRE(keep_alive.get_timestamp() == 0xAABBCCDD);
}
}