// Unit tests for SqlBind (the MySQL-free part of /) - // see docs/design-sql-bound-parameters.md section 10. // Built and run by tests/runbindtests.sh. #include "SqlBind.h" #include "Exception.h" #include #include #include using namespace jet; static int failures = 0, passes = 0; #define CHECK(cond) do { if(cond) ++passes; else { ++failures; std::cout << "FAIL line " << __LINE__ << ": " #cond << std::endl; } } while(0) // A stand-in escaper (the real one is mysql_real_escape_string_quote). static std::string esc(const std::string &s) { std::string o; for(char c : s) { if(c == '\'' || c == '\\') o += '\\'; o += c; } return o; } static BoundValue bv(const std::string &value, const std::string &type = "", const std::string &nullMode = "", bool hasAllow = false, const std::string &allow = "") { return makeBoundValue(value, type, nullMode, hasAllow, allow, 1); } static bool throws(const std::string &value, const std::string &type, const std::string &nullMode = "", bool hasAllow = false, const std::string &allow = "", std::string *msg = nullptr) { try { bv(value, type, nullMode, hasAllow, allow); } catch(coreutils::Exception &e) { if(msg) *msg = e.text; return true; } return false; } static std::string bind(const std::string &q, std::vector v, bool nbe = false) { return bindPlaceholders(q, v, nbe, esc); } int main() { // --- types: valid values --- CHECK(bv("abc").kind == BoundValue::QUOTED && bv("abc").text == "abc"); CHECK(bv("abc", "string").kind == BoundValue::QUOTED); CHECK(bv("", "string").kind == BoundValue::QUOTED && bv("").text == ""); CHECK(bv("42", "int").kind == BoundValue::BARE && bv("42", "int").text == "42"); CHECK(bv("-7", "INT").text == "-7"); CHECK(bv("1234567890123456789", "int").text == "1234567890123456789"); CHECK(bv("3.14", "decimal").text == "3.14"); CHECK(bv("-10", "decimal").text == "-10"); for(auto t : {"1", "true", "TRUE", "yes", "Yes"}) CHECK(bv(t, "bool").text == "1"); for(auto f : {"0", "false", "no", "NO"}) CHECK(bv(f, "bool").text == "0"); CHECK(bv("2026-10-23", "date").kind == BoundValue::QUOTED); CHECK(bv("2026-10-23 14:05", "datetime").kind == BoundValue::QUOTED); CHECK(bv("2026-10-23 23:59:59", "datetime").text == "2026-10-23 23:59:59"); CHECK(bv("price", "ident", "", true, "name, price ,created").text == "`price`"); CHECK(bv("PRICE", "ident", "", true, "name,price").text == "`price`"); // allow list spelling wins CHECK(bv("desc", "ident", "", true, "ASC,DESC").text == "DESC"); CHECK(bv("asc", "ident", "", true, "ASC,DESC").kind == BoundValue::BARE); CHECK(bv("we`ird", "ident", "", true, "we`ird").text == "`we``ird`"); // --- null="empty" --- CHECK(bv("", "string", "empty").kind == BoundValue::SQLNULL); CHECK(bv("", "int", "empty").kind == BoundValue::SQLNULL); CHECK(bv("5", "int", "empty").text == "5"); CHECK(bv("x", "", "EMPTY").kind == BoundValue::QUOTED); // --- types: invalid values throw --- for(auto v : {"", "1 or 1=1", "1.5", "abc", "12345678901234567890", "-", " 1", "0x10", "1e5"}) CHECK(throws(v, "int")); for(auto v : {"", "1.", ".5", "1.2.3", "1,5", "1;drop"}) CHECK(throws(v, "decimal")); for(auto v : {"", "2", "on", "y"}) CHECK(throws(v, "bool")); for(auto v : {"", "2026-13-01", "2026-00-10", "2026-01-32", "26-01-01", "2026/01/01", "2026-01-01'"}) CHECK(throws(v, "date")); for(auto v : {"2026-01-01", "2026-01-01 24:00", "2026-01-01 12:60", "2026-01-01T12:00", "2026-01-01 12:00:61"}) CHECK(throws(v, "datetime")); CHECK(throws("name; drop table x", "ident", "", true, "name,price")); CHECK(throws("price", "ident")); // ident needs allow CHECK(throws("x", "string", "", true, "x")); // allow only with ident CHECK(throws("x", "blob")); // unknown type CHECK(throws("x", "string", "always")); // bad null mode CHECK(throws("", "int")); // empty is not an int without null="empty" // error text never includes the value (it may be a password) { std::string msg; CHECK(throws("s3cret-pw", "int", "", false, "", &msg)); CHECK(msg.find("s3cret-pw") == std::string::npos); CHECK(msg.find("bind 1") != std::string::npos); CHECK(throws("s3cret-pw", "ident", "", true, "a,b", &msg)); CHECK(msg.find("s3cret-pw") == std::string::npos); } // --- placeholder scanning: replaced in order --- CHECK(bind("select ? , ?", {bv("a"), bv("1", "int")}) == "select 'a' , 1"); CHECK(bind("x=?", {bv("", "", "empty")}) == "x=NULL"); CHECK(bind("where email=? and status='ACTIVE'", {bv("x' or '1'='1")}) == "where email='x\\' or \\'1\\'=\\'1' and status='ACTIVE'"); // replacement text is never rescanned CHECK(bind("a=? and b=?", {bv("?"), bv("??")}) == "a='?' and b='??'"); // --- ? inside literals, identifiers and comments is never replaced --- CHECK(countPlaceholders("select '?', \"?\", `?` from t", false) == 0); CHECK(countPlaceholders("select 1 -- ?\n, ?", false) == 1); CHECK(countPlaceholders("select 1 # ?\n, ?", false) == 1); CHECK(countPlaceholders("select /* ? */ ?", false) == 1); CHECK(countPlaceholders("select /* ? \n ? */ 1", false) == 0); CHECK(countPlaceholders("select 1--?", false) == 1); // "--" not followed by space is not a comment CHECK(countPlaceholders("select 'it''s ?' , ?", false) == 1); CHECK(countPlaceholders("select \"say \"\"?\"\"\" , ?", false) == 1); CHECK(countPlaceholders("select `a``?` , ?", false) == 1); CHECK(countPlaceholders("select 'a\\'?' , ?", false) == 1); // \' escapes the quote // with NO_BACKSLASH_ESCAPES, 'a\' is a complete literal, so both ? count CHECK(countPlaceholders("select 'a\\', ? , ?", true) == 2); CHECK(countPlaceholders("select 'a\\', ? , ?", false) == 0); CHECK(countPlaceholders("select ?", false) == 1); CHECK(countPlaceholders("", false) == 0); // --- count mismatch throws --- { bool threw = false; try { bind("select ?, ?, ?", {bv("a"), bv("b")}); } catch(coreutils::Exception &e) { threw = true; CHECK(e.text == "sql: 3 placeholders but 2 bind values"); } CHECK(threw); threw = false; try { bind("select '?'", {bv("a")}); } catch(coreutils::Exception &) { threw = true; } CHECK(threw); } // --- charset changes are refused --- CHECK(isCharsetChange("SET NAMES latin1")); CHECK(isCharsetChange(" set names 'gbk'")); CHECK(isCharsetChange("/* x */ SET CHARACTER SET gbk")); CHECK(isCharsetChange("set charset gbk")); CHECK(isCharsetChange("set @a=1, names gbk")); CHECK(isCharsetChange("set character_set_client = gbk")); CHECK(isCharsetChange("SET @@session.character_set_connection=gbk")); CHECK(isCharsetChange("set collation_connection = 'gbk_bin'")); CHECK(!isCharsetChange("select names from t")); CHECK(!isCharsetChange("update t set names='x'")); CHECK(!isCharsetChange("set @names = 'names'")); CHECK(!isCharsetChange("set session sql_mode = 'NO_BACKSLASH_ESCAPES'")); CHECK(!isCharsetChange("set @x = 'set names gbk'")); std::cout << passes << " passed, " << failures << " failed" << std::endl; return failures == 0 ? 0 : 1; }