/* * 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 #include #include #include 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[64]; 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(dc_test_tmpdir(tmp, sizeof tmp, "pair") == 0); 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; }