| 1 | #include <gtest/gtest.h> |
| 2 | |
| 3 | #include <nlohmann/json.hpp> |
| 4 | |
| 5 | #include <mtx/requests.hpp> |
| 6 | #include <mtx/user_interactive.hpp> |
| 7 | |
| 8 | using json = nlohmann::json; |
| 9 | using namespace mtx::requests; |
| 10 | |
| 11 | TEST(Requests, Login) |
| 12 | { |
| 13 | Login t1, t2, t3; |
| 14 | |
| 15 | t1.identifier = login_identifier::User{"@alice:matrix.org"}; |
| 16 | t1.password = "secret"; |
| 17 | t1.initial_device_display_name = "Mobile"; |
| 18 | |
| 19 | json j = t1; |
| 20 | ASSERT_EQ(j.dump(), |
| 21 | "{\"identifier\":{\"type\":\"m.id.user\",\"user\":\"@alice:matrix.org\"}," |
| 22 | "\"initial_device_display_name\":\"Mobile\",\"password\":\"secret\",\"type\":" |
| 23 | "\"m.login.password\"}"); |
| 24 | |
| 25 | t2.identifier = login_identifier::User{"@bob:matrix.org"}; |
| 26 | t2.password = "secret2"; |
| 27 | |
| 28 | j = t2; |
| 29 | ASSERT_EQ(j.dump(), |
| 30 | "{\"identifier\":{\"type\":\"m.id.user\",\"user\":\"@bob:matrix.org\"}," |
| 31 | "\"password\":\"secret2\",\"type\":\"m.login.password\"}"); |
| 32 | |
| 33 | t3.identifier = login_identifier::User{"@carl:matrix.org"}; |
| 34 | t3.password = "secret3"; |
| 35 | t3.device_id = "ZSDF2RG"; |
| 36 | |
| 37 | j = t3; |
| 38 | ASSERT_EQ(j.dump(), |
| 39 | "{\"device_id\":\"ZSDF2RG\",\"identifier\":{\"type\":\"m.id.user\",\"user\":\"" |
| 40 | "@carl:matrix.org\"},\"password\":\"secret3\",\"type\":\"m.login." |
| 41 | "password\"}"); |
| 42 | } |
| 43 | |
| 44 | TEST(Requests, Typing) |
| 45 | { |
| 46 | TypingNotification t1, t2; |
| 47 | |
| 48 | t1.timeout = 4000; |
| 49 | |
| 50 | json j = t1; |
| 51 | ASSERT_EQ(j.dump(), "{\"timeout\":4000,\"typing\":false}"); |
| 52 | |
| 53 | t2.typing = true; |
| 54 | t2.timeout = 4242; |
| 55 | |
| 56 | j = t2; |
| 57 | ASSERT_EQ(j.dump(), "{\"timeout\":4242,\"typing\":true}"); |
| 58 | } |
| 59 | |
| 60 | TEST(Requests, UploadKeys) |
| 61 | { |
| 62 | UploadKeys r1, r2, r3; |
| 63 | |
| 64 | json j = r1; |
| 65 | ASSERT_EQ(j.dump(), "{}"); |
| 66 | |
| 67 | r2.device_keys.user_id = "@alice:example.com"; |
| 68 | r2.device_keys.device_id = "JLAFKJWSCS"; |
| 69 | r2.device_keys.keys.emplace("curve25519:JLAFKJWSCS", |
| 70 | "3C5BFWi2Y8MaVvjM8M22DBmh24PmgR0nPvJOIArzgyI"); |
| 71 | std::map<std::string, std::string> tmp = { |
| 72 | {"ed25519:JLAFKJWSCS", |
| 73 | "dSO80A01XiigH3uBiDVx/EjzaoycHcjq9lfQX0uWsqxl2giMIiSPR8a4d291W1ihKJL/" |
| 74 | "a+myXS367WT6NAIcBA"}}; |
| 75 | r2.device_keys.signatures.emplace("@alice:example.com", tmp); |
| 76 | |
| 77 | // Only device_keys are present |
| 78 | j = r2; |
| 79 | ASSERT_EQ(j.dump(), |
| 80 | "{\"device_keys\":{\"algorithms\":[\"m.olm.v1.curve25519-aes-sha2\",\"m.megolm.v1." |
| 81 | "aes-sha2\"],\"device_id\":\"JLAFKJWSCS\",\"keys\":{\"curve25519:JLAFKJWSCS\":" |
| 82 | "\"3C5BFWi2Y8MaVvjM8M22DBmh24PmgR0nPvJOIArzgyI\"},\"signatures\":{\"@alice:" |
| 83 | "example.com\":{\"ed25519:JLAFKJWSCS\":\"dSO80A01XiigH3uBiDVx/" |
| 84 | "EjzaoycHcjq9lfQX0uWsqxl2giMIiSPR8a4d291W1ihKJL/" |
| 85 | "a+myXS367WT6NAIcBA\"}},\"user_id\":\"@alice:example.com\"}}"); |
| 86 | |
| 87 | mtx::requests::SignedOneTimeKey k1; |
| 88 | k1.key = "zKbLg+NrIjpnagy+pIY6uPL4ZwEG2v+8F9lmgsnlZzs"; |
| 89 | k1.signatures = {{"@alice:example.com", |
| 90 | {{"ed25519:JLAFKJWSCS", |
| 91 | "IQeCEPb9HFk217cU9kw9EOiusC6kMIkoIRnbnfOh5Oc63S1ghgyjShBGpu34blQomoalCy" |
| 92 | "XWyhaaT3MrLZYQAA"}}}}; |
| 93 | |
| 94 | mtx::requests::SignedOneTimeKey k2; |
| 95 | k2.key = "j3fR3HemM16M7CWhoI4Sk5ZsdmdfQHsKL1xuSft6MSw"; |
| 96 | k2.signatures["@alice:example.com"]["ed25519:JLAFKJWSCS"] = |
| 97 | "FLWxXqGbwrb8SM3Y795eB6OA8bwBcoMZFXBqnTn58AYWZSqiD45tlBVcDa2L7RwdKXebW/VzDlnfVJ+9jok1Bw"; |
| 98 | |
| 99 | r3.one_time_keys.emplace("curve25519:AAAAAQ", "/qyvZvwjiTxGdGU0RCguDCLeR+nmsb3FfNG3/Ve4vU8"); |
| 100 | r3.one_time_keys.emplace("signed_curve25519:AAAAHg", k1); |
| 101 | r3.one_time_keys.emplace("signed_curve25519:AAAAHQ", k2); |
| 102 | |
| 103 | j = r3; |
| 104 | ASSERT_EQ( |
| 105 | j.dump(), |
| 106 | "{\"one_time_keys\":{\"curve25519:AAAAAQ\":\"/qyvZvwjiTxGdGU0RCguDCLeR+nmsb3FfNG3/" |
| 107 | "Ve4vU8\",\"signed_curve25519:AAAAHQ\":{\"key\":" |
| 108 | "\"j3fR3HemM16M7CWhoI4Sk5ZsdmdfQHsKL1xuSft6MSw\",\"signatures\":{\"@alice:example.com\":{" |
| 109 | "\"ed25519:JLAFKJWSCS\":" |
| 110 | "\"FLWxXqGbwrb8SM3Y795eB6OA8bwBcoMZFXBqnTn58AYWZSqiD45tlBVcDa2L7RwdKXebW/" |
| 111 | "VzDlnfVJ+9jok1Bw\"}}},\"signed_curve25519:AAAAHg\":{\"key\":\"zKbLg+NrIjpnagy+" |
| 112 | "pIY6uPL4ZwEG2v+8F9lmgsnlZzs\",\"signatures\":{\"@alice:example.com\":{\"ed25519:" |
| 113 | "JLAFKJWSCS\":" |
| 114 | "\"IQeCEPb9HFk217cU9kw9EOiusC6kMIkoIRnbnfOh5Oc63S1ghgyjShBGpu34blQomoalCyXWyhaaT3MrLZYQAA" |
| 115 | "\"}}}}}"); |
| 116 | |
| 117 | k1.fallback = true; |
| 118 | r3.fallback_keys.emplace("signed_curve25519:AAAAAA", k1); |
| 119 | j = r3; |
| 120 | |
| 121 | ASSERT_EQ( |
| 122 | j.dump(), |
| 123 | "{\"fallback_keys\":{\"signed_curve25519:AAAAAA\":{\"fallback\":true,\"key\":\"zKbLg+" |
| 124 | "NrIjpnagy+pIY6uPL4ZwEG2v+8F9lmgsnlZzs\",\"signatures\":{\"@alice:example.com\":{\"ed25519:" |
| 125 | "JLAFKJWSCS\":" |
| 126 | "\"IQeCEPb9HFk217cU9kw9EOiusC6kMIkoIRnbnfOh5Oc63S1ghgyjShBGpu34blQomoalCyXWyhaaT3MrLZYQAA\"}}" |
| 127 | "}},\"one_time_keys\":{\"curve25519:AAAAAQ\":\"/qyvZvwjiTxGdGU0RCguDCLeR+nmsb3FfNG3/" |
| 128 | "Ve4vU8\",\"signed_curve25519:AAAAHQ\":{\"key\":" |
| 129 | "\"j3fR3HemM16M7CWhoI4Sk5ZsdmdfQHsKL1xuSft6MSw\",\"signatures\":{\"@alice:example.com\":{" |
| 130 | "\"ed25519:JLAFKJWSCS\":" |
| 131 | "\"FLWxXqGbwrb8SM3Y795eB6OA8bwBcoMZFXBqnTn58AYWZSqiD45tlBVcDa2L7RwdKXebW/" |
| 132 | "VzDlnfVJ+9jok1Bw\"}}},\"signed_curve25519:AAAAHg\":{\"key\":\"zKbLg+NrIjpnagy+" |
| 133 | "pIY6uPL4ZwEG2v+8F9lmgsnlZzs\",\"signatures\":{\"@alice:example.com\":{\"ed25519:" |
| 134 | "JLAFKJWSCS\":" |
| 135 | "\"IQeCEPb9HFk217cU9kw9EOiusC6kMIkoIRnbnfOh5Oc63S1ghgyjShBGpu34blQomoalCyXWyhaaT3MrLZYQAA\"}}" |
| 136 | "}}}"); |
| 137 | } |
| 138 | |
| 139 | TEST(Requests, QueryKeys) |
| 140 | { |
| 141 | QueryKeys k1; |
| 142 | |
| 143 | std::vector<std::string> empty_vec; |
| 144 | |
| 145 | k1.device_keys.emplace("@alice:localhost", empty_vec); |
| 146 | k1.token = "this_is_a_token"; |
| 147 | |
| 148 | json j = k1; |
| 149 | ASSERT_EQ(j.dump(), |
| 150 | "{\"device_keys\":{\"@alice:localhost\":[]},\"timeout\":10000,\"token\":\"this_" |
| 151 | "is_a_token\"}"); |
| 152 | } |
| 153 | |
| 154 | TEST(Requests, ClaimKeys) |
| 155 | { |
| 156 | ClaimKeys k1; |
| 157 | |
| 158 | std::map<std::string, std::string> devices; |
| 159 | devices.emplace("JLAFKJWSCS", "curve25519"); |
| 160 | |
| 161 | k1.one_time_keys.emplace("@alice:localhost", devices); |
| 162 | |
| 163 | json j = k1; |
| 164 | ASSERT_EQ(j.dump(), |
| 165 | "{\"one_time_keys\":{\"@alice:localhost\":{\"JLAFKJWSCS\":\"curve25519\"}}," |
| 166 | "\"timeout\":10000}"); |
| 167 | } |
| 168 | |
| 169 | TEST(Requests, UserInteractiveAuth) |
| 170 | { |
| 171 | using namespace mtx::user_interactive; |
| 172 | // Auth pw{ |
| 173 | // .session = "<session ID>", |
| 174 | // .content = auth::Password{.password = "<password>", |
| 175 | // .identifier_type = auth::Password::IdType::UserId, |
| 176 | // .identifier_user = "<user_id or user localpart>"}, |
| 177 | //}; |
| 178 | Auth a; |
| 179 | a.session = "<session ID>"; |
| 180 | auth::Password pw_content; |
| 181 | pw_content.password = "<password>"; |
| 182 | pw_content.identifier_type = auth::Password::IdType::UserId; |
| 183 | pw_content.identifier_user = "<user_id or user localpart>"; |
| 184 | a.content = pw_content; |
| 185 | |
| 186 | EXPECT_EQ(nlohmann::json(a), R"({ |
| 187 | "type": "m.login.password", |
| 188 | "identifier": { |
| 189 | "type": "m.id.user", |
| 190 | "user": "<user_id or user localpart>" |
| 191 | }, |
| 192 | "password": "<password>", |
| 193 | "session": "<session ID>" |
| 194 | })"_json); |
| 195 | |
| 196 | pw_content.identifier_type = auth::Password::IdType::ThirdPartyId; |
| 197 | pw_content.identifier_user = ""; |
| 198 | pw_content.identifier_medium = "<The medium of the third party identifier.>"; |
| 199 | pw_content.identifier_address = "<The third party address of the user>"; |
| 200 | a.content = pw_content; |
| 201 | |
| 202 | EXPECT_EQ(nlohmann::json(a), R"({ |
| 203 | "type": "m.login.password", |
| 204 | "identifier": { |
| 205 | "type": "m.id.thirdparty", |
| 206 | "medium": "<The medium of the third party identifier.>", |
| 207 | "address": "<The third party address of the user>" |
| 208 | }, |
| 209 | "password": "<password>", |
| 210 | "session": "<session ID>" |
| 211 | })"_json); |
| 212 | |
| 213 | a.content = auth::ReCaptcha{"<captcha response>"}; |
| 214 | |
| 215 | EXPECT_EQ(nlohmann::json(a), R"({ |
| 216 | "type": "m.login.recaptcha", |
| 217 | "response": "<captcha response>", |
| 218 | "session": "<session ID>" |
| 219 | })"_json); |
| 220 | |
| 221 | a.content = auth::Token{"<token>", "<client generated nonce>"}; |
| 222 | EXPECT_EQ(nlohmann::json(a), R"({ |
| 223 | "type": "m.login.token", |
| 224 | "token": "<token>", |
| 225 | "txn_id": "<client generated nonce>", |
| 226 | "session": "<session ID>" |
| 227 | })"_json); |
| 228 | |
| 229 | a.content = auth::OAuth2{}; |
| 230 | EXPECT_EQ(nlohmann::json(a), R"({ |
| 231 | "type": "m.login.oauth2", |
| 232 | "session": "<session ID>" |
| 233 | })"_json); |
| 234 | a.content = auth::Terms{}; |
| 235 | EXPECT_EQ(nlohmann::json(a), R"({ |
| 236 | "type": "m.login.terms", |
| 237 | "session": "<session ID>" |
| 238 | })"_json); |
| 239 | a.content = auth::Dummy{}; |
| 240 | EXPECT_EQ(nlohmann::json(a), R"({ |
| 241 | "type": "m.login.dummy", |
| 242 | "session": "<session ID>" |
| 243 | })"_json); |
| 244 | a.content = auth::Fallback{}; |
| 245 | EXPECT_EQ(nlohmann::json(a), R"({ |
| 246 | "session": "<session ID>" |
| 247 | })"_json); |
| 248 | |
| 249 | a.content = |
| 250 | auth::EmailIdentity{"<identity server session id>", |
| 251 | "<identity server client secret>", |
| 252 | "<url of identity server authed with, e.g. 'matrix.org:8090'>", |
| 253 | "<access token previously registered with the identity server>"}; |
| 254 | |
| 255 | EXPECT_EQ(nlohmann::json(a), R"({ |
| 256 | "type": "m.login.email.identity", |
| 257 | "threepid_creds": { |
| 258 | "sid": "<identity server session id>", |
| 259 | "client_secret": "<identity server client secret>", |
| 260 | "id_server": "<url of identity server authed with, e.g. 'matrix.org:8090'>", |
| 261 | "id_access_token": "<access token previously registered with the identity server>" |
| 262 | }, |
| 263 | "session": "<session ID>" |
| 264 | })"_json); |
| 265 | |
| 266 | a.content = auth::MSISDN{"<identity server session id>", |
| 267 | "<identity server client secret>", |
| 268 | "<url of identity server authed with, e.g. 'matrix.org:8090'>", |
| 269 | "<access token previously registered with the identity server>"}; |
| 270 | |
| 271 | EXPECT_EQ(nlohmann::json(a), R"({ |
| 272 | "type": "m.login.msisdn", |
| 273 | "threepid_creds": { |
| 274 | "sid": "<identity server session id>", |
| 275 | "client_secret": "<identity server client secret>", |
| 276 | "id_server": "<url of identity server authed with, e.g. 'matrix.org:8090'>", |
| 277 | "id_access_token": "<access token previously registered with the identity server>" |
| 278 | }, |
| 279 | "session": "<session ID>" |
| 280 | })"_json); |
| 281 | |
| 282 | a.content = |
| 283 | auth::MSISDN{"<identity server session id>", "<identity server client secret>", "", ""}; |
| 284 | |
| 285 | EXPECT_EQ(nlohmann::json(a), R"({ |
| 286 | "type": "m.login.msisdn", |
| 287 | "threepid_creds": { |
| 288 | "sid": "<identity server session id>", |
| 289 | "client_secret": "<identity server client secret>" |
| 290 | }, |
| 291 | "session": "<session ID>" |
| 292 | })"_json); |
| 293 | |
| 294 | a.content = auth::RegistrationToken{"<token>"}; |
| 295 | EXPECT_EQ(nlohmann::json(a), R"({ |
| 296 | "type": "m.login.registration_token", |
| 297 | "token": "<token>", |
| 298 | "session": "<session ID>" |
| 299 | })"_json); |
| 300 | } |
| 301 | |
| 302 | TEST(Requests, RoomVisibility) |
| 303 | { |
| 304 | mtx::requests::PublicRoomVisibility req; |
| 305 | req.visibility = mtx::common::RoomVisibility::Private; |
| 306 | json j = req; |
| 307 | EXPECT_EQ(j, R"({ |
| 308 | "visibility" : "private" |
| 309 | })"_json); |
| 310 | |
| 311 | req.visibility = mtx::common::RoomVisibility::Public; |
| 312 | j = req; |
| 313 | EXPECT_EQ(j, R"({ |
| 314 | "visibility" : "public" |
| 315 | })"_json); |
| 316 | } |
| 317 | |
| 318 | TEST(Requests, PublicRooms) |
| 319 | { |
| 320 | PublicRooms b1, b2, b3; |
| 321 | |
| 322 | b1.limit = 10; |
| 323 | b1.filter.generic_search_term = "foo"; |
| 324 | b1.include_all_networks = false; |
| 325 | b1.third_party_instance_id = "irc"; |
| 326 | |
| 327 | json j = b1; |
| 328 | |
| 329 | EXPECT_EQ(j, R"({ |
| 330 | "limit" : 10, |
| 331 | "filter" : { |
| 332 | "generic_search_term" : "foo" |
| 333 | }, |
| 334 | "include_all_networks" : false, |
| 335 | "third_party_instance_id" : "irc" |
| 336 | })"_json); |
| 337 | |
| 338 | // if third_party_instance_id is set, then the include_all_networks flag should |
| 339 | // default to false |
| 340 | b2.limit = 10; |
| 341 | b2.third_party_instance_id = "matrix"; |
| 342 | j = b2; |
| 343 | EXPECT_EQ(j, R"({ |
| 344 | "limit" : 10, |
| 345 | "include_all_networks" : false, |
| 346 | "third_party_instance_id" : "matrix" |
| 347 | })"_json); |
| 348 | |
| 349 | // if include_all_networks is true, then third_party_instance_id cannot be used. |
| 350 | // if it is somehow set, then we expect an exception to be thrown |
| 351 | b3.limit = 2; |
| 352 | b3.include_all_networks = true; |
| 353 | b3.third_party_instance_id = "irc"; |
| 354 | |
| 355 | EXPECT_THROW(json req = b3, std::invalid_argument); |
| 356 | } |