xrpld
Toggle main menu visibility
Loading...
Searching...
No Matches
test
conditions
PreimageSha256_test.cpp
1
#include <xrpl/basics/Buffer.h>
2
#include <xrpl/basics/Slice.h>
3
#include <xrpl/basics/StringUtilities.h>
4
#include <xrpl/beast/unit_test/suite.h>
5
#include <xrpl/conditions/Condition.h>
6
#include <xrpl/conditions/Fulfillment.h>
7
8
#include <
string
>
9
#include <
system_error
>
10
#include <
utility
>
11
#include <
vector
>
12
13
namespace
xrpl::cryptoconditions
{
14
15
class
PreimageSha256_test
:
public
beast::unit_test::Suite
16
{
17
Buffer
18
hexblob
(
std::string
const
& s)
19
{
20
auto
blob =
strUnHex
(s);
21
BEAST_EXPECT(blob);
22
return
{blob->data(), blob->size()};
// NOLINT(bugprone-unchecked-optional-access)
23
}
24
25
void
26
testKnownVectors
()
27
{
28
testcase
(
"Known Vectors"
);
29
30
std::pair<std::string, std::string>
const
known[] = {
31
{
"A0028000"
,
32
"A0258020E3B0C44298FC1C149AFBF4C8996FB92427AE41E4649B934CA495991B7"
33
"852B855810100"
},
34
{
"A0058003616161"
,
35
"A02580209834876DCFB05CB167A5C24953EBA58C4AC89B1ADF57F28F2F9D09AF1"
36
"07EE8F0810103"
},
37
};
38
39
std::error_code
ec;
40
41
auto
f1 =
Fulfillment::deserialize
(
hexblob
(known[0].first), ec);
42
BEAST_EXPECT(f1);
43
BEAST_EXPECT(!ec);
44
45
auto
c1 =
Condition::deserialize
(
hexblob
(known[0].second), ec);
46
BEAST_EXPECT(c1);
47
BEAST_EXPECT(!ec);
48
49
auto
f2 =
Fulfillment::deserialize
(
hexblob
(known[1].first), ec);
50
BEAST_EXPECT(f2);
51
BEAST_EXPECT(!ec);
52
53
auto
c2 =
Condition::deserialize
(
hexblob
(known[1].second), ec);
54
BEAST_EXPECT(c2);
55
BEAST_EXPECT(!ec);
56
57
// Check equality and inequality
58
BEAST_EXPECT(f1->condition() == *c1);
59
BEAST_EXPECT(f1->condition() != *c2);
60
BEAST_EXPECT(f2->condition() == *c2);
61
BEAST_EXPECT(f2->condition() != *c1);
62
BEAST_EXPECT(*c1 != *c2);
63
BEAST_EXPECT(*c1 == *c1);
64
BEAST_EXPECT(f1->condition() == f1->condition());
65
66
// Should validate with the empty string
67
BEAST_EXPECT(
validate
(*f1, *c1));
68
BEAST_EXPECT(
validate
(*f2, *c2));
69
70
// And with any string - the message doesn't matter for PrefixSha256
71
BEAST_EXPECT(
validate
(*f1, *c1,
makeSlice
(known[0].first)));
72
BEAST_EXPECT(
validate
(*f1, *c1,
makeSlice
(known[0].second)));
73
BEAST_EXPECT(
validate
(*f2, *c2,
makeSlice
(known[0].first)));
74
BEAST_EXPECT(
validate
(*f2, *c2,
makeSlice
(known[0].second)));
75
76
// Shouldn't validate if the kFulfillment & condition don't match
77
// regardless of the message.
78
BEAST_EXPECT(!
validate
(*f2, *c1));
79
BEAST_EXPECT(!
validate
(*f2, *c1,
makeSlice
(known[0].first)));
80
BEAST_EXPECT(!
validate
(*f2, *c1,
makeSlice
(known[0].second)));
81
BEAST_EXPECT(!
validate
(*f1, *c2));
82
BEAST_EXPECT(!
validate
(*f1, *c2,
makeSlice
(known[0].first)));
83
BEAST_EXPECT(!
validate
(*f1, *c2,
makeSlice
(known[0].second)));
84
}
85
86
void
87
testOtherTypes
()
88
{
89
testcase
(
"Other Types"
);
90
91
std::pair<std::string, std::string>
const
others[] = {
92
// PREFIX + PREIMAGE:
93
{
"A10B8000810100A204A0028000"
,
94
95
"A12A8020BB1AC5260C0141B7E54B26EC2330637C5597BF811951AC09E744AD20F"
96
"F77E287810204"
97
"0082020780"
},
98
99
// THRESHOLD:
100
{
"A208A004A0028000A100"
,
101
102
"A22A8020B4B84136DF48A71D73F4985C04C6767A778ECB65BA7023B4506823BEE"
103
"E7631B9810204"
104
"0082020780"
},
105
106
// RSA:
107
{
"A382020880820100E1EF8B24D6F76B09C81ED7752AA262F044F04A874D43809D3"
108
"1CEA612F99B0C97"
109
"A8B4374153E3EEF3D66616843E0E41C293264B71B6173DB1CF0D6CD558C586577"
110
"06FCF097F704C48"
111
"3E59CBFDFD5B3EE7BC80D740C5E0F047F3E85FC0D75815776A6F3F23C5DC5E797"
112
"139A6882E38336A"
113
"4A5FB36137620FF3663DBAE328472801862F72F2F87B202B9C89ADD7CD5B0A076"
114
"F7C53E35039F67E"
115
"D17EC815E5B4305CC63197068D5E6E579BA6DE5F4E3E57DF5E4E072FF2CE4C66E"
116
"B45233973875275"
117
"9639F0257BF57DBD5C443FB5158CCE0A3D36ADC7BA01F33A0BB6DBB2BF989D607"
118
"112F2344D993E77"
119
"E563C1D361DEDF57DA96EF2CFC685F002B638246A5B309B981820100BD42D6569"
120
"F6599AED455F96B"
121
"C0ED08ED1480BF36CD9E1467F9C6F74461C9E3A749334B2F6404AA5F9F6BAFE76"
122
"C347D069250B35D"
123
"1C970C793059EE733A8193F30FA78FEC7CAE459E3DDFD7633805D476940D0CB53"
124
"D7FB389DCDAEAF6"
125
"E8CF48C4B5635430E4F2BCDFE505C2C0FC17B40D93C7EDB7C261EBF43895A705E"
126
"024AA0549A660F7"
127
"0A32150647522DBE6B63520497CFF8F8D5D74768A27C5B86E580BE3FCDC96F197"
128
"6293CBA0D58DFC6"
129
"0B518B632A6DC1E950C43E231FE1A379AA6DDCC52C70EDF851C6C0123A964261C"
130
"FDB3857CD6CD5AD"
131
"C37D8DA2CC924EDAE1D84CF6124587F274C1FA3697DA2901F0269F03B243C03B6"
132
"14E0385E1961FAC"
133
"5000F9BB"
,
134
135
"A3258020484950515253545548495051525354554849505152535455484950515"
136
"2535455810101"
},
137
138
// ED25519:
139
{
"A4648020D75A980182B10AB7D54BFED3C964073A0EE172F3DAA62325AF021A68F"
140
"707511A8140E556"
141
"4300C360AC729086E2CC806E828A84877F1EB8E5D974D873E065224901555FB88"
142
"21590A33BACC61E"
143
"39701CF9B46BD25BF5F0595BBE24655141438E7A100B"
,
144
145
"A4278020799239ABA8FC4FF7EABFBC4C44E69E8BDFED993324E12ED64792ABE28"
146
"9CF1D5F810302"
147
"0000"
}};
148
149
for
(
auto
const
& x : others)
150
{
151
std::error_code
ec;
152
153
BEAST_EXPECT(!
Fulfillment::deserialize
(
hexblob
(x.first), ec));
154
BEAST_EXPECT(!
Condition::deserialize
(
hexblob
(x.second), ec));
155
}
156
}
157
158
void
159
run
()
override
160
{
161
testKnownVectors
();
162
testOtherTypes
();
163
}
164
};
165
166
BEAST_DEFINE_TESTSUITE
(
PreimageSha256
, conditions,
xrpl
);
167
168
}
// namespace xrpl::cryptoconditions
std::string
beast::unit_test::Suite
A testsuite class.
Definition
suite.h:52
beast::unit_test::Suite::testcase
TestcaseT testcase
Memberspace for declaring test cases.
Definition
suite.h:155
xrpl::Buffer
Like std::vector<char> but better.
Definition
Buffer.h:19
xrpl::cryptoconditions::Condition::deserialize
static std::unique_ptr< Condition > deserialize(Slice s, std::error_code &ec)
Load a condition from its binary form.
Definition
Condition.cpp:138
xrpl::cryptoconditions::PreimageSha256_test
Definition
PreimageSha256_test.cpp:16
xrpl::cryptoconditions::PreimageSha256_test::testOtherTypes
void testOtherTypes()
Definition
PreimageSha256_test.cpp:87
xrpl::cryptoconditions::PreimageSha256_test::run
void run() override
Runs the suite.
Definition
PreimageSha256_test.cpp:159
xrpl::cryptoconditions::PreimageSha256_test::testKnownVectors
void testKnownVectors()
Definition
PreimageSha256_test.cpp:26
xrpl::cryptoconditions::PreimageSha256_test::hexblob
Buffer hexblob(std::string const &s)
Definition
PreimageSha256_test.cpp:18
xrpl::cryptoconditions::PreimageSha256
Definition
PreimageSha256.h:19
std::error_code
xrpl::cryptoconditions
Definition
Condition.h:14
xrpl::cryptoconditions::validate
bool validate(Fulfillment const &f, Condition const &c, Slice m)
Verify if the given message satisfies the fulfillment.
Definition
Fulfillment.cpp:29
xrpl::cryptoconditions::BEAST_DEFINE_TESTSUITE
BEAST_DEFINE_TESTSUITE(PreimageSha256, conditions, xrpl)
xrpl
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition
algorithm.h:5
xrpl::makeSlice
Slice makeSlice(std::array< T, N > const &a)
Definition
Slice.h:228
xrpl::strUnHex
std::optional< Blob > strUnHex(std::size_t strSize, Iterator begin, Iterator end)
Definition
StringUtilities.h:64
std::pair
string
xrpl::cryptoconditions::Fulfillment::deserialize
static std::unique_ptr< Fulfillment > deserialize(Slice s, std::error_code &ec)
Load a fulfillment from its binary form.
Definition
Fulfillment.cpp:41
system_error
utility
vector
Generated by
1.17.0