rippled
Hooks_test.cpp
1 //------------------------------------------------------------------------------
2 /*
3  This file is part of rippled: https://github.com/ripple/rippled
4  Copyright (c) 2012-2017 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/protocol/Feature.h>
21 #include <ripple/protocol/jss.h>
22 #include <functional>
23 #include <test/jtx.h>
24 #include <vector>
25 
26 namespace ripple {
27 
28 class Hooks_test : public beast::unit_test::suite
29 {
37  void
39  {
40  testcase("Test Hooks fields");
41 
42  using namespace test::jtx;
43 
52  sfHookOn,
61  sfHookHash,
71  sfHook,
79  sfHooks,
80  sfHookGrants};
81 
82  for (auto const& rf : fields_to_test)
83  {
84  SField const& f = rf.get();
85 
86  STObject dummy{sfGeneric};
87 
88  BEAST_EXPECT(!dummy.isFieldPresent(f));
89 
90  switch (f.fieldType)
91  {
92  case STI_UINT8: {
93  dummy.setFieldU8(f, 0);
94  BEAST_EXPECT(dummy.getFieldU8(f) == 0);
95 
96  dummy.setFieldU8(f, 255);
97  BEAST_EXPECT(dummy.getFieldU8(f) == 255);
98 
99  BEAST_EXPECT(dummy.isFieldPresent(f));
100  break;
101  }
102 
103  case STI_UINT16: {
104  dummy.setFieldU16(f, 0);
105  BEAST_EXPECT(dummy.getFieldU16(f) == 0);
106 
107  dummy.setFieldU16(f, 0xFFFFU);
108  BEAST_EXPECT(dummy.getFieldU16(f) == 0xFFFFU);
109 
110  BEAST_EXPECT(dummy.isFieldPresent(f));
111  break;
112  }
113 
114  case STI_UINT32: {
115  dummy.setFieldU32(f, 0);
116  BEAST_EXPECT(dummy.getFieldU32(f) == 0);
117 
118  dummy.setFieldU32(f, 0xFFFFFFFFU);
119  BEAST_EXPECT(dummy.getFieldU32(f) == 0xFFFFFFFFU);
120 
121  BEAST_EXPECT(dummy.isFieldPresent(f));
122  break;
123  }
124 
125  case STI_UINT64: {
126  dummy.setFieldU64(f, 0);
127  BEAST_EXPECT(dummy.getFieldU64(f) == 0);
128 
129  dummy.setFieldU64(f, 0xFFFFFFFFFFFFFFFFU);
130  BEAST_EXPECT(dummy.getFieldU64(f) == 0xFFFFFFFFFFFFFFFFU);
131 
132  BEAST_EXPECT(dummy.isFieldPresent(f));
133  break;
134  }
135 
136  case STI_UINT256: {
138  "DEADBEEFDEADBEEFDEADBEEFDEADBEEFDEADBEEFDEADBEEFDEADBE"
139  "EFDEADBEEF");
140  dummy.setFieldH256(f, u);
141  BEAST_EXPECT(dummy.getFieldH256(f) == u);
142  BEAST_EXPECT(dummy.isFieldPresent(f));
143  break;
144  }
145 
146  case STI_VL: {
147  std::vector<uint8_t> v{1, 2, 3};
148  dummy.setFieldVL(f, v);
149  BEAST_EXPECT(dummy.getFieldVL(f) == v);
150  BEAST_EXPECT(dummy.isFieldPresent(f));
151  break;
152  }
153 
154  case STI_ACCOUNT: {
155  AccountID id = *parseBase58<AccountID>(
156  "rwfSjJNK2YQuN64bSWn7T2eY9FJAyAPYJT");
157  dummy.setAccountID(f, id);
158  BEAST_EXPECT(dummy.getAccountID(f) == id);
159  BEAST_EXPECT(dummy.isFieldPresent(f));
160  break;
161  }
162 
163  case STI_OBJECT: {
164  dummy.emplace_back(STObject{f});
165  BEAST_EXPECT(dummy.getField(f).getFName() == f);
166  BEAST_EXPECT(dummy.isFieldPresent(f));
167  break;
168  }
169 
170  case STI_ARRAY: {
171  STArray dummy2{f, 2};
172  dummy2.push_back(STObject{sfGeneric});
173  dummy2.push_back(STObject{sfGeneric});
174  dummy.setFieldArray(f, dummy2);
175  BEAST_EXPECT(dummy.getFieldArray(f) == dummy2);
176  BEAST_EXPECT(dummy.isFieldPresent(f));
177  break;
178  }
179 
180  default:
181  BEAST_EXPECT(false);
182  }
183  }
184  }
185 
186 public:
187  void
188  run() override
189  {
190  using namespace test::jtx;
191  testHookFields();
192  }
193 };
194 
196 
197 } // namespace ripple
ripple::sfHookAccount
const SF_ACCOUNT sfHookAccount
ripple::sfHookHash
const SF_UINT256 sfHookHash
ripple::sfHookApiVersion
const SF_UINT16 sfHookApiVersion
ripple::sfHooks
const SField sfHooks
ripple::sfHookParameterValue
const SF_VL sfHookParameterValue
ripple::sfEmitParentTxnID
const SF_UINT256 sfEmitParentTxnID
ripple::sfHookExecutionIndex
const SF_UINT16 sfHookExecutionIndex
ripple::BEAST_DEFINE_TESTSUITE
BEAST_DEFINE_TESTSUITE(AccountTxPaging, app, ripple)
ripple::sfEmitHookHash
const SF_UINT256 sfEmitHookHash
ripple::sfHookReturnCode
const SF_UINT64 sfHookReturnCode
ripple::sfGeneric
const SField sfGeneric(access, 0)
Definition: SField.h:327
functional
ripple::sfHookStateChangeCount
const SF_UINT16 sfHookStateChangeCount
vector
ripple::STI_UINT8
@ STI_UINT8
Definition: SField.h:71
ripple::sfHookResult
const SF_UINT8 sfHookResult
ripple::sfHookOn
const SF_UINT64 sfHookOn
ripple::sfHookReturnString
const SF_VL sfHookReturnString
ripple::sfHookParameters
const SField sfHookParameters
ripple::STI_ACCOUNT
@ STI_ACCOUNT
Definition: SField.h:65
ripple::STI_ARRAY
@ STI_ARRAY
Definition: SField.h:68
ripple::sfHookStateCount
const SF_UINT32 sfHookStateCount
ripple::sfEmitDetails
const SField sfEmitDetails
ripple::sfHookInstructionCount
const SF_UINT64 sfHookInstructionCount
ripple::sfHookEmitCount
const SF_UINT16 sfHookEmitCount
ripple::Hooks_test::run
void run() override
Definition: Hooks_test.cpp:188
ripple::sfHookStateData
const SF_VL sfHookStateData
ripple::SField::fieldType
const SerializedTypeID fieldType
Definition: SField.h:130
ripple::Hooks_test::testHookFields
void testHookFields()
This unit test was requested here: https://github.com/ripple/rippled/pull/4089#issuecomment-105027453...
Definition: Hooks_test.cpp:38
ripple::sfEmitNonce
const SF_UINT256 sfEmitNonce
ripple::base_uint< 256 >
ripple::sfHookExecution
const SField sfHookExecution
ripple::sfHookParameter
const SField sfHookParameter
ripple::STArray
Definition: STArray.h:28
ripple::sfHookStateKey
const SF_UINT256 sfHookStateKey
ripple::STI_VL
@ STI_VL
Definition: SField.h:64
ripple::STI_UINT16
@ STI_UINT16
Definition: SField.h:58
ripple::sfEmitGeneration
const SF_UINT32 sfEmitGeneration
ripple::sfHookExecutions
const SField sfHookExecutions
ripple::sfReferenceCount
const SF_UINT64 sfReferenceCount
ripple::STI_UINT32
@ STI_UINT32
Definition: SField.h:59
ripple::STObject
Definition: STObject.h:51
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::sfHookGrants
const SField sfHookGrants
ripple::sfHookGrant
const SField sfHookGrant
protocol
Definition: ValidatorList.h:38
ripple::SField
Identifies fields.
Definition: SField.h:112
ripple::STI_UINT64
@ STI_UINT64
Definition: SField.h:60
ripple::base_uint< 256 >::fromVoid
static base_uint fromVoid(void const *data)
Definition: base_uint.h:312
ripple::sfHookParameterName
const SF_VL sfHookParameterName
ripple::sfHook
const SField sfHook
ripple::sfHookDefinition
const SField sfHookDefinition
ripple::sfEmitCallback
const SF_ACCOUNT sfEmitCallback
ripple::sfEmittedTxn
const SField sfEmittedTxn
ripple::sfEmitBurden
const SF_UINT64 sfEmitBurden
ripple::STI_OBJECT
@ STI_OBJECT
Definition: SField.h:67
ripple::sfHookNamespace
const SF_UINT256 sfHookNamespace
ripple::STI_UINT256
@ STI_UINT256
Definition: SField.h:62
ripple::Hooks_test
Definition: Hooks_test.cpp:28
ripple::sfHookSetTxnID
const SF_UINT256 sfHookSetTxnID