Files
mcde/lib/libDtChat/test_pair.c
T
lotherk e9d8d9df4c dtchat: add the pairing SAS and the TOFU pin store (T5.4a)
pair.c derives the 6-digit SAS from both certificate pins (sorted, SHA-256, mod 1000000) so both sides compute the same code; peers.json keeps id -> pin and the blocked flag (0600, atomic store). dc_pins_check answers 1 for a known pin, 0 for an unknown id and -1 for a changed pin; only dc_pins_confirm stores a new pin after the person compared the SAS; a changed pin stays blocked until then. test_pair: known SAS answers (448183 for A+B, 500498 for A+A), order independence, malformed pins, empty store, confirm/check, changed pin, block persistence, save/reload. Standalone and ASan/UBSan clean. make mcde-host still blocked by pid 2126511.
2026-11-16 20:46:19 +00:00

107 lines
2.9 KiB
C

/*
* test_pair.c -- tests for the pairing code and the TOFU pin store (T5.4a)
*
* License: CNCL 1.0 (Could Not Care Less), see LICENSE at the top of the tree.
*/
#include "dc_internal.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
static int fails;
#define CHECK(cond) do {\
if (!(cond)) {\
fprintf(stderr, "FAIL %s:%d: %s\n", __FILE__, __LINE__,\
#cond);\
fails++;\
}\
} while (0)
#define PIN_A \
"sha256:1111111111111111111111111111111111111111111111111111111111111111"
#define PIN_B \
"sha256:2222222222222222222222222222222222222222222222222222222222222222"
#define PIN_C \
"sha256:3333333333333333333333333333333333333333333333333333333333333333"
int
main(void)
{
char tmp[] = "/tmp/opencode/dtchat-pair-XXXXXX";
char dir[512];
DcPins *pins;
DcPins *again;
char *sas;
size_t i;
sas = dc_pair_sas(PIN_A, PIN_B);
CHECK(sas != NULL);
if (sas != NULL) {
CHECK(strlen(sas) == 6);
for (i = 0; i < 6; i++)
CHECK(sas[i] >= '0' && sas[i] <= '9');
/* known answer for these two pins */
CHECK(strcmp(sas, "448183") == 0);
free(sas);
}
sas = dc_pair_sas(PIN_B, PIN_A);
CHECK(sas != NULL && strcmp(sas, "448183") == 0);
free(sas);
sas = dc_pair_sas(PIN_A, PIN_A);
CHECK(sas != NULL && strcmp(sas, "500498") == 0);
free(sas);
CHECK(dc_pair_sas("nonsense", PIN_B) == NULL);
CHECK(dc_pair_sas(PIN_A, NULL) == NULL);
CHECK(mkdtemp(tmp) != NULL);
snprintf(dir, sizeof(dir), "%s/state", tmp);
pins = dc_pins_load(dir);
CHECK(pins != NULL);
if (pins == NULL)
return 1;
CHECK(dc_pins_count(pins) == 0);
CHECK(dc_pins_check(pins, "alice", PIN_A) == 0);
CHECK(dc_pins_confirm(pins, "alice", PIN_A) == 0);
CHECK(dc_pins_count(pins) == 1);
CHECK(dc_pins_check(pins, "alice", PIN_A) == 1);
CHECK(dc_pins_check(pins, "alice", PIN_B) == -1);
CHECK(dc_pins_check(pins, "bob", PIN_B) == 0);
CHECK(dc_pins_set_blocked(pins, "alice", 1) == 0);
CHECK(dc_pins_is_blocked(pins, "alice") == 1);
CHECK(dc_pins_confirm(pins, "bob", PIN_B) == 0);
CHECK(dc_pins_count(pins) == 2);
CHECK(dc_pins_save(pins, dir) == 0);
dc_pins_free(pins);
again = dc_pins_load(dir);
CHECK(again != NULL);
if (again != NULL) {
CHECK(dc_pins_count(again) == 2);
CHECK(dc_pins_check(again, "alice", PIN_A) == 1);
CHECK(dc_pins_is_blocked(again, "alice") == 1);
CHECK(dc_pins_check(again, "bob", PIN_B) == 1);
CHECK(dc_pins_is_blocked(again, "bob") == 0);
/* the person re-pairs: a new pin is stored after confirmation */
CHECK(dc_pins_check(again, "alice", PIN_C) == -1);
CHECK(dc_pins_confirm(again, "alice", PIN_C) == 0);
CHECK(dc_pins_check(again, "alice", PIN_C) == 1);
dc_pins_free(again);
}
{
char path[600];
snprintf(path, sizeof(path), "%s/peers.json", dir);
CHECK(access(path, F_OK) == 0);
}
if (fails != 0) {
fprintf(stderr, "%d test(s) failed\n", fails);
return 1;
}
printf("test_pair: all ok\n");
return 0;
}