Skip to content

Commit 06223d3

Browse files
committed
fix_test
1 parent 0e2c2ff commit 06223d3

File tree

1 file changed

+50
-16
lines changed

1 file changed

+50
-16
lines changed

tests/jsonpath_test.cpp

Lines changed: 50 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,15 @@
1616

1717
#include <gtest/gtest.h>
1818

19+
#include <cstdio>
20+
#include <cstdlib>
1921
#include <limits>
2022

23+
#if !defined(_WIN32)
24+
#include <signal.h>
25+
#include <unistd.h>
26+
#endif
27+
2128
#include "sonic/sonic.h"
2229

2330
namespace {
@@ -621,23 +628,50 @@ std::vector<int> splitToInts(const std::string& str) {
621628

622629
return numbers;
623630
}
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+
}
635656

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
641675
}
642676
TEST(JsonPath, JsonInfiniteLoop2) {
643677
std::string json("[8");

0 commit comments

Comments
 (0)