Team Ai
Modelpublic

AryaWu/sqlite

sourceHugging Faceupdated 10mo agoView on Hugging Face
0likes
tests_func_loadExt.c158 linesDownload Raw Back to tests
1#include "sqliteInt.h"2#include "unity.h"3 4#include <string.h>5#include <stdio.h>6 7/* Wrapper provided by the module for the static function */8extern void test_loadExt(sqlite3_context *context, int argc, sqlite3_value **argv);9 10/* Proxy SQL function: directly calls the target wrapper */11static void proxy_loadExt(sqlite3_context *ctx, int argc, sqlite3_value **argv){12  test_loadExt(ctx, argc, argv);13}14 15static void register_proxy(sqlite3 *db){16  int rc = sqlite3_create_function(db, "loadext_proxy", -1, SQLITE_UTF8, 0,17                                   proxy_loadExt, 0, 0);18  TEST_ASSERT_EQUAL_INT(SQLITE_OK, rc);19}20 21static sqlite3* open_memdb(void){22  sqlite3 *db = 0;23  int rc = sqlite3_open(":memory:", &db);24  TEST_ASSERT_EQUAL_INT(SQLITE_OK, rc);25  return db;26}27 28static void finalize_and_close(sqlite3_stmt *pStmt, sqlite3 *db){29  if( pStmt ) sqlite3_finalize(pStmt);30  if( db ) sqlite3_close(db);31}32 33void setUp(void) {34  /* Setup code here, or leave empty */35}36void tearDown(void) {37  /* Cleanup code here, or leave empty */38}39 40/* Unauthorized: single argument -> "not authorized" error */41void test_static_void_loadExt_sqlite3_context__int__sqlite3_value_ptr_Unauthorized_single_arg(void){42  sqlite3 *db = open_memdb();43  register_proxy(db);44  /* Ensure disabled */45  (void)sqlite3_enable_load_extension(db, 0);46 47  sqlite3_stmt *pStmt = 0;48  int rc = sqlite3_prepare_v2(db, "SELECT loadext_proxy(?1)", -1, &pStmt, 0);49  TEST_ASSERT_EQUAL_INT(SQLITE_OK, rc);50  rc = sqlite3_bind_text(pStmt, 1, "anything", -1, SQLITE_TRANSIENT);51  TEST_ASSERT_EQUAL_INT(SQLITE_OK, rc);52 53  rc = sqlite3_step(pStmt);54  TEST_ASSERT_EQUAL_INT(SQLITE_ERROR, rc);55 56  const char *zErr = sqlite3_errmsg(db);57  TEST_ASSERT_NOT_NULL(zErr);58  /* loadExt sets exactly "not authorized" */59  TEST_ASSERT_NOT_NULL(strstr(zErr, "not authorized"));60 61  finalize_and_close(pStmt, db);62}63 64/* Unauthorized: two arguments -> "not authorized" error */65void test_static_void_loadExt_sqlite3_context__int__sqlite3_value_ptr_Unauthorized_two_args(void){66  sqlite3 *db = open_memdb();67  register_proxy(db);68  (void)sqlite3_enable_load_extension(db, 0);69 70  sqlite3_stmt *pStmt = 0;71  int rc = sqlite3_prepare_v2(db, "SELECT loadext_proxy(?1, ?2)", -1, &pStmt, 0);72  TEST_ASSERT_EQUAL_INT(SQLITE_OK, rc);73  rc = sqlite3_bind_text(pStmt, 1, "file_does_not_matter", -1, SQLITE_TRANSIENT);74  TEST_ASSERT_EQUAL_INT(SQLITE_OK, rc);75  rc = sqlite3_bind_text(pStmt, 2, "entrypoint", -1, SQLITE_TRANSIENT);76  TEST_ASSERT_EQUAL_INT(SQLITE_OK, rc);77 78  rc = sqlite3_step(pStmt);79  TEST_ASSERT_EQUAL_INT(SQLITE_ERROR, rc);80 81  const char *zErr = sqlite3_errmsg(db);82  TEST_ASSERT_NOT_NULL(zErr);83  TEST_ASSERT_NOT_NULL(strstr(zErr, "not authorized"));84 85  finalize_and_close(pStmt, db);86}87 88/* Authorized: non-existent file (single arg) -> error from sqlite3_load_extension propagated */89void test_static_void_loadExt_sqlite3_context__int__sqlite3_value_ptr_Authorized_nonexistent_file_single_arg(void){90  sqlite3 *db = open_memdb();91  register_proxy(db);92  /* Enable load_extension SQL function */93  (void)sqlite3_enable_load_extension(db, 1);94 95  sqlite3_stmt *pStmt = 0;96  int rc = sqlite3_prepare_v2(db, "SELECT loadext_proxy(?1)", -1, &pStmt, 0);97  TEST_ASSERT_EQUAL_INT(SQLITE_OK, rc);98  rc = sqlite3_bind_text(pStmt, 1, "this_file_should_not_exist_123456789.so", -1, SQLITE_TRANSIENT);99  TEST_ASSERT_EQUAL_INT(SQLITE_OK, rc);100 101  rc = sqlite3_step(pStmt);102  /* Expect error; message text may vary by platform */103  TEST_ASSERT_EQUAL_INT(SQLITE_ERROR, rc);104 105  finalize_and_close(pStmt, db);106}107 108/* Authorized: non-existent file with entrypoint (two args) -> error propagated */109void test_static_void_loadExt_sqlite3_context__int__sqlite3_value_ptr_Authorized_nonexistent_file_two_args(void){110  sqlite3 *db = open_memdb();111  register_proxy(db);112  (void)sqlite3_enable_load_extension(db, 1);113 114  sqlite3_stmt *pStmt = 0;115  int rc = sqlite3_prepare_v2(db, "SELECT loadext_proxy(?1, ?2)", -1, &pStmt, 0);116  TEST_ASSERT_EQUAL_INT(SQLITE_OK, rc);117  rc = sqlite3_bind_text(pStmt, 1, "missing_extension_abcdef.dylib", -1, SQLITE_TRANSIENT);118  TEST_ASSERT_EQUAL_INT(SQLITE_OK, rc);119  rc = sqlite3_bind_text(pStmt, 2, "sqlite3_extension_init", -1, SQLITE_TRANSIENT);120  TEST_ASSERT_EQUAL_INT(SQLITE_OK, rc);121 122  rc = sqlite3_step(pStmt);123  TEST_ASSERT_EQUAL_INT(SQLITE_ERROR, rc);124 125  finalize_and_close(pStmt, db);126}127 128/* Authorized: NULL file argument -> no load attempted, returns NULL result successfully */129void test_static_void_loadExt_sqlite3_context__int__sqlite3_value_ptr_Authorized_null_file_returns_null(void){130  sqlite3 *db = open_memdb();131  register_proxy(db);132  (void)sqlite3_enable_load_extension(db, 1);133 134  sqlite3_stmt *pStmt = 0;135  int rc = sqlite3_prepare_v2(db, "SELECT loadext_proxy(?1)", -1, &pStmt, 0);136  TEST_ASSERT_EQUAL_INT(SQLITE_OK, rc);137  rc = sqlite3_bind_null(pStmt, 1);138  TEST_ASSERT_EQUAL_INT(SQLITE_OK, rc);139 140  rc = sqlite3_step(pStmt);141  TEST_ASSERT_EQUAL_INT(SQLITE_ROW, rc);142  TEST_ASSERT_EQUAL_INT(SQLITE_NULL, sqlite3_column_type(pStmt, 0));143 144  rc = sqlite3_step(pStmt);145  TEST_ASSERT_EQUAL_INT(SQLITE_DONE, rc);146 147  finalize_and_close(pStmt, db);148}149 150int main(void) {151  UNITY_BEGIN();152  RUN_TEST(test_static_void_loadExt_sqlite3_context__int__sqlite3_value_ptr_Unauthorized_single_arg);153  RUN_TEST(test_static_void_loadExt_sqlite3_context__int__sqlite3_value_ptr_Unauthorized_two_args);154  RUN_TEST(test_static_void_loadExt_sqlite3_context__int__sqlite3_value_ptr_Authorized_nonexistent_file_single_arg);155  RUN_TEST(test_static_void_loadExt_sqlite3_context__int__sqlite3_value_ptr_Authorized_nonexistent_file_two_args);156  RUN_TEST(test_static_void_loadExt_sqlite3_context__int__sqlite3_value_ptr_Authorized_null_file_returns_null);157  return UNITY_END();158}