[ADD/WIP] Start testing code
Some checks are pending
Compile Telodendria / Compile Telodendria (x86, alpine-v3.19) (push) Waiting to run
Compile Telodendria / Compile Telodendria (x86, debian-v12.4) (push) Waiting to run
Compile Telodendria / Compile Telodendria (x86, freebsd-v14.0) (push) Waiting to run
Compile Telodendria / Compile Telodendria (x86, netbsd-v9.3) (push) Waiting to run
Compile Telodendria / Compile Telodendria (x86_64, alpine-v3.19) (push) Waiting to run
Compile Telodendria / Compile Telodendria (x86_64, debian-v12.4) (push) Waiting to run
Compile Telodendria / Compile Telodendria (x86_64, freebsd-v14.0) (push) Waiting to run
Compile Telodendria / Compile Telodendria (x86_64, netbsd-v9.3) (push) Waiting to run
Compile Telodendria / Compile Telodendria (x86_64, openbsd-v7.4) (push) Waiting to run

I think I'll manage PDU depth later(with an actual good way to handle
it properly(that is not just setting it to the max and calling it a
day.)
This commit is contained in:
lda 2024-05-17 23:57:32 +02:00
parent 1753c2188b
commit e36f4357ab
6 changed files with 219 additions and 45 deletions

View file

@ -51,7 +51,8 @@
"required": true
},
"redacts": {
"type": "string"
"type": "string",
"required": false
},
"room_id": {
"type": "string",

View file

@ -118,7 +118,10 @@ CanonicalJsonEncode(HashMap * object, Stream * out)
ArrayAdd(keys, key);
}
ArraySort(keys, CanonicalJsonKeyCompare);
if (ArraySize(keys) != 0)
{
ArraySort(keys, CanonicalJsonKeyCompare);
}
/* The total number of bytes written */
length = 0;

View file

