Skip to content
Snippets Groups Projects
Commit 6ecda9ed authored by Konstantinos Sideris's avatar Konstantinos Sideris
Browse files

Replace std::optional with boost::optional

On Windows std::optional requires C++17
parent 3ac3a5cb
No related branches found
No related tags found
No related merge requests found
......@@ -16,11 +16,10 @@ using namespace std;
using namespace mtx::client;
using namespace mtx::events;
using ErrType = experimental::optional<mtx::client::errors::ClientError>;
using TimelineEvent = mtx::events::collections::TimelineEvents;
void
print_errors(ErrType err)
print_errors(RequestErr err)
{
if (err->status_code != boost::beast::http::status::unknown)
cout << err->status_code << "\n";
......@@ -82,7 +81,7 @@ print_message(const TimelineEvent &event)
// Callback to executed after a /sync request completes.
void
sync_handler(shared_ptr<Client> client, const mtx::responses::Sync &res, ErrType err)
sync_handler(shared_ptr<Client> client, const mtx::responses::Sync &res, RequestErr err)
{
if (err) {
cout << "sync error:\n";
......@@ -115,7 +114,7 @@ sync_handler(shared_ptr<Client> client, const mtx::responses::Sync &res, ErrType
// Callback to executed after the first (initial) /sync request completes.
void
initial_sync_handler(shared_ptr<Client> client, const nlohmann::json &res, ErrType err)
initial_sync_handler(shared_ptr<Client> client, const nlohmann::json &res, RequestErr err)
{
if (err) {
cout << "error during initial sync:\n";
......@@ -161,24 +160,25 @@ main()
auto client = std::make_shared<Client>(server);
client->login(username, password, [client](const mtx::responses::Login &res, ErrType err) {
if (err) {
cout << "There was an error during login: " << err->matrix_error.error
<< "\n";
return;
}
cout << "Logged in as: " << res.user_id.to_string() << "\n";
client->set_access_token(res.access_token);
client->sync(
"",
"",
false,
0,
std::bind(
&initial_sync_handler, client, std::placeholders::_1, std::placeholders::_2));
});
client->login(
username, password, [client](const mtx::responses::Login &res, RequestErr err) {
if (err) {
cout << "There was an error during login: " << err->matrix_error.error
<< "\n";
return;
}
cout << "Logged in as: " << res.user_id.to_string() << "\n";
client->set_access_token(res.access_token);
client->sync(
"",
"",
false,
0,
std::bind(
&initial_sync_handler, client, std::placeholders::_1, std::placeholders::_2));
});
client->close();
......
......@@ -176,11 +176,9 @@ Client::setup_auth(std::shared_ptr<Session> session, bool auth)
//
void
Client::login(
const std::string &user,
const std::string &password,
std::function<void(const mtx::responses::Login &response,
std::experimental::optional<mtx::client::errors::ClientError>)> callback)
Client::login(const std::string &user,
const std::string &password,
std::function<void(const mtx::responses::Login &response, RequestErr err)> callback)
{
mtx::requests::Login req;
req.user = user;
......@@ -189,9 +187,8 @@ Client::login(
post<mtx::requests::Login, mtx::responses::Login>(
"/client/r0/login",
req,
[_this = shared_from_this(),
callback](const mtx::responses::Login &resp,
std::experimental::optional<mtx::client::errors::ClientError> err) {
[_this = shared_from_this(), callback](const mtx::responses::Login &resp,
RequestErr err) {
if (!err && resp.access_token.size()) {
_this->user_id_ = resp.user_id;
_this->device_id_ = resp.device_id;
......@@ -203,18 +200,15 @@ Client::login(
}
void
Client::logout(
std::function<void(const mtx::responses::Logout &response,
std::experimental::optional<mtx::client::errors::ClientError>)> callback)
Client::logout(std::function<void(const mtx::responses::Logout &response, RequestErr)> callback)
{
mtx::requests::Logout req;
post<mtx::requests::Logout, mtx::responses::Logout>(
"/client/r0/logout",
req,
[_this = shared_from_this(),
callback](const mtx::responses::Logout &res,
std::experimental::optional<mtx::client::errors::ClientError> err) {
[_this = shared_from_this(), callback](const mtx::responses::Logout &res,
RequestErr err) {
if (!err) {
// Clear the now invalid access token when logout is successful
_this->access_token_.clear();
......
#pragma once
#include <experimental/optional>
#include <memory>
#include <mutex>
#include <thread>
......@@ -9,6 +8,7 @@
#include <boost/asio/ssl.hpp>
#include <boost/beast.hpp>
#include <boost/iostreams/stream.hpp>
#include <boost/optional.hpp>
#include <boost/thread/thread.hpp>
#include <json.hpp>
......@@ -38,6 +38,8 @@ to_string(PaginationDirection dir)
return "f";
}
using RequestErr = const boost::optional<mtx::client::errors::ClientError> &;
//! The main object that the user will interact.
class Client : public std::enable_shared_from_this<Client>
{
......@@ -63,8 +65,7 @@ public:
//! Generate a new transaction id.
std::string generate_txn_id() { return utils::random_token(32, false); }
using HeaderFields = std::experimental::optional<boost::beast::http::fields>;
using RequestErr = std::experimental::optional<mtx::client::errors::ClientError>;
using HeaderFields = const boost::optional<boost::beast::http::fields> &;
//! Perfom login.
void login(const std::string &username,
......
This diff is collapsed.
......@@ -9,15 +9,14 @@ using namespace mtx::client;
using namespace std;
using ErrType = std::experimental::optional<errors::ClientError>;
TEST(Basic, Connection)
{
auto alice = std::make_shared<Client>("localhost", 8448);
auto bob = std::make_shared<Client>("localhost", 443);
alice->versions([](const mtx::responses::Versions &, ErrType err) { ASSERT_FALSE(err); });
bob->versions([](const mtx::responses::Versions &, ErrType err) { ASSERT_FALSE(err); });
alice->versions(
[](const mtx::responses::Versions &, RequestErr err) { ASSERT_FALSE(err); });
bob->versions([](const mtx::responses::Versions &, RequestErr err) { ASSERT_FALSE(err); });
bob->close();
alice->close();
......@@ -26,6 +25,6 @@ TEST(Basic, Connection)
TEST(Basic, Failure)
{
auto alice = std::make_shared<Client>("not-resolvable-example-domain.wrong");
alice->versions([](const mtx::responses::Versions &, ErrType err) { ASSERT_TRUE(err); });
alice->versions([](const mtx::responses::Versions &, RequestErr err) { ASSERT_TRUE(err); });
alice->close();
}
......@@ -22,8 +22,6 @@ using namespace mtx::events::collections;
using namespace std;
using ErrType = std::experimental::optional<errors::ClientError>;
mtx::requests::UploadKeys
generate_keys(std::shared_ptr<olm::Account> account,
const mtx::identifiers::User &user_id,
......@@ -39,7 +37,7 @@ generate_keys(std::shared_ptr<olm::Account> account,
}
void
check_error(ErrType err)
check_error(RequestErr err)
{
if (err) {
cout << "matrix (error) : " << err->matrix_error.error << "\n";
......@@ -60,8 +58,9 @@ TEST(Encryption, UploadIdentityKeys)
auto alice = std::make_shared<Client>("localhost");
auto olm_account = mtx::client::crypto::olm_new_account();
alice->login(
"alice", "secret", [](const mtx::responses::Login &, ErrType err) { check_error(err); });
alice->login("alice", "secret", [](const mtx::responses::Login &, RequestErr err) {
check_error(err);
});
while (alice->access_token().empty())
std::this_thread::sleep_for(std::chrono::milliseconds(100));
......@@ -76,7 +75,7 @@ TEST(Encryption, UploadIdentityKeys)
olm_account, identity_keys, unused, alice->user_id(), alice->device_id());
// Make the request with the signed identity keys.
alice->upload_keys(request, [](const mtx::responses::UploadKeys &res, ErrType err) {
alice->upload_keys(request, [](const mtx::responses::UploadKeys &res, RequestErr err) {
check_error(err);
EXPECT_EQ(res.one_time_key_counts.size(), 0);
});
......@@ -89,8 +88,9 @@ TEST(Encryption, UploadOneTimeKeys)
auto alice = std::make_shared<Client>("localhost");
auto olm_account = mtx::client::crypto::olm_new_account();
alice->login(
"alice", "secret", [](const mtx::responses::Login &, ErrType err) { check_error(err); });
alice->login("alice", "secret", [](const mtx::responses::Login &, RequestErr err) {
check_error(err);
});
while (alice->access_token().empty())
std::this_thread::sleep_for(std::chrono::milliseconds(100));
......@@ -111,7 +111,7 @@ TEST(Encryption, UploadOneTimeKeys)
req.one_time_keys = unsigned_keys;
alice->upload_keys(req, [](const mtx::responses::UploadKeys &res, ErrType err) {
alice->upload_keys(req, [](const mtx::responses::UploadKeys &res, RequestErr err) {
check_error(err);
EXPECT_EQ(res.one_time_key_counts.size(), 1);
EXPECT_EQ(res.one_time_key_counts.at("curve25519"), 5);
......@@ -125,8 +125,9 @@ TEST(Encryption, UploadSignedOneTimeKeys)
auto alice = std::make_shared<Client>("localhost");
auto olm_account = mtx::client::crypto::olm_new_account();
alice->login(
"alice", "secret", [](const mtx::responses::Login &, ErrType err) { check_error(err); });
alice->login("alice", "secret", [](const mtx::responses::Login &, RequestErr err) {
check_error(err);
});
while (alice->access_token().empty())
std::this_thread::sleep_for(std::chrono::milliseconds(100));
......@@ -140,7 +141,7 @@ TEST(Encryption, UploadSignedOneTimeKeys)
req.one_time_keys = mtx::client::crypto::sign_one_time_keys(
olm_account, one_time_keys, alice->user_id(), alice->device_id());
alice->upload_keys(req, [nkeys](const mtx::responses::UploadKeys &res, ErrType err) {
alice->upload_keys(req, [nkeys](const mtx::responses::UploadKeys &res, RequestErr err) {
check_error(err);
EXPECT_EQ(res.one_time_key_counts.size(), 1);
EXPECT_EQ(res.one_time_key_counts.at("signed_curve25519"), nkeys);
......@@ -154,15 +155,16 @@ TEST(Encryption, UploadKeys)
auto alice = std::make_shared<Client>("localhost");
auto olm_account = mtx::client::crypto::olm_new_account();
alice->login(
"alice", "secret", [](const mtx::responses::Login &, ErrType err) { check_error(err); });
alice->login("alice", "secret", [](const mtx::responses::Login &, RequestErr err) {
check_error(err);
});
while (alice->access_token().empty())
std::this_thread::sleep_for(std::chrono::milliseconds(100));
auto req = ::generate_keys(olm_account, alice->user_id(), alice->device_id());
alice->upload_keys(req, [](const mtx::responses::UploadKeys &res, ErrType err) {
alice->upload_keys(req, [](const mtx::responses::UploadKeys &res, RequestErr err) {
check_error(err);
EXPECT_EQ(res.one_time_key_counts.size(), 1);
EXPECT_EQ(res.one_time_key_counts.at("signed_curve25519"), 1);
......@@ -179,11 +181,12 @@ TEST(Encryption, QueryKeys)
auto bob = std::make_shared<Client>("localhost");
auto bob_olm = mtx::client::crypto::olm_new_account();
alice->login(
"alice", "secret", [](const mtx::responses::Login &, ErrType err) { check_error(err); });
alice->login("alice", "secret", [](const mtx::responses::Login &, RequestErr err) {
check_error(err);
});
bob->login(
"bob", "secret", [](const mtx::responses::Login &, ErrType err) { check_error(err); });
"bob", "secret", [](const mtx::responses::Login &, RequestErr err) { check_error(err); });
while (alice->access_token().empty() || bob->access_token().empty())
std::this_thread::sleep_for(std::chrono::milliseconds(100));
......@@ -196,7 +199,7 @@ TEST(Encryption, QueryKeys)
atomic_int uploads(0);
alice->upload_keys(alice_req,
[&uploads](const mtx::responses::UploadKeys &res, ErrType err) {
[&uploads](const mtx::responses::UploadKeys &res, RequestErr err) {
check_error(err);
EXPECT_EQ(res.one_time_key_counts.size(), 1);
EXPECT_EQ(res.one_time_key_counts.at("signed_curve25519"), 1);
......@@ -204,13 +207,14 @@ TEST(Encryption, QueryKeys)
uploads += 1;
});
bob->upload_keys(bob_req, [&uploads](const mtx::responses::UploadKeys &res, ErrType err) {
check_error(err);
EXPECT_EQ(res.one_time_key_counts.size(), 1);
EXPECT_EQ(res.one_time_key_counts.at("signed_curve25519"), 1);
bob->upload_keys(bob_req,
[&uploads](const mtx::responses::UploadKeys &res, RequestErr err) {
check_error(err);
EXPECT_EQ(res.one_time_key_counts.size(), 1);
EXPECT_EQ(res.one_time_key_counts.at("signed_curve25519"), 1);
uploads += 1;
});
uploads += 1;
});
while (uploads != 2)
std::this_thread::sleep_for(std::chrono::milliseconds(100));
......@@ -221,7 +225,8 @@ TEST(Encryption, QueryKeys)
mtx::requests::QueryKeys alice_rk;
alice_rk.device_keys[bob->user_id().to_string()] = {};
alice->query_keys(
alice_rk, [&responses, bob, bob_req](const mtx::responses::QueryKeys &res, ErrType err) {
alice_rk,
[&responses, bob, bob_req](const mtx::responses::QueryKeys &res, RequestErr err) {
check_error(err);
ASSERT_TRUE(res.failures.size() == 0);
......@@ -242,7 +247,7 @@ TEST(Encryption, QueryKeys)
bob_rk.device_keys[alice->user_id().to_string()] = {};
bob->query_keys(
bob_rk,
[&responses, alice, alice_req](const mtx::responses::QueryKeys &res, ErrType err) {
[&responses, alice, alice_req](const mtx::responses::QueryKeys &res, RequestErr err) {
check_error(err);
ASSERT_TRUE(res.failures.size() == 0);
......@@ -271,53 +276,59 @@ TEST(Encryption, KeyChanges)
auto carl = std::make_shared<Client>("localhost");
auto carl_olm = mtx::client::crypto::olm_new_account();
carl->login(
"carl", "secret", [](const mtx::responses::Login &, ErrType err) { check_error(err); });
carl->login("carl", "secret", [](const mtx::responses::Login &, RequestErr err) {
check_error(err);
});
while (carl->access_token().empty())
std::this_thread::sleep_for(std::chrono::milliseconds(100));
mtx::requests::CreateRoom req;
carl->create_room(req, [carl, carl_olm](const mtx::responses::CreateRoom &, ErrType err) {
check_error(err);
carl->create_room(
req, [carl, carl_olm](const mtx::responses::CreateRoom &, RequestErr err) {
check_error(err);
// Carl syncs to get the first next_batch token.
carl->sync(
"", "", false, 0, [carl, carl_olm](const mtx::responses::Sync &res, ErrType err) {
check_error(err);
const auto next_batch_token = res.next_batch;
auto key_req =
::generate_keys(carl_olm, carl->user_id(), carl->device_id());
atomic_bool keys_uploaded(false);
// Changes his keys.
carl->upload_keys(
key_req,
[&keys_uploaded](const mtx::responses::UploadKeys &, ErrType err) {
check_error(err);
keys_uploaded = true;
});
while (!keys_uploaded)
std::this_thread::sleep_for(std::chrono::milliseconds(100));
// The key changes should contain his username
// because of the key uploading.
carl->key_changes(
next_batch_token,
"",
[carl](const mtx::responses::KeyChanges &res, ErrType err) {
check_error(err);
EXPECT_EQ(res.changed.size(), 1);
EXPECT_EQ(res.left.size(), 0);
EXPECT_EQ(res.changed.at(0), carl->user_id().to_string());
});
});
});
// Carl syncs to get the first next_batch token.
carl->sync(
"",
"",
false,
0,
[carl, carl_olm](const mtx::responses::Sync &res, RequestErr err) {
check_error(err);
const auto next_batch_token = res.next_batch;
auto key_req =
::generate_keys(carl_olm, carl->user_id(), carl->device_id());
atomic_bool keys_uploaded(false);
// Changes his keys.
carl->upload_keys(
key_req,
[&keys_uploaded](const mtx::responses::UploadKeys &, RequestErr err) {
check_error(err);
keys_uploaded = true;
});
while (!keys_uploaded)
std::this_thread::sleep_for(std::chrono::milliseconds(100));
// The key changes should contain his username
// because of the key uploading.
carl->key_changes(
next_batch_token,
"",
[carl](const mtx::responses::KeyChanges &res, RequestErr err) {
check_error(err);
EXPECT_EQ(res.changed.size(), 1);
EXPECT_EQ(res.left.size(), 0);
EXPECT_EQ(res.changed.at(0), carl->user_id().to_string());
});
});
});
carl->close();
}
......@@ -16,8 +16,6 @@ using namespace mtx::identifiers;
using namespace std;
using ErrType = std::experimental::optional<errors::ClientError>;
string
get_media_id(const mtx::responses::ContentURI &res)
{
......@@ -37,7 +35,7 @@ read_file(const string &file_path)
}
void
validate_upload(const mtx::responses::ContentURI &res, ErrType err)
validate_upload(const mtx::responses::ContentURI &res, RequestErr err)
{
ASSERT_FALSE(err);
ASSERT_TRUE(res.content_uri.size() > 10);
......@@ -47,7 +45,7 @@ TEST(MediaAPI, UploadTextFile)
{
std::shared_ptr<Client> alice = std::make_shared<Client>("localhost");
alice->login("alice", "secret", [alice](const mtx::responses::Login &, ErrType err) {
alice->login("alice", "secret", [alice](const mtx::responses::Login &, RequestErr err) {
ASSERT_FALSE(err);
const auto text = "This is some random text";
......@@ -55,7 +53,7 @@ TEST(MediaAPI, UploadTextFile)
alice->upload(text,
"text/plain",
"doc.txt",
[alice, text](const mtx::responses::ContentURI &res, ErrType err) {
[alice, text](const mtx::responses::ContentURI &res, RequestErr err) {
validate_upload(res, err);
alice->download("localhost",
......@@ -63,7 +61,7 @@ TEST(MediaAPI, UploadTextFile)
[text](const string &data,
const string &content_type,
const string &original_filename,
ErrType err) {
RequestErr err) {
ASSERT_FALSE(err);
EXPECT_EQ(data, text);
EXPECT_EQ(content_type, "text/plain");
......@@ -80,7 +78,7 @@ TEST(MediaAPI, UploadAudio)
{
std::shared_ptr<Client> bob = std::make_shared<Client>("localhost");
bob->login("bob", "secret", [bob](const mtx::responses::Login &, ErrType err) {
bob->login("bob", "secret", [bob](const mtx::responses::Login &, RequestErr err) {
ASSERT_FALSE(err);
const auto audio = read_file("./fixtures/sound.mp3");
......@@ -88,7 +86,7 @@ TEST(MediaAPI, UploadAudio)
bob->upload(audio,
"audio/mp3",
"sound.mp3",
[bob, audio](const mtx::responses::ContentURI &res, ErrType err) {
[bob, audio](const mtx::responses::ContentURI &res, RequestErr err) {
validate_upload(res, err);
bob->download("localhost",
......@@ -96,7 +94,7 @@ TEST(MediaAPI, UploadAudio)
[audio](const string &data,
const string &content_type,
const string &original_filename,
ErrType err) {
RequestErr err) {
ASSERT_FALSE(err);
EXPECT_EQ(data, audio);
EXPECT_EQ(content_type, "audio/mp3");
......@@ -112,7 +110,7 @@ TEST(MediaAPI, UploadImage)
{
std::shared_ptr<Client> carl = std::make_shared<Client>("localhost");
carl->login("carl", "secret", [carl](const mtx::responses::Login &, ErrType err) {
carl->login("carl", "secret", [carl](const mtx::responses::Login &, RequestErr err) {
ASSERT_FALSE(err);
const auto img = read_file("./fixtures/test.jpeg");
......@@ -120,7 +118,7 @@ TEST(MediaAPI, UploadImage)
carl->upload(img,
"image/jpeg",
"test.jpeg",
[carl, img](const mtx::responses::ContentURI &res, ErrType err) {
[carl, img](const mtx::responses::ContentURI &res, RequestErr err) {
validate_upload(res, err);
carl->download("localhost",
......@@ -128,7 +126,7 @@ TEST(MediaAPI, UploadImage)
[img](const string &data,
const string &content_type,
const string &original_filename,
ErrType err) {
RequestErr err) {
ASSERT_FALSE(err);
EXPECT_EQ(data, img);
EXPECT_EQ(content_type, "image/jpeg");
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment