|
16 | 16 |
|
17 | 17 | #include <gtest/gtest.h> |
18 | 18 |
|
| 19 | +#include <cstdio> |
| 20 | +#include <cstdlib> |
19 | 21 | #include <limits> |
20 | 22 |
|
| 23 | +#if !defined(_WIN32) |
| 24 | +#include <signal.h> |
| 25 | +#include <unistd.h> |
| 26 | +#endif |
| 27 | + |
21 | 28 | #include "sonic/sonic.h" |
22 | 29 |
|
23 | 30 | namespace { |
@@ -621,23 +628,50 @@ std::vector<int> splitToInts(const std::string& str) { |
621 | 628 |
|
622 | 629 | return numbers; |
623 | 630 | } |
624 | | -TEST(JsonPath, DISABLED_JsonInfiniteLoop) { |
625 | | - const std::string integers( |
626 | | - "123 34 -28 -65 -99 -23 -103 -87 34 58 32 48 46 55 57 49 53 44 32 34 -23 " |
627 | | - "-127 -109 -27 -91 -121 -23 -123 -73 -27 -88 -127 34 58 32 48 46 55 56 " |
628 | | - "48 56 44 32 34 -24 -121 -86 -27 -118 -88 -26 -116 -95 34 58 32 48 46 55 " |
629 | | - "55 56 49 125"); |
630 | | - auto ints = splitToInts(integers); |
631 | | - std::string json(""); |
632 | | - for (const auto i : ints) { |
633 | | - json.push_back((char)i); |
634 | | - } |
| 631 | +TEST(JsonPath, JsonInfiniteLoop) { |
| 632 | +#if defined(_WIN32) |
| 633 | + GTEST_SKIP() << "Windows does not support alarm()-based timeout guard."; |
| 634 | +#else |
| 635 | + // This case historically could hang; run it in a subprocess with an alarm so |
| 636 | + // the whole test suite won't get stuck. |
| 637 | + ASSERT_EXIT( |
| 638 | + { |
| 639 | + alarm(2); |
| 640 | + |
| 641 | + const std::string integers( |
| 642 | + "123 34 -28 -65 -99 -23 -103 -87 34 58 32 48 46 55 57 49 53 44 32 " |
| 643 | + "34 " |
| 644 | + "-23 " |
| 645 | + "-127 -109 -27 -91 -121 -23 -123 -73 -27 -88 -127 34 58 32 48 46 " |
| 646 | + "55 " |
| 647 | + "56 " |
| 648 | + "48 56 44 32 34 -24 -121 -86 -27 -118 -88 -26 -116 -95 34 58 32 48 " |
| 649 | + "46 55 " |
| 650 | + "55 56 49 125"); |
| 651 | + auto ints = splitToInts(integers); |
| 652 | + std::string json(""); |
| 653 | + for (const auto i : ints) { |
| 654 | + json.push_back((char)i); |
| 655 | + } |
635 | 656 |
|
636 | | - auto got = |
637 | | - GetByJsonPathOnDemand<SerializeFlags::kSerializeUnicodeEscapeUppercase>( |
638 | | - json, "$.motor_content_boost"); |
639 | | - EXPECT_EQ(std::get<1>(got), kParseErrorUnexpect); |
640 | | - EXPECT_EQ(std::get<0>(got), ""); |
| 657 | + auto got = GetByJsonPathOnDemand< |
| 658 | + SerializeFlags::kSerializeUnicodeEscapeUppercase>( |
| 659 | + json, "$.motor_content_boost"); |
| 660 | + const auto& out = std::get<0>(got); |
| 661 | + const auto err = std::get<1>(got); |
| 662 | + if (err == kErrorNone) { |
| 663 | + std::fprintf(stderr, "unexpected success, out=%s\n", out.c_str()); |
| 664 | + std::_Exit(1); |
| 665 | + } |
| 666 | + if (!out.empty()) { |
| 667 | + std::fprintf(stderr, "expected empty output, err=%d, out=%s\n", |
| 668 | + static_cast<int>(err), out.c_str()); |
| 669 | + std::_Exit(1); |
| 670 | + } |
| 671 | + std::_Exit(0); |
| 672 | + }, |
| 673 | + ::testing::ExitedWithCode(0), ""); |
| 674 | +#endif |
641 | 675 | } |
642 | 676 | TEST(JsonPath, JsonInfiniteLoop2) { |
643 | 677 | std::string json("[8"); |
|
0 commit comments