Files
mbs/tests/test_script.cpp
Allan K. Koech cda2e9daa7 fix: harden arithmetic, string ops, and lexer error handling
- S4: add isfinite checks after all arithmetic ops (Add, Sub, Mul, Div,
  Pow, Neg) so NaN/Infinity can never enter JSON output
- S5: fix is_integral_number INT64_MAX boundary check to eliminate
  undefined behaviour from double rounding
- S6: cap string concatenation result at MAX_STRING_SIZE (1MB)
- S7: add optional max_source_length parameter to test/eval_val/eval_true
- S8: catch std::out_of_range in lexNumber() and report a descriptive
  ParseError instead of a bare 'stod' message
2026-08-20 14:00:17 +03:00

241 lines
8.2 KiB
C++

#include <catch2/catch_test_macros.hpp>
#include <mbs/script.hpp>
#include <nlohmann/json.hpp>
using mbs::EvalErr;
using mbs::EvalOk;
using mbs::eval_true;
using mbs::eval_val;
using mbs::test;
using nlohmann::json;
TEST_CASE("single-line comments are skipped", "[lexer]") {
auto r = eval_val(
R"(
// comment
1 + 2 // trailing
)",
json::object());
REQUIRE(std::holds_alternative<EvalOk>(r));
REQUIRE(std::get<EvalOk>(r).value == 3.0);
}
TEST_CASE("string times integer repeats like Python", "[ops]") {
auto r = eval_val(R"("ab" * 3)", json::object());
REQUIRE(std::holds_alternative<EvalOk>(r));
REQUIRE(std::get<EvalOk>(r).value == "ababab");
}
TEST_CASE("integer times string repeats", "[ops]") {
auto r = eval_val(R"(4 * "x")", json::object());
REQUIRE(std::holds_alternative<EvalOk>(r));
REQUIRE(std::get<EvalOk>(r).value == "xxxx");
}
TEST_CASE("string times non-integral number is invalid (null)", "[ops]") {
auto r = eval_val(R"("a" * 2.5)", json::object());
REQUIRE(std::holds_alternative<EvalOk>(r));
REQUIRE(std::get<EvalOk>(r).value.is_null());
}
TEST_CASE("string times zero yields empty string", "[ops]") {
auto r = eval_val(R"("hi" * 0)", json::object());
REQUIRE(std::holds_alternative<EvalOk>(r));
REQUIRE(std::get<EvalOk>(r).value == "");
}
TEST_CASE("power requires numeric operands", "[ops]") {
SECTION("string ^ number -> null") {
auto r = eval_val(R"("a" ^ 2)", json::object());
REQUIRE(std::holds_alternative<EvalOk>(r));
REQUIRE(std::get<EvalOk>(r).value.is_null());
}
SECTION("number ^ string -> null") {
auto r = eval_val(R"(2 ^ "b")", json::object());
REQUIRE(std::holds_alternative<EvalOk>(r));
REQUIRE(std::get<EvalOk>(r).value.is_null());
}
SECTION("valid number power") {
auto r = eval_val(R"(2 ^ 3)", json::object());
REQUIRE(std::holds_alternative<EvalOk>(r));
REQUIRE(std::get<EvalOk>(r).value == 8.0);
}
}
TEST_CASE("invalid mixed arithmetic returns null", "[ops]") {
auto r = eval_val(R"("a" + 1)", json::object());
REQUIRE(std::holds_alternative<EvalOk>(r));
REQUIRE(std::get<EvalOk>(r).value.is_null());
}
TEST_CASE("string + string concatenation", "[ops]") {
auto r = eval_val(R"("foo" + "bar")", json::object());
REQUIRE(std::holds_alternative<EvalOk>(r));
REQUIRE(std::get<EvalOk>(r).value == "foobar");
}
TEST_CASE("test() rejects unknown methods", "[test]") {
auto t = test(R"(@x.unknown())");
REQUIRE_FALSE(t.ok);
REQUIRE(t.error.find("unknown method") != std::string::npos);
}
TEST_CASE("test() accepts known script", "[test]") {
auto t = test(R"(@user.age > 0 && ['a'].has("a"))");
REQUIRE(t.ok);
}
TEST_CASE("eval_true uses truthiness", "[eval]") {
json ctx = {{"flag", false}};
auto b = eval_true("@flag", ctx);
REQUIRE(b.ok);
REQUIRE_FALSE(b.value);
}
TEST_CASE("context variable missing is null", "[eval]") {
auto r = eval_val("@missing", json::object());
REQUIRE(std::holds_alternative<EvalOk>(r));
REQUIRE(std::get<EvalOk>(r).value.is_null());
}
TEST_CASE("len on number returns number", "[builtin]") {
auto r = eval_val(R"( (5).len() )", json::object());
REQUIRE(std::holds_alternative<EvalOk>(r));
REQUIRE(std::get<EvalOk>(r).value == 5.0);
}
TEST_CASE("array literal has membership", "[eval]") {
json ctx = {{"role", "student"}};
auto r = eval_val(R"(['student','teacher'].has(@role))", ctx);
REQUIRE(std::holds_alternative<EvalOk>(r));
REQUIRE(std::get<EvalOk>(r).value == true);
}
TEST_CASE("short-circuit and skips rhs", "[eval]") {
json ctx = json::object();
auto r = eval_val(R"(false && @nope.foo)", ctx);
REQUIRE(std::holds_alternative<EvalOk>(r));
REQUIRE(std::get<EvalOk>(r).value == false);
}
TEST_CASE("string repeat with huge count returns null", "[security]") {
auto r = eval_val(R"("x" * 999999999)", json::object());
REQUIRE(std::holds_alternative<EvalOk>(r));
REQUIRE(std::get<EvalOk>(r).value.is_null());
}
TEST_CASE("number * string with huge count returns null", "[security]") {
auto r = eval_val(R"(999999999 * "y")", json::object());
REQUIRE(std::holds_alternative<EvalOk>(r));
REQUIRE(std::get<EvalOk>(r).value.is_null());
}
TEST_CASE("deeply nested parentheses throw parse depth error", "[security]") {
std::string expr(2000, '(');
expr += "1";
expr += std::string(2000, ')');
auto r = eval_val(expr, json::object());
REQUIRE(std::holds_alternative<EvalErr>(r));
REQUIRE(std::get<EvalErr>(r).error.find("deeply nested") != std::string::npos);
}
TEST_CASE("deeply nested unary chains throw parse depth error", "[security]") {
std::string expr(2000, '!');
expr += "true";
auto r = eval_val(expr, json::object());
REQUIRE(std::holds_alternative<EvalErr>(r));
REQUIRE(std::get<EvalErr>(r).error.find("deeply nested") != std::string::npos);
}
TEST_CASE("evaluation depth exceeded throws error", "[security]") {
std::string expr(600, '(');
expr += "1";
expr += std::string(600, ')');
auto r = eval_val(expr, json::object());
REQUIRE(std::holds_alternative<EvalErr>(r));
auto& err = std::get<EvalErr>(r).error;
bool is_depth_error = err.find("deeply nested") != std::string::npos ||
err.find("depth exceeded") != std::string::npos;
REQUIRE(is_depth_error);
}
TEST_CASE("moderate string repeat still works", "[security]") {
auto r = eval_val(R"("ab" * 100)", json::object());
REQUIRE(std::holds_alternative<EvalOk>(r));
REQUIRE(std::get<EvalOk>(r).value.get<std::string>().size() == 200);
}
TEST_CASE("NaN/Infinity from arithmetic returns null (S4)", "[security]") {
SECTION("0 ^ -1 is Inf -> null") {
auto r = eval_val(R"(0 ^ -1)", json::object());
REQUIRE(std::holds_alternative<EvalOk>(r));
REQUIRE(std::get<EvalOk>(r).value.is_null());
}
SECTION("(-1) ^ 0.5 is NaN -> null") {
auto r = eval_val(R"((-1) ^ 0.5)", json::object());
REQUIRE(std::holds_alternative<EvalOk>(r));
REQUIRE(std::get<EvalOk>(r).value.is_null());
}
SECTION("1e308 + 1e308 is Inf -> null") {
json ctx = {{"big", 1e308}};
auto r = eval_val(R"(@big + @big)", ctx);
REQUIRE(std::holds_alternative<EvalOk>(r));
REQUIRE(std::get<EvalOk>(r).value.is_null());
}
SECTION("1e308 * 2 is Inf -> null") {
json ctx = {{"big", 1e308}};
auto r = eval_val(R"(@big * 2)", ctx);
REQUIRE(std::holds_alternative<EvalOk>(r));
REQUIRE(std::get<EvalOk>(r).value.is_null());
}
SECTION("subtraction overflow -> null") {
json ctx = {{"big", 1e308}, {"negbig", -1e308}};
auto r = eval_val(R"(@negbig - @big)", ctx);
REQUIRE(std::holds_alternative<EvalOk>(r));
REQUIRE(std::get<EvalOk>(r).value.is_null());
}
}
TEST_CASE("string concatenation exceeding MAX_STRING_SIZE returns null (S6)", "[security]") {
std::string big(600000, 'a');
json ctx = {{"s1", big}, {"s2", big}};
auto r = eval_val(R"(@s1 + @s2)", ctx);
REQUIRE(std::holds_alternative<EvalOk>(r));
REQUIRE(std::get<EvalOk>(r).value.is_null());
}
TEST_CASE("max_source_length rejects oversized input (S7)", "[security]") {
SECTION("eval_val rejects long source") {
auto r = eval_val("1 + 2", json::object(), 3);
REQUIRE(std::holds_alternative<EvalErr>(r));
REQUIRE(std::get<EvalErr>(r).error.find("exceeds maximum length") != std::string::npos);
}
SECTION("eval_val accepts source within limit") {
auto r = eval_val("1 + 2", json::object(), 100);
REQUIRE(std::holds_alternative<EvalOk>(r));
REQUIRE(std::get<EvalOk>(r).value == 3.0);
}
SECTION("eval_true rejects long source") {
auto r = eval_true("true", json::object(), 2);
REQUIRE_FALSE(r.ok);
REQUIRE(r.error.find("exceeds maximum length") != std::string::npos);
}
SECTION("test rejects long source") {
auto t = test("1 + 2", 3);
REQUIRE_FALSE(t.ok);
REQUIRE(t.error.find("exceeds maximum length") != std::string::npos);
}
SECTION("zero max_source_length means unlimited") {
auto r = eval_val("1 + 2", json::object(), 0);
REQUIRE(std::holds_alternative<EvalOk>(r));
REQUIRE(std::get<EvalOk>(r).value == 3.0);
}
}
TEST_CASE("out-of-range number literal gives descriptive error (S8)", "[security]") {
auto r = eval_val("1e99999", json::object());
REQUIRE(std::holds_alternative<EvalErr>(r));
REQUIRE(std::get<EvalErr>(r).error.find("out of range") != std::string::npos);
}