Files
breeze/tests/mesh_native_zi_test.c
waoziandClaude Opus 5.5 6f6d19f24d Rename account sync to Mesh
The codex-daochi-client-mesh-client-rename integration, committed as that
task staged it: mesh_client replaces daochi_client, account storage and
tests move to the Mesh names, and docs, locales and CI follow, with
existing accounts, encrypted records and local storage kept compatible.
The task stopped before its dependency versions were pushed; the next
commit moves Breeze to the pushed pins it needs.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-18 13:14:02 -03:00

182 lines
5.7 KiB
C

#include "storage/mesh_native.h"
#include "zir_string.h"
#include <assert.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
typedef MESH_FIXTURE_SESSION_TYPE MeshFixtureSession;
/* liboqs stands in for the signer: it checks the exact bytes each Mesh
* message signs and returns a recognizable signature. */
int32_t OQS_SIG_ml_dsa_44_sign(uint8_t *signature, size_t *signature_length,
const uint8_t *message, size_t message_length,
const uint8_t *private_key)
{
const char login[] = "daochi-sync-v1\nPOST\n/api/v1/sync/login\n";
const char registration[] = "daochi-device-registration-v1\n";
const char transaction[] = "daochi-tx-v1\n6\n";
if (message_length >= sizeof login - 1 &&
memcmp(message, login, sizeof login - 1) == 0) {
const size_t head = sizeof login - 1;
assert(message_length == head + 64 + 1 + 64 + 1);
assert(message[head + 64] == '\n');
assert(message[head + 129] == '\n');
memcpy(signature, message + head, 64);
memcpy(signature + 64, message + head + 65, 64);
} else if (message_length >= sizeof registration - 1 &&
memcmp(message, registration, sizeof registration - 1) == 0) {
assert(strstr((const char *)message, "\ninbe\n") != NULL);
assert(message[message_length - 1] == '\n');
} else {
assert(message_length >= sizeof transaction - 1);
assert(memcmp(message, transaction, sizeof transaction - 1) == 0);
assert(strstr((const char *)message, "\nPOST\n/api/v1/sync\n") != NULL);
assert(message[message_length - 1] == '\n');
}
for (size_t index = 0; index < 2560; ++index) {
assert(private_key[index] == 0xaa);
}
if (memcmp(message, login, sizeof login - 1) == 0) {
memset(signature + 128, 0xbb, 2420 - 128);
} else {
memset(signature, 0xbb, 2420);
}
*signature_length = 2420;
return 0;
}
void OQS_randombytes(uint8_t *output, size_t length)
{
static uint8_t next = 1;
assert(length == 32);
memset(output, next++, length);
}
int32_t OQS_SIG_ml_dsa_44_keypair(uint8_t *public_key, uint8_t *secret_key)
{
(void)public_key;
(void)secret_key;
return -1;
}
int32_t OQS_SIG_ml_dsa_44_verify(const uint8_t *message, size_t message_length,
const uint8_t *signature,
size_t signature_length,
const uint8_t *public_key)
{
(void)message;
(void)message_length;
(void)signature;
(void)signature_length;
(void)public_key;
return -1;
}
void OQS_MEM_cleanse(void *output, size_t length)
{
memset(output, 0, length);
}
/* Storage stays closed: no database and no stored account, so the account
* mode is the owner's own keys passed to each call. */
void *storage_db_handle(void)
{
return NULL;
}
String storage_get_setting_text(String key)
{
(void)key;
return (String){0};
}
bool storage_setting_present(String key)
{
(void)key;
return false;
}
void *sqlite3_db_mutex(void *database)
{
(void)database;
return NULL;
}
void sqlite3_mutex_enter(void *mutex)
{
(void)mutex;
}
void sqlite3_mutex_leave(void *mutex)
{
(void)mutex;
}
static bool sign_device(void *context, String message, Slice output)
{
assert(context == (void *)0x1);
assert(message.length > 200 && output.length >= 129);
assert(memcmp(message.data, "daochi-tx-v1\n6\n", 15) == 0);
memset(output.data, 'd', 128);
((char *)output.data)[128] = 0;
return true;
}
int main(int argc, char **argv)
{
assert(argc == 2);
AccountKeys account = {0};
memset(account.public_id, 'a', 64);
strcpy((char *)account.public_key_hex, "pub");
memset(account.private_key_hex, 'a', 5120);
MeshFixtureSession session = {0};
String base = StringView(argv[1], strlen(argv[1]));
AuthResult result = LoginAccount(base, &account,
StringLiteral("client-1"), &session);
assert(result == AuthResult_AUTH_OK);
assert(strcmp((char *)session.token, "token-from-server") == 0);
assert(session.expires_in_seconds == 1800);
assert(session.server_time == 1700000000);
session.expires_at_local = 2000;
char response[512];
int32_t status = 0;
result = RequestAccount(base, &account, StringLiteral("client-1"),
&session, 1000, StringLiteral("GET"),
StringLiteral("/api/v1/friends"), StringLiteral(""),
(Slice){.data = response, .length = sizeof response}, &status);
assert(result == AuthResult_AUTH_OK && status == 200);
assert(strcmp(response, "{\"friends\":[]}") == 0);
assert(strcmp((char *)session.token, "token-from-server") == 0);
char device_id[65];
memset(device_id, 'b', 64);
device_id[64] = 0;
Device device = {
.key_id = StringView(device_id, 64),
.public_key_hex = StringView(device_id, 64),
.signing_context = (void *)0x1,
.signer = sign_device,
};
result = SyncAccountPayload(base, &account, StringLiteral("client-1"),
&session, device, 1001, StringLiteral("{\"protocol_version\":6}"),
(Slice){.data = response, .length = sizeof response}, &status);
assert(result == AuthResult_AUTH_OK && status == 200);
assert(strcmp(response, "{\"server_version\":7}") == 0);
result = LoginAccount(StringLiteral("http://public.example.org"),
&account, StringLiteral("client-1"), &session);
assert(result == AuthResult_AUTH_INVALID_URL);
assert(session.token[0] == 0);
result = LoginAccount(base, &account,
StringLiteral("bad-client"), &session);
assert(result == AuthResult_AUTH_FAILED);
assert(session.token[0] == 0);
puts("Inbe Mesh Ziran native adapter passed");
return 0;
}