#include "sc2k/io/native_civic_input_inbox.hpp" #include #include #include #include #include "test_harness.hpp" namespace { using namespace sc2k; void set_u16(std::vector& bytes, std::size_t offset, std::uint16_t value) { bytes[offset] = static_cast(value); bytes[offset + 1] = static_cast(value >> 8); } void set_u32(std::vector& bytes, std::size_t offset, std::uint32_t value) { for (int byte = 0; byte < 4; ++byte) bytes[offset + static_cast(byte)] = static_cast(value >> (8 * byte)); } void set_u64(std::vector& bytes, std::size_t offset, std::uint64_t value) { for (int byte = 0; byte < 8; ++byte) bytes[offset + static_cast(byte)] = static_cast(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 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 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 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; }