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