rippled
Loading...
Searching...
No Matches
NegativeUNLTests.cpp
1// Auto-generated unit tests for ledger entry NegativeUNL
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/NegativeUNL.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(NegativeUNLTests, BuilderSettersRoundTrip)
20{
21 uint256 const index{1u};
22
23 auto const disabledValidatorsValue = canonical_ARRAY();
24 auto const validatorToDisableValue = canonical_VL();
25 auto const validatorToReEnableValue = canonical_VL();
26 auto const previousTxnIDValue = canonical_UINT256();
27 auto const previousTxnLgrSeqValue = canonical_UINT32();
28
29 NegativeUNLBuilder builder{
30 };
31
32 builder.setDisabledValidators(disabledValidatorsValue);
33 builder.setValidatorToDisable(validatorToDisableValue);
34 builder.setValidatorToReEnable(validatorToReEnableValue);
35 builder.setPreviousTxnID(previousTxnIDValue);
36 builder.setPreviousTxnLgrSeq(previousTxnLgrSeqValue);
37
38 builder.setLedgerIndex(index);
39 builder.setFlags(0x1u);
40
41 EXPECT_TRUE(builder.validate());
42
43 auto const entry = builder.build(index);
44
45 EXPECT_TRUE(entry.validate());
46
47 {
48 auto const& expected = disabledValidatorsValue;
49 auto const actualOpt = entry.getDisabledValidators();
50 ASSERT_TRUE(actualOpt.has_value());
51 expectEqualField(expected, *actualOpt, "sfDisabledValidators");
52 EXPECT_TRUE(entry.hasDisabledValidators());
53 }
54
55 {
56 auto const& expected = validatorToDisableValue;
57 auto const actualOpt = entry.getValidatorToDisable();
58 ASSERT_TRUE(actualOpt.has_value());
59 expectEqualField(expected, *actualOpt, "sfValidatorToDisable");
60 EXPECT_TRUE(entry.hasValidatorToDisable());
61 }
62
63 {
64 auto const& expected = validatorToReEnableValue;
65 auto const actualOpt = entry.getValidatorToReEnable();
66 ASSERT_TRUE(actualOpt.has_value());
67 expectEqualField(expected, *actualOpt, "sfValidatorToReEnable");
68 EXPECT_TRUE(entry.hasValidatorToReEnable());
69 }
70
71 {
72 auto const& expected = previousTxnIDValue;
73 auto const actualOpt = entry.getPreviousTxnID();
74 ASSERT_TRUE(actualOpt.has_value());
75 expectEqualField(expected, *actualOpt, "sfPreviousTxnID");
76 EXPECT_TRUE(entry.hasPreviousTxnID());
77 }
78
79 {
80 auto const& expected = previousTxnLgrSeqValue;
81 auto const actualOpt = entry.getPreviousTxnLgrSeq();
82 ASSERT_TRUE(actualOpt.has_value());
83 expectEqualField(expected, *actualOpt, "sfPreviousTxnLgrSeq");
84 EXPECT_TRUE(entry.hasPreviousTxnLgrSeq());
85 }
86
87 EXPECT_TRUE(entry.hasLedgerIndex());
88 auto const ledgerIndex = entry.getLedgerIndex();
89 ASSERT_TRUE(ledgerIndex.has_value());
90 EXPECT_EQ(*ledgerIndex, index);
91 EXPECT_EQ(entry.getKey(), index);
92}
93
94// 2 & 4) Start from an SLE, set fields directly on it, construct a builder
95// from that SLE, build a new wrapper, and verify all fields (and validate()).
96TEST(NegativeUNLTests, BuilderFromSleRoundTrip)
97{
98 uint256 const index{2u};
99
100 auto const disabledValidatorsValue = canonical_ARRAY();
101 auto const validatorToDisableValue = canonical_VL();
102 auto const validatorToReEnableValue = canonical_VL();
103 auto const previousTxnIDValue = canonical_UINT256();
104 auto const previousTxnLgrSeqValue = canonical_UINT32();
105
107
108 sle->setFieldArray(sfDisabledValidators, disabledValidatorsValue);
109 sle->at(sfValidatorToDisable) = validatorToDisableValue;
110 sle->at(sfValidatorToReEnable) = validatorToReEnableValue;
111 sle->at(sfPreviousTxnID) = previousTxnIDValue;
112 sle->at(sfPreviousTxnLgrSeq) = previousTxnLgrSeqValue;
113
114 NegativeUNLBuilder builderFromSle{sle};
115 EXPECT_TRUE(builderFromSle.validate());
116
117 auto const entryFromBuilder = builderFromSle.build(index);
118
119 NegativeUNL entryFromSle{sle};
120 EXPECT_TRUE(entryFromBuilder.validate());
121 EXPECT_TRUE(entryFromSle.validate());
122
123 {
124 auto const& expected = disabledValidatorsValue;
125
126 auto const fromSleOpt = entryFromSle.getDisabledValidators();
127 auto const fromBuilderOpt = entryFromBuilder.getDisabledValidators();
128
129 ASSERT_TRUE(fromSleOpt.has_value());
130 ASSERT_TRUE(fromBuilderOpt.has_value());
131
132 expectEqualField(expected, *fromSleOpt, "sfDisabledValidators");
133 expectEqualField(expected, *fromBuilderOpt, "sfDisabledValidators");
134 }
135
136 {
137 auto const& expected = validatorToDisableValue;
138
139 auto const fromSleOpt = entryFromSle.getValidatorToDisable();
140 auto const fromBuilderOpt = entryFromBuilder.getValidatorToDisable();
141
142 ASSERT_TRUE(fromSleOpt.has_value());
143 ASSERT_TRUE(fromBuilderOpt.has_value());
144
145 expectEqualField(expected, *fromSleOpt, "sfValidatorToDisable");
146 expectEqualField(expected, *fromBuilderOpt, "sfValidatorToDisable");
147 }
148
149 {
150 auto const& expected = validatorToReEnableValue;
151
152 auto const fromSleOpt = entryFromSle.getValidatorToReEnable();
153 auto const fromBuilderOpt = entryFromBuilder.getValidatorToReEnable();
154
155 ASSERT_TRUE(fromSleOpt.has_value());
156 ASSERT_TRUE(fromBuilderOpt.has_value());
157
158 expectEqualField(expected, *fromSleOpt, "sfValidatorToReEnable");
159 expectEqualField(expected, *fromBuilderOpt, "sfValidatorToReEnable");
160 }
161
162 {
163 auto const& expected = previousTxnIDValue;
164
165 auto const fromSleOpt = entryFromSle.getPreviousTxnID();
166 auto const fromBuilderOpt = entryFromBuilder.getPreviousTxnID();
167
168 ASSERT_TRUE(fromSleOpt.has_value());
169 ASSERT_TRUE(fromBuilderOpt.has_value());
170
171 expectEqualField(expected, *fromSleOpt, "sfPreviousTxnID");
172 expectEqualField(expected, *fromBuilderOpt, "sfPreviousTxnID");
173 }
174
175 {
176 auto const& expected = previousTxnLgrSeqValue;
177
178 auto const fromSleOpt = entryFromSle.getPreviousTxnLgrSeq();
179 auto const fromBuilderOpt = entryFromBuilder.getPreviousTxnLgrSeq();
180
181 ASSERT_TRUE(fromSleOpt.has_value());
182 ASSERT_TRUE(fromBuilderOpt.has_value());
183
184 expectEqualField(expected, *fromSleOpt, "sfPreviousTxnLgrSeq");
185 expectEqualField(expected, *fromBuilderOpt, "sfPreviousTxnLgrSeq");
186 }
187
188 EXPECT_EQ(entryFromSle.getKey(), index);
189 EXPECT_EQ(entryFromBuilder.getKey(), index);
190}
191
192// 3) Verify wrapper throws when constructed from wrong ledger entry type.
193TEST(NegativeUNLTests, WrapperThrowsOnWrongEntryType)
194{
195 uint256 const index{3u};
196
197 // Build a valid ledger entry of a different type
198 // Ticket requires: Account, OwnerNode, TicketSequence, PreviousTxnID, PreviousTxnLgrSeq
199 // Check requires: Account, Destination, SendMax, Sequence, OwnerNode, DestinationNode, PreviousTxnID, PreviousTxnLgrSeq
200 TicketBuilder wrongBuilder{
206 auto wrongEntry = wrongBuilder.build(index);
207
208 EXPECT_THROW(NegativeUNL{wrongEntry.getSle()}, std::runtime_error);
209}
210
211// 4) Verify builder throws when constructed from wrong ledger entry type.
212TEST(NegativeUNLTests, BuilderThrowsOnWrongEntryType)
213{
214 uint256 const index{4u};
215
216 // Build a valid ledger entry of a different type
217 TicketBuilder wrongBuilder{
223 auto wrongEntry = wrongBuilder.build(index);
224
225 EXPECT_THROW(NegativeUNLBuilder{wrongEntry.getSle()}, std::runtime_error);
226}
227
228// 5) Build with only required fields and verify optional fields return nullopt.
229TEST(NegativeUNLTests, OptionalFieldsReturnNullopt)
230{
231 uint256 const index{3u};
232
233
234 NegativeUNLBuilder builder{
235 };
236
237 auto const entry = builder.build(index);
238
239 // Verify optional fields are not present
240 EXPECT_FALSE(entry.hasDisabledValidators());
241 EXPECT_FALSE(entry.getDisabledValidators().has_value());
242 EXPECT_FALSE(entry.hasValidatorToDisable());
243 EXPECT_FALSE(entry.getValidatorToDisable().has_value());
244 EXPECT_FALSE(entry.hasValidatorToReEnable());
245 EXPECT_FALSE(entry.getValidatorToReEnable().has_value());
246 EXPECT_FALSE(entry.hasPreviousTxnID());
247 EXPECT_FALSE(entry.getPreviousTxnID().has_value());
248 EXPECT_FALSE(entry.hasPreviousTxnLgrSeq());
249 EXPECT_FALSE(entry.getPreviousTxnLgrSeq().has_value());
250}
251}
std::shared_ptr< SLE const > getSle() const
Get the underlying SLE object.
Derived & setLedgerIndex(uint256 const &value)
Set the ledger index.
Derived & setFlags(uint32_t value)
Set the flags.
Builder for NegativeUNL ledger entries.
NegativeUNLBuilder & setPreviousTxnID(std::decay_t< typename SF_UINT256::type::value_type > const &value)
Set sfPreviousTxnID (soeOPTIONAL)
NegativeUNLBuilder & setValidatorToDisable(std::decay_t< typename SF_VL::type::value_type > const &value)
Set sfValidatorToDisable (soeOPTIONAL)
NegativeUNLBuilder & setDisabledValidators(STArray const &value)
Ledger entry-specific field setters.
NegativeUNLBuilder & setPreviousTxnLgrSeq(std::decay_t< typename SF_UINT32::type::value_type > const &value)
Set sfPreviousTxnLgrSeq (soeOPTIONAL)
NegativeUNLBuilder & setValidatorToReEnable(std::decay_t< typename SF_VL::type::value_type > const &value)
Set sfValidatorToReEnable (soeOPTIONAL)
Ledger Entry: NegativeUNL.
Definition NegativeUNL.h:28
static constexpr LedgerEntryType entryType
Definition NegativeUNL.h:30
Builder for Ticket ledger entries.
Definition Ticket.h:112
T is_same_v
TEST(AccountRootTests, BuilderSettersRoundTrip)
void expectEqualField(T const &expected, T const &actual, char const *fieldName)