@ -487,7 +487,7 @@ ParserRecomposeCommonID(CommonID id)
if (id.server.hostname)
{
char *server = ParserRecomposeServerPart(id.server);
char *tmp = StrConcat(4, "@", ret, ":", server);
char *tmp = StrConcat(3, ret, ":", server);
Free(ret);
Free(server);

View file

@ -74,6 +74,7 @@ GenerateRoomId(ServerPart s)
string = ParserRecomposeCommonID(cid);
Free(cid.local);
return string;
}
@ -112,7 +113,58 @@ RoomCreate(Db * db, User *user, RoomCreateRequest * req, ServerPart s)
JsonSet(json, JsonValueString(full_creator), 1, "creator");
Free(full_creator);
/* TODO: Populate room with information */
{
HashMap *event = HashMapCreate();
HashMap *content = HashMapCreate();
CommonID sender;
char *sender_str;
sender.sigil = '@';
sender.local = UserGetName(user);
sender.server = s;
sender_str = ParserRecomposeCommonID(sender);
JsonSet(event, JsonValueString(sender_str), 1, "sender");
if (room->version <= 10)
{
JsonSet(content, JsonValueString(sender_str), 1, "creator");
}
Free(sender_str);
JsonSet(event, JsonValueString("m.room.create"), 1, "type");
JsonSet(event, JsonValueString(""), 1, "state_key");
JsonSet(event, JsonValueObject(content), 1, "content");
JsonFree(RoomEventSend(room, event));
JsonFree(event);
}
{
HashMap *event = HashMapCreate();
HashMap *content = HashMapCreate();
CommonID sender;
char *sender_str;
sender.sigil = '@';
sender.local = UserGetName(user);
sender.server = s;
sender_str = ParserRecomposeCommonID(sender);
JsonSet(event, JsonValueString(sender_str), 1, "sender");
JsonSet(content, JsonValueString("join"), 1, "membership");
JsonSet(event, JsonValueString("m.room.member"), 1, "type");
JsonSet(event, JsonValueString(sender_str), 1, "state_key");
Free(sender_str);
JsonSet(event, JsonValueObject(content), 1, "content");
JsonFree(RoomEventSend(room, event));
JsonFree(event);
}
/* TODO: The rest of the events mandated by the specification on
* POST /createRoom. Also clean up that code, so that it is more
* straightforward(and short). */
return room;
}
@ -233,7 +285,7 @@ RoomStateGetID(Room * room, char *event_id)
do \
{ \
id_##n = StateGet(S, type, key); \
if (id_##n) \
if (!id_##n) \
{ \
goto finish; \
} \
@ -286,6 +338,7 @@ RoomUserHasMembership(Room * room, HashMap *state, char *user, char *mbr)
membership_value =
JsonValueAsString(JsonGet(membership, 2, "content", "membership"));
ret = StrEquals(membership_value, mbr);
finish:
@ -395,9 +448,20 @@ PopulateEventV1(Room * room, HashMap * event, PduV1 * pdu, ServerPart serv)
* has some ideas on how this could be done(up until stage 5). */
pdu->sender =
StrDuplicate(JsonValueAsString(JsonGet(event, 1, "sender")));
pdu->type =
StrDuplicate(JsonValueAsString(JsonGet(event, 1, "type")));
pdu->redacts = NULL;
if (JsonGet(event, 1, "state_key"))
{
pdu->state_key =
StrDuplicate(JsonValueAsString(JsonGet(event, 1, "state_key")));
}
pdu->auth_events = ArrayCreate();
pdu->origin_server_ts = UtilTsMillis();
pdu->content = JsonDuplicate(event); /* Copy the original JSON data */
pdu->content =
JsonDuplicate(JsonValueAsObject(JsonGet(event, 1, "content")));
pdu->room_id = StrDuplicate(room->id);
pdu->signatures = HashMapCreate();
/* Create a random event ID for this PDU.
* TODO: Optionally verify whenever it's already used, but that
@ -417,10 +481,10 @@ PopulateEventV1(Room * room, HashMap * event, PduV1 * pdu, ServerPart serv)
prev_events = RoomPrevEventsGet(room);
for (i = 0; i < ArraySize(prev_events); i++)
{
HashMap *event = ArrayGet(prev_events, i);
char *event_id = JsonValueAsString(JsonGet(event, 1, "event_id"));
HashMap *event = JsonValueAsObject(ArrayGet(prev_events, i));
JsonValue *event_id = JsonGet(event, 1, "event_id");
ArrayAdd(pdu->prev_events, JsonValueString(event_id));
ArrayAdd(pdu->prev_events, JsonValueDuplicate(event_id));
}
/* TODO: Signature and alldat. */
@ -484,6 +548,11 @@ ValidAuthEventV1(PduV1 *auth_pdu, PduV1 *pdu)
/* TODO: Check if it's the latest in terms of [pdu] */
return true;
}
if (IsState(auth_pdu, "m.room.member", pdu->sender))
{
/* TODO: Check if it's the latest in terms of [pdu] */
return true;
}
if (StrEquals(pdu->type, "m.room.member"))
{
char *membership =
@ -526,7 +595,7 @@ VerifyPDUV1(PduV1 *auth_pdu)
* https://spec.matrix.org/v1.7/server-server-api/
* #checks-performed-on-receipt-of-a-pdu */
(void) auth_pdu;
return false; /* This only shows whenever an event was rejected, not
return true; /* This only shows whenever an event was rejected, not
* soft-failed */
}
static bool
@ -539,15 +608,16 @@ ConsiderAuthEventsV1(Room * room, PduV1 pdu)
state_keytype = HashMapCreate();
for (i = 0; i < ArraySize(pdu.auth_events); i++)
{
char *event_id = ArrayGet(pdu.auth_events, i);
char *event_id = JsonValueAsString(ArrayGet(pdu.auth_events, i));
HashMap *event = RoomEventFetch(room, event_id);
PduV1 auth_pdu;
PduV1 auth_pdu = { 0 };
char *key_type_id;
if (!PduV1FromJson(event, &auth_pdu, &ignored))
{
HashMapFree(event);
JsonFree(event);
HashMapFree(state_keytype);
return false; /* Yeah... we aren't doing that. */
}
@ -559,7 +629,7 @@ ConsiderAuthEventsV1(Room * room, PduV1 pdu)
if (HashMapGet(state_keytype, key_type_id))
{
/* Duplicate found! We actually ignore it's actual value. */
HashMapFree(event);
JsonFree(event);
PduV1Free(&auth_pdu);
HashMapFree(state_keytype);
@ -576,6 +646,9 @@ ConsiderAuthEventsV1(Room * room, PduV1 pdu)
* described in the server specification, reject. */
if (!ValidAuthEventV1(&auth_pdu, &pdu))
{
JsonFree(event);
PduV1Free(&auth_pdu);
HashMapFree(state_keytype);
return false;
}
/* Step 2.3: If there are entries which were themselves rejected
@ -583,6 +656,9 @@ ConsiderAuthEventsV1(Room * room, PduV1 pdu)
* TODO */
if (!VerifyPDUV1(&auth_pdu))
{
PduV1Free(&auth_pdu);
JsonFree(event);
HashMapFree(state_keytype);
return false;
}
@ -593,7 +669,7 @@ ConsiderAuthEventsV1(Room * room, PduV1 pdu)
room_create = true; /* Here, we check for the opposite. */
}
HashMapFree(event);
JsonFree(event);
PduV1Free(&auth_pdu);
}
HashMapFree(state_keytype);
@ -708,8 +784,10 @@ AuthorizeInviteMembershipV1(Room * room, PduV1 pdu, HashMap *state)
thirdpi_event_sender = JsonValueAsString(JsonGet(third_pi_event, 1, "sender"));
if (!StrEquals(thirdpi_event_sender, pdu.sender))
{
JsonFree(third_pi_event);
return false;
}
JsonFree(third_pi_event);
/* TODO:
* Step 5.3.1.7: If any signature in signed matches any public key in
@ -1130,6 +1208,7 @@ RoomAuthoriseEventV1(Room * room, PduV1 pdu, HashMap *state)
}
}
}
JsonFree(create_event);
/* Step 4: If type is m.room.aliases */
if (StrEquals(pdu.type, "m.room.aliases"))
@ -1278,21 +1357,21 @@ RoomEventSendV1(Room * room, HashMap * event)
bool client_event, valid = false;
HashMap *state = NULL;
client_event = PopulateEventV1(room, event, &pdu, RoomGetCreator(room));
client_event = !PopulateEventV1(room, event, &pdu, RoomGetCreator(room));
pdu_object = PduV1ToJson(&pdu);
state = StateResolve(room, pdu_object); /* Compute the state
* at that point for later. */
if (client_event)
{
char *event_id;
char *ev_id;
#define AddState(type, key) do \
{ \
event_id = StateGet(state, type, key); \
if (event_id) \
ev_id = StateGet(state, type, key); \
if (ev_id) \
{ \
char *dup = StrDuplicate(event_id); \
ArrayAdd(pdu.auth_events, dup); \
JsonValue *v = JsonValueString(ev_id); \
ArrayAdd(pdu.auth_events, v); \
} \
} \
while (0)
@ -1470,23 +1549,71 @@ RoomGetCreator(Room *room)
}
return room->creator;
}
bool RoomAddEventV1(Room *room, PduV1 pdu)
bool
RoomAddEventV1(Room *room, PduV1 pdu)
{
DbRef *event_ref;
Array *prev_events, *leaves;
HashMap *leaves_json, *pdu_json;
JsonValue *leaves_val;
char *safe_id;
size_t i;
if (!room || room->version >= 3)
{
return false;
}
/* TODO: Update leaf events. */
leaves_json = DbJson(room->leaves_ref);
leaves_val = JsonValueDuplicate(JsonGet(leaves_json, 1, "leaves"));
leaves = JsonValueAsArray(leaves_val);
Free(leaves_val);
prev_events = pdu.prev_events;
for (i = 0; i < ArraySize(prev_events); i++)
{
JsonValue *event_val = ArrayGet(prev_events, i);
char *event_id = JsonValueAsString(event_val);
size_t j;
ssize_t delete_index = -1;
for (j = 0; j < ArraySize(leaves); j++)
{
JsonValue *leaf_val = ArrayGet(leaves, j);
HashMap *leaf_object = JsonValueAsObject(leaf_val);
char *leaf_id =
JsonValueAsString(JsonGet(leaf_object, 1, "event_id"));
if (StrEquals(leaf_id, event_id))
{
delete_index = j;
break;
}
}
if (delete_index == -1)
{
continue;
}
JsonValueFree(ArrayDelete(leaves, delete_index));
}
safe_id = CreateSafeID(pdu.event_id);
event_ref = DbLock(room->db, 4, "rooms", room->id, "events", safe_id);
event_ref = DbCreate(room->db, 4, "rooms", room->id, "events", safe_id);
Free(safe_id);
DbJsonSet(event_ref, PduV1ToJson(&pdu));
pdu_json = PduV1ToJson(&pdu);
DbJsonSet(event_ref, pdu_json);
ArrayAdd(leaves, JsonValueObject(pdu_json));
leaves_json = JsonDuplicate(leaves_json);
JsonValueFree(HashMapDelete(leaves_json, "leaves"));
JsonSet(leaves_json, JsonValueArray(leaves), 1, "leaves");
DbJsonSet(room->leaves_ref, leaves_json);
JsonFree(leaves_json);
DbUnlock(room->db, event_ref);
/* TODO: Store DAG relationships, somehow. */
/* TODO: Store DAG relationships, somehow.
* Also keep track of PDU depth. */
return true;
}

View file

@ -27,6 +27,7 @@
#include <Routes.h>
#include <Cytoplasm/Json.h>
#include <Cytoplasm/Log.h>
#include <Room.h>
#include <User.h>
@ -91,6 +92,8 @@ ROUTE_IMPL(RouteCreateRoom, path, argp)
JsonFree(request);
request = NULL;
Log(LOG_INFO, "Creating room...");
if (!(room = RoomCreate(db, user, &parsed, server)))
{
err = "Couldn't create room.";

View file

@ -40,9 +40,11 @@ int
V1Cmp(void *a, void *b)
{
HashMap *e1 = a, *e2 = b;
int64_t depth1 =
int64_t depth1, depth2;
depth1 =
JsonValueAsInteger(JsonGet(e1, 1, "depth"));
int64_t depth2 =
depth2 =
JsonValueAsInteger(JsonGet(e2, 1, "depth"));
if (depth1 > depth2)
@ -94,6 +96,7 @@ StateResolveV1(Room * room, Array * states)
if (HashMapGet(R, tuple))
{
Array *arr;
HashMap *hm;
/* Conflicts! */
HashMapDelete(R, tuple);
@ -102,13 +105,14 @@ StateResolveV1(Room * room, Array * states)
{
arr = ArrayCreate();
}
ArrayAdd(arr, RoomEventFetch(room, event_id));
hm = RoomEventFetch(room, event_id);
ArrayAdd(arr, hm);
HashMapSet(conflicts, tuple, arr);
}
else
{
/* Add to R */
HashMapSet(R, tuple, event_id);
HashMapSet(R, tuple, StrDuplicate(event_id));
}
}
@ -138,7 +142,13 @@ StateResolveV1(Room * room, Array * states)
Free(type);
Free(key);
}
ArraySort(events, V1Cmp);
if (ArraySize(events) != 0)
{
/* TODO: Cytoplasm currently doesn't behave correctly
* when the array size is 0, I'll need to file a bug
* report/PR eventually. */
ArraySort(events, V1Cmp);
}
/* Add first event. */
first = ArrayDelete(events, 0);
StateSet(
@ -146,6 +156,7 @@ StateResolveV1(Room * room, Array * states)
JsonValueAsString(JsonGet(first, 1, "type")),
JsonValueAsString(JsonGet(first, 1, "state_key")),
JsonValueAsString(JsonGet(first, 1, "event_id")));
JsonFree(first);
for (j = 0; j < (ssize_t) ArraySize(events); j++)
{
@ -161,10 +172,12 @@ StateResolveV1(Room * room, Array * states)
else
{
PduV1Free(&pdu);
JsonFree(event);
break;
}
(void) msg;
PduV1Free(&pdu);
JsonFree(event);
}
ArrayFree(events);
/* Delete all elements within a key. */
@ -183,13 +196,16 @@ StateResolveV1(Room * room, Array * states)
StateSet(conflicts, t, state_key, NULL);
Free(state_key);
}
Free(state_keys);
ArrayFree(state_keys);
}
ArrayFree(types);
while (StateIterate(conflicts, &type, &key, (void **) &conflicting))
{
ArraySort(conflicting, V1Cmp);
if (ArraySize(conflicting) != 0)
{
ArraySort(conflicting, V1Cmp);
}
for (j = ArraySize(conflicting) - 1; j >= 0; j--)
{
HashMap *event = ArrayGet(events, j);
@ -210,8 +226,7 @@ StateResolveV1(Room * room, Array * states)
Free(type);
Free(key);
}
while (StateIterate(conflicts, &type, &key, (void **) &conflicting))
while (HashMapIterate(conflicts, &type, (void **) &conflicting))
{
for (i = 0; i < ArraySize(conflicting); i++)
{
@ -239,6 +254,8 @@ StateResolve(Room * room, HashMap * event)
Array *prevEvents;
HashMap *ret_state;
if (!room || !event)
{
return NULL;
@ -251,26 +268,46 @@ StateResolve(Room * room, HashMap * event)
{
return NULL;
}
prevEvents = HashMapGet(event, "prev_events");
prevEvents = JsonValueAsArray(HashMapGet(event, "prev_events"));
for (i = 0; i < ArraySize(prevEvents); i++)
{
HashMap *prevEvent = ArrayGet(prevEvents, i);
HashMap *prevEvent =
RoomEventFetch(room, JsonValueAsString(ArrayGet(prevEvents, i)));
HashMap *state = StateResolve(room, prevEvent);
/* TODO: Apply prevEvent to state if it is a state event */
if (HashMapGet(prevEvent, "state_key"))
{
StateSet(
state,
JsonValueAsString(HashMapGet(prevEvent, "type")),
JsonValueAsString(HashMapGet(prevEvent, "state_key")),
JsonValueAsString(HashMapGet(prevEvent, "event_id")));
}
ArrayAdd(states, state);
JsonFree(prevEvent);
}
ret_state = NULL;
switch (RoomVersionGet(room))
{
case 1:
return StateResolveV1(room, states);
ret_state = StateResolveV1(room, states);
break;
default:
return StateResolveV2(states);
ret_state = StateResolveV2(states);
break;
}
for (i = 0; i < ArraySize(states); i++)
{
HashMap *state = ArrayGet(states, i);
StateFree(state);
}
ArrayFree(states);
return ret_state;
}
bool StateIterate(HashMap *state, char **type, char **key, void **event)
{
@ -282,11 +319,14 @@ bool StateIterate(HashMap *state, char **type, char **key, void **event)
}
ret = HashMapIterate(state, &tuple, event);
tuple = StrDuplicate(tuple);
*(strchr(tuple, ',')) = '\0';
if (ret)
{
tuple = StrDuplicate(tuple);
*(strchr(tuple, ',')) = '\0';
*type = tuple;
*key = StrDuplicate(tuple + strlen(tuple) + 1);
*type = tuple;
*key = StrDuplicate(tuple + strlen(tuple) + 1);
}
return ret;
}