xrpld
Loading...
Searching...
No Matches
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
8using 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.
15TEST(InfoSubSubscriptionCap, Boundary)
16{
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
33TEST(InfoSubSubscriptionCap, NoOverflow)
34{
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
44TEST(InfoSubSubscriptionCap, ExplicitCap)
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.
60}
T max(T... args)
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
TEST(FileUtilitiesTest, get_file_contents)
constexpr std::size_t kMaxSubscriptionsPerConnection
Maximum number of subscriptions a single client connection may hold at once.
Definition InfoSub.h:35
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:51