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 #include "sqlite3.h" 16 #include "tcl.h" 17 #include <stdlib.h> 18 #include <string.h> 19 #include <assert.h> 20 21 #include "sqliteInt.h" 22 #include "vdbeInt.h" 23 24 25 /* 26 ** Allocate nByte bytes of space using sqlite3_malloc(). If the 27 ** allocation fails, call sqlite3_result_error_nomem() to notify 28 ** the database handle that malloc() has failed. 29 */ 30 static void *testContextMalloc(sqlite3_context *context, int nByte){ 31 char *z = sqlite3_malloc(nByte); 32 if( !z && nByte>0 ){ 33 sqlite3_result_error_nomem(context); 34 } 35 return z; 36 } 37 38 /* 39 ** This function generates a string of random characters. Used for 40 ** generating test data. 41 */ 42 static void randStr(sqlite3_context *context, int argc, sqlite3_value **argv){ 43 static const unsigned char zSrc[] = 44 "abcdefghijklmnopqrstuvwxyz" 45 "ABCDEFGHIJKLMNOPQRSTUVWXYZ" 46 "0123456789" 47 ".-!,:*^+=_|?/<> "; 48 int iMin, iMax, n, r, i; 49 unsigned char zBuf[1000]; 50 51 /* It used to be possible to call randstr() with any number of arguments, 52 ** but now it is registered with SQLite as requiring exactly 2. 53 */ 54 assert(argc==2); 55 56 iMin = sqlite3_value_int(argv[0]); 57 if( iMin<0 ) iMin = 0; 58 if( iMin>=sizeof(zBuf) ) iMin = sizeof(zBuf)-1; 59 iMax = sqlite3_value_int(argv[1]); 60 if( iMax<iMin ) iMax = iMin; 61 if( iMax>=sizeof(zBuf) ) iMax = sizeof(zBuf)-1; 62 n = iMin; 63 if( iMax>iMin ){ 64 sqlite3_randomness(sizeof(r), &r); 65 r &= 0x7fffffff; 66 n += r%(iMax + 1 - iMin); 67 } 68 assert( n<sizeof(zBuf) ); 69 sqlite3_randomness(n, zBuf); 70 for(i=0; i<n; i++){ 71 zBuf[i] = zSrc[zBuf[i]%(sizeof(zSrc)-1)]; 72 } 73 zBuf[n] = 0; 74 sqlite3_result_text(context, (char*)zBuf, n, SQLITE_TRANSIENT); 75 } 76 77 /* 78 ** The following two SQL functions are used to test returning a text 79 ** result with a destructor. Function 'test_destructor' takes one argument 80 ** and returns the same argument interpreted as TEXT. A destructor is 81 ** passed with the sqlite3_result_text() call. 82 ** 83 ** SQL function 'test_destructor_count' returns the number of outstanding 84 ** allocations made by 'test_destructor'; 85 ** 86 ** WARNING: Not threadsafe. 87 */ 88 static int test_destructor_count_var = 0; 89 static void destructor(void *p){ 90 char *zVal = (char *)p; 91 assert(zVal); 92 zVal--; 93 sqlite3_free(zVal); 94 test_destructor_count_var--; 95 } 96 static void test_destructor( 97 sqlite3_context *pCtx, 98 int nArg, 99 sqlite3_value **argv 100 ){ 101 char *zVal; 102 int len; 103 104 test_destructor_count_var++; 105 assert( nArg==1 ); 106 if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return; 107 len = sqlite3_value_bytes(argv[0]); 108 zVal = testContextMalloc(pCtx, len+3); 109 if( !zVal ){ 110 return; 111 } 112 zVal[len+1] = 0; 113 zVal[len+2] = 0; 114 zVal++; 115 memcpy(zVal, sqlite3_value_text(argv[0]), len); 116 sqlite3_result_text(pCtx, zVal, -1, destructor); 117 } 118 #ifndef SQLITE_OMIT_UTF16 119 static void test_destructor16( 120 sqlite3_context *pCtx, 121 int nArg, 122 sqlite3_value **argv 123 ){ 124 char *zVal; 125 int len; 126 127 test_destructor_count_var++; 128 assert( nArg==1 ); 129 if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return; 130 len = sqlite3_value_bytes16(argv[0]); 131 zVal = testContextMalloc(pCtx, len+3); 132 if( !zVal ){ 133 return; 134 } 135 zVal[len+1] = 0; 136 zVal[len+2] = 0; 137 zVal++; 138 memcpy(zVal, sqlite3_value_text16(argv[0]), len); 139 sqlite3_result_text16(pCtx, zVal, -1, destructor); 140 } 141 #endif 142 static void test_destructor_count( 143 sqlite3_context *pCtx, 144 int nArg, 145 sqlite3_value **argv 146 ){ 147 sqlite3_result_int(pCtx, test_destructor_count_var); 148 } 149 150 /* 151 ** The following aggregate function, test_agg_errmsg16(), takes zero 152 ** arguments. It returns the text value returned by the sqlite3_errmsg16() 153 ** API function. 154 */ 155 #ifndef SQLITE_OMIT_BUILTIN_TEST 156 void sqlite3BeginBenignMalloc(void); 157 void sqlite3EndBenignMalloc(void); 158 #else 159 #define sqlite3BeginBenignMalloc() 160 #define sqlite3EndBenignMalloc() 161 #endif 162 static void test_agg_errmsg16_step(sqlite3_context *a, int b,sqlite3_value **c){ 163 } 164 static void test_agg_errmsg16_final(sqlite3_context *ctx){ 165 #ifndef SQLITE_OMIT_UTF16 166 const void *z; 167 sqlite3 * db = sqlite3_context_db_handle(ctx); 168 sqlite3_aggregate_context(ctx, 2048); 169 sqlite3BeginBenignMalloc(); 170 z = sqlite3_errmsg16(db); 171 sqlite3EndBenignMalloc(); 172 sqlite3_result_text16(ctx, z, -1, SQLITE_TRANSIENT); 173 #endif 174 } 175 176 /* 177 ** Routines for testing the sqlite3_get_auxdata() and sqlite3_set_auxdata() 178 ** interface. 179 ** 180 ** The test_auxdata() SQL function attempts to register each of its arguments 181 ** as auxiliary data. If there are no prior registrations of aux data for 182 ** that argument (meaning the argument is not a constant or this is its first 183 ** call) then the result for that argument is 0. If there is a prior 184 ** registration, the result for that argument is 1. The overall result 185 ** is the individual argument results separated by spaces. 186 */ 187 static void free_test_auxdata(void *p) {sqlite3_free(p);} 188 static void test_auxdata( 189 sqlite3_context *pCtx, 190 int nArg, 191 sqlite3_value **argv 192 ){ 193 int i; 194 char *zRet = testContextMalloc(pCtx, nArg*2); 195 if( !zRet ) return; 196 memset(zRet, 0, nArg*2); 197 for(i=0; i<nArg; i++){ 198 char const *z = (char*)sqlite3_value_text(argv[i]); 199 if( z ){ 200 int n; 201 char *zAux = sqlite3_get_auxdata(pCtx, i); 202 if( zAux ){ 203 zRet[i*2] = '1'; 204 assert( strcmp(zAux,z)==0 ); 205 }else { 206 zRet[i*2] = '0'; 207 } 208 n = (int)strlen(z) + 1; 209 zAux = testContextMalloc(pCtx, n); 210 if( zAux ){ 211 memcpy(zAux, z, n); 212 sqlite3_set_auxdata(pCtx, i, zAux, free_test_auxdata); 213 } 214 zRet[i*2+1] = ' '; 215 } 216 } 217 sqlite3_result_text(pCtx, zRet, 2*nArg-1, free_test_auxdata); 218 } 219 220 /* 221 ** A function to test error reporting from user functions. This function 222 ** returns a copy of its first argument as the error message. If the 223 ** second argument exists, it becomes the error code. 224 */ 225 static void test_error( 226 sqlite3_context *pCtx, 227 int nArg, 228 sqlite3_value **argv 229 ){ 230 sqlite3_result_error(pCtx, (char*)sqlite3_value_text(argv[0]), -1); 231 if( nArg==2 ){ 232 sqlite3_result_error_code(pCtx, sqlite3_value_int(argv[1])); 233 } 234 } 235 236 /* 237 ** Implementation of the counter(X) function. If X is an integer 238 ** constant, then the first invocation will return X. The second X+1. 239 ** and so forth. Can be used (for example) to provide a sequence number 240 ** in a result set. 241 */ 242 static void counterFunc( 243 sqlite3_context *pCtx, /* Function context */ 244 int nArg, /* Number of function arguments */ 245 sqlite3_value **argv /* Values for all function arguments */ 246 ){ 247 int *pCounter = (int*)sqlite3_get_auxdata(pCtx, 0); 248 if( pCounter==0 ){ 249 pCounter = sqlite3_malloc( sizeof(*pCounter) ); 250 if( pCounter==0 ){ 251 sqlite3_result_error_nomem(pCtx); 252 return; 253 } 254 *pCounter = sqlite3_value_int(argv[0]); 255 sqlite3_set_auxdata(pCtx, 0, pCounter, sqlite3_free); 256 }else{ 257 ++*pCounter; 258 } 259 sqlite3_result_int(pCtx, *pCounter); 260 } 261 262 263 /* 264 ** This function takes two arguments. It performance UTF-8/16 type 265 ** conversions on the first argument then returns a copy of the second 266 ** argument. 267 ** 268 ** This function is used in cases such as the following: 269 ** 270 ** SELECT test_isolation(x,x) FROM t1; 271 ** 272 ** We want to verify that the type conversions that occur on the 273 ** first argument do not invalidate the second argument. 274 */ 275 static void test_isolation( 276 sqlite3_context *pCtx, 277 int nArg, 278 sqlite3_value **argv 279 ){ 280 #ifndef SQLITE_OMIT_UTF16 281 sqlite3_value_text16(argv[0]); 282 sqlite3_value_text(argv[0]); 283 sqlite3_value_text16(argv[0]); 284 sqlite3_value_text(argv[0]); 285 #endif 286 sqlite3_result_value(pCtx, argv[1]); 287 } 288 289 /* 290 ** Invoke an SQL statement recursively. The function result is the 291 ** first column of the first row of the result set. 292 */ 293 static void test_eval( 294 sqlite3_context *pCtx, 295 int nArg, 296 sqlite3_value **argv 297 ){ 298 sqlite3_stmt *pStmt; 299 int rc; 300 sqlite3 *db = sqlite3_context_db_handle(pCtx); 301 const char *zSql; 302 303 zSql = (char*)sqlite3_value_text(argv[0]); 304 rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0); 305 if( rc==SQLITE_OK ){ 306 rc = sqlite3_step(pStmt); 307 if( rc==SQLITE_ROW ){ 308 sqlite3_result_value(pCtx, sqlite3_column_value(pStmt, 0)); 309 } 310 rc = sqlite3_finalize(pStmt); 311 } 312 if( rc ){ 313 char *zErr; 314 assert( pStmt==0 ); 315 zErr = sqlite3_mprintf("sqlite3_prepare_v2() error: %s",sqlite3_errmsg(db)); 316 sqlite3_result_text(pCtx, zErr, -1, sqlite3_free); 317 sqlite3_result_error_code(pCtx, rc); 318 } 319 } 320 321 322 /* 323 ** convert one character from hex to binary 324 */ 325 static int testHexChar(char c){ 326 if( c>='0' && c<='9' ){ 327 return c - '0'; 328 }else if( c>='a' && c<='f' ){ 329 return c - 'a' + 10; 330 }else if( c>='A' && c<='F' ){ 331 return c - 'A' + 10; 332 } 333 return 0; 334 } 335 336 /* 337 ** Convert hex to binary. 338 */ 339 static void testHexToBin(const char *zIn, char *zOut){ 340 while( zIn[0] && zIn[1] ){ 341 *(zOut++) = (testHexChar(zIn[0])<<4) + testHexChar(zIn[1]); 342 zIn += 2; 343 } 344 } 345 346 /* 347 ** hex_to_utf16be(HEX) 348 ** 349 ** Convert the input string from HEX into binary. Then return the 350 ** result using sqlite3_result_text16le(). 351 */ 352 #ifndef SQLITE_OMIT_UTF16 353 static void testHexToUtf16be( 354 sqlite3_context *pCtx, 355 int nArg, 356 sqlite3_value **argv 357 ){ 358 int n; 359 const char *zIn; 360 char *zOut; 361 assert( nArg==1 ); 362 n = sqlite3_value_bytes(argv[0]); 363 zIn = (const char*)sqlite3_value_text(argv[0]); 364 zOut = sqlite3_malloc( n/2 ); 365 if( zOut==0 ){ 366 sqlite3_result_error_nomem(pCtx); 367 }else{ 368 testHexToBin(zIn, zOut); 369 sqlite3_result_text16be(pCtx, zOut, n/2, sqlite3_free); 370 } 371 } 372 #endif 373 374 /* 375 ** hex_to_utf8(HEX) 376 ** 377 ** Convert the input string from HEX into binary. Then return the 378 ** result using sqlite3_result_text16le(). 379 */ 380 static void testHexToUtf8( 381 sqlite3_context *pCtx, 382 int nArg, 383 sqlite3_value **argv 384 ){ 385 int n; 386 const char *zIn; 387 char *zOut; 388 assert( nArg==1 ); 389 n = sqlite3_value_bytes(argv[0]); 390 zIn = (const char*)sqlite3_value_text(argv[0]); 391 zOut = sqlite3_malloc( n/2 ); 392 if( zOut==0 ){ 393 sqlite3_result_error_nomem(pCtx); 394 }else{ 395 testHexToBin(zIn, zOut); 396 sqlite3_result_text(pCtx, zOut, n/2, sqlite3_free); 397 } 398 } 399 400 /* 401 ** hex_to_utf16le(HEX) 402 ** 403 ** Convert the input string from HEX into binary. Then return the 404 ** result using sqlite3_result_text16le(). 405 */ 406 #ifndef SQLITE_OMIT_UTF16 407 static void testHexToUtf16le( 408 sqlite3_context *pCtx, 409 int nArg, 410 sqlite3_value **argv 411 ){ 412 int n; 413 const char *zIn; 414 char *zOut; 415 assert( nArg==1 ); 416 n = sqlite3_value_bytes(argv[0]); 417 zIn = (const char*)sqlite3_value_text(argv[0]); 418 zOut = sqlite3_malloc( n/2 ); 419 if( zOut==0 ){ 420 sqlite3_result_error_nomem(pCtx); 421 }else{ 422 testHexToBin(zIn, zOut); 423 sqlite3_result_text16le(pCtx, zOut, n/2, sqlite3_free); 424 } 425 } 426 #endif 427 428 /* 429 ** SQL function: real2hex(X) 430 ** 431 ** If argument X is a real number, then convert it into a string which is 432 ** the big-endian hexadecimal representation of the ieee754 encoding of 433 ** that number. If X is not a real number, return NULL. 434 */ 435 static void real2hex( 436 sqlite3_context *context, 437 int argc, 438 sqlite3_value **argv 439 ){ 440 union { 441 sqlite3_uint64 i; 442 double r; 443 unsigned char x[8]; 444 } v; 445 char zOut[20]; 446 int i; 447 int bigEndian; 448 v.i = 1; 449 bigEndian = v.x[0]==0; 450 v.r = sqlite3_value_double(argv[0]); 451 for(i=0; i<8; i++){ 452 if( bigEndian ){ 453 zOut[i*2] = "0123456789abcdef"[v.x[i]>>4]; 454 zOut[i*2+1] = "0123456789abcdef"[v.x[i]&0xf]; 455 }else{ 456 zOut[14-i*2] = "0123456789abcdef"[v.x[i]>>4]; 457 zOut[14-i*2+1] = "0123456789abcdef"[v.x[i]&0xf]; 458 } 459 } 460 zOut[16] = 0; 461 sqlite3_result_text(context, zOut, -1, SQLITE_TRANSIENT); 462 } 463 464 /* 465 ** tclcmd: test_extract(record, field) 466 ** 467 ** This function implements an SQL user-function that accepts a blob 468 ** containing a formatted database record as the first argument. The 469 ** second argument is the index of the field within that record to 470 ** extract and return. 471 */ 472 static void test_extract( 473 sqlite3_context *context, 474 int argc, 475 sqlite3_value **argv 476 ){ 477 sqlite3 *db = sqlite3_context_db_handle(context); 478 u8 *pRec; 479 u8 *pEndHdr; /* Points to one byte past record header */ 480 u8 *pHdr; /* Current point in record header */ 481 u8 *pBody; /* Current point in record data */ 482 u64 nHdr; /* Bytes in record header */ 483 int iIdx; /* Required field */ 484 int iCurrent = 0; /* Current field */ 485 486 assert( argc==2 ); 487 pRec = (u8*)sqlite3_value_blob(argv[0]); 488 iIdx = sqlite3_value_int(argv[1]); 489 490 pHdr = pRec + sqlite3GetVarint(pRec, &nHdr); 491 pBody = pEndHdr = &pRec[nHdr]; 492 493 for(iCurrent=0; pHdr<pEndHdr && iCurrent<=iIdx; iCurrent++){ 494 u64 iSerialType; 495 Mem mem; 496 497 memset(&mem, 0, sizeof(mem)); 498 mem.db = db; 499 mem.enc = ENC(db); 500 pHdr += sqlite3GetVarint(pHdr, &iSerialType); 501 pBody += sqlite3VdbeSerialGet(pBody, (u32)iSerialType, &mem); 502 sqlite3VdbeMemStoreType(&mem); 503 504 if( iCurrent==iIdx ){ 505 sqlite3_result_value(context, &mem); 506 } 507 508 sqlite3DbFree(db, mem.zMalloc); 509 } 510 } 511 512 /* 513 ** tclcmd: test_decode(record) 514 ** 515 ** This function implements an SQL user-function that accepts a blob 516 ** containing a formatted database record as its only argument. It returns 517 ** a tcl list (type SQLITE_TEXT) containing each of the values stored 518 ** in the record. 519 */ 520 static void test_decode( 521 sqlite3_context *context, 522 int argc, 523 sqlite3_value **argv 524 ){ 525 sqlite3 *db = sqlite3_context_db_handle(context); 526 u8 *pRec; 527 u8 *pEndHdr; /* Points to one byte past record header */ 528 u8 *pHdr; /* Current point in record header */ 529 u8 *pBody; /* Current point in record data */ 530 u64 nHdr; /* Bytes in record header */ 531 Tcl_Obj *pRet; /* Return value */ 532 533 pRet = Tcl_NewObj(); 534 Tcl_IncrRefCount(pRet); 535 536 assert( argc==1 ); 537 pRec = (u8*)sqlite3_value_blob(argv[0]); 538 539 pHdr = pRec + sqlite3GetVarint(pRec, &nHdr); 540 pBody = pEndHdr = &pRec[nHdr]; 541 while( pHdr<pEndHdr ){ 542 Tcl_Obj *pVal = 0; 543 u64 iSerialType; 544 Mem mem; 545 546 memset(&mem, 0, sizeof(mem)); 547 mem.db = db; 548 mem.enc = ENC(db); 549 pHdr += sqlite3GetVarint(pHdr, &iSerialType); 550 pBody += sqlite3VdbeSerialGet(pBody, (u32)iSerialType, &mem); 551 552 sqlite3VdbeMemStoreType(&mem); 553 switch( sqlite3_value_type(&mem) ){ 554 case SQLITE_TEXT: 555 pVal = Tcl_NewStringObj((const char*)sqlite3_value_text(&mem), -1); 556 break; 557 558 case SQLITE_BLOB: { 559 char hexdigit[] = { 560 '0', '1', '2', '3', '4', '5', '6', '7', 561 '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' 562 }; 563 int n = sqlite3_value_bytes(&mem); 564 u8 *z = (u8*)sqlite3_value_blob(&mem); 565 int i; 566 pVal = Tcl_NewStringObj("x'", -1); 567 for(i=0; i<n; i++){ 568 char hex[3]; 569 hex[0] = hexdigit[((z[i] >> 4) & 0x0F)]; 570 hex[1] = hexdigit[(z[i] & 0x0F)]; 571 hex[2] = '\0'; 572 Tcl_AppendStringsToObj(pVal, hex, 0); 573 } 574 Tcl_AppendStringsToObj(pVal, "'", 0); 575 break; 576 } 577 578 case SQLITE_FLOAT: 579 pVal = Tcl_NewDoubleObj(sqlite3_value_double(&mem)); 580 break; 581 582 case SQLITE_INTEGER: 583 pVal = Tcl_NewWideIntObj(sqlite3_value_int64(&mem)); 584 break; 585 586 case SQLITE_NULL: 587 pVal = Tcl_NewStringObj("NULL", -1); 588 break; 589 590 default: 591 assert( 0 ); 592 } 593 594 Tcl_ListObjAppendElement(0, pRet, pVal); 595 596 if( mem.zMalloc ){ 597 sqlite3DbFree(db, mem.zMalloc); 598 } 599 } 600 601 sqlite3_result_text(context, Tcl_GetString(pRet), -1, SQLITE_TRANSIENT); 602 Tcl_DecrRefCount(pRet); 603 } 604 605 606 static int registerTestFunctions(sqlite3 *db){ 607 static const struct { 608 char *zName; 609 signed char nArg; 610 unsigned char eTextRep; /* 1: UTF-16. 0: UTF-8 */ 611 void (*xFunc)(sqlite3_context*,int,sqlite3_value **); 612 } aFuncs[] = { 613 { "randstr", 2, SQLITE_UTF8, randStr }, 614 { "test_destructor", 1, SQLITE_UTF8, test_destructor}, 615 #ifndef SQLITE_OMIT_UTF16 616 { "test_destructor16", 1, SQLITE_UTF8, test_destructor16}, 617 { "hex_to_utf16be", 1, SQLITE_UTF8, testHexToUtf16be}, 618 { "hex_to_utf16le", 1, SQLITE_UTF8, testHexToUtf16le}, 619 #endif 620 { "hex_to_utf8", 1, SQLITE_UTF8, testHexToUtf8}, 621 { "test_destructor_count", 0, SQLITE_UTF8, test_destructor_count}, 622 { "test_auxdata", -1, SQLITE_UTF8, test_auxdata}, 623 { "test_error", 1, SQLITE_UTF8, test_error}, 624 { "test_error", 2, SQLITE_UTF8, test_error}, 625 { "test_eval", 1, SQLITE_UTF8, test_eval}, 626 { "test_isolation", 2, SQLITE_UTF8, test_isolation}, 627 { "test_counter", 1, SQLITE_UTF8, counterFunc}, 628 { "real2hex", 1, SQLITE_UTF8, real2hex}, 629 { "test_decode", 1, SQLITE_UTF8, test_decode}, 630 { "test_extract", 2, SQLITE_UTF8, test_extract}, 631 }; 632 int i; 633 634 for(i=0; i<sizeof(aFuncs)/sizeof(aFuncs[0]); i++){ 635 sqlite3_create_function(db, aFuncs[i].zName, aFuncs[i].nArg, 636 aFuncs[i].eTextRep, 0, aFuncs[i].xFunc, 0, 0); 637 } 638 639 sqlite3_create_function(db, "test_agg_errmsg16", 0, SQLITE_ANY, 0, 0, 640 test_agg_errmsg16_step, test_agg_errmsg16_final); 641 642 return SQLITE_OK; 643 } 644 645 /* 646 ** TCLCMD: autoinstall_test_functions 647 ** 648 ** Invoke this TCL command to use sqlite3_auto_extension() to cause 649 ** the standard set of test functions to be loaded into each new 650 ** database connection. 651 */ 652 static int autoinstall_test_funcs( 653 void * clientData, 654 Tcl_Interp *interp, 655 int objc, 656 Tcl_Obj *CONST objv[] 657 ){ 658 extern int Md5_Register(sqlite3*); 659 int rc = sqlite3_auto_extension((void*)registerTestFunctions); 660 if( rc==SQLITE_OK ){ 661 rc = sqlite3_auto_extension((void*)Md5_Register); 662 } 663 Tcl_SetObjResult(interp, Tcl_NewIntObj(rc)); 664 return TCL_OK; 665 } 666 667 /* 668 ** A bogus step function and finalizer function. 669 */ 670 static void tStep(sqlite3_context *a, int b, sqlite3_value **c){} 671 static void tFinal(sqlite3_context *a){} 672 673 674 /* 675 ** tclcmd: abuse_create_function 676 ** 677 ** Make various calls to sqlite3_create_function that do not have valid 678 ** parameters. Verify that the error condition is detected and reported. 679 */ 680 static int abuse_create_function( 681 void * clientData, 682 Tcl_Interp *interp, 683 int objc, 684 Tcl_Obj *CONST objv[] 685 ){ 686 extern int getDbPointer(Tcl_Interp*, const char*, sqlite3**); 687 sqlite3 *db; 688 int rc; 689 int mxArg; 690 691 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; 692 693 rc = sqlite3_create_function(db, "tx", 1, SQLITE_UTF8, 0, tStep,tStep,tFinal); 694 if( rc!=SQLITE_MISUSE ) goto abuse_err; 695 696 rc = sqlite3_create_function(db, "tx", 1, SQLITE_UTF8, 0, tStep, tStep, 0); 697 if( rc!=SQLITE_MISUSE ) goto abuse_err; 698 699 rc = sqlite3_create_function(db, "tx", 1, SQLITE_UTF8, 0, tStep, 0, tFinal); 700 if( rc!=SQLITE_MISUSE) goto abuse_err; 701 702 rc = sqlite3_create_function(db, "tx", 1, SQLITE_UTF8, 0, 0, 0, tFinal); 703 if( rc!=SQLITE_MISUSE ) goto abuse_err; 704 705 rc = sqlite3_create_function(db, "tx", 1, SQLITE_UTF8, 0, 0, tStep, 0); 706 if( rc!=SQLITE_MISUSE ) goto abuse_err; 707 708 rc = sqlite3_create_function(db, "tx", -2, SQLITE_UTF8, 0, tStep, 0, 0); 709 if( rc!=SQLITE_MISUSE ) goto abuse_err; 710 711 rc = sqlite3_create_function(db, "tx", 128, SQLITE_UTF8, 0, tStep, 0, 0); 712 if( rc!=SQLITE_MISUSE ) goto abuse_err; 713 714 rc = sqlite3_create_function(db, "funcxx" 715 "_123456789_123456789_123456789_123456789_123456789" 716 "_123456789_123456789_123456789_123456789_123456789" 717 "_123456789_123456789_123456789_123456789_123456789" 718 "_123456789_123456789_123456789_123456789_123456789" 719 "_123456789_123456789_123456789_123456789_123456789", 720 1, SQLITE_UTF8, 0, tStep, 0, 0); 721 if( rc!=SQLITE_MISUSE ) goto abuse_err; 722 723 /* This last function registration should actually work. Generate 724 ** a no-op function (that always returns NULL) and which has the 725 ** maximum-length function name and the maximum number of parameters. 726 */ 727 sqlite3_limit(db, SQLITE_LIMIT_FUNCTION_ARG, 10000); 728 mxArg = sqlite3_limit(db, SQLITE_LIMIT_FUNCTION_ARG, -1); 729 rc = sqlite3_create_function(db, "nullx" 730 "_123456789_123456789_123456789_123456789_123456789" 731 "_123456789_123456789_123456789_123456789_123456789" 732 "_123456789_123456789_123456789_123456789_123456789" 733 "_123456789_123456789_123456789_123456789_123456789" 734 "_123456789_123456789_123456789_123456789_123456789", 735 mxArg, SQLITE_UTF8, 0, tStep, 0, 0); 736 if( rc!=SQLITE_OK ) goto abuse_err; 737 738 return TCL_OK; 739 740 abuse_err: 741 Tcl_AppendResult(interp, "sqlite3_create_function abused test failed", 742 (char*)0); 743 return TCL_ERROR; 744 } 745 746 /* 747 ** Register commands with the TCL interpreter. 748 */ 749 int Sqlitetest_func_Init(Tcl_Interp *interp){ 750 static struct { 751 char *zName; 752 Tcl_ObjCmdProc *xProc; 753 } aObjCmd[] = { 754 { "autoinstall_test_functions", autoinstall_test_funcs }, 755 { "abuse_create_function", abuse_create_function }, 756 }; 757 int i; 758 extern int Md5_Register(sqlite3*); 759 760 for(i=0; i<sizeof(aObjCmd)/sizeof(aObjCmd[0]); i++){ 761 Tcl_CreateObjCommand(interp, aObjCmd[i].zName, aObjCmd[i].xProc, 0, 0); 762 } 763 sqlite3_initialize(); 764 sqlite3_auto_extension((void*)registerTestFunctions); 765 sqlite3_auto_extension((void*)Md5_Register); 766 return TCL_OK; 767 } 768