| OLD | NEW |
| 1 /* | 1 /* |
| 2 ** 2008 March 19 | 2 ** 2008 March 19 |
| 3 ** | 3 ** |
| 4 ** The author disclaims copyright to this source code. In place of | 4 ** The author disclaims copyright to this source code. In place of |
| 5 ** a legal notice, here is a blessing: | 5 ** a legal notice, here is a blessing: |
| 6 ** | 6 ** |
| 7 ** May you do good and not evil. | 7 ** May you do good and not evil. |
| 8 ** May you find forgiveness for yourself and forgive others. | 8 ** May you find forgiveness for yourself and forgive others. |
| 9 ** May you share freely, never taking more than you give. | 9 ** May you share freely, never taking more than you give. |
| 10 ** | 10 ** |
| 11 ************************************************************************* | 11 ************************************************************************* |
| 12 ** Code for testing all sorts of SQLite interfaces. This code | 12 ** Code for testing all sorts of SQLite interfaces. This code |
| 13 ** implements new SQL functions used by the test scripts. | 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 */ | 14 */ |
| 17 #include "sqlite3.h" | 15 #include "sqlite3.h" |
| 18 #include "tcl.h" | 16 #include "tcl.h" |
| 19 #include <stdlib.h> | 17 #include <stdlib.h> |
| 20 #include <string.h> | 18 #include <string.h> |
| 21 #include <assert.h> | 19 #include <assert.h> |
| 22 | 20 |
| 23 | 21 |
| 24 /* | 22 /* |
| 25 ** Allocate nByte bytes of space using sqlite3_malloc(). If the | 23 ** Allocate nByte bytes of space using sqlite3_malloc(). If the |
| (...skipping 280 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 306 if( rc ){ | 304 if( rc ){ |
| 307 char *zErr; | 305 char *zErr; |
| 308 assert( pStmt==0 ); | 306 assert( pStmt==0 ); |
| 309 zErr = sqlite3_mprintf("sqlite3_prepare_v2() error: %s",sqlite3_errmsg(db)); | 307 zErr = sqlite3_mprintf("sqlite3_prepare_v2() error: %s",sqlite3_errmsg(db)); |
| 310 sqlite3_result_text(pCtx, zErr, -1, sqlite3_free); | 308 sqlite3_result_text(pCtx, zErr, -1, sqlite3_free); |
| 311 sqlite3_result_error_code(pCtx, rc); | 309 sqlite3_result_error_code(pCtx, rc); |
| 312 } | 310 } |
| 313 } | 311 } |
| 314 | 312 |
| 315 | 313 |
| 314 /* |
| 315 ** convert one character from hex to binary |
| 316 */ |
| 317 static int testHexChar(char c){ |
| 318 if( c>='0' && c<='9' ){ |
| 319 return c - '0'; |
| 320 }else if( c>='a' && c<='f' ){ |
| 321 return c - 'a' + 10; |
| 322 }else if( c>='A' && c<='F' ){ |
| 323 return c - 'A' + 10; |
| 324 } |
| 325 return 0; |
| 326 } |
| 327 |
| 328 /* |
| 329 ** Convert hex to binary. |
| 330 */ |
| 331 static void testHexToBin(const char *zIn, char *zOut){ |
| 332 while( zIn[0] && zIn[1] ){ |
| 333 *(zOut++) = (testHexChar(zIn[0])<<4) + testHexChar(zIn[1]); |
| 334 zIn += 2; |
| 335 } |
| 336 } |
| 337 |
| 338 /* |
| 339 ** hex_to_utf16be(HEX) |
| 340 ** |
| 341 ** Convert the input string from HEX into binary. Then return the |
| 342 ** result using sqlite3_result_text16le(). |
| 343 */ |
| 344 #ifndef SQLITE_OMIT_UTF16 |
| 345 static void testHexToUtf16be( |
| 346 sqlite3_context *pCtx, |
| 347 int nArg, |
| 348 sqlite3_value **argv |
| 349 ){ |
| 350 int n; |
| 351 const char *zIn; |
| 352 char *zOut; |
| 353 assert( nArg==1 ); |
| 354 n = sqlite3_value_bytes(argv[0]); |
| 355 zIn = (const char*)sqlite3_value_text(argv[0]); |
| 356 zOut = sqlite3_malloc( n/2 ); |
| 357 if( zOut==0 ){ |
| 358 sqlite3_result_error_nomem(pCtx); |
| 359 }else{ |
| 360 testHexToBin(zIn, zOut); |
| 361 sqlite3_result_text16be(pCtx, zOut, n/2, sqlite3_free); |
| 362 } |
| 363 } |
| 364 #endif |
| 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 #ifndef SQLITE_OMIT_UTF16 |
| 399 static void testHexToUtf16le( |
| 400 sqlite3_context *pCtx, |
| 401 int nArg, |
| 402 sqlite3_value **argv |
| 403 ){ |
| 404 int n; |
| 405 const char *zIn; |
| 406 char *zOut; |
| 407 assert( nArg==1 ); |
| 408 n = sqlite3_value_bytes(argv[0]); |
| 409 zIn = (const char*)sqlite3_value_text(argv[0]); |
| 410 zOut = sqlite3_malloc( n/2 ); |
| 411 if( zOut==0 ){ |
| 412 sqlite3_result_error_nomem(pCtx); |
| 413 }else{ |
| 414 testHexToBin(zIn, zOut); |
| 415 sqlite3_result_text16le(pCtx, zOut, n/2, sqlite3_free); |
| 416 } |
| 417 } |
| 418 #endif |
| 419 |
| 316 static int registerTestFunctions(sqlite3 *db){ | 420 static int registerTestFunctions(sqlite3 *db){ |
| 317 static const struct { | 421 static const struct { |
| 318 char *zName; | 422 char *zName; |
| 319 signed char nArg; | 423 signed char nArg; |
| 320 unsigned char eTextRep; /* 1: UTF-16. 0: UTF-8 */ | 424 unsigned char eTextRep; /* 1: UTF-16. 0: UTF-8 */ |
| 321 void (*xFunc)(sqlite3_context*,int,sqlite3_value **); | 425 void (*xFunc)(sqlite3_context*,int,sqlite3_value **); |
| 322 } aFuncs[] = { | 426 } aFuncs[] = { |
| 323 { "randstr", 2, SQLITE_UTF8, randStr }, | 427 { "randstr", 2, SQLITE_UTF8, randStr }, |
| 324 { "test_destructor", 1, SQLITE_UTF8, test_destructor}, | 428 { "test_destructor", 1, SQLITE_UTF8, test_destructor}, |
| 325 #ifndef SQLITE_OMIT_UTF16 | 429 #ifndef SQLITE_OMIT_UTF16 |
| 326 { "test_destructor16", 1, SQLITE_UTF8, test_destructor16}, | 430 { "test_destructor16", 1, SQLITE_UTF8, test_destructor16}, |
| 431 { "hex_to_utf16be", 1, SQLITE_UTF8, testHexToUtf16be}, |
| 432 { "hex_to_utf16le", 1, SQLITE_UTF8, testHexToUtf16le}, |
| 327 #endif | 433 #endif |
| 434 { "hex_to_utf8", 1, SQLITE_UTF8, testHexToUtf8}, |
| 328 { "test_destructor_count", 0, SQLITE_UTF8, test_destructor_count}, | 435 { "test_destructor_count", 0, SQLITE_UTF8, test_destructor_count}, |
| 329 { "test_auxdata", -1, SQLITE_UTF8, test_auxdata}, | 436 { "test_auxdata", -1, SQLITE_UTF8, test_auxdata}, |
| 330 { "test_error", 1, SQLITE_UTF8, test_error}, | 437 { "test_error", 1, SQLITE_UTF8, test_error}, |
| 331 { "test_error", 2, SQLITE_UTF8, test_error}, | 438 { "test_error", 2, SQLITE_UTF8, test_error}, |
| 332 { "test_eval", 1, SQLITE_UTF8, test_eval}, | 439 { "test_eval", 1, SQLITE_UTF8, test_eval}, |
| 333 { "test_isolation", 2, SQLITE_UTF8, test_isolation}, | 440 { "test_isolation", 2, SQLITE_UTF8, test_isolation}, |
| 334 { "test_counter", 1, SQLITE_UTF8, counterFunc}, | 441 { "test_counter", 1, SQLITE_UTF8, counterFunc}, |
| 335 }; | 442 }; |
| 336 int i; | 443 int i; |
| 337 | 444 |
| (...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 440 if( rc!=SQLITE_OK ) goto abuse_err; | 547 if( rc!=SQLITE_OK ) goto abuse_err; |
| 441 | 548 |
| 442 return TCL_OK; | 549 return TCL_OK; |
| 443 | 550 |
| 444 abuse_err: | 551 abuse_err: |
| 445 Tcl_AppendResult(interp, "sqlite3_create_function abused test failed", | 552 Tcl_AppendResult(interp, "sqlite3_create_function abused test failed", |
| 446 (char*)0); | 553 (char*)0); |
| 447 return TCL_ERROR; | 554 return TCL_ERROR; |
| 448 } | 555 } |
| 449 | 556 |
| 450 | |
| 451 | |
| 452 /* | 557 /* |
| 453 ** Register commands with the TCL interpreter. | 558 ** Register commands with the TCL interpreter. |
| 454 */ | 559 */ |
| 455 int Sqlitetest_func_Init(Tcl_Interp *interp){ | 560 int Sqlitetest_func_Init(Tcl_Interp *interp){ |
| 456 static struct { | 561 static struct { |
| 457 char *zName; | 562 char *zName; |
| 458 Tcl_ObjCmdProc *xProc; | 563 Tcl_ObjCmdProc *xProc; |
| 459 } aObjCmd[] = { | 564 } aObjCmd[] = { |
| 460 { "autoinstall_test_functions", autoinstall_test_funcs }, | 565 { "autoinstall_test_functions", autoinstall_test_funcs }, |
| 461 { "abuse_create_function", abuse_create_function }, | 566 { "abuse_create_function", abuse_create_function }, |
| 462 }; | 567 }; |
| 463 int i; | 568 int i; |
| 464 extern int Md5_Register(sqlite3*); | 569 extern int Md5_Register(sqlite3*); |
| 465 | 570 |
| 466 for(i=0; i<sizeof(aObjCmd)/sizeof(aObjCmd[0]); i++){ | 571 for(i=0; i<sizeof(aObjCmd)/sizeof(aObjCmd[0]); i++){ |
| 467 Tcl_CreateObjCommand(interp, aObjCmd[i].zName, aObjCmd[i].xProc, 0, 0); | 572 Tcl_CreateObjCommand(interp, aObjCmd[i].zName, aObjCmd[i].xProc, 0, 0); |
| 468 } | 573 } |
| 469 sqlite3_initialize(); | 574 sqlite3_initialize(); |
| 470 sqlite3_auto_extension((void*)registerTestFunctions); | 575 sqlite3_auto_extension((void*)registerTestFunctions); |
| 471 sqlite3_auto_extension((void*)Md5_Register); | 576 sqlite3_auto_extension((void*)Md5_Register); |
| 472 return TCL_OK; | 577 return TCL_OK; |
| 473 } | 578 } |
| OLD | NEW |