350 lines
15 KiB
C
350 lines
15 KiB
C
#include MESH_SYNC_HEADER
|
|
|
|
#include <assert.h>
|
|
#include <stdio.h>
|
|
#include <string.h>
|
|
|
|
typedef struct FakeServer {
|
|
int stage;
|
|
int account_signs;
|
|
int device_signs;
|
|
int nonces;
|
|
bool daochi;
|
|
} FakeServer;
|
|
|
|
static void assert_prefix(String message, const char *prefix)
|
|
{
|
|
size_t size = strlen(prefix);
|
|
assert(message.length >= size);
|
|
assert(memcmp(message.data, prefix, size) == 0);
|
|
}
|
|
|
|
static bool digest(void *closure, String body, Slice output)
|
|
{
|
|
(void)closure;
|
|
assert(body.length > 0 && output.length >= 65);
|
|
memset(output.data, 'f', 64);
|
|
((char *)output.data)[64] = 0;
|
|
return true;
|
|
}
|
|
|
|
static bool account_sign(void *closure, void *context, String message, Slice output)
|
|
{
|
|
(void)closure;
|
|
FakeServer *server = context;
|
|
++server->account_signs;
|
|
if (server->account_signs == 1) {
|
|
assert_prefix(message, server->daochi ? "daochi-sync-v1\nPOST\n/api/v1/sync/login\n"
|
|
: "mesh-sync-v1\nPOST\n/api/v1/sync/login\n");
|
|
} else if (server->account_signs == 2) {
|
|
assert_prefix(message, server->daochi ? "daochi-device-registration-v1\n"
|
|
: "mesh-device-registration-v1\n");
|
|
assert(strstr(message.data, "\ninbe\n") != NULL);
|
|
assert(strstr(message.data, "\n1700000300\n") != NULL);
|
|
} else {
|
|
assert(server->account_signs == 3 || server->account_signs == 4);
|
|
assert_prefix(message, server->daochi ? "daochi-tx-v1\n6\n" : "mesh-tx-v1\n6\n");
|
|
assert(strstr(message.data, "\nPOST\n/api/v1/sync\n") != NULL);
|
|
assert(strstr(message.data, "\n1700000300\n") != NULL);
|
|
}
|
|
assert(output.length >= 4841);
|
|
memset(output.data, 'a', 4840);
|
|
((char *)output.data)[4840] = 0;
|
|
return true;
|
|
}
|
|
|
|
static bool device_sign(void *closure, void *context, String message, Slice output)
|
|
{
|
|
(void)closure;
|
|
FakeServer *server = context;
|
|
++server->device_signs;
|
|
assert(server->device_signs <= 2);
|
|
assert_prefix(message, server->daochi ? "daochi-tx-v1\n6\n" : "mesh-tx-v1\n6\n");
|
|
assert(output.length >= 129);
|
|
memset(output.data, 'd', 128);
|
|
((char *)output.data)[128] = 0;
|
|
return true;
|
|
}
|
|
|
|
static bool nonce(void *closure, void *context, Slice output)
|
|
{
|
|
(void)closure;
|
|
FakeServer *server = context;
|
|
++server->nonces;
|
|
assert(server->nonces <= 5 && output.length >= 65);
|
|
memset(output.data, '0' + server->nonces, 64);
|
|
((char *)output.data)[64] = 0;
|
|
return true;
|
|
}
|
|
|
|
static bool send_request(void *closure, void *context, HttpRequest request,
|
|
Slice output, int32_t *status)
|
|
{
|
|
(void)closure;
|
|
FakeServer *server = context;
|
|
++server->stage;
|
|
assert(request.url.length < 1024);
|
|
assert(request.url.data[request.url.length] == 0);
|
|
switch (server->stage) {
|
|
case 1:
|
|
assert(strstr(request.url.data, "/api/v1/sync/challenge?") != NULL);
|
|
strcpy(output.data, "{\"nonce\":\"cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc\"}");
|
|
break;
|
|
case 2:
|
|
assert(strstr(request.url.data, "/api/v1/sync/login") != NULL);
|
|
assert(request.headers.length == 2);
|
|
strcpy(output.data, "{\"auth_token\":\"token\",\"expires_in_seconds\":1800,\"server_time\":1700000000}");
|
|
break;
|
|
case 3:
|
|
assert(strstr(request.url.data, "/api/v1/account/devices") != NULL);
|
|
assert(request.token.length == 5);
|
|
assert(strstr(request.body.data, "\"app_id\":\"inbe\"") != NULL);
|
|
assert(strstr(request.body.data, "\"nonce\":\"111111") != NULL);
|
|
assert(strstr(request.body.data, "\"expires_at\":1700000300") != NULL);
|
|
strcpy(output.data, "{}");
|
|
break;
|
|
case 4:
|
|
case 5: {
|
|
assert(strstr(request.url.data, "/api/v1/sync") != NULL);
|
|
assert(request.headers.length == 3);
|
|
const HttpHeader *headers = request.headers.data;
|
|
assert(headers[2].name.length == 9);
|
|
assert(memcmp(headers[2].name.data, "X-Mesh-Tx", 9) == 0);
|
|
assert(strstr(headers[2].value.data, "\"protocol_version\":6") != NULL);
|
|
assert(strstr(headers[2].value.data, server->daochi ? "\"signature_context\":\"daochi-tx-v1\""
|
|
: "\"signature_context\":\"mesh-tx-v1\"") != NULL);
|
|
assert(strstr(headers[2].value.data, server->stage == 4 ? "\"tx_id\":\"222222" : "\"tx_id\":\"444444") != NULL);
|
|
assert(strstr(headers[2].value.data, server->stage == 4 ? "\"nonce\":\"333333" : "\"nonce\":\"555555") != NULL);
|
|
assert(strstr(headers[2].value.data, "\"body_sha256\":\"ffffff") != NULL);
|
|
assert(strstr(headers[2].value.data, "\"device_signature\":\"dddddd") != NULL);
|
|
assert(request.body.length == 22);
|
|
assert(memcmp(request.body.data, "{\"protocol_version\":6}", 22) == 0);
|
|
strcpy(output.data, "{\"server_version\":7}");
|
|
break;
|
|
}
|
|
default:
|
|
assert(!"unexpected request");
|
|
}
|
|
*status = 200;
|
|
return true;
|
|
}
|
|
|
|
typedef struct HeaderPreparation {
|
|
char message[4096];
|
|
int account_signs;
|
|
int device_signs;
|
|
bool fail_device;
|
|
} HeaderPreparation;
|
|
|
|
static String expected_body;
|
|
|
|
static bool request_digest(void *closure, String body, Slice output)
|
|
{
|
|
(void)closure;
|
|
assert(body.length == expected_body.length);
|
|
assert(body.length == 0 || memcmp(body.data, expected_body.data, body.length) == 0);
|
|
assert(output.length >= 65);
|
|
memset(output.data, 'f', 64);
|
|
((char *)output.data)[64] = 0;
|
|
return true;
|
|
}
|
|
|
|
static bool request_account_sign(void *closure, void *context, String message, Slice output)
|
|
{
|
|
(void)closure;
|
|
HeaderPreparation *prepared = context;
|
|
assert(message.length < sizeof prepared->message);
|
|
memcpy(prepared->message, message.data, message.length);
|
|
prepared->message[message.length] = 0;
|
|
++prepared->account_signs;
|
|
assert(output.length >= 4841);
|
|
memset(output.data, 'a', 4840);
|
|
((char *)output.data)[4840] = 0;
|
|
return true;
|
|
}
|
|
|
|
static bool request_device_sign(void *closure, void *context, String message, Slice output)
|
|
{
|
|
(void)closure;
|
|
HeaderPreparation *prepared = context;
|
|
assert(message.length == strlen(prepared->message));
|
|
assert(memcmp(message.data, prepared->message, message.length) == 0);
|
|
++prepared->device_signs;
|
|
if (prepared->fail_device) return false;
|
|
assert(output.length >= 129);
|
|
memset(output.data, 'd', 128);
|
|
((char *)output.data)[128] = 0;
|
|
return true;
|
|
}
|
|
|
|
static bool request_nonce(void *closure, void *context, Slice output)
|
|
{
|
|
(void)closure;
|
|
(void)context;
|
|
assert(output.length >= 65);
|
|
memset(output.data, 'b', 64);
|
|
((char *)output.data)[64] = 0;
|
|
return true;
|
|
}
|
|
|
|
static void check_request_preparation(Client client, Device device)
|
|
{
|
|
HeaderPreparation prepared = {0};
|
|
client.identity.signing_context = &prepared;
|
|
client.signer = (SignMessage){.call = request_account_sign};
|
|
client.digest = (DigestBody){.call = request_digest};
|
|
device.signing_context = &prepared;
|
|
device.signer = (SignMessage){.call = request_device_sign};
|
|
Session session = {.server_time = 1700000000, .login_local_time = 1000};
|
|
char output[8192], legacy[8192];
|
|
Slice buffer = {.data = output, .length = sizeof output};
|
|
const char binary[] = {'\0', '\xff', 'x', '\0'};
|
|
const char *methods[] = {"GET", "HEAD", "PUT", "POST"};
|
|
for (size_t i = 0; i < sizeof methods / sizeof *methods; ++i) {
|
|
expected_body = i < 2 ? StringLiteral("") : StringView(binary, sizeof binary);
|
|
String method = StringView(methods[i], strlen(methods[i]));
|
|
String path = StringLiteral("/api/v1/blobs/photos/abcdef?part=1");
|
|
assert(MESH_SYNC_SYMBOL(PrepareRequestHeader)(client, &session, device, 1007, NULL, (RandomNonce){.call = request_nonce},
|
|
method, path, expected_body, buffer) == AuthResult_AUTH_OK);
|
|
char expected[128];
|
|
snprintf(expected, sizeof expected, "\n%s\n/api/v1/blobs/photos/abcdef?part=1\n", methods[i]);
|
|
assert(strstr(prepared.message, expected));
|
|
assert(strstr(prepared.message, "\n1700000307\n"));
|
|
assert(strstr(output, "\"expires_at\":1700000307"));
|
|
}
|
|
|
|
expected_body = StringLiteral("{\"protocol_version\":6}");
|
|
assert(MESH_SYNC_SYMBOL(PrepareSyncHeader)(client, &session, device, 1007, NULL, (RandomNonce){.call = request_nonce},
|
|
expected_body, buffer) == AuthResult_AUTH_OK);
|
|
strcpy(legacy, output);
|
|
assert(MESH_SYNC_SYMBOL(PrepareRequestHeader)(client, &session, device, 1007, NULL, (RandomNonce){.call = request_nonce},
|
|
StringLiteral("POST"), StringLiteral("/api/v1/sync"), expected_body,
|
|
buffer) == AuthResult_AUTH_OK);
|
|
assert(strcmp(legacy, output) == 0);
|
|
|
|
char path[2049];
|
|
memset(path, 'x', sizeof path - 1);
|
|
path[0] = '/';
|
|
path[sizeof path - 1] = 0;
|
|
assert(MESH_SYNC_SYMBOL(PrepareRequestHeader)(client, &session, device, 1007, NULL, (RandomNonce){.call = request_nonce},
|
|
StringLiteral("PUT"), StringView(path, sizeof path - 1), expected_body,
|
|
buffer) == AuthResult_AUTH_OK);
|
|
|
|
int signs = prepared.account_signs;
|
|
assert(MESH_SYNC_SYMBOL(PrepareRequestHeader)(client, &session, device, 1007, NULL, (RandomNonce){.call = request_nonce},
|
|
StringLiteral("PUT"), StringLiteral("/bad\npath"), expected_body,
|
|
buffer) == AuthResult_AUTH_PAYLOAD_FAILED);
|
|
assert(output[0] == 0 && prepared.account_signs == signs);
|
|
assert(MESH_SYNC_SYMBOL(PrepareRequestHeader)(client, NULL, device, 1007, NULL, (RandomNonce){.call = request_nonce},
|
|
StringLiteral("PUT"), StringLiteral("/valid"), expected_body,
|
|
buffer) == AuthResult_AUTH_PAYLOAD_FAILED);
|
|
assert(output[0] == 0 && prepared.account_signs == signs);
|
|
assert(MESH_SYNC_SYMBOL(PrepareRequestHeader)(client, &session, device, 1007, NULL, (RandomNonce){0},
|
|
StringLiteral("PUT"), StringLiteral("/valid"), expected_body,
|
|
buffer) == AuthResult_AUTH_PAYLOAD_FAILED);
|
|
|
|
char hex[65];
|
|
memset(hex, 'c', 64);
|
|
hex[64] = 0;
|
|
SyncTransaction transaction = {
|
|
.account_id = client.identity.account_id,
|
|
.app_id = client.app_id,
|
|
.device_key_id = device.key_id,
|
|
.tx_id = StringView(hex, 64),
|
|
.nonce = StringView(hex, 64),
|
|
.body_sha256_hex = StringView(hex, 64),
|
|
.expires_at = 2000000000,
|
|
};
|
|
assert(MESH_SYNC_SYMBOL(SignTransactionHeader)(client, device, transaction, StringLiteral("HEAD"),
|
|
StringLiteral("/api/v1/blobs/photos/abcdef"), buffer, SignedProtocolMesh) == AuthResult_AUTH_OK);
|
|
assert(strstr(output, "\"expires_at\":2000000000"));
|
|
signs = prepared.account_signs;
|
|
assert(MESH_SYNC_SYMBOL(SignTransactionHeader)(client, device, transaction, StringLiteral("HEAD"),
|
|
StringLiteral("/valid"), buffer, (SignedProtocol)99) == AuthResult_AUTH_PAYLOAD_FAILED);
|
|
assert(output[0] == 0 && prepared.account_signs == signs);
|
|
output[0] = 'x';
|
|
assert(MESH_SYNC_SYMBOL(PrepareDeviceRegistration)(client, &session, device, 1007,
|
|
NULL, (RandomNonce){0}, buffer, (SignedProtocol)99) == AuthResult_AUTH_PAYLOAD_FAILED);
|
|
assert(output[0] == 0 && prepared.account_signs == signs);
|
|
SyncTransaction wrong = transaction;
|
|
wrong.account_id = StringView(hex, 64);
|
|
assert(MESH_SYNC_SYMBOL(SignTransactionHeader)(client, device, wrong, StringLiteral("HEAD"),
|
|
StringLiteral("/valid"), buffer, SignedProtocolMesh) == AuthResult_AUTH_PAYLOAD_FAILED);
|
|
wrong = transaction;
|
|
wrong.app_id = StringLiteral("another-app");
|
|
assert(MESH_SYNC_SYMBOL(SignTransactionHeader)(client, device, wrong, StringLiteral("HEAD"),
|
|
StringLiteral("/valid"), buffer, SignedProtocolMesh) == AuthResult_AUTH_PAYLOAD_FAILED);
|
|
wrong = transaction;
|
|
wrong.device_key_id = StringView(hex, 64);
|
|
assert(MESH_SYNC_SYMBOL(SignTransactionHeader)(client, device, wrong, StringLiteral("HEAD"),
|
|
StringLiteral("/valid"), buffer, SignedProtocolMesh) == AuthResult_AUTH_PAYLOAD_FAILED);
|
|
assert(output[0] == 0 && prepared.account_signs == signs);
|
|
prepared.fail_device = true;
|
|
assert(MESH_SYNC_SYMBOL(SignTransactionHeader)(client, device, transaction, StringLiteral("HEAD"),
|
|
StringLiteral("/valid"), buffer, SignedProtocolMesh) == AuthResult_AUTH_SIGN_FAILED);
|
|
assert(output[0] == 0);
|
|
prepared.fail_device = false;
|
|
Slice tiny = {.data = output, .length = 16};
|
|
assert(MESH_SYNC_SYMBOL(SignTransactionHeader)(client, device, transaction, StringLiteral("HEAD"),
|
|
StringLiteral("/valid"), tiny, SignedProtocolMesh) == AuthResult_AUTH_PAYLOAD_FAILED);
|
|
assert(output[0] == 0);
|
|
device.signer = (SignMessage){0};
|
|
assert(MESH_SYNC_SYMBOL(SignTransactionHeader)(client, device, transaction, StringLiteral("HEAD"),
|
|
StringLiteral("/valid"), buffer, SignedProtocolMesh) == AuthResult_AUTH_PAYLOAD_FAILED);
|
|
assert(output[0] == 0);
|
|
}
|
|
|
|
static void check_signed_sync(SignedProtocol protocol)
|
|
{
|
|
FakeServer server = {.daochi = protocol == SignedProtocolDaochi};
|
|
char account[65], device_key[65];
|
|
memset(account, 'a', 64);
|
|
account[64] = 0;
|
|
memset(device_key, 'b', 64);
|
|
device_key[64] = 0;
|
|
Client client = {0};
|
|
client.signed_protocol = protocol;
|
|
client.server_url = StringLiteral("http://127.0.0.1:8080");
|
|
client.app_id = StringLiteral("inbe");
|
|
client.identity.account_id = StringView(account, 64);
|
|
client.identity.public_key = StringLiteral("public-key");
|
|
client.identity.client_id = StringLiteral("client-1");
|
|
client.identity.signing_context = &server;
|
|
client.digest = (DigestBody){.call = digest};
|
|
client.signer = (SignMessage){.call = account_sign};
|
|
client.send = (SendRequest){.call = send_request};
|
|
client.transport_context = &server;
|
|
Device device = {
|
|
.key_id = StringView(device_key, 64),
|
|
.public_key_hex = StringView(device_key, 64),
|
|
.signing_context = &server,
|
|
.signer = (SignMessage){.call = device_sign},
|
|
};
|
|
Session session = {0};
|
|
char response[256];
|
|
int32_t status = 0;
|
|
AuthResult result = Sync(client, &session, device, 1000, &server, (RandomNonce){.call = nonce},
|
|
StringLiteral("{\"protocol_version\":6}"),
|
|
(Slice){.data = response, .length = sizeof response}, &status);
|
|
assert(result == AuthResult_AUTH_OK);
|
|
assert(status == 200 && server.stage == 4);
|
|
assert(server.account_signs == 3 && server.device_signs == 1);
|
|
assert(server.nonces == 3);
|
|
assert(strcmp(response, "{\"server_version\":7}") == 0);
|
|
result = Sync(client, &session, device, 1000, &server, (RandomNonce){.call = nonce},
|
|
StringLiteral("{\"protocol_version\":6}"),
|
|
(Slice){.data = response, .length = sizeof response}, &status);
|
|
assert(result == AuthResult_AUTH_OK && status == 200 && server.stage == 5);
|
|
assert(server.account_signs == 4 && server.device_signs == 2 && server.nonces == 5);
|
|
if (protocol == SignedProtocolMesh)
|
|
check_request_preparation(client, device);
|
|
}
|
|
|
|
int main(void)
|
|
{
|
|
check_signed_sync(SignedProtocolMesh);
|
|
check_signed_sync(SignedProtocolDaochi);
|
|
puts("Mesh Ziran signed sync passed");
|
|
return 0;
|
|
}
|