Something went wrong. Try again.
[Mirror] Libreopolis is a cross-platform city simulation game fully compatible with SimCity 2000. gitlab.com/conasg/libreopolis
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235#include "sc2k/io/native_civic_input_inbox.hpp"
#include <cstddef>#include <cstdint>#include <cstdio>#include <vector>
#include "test_harness.hpp"
namespace {
using namespace sc2k;
void set_u16(std::vector<std::uint8_t>& bytes, std::size_t offset, std::uint16_t value) { bytes[offset] = static_cast<std::uint8_t>(value); bytes[offset + 1] = static_cast<std::uint8_t>(value >> 8);}
void set_u32(std::vector<std::uint8_t>& bytes, std::size_t offset, std::uint32_t value) { for (int byte = 0; byte < 4; ++byte) bytes[offset + static_cast<std::size_t>(byte)] = static_cast<std::uint8_t>(value >> (8 * byte));}
void set_u64(std::vector<std::uint8_t>& bytes, std::size_t offset, std::uint64_t value) { for (int byte = 0; byte < 8; ++byte) bytes[offset + static_cast<std::size_t>(byte)] = static_cast<std::uint8_t>(value >> (8 * byte));}
sim::CivicInputInboxState populated_state() { const auto dormant = sim::make_dormant_civic_input_inbox_state(16, 20); CHECK(dormant.has_value()); if (!dormant) return {}; const sim::CivicInputActivationResult activated = sim::activate_civic_input_inbox(*dormant, 10, {7, 6, 5}); CHECK(activated.activated()); sim::CivicInputInboxState state = activated.nextState;
sim::CivicInputAppendResult appended = sim::append_civic_tax_change(state, 10, 0, 7, 8); CHECK(appended.appended()); state = appended.nextState; appended = sim::append_civic_service_disruption(state, 11, sim::CivicServiceKind::Police, -1, -1, 7500, true); CHECK(appended.appended()); state = appended.nextState; appended = sim::append_civic_stadium_outcome(state, 12, sim::CivicStadiumOutcome::Failure, 15, 19, 6000); CHECK(appended.appended()); return appended.nextState;}
void exact_round_trip_and_extent_binding() { const auto dormant = sim::make_dormant_civic_input_inbox_state(16, 20); CHECK(dormant.has_value()); if (!dormant) return; const auto dormantBytes = io::encode_native_civic_input_inbox(*dormant); CHECK(dormantBytes.has_value()); CHECK(dormantBytes && dormantBytes->size() == io::kNativeCivicInputInboxHeaderBytes); CHECK(dormantBytes && io::decode_native_civic_input_inbox(*dormantBytes, 16, 20) == dormant);
const sim::CivicInputInboxState state = populated_state(); const auto encoded = io::encode_native_civic_input_inbox(state); CHECK(encoded.has_value()); CHECK(encoded && encoded->size() == io::kNativeCivicInputInboxHeaderBytes + io::kNativeCivicTaxInputRecordBytes + io::kNativeCivicServiceInputRecordBytes + io::kNativeCivicStadiumInputRecordBytes); CHECK(encoded && io::decode_native_civic_input_inbox(*encoded, 16, 20) == state); CHECK(encoded && !io::decode_native_civic_input_inbox(*encoded, 20, 16));
if (!encoded) return; std::vector<std::uint8_t> malformed = *encoded; malformed.pop_back(); CHECK(!io::decode_native_civic_input_inbox(malformed, 16, 20)); malformed = *encoded; malformed.push_back(0); CHECK(!io::decode_native_civic_input_inbox(malformed, 16, 20));}
void header_corruption_is_rejected() { const auto encoded = io::encode_native_civic_input_inbox(populated_state()); CHECK(encoded.has_value()); if (!encoded) return; std::vector<std::uint8_t> malformed = *encoded;
malformed[0] = 'X'; CHECK(!io::decode_native_civic_input_inbox(malformed, 16, 20)); malformed = *encoded; set_u16(malformed, 4, 2); CHECK(!io::decode_native_civic_input_inbox(malformed, 16, 20)); malformed = *encoded; set_u16(malformed, 6, 2); CHECK(!io::decode_native_civic_input_inbox(malformed, 16, 20)); malformed = *encoded; set_u32(malformed, 8, 15); CHECK(!io::decode_native_civic_input_inbox(malformed, 16, 20)); malformed = *encoded; set_u64(malformed, 16, std::uint64_t(-1)); CHECK(!io::decode_native_civic_input_inbox(malformed, 16, 20)); malformed = *encoded; set_u64(malformed, 24, 10); CHECK(!io::decode_native_civic_input_inbox(malformed, 16, 20)); malformed = *encoded; set_u64(malformed, 32, 0); CHECK(!io::decode_native_civic_input_inbox(malformed, 16, 20)); malformed = *encoded; set_u16(malformed, 40, 21); CHECK(!io::decode_native_civic_input_inbox(malformed, 16, 20)); malformed = *encoded; set_u16(malformed, 46, 1); CHECK(!io::decode_native_civic_input_inbox(malformed, 16, 20)); malformed = *encoded; set_u16(malformed, 48, 257); CHECK(!io::decode_native_civic_input_inbox(malformed, 16, 20)); malformed = *encoded; set_u16(malformed, 54, 1); CHECK(!io::decode_native_civic_input_inbox(malformed, 16, 20));}
void record_corruption_is_rejected() { const auto encoded = io::encode_native_civic_input_inbox(populated_state()); CHECK(encoded.has_value()); if (!encoded) return; constexpr std::size_t tax = io::kNativeCivicInputInboxHeaderBytes; constexpr std::size_t service = tax + io::kNativeCivicTaxInputRecordBytes; constexpr std::size_t stadium = service + io::kNativeCivicServiceInputRecordBytes; std::vector<std::uint8_t> malformed = *encoded;
set_u64(malformed, tax, 0); CHECK(!io::decode_native_civic_input_inbox(malformed, 16, 20)); malformed = *encoded; set_u64(malformed, tax + 8, 9); CHECK(!io::decode_native_civic_input_inbox(malformed, 16, 20)); malformed = *encoded; malformed[tax + 16] = 3; CHECK(!io::decode_native_civic_input_inbox(malformed, 16, 20)); malformed = *encoded; malformed[tax + 17] = 1; CHECK(!io::decode_native_civic_input_inbox(malformed, 16, 20)); malformed = *encoded; set_u16(malformed, tax + 20, 9); CHECK(!io::decode_native_civic_input_inbox(malformed, 16, 20)); malformed = *encoded; set_u64(malformed, tax + 24, 1); CHECK(!io::decode_native_civic_input_inbox(malformed, 16, 20));
malformed = *encoded; malformed[service + 16] = 0; CHECK(!io::decode_native_civic_input_inbox(malformed, 16, 20)); malformed = *encoded; malformed[service + 17] = 2; CHECK(!io::decode_native_civic_input_inbox(malformed, 16, 20)); malformed = *encoded; set_u16(malformed, service + 18, 0); CHECK(!io::decode_native_civic_input_inbox(malformed, 16, 20)); malformed = *encoded; set_u32(malformed, service + 20, 0); CHECK(!io::decode_native_civic_input_inbox(malformed, 16, 20)); malformed = *encoded; set_u32(malformed, service + 28, 1); CHECK(!io::decode_native_civic_input_inbox(malformed, 16, 20)); malformed = *encoded; set_u64(malformed, service + 32, 1); CHECK(!io::decode_native_civic_input_inbox(malformed, 16, 20));
malformed = *encoded; malformed[stadium + 16] = 0; CHECK(!io::decode_native_civic_input_inbox(malformed, 16, 20)); malformed = *encoded; malformed[stadium + 17] = 1; CHECK(!io::decode_native_civic_input_inbox(malformed, 16, 20)); malformed = *encoded; set_u32(malformed, stadium + 24, 20); CHECK(!io::decode_native_civic_input_inbox(malformed, 16, 20)); malformed = *encoded; set_u16(malformed, stadium + 28, 0); CHECK(!io::decode_native_civic_input_inbox(malformed, 16, 20)); malformed = *encoded; set_u64(malformed, stadium + 32, 1); CHECK(!io::decode_native_civic_input_inbox(malformed, 16, 20));
// Cross-kind source identity is one namespace. malformed = *encoded; set_u64(malformed, service, 1); CHECK(!io::decode_native_civic_input_inbox(malformed, 16, 20));}
void encoder_and_policy_reject_invalid_authority() { sim::CivicInputInboxState state = populated_state(); state.taxChanges.front().newRatePercent = 9; CHECK(!io::encode_native_civic_input_inbox(state)); state = populated_state(); state.serviceDisruptions.front().anchorRow = 16; state.serviceDisruptions.front().global = false; CHECK(!io::encode_native_civic_input_inbox(state)); state = populated_state(); state.serviceDisruptions.front().sourceId = state.taxChanges.front().sourceId; CHECK(!io::encode_native_civic_input_inbox(state));
sim::CivicSatisfactionPolicy invalidPolicy; invalidPolicy.policyVersion = 2; CHECK(!io::encode_native_civic_input_inbox( populated_state(), invalidPolicy));}
} // namespace
int main() { exact_round_trip_and_extent_binding(); header_corruption_is_rejected(); record_corruption_is_rejected(); encoder_and_policy_reject_invalid_authority();
if (failures == 0) { std::printf("native civic input inbox: all checks passed\n"); return 0; } std::printf("native civic input inbox: %d failure(s)\n", failures); return 1;}