rippled
Loading...
Searching...
No Matches
LedgerStateFixTests.cpp
1// Auto-generated unit tests for transaction LedgerStateFix
2
3
4#include <gtest/gtest.h>
5
6#include <protocol_autogen/TestHelpers.h>
7
8#include <xrpl/protocol/SecretKey.h>
9#include <xrpl/protocol/Seed.h>
10#include <xrpl/protocol/STTx.h>
11#include <xrpl/protocol_autogen/transactions/LedgerStateFix.h>
12#include <xrpl/protocol_autogen/transactions/AccountSet.h>
13
14#include <string>
15
16namespace xrpl::transactions {
17
18// 1 & 4) Set fields via builder setters, build, then read them back via
19// wrapper getters. After build(), validate() should succeed.
20TEST(TransactionsLedgerStateFixTests, BuilderSettersRoundTrip)
21{
22 // Generate a deterministic keypair for signing
23 auto const [publicKey, secretKey] =
24 generateKeyPair(KeyType::secp256k1, generateSeed("testLedgerStateFix"));
25
26 // Common transaction fields
27 auto const accountValue = calcAccountID(publicKey);
28 std::uint32_t const sequenceValue = 1;
29 auto const feeValue = canonical_AMOUNT();
30
31 // Transaction-specific field values
32 auto const ledgerFixTypeValue = canonical_UINT16();
33 auto const ownerValue = canonical_ACCOUNT();
34
36 accountValue,
37 ledgerFixTypeValue,
38 sequenceValue,
39 feeValue
40 };
41
42 // Set optional fields
43 builder.setOwner(ownerValue);
44
45 auto tx = builder.build(publicKey, secretKey);
46
47 std::string reason;
48 EXPECT_TRUE(tx.validate(reason)) << reason;
49
50 // Verify signing was applied
51 EXPECT_FALSE(tx.getSigningPubKey().empty());
52 EXPECT_TRUE(tx.hasTxnSignature());
53
54 // Verify common fields
55 EXPECT_EQ(tx.getAccount(), accountValue);
56 EXPECT_EQ(tx.getSequence(), sequenceValue);
57 EXPECT_EQ(tx.getFee(), feeValue);
58
59 // Verify required fields
60 {
61 auto const& expected = ledgerFixTypeValue;
62 auto const actual = tx.getLedgerFixType();
63 expectEqualField(expected, actual, "sfLedgerFixType");
64 }
65
66 // Verify optional fields
67 {
68 auto const& expected = ownerValue;
69 auto const actualOpt = tx.getOwner();
70 ASSERT_TRUE(actualOpt.has_value()) << "Optional field sfOwner should be present";
71 expectEqualField(expected, *actualOpt, "sfOwner");
72 EXPECT_TRUE(tx.hasOwner());
73 }
74
75}
76
77// 2 & 4) Start from an STTx, construct a builder from it, build a new wrapper,
78// and verify all fields match.
79TEST(TransactionsLedgerStateFixTests, BuilderFromStTxRoundTrip)
80{
81 // Generate a deterministic keypair for signing
82 auto const [publicKey, secretKey] =
83 generateKeyPair(KeyType::secp256k1, generateSeed("testLedgerStateFixFromTx"));
84
85 // Common transaction fields
86 auto const accountValue = calcAccountID(publicKey);
87 std::uint32_t const sequenceValue = 2;
88 auto const feeValue = canonical_AMOUNT();
89
90 // Transaction-specific field values
91 auto const ledgerFixTypeValue = canonical_UINT16();
92 auto const ownerValue = canonical_ACCOUNT();
93
94 // Build an initial transaction
95 LedgerStateFixBuilder initialBuilder{
96 accountValue,
97 ledgerFixTypeValue,
98 sequenceValue,
99 feeValue
100 };
101
102 initialBuilder.setOwner(ownerValue);
103
104 auto initialTx = initialBuilder.build(publicKey, secretKey);
105
106 // Create builder from existing STTx
107 LedgerStateFixBuilder builderFromTx{initialTx.getSTTx()};
108
109 auto rebuiltTx = builderFromTx.build(publicKey, secretKey);
110
111 std::string reason;
112 EXPECT_TRUE(rebuiltTx.validate(reason)) << reason;
113
114 // Verify common fields
115 EXPECT_EQ(rebuiltTx.getAccount(), accountValue);
116 EXPECT_EQ(rebuiltTx.getSequence(), sequenceValue);
117 EXPECT_EQ(rebuiltTx.getFee(), feeValue);
118
119 // Verify required fields
120 {
121 auto const& expected = ledgerFixTypeValue;
122 auto const actual = rebuiltTx.getLedgerFixType();
123 expectEqualField(expected, actual, "sfLedgerFixType");
124 }
125
126 // Verify optional fields
127 {
128 auto const& expected = ownerValue;
129 auto const actualOpt = rebuiltTx.getOwner();
130 ASSERT_TRUE(actualOpt.has_value()) << "Optional field sfOwner should be present";
131 expectEqualField(expected, *actualOpt, "sfOwner");
132 }
133
134}
135
136// 3) Verify wrapper throws when constructed from wrong transaction type.
137TEST(TransactionsLedgerStateFixTests, WrapperThrowsOnWrongTxType)
138{
139 // Build a valid transaction of a different type
140 auto const [pk, sk] =
142 auto const account = calcAccountID(pk);
143
144 AccountSetBuilder wrongBuilder{account, 1, canonical_AMOUNT()};
145 auto wrongTx = wrongBuilder.build(pk, sk);
146
147 EXPECT_THROW(LedgerStateFix{wrongTx.getSTTx()}, std::runtime_error);
148}
149
150// 4) Verify builder throws when constructed from wrong transaction type.
151TEST(TransactionsLedgerStateFixTests, BuilderThrowsOnWrongTxType)
152{
153 // Build a valid transaction of a different type
154 auto const [pk, sk] =
155 generateKeyPair(KeyType::secp256k1, generateSeed("testWrongTypeBuilder"));
156 auto const account = calcAccountID(pk);
157
158 AccountSetBuilder wrongBuilder{account, 1, canonical_AMOUNT()};
159 auto wrongTx = wrongBuilder.build(pk, sk);
160
161 EXPECT_THROW(LedgerStateFixBuilder{wrongTx.getSTTx()}, std::runtime_error);
162}
163
164// 5) Build with only required fields and verify optional fields return nullopt.
165TEST(TransactionsLedgerStateFixTests, OptionalFieldsReturnNullopt)
166{
167 // Generate a deterministic keypair for signing
168 auto const [publicKey, secretKey] =
169 generateKeyPair(KeyType::secp256k1, generateSeed("testLedgerStateFixNullopt"));
170
171 // Common transaction fields
172 auto const accountValue = calcAccountID(publicKey);
173 std::uint32_t const sequenceValue = 3;
174 auto const feeValue = canonical_AMOUNT();
175
176 // Transaction-specific required field values
177 auto const ledgerFixTypeValue = canonical_UINT16();
178
179 LedgerStateFixBuilder builder{
180 accountValue,
181 ledgerFixTypeValue,
182 sequenceValue,
183 feeValue
184 };
185
186 // Do NOT set optional fields
187
188 auto tx = builder.build(publicKey, secretKey);
189
190 // Verify optional fields are not present
191 EXPECT_FALSE(tx.hasOwner());
192 EXPECT_FALSE(tx.getOwner().has_value());
193}
194
195}
LedgerStateFix build(PublicKey const &publicKey, SecretKey const &secretKey)
Build and return the LedgerStateFix wrapper.
std::shared_ptr< STTx const > getSTTx() const
Get the underlying STTx object.
TEST(TransactionsAccountDeleteTests, BuilderSettersRoundTrip)
Seed generateSeed(std::string const &passPhrase)
Generate a seed deterministically.
Definition Seed.cpp:57
std::pair< PublicKey, SecretKey > generateKeyPair(KeyType type, Seed const &seed)
Generate a key pair deterministically.
AccountID calcAccountID(PublicKey const &pk)
void expectEqualField(T const &expected, T const &actual, char const *fieldName)