xrpld
Loading...
Searching...
No Matches
STParsedJSON_test.cpp
1
2#include <test/jtx/Env.h>
3
4#include <xrpl/basics/base_uint.h>
5#include <xrpl/beast/unit_test/suite.h>
6#include <xrpl/json/json_forwards.h>
7#include <xrpl/json/json_reader.h>
8#include <xrpl/json/json_value.h>
9#include <xrpl/json/to_string.h>
10#include <xrpl/protocol/AccountID.h>
11#include <xrpl/protocol/SField.h>
12#include <xrpl/protocol/STAmount.h>
13#include <xrpl/protocol/STArray.h>
14#include <xrpl/protocol/STNumber.h>
15#include <xrpl/protocol/STParsedJSON.h>
16#include <xrpl/protocol/UintTypes.h>
17#include <xrpl/protocol/jss.h>
18
19#include <algorithm>
20#include <array>
21#include <cstddef>
22#include <cstdint>
23#include <memory>
24#include <stdexcept>
25#include <string>
26
27namespace xrpl {
28
30{
31 static bool
33 {
34 json::Reader reader;
35 return reader.parse(json, to) && to.isObject();
36 }
37
38 void
40 {
41 testcase("UInt8");
42 {
44 j[sfCloseResolution] = 255;
45 STParsedJSONObject obj("Test", j);
46 BEAST_EXPECT(obj.object.has_value());
47 // NOLINTNEXTLINE(bugprone-unchecked-optional-access)
48 BEAST_EXPECT(obj.object->isFieldPresent(sfCloseResolution));
49 // NOLINTNEXTLINE(bugprone-unchecked-optional-access)
50 BEAST_EXPECT(obj.object->getFieldU8(sfCloseResolution) == 255);
51 }
52
53 // test with uint value
54 {
56 j[sfCloseResolution] = 255u;
57 STParsedJSONObject obj("Test", j);
58 BEAST_EXPECT(obj.object.has_value());
59 // NOLINTNEXTLINE(bugprone-unchecked-optional-access)
60 BEAST_EXPECT(obj.object->isFieldPresent(sfCloseResolution));
61 // NOLINTNEXTLINE(bugprone-unchecked-optional-access)
62 BEAST_EXPECT(obj.object->getFieldU8(sfCloseResolution) == 255);
63 }
64
65 // Test with string value
66 {
68 j[sfCloseResolution] = "255";
69 STParsedJSONObject obj("Test", j);
70 BEAST_EXPECT(obj.object.has_value());
71 // NOLINTNEXTLINE(bugprone-unchecked-optional-access)
72 BEAST_EXPECT(obj.object->isFieldPresent(sfCloseResolution));
73 // NOLINTNEXTLINE(bugprone-unchecked-optional-access)
74 BEAST_EXPECT(obj.object->getFieldU8(sfCloseResolution) == 255);
75 }
76
77 // Test min value for uint8
78 {
80 j[sfCloseResolution] = 0;
81 STParsedJSONObject obj("Test", j);
82 BEAST_EXPECT(obj.object.has_value());
83 // NOLINTNEXTLINE(bugprone-unchecked-optional-access)
84 BEAST_EXPECT(obj.object->getFieldU8(sfCloseResolution) == 0);
85 }
86
87 // Test out of range value for UInt8 (negative)
88 {
90 j[sfCloseResolution] = -1;
91 STParsedJSONObject const obj("Test", j);
92 BEAST_EXPECT(!obj.object.has_value());
93 }
94
95 // Test out of range value for UInt8 (too large)
96 {
98 j[sfCloseResolution] = 256;
99 STParsedJSONObject const obj("Test", j);
100 BEAST_EXPECT(!obj.object.has_value());
101 }
102
103 // Test bad_type (not a string/int/uint)
104 {
105 json::Value j;
106 j[sfCloseResolution] = json::Value(json::ValueType::Array);
107 STParsedJSONObject const obj("Test", j);
108 BEAST_EXPECT(!obj.object.has_value());
109 }
110
111 // Test bad_type (not a string/int/uint)
112 {
113 json::Value j;
114 j[sfCloseResolution] = json::Value(json::ValueType::Object);
115 STParsedJSONObject const obj("Test", j);
116 BEAST_EXPECT(!obj.object.has_value());
117 }
118 }
119
120 void
122 {
123 testcase("UInt16");
124 // Test with int value
125 {
126 json::Value j;
127 j[sfLedgerEntryType] = 65535;
128 STParsedJSONObject obj("Test", j);
129 BEAST_EXPECT(obj.object.has_value());
130 // NOLINTNEXTLINE(bugprone-unchecked-optional-access)
131 BEAST_EXPECT(obj.object->isFieldPresent(sfLedgerEntryType));
132 // NOLINTNEXTLINE(bugprone-unchecked-optional-access)
133 BEAST_EXPECT(obj.object->getFieldU16(sfLedgerEntryType) == 65535);
134 }
135
136 // Test with uint value
137 {
138 json::Value j;
139 j[sfLedgerEntryType] = 65535u;
140 STParsedJSONObject obj("Test", j);
141 BEAST_EXPECT(obj.object.has_value());
142 // NOLINTNEXTLINE(bugprone-unchecked-optional-access)
143 BEAST_EXPECT(obj.object->isFieldPresent(sfLedgerEntryType));
144 // NOLINTNEXTLINE(bugprone-unchecked-optional-access)
145 BEAST_EXPECT(obj.object->getFieldU16(sfLedgerEntryType) == 65535);
146 }
147
148 // Test with string value
149 {
150 json::Value j;
151 j[sfLedgerEntryType] = "65535";
152 STParsedJSONObject obj("Test", j);
153 BEAST_EXPECT(obj.object.has_value());
154 // NOLINTNEXTLINE(bugprone-unchecked-optional-access)
155 BEAST_EXPECT(obj.object->isFieldPresent(sfLedgerEntryType));
156 // NOLINTNEXTLINE(bugprone-unchecked-optional-access)
157 BEAST_EXPECT(obj.object->getFieldU16(sfLedgerEntryType) == 65535);
158 }
159
160 // Test min value for uint16
161 {
162 json::Value j;
163 j[sfLedgerEntryType] = 0;
164 STParsedJSONObject obj("Test", j);
165 BEAST_EXPECT(obj.object.has_value());
166 // NOLINTNEXTLINE(bugprone-unchecked-optional-access)
167 BEAST_EXPECT(obj.object->getFieldU16(sfLedgerEntryType) == 0);
168 }
169
170 // Test out of range value for UInt16 (negative)
171 {
172 json::Value j;
173 j[sfLedgerEntryType] = -1;
174 STParsedJSONObject const obj("Test", j);
175 BEAST_EXPECT(!obj.object.has_value());
176 }
177
178 // Test out of range value for UInt16 (too large)
179 {
180 json::Value j;
181 j[sfLedgerEntryType] = 65536;
182 STParsedJSONObject const obj("Test", j);
183 BEAST_EXPECT(!obj.object.has_value());
184 }
185
186 // Test string value out of range
187 {
188 json::Value j;
189 j[sfLedgerEntryType] = "65536";
190 STParsedJSONObject const obj("Test", j);
191 BEAST_EXPECT(!obj.object.has_value());
192 }
193
194 // Test bad_type (not a string/int/uint)
195 {
196 json::Value j;
197 j[sfLedgerEntryType] = json::Value(json::ValueType::Array);
198 STParsedJSONObject const obj("Test", j);
199 BEAST_EXPECT(!obj.object.has_value());
200 }
201
202 // Test bad_type (not a string/int/uint)
203 {
204 json::Value j;
205 j[sfLedgerEntryType] = json::Value(json::ValueType::Object);
206 STParsedJSONObject const obj("Test", j);
207 BEAST_EXPECT(!obj.object.has_value());
208 }
209
210 // Invalid input for other field
211 {
212 json::Value j;
213 j[sfTransferFee] = "Payment";
214 STParsedJSONObject const obj("Test", j);
215 BEAST_EXPECT(!obj.object.has_value());
216 }
217 }
218
219 void
221 {
222 testcase("UInt32");
223 {
224 json::Value j;
225 j[sfNetworkID] = 4294967295u;
226 STParsedJSONObject obj("Test", j);
227 BEAST_EXPECT(obj.object.has_value());
228 // NOLINTNEXTLINE(bugprone-unchecked-optional-access)
229 BEAST_EXPECT(obj.object->isFieldPresent(sfNetworkID));
230 // NOLINTNEXTLINE(bugprone-unchecked-optional-access)
231 BEAST_EXPECT(obj.object->getFieldU32(sfNetworkID) == 4294967295u);
232 }
233
234 // Test with string value
235 {
236 json::Value j;
237 j[sfNetworkID] = "4294967295";
238 STParsedJSONObject obj("Test", j);
239 BEAST_EXPECT(obj.object.has_value());
240 // NOLINTNEXTLINE(bugprone-unchecked-optional-access)
241 BEAST_EXPECT(obj.object->isFieldPresent(sfNetworkID));
242 // NOLINTNEXTLINE(bugprone-unchecked-optional-access)
243 BEAST_EXPECT(obj.object->getFieldU32(sfNetworkID) == 4294967295u);
244 }
245
246 // Test min value for uint32
247 {
248 json::Value j;
249 j[sfNetworkID] = 0;
250 STParsedJSONObject obj("Test", j);
251 BEAST_EXPECT(obj.object.has_value());
252 // NOLINTNEXTLINE(bugprone-unchecked-optional-access)
253 BEAST_EXPECT(obj.object->getFieldU32(sfNetworkID) == 0);
254 }
255
256 // Test out of range value for uint32 (negative)
257 {
258 json::Value j;
259 j[sfNetworkID] = -1;
260 STParsedJSONObject const obj("Test", j);
261 BEAST_EXPECT(!obj.object.has_value());
262 }
263
264 // Test string value out of range
265 {
266 json::Value j;
267 j[sfNetworkID] = "4294967296";
268 STParsedJSONObject const obj("Test", j);
269 BEAST_EXPECT(!obj.object.has_value());
270 }
271
272 // Test bad_type (arrayValue)
273 {
274 json::Value j;
275 j[sfNetworkID] = json::Value(json::ValueType::Array);
276 STParsedJSONObject const obj("Test", j);
277 BEAST_EXPECT(!obj.object.has_value());
278 }
279
280 // Test bad_type (objectValue)
281 {
282 json::Value j;
283 j[sfNetworkID] = json::Value(json::ValueType::Object);
284 STParsedJSONObject const obj("Test", j);
285 BEAST_EXPECT(!obj.object.has_value());
286 }
287 }
288
289 void
291 {
292 testcase("UInt64");
293 {
294 json::Value j;
295 j[sfIndexNext] = "ffffffffffffffff";
296 STParsedJSONObject obj("Test", j);
297 BEAST_EXPECT(obj.object.has_value());
298 // NOLINTNEXTLINE(bugprone-unchecked-optional-access)
299 BEAST_EXPECT(obj.object->isFieldPresent(sfIndexNext));
300 // NOLINTNEXTLINE(bugprone-unchecked-optional-access)
301 BEAST_EXPECT(obj.object->getFieldU64(sfIndexNext) == 18446744073709551615ull);
302 }
303
304 // Test min value for uint64
305 {
306 json::Value j;
307 j[sfIndexNext] = 0;
308 STParsedJSONObject obj("Test", j);
309 BEAST_EXPECT(obj.object.has_value());
310 // NOLINTNEXTLINE(bugprone-unchecked-optional-access)
311 BEAST_EXPECT(obj.object->getFieldU64(sfIndexNext) == 0ull);
312 }
313
314 // Test out of range value for uint64 (negative)
315 {
316 json::Value j;
317 j[sfIndexNext] = -1;
318 STParsedJSONObject const obj("Test", j);
319 BEAST_EXPECT(!obj.object.has_value());
320 }
321
322 // NOTE: the JSON parser doesn't support > UInt32, so those values must
323 // be in hex
324 // Test string value out of range
325 // string is interpreted as hex
326 {
327 json::Value j;
328 j[sfIndexNext] = "10000000000000000"; // uint64 max + 1 (in hex)
329 STParsedJSONObject const obj("Test", j);
330 BEAST_EXPECT(!obj.object.has_value());
331 }
332
333 // Test hex string value with 0x prefix (should fail)
334 {
335 json::Value j;
336 j[sfIndexNext] = "0xabcdefabcdef";
337 STParsedJSONObject const obj("Test", j);
338 BEAST_EXPECT(!obj.object.has_value());
339 }
340
341 // Test hex string value with invalid characters
342 {
343 json::Value j;
344 j[sfIndexNext] = "abcdefga";
345 STParsedJSONObject const obj("Test", j);
346 BEAST_EXPECT(!obj.object.has_value());
347 }
348
349 // test arrayValue
350 {
351 json::Value j;
352 j[sfIndexNext] = json::Value(json::ValueType::Array);
353 STParsedJSONObject const obj("Test", j);
354 BEAST_EXPECT(!obj.object.has_value());
355 }
356
357 // test objectValue
358 {
359 json::Value j;
360 j[sfIndexNext] = json::Value(json::ValueType::Object);
361 STParsedJSONObject const obj("Test", j);
362 BEAST_EXPECT(!obj.object.has_value());
363 }
364 }
365
366 void
368 {
369 testcase("UInt128");
370 {
371 json::Value j;
372 j[sfEmailHash] = "0123456789ABCDEF0123456789ABCDEF";
373 STParsedJSONObject obj("Test", j);
374 BEAST_EXPECT(obj.object.has_value());
375 // NOLINTNEXTLINE(bugprone-unchecked-optional-access)
376 BEAST_EXPECT(obj.object->isFieldPresent(sfEmailHash));
377 // NOLINTNEXTLINE(bugprone-unchecked-optional-access)
378 BEAST_EXPECT(obj.object->getFieldH128(sfEmailHash).size() == 16);
379 std::array<uint8_t, 16> const expected = {
380 0x01,
381 0x23,
382 0x45,
383 0x67,
384 0x89,
385 0xAB,
386 0xCD,
387 0xEF,
388 0x01,
389 0x23,
390 0x45,
391 0x67,
392 0x89,
393 0xAB,
394 0xCD,
395 0xEF,
396 };
397 // NOLINTNEXTLINE(bugprone-unchecked-optional-access)
398 BEAST_EXPECT(obj.object->getFieldH128(sfEmailHash) == UInt128::fromRaw(expected));
399 }
400
401 // Valid lowercase hex string for UInt128
402 {
403 json::Value j;
404 j[sfEmailHash] = "0123456789abcdef0123456789abcdef";
405 STParsedJSONObject obj("Test", j);
406 BEAST_EXPECT(obj.object.has_value());
407 // NOLINTNEXTLINE(bugprone-unchecked-optional-access)
408 BEAST_EXPECT(obj.object->isFieldPresent(sfEmailHash));
409 // NOLINTNEXTLINE(bugprone-unchecked-optional-access)
410 BEAST_EXPECT(obj.object->getFieldH128(sfEmailHash).size() == 16);
411 }
412
413 // Empty string for UInt128 (should be valid, all zero)
414 {
415 json::Value j;
416 j[sfEmailHash] = "";
417 STParsedJSONObject obj("Test", j);
418 BEAST_EXPECT(obj.object.has_value());
419 // NOLINTNEXTLINE(bugprone-unchecked-optional-access)
420 BEAST_EXPECT(obj.object->isFieldPresent(sfEmailHash));
421 // NOLINTNEXTLINE(bugprone-unchecked-optional-access)
422 auto const& h128 = obj.object->getFieldH128(sfEmailHash);
423 BEAST_EXPECT(h128.size() == 16);
424 bool const allZero = std::ranges::all_of(h128, [](auto b) { return b == 0; });
425 BEAST_EXPECT(allZero);
426 }
427
428 // Odd-length hex string for UInt128 (should fail)
429 {
430 json::Value j;
431 j[sfEmailHash] = "0123456789ABCDEF0123456789ABCDE";
432 STParsedJSONObject const obj("Test", j);
433 BEAST_EXPECT(!obj.object.has_value());
434 }
435
436 // Non-hex string for UInt128 (should fail)
437 {
438 json::Value j;
439 j[sfEmailHash] = "nothexstring";
440 STParsedJSONObject const obj("Test", j);
441 BEAST_EXPECT(!obj.object.has_value());
442 }
443
444 // Hex string too short for UInt128 (should fail)
445 {
446 json::Value j;
447 j[sfEmailHash] = "01234567";
448 STParsedJSONObject const obj("Test", j);
449 BEAST_EXPECT(!obj.object.has_value());
450 }
451
452 // Hex string too long for UInt128 (should fail)
453 {
454 json::Value j;
455 j[sfEmailHash] = "0123456789ABCDEF0123456789ABCDEF00";
456 STParsedJSONObject const obj("Test", j);
457 BEAST_EXPECT(!obj.object.has_value());
458 }
459
460 // Array value for UInt128 (should fail)
461 {
462 json::Value j;
463 j[sfEmailHash] = json::Value(json::ValueType::Array);
464 STParsedJSONObject const obj("Test", j);
465 BEAST_EXPECT(!obj.object.has_value());
466 }
467
468 // Object value for UInt128 (should fail)
469 {
470 json::Value j;
471 j[sfEmailHash] = json::Value(json::ValueType::Object);
472 STParsedJSONObject const obj("Test", j);
473 BEAST_EXPECT(!obj.object.has_value());
474 }
475 }
476
477 void
479 {
480 testcase("UInt160");
481 {
482 json::Value j;
483 j[sfTakerPaysCurrency] = "0123456789ABCDEF0123456789ABCDEF01234567";
484 STParsedJSONObject obj("Test", j);
485 BEAST_EXPECT(obj.object.has_value());
486 // NOLINTNEXTLINE(bugprone-unchecked-optional-access)
487 BEAST_EXPECT(obj.object->isFieldPresent(sfTakerPaysCurrency));
488 // NOLINTNEXTLINE(bugprone-unchecked-optional-access)
489 BEAST_EXPECT(obj.object->getFieldH160(sfTakerPaysCurrency).size() == 20);
490 std::array<uint8_t, 20> const expected = {
491 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF, 0x01, 0x23,
492 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF, 0x01, 0x23, 0x45, 0x67,
493 };
494 BEAST_EXPECT(
495 // NOLINTNEXTLINE(bugprone-unchecked-optional-access)
496 obj.object->getFieldH160(sfTakerPaysCurrency) == UInt160::fromRaw(expected));
497 }
498 // Valid lowercase hex string for UInt160
499 {
500 json::Value j;
501 j[sfTakerPaysCurrency] = "0123456789abcdef0123456789abcdef01234567";
502 STParsedJSONObject obj("Test", j);
503 BEAST_EXPECT(obj.object.has_value());
504 // NOLINTNEXTLINE(bugprone-unchecked-optional-access)
505 BEAST_EXPECT(obj.object->isFieldPresent(sfTakerPaysCurrency));
506 // NOLINTNEXTLINE(bugprone-unchecked-optional-access)
507 BEAST_EXPECT(obj.object->getFieldH160(sfTakerPaysCurrency).size() == 20);
508 }
509
510 // Empty string for UInt160 (should be valid, all zero)
511 {
512 json::Value j;
513 j[sfTakerPaysCurrency] = "";
514 STParsedJSONObject obj("Test", j);
515 BEAST_EXPECT(obj.object.has_value());
516 // NOLINTNEXTLINE(bugprone-unchecked-optional-access)
517 BEAST_EXPECT(obj.object->isFieldPresent(sfTakerPaysCurrency));
518 // NOLINTNEXTLINE(bugprone-unchecked-optional-access)
519 auto const& h160 = obj.object->getFieldH160(sfTakerPaysCurrency);
520 BEAST_EXPECT(h160.size() == 20);
521 bool const allZero = std::ranges::all_of(h160, [](auto b) { return b == 0; });
522 BEAST_EXPECT(allZero);
523 }
524
525 // Non-hex string for UInt160 (should fail)
526 {
527 json::Value j;
528 j[sfTakerPaysCurrency] = "nothexstring";
529 STParsedJSONObject const obj("Test", j);
530 BEAST_EXPECT(!obj.object.has_value());
531 }
532
533 // Hex string too short for UInt160 (should fail)
534 {
535 json::Value j;
536 j[sfTakerPaysCurrency] = "01234567";
537 STParsedJSONObject const obj("Test", j);
538 BEAST_EXPECT(!obj.object.has_value());
539 }
540
541 // Hex string too long for UInt160 (should fail)
542 {
543 json::Value j;
544 j[sfTakerPaysCurrency] = "0123456789ABCDEF0123456789ABCDEF0123456789";
545 STParsedJSONObject const obj("Test", j);
546 BEAST_EXPECT(!obj.object.has_value());
547 }
548
549 // Array value for UInt160 (should fail)
550 {
551 json::Value j;
552 j[sfTakerPaysCurrency] = json::Value(json::ValueType::Array);
553 STParsedJSONObject const obj("Test", j);
554 BEAST_EXPECT(!obj.object.has_value());
555 }
556
557 // Object value for UInt160 (should fail)
558 {
559 json::Value j;
560 j[sfTakerPaysCurrency] = json::Value(json::ValueType::Object);
561 STParsedJSONObject const obj("Test", j);
562 BEAST_EXPECT(!obj.object.has_value());
563 }
564 }
565
566 void
568 {
569 testcase("UInt192");
570 {
571 json::Value j;
572 j[sfMPTokenIssuanceID] = "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF";
573 STParsedJSONObject obj("Test", j);
574 BEAST_EXPECT(obj.object.has_value());
575 // NOLINTNEXTLINE(bugprone-unchecked-optional-access)
576 BEAST_EXPECT(obj.object->isFieldPresent(sfMPTokenIssuanceID));
577 // NOLINTNEXTLINE(bugprone-unchecked-optional-access)
578 BEAST_EXPECT(obj.object->getFieldH192(sfMPTokenIssuanceID).size() == 24);
579 std::array<uint8_t, 24> const expected = {
580 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
581 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
582 };
583 BEAST_EXPECT(
584 // NOLINTNEXTLINE(bugprone-unchecked-optional-access)
585 obj.object->getFieldH192(sfMPTokenIssuanceID) == UInt192::fromRaw(expected));
586 }
587
588 // Valid lowercase hex string for UInt192
589 {
590 json::Value j;
591 j[sfMPTokenIssuanceID] = "ffffffffffffffffffffffffffffffffffffffffffffffff";
592 STParsedJSONObject obj("Test", j);
593 BEAST_EXPECT(obj.object.has_value());
594 // NOLINTNEXTLINE(bugprone-unchecked-optional-access)
595 BEAST_EXPECT(obj.object->isFieldPresent(sfMPTokenIssuanceID));
596 // NOLINTNEXTLINE(bugprone-unchecked-optional-access)
597 BEAST_EXPECT(obj.object->getFieldH192(sfMPTokenIssuanceID).size() == 24);
598 }
599
600 // Empty string for UInt192 (should be valid, all zero)
601 {
602 json::Value j;
603 j[sfMPTokenIssuanceID] = "";
604 STParsedJSONObject obj("Test", j);
605 BEAST_EXPECT(obj.object.has_value());
606 // NOLINTNEXTLINE(bugprone-unchecked-optional-access)
607 BEAST_EXPECT(obj.object->isFieldPresent(sfMPTokenIssuanceID));
608 // NOLINTNEXTLINE(bugprone-unchecked-optional-access)
609 auto const& h192 = obj.object->getFieldH192(sfMPTokenIssuanceID);
610 BEAST_EXPECT(h192.size() == 24);
611 bool const allZero = std::ranges::all_of(h192, [](auto b) { return b == 0; });
612 BEAST_EXPECT(allZero);
613 }
614
615 // Odd-length hex string for UInt192 (should fail)
616 {
617 json::Value j;
618 j[sfMPTokenIssuanceID] = "0123456789ABCDEF0123456789ABCDEF0123456789ABCDE";
619 STParsedJSONObject const obj("Test", j);
620 BEAST_EXPECT(!obj.object.has_value());
621 }
622
623 // Non-hex string for UInt192 (should fail)
624 {
625 json::Value j;
626 j[sfMPTokenIssuanceID] = "nothexstring";
627 STParsedJSONObject const obj("Test", j);
628 BEAST_EXPECT(!obj.object.has_value());
629 }
630
631 // Hex string too short for UInt192 (should fail)
632 {
633 json::Value j;
634 j[sfMPTokenIssuanceID] = "01234567";
635 STParsedJSONObject const obj("Test", j);
636 BEAST_EXPECT(!obj.object.has_value());
637 }
638
639 // Hex string too long for UInt192 (should fail)
640 {
641 json::Value j;
642 j[sfMPTokenIssuanceID] = "0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF00";
643 STParsedJSONObject const obj("Test", j);
644 BEAST_EXPECT(!obj.object.has_value());
645 }
646
647 // Array value for UInt192 (should fail)
648 {
649 json::Value j;
650 j[sfMPTokenIssuanceID] = json::Value(json::ValueType::Array);
651 STParsedJSONObject const obj("Test", j);
652 BEAST_EXPECT(!obj.object.has_value());
653 }
654
655 // Object value for UInt192 (should fail)
656 {
657 json::Value j;
658 j[sfMPTokenIssuanceID] = json::Value(json::ValueType::Object);
659 STParsedJSONObject const obj("Test", j);
660 BEAST_EXPECT(!obj.object.has_value());
661 }
662 }
663
664 void
666 {
667 testcase("UInt256");
668 // Test with valid hex string for UInt256
669 {
670 json::Value j;
671 j[sfLedgerHash] =
672 "0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCD"
673 "EF";
674 STParsedJSONObject obj("Test", j);
675 BEAST_EXPECT(obj.object.has_value());
676 // NOLINTNEXTLINE(bugprone-unchecked-optional-access)
677 BEAST_EXPECT(obj.object->isFieldPresent(sfLedgerHash));
678 // NOLINTNEXTLINE(bugprone-unchecked-optional-access)
679 BEAST_EXPECT(obj.object->getFieldH256(sfLedgerHash).size() == 32);
680 std::array<uint8_t, 32> const expected = {
681 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF, 0x01, 0x23, 0x45,
682 0x67, 0x89, 0xAB, 0xCD, 0xEF, 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB,
683 0xCD, 0xEF, 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF,
684 };
685 // NOLINTNEXTLINE(bugprone-unchecked-optional-access)
686 BEAST_EXPECT(obj.object->getFieldH256(sfLedgerHash) == UInt256::fromRaw(expected));
687 }
688 // Valid lowercase hex string for UInt256
689 {
690 json::Value j;
691 j[sfLedgerHash] =
692 "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcd"
693 "ef";
694 STParsedJSONObject obj("Test", j);
695 BEAST_EXPECT(obj.object.has_value());
696 // NOLINTNEXTLINE(bugprone-unchecked-optional-access)
697 BEAST_EXPECT(obj.object->isFieldPresent(sfLedgerHash));
698 // NOLINTNEXTLINE(bugprone-unchecked-optional-access)
699 BEAST_EXPECT(obj.object->getFieldH256(sfLedgerHash).size() == 32);
700 }
701
702 // Empty string for UInt256 (should be valid, all zero)
703 {
704 json::Value j;
705 j[sfLedgerHash] = "";
706 STParsedJSONObject obj("Test", j);
707 BEAST_EXPECT(obj.object.has_value());
708 // NOLINTNEXTLINE(bugprone-unchecked-optional-access)
709 BEAST_EXPECT(obj.object->isFieldPresent(sfLedgerHash));
710 // NOLINTNEXTLINE(bugprone-unchecked-optional-access)
711 auto const& h256 = obj.object->getFieldH256(sfLedgerHash);
712 BEAST_EXPECT(h256.size() == 32);
713 bool const allZero = std::ranges::all_of(h256, [](auto b) { return b == 0; });
714 BEAST_EXPECT(allZero);
715 }
716
717 // Odd-length hex string for UInt256 (should fail)
718 {
719 json::Value j;
720 j[sfLedgerHash] =
721 "0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCD"
722 "E";
723 STParsedJSONObject const obj("Test", j);
724 BEAST_EXPECT(!obj.object.has_value());
725 }
726
727 // Non-hex string for UInt256 (should fail)
728 {
729 json::Value j;
730 j[sfLedgerHash] = "nothexstring";
731 STParsedJSONObject const obj("Test", j);
732 BEAST_EXPECT(!obj.object.has_value());
733 }
734
735 // Hex string too short for UInt256 (should fail)
736 {
737 json::Value j;
738 j[sfLedgerHash] = "01234567";
739 STParsedJSONObject const obj("Test", j);
740 BEAST_EXPECT(!obj.object.has_value());
741 }
742
743 // Hex string too long for UInt256 (should fail)
744 {
745 json::Value j;
746 j[sfLedgerHash] =
747 "0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCD"
748 "EF00";
749 STParsedJSONObject const obj("Test", j);
750 BEAST_EXPECT(!obj.object.has_value());
751 }
752
753 // Array value for UInt256 (should fail)
754 {
755 json::Value j;
756 j[sfLedgerHash] = json::Value(json::ValueType::Array);
757 STParsedJSONObject const obj("Test", j);
758 BEAST_EXPECT(!obj.object.has_value());
759 }
760
761 // Object value for UInt256 (should fail)
762 {
763 json::Value j;
764 j[sfLedgerHash] = json::Value(json::ValueType::Object);
765 STParsedJSONObject const obj("Test", j);
766 BEAST_EXPECT(!obj.object.has_value());
767 }
768 }
769
770 void
772 {
773 testcase("Int32");
774 {
775 json::Value j;
776 int const minInt32 = -2147483648;
777 j[sfLoanScale] = minInt32;
778 STParsedJSONObject obj("Test", j);
779 BEAST_EXPECT(obj.object.has_value());
780 // NOLINTNEXTLINE(bugprone-unchecked-optional-access)
781 if (BEAST_EXPECT(obj.object->isFieldPresent(sfLoanScale)))
782 {
783 // NOLINTNEXTLINE(bugprone-unchecked-optional-access)
784 BEAST_EXPECT(obj.object->getFieldI32(sfLoanScale) == minInt32);
785 }
786 }
787
788 // max value
789 {
790 json::Value j;
791 int const maxInt32 = 2147483647;
792 j[sfLoanScale] = maxInt32;
793 STParsedJSONObject obj("Test", j);
794 BEAST_EXPECT(obj.object.has_value());
795 // NOLINTNEXTLINE(bugprone-unchecked-optional-access)
796 if (BEAST_EXPECT(obj.object->isFieldPresent(sfLoanScale)))
797 {
798 // NOLINTNEXTLINE(bugprone-unchecked-optional-access)
799 BEAST_EXPECT(obj.object->getFieldI32(sfLoanScale) == maxInt32);
800 }
801 }
802
803 // max uint value
804 {
805 json::Value j;
806 unsigned int const maxUInt32 = 2147483647u;
807 j[sfLoanScale] = maxUInt32;
808 STParsedJSONObject obj("Test", j);
809 BEAST_EXPECT(obj.object.has_value());
810 // NOLINTNEXTLINE(bugprone-unchecked-optional-access)
811 if (BEAST_EXPECT(obj.object->isFieldPresent(sfLoanScale)))
812 {
813 BEAST_EXPECT(
814 // NOLINTNEXTLINE(bugprone-unchecked-optional-access)
815 obj.object->getFieldI32(sfLoanScale) == static_cast<int32_t>(maxUInt32));
816 }
817 }
818
819 // Test with string value
820 {
821 json::Value j;
822 j[sfLoanScale] = "2147483647";
823 STParsedJSONObject obj("Test", j);
824 BEAST_EXPECT(obj.object.has_value());
825 // NOLINTNEXTLINE(bugprone-unchecked-optional-access)
826 if (BEAST_EXPECT(obj.object->isFieldPresent(sfLoanScale)))
827 {
828 // NOLINTNEXTLINE(bugprone-unchecked-optional-access)
829 BEAST_EXPECT(obj.object->getFieldI32(sfLoanScale) == 2147483647u);
830 }
831 }
832
833 // Test with string negative value
834 {
835 json::Value j;
836 int const value = -2147483648;
837 j[sfLoanScale] = std::to_string(value);
838 STParsedJSONObject obj("Test", j);
839 BEAST_EXPECT(obj.object.has_value());
840 // NOLINTNEXTLINE(bugprone-unchecked-optional-access)
841 if (BEAST_EXPECT(obj.object->isFieldPresent(sfLoanScale)))
842 {
843 // NOLINTNEXTLINE(bugprone-unchecked-optional-access)
844 BEAST_EXPECT(obj.object->getFieldI32(sfLoanScale) == value);
845 }
846 }
847
848 // Test out of range value for int32 (negative)
849 {
850 json::Value j;
851 j[sfLoanScale] = "-2147483649";
852 STParsedJSONObject const obj("Test", j);
853 BEAST_EXPECT(!obj.object.has_value());
854 }
855
856 // Test out of range value for int32 (positive)
857 {
858 json::Value j;
859 j[sfLoanScale] = 2147483648u;
860 STParsedJSONObject const obj("Test", j);
861 BEAST_EXPECT(!obj.object.has_value());
862 }
863
864 // Test string value out of range
865 {
866 json::Value j;
867 j[sfLoanScale] = "2147483648";
868 STParsedJSONObject const obj("Test", j);
869 BEAST_EXPECT(!obj.object.has_value());
870 }
871
872 // Test bad_type (arrayValue)
873 {
874 json::Value j;
875 j[sfLoanScale] = json::Value(json::ValueType::Array);
876 STParsedJSONObject const obj("Test", j);
877 BEAST_EXPECT(!obj.object.has_value());
878 }
879
880 // Test bad_type (objectValue)
881 {
882 json::Value j;
883 j[sfLoanScale] = json::Value(json::ValueType::Object);
884 STParsedJSONObject const obj("Test", j);
885 BEAST_EXPECT(!obj.object.has_value());
886 }
887 }
888
889 void
891 {
892 testcase("Blob");
893 // Test with valid hex string for blob
894 {
895 json::Value j;
896 j[sfPublicKey] = "DEADBEEF";
897 STParsedJSONObject obj("Test", j);
898 BEAST_EXPECT(obj.object.has_value());
899 // NOLINTNEXTLINE(bugprone-unchecked-optional-access)
900 BEAST_EXPECT(obj.object->isFieldPresent(sfPublicKey));
901 auto const& blob =
902 obj.object->getFieldVL(sfPublicKey); // NOLINT(bugprone-unchecked-optional-access)
903 BEAST_EXPECT(blob.size() == 4);
904 BEAST_EXPECT(blob[0] == 0xDE);
905 BEAST_EXPECT(blob[1] == 0xAD);
906 BEAST_EXPECT(blob[2] == 0xBE);
907 BEAST_EXPECT(blob[3] == 0xEF);
908 }
909
910 // Test empty string for blob (should be valid, size 0)
911 {
912 json::Value j;
913 j[sfPublicKey] = "";
914 STParsedJSONObject obj("Test", j);
915 BEAST_EXPECT(obj.object.has_value());
916 // NOLINTNEXTLINE(bugprone-unchecked-optional-access)
917 BEAST_EXPECT(obj.object->isFieldPresent(sfPublicKey));
918 auto const& blob =
919 obj.object->getFieldVL(sfPublicKey); // NOLINT(bugprone-unchecked-optional-access)
920 BEAST_EXPECT(blob.empty());
921 }
922
923 // Test lowercase hex string for blob
924 {
925 json::Value j;
926 j[sfPublicKey] = "deadbeef";
927 STParsedJSONObject obj("Test", j);
928 BEAST_EXPECT(obj.object.has_value());
929 // NOLINTNEXTLINE(bugprone-unchecked-optional-access)
930 BEAST_EXPECT(obj.object->isFieldPresent(sfPublicKey));
931 auto const& blob =
932 obj.object->getFieldVL(sfPublicKey); // NOLINT(bugprone-unchecked-optional-access)
933 BEAST_EXPECT(blob.size() == 4);
934 BEAST_EXPECT(blob[0] == 0xDE);
935 BEAST_EXPECT(blob[1] == 0xAD);
936 BEAST_EXPECT(blob[2] == 0xBE);
937 BEAST_EXPECT(blob[3] == 0xEF);
938 }
939
940 // Test non-hex string for blob (should fail)
941 {
942 json::Value j;
943 j[sfPublicKey] = "XYZ123";
944 STParsedJSONObject const obj("Test", j);
945 BEAST_EXPECT(!obj.object.has_value());
946 }
947
948 // Test array value for blob (should fail)
949 {
950 json::Value j;
951 j[sfPublicKey] = json::Value(json::ValueType::Array);
952 STParsedJSONObject const obj("Test", j);
953 BEAST_EXPECT(!obj.object.has_value());
954 }
955
956 // Test object value for blob (should fail)
957 {
958 json::Value j;
959 j[sfPublicKey] = json::Value(json::ValueType::Object);
960 STParsedJSONObject const obj("Test", j);
961 BEAST_EXPECT(!obj.object.has_value());
962 }
963 }
964
965 void
967 {
968 testcase("Vector256");
969 // Test with valid array of hex strings for Vector256
970 {
971 json::Value j;
973 arr.append(
974 "0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCD"
975 "EF");
976 arr.append(
977 "FEDCBA9876543210FEDCBA9876543210FEDCBA9876543210FEDCBA98765432"
978 "10");
979 j[sfHashes] = arr;
980 STParsedJSONObject obj("Test", j);
981 BEAST_EXPECT(obj.object.has_value());
982 // NOLINTNEXTLINE(bugprone-unchecked-optional-access)
983 BEAST_EXPECT(obj.object->isFieldPresent(sfHashes));
984 auto const& vec =
985 obj.object->getFieldV256(sfHashes); // NOLINT(bugprone-unchecked-optional-access)
986 BEAST_EXPECT(vec.size() == 2);
987 BEAST_EXPECT(to_string(vec[0]) == arr[0u].asString());
988 BEAST_EXPECT(to_string(vec[1]) == arr[1u].asString());
989 }
990 // Test empty array for Vector256 (should be valid, size 0)
991 {
992 json::Value j;
994 j[sfHashes] = arr;
995 STParsedJSONObject obj("Test", j);
996 BEAST_EXPECT(obj.object.has_value());
997 // NOLINTNEXTLINE(bugprone-unchecked-optional-access)
998 BEAST_EXPECT(obj.object->isFieldPresent(sfHashes));
999 auto const& vec =
1000 obj.object->getFieldV256(sfHashes); // NOLINT(bugprone-unchecked-optional-access)
1001 BEAST_EXPECT(vec.empty());
1002 }
1003
1004 // Test array with invalid hex string (should fail)
1005 {
1006 json::Value j;
1008 arr.append("nothexstring");
1009 j[sfHashes] = arr;
1010 STParsedJSONObject const obj("Test", j);
1011 BEAST_EXPECT(!obj.object.has_value());
1012 }
1013
1014 // Test array with string of wrong length (should fail)
1015 {
1016 json::Value j;
1018 arr.append("0123456789ABCDEF"); // too short for UInt256
1019 j[sfHashes] = arr;
1020 STParsedJSONObject const obj("Test", j);
1021 BEAST_EXPECT(!obj.object.has_value());
1022 }
1023
1024 // Test array with non-string element (should fail)
1025 {
1026 json::Value j;
1028 arr.append(12345);
1029 j[sfHashes] = arr;
1030 STParsedJSONObject const obj("Test", j);
1031 BEAST_EXPECT(!obj.object.has_value());
1032 }
1033
1034 // Test non-array value for Vector256 (should fail)
1035 {
1036 json::Value j;
1037 j[sfHashes] = "notanarray";
1038 STParsedJSONObject const obj("Test", j);
1039 BEAST_EXPECT(!obj.object.has_value());
1040 }
1041
1042 // Test array with object element (should fail)
1043 {
1044 json::Value j;
1047 objElem["foo"] = "bar";
1048 arr.append(objElem);
1049 j[sfHashes] = arr;
1050 STParsedJSONObject const obj("Test", j);
1051 BEAST_EXPECT(!obj.object.has_value());
1052 }
1053 }
1054
1055 void
1057 {
1058 testcase("Account");
1059 // Test with valid base58 string for AccountID
1060 {
1061 json::Value j;
1062 j[sfAccount] = "rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh";
1063 STParsedJSONObject obj("Test", j);
1064 BEAST_EXPECT(obj.object.has_value());
1065 // NOLINTNEXTLINE(bugprone-unchecked-optional-access)
1066 BEAST_EXPECT(obj.object->isFieldPresent(sfAccount));
1067 auto const& acct =
1068 obj.object->getAccountID(sfAccount); // NOLINT(bugprone-unchecked-optional-access)
1069 BEAST_EXPECT(acct.size() == 20);
1070 BEAST_EXPECT(toBase58(acct) == "rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh");
1071 }
1072
1073 // Valid hex string for AccountID
1074 {
1075 json::Value j;
1076 j[sfAccount] = "000102030405060708090A0B0C0D0E0F10111213";
1077 STParsedJSONObject obj("Test", j);
1078 BEAST_EXPECT(obj.object.has_value());
1079 // NOLINTNEXTLINE(bugprone-unchecked-optional-access)
1080 BEAST_EXPECT(obj.object->isFieldPresent(sfAccount));
1081 auto const& acct =
1082 obj.object->getAccountID(sfAccount); // NOLINT(bugprone-unchecked-optional-access)
1083 BEAST_EXPECT(acct.size() == 20);
1084 }
1085
1086 // Invalid base58 string for AccountID
1087 {
1088 json::Value j;
1089 j[sfAccount] = "notAValidBase58Account";
1090 STParsedJSONObject const obj("Test", j);
1091 BEAST_EXPECT(!obj.object.has_value());
1092 }
1093
1094 // Invalid hex string for AccountID (too short)
1095 {
1096 json::Value j;
1097 j[sfAccount] = "001122334455";
1098 STParsedJSONObject const obj("Test", j);
1099 BEAST_EXPECT(!obj.object.has_value());
1100 }
1101
1102 // Invalid hex string for AccountID (too long)
1103 {
1104 json::Value j;
1105 j[sfAccount] = "000102030405060708090A0B0C0D0E0F101112131415";
1106 STParsedJSONObject const obj("Test", j);
1107 BEAST_EXPECT(!obj.object.has_value());
1108 }
1109
1110 // Invalid hex string for AccountID (bad chars)
1111 {
1112 json::Value j;
1113 j[sfAccount] = "000102030405060708090A0B0C0D0E0F1011121G";
1114 STParsedJSONObject const obj("Test", j);
1115 BEAST_EXPECT(!obj.object.has_value());
1116 }
1117
1118 // Empty string for AccountID (should fail)
1119 {
1120 json::Value j;
1121 j[sfAccount] = "";
1122 STParsedJSONObject const obj("Test", j);
1123 BEAST_EXPECT(!obj.object.has_value());
1124 }
1125
1126 // Array value for AccountID (should fail)
1127 {
1128 json::Value j;
1129 j[sfAccount] = json::Value(json::ValueType::Array);
1130 STParsedJSONObject const obj("Test", j);
1131 BEAST_EXPECT(!obj.object.has_value());
1132 }
1133
1134 // Object value for AccountID (should fail)
1135 {
1136 json::Value j;
1137 j[sfAccount] = json::Value(json::ValueType::Object);
1138 STParsedJSONObject const obj("Test", j);
1139 BEAST_EXPECT(!obj.object.has_value());
1140 }
1141 }
1142
1143 void
1145 {
1146 testcase("Currency");
1147 // Test with valid ISO code for currency
1148 {
1149 json::Value j;
1150 j[sfBaseAsset] = "USD";
1151 STParsedJSONObject obj("Test", j);
1152 BEAST_EXPECT(obj.object.has_value());
1153 // NOLINTNEXTLINE(bugprone-unchecked-optional-access)
1154 BEAST_EXPECT(obj.object->isFieldPresent(sfBaseAsset));
1155 // NOLINTNEXTLINE(bugprone-unchecked-optional-access)
1156 auto const& curr = obj.object->getFieldCurrency(sfBaseAsset);
1157 BEAST_EXPECT(curr.currency().size() == 20);
1158 }
1159
1160 // Valid ISO code
1161 {
1162 json::Value j;
1163 j[sfBaseAsset] = "EUR";
1164 STParsedJSONObject obj("Test", j);
1165 BEAST_EXPECT(obj.object.has_value());
1166 // NOLINTNEXTLINE(bugprone-unchecked-optional-access)
1167 BEAST_EXPECT(obj.object->isFieldPresent(sfBaseAsset));
1168 // NOLINTNEXTLINE(bugprone-unchecked-optional-access)
1169 auto const& curr = obj.object->getFieldCurrency(sfBaseAsset);
1170 BEAST_EXPECT(curr.currency().size() == 20);
1171 }
1172
1173 // Valid hex string for currency
1174 {
1175 json::Value j;
1176 j[sfBaseAsset] = "0123456789ABCDEF01230123456789ABCDEF0123";
1177 STParsedJSONObject obj("Test", j);
1178 if (BEAST_EXPECT(obj.object); obj.object.has_value())
1179 {
1180 BEAST_EXPECT(obj.object->isFieldPresent(sfBaseAsset));
1181 auto const& curr = obj.object->getFieldCurrency(sfBaseAsset);
1182 BEAST_EXPECT(curr.currency().size() == 20);
1183 }
1184 }
1185
1186 // Invalid ISO code (too long)
1187 {
1188 json::Value j;
1189 j[sfBaseAsset] = "USDD";
1190 STParsedJSONObject const obj("Test", j);
1191 BEAST_EXPECT(!obj.object.has_value());
1192 }
1193
1194 // lowercase ISO code
1195 {
1196 json::Value j;
1197 j[sfBaseAsset] = "usd";
1198 STParsedJSONObject obj("Test", j);
1199 BEAST_EXPECT(obj.object.has_value());
1200 // NOLINTNEXTLINE(bugprone-unchecked-optional-access)
1201 BEAST_EXPECT(obj.object->isFieldPresent(sfBaseAsset));
1202 // NOLINTNEXTLINE(bugprone-unchecked-optional-access)
1203 auto const& curr = obj.object->getFieldCurrency(sfBaseAsset);
1204 BEAST_EXPECT(curr.currency().size() == 20);
1205 }
1206
1207 // Invalid hex string (too short)
1208 {
1209 json::Value j;
1210 j[sfBaseAsset] = "0123456789AB";
1211 STParsedJSONObject const obj("Test", j);
1212 BEAST_EXPECT(!obj.object.has_value());
1213 }
1214
1215 // Invalid hex string (too long)
1216 {
1217 json::Value j;
1218 j[sfBaseAsset] = "0123456789ABCDEF0123456789";
1219 STParsedJSONObject const obj("Test", j);
1220 BEAST_EXPECT(!obj.object.has_value());
1221 }
1222
1223 // Empty string for currency (should fail)
1224 {
1225 json::Value j;
1226 j[sfBaseAsset] = "";
1227 STParsedJSONObject obj("Test", j);
1228 BEAST_EXPECT(obj.object.has_value());
1229 // NOLINTNEXTLINE(bugprone-unchecked-optional-access)
1230 BEAST_EXPECT(obj.object->isFieldPresent(sfBaseAsset));
1231 // NOLINTNEXTLINE(bugprone-unchecked-optional-access)
1232 auto const& curr = obj.object->getFieldCurrency(sfBaseAsset);
1233 BEAST_EXPECT(curr.currency().size() == 20);
1234 }
1235
1236 // Array value for currency (should fail)
1237 {
1238 json::Value j;
1239 j[sfBaseAsset] = json::Value(json::ValueType::Array);
1240 STParsedJSONObject const obj("Test", j);
1241 BEAST_EXPECT(!obj.object.has_value());
1242 }
1243
1244 // Object value for currency (should fail)
1245 {
1246 json::Value j;
1247 j[sfBaseAsset] = json::Value(json::ValueType::Object);
1248 STParsedJSONObject const obj("Test", j);
1249 BEAST_EXPECT(!obj.object.has_value());
1250 }
1251 }
1252
1253 void
1255 {
1256 testcase("Amount");
1257 // Test with string value for Amount
1258 {
1259 json::Value j;
1260 j[sfAmount] = "100000000000000000";
1261 STParsedJSONObject obj("Test", j);
1262 BEAST_EXPECT(obj.object.has_value());
1263 // NOLINTNEXTLINE(bugprone-unchecked-optional-access)
1264 BEAST_EXPECT(obj.object->isFieldPresent(sfAmount));
1265 // NOLINTNEXTLINE(bugprone-unchecked-optional-access)
1266 BEAST_EXPECT(obj.object->getFieldAmount(sfAmount) == STAmount(100000000000000000ull));
1267 }
1268
1269 // Test with int value for Amount
1270 {
1271 json::Value j;
1272 j[sfAmount] = 4294967295u;
1273 STParsedJSONObject obj("Test", j);
1274 BEAST_EXPECT(obj.object.has_value());
1275 // NOLINTNEXTLINE(bugprone-unchecked-optional-access)
1276 BEAST_EXPECT(obj.object->isFieldPresent(sfAmount));
1277 // NOLINTNEXTLINE(bugprone-unchecked-optional-access)
1278 BEAST_EXPECT(obj.object->getFieldAmount(sfAmount) == STAmount(4294967295u));
1279 }
1280
1281 // Test with decimal string for Amount (should fail)
1282 {
1283 json::Value j;
1284 j[sfAmount] = "123.45";
1285 STParsedJSONObject const obj("Test", j);
1286 BEAST_EXPECT(!obj.object.has_value());
1287 }
1288
1289 // Test with empty string for Amount (should fail)
1290 {
1291 json::Value j;
1292 j[sfAmount] = "";
1293 STParsedJSONObject const obj("Test", j);
1294 BEAST_EXPECT(!obj.object.has_value());
1295 }
1296
1297 // Test with non-numeric string for Amount (should fail)
1298 {
1299 json::Value j;
1300 j[sfAmount] = "notanumber";
1301 STParsedJSONObject const obj("Test", j);
1302 BEAST_EXPECT(!obj.object.has_value());
1303 }
1304
1305 // Test with object value for Amount (should fail)
1306 {
1307 json::Value j;
1308 j[sfAmount] = json::Value(json::ValueType::Object);
1309 STParsedJSONObject const obj("Test", j);
1310 BEAST_EXPECT(!obj.object.has_value());
1311 }
1312 }
1313
1314 void
1316 {
1317 testcase("PathSet");
1318 // Valid test: single path with single element
1319 {
1320 json::Value j;
1323 elem["account"] = "rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh";
1324 elem["currency"] = "USD";
1325 elem["issuer"] = "rPT1Sjq2YGrBMTttX4GZHjKu9dyfzbpAYe";
1326 path.append(elem);
1328 pathset.append(path);
1329 j[sfPaths] = pathset;
1330 STParsedJSONObject obj("Test", j);
1331 if (BEAST_EXPECT(obj.object.has_value()))
1332 {
1333 // NOLINTNEXTLINE(bugprone-unchecked-optional-access)
1334 BEAST_EXPECT(obj.object->isFieldPresent(sfPaths));
1335 // NOLINTNEXTLINE(bugprone-unchecked-optional-access)
1336 auto const& ps = obj.object->getFieldPathSet(sfPaths);
1337 BEAST_EXPECT(!ps.empty());
1338 BEAST_EXPECT(ps.size() == 1);
1339 BEAST_EXPECT(ps[0].size() == 1);
1340 BEAST_EXPECT(
1341 ps[0][0].getAccountID() ==
1342 parseBase58<AccountID>("rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh"));
1343 BEAST_EXPECT(to_string(ps[0][0].getCurrency()) == "USD");
1344 BEAST_EXPECT(
1345 ps[0][0].getIssuerID() ==
1346 parseBase58<AccountID>("rPT1Sjq2YGrBMTttX4GZHjKu9dyfzbpAYe"));
1347 }
1348 }
1349
1350 // Valid test: non-standard currency code
1351 {
1352 json::Value j;
1355 elem["account"] = "rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh";
1356 elem["currency"] = "0123456789ABCDEF01230123456789ABCDEF0123";
1357 elem["issuer"] = "rPT1Sjq2YGrBMTttX4GZHjKu9dyfzbpAYe";
1358 path.append(elem);
1360 pathset.append(path);
1361 j[sfPaths] = pathset;
1362 STParsedJSONObject obj("Test", j);
1363 BEAST_EXPECT(obj.object.has_value());
1364 BEAST_EXPECT(
1365 obj.object->isFieldPresent(sfPaths)); // NOLINT(bugprone-unchecked-optional-access)
1366 auto const& ps =
1367 obj.object->getFieldPathSet(sfPaths); // NOLINT(bugprone-unchecked-optional-access)
1368 BEAST_EXPECT(!ps.empty());
1369 }
1370
1371 // Test with non-array value for PathSet (should fail)
1372 {
1373 json::Value j;
1374 j[sfPaths] = "notanarray";
1375 STParsedJSONObject const obj("Test", j);
1376 BEAST_EXPECT(!obj.object.has_value());
1377 }
1378
1379 // Test with array containing non-array element (should fail)
1380 {
1381 json::Value j;
1383 pathset.append("notanarray");
1384 j[sfPaths] = pathset;
1385 STParsedJSONObject const obj("Test", j);
1386 BEAST_EXPECT(!obj.object.has_value());
1387 }
1388
1389 // Test with array containing array with non-object element (should
1390 // fail)
1391 {
1392 json::Value j;
1394 path.append("notanobject");
1396 pathset.append(path);
1397 j[sfPaths] = pathset;
1398 STParsedJSONObject const obj("Test", j);
1399 BEAST_EXPECT(!obj.object.has_value());
1400 }
1401
1402 // Test with array containing array with object missing required keys
1403 // (should fail)
1404 {
1405 json::Value j;
1408 elem["foo"] = "bar"; // not a valid path element key
1409 path.append(elem);
1411 pathset.append(path);
1412 j[sfPaths] = pathset;
1413 STParsedJSONObject const obj("Test", j);
1414 BEAST_EXPECT(!obj.object.has_value());
1415 }
1416
1417 // Test with array containing array with object with invalid account
1418 // value (should fail)
1419 {
1420 json::Value j;
1423 elem["account"] = "notAValidBase58Account";
1424 path.append(elem);
1426 pathset.append(path);
1427 j[sfPaths] = pathset;
1428 STParsedJSONObject const obj("Test", j);
1429 BEAST_EXPECT(!obj.object.has_value());
1430 }
1431
1432 // Test with account not string (should fail)
1433 {
1434 json::Value j;
1437 elem["account"] = 12345;
1438 path.append(elem);
1440 pathset.append(path);
1441 j[sfPaths] = pathset;
1442 STParsedJSONObject const obj("Test", j);
1443 BEAST_EXPECT(!obj.object.has_value());
1444 }
1445
1446 // Test with currency not string (should fail)
1447 {
1448 json::Value j;
1451 elem["currency"] = 12345;
1452 path.append(elem);
1454 pathset.append(path);
1455 j[sfPaths] = pathset;
1456 STParsedJSONObject const obj("Test", j);
1457 BEAST_EXPECT(!obj.object.has_value());
1458 }
1459
1460 // Test with non-standard currency not hex (should fail)
1461 {
1462 json::Value j;
1465 elem["currency"] = "notAValidCurrency";
1466 path.append(elem);
1468 pathset.append(path);
1469 j[sfPaths] = pathset;
1470 STParsedJSONObject const obj("Test", j);
1471 BEAST_EXPECT(!obj.object.has_value());
1472 }
1473
1474 // Test with issuer not string (should fail)
1475 {
1476 json::Value j;
1479 elem["issuer"] = 12345;
1480 path.append(elem);
1482 pathset.append(path);
1483 j[sfPaths] = pathset;
1484 STParsedJSONObject const obj("Test", j);
1485 BEAST_EXPECT(!obj.object.has_value());
1486 }
1487
1488 // Test with issuer not base58 (should fail)
1489 {
1490 json::Value j;
1493 elem["issuer"] = "notAValidBase58Account";
1494 path.append(elem);
1496 pathset.append(path);
1497 j[sfPaths] = pathset;
1498 STParsedJSONObject const obj("Test", j);
1499 BEAST_EXPECT(!obj.object.has_value());
1500 }
1501 }
1502
1503 void
1505 {
1506 testcase("Issue");
1507 // Valid Issue: currency and issuer as base58
1508 {
1509 json::Value j;
1511 issueJson["currency"] = "USD";
1512 issueJson["issuer"] = "rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh";
1513 j[sfAsset] = issueJson;
1514 STParsedJSONObject obj("Test", j);
1515 if (BEAST_EXPECT(obj.object.has_value()))
1516 {
1517 // NOLINTNEXTLINE(bugprone-unchecked-optional-access)
1518 BEAST_EXPECT(obj.object->isFieldPresent(sfAsset));
1519 auto const& issueField =
1520 (*obj.object)[sfAsset]; // NOLINT(bugprone-unchecked-optional-access)
1521 auto const issue = issueField.value().get<Issue>();
1522 BEAST_EXPECT(issue.currency.size() == 20);
1523 BEAST_EXPECT(to_string(issue.currency) == "USD");
1524 BEAST_EXPECT(issue.account.size() == 20);
1525 BEAST_EXPECT(
1526 issue.account == parseBase58<AccountID>("rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh"));
1527 }
1528 }
1529
1530 // Valid Issue: currency as hex
1531 {
1532 json::Value j;
1534 issueJson["currency"] = "0123456789ABCDEF01230123456789ABCDEF0123";
1535 issueJson["issuer"] = "rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh";
1536 j[sfAsset] = issueJson;
1537 STParsedJSONObject obj("Test", j);
1538 if (BEAST_EXPECT(obj.object); obj.object.has_value())
1539 {
1540 BEAST_EXPECT(obj.object->isFieldPresent(sfAsset));
1541 auto const& issueField = (*obj.object)[sfAsset];
1542 auto const issue = issueField.value().get<Issue>();
1543 BEAST_EXPECT(issue.currency.size() == 20);
1544 BEAST_EXPECT(issue.account.size() == 20);
1545 }
1546 }
1547
1548 // Valid Issue: MPTID
1549 {
1550 json::Value j;
1552 issueJson["mpt_issuance_id"] = "0000000000000000000000004D5054494431323334234234";
1553 j[sfAsset] = issueJson;
1554 STParsedJSONObject obj("Test", j);
1555 if (BEAST_EXPECT(obj.object); obj.object.has_value())
1556 {
1557 BEAST_EXPECT(obj.object->isFieldPresent(sfAsset));
1558 auto const& issueField = (*obj.object)[sfAsset];
1559 auto const issue = issueField.value().get<MPTIssue>();
1560 BEAST_EXPECT(issue.getMptID().size() == 24);
1561 }
1562 }
1563
1564 // Invalid Issue: missing currency
1565 {
1566 json::Value j;
1568 issue["issuer"] = "rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh";
1569 j[sfAsset] = issue;
1570 STParsedJSONObject const obj("Test", j);
1571 BEAST_EXPECT(!obj.object.has_value());
1572 }
1573
1574 // Invalid Issue: missing issuer
1575 {
1576 json::Value j;
1578 issue["currency"] = "USD";
1579 j[sfAsset] = issue;
1580 STParsedJSONObject const obj("Test", j);
1581 BEAST_EXPECT(!obj.object.has_value());
1582 }
1583
1584 // Invalid Issue: currency too long
1585 {
1586 json::Value j;
1588 issue["currency"] = "USDD";
1589 issue["issuer"] = "rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh";
1590 j[sfAsset] = issue;
1591 STParsedJSONObject const obj("Test", j);
1592 BEAST_EXPECT(!obj.object.has_value());
1593 }
1594
1595 // Invalid Issue: issuer not base58 or hex
1596 {
1597 json::Value j;
1599 issue["currency"] = "USD";
1600 issue["issuer"] = "notAValidIssuer";
1601 j[sfAsset] = issue;
1602 STParsedJSONObject const obj("Test", j);
1603 BEAST_EXPECT(!obj.object.has_value());
1604 }
1605
1606 // Invalid Issue: currency not string
1607 {
1608 json::Value j;
1610 issue["currency"] = json::Value(json::ValueType::Array);
1611 issue["issuer"] = "rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh";
1612 j[sfAsset] = issue;
1613 STParsedJSONObject const obj("Test", j);
1614 BEAST_EXPECT(!obj.object.has_value());
1615 }
1616
1617 // Invalid Issue: issuer not string
1618 {
1619 json::Value j;
1621 issue["currency"] = "USD";
1622 issue["issuer"] = json::Value(json::ValueType::Object);
1623 j[sfAsset] = issue;
1624 STParsedJSONObject const obj("Test", j);
1625 BEAST_EXPECT(!obj.object.has_value());
1626 }
1627
1628 // Invalid Issue: not an object
1629 {
1630 json::Value j;
1631 j[sfAsset] = "notanobject";
1632 STParsedJSONObject const obj("Test", j);
1633 BEAST_EXPECT(!obj.object.has_value());
1634 }
1635 }
1636
1637 void
1639 {
1640 testcase("Number");
1641 // Valid integer value for STNumber
1642 {
1643 json::Value j;
1644 j[sfNumber] = 12345;
1645 STParsedJSONObject obj("Test", j);
1646 BEAST_EXPECT(obj.object.has_value());
1647 // NOLINTNEXTLINE(bugprone-unchecked-optional-access)
1648 BEAST_EXPECT(obj.object->isFieldPresent(sfNumber));
1649 // NOLINTNEXTLINE(bugprone-unchecked-optional-access)
1650 BEAST_EXPECT(obj.object->getFieldNumber(sfNumber).value() == Number(12345, 0));
1651 }
1652
1653 // Valid uint value for STNumber
1654 {
1655 json::Value j;
1656 j[sfNumber] = 12345u;
1657 STParsedJSONObject obj("Test", j);
1658 BEAST_EXPECT(obj.object.has_value());
1659 // NOLINTNEXTLINE(bugprone-unchecked-optional-access)
1660 BEAST_EXPECT(obj.object->isFieldPresent(sfNumber));
1661 // NOLINTNEXTLINE(bugprone-unchecked-optional-access)
1662 BEAST_EXPECT(obj.object->getFieldNumber(sfNumber).value() == Number(12345, 0));
1663 }
1664
1665 // Valid string integer value for STNumber
1666 {
1667 json::Value j;
1668 j[sfNumber] = "67890";
1669 STParsedJSONObject obj("Test", j);
1670 BEAST_EXPECT(obj.object.has_value());
1671 // NOLINTNEXTLINE(bugprone-unchecked-optional-access)
1672 BEAST_EXPECT(obj.object->isFieldPresent(sfNumber));
1673 // NOLINTNEXTLINE(bugprone-unchecked-optional-access)
1674 BEAST_EXPECT(obj.object->getFieldNumber(sfNumber).value() == Number(67890, 0));
1675 }
1676
1677 // Valid negative integer value for STNumber
1678 {
1679 json::Value j;
1680 j[sfNumber] = -42;
1681 STParsedJSONObject obj("Test", j);
1682 BEAST_EXPECT(obj.object.has_value());
1683 // NOLINTNEXTLINE(bugprone-unchecked-optional-access)
1684 BEAST_EXPECT(obj.object->isFieldPresent(sfNumber));
1685 // NOLINTNEXTLINE(bugprone-unchecked-optional-access)
1686 BEAST_EXPECT(obj.object->getFieldNumber(sfNumber).value() == Number(-42, 0));
1687 }
1688
1689 // Valid string negative integer value for STNumber
1690 {
1691 json::Value j;
1692 j[sfNumber] = "-123";
1693 STParsedJSONObject obj("Test", j);
1694 BEAST_EXPECT(obj.object.has_value());
1695 // NOLINTNEXTLINE(bugprone-unchecked-optional-access)
1696 BEAST_EXPECT(obj.object->isFieldPresent(sfNumber));
1697 // NOLINTNEXTLINE(bugprone-unchecked-optional-access)
1698 BEAST_EXPECT(obj.object->getFieldNumber(sfNumber).value() == Number(-123, 0));
1699 }
1700
1701 // Valid floating point value for STNumber
1702 {
1703 json::Value j;
1704 j[sfNumber] = "3.14159";
1705 STParsedJSONObject obj("Test", j);
1706 if (BEAST_EXPECT(obj.object); obj.object.has_value())
1707 {
1708 BEAST_EXPECT(obj.object->isFieldPresent(sfNumber));
1709 BEAST_EXPECT(obj.object->getFieldNumber(sfNumber).value() == Number(314159, -5));
1710 }
1711 }
1712
1713 // Invalid string value for STNumber (not a number)
1714 {
1715 json::Value j;
1716 j[sfNumber] = "notanumber";
1717 STParsedJSONObject const obj("Test", j);
1718 BEAST_EXPECT(!obj.object.has_value());
1719 }
1720
1721 // Invalid array value for STNumber
1722 {
1723 json::Value j;
1724 j[sfNumber] = json::Value(json::ValueType::Array);
1725 STParsedJSONObject const obj("Test", j);
1726 BEAST_EXPECT(!obj.object.has_value());
1727 }
1728
1729 // Invalid object value for STNumber
1730 {
1731 json::Value j;
1732 j[sfNumber] = json::Value(json::ValueType::Object);
1733 STParsedJSONObject const obj("Test", j);
1734 BEAST_EXPECT(!obj.object.has_value());
1735 }
1736
1737 // Empty string for STNumber (should fail)
1738 {
1739 json::Value j;
1740 j[sfNumber] = "";
1741 STParsedJSONObject const obj("Test", j);
1742 BEAST_EXPECT(!obj.object.has_value());
1743 }
1744 }
1745
1746 void
1748 {
1749 testcase("Object");
1750 // Test with valid object for Object
1751 {
1752 json::Value j;
1754 objVal[sfTransactionResult] = 1;
1755 j[sfTransactionMetaData] = objVal;
1756 STParsedJSONObject obj("Test", j);
1757 BEAST_EXPECT(obj.object.has_value());
1758 // NOLINTNEXTLINE(bugprone-unchecked-optional-access)
1759 BEAST_EXPECT(obj.object->isFieldPresent(sfTransactionMetaData));
1760 // NOLINTNEXTLINE(bugprone-unchecked-optional-access)
1761 auto const& result = obj.object->peekFieldObject(sfTransactionMetaData);
1762 BEAST_EXPECT(result.getFieldU8(sfTransactionResult) == 1);
1763 }
1764
1765 // Test with non-object value for Object (should fail)
1766 {
1767 json::Value j;
1768 j[sfTransactionMetaData] = "notanobject";
1769 STParsedJSONObject const obj("Test", j);
1770 BEAST_EXPECT(!obj.object.has_value());
1771 }
1772
1773 // Test with array value for Object (should fail)
1774 {
1775 json::Value j;
1777 arr.append(1);
1778 j[sfTransactionMetaData] = arr;
1779 STParsedJSONObject const obj("Test", j);
1780 BEAST_EXPECT(!obj.object.has_value());
1781 }
1782
1783 // Test with null value for Object (should fail)
1784 {
1785 json::Value j;
1786 j[sfTransactionMetaData] = json::Value(json::ValueType::Null);
1787 STParsedJSONObject const obj("Test", j);
1788 BEAST_EXPECT(!obj.object.has_value());
1789 }
1790
1791 // Test with max depth (should succeed)
1792 // max depth is 64
1793 {
1794 json::Value j;
1796 json::Value* current = &obj;
1797 for (std::size_t i = 1; i < kMaxParsedJsonDepth; ++i)
1798 {
1800 (*current)[sfTransactionMetaData] = next;
1801 current = &(*current)[sfTransactionMetaData];
1802 }
1803 (*current)[sfTransactionResult.getJsonName()] = 1;
1804 j[sfTransactionMetaData] = obj;
1805 STParsedJSONObject parsed("Test", j);
1806 BEAST_EXPECT(parsed.object.has_value());
1807 // NOLINTNEXTLINE(bugprone-unchecked-optional-access)
1808 BEAST_EXPECT(parsed.object->isFieldPresent(sfTransactionMetaData));
1809 }
1810
1811 // Test with depth exceeding maxDepth (should fail)
1812 {
1813 json::Value j;
1815 json::Value* current = &obj;
1816 for (std::size_t i = 1; i <= kMaxParsedJsonDepth; ++i)
1817 {
1819 (*current)[sfTransactionMetaData] = next;
1820 current = &(*current)[sfTransactionMetaData];
1821 }
1822 (*current)[sfTransactionResult.getJsonName()] = 1;
1823 j[sfTransactionMetaData] = obj;
1824 STParsedJSONObject const parsed("Test", j);
1825 BEAST_EXPECT(!parsed.object.has_value());
1826 }
1827 }
1828
1829 void
1831 {
1832 testcase("Array");
1833 // Test with valid array for Array
1834 {
1835 json::Value j;
1838 elem[sfTransactionResult] = 2;
1840 elem2[sfTransactionMetaData] = elem;
1841 arr.append(elem2);
1842 j[sfSignerEntries] = arr;
1843 STParsedJSONObject obj("Test", j);
1844 BEAST_EXPECT(obj.object.has_value());
1845 // NOLINTNEXTLINE(bugprone-unchecked-optional-access)
1846 BEAST_EXPECT(obj.object->isFieldPresent(sfSignerEntries));
1847 // NOLINTNEXTLINE(bugprone-unchecked-optional-access)
1848 auto const& result = obj.object->getFieldArray(sfSignerEntries);
1849 if (BEAST_EXPECT(result.size() == 1))
1850 {
1851 BEAST_EXPECT(result[0].getFName() == sfTransactionMetaData);
1852 BEAST_EXPECT(result[0].getJson(0) == elem);
1853 }
1854 }
1855
1856 // Test with array containing non-object element (should fail)
1857 {
1858 json::Value j;
1860 arr.append("notanobject");
1861 j[sfSignerEntries] = arr;
1862 STParsedJSONObject const obj("Test", j);
1863 BEAST_EXPECT(!obj.object.has_value());
1864 }
1865
1866 // Test with array containing object with invalid field (should fail)
1867 {
1868 json::Value j;
1871 elem["invalidField"] = 1;
1872 arr.append(elem);
1873 j[sfSignerEntries] = arr;
1874 STParsedJSONObject const obj("Test", j);
1875 BEAST_EXPECT(!obj.object.has_value());
1876 }
1877
1878 // Test with array containing object with multiple keys (should fail)
1879 {
1880 json::Value j;
1883 elem[sfTransactionResult] = 2;
1884 elem[sfNetworkID] = 3;
1885 arr.append(elem);
1886 j[sfSignerEntries] = arr;
1887 STParsedJSONObject const obj("Test", j);
1888 BEAST_EXPECT(!obj.object.has_value());
1889 }
1890
1891 // Test with non-array value for Array (should fail)
1892 {
1893 json::Value j;
1894 j[sfSignerEntries] = "notanarray";
1895 STParsedJSONObject const obj("Test", j);
1896 BEAST_EXPECT(!obj.object.has_value());
1897 }
1898
1899 // Test with array containing object with valid field but invalid value
1900 // (should fail)
1901 {
1902 json::Value j;
1905 elem[sfTransactionResult] = "notanint";
1906 arr.append(elem);
1907 j[sfSignerEntries] = arr;
1908 STParsedJSONObject const obj("Test", j);
1909 BEAST_EXPECT(!obj.object.has_value());
1910 }
1911
1912 // Test with empty array for Array (should be valid)
1913 {
1914 json::Value j;
1916 j[sfSignerEntries] = arr;
1917 STParsedJSONObject obj("Test", j);
1918 BEAST_EXPECT(obj.object.has_value());
1919 // NOLINTNEXTLINE(bugprone-unchecked-optional-access)
1920 BEAST_EXPECT(obj.object->isFieldPresent(sfSignerEntries));
1921 }
1922
1923 // Test with object provided but not object SField
1924 {
1925 json::Value j;
1928 obj[0u][sfTransactionResult] = 1;
1929 j[sfSignerEntries] = obj;
1930 STParsedJSONObject const parsed("Test", j);
1931 BEAST_EXPECT(!parsed.object.has_value());
1932 }
1933
1934 // Test invalid children
1935 {
1936 try
1937 {
1938 /*
1939
1940 STArray/STObject constructs don't really map perfectly to json
1941 arrays/objects.
1942
1943 STObject is an associative container, mapping fields to value,
1944 but an STObject may also have a Field as its name, stored
1945 outside the associative structure. The name is important, so to
1946 maintain fidelity, it will take TWO json objects to represent
1947 them.
1948
1949 */
1950 std::string const faulty(
1951 "{\"Template\":[{"
1952 "\"ModifiedNode\":{\"Sequence\":1}, "
1953 "\"DeletedNode\":{\"Sequence\":1}"
1954 "}]}");
1955
1957 json::Value faultyJson;
1958 bool const parsedOK(parseJSONString(faulty, faultyJson));
1959 unexpected(!parsedOK, "failed to parse");
1960 STParsedJSONObject const parsed("test", faultyJson);
1961 BEAST_EXPECT(!parsed.object);
1962 }
1963 catch (std::runtime_error const& e)
1964 {
1965 std::string const what(e.what());
1966 unexpected(!what.starts_with("First level children of `Template`"));
1967 }
1968 }
1969 }
1970
1971 void
1973 {
1974 testcase("Array bounds checking");
1975
1976 auto const limitStr = std::to_string(kMaxParsedJsonArraySize) + " elements per field.";
1977
1978 // parseArray rejects oversized STI_ARRAY (SignerEntries)
1979 {
1980 json::Value jv;
1982 for (std::size_t i = 0; i <= kMaxParsedJsonArraySize; ++i)
1983 {
1984 json::Value entry;
1985 entry["SignerEntry"]["Account"] = "rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh";
1986 entry["SignerEntry"]["SignerWeight"] = 1;
1987 entries.append(entry);
1988 }
1989 jv[sfSignerEntries] = entries;
1990
1991 STParsedJSONObject parsed("test", jv);
1992 BEAST_EXPECT(!parsed.object);
1993 BEAST_EXPECT(parsed.error[jss::error] == "invalidParams");
1994 BEAST_EXPECT(
1995 parsed.error[jss::error_message] ==
1996 "Field 'test.SignerEntries' exceeds allowed JSON array size of " + limitStr);
1997 }
1998
1999 // parseObject rejects oversized STI_VECTOR256 (Amendments)
2000 {
2001 json::Value jv;
2003 std::string const hash(64, '0');
2004 for (std::size_t i = 0; i <= kMaxParsedJsonArraySize; ++i)
2005 amendments.append(hash);
2006 jv[sfAmendments] = amendments;
2007
2008 STParsedJSONObject parsed("test", jv);
2009 BEAST_EXPECT(!parsed.object);
2010 BEAST_EXPECT(parsed.error[jss::error] == "invalidParams");
2011 BEAST_EXPECT(
2012 parsed.error[jss::error_message] ==
2013 "Field 'test.Amendments' exceeds allowed JSON array size of " + limitStr);
2014 }
2015
2016 // parseObject accepts exactly kMaxParsedJsonArraySize STI_VECTOR256 (Amendments)
2017 {
2018 json::Value jv;
2020 std::string const hash(64, '0');
2021 for (std::size_t i = 0; i < kMaxParsedJsonArraySize; ++i)
2022 amendments.append(hash);
2023 jv[sfAmendments] = amendments;
2024
2025 STParsedJSONObject const parsed("test", jv);
2026 BEAST_EXPECT(parsed.object);
2027 // NOLINTNEXTLINE(bugprone-unchecked-optional-access)
2028 auto const arrSize = parsed.object->getFieldV256(sfAmendments).size();
2029 BEAST_EXPECT(arrSize == kMaxParsedJsonArraySize);
2030 }
2031
2032 // parseObject rejects oversized STI_PATHSET (outer array)
2033 {
2034 json::Value jv;
2036 for (std::size_t i = 0; i <= kMaxParsedJsonArraySize; ++i)
2037 {
2039 json::Value hop;
2040 hop["account"] = "rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh";
2041 path.append(hop);
2042 paths.append(path);
2043 }
2044 jv[sfPaths] = paths;
2045
2046 STParsedJSONObject parsed("test", jv);
2047 BEAST_EXPECT(!parsed.object);
2048 BEAST_EXPECT(parsed.error[jss::error] == "invalidParams");
2049 BEAST_EXPECT(
2050 parsed.error[jss::error_message] ==
2051 "Field 'test.Paths' exceeds allowed JSON array size of " + limitStr);
2052 }
2053
2054 // parseObject accepts exactly kMaxParsedJsonArraySize STI_PATHSET (outer array)
2055 {
2056 json::Value jv;
2058 for (std::size_t i = 0; i < kMaxParsedJsonArraySize; ++i)
2059 {
2061 json::Value hop;
2062 hop["account"] = "rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh";
2063 path.append(hop);
2064 paths.append(path);
2065 }
2066 jv[sfPaths] = paths;
2067
2068 STParsedJSONObject const parsed("test", jv);
2069 BEAST_EXPECT(parsed.object);
2070 // NOLINTNEXTLINE(bugprone-unchecked-optional-access)
2071 auto const arrSize = parsed.object->getFieldPathSet(sfPaths).size();
2072 BEAST_EXPECT(arrSize == kMaxParsedJsonArraySize);
2073 }
2074
2075 // parseObject rejects oversized STI_PATHSET (inner path hop array)
2076 {
2077 json::Value jv;
2080 json::Value hop;
2081 hop["account"] = "rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh";
2082 for (std::size_t i = 0; i <= kMaxParsedJsonArraySize; ++i)
2083 path.append(hop);
2084 paths.append(path);
2085 jv[sfPaths] = paths;
2086
2087 STParsedJSONObject parsed("test", jv);
2088 BEAST_EXPECT(!parsed.object);
2089 BEAST_EXPECT(parsed.error[jss::error] == "invalidParams");
2090 BEAST_EXPECT(
2091 parsed.error[jss::error_message] ==
2092 "Field 'test.Paths[0]' exceeds allowed JSON array size of " + limitStr);
2093 }
2094
2095 // parseObject accepts exactly kMaxParsedJsonArraySize hops in a single STI_PATHSET path
2096 {
2097 json::Value jv;
2100 json::Value hop;
2101 hop["account"] = "rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh";
2102 for (std::size_t i = 0; i < kMaxParsedJsonArraySize; ++i)
2103 path.append(hop);
2104 paths.append(path);
2105 jv[sfPaths] = paths;
2106
2107 STParsedJSONObject const parsed("test", jv);
2108 BEAST_EXPECT(parsed.object);
2109 // NOLINTNEXTLINE(bugprone-unchecked-optional-access)
2110 auto const arrSize = parsed.object->getFieldPathSet(sfPaths)[0].size();
2111 BEAST_EXPECT(arrSize == kMaxParsedJsonArraySize);
2112 }
2113
2114 // parseArray accepts exactly kMaxParsedJsonArraySize Memos (boundary)
2115 {
2116 json::Value jv;
2118 for (std::size_t i = 0; i < kMaxParsedJsonArraySize; ++i)
2119 {
2120 json::Value memo;
2121 memo["Memo"] = json::Value(json::ValueType::Object);
2122 memo["Memo"]["MemoData"] = "00";
2123 memos.append(memo);
2124 }
2125 jv[sfMemos] = memos;
2126
2127 STParsedJSONObject const parsed("test", jv);
2128 BEAST_EXPECT(parsed.object);
2129 // NOLINTNEXTLINE(bugprone-unchecked-optional-access)
2130 auto const arrSize = parsed.object->getFieldArray(sfMemos).size();
2131 BEAST_EXPECT(arrSize == kMaxParsedJsonArraySize);
2132 }
2133
2134 // parseArray rejects one more than kMaxParsedJsonArraySize Memos
2135 {
2136 json::Value jv;
2138 for (std::size_t i = 0; i <= kMaxParsedJsonArraySize; ++i)
2139 {
2140 json::Value memo;
2141 memo["Memo"] = json::Value(json::ValueType::Object);
2142 memo["Memo"]["MemoData"] = "00";
2143 memos.append(memo);
2144 }
2145 jv[sfMemos] = memos;
2146
2147 STParsedJSONObject parsed("test", jv);
2148 BEAST_EXPECT(!parsed.object);
2149 BEAST_EXPECT(parsed.error[jss::error] == "invalidParams");
2150 BEAST_EXPECT(
2151 parsed.error[jss::error_message] ==
2152 "Field 'test.Memos' exceeds allowed JSON array size of " + limitStr);
2153 }
2154 }
2155
2156 void
2158 {
2159 testcase("General Invalid Cases");
2160
2161 {
2162 json::Value j;
2163 j[sfLedgerEntry] = 1; // not a valid SField for STParsedJSON
2164 }
2165
2166 {
2167 std::string const goodJson(
2168 R"({"CloseResolution":19,"Method":250,)"
2169 R"("TransactionResult":"tecFROZEN"})");
2170
2171 json::Value jv;
2172 if (BEAST_EXPECT(parseJSONString(goodJson, jv)))
2173 {
2174 STParsedJSONObject parsed("test", jv);
2175 if (BEAST_EXPECT(parsed.object))
2176 {
2177 std::string const& serialized(
2178 // NOLINTNEXTLINE(bugprone-unchecked-optional-access)
2179 to_string(parsed.object->getJson(JsonOptions::Values::None)));
2180 BEAST_EXPECT(serialized == goodJson);
2181 }
2182 }
2183 }
2184
2185 {
2186 std::string const goodJson(
2187 R"({"CloseResolution":19,"Method":"250",)"
2188 R"("TransactionResult":"tecFROZEN"})");
2189 std::string const expectedJson(
2190 R"({"CloseResolution":19,"Method":250,)"
2191 R"("TransactionResult":"tecFROZEN"})");
2192
2193 json::Value jv;
2194 if (BEAST_EXPECT(parseJSONString(goodJson, jv)))
2195 {
2196 // Integer values are always parsed as int,
2197 // unless they're too big. We want a small uint.
2198 jv["CloseResolution"] = json::UInt(19);
2199 STParsedJSONObject parsed("test", jv);
2200 if (BEAST_EXPECT(parsed.object))
2201 {
2202 std::string const& serialized(
2203 // NOLINTNEXTLINE(bugprone-unchecked-optional-access)
2204 to_string(parsed.object->getJson(JsonOptions::Values::None)));
2205 BEAST_EXPECT(serialized == expectedJson);
2206 }
2207 }
2208 }
2209
2210 {
2211 std::string const goodJson(
2212 R"({"CloseResolution":"19","Method":"250",)"
2213 R"("TransactionResult":"tecFROZEN"})");
2214 std::string const expectedJson(
2215 R"({"CloseResolution":19,"Method":250,)"
2216 R"("TransactionResult":"tecFROZEN"})");
2217
2218 json::Value jv;
2219 if (BEAST_EXPECT(parseJSONString(goodJson, jv)))
2220 {
2221 // Integer values are always parsed as int,
2222 // unless they're too big. We want a small uint.
2223 jv["CloseResolution"] = json::UInt(19);
2224 STParsedJSONObject parsed("test", jv);
2225 if (BEAST_EXPECT(parsed.object))
2226 {
2227 std::string const& serialized(
2228 // NOLINTNEXTLINE(bugprone-unchecked-optional-access)
2229 to_string(parsed.object->getJson(JsonOptions::Values::None)));
2230 BEAST_EXPECT(serialized == expectedJson);
2231 }
2232 }
2233 }
2234
2235 {
2236 std::string const json(
2237 R"({"CloseResolution":19,"Method":250,)"
2238 R"("TransactionResult":"terQUEUED"})");
2239
2240 json::Value jv;
2241 if (BEAST_EXPECT(parseJSONString(json, jv)))
2242 {
2243 STParsedJSONObject parsed("test", jv);
2244 BEAST_EXPECT(!parsed.object);
2245 BEAST_EXPECT(parsed.error);
2246 BEAST_EXPECT(parsed.error[jss::error] == "invalidParams");
2247 BEAST_EXPECT(
2248 parsed.error[jss::error_message] ==
2249 "Field 'test.TransactionResult' is out of range.");
2250 }
2251 }
2252
2253 {
2254 std::string const json(
2255 R"({"CloseResolution":19,"Method":"pony",)"
2256 R"("TransactionResult":"tesSUCCESS"})");
2257
2258 json::Value jv;
2259 if (BEAST_EXPECT(parseJSONString(json, jv)))
2260 {
2261 STParsedJSONObject parsed("test", jv);
2262 BEAST_EXPECT(!parsed.object);
2263 BEAST_EXPECT(parsed.error);
2264 BEAST_EXPECT(parsed.error[jss::error] == "invalidParams");
2265 BEAST_EXPECT(
2266 parsed.error[jss::error_message] == "Field 'test.Method' has bad type.");
2267 }
2268 }
2269
2270 {
2271 std::string const json(
2272 R"({"CloseResolution":19,"Method":3294967296,)"
2273 R"("TransactionResult":"tesSUCCESS"})");
2274
2275 json::Value jv;
2276 if (BEAST_EXPECT(parseJSONString(json, jv)))
2277 {
2278 STParsedJSONObject parsed("test", jv);
2279 BEAST_EXPECT(!parsed.object);
2280 BEAST_EXPECT(parsed.error);
2281 BEAST_EXPECT(parsed.error[jss::error] == "invalidParams");
2282 BEAST_EXPECT(
2283 parsed.error[jss::error_message] == "Field 'test.Method' is out of range.");
2284 }
2285 }
2286
2287 {
2288 std::string const json(
2289 R"({"CloseResolution":-10,"Method":42,)"
2290 R"("TransactionResult":"tesSUCCESS"})");
2291
2292 json::Value jv;
2293 if (BEAST_EXPECT(parseJSONString(json, jv)))
2294 {
2295 STParsedJSONObject parsed("test", jv);
2296 BEAST_EXPECT(!parsed.object);
2297 BEAST_EXPECT(parsed.error);
2298 BEAST_EXPECT(parsed.error[jss::error] == "invalidParams");
2299 BEAST_EXPECT(
2300 parsed.error[jss::error_message] ==
2301 "Field 'test.CloseResolution' is out of range.");
2302 }
2303 }
2304
2305 {
2306 std::string const json(
2307 R"({"CloseResolution":19,"Method":3.141592653,)"
2308 R"("TransactionResult":"tesSUCCESS"})");
2309
2310 json::Value jv;
2311 if (BEAST_EXPECT(parseJSONString(json, jv)))
2312 {
2313 STParsedJSONObject parsed("test", jv);
2314 BEAST_EXPECT(!parsed.object);
2315 BEAST_EXPECT(parsed.error);
2316 BEAST_EXPECT(parsed.error[jss::error] == "invalidParams");
2317 BEAST_EXPECT(
2318 parsed.error[jss::error_message] == "Field 'test.Method' has bad type.");
2319 }
2320 }
2321
2322 {
2323 std::string const goodJson(
2324 R"({"CloseResolution":19,"Method":250,)"
2325 R"("TransferFee":"65535"})");
2326 std::string const expectedJson(
2327 R"({"CloseResolution":19,"Method":250,)"
2328 R"("TransferFee":65535})");
2329
2330 json::Value jv;
2331 if (BEAST_EXPECT(parseJSONString(goodJson, jv)))
2332 {
2333 STParsedJSONObject parsed("test", jv);
2334 if (BEAST_EXPECT(parsed.object))
2335 {
2336 std::string const& serialized(
2337 // NOLINTNEXTLINE(bugprone-unchecked-optional-access)
2338 to_string(parsed.object->getJson(JsonOptions::Values::None)));
2339 BEAST_EXPECT(serialized == expectedJson);
2340 }
2341 }
2342 }
2343
2344 {
2345 std::string const json(
2346 R"({"CloseResolution":19,"Method":250,)"
2347 R"("TransferFee":"65536"})");
2348
2349 json::Value jv;
2350 if (BEAST_EXPECT(parseJSONString(json, jv)))
2351 {
2352 STParsedJSONObject parsed("test", jv);
2353 BEAST_EXPECT(!parsed.object);
2354 BEAST_EXPECT(parsed.error);
2355 BEAST_EXPECT(parsed.error[jss::error] == "invalidParams");
2356 BEAST_EXPECT(
2357 parsed.error[jss::error_message] ==
2358 "Field 'test.TransferFee' has invalid data.");
2359 }
2360 }
2361
2362 {
2363 std::string const json(
2364 R"({"CloseResolution":19,"Method":250,)"
2365 R"("TransferFee":"Payment"})");
2366
2367 json::Value jv;
2368 if (BEAST_EXPECT(parseJSONString(json, jv)))
2369 {
2370 STParsedJSONObject parsed("test", jv);
2371 BEAST_EXPECT(!parsed.object);
2372 BEAST_EXPECT(parsed.error);
2373 BEAST_EXPECT(parsed.error[jss::error] == "invalidParams");
2374 BEAST_EXPECT(
2375 parsed.error[jss::error_message] ==
2376 "Field 'test.TransferFee' has invalid data.");
2377 }
2378 }
2379
2380 {
2381 std::string const json(
2382 R"({"CloseResolution":19,"Method":250,)"
2383 R"("TransferFee":true})");
2384
2385 json::Value jv;
2386 if (BEAST_EXPECT(parseJSONString(json, jv)))
2387 {
2388 STParsedJSONObject parsed("test", jv);
2389 BEAST_EXPECT(!parsed.object);
2390 BEAST_EXPECT(parsed.error);
2391 BEAST_EXPECT(parsed.error[jss::error] == "invalidParams");
2392 BEAST_EXPECT(
2393 parsed.error[jss::error_message] == "Field 'test.TransferFee' has bad type.");
2394 }
2395 }
2396 }
2397
2398 void
2399 run() override
2400 {
2401 // Instantiate a jtx::Env so debugLog writes are exercised.
2402 test::jtx::Env const env(*this);
2403 testUInt8();
2404 testUInt16();
2405 testUInt32();
2406 testUInt64();
2407 testUInt128();
2408 testUInt160();
2409 testUInt192();
2410 testUInt256();
2411 testInt32();
2412 testBlob();
2413 testVector256();
2414 testAccount();
2415 testCurrency();
2416 testAmount();
2417 testPathSet();
2418 testIssue();
2419 testNumber();
2420 testObject();
2421 testArray();
2423 testEdgeCases();
2424 }
2425};
2426
2428
2429} // namespace xrpl
T all_of(T... args)
A testsuite class.
Definition suite.h:52
bool unexpected(Condition shouldBeFalse, String const &reason)
DEPRECATED.
Definition suite.h:516
TestcaseT testcase
Memberspace for declaring test cases.
Definition suite.h:155
Unserialize a JSON document into a Value.
Definition json_reader.h:20
bool parse(std::string const &document, Value &root)
Read a Value from a JSON document.
Represents a JSON value.
Definition json_value.h:121
bool isObject() const
Value & append(Value const &value)
Append value to array at the end.
static BaseUInt fromRaw(Container const &c)
Definition base_uint.h:303
A currency issued by an account.
Definition Issue.h:18
Number is a floating point type that can represent a wide range of values.
Definition Number.h:351
Holds the serialized result of parsing an input JSON object.
std::optional< STObject > object
The STObject if the parse was successful.
json::Value error
On failure, an appropriate set of error values.
static bool parseJSONString(std::string const &json, json::Value &to)
void run() override
Runs the suite.
A transaction testing environment.
Definition Env.h:161
JSON (JavaScript Object Notation).
Definition json_errors.h:5
unsigned int UInt
@ Array
array value (ordered list)
Definition json_value.h:32
@ Object
object value (collection of name/value pairs).
Definition json_value.h:33
@ Null
'null' value
Definition json_value.h:26
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
std::optional< AccountID > parseBase58(std::string const &s)
Parse AccountID from checked, base58 string.
std::string toBase58(AccountID const &v)
Convert AccountID to base58 checked string.
Definition AccountID.cpp:95
constexpr std::size_t kMaxParsedJsonDepth
Maximum JSON object nesting depth permitted during parsing.
std::string to_string(BaseUInt< Bits, Tag > const &a)
Definition base_uint.h:658
constexpr std::size_t kMaxParsedJsonArraySize
Maximum number of elements permitted in any JSON array field during parsing.
json::Value getJson(LedgerFill const &fill)
Return a new json::Value representing the ledger with given options.
BEAST_DEFINE_TESTSUITE(AccountTxPaging, app, xrpl)
T starts_with(T... args)
T to_string(T... args)
T what(T... args)