Parse unsigned key

This commit is contained in:
Konstantinos Sideris 2017-11-06 22:14:16 +02:00
parent 9cf5bf3833
commit 24dd76ee0b
3 changed files with 181 additions and 117 deletions

View file

@ -60,6 +60,24 @@ isMessageEvent(EventType type);
bool bool
isStateEvent(EventType type); isStateEvent(EventType type);
class UnsignedData
: public Deserializable
, public Serializable
{
public:
double age() const { return age_; }
QString transactionId() const { return transaction_id_; }
bool isEmpty() const { return age_ <= 0 && transaction_id_.isEmpty(); }
void deserialize(const QJsonValue &data) override;
QJsonObject serialize() const override;
private:
double age_ = 0;
QString transaction_id_;
};
template<class Content> template<class Content>
class Event class Event
: public Deserializable : public Deserializable
@ -68,6 +86,7 @@ class Event
public: public:
Content content() const; Content content() const;
EventType eventType() const; EventType eventType() const;
UnsignedData unsignedData() const { return unsignedData_; }
void deserialize(const QJsonValue &data) override; void deserialize(const QJsonValue &data) override;
QJsonObject serialize() const override; QJsonObject serialize() const override;
@ -75,6 +94,7 @@ public:
private: private:
Content content_; Content content_;
EventType type_; EventType type_;
UnsignedData unsignedData_;
}; };
template<class Content> template<class Content>
@ -102,6 +122,9 @@ Event<Content>::deserialize(const QJsonValue &data)
content_.deserialize(object.value("content")); content_.deserialize(object.value("content"));
type_ = extractEventType(object); type_ = extractEventType(object);
if (object.contains("unsigned"))
unsignedData_.deserialize(object.value("unsigned"));
} }
template<class Content> template<class Content>
@ -151,6 +174,9 @@ Event<Content>::serialize() const
object["content"] = content_.serialize(); object["content"] = content_.serialize();
if (!unsignedData_.isEmpty())
object["unsigned"] = unsignedData_.serialize();
return object; return object;
} }
} // namespace events } // namespace events

View file

@ -78,3 +78,29 @@ matrix::events::isMessageEvent(EventType type)
{ {
return type == EventType::RoomMessage; return type == EventType::RoomMessage;
} }
void
matrix::events::UnsignedData::deserialize(const QJsonValue &data)
{
if (!data.isObject())
throw DeserializationException("UnsignedData is not a JSON object");
auto object = data.toObject();
transaction_id_ = object.value("transaction_id").toString();
age_ = object.value("age").toDouble();
}
QJsonObject
matrix::events::UnsignedData::serialize() const
{
QJsonObject object;
if (!transaction_id_.isEmpty())
object["transaction_id"] = transaction_id_;
if (age_ > 0)
object["age"] = age_;
return object;
}

View file

