mirror of
https://github.com/basiliscos/syncspirit.git
synced 2026-06-01 17:47:32 +00:00
core, remove uknown device when adding the same ignored
This commit is contained in:
@@ -4,13 +4,26 @@
|
||||
#include "add_ignored_device.h"
|
||||
#include "model/cluster.h"
|
||||
#include "model/diff/cluster_visitor.h"
|
||||
#include "model/diff/modify/remove_unknown_device.h"
|
||||
|
||||
using namespace syncspirit::model::diff::modify;
|
||||
|
||||
add_ignored_device_t::add_ignored_device_t(const device_id_t &id_, db::SomeDevice db_device_) noexcept
|
||||
: device_id{id_}, db_device{db_device_} {}
|
||||
add_ignored_device_t::add_ignored_device_t(const cluster_t &cluster, const device_id_t &id_,
|
||||
db::SomeDevice db_device_) noexcept
|
||||
: device_id{id_}, db_device{db_device_} {
|
||||
auto peer = cluster.get_unknown_devices().by_sha256(device_id.get_sha256());
|
||||
if (peer) {
|
||||
auto diff = cluster_diff_ptr_t{};
|
||||
diff = new remove_unknown_device_t(*peer);
|
||||
diffs.emplace_back(std::move(diff));
|
||||
}
|
||||
}
|
||||
|
||||
auto add_ignored_device_t::apply_impl(cluster_t &cluster) const noexcept -> outcome::result<void> {
|
||||
auto r = parent_t::apply_impl(cluster);
|
||||
if (!r) {
|
||||
return r;
|
||||
}
|
||||
auto opt = ignored_device_t::create(device_id, db_device);
|
||||
if (!opt) {
|
||||
return opt.assume_error();
|
||||
|
||||
@@ -8,8 +8,10 @@
|
||||
|
||||
namespace syncspirit::model::diff::modify {
|
||||
|
||||
struct SYNCSPIRIT_API add_ignored_device_t final : cluster_diff_t {
|
||||
add_ignored_device_t(const device_id_t &id, db::SomeDevice db_device) noexcept;
|
||||
struct SYNCSPIRIT_API add_ignored_device_t final : cluster_aggregate_diff_t {
|
||||
using parent_t = cluster_aggregate_diff_t;
|
||||
|
||||
add_ignored_device_t(const cluster_t &cluster, const device_id_t &id, db::SomeDevice db_device) noexcept;
|
||||
outcome::result<void> apply_impl(cluster_t &) const noexcept override;
|
||||
outcome::result<void> visit(cluster_visitor_t &, void *) const noexcept override;
|
||||
|
||||
|
||||
@@ -9,7 +9,7 @@
|
||||
namespace syncspirit::model::diff::modify {
|
||||
|
||||
struct SYNCSPIRIT_API remove_unknown_device_t final : cluster_diff_t {
|
||||
remove_unknown_device_t(const unknown_device_t &id) noexcept;
|
||||
remove_unknown_device_t(const unknown_device_t &device) noexcept;
|
||||
outcome::result<void> apply_impl(cluster_t &) const noexcept override;
|
||||
outcome::result<void> visit(cluster_visitor_t &, void *) const noexcept override;
|
||||
|
||||
|
||||
@@ -438,7 +438,7 @@ auto db_actor_t::operator()(const model::diff::modify::add_unknown_folders_t &di
|
||||
return commit(true);
|
||||
}
|
||||
|
||||
auto db_actor_t::operator()(const model::diff::modify::add_ignored_device_t &diff, void *) noexcept
|
||||
auto db_actor_t::operator()(const model::diff::modify::add_ignored_device_t &diff, void *custom) noexcept
|
||||
-> outcome::result<void> {
|
||||
if (cluster->is_tainted()) {
|
||||
return outcome::success();
|
||||
@@ -448,12 +448,17 @@ auto db_actor_t::operator()(const model::diff::modify::add_ignored_device_t &dif
|
||||
return txn_opt.assume_error();
|
||||
}
|
||||
auto &txn = *txn_opt.assume_value();
|
||||
auto device = cluster->get_ignored_devices().by_sha256(diff.device_id.get_sha256());
|
||||
|
||||
auto r = diff.model::diff::cluster_aggregate_diff_t::visit(*this, custom);
|
||||
if (r.has_error()) {
|
||||
return r.assume_error();
|
||||
}
|
||||
|
||||
auto device = cluster->get_ignored_devices().by_sha256(diff.device_id.get_sha256());
|
||||
auto key = device->get_key();
|
||||
auto data = device->serialize();
|
||||
|
||||
auto r = db::save({key, data}, txn);
|
||||
r = db::save({key, data}, txn);
|
||||
if (!r) {
|
||||
return r.assume_error();
|
||||
}
|
||||
|
||||
@@ -102,3 +102,26 @@ TEST_CASE("ignored device is removed when connecting to it ", "[model]") {
|
||||
CHECK(cluster->get_ignored_devices().size() == 0);
|
||||
CHECK(cluster->get_devices().size() == 2);
|
||||
}
|
||||
|
||||
TEST_CASE("unknown device is removed adding the same ignored device", "[model]") {
|
||||
auto my_id = device_id_t::from_string("KHQNO2S-5QSILRK-YX4JZZ4-7L77APM-QNVGZJT-EKU7IFI-PNEPBMY-4MXFMQD").value();
|
||||
auto peer_id = device_id_t::from_string("VUV42CZ-IQD5A37-RPEBPM4-VVQK6E4-6WSKC7B-PVJQHHD-4PZD44V-ENC6WAZ").value();
|
||||
auto my_device = device_t::create(my_id, "my-device").value();
|
||||
|
||||
auto cluster = cluster_ptr_t(new cluster_t(my_device, 1, 1));
|
||||
auto &devices = cluster->get_devices();
|
||||
devices.put(my_device);
|
||||
|
||||
db::SomeDevice db_device;
|
||||
db_device.set_name("a name");
|
||||
auto buider = diff_builder_t(*cluster);
|
||||
REQUIRE(buider.add_unknown_device(peer_id, db_device).apply());
|
||||
REQUIRE(cluster->get_unknown_devices().size() == 1);
|
||||
REQUIRE(cluster->get_ignored_devices().size() == 0);
|
||||
REQUIRE(cluster->get_devices().size() == 1);
|
||||
|
||||
REQUIRE(buider.add_ignored_device(peer_id, db_device).apply());
|
||||
REQUIRE(cluster->get_unknown_devices().size() == 0);
|
||||
REQUIRE(cluster->get_ignored_devices().size() == 1);
|
||||
REQUIRE(cluster->get_devices().size() == 1);
|
||||
}
|
||||
|
||||
+39
-2
@@ -180,7 +180,7 @@ void test_folder_creation() {
|
||||
F().run();
|
||||
}
|
||||
|
||||
void test_miscellaneous() {
|
||||
void test_unknown_and_ignored_devices_1() {
|
||||
struct F : fixture_t {
|
||||
void main() noexcept override {
|
||||
auto d_id1 =
|
||||
@@ -255,6 +255,42 @@ void test_miscellaneous() {
|
||||
F().run();
|
||||
}
|
||||
|
||||
void test_unknown_and_ignored_devices_2() {
|
||||
struct F : fixture_t {
|
||||
void main() noexcept override {
|
||||
auto d_id =
|
||||
device_id_t::from_string("LYXKCHX-VI3NYZR-ALCJBHF-WMZYSPK-QG6QJA3-MPFYMSO-U56GTUK-NA2MIAW").value();
|
||||
|
||||
db::SomeDevice sd;
|
||||
sd.set_name("x1");
|
||||
auto builder = diff_builder_t(*cluster);
|
||||
|
||||
builder.add_unknown_device(d_id, sd).apply(*sup);
|
||||
{
|
||||
sup->request<net::payload::load_cluster_request_t>(db_addr).send(timeout);
|
||||
sup->do_process();
|
||||
REQUIRE(reply);
|
||||
auto cluster_clone = make_cluster();
|
||||
REQUIRE(reply->payload.res.diff->apply(*cluster_clone));
|
||||
CHECK(cluster_clone->get_unknown_devices().by_sha256(d_id.get_sha256()));
|
||||
CHECK(!cluster_clone->get_ignored_devices().by_sha256(d_id.get_sha256()));
|
||||
}
|
||||
|
||||
builder.add_ignored_device(d_id, sd).apply(*sup);
|
||||
{
|
||||
sup->request<net::payload::load_cluster_request_t>(db_addr).send(timeout);
|
||||
sup->do_process();
|
||||
REQUIRE(reply);
|
||||
auto cluster_clone = make_cluster();
|
||||
REQUIRE(reply->payload.res.diff->apply(*cluster_clone));
|
||||
CHECK(!cluster_clone->get_unknown_devices().by_sha256(d_id.get_sha256()));
|
||||
CHECK(cluster_clone->get_ignored_devices().by_sha256(d_id.get_sha256()));
|
||||
}
|
||||
}
|
||||
};
|
||||
F().run();
|
||||
}
|
||||
|
||||
void test_peer_updating() {
|
||||
struct F : fixture_t {
|
||||
void main() noexcept override {
|
||||
@@ -810,7 +846,8 @@ void test_update_peer() {
|
||||
|
||||
int _init() {
|
||||
REGISTER_TEST_CASE(test_loading_empty_db, "test_loading_empty_db", "[db]");
|
||||
REGISTER_TEST_CASE(test_miscellaneous, "test_miscellaneous", "[db]");
|
||||
REGISTER_TEST_CASE(test_unknown_and_ignored_devices_1, "test_unknown_and_ignored_devices_1", "[db]");
|
||||
REGISTER_TEST_CASE(test_unknown_and_ignored_devices_2, "test_unknown_and_ignored_devices_2", "[db]");
|
||||
REGISTER_TEST_CASE(test_folder_creation, "test_folder_creation", "[db]");
|
||||
REGISTER_TEST_CASE(test_peer_updating, "test_peer_updating", "[db]");
|
||||
REGISTER_TEST_CASE(test_folder_sharing, "test_folder_sharing", "[db]");
|
||||
|
||||
@@ -235,7 +235,7 @@ diff_builder_t &diff_builder_t::ack_block(const model::diff::modify::block_trans
|
||||
|
||||
diff_builder_t &diff_builder_t::add_ignored_device(const model::device_id_t &device,
|
||||
db::SomeDevice db_device) noexcept {
|
||||
diffs.emplace_back(new diff::modify::add_ignored_device_t(device, db_device));
|
||||
diffs.emplace_back(new diff::modify::add_ignored_device_t(cluster, device, db_device));
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user