20 #include <ripple/basics/Log.h>
21 #include <ripple/protocol/jss.h>
22 #include <ripple/protocol/SecretKey.h>
23 #include <ripple/protocol/st.h>
24 #include <ripple/json/json_reader.h>
25 #include <ripple/json/to_string.h>
26 #include <ripple/beast/unit_test.h>
46 testcase (
"parse json array invalid children");
61 "\"ModifiedNode\":{\"Sequence\":1}, "
62 "\"DeletedNode\":{\"Sequence\":1}"
68 unexpected(!parsedOK,
"failed to parse");
70 BEAST_EXPECT(! parsed.
object);
75 unexpected (what.
find(
"First level children of `Template`") != 0);
81 testcase (
"parse json array");
83 "{\"Template\":[{\"ModifiedNode\":{\"Sequence\":1}}]}");
90 BEAST_EXPECT(parsed.
object);
93 BEAST_EXPECT(serialized == json);
97 fail (
"Couldn't parse json: " + json);
103 testcase(
"parse json object");
107 R
"({"CloseResolution":19,"Method":250,)"
108 R"("TransactionResult":"tecFROZEN"})");
114 if (BEAST_EXPECT(parsed.
object))
118 BEAST_EXPECT(serialized == goodJson);
125 R
"({"CloseResolution":19,"Method":"250",)"
126 R"("TransactionResult":"tecFROZEN"})");
128 R
"({"CloseResolution":19,"Method":250,)"
129 R"("TransactionResult":"tecFROZEN"})");
138 if (BEAST_EXPECT(parsed.
object))
142 BEAST_EXPECT(serialized == expectedJson);
149 R
"({"CloseResolution":19,"Method":250,)"
150 R"("TransactionResult":"terQUEUED"})");
156 BEAST_EXPECT(!parsed.
object);
157 BEAST_EXPECT(parsed.
error);
158 BEAST_EXPECT(parsed.
error[jss::error] ==
"invalidParams");
159 BEAST_EXPECT(parsed.
error[jss::error_message] ==
160 "Field 'test.TransactionResult' is out of range.");
166 R
"({"CloseResolution":19,"Method":"pony",)"
167 R"("TransactionResult":"tesSUCCESS"})");
173 BEAST_EXPECT(!parsed.
object);
174 BEAST_EXPECT(parsed.
error);
175 BEAST_EXPECT(parsed.
error[jss::error] ==
"invalidParams");
176 BEAST_EXPECT(parsed.
error[jss::error_message] ==
177 "Field 'test.Method' has bad type.");
183 R
"({"CloseResolution":19,"Method":3294967296,)"
184 R"("TransactionResult":"tesSUCCESS"})");
190 BEAST_EXPECT(!parsed.
object);
191 BEAST_EXPECT(parsed.
error);
192 BEAST_EXPECT(parsed.
error[jss::error] ==
"invalidParams");
193 BEAST_EXPECT(parsed.
error[jss::error_message] ==
194 "Field 'test.Method' is out of range.");
200 R
"({"CloseResolution":-10,"Method":42,)"
201 R"("TransactionResult":"tesSUCCESS"})");
207 BEAST_EXPECT(!parsed.
object);
208 BEAST_EXPECT(parsed.
error);
209 BEAST_EXPECT(parsed.
error[jss::error] ==
"invalidParams");
210 BEAST_EXPECT(parsed.
error[jss::error_message] ==
211 "Field 'test.CloseResolution' is out of range.");
217 R
"({"CloseResolution":19,"Method":3.141592653,)"
218 R"("TransactionResult":"tesSUCCESS"})");
224 BEAST_EXPECT(!parsed.
object);
225 BEAST_EXPECT(parsed.
error);
226 BEAST_EXPECT(parsed.
error[jss::error] ==
"invalidParams");
227 BEAST_EXPECT(parsed.
error[jss::error_message] ==
228 "Field 'test.Method' has bad type.");
235 testcase (
"serialization");
240 except<std::runtime_error>( [&]()
252 BEAST_EXPECT (shouldBeInvalid ==
sfInvalid);
254 testInvalid (
STI_VL, 255);
262 except<std::runtime_error>( [&]()
269 except<std::runtime_error>( [&]()
295 STObject object1 (elements, sfTestObject);
306 unexpected (!object1.
isFieldPresent (sfTestH256),
"STObject Error 2");
316 fail (
"STObject error 4");
325 unexpected (object1.
isFieldPresent (sfTestH256),
"STObject error 5");
327 unexpected (object1.
getFlags () != 0,
"STObject error 6");
334 unexpected (object1.
isFieldPresent (sfTestH256),
"STObject error 8");
336 unexpected (copy.isFieldPresent (sfTestH256),
"STObject error 9");
338 unexpected (object1.
getSerializer () != copy.getSerializer (),
339 "STObject error 10");
341 copy.setFieldU32 (sfTestU32, 1);
343 unexpected (object1.
getSerializer () == copy.getSerializer (),
344 "STObject error 11");
346 for (
int i = 0; i < 1000; i++)
356 STObject object3 (elements, it, sfTestObject);
358 unexpected (object1.
getFieldVL (sfTestVL) != j,
"STObject error");
360 unexpected (object3.
getFieldVL (sfTestVL) != j,
"STObject error");
366 for (
int i = 0; i < uints.
capacity(); ++i)
376 STObject object3(elements, it, sfTestObject);
381 BEAST_EXPECT(uints1 == uints3);
400 auto const st = [&]()
408 BEAST_EXPECT(st[sf1Outer] == 1);
409 BEAST_EXPECT(st[sf2Outer] == 2);
410 except<STObject::FieldErr>([&]()
412 BEAST_EXPECT(*st[~sf1Outer] == 1);
413 BEAST_EXPECT(*st[~sf2Outer] == 2);
414 BEAST_EXPECT(st[~sf3Outer] == boost::none);
415 BEAST_EXPECT(!! st[~sf1Outer]);
416 BEAST_EXPECT(!! st[~sf2Outer]);
417 BEAST_EXPECT(! st[~sf3Outer]);
418 BEAST_EXPECT(st[sf1Outer] != st[sf2Outer]);
419 BEAST_EXPECT(st[~sf1Outer] != st[~sf2Outer]);
433 auto const st = [&]()
441 BEAST_EXPECT(st[sf1Outer] == 1);
442 BEAST_EXPECT(st[sf2Outer] == 2);
443 BEAST_EXPECT(st[sf3Outer] == 0);
444 BEAST_EXPECT(*st[~sf1Outer] == 1);
445 BEAST_EXPECT(*st[~sf2Outer] == 2);
446 BEAST_EXPECT(*st[~sf3Outer] == 0);
447 BEAST_EXPECT(!! st[~sf1Outer]);
448 BEAST_EXPECT(!! st[~sf2Outer]);
449 BEAST_EXPECT(!! st[~sf3Outer]);
456 unexcept([&]() { st[sf1Outer]; });
457 except([&](){
return st[sf1Outer] == 0; });
458 BEAST_EXPECT(st[~sf1Outer] == boost::none);
459 BEAST_EXPECT(st[~sf1Outer] == boost::optional<std::uint32_t>{});
460 BEAST_EXPECT(st[~sf1Outer] != boost::optional<std::uint32_t>(1));
461 BEAST_EXPECT(! st[~sf1Outer]);
463 BEAST_EXPECT(st[sf1Outer] == 2);
464 BEAST_EXPECT(st[~sf1Outer] != boost::none);
465 BEAST_EXPECT(st[~sf1Outer] == boost::optional<std::uint32_t>(2));
466 BEAST_EXPECT(!! st[~sf1Outer]);
468 BEAST_EXPECT(st[sf1Outer] == 1);
469 BEAST_EXPECT(!! st[sf1Outer]);
470 BEAST_EXPECT(!! st[~sf1Outer]);
472 BEAST_EXPECT(! st[sf1Outer]);
473 BEAST_EXPECT(!! st[~sf1Outer]);
474 st[~sf1Outer] = boost::none;
475 BEAST_EXPECT(! st[~sf1Outer]);
476 BEAST_EXPECT(st[~sf1Outer] == boost::none);
477 BEAST_EXPECT(st[~sf1Outer] == boost::optional<std::uint32_t>{});
478 st[~sf1Outer] = boost::none;
479 BEAST_EXPECT(! st[~sf1Outer]);
480 except([&]() {
return st[sf1Outer] == 0; });
481 except([&]() {
return *st[~sf1Outer]; });
483 BEAST_EXPECT(st[sf1Outer] == 1);
484 BEAST_EXPECT(!! st[sf1Outer]);
485 BEAST_EXPECT(!! st[~sf1Outer]);
487 st[sf2Outer] = st[sf1Outer];
488 BEAST_EXPECT(st[sf1Outer] == 3);
489 BEAST_EXPECT(st[sf2Outer] == 3);
490 BEAST_EXPECT(st[sf2Outer] == st[sf1Outer]);
492 st[sf2Outer] = st[sf1Outer];
493 BEAST_EXPECT(st[sf1Outer] == 4);
494 BEAST_EXPECT(st[sf2Outer] == 4);
495 BEAST_EXPECT(st[sf2Outer] == st[sf1Outer]);
502 BEAST_EXPECT(!! st[~sf1Outer]);
503 BEAST_EXPECT(st[~sf1Outer] != boost::none);
504 BEAST_EXPECT(st[sf1Outer] == 0);
505 BEAST_EXPECT(*st[~sf1Outer] == 0);
506 BEAST_EXPECT(! st[~sf2Outer]);
507 BEAST_EXPECT(st[~sf2Outer] == boost::none);
508 except([&]() {
return st[sf2Outer] == 0; });
509 BEAST_EXPECT(!! st[~sf3Outer]);
510 BEAST_EXPECT(st[~sf3Outer] != boost::none);
511 BEAST_EXPECT(st[sf3Outer] == 0);
512 except([&]() { st[~sf1Outer] = boost::none; });
514 BEAST_EXPECT(st[sf1Outer] == 1);
515 BEAST_EXPECT(*st[~sf1Outer] == 1);
516 BEAST_EXPECT(!! st[~sf1Outer]);
518 BEAST_EXPECT(st[sf1Outer] == 0);
519 BEAST_EXPECT(*st[~sf1Outer] == 0);
520 BEAST_EXPECT(!! st[~sf1Outer]);
522 BEAST_EXPECT(st[sf2Outer] == 2);
523 BEAST_EXPECT(*st[~sf2Outer] == 2);
524 BEAST_EXPECT(!! st[~sf2Outer]);
525 st[~sf2Outer] = boost::none;
526 except([&]() {
return *st[~sf2Outer]; });
527 BEAST_EXPECT(! st[~sf2Outer]);
529 BEAST_EXPECT(st[sf3Outer] == 3);
530 BEAST_EXPECT(*st[~sf3Outer] == 3);
531 BEAST_EXPECT(!! st[~sf3Outer]);
533 BEAST_EXPECT(st[sf3Outer] == 2);
534 BEAST_EXPECT(*st[~sf3Outer] == 2);
535 BEAST_EXPECT(!! st[~sf3Outer]);
537 BEAST_EXPECT(st[sf3Outer] == 0);
538 BEAST_EXPECT(*st[~sf3Outer] == 0);
539 BEAST_EXPECT(!! st[~sf3Outer]);
540 except([&]() { st[~sf3Outer] = boost::none; });
541 BEAST_EXPECT(st[sf3Outer] == 0);
542 BEAST_EXPECT(*st[~sf3Outer] == 0);
543 BEAST_EXPECT(!! st[~sf3Outer]);
550 auto const v = ~st[~sf1Outer];
553 boost::optional<std::uint32_t>>::value,
"");
574 BEAST_EXPECT(! b.
empty());
575 st[sf4] = std::move(b);
576 BEAST_EXPECT(b.
empty());
577 BEAST_EXPECT(
Slice(st[sf4]).size() == 1);
578 st[~sf4] = boost::none;
579 BEAST_EXPECT(! ~st[~sf4]);
582 BEAST_EXPECT(b.
size() == 2);
583 BEAST_EXPECT(
Slice(st[sf4]).size() == 2);
585 BEAST_EXPECT(
Slice(st[sf4]).size() == 2);
586 BEAST_EXPECT(
Slice(st[sf5]).size() == 2);
590 BEAST_EXPECT(st[sf5] ==
Slice{});
591 BEAST_EXPECT(!! st[~sf5]);
592 BEAST_EXPECT(!! ~st[~sf5]);
594 st[sf5] = std::move(b);
595 BEAST_EXPECT(b.
empty());
596 BEAST_EXPECT(
Slice(st[sf5]).size() == 1);
597 st[~sf4] = boost::none;
598 BEAST_EXPECT(! ~st[~sf4]);
606 BEAST_EXPECT(! st[~sf5]);
612 st[~sf5] = boost::none;
615 BEAST_EXPECT(pk.size() == 0);
628 st[sf] = std::move(v);
629 auto const& cst = st;
630 BEAST_EXPECT(cst[sf].size() == 2);
631 BEAST_EXPECT(cst[~sf]->size() == 2);
632 BEAST_EXPECT(cst[sf][0] == 1);
633 BEAST_EXPECT(cst[sf][1] == 2);
653 BEAST_EXPECT(cst[sf1].size() == 0);
654 BEAST_EXPECT(! cst[~sf2]);
655 BEAST_EXPECT(cst[sf3].size() == 0);
659 BEAST_EXPECT(cst[sf1].size() == 1);
660 BEAST_EXPECT(cst[sf1][0] ==
uint256{1});
662 BEAST_EXPECT(cst[sf2].size() == 1);
663 BEAST_EXPECT(cst[sf2][0] ==
uint256{1});
664 st[~sf2] = boost::none;
665 BEAST_EXPECT(! st[~sf2]);
667 BEAST_EXPECT(cst[sf3].size() == 1);
668 BEAST_EXPECT(cst[sf3][0] ==
uint256{1});
670 BEAST_EXPECT(cst[sf3].size() == 0);
677 testcase (
"Malformed serialized forms");
682 { 0xe9, 0x12, 0xab, 0xcd, 0x12, 0xfe, 0xdc }};
684 auto obj = std::make_shared<STArray>(sit,
sfMetadata);
689 BEAST_EXPECT(strcmp(e.
what(),
"Duplicate field detected") == 0);
696 auto obj = std::make_shared<STObject>(sit,
sfMetadata);
701 BEAST_EXPECT(strcmp(e.
what(),
"Duplicate field detected") == 0);