@ -22,8 +22,8 @@ using namespace matrix::events;
TEST(BaseEvent, Deserialization) TEST(BaseEvent, Deserialization)
{ {
// NameEventContent // NameEventContent
auto data = QJsonObject{ { "content", QJsonObject{ { "name", "Room Name" } } }, auto data =
{ "type", "m.room.name" } }; QJsonObject{{"content", QJsonObject{{"name", "Room Name"}}}, {"type", "m.room.name"}};
Event<NameEventContent> name_event; Event<NameEventContent> name_event;
name_event.deserialize(data); name_event.deserialize(data);
@ -31,47 +31,57 @@ TEST(BaseEvent, Deserialization)
EXPECT_EQ(name_event.serialize(), data); EXPECT_EQ(name_event.serialize(), data);
// TopicEventContent // TopicEventContent
data = QJsonObject{ { "content", QJsonObject{ { "topic", "Room Topic" } } }, data = QJsonObject{{"content", QJsonObject{{"topic", "Room Topic"}}},
{ "type", "m.room.topic" } }; {"unsigned", QJsonObject{{"age", 22}, {"transaction_id", "randomid"}}},
{"type", "m.room.topic"}};
Event<TopicEventContent> topic_event; Event<TopicEventContent> topic_event;
topic_event.deserialize(data); topic_event.deserialize(data);
EXPECT_EQ(topic_event.content().topic(), "Room Topic"); EXPECT_EQ(topic_event.content().topic(), "Room Topic");
EXPECT_EQ(topic_event.unsignedData().age(), 22);
EXPECT_EQ(topic_event.unsignedData().transactionId(), "randomid");
EXPECT_EQ(topic_event.serialize(), data); EXPECT_EQ(topic_event.serialize(), data);
// AvatarEventContent // AvatarEventContent
data = QJsonObject{ { "content", QJsonObject{ { "url", "https://matrix.org" } } }, data = QJsonObject{
{ "type", "m.room.avatar" } }; {"content", QJsonObject{{"url", "https://matrix.org"}}},
{"unsigned", QJsonObject{{"age", 1343434343}, {"transaction_id", "m33434.33"}}},
{"type", "m.room.avatar"}};
Event<AvatarEventContent> avatar_event; Event<AvatarEventContent> avatar_event;
avatar_event.deserialize(data); avatar_event.deserialize(data);
EXPECT_EQ(avatar_event.content().url().toString(), "https://matrix.org"); EXPECT_EQ(avatar_event.content().url().toString(), "https://matrix.org");
EXPECT_EQ(avatar_event.unsignedData().age(), 1343434343);
EXPECT_EQ(avatar_event.unsignedData().transactionId(), "m33434.33");
EXPECT_EQ(avatar_event.serialize(), data); EXPECT_EQ(avatar_event.serialize(), data);
// AliasesEventContent // AliasesEventContent
data = data = QJsonObject{
QJsonObject{ { "content", {"content",
QJsonObject{ QJsonObject{{"aliases", QJsonArray{"#test:matrix.org", "#test2:matrix.org"}}}},
{ "aliases", QJsonArray{ "#test:matrix.org", "#test2:matrix.org" } } } }, {"unsigned", QJsonObject{{"transaction_id", "m33434.33"}}},
{ "type", "m.room.aliases" } }; {"type", "m.room.aliases"}};
Event<AliasesEventContent> aliases_event; Event<AliasesEventContent> aliases_event;
aliases_event.deserialize(data); aliases_event.deserialize(data);
EXPECT_EQ(aliases_event.content().aliases().size(), 2); EXPECT_EQ(aliases_event.content().aliases().size(), 2);
EXPECT_EQ(aliases_event.unsignedData().transactionId(), "m33434.33");
EXPECT_EQ(aliases_event.serialize(), data); EXPECT_EQ(aliases_event.serialize(), data);
// CreateEventContent // CreateEventContent
data = QJsonObject{ { "content", QJsonObject{ { "creator", "@alice:matrix.org" } } }, data = QJsonObject{{"content", QJsonObject{{"creator", "@alice:matrix.org"}}},
{ "type", "m.room.create" } }; {"unsigned", QJsonObject{{"age", 2233}}},
{"type", "m.room.create"}};
Event<CreateEventContent> create_event; Event<CreateEventContent> create_event;
create_event.deserialize(data); create_event.deserialize(data);
EXPECT_EQ(create_event.content().creator(), "@alice:matrix.org"); EXPECT_EQ(create_event.content().creator(), "@alice:matrix.org");
EXPECT_EQ(create_event.unsignedData().age(), 2233);
EXPECT_EQ(create_event.serialize(), data); EXPECT_EQ(create_event.serialize(), data);
// JoinRulesEventContent // JoinRulesEventContent
data = QJsonObject{ { "content", QJsonObject{ { "join_rule", "private" } } }, data = QJsonObject{{"content", QJsonObject{{"join_rule", "private"}}},
{ "type", "m.room.join_rules" } }; {"type", "m.room.join_rules"}};
Event<JoinRulesEventContent> join_rules_event; Event<JoinRulesEventContent> join_rules_event;
join_rules_event.deserialize(data); join_rules_event.deserialize(data);
@ -81,27 +91,34 @@ TEST(BaseEvent, Deserialization)
TEST(BaseEvent, DeserializationException) TEST(BaseEvent, DeserializationException)
{ {
auto data = QJsonObject{ { "content", QJsonObject{ { "rule", "private" } } }, auto data =
{ "type", "m.room.join_rules" } }; QJsonObject{{"content", QJsonObject{{"rule", "private"}}}, {"type", "m.room.join_rules"}};
Event<JoinRulesEventContent> event1; Event<JoinRulesEventContent> event1;
ASSERT_THROW(event1.deserialize(data), DeserializationException); ASSERT_THROW(event1.deserialize(data), DeserializationException);
data = QJsonObject{ { "contents", QJsonObject{ { "join_rule", "private" } } }, data = QJsonObject{{"contents", QJsonObject{{"join_rule", "private"}}},
{ "type", "m.room.join_rules" } }; {"type", "m.room.join_rules"}};
Event<JoinRulesEventContent> event2; Event<JoinRulesEventContent> event2;
ASSERT_THROW(event2.deserialize(data), DeserializationException); ASSERT_THROW(event2.deserialize(data), DeserializationException);
data = QJsonObject{{"contents", QJsonObject{{"join_rule", "private"}}},
{"unsigned", QJsonObject{{"age", "222"}}},
{"type", "m.room.join_rules"}};
Event<JoinRulesEventContent> event3;
ASSERT_THROW(event3.deserialize(data), DeserializationException);
} }
TEST(RoomEvent, Deserialization) TEST(RoomEvent, Deserialization)
{ {
auto data = QJsonObject{ { "content", QJsonObject{ { "name", "Name" } } }, auto data = QJsonObject{{"content", QJsonObject{{"name", "Name"}}},
{ "event_id", "$asdfafdf8af:matrix.org" }, {"event_id", "$asdfafdf8af:matrix.org"},
{ "room_id", "!aasdfaeae23r9:matrix.org" }, {"room_id", "!aasdfaeae23r9:matrix.org"},
{ "sender", "@alice:matrix.org" }, {"sender", "@alice:matrix.org"},
{ "origin_server_ts", 1323238293289323LL }, {"origin_server_ts", 1323238293289323LL},
{ "type", "m.room.name" } }; {"type", "m.room.name"}};
RoomEvent<NameEventContent> event; RoomEvent<NameEventContent> event;
event.deserialize(data); event.deserialize(data);
@ -116,11 +133,11 @@ TEST(RoomEvent, Deserialization)
TEST(RoomEvent, DeserializationException) TEST(RoomEvent, DeserializationException)
{ {
auto data = QJsonObject{ { "content", QJsonObject{ { "name", "Name" } } }, auto data = QJsonObject{{"content", QJsonObject{{"name", "Name"}}},
{ "event_id", "$asdfafdf8af:matrix.org" }, {"event_id", "$asdfafdf8af:matrix.org"},
{ "room_id", "!aasdfaeae23r9:matrix.org" }, {"room_id", "!aasdfaeae23r9:matrix.org"},
{ "origin_server_ts", 1323238293289323LL }, {"origin_server_ts", 1323238293289323LL},
{ "type", "m.room.name" } }; {"type", "m.room.name"}};
RoomEvent<NameEventContent> event; RoomEvent<NameEventContent> event;
@ -133,14 +150,14 @@ TEST(RoomEvent, DeserializationException)
TEST(StateEvent, Deserialization) TEST(StateEvent, Deserialization)
{ {
auto data = QJsonObject{ { "content", QJsonObject{ { "name", "Name" } } }, auto data = QJsonObject{{"content", QJsonObject{{"name", "Name"}}},
{ "event_id", "$asdfafdf8af:matrix.org" }, {"event_id", "$asdfafdf8af:matrix.org"},
{ "state_key", "some_state_key" }, {"state_key", "some_state_key"},
{ "prev_content", QJsonObject{ { "name", "Previous Name" } } }, {"prev_content", QJsonObject{{"name", "Previous Name"}}},
{ "room_id", "!aasdfaeae23r9:matrix.org" }, {"room_id", "!aasdfaeae23r9:matrix.org"},
{ "sender", "@alice:matrix.org" }, {"sender", "@alice:matrix.org"},
{ "origin_server_ts", 1323238293289323LL }, {"origin_server_ts", 1323238293289323LL},
{ "type", "m.room.name" } }; {"type", "m.room.name"}};
StateEvent<NameEventContent> event; StateEvent<NameEventContent> event;
event.deserialize(data); event.deserialize(data);
@ -157,13 +174,13 @@ TEST(StateEvent, Deserialization)
TEST(StateEvent, DeserializationException) TEST(StateEvent, DeserializationException)
{ {
auto data = QJsonObject{ { "content", QJsonObject{ { "name", "Name" } } }, auto data = QJsonObject{{"content", QJsonObject{{"name", "Name"}}},
{ "event_id", "$asdfafdf8af:matrix.org" }, {"event_id", "$asdfafdf8af:matrix.org"},
{ "prev_content", QJsonObject{ { "name", "Previous Name" } } }, {"prev_content", QJsonObject{{"name", "Previous Name"}}},
{ "room_id", "!aasdfaeae23r9:matrix.org" }, {"room_id", "!aasdfaeae23r9:matrix.org"},
{ "sender", "@alice:matrix.org" }, {"sender", "@alice:matrix.org"},
{ "origin_server_ts", 1323238293289323LL }, {"origin_server_ts", 1323238293289323LL},
{ "type", "m.room.name" } }; {"type", "m.room.name"}};
StateEvent<NameEventContent> event; StateEvent<NameEventContent> event;
@ -176,35 +193,30 @@ TEST(StateEvent, DeserializationException)
TEST(EventType, Mapping) TEST(EventType, Mapping)
{ {
EXPECT_EQ(extractEventType(QJsonObject{ { "type", "m.room.aliases" } }), EXPECT_EQ(extractEventType(QJsonObject{{"type", "m.room.aliases"}}),
EventType::RoomAliases); EventType::RoomAliases);
EXPECT_EQ(extractEventType(QJsonObject{ { "type", "m.room.avatar" } }), EXPECT_EQ(extractEventType(QJsonObject{{"type", "m.room.avatar"}}), EventType::RoomAvatar);
EventType::RoomAvatar); EXPECT_EQ(extractEventType(QJsonObject{{"type", "m.room.canonical_alias"}}),
EXPECT_EQ(extractEventType(QJsonObject{ { "type", "m.room.canonical_alias" } }),
EventType::RoomCanonicalAlias); EventType::RoomCanonicalAlias);
EXPECT_EQ(extractEventType(QJsonObject{ { "type", "m.room.create" } }), EXPECT_EQ(extractEventType(QJsonObject{{"type", "m.room.create"}}), EventType::RoomCreate);
EventType::RoomCreate); EXPECT_EQ(extractEventType(QJsonObject{{"type", "m.room.history_visibility"}}),
EXPECT_EQ(extractEventType(QJsonObject{ { "type", "m.room.history_visibility" } }),
EventType::RoomHistoryVisibility); EventType::RoomHistoryVisibility);
EXPECT_EQ(extractEventType(QJsonObject{ { "type", "m.room.join_rules" } }), EXPECT_EQ(extractEventType(QJsonObject{{"type", "m.room.join_rules"}}),
EventType::RoomJoinRules); EventType::RoomJoinRules);
EXPECT_EQ(extractEventType(QJsonObject{ { "type", "m.room.member" } }), EXPECT_EQ(extractEventType(QJsonObject{{"type", "m.room.member"}}), EventType::RoomMember);
EventType::RoomMember); EXPECT_EQ(extractEventType(QJsonObject{{"type", "m.room.message"}}),
EXPECT_EQ(extractEventType(QJsonObject{ { "type", "m.room.message" } }),
EventType::RoomMessage); EventType::RoomMessage);
EXPECT_EQ(extractEventType(QJsonObject{ { "type", "m.room.name" } }), EventType::RoomName); EXPECT_EQ(extractEventType(QJsonObject{{"type", "m.room.name"}}), EventType::RoomName);
EXPECT_EQ(extractEventType(QJsonObject{ { "type", "m.room.power_levels" } }), EXPECT_EQ(extractEventType(QJsonObject{{"type", "m.room.power_levels"}}),
EventType::RoomPowerLevels); EventType::RoomPowerLevels);
EXPECT_EQ(extractEventType(QJsonObject{ { "type", "m.room.topic" } }), EXPECT_EQ(extractEventType(QJsonObject{{"type", "m.room.topic"}}), EventType::RoomTopic);
EventType::RoomTopic); EXPECT_EQ(extractEventType(QJsonObject{{"type", "m.room.unknown"}}),
EXPECT_EQ(extractEventType(QJsonObject{ { "type", "m.room.unknown" } }),
EventType::Unsupported); EventType::Unsupported);
} }
TEST(AliasesEventContent, Deserialization) TEST(AliasesEventContent, Deserialization)
{ {
auto data = auto data = QJsonObject{{"aliases", QJsonArray{"#test:matrix.org", "#test2:matrix.org"}}};
QJsonObject{ { "aliases", QJsonArray{ "#test:matrix.org", "#test2:matrix.org" } } };
AliasesEventContent content; AliasesEventContent content;
content.deserialize(data); content.deserialize(data);
@ -215,7 +227,7 @@ TEST(AliasesEventContent, Deserialization)
TEST(AliasesEventContent, NotAnObject) TEST(AliasesEventContent, NotAnObject)
{ {
auto data = QJsonArray{ "#test:matrix.org", "#test2:matrix.org" }; auto data = QJsonArray{"#test:matrix.org", "#test2:matrix.org"};
AliasesEventContent content; AliasesEventContent content;
ASSERT_THROW(content.deserialize(data), DeserializationException); ASSERT_THROW(content.deserialize(data), DeserializationException);
@ -223,7 +235,7 @@ TEST(AliasesEventContent, NotAnObject)
TEST(AliasesEventContent, MissingKey) TEST(AliasesEventContent, MissingKey)
{ {
auto data = QJsonObject{ { "key", QJsonArray{ "#test:matrix.org", "#test2:matrix.org" } } }; auto data = QJsonObject{{"key", QJsonArray{"#test:matrix.org", "#test2:matrix.org"}}};
AliasesEventContent content; AliasesEventContent content;
ASSERT_THROW(content.deserialize(data), DeserializationException); ASSERT_THROW(content.deserialize(data), DeserializationException);
@ -237,7 +249,7 @@ TEST(AliasesEventContent, MissingKey)
TEST(AvatarEventContent, Deserialization) TEST(AvatarEventContent, Deserialization)
{ {
auto data = QJsonObject{ { "url", "https://matrix.org/avatar.png" } }; auto data = QJsonObject{{"url", "https://matrix.org/avatar.png"}};
AvatarEventContent content; AvatarEventContent content;
content.deserialize(data); content.deserialize(data);
@ -248,7 +260,7 @@ TEST(AvatarEventContent, Deserialization)
TEST(AvatarEventContent, NotAnObject) TEST(AvatarEventContent, NotAnObject)
{ {
auto data = QJsonArray{ "key", "url" }; auto data = QJsonArray{"key", "url"};
AvatarEventContent content; AvatarEventContent content;
ASSERT_THROW(content.deserialize(data), DeserializationException); ASSERT_THROW(content.deserialize(data), DeserializationException);
@ -256,7 +268,7 @@ TEST(AvatarEventContent, NotAnObject)
TEST(AvatarEventContent, MissingKey) TEST(AvatarEventContent, MissingKey)
{ {
auto data = QJsonObject{ { "key", "https://matrix.org" } }; auto data = QJsonObject{{"key", "https://matrix.org"}};
AvatarEventContent content; AvatarEventContent content;
ASSERT_THROW(content.deserialize(data), DeserializationException); ASSERT_THROW(content.deserialize(data), DeserializationException);
@ -270,7 +282,7 @@ TEST(AvatarEventContent, MissingKey)
TEST(CreateEventContent, Deserialization) TEST(CreateEventContent, Deserialization)
{ {
auto data = QJsonObject{ { "creator", "@alice:matrix.org" } }; auto data = QJsonObject{{"creator", "@alice:matrix.org"}};
CreateEventContent content; CreateEventContent content;
content.deserialize(data); content.deserialize(data);
@ -281,7 +293,7 @@ TEST(CreateEventContent, Deserialization)
TEST(CreateEventContent, NotAnObject) TEST(CreateEventContent, NotAnObject)
{ {
auto data = QJsonArray{ "creator", "alice" }; auto data = QJsonArray{"creator", "alice"};
CreateEventContent content; CreateEventContent content;
@ -290,7 +302,7 @@ TEST(CreateEventContent, NotAnObject)
TEST(CreateEventContent, MissingKey) TEST(CreateEventContent, MissingKey)
{ {
auto data = QJsonObject{ { "key", "@alice:matrix.org" } }; auto data = QJsonObject{{"key", "@alice:matrix.org"}};
CreateEventContent content; CreateEventContent content;
ASSERT_THROW(content.deserialize(data), DeserializationException); ASSERT_THROW(content.deserialize(data), DeserializationException);
@ -304,23 +316,23 @@ TEST(CreateEventContent, MissingKey)
TEST(HistoryVisibilityEventContent, Deserialization) TEST(HistoryVisibilityEventContent, Deserialization)
{ {
auto data = QJsonObject{ { "history_visibility", "invited" } }; auto data = QJsonObject{{"history_visibility", "invited"}};
HistoryVisibilityEventContent content; HistoryVisibilityEventContent content;
content.deserialize(data); content.deserialize(data);
EXPECT_EQ(content.historyVisibility(), HistoryVisibility::Invited); EXPECT_EQ(content.historyVisibility(), HistoryVisibility::Invited);
data = QJsonObject{ { "history_visibility", "joined" } }; data = QJsonObject{{"history_visibility", "joined"}};
content.deserialize(data); content.deserialize(data);
EXPECT_EQ(content.historyVisibility(), HistoryVisibility::Joined); EXPECT_EQ(content.historyVisibility(), HistoryVisibility::Joined);
data = QJsonObject{ { "history_visibility", "shared" } }; data = QJsonObject{{"history_visibility", "shared"}};
content.deserialize(data); content.deserialize(data);
EXPECT_EQ(content.historyVisibility(), HistoryVisibility::Shared); EXPECT_EQ(content.historyVisibility(), HistoryVisibility::Shared);
data = QJsonObject{ { "history_visibility", "world_readable" } }; data = QJsonObject{{"history_visibility", "world_readable"}};
content.deserialize(data); content.deserialize(data);
EXPECT_EQ(content.historyVisibility(), HistoryVisibility::WorldReadable); EXPECT_EQ(content.historyVisibility(), HistoryVisibility::WorldReadable);
@ -328,7 +340,7 @@ TEST(HistoryVisibilityEventContent, Deserialization)
TEST(HistoryVisibilityEventContent, NotAnObject) TEST(HistoryVisibilityEventContent, NotAnObject)
{ {
auto data = QJsonArray{ "history_visibility", "alice" }; auto data = QJsonArray{"history_visibility", "alice"};
HistoryVisibilityEventContent content; HistoryVisibilityEventContent content;
@ -337,7 +349,7 @@ TEST(HistoryVisibilityEventContent, NotAnObject)
TEST(HistoryVisibilityEventContent, InvalidHistoryVisibility) TEST(HistoryVisibilityEventContent, InvalidHistoryVisibility)
{ {
auto data = QJsonObject{ { "history_visibility", "wrong" } }; auto data = QJsonObject{{"history_visibility", "wrong"}};
HistoryVisibilityEventContent content; HistoryVisibilityEventContent content;
ASSERT_THROW(content.deserialize(data), DeserializationException); ASSERT_THROW(content.deserialize(data), DeserializationException);
@ -351,7 +363,7 @@ TEST(HistoryVisibilityEventContent, InvalidHistoryVisibility)
TEST(HistoryVisibilityEventContent, MissingKey) TEST(HistoryVisibilityEventContent, MissingKey)
{ {
auto data = QJsonObject{ { "key", "joined" } }; auto data = QJsonObject{{"key", "joined"}};
HistoryVisibilityEventContent content; HistoryVisibilityEventContent content;
ASSERT_THROW(content.deserialize(data), DeserializationException); ASSERT_THROW(content.deserialize(data), DeserializationException);
@ -365,23 +377,23 @@ TEST(HistoryVisibilityEventContent, MissingKey)
TEST(JoinRulesEventContent, Deserialization) TEST(JoinRulesEventContent, Deserialization)
{ {
auto data = QJsonObject{ { "join_rule", "invite" } }; auto data = QJsonObject{{"join_rule", "invite"}};
JoinRulesEventContent content; JoinRulesEventContent content;
content.deserialize(data); content.deserialize(data);
EXPECT_EQ(content.joinRule(), JoinRule::Invite); EXPECT_EQ(content.joinRule(), JoinRule::Invite);
data = QJsonObject{ { "join_rule", "knock" } }; data = QJsonObject{{"join_rule", "knock"}};
content.deserialize(data); content.deserialize(data);
EXPECT_EQ(content.joinRule(), JoinRule::Knock); EXPECT_EQ(content.joinRule(), JoinRule::Knock);
data = QJsonObject{ { "join_rule", "private" } }; data = QJsonObject{{"join_rule", "private"}};
content.deserialize(data); content.deserialize(data);
EXPECT_EQ(content.joinRule(), JoinRule::Private); EXPECT_EQ(content.joinRule(), JoinRule::Private);
data = QJsonObject{ { "join_rule", "public" } }; data = QJsonObject{{"join_rule", "public"}};
content.deserialize(data); content.deserialize(data);
EXPECT_EQ(content.joinRule(), JoinRule::Public); EXPECT_EQ(content.joinRule(), JoinRule::Public);
@ -389,7 +401,7 @@ TEST(JoinRulesEventContent, Deserialization)
TEST(JoinRulesEventContent, NotAnObject) TEST(JoinRulesEventContent, NotAnObject)
{ {
auto data = QJsonArray{ "rule", "alice" }; auto data = QJsonArray{"rule", "alice"};
JoinRulesEventContent content; JoinRulesEventContent content;
@ -398,7 +410,7 @@ TEST(JoinRulesEventContent, NotAnObject)
TEST(JoinRulesEventContent, InvalidHistoryVisibility) TEST(JoinRulesEventContent, InvalidHistoryVisibility)
{ {
auto data = QJsonObject{ { "join_rule", "wrong" } }; auto data = QJsonObject{{"join_rule", "wrong"}};
JoinRulesEventContent content; JoinRulesEventContent content;
ASSERT_THROW(content.deserialize(data), DeserializationException); ASSERT_THROW(content.deserialize(data), DeserializationException);
@ -412,7 +424,7 @@ TEST(JoinRulesEventContent, InvalidHistoryVisibility)
TEST(JoinRulesEventContent, MissingKey) TEST(JoinRulesEventContent, MissingKey)
{ {
auto data = QJsonObject{ { "key", "invite" } }; auto data = QJsonObject{{"key", "invite"}};
JoinRulesEventContent content; JoinRulesEventContent content;
ASSERT_THROW(content.deserialize(data), DeserializationException); ASSERT_THROW(content.deserialize(data), DeserializationException);
@ -426,7 +438,7 @@ TEST(JoinRulesEventContent, MissingKey)
TEST(CanonicalAliasEventContent, Deserialization) TEST(CanonicalAliasEventContent, Deserialization)
{ {
auto data = QJsonObject{ { "alias", "Room Alias" } }; auto data = QJsonObject{{"alias", "Room Alias"}};
CanonicalAliasEventContent content; CanonicalAliasEventContent content;
content.deserialize(data); content.deserialize(data);
@ -437,7 +449,7 @@ TEST(CanonicalAliasEventContent, Deserialization)
TEST(CanonicalAliasEventContent, NotAnObject) TEST(CanonicalAliasEventContent, NotAnObject)
{ {
auto data = QJsonArray{ "alias", "Room Alias" }; auto data = QJsonArray{"alias", "Room Alias"};
CanonicalAliasEventContent content; CanonicalAliasEventContent content;
@ -446,7 +458,7 @@ TEST(CanonicalAliasEventContent, NotAnObject)
TEST(CanonicalAliasEventContent, MissingKey) TEST(CanonicalAliasEventContent, MissingKey)
{ {
auto data = QJsonObject{ { "key", "alias" } }; auto data = QJsonObject{{"key", "alias"}};
CanonicalAliasEventContent content; CanonicalAliasEventContent content;
ASSERT_THROW(content.deserialize(data), DeserializationException); ASSERT_THROW(content.deserialize(data), DeserializationException);
@ -462,29 +474,29 @@ TEST(MemberEventContent, Deserialization)
{ {
MemberEventContent content; MemberEventContent content;
auto data = QJsonObject{ { "membership", "join" } }; auto data = QJsonObject{{"membership", "join"}};
content.deserialize(data); content.deserialize(data);
EXPECT_EQ(content.membershipState(), Membership::Join); EXPECT_EQ(content.membershipState(), Membership::Join);
data = QJsonObject{ { "membership", "invite" }, { "displayname", "Username" } }; data = QJsonObject{{"membership", "invite"}, {"displayname", "Username"}};
content.deserialize(data); content.deserialize(data);
EXPECT_EQ(content.membershipState(), Membership::Invite); EXPECT_EQ(content.membershipState(), Membership::Invite);
EXPECT_EQ(content.displayName(), "Username"); EXPECT_EQ(content.displayName(), "Username");
data = QJsonObject{ { "membership", "leave" }, { "avatar_url", "https://matrix.org" } }; data = QJsonObject{{"membership", "leave"}, {"avatar_url", "https://matrix.org"}};
content.deserialize(data); content.deserialize(data);
EXPECT_EQ(content.membershipState(), Membership::Leave); EXPECT_EQ(content.membershipState(), Membership::Leave);
EXPECT_EQ(content.avatarUrl().toString(), "https://matrix.org"); EXPECT_EQ(content.avatarUrl().toString(), "https://matrix.org");
data = QJsonObject{ { "membership", "ban" } }; data = QJsonObject{{"membership", "ban"}};
content.deserialize(data); content.deserialize(data);
EXPECT_EQ(content.membershipState(), Membership::Ban); EXPECT_EQ(content.membershipState(), Membership::Ban);
data = QJsonObject{ { "membership", "knock" } }; data = QJsonObject{{"membership", "knock"}};
content.deserialize(data); content.deserialize(data);
EXPECT_EQ(content.membershipState(), Membership::Knock); EXPECT_EQ(content.membershipState(), Membership::Knock);
@ -492,7 +504,7 @@ TEST(MemberEventContent, Deserialization)
TEST(MemberEventContent, InvalidMembership) TEST(MemberEventContent, InvalidMembership)
{ {
auto data = QJsonObject{ { "membership", "wrong" } }; auto data = QJsonObject{{"membership", "wrong"}};
MemberEventContent content; MemberEventContent content;
ASSERT_THROW(content.deserialize(data), DeserializationException); ASSERT_THROW(content.deserialize(data), DeserializationException);
@ -506,7 +518,7 @@ TEST(MemberEventContent, InvalidMembership)
TEST(MemberEventContent, NotAnObject) TEST(MemberEventContent, NotAnObject)
{ {
auto data = QJsonArray{ "name", "join" }; auto data = QJsonArray{"name", "join"};
MemberEventContent content; MemberEventContent content;
@ -515,7 +527,7 @@ TEST(MemberEventContent, NotAnObject)
TEST(MemberEventContent, MissingName) TEST(MemberEventContent, MissingName)
{ {
auto data = QJsonObject{ { "key", "random" } }; auto data = QJsonObject{{"key", "random"}};
MemberEventContent content; MemberEventContent content;
ASSERT_THROW(content.deserialize(data), DeserializationException); ASSERT_THROW(content.deserialize(data), DeserializationException);
@ -529,7 +541,7 @@ TEST(MemberEventContent, MissingName)
TEST(NameEventContent, Deserialization) TEST(NameEventContent, Deserialization)
{ {
auto data = QJsonObject{ { "name", "Room Name" } }; auto data = QJsonObject{{"name", "Room Name"}};
NameEventContent content; NameEventContent content;
content.deserialize(data); content.deserialize(data);
@ -540,7 +552,7 @@ TEST(NameEventContent, Deserialization)
TEST(NameEventContent, NotAnObject) TEST(NameEventContent, NotAnObject)
{ {
auto data = QJsonArray{ "name", "Room Name" }; auto data = QJsonArray{"name", "Room Name"};
NameEventContent content; NameEventContent content;
@ -549,7 +561,7 @@ TEST(NameEventContent, NotAnObject)
TEST(NameEventContent, MissingName) TEST(NameEventContent, MissingName)
{ {
auto data = QJsonObject{ { "key", "Room Name" } }; auto data = QJsonObject{{"key", "Room Name"}};
NameEventContent content; NameEventContent content;
ASSERT_THROW(content.deserialize(data), DeserializationException); ASSERT_THROW(content.deserialize(data), DeserializationException);
@ -582,17 +594,17 @@ TEST(PowerLevelsEventContent, DefaultValues)
TEST(PowerLevelsEventContent, FullDeserialization) TEST(PowerLevelsEventContent, FullDeserialization)
{ {
auto data = QJsonObject{ auto data = QJsonObject{
{ "ban", 1 }, {"ban", 1},
{ "invite", 2 }, {"invite", 2},
{ "kick", 3 }, {"kick", 3},
{ "redact", 4 }, {"redact", 4},
{ "events_default", 5 }, {"events_default", 5},
{ "state_default", 6 }, {"state_default", 6},
{ "users_default", 7 }, {"users_default", 7},
{ "events", QJsonObject{ { "m.message.text", 8 }, { "m.message.image", 9 } } }, {"events", QJsonObject{{"m.message.text", 8}, {"m.message.image", 9}}},
{ "users", QJsonObject{ { "@alice:matrix.org", 10 }, { "@bob:matrix.org", 11 } } }, {"users", QJsonObject{{"@alice:matrix.org", 10}, {"@bob:matrix.org", 11}}},
}; };
PowerLevelsEventContent power_levels; PowerLevelsEventContent power_levels;
@ -621,13 +633,13 @@ TEST(PowerLevelsEventContent, FullDeserialization)
TEST(PowerLevelsEventContent, PartialDeserialization) TEST(PowerLevelsEventContent, PartialDeserialization)
{ {
auto data = QJsonObject{ auto data = QJsonObject{
{ "ban", 1 }, {"ban", 1},
{ "invite", 2 }, {"invite", 2},
{ "events_default", 5 }, {"events_default", 5},
{ "users_default", 7 }, {"users_default", 7},
{ "users", QJsonObject{ { "@alice:matrix.org", 10 }, { "@bob:matrix.org", 11 } } }, {"users", QJsonObject{{"@alice:matrix.org", 10}, {"@bob:matrix.org", 11}}},
}; };
PowerLevelsEventContent power_levels; PowerLevelsEventContent power_levels;
@ -653,7 +665,7 @@ TEST(PowerLevelsEventContent, PartialDeserialization)
TEST(PowerLevelsEventContent, NotAnObject) TEST(PowerLevelsEventContent, NotAnObject)
{ {
auto data = QJsonArray{ "test", "test2" }; auto data = QJsonArray{"test", "test2"};
PowerLevelsEventContent power_levels; PowerLevelsEventContent power_levels;
@ -662,7 +674,7 @@ TEST(PowerLevelsEventContent, NotAnObject)
TEST(TopicEventContent, Deserialization) TEST(TopicEventContent, Deserialization)
{ {
auto data = QJsonObject{ { "topic", "Room Topic" } }; auto data = QJsonObject{{"topic", "Room Topic"}};
TopicEventContent content; TopicEventContent content;
content.deserialize(data); content.deserialize(data);
@ -673,7 +685,7 @@ TEST(TopicEventContent, Deserialization)
TEST(TopicEventContent, NotAnObject) TEST(TopicEventContent, NotAnObject)
{ {
auto data = QJsonArray{ "topic", "Room Topic" }; auto data = QJsonArray{"topic", "Room Topic"};
TopicEventContent content; TopicEventContent content;
@ -682,7 +694,7 @@ TEST(TopicEventContent, NotAnObject)
TEST(TopicEventContent, MissingName) TEST(TopicEventContent, MissingName)
{ {
auto data = QJsonObject{ { "key", "Room Name" } }; auto data = QJsonObject{{"key", "Room Name"}};
TopicEventContent content; TopicEventContent content;
ASSERT_THROW(content.deserialize(data), DeserializationException); ASSERT_THROW(content.deserialize(data), DeserializationException);