87 lines
2.5 KiB
C
87 lines
2.5 KiB
C
#include "amduat/enc/asl_log.h"
|
|
|
|
#include <stdbool.h>
|
|
#include <stdint.h>
|
|
#include <stdio.h>
|
|
#include <string.h>
|
|
|
|
static void fill_hash(uint8_t *out, uint8_t seed) {
|
|
for (size_t i = 0; i < 32u; ++i) {
|
|
out[i] = (uint8_t)(seed + i);
|
|
}
|
|
}
|
|
|
|
static bool records_equal(const amduat_asl_log_record_t *lhs,
|
|
const amduat_asl_log_record_t *rhs) {
|
|
if (lhs->logseq != rhs->logseq || lhs->record_type != rhs->record_type ||
|
|
lhs->payload.len != rhs->payload.len) {
|
|
return false;
|
|
}
|
|
if (lhs->payload.len != 0 &&
|
|
memcmp(lhs->payload.data, rhs->payload.data, lhs->payload.len) != 0) {
|
|
return false;
|
|
}
|
|
if (memcmp(lhs->record_hash, rhs->record_hash, sizeof(lhs->record_hash)) != 0) {
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
int main(void) {
|
|
uint8_t payload_a[4] = {0x01, 0x02, 0x03, 0x04};
|
|
uint8_t payload_b[8] = {0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17};
|
|
uint8_t payload_c[2] = {0xaa, 0xbb};
|
|
amduat_asl_log_record_t records[3];
|
|
amduat_asl_log_record_t *decoded = NULL;
|
|
size_t decoded_len = 0u;
|
|
amduat_octets_t bytes;
|
|
|
|
memset(records, 0, sizeof(records));
|
|
|
|
records[0].logseq = 1u;
|
|
records[0].record_type = AMDUAT_ASL_LOG_RECORD_SEGMENT_SEAL;
|
|
records[0].payload = amduat_octets(payload_a, sizeof(payload_a));
|
|
fill_hash(records[0].record_hash, 0x10);
|
|
|
|
records[1].logseq = 2u;
|
|
records[1].record_type = AMDUAT_ASL_LOG_RECORD_TOMBSTONE;
|
|
records[1].payload = amduat_octets(payload_b, sizeof(payload_b));
|
|
fill_hash(records[1].record_hash, 0x20);
|
|
|
|
records[2].logseq = 3u;
|
|
records[2].record_type = AMDUAT_ASL_LOG_RECORD_SNAPSHOT_ANCHOR;
|
|
records[2].payload = amduat_octets(payload_c, sizeof(payload_c));
|
|
fill_hash(records[2].record_hash, 0x30);
|
|
|
|
if (!amduat_enc_asl_log_encode_v1(records, 3u, &bytes)) {
|
|
fprintf(stderr, "encode failed\n");
|
|
return 1;
|
|
}
|
|
|
|
if (!amduat_enc_asl_log_decode_v1(bytes, &decoded, &decoded_len)) {
|
|
fprintf(stderr, "decode failed\n");
|
|
amduat_octets_free(&bytes);
|
|
return 1;
|
|
}
|
|
|
|
if (decoded_len != 3u) {
|
|
fprintf(stderr, "decoded length mismatch\n");
|
|
amduat_octets_free(&bytes);
|
|
amduat_enc_asl_log_free(decoded, decoded_len);
|
|
return 1;
|
|
}
|
|
|
|
for (size_t i = 0; i < decoded_len; ++i) {
|
|
if (!records_equal(&records[i], &decoded[i])) {
|
|
fprintf(stderr, "record mismatch at %zu\n", i);
|
|
amduat_octets_free(&bytes);
|
|
amduat_enc_asl_log_free(decoded, decoded_len);
|
|
return 1;
|
|
}
|
|
}
|
|
|
|
amduat_octets_free(&bytes);
|
|
amduat_enc_asl_log_free(decoded, decoded_len);
|
|
return 0;
|
|
}
|