| 1 | #include <gtest/gtest.h> |
| 2 | |
| 3 | #include <fstream> |
| 4 | #include <variant> |
| 5 | |
| 6 | #include <nlohmann/json.hpp> |
| 7 | |
| 8 | #include <mtx.hpp> |
| 9 | |
| 10 | #include "test_helpers.hpp" |
| 11 | |
| 12 | using json = nlohmann::json; |
| 13 | |
| 14 | using namespace mtx::responses; |
| 15 | using namespace mtx::events; |
| 16 | |
| 17 | TEST(Responses, State) |
| 18 | { |
| 19 | json data = R"({ |
| 20 | "events": [ |
| 21 | { |
| 22 | "unsigned": { |
| 23 | "age": 242352, |
| 24 | "transaction_id": "txnid" |
| 25 | }, |
| 26 | "content": { |
| 27 | "aliases": [ |
| 28 | "#somewhere:localhost", |
| 29 | "#another:localhost" |
| 30 | ] |
| 31 | }, |
| 32 | "event_id": "$WLGTSEFSEF:localhost", |
| 33 | "origin_server_ts": 1431961217939, |
| 34 | "room_id": "!Cuyf34gef24t:localhost", |
| 35 | "sender": "@example:localhost", |
| 36 | "state_key": "localhost", |
| 37 | "type": "m.room.aliases" |
| 38 | }, |
| 39 | { |
| 40 | "unsigned": { |
| 41 | "age": 242352, |
| 42 | "transaction_id": "txnid" |
| 43 | }, |
| 44 | "content": { |
| 45 | "name": "Random name" |
| 46 | }, |
| 47 | "event_id": "$WLGTSEFSEF2:localhost", |
| 48 | "origin_server_ts": 1431961217939, |
| 49 | "room_id": "!Cuyf34gef24t:localhost", |
| 50 | "sender": "@example2:localhost", |
| 51 | "state_key": "localhost", |
| 52 | "type": "m.room.name" |
| 53 | } |
| 54 | ] |
| 55 | })"_json; |
| 56 | |
| 57 | State state = data.get<State>(); |
| 58 | |
| 59 | EXPECT_EQ(state.events.size(), 2); |
| 60 | |
| 61 | auto aliases = std::get<StateEvent<state::Aliases>>(state.events[0]); |
| 62 | EXPECT_EQ(aliases.event_id, "$WLGTSEFSEF:localhost"); |
| 63 | EXPECT_EQ(aliases.type, EventType::RoomAliases); |
| 64 | EXPECT_EQ(aliases.sender, "@example:localhost"); |
| 65 | EXPECT_EQ(aliases.content.aliases.size(), 2); |
| 66 | EXPECT_EQ(aliases.content.aliases[0], "#somewhere:localhost"); |
| 67 | |
| 68 | auto name = std::get<StateEvent<state::Name>>(state.events[1]); |
| 69 | EXPECT_EQ(name.event_id, "$WLGTSEFSEF2:localhost"); |
| 70 | EXPECT_EQ(name.type, EventType::RoomName); |
| 71 | EXPECT_EQ(name.sender, "@example2:localhost"); |
| 72 | EXPECT_EQ(name.content.name, "Random name"); |
| 73 | |
| 74 | // The first event is malformed (has null as the user id) |
| 75 | // and therefore is should be skipped. |
| 76 | json malformed_data = R"({ |
| 77 | "events": [ |
| 78 | { |
| 79 | "unsigned": { |
| 80 | "age": 242352, |
| 81 | "transaction_id": "txnid" |
| 82 | }, |
| 83 | "content": { |
| 84 | "aliases": [ |
| 85 | "#somewhere:localhost", |
| 86 | "#another:localhost" |
| 87 | ] |
| 88 | }, |
| 89 | "event_id": "$WLGTSEFSEF:localhost", |
| 90 | "origin_server_ts": 1431961217939, |
| 91 | "room_id": "!Cuyf34gef24t:localhost", |
| 92 | "sender": null, |
| 93 | "state_key": "localhost", |
| 94 | "type": "m.room.aliases" |
| 95 | }, |
| 96 | { |
| 97 | "unsigned": { |
| 98 | "age": 242352, |
| 99 | "transaction_id": "txnid" |
| 100 | }, |
| 101 | "content": { |
| 102 | "name": "Random name" |
| 103 | }, |
| 104 | "event_id": "$WLGTSEFSEF2:localhost", |
| 105 | "origin_server_ts": 1431961217939, |
| 106 | "room_id": "!Cuyf34gef24t:localhost", |
| 107 | "sender": "@example2:localhost", |
| 108 | "state_key": "localhost", |
| 109 | "type": "m.room.name" |
| 110 | } |
| 111 | ] |
| 112 | })"_json; |
| 113 | |
| 114 | State malformed_state = malformed_data.get<State>(); |
| 115 | |
| 116 | EXPECT_EQ(malformed_state.events.size(), 1); |
| 117 | |
| 118 | name = std::get<StateEvent<state::Name>>(malformed_state.events[0]); |
| 119 | EXPECT_EQ(name.event_id, "$WLGTSEFSEF2:localhost"); |
| 120 | EXPECT_EQ(name.type, EventType::RoomName); |
| 121 | EXPECT_EQ(name.sender, "@example2:localhost"); |
| 122 | EXPECT_EQ(name.content.name, "Random name"); |
| 123 | } |
| 124 | |
| 125 | TEST(Responses, Timeline) {} |
| 126 | TEST(Responses, JoinedRoom) |
| 127 | { |
| 128 | json data1 = R"({ |
| 129 | "ephemeral": { |
| 130 | "events": [ |
| 131 | { |
| 132 | "content": { |
| 133 | "$123456789123456789ABC:matrix.org": { |
| 134 | "m.read": { |
| 135 | "@user1:s1.example.com": { |
| 136 | "ts": 1515754170039 |
| 137 | }, |
| 138 | "@user2:s2.example.com": { |
| 139 | "ts": 1515713767417 |
| 140 | } |
| 141 | } |
| 142 | } |
| 143 | }, |
| 144 | "type": "m.receipt" |
| 145 | } |
| 146 | ] |
| 147 | }, |
| 148 | "unread_notifications": { |
| 149 | "highlight_count": 2, |
| 150 | "notification_count": 4 |
| 151 | } |
| 152 | })"_json; |
| 153 | |
| 154 | JoinedRoom room1 = data1.get<JoinedRoom>(); |
| 155 | |
| 156 | // It this succeeds parsing was done successfully |
| 157 | EXPECT_EQ(room1.ephemeral.events.size(), 1); |
| 158 | EXPECT_EQ(room1.timeline.events.size(), 0); |
| 159 | EXPECT_EQ(room1.unread_notifications.highlight_count, 2); |
| 160 | EXPECT_EQ(room1.unread_notifications.notification_count, 4); |
| 161 | |
| 162 | json data2 = R"({ |
| 163 | "timeline": { |
| 164 | "events": [ |
| 165 | { |
| 166 | "content": { |
| 167 | "avatar_url": "mxc://matrix.org/MatrixContentID123456789", |
| 168 | "displayname": "DisplayName", |
| 169 | "membership": "join" |
| 170 | }, |
| 171 | "event_id": "$1234567898765432123456:matrix.org", |
| 172 | "membership": "join", |
| 173 | "origin_server_ts": 1515756721018, |
| 174 | "sender": "@user1:s1.example.com", |
| 175 | "state_key": "@user1:s1.example.com", |
| 176 | "type": "m.room.member", |
| 177 | "unsigned": { |
| 178 | "age": 18585, |
| 179 | "prev_content": { |
| 180 | "avatar_url": null, |
| 181 | "displayname": "DisplayName", |
| 182 | "membership": "join" |
| 183 | }, |
| 184 | "prev_sender": "@user1:s1.example.com", |
| 185 | "replaces_state": "$1234567898765432123455:matrix.org" |
| 186 | } |
| 187 | }, |
| 188 | { |
| 189 | "content": { |
| 190 | "body": "Hello World", |
| 191 | "msgtype": "m.text" |
| 192 | }, |
| 193 | "event_id": "$12345678987654321asdfg:matrix.org", |
| 194 | "origin_server_ts": 1515756732500, |
| 195 | "sender": "@user1:s1.example.com", |
| 196 | "type": "m.room.message", |
| 197 | "unsigned": { |
| 198 | "age": 8032 |
| 199 | } |
| 200 | } |
| 201 | ], |
| 202 | "limited": false, |
| 203 | "prev_batch": "s42_42_42_42_42_42_42_42_1" |
| 204 | }, |
| 205 | "unread_notifications": { |
| 206 | "highlight_count": 2, |
| 207 | "notification_count": 4 |
| 208 | } |
| 209 | })"_json; |
| 210 | |
| 211 | JoinedRoom room2 = data2.get<JoinedRoom>(); |
| 212 | EXPECT_EQ(room2.ephemeral.events.size(), 0); |
| 213 | EXPECT_EQ(room2.timeline.events.size(), 2); |
| 214 | EXPECT_EQ(room2.timeline.prev_batch, "s42_42_42_42_42_42_42_42_1"); |
| 215 | EXPECT_EQ(room2.unread_notifications.highlight_count, 2); |
| 216 | EXPECT_EQ(room2.unread_notifications.notification_count, 4); |
| 217 | } |
| 218 | TEST(Responses, LeftRoom) |
| 219 | { |
| 220 | json data = R"({ |
| 221 | "timeline": { |
| 222 | "events": [ |
| 223 | { |
| 224 | "content": { |
| 225 | "membership": "leave" |
| 226 | }, |
| 227 | "event_id": "$12345678923456789:s1.example.com", |
| 228 | "membership": "leave", |
| 229 | "origin_server_ts": 1234567894342, |
| 230 | "sender": "@u1:s1.example.com", |
| 231 | "state_key": "@u1:s1.example.com", |
| 232 | "type": "m.room.member", |
| 233 | "unsigned": { |
| 234 | "age": 1566, |
| 235 | "prev_content": { |
| 236 | "avatar_url": "mxc://msgs.tk/MatrixContentId123456789", |
| 237 | "displayname": "User 1!", |
| 238 | "membership": "join" |
| 239 | }, |
| 240 | "prev_sender": "@u1:s1.example.com", |
| 241 | "replaces_state": "$12345678912345678:s1.example.com" |
| 242 | } |
| 243 | } |
| 244 | ], |
| 245 | "limited": false, |
| 246 | "prev_batch": "s123_42_1234_4321123_13579_12_14400_4221_7" |
| 247 | } |
| 248 | })"_json; |
| 249 | |
| 250 | LeftRoom room = data.get<LeftRoom>(); |
| 251 | |
| 252 | EXPECT_EQ(room.timeline.events.size(), 1); |
| 253 | EXPECT_EQ(room.timeline.limited, false); |
| 254 | EXPECT_EQ(room.state.events.size(), 0); |
| 255 | } |
| 256 | |
| 257 | TEST(Responses, InvitedRoom) |
| 258 | { |
| 259 | json data = R"({ |
| 260 | "invite_state": { |
| 261 | "events":[{ |
| 262 | "content":{ |
| 263 | "name":"Testing room" |
| 264 | }, |
| 265 | "sender":"@mujx:matrix.org", |
| 266 | "state_key":"", |
| 267 | "type":"m.room.name" |
| 268 | },{ |
| 269 | "content":{"url":"mxc://matrix.org/wdjzHdrThpqWyVArfyWmRbBx"}, |
| 270 | "sender":"@mujx:matrix.org", |
| 271 | "state_key":"", |
| 272 | "type":"m.room.avatar" |
| 273 | },{ |
| 274 | "content":{ |
| 275 | "avatar_url":"mxc://matrix.org/JKiSOBDDxCHxmaLAgoQwSAHa", |
| 276 | "displayname":"NhekoTest", |
| 277 | "membership":"join" |
| 278 | }, |
| 279 | "sender":"@nheko_test:matrix.org", |
| 280 | "state_key":"@nheko_test:matrix.org", |
| 281 | "type":"m.room.member" |
| 282 | },{ |
| 283 | "content":{"alias":"#tessssssst:matrix.org"}, |
| 284 | "sender":"@mujx:matrix.org", |
| 285 | "state_key":"", |
| 286 | "type":"m.room.canonical_alias"}, |
| 287 | { |
| 288 | "content":{"join_rule":"invite"}, |
| 289 | "sender":"@mujx:matrix.org", |
| 290 | "state_key":"", |
| 291 | "type":"m.room.join_rules" |
| 292 | },{ |
| 293 | "content":{"avatar_url":"mxc://matrix.org/mGOKULXnAOnyplROyaxQcyoC", |
| 294 | "displayname":"mujx", |
| 295 | "membership":"invite" |
| 296 | }, |
| 297 | "event_id":"$1513636848665012cjHwG:matrix.org", |
| 298 | "membership":"invite", |
| 299 | "origin_server_ts":1513636848325, |
| 300 | "sender":"@nheko_test:matrix.org", |
| 301 | "state_key":"@mujx:matrix.org", |
| 302 | "type":"m.room.member", |
| 303 | "unsigned":{ |
| 304 | "age":279, |
| 305 | "prev_content":{"membership":"leave"}, |
| 306 | "prev_sender":"@mujx:matrix.org", |
| 307 | "replaces_state":"$15068762701126850oGdvT:matrix.org" |
| 308 | } |
| 309 | } |
| 310 | ]}} |
| 311 | )"_json; |
| 312 | |
| 313 | InvitedRoom room = data.get<InvitedRoom>(); |
| 314 | |
| 315 | EXPECT_EQ(room.invite_state.size(), 6); |
| 316 | |
| 317 | auto name = std::get<StrippedEvent<state::Name>>(room.invite_state[0]); |
| 318 | EXPECT_EQ(name.type, EventType::RoomName); |
| 319 | EXPECT_EQ(name.content.name, "Testing room"); |
| 320 | |
| 321 | auto avatar = std::get<StrippedEvent<state::Avatar>>(room.invite_state[1]); |
| 322 | EXPECT_EQ(avatar.type, EventType::RoomAvatar); |
| 323 | EXPECT_EQ(avatar.content.url, "mxc://matrix.org/wdjzHdrThpqWyVArfyWmRbBx"); |
| 324 | } |
| 325 | |
| 326 | TEST(Responses, Sync) |
| 327 | { |
| 328 | std::ifstream file(fixture_prefix() + "/fixtures/responses/sync.json"); |
| 329 | |
| 330 | json data1; |
| 331 | file >> data1; |
| 332 | |
| 333 | Sync sync1 = data1.get<Sync>(); |
| 334 | |
| 335 | EXPECT_EQ(sync1.next_batch, |
| 336 | "s333358558_324502987_444424_65663508_21685260_193623_2377336_2940807_454"); |
| 337 | EXPECT_EQ(sync1.rooms.join.size(), 5); |
| 338 | |
| 339 | auto nheko = sync1.rooms.join["!BPvgRcBVHzyFSlYkrg:matrix.org"]; |
| 340 | EXPECT_GT(nheko.state.events.size(), 0); |
| 341 | EXPECT_GT(nheko.timeline.events.size(), 0); |
| 342 | EXPECT_EQ(nheko.timeline.limited, true); |
| 343 | EXPECT_EQ(nheko.timeline.prev_batch, |
| 344 | "t10853-333025362_324502987_444424_65663508_21685260_193623_2377336_2940807_454"); |
| 345 | EXPECT_EQ(nheko.account_data.events.size(), 2); |
| 346 | |
| 347 | EXPECT_EQ(sync1.rooms.leave.size(), 1); |
| 348 | EXPECT_EQ(sync1.rooms.invite.size(), 0); |
| 349 | |
| 350 | // Check consistency of incomplete sync |
| 351 | json data2 = R"({ |
| 352 | "device_one_time_keys_count": {}, |
| 353 | "next_batch": "s123_42_42_42_42_42_42_42_7" |
| 354 | })"_json; |
| 355 | |
| 356 | Sync sync2 = data2.get<Sync>(); |
| 357 | |
| 358 | EXPECT_EQ(sync2.next_batch, "s123_42_42_42_42_42_42_42_7"); |
| 359 | EXPECT_EQ(sync2.rooms.join.size(), 0); |
| 360 | EXPECT_EQ(sync2.rooms.leave.size(), 0); |
| 361 | EXPECT_EQ(sync2.rooms.invite.size(), 0); |
| 362 | |
| 363 | json data_missing_fields = R"( |
| 364 | { |
| 365 | "account_data": { |
| 366 | "events": [ |
| 367 | { |
| 368 | "content": { |
| 369 | "custom_config_key": "custom_config_value" |
| 370 | }, |
| 371 | "type": "org.example.custom.config" |
| 372 | } |
| 373 | ] |
| 374 | }, |
| 375 | "next_batch": "s72595_4483_1934", |
| 376 | "presence": { |
| 377 | "events": [ |
| 378 | { |
| 379 | "content": { |
| 380 | "avatar_url": "mxc://localhost/wefuiwegh8742w", |
| 381 | "currently_active": false, |
| 382 | "last_active_ago": 2478593, |
| 383 | "presence": "online", |
| 384 | "status_msg": "Making cupcakes" |
| 385 | }, |
| 386 | "sender": "@example:localhost", |
| 387 | "type": "m.presence" |
| 388 | } |
| 389 | ] |
| 390 | }, |
| 391 | "rooms": { |
| 392 | "invite": { |
| 393 | "!696r7674:example.com": { |
| 394 | "invite_state": { |
| 395 | "events": [ |
| 396 | { |
| 397 | "content": { |
| 398 | "name": "My Room Name" |
| 399 | }, |
| 400 | "sender": "@alice:example.com", |
| 401 | "state_key": "", |
| 402 | "type": "m.room.name" |
| 403 | }, |
| 404 | { |
| 405 | "content": { |
| 406 | "membership": "invite" |
| 407 | }, |
| 408 | "sender": "@alice:example.com", |
| 409 | "state_key": "@bob:example.com", |
| 410 | "type": "m.room.member" |
| 411 | } |
| 412 | ] |
| 413 | } |
| 414 | }, |
| 415 | "!696r7675:example.com": {} |
| 416 | }, |
| 417 | "join": { |
| 418 | "!726s6s6q:example.com": { |
| 419 | "account_data": { |
| 420 | "events": [ |
| 421 | { |
| 422 | "content": { |
| 423 | "tags": { |
| 424 | "u.work": { |
| 425 | "order": 0.9 |
| 426 | } |
| 427 | } |
| 428 | }, |
| 429 | "type": "m.tag" |
| 430 | }, |
| 431 | { |
| 432 | "content": { |
| 433 | "custom_config_key": "custom_config_value" |
| 434 | }, |
| 435 | "type": "org.example.custom.room.config" |
| 436 | } |
| 437 | ] |
| 438 | }, |
| 439 | "ephemeral": { |
| 440 | "events": [ |
| 441 | { |
| 442 | "content": { |
| 443 | "user_ids": [ |
| 444 | "@alice:matrix.org", |
| 445 | "@bob:example.com" |
| 446 | ] |
| 447 | }, |
| 448 | "type": "m.typing" |
| 449 | } |
| 450 | ] |
| 451 | }, |
| 452 | "state": { |
| 453 | "events": [ |
| 454 | { |
| 455 | "content": { |
| 456 | "avatar_url": "mxc://example.org/SEsfnsuifSDFSSEF", |
| 457 | "displayname": "Alice Margatroid", |
| 458 | "membership": "join", |
| 459 | "reason": "Looking for support" |
| 460 | }, |
| 461 | "event_id": "$143273582443PhrSn:example.org", |
| 462 | "origin_server_ts": 1432735824653, |
| 463 | "room_id": "!jEsUZKDJdhlrceRyVU:example.org", |
| 464 | "sender": "@example:example.org", |
| 465 | "state_key": "@alice:example.org", |
| 466 | "type": "m.room.member", |
| 467 | "unsigned": { |
| 468 | "age": 1234 |
| 469 | } |
| 470 | } |
| 471 | ] |
| 472 | }, |
| 473 | "summary": { |
| 474 | "m.heroes": [ |
| 475 | "@alice:example.com", |
| 476 | "@bob:example.com" |
| 477 | ], |
| 478 | "m.invited_member_count": 0, |
| 479 | "m.joined_member_count": 2 |
| 480 | }, |
| 481 | "timeline": { |
| 482 | "events": [ |
| 483 | { |
| 484 | "content": { |
| 485 | "avatar_url": "mxc://example.org/SEsfnsuifSDFSSEF", |
| 486 | "displayname": "Alice Margatroid", |
| 487 | "membership": "join", |
| 488 | "reason": "Looking for support" |
| 489 | }, |
| 490 | "event_id": "$143273582443PhrSn:example.org", |
| 491 | "origin_server_ts": 1432735824653, |
| 492 | "room_id": "!jEsUZKDJdhlrceRyVU:example.org", |
| 493 | "sender": "@example:example.org", |
| 494 | "state_key": "@alice:example.org", |
| 495 | "type": "m.room.member", |
| 496 | "unsigned": { |
| 497 | "age": 1234 |
| 498 | } |
| 499 | }, |
| 500 | { |
| 501 | "content": { |
| 502 | "body": "This is an example text message", |
| 503 | "format": "org.matrix.custom.html", |
| 504 | "formatted_body": "<b>This is an example text message</b>", |
| 505 | "msgtype": "m.text" |
| 506 | }, |
| 507 | "event_id": "$143273582443PhrSn:example.org", |
| 508 | "origin_server_ts": 1432735824653, |
| 509 | "room_id": "!jEsUZKDJdhlrceRyVU:example.org", |
| 510 | "sender": "@example:example.org", |
| 511 | "type": "m.room.message", |
| 512 | "unsigned": { |
| 513 | "age": 1234 |
| 514 | } |
| 515 | } |
| 516 | ], |
| 517 | "limited": true, |
| 518 | "prev_batch": "t34-23535_0_0" |
| 519 | } |
| 520 | }, |
| 521 | "!726s7s6q:example.com": {} |
| 522 | }, |
| 523 | "knock": { |
| 524 | "!223asd456:example.com": { |
| 525 | "knock_state": { |
| 526 | "events": [ |
| 527 | { |
| 528 | "content": { |
| 529 | "name": "My Room Name" |
| 530 | }, |
| 531 | "sender": "@alice:example.com", |
| 532 | "state_key": "", |
| 533 | "type": "m.room.name" |
| 534 | }, |
| 535 | { |
| 536 | "content": { |
| 537 | "membership": "knock" |
| 538 | }, |
| 539 | "sender": "@bob:example.com", |
| 540 | "state_key": "@bob:example.com", |
| 541 | "type": "m.room.member" |
| 542 | } |
| 543 | ] |
| 544 | } |
| 545 | }, |
| 546 | "!223asd457:example.com": {} |
| 547 | }, |
| 548 | "leave": {} |
| 549 | } |
| 550 | })"_json; |
| 551 | Sync sync3 = data_missing_fields.get<Sync>(); |
| 552 | EXPECT_EQ(sync3.rooms.invite.count("!696r7675:example.com"), 1); |
| 553 | EXPECT_EQ(sync3.rooms.knock.count("!223asd457:example.com"), 1); |
| 554 | EXPECT_EQ(sync3.rooms.join.count("!726s7s6q:example.com"), 1); |
| 555 | |
| 556 | // verify that fields without "events" key parse |
| 557 | // see: https://github.com/Nheko-Reborn/nheko/issues/1257 |
| 558 | json data4 = R"({ |
| 559 | "device_one_time_keys_count": {}, |
| 560 | "account_data": null, |
| 561 | "next_batch": "s123_42_42_42_42_42_42_42_7", |
| 562 | "presence": { |
| 563 | "events": [ |
| 564 | { |
| 565 | "content": { |
| 566 | "avatar_url": "mxc://localhost/wefuiwegh8742w", |
| 567 | "currently_active": false, |
| 568 | "last_active_ago": 2478593, |
| 569 | "presence": "online", |
| 570 | "status_msg": "Making cupcakes" |
| 571 | }, |
| 572 | "sender": "@example:localhost", |
| 573 | "type": "m.presence" |
| 574 | } |
| 575 | ] |
| 576 | }, |
| 577 | "rooms": { |
| 578 | "invite": { |
| 579 | "!696r7674:example.com": { |
| 580 | "invite_state": { |
| 581 | } |
| 582 | }, |
| 583 | "!696r7675:example.com": {} |
| 584 | }, |
| 585 | "join": { |
| 586 | "!726s6s6q:example.com": { |
| 587 | "account_data": { |
| 588 | }, |
| 589 | "ephemeral": { |
| 590 | }, |
| 591 | "state": { |
| 592 | }, |
| 593 | "summary": { |
| 594 | "m.heroes": [ |
| 595 | "@alice:example.com", |
| 596 | "@bob:example.com" |
| 597 | ], |
| 598 | "m.invited_member_count": 0, |
| 599 | "m.joined_member_count": 2 |
| 600 | }, |
| 601 | "timeline": { |
| 602 | "events": [ |
| 603 | ], |
| 604 | "limited": true, |
| 605 | "prev_batch": "t34-23535_0_0" |
| 606 | } |
| 607 | }, |
| 608 | "!726s7s6q:example.com": {} |
| 609 | } |
| 610 | } |
| 611 | })"_json; |
| 612 | |
| 613 | Sync sync4 = data4.get<Sync>(); |
| 614 | |
| 615 | EXPECT_EQ(sync4.next_batch, "s123_42_42_42_42_42_42_42_7"); |
| 616 | } |
| 617 | |
| 618 | TEST(Responses, SyncWithEncryption) |
| 619 | { |
| 620 | std::ifstream file(fixture_prefix() + "/fixtures/responses/sync_with_crypto.json"); |
| 621 | |
| 622 | json data; |
| 623 | file >> data; |
| 624 | |
| 625 | Sync sync = data.get<Sync>(); |
| 626 | |
| 627 | EXPECT_EQ(sync.device_lists.changed.size(), 1); |
| 628 | EXPECT_EQ(sync.device_lists.changed.at(0), "@carl:matrix.org"); |
| 629 | |
| 630 | EXPECT_EQ(sync.device_lists.left.size(), 2); |
| 631 | EXPECT_EQ(sync.device_lists.left.at(0), "@alice:matrix.org"); |
| 632 | EXPECT_EQ(sync.device_lists.left.at(1), "@bob:matrix.org"); |
| 633 | |
| 634 | EXPECT_EQ(sync.device_one_time_keys_count.size(), 2); |
| 635 | EXPECT_EQ(sync.device_one_time_keys_count["curve25519"], 10); |
| 636 | EXPECT_EQ(sync.device_one_time_keys_count["signed_curve25519"], 50); |
| 637 | |
| 638 | auto timeline_events = sync.rooms.join.begin()->second.timeline.events; |
| 639 | |
| 640 | std::string algorithm_found; |
| 641 | std::string event_id; |
| 642 | for (const auto &e : timeline_events) { |
| 643 | if (auto enc_event = std::get_if<StateEvent<mtx::events::state::Encryption>>(&e); |
| 644 | enc_event != nullptr) { |
| 645 | algorithm_found = enc_event->content.algorithm; |
| 646 | event_id = enc_event->event_id; |
| 647 | } |
| 648 | } |
| 649 | |
| 650 | EXPECT_EQ(algorithm_found, "m.megolm.v1.aes-sha2"); |
| 651 | EXPECT_EQ(event_id, "$1522842442112652dsEBQ:matrix.org"); |
| 652 | } |
| 653 | |
| 654 | TEST(Responses, Rooms) {} |
| 655 | |
| 656 | TEST(Responses, Members) |
| 657 | { |
| 658 | json data1 = R"( |
| 659 | { |
| 660 | "chunk": [ |
| 661 | { |
| 662 | "content": { |
| 663 | "avatar_url": "mxc://example.org/SEsfnsuifSDFSSEF", |
| 664 | "displayname": "Alice Margatroid", |
| 665 | "membership": "join", |
| 666 | "reason": "Looking for support" |
| 667 | }, |
| 668 | "event_id": "$143273582443PhrSn:example.org", |
| 669 | "origin_server_ts": 1432735824653, |
| 670 | "room_id": "!636q39766251:example.com", |
| 671 | "sender": "@example:example.org", |
| 672 | "state_key": "@alice:example.org", |
| 673 | "type": "m.room.member", |
| 674 | "unsigned": { |
| 675 | "age": 1234 |
| 676 | } |
| 677 | } |
| 678 | ] |
| 679 | })"_json; |
| 680 | |
| 681 | Members members = data1.get<Members>(); |
| 682 | |
| 683 | // It this succeeds parsing was done successfully |
| 684 | EXPECT_EQ(members.chunk.size(), 1); |
| 685 | EXPECT_EQ(members.chunk[0].content.display_name, "Alice Margatroid"); |
| 686 | } |
| 687 | |
| 688 | TEST(Responses, Profile) |
| 689 | { |
| 690 | json response = R"({ |
| 691 | "avatar_url": "mxc://matrix.org/SDGdghriugerRg", |
| 692 | "displayname": "Alice Margatroid" |
| 693 | })"_json; |
| 694 | |
| 695 | json null_response = R"({ |
| 696 | "avatar_url": "mxc://matrix.org/SDGdghriugerRg", |
| 697 | "displayname": null |
| 698 | })"_json; |
| 699 | |
| 700 | json missing_response = R"({ |
| 701 | "displayname": "Alice Margatroid" |
| 702 | })"_json; |
| 703 | |
| 704 | json error_response = R"({ |
| 705 | "displayname": 42 |
| 706 | })"_json; |
| 707 | |
| 708 | Profile profile = response.get<Profile>(); |
| 709 | EXPECT_EQ(profile.avatar_url, "mxc://matrix.org/SDGdghriugerRg"); |
| 710 | EXPECT_EQ(profile.display_name, "Alice Margatroid"); |
| 711 | |
| 712 | Profile null_profile = null_response.get<Profile>(); |
| 713 | EXPECT_EQ(null_profile.avatar_url, "mxc://matrix.org/SDGdghriugerRg"); |
| 714 | EXPECT_EQ(null_profile.display_name, ""); |
| 715 | |
| 716 | Profile missing_profile = missing_response.get<Profile>(); |
| 717 | EXPECT_EQ(missing_profile.avatar_url, ""); |
| 718 | EXPECT_EQ(missing_profile.display_name, "Alice Margatroid"); |
| 719 | |
| 720 | ASSERT_THROW(Profile error_profile = error_response.get<Profile>(), std::exception); |
| 721 | } |
| 722 | |
| 723 | TEST(Responses, Versions) |
| 724 | { |
| 725 | json data = R"({ |
| 726 | "versions" : [ |
| 727 | "r0.0.1", |
| 728 | "r0.2.0", |
| 729 | "r0.3.0" |
| 730 | ] |
| 731 | })"_json; |
| 732 | |
| 733 | Versions versions = data.get<Versions>(); |
| 734 | EXPECT_EQ(versions.versions.size(), 3); |
| 735 | EXPECT_EQ(versions.versions[0], "r0.0.1"); |
| 736 | EXPECT_EQ(versions.versions[1], "r0.2.0"); |
| 737 | EXPECT_EQ(versions.versions[2], "r0.3.0"); |
| 738 | |
| 739 | json error_data = R"({ |
| 740 | "versions" : [ |
| 741 | "" |
| 742 | ] |
| 743 | })"_json; |
| 744 | |
| 745 | ASSERT_THROW(Versions versions = error_data.get<Versions>(), std::invalid_argument); |
| 746 | } |
| 747 | |
| 748 | TEST(Responses, WellKnown) |
| 749 | { |
| 750 | json data = R"({ |
| 751 | "m.homeserver": { |
| 752 | "base_url": "https://matrix.example.com" |
| 753 | }, |
| 754 | "m.identity_server": { |
| 755 | "base_url": "https://identity.example.com" |
| 756 | }, |
| 757 | "org.example.custom.property": { |
| 758 | "app_url": "https://custom.app.example.org" |
| 759 | } |
| 760 | })"_json; |
| 761 | |
| 762 | WellKnown wellknown = data.get<WellKnown>(); |
| 763 | EXPECT_EQ(wellknown.homeserver.base_url, "https://matrix.example.com"); |
| 764 | EXPECT_EQ(wellknown.identity_server->base_url, "https://identity.example.com"); |
| 765 | } |
| 766 | |
| 767 | TEST(Responses, RegistrationTokenValidity) |
| 768 | { |
| 769 | json data = R"({"valid" : true})"_json; |
| 770 | |
| 771 | RegistrationTokenValidity validity = data.get<RegistrationTokenValidity>(); |
| 772 | EXPECT_EQ(validity.valid, true); |
| 773 | } |
| 774 | |
| 775 | TEST(Responses, Capabilities) |
| 776 | { |
| 777 | json data = R"( |
| 778 | { |
| 779 | "capabilities": { |
| 780 | "com.example.custom.ratelimit": { |
| 781 | "max_requests_per_hour": 600 |
| 782 | }, |
| 783 | "m.change_password": { |
| 784 | "enabled": false |
| 785 | }, |
| 786 | "m.room_versions": { |
| 787 | "available": { |
| 788 | "1": "stable", |
| 789 | "2": "stable", |
| 790 | "3": "unstable", |
| 791 | "test-version": "unstable" |
| 792 | }, |
| 793 | "default": "1" |
| 794 | } |
| 795 | } |
| 796 | } |
| 797 | )"_json; |
| 798 | |
| 799 | namespace cap = mtx::responses::capabilities; |
| 800 | |
| 801 | cap::Capabilities c = data.get<cap::Capabilities>(); |
| 802 | EXPECT_EQ(c.room_versions.default_, "1"); |
| 803 | EXPECT_EQ(c.room_versions.available["1"], capabilities::RoomVersionStability::Stable); |
| 804 | EXPECT_EQ(c.room_versions.available["2"], capabilities::RoomVersionStability::Stable); |
| 805 | EXPECT_EQ(c.room_versions.available["3"], capabilities::RoomVersionStability::Unstable); |
| 806 | EXPECT_EQ(c.room_versions.available["not-listed"], |
| 807 | capabilities::RoomVersionStability::Unstable); |
| 808 | EXPECT_EQ(c.room_versions.available["test-version"], |
| 809 | capabilities::RoomVersionStability::Unstable); |
| 810 | EXPECT_EQ(c.change_3pid.enabled, true); |
| 811 | EXPECT_EQ(c.change_password.enabled, false); |
| 812 | EXPECT_EQ(c.set_avatar_url.enabled, true); |
| 813 | EXPECT_EQ(c.set_displayname.enabled, true); |
| 814 | |
| 815 | data = R"( |
| 816 | { |
| 817 | "capabilities": { |
| 818 | "m.change_password": { |
| 819 | "enabled": false |
| 820 | } |
| 821 | } |
| 822 | } |
| 823 | )"_json; |
| 824 | |
| 825 | c = data.get<cap::Capabilities>(); |
| 826 | EXPECT_EQ(c.room_versions.default_, "1"); |
| 827 | EXPECT_EQ(c.room_versions.available["1"], capabilities::RoomVersionStability::Stable); |
| 828 | EXPECT_EQ(c.room_versions.available["2"], capabilities::RoomVersionStability::Unstable); |
| 829 | EXPECT_EQ(c.room_versions.available["3"], capabilities::RoomVersionStability::Unstable); |
| 830 | EXPECT_EQ(c.room_versions.available["not-listed"], |
| 831 | capabilities::RoomVersionStability::Unstable); |
| 832 | EXPECT_EQ(c.room_versions.available["test-version"], |
| 833 | capabilities::RoomVersionStability::Unstable); |
| 834 | EXPECT_EQ(c.change_3pid.enabled, true); |
| 835 | EXPECT_EQ(c.change_password.enabled, false); |
| 836 | EXPECT_EQ(c.set_avatar_url.enabled, true); |
| 837 | EXPECT_EQ(c.set_displayname.enabled, true); |
| 838 | |
| 839 | data = R"( |
| 840 | { |
| 841 | "capabilities": { |
| 842 | "m.room_versions": { |
| 843 | "default": "2", |
| 844 | "available": { |
| 845 | "1": "stable", |
| 846 | "2": "stable", |
| 847 | "3": "unstable", |
| 848 | "custom-version": "unstable" |
| 849 | } |
| 850 | } |
| 851 | } |
| 852 | } |
| 853 | )"_json; |
| 854 | |
| 855 | c = data.get<cap::Capabilities>(); |
| 856 | EXPECT_EQ(c.room_versions.default_, "2"); |
| 857 | EXPECT_EQ(c.room_versions.available["1"], capabilities::RoomVersionStability::Stable); |
| 858 | EXPECT_EQ(c.room_versions.available["2"], capabilities::RoomVersionStability::Stable); |
| 859 | EXPECT_EQ(c.room_versions.available["3"], capabilities::RoomVersionStability::Unstable); |
| 860 | EXPECT_EQ(c.room_versions.available["not-listed"], |
| 861 | capabilities::RoomVersionStability::Unstable); |
| 862 | EXPECT_EQ(c.room_versions.available["test-version"], |
| 863 | capabilities::RoomVersionStability::Unstable); |
| 864 | EXPECT_EQ(c.change_3pid.enabled, true); |
| 865 | EXPECT_EQ(c.change_password.enabled, true); |
| 866 | EXPECT_EQ(c.set_avatar_url.enabled, true); |
| 867 | EXPECT_EQ(c.set_displayname.enabled, true); |
| 868 | |
| 869 | data = R"( |
| 870 | { |
| 871 | "capabilities": { |
| 872 | "m.set_displayname": { |
| 873 | "enabled": false |
| 874 | } |
| 875 | } |
| 876 | } |
| 877 | )"_json; |
| 878 | |
| 879 | c = data.get<cap::Capabilities>(); |
| 880 | EXPECT_EQ(c.room_versions.default_, "1"); |
| 881 | EXPECT_EQ(c.room_versions.available["1"], capabilities::RoomVersionStability::Stable); |
| 882 | EXPECT_EQ(c.room_versions.available["2"], capabilities::RoomVersionStability::Unstable); |
| 883 | EXPECT_EQ(c.room_versions.available["3"], capabilities::RoomVersionStability::Unstable); |
| 884 | EXPECT_EQ(c.room_versions.available["not-listed"], |
| 885 | capabilities::RoomVersionStability::Unstable); |
| 886 | EXPECT_EQ(c.room_versions.available["test-version"], |
| 887 | capabilities::RoomVersionStability::Unstable); |
| 888 | EXPECT_EQ(c.change_3pid.enabled, true); |
| 889 | EXPECT_EQ(c.change_password.enabled, true); |
| 890 | EXPECT_EQ(c.set_avatar_url.enabled, true); |
| 891 | EXPECT_EQ(c.set_displayname.enabled, false); |
| 892 | |
| 893 | data = R"( |
| 894 | { |
| 895 | "capabilities": { |
| 896 | "m.set_avatar_url": { |
| 897 | "enabled": false |
| 898 | } |
| 899 | } |
| 900 | } |
| 901 | )"_json; |
| 902 | |
| 903 | c = data.get<cap::Capabilities>(); |
| 904 | EXPECT_EQ(c.room_versions.default_, "1"); |
| 905 | EXPECT_EQ(c.room_versions.available["1"], capabilities::RoomVersionStability::Stable); |
| 906 | EXPECT_EQ(c.room_versions.available["2"], capabilities::RoomVersionStability::Unstable); |
| 907 | EXPECT_EQ(c.room_versions.available["3"], capabilities::RoomVersionStability::Unstable); |
| 908 | EXPECT_EQ(c.room_versions.available["not-listed"], |
| 909 | capabilities::RoomVersionStability::Unstable); |
| 910 | EXPECT_EQ(c.room_versions.available["test-version"], |
| 911 | capabilities::RoomVersionStability::Unstable); |
| 912 | EXPECT_EQ(c.change_3pid.enabled, true); |
| 913 | EXPECT_EQ(c.change_password.enabled, true); |
| 914 | EXPECT_EQ(c.set_avatar_url.enabled, false); |
| 915 | EXPECT_EQ(c.set_displayname.enabled, true); |
| 916 | |
| 917 | data = R"( |
| 918 | { |
| 919 | "capabilities": { |
| 920 | "m.3pid_changes": { |
| 921 | "enabled": false |
| 922 | } |
| 923 | } |
| 924 | } |
| 925 | )"_json; |
| 926 | |
| 927 | c = data.get<cap::Capabilities>(); |
| 928 | EXPECT_EQ(c.room_versions.default_, "1"); |
| 929 | EXPECT_EQ(c.room_versions.available["1"], capabilities::RoomVersionStability::Stable); |
| 930 | EXPECT_EQ(c.room_versions.available["2"], capabilities::RoomVersionStability::Unstable); |
| 931 | EXPECT_EQ(c.room_versions.available["3"], capabilities::RoomVersionStability::Unstable); |
| 932 | EXPECT_EQ(c.room_versions.available["not-listed"], |
| 933 | capabilities::RoomVersionStability::Unstable); |
| 934 | EXPECT_EQ(c.room_versions.available["test-version"], |
| 935 | capabilities::RoomVersionStability::Unstable); |
| 936 | EXPECT_EQ(c.change_3pid.enabled, false); |
| 937 | EXPECT_EQ(c.change_password.enabled, true); |
| 938 | EXPECT_EQ(c.set_avatar_url.enabled, true); |
| 939 | EXPECT_EQ(c.set_displayname.enabled, true); |
| 940 | } |
| 941 | TEST(Responses, CreateRoom) |
| 942 | { |
| 943 | json data = R"({"room_id" : "!sefiuhWgwghwWgh:example.com"})"_json; |
| 944 | |
| 945 | mtx::responses::CreateRoom create_room = data.get<mtx::responses::CreateRoom>(); |
| 946 | EXPECT_EQ(create_room.room_id.to_string(), "!sefiuhWgwghwWgh:example.com"); |
| 947 | |
| 948 | json error_data = R"({"room_id" : "#akajdkf:example.com"})"_json; |
| 949 | |
| 950 | ASSERT_THROW(CreateRoom create_room = error_data.get<CreateRoom>(), std::invalid_argument); |
| 951 | } |
| 952 | |
| 953 | TEST(Responses, Login) |
| 954 | { |
| 955 | json data = R"({ |
| 956 | "user_id": "@cheeky_monkey:matrix.org", |
| 957 | "access_token": "abc123", |
| 958 | "home_server": "matrix.org", |
| 959 | "device_id": "GHTYAJCE", |
| 960 | "well_known": { |
| 961 | "m.homeserver": { |
| 962 | "base_url": "https://example.org" |
| 963 | }, |
| 964 | "m.identity_server": { |
| 965 | "base_url": "https://id.example.org" |
| 966 | } |
| 967 | } |
| 968 | })"_json; |
| 969 | |
| 970 | Login login = data.get<Login>(); |
| 971 | EXPECT_EQ(login.user_id.to_string(), "@cheeky_monkey:matrix.org"); |
| 972 | EXPECT_EQ(login.access_token, "abc123"); |
| 973 | EXPECT_EQ(login.device_id, "GHTYAJCE"); |
| 974 | EXPECT_EQ(login.well_known->homeserver.base_url, "https://example.org"); |
| 975 | EXPECT_EQ(login.well_known->identity_server->base_url, "https://id.example.org"); |
| 976 | |
| 977 | json data2 = R"({ |
| 978 | "user_id": "@cheeky_monkey:matrix.org", |
| 979 | "access_token": "abc123", |
| 980 | "home_server": "matrix.org" |
| 981 | })"_json; |
| 982 | |
| 983 | Login login2 = data2.get<Login>(); |
| 984 | EXPECT_EQ(login2.user_id.to_string(), "@cheeky_monkey:matrix.org"); |
| 985 | EXPECT_EQ(login2.access_token, "abc123"); |
| 986 | EXPECT_EQ(login2.device_id, ""); |
| 987 | |
| 988 | json data3 = R"({ |
| 989 | "user_id": "@cheeky_monkey:matrix.org", |
| 990 | "access_token": "abc123" |
| 991 | })"_json; |
| 992 | Login login3 = data3.get<Login>(); |
| 993 | EXPECT_EQ(login3.user_id.to_string(), "@cheeky_monkey:matrix.org"); |
| 994 | EXPECT_EQ(login3.access_token, "abc123"); |
| 995 | EXPECT_EQ(login3.device_id, ""); |
| 996 | } |
| 997 | |
| 998 | TEST(Responses, Messages) |
| 999 | { |
| 1000 | json data = R"({ |
| 1001 | "start": "t47429-4392820_219380_26003_2265", |
| 1002 | "end": "t47409-4357353_219380_26003_2265", |
| 1003 | "chunk": [{ |
| 1004 | "origin_server_ts": 1444812213737, |
| 1005 | "sender": "@alice:example.com", |
| 1006 | "event_id": "$1444812213350496Caaaa:example.com", |
| 1007 | "content": { |
| 1008 | "body": "hello world", |
| 1009 | "msgtype": "m.text" |
| 1010 | }, |
| 1011 | "room_id": "!Xq3620DUiqCaoxq:example.com", |
| 1012 | "type": "m.room.message", |
| 1013 | "age": 1042 |
| 1014 | }, { |
| 1015 | "origin_server_ts": 1444812194656, |
| 1016 | "sender": "@bob:example.com", |
| 1017 | "event_id": "$1444812213350496Cbbbb:example.com", |
| 1018 | "content": { |
| 1019 | "body": "the world is big", |
| 1020 | "msgtype": "m.text" |
| 1021 | }, |
| 1022 | "room_id": "!Xq3620DUiqCaoxq:example.com", |
| 1023 | "type": "m.room.message", |
| 1024 | "age": 20123 |
| 1025 | }, { |
| 1026 | "origin_server_ts": 1444812163990, |
| 1027 | "sender": "@bob:example.com", |
| 1028 | "event_id": "$1444812213350496Ccccc:example.com", |
| 1029 | "content": { |
| 1030 | "name": "New room name" |
| 1031 | }, |
| 1032 | "prev_content": { |
| 1033 | "name": "Old room name" |
| 1034 | }, |
| 1035 | "state_key": "", |
| 1036 | "room_id": "!Xq3620DUiqCaoxq:example.com", |
| 1037 | "type": "m.room.name", |
| 1038 | "age": 50789 |
| 1039 | }, { |
| 1040 | "origin_server_ts": 1444812198888, |
| 1041 | "sender": "@bob:example.com", |
| 1042 | "event_id": "$1444812213350496Cdddd:example.com", |
| 1043 | "content": { |
| 1044 | "body": "i like custom things", |
| 1045 | "msgtype": "im.nheko.test_msgtype" |
| 1046 | }, |
| 1047 | "room_id": "!Xq3620DUiqCaoxq:example.com", |
| 1048 | "type": "m.room.message", |
| 1049 | "age": 69290 |
| 1050 | } |
| 1051 | ]})"_json; |
| 1052 | |
| 1053 | Messages messages = data.get<Messages>(); |
| 1054 | EXPECT_EQ(messages.start, "t47429-4392820_219380_26003_2265"); |
| 1055 | EXPECT_EQ(messages.end, "t47409-4357353_219380_26003_2265"); |
| 1056 | EXPECT_EQ(messages.chunk.size(), 4); |
| 1057 | |
| 1058 | using mtx::events::RoomEvent; |
| 1059 | using mtx::events::StateEvent; |
| 1060 | using mtx::events::msg::Text; |
| 1061 | using mtx::events::state::Name; |
| 1062 | |
| 1063 | auto first_event = std::get<RoomEvent<Text>>(messages.chunk[0]); |
| 1064 | EXPECT_EQ(first_event.content.body, "hello world"); |
| 1065 | EXPECT_EQ(first_event.content.msgtype, "m.text"); |
| 1066 | EXPECT_EQ(first_event.type, mtx::events::EventType::RoomMessage); |
| 1067 | EXPECT_EQ(first_event.event_id, "$1444812213350496Caaaa:example.com"); |
| 1068 | |
| 1069 | auto second_event = std::get<RoomEvent<Text>>(messages.chunk[1]); |
| 1070 | EXPECT_EQ(second_event.content.body, "the world is big"); |
| 1071 | EXPECT_EQ(second_event.content.msgtype, "m.text"); |
| 1072 | EXPECT_EQ(second_event.type, mtx::events::EventType::RoomMessage); |
| 1073 | EXPECT_EQ(second_event.event_id, "$1444812213350496Cbbbb:example.com"); |
| 1074 | |
| 1075 | auto third_event = std::get<StateEvent<Name>>(messages.chunk[2]); |
| 1076 | EXPECT_EQ(third_event.content.name, "New room name"); |
| 1077 | EXPECT_EQ(third_event.type, mtx::events::EventType::RoomName); |
| 1078 | EXPECT_EQ(third_event.event_id, "$1444812213350496Ccccc:example.com"); |
| 1079 | EXPECT_EQ(third_event.sender, "@bob:example.com"); |
| 1080 | |
| 1081 | auto fourth_event = std::get<RoomEvent<mtx::events::msg::Unknown>>(messages.chunk[3]); |
| 1082 | EXPECT_EQ(fourth_event.content.body, "i like custom things"); |
| 1083 | EXPECT_EQ(fourth_event.content.msgtype, "im.nheko.test_msgtype"); |
| 1084 | EXPECT_EQ(fourth_event.type, mtx::events::EventType::RoomMessage); |
| 1085 | EXPECT_EQ(fourth_event.event_id, "$1444812213350496Cdddd:example.com"); |
| 1086 | |
| 1087 | // Three of the events are malformed |
| 1088 | // 1. Missing "type" key and should be dropped. |
| 1089 | // 2. Content is null and should be parsed as redacted. |
| 1090 | // 3. msgtype is not a string and should be dropped. |
| 1091 | json malformed_data = R"({ |
| 1092 | "start": "t47429-4392820_219380_26003_2265", |
| 1093 | "end": "t47409-4357353_219380_26003_2265", |
| 1094 | "chunk": [{ |
| 1095 | "origin_server_ts": 1444812213737, |
| 1096 | "sender": "@alice:example.com", |
| 1097 | "event_id": "$1444812213350496Caaaa:example.com", |
| 1098 | "content": { |
| 1099 | "body": "hello world", |
| 1100 | "msgtype": "m.text" |
| 1101 | }, |
| 1102 | "room_id": "!Xq3620DUiqCaoxq:example.com", |
| 1103 | "age": 1042 |
| 1104 | }, { |
| 1105 | "origin_server_ts": 1444812194656, |
| 1106 | "sender": "@bob:example.com", |
| 1107 | "event_id": "$1444812213350496Cbbbb:example.com", |
| 1108 | "content": null, |
| 1109 | "room_id": "!Xq3620DUiqCaoxq:example.com", |
| 1110 | "type": "m.room.message", |
| 1111 | "age": 20123 |
| 1112 | }, { |
| 1113 | "origin_server_ts": 1510489356530, |
| 1114 | "sender": "@nheko_test:matrix.org", |
| 1115 | "event_id": "$15104893562785758wEgEU:matrix.org", |
| 1116 | "unsigned": { |
| 1117 | "age": 2225, |
| 1118 | "transaction_id": "m1510489356267.2" |
| 1119 | }, |
| 1120 | "content": { |
| 1121 | "body": "DON'T PANIC", |
| 1122 | "msgtype": 42 |
| 1123 | }, |
| 1124 | "type": "m.room.message", |
| 1125 | "room_id": "!lfoDRlNFWlvOnvkBwQ:matrix.org" |
| 1126 | }, { |
| 1127 | "origin_server_ts": 1444812163990, |
| 1128 | "sender": "@bob:example.com", |
| 1129 | "event_id": "$1444812213350496Ccccc:example.com", |
| 1130 | "content": { |
| 1131 | "name": "New room name" |
| 1132 | }, |
| 1133 | "prev_content": { |
| 1134 | "name": "Old room name" |
| 1135 | }, |
| 1136 | "state_key": "", |
| 1137 | "room_id": "!Xq3620DUiqCaoxq:example.com", |
| 1138 | "type": "m.room.name", |
| 1139 | "age": 50789 |
| 1140 | } |
| 1141 | ]})"_json; |
| 1142 | |
| 1143 | messages = malformed_data.get<Messages>(); |
| 1144 | EXPECT_EQ(messages.start, "t47429-4392820_219380_26003_2265"); |
| 1145 | EXPECT_EQ(messages.end, "t47409-4357353_219380_26003_2265"); |
| 1146 | ASSERT_EQ(messages.chunk.size(), 2); |
| 1147 | |
| 1148 | EXPECT_TRUE(std::holds_alternative<RoomEvent<Unknown>>(messages.chunk[0])); |
| 1149 | |
| 1150 | third_event = std::get<StateEvent<Name>>(messages.chunk[1]); |
| 1151 | EXPECT_EQ(third_event.content.name, "New room name"); |
| 1152 | EXPECT_EQ(third_event.type, mtx::events::EventType::RoomName); |
| 1153 | EXPECT_EQ(third_event.event_id, "$1444812213350496Ccccc:example.com"); |
| 1154 | EXPECT_EQ(third_event.sender, "@bob:example.com"); |
| 1155 | |
| 1156 | json redactions = R"({ |
| 1157 | "start": "t47429-4392820_219380_26003_2265", |
| 1158 | "end": "t47409-4357353_219380_26003_2265", |
| 1159 | "chunk": [{ |
| 1160 | "content": { |
| 1161 | "reason": "Spamming" |
| 1162 | }, |
| 1163 | "event_id": "$143273582443PhrSn:example.org", |
| 1164 | "origin_server_ts": 1432735824653, |
| 1165 | "redacts": "$fukweghifu23:localhost", |
| 1166 | "room_id": "!jEsUZKDJdhlrceRyVU:example.org", |
| 1167 | "sender": "@example:example.org", |
| 1168 | "type": "m.room.redaction", |
| 1169 | "unsigned": { |
| 1170 | "age": 1234 |
| 1171 | } |
| 1172 | }, |
| 1173 | { |
| 1174 | "content": { |
| 1175 | "reason": "Spamming" |
| 1176 | }, |
| 1177 | "event_id": "$143273582443PhrSn:example.org", |
| 1178 | "origin_server_ts": 1432735824653, |
| 1179 | "redacts": "$fukweghifu23:localhost", |
| 1180 | "room_id": "!jEsUZKDJdhlrceRyVU:example.org", |
| 1181 | "sender": "@example:example.org", |
| 1182 | "type": "m.room.redaction", |
| 1183 | "unsigned": { |
| 1184 | "age": 1234 |
| 1185 | } |
| 1186 | } |
| 1187 | ]})"_json; |
| 1188 | |
| 1189 | messages = redactions.get<Messages>(); |
| 1190 | |
| 1191 | ASSERT_EQ(messages.chunk.size(), 2); |
| 1192 | auto redaction = std::get<RedactionEvent<msg::Redaction>>(messages.chunk[0]); |
| 1193 | EXPECT_EQ(redaction.type, mtx::events::EventType::RoomRedaction); |
| 1194 | redaction = std::get<RedactionEvent<msg::Redaction>>(messages.chunk[1]); |
| 1195 | EXPECT_EQ(redaction.type, mtx::events::EventType::RoomRedaction); |
| 1196 | } |
| 1197 | |
| 1198 | TEST(Responses, EphemeralTyping) |
| 1199 | { |
| 1200 | json data = R"({ |
| 1201 | "events": [{ |
| 1202 | "type": "m.typing", |
| 1203 | "content": { |
| 1204 | "user_ids": [ |
| 1205 | "@alice:example.com", |
| 1206 | "@bob:example.com" |
| 1207 | ] |
| 1208 | } |
| 1209 | }] |
| 1210 | })"_json; |
| 1211 | |
| 1212 | mtx::responses::Ephemeral ephemeral = data.get<mtx::responses::Ephemeral>(); |
| 1213 | |
| 1214 | EXPECT_EQ(ephemeral.events.size(), 1); |
| 1215 | ASSERT_TRUE(std::holds_alternative<mtx::events::EphemeralEvent<mtx::events::ephemeral::Typing>>( |
| 1216 | ephemeral.events[0])); |
| 1217 | auto e = |
| 1218 | std::get<mtx::events::EphemeralEvent<mtx::events::ephemeral::Typing>>(ephemeral.events[0]); |
| 1219 | |
| 1220 | EXPECT_EQ(e.content.user_ids.size(), 2); |
| 1221 | EXPECT_EQ(e.content.user_ids[0], "@alice:example.com"); |
| 1222 | EXPECT_EQ(e.content.user_ids[1], "@bob:example.com"); |
| 1223 | } |
| 1224 | |
| 1225 | TEST(Responses, EphemeralReceipts) |
| 1226 | { |
| 1227 | json data = R"({ |
| 1228 | "events": [{ |
| 1229 | "type": "m.typing", |
| 1230 | "content": { |
| 1231 | "user_ids": [ |
| 1232 | "@alice:example.com", |
| 1233 | "@bob:example.com" |
| 1234 | ] |
| 1235 | } |
| 1236 | }, { |
| 1237 | "type": "m.receipt", |
| 1238 | "content": { |
| 1239 | "$1493012095444993JeMrW:matrix.org": { |
| 1240 | "m.read": { |
| 1241 | "@trilobite17:matrix.org": { "ts": 1493020945945 } |
| 1242 | } |
| 1243 | }, |
| 1244 | "$1493135885261887UVyOW:matrix.org": { |
| 1245 | "m.read": { |
| 1246 | "@aaron:matrix.org": { "ts": 1493161552008 } |
| 1247 | } |
| 1248 | }, |
| 1249 | "$149339947230ohuCC:krtdex.com": { |
| 1250 | "m.read": { |
| 1251 | "@walle303:matrix.eastcoast.hosting": { "ts": 1493404654684 } |
| 1252 | } |
| 1253 | }, |
| 1254 | "$1493556582917fOMpi:vurpo.fi": { |
| 1255 | "m.read": { |
| 1256 | "@matthew2:matrix.org": { "ts": 1493557057338 } |
| 1257 | } |
| 1258 | }, |
| 1259 | "$14935874261161012PaoJD:matrix.org": { |
| 1260 | "m.read": { |
| 1261 | "@frantisek:gajdusek.net": { "ts": 1493623595682 }, |
| 1262 | "@PhoenixLandPirate:matrix.org": { "ts": 1510630539168 }, |
| 1263 | "@Tokodomo:matrix.org": { "ts": 1510588032780 }, |
| 1264 | "@matthew:matrix.org": { "ts": 1510440324233 }, |
| 1265 | "@memoryruins:matrix.org": { "ts": 1510518443679 }, |
| 1266 | "@nagua:2hg.org": { "ts": 1510451215569 }, |
| 1267 | "@nioshd:matrix.org": { "ts": 1510521086750 } |
| 1268 | } |
| 1269 | } |
| 1270 | } |
| 1271 | }] |
| 1272 | })"_json; |
| 1273 | |
| 1274 | mtx::responses::Ephemeral ephemeral = data.get<mtx::responses::Ephemeral>(); |
| 1275 | |
| 1276 | EXPECT_EQ(ephemeral.events.size(), 2); |
| 1277 | ASSERT_TRUE(std::holds_alternative<mtx::events::EphemeralEvent<mtx::events::ephemeral::Typing>>( |
| 1278 | ephemeral.events[0])); |
| 1279 | auto e = |
| 1280 | std::get<mtx::events::EphemeralEvent<mtx::events::ephemeral::Typing>>(ephemeral.events[0]); |
| 1281 | |
| 1282 | EXPECT_EQ(e.content.user_ids.size(), 2); |
| 1283 | EXPECT_EQ(e.content.user_ids[0], "@alice:example.com"); |
| 1284 | EXPECT_EQ(e.content.user_ids[1], "@bob:example.com"); |
| 1285 | |
| 1286 | ASSERT_TRUE( |
| 1287 | std::holds_alternative<mtx::events::EphemeralEvent<mtx::events::ephemeral::Receipt>>( |
| 1288 | ephemeral.events[1])); |
| 1289 | auto read = |
| 1290 | std::get<mtx::events::EphemeralEvent<mtx::events::ephemeral::Receipt>>(ephemeral.events[1]); |
| 1291 | |
| 1292 | using mtx::events::ephemeral::Receipt; |
| 1293 | EXPECT_EQ(read.content.receipts.size(), 5); |
| 1294 | EXPECT_EQ(read.content.receipts["$149339947230ohuCC:krtdex.com"][Receipt::Read].users.size(), |
| 1295 | 1); |
| 1296 | EXPECT_EQ( |
| 1297 | read.content.receipts["$14935874261161012PaoJD:matrix.org"][Receipt::Read].users.size(), 7); |
| 1298 | EXPECT_EQ(read.content.receipts["$14935874261161012PaoJD:matrix.org"][Receipt::Read] |
| 1299 | .users["@matthew:matrix.org"] |
| 1300 | .ts, |
| 1301 | 1510440324233); |
| 1302 | } |
| 1303 | |
| 1304 | TEST(Responses, Empty) |
| 1305 | { |
| 1306 | json data = R"({})"_json; |
| 1307 | |
| 1308 | Empty e = data.get<Empty>(); |
| 1309 | (void)e; |
| 1310 | } |
| 1311 | |
| 1312 | TEST(Responses, Media) |
| 1313 | { |
| 1314 | json data = R"({ |
| 1315 | "content_uri": "mxc://example.com/AQwafuaFswefuhsfAFAgsw" |
| 1316 | })"_json; |
| 1317 | |
| 1318 | ContentURI res = data.get<ContentURI>(); |
| 1319 | EXPECT_EQ(res.content_uri, "mxc://example.com/AQwafuaFswefuhsfAFAgsw"); |
| 1320 | } |
| 1321 | |
| 1322 | TEST(Responses, UploadKeys) |
| 1323 | { |
| 1324 | json data = R"({ |
| 1325 | "one_time_key_counts": { |
| 1326 | "curve25519": 10, |
| 1327 | "signed_curve25519": 20 |
| 1328 | } |
| 1329 | })"_json; |
| 1330 | |
| 1331 | UploadKeys res = data.get<UploadKeys>(); |
| 1332 | |
| 1333 | EXPECT_EQ(res.one_time_key_counts.size(), 2); |
| 1334 | EXPECT_EQ(res.one_time_key_counts["curve25519"], 10); |
| 1335 | EXPECT_EQ(res.one_time_key_counts["signed_curve25519"], 20); |
| 1336 | } |
| 1337 | |
| 1338 | TEST(Responses, QueryKeys) |
| 1339 | { |
| 1340 | json data = R"({ |
| 1341 | "failures": { |
| 1342 | "noidea": { "what": 0 }, |
| 1343 | "toput": { "here": 1 } |
| 1344 | }, |
| 1345 | "master_keys": { |
| 1346 | "@alice:example.org": { |
| 1347 | "user_id": "@alice:example.org", |
| 1348 | "usage": ["master_keys"], |
| 1349 | "keys": { |
| 1350 | "ed25519:base64+self+signing+public+key": "base64+self+signing+public+key" |
| 1351 | }, |
| 1352 | "signatures": { |
| 1353 | "@alice:example.org": { |
| 1354 | "ed25519:base64+device+id": "signature+of+master+key" |
| 1355 | } |
| 1356 | } |
| 1357 | } |
| 1358 | }, |
| 1359 | "user_signing_keys": { |
| 1360 | "@alice:example.org": { |
| 1361 | "user_id": "@alice:example.org", |
| 1362 | "usage": ["user_signing"], |
| 1363 | "keys": { |
| 1364 | "ed25519:base64+user+signing+public+key": "base64+user+signing+public+key" |
| 1365 | }, |
| 1366 | "signatures": { |
| 1367 | "@alice:example.org": { |
| 1368 | "ed25519:base64+master+public+key": "signature+of+user+signing+key" |
| 1369 | } |
| 1370 | } |
| 1371 | } |
| 1372 | }, |
| 1373 | "self_signing_keys": { |
| 1374 | "@alice:example.org": { |
| 1375 | "user_id": "@alice:example.org", |
| 1376 | "usage": ["self_signing"], |
| 1377 | "keys": { |
| 1378 | "ed25519:base64+self+signing+public+key": "base64+self+signing+public+key" |
| 1379 | }, |
| 1380 | "signatures": { |
| 1381 | "@alice:example.org": { |
| 1382 | "ed25519:base64+master+public+key": "signature+of+self+signing+key" |
| 1383 | } |
| 1384 | } |
| 1385 | } |
| 1386 | }, |
| 1387 | "device_keys": { |
| 1388 | "@alice:example.com": { |
| 1389 | "JLAFKJWSCS": { |
| 1390 | "user_id": "@alice:example.com", |
| 1391 | "device_id": "JLAFKJWSCS", |
| 1392 | "algorithms": [ "m.olm.v1.curve25519-aes-sha2", "m.megolm.v1.aes-sha2" ], |
| 1393 | "keys": { |
| 1394 | "curve25519:JLAFKJWSCS": "3C5BFWi2Y8MaVvjM8M22DBmh24PmgR0nPvJOIArzgyI", |
| 1395 | "ed25519:JLAFKJWSCS": "lEuiRJBit0IG6nUf5pUzWTUEsRVVe/HJkoKuEww9ULI" |
| 1396 | }, |
| 1397 | "signatures": { |
| 1398 | "@alice:example.com": { |
| 1399 | "ed25519:JLAFKJWSCS": "dSO80A01XiigH3uBiDVx/EjzaoycHcjq9lfQX0uWsqxl2giMIiSPR8a4d291W1ihKJL/a+myXS367WT6NAIcBA" |
| 1400 | } |
| 1401 | }, |
| 1402 | "unsigned": { |
| 1403 | "device_display_name": "Alice's mobile phone" |
| 1404 | } |
| 1405 | } |
| 1406 | } |
| 1407 | } |
| 1408 | })"_json; |
| 1409 | |
| 1410 | QueryKeys res = data.get<QueryKeys>(); |
| 1411 | |
| 1412 | EXPECT_EQ(res.failures.size(), 2); |
| 1413 | EXPECT_EQ(res.device_keys.size(), 1); |
| 1414 | |
| 1415 | auto device_keys = res.device_keys["@alice:example.com"]["JLAFKJWSCS"]; |
| 1416 | |
| 1417 | EXPECT_EQ(device_keys.user_id, "@alice:example.com"); |
| 1418 | EXPECT_EQ(device_keys.device_id, "JLAFKJWSCS"); |
| 1419 | EXPECT_EQ(device_keys.algorithms[0], "m.olm.v1.curve25519-aes-sha2"); |
| 1420 | EXPECT_EQ(device_keys.algorithms[1], "m.megolm.v1.aes-sha2"); |
| 1421 | EXPECT_EQ(device_keys.keys.size(), 2); |
| 1422 | EXPECT_EQ(device_keys.keys["curve25519:JLAFKJWSCS"], |
| 1423 | "3C5BFWi2Y8MaVvjM8M22DBmh24PmgR0nPvJOIArzgyI"); |
| 1424 | EXPECT_EQ(device_keys.keys["ed25519:JLAFKJWSCS"], |
| 1425 | "lEuiRJBit0IG6nUf5pUzWTUEsRVVe/HJkoKuEww9ULI"); |
| 1426 | EXPECT_EQ( |
| 1427 | device_keys.signatures["@alice:example.com"]["ed25519:JLAFKJWSCS"], |
| 1428 | "dSO80A01XiigH3uBiDVx/EjzaoycHcjq9lfQX0uWsqxl2giMIiSPR8a4d291W1ihKJL/a+myXS367WT6NAIcBA"); |
| 1429 | EXPECT_EQ(device_keys.unsigned_info.device_display_name, "Alice's mobile phone"); |
| 1430 | |
| 1431 | auto master_keys = res.master_keys["@alice:example.org"]; |
| 1432 | auto self_signing_keys = res.self_signing_keys["@alice:example.org"]; |
| 1433 | auto user_signing_keys = res.user_signing_keys["@alice:example.org"]; |
| 1434 | |
| 1435 | EXPECT_EQ(master_keys.keys.size(), 1); |
| 1436 | EXPECT_EQ(self_signing_keys.keys.size(), 1); |
| 1437 | EXPECT_EQ(user_signing_keys.keys.size(), 1); |
| 1438 | EXPECT_EQ(master_keys.signatures["@alice:example.org"].size(), 1); |
| 1439 | EXPECT_EQ(self_signing_keys.signatures["@alice:example.org"].size(), 1); |
| 1440 | EXPECT_EQ(user_signing_keys.signatures["@alice:example.org"].size(), 1); |
| 1441 | |
| 1442 | json data2 = R"({})"_json; |
| 1443 | QueryKeys res2 = data2.get<QueryKeys>(); |
| 1444 | EXPECT_TRUE(res2.failures.empty()); |
| 1445 | EXPECT_TRUE(res2.device_keys.empty()); |
| 1446 | EXPECT_TRUE(res2.user_signing_keys.empty()); |
| 1447 | EXPECT_TRUE(res2.self_signing_keys.empty()); |
| 1448 | EXPECT_TRUE(res2.master_keys.empty()); |
| 1449 | } |
| 1450 | |
| 1451 | TEST(Crypto, KeyChanges) |
| 1452 | { |
| 1453 | json data = R"({ |
| 1454 | "changed": [ |
| 1455 | "@alice:example.com", |
| 1456 | "@bob:example.org" |
| 1457 | ], |
| 1458 | "left": [ |
| 1459 | "@clara:example.com", |
| 1460 | "@doug:example.org" |
| 1461 | ] |
| 1462 | })"_json; |
| 1463 | |
| 1464 | KeyChanges res = data.get<KeyChanges>(); |
| 1465 | |
| 1466 | EXPECT_EQ(res.changed.size(), 2); |
| 1467 | EXPECT_EQ(res.changed[0], "@alice:example.com"); |
| 1468 | EXPECT_EQ(res.changed[1], "@bob:example.org"); |
| 1469 | |
| 1470 | EXPECT_EQ(res.left.size(), 2); |
| 1471 | EXPECT_EQ(res.left[0], "@clara:example.com"); |
| 1472 | EXPECT_EQ(res.left[1], "@doug:example.org"); |
| 1473 | } |
| 1474 | |
| 1475 | TEST(Crypto, ClaimKeys) |
| 1476 | { |
| 1477 | json data = R"({ |
| 1478 | "failures": {}, |
| 1479 | "one_time_keys": { |
| 1480 | "@alice:example.com": { |
| 1481 | "JLAFKJWSCS": { |
| 1482 | "signed_curve25519:AAAAHg": { |
| 1483 | "key": "zKbLg+NrIjpnagy+pIY6uPL4ZwEG2v+8F9lmgsnlZzs", |
| 1484 | "signatures": { |
| 1485 | "@alice:example.com": { |
| 1486 | "ed25519:JLAFKJWSCS": "FLWxXqGbwrb8SM3Y795eB6OA8bwBcoMZFXBqnTn58AYWZSqiD45tlBVcDa2L7RwdKXebW/VzDlnfVJ+9jok1Bw" |
| 1487 | } |
| 1488 | } |
| 1489 | } |
| 1490 | } |
| 1491 | } |
| 1492 | } |
| 1493 | })"_json; |
| 1494 | |
| 1495 | ClaimKeys res = data.get<ClaimKeys>(); |
| 1496 | EXPECT_EQ(res.failures.size(), 0); |
| 1497 | EXPECT_EQ(res.one_time_keys.size(), 1); |
| 1498 | |
| 1499 | auto device = res.one_time_keys["@alice:example.com"]["JLAFKJWSCS"]; |
| 1500 | EXPECT_EQ(device["signed_curve25519:AAAAHg"]["key"], |
| 1501 | "zKbLg+NrIjpnagy+pIY6uPL4ZwEG2v+8F9lmgsnlZzs"); |
| 1502 | } |
| 1503 | |
| 1504 | TEST(Responses, Notifications) |
| 1505 | { |
| 1506 | json data = R"({ |
| 1507 | "next_token": "abcdef", |
| 1508 | "notifications": [{ |
| 1509 | "actions": [ |
| 1510 | "notify" |
| 1511 | ], |
| 1512 | "profile_tag": null, |
| 1513 | "read": true, |
| 1514 | "room_id": "!abcdefg:example.com", |
| 1515 | "ts": 1475508881945, |
| 1516 | "event": { |
| 1517 | "sender": "@alice:example.com", |
| 1518 | "type": "m.room.message", |
| 1519 | "age": 124524, |
| 1520 | "txn_id": "1234", |
| 1521 | "content": { |
| 1522 | "body": "I am a fish", |
| 1523 | "msgtype": "m.text" |
| 1524 | }, |
| 1525 | "origin_server_ts": 1417731086797, |
| 1526 | "event_id": "$74686972643033:example.com" |
| 1527 | } |
| 1528 | }] |
| 1529 | })"_json; |
| 1530 | |
| 1531 | mtx::responses::Notifications notif = data.get<mtx::responses::Notifications>(); |
| 1532 | |
| 1533 | // EXPECT_EQ(notif.next_token, "abcdef"); |
| 1534 | EXPECT_EQ(notif.notifications.size(), 1); |
| 1535 | EXPECT_EQ(notif.notifications.at(0).profile_tag, ""); |
| 1536 | EXPECT_EQ(notif.notifications.at(0).read, true); |
| 1537 | EXPECT_EQ(notif.notifications.at(0).ts, 1475508881945L); |
| 1538 | EXPECT_EQ(notif.notifications.at(0).room_id, "!abcdefg:example.com"); |
| 1539 | |
| 1540 | using TextEvent = mtx::events::RoomEvent<msg::Text>; |
| 1541 | auto event = std::get<TextEvent>(notif.notifications.at(0).event); |
| 1542 | |
| 1543 | EXPECT_EQ(event.content.body, "I am a fish"); |
| 1544 | EXPECT_EQ(event.sender, "@alice:example.com"); |
| 1545 | } |
| 1546 | |
| 1547 | TEST(Responses, Userinteractive) |
| 1548 | { |
| 1549 | json data = |
| 1550 | R"( |
| 1551 | { |
| 1552 | "completed": [ "example.type.foo" ], |
| 1553 | "session": "YQVPFRiztSYtmsjLNQmsxTCg", |
| 1554 | "flows": [ |
| 1555 | { |
| 1556 | "stages": [ |
| 1557 | "m.login.recaptcha", |
| 1558 | "m.login.terms", |
| 1559 | "m.login.dummy" |
| 1560 | ] |
| 1561 | }, |
| 1562 | { |
| 1563 | "stages": [ |
| 1564 | "m.login.recaptcha", |
| 1565 | "m.login.terms", |
| 1566 | "m.login.email.identity" |
| 1567 | ] |
| 1568 | } |
| 1569 | ], |
| 1570 | "params": { |
| 1571 | "m.login.recaptcha": { |
| 1572 | "public_key": "6LcgI54UAAAAABGdGmruw 6DdOocFpYVdjYBRe4zb" |
| 1573 | }, |
| 1574 | "m.login.terms": { |
| 1575 | "policies": { |
| 1576 | "privacy_policy": { |
| 1577 | "version": "1.0", |
| 1578 | "en": { |
| 1579 | "name": "Terms and Conditions", |
| 1580 | "url": "https://matrix-client.matrix.org/_matrix/consent?v=1.0" |
| 1581 | } |
| 1582 | } |
| 1583 | } |
| 1584 | } |
| 1585 | } |
| 1586 | })"_json; |
| 1587 | mtx::user_interactive::Unauthorized unauthorized = |
| 1588 | data.get<mtx::user_interactive::Unauthorized>(); |
| 1589 | |
| 1590 | EXPECT_EQ(unauthorized.completed[0], "example.type.foo"); |
| 1591 | EXPECT_EQ(unauthorized.session, "YQVPFRiztSYtmsjLNQmsxTCg"); |
| 1592 | EXPECT_EQ(unauthorized.flows.size(), 2); |
| 1593 | EXPECT_EQ(unauthorized.flows[0].stages[0], "m.login.recaptcha"); |
| 1594 | EXPECT_EQ(unauthorized.flows[0].stages[1], "m.login.terms"); |
| 1595 | EXPECT_EQ(unauthorized.flows[0].stages[2], "m.login.dummy"); |
| 1596 | EXPECT_EQ(unauthorized.flows[1].stages[0], "m.login.recaptcha"); |
| 1597 | EXPECT_EQ(unauthorized.flows[1].stages[1], "m.login.terms"); |
| 1598 | EXPECT_EQ(unauthorized.flows[1].stages[2], "m.login.email.identity"); |
| 1599 | |
| 1600 | EXPECT_EQ(std::get<mtx::user_interactive::TermsParams>( |
| 1601 | unauthorized.params[std::string{mtx::user_interactive::auth_types::terms}]) |
| 1602 | .policies.size(), |
| 1603 | 1); |
| 1604 | EXPECT_EQ(std::get<mtx::user_interactive::TermsParams>( |
| 1605 | unauthorized.params[std::string{mtx::user_interactive::auth_types::terms}]) |
| 1606 | .policies["privacy_policy"] |
| 1607 | .version, |
| 1608 | "1.0"); |
| 1609 | EXPECT_EQ(std::get<mtx::user_interactive::TermsParams>( |
| 1610 | unauthorized.params[std::string{mtx::user_interactive::auth_types::terms}]) |
| 1611 | .policies["privacy_policy"] |
| 1612 | .langToPolicy["en"] |
| 1613 | .name, |
| 1614 | "Terms and Conditions"); |
| 1615 | |
| 1616 | json data2 = R"( |
| 1617 | { |
| 1618 | "session": "CFNYzCbLYyGTpURjdmkIXMHc", |
| 1619 | "flows": [ |
| 1620 | { |
| 1621 | "stages": [ |
| 1622 | "m.login.password" |
| 1623 | ] |
| 1624 | } |
| 1625 | ], |
| 1626 | "params": {} |
| 1627 | })"_json; |
| 1628 | |
| 1629 | unauthorized = data2.get<mtx::user_interactive::Unauthorized>(); |
| 1630 | EXPECT_EQ(unauthorized.flows[0].stages[0], mtx::user_interactive::auth_types::password); |
| 1631 | } |
| 1632 | |
| 1633 | TEST(Responses, TurnServer) |
| 1634 | { |
| 1635 | json data = R"({ |
| 1636 | "username": "1443779631:@user:example.com", |
| 1637 | "password": "JlKfBy1QwLrO20385QyAtEyIv0=", |
| 1638 | "uris": [ |
| 1639 | "turn:turn.example.com:3478?transport=udp", |
| 1640 | "turn:10.20.30.40:3478?transport=tcp", |
| 1641 | "turns:10.20.30.40:443?transport=tcp" |
| 1642 | ], |
| 1643 | "ttl": 86400 |
| 1644 | })"_json; |
| 1645 | |
| 1646 | TurnServer turnServer = data.get<TurnServer>(); |
| 1647 | EXPECT_EQ(turnServer.username, "1443779631:@user:example.com"); |
| 1648 | EXPECT_EQ(turnServer.password, "JlKfBy1QwLrO20385QyAtEyIv0="); |
| 1649 | EXPECT_EQ(turnServer.uris[0], "turn:turn.example.com:3478?transport=udp"); |
| 1650 | EXPECT_EQ(turnServer.uris[1], "turn:10.20.30.40:3478?transport=tcp"); |
| 1651 | EXPECT_EQ(turnServer.uris[2], "turns:10.20.30.40:443?transport=tcp"); |
| 1652 | EXPECT_EQ(turnServer.ttl, 86400); |
| 1653 | } |
| 1654 | |
| 1655 | TEST(Responses, PublicRoomVisibility) |
| 1656 | { |
| 1657 | json data = {{"visibility", "public"}}; |
| 1658 | mtx::responses::PublicRoomVisibility roomVisibility = |
| 1659 | data.get<mtx::responses::PublicRoomVisibility>(); |
| 1660 | EXPECT_EQ(roomVisibility.visibility, mtx::common::RoomVisibility::Public); |
| 1661 | |
| 1662 | data = {{"visibility", "private"}}; |
| 1663 | roomVisibility = data.get<mtx::responses::PublicRoomVisibility>(); |
| 1664 | EXPECT_EQ(roomVisibility.visibility, mtx::common::RoomVisibility::Private); |
| 1665 | } |
| 1666 | |
| 1667 | TEST(Responses, PublicRooms) |
| 1668 | { |
| 1669 | json data = R"({ |
| 1670 | "chunk": [ |
| 1671 | { |
| 1672 | "aliases": [ |
| 1673 | "#murrays:cheese.bar" |
| 1674 | ], |
| 1675 | "avatar_url": "mxc://bleeker.street/CHEDDARandBRIE", |
| 1676 | "guest_can_join": false, |
| 1677 | "name": "CHEESE", |
| 1678 | "num_joined_members": 37, |
| 1679 | "room_id": "!ol19s:bleecker.street", |
| 1680 | "topic": "Tasty tasty cheese", |
| 1681 | "world_readable": true |
| 1682 | } |
| 1683 | ], |
| 1684 | "next_batch": "p190q", |
| 1685 | "prev_batch": "p1902", |
| 1686 | "total_room_count_estimate": 115 |
| 1687 | })"_json; |
| 1688 | |
| 1689 | PublicRooms publicRooms = data.get<PublicRooms>(); |
| 1690 | EXPECT_EQ(publicRooms.chunk.size(), 1); |
| 1691 | auto &chunk = publicRooms.chunk[0]; |
| 1692 | EXPECT_EQ(chunk.aliases.size(), 1); |
| 1693 | EXPECT_EQ(chunk.aliases[0], "#murrays:cheese.bar"); |
| 1694 | EXPECT_EQ(chunk.avatar_url, "mxc://bleeker.street/CHEDDARandBRIE"); |
| 1695 | EXPECT_EQ(chunk.guest_can_join, false); |
| 1696 | EXPECT_EQ(chunk.name, "CHEESE"); |
| 1697 | EXPECT_EQ(chunk.num_joined_members, 37); |
| 1698 | EXPECT_EQ(chunk.room_id, "!ol19s:bleecker.street"); |
| 1699 | EXPECT_EQ(chunk.topic, "Tasty tasty cheese"); |
| 1700 | EXPECT_EQ(publicRooms.next_batch, "p190q"); |
| 1701 | EXPECT_EQ(publicRooms.prev_batch, "p1902"); |
| 1702 | EXPECT_EQ(publicRooms.total_room_count_estimate, 115); |
| 1703 | } |
| 1704 | |
| 1705 | TEST(Response, Users) |
| 1706 | { |
| 1707 | json data = R"({ |
| 1708 | "limited": false, |
| 1709 | "results": [ |
| 1710 | { |
| 1711 | "avatar_url": "mxc://bar.com/foo", |
| 1712 | "display_name": "Foo", |
| 1713 | "user_id": "@foo:bar.com" |
| 1714 | } |
| 1715 | ] |
| 1716 | })"_json; |
| 1717 | Users users = data.get<Users>(); |
| 1718 | EXPECT_EQ(users.results.size(), 1); |
| 1719 | EXPECT_EQ(users.results[0].avatar_url, "mxc://bar.com/foo"); |
| 1720 | EXPECT_EQ(users.results[0].display_name, "Foo"); |
| 1721 | EXPECT_EQ(users.results[0].user_id, "@foo:bar.com"); |
| 1722 | EXPECT_EQ(users.limited, false); |
| 1723 | } |
| 1724 | |
| 1725 | TEST(Response, PreviewURL) |
| 1726 | { |
| 1727 | // Tests with both actual numbers and strings containing them, as both may be returned |
| 1728 | json fullData = R"({ |
| 1729 | "og:title": "Example", |
| 1730 | "og:url": "https://example.org", |
| 1731 | "og:image:type": "image/png", |
| 1732 | "og:image:width": 12345, |
| 1733 | "og:image:height": 12345, |
| 1734 | "og:image:alt": "Alt text", |
| 1735 | "og:image": "mxc://example.org/abc", |
| 1736 | "matrix:image:size": 12345, |
| 1737 | "og:description": "Description", |
| 1738 | "og:site_name": "example.org" |
| 1739 | })"_json; |
| 1740 | |
| 1741 | URLPreview full = fullData.get<URLPreview>(); |
| 1742 | ASSERT_EQ(full.title, "Example"); |
| 1743 | ASSERT_EQ(full.url, "https://example.org"); |
| 1744 | ASSERT_EQ(full.image.type, "image/png"); |
| 1745 | ASSERT_EQ(full.image.width, 12345); |
| 1746 | ASSERT_EQ(full.image.height, 12345); |
| 1747 | ASSERT_EQ(full.image.alt, "Alt text"); |
| 1748 | ASSERT_EQ(full.image.url, "mxc://example.org/abc"); |
| 1749 | ASSERT_EQ(full.image.size, 12345); |
| 1750 | ASSERT_TRUE(full.site_name); |
| 1751 | ASSERT_TRUE(full.description); |
| 1752 | ASSERT_EQ(*full.description, "Description"); |
| 1753 | ASSERT_EQ(*full.site_name, "example.org"); |
| 1754 | |
| 1755 | json minData = R"({ |
| 1756 | "og:title": "Example", |
| 1757 | "og:url": "https://example.org", |
| 1758 | "og:image": "mxc://example.org/abc", |
| 1759 | "matrix:image:size": 12345 |
| 1760 | })"_json; |
| 1761 | URLPreview min = minData.get<URLPreview>(); |
| 1762 | ASSERT_EQ(min.title, "Example"); |
| 1763 | ASSERT_EQ(min.url, "https://example.org"); |
| 1764 | ASSERT_EQ(min.image.url, "mxc://example.org/abc"); |
| 1765 | ASSERT_EQ(min.image.size, 12345); |
| 1766 | ASSERT_FALSE(min.site_name); |
| 1767 | ASSERT_FALSE(min.description); |
| 1768 | } |