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
22#include <xrpl/beast/unit_test.h>
23#include <xrpl/protocol/STIssue.h>
24
25namespace ripple {
26namespace test {
27
29{
30public:
31 void
33 {
34 testcase("Constructor");
35 using namespace jtx;
36 Account const alice{"alice"};
37 auto const USD = alice["USD"];
38 Issue issue;
39
40 try
41 {
42 issue = xrpIssue();
43 issue.account = alice;
44 STIssue stissue(sfAsset, Asset{issue});
45 fail("Inconsistent XRP Issue doesn't fail");
46 }
47 catch (...)
48 {
49 pass();
50 }
51
52 try
53 {
54 issue = USD;
55 issue.account = xrpAccount();
56 STIssue stissue(sfAsset, Asset{issue});
57 fail("Inconsistent IOU Issue doesn't fail");
58 }
59 catch (...)
60 {
61 pass();
62 }
63
64 try
65 {
66 // Currency is USD but account is XRP
67 auto const data =
68 "00000000000000000000000055534400000000000000000000000000000000"
69 "000000000000000000";
70 base_uint<320> uint;
71 (void)uint.parseHex(data);
72 SerialIter iter(Slice(uint.data(), uint.size()));
73 STIssue stissue(iter, sfAsset);
74 fail("Inconsistent IOU Issue doesn't fail on serializer");
75 }
76 catch (...)
77 {
78 pass();
79 }
80
81 try
82 {
83 STIssue stissue(sfAsset, Asset{xrpIssue()});
84 }
85 catch (...)
86 {
87 fail("XRP issue failed");
88 }
89
90 try
91 {
92 STIssue stissue(sfAsset, Asset{USD});
93 }
94 catch (...)
95 {
96 fail("USD issue failed");
97 }
98
99 try
100 {
101 auto const data =
102 "0000000000000000000000005553440000000000ae123a8556f3cf91154711"
103 "376afb0f894f832b3d";
104 base_uint<320> uint;
105 (void)uint.parseHex(data);
106 SerialIter iter(Slice(uint.data(), uint.size()));
107 STIssue stissue(iter, sfAsset);
108 BEAST_EXPECT(stissue.value() == USD);
109 }
110 catch (...)
111 {
112 fail("USD Issue fails on serializer");
113 }
114
115 try
116 {
117 auto const data = "0000000000000000000000000000000000000000";
118 base_uint<160> uint;
119 (void)uint.parseHex(data);
120 SerialIter iter(Slice(uint.data(), uint.size()));
121 STIssue stissue(iter, sfAsset);
122 BEAST_EXPECT(stissue.value() == xrpCurrency());
123 }
124 catch (...)
125 {
126 fail("XRP Issue fails on serializer");
127 }
128 }
129
130 void
132 {
133 testcase("Compare");
134 using namespace jtx;
135 Account const alice{"alice"};
136 auto const USD = alice["USD"];
137 Asset const asset1{xrpIssue()};
138 Asset const asset2{USD};
139 Asset const asset3{MPTID{2}};
140
141 BEAST_EXPECT(STIssue(sfAsset, asset1) != asset2);
142 BEAST_EXPECT(STIssue(sfAsset, asset1) != asset3);
143 BEAST_EXPECT(STIssue(sfAsset, asset1) == asset1);
144 BEAST_EXPECT(STIssue(sfAsset, asset1).getText() == "XRP");
145 BEAST_EXPECT(
146 STIssue(sfAsset, asset2).getText() ==
147 "USD/rG1QQv2nh2gr7RCZ1P8YYcBUKCCN633jCn");
148 BEAST_EXPECT(
149 STIssue(sfAsset, asset3).getText() ==
150 "000000000000000000000000000000000000000000000002");
151 }
152
153 void
154 run() override
155 {
156 // compliments other unit tests to ensure complete coverage
158 testCompare();
159 }
160};
161
162BEAST_DEFINE_TESTSUITE(STIssue, protocol, ripple);
163
164} // namespace test
165} // 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:33
AccountID account
Definition Issue.h:36
value_type const & value() const noexcept
Definition STIssue.h:133
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
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
auto const data
General field definitions, or fields used in multiple transaction namespaces.
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:25
Issue const & xrpIssue()
Returns an asset specifier that represents XRP.
Definition Issue.h:115
AccountID const & xrpAccount()
Compute AccountID from public key.
Currency const & xrpCurrency()
XRP currency.