rippled
Loading...
Searching...
No Matches
SetRegularKeyTests.cpp
1// Auto-generated unit tests for transaction SetRegularKey
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/SetRegularKey.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(TransactionsSetRegularKeyTests, BuilderSettersRoundTrip)
21{
22 // Generate a deterministic keypair for signing
23 auto const [publicKey, secretKey] =
24 generateKeyPair(KeyType::secp256k1, generateSeed("testSetRegularKey"));
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 regularKeyValue = canonical_ACCOUNT();
33
35 accountValue,
36 sequenceValue,
37 feeValue
38 };
39
40 // Set optional fields
41 builder.setRegularKey(regularKeyValue);
42
43 auto tx = builder.build(publicKey, secretKey);
44
45 std::string reason;
46 EXPECT_TRUE(tx.validate(reason)) << reason;
47
48 // Verify signing was applied
49 EXPECT_FALSE(tx.getSigningPubKey().empty());
50 EXPECT_TRUE(tx.hasTxnSignature());
51
52 // Verify common fields
53 EXPECT_EQ(tx.getAccount(), accountValue);
54 EXPECT_EQ(tx.getSequence(), sequenceValue);
55 EXPECT_EQ(tx.getFee(), feeValue);
56
57 // Verify required fields
58 // Verify optional fields
59 {
60 auto const& expected = regularKeyValue;
61 auto const actualOpt = tx.getRegularKey();
62 ASSERT_TRUE(actualOpt.has_value()) << "Optional field sfRegularKey should be present";
63 expectEqualField(expected, *actualOpt, "sfRegularKey");
64 EXPECT_TRUE(tx.hasRegularKey());
65 }
66
67}
68
69// 2 & 4) Start from an STTx, construct a builder from it, build a new wrapper,
70// and verify all fields match.
71TEST(TransactionsSetRegularKeyTests, BuilderFromStTxRoundTrip)
72{
73 // Generate a deterministic keypair for signing
74 auto const [publicKey, secretKey] =
75 generateKeyPair(KeyType::secp256k1, generateSeed("testSetRegularKeyFromTx"));
76
77 // Common transaction fields
78 auto const accountValue = calcAccountID(publicKey);
79 std::uint32_t const sequenceValue = 2;
80 auto const feeValue = canonical_AMOUNT();
81
82 // Transaction-specific field values
83 auto const regularKeyValue = canonical_ACCOUNT();
84
85 // Build an initial transaction
86 SetRegularKeyBuilder initialBuilder{
87 accountValue,
88 sequenceValue,
89 feeValue
90 };
91
92 initialBuilder.setRegularKey(regularKeyValue);
93
94 auto initialTx = initialBuilder.build(publicKey, secretKey);
95
96 // Create builder from existing STTx
97 SetRegularKeyBuilder builderFromTx{initialTx.getSTTx()};
98
99 auto rebuiltTx = builderFromTx.build(publicKey, secretKey);
100
101 std::string reason;
102 EXPECT_TRUE(rebuiltTx.validate(reason)) << reason;
103
104 // Verify common fields
105 EXPECT_EQ(rebuiltTx.getAccount(), accountValue);
106 EXPECT_EQ(rebuiltTx.getSequence(), sequenceValue);
107 EXPECT_EQ(rebuiltTx.getFee(), feeValue);
108
109 // Verify required fields
110 // Verify optional fields
111 {
112 auto const& expected = regularKeyValue;
113 auto const actualOpt = rebuiltTx.getRegularKey();
114 ASSERT_TRUE(actualOpt.has_value()) << "Optional field sfRegularKey should be present";
115 expectEqualField(expected, *actualOpt, "sfRegularKey");
116 }
117
118}
119
120// 3) Verify wrapper throws when constructed from wrong transaction type.
121TEST(TransactionsSetRegularKeyTests, WrapperThrowsOnWrongTxType)
122{
123 // Build a valid transaction of a different type
124 auto const [pk, sk] =
126 auto const account = calcAccountID(pk);
127
128 AccountSetBuilder wrongBuilder{account, 1, canonical_AMOUNT()};
129 auto wrongTx = wrongBuilder.build(pk, sk);
130
131 EXPECT_THROW(SetRegularKey{wrongTx.getSTTx()}, std::runtime_error);
132}
133
134// 4) Verify builder throws when constructed from wrong transaction type.
135TEST(TransactionsSetRegularKeyTests, BuilderThrowsOnWrongTxType)
136{
137 // Build a valid transaction of a different type
138 auto const [pk, sk] =
139 generateKeyPair(KeyType::secp256k1, generateSeed("testWrongTypeBuilder"));
140 auto const account = calcAccountID(pk);
141
142 AccountSetBuilder wrongBuilder{account, 1, canonical_AMOUNT()};
143 auto wrongTx = wrongBuilder.build(pk, sk);
144
145 EXPECT_THROW(SetRegularKeyBuilder{wrongTx.getSTTx()}, std::runtime_error);
146}
147
148// 5) Build with only required fields and verify optional fields return nullopt.
149TEST(TransactionsSetRegularKeyTests, OptionalFieldsReturnNullopt)
150{
151 // Generate a deterministic keypair for signing
152 auto const [publicKey, secretKey] =
153 generateKeyPair(KeyType::secp256k1, generateSeed("testSetRegularKeyNullopt"));
154
155 // Common transaction fields
156 auto const accountValue = calcAccountID(publicKey);
157 std::uint32_t const sequenceValue = 3;
158 auto const feeValue = canonical_AMOUNT();
159
160 // Transaction-specific required field values
161
162 SetRegularKeyBuilder builder{
163 accountValue,
164 sequenceValue,
165 feeValue
166 };
167
168 // Do NOT set optional fields
169
170 auto tx = builder.build(publicKey, secretKey);
171
172 // Verify optional fields are not present
173 EXPECT_FALSE(tx.hasRegularKey());
174 EXPECT_FALSE(tx.getRegularKey().has_value());
175}
176
177}
SetRegularKey build(PublicKey const &publicKey, SecretKey const &secretKey)
Build and return the SetRegularKey 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)