rippled
Loading...
Searching...
No Matches
transactions/DepositPreauthTests.cpp
1// Auto-generated unit tests for transaction DepositPreauth
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/DepositPreauth.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(TransactionsDepositPreauthTests, BuilderSettersRoundTrip)
21{
22 // Generate a deterministic keypair for signing
23 auto const [publicKey, secretKey] =
24 generateKeyPair(KeyType::secp256k1, generateSeed("testDepositPreauth"));
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 authorizeValue = canonical_ACCOUNT();
33 auto const unauthorizeValue = canonical_ACCOUNT();
34 auto const authorizeCredentialsValue = canonical_ARRAY();
35 auto const unauthorizeCredentialsValue = canonical_ARRAY();
36
38 accountValue,
39 sequenceValue,
40 feeValue
41 };
42
43 // Set optional fields
44 builder.setAuthorize(authorizeValue);
45 builder.setUnauthorize(unauthorizeValue);
46 builder.setAuthorizeCredentials(authorizeCredentialsValue);
47 builder.setUnauthorizeCredentials(unauthorizeCredentialsValue);
48
49 auto tx = builder.build(publicKey, secretKey);
50
51 std::string reason;
52 EXPECT_TRUE(tx.validate(reason)) << reason;
53
54 // Verify signing was applied
55 EXPECT_FALSE(tx.getSigningPubKey().empty());
56 EXPECT_TRUE(tx.hasTxnSignature());
57
58 // Verify common fields
59 EXPECT_EQ(tx.getAccount(), accountValue);
60 EXPECT_EQ(tx.getSequence(), sequenceValue);
61 EXPECT_EQ(tx.getFee(), feeValue);
62
63 // Verify required fields
64 // Verify optional fields
65 {
66 auto const& expected = authorizeValue;
67 auto const actualOpt = tx.getAuthorize();
68 ASSERT_TRUE(actualOpt.has_value()) << "Optional field sfAuthorize should be present";
69 expectEqualField(expected, *actualOpt, "sfAuthorize");
70 EXPECT_TRUE(tx.hasAuthorize());
71 }
72
73 {
74 auto const& expected = unauthorizeValue;
75 auto const actualOpt = tx.getUnauthorize();
76 ASSERT_TRUE(actualOpt.has_value()) << "Optional field sfUnauthorize should be present";
77 expectEqualField(expected, *actualOpt, "sfUnauthorize");
78 EXPECT_TRUE(tx.hasUnauthorize());
79 }
80
81 {
82 auto const& expected = authorizeCredentialsValue;
83 auto const actualOpt = tx.getAuthorizeCredentials();
84 ASSERT_TRUE(actualOpt.has_value()) << "Optional field sfAuthorizeCredentials should be present";
85 expectEqualField(expected, *actualOpt, "sfAuthorizeCredentials");
86 EXPECT_TRUE(tx.hasAuthorizeCredentials());
87 }
88
89 {
90 auto const& expected = unauthorizeCredentialsValue;
91 auto const actualOpt = tx.getUnauthorizeCredentials();
92 ASSERT_TRUE(actualOpt.has_value()) << "Optional field sfUnauthorizeCredentials should be present";
93 expectEqualField(expected, *actualOpt, "sfUnauthorizeCredentials");
94 EXPECT_TRUE(tx.hasUnauthorizeCredentials());
95 }
96
97}
98
99// 2 & 4) Start from an STTx, construct a builder from it, build a new wrapper,
100// and verify all fields match.
101TEST(TransactionsDepositPreauthTests, BuilderFromStTxRoundTrip)
102{
103 // Generate a deterministic keypair for signing
104 auto const [publicKey, secretKey] =
105 generateKeyPair(KeyType::secp256k1, generateSeed("testDepositPreauthFromTx"));
106
107 // Common transaction fields
108 auto const accountValue = calcAccountID(publicKey);
109 std::uint32_t const sequenceValue = 2;
110 auto const feeValue = canonical_AMOUNT();
111
112 // Transaction-specific field values
113 auto const authorizeValue = canonical_ACCOUNT();
114 auto const unauthorizeValue = canonical_ACCOUNT();
115 auto const authorizeCredentialsValue = canonical_ARRAY();
116 auto const unauthorizeCredentialsValue = canonical_ARRAY();
117
118 // Build an initial transaction
119 DepositPreauthBuilder initialBuilder{
120 accountValue,
121 sequenceValue,
122 feeValue
123 };
124
125 initialBuilder.setAuthorize(authorizeValue);
126 initialBuilder.setUnauthorize(unauthorizeValue);
127 initialBuilder.setAuthorizeCredentials(authorizeCredentialsValue);
128 initialBuilder.setUnauthorizeCredentials(unauthorizeCredentialsValue);
129
130 auto initialTx = initialBuilder.build(publicKey, secretKey);
131
132 // Create builder from existing STTx
133 DepositPreauthBuilder builderFromTx{initialTx.getSTTx()};
134
135 auto rebuiltTx = builderFromTx.build(publicKey, secretKey);
136
137 std::string reason;
138 EXPECT_TRUE(rebuiltTx.validate(reason)) << reason;
139
140 // Verify common fields
141 EXPECT_EQ(rebuiltTx.getAccount(), accountValue);
142 EXPECT_EQ(rebuiltTx.getSequence(), sequenceValue);
143 EXPECT_EQ(rebuiltTx.getFee(), feeValue);
144
145 // Verify required fields
146 // Verify optional fields
147 {
148 auto const& expected = authorizeValue;
149 auto const actualOpt = rebuiltTx.getAuthorize();
150 ASSERT_TRUE(actualOpt.has_value()) << "Optional field sfAuthorize should be present";
151 expectEqualField(expected, *actualOpt, "sfAuthorize");
152 }
153
154 {
155 auto const& expected = unauthorizeValue;
156 auto const actualOpt = rebuiltTx.getUnauthorize();
157 ASSERT_TRUE(actualOpt.has_value()) << "Optional field sfUnauthorize should be present";
158 expectEqualField(expected, *actualOpt, "sfUnauthorize");
159 }
160
161 {
162 auto const& expected = authorizeCredentialsValue;
163 auto const actualOpt = rebuiltTx.getAuthorizeCredentials();
164 ASSERT_TRUE(actualOpt.has_value()) << "Optional field sfAuthorizeCredentials should be present";
165 expectEqualField(expected, *actualOpt, "sfAuthorizeCredentials");
166 }
167
168 {
169 auto const& expected = unauthorizeCredentialsValue;
170 auto const actualOpt = rebuiltTx.getUnauthorizeCredentials();
171 ASSERT_TRUE(actualOpt.has_value()) << "Optional field sfUnauthorizeCredentials should be present";
172 expectEqualField(expected, *actualOpt, "sfUnauthorizeCredentials");
173 }
174
175}
176
177// 3) Verify wrapper throws when constructed from wrong transaction type.
178TEST(TransactionsDepositPreauthTests, WrapperThrowsOnWrongTxType)
179{
180 // Build a valid transaction of a different type
181 auto const [pk, sk] =
183 auto const account = calcAccountID(pk);
184
185 AccountSetBuilder wrongBuilder{account, 1, canonical_AMOUNT()};
186 auto wrongTx = wrongBuilder.build(pk, sk);
187
188 EXPECT_THROW(DepositPreauth{wrongTx.getSTTx()}, std::runtime_error);
189}
190
191// 4) Verify builder throws when constructed from wrong transaction type.
192TEST(TransactionsDepositPreauthTests, BuilderThrowsOnWrongTxType)
193{
194 // Build a valid transaction of a different type
195 auto const [pk, sk] =
196 generateKeyPair(KeyType::secp256k1, generateSeed("testWrongTypeBuilder"));
197 auto const account = calcAccountID(pk);
198
199 AccountSetBuilder wrongBuilder{account, 1, canonical_AMOUNT()};
200 auto wrongTx = wrongBuilder.build(pk, sk);
201
202 EXPECT_THROW(DepositPreauthBuilder{wrongTx.getSTTx()}, std::runtime_error);
203}
204
205// 5) Build with only required fields and verify optional fields return nullopt.
206TEST(TransactionsDepositPreauthTests, OptionalFieldsReturnNullopt)
207{
208 // Generate a deterministic keypair for signing
209 auto const [publicKey, secretKey] =
210 generateKeyPair(KeyType::secp256k1, generateSeed("testDepositPreauthNullopt"));
211
212 // Common transaction fields
213 auto const accountValue = calcAccountID(publicKey);
214 std::uint32_t const sequenceValue = 3;
215 auto const feeValue = canonical_AMOUNT();
216
217 // Transaction-specific required field values
218
219 DepositPreauthBuilder builder{
220 accountValue,
221 sequenceValue,
222 feeValue
223 };
224
225 // Do NOT set optional fields
226
227 auto tx = builder.build(publicKey, secretKey);
228
229 // Verify optional fields are not present
230 EXPECT_FALSE(tx.hasAuthorize());
231 EXPECT_FALSE(tx.getAuthorize().has_value());
232 EXPECT_FALSE(tx.hasUnauthorize());
233 EXPECT_FALSE(tx.getUnauthorize().has_value());
234 EXPECT_FALSE(tx.hasAuthorizeCredentials());
235 EXPECT_FALSE(tx.getAuthorizeCredentials().has_value());
236 EXPECT_FALSE(tx.hasUnauthorizeCredentials());
237 EXPECT_FALSE(tx.getUnauthorizeCredentials().has_value());
238}
239
240}
DepositPreauth build(PublicKey const &publicKey, SecretKey const &secretKey)
Build and return the DepositPreauth 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)