xrpld
Loading...
Searching...
No Matches
SpecBridge.cpp
1#include <xrpld/rpc/detail/SpecBridge.hpp>
2
3#include <xrpld/rpc/detail/JsonObjectView.hpp>
4
5#include <xrpl/json/json_reader.h>
6#include <xrpl/json/json_value.h>
7#include <xrpl/protocol/ErrorCodes.h>
8#include <xrpl/protocol/jss.h>
9
10#include <gtest/gtest.h>
11#include <rpcspec/Errors.hpp>
12#include <rpcspec/Types.hpp>
13#include <rpcspec/handlers/ledger/Spec.hpp>
14
15#include <cstdint>
16#include <string>
17#include <string_view>
18#include <variant>
19
20namespace xrpl {
21
22namespace {
23
24using ObjectView = rpc::JsonObjectView;
25
26json::Value
27makeParams()
28{
29 static constexpr auto kParams = R"JSON({
30 "str": "hello",
31 "num": 42,
32 "big": 3000000000,
33 "neg": -7,
34 "real": 1.5,
35 "flag": true,
36 "nothing": null,
37 "obj": {"inner": "deep"},
38 "arr": ["a", "b"]
39 })JSON";
40
41 json::Value params;
42 EXPECT_TRUE(json::Reader{}.parse(kParams, params));
43
44 return params;
45}
46
47} // namespace
48
49TEST(JsonView, field_types)
50{
51 auto params = makeParams();
52 ObjectView const root{params};
53
54 auto const str = root.child("str");
55 EXPECT_TRUE(str.present());
56 EXPECT_EQ(str.key(), "str");
57 EXPECT_TRUE(str.isString());
58 EXPECT_FALSE(str.isNull());
59 EXPECT_EQ(str.asString(), "hello");
60 EXPECT_TRUE(str.is<std::string>());
61 EXPECT_FALSE(str.isBool());
62 EXPECT_FALSE(str.isInt64());
63 EXPECT_FALSE(str.isObject());
64 EXPECT_FALSE(str.isArray());
65
66 // uint32-ness is about the value, not the storage: a non-negative Int answers true.
67 auto const num = root.child("num");
68 EXPECT_TRUE(num.isInt64());
69 EXPECT_EQ(num.asInt64(), 42);
70 EXPECT_TRUE(num.isUint32());
71 EXPECT_EQ(num.asUint32(), 42U);
72 EXPECT_TRUE(num.is<std::int64_t>());
73 EXPECT_TRUE(num.is<std::uint32_t>());
74
75 auto const big = root.child("big");
76 EXPECT_TRUE(big.isInt64());
77 EXPECT_EQ(big.asInt64(), 3000000000LL);
78 EXPECT_TRUE(big.isUint32());
79 EXPECT_EQ(big.asUint32(), 3000000000U);
80
81 auto const neg = root.child("neg");
82 EXPECT_TRUE(neg.isInt64());
83 EXPECT_EQ(neg.asInt64(), -7);
84 EXPECT_FALSE(neg.isUint32());
85 EXPECT_FALSE(neg.is<std::uint32_t>());
86
87 auto const real = root.child("real");
88 EXPECT_TRUE(real.isDouble());
89 EXPECT_EQ(real.asDouble(), 1.5);
90 EXPECT_TRUE(real.is<double>());
91 EXPECT_FALSE(real.isInt64());
92
93 auto const flag = root.child("flag");
94 EXPECT_TRUE(flag.isBool());
95 EXPECT_TRUE(flag.asBool());
96 EXPECT_TRUE(flag.is<bool>());
97
98 // A boolean is not a number, though json::Value::isIntegral() says otherwise.
99 EXPECT_FALSE(flag.isInt64());
100 EXPECT_FALSE(flag.isUint32());
101
102 // Present, but null: no type predicate claims it.
103 auto const nothing = root.child("nothing");
104 EXPECT_TRUE(nothing.present());
105 EXPECT_TRUE(nothing.isNull());
106 EXPECT_FALSE(nothing.isString());
107 EXPECT_FALSE(nothing.isInt64());
108 EXPECT_FALSE(nothing.isBool());
109 EXPECT_FALSE(nothing.isDouble());
110 EXPECT_FALSE(nothing.isObject());
111 EXPECT_FALSE(nothing.isArray());
112
113 auto const obj = root.child("obj");
114 EXPECT_TRUE(obj.isObject());
115 EXPECT_TRUE(obj.is<::rpc::spec::JsonObject>());
116 EXPECT_EQ(obj.objectSize(), 1U);
117 EXPECT_EQ(obj.arraySize(), 0U);
118
119 auto const arr = root.child("arr");
120 EXPECT_TRUE(arr.isArray());
121 EXPECT_TRUE(arr.is<::rpc::spec::JsonArray>());
122 EXPECT_EQ(arr.arraySize(), 2U);
123 EXPECT_EQ(arr.objectSize(), 0U);
124}
125
126TEST(JsonView, field_absent)
127{
128 auto params = makeParams();
129 ObjectView const root{params};
130
131 // Every predicate has to answer for an absent field: that is how a spec asks whether
132 // an optional field was supplied.
133 auto const missing = root.child("nope");
134 EXPECT_FALSE(missing.present());
135 EXPECT_FALSE(missing.isNull());
136 EXPECT_EQ(missing.key(), "nope");
137 EXPECT_FALSE(missing.isString());
138 EXPECT_FALSE(missing.isInt64());
139 EXPECT_FALSE(missing.isUint32());
140 EXPECT_FALSE(missing.isBool());
141 EXPECT_FALSE(missing.isDouble());
142 EXPECT_FALSE(missing.isObject());
143 EXPECT_FALSE(missing.isArray());
144 EXPECT_EQ(missing.arraySize(), 0U);
145 EXPECT_EQ(missing.objectSize(), 0U);
146 EXPECT_FALSE(missing.is<std::string>());
147 EXPECT_FALSE(missing.is<::rpc::spec::JsonObject>());
148
149 // Walking down from an absent field stays absent rather than faulting.
150 EXPECT_FALSE(missing.child("deeper").present());
151 EXPECT_FALSE(missing.element(0).present());
152
153 // Same for a non-object asked for a child, or a non-array for an element.
154 EXPECT_FALSE(root.child("str").child("inner").present());
155 EXPECT_FALSE(root.child("str").element(0).present());
156}
157
158TEST(JsonView, field_navigation)
159{
160 auto params = makeParams();
161 ObjectView const root{params};
162
163 auto const inner = root.child("obj").child("inner");
164 EXPECT_TRUE(inner.present());
165 EXPECT_EQ(inner.asString(), "deep");
166 EXPECT_EQ(inner.key(), "inner");
167
168 // An element borrows its parent's key.
169 auto const first = root.child("arr").element(0);
170 EXPECT_TRUE(first.present());
171 EXPECT_EQ(first.asString(), "a");
172 EXPECT_EQ(first.key(), "arr");
173 EXPECT_EQ(root.child("arr").element(1).asString(), "b");
174 EXPECT_FALSE(root.child("arr").element(2).present());
175}
176
177TEST(JsonView, field_set)
178{
179 auto params = makeParams();
180 ObjectView root{params};
181
182 // What a modifier does: normalise in place, so the converter after it sees that.
183 root.child("str").set(std::string_view{"world"});
184 EXPECT_EQ(params["str"].asString(), "world");
185
186 root.child("num").set(std::uint32_t{7});
187 EXPECT_EQ(params["num"].asUInt(), 7U);
188
189 root.child("neg").set(std::int64_t{-1});
190 EXPECT_EQ(params["neg"].asInt(), -1);
191
192 root.child("flag").set(false);
193 EXPECT_FALSE(params["flag"].asBool());
194
195 root.child("real").set(2.5);
196 EXPECT_EQ(params["real"].asDouble(), 2.5);
197
198 // Write access reaches through a child.
199 root.child("obj").child("inner").set(std::string_view{"changed"});
200 EXPECT_EQ(params["obj"]["inner"].asString(), "changed");
201}
202
203TEST(JsonView, field_set_spans_both_32_bit_types)
204{
205 auto params = makeParams();
206 ObjectView root{params};
207
208 // The spec's toNumber modifier produces values up to UINT32_MAX. json::Int is 32 bits,
209 // so routing everything through it would wrap those negative and lose the value.
210 root.child("num").set(std::int64_t{3000000000});
211 auto const big = root.child("num");
212 EXPECT_TRUE(big.isUint32());
213 EXPECT_EQ(big.asUint32(), 3000000000U);
214 EXPECT_EQ(big.asInt64(), 3000000000LL);
215
216 root.child("neg").set(std::int64_t{-2000000000});
217 auto const negative = root.child("neg");
218 EXPECT_TRUE(negative.isInt64());
219 EXPECT_EQ(negative.asInt64(), -2000000000LL);
220 EXPECT_FALSE(negative.isUint32());
221}
222
223TEST(JsonView, mutable_navigation)
224{
225 auto params = makeParams();
226 ObjectView root{params};
227
228 EXPECT_FALSE(root.child("nope").present());
229 EXPECT_FALSE(root.child("obj").child("nope").present());
230 EXPECT_FALSE(root.child("str").child("inner").present());
231 EXPECT_FALSE(root.child("arr").element(2).present());
232 EXPECT_FALSE(root.child("str").element(0).present());
233 EXPECT_FALSE(params.isMember("nope"));
234 EXPECT_FALSE(params["obj"].isMember("nope"));
235
236 auto second = root.child("arr").element(1);
237 ASSERT_TRUE(second.present());
238 EXPECT_EQ(second.key(), "arr");
239 second.set(std::string_view{"z"});
240 EXPECT_EQ(params["arr"][1u].asString(), "z");
241
243 ObjectView arrayRoot{array};
244 EXPECT_FALSE(arrayRoot.child("anything").present());
245}
246
247TEST(JsonView, object_root)
248{
249 auto params = makeParams();
250
251 ObjectView const root{params};
252 EXPECT_TRUE(root.isObject());
253 EXPECT_FALSE(root.isArray());
254 EXPECT_TRUE(root.child("str").present());
255 EXPECT_FALSE(root.child("nope").present());
256
257 // Must not create the key: the non-const operator[] would have inserted a null.
258 EXPECT_FALSE(params.isMember("nope"));
259
261 ObjectView const arrayRoot{array};
262 EXPECT_TRUE(arrayRoot.isArray());
263 EXPECT_FALSE(arrayRoot.isObject());
264 EXPECT_FALSE(arrayRoot.child("anything").present());
265}
266
267TEST(JsonView, mutable_root_over_const_source_reads)
268{
269 auto const params = makeParams();
270 ObjectView root{params};
271
272 auto const str = root.child("str");
273 ASSERT_TRUE(str.present());
274 EXPECT_EQ(str.asString(), "hello");
275 EXPECT_EQ(root.child("obj").child("inner").asString(), "deep");
276 EXPECT_EQ(root.child("arr").element(1).asString(), "b");
277 EXPECT_FALSE(root.child("nope").present());
278}
279
280TEST(SpecBridge, shared_spec_parses_json_value)
281{
283 params["ledger_index"] = 42U;
284 params["binary"] = true;
285 params["transactions"] = true;
286
287 auto const input = ::rpc::spec::handlers::ledger::kSpec.parse(params, 2);
288 ASSERT_TRUE(input.has_value());
289
290 EXPECT_TRUE(input->binary);
291 EXPECT_TRUE(input->transactions);
292 EXPECT_FALSE(input->expand);
293 ASSERT_TRUE(std::holds_alternative<std::uint32_t>(input->ledger.value));
294 EXPECT_EQ(std::get<std::uint32_t>(input->ledger.value), 42U);
295}
296
297TEST(SpecBridge, shared_spec_reports_a_bad_json_value)
298{
300 params["ledger_index"] = "not-a-ledger";
301
302 auto const input = ::rpc::spec::handlers::ledger::kSpec.parse(params, 2);
303 ASSERT_FALSE(input.has_value());
304 EXPECT_EQ(std::get<::rpc::XrpldError>(input.error().code), RpcInvalidParams);
305}
306
307TEST(SpecBridge, shared_spec_warns_on_a_deprecated_field)
308{
310 params["type"] = "hashes";
311
312 auto const warnings = ::rpc::spec::handlers::ledger::kSpec.check(params, 2);
313 ASSERT_EQ(warnings.size(), 1U);
314 EXPECT_EQ(warnings[0].code, ::rpc::WarningCode::WarnRpcDeprecated);
315}
316
317TEST(SpecBridge, inject_spec_error_uses_default_message)
318{
320 rpc::injectSpecError(out, ::rpc::Status{RpcLgrNotFound});
321 EXPECT_EQ(out, rpc::makeError(RpcLgrNotFound));
322}
323
324TEST(SpecBridge, inject_spec_error_uses_custom_message)
325{
327 rpc::injectSpecError(out, ::rpc::Status{RpcInvalidParams, "Invalid field 'ledger'."});
328 EXPECT_EQ(out, rpc::makeError(RpcInvalidParams, "Invalid field 'ledger'."));
329}
330
331TEST(SpecBridge, inject_spec_warnings)
332{
333 std::string const kDeprecatedBase{
334 ::rpc::getWarningInfo(::rpc::WarningCode::WarnRpcDeprecated).message};
335
336 {
338 rpc::injectSpecWarnings(out, {});
339 EXPECT_FALSE(out.isMember(jss::warnings));
340 }
341 {
343 rpc::injectSpecWarnings(
344 out,
345 {
346 {
347 .code = ::rpc::WarningCode::WarnRpcDeprecated,
348 .field = "type",
349 .message = "Field 'type' is deprecated.",
350 },
351 });
352
353 ASSERT_TRUE(out[jss::warnings].isArray());
354 ASSERT_EQ(out[jss::warnings].size(), 1U);
355 EXPECT_EQ(out[jss::warnings][0u][jss::id].asInt(), WarnRpcFieldsDeprecated);
356 EXPECT_EQ(
357 out[jss::warnings][0u][jss::message], kDeprecatedBase + " Field 'type' is deprecated.");
358 }
359 {
360 // One entry per code, both details appended.
362 rpc::injectSpecWarnings(
363 out,
364 {
365 {
366 .code = ::rpc::WarningCode::WarnRpcDeprecated,
367 .field = "type",
368 .message = "Field 'type' is deprecated.",
369 },
370 {
371 .code = ::rpc::WarningCode::WarnRpcDeprecated,
372 .field = "ledger",
373 .message = "Field 'ledger' is deprecated.",
374 },
375 });
376
377 ASSERT_EQ(out[jss::warnings].size(), 1U);
378 EXPECT_EQ(
379 out[jss::warnings][0u][jss::message],
380 kDeprecatedBase + " Field 'type' is deprecated. Field 'ledger' is deprecated.");
381 }
382 {
383 // Ordered by code, so the response does not depend on field visit order.
385 rpc::injectSpecWarnings(
386 out,
387 {
388 {
389 .code = ::rpc::WarningCode::WarnRpcDeprecated,
390 .field = "type",
391 .message = "Field 'type' is deprecated.",
392 },
393 {.code = ::rpc::WarningCode::WarnRpcOutdated, .field = "", .message = ""},
394 });
395
396 ASSERT_EQ(out[jss::warnings].size(), 2U);
397 EXPECT_EQ(
398 out[jss::warnings][0u][jss::id].asInt(),
399 static_cast<int>(::rpc::WarningCode::WarnRpcOutdated));
400 EXPECT_EQ(
401 out[jss::warnings][0u][jss::message],
402 std::string{::rpc::getWarningInfo(::rpc::WarningCode::WarnRpcOutdated).message});
403 EXPECT_EQ(out[jss::warnings][1u][jss::id].asInt(), WarnRpcFieldsDeprecated);
404 }
405 {
406 // A handler's own warnings are kept.
410 entry[jss::id] = WarnRpcUnsupportedMajority;
411 out[jss::warnings] = existing;
412
413 rpc::injectSpecWarnings(
414 out,
415 {
416 {
417 .code = ::rpc::WarningCode::WarnRpcDeprecated,
418 .field = "type",
419 .message = "Field 'type' is deprecated.",
420 },
421 });
422
423 ASSERT_EQ(out[jss::warnings].size(), 2U);
424 EXPECT_EQ(out[jss::warnings][0u][jss::id].asInt(), WarnRpcUnsupportedMajority);
425 EXPECT_EQ(out[jss::warnings][1u][jss::id].asInt(), WarnRpcFieldsDeprecated);
426 }
427}
428
429} // namespace xrpl
Represents a JSON value.
Definition json_value.h:121
Value & append(Value const &value)
Append value to array at the end.
bool isMember(char const *key) const
Return true if the object has a member named key.
T holds_alternative(T... args)
@ Array
array value (ordered list)
Definition json_value.h:32
@ Object
object value (collection of name/value pairs).
Definition json_value.h:33
json::Value makeError(ErrorCodeI code)
Returns a new json object that reflects the error code.
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
@ WarnRpcFieldsDeprecated
Definition ErrorCodes.h:162
@ WarnRpcUnsupportedMajority
Definition ErrorCodes.h:158
@ RpcLgrNotFound
Definition ErrorCodes.h:55
@ RpcInvalidParams
Definition ErrorCodes.h:67
TEST(FileUtilitiesTest, get_file_contents)
Number root(Number f, unsigned d)