1 #include <gtest/gtest.h>
2 
3 #include <mtx.hpp>
4 #include <nlohmann/json.hpp>
5 
6 using json = nlohmann::json;
7 
8 using namespace mtx::events;
9 
TEST(RoomEvents,Reaction)10 TEST(RoomEvents, Reaction)
11 {
12     json data = R"({
13   "type": "m.reaction",
14   "room_id": "!CYvyeleADEeDAsndMom:localhost",
15   "sender": "@example:localhost",
16   "content": {
17     "m.relates_to": {
18       "rel_type": "m.annotation",
19       "event_id": "$oGKg0tfsnDamWPsGxUptGLWR5b8Xq6QNFFsysQNSnake",
20       "key": "��"
21     }
22   },
23   "origin_server_ts": 1588536414112,
24   "unsigned": {
25     "age": 1905609
26   },
27   "event_id": "$ujXAq1WXebS-vcpA4yBIZPvCeqGvnrMFP1c1qn8_wJump"
28   })"_json;
29 
30     RoomEvent<msg::Reaction> event = data;
31 
32     EXPECT_EQ(event.type, EventType::Reaction);
33     EXPECT_EQ(event.event_id, "$ujXAq1WXebS-vcpA4yBIZPvCeqGvnrMFP1c1qn8_wJump");
34     EXPECT_EQ(event.room_id, "!CYvyeleADEeDAsndMom:localhost");
35     EXPECT_EQ(event.sender, "@example:localhost");
36     EXPECT_EQ(event.origin_server_ts, 1588536414112L);
37     EXPECT_EQ(event.unsigned_data.age, 1905609L);
38     EXPECT_EQ(event.content.relations.relations.at(0).event_id,
39               "$oGKg0tfsnDamWPsGxUptGLWR5b8Xq6QNFFsysQNSnake");
40     EXPECT_EQ(event.content.relations.relations.at(0).key, "��");
41     EXPECT_EQ(event.content.relations.relations.at(0).rel_type,
42               mtx::common::RelationType::Annotation);
43 
44     EXPECT_EQ(data.dump(), json(event).dump());
45 }
46 
TEST(RoomEvents,Redacted)47 TEST(RoomEvents, Redacted)
48 {
49     json data = R"({
50 	  "unsigned": {
51             "age": 146,
52 	    "redacted_by": "$152135702813129HltcO:matrix.org"
53 	  },
54           "content": {},
55           "event_id": "$143273582443PhrSn:localhost",
56           "origin_server_ts": 1432735824653,
57           "room_id": "!jEsUZKDJdhlrceRyVU:localhost",
58           "sender": "@example:localhost",
59           "type": "m.room.message"
60         })"_json;
61 
62     RoomEvent<msg::Redacted> event = data;
63 
64     EXPECT_EQ(event.type, EventType::RoomMessage);
65     EXPECT_EQ(event.event_id, "$143273582443PhrSn:localhost");
66     EXPECT_EQ(event.room_id, "!jEsUZKDJdhlrceRyVU:localhost");
67     EXPECT_EQ(event.sender, "@example:localhost");
68     EXPECT_EQ(event.origin_server_ts, 1432735824653L);
69     EXPECT_EQ(event.unsigned_data.age, 146);
70     EXPECT_EQ(event.unsigned_data.redacted_by, "$152135702813129HltcO:matrix.org");
71 }
72 
TEST(RoomEvents,AudioMessage)73 TEST(RoomEvents, AudioMessage)
74 {
75     json data = R"({
76 	  "unsigned": {
77             "age": 146
78 	  },
79           "content": {
80               "body": "Bee Gees - Stayin' Alive",
81               "info": {
82                   "duration": 2140786,
83                   "mimetype": "audio/mpeg",
84                   "size": 1563685
85               },
86               "msgtype": "m.audio",
87               "url": "mxc://localhost/ffed755USFFxlgbQYZGtryd",
88 	      "m.relates_to": {
89 		  "m.in_reply_to": {
90                        "event_id": "$6GKhAfJOcwNd69lgSizdcTob8z2pWQgBOZPrnsWMA1E"
91                   }
92               }
93           },
94           "event_id": "$143273582443PhrSn:localhost",
95           "origin_server_ts": 1432735824653,
96           "room_id": "!jEsUZKDJdhlrceRyVU:localhost",
97           "sender": "@example:localhost",
98           "type": "m.room.message"
99         })"_json;
100 
101     RoomEvent<msg::Audio> event = data;
102 
103     EXPECT_EQ(event.type, EventType::RoomMessage);
104     EXPECT_EQ(event.event_id, "$143273582443PhrSn:localhost");
105     EXPECT_EQ(event.room_id, "!jEsUZKDJdhlrceRyVU:localhost");
106     EXPECT_EQ(event.sender, "@example:localhost");
107     EXPECT_EQ(event.origin_server_ts, 1432735824653L);
108     EXPECT_EQ(event.unsigned_data.age, 146);
109 
110     EXPECT_EQ(event.content.body, "Bee Gees - Stayin' Alive");
111     EXPECT_EQ(event.content.msgtype, "m.audio");
112     EXPECT_EQ(event.content.url, "mxc://localhost/ffed755USFFxlgbQYZGtryd");
113     EXPECT_EQ(event.content.info.mimetype, "audio/mpeg");
114     EXPECT_EQ(event.content.info.size, 1563685);
115     EXPECT_EQ(event.content.info.duration, 2140786);
116     EXPECT_EQ(event.content.relations.reply_to().value(),
117               "$6GKhAfJOcwNd69lgSizdcTob8z2pWQgBOZPrnsWMA1E");
118     EXPECT_EQ(event.content.relations.relations.at(0).rel_type,
119               mtx::common::RelationType::InReplyTo);
120 }
121 
TEST(RoomEvents,EmoteMessage)122 TEST(RoomEvents, EmoteMessage)
123 {
124     json data = R"({
125           "origin_server_ts": 1509878682149,
126           "sender": "@mujx:matrix.org",
127           "event_id": "$15098786822025533uttji:matrix.org",
128           "unsigned": {
129             "age": 626351821
130           },
131           "content": {
132             "body": "tests",
133             "msgtype": "m.emote",
134 	      "m.relates_to": {
135 		  "m.in_reply_to": {
136                        "event_id": "$6GKhAfJOcwNd69lgSizdcTob8z2pWQgBOZPrnsWMA1E"
137                   }
138               }
139           },
140           "type": "m.room.message",
141           "room_id": "!VaMCVKSVcyPtXbcMXh:matrix.org"
142         })"_json;
143 
144     RoomEvent<msg::Emote> event = data;
145 
146     EXPECT_EQ(event.type, EventType::RoomMessage);
147     EXPECT_EQ(event.event_id, "$15098786822025533uttji:matrix.org");
148     EXPECT_EQ(event.room_id, "!VaMCVKSVcyPtXbcMXh:matrix.org");
149     EXPECT_EQ(event.sender, "@mujx:matrix.org");
150     EXPECT_EQ(event.origin_server_ts, 1509878682149L);
151     EXPECT_EQ(event.unsigned_data.age, 626351821);
152     EXPECT_EQ(event.content.body, "tests");
153     EXPECT_EQ(event.content.msgtype, "m.emote");
154     EXPECT_EQ(event.content.relations.relations.at(0).event_id,
155               "$6GKhAfJOcwNd69lgSizdcTob8z2pWQgBOZPrnsWMA1E");
156     EXPECT_EQ(event.content.relations.relations.at(0).rel_type,
157               mtx::common::RelationType::InReplyTo);
158 }
159 
TEST(RoomEvents,FileMessage)160 TEST(RoomEvents, FileMessage)
161 {
162     json data = R"({
163           "origin_server_ts": 1510485607737,
164           "sender": "@nheko_test:matrix.org",
165           "event_id": "$15104856072749611ERqhw:matrix.org",
166           "unsigned": {
167             "age": 31,
168             "transaction_id": "m1510485607454.1"
169           },
170           "content": {
171             "body": "optimize.pdf",
172             "info": {
173               "mimetype": "application/pdf",
174               "size": 40565,
175 	      "thumbnail_info": {
176                 "h": 200,
177                 "mimetype": "image/png",
178                 "size": 73602,
179                 "w": 140
180               },
181 	      "thumbnail_url": "mxc://matrix.org/XpxykZBESCSQnYkLKbbIKnVn"
182             },
183             "msgtype": "m.file",
184             "url": "mxc://matrix.org/XpxykZBESCSQnYkLKbbIKnVn",
185 	      "m.relates_to": {
186 		  "m.in_reply_to": {
187                        "event_id": "$6GKhAfJOcwNd69lgSizdcTob8z2pWQgBOZPrnsWMA1E"
188                   }
189               }
190           },
191           "type": "m.room.message",
192           "room_id": "!lfoDRlNFWlvOnvkBwQ:matrix.org"
193         })"_json;
194 
195     RoomEvent<msg::File> event = data;
196 
197     EXPECT_EQ(event.type, EventType::RoomMessage);
198     EXPECT_EQ(event.event_id, "$15104856072749611ERqhw:matrix.org");
199     EXPECT_EQ(event.room_id, "!lfoDRlNFWlvOnvkBwQ:matrix.org");
200     EXPECT_EQ(event.sender, "@nheko_test:matrix.org");
201     EXPECT_EQ(event.origin_server_ts, 1510485607737L);
202     EXPECT_EQ(event.unsigned_data.age, 31);
203     EXPECT_EQ(event.unsigned_data.transaction_id, "m1510485607454.1");
204 
205     EXPECT_EQ(event.content.body, "optimize.pdf");
206     EXPECT_EQ(event.content.msgtype, "m.file");
207     EXPECT_EQ(event.content.url, "mxc://matrix.org/XpxykZBESCSQnYkLKbbIKnVn");
208     EXPECT_EQ(event.content.info.mimetype, "application/pdf");
209     EXPECT_EQ(event.content.info.size, 40565);
210     EXPECT_EQ(event.content.relations.relations.at(0).event_id,
211               "$6GKhAfJOcwNd69lgSizdcTob8z2pWQgBOZPrnsWMA1E");
212     EXPECT_EQ(event.content.relations.relations.at(0).rel_type,
213               mtx::common::RelationType::InReplyTo);
214 
215     json withThumb = event;
216     EXPECT_EQ(withThumb["content"]["info"].count("thumbnail_url"), 1);
217     EXPECT_EQ(withThumb["content"]["info"].count("thumbnail_info"), 1);
218 
219     event.content.info.thumbnail_url = "";
220     json withoutThumb                = event;
221     EXPECT_EQ(withoutThumb["content"]["info"].count("thumbnail_url"), 0);
222     EXPECT_EQ(withoutThumb["content"]["info"].count("thumbnail_info"), 0);
223 }
224 
TEST(RoomEvents,EncryptedImageMessage)225 TEST(RoomEvents, EncryptedImageMessage)
226 {
227     json data                   = R"(
228 {
229   "content": {
230     "body": "something-important.jpg",
231     "file": {
232       "url": "mxc://example.org/FHyPlCeYUSFFxlgbQYZmoEoe",
233       "mimetype": "image/jpeg",
234       "v": "v2",
235       "key": {
236         "alg": "A256CTR",
237         "ext": true,
238         "k": "aWF6-32KGYaC3A_FEUCk1Bt0JA37zP0wrStgmdCaW-0",
239         "key_ops": ["encrypt","decrypt"],
240         "kty": "oct"
241       },
242       "iv": "w+sE15fzSc0AAAAAAAAAAA",
243       "hashes": {
244         "sha256": "fdSLu/YkRx3Wyh3KQabP3rd6+SFiKg5lsJZQHtkSAYA"
245       }
246     },
247     "info": {
248       "mimetype": "image/jpeg",
249       "h": 1536,
250       "size": 422018,
251       "thumbnail_file": {
252         "hashes": {
253           "sha256": "/NogKqW5bz/m8xHgFiH5haFGjCNVmUIPLzfvOhHdrxY"
254         },
255         "iv": "U+k7PfwLr6UAAAAAAAAAAA",
256         "key": {
257           "alg": "A256CTR",
258           "ext": true,
259           "k": "RMyd6zhlbifsACM1DXkCbioZ2u0SywGljTH8JmGcylg",
260           "key_ops": ["encrypt", "decrypt"],
261           "kty": "oct"
262         },
263         "mimetype": "image/jpeg",
264         "url": "mxc://example.org/pmVJxyxGlmxHposwVSlOaEOv",
265         "v": "v2"
266       },
267       "thumbnail_info": {
268         "h": 768,
269         "mimetype": "image/jpeg",
270         "size": 211009,
271         "w": 432
272       },
273       "w": 864
274     },
275     "msgtype": "m.image"
276   },
277   "event_id": "$143273582443PhrSn:example.org",
278   "origin_server_ts": 1432735824653,
279   "room_id": "!jEsUZKDJdhlrceRyVU:example.org",
280   "sender": "@example:example.org",
281   "type": "m.room.message",
282   "unsigned": {
283       "age": 1234
284   }
285 })"_json;
286     RoomEvent<msg::Image> event = data;
287 
288     EXPECT_EQ(event.type, EventType::RoomMessage);
289     EXPECT_EQ(event.event_id, "$143273582443PhrSn:example.org");
290     EXPECT_EQ(event.room_id, "!jEsUZKDJdhlrceRyVU:example.org");
291     EXPECT_EQ(event.sender, "@example:example.org");
292     EXPECT_EQ(event.origin_server_ts, 1432735824653L);
293     EXPECT_EQ(event.unsigned_data.age, 1234);
294 
295     EXPECT_EQ(event.content.body, "something-important.jpg");
296     EXPECT_EQ(event.content.msgtype, "m.image");
297     EXPECT_EQ(event.content.url, "");
298     EXPECT_EQ(event.content.info.mimetype, "image/jpeg");
299     EXPECT_EQ(event.content.info.size, 422018);
300     EXPECT_EQ(event.content.file.value().url, "mxc://example.org/FHyPlCeYUSFFxlgbQYZmoEoe");
301     EXPECT_EQ(event.content.info.thumbnail_file.value().url,
302               "mxc://example.org/pmVJxyxGlmxHposwVSlOaEOv");
303 }
304 
TEST(RoomEvents,ImageMessage)305 TEST(RoomEvents, ImageMessage)
306 {
307     json data = R"({
308           "origin_server_ts": 1510504294993,
309           "sender": "@max:kamax.io",
310           "event_id": "$15105042942524OGmZm:kamax.io",
311           "unsigned": {
312             "age": 738977
313           },
314           "content": {
315             "body": "image.png",
316             "info": {
317               "mimetype": "image/png",
318               "thumbnail_info": {
319                 "mimetype": "image/png",
320                 "h": 302,
321                 "w": 474,
322                 "size": 33504
323               },
324               "h": 302,
325               "thumbnail_url": "mxc://kamax.io/IlTRDmpGMPkiwlyYUpHXSqjH",
326               "w": 474,
327               "size": 32573
328             },
329             "msgtype": "m.image",
330             "url": "mxc://kamax.io/ewDDLHYnysbHYCPViZwAEIjT",
331 	      "m.relates_to": {
332 		  "m.in_reply_to": {
333                        "event_id": "$6GKhAfJOcwNd69lgSizdcTob8z2pWQgBOZPrnsWMA1E"
334                   }
335               }
336           },
337           "type": "m.room.message",
338           "room_id": "!cURbafjkfsMDVwdRDQ:matrix.org"
339         })"_json;
340 
341     RoomEvent<msg::Image> event = data;
342 
343     EXPECT_EQ(event.type, EventType::RoomMessage);
344     EXPECT_EQ(event.event_id, "$15105042942524OGmZm:kamax.io");
345     EXPECT_EQ(event.room_id, "!cURbafjkfsMDVwdRDQ:matrix.org");
346     EXPECT_EQ(event.sender, "@max:kamax.io");
347     EXPECT_EQ(event.origin_server_ts, 1510504294993L);
348     EXPECT_EQ(event.unsigned_data.age, 738977);
349 
350     EXPECT_EQ(event.content.body, "image.png");
351     EXPECT_EQ(event.content.info.mimetype, "image/png");
352     EXPECT_EQ(event.content.info.h, 302);
353     EXPECT_EQ(event.content.info.w, 474);
354     EXPECT_EQ(event.content.info.size, 32573);
355     EXPECT_EQ(event.content.info.thumbnail_url, "mxc://kamax.io/IlTRDmpGMPkiwlyYUpHXSqjH");
356     EXPECT_EQ(event.content.info.thumbnail_info.mimetype, "image/png");
357     EXPECT_EQ(event.content.info.thumbnail_info.w, 474);
358     EXPECT_EQ(event.content.info.thumbnail_info.h, 302);
359     EXPECT_EQ(event.content.info.thumbnail_info.size, 33504);
360     EXPECT_EQ(event.content.relations.relations.at(0).event_id,
361               "$6GKhAfJOcwNd69lgSizdcTob8z2pWQgBOZPrnsWMA1E");
362     EXPECT_EQ(event.content.relations.relations.at(0).rel_type,
363               mtx::common::RelationType::InReplyTo);
364 
365     json withThumb = event;
366     EXPECT_EQ(withThumb["content"]["info"].count("thumbnail_url"), 1);
367     EXPECT_EQ(withThumb["content"]["info"].count("thumbnail_info"), 1);
368 
369     event.content.info.thumbnail_url = "";
370     json withoutThumb                = event;
371     EXPECT_EQ(withoutThumb["content"]["info"].count("thumbnail_url"), 0);
372     EXPECT_EQ(withoutThumb["content"]["info"].count("thumbnail_info"), 0);
373 
374     data = R"({
375           "origin_server_ts": 1510504294993,
376           "sender": "@max:kamax.io",
377           "event_id": "$15105042942524OGmZm:kamax.io",
378           "unsigned": {
379             "age": 738977
380           },
381           "content": {
382             "body": "image.png",
383             "info": {
384               "mimetype": "image/png",
385               "thumbnail_info": {
386                 "mimetype": "image/png",
387                 "h": null,
388                 "w": null,
389                 "size": null
390               },
391               "h": null,
392               "thumbnail_url": "mxc://kamax.io/IlTRDmpGMPkiwlyYUpHXSqjH",
393               "w": null,
394               "size": null
395             },
396             "msgtype": "m.image",
397             "url": "mxc://kamax.io/ewDDLHYnysbHYCPViZwAEIjT",
398 	      "m.relates_to": {
399 		  "m.in_reply_to": {
400                        "event_id": "$6GKhAfJOcwNd69lgSizdcTob8z2pWQgBOZPrnsWMA1E"
401                   }
402               }
403           },
404           "type": "m.room.message",
405           "room_id": "!cURbafjkfsMDVwdRDQ:matrix.org"
406         })"_json;
407 
408     event = data;
409 
410     EXPECT_EQ(event.type, EventType::RoomMessage);
411     EXPECT_EQ(event.event_id, "$15105042942524OGmZm:kamax.io");
412     EXPECT_EQ(event.room_id, "!cURbafjkfsMDVwdRDQ:matrix.org");
413     EXPECT_EQ(event.sender, "@max:kamax.io");
414     EXPECT_EQ(event.origin_server_ts, 1510504294993L);
415     EXPECT_EQ(event.unsigned_data.age, 738977);
416 
417     EXPECT_EQ(event.content.body, "image.png");
418     EXPECT_EQ(event.content.info.mimetype, "image/png");
419     EXPECT_EQ(event.content.info.h, 0);
420     EXPECT_EQ(event.content.info.w, 0);
421     EXPECT_EQ(event.content.info.size, 0);
422     EXPECT_EQ(event.content.info.thumbnail_url, "mxc://kamax.io/IlTRDmpGMPkiwlyYUpHXSqjH");
423     EXPECT_EQ(event.content.info.thumbnail_info.mimetype, "image/png");
424     EXPECT_EQ(event.content.info.thumbnail_info.w, 0);
425     EXPECT_EQ(event.content.info.thumbnail_info.h, 0);
426     EXPECT_EQ(event.content.info.thumbnail_info.size, 0);
427     EXPECT_EQ(event.content.relations.relations.at(0).event_id,
428               "$6GKhAfJOcwNd69lgSizdcTob8z2pWQgBOZPrnsWMA1E");
429     EXPECT_EQ(event.content.relations.relations.at(0).rel_type,
430               mtx::common::RelationType::InReplyTo);
431 }
432 
TEST(RoomEvents,LocationMessage)433 TEST(RoomEvents, LocationMessage) {}
434 
TEST(RoomEvents,NoticeMessage)435 TEST(RoomEvents, NoticeMessage)
436 {
437     json data = R"({
438           "origin_server_ts": 1510435865515,
439           "sender": "@_neb_github:matrix.org",
440           "event_id": "$15104358652239178iCnZy:matrix.org",
441           "unsigned": {
442             "age": 69168455
443           },
444           "content": {
445             "body": "https://github.com/postmarketOS/pmbootstrap/issues/900 : Package nheko",
446             "msgtype": "m.notice",
447 	      "m.relates_to": {
448 		  "m.in_reply_to": {
449                        "event_id": "$6GKhAfJOcwNd69lgSizdcTob8z2pWQgBOZPrnsWMA1E"
450                   }
451               }
452           },
453           "type": "m.room.message",
454           "room_id": "!BPvgRcBVHzyFSlYkrg:matrix.org"
455         })"_json;
456 
457     RoomEvent<msg::Notice> event = data;
458 
459     EXPECT_EQ(event.type, EventType::RoomMessage);
460     EXPECT_EQ(event.event_id, "$15104358652239178iCnZy:matrix.org");
461     EXPECT_EQ(event.room_id, "!BPvgRcBVHzyFSlYkrg:matrix.org");
462     EXPECT_EQ(event.sender, "@_neb_github:matrix.org");
463     EXPECT_EQ(event.origin_server_ts, 1510435865515L);
464     EXPECT_EQ(event.unsigned_data.age, 69168455);
465     EXPECT_EQ(event.content.body,
466               "https://github.com/postmarketOS/pmbootstrap/issues/900 : Package nheko");
467     EXPECT_EQ(event.content.msgtype, "m.notice");
468     EXPECT_EQ(event.content.relations.relations.at(0).event_id,
469               "$6GKhAfJOcwNd69lgSizdcTob8z2pWQgBOZPrnsWMA1E");
470     EXPECT_EQ(event.content.relations.relations.at(0).rel_type,
471               mtx::common::RelationType::InReplyTo);
472 }
473 
TEST(RoomEvents,TextMessage)474 TEST(RoomEvents, TextMessage)
475 {
476     json data = R"({
477           "origin_server_ts": 1510489356530,
478           "sender": "@nheko_test:matrix.org",
479           "event_id": "$15104893562785758wEgEU:matrix.org",
480           "unsigned": {
481             "age": 2225,
482             "transaction_id": "m1510489356267.2"
483           },
484           "content": {
485             "body": "hey there",
486             "msgtype": "m.text",
487 	      "m.relates_to": {
488 		  "m.in_reply_to": {
489                        "event_id": "$6GKhAfJOcwNd69lgSizdcTob8z2pWQgBOZPrnsWMA1E"
490                   }
491               }
492           },
493           "type": "m.room.message",
494           "room_id": "!lfoDRlNFWlvOnvkBwQ:matrix.org"
495          })"_json;
496 
497     RoomEvent<msg::Text> event = data;
498 
499     EXPECT_EQ(event.type, EventType::RoomMessage);
500     EXPECT_EQ(event.event_id, "$15104893562785758wEgEU:matrix.org");
501     EXPECT_EQ(event.room_id, "!lfoDRlNFWlvOnvkBwQ:matrix.org");
502     EXPECT_EQ(event.sender, "@nheko_test:matrix.org");
503     EXPECT_EQ(event.origin_server_ts, 1510489356530L);
504     EXPECT_EQ(event.unsigned_data.age, 2225);
505     EXPECT_EQ(event.unsigned_data.transaction_id, "m1510489356267.2");
506 
507     EXPECT_EQ(event.content.body, "hey there");
508     EXPECT_EQ(event.content.msgtype, "m.text");
509     EXPECT_EQ(event.content.relations.relations.at(0).event_id,
510               "$6GKhAfJOcwNd69lgSizdcTob8z2pWQgBOZPrnsWMA1E");
511     EXPECT_EQ(event.content.relations.relations.at(0).rel_type,
512               mtx::common::RelationType::InReplyTo);
513 
514     EXPECT_EQ(data.dump(), json(event).dump());
515 }
516 
TEST(RoomEvents,VideoMessage)517 TEST(RoomEvents, VideoMessage)
518 {
519     json data = R"({
520 	  "unsigned": {
521             "age": 146
522 	  },
523           "content": {
524               "body": "Gangnam Style",
525               "info": {
526                   "duration": 2140786,
527                   "mimetype": "video/mp4",
528                   "size": 1563685,
529 		  "h": 320,
530 		  "w": 480,
531 		  "thumbnail_url": "mxc://localhost/FHyPlCeYUSFFxlgbQYZmoEoe",
532 		  "thumbnail_info": {
533 		    "h": 300,
534 		    "mimetype": "image/jpeg",
535 		    "size": 46144,
536 		    "w": 310
537 		  }
538               },
539               "msgtype": "m.video",
540               "url": "mxc://localhost/ffed755USFFxlgbQYZGtryd",
541 	      "m.relates_to": {
542 		  "m.in_reply_to": {
543                        "event_id": "$6GKhAfJOcwNd69lgSizdcTob8z2pWQgBOZPrnsWMA1E"
544                   }
545               }
546           },
547           "event_id": "$143273582443PhrSn:localhost",
548           "origin_server_ts": 1432735824653,
549           "room_id": "!jEsUZKDJdhlrceRyVU:localhost",
550           "sender": "@example:localhost",
551           "type": "m.room.message"
552         })"_json;
553 
554     RoomEvent<msg::Video> event = data;
555 
556     EXPECT_EQ(event.type, EventType::RoomMessage);
557     EXPECT_EQ(event.event_id, "$143273582443PhrSn:localhost");
558     EXPECT_EQ(event.room_id, "!jEsUZKDJdhlrceRyVU:localhost");
559     EXPECT_EQ(event.sender, "@example:localhost");
560     EXPECT_EQ(event.origin_server_ts, 1432735824653L);
561     EXPECT_EQ(event.unsigned_data.age, 146);
562 
563     EXPECT_EQ(event.content.body, "Gangnam Style");
564     EXPECT_EQ(event.content.msgtype, "m.video");
565     EXPECT_EQ(event.content.url, "mxc://localhost/ffed755USFFxlgbQYZGtryd");
566     EXPECT_EQ(event.content.info.mimetype, "video/mp4");
567     EXPECT_EQ(event.content.info.w, 480);
568     EXPECT_EQ(event.content.info.h, 320);
569     EXPECT_EQ(event.content.info.size, 1563685);
570     EXPECT_EQ(event.content.info.duration, 2140786);
571     EXPECT_EQ(event.content.info.thumbnail_info.h, 300);
572     EXPECT_EQ(event.content.info.thumbnail_info.w, 310);
573     EXPECT_EQ(event.content.info.thumbnail_info.size, 46144);
574     EXPECT_EQ(event.content.relations.relations.at(0).event_id,
575               "$6GKhAfJOcwNd69lgSizdcTob8z2pWQgBOZPrnsWMA1E");
576     EXPECT_EQ(event.content.relations.relations.at(0).rel_type,
577               mtx::common::RelationType::InReplyTo);
578 }
579 
TEST(RoomEvents,Sticker)580 TEST(RoomEvents, Sticker)
581 {
582     json data = R"({
583           "age": 242352,
584           "content": {
585             "body": "Landing",
586             "info": {
587               "h": 200,
588               "mimetype": "image/png",
589 	      "size": 73602,
590 	      "thumbnail_info": {
591                 "h": 200,
592                 "mimetype": "image/png",
593                 "size": 73602,
594                 "w": 140
595               },
596               "thumbnail_url": "mxc://matrix.org/sHhqkFCvSkFwtmvtETOtKnLP",
597               "w": 140
598             },
599             "url": "mxc://matrix.org/sHhqkFCvSkFwtmvtETOtKnLP",
600 	      "m.relates_to": {
601 		  "m.in_reply_to": {
602                        "event_id": "$6GKhAfJOcwNd69lgSizdcTob8z2pWQgBOZPrnsWMA1E"
603                   }
604               }
605           },
606           "event_id": "$WLGTSEFSEF:localhost",
607           "origin_server_ts": 1431961217939,
608           "room_id": "!Cuyf34gef24t:localhost",
609           "sender": "@example:localhost",
610           "type": "m.sticker"
611 	})"_json;
612 
613     Sticker event = data;
614 
615     EXPECT_EQ(event.type, EventType::Sticker);
616     EXPECT_EQ(event.event_id, "$WLGTSEFSEF:localhost");
617     EXPECT_EQ(event.content.body, "Landing");
618     EXPECT_EQ(event.content.url, "mxc://matrix.org/sHhqkFCvSkFwtmvtETOtKnLP");
619     EXPECT_EQ(event.content.info.w, 140);
620     EXPECT_EQ(event.content.info.h, 200);
621     EXPECT_EQ(event.content.info.size, 73602);
622     EXPECT_EQ(event.content.relations.relations.at(0).event_id,
623               "$6GKhAfJOcwNd69lgSizdcTob8z2pWQgBOZPrnsWMA1E");
624     EXPECT_EQ(event.content.relations.relations.at(0).rel_type,
625               mtx::common::RelationType::InReplyTo);
626 
627     json data2     = R"({
628 	  "type": "m.sticker",
629 	  "sender": "masked",
630 	  "content": {
631 	    "msgtype": "m.sticker",
632 	    "url": "mxc://devture.com/OCPxxxxxxxxxxxxxxxx",
633 	    "info": {
634 	      "mimetype": "image/png",
635 	      "size": 16901,
636 	      "h": 240,
637 	      "w": 240
638 	    }
639 	  },
640 	  "origin_server_ts": 1586985031228,
641 	  "unsigned": {
642 	    "age": 38
643 	  },
644 	  "event_id": "masked",
645 	  "room_id": "!masked:devture.com"
646 	})"_json;
647     Sticker event2 = data2;
648     EXPECT_EQ(event2.type, EventType::Sticker);
649     EXPECT_EQ(event2.content.body, "");
650     EXPECT_EQ(event2.content.url, "mxc://devture.com/OCPxxxxxxxxxxxxxxxx");
651     EXPECT_EQ(event2.content.info.w, 240);
652     EXPECT_EQ(event2.content.info.h, 240);
653     EXPECT_EQ(event2.content.info.size, 16901);
654 }
655 
TEST(FormattedMessages,Deserialization)656 TEST(FormattedMessages, Deserialization)
657 {
658     json notice_data = R"({
659           "origin_server_ts": 1510435865515,
660           "sender": "@_neb_github:matrix.org",
661           "event_id": "$15104358652239178iCnZy:matrix.org",
662           "content": {
663             "body": "https://github.com/postmarketOS/pmbootstrap/issues/900 : Package nheko",
664             "msgtype": "m.notice",
665             "format": "org.matrix.custom.html",
666             "formatted_body": "<h1> Hello World! </h1>"
667           },
668           "type": "m.room.message",
669           "room_id": "!BPvgRcBVHzyFSlYkrg:matrix.org"
670         })"_json;
671 
672     RoomEvent<msg::Notice> notice = notice_data;
673 
674     EXPECT_EQ(notice.type, EventType::RoomMessage);
675     EXPECT_EQ(notice.content.msgtype, "m.notice");
676     EXPECT_EQ(notice.content.format, "org.matrix.custom.html");
677     EXPECT_EQ(notice.content.formatted_body, "<h1> Hello World! </h1>");
678 
679     json text_data = R"({
680           "origin_server_ts": 1510489356530,
681           "sender": "@nheko_test:matrix.org",
682           "event_id": "$15104893562785758wEgEU:matrix.org",
683           "content": {
684             "body": "hey there",
685             "msgtype": "m.text",
686             "format": "org.matrix.custom.html",
687             "formatted_body": "<h1> Hello World! </h1>"
688           },
689           "type": "m.room.message",
690           "room_id": "!lfoDRlNFWlvOnvkBwQ:matrix.org"
691          })"_json;
692 
693     RoomEvent<msg::Text> text = text_data;
694 
695     EXPECT_EQ(text.content.msgtype, "m.text");
696     EXPECT_EQ(text.content.format, "org.matrix.custom.html");
697     EXPECT_EQ(text.content.formatted_body, "<h1> Hello World! </h1>");
698 
699     json emote_data = R"({
700           "origin_server_ts": 1509878682149,
701           "sender": "@mujx:matrix.org",
702           "event_id": "$15098786822025533uttji:matrix.org",
703           "content": {
704             "body": "tests",
705             "msgtype": "m.emote",
706             "format": "org.matrix.custom.html",
707             "formatted_body": "<h1> Hello World! </h1>"
708           },
709           "type": "m.room.message",
710           "room_id": "!VaMCVKSVcyPtXbcMXh:matrix.org"
711         })"_json;
712 
713     RoomEvent<msg::Emote> emote = emote_data;
714 
715     EXPECT_EQ(emote.content.msgtype, "m.emote");
716     EXPECT_EQ(emote.content.format, "org.matrix.custom.html");
717     EXPECT_EQ(emote.content.formatted_body, "<h1> Hello World! </h1>");
718 }
719 
TEST(RoomEvents,Encrypted)720 TEST(RoomEvents, Encrypted)
721 {
722     json data = R"({
723 	    "content": {
724 		"algorithm": "m.megolm.v1.aes-sha2",
725 		"ciphertext": "AwgAEnACgAkLmt6qF84IK++J7UDH2Za1YVchHyprqTqsg...",
726 		"device_id": "RJYKSTBOIE",
727 		"sender_key": "IlRMeOPX2e0MurIyfWEucYBRVOEEUMrOHqn/8mLqMjA",
728 		"session_id": "X3lUlvLELLYxeTx4yOVu6UDpasGEVO0Jbu+QFnm0cKQ",
729 	   "m.relates_to": {
730 		    "m.in_reply_to": {
731                          "event_id": "$6GKhAfJOcwNd69lgSizdcTob8z2pWQgBOZPrnsWMA1E"
732                     }
733                 }
734 	    },
735 	    "event_id": "$143273582443PhrSn:example.org",
736 	    "origin_server_ts": 1432735824653,
737 	    "room_id": "!jEsUZKDJdhlrceRyVU:example.org",
738 	    "sender": "@example:example.org",
739 	    "type": "m.room.encrypted",
740 	    "unsigned": {
741 		"age": 1234
742 	    }
743 	})"_json;
744 
745     RoomEvent<msg::Encrypted> event = data;
746 
747     std::cout << "*****" << std::endl;
748     std::cout << data.dump(2) << std::endl;
749     std::cout << "*****" << std::endl;
750     std::cout << json(event).dump(2) << std::endl;
751     std::cout << "*****" << std::endl;
752 
753     EXPECT_EQ(event.type, EventType::RoomEncrypted);
754     EXPECT_EQ(event.event_id, "$143273582443PhrSn:example.org");
755     EXPECT_EQ(event.room_id, "!jEsUZKDJdhlrceRyVU:example.org");
756     EXPECT_EQ(event.sender, "@example:example.org");
757     EXPECT_EQ(event.origin_server_ts, 1432735824653L);
758     EXPECT_EQ(event.unsigned_data.age, 1234);
759     EXPECT_EQ(event.content.algorithm, "m.megolm.v1.aes-sha2");
760     EXPECT_EQ(event.content.ciphertext, "AwgAEnACgAkLmt6qF84IK++J7UDH2Za1YVchHyprqTqsg...");
761     EXPECT_EQ(event.content.device_id, "RJYKSTBOIE");
762     EXPECT_EQ(event.content.sender_key, "IlRMeOPX2e0MurIyfWEucYBRVOEEUMrOHqn/8mLqMjA");
763     EXPECT_EQ(event.content.session_id, "X3lUlvLELLYxeTx4yOVu6UDpasGEVO0Jbu+QFnm0cKQ");
764     EXPECT_EQ(event.content.relations.relations.at(0).event_id,
765               "$6GKhAfJOcwNd69lgSizdcTob8z2pWQgBOZPrnsWMA1E");
766     EXPECT_EQ(event.content.relations.relations.at(0).rel_type,
767               mtx::common::RelationType::InReplyTo);
768 
769     EXPECT_EQ(data, json(event));
770 }
771 
TEST(RoomEvents,CallInvite)772 TEST(RoomEvents, CallInvite)
773 {
774     nlohmann::json data = R"({
775           "content": {
776             "call_id": "c1591052749788",
777             "offer": {
778               "sdp": "v=0\r\no=- 6584580628695956864 2 IN IP4 127.0.0.1[...]",
779               "type": "offer"
780             },
781             "version": 0,
782             "lifetime": 120000
783           },
784           "event_id": "$143273582443PhrSn:example.org",
785           "origin_server_ts": 1432735824653,
786           "room_id": "!jEsUZKDJdhlrceRyVU:example.org",
787           "sender": "@example:example.org",
788           "type": "m.call.invite",
789           "unsigned": {
790             "age": 1234
791           }
792         })"_json;
793 
794     RoomEvent<msg::CallInvite> event = data;
795 
796     EXPECT_EQ(event.event_id, "$143273582443PhrSn:example.org");
797     EXPECT_EQ(event.origin_server_ts, 1432735824653L);
798     EXPECT_EQ(event.room_id, "!jEsUZKDJdhlrceRyVU:example.org");
799     EXPECT_EQ(event.sender, "@example:example.org");
800     EXPECT_EQ(event.type, EventType::CallInvite);
801     EXPECT_EQ(event.unsigned_data.age, 1234);
802     EXPECT_EQ(event.content.call_id, "c1591052749788");
803     EXPECT_EQ(event.content.sdp, "v=0\r\no=- 6584580628695956864 2 IN IP4 127.0.0.1[...]");
804     EXPECT_EQ(event.content.version, "0");
805     EXPECT_EQ(event.content.lifetime, 120000);
806 
807     EXPECT_EQ(data, json(event));
808 }
809 
TEST(RoomEvents,CallCandidates)810 TEST(RoomEvents, CallCandidates)
811 {
812     nlohmann::json data = R"({
813           "content": {
814             "call_id": "c1591052749788",
815             "candidates": [
816               {
817                 "sdpMid": "audio",
818                 "sdpMLineIndex": 0,
819                 "candidate": "candidate:863018703 1 udp 2122260223 10.9.64.156 43670 typ host generation 0"
820               }
821             ],
822             "version": 0
823           },
824           "event_id": "$143273582443PhrSn:example.org",
825           "origin_server_ts": 1432735824653,
826           "room_id": "!jEsUZKDJdhlrceRyVU:example.org",
827           "sender": "@example:example.org",
828           "type": "m.call.candidates",
829           "unsigned": {
830             "age": 1234
831           }
832         })"_json;
833 
834     RoomEvent<msg::CallCandidates> event = data;
835 
836     EXPECT_EQ(event.event_id, "$143273582443PhrSn:example.org");
837     EXPECT_EQ(event.origin_server_ts, 1432735824653L);
838     EXPECT_EQ(event.room_id, "!jEsUZKDJdhlrceRyVU:example.org");
839     EXPECT_EQ(event.sender, "@example:example.org");
840     EXPECT_EQ(event.type, EventType::CallCandidates);
841     EXPECT_EQ(event.unsigned_data.age, 1234);
842     EXPECT_EQ(event.content.call_id, "c1591052749788");
843     EXPECT_EQ(event.content.candidates[0].sdpMid, "audio");
844     EXPECT_EQ(event.content.candidates[0].sdpMLineIndex, 0);
845     EXPECT_EQ(event.content.candidates[0].candidate,
846               "candidate:863018703 1 udp 2122260223 10.9.64.156 43670 typ host generation 0");
847     EXPECT_EQ(event.content.version, "0");
848 
849     EXPECT_EQ(data, json(event));
850 }
851 
TEST(RoomEvents,CallAnswer)852 TEST(RoomEvents, CallAnswer)
853 {
854     nlohmann::json data = R"({
855           "content": {
856             "call_id": "c1591052749788",
857             "answer": {
858               "sdp": "v=0\r\no=- 6584580628695956864 2 IN IP4 127.0.0.1[...]",
859               "type": "answer"
860             },
861             "version": 0
862           },
863           "event_id": "$143273582443PhrSn:example.org",
864           "origin_server_ts": 1432735824653,
865           "room_id": "!jEsUZKDJdhlrceRyVU:example.org",
866           "sender": "@example:example.org",
867           "type": "m.call.answer",
868           "unsigned": {
869             "age": 1234
870           }
871         })"_json;
872 
873     RoomEvent<msg::CallAnswer> event = data;
874 
875     EXPECT_EQ(event.event_id, "$143273582443PhrSn:example.org");
876     EXPECT_EQ(event.origin_server_ts, 1432735824653L);
877     EXPECT_EQ(event.room_id, "!jEsUZKDJdhlrceRyVU:example.org");
878     EXPECT_EQ(event.sender, "@example:example.org");
879     EXPECT_EQ(event.type, EventType::CallAnswer);
880     EXPECT_EQ(event.unsigned_data.age, 1234);
881     EXPECT_EQ(event.content.call_id, "c1591052749788");
882     EXPECT_EQ(event.content.sdp, "v=0\r\no=- 6584580628695956864 2 IN IP4 127.0.0.1[...]");
883     EXPECT_EQ(event.content.version, "0");
884 
885     EXPECT_EQ(data, json(event));
886 }
887 
TEST(RoomEvents,CallHangUp)888 TEST(RoomEvents, CallHangUp)
889 {
890     nlohmann::json data = R"({
891           "content": {
892             "call_id": "c1591052749788",
893             "version": 0,
894             "reason": "invite_timeout"
895           },
896           "event_id": "$143273582443PhrSn:example.org",
897           "origin_server_ts": 1432735824653,
898           "room_id": "!jEsUZKDJdhlrceRyVU:example.org",
899           "sender": "@example:example.org",
900           "type": "m.call.hangup",
901           "unsigned": {
902             "age": 1234
903           }
904         })"_json;
905 
906     RoomEvent<msg::CallHangUp> event = data;
907 
908     EXPECT_EQ(event.event_id, "$143273582443PhrSn:example.org");
909     EXPECT_EQ(event.origin_server_ts, 1432735824653L);
910     EXPECT_EQ(event.room_id, "!jEsUZKDJdhlrceRyVU:example.org");
911     EXPECT_EQ(event.sender, "@example:example.org");
912     EXPECT_EQ(event.type, EventType::CallHangUp);
913     EXPECT_EQ(event.unsigned_data.age, 1234);
914     EXPECT_EQ(event.content.call_id, "c1591052749788");
915     EXPECT_EQ(event.content.version, "0");
916     EXPECT_EQ(event.content.reason, msg::CallHangUp::Reason::InviteTimeOut);
917 
918     EXPECT_EQ(data, json(event));
919 }
920