118 lines
3.1 KiB
C++
118 lines
3.1 KiB
C++
#include "reloadable_host/checkpoint.hpp"
|
|
|
|
#include <cassert>
|
|
#include <cstdio>
|
|
#include <cstdint>
|
|
#include <string>
|
|
#include <string_view>
|
|
#include <vector>
|
|
|
|
namespace
|
|
{
|
|
using namespace ReloadableHost;
|
|
|
|
void append_u32(std::vector<std::byte>& out, std::uint32_t value)
|
|
{
|
|
for (int shift = 24; shift >= 0; shift -= 8) {
|
|
out.push_back(static_cast<std::byte>((value >> shift) & 0xFFU));
|
|
}
|
|
}
|
|
|
|
void append_u64(std::vector<std::byte>& out, std::uint64_t value)
|
|
{
|
|
for (int shift = 56; shift >= 0; shift -= 8) {
|
|
out.push_back(static_cast<std::byte>((value >> shift) & 0xFFU));
|
|
}
|
|
}
|
|
|
|
std::vector<std::byte> legacy(std::uint32_t version, std::uint64_t counter,
|
|
std::string_view workflow = {})
|
|
{
|
|
std::vector<std::byte> out{std::byte{'V'}, std::byte{'C'},
|
|
std::byte{'H'}, std::byte{'K'}};
|
|
append_u32(out, version);
|
|
append_u64(out, counter);
|
|
if (version >= 2) {
|
|
append_u32(out, static_cast<std::uint32_t>(workflow.size()));
|
|
for (const char ch : workflow) {
|
|
out.push_back(static_cast<std::byte>(ch));
|
|
}
|
|
}
|
|
return out;
|
|
}
|
|
|
|
void current_schema_round_trips()
|
|
{
|
|
const CheckpointState state{7, "workflow-a", 3};
|
|
auto encoded = encode_checkpoint(state);
|
|
assert(encoded.has_value());
|
|
auto decoded = decode_checkpoint(*encoded);
|
|
assert(decoded.has_value());
|
|
assert(decoded->counter == 7);
|
|
assert(decoded->workflow == "workflow-a");
|
|
assert(decoded->generation == 3);
|
|
}
|
|
|
|
void older_schemas_migrate_forward()
|
|
{
|
|
auto v1 = decode_checkpoint(legacy(1, 42));
|
|
assert(v1.has_value());
|
|
assert(v1->counter == 42);
|
|
assert(v1->workflow.empty());
|
|
assert(v1->generation == 0);
|
|
|
|
auto v2 = decode_checkpoint(legacy(2, 9, "wf"));
|
|
assert(v2.has_value());
|
|
assert(v2->counter == 9);
|
|
assert(v2->workflow == "wf");
|
|
assert(v2->generation == 0);
|
|
}
|
|
|
|
void malformed_payloads_are_rejected()
|
|
{
|
|
const CheckpointState state{1, "wf", 2};
|
|
auto encoded = encode_checkpoint(state);
|
|
assert(encoded.has_value());
|
|
|
|
// Unknown newer schema.
|
|
auto future = legacy(1, 5);
|
|
future[7] = std::byte{4}; // version field is bytes 4..7 (big-endian)
|
|
assert(!decode_checkpoint(future).has_value());
|
|
|
|
// Bad magic.
|
|
auto bad_magic = *encoded;
|
|
bad_magic[0] = std::byte{'X'};
|
|
assert(!decode_checkpoint(bad_magic).has_value());
|
|
|
|
// Truncated and trailing bytes.
|
|
auto truncated = *encoded;
|
|
truncated.pop_back();
|
|
assert(!decode_checkpoint(truncated).has_value());
|
|
auto trailing = *encoded;
|
|
trailing.push_back(std::byte{0});
|
|
assert(!decode_checkpoint(trailing).has_value());
|
|
}
|
|
|
|
void limits_are_enforced()
|
|
{
|
|
auto big = encode_checkpoint(CheckpointState{0, std::string(10, 'x'), 0});
|
|
assert(big.has_value());
|
|
assert(!decode_checkpoint(*big, CheckpointLimits{.max_workflow_bytes = 4})
|
|
.has_value());
|
|
assert(!encode_checkpoint(
|
|
CheckpointState{0, std::string(100, 'x'), 0},
|
|
CheckpointLimits{.max_workflow_bytes = 4})
|
|
.has_value());
|
|
}
|
|
} // namespace
|
|
|
|
int main()
|
|
{
|
|
current_schema_round_trips();
|
|
older_schemas_migrate_forward();
|
|
malformed_payloads_are_rejected();
|
|
limits_are_enforced();
|
|
std::puts("reloadable-host checkpoint ok");
|
|
return 0;
|
|
}
|