Page MenuHomePhorge

No OneTemporary

Size
9 KB
Referenced Files
None
Subscribers
None
diff --git a/src/client/verification-strategy.cpp b/src/client/verification-strategy.cpp
index b6a3e8d..c61b456 100644
--- a/src/client/verification-strategy.cpp
+++ b/src/client/verification-strategy.cpp
@@ -1,56 +1,101 @@
/*
* This file is part of libkazv.
* SPDX-FileCopyrightText: 2021 Tusooa Zhu <tusooa@kazv.moe>
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
#include <libkazv-config.hpp>
#include <immer/algorithm.hpp>
#include "cursorutil.hpp"
#include "verification-strategy.hpp"
namespace Kazv
{
immer::flex_vector<std::string> devicesToSend(VerificationStrategy strategy, DeviceMap devMap)
{
auto extractDevInfoF = [](auto node) { return node.second; };
auto extractDevInfo = zug::map(extractDevInfoF);
auto notBlockedP = [](auto dev) {
return dev.trustLevel > Blocked;
};
auto verifiedP = [](auto dev) {
return dev.trustLevel >= Verified;
};
auto notVerifiedP = [verifiedP](auto dev) { return !verifiedP(std::move(dev)); };
auto devInfoToId = zug::map([](auto dev) { return dev.deviceId; });
if (strategy == TrustAllStrategy) {
return intoImmer(immer::flex_vector<std::string>{},
extractDevInfo | zug::filter(notBlockedP) | devInfoToId,
devMap);
} else if (strategy == VerifyAllStrategy) {
return intoImmer(immer::flex_vector<std::string>{},
extractDevInfo | zug::filter(verifiedP) | devInfoToId,
devMap);
} else if (strategy == TrustIfNeverVerifiedStrategy) {
if (std::all_of(devMap.begin(), devMap.end(),
[=](auto node) {
return notVerifiedP(extractDevInfoF(std::move(node)));
})) {
return intoImmer(immer::flex_vector<std::string>{},
extractDevInfo | zug::filter(notBlockedP) | devInfoToId, devMap);
} else {
return intoImmer(immer::flex_vector<std::string>{},
extractDevInfo | zug::filter(verifiedP) | devInfoToId, devMap);
}
} else {
assert(false);
return {};
}
}
+
+ immer::flex_vector<std::string> unknownDevices(VerificationStrategy strategy, DeviceMap devMap)
+ {
+ auto extractDevInfoF = [](auto node) { return node.second; };
+ auto extractDevInfo = zug::map(extractDevInfoF);
+ auto notBlockedP = [](auto dev) {
+ return dev.trustLevel > Blocked;
+ };
+ auto verifiedP = [](auto dev) {
+ return dev.trustLevel >= Verified;
+ };
+ auto unseenP = [](auto dev) {
+ return dev.trustLevel == Unseen;
+ };
+
+ auto notVerifiedP = [verifiedP](auto dev) { return !verifiedP(std::move(dev)); };
+
+ auto devInfoToId = zug::map([](auto dev) { return dev.deviceId; });
+
+ if (strategy == TrustAllStrategy) {
+ return {};
+ } else if (strategy == VerifyAllStrategy) {
+ return intoImmer(
+ immer::flex_vector<std::string>{},
+ extractDevInfo | zug::filter(unseenP) | devInfoToId,
+ devMap
+ );
+ } else if (strategy == TrustIfNeverVerifiedStrategy) {
+ if (std::all_of(devMap.begin(), devMap.end(),
+ [=](auto node) {
+ return notVerifiedP(extractDevInfoF(std::move(node)));
+ })) {
+ return {};
+ } else {
+ return intoImmer(
+ immer::flex_vector<std::string>{},
+ extractDevInfo | zug::filter(unseenP) | devInfoToId,
+ devMap
+ );
+ }
+ } else {
+ assert(false);
+ return {};
+ }
+ }
}
diff --git a/src/client/verification-strategy.hpp b/src/client/verification-strategy.hpp
index e748711..e79abb0 100644
--- a/src/client/verification-strategy.hpp
+++ b/src/client/verification-strategy.hpp
@@ -1,28 +1,54 @@
/*
* This file is part of libkazv.
* SPDX-FileCopyrightText: 2021 Tusooa Zhu <tusooa@kazv.moe>
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
#pragma once
#include <libkazv-config.hpp>
#include <immer/flex_vector.hpp>
#include <immer/map.hpp>
#include "device-list-tracker.hpp"
namespace Kazv
{
+ /**
+ * A map from device id to DeviceKeyInfo
+ */
using DeviceMap = immer::map<std::string, DeviceKeyInfo>;
enum VerificationStrategy
{
+ /**
+ * Send to every not-blocked device, no device is unknown.
+ */
TrustAllStrategy,
+ /**
+ * Send to every verified device, unseen devices are unknown.
+ */
VerifyAllStrategy,
+ /**
+ * If we have not verified any device of a particular user,
+ * send to every non-blocked device, no device is unknown.
+ *
+ * If we have verified any device of a particular user,
+ * send to every verified device, unseen devices are unknown.
+ */
TrustIfNeverVerifiedStrategy,
};
+ /**
+ * Return the devices we should send messages to without making the user confirm manually
+ * among `devices`, according to `strategy`.
+ */
immer::flex_vector<std::string> devicesToSend(VerificationStrategy strategy, DeviceMap devices);
+
+ /**
+ * Return the devices we should make the user confirm manually before sending any messages
+ * among `devices`, according to `strategy`.
+ */
+ immer::flex_vector<std::string> unknownDevices(VerificationStrategy strategy, DeviceMap devices);
}
diff --git a/src/tests/client/verification-test.cpp b/src/tests/client/verification-test.cpp
index 022d875..b820515 100644
--- a/src/tests/client/verification-test.cpp
+++ b/src/tests/client/verification-test.cpp
@@ -1,64 +1,79 @@
/*
* This file is part of libkazv.
* SPDX-FileCopyrightText: 2021 Tusooa Zhu <tusooa@kazv.moe>
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
#include <libkazv-config.hpp>
#include <catch2/catch.hpp>
#include <verification-strategy.hpp>
-
-#include <sdk.hpp>
-#include <cprjobhandler.hpp>
-#include <lagerstoreeventemitter.hpp>
-#include <asio-promise-handler.hpp>
-#include <random-generator.hpp>
+#include <client-model.hpp>
using namespace Kazv;
using DeviceMapT = immer::map<std::string, DeviceKeyInfo>;
using DeviceIdList = immer::flex_vector<std::string>;
static DeviceKeyInfo genInfo(std::string id, DeviceTrustLevel level)
{
return {id, "", "", std::nullopt, level};
}
static bool isSuperset(DeviceIdList set, DeviceIdList pattern)
{
return immer::all_of(set.begin(), set.end(),
[pattern](std::string id) {
return std::find(pattern.begin(), pattern.end(), id) != pattern.end();
});
}
static bool isEquiv(DeviceIdList set, DeviceIdList pattern)
{
return isSuperset(set, pattern) && isSuperset(pattern, set);
}
+static DeviceMapT devMap1 =
+ DeviceMapT()
+ .set("foo", genInfo("foo", Unseen))
+ .set("bar", genInfo("bar", Seen))
+ .set("baz", genInfo("baz", Blocked))
+ .set("doge", genInfo("doge", Verified));
+
+static DeviceMapT devMap2 =
+ DeviceMapT()
+ .set("foo", genInfo("foo", Unseen))
+ .set("bar", genInfo("bar", Seen))
+ .set("baz", genInfo("baz", Blocked));
+
+static DeviceMapT devMap3 =
+ DeviceMapT()
+ .set("bar", genInfo("bar", Seen))
+ .set("baz", genInfo("baz", Blocked))
+ .set("doge", genInfo("doge", Verified));
+
+static DeviceMapT devMap4 =
+ DeviceMapT()
+ .set("bar", genInfo("bar", Seen))
+ .set("baz", genInfo("baz", Blocked));
+
+
TEST_CASE("verification strategies should work", "[client][verification]")
{
- DeviceMapT devMap1 =
- DeviceMapT()
- .set("foo", genInfo("foo", Unseen))
- .set("bar", genInfo("bar", Seen))
- .set("baz", genInfo("baz", Blocked))
- .set("doge", genInfo("doge", Verified));
-
- DeviceMapT devMap2 =
- DeviceMapT()
- .set("foo", genInfo("foo", Unseen))
- .set("bar", genInfo("bar", Seen))
- .set("baz", genInfo("baz", Blocked));
-
REQUIRE(isEquiv(devicesToSend(TrustAllStrategy, devMap1), {"foo", "bar", "doge"}));
REQUIRE(isEquiv(devicesToSend(VerifyAllStrategy, devMap1), {"doge"}));
REQUIRE(isEquiv(devicesToSend(TrustIfNeverVerifiedStrategy, devMap1), {"doge"}));
REQUIRE(isEquiv(devicesToSend(TrustIfNeverVerifiedStrategy, devMap2), {"foo", "bar"}));
+
+ REQUIRE(isEquiv(unknownDevices(TrustAllStrategy, devMap1), {}));
+
+ REQUIRE(isEquiv(unknownDevices(VerifyAllStrategy, devMap1), {"foo"}));
+
+ REQUIRE(isEquiv(unknownDevices(TrustIfNeverVerifiedStrategy, devMap1), {"foo"}));
+
+ REQUIRE(isEquiv(unknownDevices(TrustIfNeverVerifiedStrategy, devMap2), {}));
}

File Metadata

Mime Type
text/x-diff
Expires
Sun, Aug 9, 1:12 PM (1 d, 12 h)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
1724552
Default Alt Text
(9 KB)

Event Timeline