1 /* 2 ** 2008 March 19 3 ** 4 ** The author disclaims copyright to this source code. In place of 5 ** a legal notice, here is a blessing: 6 ** 7 ** May you do good and not evil. 8 ** May you find forgiveness for yourself and forgive others. 9 ** May you share freely, never taking more than you give. 10 ** 11 ************************************************************************* 12 ** Code for testing all sorts of SQLite interfaces. This code 13 ** implements new SQL functions used by the test scripts. 14 ** 15 ** $Id: test_func.c,v 1.16 2009/07/22 07:27:57 danielk1977 Exp $ 16 */ 17 #include "sqlite3.h" 18 #include "tcl.h" 19 #include <stdlib.h> 20 #include <string.h> 21 #include <assert.h> 22 23 24 /* 25 ** Allocate nByte bytes of space using sqlite3_malloc(). If the 26 ** allocation fails, call sqlite3_result_error_nomem() to notify 27 ** the database handle that malloc() has failed. 28 */ 29 static void *testContextMalloc(sqlite3_context *context, int nByte){ 30 char *z = sqlite3_malloc(nByte); 31 if( !z && nByte>0 ){ 32 sqlite3_result_error_nomem(context); 33 } 34 return z; 35 } 36 37 /* 38 ** This function generates a string of random characters. Used for 39 ** generating test data. 40 */ 41 static void randStr(sqlite3_context *context, int argc, sqlite3_value **argv){ 42 static const unsigned char zSrc[] = 43 "abcdefghijklmnopqrstuvwxyz" 44 "ABCDEFGHIJKLMNOPQRSTUVWXYZ" 45 "0123456789" 46 ".-!,:*^+=_|?/<> "; 47 int iMin, iMax, n, r, i; 48 unsigned char zBuf[1000]; 49 50 /* It used to be possible to call randstr() with any number of arguments, 51 ** but now it is registered with SQLite as requiring exactly 2. 52 */ 53 assert(argc==2); 54 55 iMin = sqlite3_value_int(argv[0]); 56 if( iMin<0 ) iMin = 0; 57 if( iMin>=sizeof(zBuf) ) iMin = sizeof(zBuf)-1; 58 iMax = sqlite3_value_int(argv[1]); 59 if( iMax<iMin ) iMax = iMin; 60 if( iMax>=sizeof(zBuf) ) iMax = sizeof(zBuf)-1; 61 n = iMin; 62 if( iMax>iMin ){ 63 sqlite3_randomness(sizeof(r), &r); 64 r &= 0x7fffffff; 65 n += r%(iMax + 1 - iMin); 66 } 67 assert( n<sizeof(zBuf) ); 68 sqlite3_randomness(n, zBuf); 69 for(i=0; i<n; i++){ 70 zBuf[i] = zSrc[zBuf[i]%(sizeof(zSrc)-1)]; 71 } 72 zBuf[n] = 0; 73 sqlite3_result_text(context, (char*)zBuf, n, SQLITE_TRANSIENT); 74 } 75 76 /* 77 ** The following two SQL functions are used to test returning a text 78 ** result with a destructor. Function 'test_destructor' takes one argument 79 ** and returns the same argument interpreted as TEXT. A destructor is 80 ** passed with the sqlite3_result_text() call. 81 ** 82 ** SQL function 'test_destructor_count' returns the number of outstanding 83 ** allocations made by 'test_destructor'; 84 ** 85 ** WARNING: Not threadsafe. 86 */ 87 static int test_destructor_count_var = 0; 88 static void destructor(void *p){ 89 char *zVal = (char *)p; 90 assert(zVal); 91 zVal--; 92 sqlite3_free(zVal); 93 test_destructor_count_var--; 94 } 95 static void test_destructor( 96 sqlite3_context *pCtx, 97 int nArg, 98 sqlite3_value **argv 99 ){ 100 char *zVal; 101 int len; 102 103 test_destructor_count_var++; 104 assert( nArg==1 ); 105 if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return; 106 len = sqlite3_value_bytes(argv[0]); 107 zVal = testContextMalloc(pCtx, len+3); 108 if( !zVal ){ 109 return; 110 } 111 zVal[len+1] = 0; 112 zVal[len+2] = 0; 113 zVal++; 114 memcpy(zVal, sqlite3_value_text(argv[0]), len); 115 sqlite3_result_text(pCtx, zVal, -1, destructor); 116 } 117 #ifndef SQLITE_OMIT_UTF16 118 static void test_destructor16( 119 sqlite3_context *pCtx, 120 int nArg, 121 sqlite3_value **argv 122 ){ 123 char *zVal; 124 int len; 125 126 test_destructor_count_var++; 127 assert( nArg==1 ); 128 if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return; 129 len = sqlite3_value_bytes16(argv[0]); 130 zVal = testContextMalloc(pCtx, len+3); 131 if( !zVal ){ 132 return; 133 } 134 zVal[len+1] = 0; 135 zVal[len+2] = 0; 136 zVal++; 137 memcpy(zVal, sqlite3_value_text16(argv[0]), len); 138 sqlite3_result_text16(pCtx, zVal, -1, destructor); 139 } 140 #endif 141 static void test_destructor_count( 142 sqlite3_context *pCtx, 143 int nArg, 144 sqlite3_value **argv 145 ){ 146 sqlite3_result_int(pCtx, test_destructor_count_var); 147 } 148 149 /* 150 ** The following aggregate function, test_agg_errmsg16(), takes zero 151 ** arguments. It returns the text value returned by the sqlite3_errmsg16() 152 ** API function. 153 */ 154 void sqlite3BeginBenignMalloc(void); 155 void sqlite3EndBenignMalloc(void); 156 static void test_agg_errmsg16_step(sqlite3_context *a, int b,sqlite3_value **c){ 157 } 158 static void test_agg_errmsg16_final(sqlite3_context *ctx){ 159 #ifndef SQLITE_OMIT_UTF16 160 const void *z; 161 sqlite3 * db = sqlite3_context_db_handle(ctx); 162 sqlite3_aggregate_context(ctx, 2048); 163 sqlite3BeginBenignMalloc(); 164 z = sqlite3_errmsg16(db); 165 sqlite3EndBenignMalloc(); 166 sqlite3_result_text16(ctx, z, -1, SQLITE_TRANSIENT); 167 #endif 168 } 169 170 /* 171 ** Routines for testing the sqlite3_get_auxdata() and sqlite3_set_auxdata() 172 ** interface. 173 ** 174 ** The test_auxdata() SQL function attempts to register each of its arguments 175 ** as auxiliary data. If there are no prior registrations of aux data for 176 ** that argument (meaning the argument is not a constant or this is its first 177 ** call) then the result for that argument is 0. If there is a prior 178 ** registration, the result for that argument is 1. The overall result 179 ** is the individual argument results separated by spaces. 180 */ 181 static void free_test_auxdata(void *p) {sqlite3_free(p);} 182 static void test_auxdata( 183 sqlite3_context *pCtx, 184 int nArg, 185 sqlite3_value **argv 186 ){ 187 int i; 188 char *zRet = testContextMalloc(pCtx, nArg*2); 189 if( !zRet ) return; 190 memset(zRet, 0, nArg*2); 191 for(i=0; i<nArg; i++){ 192 char const *z = (char*)sqlite3_value_text(argv[i]); 193 if( z ){ 194 int n; 195 char *zAux = sqlite3_get_auxdata(pCtx, i); 196 if( zAux ){ 197 zRet[i*2] = '1'; 198 assert( strcmp(zAux,z)==0 ); 199 }else { 200 zRet[i*2] = '0'; 201 } 202 n = strlen(z) + 1; 203 zAux = testContextMalloc(pCtx, n); 204 if( zAux ){ 205 memcpy(zAux, z, n); 206 sqlite3_set_auxdata(pCtx, i, zAux, free_test_auxdata); 207 } 208 zRet[i*2+1] = ' '; 209 } 210 } 211 sqlite3_result_text(pCtx, zRet, 2*nArg-1, free_test_auxdata); 212 } 213 214 /* 215 ** A function to test error reporting from user functions. This function 216 ** returns a copy of its first argument as the error message. If the 217 ** second argument exists, it becomes the error code. 218 */ 219 static void test_error( 220 sqlite3_context *pCtx, 221 int nArg, 222 sqlite3_value **argv 223 ){ 224 sqlite3_result_error(pCtx, (char*)sqlite3_value_text(argv[0]), -1); 225 if( nArg==2 ){ 226 sqlite3_result_error_code(pCtx, sqlite3_value_int(argv[1])); 227 } 228 } 229 230 /* 231 ** Implementation of the counter(X) function. If X is an integer 232 ** constant, then the first invocation will return X. The second X+1. 233 ** and so forth. Can be used (for example) to provide a sequence number 234 ** in a result set. 235 */ 236 static void counterFunc( 237 sqlite3_context *pCtx, /* Function context */ 238 int nArg, /* Number of function arguments */ 239 sqlite3_value **argv /* Values for all function arguments */ 240 ){ 241 int *pCounter = (int*)sqlite3_get_auxdata(pCtx, 0); 242 if( pCounter==0 ){ 243 pCounter = sqlite3_malloc( sizeof(*pCounter) ); 244 if( pCounter==0 ){ 245 sqlite3_result_error_nomem(pCtx); 246 return; 247 } 248 *pCounter = sqlite3_value_int(argv[0]); 249 sqlite3_set_auxdata(pCtx, 0, pCounter, sqlite3_free); 250 }else{ 251 ++*pCounter; 252 } 253 sqlite3_result_int(pCtx, *pCounter); 254 } 255 256 257 /* 258 ** This function takes two arguments. It performance UTF-8/16 type 259 ** conversions on the first argument then returns a copy of the second 260 ** argument. 261 ** 262 ** This function is used in cases such as the following: 263 ** 264 ** SELECT test_isolation(x,x) FROM t1; 265 ** 266 ** We want to verify that the type conversions that occur on the 267 ** first argument do not invalidate the second argument. 268 */ 269 static void test_isolation( 270 sqlite3_context *pCtx, 271 int nArg, 272 sqlite3_value **argv 273 ){ 274 #ifndef SQLITE_OMIT_UTF16 275 sqlite3_value_text16(argv[0]); 276 sqlite3_value_text(argv[0]); 277 sqlite3_value_text16(argv[0]); 278 sqlite3_value_text(argv[0]); 279 #endif 280 sqlite3_result_value(pCtx, argv[1]); 281 } 282 283 /* 284 ** Invoke an SQL statement recursively. The function result is the 285 ** first column of the first row of the result set. 286 */ 287 static void test_eval( 288 sqlite3_context *pCtx, 289 int nArg, 290 sqlite3_value **argv 291 ){ 292 sqlite3_stmt *pStmt; 293 int rc; 294 sqlite3 *db = sqlite3_context_db_handle(pCtx); 295 const char *zSql; 296 297 zSql = (char*)sqlite3_value_text(argv[0]); 298 rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0); 299 if( rc==SQLITE_OK ){ 300 rc = sqlite3_step(pStmt); 301 if( rc==SQLITE_ROW ){ 302 sqlite3_result_value(pCtx, sqlite3_column_value(pStmt, 0)); 303 } 304 rc = sqlite3_finalize(pStmt); 305 } 306 if( rc ){ 307 char *zErr; 308 assert( pStmt==0 ); 309 zErr = sqlite3_mprintf("sqlite3_prepare_v2() error: %s",sqlite3_errmsg(db)); 310 sqlite3_result_text(pCtx, zErr, -1, sqlite3_free); 311 sqlite3_result_error_code(pCtx, rc); 312 } 313 } 314 315 316 /* 317 ** convert one character from hex to binary 318 */ 319 static int testHexChar(char c){ 320 if( c>='0' && c<='9' ){ 321 return c - '0'; 322 }else if( c>='a' && c<='f' ){ 323 return c - 'a' + 10; 324 }else if( c>='A' && c<='F' ){ 325 return c - 'A' + 10; 326 } 327 return 0; 328 } 329 330 /* 331 ** Convert hex to binary. 332 */ 333 static void testHexToBin(const char *zIn, char *zOut){ 334 while( zIn[0] && zIn[1] ){ 335 *(zOut++) = (testHexChar(zIn[0])<<4) + testHexChar(zIn[1]); 336 zIn += 2; 337 } 338 } 339 340 /* 341 ** hex_to_utf16be(HEX) 342 ** 343 ** Convert the input string from HEX into binary. Then return the 344 ** result using sqlite3_result_text16le(). 345 */ 346 static void testHexToUtf16be( 347 sqlite3_context *pCtx, 348 int nArg, 349 sqlite3_value **argv 350 ){ 351 int n; 352 const char *zIn; 353 char *zOut; 354 assert( nArg==1 ); 355 n = sqlite3_value_bytes(argv[0]); 356 zIn = (const char*)sqlite3_value_text(argv[0]); 357 zOut = sqlite3_malloc( n/2 ); 358 if( zOut==0 ){ 359 sqlite3_result_error_nomem(pCtx); 360 }else{ 361 testHexToBin(zIn, zOut); 362 sqlite3_result_text16be(pCtx, zOut, n/2, sqlite3_free); 363 } 364 } 365 366 /* 367 ** hex_to_utf8(HEX) 368 ** 369 ** Convert the input string from HEX into binary. Then return the 370 ** result using sqlite3_result_text16le(). 371 */ 372 static void testHexToUtf8( 373 sqlite3_context *pCtx, 374 int nArg, 375 sqlite3_value **argv 376 ){ 377 int n; 378 const char *zIn; 379 char *zOut; 380 assert( nArg==1 ); 381 n = sqlite3_value_bytes(argv[0]); 382 zIn = (const char*)sqlite3_value_text(argv[0]); 383 zOut = sqlite3_malloc( n/2 ); 384 if( zOut==0 ){ 385 sqlite3_result_error_nomem(pCtx); 386 }else{ 387 testHexToBin(zIn, zOut); 388 sqlite3_result_text(pCtx, zOut, n/2, sqlite3_free); 389 } 390 } 391 392 /* 393 ** hex_to_utf16le(HEX) 394 ** 395 ** Convert the input string from HEX into binary. Then return the 396 ** result using sqlite3_result_text16le(). 397 */ 398 static void testHexToUtf16le( 399 sqlite3_context *pCtx, 400 int nArg, 401 sqlite3_value **argv 402 ){ 403 int n; 404 const char *zIn; 405 char *zOut; 406 assert( nArg==1 ); 407 n = sqlite3_value_bytes(argv[0]); 408 zIn = (const char*)sqlite3_value_text(argv[0]); 409 zOut = sqlite3_malloc( n/2 ); 410 if( zOut==0 ){ 411 sqlite3_result_error_nomem(pCtx); 412 }else{ 413 testHexToBin(zIn, zOut); 414 sqlite3_result_text16le(pCtx, zOut, n/2, sqlite3_free); 415 } 416 } 417 418 static int registerTestFunctions(sqlite3 *db){ 419 static const struct { 420 char *zName; 421 signed char nArg; 422 unsigned char eTextRep; /* 1: UTF-16. 0: UTF-8 */ 423 void (*xFunc)(sqlite3_context*,int,sqlite3_value **); 424 } aFuncs[] = { 425 { "randstr", 2, SQLITE_UTF8, randStr }, 426 { "test_destructor", 1, SQLITE_UTF8, test_destructor}, 427 #ifndef SQLITE_OMIT_UTF16 428 { "test_destructor16", 1, SQLITE_UTF8, test_destructor16}, 429 { "hex_to_utf16be", 1, SQLITE_UTF8, testHexToUtf16be}, 430 { "hex_to_utf16le", 1, SQLITE_UTF8, testHexToUtf16le}, 431 #endif 432 { "hex_to_utf8", 1, SQLITE_UTF8, testHexToUtf8}, 433 { "test_destructor_count", 0, SQLITE_UTF8, test_destructor_count}, 434 { "test_auxdata", -1, SQLITE_UTF8, test_auxdata}, 435 { "test_error", 1, SQLITE_UTF8, test_error}, 436 { "test_error", 2, SQLITE_UTF8, test_error}, 437 { "test_eval", 1, SQLITE_UTF8, test_eval}, 438 { "test_isolation", 2, SQLITE_UTF8, test_isolation}, 439 { "test_counter", 1, SQLITE_UTF8, counterFunc}, 440 }; 441 int i; 442 443 for(i=0; i<sizeof(aFuncs)/sizeof(aFuncs[0]); i++){ 444 sqlite3_create_function(db, aFuncs[i].zName, aFuncs[i].nArg, 445 aFuncs[i].eTextRep, 0, aFuncs[i].xFunc, 0, 0); 446 } 447 448 sqlite3_create_function(db, "test_agg_errmsg16", 0, SQLITE_ANY, 0, 0, 449 test_agg_errmsg16_step, test_agg_errmsg16_final); 450 451 return SQLITE_OK; 452 } 453 454 /* 455 ** TCLCMD: autoinstall_test_functions 456 ** 457 ** Invoke this TCL command to use sqlite3_auto_extension() to cause 458 ** the standard set of test functions to be loaded into each new 459 ** database connection. 460 */ 461 static int autoinstall_test_funcs( 462 void * clientData, 463 Tcl_Interp *interp, 464 int objc, 465 Tcl_Obj *CONST objv[] 466 ){ 467 extern int Md5_Register(sqlite3*); 468 int rc = sqlite3_auto_extension((void*)registerTestFunctions); 469 if( rc==SQLITE_OK ){ 470 rc = sqlite3_auto_extension((void*)Md5_Register); 471 } 472 Tcl_SetObjResult(interp, Tcl_NewIntObj(rc)); 473 return TCL_OK; 474 } 475 476 /* 477 ** A bogus step function and finalizer function. 478 */ 479 static void tStep(sqlite3_context *a, int b, sqlite3_value **c){} 480 static void tFinal(sqlite3_context *a){} 481 482 483 /* 484 ** tclcmd: abuse_create_function 485 ** 486 ** Make various calls to sqlite3_create_function that do not have valid 487 ** parameters. Verify that the error condition is detected and reported. 488 */ 489 static int abuse_create_function( 490 void * clientData, 491 Tcl_Interp *interp, 492 int objc, 493 Tcl_Obj *CONST objv[] 494 ){ 495 extern int getDbPointer(Tcl_Interp*, const char*, sqlite3**); 496 sqlite3 *db; 497 int rc; 498 int mxArg; 499 500 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; 501 502 rc = sqlite3_create_function(db, "tx", 1, SQLITE_UTF8, 0, tStep,tStep,tFinal); 503 if( rc!=SQLITE_MISUSE ) goto abuse_err; 504 505 rc = sqlite3_create_function(db, "tx", 1, SQLITE_UTF8, 0, tStep, tStep, 0); 506 if( rc!=SQLITE_MISUSE ) goto abuse_err; 507 508 rc = sqlite3_create_function(db, "tx", 1, SQLITE_UTF8, 0, tStep, 0, tFinal); 509 if( rc!=SQLITE_MISUSE) goto abuse_err; 510 511 rc = sqlite3_create_function(db, "tx", 1, SQLITE_UTF8, 0, 0, 0, tFinal); 512 if( rc!=SQLITE_MISUSE ) goto abuse_err; 513 514 rc = sqlite3_create_function(db, "tx", 1, SQLITE_UTF8, 0, 0, tStep, 0); 515 if( rc!=SQLITE_MISUSE ) goto abuse_err; 516 517 rc = sqlite3_create_function(db, "tx", -2, SQLITE_UTF8, 0, tStep, 0, 0); 518 if( rc!=SQLITE_MISUSE ) goto abuse_err; 519 520 rc = sqlite3_create_function(db, "tx", 128, SQLITE_UTF8, 0, tStep, 0, 0); 521 if( rc!=SQLITE_MISUSE ) goto abuse_err; 522 523 rc = sqlite3_create_function(db, "funcxx" 524 "_123456789_123456789_123456789_123456789_123456789" 525 "_123456789_123456789_123456789_123456789_123456789" 526 "_123456789_123456789_123456789_123456789_123456789" 527 "_123456789_123456789_123456789_123456789_123456789" 528 "_123456789_123456789_123456789_123456789_123456789", 529 1, SQLITE_UTF8, 0, tStep, 0, 0); 530 if( rc!=SQLITE_MISUSE ) goto abuse_err; 531 532 /* This last function registration should actually work. Generate 533 ** a no-op function (that always returns NULL) and which has the 534 ** maximum-length function name and the maximum number of parameters. 535 */ 536 sqlite3_limit(db, SQLITE_LIMIT_FUNCTION_ARG, 10000); 537 mxArg = sqlite3_limit(db, SQLITE_LIMIT_FUNCTION_ARG, -1); 538 rc = sqlite3_create_function(db, "nullx" 539 "_123456789_123456789_123456789_123456789_123456789" 540 "_123456789_123456789_123456789_123456789_123456789" 541 "_123456789_123456789_123456789_123456789_123456789" 542 "_123456789_123456789_123456789_123456789_123456789" 543 "_123456789_123456789_123456789_123456789_123456789", 544 mxArg, SQLITE_UTF8, 0, tStep, 0, 0); 545 if( rc!=SQLITE_OK ) goto abuse_err; 546 547 return TCL_OK; 548 549 abuse_err: 550 Tcl_AppendResult(interp, "sqlite3_create_function abused test failed", 551 (char*)0); 552 return TCL_ERROR; 553 } 554 555 /* 556 ** Register commands with the TCL interpreter. 557 */ 558 int Sqlitetest_func_Init(Tcl_Interp *interp){ 559 static struct { 560 char *zName; 561 Tcl_ObjCmdProc *xProc; 562 } aObjCmd[] = { 563 { "autoinstall_test_functions", autoinstall_test_funcs }, 564 { "abuse_create_function", abuse_create_function }, 565 }; 566 int i; 567 extern int Md5_Register(sqlite3*); 568 569 for(i=0; i<sizeof(aObjCmd)/sizeof(aObjCmd[0]); i++){ 570 Tcl_CreateObjCommand(interp, aObjCmd[i].zName, aObjCmd[i].xProc, 0, 0); 571 } 572 sqlite3_initialize(); 573 sqlite3_auto_extension((void*)registerTestFunctions); 574 sqlite3_auto_extension((void*)Md5_Register); 575 return TCL_OK; 576 } 577