rippled
Loading...
Searching...
No Matches
Memo_test.cpp
1//------------------------------------------------------------------------------
2/*
3 This file is part of rippled: https://github.com/ripple/rippled
4 Copyright (c) 2022 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/basics/strHex.h>
23
24namespace ripple {
25
27{
28public:
29 void
31 {
32 testcase("Test memos");
33
34 using namespace test::jtx;
35 Account alice{"alice"};
36
37 Env env(*this);
38 env.fund(XRP(10000), alice);
39 env.close();
40
41 // Lambda that returns a valid JTx with a memo that we can hack up.
42 // This is the basis for building tests of invalid states.
43 auto makeJtxWithMemo = [&env, &alice]() {
44 JTx example = noop(alice);
45 memo const exampleMemo{"tic", "tac", "toe"};
46 exampleMemo(env, example);
47 return example;
48 };
49
50 // A valid memo.
51 env(makeJtxWithMemo());
52 env.close();
53
54 {
55 // Make sure that too big a memo is flagged as invalid.
56 JTx memoSize = makeJtxWithMemo();
57 memoSize.jv[sfMemos.jsonName][0u][sfMemo.jsonName]
58 [sfMemoData.jsonName] = std::string(2020, '0');
59 env(memoSize,
60 rpc("invalidTransaction",
61 "fails local checks: The memo exceeds the maximum allowed "
62 "size."));
63
64 // This memo is just barely small enough.
65 memoSize.jv[sfMemos.jsonName][0u][sfMemo.jsonName]
66 [sfMemoData.jsonName] = std::string(2018, '1');
67 env(memoSize);
68 }
69 {
70 // Put a non-Memo in the Memos array.
71 JTx memoNonMemo = noop(alice);
72 auto& jv = memoNonMemo.jv;
73 auto& ma = jv[sfMemos.jsonName];
74 auto& mi = ma[ma.size()];
75 auto& m = mi[sfCreatedNode.jsonName]; // CreatedNode in Memos
76 m[sfMemoData.jsonName] = "3030303030";
77
78 env(memoNonMemo,
79 rpc("invalidTransaction",
80 "fails local checks: A memo array may contain only Memo "
81 "objects."));
82 }
83 {
84 // Put an invalid field in a Memo object.
85 JTx memoExtra = makeJtxWithMemo();
86 memoExtra
87 .jv[sfMemos.jsonName][0u][sfMemo.jsonName][sfFlags.jsonName] =
88 13;
89 env(memoExtra,
90 rpc("invalidTransaction",
91 "fails local checks: A memo may contain only MemoType, "
92 "MemoData or MemoFormat fields."));
93 }
94 {
95 // Put a character that is not allowed in a URL in a MemoType field.
96 JTx memoBadChar = makeJtxWithMemo();
97 memoBadChar.jv[sfMemos.jsonName][0u][sfMemo.jsonName]
98 [sfMemoType.jsonName] =
99 strHex(std::string_view("ONE<INFINITY"));
100 env(memoBadChar,
101 rpc("invalidTransaction",
102 "fails local checks: The MemoType and MemoFormat fields "
103 "may only contain characters that are allowed in URLs "
104 "under RFC 3986."));
105 }
106 {
107 // Put a character that is not allowed in a URL in a MemoData field.
108 // That's okay.
109 JTx memoLegitChar = makeJtxWithMemo();
110 memoLegitChar.jv[sfMemos.jsonName][0u][sfMemo.jsonName]
111 [sfMemoData.jsonName] =
112 strHex(std::string_view("ONE<INFINITY"));
113 env(memoLegitChar);
114 }
115 {
116 // Put a character that is not allowed in a URL in a MemoFormat.
117 JTx memoBadChar = makeJtxWithMemo();
118 memoBadChar.jv[sfMemos.jsonName][0u][sfMemo.jsonName]
119 [sfMemoFormat.jsonName] =
120 strHex(std::string_view("NoBraces{}InURL"));
121 env(memoBadChar,
122 rpc("invalidTransaction",
123 "fails local checks: The MemoType and MemoFormat fields "
124 "may only contain characters that are allowed in URLs "
125 "under RFC 3986."));
126 }
127 }
128
129 //--------------------------------------------------------------------------
130
131 void
132 run() override
133 {
134 testMemos();
135 }
136};
137
138BEAST_DEFINE_TESTSUITE(Memo, ripple_data, ripple);
139
140} // namespace ripple
A testsuite class.
Definition: suite.h:55
testcase_t testcase
Memberspace for declaring test cases.
Definition: suite.h:155
void run() override
Runs the suite.
Definition: Memo_test.cpp:132
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: algorithm.h:25
std::string strHex(FwdIt begin, FwdIt end)
Definition: strHex.h:30