Add reusable strict JSON, rate limiting, and sealed secrets
This commit is contained in:
10 files changed
+778
-40
No files matched your search
@@ -0,0 +1,41 @@
|
||||
#include <Void/Data/StrictJson.hpp>
|
||||
#include <Void/Testing/Assert.hpp>
|
||||
#include <Void/Testing/Test.hpp>
|
||||
|
||||
#include <cstdint>
|
||||
#include <string>
|
||||
#include <variant>
|
||||
|
||||
Void::Testing::Test strict_json_preserves_signed_64_bit_integers()
|
||||
{
|
||||
const auto result = Void::Json::Strict::parse(
|
||||
R"({"minimum":-9223372036854775808,"maximum":9223372036854775807})");
|
||||
Void::Testing::require(result.has_value());
|
||||
const auto* object =
|
||||
std::get_if<Void::Json::Strict::Value::Object>(&result->value);
|
||||
Void::Testing::require(object != nullptr);
|
||||
Void::Testing::expect_eq(
|
||||
std::get<std::int64_t>(object->at("minimum").value), INT64_MIN);
|
||||
Void::Testing::expect_eq(
|
||||
std::get<std::int64_t>(object->at("maximum").value), INT64_MAX);
|
||||
}
|
||||
|
||||
Void::Testing::Test strict_json_rejects_duplicates_floats_and_deep_inputs()
|
||||
{
|
||||
Void::Testing::expect(
|
||||
!Void::Json::Strict::parse(R"({"key":1,"key":2})").has_value());
|
||||
Void::Testing::expect(
|
||||
!Void::Json::Strict::parse(R"({"value":1.5})").has_value());
|
||||
Void::Testing::expect(
|
||||
!Void::Json::Strict::parse("[[[0]]]", 1U).has_value());
|
||||
}
|
||||
|
||||
Void::Testing::Test strict_json_decodes_valid_unicode_pairs()
|
||||
{
|
||||
const auto result = Void::Json::Strict::parse(R"("\uD83D\uDE03")");
|
||||
Void::Testing::require(result.has_value());
|
||||
Void::Testing::expect_eq(std::get<std::string>(result->value),
|
||||
std::string{"\xF0\x9F\x98\x83"});
|
||||
Void::Testing::expect(
|
||||
!Void::Json::Strict::parse(R"("\uD83D")").has_value());
|
||||
}
|
||||
@@ -35,7 +35,7 @@ namespace
|
||||
Void::Testing::expect(rl.allow(1));
|
||||
Void::Testing::expect(rl.allow(1));
|
||||
Void::Testing::expect(!rl.allow(1)); // key 1 exhausted
|
||||
Void::Testing::expect(rl.allow(2)); // key 2 still full
|
||||
Void::Testing::expect(rl.allow(2)); // key 2 still full
|
||||
}
|
||||
|
||||
Test refills_per_key_over_time()
|
||||
@@ -60,16 +60,43 @@ namespace
|
||||
|
||||
Test cleanup_drops_stale_keys()
|
||||
{
|
||||
KeyedRateLimiter<int> rl{1.0, 1.0};
|
||||
ManualClock clk;
|
||||
KeyedRateLimiter<int> rl{1.0, 1.0, clk.fn()};
|
||||
(void)rl.allow(1);
|
||||
(void)rl.allow(2);
|
||||
Void::Testing::expect_eq(rl.tracked(), std::size_t{2});
|
||||
// Nothing is older than an hour yet.
|
||||
Void::Testing::expect_eq(rl.cleanup(1h), std::size_t{0});
|
||||
Void::Testing::expect_eq(rl.tracked(), std::size_t{2});
|
||||
// Everything is older than 0ns.
|
||||
Void::Testing::expect_eq(rl.cleanup(0ns), std::size_t{2});
|
||||
clk.advance(2h);
|
||||
Void::Testing::expect_eq(rl.cleanup(1h), std::size_t{2});
|
||||
Void::Testing::expect_eq(rl.tracked(), std::size_t{0});
|
||||
}
|
||||
|
||||
Test bounds_untrusted_key_cardinality()
|
||||
{
|
||||
ManualClock clk;
|
||||
KeyedRateLimiter<std::string> rl{2.0, 0.0, clk.fn(),
|
||||
std::size_t{2}};
|
||||
Void::Testing::expect(rl.allow("a"));
|
||||
Void::Testing::expect(rl.allow("b"));
|
||||
Void::Testing::expect(!rl.allow("c"));
|
||||
Void::Testing::expect_eq(rl.tracked(), std::size_t{2});
|
||||
clk.advance(2min);
|
||||
Void::Testing::expect_eq(rl.cleanup(1min), std::size_t{2});
|
||||
Void::Testing::expect(rl.allow("c"));
|
||||
}
|
||||
|
||||
Test explicit_time_is_shared_by_tracking_and_refill()
|
||||
{
|
||||
ManualClock clk;
|
||||
KeyedRateLimiter<int> rl{1.0, 1.0, clk.fn(), std::size_t{1}};
|
||||
const TP start = *clk.now;
|
||||
Void::Testing::expect(rl.allow_at(1, start));
|
||||
Void::Testing::expect(!rl.allow_at(1, start));
|
||||
Void::Testing::expect(rl.allow_at(1, start + 1s));
|
||||
Void::Testing::expect_eq(rl.cleanup_at(start + 3s, 1s),
|
||||
std::size_t{1});
|
||||
}
|
||||
};
|
||||
} // namespace
|
||||
@@ -3,6 +3,7 @@
|
||||
#include <Void/Testing/Suite.hpp>
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace
|
||||
{
|
||||
@@ -91,5 +92,40 @@ namespace
|
||||
Void::Testing::expect(!*invalid);
|
||||
Void::Testing::expect(!*malformed);
|
||||
}
|
||||
|
||||
Test sealed_secrets_round_trip_and_use_random_nonces()
|
||||
{
|
||||
auto key = Void::Security::random_secret_key();
|
||||
Void::Testing::require(key.has_value());
|
||||
const std::vector<unsigned char> plaintext{'s', 'e', 'c',
|
||||
'r', 'e', 't'};
|
||||
auto first = Void::Security::seal_secret(plaintext, *key);
|
||||
auto second = Void::Security::seal_secret(plaintext, *key);
|
||||
Void::Testing::require(first.has_value());
|
||||
Void::Testing::require(second.has_value());
|
||||
Void::Testing::expect_ne(*first, *second);
|
||||
|
||||
auto opened = Void::Security::open_secret(*first, *key);
|
||||
Void::Testing::require(opened.has_value());
|
||||
Void::Testing::expect_eq(*opened, plaintext);
|
||||
}
|
||||
|
||||
Test sealed_secrets_reject_tampering_and_wrong_keys()
|
||||
{
|
||||
auto key = Void::Security::random_secret_key();
|
||||
auto wrong = Void::Security::random_secret_key();
|
||||
Void::Testing::require(key.has_value());
|
||||
Void::Testing::require(wrong.has_value());
|
||||
const std::vector<unsigned char> plaintext{'t', 'o', 'k', 'e',
|
||||
'n'};
|
||||
auto envelope = Void::Security::seal_secret(plaintext, *key);
|
||||
Void::Testing::require(envelope.has_value());
|
||||
|
||||
auto wrong_key = Void::Security::open_secret(*envelope, *wrong);
|
||||
Void::Testing::expect(!wrong_key.has_value());
|
||||
envelope->back() ^= 0x01U;
|
||||
auto tampered = Void::Security::open_secret(*envelope, *key);
|
||||
Void::Testing::expect(!tampered.has_value());
|
||||
}
|
||||
};
|
||||
} // namespace
|
||||
Reference in new issue
Block a user