93 lines
2.9 KiB
C
93 lines
2.9 KiB
C
|
|
#include "amduat/enc/pel_program_dag_desc.h"
|
||
|
|
#include "amduat/enc/pel_program_dag.h"
|
||
|
|
#include "amduat/pel/program_dag_desc.h"
|
||
|
|
|
||
|
|
#include <stdio.h>
|
||
|
|
#include <string.h>
|
||
|
|
|
||
|
|
static bool bytes_equal(amduat_octets_t bytes,
|
||
|
|
const uint8_t *expected,
|
||
|
|
size_t expected_len) {
|
||
|
|
if (bytes.len != expected_len) {
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
if (bytes.len == 0) {
|
||
|
|
return true;
|
||
|
|
}
|
||
|
|
return memcmp(bytes.data, expected, expected_len) == 0;
|
||
|
|
}
|
||
|
|
|
||
|
|
static int test_descriptor_round_trip(void) {
|
||
|
|
static const uint8_t k_desc_bytes[] = {
|
||
|
|
0x00, 0x01, 0x00, 0x00, 0x00, 0x11, 0x50, 0x45,
|
||
|
|
0x4c, 0x2f, 0x50, 0x52, 0x4f, 0x47, 0x52, 0x41,
|
||
|
|
0x4d, 0x2d, 0x44, 0x41, 0x47, 0x2f, 0x31, 0x00,
|
||
|
|
0x00, 0x01, 0x01, 0x01, 0x01, 0x00, 0x00
|
||
|
|
};
|
||
|
|
const char k_scheme_name[] = "PEL/PROGRAM-DAG/1";
|
||
|
|
amduat_pel_dag_scheme_descriptor_t desc;
|
||
|
|
amduat_pel_dag_scheme_descriptor_t decoded;
|
||
|
|
amduat_octets_t encoded = amduat_octets(NULL, 0);
|
||
|
|
amduat_octets_t encoded_roundtrip = amduat_octets(NULL, 0);
|
||
|
|
int exit_code = 1;
|
||
|
|
|
||
|
|
memset(&desc, 0, sizeof(desc));
|
||
|
|
desc.pel1_version = 1;
|
||
|
|
desc.scheme_name = amduat_octets(k_scheme_name, strlen(k_scheme_name));
|
||
|
|
desc.program_type_tag = amduat_type_tag(AMDUAT_PEL_TYPE_TAG_PROGRAM_DAG_1);
|
||
|
|
desc.program_enc_profile = AMDUAT_PEL_ENC_PROGRAM_DAG_V1;
|
||
|
|
desc.has_trace_profile_ref = false;
|
||
|
|
desc.has_opreg_ref = false;
|
||
|
|
|
||
|
|
if (!amduat_enc_pel_program_dag_desc_encode_v1(&desc, &encoded)) {
|
||
|
|
fprintf(stderr, "encode failed\n");
|
||
|
|
goto cleanup;
|
||
|
|
}
|
||
|
|
if (!bytes_equal(encoded, k_desc_bytes, sizeof(k_desc_bytes))) {
|
||
|
|
fprintf(stderr, "encoded bytes mismatch\n");
|
||
|
|
goto cleanup;
|
||
|
|
}
|
||
|
|
|
||
|
|
memset(&decoded, 0, sizeof(decoded));
|
||
|
|
if (!amduat_enc_pel_program_dag_desc_decode_v1(
|
||
|
|
amduat_octets(k_desc_bytes, sizeof(k_desc_bytes)), &decoded)) {
|
||
|
|
fprintf(stderr, "decode failed\n");
|
||
|
|
goto cleanup;
|
||
|
|
}
|
||
|
|
if (decoded.pel1_version != 1 ||
|
||
|
|
decoded.program_type_tag.tag_id != AMDUAT_PEL_TYPE_TAG_PROGRAM_DAG_1 ||
|
||
|
|
decoded.program_enc_profile != AMDUAT_PEL_ENC_PROGRAM_DAG_V1 ||
|
||
|
|
decoded.has_trace_profile_ref || decoded.has_opreg_ref) {
|
||
|
|
fprintf(stderr, "decoded fields mismatch\n");
|
||
|
|
goto cleanup;
|
||
|
|
}
|
||
|
|
if (!bytes_equal(decoded.scheme_name,
|
||
|
|
(const uint8_t *)k_scheme_name,
|
||
|
|
strlen(k_scheme_name))) {
|
||
|
|
fprintf(stderr, "decoded scheme name mismatch\n");
|
||
|
|
goto cleanup;
|
||
|
|
}
|
||
|
|
|
||
|
|
if (!amduat_enc_pel_program_dag_desc_encode_v1(&decoded,
|
||
|
|
&encoded_roundtrip)) {
|
||
|
|
fprintf(stderr, "roundtrip encode failed\n");
|
||
|
|
goto cleanup;
|
||
|
|
}
|
||
|
|
if (!bytes_equal(encoded_roundtrip, k_desc_bytes, sizeof(k_desc_bytes))) {
|
||
|
|
fprintf(stderr, "roundtrip bytes mismatch\n");
|
||
|
|
goto cleanup;
|
||
|
|
}
|
||
|
|
|
||
|
|
exit_code = 0;
|
||
|
|
|
||
|
|
cleanup:
|
||
|
|
amduat_octets_free(&encoded);
|
||
|
|
amduat_octets_free(&encoded_roundtrip);
|
||
|
|
amduat_enc_pel_program_dag_desc_free(&decoded);
|
||
|
|
return exit_code;
|
||
|
|
}
|
||
|
|
|
||
|
|
int main(void) {
|
||
|
|
return test_descriptor_round_trip();
|
||
|
|
}
|