xrpld
Loading...
Searching...
No Matches
SponsorshipSetTests.cpp
1// Auto-generated unit tests for transaction SponsorshipSet
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/SponsorshipSet.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(TransactionsSponsorshipSetTests, BuilderSettersRoundTrip)
21{
22 // Generate a deterministic keypair for signing
23 auto const [publicKey, secretKey] =
24 generateKeyPair(KeyType::Secp256k1, generateSeed("testSponsorshipSet"));
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 counterpartySponsorValue = canonical_ACCOUNT();
33 auto const sponseeValue = canonical_ACCOUNT();
34 auto const feeAmountDeltaValue = canonical_AMOUNT();
35 auto const maxFeeValue = canonical_AMOUNT();
36 auto const remainingOwnerCountDeltaValue = canonical_INT32();
37
39 accountValue,
40 sequenceValue,
41 feeValue
42 };
43
44 // Set optional fields
45 builder.setCounterpartySponsor(counterpartySponsorValue);
46 builder.setSponsee(sponseeValue);
47 builder.setFeeAmountDelta(feeAmountDeltaValue);
48 builder.setMaxFee(maxFeeValue);
49 builder.setRemainingOwnerCountDelta(remainingOwnerCountDeltaValue);
50
51 auto tx = builder.build(publicKey, secretKey);
52
53 std::string reason;
54 EXPECT_TRUE(tx.validate(reason)) << reason;
55
56 // Verify signing was applied
57 EXPECT_FALSE(tx.getSigningPubKey().empty());
58 EXPECT_TRUE(tx.hasTxnSignature());
59
60 // Verify common fields
61 EXPECT_EQ(tx.getAccount(), accountValue);
62 EXPECT_EQ(tx.getSequence(), sequenceValue);
63 EXPECT_EQ(tx.getFee(), feeValue);
64
65 // Verify required fields
66 // Verify optional fields
67 {
68 auto const& expected = counterpartySponsorValue;
69 auto const actualOpt = tx.getCounterpartySponsor();
70 ASSERT_TRUE(actualOpt.has_value()) << "Optional field sfCounterpartySponsor should be present";
71 expectEqualField(expected, *actualOpt, "sfCounterpartySponsor");
72 EXPECT_TRUE(tx.hasCounterpartySponsor());
73 }
74
75 {
76 auto const& expected = sponseeValue;
77 auto const actualOpt = tx.getSponsee();
78 ASSERT_TRUE(actualOpt.has_value()) << "Optional field sfSponsee should be present";
79 expectEqualField(expected, *actualOpt, "sfSponsee");
80 EXPECT_TRUE(tx.hasSponsee());
81 }
82
83 {
84 auto const& expected = feeAmountDeltaValue;
85 auto const actualOpt = tx.getFeeAmountDelta();
86 ASSERT_TRUE(actualOpt.has_value()) << "Optional field sfFeeAmountDelta should be present";
87 expectEqualField(expected, *actualOpt, "sfFeeAmountDelta");
88 EXPECT_TRUE(tx.hasFeeAmountDelta());
89 }
90
91 {
92 auto const& expected = maxFeeValue;
93 auto const actualOpt = tx.getMaxFee();
94 ASSERT_TRUE(actualOpt.has_value()) << "Optional field sfMaxFee should be present";
95 expectEqualField(expected, *actualOpt, "sfMaxFee");
96 EXPECT_TRUE(tx.hasMaxFee());
97 }
98
99 {
100 auto const& expected = remainingOwnerCountDeltaValue;
101 auto const actualOpt = tx.getRemainingOwnerCountDelta();
102 ASSERT_TRUE(actualOpt.has_value()) << "Optional field sfRemainingOwnerCountDelta should be present";
103 expectEqualField(expected, *actualOpt, "sfRemainingOwnerCountDelta");
104 EXPECT_TRUE(tx.hasRemainingOwnerCountDelta());
105 }
106
107}
108
109// 2 & 4) Start from an STTx, construct a builder from it, build a new wrapper,
110// and verify all fields match.
111TEST(TransactionsSponsorshipSetTests, BuilderFromStTxRoundTrip)
112{
113 // Generate a deterministic keypair for signing
114 auto const [publicKey, secretKey] =
115 generateKeyPair(KeyType::Secp256k1, generateSeed("testSponsorshipSetFromTx"));
116
117 // Common transaction fields
118 auto const accountValue = calcAccountID(publicKey);
119 std::uint32_t const sequenceValue = 2;
120 auto const feeValue = canonical_AMOUNT();
121
122 // Transaction-specific field values
123 auto const counterpartySponsorValue = canonical_ACCOUNT();
124 auto const sponseeValue = canonical_ACCOUNT();
125 auto const feeAmountDeltaValue = canonical_AMOUNT();
126 auto const maxFeeValue = canonical_AMOUNT();
127 auto const remainingOwnerCountDeltaValue = canonical_INT32();
128
129 // Build an initial transaction
130 SponsorshipSetBuilder initialBuilder{
131 accountValue,
132 sequenceValue,
133 feeValue
134 };
135
136 initialBuilder.setCounterpartySponsor(counterpartySponsorValue);
137 initialBuilder.setSponsee(sponseeValue);
138 initialBuilder.setFeeAmountDelta(feeAmountDeltaValue);
139 initialBuilder.setMaxFee(maxFeeValue);
140 initialBuilder.setRemainingOwnerCountDelta(remainingOwnerCountDeltaValue);
141
142 auto initialTx = initialBuilder.build(publicKey, secretKey);
143
144 // Create builder from existing STTx
145 SponsorshipSetBuilder builderFromTx{initialTx.getSTTx()};
146
147 auto rebuiltTx = builderFromTx.build(publicKey, secretKey);
148
149 std::string reason;
150 EXPECT_TRUE(rebuiltTx.validate(reason)) << reason;
151
152 // Verify common fields
153 EXPECT_EQ(rebuiltTx.getAccount(), accountValue);
154 EXPECT_EQ(rebuiltTx.getSequence(), sequenceValue);
155 EXPECT_EQ(rebuiltTx.getFee(), feeValue);
156
157 // Verify required fields
158 // Verify optional fields
159 {
160 auto const& expected = counterpartySponsorValue;
161 auto const actualOpt = rebuiltTx.getCounterpartySponsor();
162 ASSERT_TRUE(actualOpt.has_value()) << "Optional field sfCounterpartySponsor should be present";
163 expectEqualField(expected, *actualOpt, "sfCounterpartySponsor");
164 }
165
166 {
167 auto const& expected = sponseeValue;
168 auto const actualOpt = rebuiltTx.getSponsee();
169 ASSERT_TRUE(actualOpt.has_value()) << "Optional field sfSponsee should be present";
170 expectEqualField(expected, *actualOpt, "sfSponsee");
171 }
172
173 {
174 auto const& expected = feeAmountDeltaValue;
175 auto const actualOpt = rebuiltTx.getFeeAmountDelta();
176 ASSERT_TRUE(actualOpt.has_value()) << "Optional field sfFeeAmountDelta should be present";
177 expectEqualField(expected, *actualOpt, "sfFeeAmountDelta");
178 }
179
180 {
181 auto const& expected = maxFeeValue;
182 auto const actualOpt = rebuiltTx.getMaxFee();
183 ASSERT_TRUE(actualOpt.has_value()) << "Optional field sfMaxFee should be present";
184 expectEqualField(expected, *actualOpt, "sfMaxFee");
185 }
186
187 {
188 auto const& expected = remainingOwnerCountDeltaValue;
189 auto const actualOpt = rebuiltTx.getRemainingOwnerCountDelta();
190 ASSERT_TRUE(actualOpt.has_value()) << "Optional field sfRemainingOwnerCountDelta should be present";
191 expectEqualField(expected, *actualOpt, "sfRemainingOwnerCountDelta");
192 }
193
194}
195
196// 3) Verify wrapper throws when constructed from wrong transaction type.
197TEST(TransactionsSponsorshipSetTests, WrapperThrowsOnWrongTxType)
198{
199 // Build a valid transaction of a different type
200 auto const [pk, sk] =
202 auto const account = calcAccountID(pk);
203
204 AccountSetBuilder wrongBuilder{account, 1, canonical_AMOUNT()};
205 auto wrongTx = wrongBuilder.build(pk, sk);
206
207 EXPECT_THROW(SponsorshipSet{wrongTx.getSTTx()}, std::runtime_error);
208}
209
210// 4) Verify builder throws when constructed from wrong transaction type.
211TEST(TransactionsSponsorshipSetTests, BuilderThrowsOnWrongTxType)
212{
213 // Build a valid transaction of a different type
214 auto const [pk, sk] =
215 generateKeyPair(KeyType::Secp256k1, generateSeed("testWrongTypeBuilder"));
216 auto const account = calcAccountID(pk);
217
218 AccountSetBuilder wrongBuilder{account, 1, canonical_AMOUNT()};
219 auto wrongTx = wrongBuilder.build(pk, sk);
220
221 EXPECT_THROW(SponsorshipSetBuilder{wrongTx.getSTTx()}, std::runtime_error);
222}
223
224// 5) Build with only required fields and verify optional fields return nullopt.
225TEST(TransactionsSponsorshipSetTests, OptionalFieldsReturnNullopt)
226{
227 // Generate a deterministic keypair for signing
228 auto const [publicKey, secretKey] =
229 generateKeyPair(KeyType::Secp256k1, generateSeed("testSponsorshipSetNullopt"));
230
231 // Common transaction fields
232 auto const accountValue = calcAccountID(publicKey);
233 std::uint32_t const sequenceValue = 3;
234 auto const feeValue = canonical_AMOUNT();
235
236 // Transaction-specific required field values
237
238 SponsorshipSetBuilder builder{
239 accountValue,
240 sequenceValue,
241 feeValue
242 };
243
244 // Do NOT set optional fields
245
246 auto tx = builder.build(publicKey, secretKey);
247
248 // Verify optional fields are not present
249 EXPECT_FALSE(tx.hasCounterpartySponsor());
250 EXPECT_FALSE(tx.getCounterpartySponsor().has_value());
251 EXPECT_FALSE(tx.hasSponsee());
252 EXPECT_FALSE(tx.getSponsee().has_value());
253 EXPECT_FALSE(tx.hasFeeAmountDelta());
254 EXPECT_FALSE(tx.getFeeAmountDelta().has_value());
255 EXPECT_FALSE(tx.hasMaxFee());
256 EXPECT_FALSE(tx.getMaxFee().has_value());
257 EXPECT_FALSE(tx.hasRemainingOwnerCountDelta());
258 EXPECT_FALSE(tx.getRemainingOwnerCountDelta().has_value());
259}
260
261}
AccountSet build(PublicKey const &publicKey, SecretKey const &secretKey)
Build and return the AccountSet wrapper.
SponsorshipSetBuilder & setRemainingOwnerCountDelta(std::decay_t< typename SF_INT32::type::value_type > const &value)
Set sfRemainingOwnerCountDelta (SoeOptional).
SponsorshipSetBuilder & setSponsee(std::decay_t< typename SF_ACCOUNT::type::value_type > const &value)
Set sfSponsee (SoeOptional).
SponsorshipSetBuilder & setMaxFee(std::decay_t< typename SF_AMOUNT::type::value_type > const &value)
Set sfMaxFee (SoeOptional).
SponsorshipSetBuilder & setCounterpartySponsor(std::decay_t< typename SF_ACCOUNT::type::value_type > const &value)
Transaction-specific field setters.
SponsorshipSet build(PublicKey const &publicKey, SecretKey const &secretKey)
Build and return the SponsorshipSet wrapper.
SponsorshipSetBuilder & setFeeAmountDelta(std::decay_t< typename SF_AMOUNT::type::value_type > const &value)
Set sfFeeAmountDelta (SoeOptional).
TEST(TransactionsAccountDeleteTests, BuilderSettersRoundTrip)
Seed generateSeed(std::string const &passPhrase)
Generate a seed deterministically.
Definition Seed.cpp:58
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)