xrpld
Toggle main menu visibility
Loading...
Searching...
No Matches
tests
libxrpl
server
tests/libxrpl/server/InfoSub.cpp
1
#include <xrpl/server/InfoSub.h>
2
3
#include <gtest/gtest.h>
4
5
#include <
cstddef
>
6
#include <
limits
>
7
8
using namespace
xrpl
;
9
10
// The per-connection subscription cap is enforced by the pure predicate
11
// exceedsSubscriptionCap(current, additional). Testing it directly (rather than
12
// by subscribing the real cap through a WebSocket, which would exceed the frame
13
// limit and drop the connection before the check runs) lets the boundary be
14
// asserted exactly.
15
TEST
(InfoSubSubscriptionCap, boundary)
16
{
17
constexpr
std::size_t
cap =
kMaxSubscriptionsPerConnection
;
18
19
// Empty connection: anything up to the cap is admitted, cap+1 is not.
20
EXPECT_FALSE(
exceedsSubscriptionCap
(0, 0));
21
EXPECT_FALSE(
exceedsSubscriptionCap
(0, cap));
22
EXPECT_TRUE(
exceedsSubscriptionCap
(0, cap + 1));
23
24
// Exactly at the cap: zero more is fine, one more is rejected.
25
EXPECT_FALSE(
exceedsSubscriptionCap
(cap, 0));
26
EXPECT_TRUE(
exceedsSubscriptionCap
(cap, 1));
27
28
// One below the cap: exactly one more reaches the cap; two exceed it.
29
EXPECT_FALSE(
exceedsSubscriptionCap
(cap - 1, 1));
30
EXPECT_TRUE(
exceedsSubscriptionCap
(cap - 1, 2));
31
}
32
33
TEST
(InfoSubSubscriptionCap, no_overflow)
34
{
35
constexpr
std::size_t
cap =
kMaxSubscriptionsPerConnection
;
36
constexpr
std::size_t
max
=
std::numeric_limits<std::size_t>::max
();
37
38
// current + additional must not wrap: a huge additional is rejected even
39
// when current is 0 (the additional > cap term guards the subtraction).
40
EXPECT_TRUE(
exceedsSubscriptionCap
(0, max));
41
EXPECT_TRUE(
exceedsSubscriptionCap
(cap, max));
42
}
43
44
TEST
(InfoSubSubscriptionCap, explicit_cap)
45
{
46
// A configured override is honored: the boundary tracks the passed cap, not
47
// the built-in default. This is the seam doSubscribe uses to enforce a
48
// per-connection cap set via [max_subscriptions_per_connection].
49
constexpr
std::size_t
cap = 5;
50
51
EXPECT_FALSE(
exceedsSubscriptionCap
(0, cap, cap));
52
EXPECT_TRUE(
exceedsSubscriptionCap
(0, cap + 1, cap));
53
EXPECT_FALSE(
exceedsSubscriptionCap
(cap, 0, cap));
54
EXPECT_TRUE(
exceedsSubscriptionCap
(cap, 1, cap));
55
EXPECT_FALSE(
exceedsSubscriptionCap
(cap - 1, 1, cap));
56
EXPECT_TRUE(
exceedsSubscriptionCap
(cap - 1, 2, cap));
57
58
// The overflow guard still holds with a small explicit cap.
59
EXPECT_TRUE(
exceedsSubscriptionCap
(0,
std::numeric_limits<std::size_t>::max
(), cap));
60
}
cstddef
limits
std::max
T max(T... args)
xrpl
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition
algorithm.h:5
xrpl::TEST
TEST(FileUtilitiesTest, get_file_contents)
Definition
tests/libxrpl/basics/FileUtilities.cpp:60
xrpl::kMaxSubscriptionsPerConnection
constexpr std::size_t kMaxSubscriptionsPerConnection
Maximum number of subscriptions a single client connection may hold at once.
Definition
InfoSub.h:36
xrpl::exceedsSubscriptionCap
constexpr bool exceedsSubscriptionCap(std::size_t current, std::size_t additional, std::size_t cap=kMaxSubscriptionsPerConnection)
Whether adding additional subscriptions to a connection already holding current would exceed the cap.
Definition
InfoSub.h:52
std::size_t
Generated by
1.17.0