rippled
Loading...
Searching...
No Matches
ServerDefinitions_test.cpp
1//------------------------------------------------------------------------------
2/*
3 This file is part of rippled: https://github.com/ripple/rippled
4 Copyright (c) 2023 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/jss.h>
24
25namespace ripple {
26
27namespace test {
28
30{
31public:
32 void
34 {
35 testcase("server_definitions");
36
37 using namespace test::jtx;
38
39 {
40 Env env(*this);
41 auto const result = env.rpc("server_definitions");
42 BEAST_EXPECT(!result[jss::result].isMember(jss::error));
43 BEAST_EXPECT(result[jss::result][jss::status] == "success");
44 BEAST_EXPECT(result[jss::result].isMember(jss::FIELDS));
45 BEAST_EXPECT(result[jss::result].isMember(jss::LEDGER_ENTRY_TYPES));
46 BEAST_EXPECT(
47 result[jss::result].isMember(jss::TRANSACTION_RESULTS));
48 BEAST_EXPECT(result[jss::result].isMember(jss::TRANSACTION_TYPES));
49 BEAST_EXPECT(result[jss::result].isMember(jss::TYPES));
50 BEAST_EXPECT(result[jss::result].isMember(jss::hash));
51
52 // test a random element of each result
53 // (testing the whole output would be difficult to maintain)
54
55 {
56 auto const firstField = result[jss::result][jss::FIELDS][0u];
57 BEAST_EXPECT(firstField[0u].asString() == "Generic");
58 BEAST_EXPECT(
59 firstField[1][jss::isSerialized].asBool() == false);
60 BEAST_EXPECT(
61 firstField[1][jss::isSigningField].asBool() == false);
62 BEAST_EXPECT(firstField[1][jss::isVLEncoded].asBool() == false);
63 BEAST_EXPECT(firstField[1][jss::nth].asUInt() == 0);
64 BEAST_EXPECT(firstField[1][jss::type].asString() == "Unknown");
65 }
66
67 BEAST_EXPECT(
68 result[jss::result][jss::LEDGER_ENTRY_TYPES]["AccountRoot"]
69 .asUInt() == 97);
70 BEAST_EXPECT(
71 result[jss::result][jss::TRANSACTION_RESULTS]["tecDIR_FULL"]
72 .asUInt() == 121);
73 BEAST_EXPECT(
74 result[jss::result][jss::TRANSACTION_TYPES]["Payment"]
75 .asUInt() == 0);
76 BEAST_EXPECT(
77 result[jss::result][jss::TYPES]["AccountID"].asUInt() == 8);
78
79 // check exception SFields
80 {
81 auto const fieldExists = [&](std::string name) {
82 for (auto& field : result[jss::result][jss::FIELDS])
83 {
84 if (field[0u].asString() == name)
85 {
86 return true;
87 }
88 }
89 return false;
90 };
91 BEAST_EXPECT(fieldExists("Generic"));
92 BEAST_EXPECT(fieldExists("Invalid"));
93 BEAST_EXPECT(fieldExists("ObjectEndMarker"));
94 BEAST_EXPECT(fieldExists("ArrayEndMarker"));
95 BEAST_EXPECT(fieldExists("taker_gets_funded"));
96 BEAST_EXPECT(fieldExists("taker_pays_funded"));
97 BEAST_EXPECT(fieldExists("hash"));
98 BEAST_EXPECT(fieldExists("index"));
99 }
100
101 // test that base_uint types are replaced with "Hash" prefix
102 {
103 auto const types = result[jss::result][jss::TYPES];
104 BEAST_EXPECT(types["Hash128"].asUInt() == 4);
105 BEAST_EXPECT(types["Hash160"].asUInt() == 17);
106 BEAST_EXPECT(types["Hash192"].asUInt() == 21);
107 BEAST_EXPECT(types["Hash256"].asUInt() == 5);
108 BEAST_EXPECT(types["Hash384"].asUInt() == 22);
109 BEAST_EXPECT(types["Hash512"].asUInt() == 23);
110 }
111 }
112
113 // test providing the same hash
114 {
115 Env env(*this);
116 auto const firstResult = env.rpc("server_definitions");
117 auto const hash = firstResult[jss::result][jss::hash].asString();
118 auto const hashParam =
119 std::string("{ ") + "\"hash\": \"" + hash + "\"}";
120
121 auto const result =
122 env.rpc("json", "server_definitions", hashParam);
123 BEAST_EXPECT(!result[jss::result].isMember(jss::error));
124 BEAST_EXPECT(result[jss::result][jss::status] == "success");
125 BEAST_EXPECT(!result[jss::result].isMember(jss::FIELDS));
126 BEAST_EXPECT(
127 !result[jss::result].isMember(jss::LEDGER_ENTRY_TYPES));
128 BEAST_EXPECT(
129 !result[jss::result].isMember(jss::TRANSACTION_RESULTS));
130 BEAST_EXPECT(!result[jss::result].isMember(jss::TRANSACTION_TYPES));
131 BEAST_EXPECT(!result[jss::result].isMember(jss::TYPES));
132 BEAST_EXPECT(result[jss::result].isMember(jss::hash));
133 }
134
135 // test providing a different hash
136 {
137 Env env(*this);
138 std::string const hash =
139 "54296160385A27154BFA70A239DD8E8FD4CC2DB7BA32D970BA3A5B132CF749"
140 "D1";
141 auto const hashParam =
142 std::string("{ ") + "\"hash\": \"" + hash + "\"}";
143
144 auto const result =
145 env.rpc("json", "server_definitions", hashParam);
146 BEAST_EXPECT(!result[jss::result].isMember(jss::error));
147 BEAST_EXPECT(result[jss::result][jss::status] == "success");
148 BEAST_EXPECT(result[jss::result].isMember(jss::FIELDS));
149 BEAST_EXPECT(result[jss::result].isMember(jss::LEDGER_ENTRY_TYPES));
150 BEAST_EXPECT(
151 result[jss::result].isMember(jss::TRANSACTION_RESULTS));
152 BEAST_EXPECT(result[jss::result].isMember(jss::TRANSACTION_TYPES));
153 BEAST_EXPECT(result[jss::result].isMember(jss::TYPES));
154 BEAST_EXPECT(result[jss::result].isMember(jss::hash));
155 }
156 }
157
158 void
159 run() override
160 {
162 }
163};
164
165BEAST_DEFINE_TESTSUITE(ServerDefinitions, rpc, ripple);
166
167} // namespace test
168} // namespace ripple
std::string asString() const
Returns the unquoted string value.
A testsuite class.
Definition suite.h:55
testcase_t testcase
Memberspace for declaring test cases.
Definition suite.h:155
A transaction testing environment.
Definition Env.h:121
Json::Value rpc(unsigned apiVersion, std::unordered_map< std::string, std::string > const &headers, std::string const &cmd, Args &&... args)
Execute an RPC command.
Definition Env.h:791
Set the expected result code for a JTx The test will fail if the code doesn't match.
Definition rpc.h:35
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:25