I think I fixed following calculations, again. Revived the test, though it's still very not thorough.

git-svn-id: https://www.unprompted.com/svn/projects/tildefriends/trunk@4662 ed5197a5-7fde-0310-b194-c3ffbd925b24
This commit is contained in:
2023-12-07 02:28:49 +00:00
parent f0984b19f2
commit b1714cf554
2 changed files with 141 additions and 93 deletions

View File

@ -496,6 +496,22 @@ void tf_ssb_test_rooms(const tf_test_options_t* options)
uv_loop_close(&loop);
}
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;
for (int i = 0; ids[i]; i++)
{
if (strcmp(ids[i], contact) == 0)
{
found = true;
break;
}
}
tf_free(ids);
assert(found == visible);
}
void tf_ssb_test_following(const tf_test_options_t* options)
{
tf_printf("Testing following.\n");
@ -503,91 +519,68 @@ void tf_ssb_test_following(const tf_test_options_t* options)
uv_loop_t loop = { 0 };
uv_loop_init(&loop);
tf_ssb_t* ssb0 = tf_ssb_create(&loop, NULL, ":memory:");
unlink("out/test_db0.sqlite");
tf_ssb_t* ssb0 = tf_ssb_create(&loop, NULL, "file:out/test_db0.sqlite");
tf_ssb_generate_keys(ssb0);
tf_ssb_t* ssb1 = tf_ssb_create(&loop, NULL, ":memory:");
tf_ssb_generate_keys(ssb1);
tf_ssb_t* ssb2 = tf_ssb_create(&loop, NULL, ":memory:");
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 };
tf_ssb_whoami(ssb0, id0, sizeof(id0));
tf_ssb_whoami(ssb1, id1, sizeof(id1));
tf_ssb_whoami(ssb2, id2, sizeof(id2));
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 };
uint8_t priv0[512] = { 0 };
uint8_t priv1[512] = { 0 };
uint8_t priv2[512] = { 0 };
tf_ssb_get_private_key(ssb0, priv0, sizeof(priv0));
tf_ssb_get_private_key(ssb1, priv1, sizeof(priv1));
tf_ssb_get_private_key(ssb2, priv2, sizeof(priv2));
uint8_t priv3[512] = { 0 };
JSContext* context = NULL;
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);
JSValue message;
JSValue signed_message;
bool stored;
#define FOLLOW(ssb, id, priv, follow) \
context = tf_ssb_get_context(ssb); \
#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, id)); \
JS_SetPropertyStr(context, message, "contact", JS_NewString(context, contact)); \
JS_SetPropertyStr(context, message, "following", follow ? JS_TRUE : JS_FALSE); \
signed_message = tf_ssb_sign_message(ssb, id, priv, message); \
JS_SetPropertyStr(context, message, "blocking", block ? JS_TRUE : JS_FALSE); \
signed_message = tf_ssb_sign_message(ssb0, id, priv, message); \
stored = false; \
tf_ssb_verify_strip_and_store_message(ssb, signed_message, _message_stored, &stored); \
_wait_stored(ssb, &stored); \
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); \
context = NULL
#if 1
/* TODO: This test doesn't actually really test anything anymore. */
#define DUMP(id, depth)
#else
#define DUMP(id, depth) \
do \
{ \
tf_printf("following %d:\n", depth); \
const char** tf_ssb_get_following_deep(tf_ssb_t* ssb_param, const char** ids, int depth_param); \
const char** f = tf_ssb_get_following_deep(ssb0, (const char*[]) { id, NULL }, depth); \
for (const char** p = f; p && *p; p++) \
{ \
tf_printf("* %s\n", *p); \
} \
tf_printf("\n"); \
tf_free(f); \
} \
while (0)
#endif
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);
FOLLOW(ssb0, id1, priv1, true);
FOLLOW(ssb1, id2, priv2, true);
FOLLOW(ssb2, id0, priv0, true);
DUMP(id0, 2);
DUMP(id1, 2);
DUMP(id2, 2);
FOLLOW(ssb0, id1, priv1, false);
//FOLLOW(ssb0, id1, priv1, true);
//FOLLOW(ssb0, id1, priv1, true);
DUMP(id0, 1);
DUMP(id1, 2);
//FOLLOW(ssb0, id1, priv1, false);
//DUMP(1);
//DUMP(1);
#undef FOLLOW
#undef DUMP
#undef FOLLOW_BLOCK
uv_run(&loop, UV_RUN_DEFAULT);
tf_ssb_destroy(ssb0);
tf_ssb_destroy(ssb1);
tf_ssb_destroy(ssb2);
uv_loop_close(&loop);
}