4 ** The author disclaims copyright to this source code. In place of
5 ** a legal notice, here is a blessing:
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.
11 *************************************************************************
12 ** Code for testing all sorts of SQLite interfaces. This code
13 ** implements new SQL functions used by the test scripts.
21 #include "sqliteInt.h"
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.
30 static void *testContextMalloc(sqlite3_context
*context
, int nByte
){
31 char *z
= sqlite3_malloc(nByte
);
33 sqlite3_result_error_nomem(context
);
39 ** This function generates a string of random characters. Used for
40 ** generating test data.
42 static void randStr(sqlite3_context
*context
, int argc
, sqlite3_value
**argv
){
43 static const unsigned char zSrc
[] =
44 "abcdefghijklmnopqrstuvwxyz"
45 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
48 int iMin
, iMax
, n
, r
, i
;
49 unsigned char zBuf
[1000];
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.
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;
64 sqlite3_randomness(sizeof(r
), &r
);
66 n
+= r
%(iMax
+ 1 - iMin
);
68 assert( n
<sizeof(zBuf
) );
69 sqlite3_randomness(n
, zBuf
);
71 zBuf
[i
] = zSrc
[zBuf
[i
]%(sizeof(zSrc
)-1)];
74 sqlite3_result_text(context
, (char*)zBuf
, n
, SQLITE_TRANSIENT
);
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.
83 ** SQL function 'test_destructor_count' returns the number of outstanding
84 ** allocations made by 'test_destructor';
86 ** WARNING: Not threadsafe.
88 static int test_destructor_count_var
= 0;
89 static void destructor(void *p
){
90 char *zVal
= (char *)p
;
94 test_destructor_count_var
--;
96 static void test_destructor(
97 sqlite3_context
*pCtx
,
104 test_destructor_count_var
++;
106 if( sqlite3_value_type(argv
[0])==SQLITE_NULL
) return;
107 len
= sqlite3_value_bytes(argv
[0]);
108 zVal
= testContextMalloc(pCtx
, len
+3);
115 memcpy(zVal
, sqlite3_value_text(argv
[0]), len
);
116 sqlite3_result_text(pCtx
, zVal
, -1, destructor
);
118 #ifndef SQLITE_OMIT_UTF16
119 static void test_destructor16(
120 sqlite3_context
*pCtx
,
127 test_destructor_count_var
++;
129 if( sqlite3_value_type(argv
[0])==SQLITE_NULL
) return;
130 len
= sqlite3_value_bytes16(argv
[0]);
131 zVal
= testContextMalloc(pCtx
, len
+3);
138 memcpy(zVal
, sqlite3_value_text16(argv
[0]), len
);
139 sqlite3_result_text16(pCtx
, zVal
, -1, destructor
);
142 static void test_destructor_count(
143 sqlite3_context
*pCtx
,
147 sqlite3_result_int(pCtx
, test_destructor_count_var
);
151 ** The following aggregate function, test_agg_errmsg16(), takes zero
152 ** arguments. It returns the text value returned by the sqlite3_errmsg16()
155 #ifndef SQLITE_OMIT_BUILTIN_TEST
156 void sqlite3BeginBenignMalloc(void);
157 void sqlite3EndBenignMalloc(void);
159 #define sqlite3BeginBenignMalloc()
160 #define sqlite3EndBenignMalloc()
162 static void test_agg_errmsg16_step(sqlite3_context
*a
, int b
,sqlite3_value
**c
){
164 static void test_agg_errmsg16_final(sqlite3_context
*ctx
){
165 #ifndef SQLITE_OMIT_UTF16
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
);
177 ** Routines for testing the sqlite3_get_auxdata() and sqlite3_set_auxdata()
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.
187 static void free_test_auxdata(void *p
) {sqlite3_free(p
);}
188 static void test_auxdata(
189 sqlite3_context
*pCtx
,
194 char *zRet
= testContextMalloc(pCtx
, nArg
*2);
196 memset(zRet
, 0, nArg
*2);
197 for(i
=0; i
<nArg
; i
++){
198 char const *z
= (char*)sqlite3_value_text(argv
[i
]);
201 char *zAux
= sqlite3_get_auxdata(pCtx
, i
);
204 assert( strcmp(zAux
,z
)==0 );
208 n
= (int)strlen(z
) + 1;
209 zAux
= testContextMalloc(pCtx
, n
);
212 sqlite3_set_auxdata(pCtx
, i
, zAux
, free_test_auxdata
);
217 sqlite3_result_text(pCtx
, zRet
, 2*nArg
-1, free_test_auxdata
);
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.
225 static void test_error(
226 sqlite3_context
*pCtx
,
230 sqlite3_result_error(pCtx
, (char*)sqlite3_value_text(argv
[0]), -1);
232 sqlite3_result_error_code(pCtx
, sqlite3_value_int(argv
[1]));
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
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 */
247 int *pCounter
= (int*)sqlite3_get_auxdata(pCtx
, 0);
249 pCounter
= sqlite3_malloc( sizeof(*pCounter
) );
251 sqlite3_result_error_nomem(pCtx
);
254 *pCounter
= sqlite3_value_int(argv
[0]);
255 sqlite3_set_auxdata(pCtx
, 0, pCounter
, sqlite3_free
);
259 sqlite3_result_int(pCtx
, *pCounter
);
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
268 ** This function is used in cases such as the following:
270 ** SELECT test_isolation(x,x) FROM t1;
272 ** We want to verify that the type conversions that occur on the
273 ** first argument do not invalidate the second argument.
275 static void test_isolation(
276 sqlite3_context
*pCtx
,
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]);
286 sqlite3_result_value(pCtx
, argv
[1]);
290 ** Invoke an SQL statement recursively. The function result is the
291 ** first column of the first row of the result set.
293 static void test_eval(
294 sqlite3_context
*pCtx
,
300 sqlite3
*db
= sqlite3_context_db_handle(pCtx
);
303 zSql
= (char*)sqlite3_value_text(argv
[0]);
304 rc
= sqlite3_prepare_v2(db
, zSql
, -1, &pStmt
, 0);
306 rc
= sqlite3_step(pStmt
);
307 if( rc
==SQLITE_ROW
){
308 sqlite3_result_value(pCtx
, sqlite3_column_value(pStmt
, 0));
310 rc
= sqlite3_finalize(pStmt
);
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
);
323 ** convert one character from hex to binary
325 static int testHexChar(char c
){
326 if( c
>='0' && c
<='9' ){
328 }else if( c
>='a' && c
<='f' ){
330 }else if( c
>='A' && c
<='F' ){
337 ** Convert hex to binary.
339 static void testHexToBin(const char *zIn
, char *zOut
){
340 while( zIn
[0] && zIn
[1] ){
341 *(zOut
++) = (testHexChar(zIn
[0])<<4) + testHexChar(zIn
[1]);
347 ** hex_to_utf16be(HEX)
349 ** Convert the input string from HEX into binary. Then return the
350 ** result using sqlite3_result_text16le().
352 #ifndef SQLITE_OMIT_UTF16
353 static void testHexToUtf16be(
354 sqlite3_context
*pCtx
,
362 n
= sqlite3_value_bytes(argv
[0]);
363 zIn
= (const char*)sqlite3_value_text(argv
[0]);
364 zOut
= sqlite3_malloc( n
/2 );
366 sqlite3_result_error_nomem(pCtx
);
368 testHexToBin(zIn
, zOut
);
369 sqlite3_result_text16be(pCtx
, zOut
, n
/2, sqlite3_free
);
377 ** Convert the input string from HEX into binary. Then return the
378 ** result using sqlite3_result_text16le().
380 static void testHexToUtf8(
381 sqlite3_context
*pCtx
,
389 n
= sqlite3_value_bytes(argv
[0]);
390 zIn
= (const char*)sqlite3_value_text(argv
[0]);
391 zOut
= sqlite3_malloc( n
/2 );
393 sqlite3_result_error_nomem(pCtx
);
395 testHexToBin(zIn
, zOut
);
396 sqlite3_result_text(pCtx
, zOut
, n
/2, sqlite3_free
);
401 ** hex_to_utf16le(HEX)
403 ** Convert the input string from HEX into binary. Then return the
404 ** result using sqlite3_result_text16le().
406 #ifndef SQLITE_OMIT_UTF16
407 static void testHexToUtf16le(
408 sqlite3_context
*pCtx
,
416 n
= sqlite3_value_bytes(argv
[0]);
417 zIn
= (const char*)sqlite3_value_text(argv
[0]);
418 zOut
= sqlite3_malloc( n
/2 );
420 sqlite3_result_error_nomem(pCtx
);
422 testHexToBin(zIn
, zOut
);
423 sqlite3_result_text16le(pCtx
, zOut
, n
/2, sqlite3_free
);
429 ** SQL function: real2hex(X)
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.
435 static void real2hex(
436 sqlite3_context
*context
,
449 bigEndian
= v
.x
[0]==0;
450 v
.r
= sqlite3_value_double(argv
[0]);
453 zOut
[i
*2] = "0123456789abcdef"[v
.x
[i
]>>4];
454 zOut
[i
*2+1] = "0123456789abcdef"[v
.x
[i
]&0xf];
456 zOut
[14-i
*2] = "0123456789abcdef"[v
.x
[i
]>>4];
457 zOut
[14-i
*2+1] = "0123456789abcdef"[v
.x
[i
]&0xf];
461 sqlite3_result_text(context
, zOut
, -1, SQLITE_TRANSIENT
);
465 ** tclcmd: test_extract(record, field)
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.
472 static void test_extract(
473 sqlite3_context
*context
,
477 sqlite3
*db
= sqlite3_context_db_handle(context
);
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 */
487 pRec
= (u8
*)sqlite3_value_blob(argv
[0]);
488 iIdx
= sqlite3_value_int(argv
[1]);
490 pHdr
= pRec
+ sqlite3GetVarint(pRec
, &nHdr
);
491 pBody
= pEndHdr
= &pRec
[nHdr
];
493 for(iCurrent
=0; pHdr
<pEndHdr
&& iCurrent
<=iIdx
; iCurrent
++){
497 memset(&mem
, 0, sizeof(mem
));
500 pHdr
+= sqlite3GetVarint(pHdr
, &iSerialType
);
501 pBody
+= sqlite3VdbeSerialGet(pBody
, (u32
)iSerialType
, &mem
);
503 if( iCurrent
==iIdx
){
504 sqlite3_result_value(context
, &mem
);
507 if( mem
.szMalloc
) sqlite3DbFree(db
, mem
.zMalloc
);
512 ** tclcmd: test_decode(record)
514 ** This function implements an SQL user-function that accepts a blob
515 ** containing a formatted database record as its only argument. It returns
516 ** a tcl list (type SQLITE_TEXT) containing each of the values stored
519 static void test_decode(
520 sqlite3_context
*context
,
524 sqlite3
*db
= sqlite3_context_db_handle(context
);
526 u8
*pEndHdr
; /* Points to one byte past record header */
527 u8
*pHdr
; /* Current point in record header */
528 u8
*pBody
; /* Current point in record data */
529 u64 nHdr
; /* Bytes in record header */
530 Tcl_Obj
*pRet
; /* Return value */
533 Tcl_IncrRefCount(pRet
);
536 pRec
= (u8
*)sqlite3_value_blob(argv
[0]);
538 pHdr
= pRec
+ sqlite3GetVarint(pRec
, &nHdr
);
539 pBody
= pEndHdr
= &pRec
[nHdr
];
540 while( pHdr
<pEndHdr
){
545 memset(&mem
, 0, sizeof(mem
));
548 pHdr
+= sqlite3GetVarint(pHdr
, &iSerialType
);
549 pBody
+= sqlite3VdbeSerialGet(pBody
, (u32
)iSerialType
, &mem
);
551 switch( sqlite3_value_type(&mem
) ){
553 pVal
= Tcl_NewStringObj((const char*)sqlite3_value_text(&mem
), -1);
558 '0', '1', '2', '3', '4', '5', '6', '7',
559 '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'
561 int n
= sqlite3_value_bytes(&mem
);
562 u8
*z
= (u8
*)sqlite3_value_blob(&mem
);
564 pVal
= Tcl_NewStringObj("x'", -1);
567 hex
[0] = hexdigit
[((z
[i
] >> 4) & 0x0F)];
568 hex
[1] = hexdigit
[(z
[i
] & 0x0F)];
570 Tcl_AppendStringsToObj(pVal
, hex
, 0);
572 Tcl_AppendStringsToObj(pVal
, "'", 0);
577 pVal
= Tcl_NewDoubleObj(sqlite3_value_double(&mem
));
581 pVal
= Tcl_NewWideIntObj(sqlite3_value_int64(&mem
));
585 pVal
= Tcl_NewStringObj("NULL", -1);
592 Tcl_ListObjAppendElement(0, pRet
, pVal
);
595 sqlite3DbFree(db
, mem
.zMalloc
);
599 sqlite3_result_text(context
, Tcl_GetString(pRet
), -1, SQLITE_TRANSIENT
);
600 Tcl_DecrRefCount(pRet
);
604 static int registerTestFunctions(sqlite3
*db
){
605 static const struct {
608 unsigned char eTextRep
; /* 1: UTF-16. 0: UTF-8 */
609 void (*xFunc
)(sqlite3_context
*,int,sqlite3_value
**);
611 { "randstr", 2, SQLITE_UTF8
, randStr
},
612 { "test_destructor", 1, SQLITE_UTF8
, test_destructor
},
613 #ifndef SQLITE_OMIT_UTF16
614 { "test_destructor16", 1, SQLITE_UTF8
, test_destructor16
},
615 { "hex_to_utf16be", 1, SQLITE_UTF8
, testHexToUtf16be
},
616 { "hex_to_utf16le", 1, SQLITE_UTF8
, testHexToUtf16le
},
618 { "hex_to_utf8", 1, SQLITE_UTF8
, testHexToUtf8
},
619 { "test_destructor_count", 0, SQLITE_UTF8
, test_destructor_count
},
620 { "test_auxdata", -1, SQLITE_UTF8
, test_auxdata
},
621 { "test_error", 1, SQLITE_UTF8
, test_error
},
622 { "test_error", 2, SQLITE_UTF8
, test_error
},
623 { "test_eval", 1, SQLITE_UTF8
, test_eval
},
624 { "test_isolation", 2, SQLITE_UTF8
, test_isolation
},
625 { "test_counter", 1, SQLITE_UTF8
, counterFunc
},
626 { "real2hex", 1, SQLITE_UTF8
, real2hex
},
627 { "test_decode", 1, SQLITE_UTF8
, test_decode
},
628 { "test_extract", 2, SQLITE_UTF8
, test_extract
},
632 for(i
=0; i
<sizeof(aFuncs
)/sizeof(aFuncs
[0]); i
++){
633 sqlite3_create_function(db
, aFuncs
[i
].zName
, aFuncs
[i
].nArg
,
634 aFuncs
[i
].eTextRep
, 0, aFuncs
[i
].xFunc
, 0, 0);
637 sqlite3_create_function(db
, "test_agg_errmsg16", 0, SQLITE_ANY
, 0, 0,
638 test_agg_errmsg16_step
, test_agg_errmsg16_final
);
644 ** TCLCMD: autoinstall_test_functions
646 ** Invoke this TCL command to use sqlite3_auto_extension() to cause
647 ** the standard set of test functions to be loaded into each new
648 ** database connection.
650 static int autoinstall_test_funcs(
654 Tcl_Obj
*CONST objv
[]
656 extern int Md5_Register(sqlite3
*);
657 int rc
= sqlite3_auto_extension((void*)registerTestFunctions
);
659 rc
= sqlite3_auto_extension((void*)Md5_Register
);
661 Tcl_SetObjResult(interp
, Tcl_NewIntObj(rc
));
666 ** A bogus step function and finalizer function.
668 static void tStep(sqlite3_context
*a
, int b
, sqlite3_value
**c
){}
669 static void tFinal(sqlite3_context
*a
){}
673 ** tclcmd: abuse_create_function
675 ** Make various calls to sqlite3_create_function that do not have valid
676 ** parameters. Verify that the error condition is detected and reported.
678 static int abuse_create_function(
682 Tcl_Obj
*CONST objv
[]
684 extern int getDbPointer(Tcl_Interp
*, const char*, sqlite3
**);
689 if( getDbPointer(interp
, Tcl_GetString(objv
[1]), &db
) ) return TCL_ERROR
;
691 rc
= sqlite3_create_function(db
, "tx", 1, SQLITE_UTF8
, 0, tStep
,tStep
,tFinal
);
692 if( rc
!=SQLITE_MISUSE
) goto abuse_err
;
694 rc
= sqlite3_create_function(db
, "tx", 1, SQLITE_UTF8
, 0, tStep
, tStep
, 0);
695 if( rc
!=SQLITE_MISUSE
) goto abuse_err
;
697 rc
= sqlite3_create_function(db
, "tx", 1, SQLITE_UTF8
, 0, tStep
, 0, tFinal
);
698 if( rc
!=SQLITE_MISUSE
) goto abuse_err
;
700 rc
= sqlite3_create_function(db
, "tx", 1, SQLITE_UTF8
, 0, 0, 0, tFinal
);
701 if( rc
!=SQLITE_MISUSE
) goto abuse_err
;
703 rc
= sqlite3_create_function(db
, "tx", 1, SQLITE_UTF8
, 0, 0, tStep
, 0);
704 if( rc
!=SQLITE_MISUSE
) goto abuse_err
;
706 rc
= sqlite3_create_function(db
, "tx", -2, SQLITE_UTF8
, 0, tStep
, 0, 0);
707 if( rc
!=SQLITE_MISUSE
) goto abuse_err
;
709 rc
= sqlite3_create_function(db
, "tx", 128, SQLITE_UTF8
, 0, tStep
, 0, 0);
710 if( rc
!=SQLITE_MISUSE
) goto abuse_err
;
712 rc
= sqlite3_create_function(db
, "funcxx"
713 "_123456789_123456789_123456789_123456789_123456789"
714 "_123456789_123456789_123456789_123456789_123456789"
715 "_123456789_123456789_123456789_123456789_123456789"
716 "_123456789_123456789_123456789_123456789_123456789"
717 "_123456789_123456789_123456789_123456789_123456789",
718 1, SQLITE_UTF8
, 0, tStep
, 0, 0);
719 if( rc
!=SQLITE_MISUSE
) goto abuse_err
;
721 /* This last function registration should actually work. Generate
722 ** a no-op function (that always returns NULL) and which has the
723 ** maximum-length function name and the maximum number of parameters.
725 sqlite3_limit(db
, SQLITE_LIMIT_FUNCTION_ARG
, 10000);
726 mxArg
= sqlite3_limit(db
, SQLITE_LIMIT_FUNCTION_ARG
, -1);
727 rc
= sqlite3_create_function(db
, "nullx"
728 "_123456789_123456789_123456789_123456789_123456789"
729 "_123456789_123456789_123456789_123456789_123456789"
730 "_123456789_123456789_123456789_123456789_123456789"
731 "_123456789_123456789_123456789_123456789_123456789"
732 "_123456789_123456789_123456789_123456789_123456789",
733 mxArg
, SQLITE_UTF8
, 0, tStep
, 0, 0);
734 if( rc
!=SQLITE_OK
) goto abuse_err
;
739 Tcl_AppendResult(interp
, "sqlite3_create_function abused test failed",
745 ** Register commands with the TCL interpreter.
747 int Sqlitetest_func_Init(Tcl_Interp
*interp
){
750 Tcl_ObjCmdProc
*xProc
;
752 { "autoinstall_test_functions", autoinstall_test_funcs
},
753 { "abuse_create_function", abuse_create_function
},
756 extern int Md5_Register(sqlite3
*);
758 for(i
=0; i
<sizeof(aObjCmd
)/sizeof(aObjCmd
[0]); i
++){
759 Tcl_CreateObjCommand(interp
, aObjCmd
[i
].zName
, aObjCmd
[i
].xProc
, 0, 0);
761 sqlite3_initialize();
762 sqlite3_auto_extension((void*)registerTestFunctions
);
763 sqlite3_auto_extension((void*)Md5_Register
);