Team Ai
Modelpublic

AryaWu/sqlite

sourceHugging Faceupdated 10mo agoView on Hugging Face
0likes
tests_func_unknownFunc.c131 linesDownload Raw Back to tests
1#include "sqliteInt.h"2#include "unity.h"3#include <string.h>4#include <stdint.h>5#include <stdlib.h>6 7/* Wrapper provided in the module for the static function */8extern void test_unknownFunc(sqlite3_context *context, int argc, sqlite3_value **argv);9 10void setUp(void) {11  /* No setup required */12}13void tearDown(void) {14  /* No teardown required */15}16 17/* Helper to fill a buffer with a byte pattern */18static void fill_pattern(void *p, size_t n, unsigned char b){19  memset(p, b, n);20}21 22/* Test: Calling with all NULL inputs and zero argc should not crash */23void test_unknownFunc_handles_null_inputs(void) {24  /* If this returns without crashing, it's a pass */25  test_unknownFunc(NULL, 0, NULL);26  TEST_ASSERT_TRUE(1); /* Ensure an assertion occurs */27}28 29/* Test: Context memory should remain unchanged */30void test_unknownFunc_preserves_context_memory(void) {31  sqlite3_context ctx;32  sqlite3_context before;33 34  /* Fill context with a known pattern to detect any writes */35  fill_pattern(&ctx, sizeof(ctx), 0xA5);36  memcpy(&before, &ctx, sizeof(ctx));37 38  /* Provide a dummy argv array to ensure the function still ignores it */39  sqlite3_value *dummyArgv[3] = { (sqlite3_value*)0x1, (sqlite3_value*)0x2, NULL };40  test_unknownFunc(&ctx, 3, dummyArgv);41 42  /* Verify the context memory is unchanged */43  TEST_ASSERT_EQUAL_UINT8_ARRAY(&before, &ctx, sizeof(ctx));44}45 46/* Test: argv array and the memory it points to should remain unchanged */47void test_unknownFunc_preserves_argv_and_pointed_memory(void) {48  /* Allocate two blocks to act as sqlite3_value objects (not dereferenced by the function) */49  const size_t n1 = 32, n2 = 64;50  sqlite3_value *v1 = (sqlite3_value*)sqlite3_malloc(n1);51  sqlite3_value *v2 = (sqlite3_value*)sqlite3_malloc(n2);52  TEST_ASSERT_NOT_NULL(v1);53  TEST_ASSERT_NOT_NULL(v2);54 55  /* Initialize with distinct patterns */56  fill_pattern(v1, n1, 0xAB);57  fill_pattern(v2, n2, 0xCD);58 59  /* Keep copies for comparison */60  unsigned char v1_before[32];61  unsigned char v2_before[64];62  memcpy(v1_before, v1, n1);63  memcpy(v2_before, v2, n2);64 65  sqlite3_value *argv_arr[3];66  argv_arr[0] = v1;67  argv_arr[1] = v2;68  argv_arr[2] = NULL;69 70  sqlite3_value *argv_before[3];71  memcpy(argv_before, argv_arr, sizeof(argv_arr));72 73  /* Call the function under test */74  test_unknownFunc((sqlite3_context*)0xDEADBEEF, 2, argv_arr);75 76  /* Verify argv array pointers are unchanged */77  TEST_ASSERT_EQUAL_PTR_ARRAY(argv_before, argv_arr, 3);78 79  /* Verify pointed-to memory is unchanged */80  TEST_ASSERT_EQUAL_UINT8_ARRAY(v1_before, v1, n1);81  TEST_ASSERT_EQUAL_UINT8_ARRAY(v2_before, v2, n2);82 83  sqlite3_free(v1);84  sqlite3_free(v2);85}86 87/* Test: Large argc with a large argv array should not crash and should not modify argv */88void test_unknownFunc_handles_large_argc(void) {89  enum { N = 1024 };90  sqlite3_value *argv_arr[N];91  sqlite3_value *argv_before[N];92 93  /* Fill argv with distinctive dummy pointers */94  for (int i = 0; i < N; i++) {95    argv_arr[i] = (sqlite3_value*)((uintptr_t)0x1000 + (uintptr_t)i * 16);96  }97  memcpy(argv_before, argv_arr, sizeof(argv_arr));98 99  test_unknownFunc(NULL, N, argv_arr);100 101  /* Verify argv array is unchanged */102  TEST_ASSERT_EQUAL_PTR_ARRAY(argv_before, argv_arr, N);103}104 105/* Test: Negative argc should be tolerated as the function ignores it */106void test_unknownFunc_handles_negative_argc(void) {107  sqlite3_context ctx;108  sqlite3_context before;109 110  fill_pattern(&ctx, sizeof(ctx), 0x5A);111  memcpy(&before, &ctx, sizeof(ctx));112 113  sqlite3_value *dummy = (sqlite3_value*)0xCAFEBABE;114  sqlite3_value *argv_arr[1] = { dummy };115 116  test_unknownFunc(&ctx, -123, argv_arr);117 118  /* Context unchanged; argv untouched */119  TEST_ASSERT_EQUAL_UINT8_ARRAY(&before, &ctx, sizeof(ctx));120  TEST_ASSERT_EQUAL_PTR(dummy, argv_arr[0]);121}122 123int main(void) {124  UNITY_BEGIN();125  RUN_TEST(test_unknownFunc_handles_null_inputs);126  RUN_TEST(test_unknownFunc_preserves_context_memory);127  RUN_TEST(test_unknownFunc_preserves_argv_and_pointed_memory);128  RUN_TEST(test_unknownFunc_handles_large_argc);129  RUN_TEST(test_unknownFunc_handles_negative_argc);130  return UNITY_END();131}