| 1 | #include <atomic> |
| 2 | |
| 3 | #include <gtest/gtest.h> |
| 4 | |
| 5 | #include <nlohmann/json.hpp> |
| 6 | |
| 7 | #include "mtxclient/crypto/client.hpp" |
| 8 | #include "mtxclient/crypto/types.hpp" |
| 9 | #include "mtxclient/http/client.hpp" |
| 10 | |
| 11 | #include "mtx/requests.hpp" |
| 12 | #include "mtx/responses.hpp" |
| 13 | |
| 14 | #include "test_helpers.hpp" |
| 15 | |
| 16 | using namespace mtx::client; |
| 17 | using namespace mtx::http; |
| 18 | using namespace mtx::crypto; |
| 19 | |
| 20 | using namespace mtx::identifiers; |
| 21 | using namespace mtx::events; |
| 22 | using namespace mtx::events::msg; |
| 23 | using namespace mtx::events::collections; |
| 24 | using namespace mtx::responses; |
| 25 | |
| 26 | using namespace std; |
| 27 | |
| 28 | using namespace nlohmann; |
| 29 | |
| 30 | TEST(Devices, DeleteDevices) |
| 31 | { |
| 32 | auto alice = make_test_client(); |
| 33 | |
| 34 | alice->login( |
| 35 | "alice", "secret", [](const mtx::responses::Login &, RequestErr err) { check_error(err); }); |
| 36 | |
| 37 | auto alice_dummy = make_test_client(); |
| 38 | alice_dummy->login( |
| 39 | "alice", "secret", [](const mtx::responses::Login &, RequestErr err) { check_error(err); }); |
| 40 | |
| 41 | while (alice->access_token().empty() || alice_dummy->access_token().empty()) |
| 42 | sleep(); |
| 43 | |
| 44 | std::string myDevice = alice->device_id(); |
| 45 | |
| 46 | atomic_int responses(0); |
| 47 | std::vector<std::string> device_ids; |
| 48 | |
| 49 | // Get list of devices, fill device_ids with all devices except current |
| 50 | alice->query_devices( |
| 51 | [&responses, &device_ids, myDevice](const mtx::responses::QueryDevices &res, RequestErr err) { |
| 52 | check_error(err); |
| 53 | for (auto it = res.devices.begin(); it != res.devices.end(); ++it) |
| 54 | if (it->device_id != myDevice) |
| 55 | device_ids.push_back(it->device_id); |
| 56 | responses += 1; |
| 57 | }); |
| 58 | |
| 59 | while (responses != 1) |
| 60 | sleep(); |
| 61 | |
| 62 | // Delete all devices except current |
| 63 | alice->delete_devices( |
| 64 | device_ids, |
| 65 | mtx::http::UIAHandler([](const mtx::http::UIAHandler &h, |
| 66 | const mtx::user_interactive::Unauthorized &unauthorized) { |
| 67 | ASSERT_EQ(unauthorized.flows.size(), 1); |
| 68 | ASSERT_EQ(unauthorized.flows[0].stages.size(), 1); |
| 69 | ASSERT_EQ(unauthorized.flows[0].stages[0], mtx::user_interactive::auth_types::password); |
| 70 | |
| 71 | mtx::user_interactive::Auth auth; |
| 72 | auth.session = unauthorized.session; |
| 73 | mtx::user_interactive::auth::Password pass{}; |
| 74 | pass.password = "secret"; |
| 75 | pass.identifier_user = "alice"; |
| 76 | pass.identifier_type = mtx::user_interactive::auth::Password::IdType::UserId; |
| 77 | auth.content = pass; |
| 78 | h.next(auth); |
| 79 | }), |
| 80 | [&responses](RequestErr e) { |
| 81 | check_error(e); |
| 82 | responses += 1; |
| 83 | }); |
| 84 | |
| 85 | while (responses != 2) |
| 86 | sleep(); |
| 87 | |
| 88 | // Check if the current device is the only device left |
| 89 | alice->query_devices( |
| 90 | [&responses, myDevice](const mtx::responses::QueryDevices &res, RequestErr err) { |
| 91 | check_error(err); |
| 92 | ASSERT_EQ(res.devices.size(), 1); |
| 93 | ASSERT_EQ(res.devices.begin()->device_id, myDevice); |
| 94 | responses += 1; |
| 95 | }); |
| 96 | |
| 97 | // Check if dummy can no longer retrieve list of devices because their token was removed |
| 98 | alice_dummy->query_devices([](const mtx::responses::QueryDevices &res, RequestErr err) { |
| 99 | ASSERT_TRUE(err); |
| 100 | ASSERT_EQ(res.devices.size(), 0); |
| 101 | EXPECT_EQ(mtx::errors::to_string(err->matrix_error.errcode), "M_UNKNOWN_TOKEN"); |
| 102 | }); |
| 103 | |
| 104 | while (responses != 3) |
| 105 | sleep(); |
| 106 | |
| 107 | // Delete current device |
| 108 | alice->delete_device( |
| 109 | myDevice, |
| 110 | mtx::http::UIAHandler([](const mtx::http::UIAHandler &h, |
| 111 | const mtx::user_interactive::Unauthorized &unauthorized) { |
| 112 | ASSERT_EQ(unauthorized.flows.size(), 1); |
| 113 | ASSERT_EQ(unauthorized.flows[0].stages.size(), 1); |
| 114 | ASSERT_EQ(unauthorized.flows[0].stages[0], mtx::user_interactive::auth_types::password); |
| 115 | |
| 116 | mtx::user_interactive::Auth auth; |
| 117 | auth.session = unauthorized.session; |
| 118 | mtx::user_interactive::auth::Password pass{}; |
| 119 | pass.password = "secret"; |
| 120 | pass.identifier_user = "alice"; |
| 121 | pass.identifier_type = mtx::user_interactive::auth::Password::IdType::UserId; |
| 122 | auth.content = pass; |
| 123 | h.next(auth); |
| 124 | }), |
| 125 | [&responses](RequestErr e) { |
| 126 | check_error(e); |
| 127 | responses += 1; |
| 128 | }); |
| 129 | |
| 130 | alice->close(); |
| 131 | } |
| 132 | |
| 133 | TEST(Devices, RenameDevices) |
| 134 | { |
| 135 | auto alice_one = make_test_client(); |
| 136 | auto alice_two = make_test_client(); |
| 137 | |
| 138 | alice_one->login( |
| 139 | "alice", "secret", [](const mtx::responses::Login &, RequestErr err) { check_error(err); }); |
| 140 | alice_two->login( |
| 141 | "alice", "secret", [](const mtx::responses::Login &, RequestErr err) { check_error(err); }); |
| 142 | |
| 143 | while (alice_one->access_token().empty() || alice_two->access_token().empty()) |
| 144 | sleep(); |
| 145 | |
| 146 | atomic_int responses(0); |
| 147 | |
| 148 | // Rename the two logged in devices to Alice One and Alice Two |
| 149 | alice_one->set_device_name(alice_one->device_id(), "Alice One", [&responses](RequestErr err) { |
| 150 | check_error(err); |
| 151 | responses += 1; |
| 152 | }); |
| 153 | alice_two->set_device_name(alice_two->device_id(), "Alice Two", [&responses](RequestErr err) { |
| 154 | check_error(err); |
| 155 | responses += 1; |
| 156 | }); |
| 157 | |
| 158 | while (responses != 2) |
| 159 | sleep(); |
| 160 | |
| 161 | // Request info for current devices and confirm display name changed |
| 162 | alice_one->get_device(alice_one->device_id(), |
| 163 | [&responses](const mtx::responses::Device &res, RequestErr err) { |
| 164 | ASSERT_EQ(res.display_name, "Alice One"); |
| 165 | check_error(err); |
| 166 | responses += 1; |
| 167 | }); |
| 168 | alice_two->get_device(alice_two->device_id(), |
| 169 | [&responses](const mtx::responses::Device &res, RequestErr err) { |
| 170 | ASSERT_EQ(res.display_name, "Alice Two"); |
| 171 | check_error(err); |
| 172 | responses += 1; |
| 173 | }); |
| 174 | |
| 175 | while (responses != 4) |
| 176 | sleep(); |
| 177 | |
| 178 | // Request device list and check both One and Two are in the list |
| 179 | alice_one->query_devices([&responses](const mtx::responses::QueryDevices &res, RequestErr err) { |
| 180 | check_error(err); |
| 181 | bool oneFound = false; |
| 182 | bool twoFound = false; |
| 183 | for (auto it = res.devices.begin(); it != res.devices.end(); ++it) { |
| 184 | if (it->display_name == "Alice One") |
| 185 | oneFound = true; |
| 186 | if (it->display_name == "Alice Two") |
| 187 | twoFound = true; |
| 188 | } |
| 189 | ASSERT_EQ(oneFound, true); |
| 190 | ASSERT_EQ(twoFound, true); |
| 191 | responses += 1; |
| 192 | }); |
| 193 | |
| 194 | while (responses != 5) |
| 195 | sleep(); |
| 196 | |
| 197 | alice_one->close(); |
| 198 | alice_two->close(); |
| 199 | } |