rippled
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 <ripple/basics/strHex.h>
21 #include <ripple/protocol/jss.h>
22 #include <test/jtx.h>
23 
24 namespace ripple {
25 
26 class Memo_test : public beast::unit_test::suite
27 {
28 public:
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, ter(temINVALID));
60 
61  // This memo is just barely small enough.
62  memoSize.jv[sfMemos.jsonName][0u][sfMemo.jsonName]
63  [sfMemoData.jsonName] = std::string(2018, '1');
64  env(memoSize);
65  }
66  {
67  // Put a non-Memo in the Memos array.
68  JTx memoNonMemo = noop(alice);
69  auto& jv = memoNonMemo.jv;
70  auto& ma = jv[sfMemos.jsonName];
71  auto& mi = ma[ma.size()];
72  auto& m = mi[sfCreatedNode.jsonName]; // CreatedNode in Memos
73  m[sfMemoData.jsonName] = "3030303030";
74 
75  env(memoNonMemo, ter(temINVALID));
76  }
77  {
78  // Put an invalid field in a Memo object.
79  JTx memoExtra = makeJtxWithMemo();
80  memoExtra
82  13;
83  env(memoExtra, ter(temINVALID));
84  }
85  {
86  // Put a character that is not allowed in a URL in a MemoType field.
87  JTx memoBadChar = makeJtxWithMemo();
88  memoBadChar.jv[sfMemos.jsonName][0u][sfMemo.jsonName]
90  strHex(std::string_view("ONE<INFINITY"));
91  env(memoBadChar, ter(temINVALID));
92  }
93  {
94  // Put a character that is not allowed in a URL in a MemoData field.
95  // That's okay.
96  JTx memoLegitChar = makeJtxWithMemo();
97  memoLegitChar.jv[sfMemos.jsonName][0u][sfMemo.jsonName]
99  strHex(std::string_view("ONE<INFINITY"));
100  env(memoLegitChar);
101  }
102  {
103  // Put a character that is not allowed in a URL in a MemoFormat.
104  JTx memoBadChar = makeJtxWithMemo();
105  memoBadChar.jv[sfMemos.jsonName][0u][sfMemo.jsonName]
107  strHex(std::string_view("NoBraces{}InURL"));
108  env(memoBadChar, ter(temINVALID));
109  }
110  }
111 
112  //--------------------------------------------------------------------------
113 
114  void
115  run() override
116  {
117  testMemos();
118  }
119 };
120 
121 BEAST_DEFINE_TESTSUITE(Memo, ripple_data, ripple);
122 
123 } // namespace ripple
std::string
STL class.
ripple::BEAST_DEFINE_TESTSUITE
BEAST_DEFINE_TESTSUITE(AccountTxPaging, app, ripple)
std::string_view
STL class.
ripple::Memo_test
Definition: Memo_test.cpp:26
ripple::Memo_test::testMemos
void testMemos()
Definition: Memo_test.cpp:30
ripple::SField::jsonName
const Json::StaticString jsonName
Definition: SField.h:136
ripple::Memo_test::run
void run() override
Definition: Memo_test.cpp:115
ripple::sfMemos
const SField sfMemos
ripple::sfMemoData
const SF_VL sfMemoData
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::sfFlags
const SF_UINT32 sfFlags
ripple::sfCreatedNode
const SField sfCreatedNode
ripple::sfMemo
const SField sfMemo
ripple::strHex
std::string strHex(FwdIt begin, FwdIt end)
Definition: strHex.h:30
ripple::temINVALID
@ temINVALID
Definition: TER.h:108
ripple::sfMemoFormat
const SF_VL sfMemoFormat
ripple::sfMemoType
const SF_VL sfMemoType