#include #include #include #include using json = nlohmann::json; using namespace mtx::requests; TEST(Requests, Login) { Login t1, t2, t3; t1.identifier = login_identifier::User{"@alice:matrix.org"}; t1.password = "secret"; t1.initial_device_display_name = "Mobile"; json j = t1; ASSERT_EQ(j.dump(), "{\"identifier\":{\"type\":\"m.id.user\",\"user\":\"@alice:matrix.org\"}," "\"initial_device_display_name\":\"Mobile\",\"password\":\"secret\",\"type\":" "\"m.login.password\"}"); t2.identifier = login_identifier::User{"@bob:matrix.org"}; t2.password = "secret2"; j = t2; ASSERT_EQ(j.dump(), "{\"identifier\":{\"type\":\"m.id.user\",\"user\":\"@bob:matrix.org\"}," "\"password\":\"secret2\",\"type\":\"m.login.password\"}"); t3.identifier = login_identifier::User{"@carl:matrix.org"}; t3.password = "secret3"; t3.device_id = "ZSDF2RG"; j = t3; ASSERT_EQ(j.dump(), "{\"device_id\":\"ZSDF2RG\",\"identifier\":{\"type\":\"m.id.user\",\"user\":\"" "@carl:matrix.org\"},\"password\":\"secret3\",\"type\":\"m.login." "password\"}"); } TEST(Requests, Typing) { TypingNotification t1, t2; t1.timeout = 4000; json j = t1; ASSERT_EQ(j.dump(), "{\"timeout\":4000,\"typing\":false}"); t2.typing = true; t2.timeout = 4242; j = t2; ASSERT_EQ(j.dump(), "{\"timeout\":4242,\"typing\":true}"); } TEST(Requests, UploadKeys) { UploadKeys r1, r2, r3; json j = r1; ASSERT_EQ(j.dump(), "{}"); r2.device_keys.user_id = "@alice:example.com"; r2.device_keys.device_id = "JLAFKJWSCS"; r2.device_keys.keys.emplace("curve25519:JLAFKJWSCS", "3C5BFWi2Y8MaVvjM8M22DBmh24PmgR0nPvJOIArzgyI"); std::map tmp = { {"ed25519:JLAFKJWSCS", "dSO80A01XiigH3uBiDVx/EjzaoycHcjq9lfQX0uWsqxl2giMIiSPR8a4d291W1ihKJL/" "a+myXS367WT6NAIcBA"}}; r2.device_keys.signatures.emplace("@alice:example.com", tmp); // Only device_keys are present j = r2; ASSERT_EQ(j.dump(), "{\"device_keys\":{\"algorithms\":[\"m.olm.v1.curve25519-aes-sha2\",\"m.megolm.v1." "aes-sha2\"],\"device_id\":\"JLAFKJWSCS\",\"keys\":{\"curve25519:JLAFKJWSCS\":" "\"3C5BFWi2Y8MaVvjM8M22DBmh24PmgR0nPvJOIArzgyI\"},\"signatures\":{\"@alice:" "example.com\":{\"ed25519:JLAFKJWSCS\":\"dSO80A01XiigH3uBiDVx/" "EjzaoycHcjq9lfQX0uWsqxl2giMIiSPR8a4d291W1ihKJL/" "a+myXS367WT6NAIcBA\"}},\"user_id\":\"@alice:example.com\"}}"); mtx::requests::SignedOneTimeKey k1; k1.key = "zKbLg+NrIjpnagy+pIY6uPL4ZwEG2v+8F9lmgsnlZzs"; k1.signatures = {{"@alice:example.com", {{"ed25519:JLAFKJWSCS", "IQeCEPb9HFk217cU9kw9EOiusC6kMIkoIRnbnfOh5Oc63S1ghgyjShBGpu34blQomoalCy" "XWyhaaT3MrLZYQAA"}}}}; mtx::requests::SignedOneTimeKey k2; k2.key = "j3fR3HemM16M7CWhoI4Sk5ZsdmdfQHsKL1xuSft6MSw"; k2.signatures["@alice:example.com"]["ed25519:JLAFKJWSCS"] = "FLWxXqGbwrb8SM3Y795eB6OA8bwBcoMZFXBqnTn58AYWZSqiD45tlBVcDa2L7RwdKXebW/VzDlnfVJ+9jok1Bw"; r3.one_time_keys.emplace("curve25519:AAAAAQ", "/qyvZvwjiTxGdGU0RCguDCLeR+nmsb3FfNG3/Ve4vU8"); r3.one_time_keys.emplace("signed_curve25519:AAAAHg", k1); r3.one_time_keys.emplace("signed_curve25519:AAAAHQ", k2); j = r3; ASSERT_EQ( j.dump(), "{\"one_time_keys\":{\"curve25519:AAAAAQ\":\"/qyvZvwjiTxGdGU0RCguDCLeR+nmsb3FfNG3/" "Ve4vU8\",\"signed_curve25519:AAAAHQ\":{\"key\":" "\"j3fR3HemM16M7CWhoI4Sk5ZsdmdfQHsKL1xuSft6MSw\",\"signatures\":{\"@alice:example.com\":{" "\"ed25519:JLAFKJWSCS\":" "\"FLWxXqGbwrb8SM3Y795eB6OA8bwBcoMZFXBqnTn58AYWZSqiD45tlBVcDa2L7RwdKXebW/" "VzDlnfVJ+9jok1Bw\"}}},\"signed_curve25519:AAAAHg\":{\"key\":\"zKbLg+NrIjpnagy+" "pIY6uPL4ZwEG2v+8F9lmgsnlZzs\",\"signatures\":{\"@alice:example.com\":{\"ed25519:" "JLAFKJWSCS\":" "\"IQeCEPb9HFk217cU9kw9EOiusC6kMIkoIRnbnfOh5Oc63S1ghgyjShBGpu34blQomoalCyXWyhaaT3MrLZYQAA" "\"}}}}}"); k1.fallback = true; r3.fallback_keys.emplace("signed_curve25519:AAAAAA", k1); j = r3; ASSERT_EQ( j.dump(), "{\"fallback_keys\":{\"signed_curve25519:AAAAAA\":{\"fallback\":true,\"key\":\"zKbLg+" "NrIjpnagy+pIY6uPL4ZwEG2v+8F9lmgsnlZzs\",\"signatures\":{\"@alice:example.com\":{\"ed25519:" "JLAFKJWSCS\":" "\"IQeCEPb9HFk217cU9kw9EOiusC6kMIkoIRnbnfOh5Oc63S1ghgyjShBGpu34blQomoalCyXWyhaaT3MrLZYQAA\"}}" "}},\"one_time_keys\":{\"curve25519:AAAAAQ\":\"/qyvZvwjiTxGdGU0RCguDCLeR+nmsb3FfNG3/" "Ve4vU8\",\"signed_curve25519:AAAAHQ\":{\"key\":" "\"j3fR3HemM16M7CWhoI4Sk5ZsdmdfQHsKL1xuSft6MSw\",\"signatures\":{\"@alice:example.com\":{" "\"ed25519:JLAFKJWSCS\":" "\"FLWxXqGbwrb8SM3Y795eB6OA8bwBcoMZFXBqnTn58AYWZSqiD45tlBVcDa2L7RwdKXebW/" "VzDlnfVJ+9jok1Bw\"}}},\"signed_curve25519:AAAAHg\":{\"key\":\"zKbLg+NrIjpnagy+" "pIY6uPL4ZwEG2v+8F9lmgsnlZzs\",\"signatures\":{\"@alice:example.com\":{\"ed25519:" "JLAFKJWSCS\":" "\"IQeCEPb9HFk217cU9kw9EOiusC6kMIkoIRnbnfOh5Oc63S1ghgyjShBGpu34blQomoalCyXWyhaaT3MrLZYQAA\"}}" "}}}"); } TEST(Requests, QueryKeys) { QueryKeys k1; std::vector empty_vec; k1.device_keys.emplace("@alice:localhost", empty_vec); k1.token = "this_is_a_token"; json j = k1; ASSERT_EQ(j.dump(), "{\"device_keys\":{\"@alice:localhost\":[]},\"timeout\":10000,\"token\":\"this_" "is_a_token\"}"); } TEST(Requests, ClaimKeys) { ClaimKeys k1; std::map devices; devices.emplace("JLAFKJWSCS", "curve25519"); k1.one_time_keys.emplace("@alice:localhost", devices); json j = k1; ASSERT_EQ(j.dump(), "{\"one_time_keys\":{\"@alice:localhost\":{\"JLAFKJWSCS\":\"curve25519\"}}," "\"timeout\":10000}"); } TEST(Requests, UserInteractiveAuth) { using namespace mtx::user_interactive; // Auth pw{ // .session = "", // .content = auth::Password{.password = "", // .identifier_type = auth::Password::IdType::UserId, // .identifier_user = ""}, //}; Auth a; a.session = ""; auth::Password pw_content; pw_content.password = ""; pw_content.identifier_type = auth::Password::IdType::UserId; pw_content.identifier_user = ""; a.content = pw_content; EXPECT_EQ(nlohmann::json(a), R"({ "type": "m.login.password", "identifier": { "type": "m.id.user", "user": "" }, "password": "", "session": "" })"_json); pw_content.identifier_type = auth::Password::IdType::ThirdPartyId; pw_content.identifier_user = ""; pw_content.identifier_medium = ""; pw_content.identifier_address = ""; a.content = pw_content; EXPECT_EQ(nlohmann::json(a), R"({ "type": "m.login.password", "identifier": { "type": "m.id.thirdparty", "medium": "", "address": "" }, "password": "", "session": "" })"_json); a.content = auth::ReCaptcha{""}; EXPECT_EQ(nlohmann::json(a), R"({ "type": "m.login.recaptcha", "response": "", "session": "" })"_json); a.content = auth::Token{"", ""}; EXPECT_EQ(nlohmann::json(a), R"({ "type": "m.login.token", "token": "", "txn_id": "", "session": "" })"_json); a.content = auth::OAuth2{}; EXPECT_EQ(nlohmann::json(a), R"({ "type": "m.login.oauth2", "session": "" })"_json); a.content = auth::Terms{}; EXPECT_EQ(nlohmann::json(a), R"({ "type": "m.login.terms", "session": "" })"_json); a.content = auth::Dummy{}; EXPECT_EQ(nlohmann::json(a), R"({ "type": "m.login.dummy", "session": "" })"_json); a.content = auth::Fallback{}; EXPECT_EQ(nlohmann::json(a), R"({ "session": "" })"_json); a.content = auth::EmailIdentity{"", "", "", ""}; EXPECT_EQ(nlohmann::json(a), R"({ "type": "m.login.email.identity", "threepid_creds": { "sid": "", "client_secret": "", "id_server": "", "id_access_token": "" }, "session": "" })"_json); a.content = auth::MSISDN{"", "", "", ""}; EXPECT_EQ(nlohmann::json(a), R"({ "type": "m.login.msisdn", "threepid_creds": { "sid": "", "client_secret": "", "id_server": "", "id_access_token": "" }, "session": "" })"_json); a.content = auth::MSISDN{"", "", "", ""}; EXPECT_EQ(nlohmann::json(a), R"({ "type": "m.login.msisdn", "threepid_creds": { "sid": "", "client_secret": "" }, "session": "" })"_json); a.content = auth::RegistrationToken{""}; EXPECT_EQ(nlohmann::json(a), R"({ "type": "m.login.registration_token", "token": "", "session": "" })"_json); } TEST(Requests, RoomVisibility) { mtx::requests::PublicRoomVisibility req; req.visibility = mtx::common::RoomVisibility::Private; json j = req; EXPECT_EQ(j, R"({ "visibility" : "private" })"_json); req.visibility = mtx::common::RoomVisibility::Public; j = req; EXPECT_EQ(j, R"({ "visibility" : "public" })"_json); } TEST(Requests, PublicRooms) { PublicRooms b1, b2, b3; b1.limit = 10; b1.filter.generic_search_term = "foo"; b1.include_all_networks = false; b1.third_party_instance_id = "irc"; json j = b1; EXPECT_EQ(j, R"({ "limit" : 10, "filter" : { "generic_search_term" : "foo" }, "include_all_networks" : false, "third_party_instance_id" : "irc" })"_json); // if third_party_instance_id is set, then the include_all_networks flag should // default to false b2.limit = 10; b2.third_party_instance_id = "matrix"; j = b2; EXPECT_EQ(j, R"({ "limit" : 10, "include_all_networks" : false, "third_party_instance_id" : "matrix" })"_json); // if include_all_networks is true, then third_party_instance_id cannot be used. // if it is somehow set, then we expect an exception to be thrown b3.limit = 2; b3.include_all_networks = true; b3.third_party_instance_id = "irc"; EXPECT_THROW(json req = b3, std::invalid_argument); }