amduat/tests/fed/test_fed_registry.c

94 lines
2.2 KiB
C
Raw Permalink Normal View History

#include "amduat/fed/registry.h"
#include <stdio.h>
#include <string.h>
static bool octets_equal(amduat_octets_t a, amduat_octets_t b) {
if (a.len != b.len) {
return false;
}
if (a.len == 0) {
return true;
}
return memcmp(a.data, b.data, a.len) == 0;
}
static int test_round_trip(void) {
uint8_t policy_a[] = {0xde, 0xad, 0xbe, 0xef};
amduat_fed_domain_state_t states[2];
amduat_fed_registry_value_t value;
amduat_fed_registry_value_t decoded;
amduat_octets_t encoded;
int exit_code = 1;
memset(states, 0, sizeof(states));
amduat_fed_registry_value_init(&value, states, 2);
states[0].domain_id = 42;
states[0].snapshot_id = 10;
states[0].log_prefix = 8;
states[0].last_logseq = 7;
states[0].admitted = 1;
states[0].policy_ok = 1;
states[0].policy_hash_id = 1;
states[0].policy_hash = amduat_octets(policy_a, sizeof(policy_a));
states[1].domain_id = 7;
states[1].snapshot_id = 3;
states[1].log_prefix = 2;
states[1].last_logseq = 2;
states[1].admitted = 0;
states[1].policy_ok = 0;
states[1].policy_hash_id = 0;
states[1].policy_hash = amduat_octets(NULL, 0);
if (!amduat_fed_registry_value_insert(&value, states[0]) ||
!amduat_fed_registry_value_insert(&value, states[1])) {
fprintf(stderr, "insert failed\n");
return exit_code;
}
if (!amduat_fed_registry_encode(&value, &encoded)) {
fprintf(stderr, "encode failed\n");
return exit_code;
}
memset(&decoded, 0, sizeof(decoded));
if (!amduat_fed_registry_decode(encoded, &decoded)) {
fprintf(stderr, "decode failed\n");
amduat_octets_free(&encoded);
return exit_code;
}
if (decoded.len != 2) {
fprintf(stderr, "decoded length mismatch\n");
goto cleanup;
}
if (decoded.states[0].domain_id != 7 ||
decoded.states[1].domain_id != 42) {
fprintf(stderr, "decoded order mismatch\n");
goto cleanup;
}
if (!octets_equal(decoded.states[1].policy_hash,
amduat_octets(policy_a, sizeof(policy_a)))) {
fprintf(stderr, "decoded policy hash mismatch\n");
goto cleanup;
}
exit_code = 0;
cleanup:
amduat_octets_free(&encoded);
amduat_fed_registry_value_free(&decoded);
return exit_code;
}
int main(void) {
if (test_round_trip() != 0) {
return 1;
}
return 0;
}