xrpld
Toggle main menu visibility
Loading...
Searching...
No Matches
tests
libxrpl
protocol_autogen
transactions
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
16
namespace
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.
20
TEST
(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
34
SetRegularKeyBuilder
builder{
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.
71
TEST
(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.
121
TEST
(TransactionsSetRegularKeyTests, WrapperThrowsOnWrongTxType)
122
{
123
// Build a valid transaction of a different type
124
auto
const
[pk, sk] =
125
generateKeyPair
(
KeyType::Secp256k1
,
generateSeed
(
"testWrongType"
));
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.
135
TEST
(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.
149
TEST
(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
}
std::string
xrpl::transactions::AccountSetBuilder
Builder for AccountSet transactions.
Definition
protocol_autogen/transactions/AccountSet.h:319
xrpl::transactions::AccountSetBuilder::build
AccountSet build(PublicKey const &publicKey, SecretKey const &secretKey)
Build and return the AccountSet wrapper.
Definition
protocol_autogen/transactions/AccountSet.h:470
xrpl::transactions::SetRegularKeyBuilder
Builder for SetRegularKey transactions.
Definition
protocol_autogen/transactions/SetRegularKey.h:85
xrpl::transactions::SetRegularKeyBuilder::build
SetRegularKey build(PublicKey const &publicKey, SecretKey const &secretKey)
Build and return the SetRegularKey wrapper.
Definition
protocol_autogen/transactions/SetRegularKey.h:137
xrpl::transactions::SetRegularKeyBuilder::setRegularKey
SetRegularKeyBuilder & setRegularKey(std::decay_t< typename SF_ACCOUNT::type::value_type > const &value)
Transaction-specific field setters.
Definition
protocol_autogen/transactions/SetRegularKey.h:124
xrpl::transactions::SetRegularKey
Transaction: SetRegularKey.
Definition
protocol_autogen/transactions/SetRegularKey.h:30
std::uint32_t
xrpl::transactions
Definition
TransactionBase.h:15
xrpl::transactions::TEST
TEST(TransactionsAccountDeleteTests, BuilderSettersRoundTrip)
Definition
AccountDeleteTests.cpp:20
xrpl::KeyType::Secp256k1
@ Secp256k1
Definition
KeyType.h:9
xrpl::canonical_ACCOUNT
AccountValue canonical_ACCOUNT()
Definition
/libxrpl/protocol_autogen/TestHelpers.h:111
xrpl::generateSeed
Seed generateSeed(std::string const &passPhrase)
Generate a seed deterministically.
Definition
Seed.cpp:58
xrpl::generateKeyPair
std::pair< PublicKey, SecretKey > generateKeyPair(KeyType type, Seed const &seed)
Generate a key pair deterministically.
Definition
SecretKey.cpp:338
xrpl::canonical_AMOUNT
AmountValue canonical_AMOUNT()
Definition
/libxrpl/protocol_autogen/TestHelpers.h:104
xrpl::calcAccountID
AccountID calcAccountID(PublicKey const &pk)
Definition
AccountID.cpp:149
xrpl::expectEqualField
void expectEqualField(T const &expected, T const &actual, char const *fieldName)
Definition
/libxrpl/protocol_autogen/TestHelpers.h:178
std::runtime_error
string
Generated by
1.17.0