rippled
Loading...
Searching...
No Matches
STIssue_test.cpp
1//------------------------------------------------------------------------------
2/*
3 This file is part of rippled: https://github.com/ripple/rippled
4 Copyright (c) 2024 Ripple Labs Inc.
5
6 Permission to use, copy, modify, and/or distribute this software for any
7 purpose with or without fee is hereby granted, provided that the above
8 copyright notice and this permission notice appear in all copies.
9
10 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17*/
18//==============================================================================
19
20#include <test/jtx.h>
21#include <xrpl/beast/unit_test.h>
22#include <xrpl/protocol/STIssue.h>
23
24namespace ripple {
25namespace test {
26
28{
29public:
30 void
32 {
33 testcase("Constructor");
34 using namespace jtx;
35 Account const alice{"alice"};
36 auto const USD = alice["USD"];
37 Issue issue;
38
39 try
40 {
41 issue = xrpIssue();
42 issue.account = alice;
43 STIssue stissue(sfAsset, Asset{issue});
44 fail("Inconsistent XRP Issue doesn't fail");
45 }
46 catch (...)
47 {
48 pass();
49 }
50
51 try
52 {
53 issue = USD;
54 issue.account = xrpAccount();
55 STIssue stissue(sfAsset, Asset{issue});
56 fail("Inconsistent IOU Issue doesn't fail");
57 }
58 catch (...)
59 {
60 pass();
61 }
62
63 try
64 {
65 // Currency is USD but account is XRP
66 auto const data =
67 "00000000000000000000000055534400000000000000000000000000000000"
68 "000000000000000000";
69 base_uint<320> uint;
70 (void)uint.parseHex(data);
71 SerialIter iter(Slice(uint.data(), uint.size()));
72 STIssue stissue(iter, sfAsset);
73 fail("Inconsistent IOU Issue doesn't fail on serializer");
74 }
75 catch (...)
76 {
77 pass();
78 }
79
80 try
81 {
82 STIssue stissue(sfAsset, Asset{xrpIssue()});
83 }
84 catch (...)
85 {
86 fail("XRP issue failed");
87 }
88
89 try
90 {
91 STIssue stissue(sfAsset, Asset{USD});
92 }
93 catch (...)
94 {
95 fail("USD issue failed");
96 }
97
98 try
99 {
100 auto const data =
101 "0000000000000000000000005553440000000000ae123a8556f3cf91154711"
102 "376afb0f894f832b3d";
103 base_uint<320> uint;
104 (void)uint.parseHex(data);
105 SerialIter iter(Slice(uint.data(), uint.size()));
106 STIssue stissue(iter, sfAsset);
107 BEAST_EXPECT(stissue.value() == USD);
108 }
109 catch (...)
110 {
111 fail("USD Issue fails on serializer");
112 }
113
114 try
115 {
116 auto const data = "0000000000000000000000000000000000000000";
117 base_uint<160> uint;
118 (void)uint.parseHex(data);
119 SerialIter iter(Slice(uint.data(), uint.size()));
120 STIssue stissue(iter, sfAsset);
121 BEAST_EXPECT(stissue.value() == xrpCurrency());
122 }
123 catch (...)
124 {
125 fail("XRP Issue fails on serializer");
126 }
127 }
128
129 void
131 {
132 testcase("Compare");
133 using namespace jtx;
134 Account const alice{"alice"};
135 auto const USD = alice["USD"];
136 Asset const asset1{xrpIssue()};
137 Asset const asset2{USD};
138 Asset const asset3{MPTID{2}};
139
140 BEAST_EXPECT(STIssue(sfAsset, asset1) != asset2);
141 BEAST_EXPECT(STIssue(sfAsset, asset1) != asset3);
142 BEAST_EXPECT(STIssue(sfAsset, asset1) == asset1);
143 BEAST_EXPECT(STIssue(sfAsset, asset1).getText() == "XRP");
144 BEAST_EXPECT(
145 STIssue(sfAsset, asset2).getText() ==
146 "USD/rG1QQv2nh2gr7RCZ1P8YYcBUKCCN633jCn");
147 BEAST_EXPECT(
148 STIssue(sfAsset, asset3).getText() ==
149 "000000000000000000000000000000000000000000000002");
150 }
151
152 void
153 run() override
154 {
155 // compliments other unit tests to ensure complete coverage
157 testCompare();
158 }
159};
160
161BEAST_DEFINE_TESTSUITE(STIssue, ripple_data, ripple);
162
163} // namespace test
164} // namespace ripple
A testsuite class.
Definition: suite.h:55
void pass()
Record a successful test condition.
Definition: suite.h:511
testcase_t testcase
Memberspace for declaring test cases.
Definition: suite.h:155
void fail(String const &reason, char const *file, int line)
Record a failure.
Definition: suite.h:533
A currency issued by an account.
Definition: Issue.h:36
AccountID account
Definition: Issue.h:39
value_type const & value() const noexcept
Definition: STIssue.h:129
An immutable linear range of bytes.
Definition: Slice.h:46
Integers of any length that is a multiple of 32-bits.
Definition: base_uint.h:86
pointer data()
Definition: base_uint.h:125
static constexpr std::size_t size()
Definition: base_uint.h:526
constexpr bool parseHex(std::string_view sv)
Parse a hex string into a base_uint.
Definition: base_uint.h:503
void run() override
Runs the suite.
Immutable cryptographic account descriptor.
Definition: Account.h:39
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: algorithm.h:26
Issue const & xrpIssue()
Returns an asset specifier that represents XRP.
Definition: Issue.h:118
AccountID const & xrpAccount()
Compute AccountID from public key.
Definition: AccountID.cpp:178
Currency const & xrpCurrency()
XRP currency.
Definition: UintTypes.cpp:119