AryaWu/sqlite
0
1#include "sqliteInt.h"2#include "unity.h"3#include <stdlib.h>4#include <string.h>5 6/* Forward declarations (available via sqliteInt.h, but declare to be explicit) */7extern void sqlite3RegisterLikeFunctions(sqlite3 *db, int caseSensitive);8extern FuncDef *sqlite3FindFunction(sqlite3*, const char*, int, u8, int);9 10static sqlite3* open_memory_db(void){11 sqlite3 *db = NULL;12 int rc = sqlite3_open(":memory:", &db);13 if( rc!=SQLITE_OK ){14 /* Unity assertions are used by caller */15 return NULL;16 }17 return db;18}19 20void setUp(void) {21 /* No global setup */22}23 24void tearDown(void) {25 /* No global teardown */26}27 28static void assert_like_flags(FuncDef *pDef, int expectCase){29 TEST_ASSERT_NOT_NULL(pDef);30 TEST_ASSERT_TRUE_MESSAGE((pDef->funcFlags & SQLITE_FUNC_LIKE)!=0,31 "LIKE flag not set on FuncDef");32 if( expectCase ){33 TEST_ASSERT_TRUE_MESSAGE((pDef->funcFlags & SQLITE_FUNC_CASE)!=0,34 "CASE flag expected but not set");35 }else{36 TEST_ASSERT_TRUE_MESSAGE((pDef->funcFlags & SQLITE_FUNC_CASE)==0,37 "CASE flag not expected but set");38 }39 TEST_ASSERT_TRUE_MESSAGE((pDef->funcFlags & SQLITE_FUNC_UNSAFE)==0,40 "UNSAFE flag should be cleared by registration");41}42 43void test_sqlite3RegisterLikeFunctions_case_insensitive_flags(void){44 sqlite3 *db = open_memory_db();45 TEST_ASSERT_NOT_NULL_MESSAGE(db, "Failed to open in-memory database");46 47 sqlite3RegisterLikeFunctions(db, 0);48 49 FuncDef *p2 = sqlite3FindFunction(db, "like", 2, SQLITE_UTF8, 0);50 FuncDef *p3 = sqlite3FindFunction(db, "like", 3, SQLITE_UTF8, 0);51 52 assert_like_flags(p2, 0);53 assert_like_flags(p3, 0);54 55 sqlite3_close(db);56}57 58void test_sqlite3RegisterLikeFunctions_case_sensitive_flags(void){59 sqlite3 *db = open_memory_db();60 TEST_ASSERT_NOT_NULL_MESSAGE(db, "Failed to open in-memory database");61 62 sqlite3RegisterLikeFunctions(db, 1);63 64 FuncDef *p2 = sqlite3FindFunction(db, "like", 2, SQLITE_UTF8, 0);65 FuncDef *p3 = sqlite3FindFunction(db, "like", 3, SQLITE_UTF8, 0);66 67 assert_like_flags(p2, 1);68 assert_like_flags(p3, 1);69 70 sqlite3_close(db);71}72 73void test_sqlite3RegisterLikeFunctions_case_flag_not_cleared_by_re_registering_insensitive(void){74 sqlite3 *db = open_memory_db();75 TEST_ASSERT_NOT_NULL_MESSAGE(db, "Failed to open in-memory database");76 77 /* First register as case-sensitive to set the CASE flag */78 sqlite3RegisterLikeFunctions(db, 1);79 FuncDef *p2a = sqlite3FindFunction(db, "like", 2, SQLITE_UTF8, 0);80 FuncDef *p3a = sqlite3FindFunction(db, "like", 3, SQLITE_UTF8, 0);81 assert_like_flags(p2a, 1);82 assert_like_flags(p3a, 1);83 84 /* Then re-register as case-insensitive; implementation does not clear CASE */85 sqlite3RegisterLikeFunctions(db, 0);86 FuncDef *p2b = sqlite3FindFunction(db, "like", 2, SQLITE_UTF8, 0);87 FuncDef *p3b = sqlite3FindFunction(db, "like", 3, SQLITE_UTF8, 0);88 89 /* Expect CASE flag to remain set */90 assert_like_flags(p2b, 1);91 assert_like_flags(p3b, 1);92 93 sqlite3_close(db);94}95 96void test_sqlite3RegisterLikeFunctions_registers_for_two_and_three_args(void){97 sqlite3 *db = open_memory_db();98 TEST_ASSERT_NOT_NULL_MESSAGE(db, "Failed to open in-memory database");99 100 sqlite3RegisterLikeFunctions(db, 0);101 102 FuncDef *p2 = sqlite3FindFunction(db, "like", 2, SQLITE_UTF8, 0);103 FuncDef *p3 = sqlite3FindFunction(db, "like", 3, SQLITE_UTF8, 0);104 105 TEST_ASSERT_NOT_NULL_MESSAGE(p2, "LIKE function with 2 args not found");106 TEST_ASSERT_NOT_NULL_MESSAGE(p3, "LIKE function with 3 args not found");107 108 /* Both should have LIKE flag and no UNSAFE; case flag depends on parameter (0 here) */109 assert_like_flags(p2, 0);110 assert_like_flags(p3, 0);111 112 sqlite3_close(db);113}114 115void test_sqlite3RegisterLikeFunctions_does_not_register_for_one_arg(void){116 sqlite3 *db = open_memory_db();117 TEST_ASSERT_NOT_NULL_MESSAGE(db, "Failed to open in-memory database");118 119 sqlite3RegisterLikeFunctions(db, 0);120 121 FuncDef *p1 = sqlite3FindFunction(db, "like", 1, SQLITE_UTF8, 0);122 TEST_ASSERT_NULL_MESSAGE(p1, "LIKE function should not be registered for 1 argument");123 124 sqlite3_close(db);125}126 127int main(void) {128 UNITY_BEGIN();129 RUN_TEST(test_sqlite3RegisterLikeFunctions_case_insensitive_flags);130 RUN_TEST(test_sqlite3RegisterLikeFunctions_case_sensitive_flags);131 RUN_TEST(test_sqlite3RegisterLikeFunctions_case_flag_not_cleared_by_re_registering_insensitive);132 RUN_TEST(test_sqlite3RegisterLikeFunctions_registers_for_two_and_three_args);133 RUN_TEST(test_sqlite3RegisterLikeFunctions_does_not_register_for_one_arg);134 return UNITY_END();135}