2021-01-02 13:10:00 -05:00
|
|
|
#include "ssb.h"
|
|
|
|
|
2023-03-07 12:50:17 -05:00
|
|
|
#include "log.h"
|
2022-06-04 13:04:51 -04:00
|
|
|
#include "mem.h"
|
2021-08-22 15:41:27 -04:00
|
|
|
#include "ssb.db.h"
|
2021-10-24 11:46:30 -04:00
|
|
|
#include "ssb.js.h"
|
2021-09-06 14:23:22 -04:00
|
|
|
#include "tests.h"
|
2023-07-20 01:06:15 -04:00
|
|
|
#include "trace.h"
|
2022-01-22 15:47:10 -05:00
|
|
|
#include "util.js.h"
|
2021-08-22 15:41:27 -04:00
|
|
|
|
2023-05-21 17:36:51 -04:00
|
|
|
#include "uv.h"
|
|
|
|
|
2021-01-02 13:10:00 -05:00
|
|
|
#include <assert.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2022-12-28 16:03:23 -05:00
|
|
|
#include <time.h>
|
2023-07-19 21:02:50 -04:00
|
|
|
#include <unistd.h>
|
2021-01-02 13:10:00 -05:00
|
|
|
|
2024-07-14 16:59:23 -04:00
|
|
|
#if !defined(_WIN32)
|
|
|
|
#include <sys/wait.h>
|
|
|
|
#endif
|
|
|
|
|
2024-06-16 17:08:10 -04:00
|
|
|
#if defined(_WIN32)
|
|
|
|
#define WIFEXITED(x) 1
|
|
|
|
#define WEXITSTATUS(x) (x)
|
|
|
|
#endif
|
|
|
|
|
2021-09-06 14:23:22 -04:00
|
|
|
void tf_ssb_test_id_conversion(const tf_test_options_t* options)
|
2021-01-02 13:10:00 -05:00
|
|
|
{
|
2023-03-07 12:50:17 -05:00
|
|
|
tf_printf("Testing id conversion.\n");
|
2021-01-02 13:10:00 -05:00
|
|
|
uint8_t bin[k_id_bin_len] = { 0 };
|
|
|
|
char str[k_id_base64_len] = { 0 };
|
|
|
|
const char* k_id = "@bzRTe6hgOII2yZ1keGGoNoQgostjQc830trHc453crY=.ed25519";
|
|
|
|
(void)bin;
|
|
|
|
(void)str;
|
|
|
|
(void)k_id;
|
2022-05-20 21:38:13 -04:00
|
|
|
bool b = tf_ssb_id_str_to_bin(bin, k_id);
|
|
|
|
(void)b;
|
|
|
|
assert(b);
|
|
|
|
b = tf_ssb_id_bin_to_str(str, sizeof(str), bin);
|
|
|
|
assert(b);
|
|
|
|
b = strcmp(str, k_id) == 0;
|
|
|
|
assert(b);
|
2021-01-02 13:10:00 -05:00
|
|
|
}
|
|
|
|
|
2024-02-15 18:35:01 -05:00
|
|
|
typedef struct _test_t
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
tf_ssb_t* ssb0;
|
|
|
|
tf_ssb_t* ssb1;
|
2022-11-02 19:34:44 -04:00
|
|
|
tf_ssb_t* ssb2;
|
2021-01-02 13:10:00 -05:00
|
|
|
int connection_count0;
|
|
|
|
int connection_count1;
|
2022-11-02 19:34:44 -04:00
|
|
|
int connection_count2;
|
|
|
|
int broadcast_count0;
|
|
|
|
int broadcast_count1;
|
|
|
|
int broadcast_count2;
|
2021-01-02 13:10:00 -05:00
|
|
|
} test_t;
|
|
|
|
|
|
|
|
static void _ssb_test_connections_changed(tf_ssb_t* ssb, tf_ssb_change_t change, tf_ssb_connection_t* connection, void* user_data)
|
|
|
|
{
|
|
|
|
test_t* test = user_data;
|
|
|
|
|
|
|
|
int count = 0;
|
|
|
|
const char** c = tf_ssb_get_connection_ids(ssb);
|
2021-10-10 17:51:38 -04:00
|
|
|
for (const char** p = c; *p; p++)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
count++;
|
|
|
|
}
|
2022-06-04 13:04:51 -04:00
|
|
|
tf_free(c);
|
2021-01-02 13:10:00 -05:00
|
|
|
|
2021-10-10 17:51:38 -04:00
|
|
|
if (ssb == test->ssb0)
|
|
|
|
{
|
2023-03-07 12:50:17 -05:00
|
|
|
tf_printf("callback0 change=%d connection=%p\n", change, connection);
|
2021-01-02 13:10:00 -05:00
|
|
|
test->connection_count0 = count;
|
2021-10-10 17:51:38 -04:00
|
|
|
}
|
|
|
|
else if (ssb == test->ssb1)
|
|
|
|
{
|
2023-03-07 12:50:17 -05:00
|
|
|
tf_printf("callback1 change=%d connection=%p\n", change, connection);
|
2021-01-02 13:10:00 -05:00
|
|
|
test->connection_count1 = count;
|
|
|
|
}
|
2022-11-02 19:34:44 -04:00
|
|
|
else if (ssb == test->ssb2)
|
|
|
|
{
|
2023-03-07 12:50:17 -05:00
|
|
|
tf_printf("callback2 change=%d connection=%p\n", change, connection);
|
2022-11-02 19:34:44 -04:00
|
|
|
test->connection_count2 = count;
|
|
|
|
}
|
2023-03-07 12:50:17 -05:00
|
|
|
tf_printf("conns = %d %d %d\n", test->connection_count0, test->connection_count1, test->connection_count2);
|
2021-01-02 13:10:00 -05:00
|
|
|
}
|
|
|
|
|
2022-12-31 13:59:29 -05:00
|
|
|
typedef struct _count_messages_t
|
|
|
|
{
|
|
|
|
tf_ssb_t* ssb;
|
|
|
|
int count;
|
|
|
|
} count_messages_t;
|
|
|
|
|
2021-01-02 13:10:00 -05:00
|
|
|
static void _count_messages_callback(JSValue row, void* user_data)
|
|
|
|
{
|
2022-12-31 13:59:29 -05:00
|
|
|
count_messages_t* count = user_data;
|
|
|
|
JSContext* context = tf_ssb_get_context(count->ssb);
|
|
|
|
JS_ToInt32(context, &count->count, JS_GetPropertyStr(context, row, "count"));
|
2021-01-02 13:10:00 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static int _ssb_test_count_messages(tf_ssb_t* ssb)
|
|
|
|
{
|
2022-12-31 13:59:29 -05:00
|
|
|
count_messages_t count = { .ssb = ssb };
|
|
|
|
tf_ssb_db_visit_query(ssb, "SELECT COUNT(*) AS count FROM messages", JS_UNDEFINED, _count_messages_callback, &count);
|
|
|
|
return count.count;
|
2021-01-02 13:10:00 -05:00
|
|
|
}
|
|
|
|
|
2021-11-17 18:47:55 -05:00
|
|
|
static void _message_added(tf_ssb_t* ssb, const char* id, void* user_data)
|
|
|
|
{
|
|
|
|
++*(int*)user_data;
|
|
|
|
}
|
|
|
|
|
2022-01-22 15:47:10 -05:00
|
|
|
static void _ssb_test_idle(uv_idle_t* idle)
|
|
|
|
{
|
|
|
|
tf_ssb_t* ssb = idle->data;
|
|
|
|
JSRuntime* runtime = JS_GetRuntime(tf_ssb_get_context(ssb));
|
|
|
|
while (JS_IsJobPending(runtime))
|
|
|
|
{
|
|
|
|
JSContext* context = NULL;
|
|
|
|
int r = JS_ExecutePendingJob(runtime, &context);
|
|
|
|
JSValue result = JS_GetException(context);
|
|
|
|
if (context)
|
|
|
|
{
|
|
|
|
tf_util_report_error(context, result);
|
|
|
|
}
|
2023-03-19 20:29:46 -04:00
|
|
|
if (r == 0)
|
2022-01-22 15:47:10 -05:00
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-19 21:02:50 -04:00
|
|
|
static void _message_stored(const char* id, bool verified, bool is_new, void* user_data)
|
|
|
|
{
|
|
|
|
*(bool*)user_data = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void _wait_stored(tf_ssb_t* ssb, bool* stored)
|
|
|
|
{
|
|
|
|
while (!*stored)
|
|
|
|
{
|
|
|
|
uv_run(tf_ssb_get_loop(ssb), UV_RUN_ONCE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-06 14:23:22 -04:00
|
|
|
void tf_ssb_test_ssb(const tf_test_options_t* options)
|
2021-01-02 13:10:00 -05:00
|
|
|
{
|
2023-03-07 12:50:17 -05:00
|
|
|
tf_printf("Testing SSB.\n");
|
2021-01-02 13:10:00 -05:00
|
|
|
|
|
|
|
uv_loop_t loop = { 0 };
|
|
|
|
uv_loop_init(&loop);
|
|
|
|
|
2023-07-19 21:02:50 -04:00
|
|
|
unlink("out/test_db0.sqlite");
|
2024-03-02 15:01:09 -05:00
|
|
|
tf_ssb_t* ssb0 = tf_ssb_create(&loop, NULL, "file:out/test_db0.sqlite", NULL);
|
2021-10-24 11:46:30 -04:00
|
|
|
tf_ssb_register(tf_ssb_get_context(ssb0), ssb0);
|
2023-07-19 21:02:50 -04:00
|
|
|
unlink("out/test_db1.sqlite");
|
2024-03-02 15:01:09 -05:00
|
|
|
tf_ssb_t* ssb1 = tf_ssb_create(&loop, NULL, "file:out/test_db1.sqlite", NULL);
|
2021-10-24 11:46:30 -04:00
|
|
|
tf_ssb_register(tf_ssb_get_context(ssb1), ssb1);
|
2021-01-02 13:10:00 -05:00
|
|
|
|
2022-01-22 15:47:10 -05:00
|
|
|
uv_idle_t idle0 = { .data = ssb0 };
|
|
|
|
uv_idle_init(&loop, &idle0);
|
|
|
|
uv_idle_start(&idle0, _ssb_test_idle);
|
|
|
|
|
|
|
|
uv_idle_t idle1 = { .data = ssb1 };
|
|
|
|
uv_idle_init(&loop, &idle1);
|
|
|
|
uv_idle_start(&idle1, _ssb_test_idle);
|
|
|
|
|
2021-01-02 13:10:00 -05:00
|
|
|
test_t test = {
|
|
|
|
.ssb0 = ssb0,
|
|
|
|
.ssb1 = ssb1,
|
|
|
|
};
|
|
|
|
|
2021-09-06 13:50:38 -04:00
|
|
|
tf_ssb_add_connections_changed_callback(ssb0, _ssb_test_connections_changed, NULL, &test);
|
|
|
|
tf_ssb_add_connections_changed_callback(ssb1, _ssb_test_connections_changed, NULL, &test);
|
2021-01-02 13:10:00 -05:00
|
|
|
|
|
|
|
tf_ssb_generate_keys(ssb0);
|
|
|
|
tf_ssb_generate_keys(ssb1);
|
|
|
|
|
2023-06-01 18:53:44 -04:00
|
|
|
uint8_t priv0[512] = { 0 };
|
|
|
|
uint8_t priv1[512] = { 0 };
|
|
|
|
tf_ssb_get_private_key(ssb0, priv0, sizeof(priv0));
|
|
|
|
tf_ssb_get_private_key(ssb1, priv1, sizeof(priv1));
|
|
|
|
|
2021-01-02 13:10:00 -05:00
|
|
|
char id0[k_id_base64_len] = { 0 };
|
|
|
|
char id1[k_id_base64_len] = { 0 };
|
|
|
|
bool b = tf_ssb_whoami(ssb0, id0, sizeof(id0));
|
|
|
|
(void)b;
|
|
|
|
assert(b);
|
2021-09-08 20:15:57 -04:00
|
|
|
b = tf_ssb_whoami(ssb1, id1, sizeof(id1));
|
2021-01-02 13:10:00 -05:00
|
|
|
assert(b);
|
2023-03-07 12:50:17 -05:00
|
|
|
tf_printf("ID %s and %s\n", id0, id1);
|
2021-01-02 13:10:00 -05:00
|
|
|
|
|
|
|
char blob_id[k_id_base64_len] = { 0 };
|
|
|
|
const char* k_blob = "Hello, blob!";
|
2022-10-12 08:27:32 -04:00
|
|
|
b = tf_ssb_db_blob_store(ssb0, (const uint8_t*)k_blob, strlen(k_blob), blob_id, sizeof(blob_id), NULL);
|
2021-01-02 13:10:00 -05:00
|
|
|
assert(b);
|
|
|
|
|
2023-06-01 18:53:44 -04:00
|
|
|
JSContext* context0 = tf_ssb_get_context(ssb0);
|
|
|
|
JSValue obj = JS_NewObject(context0);
|
|
|
|
JS_SetPropertyStr(context0, obj, "type", JS_NewString(context0, "post"));
|
|
|
|
JS_SetPropertyStr(context0, obj, "text", JS_NewString(context0, "Hello, world!"));
|
2023-07-19 21:02:50 -04:00
|
|
|
bool stored = false;
|
2024-06-16 07:51:06 -04:00
|
|
|
JSValue signed_message = tf_ssb_sign_message(ssb0, id0, priv0, obj, NULL, 0);
|
2023-07-19 21:02:50 -04:00
|
|
|
tf_ssb_verify_strip_and_store_message(ssb0, signed_message, _message_stored, &stored);
|
|
|
|
JS_FreeValue(context0, signed_message);
|
|
|
|
_wait_stored(ssb0, &stored);
|
2023-06-01 18:53:44 -04:00
|
|
|
JS_FreeValue(context0, obj);
|
|
|
|
|
|
|
|
obj = JS_NewObject(context0);
|
|
|
|
JS_SetPropertyStr(context0, obj, "type", JS_NewString(context0, "post"));
|
|
|
|
JS_SetPropertyStr(context0, obj, "text", JS_NewString(context0, "First post."));
|
2023-07-19 21:02:50 -04:00
|
|
|
stored = false;
|
2024-06-16 07:51:06 -04:00
|
|
|
signed_message = tf_ssb_sign_message(ssb0, id0, priv0, obj, NULL, 0);
|
2023-07-19 21:02:50 -04:00
|
|
|
tf_ssb_verify_strip_and_store_message(ssb0, signed_message, _message_stored, &stored);
|
|
|
|
JS_FreeValue(context0, signed_message);
|
|
|
|
_wait_stored(ssb0, &stored);
|
2023-06-01 18:53:44 -04:00
|
|
|
JS_FreeValue(context0, obj);
|
|
|
|
|
|
|
|
obj = JS_NewObject(context0);
|
|
|
|
JS_SetPropertyStr(context0, obj, "type", JS_NewString(context0, "post"));
|
|
|
|
JS_SetPropertyStr(context0, obj, "text", JS_NewString(context0, "First post."));
|
|
|
|
JSValue mentions = JS_NewArray(context0);
|
|
|
|
JSValue mention = JS_NewObject(context0);
|
|
|
|
JS_SetPropertyStr(context0, mention, "link", JS_NewString(context0, blob_id));
|
|
|
|
JS_SetPropertyUint32(context0, mentions, 0, mention);
|
|
|
|
JS_SetPropertyStr(context0, obj, "mentions", mentions);
|
2023-07-19 21:02:50 -04:00
|
|
|
stored = false;
|
2024-06-16 07:51:06 -04:00
|
|
|
signed_message = tf_ssb_sign_message(ssb0, id0, priv0, obj, NULL, 0);
|
2023-07-19 21:02:50 -04:00
|
|
|
tf_ssb_verify_strip_and_store_message(ssb0, signed_message, _message_stored, &stored);
|
|
|
|
JS_FreeValue(context0, signed_message);
|
|
|
|
_wait_stored(ssb0, &stored);
|
2023-06-01 18:53:44 -04:00
|
|
|
JS_FreeValue(context0, obj);
|
2021-01-02 13:10:00 -05:00
|
|
|
|
2022-11-08 21:56:41 -05:00
|
|
|
uint8_t* b0;
|
|
|
|
size_t s0 = 0;
|
|
|
|
b = tf_ssb_db_blob_get(ssb0, blob_id, &b0, &s0);
|
2022-05-20 21:38:13 -04:00
|
|
|
assert(b);
|
2022-11-08 21:56:41 -05:00
|
|
|
assert(s0 == strlen(k_blob));
|
|
|
|
assert(memcmp(b0, k_blob, strlen(k_blob)) == 0);
|
|
|
|
tf_free(b0);
|
2022-05-20 21:38:13 -04:00
|
|
|
b = !tf_ssb_db_blob_get(ssb1, blob_id, NULL, NULL);
|
|
|
|
assert(b);
|
2021-01-02 13:10:00 -05:00
|
|
|
tf_ssb_server_open(ssb0, 12347);
|
|
|
|
|
|
|
|
uint8_t id0bin[k_id_bin_len];
|
|
|
|
tf_ssb_id_str_to_bin(id0bin, id0);
|
|
|
|
tf_ssb_connect(ssb1, "127.0.0.1", 12347, id0bin);
|
|
|
|
|
2023-03-07 12:50:17 -05:00
|
|
|
tf_printf("Waiting for connection.\n");
|
2024-02-15 18:35:01 -05:00
|
|
|
while (test.connection_count0 != 1 || test.connection_count1 != 1)
|
2021-10-10 17:51:38 -04:00
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
uv_run(&loop, UV_RUN_ONCE);
|
|
|
|
}
|
|
|
|
tf_ssb_server_close(ssb0);
|
|
|
|
|
2023-03-07 12:50:17 -05:00
|
|
|
tf_printf("Waiting for messages.\n");
|
2021-10-10 17:51:38 -04:00
|
|
|
while (_ssb_test_count_messages(ssb1) < 3)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
uv_run(&loop, UV_RUN_ONCE);
|
|
|
|
}
|
|
|
|
|
2023-03-07 12:50:17 -05:00
|
|
|
tf_printf("Waiting for blob.\n");
|
2021-10-10 17:51:38 -04:00
|
|
|
while (!tf_ssb_db_blob_get(ssb1, blob_id, NULL, NULL))
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
uv_run(&loop, UV_RUN_ONCE);
|
|
|
|
}
|
2021-11-17 18:47:55 -05:00
|
|
|
|
2022-11-08 21:56:41 -05:00
|
|
|
uint8_t* b1;
|
|
|
|
size_t s1 = 0;
|
|
|
|
b = tf_ssb_db_blob_get(ssb1, blob_id, &b1, &s1);
|
|
|
|
assert(b);
|
2023-03-07 12:50:17 -05:00
|
|
|
tf_printf("s1 = %zd sl = %zd\n", s1, strlen(k_blob));
|
2022-11-08 21:56:41 -05:00
|
|
|
assert(s1 == strlen(k_blob));
|
|
|
|
assert(memcmp(b1, k_blob, strlen(k_blob)) == 0);
|
|
|
|
tf_free(b1);
|
|
|
|
|
2023-03-07 12:50:17 -05:00
|
|
|
tf_printf("Waiting for message to self.\n");
|
2021-11-17 18:47:55 -05:00
|
|
|
int count0 = 0;
|
|
|
|
int count1 = 0;
|
|
|
|
tf_ssb_add_message_added_callback(ssb0, _message_added, NULL, &count0);
|
|
|
|
tf_ssb_add_message_added_callback(ssb1, _message_added, NULL, &count1);
|
2023-06-01 18:53:44 -04:00
|
|
|
|
|
|
|
obj = JS_NewObject(context0);
|
|
|
|
JS_SetPropertyStr(context0, obj, "type", JS_NewString(context0, "post"));
|
|
|
|
JS_SetPropertyStr(context0, obj, "text", JS_NewString(context0, "Message to self."));
|
2023-07-19 21:02:50 -04:00
|
|
|
stored = false;
|
2024-06-16 07:51:06 -04:00
|
|
|
signed_message = tf_ssb_sign_message(ssb0, id0, priv0, obj, NULL, 0);
|
2023-07-19 21:02:50 -04:00
|
|
|
tf_ssb_verify_strip_and_store_message(ssb0, signed_message, _message_stored, &stored);
|
|
|
|
JS_FreeValue(context0, signed_message);
|
|
|
|
_wait_stored(ssb0, &stored);
|
2023-06-01 18:53:44 -04:00
|
|
|
JS_FreeValue(context0, obj);
|
|
|
|
|
2021-11-17 18:47:55 -05:00
|
|
|
while (count0 == 0)
|
|
|
|
{
|
|
|
|
uv_run(&loop, UV_RUN_ONCE);
|
|
|
|
}
|
|
|
|
tf_ssb_remove_message_added_callback(ssb0, _message_added, &count0);
|
|
|
|
|
2023-03-07 12:50:17 -05:00
|
|
|
tf_printf("Waiting for message from other.\n");
|
2021-11-17 18:47:55 -05:00
|
|
|
while (count1 == 0)
|
|
|
|
{
|
|
|
|
uv_run(&loop, UV_RUN_ONCE);
|
|
|
|
}
|
|
|
|
tf_ssb_remove_message_added_callback(ssb1, _message_added, &count1);
|
2023-03-07 12:50:17 -05:00
|
|
|
tf_printf("done\n");
|
2021-01-02 13:10:00 -05:00
|
|
|
|
|
|
|
tf_ssb_send_close(ssb1);
|
|
|
|
|
2022-01-22 15:47:10 -05:00
|
|
|
uv_close((uv_handle_t*)&idle0, NULL);
|
|
|
|
uv_close((uv_handle_t*)&idle1, NULL);
|
|
|
|
|
2024-01-27 09:44:17 -05:00
|
|
|
tf_printf("final run\n");
|
2021-01-02 13:10:00 -05:00
|
|
|
uv_run(&loop, UV_RUN_DEFAULT);
|
2024-01-27 09:44:17 -05:00
|
|
|
tf_printf("done\n");
|
2021-01-02 13:10:00 -05:00
|
|
|
|
2024-01-27 09:44:17 -05:00
|
|
|
tf_printf("destroy 0\n");
|
2021-01-02 13:10:00 -05:00
|
|
|
tf_ssb_destroy(ssb0);
|
2024-01-27 09:44:17 -05:00
|
|
|
tf_printf("destroy 1\n");
|
2021-01-02 13:10:00 -05:00
|
|
|
tf_ssb_destroy(ssb1);
|
|
|
|
|
2024-01-27 09:44:17 -05:00
|
|
|
tf_printf("close\n");
|
2021-01-02 13:10:00 -05:00
|
|
|
uv_loop_close(&loop);
|
|
|
|
}
|
|
|
|
|
2024-08-14 19:23:01 -04:00
|
|
|
static void _broadcasts_visit(const char* host, const struct sockaddr_in* addr, tf_ssb_broadcast_origin_t origin, tf_ssb_connection_t* tunnel, const uint8_t* pub, void* user_data)
|
2022-11-02 19:34:44 -04:00
|
|
|
{
|
|
|
|
int* count = user_data;
|
|
|
|
(*count)++;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void _broadcasts_changed(tf_ssb_t* ssb, void* user_data)
|
|
|
|
{
|
|
|
|
int* count = NULL;
|
|
|
|
test_t* test = user_data;
|
|
|
|
if (ssb == test->ssb0)
|
|
|
|
{
|
|
|
|
count = &test->broadcast_count0;
|
|
|
|
}
|
|
|
|
else if (ssb == test->ssb1)
|
|
|
|
{
|
|
|
|
count = &test->broadcast_count1;
|
|
|
|
}
|
|
|
|
else if (ssb == test->ssb2)
|
|
|
|
{
|
|
|
|
count = &test->broadcast_count2;
|
|
|
|
}
|
2023-08-25 16:57:55 -04:00
|
|
|
if (count)
|
|
|
|
{
|
|
|
|
*count = 0;
|
|
|
|
}
|
2022-11-02 19:34:44 -04:00
|
|
|
tf_ssb_visit_broadcasts(ssb, _broadcasts_visit, count);
|
2023-03-07 12:50:17 -05:00
|
|
|
tf_printf("BROADCASTS %d %d %d\n", test->broadcast_count0, test->broadcast_count1, test->broadcast_count2);
|
2022-11-02 19:34:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void tf_ssb_test_rooms(const tf_test_options_t* options)
|
|
|
|
{
|
2023-03-07 12:50:17 -05:00
|
|
|
tf_printf("Testing Rooms.\n");
|
2022-11-02 19:34:44 -04:00
|
|
|
|
|
|
|
uv_loop_t loop = { 0 };
|
|
|
|
uv_loop_init(&loop);
|
|
|
|
|
2023-07-19 21:02:50 -04:00
|
|
|
unlink("out/test_db0.sqlite");
|
2024-03-02 15:01:09 -05:00
|
|
|
tf_ssb_t* ssb0 = tf_ssb_create(&loop, NULL, "file:out/test_db0.sqlite", NULL);
|
2022-11-02 19:34:44 -04:00
|
|
|
tf_ssb_register(tf_ssb_get_context(ssb0), ssb0);
|
2023-07-19 21:02:50 -04:00
|
|
|
unlink("out/test_db1.sqlite");
|
2024-03-02 15:01:09 -05:00
|
|
|
tf_ssb_t* ssb1 = tf_ssb_create(&loop, NULL, "file:out/test_db1.sqlite", NULL);
|
2022-11-02 19:34:44 -04:00
|
|
|
tf_ssb_register(tf_ssb_get_context(ssb1), ssb1);
|
2023-07-19 21:02:50 -04:00
|
|
|
unlink("out/test_db2.sqlite");
|
2024-03-02 15:01:09 -05:00
|
|
|
tf_ssb_t* ssb2 = tf_ssb_create(&loop, NULL, "file:out/test_db2.sqlite", NULL);
|
2022-11-02 19:34:44 -04:00
|
|
|
tf_ssb_register(tf_ssb_get_context(ssb2), ssb2);
|
|
|
|
|
|
|
|
uv_idle_t idle0 = { .data = ssb0 };
|
|
|
|
uv_idle_init(&loop, &idle0);
|
|
|
|
uv_idle_start(&idle0, _ssb_test_idle);
|
|
|
|
|
|
|
|
uv_idle_t idle1 = { .data = ssb1 };
|
|
|
|
uv_idle_init(&loop, &idle1);
|
|
|
|
uv_idle_start(&idle1, _ssb_test_idle);
|
|
|
|
|
|
|
|
uv_idle_t idle2 = { .data = ssb2 };
|
|
|
|
uv_idle_init(&loop, &idle2);
|
|
|
|
uv_idle_start(&idle2, _ssb_test_idle);
|
|
|
|
|
2024-02-15 18:35:01 -05:00
|
|
|
test_t test = {
|
2022-11-02 19:34:44 -04:00
|
|
|
.ssb0 = ssb0,
|
|
|
|
.ssb1 = ssb1,
|
|
|
|
.ssb2 = ssb2,
|
|
|
|
};
|
|
|
|
|
|
|
|
tf_ssb_add_connections_changed_callback(ssb0, _ssb_test_connections_changed, NULL, &test);
|
|
|
|
tf_ssb_add_connections_changed_callback(ssb1, _ssb_test_connections_changed, NULL, &test);
|
|
|
|
tf_ssb_add_connections_changed_callback(ssb2, _ssb_test_connections_changed, NULL, &test);
|
|
|
|
|
|
|
|
tf_ssb_add_broadcasts_changed_callback(ssb0, _broadcasts_changed, NULL, &test);
|
|
|
|
tf_ssb_add_broadcasts_changed_callback(ssb1, _broadcasts_changed, NULL, &test);
|
|
|
|
tf_ssb_add_broadcasts_changed_callback(ssb2, _broadcasts_changed, NULL, &test);
|
|
|
|
|
|
|
|
tf_ssb_generate_keys(ssb0);
|
|
|
|
tf_ssb_generate_keys(ssb1);
|
|
|
|
tf_ssb_generate_keys(ssb2);
|
|
|
|
|
|
|
|
char id0[k_id_base64_len] = { 0 };
|
|
|
|
char id1[k_id_base64_len] = { 0 };
|
|
|
|
char id2[k_id_base64_len] = { 0 };
|
|
|
|
bool b = tf_ssb_whoami(ssb0, id0, sizeof(id0));
|
|
|
|
(void)b;
|
|
|
|
assert(b);
|
|
|
|
b = tf_ssb_whoami(ssb1, id1, sizeof(id1));
|
|
|
|
assert(b);
|
|
|
|
b = tf_ssb_whoami(ssb2, id2, sizeof(id2));
|
|
|
|
assert(b);
|
2023-03-07 12:50:17 -05:00
|
|
|
tf_printf("ID %s, %s, %s\n", id0, id1, id2);
|
2022-11-02 19:34:44 -04:00
|
|
|
|
|
|
|
tf_ssb_server_open(ssb0, 12347);
|
|
|
|
|
|
|
|
uint8_t id0bin[k_id_bin_len];
|
|
|
|
tf_ssb_id_str_to_bin(id0bin, id0);
|
|
|
|
tf_ssb_connect(ssb1, "127.0.0.1", 12347, id0bin);
|
|
|
|
tf_ssb_connect(ssb2, "127.0.0.1", 12347, id0bin);
|
|
|
|
|
2023-03-07 12:50:17 -05:00
|
|
|
tf_printf("Waiting for connection.\n");
|
2024-02-15 18:35:01 -05:00
|
|
|
while (test.connection_count0 != 2 || test.connection_count1 != 1 || test.connection_count2 != 1)
|
2022-11-02 19:34:44 -04:00
|
|
|
{
|
|
|
|
uv_run(&loop, UV_RUN_ONCE);
|
|
|
|
}
|
|
|
|
tf_ssb_server_close(ssb0);
|
|
|
|
|
2023-03-07 12:50:17 -05:00
|
|
|
tf_printf("Waiting for broadcasts.\n");
|
2024-02-15 18:35:01 -05:00
|
|
|
while (test.broadcast_count1 != 1 || test.broadcast_count2 != 1)
|
2022-11-02 19:34:44 -04:00
|
|
|
{
|
|
|
|
uv_run(&loop, UV_RUN_ONCE);
|
|
|
|
}
|
|
|
|
|
|
|
|
tf_ssb_connection_t* connections[4];
|
|
|
|
int count = tf_ssb_get_connections(ssb1, connections, 4);
|
2022-11-06 21:57:29 -05:00
|
|
|
(void)count;
|
2022-11-02 19:34:44 -04:00
|
|
|
assert(count == 1);
|
|
|
|
|
|
|
|
int32_t tunnel_request_number = tf_ssb_connection_next_request_number(connections[0]);
|
|
|
|
|
|
|
|
JSContext* context = tf_ssb_get_context(ssb1);
|
|
|
|
JSValue message = JS_NewObject(context);
|
|
|
|
JSValue name = JS_NewArray(context);
|
|
|
|
JS_SetPropertyUint32(context, name, 0, JS_NewString(context, "tunnel"));
|
|
|
|
JS_SetPropertyUint32(context, name, 1, JS_NewString(context, "connect"));
|
|
|
|
JS_SetPropertyStr(context, message, "name", name);
|
|
|
|
JSValue args = JS_NewArray(context);
|
|
|
|
JSValue arg = JS_NewObject(context);
|
|
|
|
JS_SetPropertyStr(context, arg, "portal", JS_NewString(context, id0));
|
|
|
|
JS_SetPropertyStr(context, arg, "target", JS_NewString(context, id2));
|
|
|
|
JS_SetPropertyUint32(context, args, 0, arg);
|
|
|
|
JS_SetPropertyStr(context, message, "args", args);
|
|
|
|
JS_SetPropertyStr(context, message, "type", JS_NewString(context, "duplex"));
|
|
|
|
|
2024-05-02 19:02:23 -04:00
|
|
|
tf_ssb_connection_rpc_send_json(connections[0], k_ssb_rpc_flag_stream | k_ssb_rpc_flag_new_request, tunnel_request_number, "tunnel.connect", message, NULL, NULL, NULL);
|
2022-11-02 19:34:44 -04:00
|
|
|
JS_FreeValue(context, message);
|
|
|
|
|
2022-11-12 22:24:30 -05:00
|
|
|
tf_ssb_connection_t* tun0 = tf_ssb_connection_tunnel_create(ssb1, id0, tunnel_request_number, id2);
|
2023-03-07 12:50:17 -05:00
|
|
|
tf_printf("tun0 = %p\n", tun0);
|
2022-11-02 19:34:44 -04:00
|
|
|
|
2023-03-07 12:50:17 -05:00
|
|
|
tf_printf("Done.\n");
|
2022-11-02 19:34:44 -04:00
|
|
|
|
2024-02-15 18:35:01 -05:00
|
|
|
while (test.connection_count0 != 2 || test.connection_count1 != 2 || test.connection_count2 != 2)
|
2022-11-02 19:34:44 -04:00
|
|
|
{
|
|
|
|
uv_run(&loop, UV_RUN_ONCE);
|
|
|
|
}
|
|
|
|
|
2023-03-07 12:50:17 -05:00
|
|
|
tf_printf("Done.\n");
|
2022-11-02 19:34:44 -04:00
|
|
|
|
2023-01-07 19:44:36 -05:00
|
|
|
uv_run(&loop, UV_RUN_NOWAIT);
|
|
|
|
|
2022-11-19 16:42:54 -05:00
|
|
|
tf_ssb_connection_close(tun0);
|
2023-01-07 19:44:36 -05:00
|
|
|
|
|
|
|
uv_run(&loop, UV_RUN_NOWAIT);
|
2022-11-02 19:34:44 -04:00
|
|
|
|
|
|
|
uv_close((uv_handle_t*)&idle0, NULL);
|
|
|
|
uv_close((uv_handle_t*)&idle1, NULL);
|
|
|
|
uv_close((uv_handle_t*)&idle2, NULL);
|
|
|
|
|
2023-01-07 19:44:36 -05:00
|
|
|
tf_ssb_send_close(ssb0);
|
|
|
|
tf_ssb_send_close(ssb1);
|
|
|
|
tf_ssb_send_close(ssb2);
|
|
|
|
|
|
|
|
tf_ssb_close_all(ssb0);
|
|
|
|
tf_ssb_close_all(ssb1);
|
|
|
|
tf_ssb_close_all(ssb2);
|
|
|
|
|
|
|
|
uv_run(&loop, UV_RUN_DEFAULT);
|
|
|
|
|
2022-11-02 19:34:44 -04:00
|
|
|
tf_ssb_destroy(ssb0);
|
|
|
|
tf_ssb_destroy(ssb1);
|
|
|
|
tf_ssb_destroy(ssb2);
|
|
|
|
|
2023-01-07 19:25:38 -05:00
|
|
|
uv_run(&loop, UV_RUN_DEFAULT);
|
|
|
|
|
2022-11-02 19:34:44 -04:00
|
|
|
uv_loop_close(&loop);
|
|
|
|
}
|
|
|
|
|
2023-12-06 21:28:49 -05:00
|
|
|
static void _assert_visible(tf_ssb_t* ssb, const char* id, const char* contact, bool visible)
|
|
|
|
{
|
|
|
|
const char** ids = tf_ssb_db_following_deep_ids(ssb, &id, 1, 2);
|
|
|
|
bool found = false;
|
2023-12-06 21:29:30 -05:00
|
|
|
(void)found;
|
2023-12-06 21:28:49 -05:00
|
|
|
for (int i = 0; ids[i]; i++)
|
|
|
|
{
|
|
|
|
if (strcmp(ids[i], contact) == 0)
|
|
|
|
{
|
|
|
|
found = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
tf_free(ids);
|
|
|
|
assert(found == visible);
|
|
|
|
}
|
|
|
|
|
2021-09-06 14:23:22 -04:00
|
|
|
void tf_ssb_test_following(const tf_test_options_t* options)
|
2021-01-02 13:10:00 -05:00
|
|
|
{
|
2023-03-07 12:50:17 -05:00
|
|
|
tf_printf("Testing following.\n");
|
2021-01-02 13:10:00 -05:00
|
|
|
|
|
|
|
uv_loop_t loop = { 0 };
|
|
|
|
uv_loop_init(&loop);
|
|
|
|
|
2023-12-06 21:28:49 -05:00
|
|
|
unlink("out/test_db0.sqlite");
|
2024-03-02 15:01:09 -05:00
|
|
|
tf_ssb_t* ssb0 = tf_ssb_create(&loop, NULL, "file:out/test_db0.sqlite", NULL);
|
2021-01-02 13:10:00 -05:00
|
|
|
tf_ssb_generate_keys(ssb0);
|
|
|
|
|
2023-12-06 21:28:49 -05:00
|
|
|
char id0[k_id_base64_len] = { "@" };
|
|
|
|
char id1[k_id_base64_len] = { "@" };
|
|
|
|
char id2[k_id_base64_len] = { "@" };
|
|
|
|
char id3[k_id_base64_len] = { "@" };
|
|
|
|
char priv0b[512] = { 0 };
|
|
|
|
char priv1b[512] = { 0 };
|
|
|
|
char priv2b[512] = { 0 };
|
|
|
|
char priv3b[512] = { 0 };
|
2023-06-01 18:53:44 -04:00
|
|
|
uint8_t priv0[512] = { 0 };
|
|
|
|
uint8_t priv1[512] = { 0 };
|
|
|
|
uint8_t priv2[512] = { 0 };
|
2023-12-06 21:28:49 -05:00
|
|
|
uint8_t priv3[512] = { 0 };
|
|
|
|
|
|
|
|
tf_ssb_generate_keys_buffer(id0 + 1, sizeof(id0) - 1, priv0b, sizeof(priv0b));
|
|
|
|
tf_ssb_generate_keys_buffer(id1 + 1, sizeof(id1) - 1, priv1b, sizeof(priv1b));
|
|
|
|
tf_ssb_generate_keys_buffer(id2 + 1, sizeof(id2) - 1, priv2b, sizeof(priv2b));
|
|
|
|
tf_ssb_generate_keys_buffer(id3 + 1, sizeof(id3) - 1, priv3b, sizeof(priv3b));
|
|
|
|
tf_base64_decode(priv0b, strlen(priv0b), priv0, sizeof(priv0));
|
|
|
|
tf_base64_decode(priv1b, strlen(priv1b), priv1, sizeof(priv1));
|
|
|
|
tf_base64_decode(priv2b, strlen(priv2b), priv2, sizeof(priv2));
|
|
|
|
tf_base64_decode(priv3b, strlen(priv3b), priv3, sizeof(priv3));
|
|
|
|
|
|
|
|
JSContext* context = tf_ssb_get_context(ssb0);
|
2021-01-02 13:10:00 -05:00
|
|
|
JSValue message;
|
2023-07-19 21:02:50 -04:00
|
|
|
JSValue signed_message;
|
|
|
|
bool stored;
|
2021-01-02 13:10:00 -05:00
|
|
|
|
2024-02-15 18:35:01 -05:00
|
|
|
#define FOLLOW_BLOCK(id, priv, contact, follow, block) \
|
|
|
|
message = JS_NewObject(context); \
|
|
|
|
JS_SetPropertyStr(context, message, "type", JS_NewString(context, "contact")); \
|
|
|
|
JS_SetPropertyStr(context, message, "contact", JS_NewString(context, contact)); \
|
|
|
|
JS_SetPropertyStr(context, message, "following", follow ? JS_TRUE : JS_FALSE); \
|
|
|
|
JS_SetPropertyStr(context, message, "blocking", block ? JS_TRUE : JS_FALSE); \
|
2024-06-16 07:51:06 -04:00
|
|
|
signed_message = tf_ssb_sign_message(ssb0, id, priv, message, NULL, 0); \
|
2024-02-15 18:35:01 -05:00
|
|
|
stored = false; \
|
|
|
|
tf_ssb_verify_strip_and_store_message(ssb0, signed_message, _message_stored, &stored); \
|
|
|
|
_wait_stored(ssb0, &stored); \
|
|
|
|
JS_FreeValue(context, signed_message); \
|
|
|
|
JS_FreeValue(context, message);
|
2023-12-06 21:28:49 -05:00
|
|
|
|
|
|
|
FOLLOW_BLOCK(id0, priv0, id1, true, false);
|
|
|
|
FOLLOW_BLOCK(id1, priv1, id2, true, false);
|
|
|
|
FOLLOW_BLOCK(id1, priv1, id3, true, false);
|
|
|
|
_assert_visible(ssb0, id0, id0, true);
|
|
|
|
_assert_visible(ssb0, id0, id1, true);
|
|
|
|
_assert_visible(ssb0, id0, id2, true);
|
|
|
|
_assert_visible(ssb0, id0, id3, true);
|
|
|
|
FOLLOW_BLOCK(id0, priv0, id3, false, true);
|
|
|
|
_assert_visible(ssb0, id0, id0, true);
|
|
|
|
_assert_visible(ssb0, id0, id1, true);
|
|
|
|
_assert_visible(ssb0, id0, id2, true);
|
|
|
|
_assert_visible(ssb0, id0, id3, false);
|
|
|
|
|
2024-02-15 18:35:01 -05:00
|
|
|
#undef FOLLOW_BLOCK
|
2021-01-02 13:10:00 -05:00
|
|
|
|
|
|
|
uv_run(&loop, UV_RUN_DEFAULT);
|
|
|
|
|
|
|
|
tf_ssb_destroy(ssb0);
|
|
|
|
|
|
|
|
uv_loop_close(&loop);
|
|
|
|
}
|
2022-12-11 22:11:32 -05:00
|
|
|
|
|
|
|
void tf_ssb_test_bench(const tf_test_options_t* options)
|
|
|
|
{
|
2023-03-07 12:50:17 -05:00
|
|
|
tf_printf("Testing following.\n");
|
2022-12-11 22:11:32 -05:00
|
|
|
|
|
|
|
uv_loop_t loop = { 0 };
|
|
|
|
uv_loop_init(&loop);
|
|
|
|
|
2023-07-20 01:06:15 -04:00
|
|
|
tf_trace_t* trace = tf_trace_create();
|
|
|
|
|
2023-07-19 21:02:50 -04:00
|
|
|
unlink("out/test_db0.sqlite");
|
2024-03-02 15:01:09 -05:00
|
|
|
tf_ssb_t* ssb0 = tf_ssb_create(&loop, NULL, "file:out/test_db0.sqlite", NULL);
|
2023-07-20 01:06:15 -04:00
|
|
|
tf_ssb_set_trace(ssb0, trace);
|
2022-12-11 22:11:32 -05:00
|
|
|
tf_ssb_generate_keys(ssb0);
|
|
|
|
|
|
|
|
char id0[k_id_base64_len] = { 0 };
|
|
|
|
tf_ssb_whoami(ssb0, id0, sizeof(id0));
|
|
|
|
|
2023-06-01 18:53:44 -04:00
|
|
|
uint8_t priv0[512];
|
|
|
|
tf_ssb_get_private_key(ssb0, priv0, sizeof(priv0));
|
|
|
|
|
2022-12-11 22:11:32 -05:00
|
|
|
struct timespec start_time = { 0 };
|
|
|
|
struct timespec end_time = { 0 };
|
|
|
|
clock_gettime(CLOCK_REALTIME, &start_time);
|
2022-12-31 13:59:29 -05:00
|
|
|
const int k_messages = 4096;
|
2023-06-01 18:53:44 -04:00
|
|
|
JSValue obj = JS_NewObject(tf_ssb_get_context(ssb0));
|
|
|
|
JS_SetPropertyStr(tf_ssb_get_context(ssb0), obj, "type", JS_NewString(tf_ssb_get_context(ssb0), "post"));
|
|
|
|
JS_SetPropertyStr(tf_ssb_get_context(ssb0), obj, "text", JS_NewString(tf_ssb_get_context(ssb0), "Hello, world!"));
|
2022-12-31 13:59:29 -05:00
|
|
|
for (int i = 0; i < k_messages; i++)
|
2022-12-29 12:01:27 -05:00
|
|
|
{
|
2023-07-19 21:02:50 -04:00
|
|
|
bool stored = false;
|
2024-06-16 07:51:06 -04:00
|
|
|
JSValue signed_message = tf_ssb_sign_message(ssb0, id0, priv0, obj, NULL, 0);
|
2023-07-19 21:02:50 -04:00
|
|
|
tf_ssb_verify_strip_and_store_message(ssb0, signed_message, _message_stored, &stored);
|
|
|
|
JS_FreeValue(tf_ssb_get_context(ssb0), signed_message);
|
|
|
|
_wait_stored(ssb0, &stored);
|
2022-12-11 22:11:32 -05:00
|
|
|
}
|
2023-06-01 18:53:44 -04:00
|
|
|
JS_FreeValue(tf_ssb_get_context(ssb0), obj);
|
2022-12-11 22:11:32 -05:00
|
|
|
clock_gettime(CLOCK_REALTIME, &end_time);
|
2023-04-29 23:00:57 -04:00
|
|
|
tf_printf("insert = %f seconds\n", (end_time.tv_sec - start_time.tv_sec) + (end_time.tv_nsec - start_time.tv_nsec) / 1e9);
|
2022-12-31 13:59:29 -05:00
|
|
|
|
2023-07-19 21:02:50 -04:00
|
|
|
unlink("out/test_db1.sqlite");
|
2024-03-02 15:01:09 -05:00
|
|
|
tf_ssb_t* ssb1 = tf_ssb_create(&loop, NULL, "file:out/test_db1.sqlite", NULL);
|
2023-07-20 01:06:15 -04:00
|
|
|
tf_ssb_set_trace(ssb1, trace);
|
2022-12-31 13:59:29 -05:00
|
|
|
tf_ssb_generate_keys(ssb1);
|
|
|
|
uint8_t id0bin[k_id_bin_len];
|
|
|
|
tf_ssb_id_str_to_bin(id0bin, id0);
|
|
|
|
|
2023-10-11 11:44:40 -04:00
|
|
|
tf_ssb_set_main_thread(ssb0, true);
|
|
|
|
tf_ssb_set_main_thread(ssb1, true);
|
2023-10-08 10:25:22 -04:00
|
|
|
|
2022-12-31 13:59:29 -05:00
|
|
|
uv_idle_t idle0 = { .data = ssb0 };
|
|
|
|
uv_idle_init(&loop, &idle0);
|
|
|
|
uv_idle_start(&idle0, _ssb_test_idle);
|
|
|
|
|
|
|
|
uv_idle_t idle1 = { .data = ssb1 };
|
|
|
|
uv_idle_init(&loop, &idle1);
|
|
|
|
uv_idle_start(&idle1, _ssb_test_idle);
|
|
|
|
|
|
|
|
tf_ssb_register(tf_ssb_get_context(ssb0), ssb0);
|
|
|
|
tf_ssb_register(tf_ssb_get_context(ssb1), ssb1);
|
|
|
|
|
|
|
|
tf_ssb_server_open(ssb0, 12347);
|
|
|
|
tf_ssb_connect(ssb1, "127.0.0.1", 12347, id0bin);
|
|
|
|
|
2023-03-07 12:50:17 -05:00
|
|
|
tf_printf("Waiting for messages.\n");
|
2022-12-31 13:59:29 -05:00
|
|
|
clock_gettime(CLOCK_REALTIME, &start_time);
|
2023-07-19 21:02:50 -04:00
|
|
|
int count = 0;
|
2023-07-20 01:06:15 -04:00
|
|
|
tf_ssb_add_message_added_callback(ssb1, _message_added, NULL, &count);
|
2023-07-19 21:02:50 -04:00
|
|
|
while (count < k_messages)
|
2022-12-31 13:59:29 -05:00
|
|
|
{
|
|
|
|
uv_run(&loop, UV_RUN_ONCE);
|
|
|
|
}
|
2023-07-20 01:06:15 -04:00
|
|
|
tf_ssb_remove_message_added_callback(ssb1, _message_added, &count);
|
2022-12-31 13:59:29 -05:00
|
|
|
clock_gettime(CLOCK_REALTIME, &end_time);
|
2023-07-20 01:06:15 -04:00
|
|
|
|
2023-10-11 11:44:40 -04:00
|
|
|
tf_ssb_set_main_thread(ssb0, false);
|
|
|
|
tf_ssb_set_main_thread(ssb1, false);
|
2023-07-20 01:06:15 -04:00
|
|
|
count = _ssb_test_count_messages(ssb1);
|
|
|
|
if (count < k_messages)
|
|
|
|
{
|
|
|
|
abort();
|
|
|
|
}
|
2023-03-07 12:50:17 -05:00
|
|
|
tf_printf("Done.\n");
|
2023-04-29 23:00:57 -04:00
|
|
|
tf_printf("replicate = %f seconds\n", (end_time.tv_sec - start_time.tv_sec) + (end_time.tv_nsec - start_time.tv_nsec) / 1e9);
|
2022-12-31 13:59:29 -05:00
|
|
|
|
|
|
|
tf_ssb_send_close(ssb1);
|
|
|
|
tf_ssb_server_close(ssb0);
|
|
|
|
|
|
|
|
uv_close((uv_handle_t*)&idle0, NULL);
|
|
|
|
uv_close((uv_handle_t*)&idle1, NULL);
|
2022-12-11 22:11:32 -05:00
|
|
|
|
|
|
|
uv_run(&loop, UV_RUN_DEFAULT);
|
|
|
|
|
2023-07-20 01:06:15 -04:00
|
|
|
char* trace_data = tf_trace_export(trace);
|
2023-10-25 22:56:33 -04:00
|
|
|
if (trace_data)
|
2023-07-20 01:06:15 -04:00
|
|
|
{
|
2023-10-25 22:56:33 -04:00
|
|
|
FILE* file = fopen("out/trace.json", "wb");
|
|
|
|
if (file)
|
|
|
|
{
|
|
|
|
fwrite(trace_data, 1, strlen(trace_data), file);
|
|
|
|
fclose(file);
|
|
|
|
}
|
|
|
|
tf_free(trace_data);
|
2023-07-20 01:06:15 -04:00
|
|
|
}
|
|
|
|
|
2022-12-31 13:59:29 -05:00
|
|
|
tf_ssb_destroy(ssb1);
|
2022-12-11 22:11:32 -05:00
|
|
|
tf_ssb_destroy(ssb0);
|
|
|
|
|
2023-07-20 01:06:15 -04:00
|
|
|
tf_trace_destroy(trace);
|
|
|
|
|
2022-12-11 22:11:32 -05:00
|
|
|
uv_loop_close(&loop);
|
|
|
|
}
|
2023-09-21 19:38:55 -04:00
|
|
|
|
|
|
|
static void _ssb_test_room_connections_changed(tf_ssb_t* ssb, tf_ssb_change_t change, tf_ssb_connection_t* connection, void* user_data)
|
|
|
|
{
|
2024-02-15 18:35:01 -05:00
|
|
|
const char* changes[] = { "create", "connect", "remove" };
|
2023-09-21 19:38:55 -04:00
|
|
|
tf_printf("change=%s %p connection=%s:%d\n", changes[change], connection, tf_ssb_connection_get_host(connection), tf_ssb_connection_get_port(connection));
|
|
|
|
}
|
|
|
|
|
|
|
|
typedef struct _close_t
|
|
|
|
{
|
|
|
|
tf_ssb_t* ssb;
|
|
|
|
tf_ssb_connection_t* connection;
|
|
|
|
uv_timer_t timer;
|
2024-01-10 20:38:30 -05:00
|
|
|
int32_t request_number;
|
|
|
|
char id[k_id_base64_len];
|
2023-09-21 19:38:55 -04:00
|
|
|
} close_t;
|
|
|
|
|
|
|
|
static void _timer_close(uv_handle_t* handle)
|
|
|
|
{
|
|
|
|
tf_free(handle->data);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void _close_callback(uv_timer_t* timer)
|
|
|
|
{
|
|
|
|
close_t* data = timer->data;
|
|
|
|
tf_printf("breaking %s %p\n", data->id, data->connection);
|
|
|
|
const char* message = "{\"name\":\"Error\",\"message\":\"whoops\",\"stack\":\"nah\"}";
|
2024-05-02 20:10:56 -04:00
|
|
|
tf_ssb_connection_rpc_send(data->connection, k_ssb_rpc_flag_stream | k_ssb_rpc_flag_json | k_ssb_rpc_flag_end_error, data->request_number, NULL, (const uint8_t*)message,
|
|
|
|
strlen(message), NULL, NULL, NULL);
|
2023-09-21 19:38:55 -04:00
|
|
|
uv_close((uv_handle_t*)timer, _timer_close);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void _break_in_a_bit(tf_ssb_t* ssb, tf_ssb_connection_t* connection, const char* id, int32_t request_number)
|
|
|
|
{
|
|
|
|
close_t* data = tf_malloc(sizeof(close_t));
|
|
|
|
*data = (close_t)
|
|
|
|
{
|
|
|
|
.ssb = ssb,
|
|
|
|
.connection = connection,
|
|
|
|
.request_number = request_number,
|
|
|
|
.timer =
|
|
|
|
{
|
|
|
|
.data = data,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
snprintf(data->id, sizeof(data->id), "%s", id);
|
|
|
|
uv_timer_init(tf_ssb_get_loop(ssb), &data->timer);
|
|
|
|
uv_timer_start(&data->timer, _close_callback, 3000, 0);
|
|
|
|
}
|
|
|
|
|
2024-08-14 19:40:20 -04:00
|
|
|
static void _ssb_test_room_broadcasts_visit(
|
|
|
|
const char* host, const struct sockaddr_in* addr, tf_ssb_broadcast_origin_t origin, tf_ssb_connection_t* tunnel, const uint8_t* pub, void* user_data)
|
2023-09-21 19:38:55 -04:00
|
|
|
{
|
|
|
|
tf_ssb_t* ssb = user_data;
|
|
|
|
char id[k_id_base64_len] = { 0 };
|
|
|
|
tf_ssb_id_bin_to_str(id, sizeof(id), pub);
|
|
|
|
tf_ssb_connection_t* connections[8];
|
2024-02-15 18:35:01 -05:00
|
|
|
if (tunnel && strcmp(id, "@Jqm63iKumgaWfUI6mXtmQCDHiQJhzMiEWXYUqtcGs9o=.ed25519") != 0 && tf_ssb_get_connections(ssb, connections, 8) == 1)
|
2023-09-21 19:38:55 -04:00
|
|
|
{
|
|
|
|
tf_printf("%s %p %s\n", host, tunnel, id);
|
|
|
|
|
|
|
|
int32_t tunnel_request_number = tf_ssb_connection_next_request_number(tunnel);
|
|
|
|
|
|
|
|
char portal[k_id_base64_len] = { 0 };
|
|
|
|
char target[k_id_base64_len] = { 0 };
|
|
|
|
tf_ssb_connection_get_id(tunnel, portal, sizeof(portal));
|
|
|
|
tf_ssb_id_bin_to_str(target, sizeof(target), pub);
|
|
|
|
|
|
|
|
JSContext* context = tf_ssb_get_context(ssb);
|
|
|
|
JSValue message = JS_NewObject(context);
|
|
|
|
JSValue name = JS_NewArray(context);
|
|
|
|
JS_SetPropertyUint32(context, name, 0, JS_NewString(context, "tunnel"));
|
|
|
|
JS_SetPropertyUint32(context, name, 1, JS_NewString(context, "connect"));
|
|
|
|
JS_SetPropertyStr(context, message, "name", name);
|
|
|
|
JSValue args = JS_NewArray(context);
|
|
|
|
JSValue arg = JS_NewObject(context);
|
|
|
|
JS_SetPropertyStr(context, arg, "portal", JS_NewString(context, portal));
|
|
|
|
JS_SetPropertyStr(context, arg, "target", JS_NewString(context, target));
|
|
|
|
JS_SetPropertyUint32(context, args, 0, arg);
|
|
|
|
JS_SetPropertyStr(context, message, "args", args);
|
|
|
|
JS_SetPropertyStr(context, message, "type", JS_NewString(context, "duplex"));
|
|
|
|
|
2024-05-02 19:02:23 -04:00
|
|
|
tf_ssb_connection_rpc_send_json(tunnel, k_ssb_rpc_flag_stream | k_ssb_rpc_flag_new_request, tunnel_request_number, "tunnel.connect", message, NULL, NULL, NULL);
|
2023-09-21 19:38:55 -04:00
|
|
|
JS_FreeValue(context, message);
|
|
|
|
|
|
|
|
tf_printf("tunnel create ssb=%p portal=%s rn=%d target=%s\n", ssb, portal, (int)tunnel_request_number, target);
|
|
|
|
tf_ssb_connection_tunnel_create(ssb, portal, tunnel_request_number, target);
|
|
|
|
_break_in_a_bit(ssb, tunnel, target, tunnel_request_number);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void _ssb_test_room_broadcasts_changed(tf_ssb_t* ssb, void* user_data)
|
|
|
|
{
|
|
|
|
tf_ssb_visit_broadcasts(ssb, _ssb_test_room_broadcasts_visit, ssb);
|
|
|
|
}
|
|
|
|
|
|
|
|
void tf_ssb_test_go_ssb_room(const tf_test_options_t* options)
|
|
|
|
{
|
|
|
|
tf_printf("Testing go_ssb_room.\n");
|
|
|
|
|
|
|
|
uv_loop_t loop = { 0 };
|
|
|
|
uv_loop_init(&loop);
|
|
|
|
|
|
|
|
tf_trace_t* trace = tf_trace_create();
|
|
|
|
|
|
|
|
unlink("out/test_db0.sqlite");
|
2024-03-02 15:01:09 -05:00
|
|
|
tf_ssb_t* ssb0 = tf_ssb_create(&loop, NULL, "file:out/test_db0.sqlite", NULL);
|
2023-09-21 19:38:55 -04:00
|
|
|
tf_ssb_set_trace(ssb0, trace);
|
|
|
|
tf_ssb_generate_keys(ssb0);
|
|
|
|
|
|
|
|
unlink("out/test_db1.sqlite");
|
2024-03-02 15:01:09 -05:00
|
|
|
tf_ssb_t* ssb1 = tf_ssb_create(&loop, NULL, "file:out/test_db1.sqlite", NULL);
|
2023-09-21 19:38:55 -04:00
|
|
|
tf_ssb_set_trace(ssb1, trace);
|
|
|
|
tf_ssb_generate_keys(ssb1);
|
|
|
|
|
|
|
|
tf_ssb_add_connections_changed_callback(ssb0, _ssb_test_room_connections_changed, NULL, NULL);
|
|
|
|
tf_ssb_add_connections_changed_callback(ssb1, _ssb_test_room_connections_changed, NULL, NULL);
|
|
|
|
|
|
|
|
tf_ssb_add_broadcasts_changed_callback(ssb0, _ssb_test_room_broadcasts_changed, NULL, NULL);
|
|
|
|
|
|
|
|
tf_ssb_connect_str(ssb0, "net:linode.unprompted.com:8008~shs:Q0pc/7kXQJGIlqJxuwayL2huayzddgkVDoGkYVWQS1Y=:SSB+Room+PSK3TLYC2T86EHQCUHBUHASCASE18JBV24=");
|
|
|
|
tf_ssb_connect_str(ssb1, "net:linode.unprompted.com:8008~shs:Q0pc/7kXQJGIlqJxuwayL2huayzddgkVDoGkYVWQS1Y=:SSB+Room+PSK3TLYC2T86EHQCUHBUHASCASE18JBV24=");
|
|
|
|
|
|
|
|
uv_run(&loop, UV_RUN_DEFAULT);
|
|
|
|
|
|
|
|
tf_ssb_destroy(ssb0);
|
|
|
|
tf_ssb_destroy(ssb1);
|
|
|
|
|
|
|
|
tf_trace_destroy(trace);
|
|
|
|
|
|
|
|
uv_loop_close(&loop);
|
|
|
|
}
|
2024-06-16 17:07:12 -04:00
|
|
|
|
2024-06-16 17:08:10 -04:00
|
|
|
#if !TARGET_OS_IPHONE
|
2024-06-16 17:07:12 -04:00
|
|
|
static void _write_file(const char* path, const char* contents)
|
|
|
|
{
|
|
|
|
FILE* file = fopen(path, "w");
|
|
|
|
if (!file)
|
|
|
|
{
|
|
|
|
printf("Unable to write %s: %s.\n", path, strerror(errno));
|
|
|
|
fflush(stdout);
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
fputs(contents, file);
|
|
|
|
fclose(file);
|
|
|
|
}
|
|
|
|
|
|
|
|
#define TEST_ARGS " --ssb-port=0 --http-port=0 --https-port=0"
|
|
|
|
|
|
|
|
void tf_ssb_test_encrypt(const tf_test_options_t* options)
|
|
|
|
{
|
|
|
|
_write_file("out/test.js",
|
|
|
|
"async function main() {\n"
|
|
|
|
" let a = await ssb.createIdentity('test');\n"
|
|
|
|
" let b = await ssb.createIdentity('test');\n"
|
2024-06-16 17:22:26 -04:00
|
|
|
" let c = await ssb.privateMessageEncrypt('test', a, [a, b], \"{'foo': 1}\");\n"
|
2024-06-16 17:07:12 -04:00
|
|
|
" if (!c.endsWith('.box')) {\n"
|
|
|
|
" exit(1);\n"
|
|
|
|
" }\n"
|
2024-06-16 17:22:26 -04:00
|
|
|
" print(await ssb.privateMessageDecrypt('test', a, c));\n"
|
2024-06-16 17:07:12 -04:00
|
|
|
"}\n"
|
|
|
|
"main().catch(() => exit(2));\n");
|
|
|
|
|
|
|
|
unlink("out/testdb.sqlite");
|
|
|
|
char command[256];
|
|
|
|
snprintf(command, sizeof(command), "%s run --db-path=out/testdb.sqlite -s out/test.js" TEST_ARGS, options->exe_path);
|
|
|
|
tf_printf("%s\n", command);
|
|
|
|
int result = system(command);
|
|
|
|
(void)result;
|
|
|
|
assert(WIFEXITED(result));
|
|
|
|
printf("returned %d\n", WEXITSTATUS(result));
|
|
|
|
assert(WEXITSTATUS(result) == 0);
|
|
|
|
}
|
2024-08-15 12:48:24 -04:00
|
|
|
|
2024-08-20 12:35:42 -04:00
|
|
|
static void _count_broadcasts_callback(
|
|
|
|
const char* host, const struct sockaddr_in* addr, tf_ssb_broadcast_origin_t origin, tf_ssb_connection_t* tunnel, const uint8_t* pub, void* user_data)
|
2024-08-19 12:29:40 -04:00
|
|
|
{
|
|
|
|
int* count = user_data;
|
|
|
|
(*count)++;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int _count_broadcasts(tf_ssb_t* ssb)
|
|
|
|
{
|
|
|
|
int count = 0;
|
|
|
|
tf_ssb_visit_broadcasts(ssb, _count_broadcasts_callback, &count);
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
2024-08-15 12:48:24 -04:00
|
|
|
void tf_ssb_test_peer_exchange(const tf_test_options_t* options)
|
|
|
|
{
|
|
|
|
uv_loop_t loop = { 0 };
|
|
|
|
uv_loop_init(&loop);
|
|
|
|
|
|
|
|
unlink("out/test_db0.sqlite");
|
|
|
|
tf_ssb_t* ssb0 = tf_ssb_create(&loop, NULL, "file:out/test_db0.sqlite", NULL);
|
|
|
|
tf_ssb_set_is_room(ssb0, false);
|
|
|
|
tf_ssb_set_is_replicator(ssb0, false);
|
|
|
|
tf_ssb_set_is_peer_exchange(ssb0, true);
|
|
|
|
tf_ssb_register(tf_ssb_get_context(ssb0), ssb0);
|
|
|
|
tf_ssb_server_open(ssb0, 12347);
|
|
|
|
|
|
|
|
unlink("out/test_db1.sqlite");
|
|
|
|
tf_ssb_t* ssb1 = tf_ssb_create(&loop, NULL, "file:out/test_db1.sqlite", NULL);
|
|
|
|
tf_ssb_set_is_room(ssb1, false);
|
|
|
|
tf_ssb_set_is_replicator(ssb1, false);
|
|
|
|
tf_ssb_set_is_peer_exchange(ssb1, true);
|
|
|
|
tf_ssb_register(tf_ssb_get_context(ssb1), ssb1);
|
|
|
|
tf_ssb_server_open(ssb1, 12348);
|
|
|
|
|
|
|
|
unlink("out/test_db2.sqlite");
|
|
|
|
tf_ssb_t* ssb2 = tf_ssb_create(&loop, NULL, "file:out/test_db2.sqlite", NULL);
|
|
|
|
tf_ssb_set_is_room(ssb2, false);
|
|
|
|
tf_ssb_set_is_replicator(ssb2, false);
|
|
|
|
tf_ssb_set_is_peer_exchange(ssb2, true);
|
|
|
|
tf_ssb_register(tf_ssb_get_context(ssb2), ssb2);
|
|
|
|
tf_ssb_server_open(ssb2, 12349);
|
|
|
|
|
|
|
|
char id0[k_id_base64_len] = { 0 };
|
|
|
|
tf_ssb_whoami(ssb0, id0, sizeof(id0));
|
|
|
|
uint8_t id0bin[k_id_bin_len];
|
|
|
|
tf_ssb_id_str_to_bin(id0bin, id0);
|
|
|
|
tf_ssb_connect(ssb1, "127.0.0.1", 12347, id0bin);
|
|
|
|
tf_ssb_connect(ssb2, "127.0.0.1", 12347, id0bin);
|
|
|
|
|
2024-08-20 12:35:42 -04:00
|
|
|
while (_count_broadcasts(ssb0) != 2 || _count_broadcasts(ssb1) != 1 || _count_broadcasts(ssb2) != 1)
|
2024-08-19 12:29:40 -04:00
|
|
|
{
|
|
|
|
uv_run(&loop, UV_RUN_ONCE);
|
|
|
|
}
|
|
|
|
|
|
|
|
tf_ssb_send_close(ssb0);
|
|
|
|
tf_ssb_send_close(ssb1);
|
|
|
|
tf_ssb_send_close(ssb2);
|
2024-08-15 12:48:24 -04:00
|
|
|
|
|
|
|
tf_ssb_destroy(ssb0);
|
|
|
|
tf_ssb_destroy(ssb1);
|
|
|
|
tf_ssb_destroy(ssb2);
|
|
|
|
|
|
|
|
uv_run(&loop, UV_RUN_DEFAULT);
|
|
|
|
|
|
|
|
uv_loop_close(&loop);
|
|
|
|
}
|
|
|
|
|
2024-06-16 17:08:10 -04:00
|
|
|
#endif
|