xrpld
Loading...
Searching...
No Matches
DelegateTests.cpp
1// Auto-generated unit tests for ledger entry Delegate
2
3
4#include <gtest/gtest.h>
5
6#include <protocol_autogen/TestHelpers.h>
7
8#include <xrpl/protocol/STLedgerEntry.h>
9#include <xrpl/protocol_autogen/ledger_entries/Delegate.h>
10#include <xrpl/protocol_autogen/ledger_entries/Ticket.h>
11
12#include <string>
13
14namespace xrpl::ledger_entries {
15
16// 1 & 4) Set fields via builder setters, build, then read them back via
17// wrapper getters. After build(), validate() should succeed for both the
18// builder's STObject and the wrapper's SLE.
19TEST(DelegateTests, BuilderSettersRoundTrip)
20{
21 uint256 const index{1u};
22
23 auto const accountValue = canonical_ACCOUNT();
24 auto const authorizeValue = canonical_ACCOUNT();
25 auto const permissionsValue = canonical_ARRAY();
26 auto const ownerNodeValue = canonical_UINT64();
27 auto const destinationNodeValue = canonical_UINT64();
28 auto const previousTxnIDValue = canonical_UINT256();
29 auto const previousTxnLgrSeqValue = canonical_UINT32();
30
31 DelegateBuilder builder{
32 accountValue,
33 authorizeValue,
34 permissionsValue,
35 ownerNodeValue,
36 previousTxnIDValue,
37 previousTxnLgrSeqValue
38 };
39
40 builder.setDestinationNode(destinationNodeValue);
41
42 builder.setLedgerIndex(index);
43 builder.setFlags(0x1u);
44
45 EXPECT_TRUE(builder.validate());
46
47 auto const entry = builder.build(index);
48
49 EXPECT_TRUE(entry.validate());
50
51 {
52 auto const& expected = accountValue;
53 auto const actual = entry.getAccount();
54 expectEqualField(expected, actual, "sfAccount");
55 }
56
57 {
58 auto const& expected = authorizeValue;
59 auto const actual = entry.getAuthorize();
60 expectEqualField(expected, actual, "sfAuthorize");
61 }
62
63 {
64 auto const& expected = permissionsValue;
65 auto const actual = entry.getPermissions();
66 expectEqualField(expected, actual, "sfPermissions");
67 }
68
69 {
70 auto const& expected = ownerNodeValue;
71 auto const actual = entry.getOwnerNode();
72 expectEqualField(expected, actual, "sfOwnerNode");
73 }
74
75 {
76 auto const& expected = previousTxnIDValue;
77 auto const actual = entry.getPreviousTxnID();
78 expectEqualField(expected, actual, "sfPreviousTxnID");
79 }
80
81 {
82 auto const& expected = previousTxnLgrSeqValue;
83 auto const actual = entry.getPreviousTxnLgrSeq();
84 expectEqualField(expected, actual, "sfPreviousTxnLgrSeq");
85 }
86
87 {
88 auto const& expected = destinationNodeValue;
89 auto const actualOpt = entry.getDestinationNode();
90 ASSERT_TRUE(actualOpt.has_value());
91 expectEqualField(expected, *actualOpt, "sfDestinationNode");
92 EXPECT_TRUE(entry.hasDestinationNode());
93 }
94
95 EXPECT_TRUE(entry.hasLedgerIndex());
96 auto const ledgerIndex = entry.getLedgerIndex();
97 ASSERT_TRUE(ledgerIndex.has_value());
98 EXPECT_EQ(*ledgerIndex, index);
99 EXPECT_EQ(entry.getKey(), index);
100}
101
102// 2 & 4) Start from an SLE, set fields directly on it, construct a builder
103// from that SLE, build a new wrapper, and verify all fields (and validate()).
104TEST(DelegateTests, BuilderFromSleRoundTrip)
105{
106 uint256 const index{2u};
107
108 auto const accountValue = canonical_ACCOUNT();
109 auto const authorizeValue = canonical_ACCOUNT();
110 auto const permissionsValue = canonical_ARRAY();
111 auto const ownerNodeValue = canonical_UINT64();
112 auto const destinationNodeValue = canonical_UINT64();
113 auto const previousTxnIDValue = canonical_UINT256();
114 auto const previousTxnLgrSeqValue = canonical_UINT32();
115
117
118 sle->at(sfAccount) = accountValue;
119 sle->at(sfAuthorize) = authorizeValue;
120 sle->setFieldArray(sfPermissions, permissionsValue);
121 sle->at(sfOwnerNode) = ownerNodeValue;
122 sle->at(sfDestinationNode) = destinationNodeValue;
123 sle->at(sfPreviousTxnID) = previousTxnIDValue;
124 sle->at(sfPreviousTxnLgrSeq) = previousTxnLgrSeqValue;
125
126 DelegateBuilder builderFromSle{sle};
127 EXPECT_TRUE(builderFromSle.validate());
128
129 auto const entryFromBuilder = builderFromSle.build(index);
130
131 Delegate entryFromSle{sle};
132 EXPECT_TRUE(entryFromBuilder.validate());
133 EXPECT_TRUE(entryFromSle.validate());
134
135 {
136 auto const& expected = accountValue;
137
138 auto const fromSle = entryFromSle.getAccount();
139 auto const fromBuilder = entryFromBuilder.getAccount();
140
141 expectEqualField(expected, fromSle, "sfAccount");
142 expectEqualField(expected, fromBuilder, "sfAccount");
143 }
144
145 {
146 auto const& expected = authorizeValue;
147
148 auto const fromSle = entryFromSle.getAuthorize();
149 auto const fromBuilder = entryFromBuilder.getAuthorize();
150
151 expectEqualField(expected, fromSle, "sfAuthorize");
152 expectEqualField(expected, fromBuilder, "sfAuthorize");
153 }
154
155 {
156 auto const& expected = permissionsValue;
157
158 auto const fromSle = entryFromSle.getPermissions();
159 auto const fromBuilder = entryFromBuilder.getPermissions();
160
161 expectEqualField(expected, fromSle, "sfPermissions");
162 expectEqualField(expected, fromBuilder, "sfPermissions");
163 }
164
165 {
166 auto const& expected = ownerNodeValue;
167
168 auto const fromSle = entryFromSle.getOwnerNode();
169 auto const fromBuilder = entryFromBuilder.getOwnerNode();
170
171 expectEqualField(expected, fromSle, "sfOwnerNode");
172 expectEqualField(expected, fromBuilder, "sfOwnerNode");
173 }
174
175 {
176 auto const& expected = previousTxnIDValue;
177
178 auto const fromSle = entryFromSle.getPreviousTxnID();
179 auto const fromBuilder = entryFromBuilder.getPreviousTxnID();
180
181 expectEqualField(expected, fromSle, "sfPreviousTxnID");
182 expectEqualField(expected, fromBuilder, "sfPreviousTxnID");
183 }
184
185 {
186 auto const& expected = previousTxnLgrSeqValue;
187
188 auto const fromSle = entryFromSle.getPreviousTxnLgrSeq();
189 auto const fromBuilder = entryFromBuilder.getPreviousTxnLgrSeq();
190
191 expectEqualField(expected, fromSle, "sfPreviousTxnLgrSeq");
192 expectEqualField(expected, fromBuilder, "sfPreviousTxnLgrSeq");
193 }
194
195 {
196 auto const& expected = destinationNodeValue;
197
198 auto const fromSleOpt = entryFromSle.getDestinationNode();
199 auto const fromBuilderOpt = entryFromBuilder.getDestinationNode();
200
201 ASSERT_TRUE(fromSleOpt.has_value());
202 ASSERT_TRUE(fromBuilderOpt.has_value());
203
204 expectEqualField(expected, *fromSleOpt, "sfDestinationNode");
205 expectEqualField(expected, *fromBuilderOpt, "sfDestinationNode");
206 }
207
208 EXPECT_EQ(entryFromSle.getKey(), index);
209 EXPECT_EQ(entryFromBuilder.getKey(), index);
210}
211
212// 3) Verify wrapper throws when constructed from wrong ledger entry type.
213TEST(DelegateTests, WrapperThrowsOnWrongEntryType)
214{
215 uint256 const index{3u};
216
217 // Build a valid ledger entry of a different type
218 // Ticket requires: Account, OwnerNode, TicketSequence, PreviousTxnID, PreviousTxnLgrSeq
219 // Check requires: Account, Destination, SendMax, Sequence, OwnerNode, DestinationNode, PreviousTxnID, PreviousTxnLgrSeq
220 TicketBuilder wrongBuilder{
226 auto wrongEntry = wrongBuilder.build(index);
227
228 EXPECT_THROW(Delegate{wrongEntry.getSle()}, std::runtime_error);
229}
230
231// 4) Verify builder throws when constructed from wrong ledger entry type.
232TEST(DelegateTests, BuilderThrowsOnWrongEntryType)
233{
234 uint256 const index{4u};
235
236 // Build a valid ledger entry of a different type
237 TicketBuilder wrongBuilder{
243 auto wrongEntry = wrongBuilder.build(index);
244
245 EXPECT_THROW(DelegateBuilder{wrongEntry.getSle()}, std::runtime_error);
246}
247
248// 5) Build with only required fields and verify optional fields return nullopt.
249TEST(DelegateTests, OptionalFieldsReturnNullopt)
250{
251 uint256 const index{3u};
252
253 auto const accountValue = canonical_ACCOUNT();
254 auto const authorizeValue = canonical_ACCOUNT();
255 auto const permissionsValue = canonical_ARRAY();
256 auto const ownerNodeValue = canonical_UINT64();
257 auto const previousTxnIDValue = canonical_UINT256();
258 auto const previousTxnLgrSeqValue = canonical_UINT32();
259
260 DelegateBuilder builder{
261 accountValue,
262 authorizeValue,
263 permissionsValue,
264 ownerNodeValue,
265 previousTxnIDValue,
266 previousTxnLgrSeqValue
267 };
268
269 auto const entry = builder.build(index);
270
271 // Verify optional fields are not present
272 EXPECT_FALSE(entry.hasDestinationNode());
273 EXPECT_FALSE(entry.getDestinationNode().has_value());
274}
275}
Builder for Delegate ledger entries.
Definition Delegate.h:148
Delegate build(uint256 const &index)
Build and return the completed Delegate wrapper.
Definition Delegate.h:269
DelegateBuilder & setDestinationNode(std::decay_t< typename SF_UINT64::type::value_type > const &value)
Set sfDestinationNode (SoeOptional).
Definition Delegate.h:235
Ledger Entry: Delegate.
Definition Delegate.h:28
SF_ACCOUNT::type::value_type getAccount() const
Get sfAccount (SoeRequired).
Definition Delegate.h:54
protocol_autogen::Optional< SF_UINT64::type::value_type > getDestinationNode() const
Get sfDestinationNode (SoeOptional).
Definition Delegate.h:99
SF_ACCOUNT::type::value_type getAuthorize() const
Get sfAuthorize (SoeRequired).
Definition Delegate.h:65
SF_UINT32::type::value_type getPreviousTxnLgrSeq() const
Get sfPreviousTxnLgrSeq (SoeRequired).
Definition Delegate.h:134
STArray const & getPermissions() const
Get sfPermissions (SoeRequired).
Definition Delegate.h:77
static constexpr LedgerEntryType entryType
Definition Delegate.h:30
SF_UINT64::type::value_type getOwnerNode() const
Get sfOwnerNode (SoeRequired).
Definition Delegate.h:88
SF_UINT256::type::value_type getPreviousTxnID() const
Get sfPreviousTxnID (SoeRequired).
Definition Delegate.h:123
bool validate() const
Validate the ledger entry.
uint256 const & getKey() const
Get the key (index) of this ledger entry.
Derived & setLedgerIndex(uint256 const &value)
Set the ledger index.
Derived & setFlags(uint32_t value)
Set the flags.
bool validate() const
Validate the ledger entry.
Builder for Ticket ledger entries.
Definition Ticket.h:112
Ticket build(uint256 const &index)
Build and return the completed Ticket wrapper.
Definition Ticket.h:209
T make_shared(T... args)
TEST(AccountRootTests, BuilderSettersRoundTrip)
BaseUInt< 256 > uint256
Definition base_uint.h:562
void expectEqualField(T const &expected, T const &actual, char const *fieldName)