Files
n_signer/tests/test_role_table.c
Laan Tungir 268b33b6d3 first
2026-05-02 12:31:26 -04:00

136 lines
4.6 KiB
C

#include "../src/role_table.h"
#include <stdio.h>
#include <string.h>
static int g_failures = 0;
static void check_condition(const char *name, int condition) {
if (condition) {
printf("PASS: %s\n", name);
} else {
printf("FAIL: %s\n", name);
g_failures++;
}
}
static role_entry_t make_nostr_entry(const char *name, const char *purpose, const char *curve, int idx) {
role_entry_t e;
memset(&e, 0, sizeof(e));
strncpy(e.name, name, sizeof(e.name) - 1);
strncpy(e.purpose_str, purpose, sizeof(e.purpose_str) - 1);
strncpy(e.curve_str, curve, sizeof(e.curve_str) - 1);
e.purpose = role_purpose_from_str(e.purpose_str);
e.curve = role_curve_from_str(e.curve_str);
e.selector_type = SELECTOR_NOSTR_INDEX;
e.nostr_index = idx;
e.derived = 0;
return e;
}
static role_entry_t make_path_entry(const char *name, const char *purpose, const char *curve, const char *path) {
role_entry_t e;
memset(&e, 0, sizeof(e));
strncpy(e.name, name, sizeof(e.name) - 1);
strncpy(e.purpose_str, purpose, sizeof(e.purpose_str) - 1);
strncpy(e.curve_str, curve, sizeof(e.curve_str) - 1);
strncpy(e.role_path, path, sizeof(e.role_path) - 1);
e.purpose = role_purpose_from_str(e.purpose_str);
e.curve = role_curve_from_str(e.curve_str);
e.selector_type = SELECTOR_ROLE_PATH;
e.nostr_index = -1;
e.derived = 0;
return e;
}
int main(void) {
role_table_t table;
role_entry_t e_main;
role_entry_t e_throwaway;
role_entry_t e_btc;
role_entry_t e_ssh;
role_entry_t *found;
int rc;
int i;
role_table_init(&table);
check_condition("init count==0", table.count == 0);
e_main = make_nostr_entry("main", "nostr", "secp256k1", 0);
rc = role_table_add(&table, &e_main);
check_condition("add main returns 0", rc == 0);
e_throwaway = make_nostr_entry("throwaway", "nostr", "secp256k1", 1);
rc = role_table_add(&table, &e_throwaway);
check_condition("add throwaway returns 0", rc == 0);
e_btc = make_path_entry("btc_savings", "bitcoin", "secp256k1", "m/84'/0'/0'/0/0");
rc = role_table_add(&table, &e_btc);
check_condition("add btc_savings returns 0", rc == 0);
e_ssh = make_path_entry("ssh_key", "ssh", "ed25519", "m/44'/822'/0'/0/0");
rc = role_table_add(&table, &e_ssh);
check_condition("add ssh_key returns 0", rc == 0);
found = role_table_find_by_name(&table, "main");
check_condition("find_by_name(main) not NULL", found != NULL);
check_condition("find_by_name(main) purpose nostr", found != NULL && found->purpose == PURPOSE_NOSTR);
found = role_table_find_by_nostr_index(&table, 1);
check_condition("find_by_nostr_index(1) is throwaway", found != NULL && strcmp(found->name, "throwaway") == 0);
found = role_table_find_by_path(&table, "m/84'/0'/0'/0/0");
check_condition("find_by_path btc path is btc_savings", found != NULL && strcmp(found->name, "btc_savings") == 0);
found = role_table_get_default(&table);
check_condition("get_default() is main", found != NULL && strcmp(found->name, "main") == 0);
rc = role_table_add(&table, &e_main);
check_condition("duplicate name rejection returns -2", rc == -2);
role_table_init(&table);
for (i = 0; i < ROLE_TABLE_MAX_ENTRIES; ++i) {
role_entry_t e;
char name_buf[ROLE_NAME_MAX];
memset(&e, 0, sizeof(e));
snprintf(name_buf, sizeof(name_buf), "role_%d", i);
strncpy(e.name, name_buf, sizeof(e.name) - 1);
strncpy(e.purpose_str, "nostr", sizeof(e.purpose_str) - 1);
strncpy(e.curve_str, "secp256k1", sizeof(e.curve_str) - 1);
e.purpose = PURPOSE_NOSTR;
e.curve = CURVE_SECP256K1;
e.selector_type = SELECTOR_NOSTR_INDEX;
e.nostr_index = i;
rc = role_table_add(&table, &e);
check_condition("fill table role add returns 0", rc == 0);
}
{
role_entry_t overflow;
memset(&overflow, 0, sizeof(overflow));
strncpy(overflow.name, "overflow", sizeof(overflow.name) - 1);
strncpy(overflow.purpose_str, "nostr", sizeof(overflow.purpose_str) - 1);
strncpy(overflow.curve_str, "secp256k1", sizeof(overflow.curve_str) - 1);
overflow.purpose = PURPOSE_NOSTR;
overflow.curve = CURVE_SECP256K1;
overflow.selector_type = SELECTOR_NOSTR_INDEX;
overflow.nostr_index = 999;
rc = role_table_add(&table, &overflow);
check_condition("table full rejection returns -1", rc == -1);
}
if (g_failures == 0) {
printf("ALL TESTS PASSED\n");
return 0;
}
printf("TESTS FAILED: %d\n", g_failures);
return 1;
}