57 lines
1.6 KiB
C++
57 lines
1.6 KiB
C++
#include "game_server/NetworkProtocol.h"
|
|
#include "LocationState.h"
|
|
#include <cstdlib>
|
|
#include <iostream>
|
|
#include <string>
|
|
|
|
namespace {
|
|
|
|
void require(bool condition, const char* message)
|
|
{
|
|
if (condition) return;
|
|
std::cerr << "NetworkProtocolTests: " << message << '\n';
|
|
std::exit(EXIT_FAILURE);
|
|
}
|
|
|
|
} // namespace
|
|
|
|
int main()
|
|
{
|
|
FRG::LocationNetworkState locationState;
|
|
nlohmann::json savedLocationState;
|
|
locationState.save(savedLocationState);
|
|
const nlohmann::json original{
|
|
{"schemaVersion", 2},
|
|
{"campaignId", "battle_zone_multiplayer"},
|
|
{"players", {{"player", {{"hp", 200.0}}}}},
|
|
{"locations", {{"uni_exterior", {{"state", savedLocationState}}}}}
|
|
};
|
|
const std::string message = FRG::makeStateMessage(original);
|
|
nlohmann::json parsed;
|
|
std::string error;
|
|
require(
|
|
FRG::parseStateMessage(message, parsed, error),
|
|
"valid state envelope was rejected");
|
|
require(parsed == original, "state payload did not round trip");
|
|
require(
|
|
!parsed["locations"]["uni_exterior"]["state"].contains("cameraAzimuth") &&
|
|
!parsed["locations"]["uni_exterior"]["state"].contains("cameraInclination"),
|
|
"local camera state leaked into the network payload");
|
|
|
|
require(
|
|
!FRG::parseStateMessage(
|
|
R"({"protocolVersion":2,"type":"state","payload":{}})",
|
|
parsed, error),
|
|
"unknown protocol version was accepted");
|
|
require(
|
|
!FRG::parseStateMessage(
|
|
R"({"protocolVersion":1,"type":"command","payload":{}})",
|
|
parsed, error),
|
|
"unknown message type was accepted");
|
|
require(
|
|
!FRG::parseStateMessage("not json", parsed, error),
|
|
"malformed JSON was accepted");
|
|
|
|
return EXIT_SUCCESS;
|
|
}
|