diff options
Diffstat (limited to 'protocols/Telegram/tdlib/td/test/secret.cpp')
-rw-r--r-- | protocols/Telegram/tdlib/td/test/secret.cpp | 376 |
1 files changed, 170 insertions, 206 deletions
diff --git a/protocols/Telegram/tdlib/td/test/secret.cpp b/protocols/Telegram/tdlib/td/test/secret.cpp index 2abed9787b..2c79b7e2d8 100644 --- a/protocols/Telegram/tdlib/td/test/secret.cpp +++ b/protocols/Telegram/tdlib/td/test/secret.cpp @@ -1,36 +1,48 @@ // -// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2018 +// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2022 // // Distributed under the Boost Software License, Version 1.0. (See accompanying // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) // -#include "td/actor/PromiseFuture.h" - -#include "td/db/binlog/detail/BinlogEventsProcessor.h" - -#include "td/mtproto/crypto.h" -#include "td/mtproto/utils.h" - +#include "td/telegram/EncryptedFile.h" +#include "td/telegram/FolderId.h" #include "td/telegram/Global.h" +#include "td/telegram/logevent/LogEvent.h" #include "td/telegram/MessageId.h" +#include "td/telegram/secret_api.h" #include "td/telegram/SecretChatActor.h" #include "td/telegram/SecretChatId.h" - -#include "td/telegram/secret_api.h" #include "td/telegram/telegram_api.h" +#include "td/telegram/UserId.h" + +#include "td/db/binlog/BinlogInterface.h" +#include "td/db/binlog/detail/BinlogEventsProcessor.h" +#include "td/db/BinlogKeyValue.h" +#include "td/db/DbKey.h" + +#include "td/mtproto/DhCallback.h" +#include "td/mtproto/utils.h" #include "td/tl/tl_object_parse.h" #include "td/tl/tl_object_store.h" +#include "td/actor/actor.h" +#include "td/actor/ConcurrentScheduler.h" + +#include "td/utils/algorithm.h" +#include "td/utils/as.h" #include "td/utils/base64.h" #include "td/utils/buffer.h" +#include "td/utils/common.h" #include "td/utils/crypto.h" #include "td/utils/format.h" #include "td/utils/Gzip.h" #include "td/utils/logging.h" #include "td/utils/misc.h" +#include "td/utils/Promise.h" #include "td/utils/Random.h" #include "td/utils/Slice.h" +#include "td/utils/SliceBuilder.h" #include "td/utils/Status.h" #include "td/utils/tests.h" #include "td/utils/tl_helpers.h" @@ -43,17 +55,14 @@ #include <map> #include <memory> -REGISTER_TESTS(secret); - namespace my_api { using namespace td; -//messages_getDhConfig class messages_getDhConfig { public: - int32 version_; - int32 random_length_; + int32 version_{}; + int32 random_length_{}; messages_getDhConfig() = default; @@ -70,7 +79,6 @@ class messages_getDhConfig { } }; -//InputUser class InputUser { public: static tl_object_ptr<InputUser> fetch(TlBufferParser &p); @@ -78,8 +86,8 @@ class InputUser { class inputUser final : public InputUser { public: - int32 user_id_; - int64 access_hash_; + int64 user_id_{}; + int64 access_hash_{}; static const int32 ID = -668391402; inputUser() = default; @@ -92,6 +100,7 @@ class inputUser final : public InputUser { { } }; + tl_object_ptr<InputUser> InputUser::fetch(TlBufferParser &p) { #define FAIL(error) \ p.set_error(error); \ @@ -109,36 +118,28 @@ tl_object_ptr<InputUser> InputUser::fetch(TlBufferParser &p) { class messages_requestEncryption final { public: tl_object_ptr<InputUser> user_id_; - int32 random_id_; + int32 random_id_{}; BufferSlice g_a_; static const int32 ID = -162681021; messages_requestEncryption(); explicit messages_requestEncryption(TlBufferParser &p) -#define FAIL(error) p.set_error(error) : user_id_(TlFetchObject<InputUser>::parse(p)) , random_id_(TlFetchInt::parse(p)) - , g_a_(TlFetchBytes<BufferSlice>::parse(p)) -#undef FAIL - { + , g_a_(TlFetchBytes<BufferSlice>::parse(p)) { } }; class inputEncryptedChat final { public: - int32 chat_id_; - int64 access_hash_; + int32 chat_id_{}; + int64 access_hash_{}; inputEncryptedChat() = default; static const int32 ID = -247351839; - explicit inputEncryptedChat(TlBufferParser &p) -#define FAIL(error) p.set_error(error) - : chat_id_(TlFetchInt::parse(p)) - , access_hash_(TlFetchLong::parse(p)) -#undef FAIL - { + explicit inputEncryptedChat(TlBufferParser &p) : chat_id_(TlFetchInt::parse(p)), access_hash_(TlFetchLong::parse(p)) { } static tl_object_ptr<inputEncryptedChat> fetch(TlBufferParser &p) { return make_tl_object<inputEncryptedChat>(p); @@ -149,56 +150,49 @@ class messages_acceptEncryption final { public: tl_object_ptr<inputEncryptedChat> peer_; BufferSlice g_b_; - int64 key_fingerprint_; + int64 key_fingerprint_{}; messages_acceptEncryption() = default; static const int32 ID = 1035731989; explicit messages_acceptEncryption(TlBufferParser &p) -#define FAIL(error) p.set_error(error) : peer_(TlFetchBoxed<TlFetchObject<inputEncryptedChat>, -247351839>::parse(p)) , g_b_(TlFetchBytes<BufferSlice>::parse(p)) - , key_fingerprint_(TlFetchLong::parse(p)) -#undef FAIL - { + , key_fingerprint_(TlFetchLong::parse(p)) { } }; class messages_sendEncryptedService final { public: tl_object_ptr<inputEncryptedChat> peer_; - int64 random_id_; + int64 random_id_{}; BufferSlice data_; messages_sendEncryptedService() = default; static const int32 ID = 852769188; explicit messages_sendEncryptedService(TlBufferParser &p) -#define FAIL(error) p.set_error(error) : peer_(TlFetchBoxed<TlFetchObject<inputEncryptedChat>, -247351839>::parse(p)) , random_id_(TlFetchLong::parse(p)) - , data_(TlFetchBytes<BufferSlice>::parse(p)) -#undef FAIL - { + , data_(TlFetchBytes<BufferSlice>::parse(p)) { } }; class messages_sendEncrypted final { public: + int32 flags_; tl_object_ptr<inputEncryptedChat> peer_; - int64 random_id_; + int64 random_id_{}; BufferSlice data_; messages_sendEncrypted() = default; - static const int32 ID = -1451792525; + static const int32 ID = 1157265941; explicit messages_sendEncrypted(TlBufferParser &p) -#define FAIL(error) p.set_error(error) - : peer_(TlFetchBoxed<TlFetchObject<inputEncryptedChat>, -247351839>::parse(p)) + : flags_(TlFetchInt::parse(p)) + , peer_(TlFetchBoxed<TlFetchObject<inputEncryptedChat>, -247351839>::parse(p)) , random_id_(TlFetchLong::parse(p)) - , data_(TlFetchBytes<BufferSlice>::parse(p)) -#undef FAIL - { + , data_(TlFetchBytes<BufferSlice>::parse(p)) { } }; @@ -217,16 +211,16 @@ static void downcast_call(TlBufferParser &p, F &&f) { case messages_sendEncryptedService::ID: return f(*make_tl_object<messages_sendEncryptedService>(p)); default: - CHECK(0) << id; + LOG(ERROR) << "Unknown constructor " << id; UNREACHABLE(); } } class messages_dhConfig final { public: - int32 g_; + int32 g_{}; BufferSlice p_; - int32 version_; + int32 version_{}; BufferSlice random_; messages_dhConfig() = default; @@ -258,17 +252,17 @@ class messages_dhConfig final { class encryptedChat final { public: - int32 id_; - int64 access_hash_; - int32 date_; - int32 admin_id_; - int32 participant_id_; + int32 id_{}; + int64 access_hash_{}; + int32 date_{}; + int64 admin_id_{}; + int64 participant_id_{}; BufferSlice g_a_or_b_; - int64 key_fingerprint_; + int64 key_fingerprint_{}; encryptedChat() = default; - encryptedChat(int32 id_, int64 access_hash_, int32 date_, int32 admin_id_, int32 participant_id_, + encryptedChat(int32 id_, int64 access_hash_, int32 date_, int64 admin_id_, int64 participant_id_, BufferSlice &&g_a_or_b_, int64 key_fingerprint_) : id_(id_) , access_hash_(access_hash_) @@ -309,7 +303,7 @@ class encryptedChat final { class messages_sentEncryptedMessage final { public: - int32 date_; + int32 date_{}; messages_sentEncryptedMessage() = default; @@ -342,32 +336,32 @@ static string prime_base64 = "WC2xF40WnGvEZbDW_5yjko_vW5rk5Bj8Feg-vqD4f6n_Xu1wBQ3tKEn0e_lZ2VaFDOkphR8NgRX2NbEF7i5OFdBLJFS_b0-t8DSxBAMRnNjjuS_MW" "w"; -class FakeDhCallback : public DhCallback { +class FakeDhCallback final : public mtproto::DhCallback { public: - int is_good_prime(Slice prime_str) const override { + int is_good_prime(Slice prime_str) const final { auto it = cache.find(prime_str.str()); if (it == cache.end()) { return -1; } return it->second; } - void add_good_prime(Slice prime_str) const override { + void add_good_prime(Slice prime_str) const final { cache[prime_str.str()] = 1; } - void add_bad_prime(Slice prime_str) const override { + void add_bad_prime(Slice prime_str) const final { cache[prime_str.str()] = 0; } mutable std::map<string, int> cache; }; -class FakeBinlog +class FakeBinlog final : public BinlogInterface , public Actor { public: FakeBinlog() { register_actor("FakeBinlog", this).release(); } - void force_sync(Promise<> promise) override { + void force_sync(Promise<> promise) final { if (pending_events_.empty()) { pending_events_.emplace_back(); } @@ -385,15 +379,15 @@ class FakeBinlog } } } - void force_flush() override { + void force_flush() final { } - uint64 next_id() override { + uint64 next_id() final { auto res = last_id_; last_id_++; return res; } - uint64 next_id(int32 shift) override { + uint64 next_id(int32 shift) final { auto res = last_id_; last_id_ += shift; return res; @@ -418,16 +412,16 @@ class FakeBinlog pending_events_.clear(); } - void change_key(DbKey key, Promise<> promise) override { + void change_key(DbKey key, Promise<> promise) final { } protected: - void close_impl(Promise<> promise) override { + void close_impl(Promise<> promise) final { } - void close_and_destroy_impl(Promise<> promise) override { + void close_and_destroy_impl(Promise<> promise) final { } - void add_raw_event_impl(uint64 id, BufferSlice &&raw_event, Promise<> promise) override { - auto event = BinlogEvent(std::move(raw_event)); + void add_raw_event_impl(uint64 id, BufferSlice &&raw_event, Promise<> promise, BinlogDebugInfo info) final { + auto event = BinlogEvent(std::move(raw_event), info); LOG(INFO) << "ADD EVENT: " << event.id_ << " " << event; pending_events_.emplace_back(); pending_events_.back().event = std::move(event); @@ -441,20 +435,18 @@ class FakeBinlog has_request_sync = false; auto pos = static_cast<size_t>(Random::fast_uint64() % pending_events_.size()); // pos = pending_events_.size() - 1; - std::vector<Promise<>> promises; + td::vector<Promise<Unit>> promises; for (size_t i = 0; i <= pos; i++) { auto &pending = pending_events_[i]; auto event = std::move(pending.event); if (!event.empty()) { LOG(INFO) << "SAVE EVENT: " << event.id_ << " " << event; - events_processor_.add_event(std::move(event)); + events_processor_.add_event(std::move(event)).ensure(); } append(promises, std::move(pending.promises_)); } pending_events_.erase(pending_events_.begin(), pending_events_.begin() + pos + 1); - for (auto &promise : promises) { - promise.set_value(Unit()); - } + set_promises(promises); for (auto &event : pending_events_) { if (event.sync_flag) { @@ -463,10 +455,10 @@ class FakeBinlog } } } - void timeout_expired() override { + void timeout_expired() final { do_force_sync(); } - void wakeup() override { + void wakeup() final { if (has_request_sync) { do_force_sync(); } @@ -478,42 +470,16 @@ class FakeBinlog struct PendingEvent { BinlogEvent event; bool sync_flag = false; - std::vector<Promise<>> promises_; + td::vector<Promise<Unit>> promises_; }; std::vector<PendingEvent> pending_events_; }; using FakeKeyValue = BinlogKeyValue<BinlogInterface>; -class OldFakeKeyValue : public KeyValueSyncInterface { - SeqNo set(string key, string value) override { - kv_[key] = value; - return 0; - } - - SeqNo erase(const string &key) override { - kv_.erase(key); - return 0; - } - - bool isset(const string &key) override { - return kv_.count(key) > 0; - } - - string get(const string &key) override { - auto it = kv_.find(key); - if (it != kv_.end()) { - return it->second; - } - return string(); - } - - private: - std::map<string, string> kv_; -}; class Master; -class FakeSecretChatContext : public SecretChatActor::Context { +class FakeSecretChatContext final : public SecretChatActor::Context { public: FakeSecretChatContext(std::shared_ptr<BinlogInterface> binlog, std::shared_ptr<KeyValueSyncInterface> key_value, std::shared_ptr<bool> close_flag, ActorShared<Master> master) @@ -521,28 +487,28 @@ class FakeSecretChatContext : public SecretChatActor::Context { , key_value_(std::move(key_value)) , close_flag_(std::move(close_flag)) , master_(std::move(master)) { - secret_chat_db_ = std::make_unique<SecretChatDb>(key_value_, 1); + secret_chat_db_ = std::make_shared<SecretChatDb>(key_value_, 1); net_query_creator_.stop_check(); // :( } - DhCallback *dh_callback() override { + mtproto::DhCallback *dh_callback() final { return &fake_dh_callback_; } - NetQueryCreator &net_query_creator() override { + NetQueryCreator &net_query_creator() final { return net_query_creator_; } - int32 unix_time() override { + int32 unix_time() final { return static_cast<int32>(std::time(nullptr)); } - bool close_flag() override { + bool close_flag() final { return *close_flag_; } - BinlogInterface *binlog() override { + BinlogInterface *binlog() final { return binlog_.get(); } - SecretChatDb *secret_chat_db() override { + SecretChatDb *secret_chat_db() final { return secret_chat_db_.get(); } - std::shared_ptr<DhConfig> dh_config() override { + std::shared_ptr<DhConfig> dh_config() final { static auto config = [] { DhConfig dh_config; dh_config.version = 12; @@ -553,39 +519,37 @@ class FakeSecretChatContext : public SecretChatActor::Context { return config; } - void set_dh_config(std::shared_ptr<DhConfig> dh_config) override { + void set_dh_config(std::shared_ptr<DhConfig> dh_config) final { // empty } - bool get_config_option_boolean(const string &name) const override { + bool get_config_option_boolean(const string &name) const final { return false; } // We don't want to expose the whole NetQueryDispatcher, MessagesManager and ContactsManager. // So it is more clear which parts of MessagesManager is really used. And it is much easier to create tests. - void send_net_query(NetQueryPtr query, ActorShared<NetQueryCallback> callback, bool ordered) override; + void send_net_query(NetQueryPtr query, ActorShared<NetQueryCallback> callback, bool ordered) final; void on_update_secret_chat(int64 access_hash, UserId user_id, SecretChatState state, bool is_outbound, int32 ttl, - int32 date, string key_hash, int32 layer) override { + int32 date, string key_hash, int32 layer, FolderId initial_folder_id) final { } - void on_inbound_message(UserId user_id, MessageId message_id, int32 date, - tl_object_ptr<telegram_api::encryptedFile> file, - tl_object_ptr<secret_api::decryptedMessage> message, Promise<>) override; + void on_inbound_message(UserId user_id, MessageId message_id, int32 date, unique_ptr<EncryptedFile> file, + tl_object_ptr<secret_api::decryptedMessage> message, Promise<>) final; - void on_send_message_error(int64 random_id, Status error, Promise<>) override; - void on_send_message_ack(int64 random_id) override; - void on_send_message_ok(int64 random_id, MessageId message_id, int32 date, - tl_object_ptr<telegram_api::EncryptedFile> file, Promise<>) override; - void on_delete_messages(std::vector<int64> random_id, Promise<>) override; - void on_flush_history(MessageId, Promise<>) override; - void on_read_message(int64, Promise<>) override; + void on_send_message_error(int64 random_id, Status error, Promise<>) final; + void on_send_message_ack(int64 random_id) final; + void on_send_message_ok(int64 random_id, MessageId message_id, int32 date, unique_ptr<EncryptedFile> file, + Promise<>) final; + void on_delete_messages(std::vector<int64> random_id, Promise<>) final; + void on_flush_history(bool, MessageId, Promise<>) final; + void on_read_message(int64, Promise<>) final; - void on_screenshot_taken(UserId user_id, MessageId message_id, int32 date, int64 random_id, - Promise<> promise) override { + void on_screenshot_taken(UserId user_id, MessageId message_id, int32 date, int64 random_id, Promise<> promise) final { } void on_set_ttl(UserId user_id, MessageId message_id, int32 date, int32 ttl, int64 random_id, - Promise<> promise) override { + Promise<> promise) final { } private: @@ -598,13 +562,13 @@ class FakeSecretChatContext : public SecretChatActor::Context { std::shared_ptr<SecretChatDb> secret_chat_db_; }; -NetQueryCreator FakeSecretChatContext::net_query_creator_; +NetQueryCreator FakeSecretChatContext::net_query_creator_{nullptr}; -class Master : public Actor { +class Master final : public Actor { public: explicit Master(Status *status) : status_(status) { } - class SecretChatProxy : public Actor { + class SecretChatProxy final : public Actor { public: SecretChatProxy(string name, ActorShared<Master> parent) : name_(std::move(name)) { binlog_ = std::make_shared<FakeBinlog>(); @@ -615,8 +579,8 @@ class Master : public Actor { parent_ = parent.get(); parent_token_ = parent.token(); actor_ = create_actor<SecretChatActor>( - "SecretChat " + name_, 123, - std::make_unique<FakeSecretChatContext>(binlog_, key_value_, close_flag_, std::move(parent)), true); + PSLICE() << "SecretChat " << name_, 123, + td::make_unique<FakeSecretChatContext>(binlog_, key_value_, close_flag_, std::move(parent)), true); on_binlog_replay_finish(); } @@ -624,12 +588,11 @@ class Master : public Actor { void add_inbound_message(int32 chat_id, BufferSlice data, uint64 crc) { CHECK(crc64(data.as_slice()) == crc); - auto event = std::make_unique<logevent::InboundSecretMessage>(); - event->qts = 0; + auto event = make_unique<log_event::InboundSecretMessage>(); event->chat_id = chat_id; event->date = 0; event->encrypted_message = std::move(data); - event->qts_ack = PromiseCreator::lambda( + event->promise = PromiseCreator::lambda( [actor_id = actor_id(this), chat_id, data = event->encrypted_message.copy(), crc](Result<> result) mutable { if (result.is_ok()) { LOG(INFO) << "FINISH add_inbound_message " << tag("crc", crc); @@ -642,7 +605,7 @@ class Master : public Actor { add_event(Event::delayed_closure(&SecretChatActor::add_inbound_message, std::move(event))); } - void send_message(tl_object_ptr<secret_api::decryptedMessage> message) { + void send_message(tl_object_ptr<secret_api::DecryptedMessage> message) { BufferSlice serialized_message(serialize(*message)); auto resend_promise = PromiseCreator::lambda( [actor_id = actor_id(this), serialized_message = std::move(serialized_message)](Result<> result) mutable { @@ -671,7 +634,7 @@ class Master : public Actor { int32 binlog_generation_ = 0; void sync_binlog(int32 binlog_generation, Promise<> promise) { if (binlog_generation != binlog_generation_) { - return promise.set_error(Status::Error("binlog generation mismatch")); + return promise.set_error(Status::Error("Binlog generation mismatch")); } binlog_->force_sync(std::move(promise)); } @@ -697,28 +660,28 @@ class Master : public Actor { key_value_->external_init_finish(binlog_); actor_ = create_actor<SecretChatActor>( - "SecretChat " + name_, 123, - std::make_unique<FakeSecretChatContext>(binlog_, key_value_, close_flag_, - ActorShared<Master>(parent_, parent_token_)), + PSLICE() << "SecretChat " << name_, 123, + td::make_unique<FakeSecretChatContext>(binlog_, key_value_, close_flag_, + ActorShared<Master>(parent_, parent_token_)), true); for (auto &event : events) { CHECK(event.type_ == LogEvent::HandlerType::SecretChats); - auto r_message = logevent::SecretChatEvent::from_buffer_slice(event.data_as_buffer_slice()); + auto r_message = log_event::SecretChatEvent::from_buffer_slice(event.data_as_buffer_slice()); LOG_IF(FATAL, r_message.is_error()) << "Failed to deserialize event: " << r_message.error(); auto message = r_message.move_as_ok(); - message->set_logevent_id(event.id_); + message->set_log_event_id(event.id_); LOG(INFO) << "Process binlog event " << *message; switch (message->get_type()) { - case logevent::SecretChatEvent::Type::InboundSecretMessage: + case log_event::SecretChatEvent::Type::InboundSecretMessage: send_closure_later(actor_, &SecretChatActor::replay_inbound_message, - std::unique_ptr<logevent::InboundSecretMessage>( - static_cast<logevent::InboundSecretMessage *>(message.release()))); + unique_ptr<log_event::InboundSecretMessage>( + static_cast<log_event::InboundSecretMessage *>(message.release()))); break; - case logevent::SecretChatEvent::Type::OutboundSecretMessage: + case log_event::SecretChatEvent::Type::OutboundSecretMessage: send_closure_later(actor_, &SecretChatActor::replay_outbound_message, - std::unique_ptr<logevent::OutboundSecretMessage>( - static_cast<logevent::OutboundSecretMessage *>(message.release()))); + unique_ptr<log_event::OutboundSecretMessage>( + static_cast<log_event::OutboundSecretMessage *>(message.release()))); break; default: UNREACHABLE(); @@ -729,7 +692,7 @@ class Master : public Actor { } void on_binlog_replay_finish() { ready_ = true; - LOG(INFO) << "on_binlog_replay_finish!"; + LOG(INFO) << "Finish replay binlog"; send_closure(actor_, &SecretChatActor::binlog_replay_finish); for (auto &event : pending_events_) { send_event(actor_, std::move(event)); @@ -769,7 +732,7 @@ class Master : public Actor { } int32 bad_cnt_ = 0; - void timeout_expired() override { + void timeout_expired() final { LOG(INFO) << "TIMEOUT EXPIRED"; if (events_cnt_ < 4) { bad_cnt_++; @@ -795,21 +758,23 @@ class Master : public Actor { auto &to() { return get_by_id(3 - get_link_token()); } - void start_up() override { - set_context(std::make_shared<Global>()); + void start_up() final { + auto old_context = set_context(std::make_shared<Global>()); alice_ = create_actor<SecretChatProxy>("SecretChatProxy alice", "alice", actor_shared(this, 1)); bob_ = create_actor<SecretChatProxy>("SecretChatProxy bob", "bob", actor_shared(this, 2)); - send_closure(alice_->get_actor_unsafe()->actor_, &SecretChatActor::create_chat, 2, 0, 123, - PromiseCreator::lambda([actor_id = actor_id(this)](Result<SecretChatId> res) { - send_closure(actor_id, &Master::got_secret_chat_id, std::move(res), 0); + send_closure(alice_.get_actor_unsafe()->actor_, &SecretChatActor::create_chat, UserId(static_cast<int64>(2)), 0, + 123, PromiseCreator::lambda([actor_id = actor_id(this)](Result<SecretChatId> res) { + send_closure(actor_id, &Master::got_secret_chat_id, std::move(res), false); })); } - void got_secret_chat_id(Result<SecretChatId> res, int) { // second parameter is needed to workaround clang bug + + void got_secret_chat_id(Result<SecretChatId> res, bool dummy) { CHECK(res.is_ok()); auto id = res.move_as_ok(); LOG(INFO) << "SecretChatId = " << id; } - bool can_fail(NetQueryPtr &query) { + + static bool can_fail(NetQueryPtr &query) { static int cnt = 20; if (cnt > 0) { cnt--; @@ -821,17 +786,18 @@ class Master : public Actor { } return false; } + void send_net_query(NetQueryPtr query, ActorShared<NetQueryCallback> callback, bool ordered) { - if (can_fail(query) && Random::fast(0, 1) == 0) { + if (can_fail(query) && Random::fast_bool()) { LOG(INFO) << "Fail query " << query; auto resend_promise = - PromiseCreator::lambda([id = actor_shared(this, get_link_token()), callback_actor = callback.get(), + PromiseCreator::lambda([self = actor_shared(this, get_link_token()), callback_actor = callback.get(), callback_token = callback.token()](Result<NetQueryPtr> r_net_query) mutable { if (r_net_query.is_error()) { - id.release(); + self.release(); return; } - send_closure(std::move(id), &Master::send_net_query, r_net_query.move_as_ok(), + send_closure(std::move(self), &Master::send_net_query, r_net_query.move_as_ok(), ActorShared<NetQueryCallback>(callback_actor, callback_token), true); }); query->set_error(Status::Error(429, "Test error")); @@ -866,31 +832,33 @@ class Master : public Actor { config.version_ = 12; auto storer = TLObjectStorer<my_api::messages_dhConfig>(config); BufferSlice answer(storer.size()); - storer.store(answer.as_slice().ubegin()); + auto real_size = storer.store(answer.as_slice().ubegin()); + CHECK(real_size == answer.size()); net_query->set_ok(std::move(answer)); send_closure(std::move(callback), &NetQueryCallback::on_result, std::move(net_query)); } void process_net_query(my_api::messages_requestEncryption &&request_encryption, NetQueryPtr net_query, ActorShared<NetQueryCallback> callback) { CHECK(get_link_token() == 1); - send_closure(alice_->get_actor_unsafe()->actor_, &SecretChatActor::update_chat, + send_closure(alice_.get_actor_unsafe()->actor_, &SecretChatActor::update_chat, make_tl_object<telegram_api::encryptedChatWaiting>(123, 321, 0, 1, 2)); - send_closure( - bob_->get_actor_unsafe()->actor_, &SecretChatActor::update_chat, - make_tl_object<telegram_api::encryptedChatRequested>(123, 321, 0, 1, 2, request_encryption.g_a_.clone())); + send_closure(bob_.get_actor_unsafe()->actor_, &SecretChatActor::update_chat, + make_tl_object<telegram_api::encryptedChatRequested>(0, false, 123, 321, 0, 1, 2, + request_encryption.g_a_.clone())); net_query->clear(); } void process_net_query(my_api::messages_acceptEncryption &&request_encryption, NetQueryPtr net_query, ActorShared<NetQueryCallback> callback) { CHECK(get_link_token() == 2); - send_closure(alice_->get_actor_unsafe()->actor_, &SecretChatActor::update_chat, + send_closure(alice_.get_actor_unsafe()->actor_, &SecretChatActor::update_chat, make_tl_object<telegram_api::encryptedChat>(123, 321, 0, 1, 2, request_encryption.g_b_.clone(), request_encryption.key_fingerprint_)); my_api::encryptedChat encrypted_chat(123, 321, 0, 1, 2, BufferSlice(), request_encryption.key_fingerprint_); auto storer = TLObjectStorer<my_api::encryptedChat>(encrypted_chat); BufferSlice answer(storer.size()); - storer.store(answer.as_slice().ubegin()); + auto real_size = storer.store(answer.as_slice().ubegin()); + CHECK(real_size == answer.size()); net_query->set_ok(std::move(answer)); send_closure(std::move(callback), &NetQueryCallback::on_result, std::move(net_query)); send_closure(alice_, &SecretChatProxy::start_test); @@ -898,49 +866,47 @@ class Master : public Actor { send_ping(1, 5000); set_timeout_in(1); } - void timeout_expired() override { + void timeout_expired() final { send_message(1, "oppa"); send_message(2, "appo"); set_timeout_in(1); } - void send_ping(int id, int cnt) { + void send_ping(int32 id, int cnt) { if (cnt % 200 == 0) { - LOG(ERROR) << "send ping " << tag("id", id) << tag("cnt", cnt); + LOG(ERROR) << "Send ping " << tag("id", id) << tag("cnt", cnt); } else { - LOG(INFO) << "send ping " << tag("id", id) << tag("cnt", cnt); + LOG(INFO) << "Send ping " << tag("id", id) << tag("cnt", cnt); } string text = PSTRING() << "PING: " << cnt; send_message(id, std::move(text)); } - void send_message(int id, string text) { + void send_message(int32 id, string text) { auto random_id = Random::secure_int64(); - LOG(INFO) << "send message: " << tag("id", id) << tag("text", text) << tag("random_id", random_id); + LOG(INFO) << "Send message: " << tag("id", id) << tag("text", text) << tag("random_id", random_id); sent_messages_[random_id] = Message{id, text}; send_closure(get_by_id(id), &SecretChatProxy::send_message, - secret_api::make_object<secret_api::decryptedMessage>(0, random_id, 0, text, Auto(), Auto(), Auto(), - Auto(), 0)); + secret_api::make_object<secret_api::decryptedMessage>(0, false /*ignored*/, random_id, 0, text, Auto(), + Auto(), Auto(), Auto(), 0)); } void process_net_query(my_api::messages_sendEncryptedService &&message, NetQueryPtr net_query, ActorShared<NetQueryCallback> callback) { - process_net_query_send_enrypted(std::move(message.data_), std::move(net_query), std::move(callback)); + process_net_query_send_encrypted(std::move(message.data_), std::move(net_query), std::move(callback)); } void process_net_query(my_api::messages_sendEncrypted &&message, NetQueryPtr net_query, ActorShared<NetQueryCallback> callback) { - process_net_query_send_enrypted(std::move(message.data_), std::move(net_query), std::move(callback)); + process_net_query_send_encrypted(std::move(message.data_), std::move(net_query), std::move(callback)); } - void process_net_query_send_enrypted(BufferSlice data, NetQueryPtr net_query, - ActorShared<NetQueryCallback> callback) { - my_api::messages_sentEncryptedMessage sent_message; - sent_message.date_ = 0; - auto storer = TLObjectStorer<my_api::messages_sentEncryptedMessage>(sent_message); - BufferSlice answer(storer.size()); - storer.store(answer.as_slice().ubegin()); + void process_net_query_send_encrypted(BufferSlice data, NetQueryPtr net_query, + ActorShared<NetQueryCallback> callback) { + BufferSlice answer(8); + answer.as_slice().fill(0); + as<int32>(answer.as_slice().begin()) = static_cast<int32>(my_api::messages_sentEncryptedMessage::ID); net_query->set_ok(std::move(answer)); send_closure(std::move(callback), &NetQueryCallback::on_result, std::move(net_query)); // We can't loose updates yet :( auto crc = crc64(data.as_slice()); - LOG(INFO) << "send SecretChatProxy::add_inbound_message" << tag("crc", crc); + LOG(INFO) << "Send SecretChatProxy::add_inbound_message" << tag("crc", crc); send_closure(to(), &SecretChatProxy::add_inbound_message, narrow_cast<int32>(3 - get_link_token()), std::move(data), crc); } @@ -967,10 +933,10 @@ class Master : public Actor { } void on_send_message_error(int64 random_id, Status error, Promise<> promise) { promise.set_value(Unit()); - LOG(INFO) << "on_send_message_error: " << tag("random_id", random_id) << error; + LOG(INFO) << "Receive send message error: " << tag("random_id", random_id) << error; auto it = sent_messages_.find(random_id); if (it == sent_messages_.end()) { - LOG(INFO) << "TODO: try fix errors about message after it is sent"; + LOG(INFO) << "TODO: try to fix errors about message after it is sent"; return; } CHECK(it != sent_messages_.end()); @@ -980,10 +946,10 @@ class Master : public Actor { } void on_send_message_ok(int64 random_id, Promise<> promise) { promise.set_value(Unit()); - LOG(INFO) << "on_send_message_ok: " << tag("random_id", random_id); + LOG(INFO) << "Receive send message ok: " << tag("random_id", random_id); auto it = sent_messages_.find(random_id); if (it == sent_messages_.end()) { - LOG(INFO) << "TODO: try fix errors about message after it is sent"; + LOG(INFO) << "TODO: try to fix errors about message after it is sent"; return; } CHECK(it != sent_messages_.end()); @@ -1000,7 +966,7 @@ class Master : public Actor { }; std::map<int64, Message> sent_messages_; - void hangup_shared() override { + void hangup_shared() final { LOG(INFO) << "GOT HANGUP: " << get_link_token(); send_closure(from(), &SecretChatProxy::on_closed); } @@ -1010,7 +976,7 @@ void FakeSecretChatContext::send_net_query(NetQueryPtr query, ActorShared<NetQue send_closure(master_, &Master::send_net_query, std::move(query), std::move(callback), ordered); } void FakeSecretChatContext::on_inbound_message(UserId user_id, MessageId message_id, int32 date, - tl_object_ptr<telegram_api::encryptedFile> file, + unique_ptr<EncryptedFile> file, tl_object_ptr<secret_api::decryptedMessage> message, Promise<> promise) { send_closure(master_, &Master::on_inbound_message, message->message_, std::move(promise)); } @@ -1020,24 +986,22 @@ void FakeSecretChatContext::on_send_message_error(int64 random_id, Status error, void FakeSecretChatContext::on_send_message_ack(int64 random_id) { } void FakeSecretChatContext::on_send_message_ok(int64 random_id, MessageId message_id, int32 date, - tl_object_ptr<telegram_api::EncryptedFile> file, Promise<> promise) { + unique_ptr<EncryptedFile> file, Promise<> promise) { send_closure(master_, &Master::on_send_message_ok, random_id, std::move(promise)); } void FakeSecretChatContext::on_delete_messages(std::vector<int64> random_id, Promise<> promise) { promise.set_value(Unit()); } -void FakeSecretChatContext::on_flush_history(MessageId, Promise<> promise) { - promise.set_error(Status::Error("unsupported")); +void FakeSecretChatContext::on_flush_history(bool, MessageId, Promise<> promise) { + promise.set_error(Status::Error("Unsupported")); } void FakeSecretChatContext::on_read_message(int64, Promise<> promise) { - promise.set_error(Status::Error("unsupported")); + promise.set_error(Status::Error("Unsupported")); } TEST(Secret, go) { - SET_VERBOSITY_LEVEL(VERBOSITY_NAME(ERROR)); - ConcurrentScheduler sched; - int threads_n = 0; - sched.init(threads_n); + return; + ConcurrentScheduler sched(0, 0); Status result; sched.create_actor_unsafe<Master>(0, "HandshakeTestActor", &result).release(); |