local.c binds a nonblocking TLS listener (identity certificate, 127.0.0.1 or all interfaces, port 0 = ephemeral) and drives the handshake from DcLoop. A peer sends one JSON line {type:hello,id,pin}; the listener checks id and pin against what it expects, answers {type:hello-ack,ok:true|false,pin:...} and closes on a wrong pin, a lying or malformed hello. The accepted hello goes to the callback once; connections stay open for the protocol of T5.5. identity.c now also exposes the private key as PEM for libtls. test_local starts a listener per case and connects with a blocking libtls client: matching pin accepted (callback sees id/pin), wrong pin rejected (no callback, ok:false), any-pin accepted. Standalone test all ok; make mcde-host could not run because another agent's release+sourcesets+usb chain holds build/.build.lock (pid 2126511); the next build verifies the wiring.
120 lines
2.9 KiB
C
120 lines
2.9 KiB
C
/*
|
|
* test_identity.c -- tests for the Ed25519 identity (T5.1)
|
|
*
|
|
* License: CNCL 1.0 (Could Not Care Less), see LICENSE at the top of the tree.
|
|
*/
|
|
#include "dc_internal.h"
|
|
|
|
#include <fcntl.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <sys/stat.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)
|
|
|
|
static void
|
|
mode_is(const char *path, mode_t want)
|
|
{
|
|
struct stat st;
|
|
|
|
CHECK(stat(path, &st) == 0);
|
|
if (stat(path, &st) == 0)
|
|
CHECK((st.st_mode & 0777) == want);
|
|
}
|
|
|
|
int
|
|
main(void)
|
|
{
|
|
char base[] = "/tmp/opencode/dtchat-id-XXXXXX";
|
|
char dir[512];
|
|
char other[512];
|
|
char path[600];
|
|
DcIdentity *id;
|
|
DcIdentity *again;
|
|
char pin[128];
|
|
char pub[128];
|
|
|
|
CHECK(mkdtemp(base) != NULL);
|
|
snprintf(dir, sizeof(dir), "%s/state", base);
|
|
snprintf(other, sizeof(other), "%s/other", base);
|
|
snprintf(path, sizeof(path), "%s/identity.json", dir);
|
|
|
|
id = dc_identity_load_or_create(dir);
|
|
CHECK(id != NULL);
|
|
if (id == NULL)
|
|
return 1;
|
|
CHECK(dc_identity_pin(id) != NULL);
|
|
snprintf(pin, sizeof(pin), "%s", dc_identity_pin(id));
|
|
CHECK(strncmp(pin, "sha256:", 7) == 0);
|
|
CHECK(strlen(pin) == 7 + 64);
|
|
snprintf(pub, sizeof(pub), "%s", dc_identity_public_b64(id));
|
|
CHECK(strlen(pub) == 44);
|
|
CHECK(dc_identity_cert_pem(id) != NULL);
|
|
CHECK(strstr(dc_identity_cert_pem(id), "BEGIN CERTIFICATE") != NULL);
|
|
CHECK(dc_identity_key_pem(id) != NULL);
|
|
CHECK(strstr(dc_identity_key_pem(id), "BEGIN PRIVATE KEY") != NULL);
|
|
CHECK(access(path, F_OK) == 0);
|
|
mode_is(dir, 0700);
|
|
mode_is(path, 0600);
|
|
|
|
/* a restart keeps the identity and the pin */
|
|
dc_identity_free(id);
|
|
again = dc_identity_load_or_create(dir);
|
|
CHECK(again != NULL);
|
|
if (again != NULL) {
|
|
CHECK(strcmp(dc_identity_pin(again), pin) == 0);
|
|
CHECK(strcmp(dc_identity_public_b64(again), pub) == 0);
|
|
dc_identity_free(again);
|
|
}
|
|
|
|
/* a second state directory gets a different identity */
|
|
id = dc_identity_load_or_create(other);
|
|
CHECK(id != NULL);
|
|
if (id != NULL) {
|
|
CHECK(strcmp(dc_identity_pin(id), pin) != 0);
|
|
dc_identity_free(id);
|
|
}
|
|
|
|
/* a corrupt identity is never silently replaced */
|
|
{
|
|
FILE *f = fopen(path, "w");
|
|
|
|
CHECK(f != NULL);
|
|
if (f != NULL) {
|
|
fputs("{not json\n", f);
|
|
fclose(f);
|
|
}
|
|
CHECK(dc_identity_load_or_create(dir) == NULL);
|
|
snprintf(path, sizeof(path), "%s/identity.json.bad", dir);
|
|
CHECK(access(path, F_OK) == 0);
|
|
}
|
|
|
|
/* best effort cleanup */
|
|
unlink(path);
|
|
snprintf(path, sizeof(path), "%s/identity.json", dir);
|
|
unlink(path);
|
|
snprintf(path, sizeof(path), "%s/other/identity.json", base);
|
|
unlink(path);
|
|
snprintf(path, sizeof(path), "%s/other", base);
|
|
rmdir(path);
|
|
snprintf(path, sizeof(path), "%s", dir);
|
|
rmdir(path);
|
|
rmdir(base);
|
|
if (fails != 0) {
|
|
fprintf(stderr, "%d test(s) failed\n", fails);
|
|
return 1;
|
|
}
|
|
printf("test_identity: all ok\n");
|
|
return 0;
|
|
}
|