forked from weaselab/weaseljson
Compare commits
11
Commits
d281c35811
...
cdff634057
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
cdff634057 | ||
|
|
16f5d3cd9d | ||
|
|
6beb538b61 | ||
|
|
6520039dc2 | ||
|
|
93203b14f5 | ||
|
|
09c0fb72ca | ||
|
|
70bb33eeaf | ||
|
|
42d37d1fd7 | ||
|
|
5e462f1477 | ||
|
|
f5ceb392c2 | ||
|
|
be92755cbf |
@@ -353,6 +353,132 @@ class SchemagenCyclicArrayTest(unittest.TestCase):
|
||||
self.assertIn("std::optional<std::vector<Role>> roles;", stdout)
|
||||
|
||||
|
||||
class SchemagenCyclicNullableObjectTest(unittest.TestCase):
|
||||
"""Regression tests for issue #14: nullable cyclic $ref targets."""
|
||||
|
||||
def setUp(self):
|
||||
self.repo_root = os.path.dirname(
|
||||
os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
||||
)
|
||||
self.include_dir = os.path.join(self.repo_root, "include")
|
||||
self.lib_src = os.path.join(self.repo_root, "src", "lib.cpp")
|
||||
self.compiler = shutil.which("c++")
|
||||
|
||||
def _compile_harness(self, tmpdir, schema, harness):
|
||||
schema_path = os.path.join(tmpdir, "schema.json")
|
||||
with open(schema_path, "w") as fp:
|
||||
json.dump(schema, fp)
|
||||
header_path = os.path.join(tmpdir, "gen.h")
|
||||
result = subprocess.run(
|
||||
[
|
||||
sys.executable,
|
||||
SCRIPT,
|
||||
schema_path,
|
||||
"-o",
|
||||
header_path,
|
||||
"--namespace",
|
||||
"test_schema",
|
||||
],
|
||||
capture_output=True,
|
||||
text=True,
|
||||
check=False,
|
||||
)
|
||||
self.assertEqual(result.returncode, 0, msg=result.stderr)
|
||||
|
||||
lib_obj = os.path.join(tmpdir, "lib.o")
|
||||
comp_lib = subprocess.run(
|
||||
[
|
||||
self.compiler,
|
||||
"-std=c++20",
|
||||
"-I",
|
||||
self.include_dir,
|
||||
"-I",
|
||||
os.path.join(self.repo_root, "third_party", "include"),
|
||||
"-I",
|
||||
os.path.join(self.repo_root, "third_party", "valgrind"),
|
||||
"-c",
|
||||
self.lib_src,
|
||||
"-o",
|
||||
lib_obj,
|
||||
],
|
||||
capture_output=True,
|
||||
text=True,
|
||||
check=False,
|
||||
)
|
||||
self.assertEqual(comp_lib.returncode, 0, msg=comp_lib.stderr)
|
||||
|
||||
cpp_path = os.path.join(tmpdir, "test.cpp")
|
||||
with open(cpp_path, "w") as fp:
|
||||
fp.write(harness)
|
||||
|
||||
exe_path = os.path.join(tmpdir, "test")
|
||||
comp = subprocess.run(
|
||||
[
|
||||
self.compiler,
|
||||
"-std=c++20",
|
||||
"-I",
|
||||
self.include_dir,
|
||||
"-I",
|
||||
tmpdir,
|
||||
lib_obj,
|
||||
cpp_path,
|
||||
"-o",
|
||||
exe_path,
|
||||
],
|
||||
capture_output=True,
|
||||
text=True,
|
||||
check=False,
|
||||
)
|
||||
self.assertEqual(comp.returncode, 0, msg=comp.stderr)
|
||||
run = subprocess.run([exe_path], capture_output=True, text=True, check=False)
|
||||
self.assertEqual(run.returncode, 0, msg=run.stdout + run.stderr)
|
||||
|
||||
def test_self_referential_nullable_object_accepts_nested_null(self):
|
||||
"""A nullable object $def must stay nullable on recursive $refs."""
|
||||
if not self.compiler:
|
||||
self.skipTest("C++ compiler not available")
|
||||
schema = {
|
||||
"type": "object",
|
||||
"properties": {"self": {"$ref": "#/$defs/Self"}},
|
||||
"$defs": {
|
||||
"Self": {
|
||||
"type": ["object", "null"],
|
||||
"properties": {"self": {"$ref": "#/$defs/Self"}},
|
||||
}
|
||||
},
|
||||
}
|
||||
harness = textwrap.dedent(
|
||||
"""
|
||||
#include "gen.h"
|
||||
#include <cstdio>
|
||||
#include <cstring>
|
||||
struct Case { const char *s; WeaselJsonStatus expected; };
|
||||
int main() {
|
||||
Case cases[] = {
|
||||
{ R"({"self": null})", WeaselJson_OK },
|
||||
{ R"({"self": {"self": null}})", WeaselJson_OK },
|
||||
{ R"({"self": {"self": {}}})", WeaselJson_OK },
|
||||
};
|
||||
for (const auto &c : cases) {
|
||||
test_schema::RootBuilder b;
|
||||
char buf[256];
|
||||
std::strncpy(buf, c.s, sizeof(buf) - 1);
|
||||
buf[sizeof(buf) - 1] = '\\0';
|
||||
WeaselJsonStatus st = b.feed(buf, std::strlen(buf));
|
||||
st = b.finish();
|
||||
if (st != c.expected) {
|
||||
std::printf("case %s expected %d got %d\\n", c.s, c.expected, st);
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
"""
|
||||
)
|
||||
with tempfile.TemporaryDirectory() as tmpdir:
|
||||
self._compile_harness(tmpdir, schema, harness)
|
||||
|
||||
|
||||
class SchemagenIntegerBoundaryTest(unittest.TestCase):
|
||||
"""Regression tests for issue #19: integer slot parsing near int64 boundaries."""
|
||||
|
||||
@@ -466,6 +592,13 @@ class SchemagenIntegerBoundaryTest(unittest.TestCase):
|
||||
("123.0", "WeaselJson_OK", 123),
|
||||
("9e18", "WeaselJson_OK", 9000000000000000000),
|
||||
("10e18", "WeaselJson_REJECT", 0),
|
||||
# Issue #56: zero written with a negative exponent must be accepted.
|
||||
("0e-1", "WeaselJson_OK", 0),
|
||||
("0e-2", "WeaselJson_OK", 0),
|
||||
("0.0e-2", "WeaselJson_OK", 0),
|
||||
("-0e-2", "WeaselJson_OK", 0),
|
||||
("0e-20", "WeaselJson_OK", 0),
|
||||
("0.000e-5", "WeaselJson_OK", 0),
|
||||
]
|
||||
cases_src = self._build_cases_array("root", cases)
|
||||
harness = textwrap.dedent(
|
||||
@@ -508,6 +641,9 @@ class SchemagenIntegerBoundaryTest(unittest.TestCase):
|
||||
('{"age":2.0}', "WeaselJson_OK", 2),
|
||||
('{"age":0.0001e4}', "WeaselJson_OK", 1),
|
||||
('{"age":0.001}', "WeaselJson_REJECT", 0),
|
||||
# Issue #56: zero written with a negative exponent must be accepted.
|
||||
('{"age":0e-2}', "WeaselJson_OK", 0),
|
||||
('{"age":-0e-20}', "WeaselJson_OK", 0),
|
||||
(
|
||||
'{"age":-9223372036854775808.0}',
|
||||
"WeaselJson_OK",
|
||||
|
||||
@@ -1058,10 +1058,10 @@ private:
|
||||
++trim;
|
||||
}}
|
||||
int64_t finalExp = exp - fracDigits + trim;
|
||||
if (finalExp < 0) return false;
|
||||
size_t leadingZeros = 0;
|
||||
while (leadingZeros < digits.size() && digits[leadingZeros] == '0') ++leadingZeros;
|
||||
if (leadingZeros == digits.size()) {{ out = 0; return true; }}
|
||||
if (finalExp < 0) return false;
|
||||
if (leadingZeros > 0) digits.erase(0, leadingZeros);
|
||||
|
||||
constexpr uint64_t kMaxNeg = 9223372036854775808ULL;
|
||||
|
||||
@@ -38,6 +38,8 @@ enum WeaselJsonStatus {
|
||||
WeaselJson_REJECT,
|
||||
/** json is too deeply nested */
|
||||
WeaselJson_OVERFLOW,
|
||||
/** Tried to call parse on a null parser */
|
||||
WeaselJson_NULL,
|
||||
};
|
||||
|
||||
typedef struct WeaselJsonParser WeaselJsonParser;
|
||||
@@ -65,7 +67,7 @@ void WeaselJsonParser_destroy(WeaselJsonParser *parser);
|
||||
/** Incrementally parse `len` more bytes starting at `buf`. `buf` may be
|
||||
* modified. Call with `len` 0 to indicate end of data. `buf` may be null if
|
||||
* `len` is 0. `len` must not be negative; a negative length is treated as a
|
||||
* rejected input. */
|
||||
* rejected input. Returns WeaselJson_NULL if parser is null */
|
||||
WeaselJsonStatus WeaselJsonParser_parse(WeaselJsonParser *parser, char *buf,
|
||||
int len);
|
||||
|
||||
|
||||
@@ -46,6 +46,9 @@ WeaselJsonParser_destroy(WeaselJsonParser *parser) {
|
||||
|
||||
__attribute__((visibility("default"))) WeaselJsonStatus
|
||||
WeaselJsonParser_parse(WeaselJsonParser *parser, char *buf, int len) {
|
||||
if (parser == nullptr) [[unlikely]] {
|
||||
return WeaselJson_NULL;
|
||||
}
|
||||
return ((Parser3 *)parser)->parse(buf, len);
|
||||
}
|
||||
}
|
||||
|
||||
+10
-10
@@ -139,7 +139,7 @@ struct Parser3 {
|
||||
stackPtr = stack();
|
||||
std::ignore = push({N_VALUE, N_WHITESPACE, T_EOF});
|
||||
inKey = false;
|
||||
rejected = false;
|
||||
terminalStatus = WeaselJson_OK;
|
||||
utf8Codepoint = 0;
|
||||
utf16Surrogate = 0;
|
||||
minCodepoint = 0;
|
||||
@@ -162,7 +162,7 @@ struct Parser3 {
|
||||
NumDfa numDfa;
|
||||
Utf8Dfa strDfa;
|
||||
bool inKey = false;
|
||||
bool rejected = false;
|
||||
WeaselJsonStatus terminalStatus = WeaselJson_OK;
|
||||
|
||||
#ifndef HAS_MUSTTAIL
|
||||
char *stashBufForTrampoline;
|
||||
@@ -650,7 +650,7 @@ inline PRESERVE_NONE ContinuationStatus n_string2(Parser3 *self, char *buf,
|
||||
self->writeBuf[0] = (0b00000111 & codepoint) | 0b11110000;
|
||||
self->writeBuf += 4;
|
||||
}
|
||||
} else if (0xdc00 <= codepoint && codepoint <= 0xdfff) {
|
||||
} else if (0xdc00 <= codepoint && codepoint <= 0xdfff) [[unlikely]] {
|
||||
return WeaselJson_REJECT;
|
||||
} else {
|
||||
if (!(self->flags & WeaselJsonRaw)) {
|
||||
@@ -1070,12 +1070,12 @@ constexpr inline struct ContinuationTable {
|
||||
inline WeaselJsonStatus Parser3::parse(char *buf, int len) {
|
||||
this->dataBegin = this->writeBuf = buf;
|
||||
|
||||
if (this->rejected) [[unlikely]] {
|
||||
return WeaselJson_REJECT;
|
||||
if (this->terminalStatus != WeaselJson_OK) [[unlikely]] {
|
||||
return this->terminalStatus;
|
||||
}
|
||||
|
||||
if (len < 0) [[unlikely]] {
|
||||
this->rejected = true;
|
||||
this->terminalStatus = WeaselJson_REJECT;
|
||||
return WeaselJson_REJECT;
|
||||
}
|
||||
|
||||
@@ -1085,8 +1085,8 @@ inline WeaselJsonStatus Parser3::parse(char *buf, int len) {
|
||||
// range.
|
||||
ContinuationStatus status =
|
||||
symbolTables.continuations[top()](this, buf, buf + len);
|
||||
if (status == WeaselJson_REJECT) {
|
||||
this->rejected = true;
|
||||
if (status > WeaselJson_AGAIN) {
|
||||
this->terminalStatus = WeaselJsonStatus(status);
|
||||
}
|
||||
return WeaselJsonStatus(status);
|
||||
#else
|
||||
@@ -1095,8 +1095,8 @@ inline WeaselJsonStatus Parser3::parse(char *buf, int len) {
|
||||
while ((result = symbolTables.continuations[top()](
|
||||
this, stashBufForTrampoline, buf + len)) == kBounce)
|
||||
;
|
||||
if (result == WeaselJson_REJECT) {
|
||||
this->rejected = true;
|
||||
if (result > WeaselJson_AGAIN) {
|
||||
this->terminalStatus = WeaselJsonStatus(result);
|
||||
}
|
||||
return WeaselJsonStatus(result);
|
||||
#endif
|
||||
|
||||
@@ -202,6 +202,62 @@ TEST_CASE("parser3") {
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("overflow state is sticky") {
|
||||
auto c = noopCallbacks();
|
||||
// stackSize 3 is exactly big enough to hold reset()'s bootstrap, but too
|
||||
// small for nested arrays. Overflows must be terminal like rejects: a later
|
||||
// end-of-data call must never report OK for an incomplete document.
|
||||
auto *parser = WeaselJsonParser_create(3, &c, nullptr, 0);
|
||||
REQUIRE(parser != nullptr);
|
||||
|
||||
std::string doc = "[[";
|
||||
REQUIRE(WeaselJsonParser_parse(parser, doc.data(), doc.size()) ==
|
||||
WeaselJson_OVERFLOW);
|
||||
|
||||
// After overflow, the end-of-data call must not return OK.
|
||||
REQUIRE(WeaselJsonParser_parse(parser, nullptr, 0) != WeaselJson_OK);
|
||||
// It should keep reporting a terminal failure.
|
||||
REQUIRE(WeaselJsonParser_parse(parser, nullptr, 0) != WeaselJson_OK);
|
||||
|
||||
// Further data chunks must also stay terminal.
|
||||
std::string more = "]]";
|
||||
REQUIRE(WeaselJsonParser_parse(parser, more.data(), more.size()) !=
|
||||
WeaselJson_OK);
|
||||
|
||||
WeaselJsonParser_destroy(parser);
|
||||
}
|
||||
|
||||
TEST_CASE("overflow is sticky for nested objects") {
|
||||
auto c = noopCallbacks();
|
||||
auto *parser = WeaselJsonParser_create(4, &c, nullptr, 0);
|
||||
REQUIRE(parser != nullptr);
|
||||
|
||||
std::string doc = "{\"a\":{ \"a\":";
|
||||
REQUIRE(WeaselJsonParser_parse(parser, doc.data(), doc.size()) ==
|
||||
WeaselJson_OVERFLOW);
|
||||
REQUIRE(WeaselJsonParser_parse(parser, nullptr, 0) != WeaselJson_OK);
|
||||
|
||||
WeaselJsonParser_destroy(parser);
|
||||
}
|
||||
|
||||
TEST_CASE("reset clears overflow state") {
|
||||
auto c = noopCallbacks();
|
||||
auto *parser = WeaselJsonParser_create(3, &c, nullptr, 0);
|
||||
REQUIRE(parser != nullptr);
|
||||
|
||||
std::string doc = "[[";
|
||||
REQUIRE(WeaselJsonParser_parse(parser, doc.data(), doc.size()) ==
|
||||
WeaselJson_OVERFLOW);
|
||||
// After reset the parser should accept a minimal document again.
|
||||
WeaselJsonParser_reset(parser);
|
||||
std::string copy = "1";
|
||||
REQUIRE(WeaselJsonParser_parse(parser, copy.data(), copy.size()) ==
|
||||
WeaselJson_AGAIN);
|
||||
REQUIRE(WeaselJsonParser_parse(parser, nullptr, 0) == WeaselJson_OK);
|
||||
|
||||
WeaselJsonParser_destroy(parser);
|
||||
}
|
||||
|
||||
TEST_CASE("rejected state is sticky") {
|
||||
auto c = noopCallbacks();
|
||||
auto *parser = WeaselJsonParser_create(1024, &c, nullptr, 0);
|
||||
@@ -274,6 +330,10 @@ TEST_CASE("parse rejects negative length") {
|
||||
WeaselJsonParser_destroy(parser);
|
||||
}
|
||||
|
||||
TEST_CASE("Calling parse with nullptr doesn't crash") {
|
||||
REQUIRE(WeaselJsonParser_parse(nullptr, nullptr, 0) == WeaselJson_NULL);
|
||||
}
|
||||
|
||||
TEST_CASE("streaming") { testStreaming(json); }
|
||||
|
||||
TEST_CASE("reset clears inKey and transient state") {
|
||||
|
||||
@@ -33,6 +33,9 @@ int main(int argc, char **argv) {
|
||||
case WeaselJson_REJECT:
|
||||
case WeaselJson_OVERFLOW:
|
||||
return 1;
|
||||
case WeaselJson_NULL:
|
||||
fprintf(stderr, "parse called with a null parser\n");
|
||||
return 1;
|
||||
}
|
||||
if (l == 0) {
|
||||
return 1;
|
||||
|
||||
@@ -30,6 +30,7 @@ class WeaselJsonStatus(enum.Enum):
|
||||
AGAIN = 1
|
||||
REJECT = 2
|
||||
OVERFLOW = 3
|
||||
NULL = 4
|
||||
|
||||
|
||||
class WeaselJsonCallbacksBase:
|
||||
|
||||
Reference in New Issue
Block a user