xref: /sqlite-3.40.0/src/test_func.c (revision 7aa3ebee)
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   z = sqlite3_errmsg16(db);
170   sqlite3_result_text16(ctx, z, -1, SQLITE_TRANSIENT);
171 #endif
172 }
173 
174 /*
175 ** Routines for testing the sqlite3_get_auxdata() and sqlite3_set_auxdata()
176 ** interface.
177 **
178 ** The test_auxdata() SQL function attempts to register each of its arguments
179 ** as auxiliary data.  If there are no prior registrations of aux data for
180 ** that argument (meaning the argument is not a constant or this is its first
181 ** call) then the result for that argument is 0.  If there is a prior
182 ** registration, the result for that argument is 1.  The overall result
183 ** is the individual argument results separated by spaces.
184 */
185 static void free_test_auxdata(void *p) {sqlite3_free(p);}
186 static void test_auxdata(
187   sqlite3_context *pCtx,
188   int nArg,
189   sqlite3_value **argv
190 ){
191   int i;
192   char *zRet = testContextMalloc(pCtx, nArg*2);
193   if( !zRet ) return;
194   memset(zRet, 0, nArg*2);
195   for(i=0; i<nArg; i++){
196     char const *z = (char*)sqlite3_value_text(argv[i]);
197     if( z ){
198       int n;
199       char *zAux = sqlite3_get_auxdata(pCtx, i);
200       if( zAux ){
201         zRet[i*2] = '1';
202         assert( strcmp(zAux,z)==0 );
203       }else {
204         zRet[i*2] = '0';
205       }
206       n = (int)strlen(z) + 1;
207       zAux = testContextMalloc(pCtx, n);
208       if( zAux ){
209         memcpy(zAux, z, n);
210         sqlite3_set_auxdata(pCtx, i, zAux, free_test_auxdata);
211       }
212       zRet[i*2+1] = ' ';
213     }
214   }
215   sqlite3_result_text(pCtx, zRet, 2*nArg-1, free_test_auxdata);
216 }
217 
218 /*
219 ** A function to test error reporting from user functions. This function
220 ** returns a copy of its first argument as the error message.  If the
221 ** second argument exists, it becomes the error code.
222 */
223 static void test_error(
224   sqlite3_context *pCtx,
225   int nArg,
226   sqlite3_value **argv
227 ){
228   sqlite3_result_error(pCtx, (char*)sqlite3_value_text(argv[0]), -1);
229   if( nArg==2 ){
230     sqlite3_result_error_code(pCtx, sqlite3_value_int(argv[1]));
231   }
232 }
233 
234 /*
235 ** Implementation of the counter(X) function.  If X is an integer
236 ** constant, then the first invocation will return X.  The second X+1.
237 ** and so forth.  Can be used (for example) to provide a sequence number
238 ** in a result set.
239 */
240 static void counterFunc(
241   sqlite3_context *pCtx,   /* Function context */
242   int nArg,                /* Number of function arguments */
243   sqlite3_value **argv     /* Values for all function arguments */
244 ){
245   int *pCounter = (int*)sqlite3_get_auxdata(pCtx, 0);
246   if( pCounter==0 ){
247     pCounter = sqlite3_malloc( sizeof(*pCounter) );
248     if( pCounter==0 ){
249       sqlite3_result_error_nomem(pCtx);
250       return;
251     }
252     *pCounter = sqlite3_value_int(argv[0]);
253     sqlite3_set_auxdata(pCtx, 0, pCounter, sqlite3_free);
254   }else{
255     ++*pCounter;
256   }
257   sqlite3_result_int(pCtx, *pCounter);
258 }
259 
260 
261 /*
262 ** This function takes two arguments.  It performance UTF-8/16 type
263 ** conversions on the first argument then returns a copy of the second
264 ** argument.
265 **
266 ** This function is used in cases such as the following:
267 **
268 **      SELECT test_isolation(x,x) FROM t1;
269 **
270 ** We want to verify that the type conversions that occur on the
271 ** first argument do not invalidate the second argument.
272 */
273 static void test_isolation(
274   sqlite3_context *pCtx,
275   int nArg,
276   sqlite3_value **argv
277 ){
278 #ifndef SQLITE_OMIT_UTF16
279   sqlite3_value_text16(argv[0]);
280   sqlite3_value_text(argv[0]);
281   sqlite3_value_text16(argv[0]);
282   sqlite3_value_text(argv[0]);
283 #endif
284   sqlite3_result_value(pCtx, argv[1]);
285 }
286 
287 /*
288 ** Invoke an SQL statement recursively.  The function result is the
289 ** first column of the first row of the result set.
290 */
291 static void test_eval(
292   sqlite3_context *pCtx,
293   int nArg,
294   sqlite3_value **argv
295 ){
296   sqlite3_stmt *pStmt;
297   int rc;
298   sqlite3 *db = sqlite3_context_db_handle(pCtx);
299   const char *zSql;
300 
301   zSql = (char*)sqlite3_value_text(argv[0]);
302   rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
303   if( rc==SQLITE_OK ){
304     rc = sqlite3_step(pStmt);
305     if( rc==SQLITE_ROW ){
306       sqlite3_result_value(pCtx, sqlite3_column_value(pStmt, 0));
307     }
308     rc = sqlite3_finalize(pStmt);
309   }
310   if( rc ){
311     char *zErr;
312     assert( pStmt==0 );
313     zErr = sqlite3_mprintf("sqlite3_prepare_v2() error: %s",sqlite3_errmsg(db));
314     sqlite3_result_text(pCtx, zErr, -1, sqlite3_free);
315     sqlite3_result_error_code(pCtx, rc);
316   }
317 }
318 
319 
320 /*
321 ** convert one character from hex to binary
322 */
323 static int testHexChar(char c){
324   if( c>='0' && c<='9' ){
325     return c - '0';
326   }else if( c>='a' && c<='f' ){
327     return c - 'a' + 10;
328   }else if( c>='A' && c<='F' ){
329     return c - 'A' + 10;
330   }
331   return 0;
332 }
333 
334 /*
335 ** Convert hex to binary.
336 */
337 static void testHexToBin(const char *zIn, char *zOut){
338   while( zIn[0] && zIn[1] ){
339     *(zOut++) = (testHexChar(zIn[0])<<4) + testHexChar(zIn[1]);
340     zIn += 2;
341   }
342 }
343 
344 /*
345 **      hex_to_utf16be(HEX)
346 **
347 ** Convert the input string from HEX into binary.  Then return the
348 ** result using sqlite3_result_text16le().
349 */
350 #ifndef SQLITE_OMIT_UTF16
351 static void testHexToUtf16be(
352   sqlite3_context *pCtx,
353   int nArg,
354   sqlite3_value **argv
355 ){
356   int n;
357   const char *zIn;
358   char *zOut;
359   assert( nArg==1 );
360   n = sqlite3_value_bytes(argv[0]);
361   zIn = (const char*)sqlite3_value_text(argv[0]);
362   zOut = sqlite3_malloc( n/2 );
363   if( zOut==0 ){
364     sqlite3_result_error_nomem(pCtx);
365   }else{
366     testHexToBin(zIn, zOut);
367     sqlite3_result_text16be(pCtx, zOut, n/2, sqlite3_free);
368   }
369 }
370 #endif
371 
372 /*
373 **      hex_to_utf8(HEX)
374 **
375 ** Convert the input string from HEX into binary.  Then return the
376 ** result using sqlite3_result_text16le().
377 */
378 static void testHexToUtf8(
379   sqlite3_context *pCtx,
380   int nArg,
381   sqlite3_value **argv
382 ){
383   int n;
384   const char *zIn;
385   char *zOut;
386   assert( nArg==1 );
387   n = sqlite3_value_bytes(argv[0]);
388   zIn = (const char*)sqlite3_value_text(argv[0]);
389   zOut = sqlite3_malloc( n/2 );
390   if( zOut==0 ){
391     sqlite3_result_error_nomem(pCtx);
392   }else{
393     testHexToBin(zIn, zOut);
394     sqlite3_result_text(pCtx, zOut, n/2, sqlite3_free);
395   }
396 }
397 
398 /*
399 **      hex_to_utf16le(HEX)
400 **
401 ** Convert the input string from HEX into binary.  Then return the
402 ** result using sqlite3_result_text16le().
403 */
404 #ifndef SQLITE_OMIT_UTF16
405 static void testHexToUtf16le(
406   sqlite3_context *pCtx,
407   int nArg,
408   sqlite3_value **argv
409 ){
410   int n;
411   const char *zIn;
412   char *zOut;
413   assert( nArg==1 );
414   n = sqlite3_value_bytes(argv[0]);
415   zIn = (const char*)sqlite3_value_text(argv[0]);
416   zOut = sqlite3_malloc( n/2 );
417   if( zOut==0 ){
418     sqlite3_result_error_nomem(pCtx);
419   }else{
420     testHexToBin(zIn, zOut);
421     sqlite3_result_text16le(pCtx, zOut, n/2, sqlite3_free);
422   }
423 }
424 #endif
425 
426 /*
427 ** SQL function:   real2hex(X)
428 **
429 ** If argument X is a real number, then convert it into a string which is
430 ** the big-endian hexadecimal representation of the ieee754 encoding of
431 ** that number.  If X is not a real number, return NULL.
432 */
433 static void real2hex(
434   sqlite3_context *context,
435   int argc,
436   sqlite3_value **argv
437 ){
438   union {
439     sqlite3_uint64 i;
440     double r;
441     unsigned char x[8];
442   } v;
443   char zOut[20];
444   int i;
445   int bigEndian;
446   v.i = 1;
447   bigEndian = v.x[0]==0;
448   v.r = sqlite3_value_double(argv[0]);
449   for(i=0; i<8; i++){
450     if( bigEndian ){
451       zOut[i*2]   = "0123456789abcdef"[v.x[i]>>4];
452       zOut[i*2+1] = "0123456789abcdef"[v.x[i]&0xf];
453     }else{
454       zOut[14-i*2]   = "0123456789abcdef"[v.x[i]>>4];
455       zOut[14-i*2+1] = "0123456789abcdef"[v.x[i]&0xf];
456     }
457   }
458   zOut[16] = 0;
459   sqlite3_result_text(context, zOut, -1, SQLITE_TRANSIENT);
460 }
461 
462 /*
463 **     test_extract(record, field)
464 **
465 ** This function implements an SQL user-function that accepts a blob
466 ** containing a formatted database record as the first argument. The
467 ** second argument is the index of the field within that record to
468 ** extract and return.
469 */
470 static void test_extract(
471   sqlite3_context *context,
472   int argc,
473   sqlite3_value **argv
474 ){
475   sqlite3 *db = sqlite3_context_db_handle(context);
476   u8 *pRec;
477   u8 *pEndHdr;                    /* Points to one byte past record header */
478   u8 *pHdr;                       /* Current point in record header */
479   u8 *pBody;                      /* Current point in record data */
480   u64 nHdr;                       /* Bytes in record header */
481   int iIdx;                       /* Required field */
482   int iCurrent = 0;               /* Current field */
483 
484   assert( argc==2 );
485   pRec = (u8*)sqlite3_value_blob(argv[0]);
486   iIdx = sqlite3_value_int(argv[1]);
487 
488   pHdr = pRec + sqlite3GetVarint(pRec, &nHdr);
489   pBody = pEndHdr = &pRec[nHdr];
490 
491   for(iCurrent=0; pHdr<pEndHdr && iCurrent<=iIdx; iCurrent++){
492     u64 iSerialType;
493     Mem mem;
494 
495     memset(&mem, 0, sizeof(mem));
496     mem.db = db;
497     mem.enc = ENC(db);
498     pHdr += sqlite3GetVarint(pHdr, &iSerialType);
499     pBody += sqlite3VdbeSerialGet(pBody, (u32)iSerialType, &mem);
500 
501     if( iCurrent==iIdx ){
502       sqlite3_result_value(context, &mem);
503     }
504 
505     if( mem.szMalloc ) sqlite3DbFree(db, mem.zMalloc);
506   }
507 }
508 
509 /*
510 **      test_decode(record)
511 **
512 ** This function implements an SQL user-function that accepts a blob
513 ** containing a formatted database record as its only argument. It returns
514 ** a tcl list (type SQLITE_TEXT) containing each of the values stored
515 ** in the record.
516 */
517 static void test_decode(
518   sqlite3_context *context,
519   int argc,
520   sqlite3_value **argv
521 ){
522   sqlite3 *db = sqlite3_context_db_handle(context);
523   u8 *pRec;
524   u8 *pEndHdr;                    /* Points to one byte past record header */
525   u8 *pHdr;                       /* Current point in record header */
526   u8 *pBody;                      /* Current point in record data */
527   u64 nHdr;                       /* Bytes in record header */
528   Tcl_Obj *pRet;                  /* Return value */
529 
530   pRet = Tcl_NewObj();
531   Tcl_IncrRefCount(pRet);
532 
533   assert( argc==1 );
534   pRec = (u8*)sqlite3_value_blob(argv[0]);
535 
536   pHdr = pRec + sqlite3GetVarint(pRec, &nHdr);
537   pBody = pEndHdr = &pRec[nHdr];
538   while( pHdr<pEndHdr ){
539     Tcl_Obj *pVal = 0;
540     u64 iSerialType;
541     Mem mem;
542 
543     memset(&mem, 0, sizeof(mem));
544     mem.db = db;
545     mem.enc = ENC(db);
546     pHdr += sqlite3GetVarint(pHdr, &iSerialType);
547     pBody += sqlite3VdbeSerialGet(pBody, (u32)iSerialType, &mem);
548 
549     switch( sqlite3_value_type(&mem) ){
550       case SQLITE_TEXT:
551         pVal = Tcl_NewStringObj((const char*)sqlite3_value_text(&mem), -1);
552         break;
553 
554       case SQLITE_BLOB: {
555         char hexdigit[] = {
556           '0', '1', '2', '3', '4', '5', '6', '7',
557           '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'
558         };
559         int n = sqlite3_value_bytes(&mem);
560         u8 *z = (u8*)sqlite3_value_blob(&mem);
561         int i;
562         pVal = Tcl_NewStringObj("x'", -1);
563         for(i=0; i<n; i++){
564           char hex[3];
565           hex[0] = hexdigit[((z[i] >> 4) & 0x0F)];
566           hex[1] = hexdigit[(z[i] & 0x0F)];
567           hex[2] = '\0';
568           Tcl_AppendStringsToObj(pVal, hex, 0);
569         }
570         Tcl_AppendStringsToObj(pVal, "'", 0);
571         break;
572       }
573 
574       case SQLITE_FLOAT:
575         pVal = Tcl_NewDoubleObj(sqlite3_value_double(&mem));
576         break;
577 
578       case SQLITE_INTEGER:
579         pVal = Tcl_NewWideIntObj(sqlite3_value_int64(&mem));
580         break;
581 
582       case SQLITE_NULL:
583         pVal = Tcl_NewStringObj("NULL", -1);
584         break;
585 
586       default:
587         assert( 0 );
588     }
589 
590     Tcl_ListObjAppendElement(0, pRet, pVal);
591 
592     if( mem.szMalloc ){
593       sqlite3DbFree(db, mem.zMalloc);
594     }
595   }
596 
597   sqlite3_result_text(context, Tcl_GetString(pRet), -1, SQLITE_TRANSIENT);
598   Tcl_DecrRefCount(pRet);
599 }
600 
601 /*
602 **       test_zeroblob(N)
603 **
604 ** The implementation of scalar SQL function "test_zeroblob()". This is
605 ** similar to the built-in zeroblob() function, except that it does not
606 ** check that the integer parameter is within range before passing it
607 ** to sqlite3_result_zeroblob().
608 */
609 static void test_zeroblob(
610   sqlite3_context *context,
611   int argc,
612   sqlite3_value **argv
613 ){
614   int nZero = sqlite3_value_int(argv[0]);
615   sqlite3_result_zeroblob(context, nZero);
616 }
617 
618 /*         test_getsubtype(V)
619 **
620 ** Return the subtype for value V.
621 */
622 static void test_getsubtype(
623   sqlite3_context *context,
624   int argc,
625   sqlite3_value **argv
626 ){
627   sqlite3_result_int(context, (int)sqlite3_value_subtype(argv[0]));
628 }
629 
630 /*         test_setsubtype(V, T)
631 **
632 ** Return the value V with its subtype changed to T
633 */
634 static void test_setsubtype(
635   sqlite3_context *context,
636   int argc,
637   sqlite3_value **argv
638 ){
639   sqlite3_result_value(context, argv[0]);
640   sqlite3_result_subtype(context, (unsigned int)sqlite3_value_int(argv[1]));
641 }
642 
643 static int registerTestFunctions(sqlite3 *db){
644   static const struct {
645      char *zName;
646      signed char nArg;
647      unsigned int eTextRep; /* 1: UTF-16.  0: UTF-8 */
648      void (*xFunc)(sqlite3_context*,int,sqlite3_value **);
649   } aFuncs[] = {
650     { "randstr",               2, SQLITE_UTF8, randStr    },
651     { "test_destructor",       1, SQLITE_UTF8, test_destructor},
652 #ifndef SQLITE_OMIT_UTF16
653     { "test_destructor16",     1, SQLITE_UTF8, test_destructor16},
654     { "hex_to_utf16be",        1, SQLITE_UTF8, testHexToUtf16be},
655     { "hex_to_utf16le",        1, SQLITE_UTF8, testHexToUtf16le},
656 #endif
657     { "hex_to_utf8",           1, SQLITE_UTF8, testHexToUtf8},
658     { "test_destructor_count", 0, SQLITE_UTF8, test_destructor_count},
659     { "test_auxdata",         -1, SQLITE_UTF8, test_auxdata},
660     { "test_error",            1, SQLITE_UTF8, test_error},
661     { "test_error",            2, SQLITE_UTF8, test_error},
662     { "test_eval",             1, SQLITE_UTF8, test_eval},
663     { "test_isolation",        2, SQLITE_UTF8, test_isolation},
664     { "test_counter",          1, SQLITE_UTF8, counterFunc},
665     { "real2hex",              1, SQLITE_UTF8, real2hex},
666     { "test_decode",           1, SQLITE_UTF8, test_decode},
667     { "test_extract",          2, SQLITE_UTF8, test_extract},
668     { "test_zeroblob",  1, SQLITE_UTF8|SQLITE_DETERMINISTIC, test_zeroblob},
669     { "test_getsubtype",       1, SQLITE_UTF8, test_getsubtype},
670     { "test_setsubtype",       2, SQLITE_UTF8, test_setsubtype},
671   };
672   int i;
673 
674   for(i=0; i<sizeof(aFuncs)/sizeof(aFuncs[0]); i++){
675     sqlite3_create_function(db, aFuncs[i].zName, aFuncs[i].nArg,
676         aFuncs[i].eTextRep, 0, aFuncs[i].xFunc, 0, 0);
677   }
678 
679   sqlite3_create_function(db, "test_agg_errmsg16", 0, SQLITE_ANY, 0, 0,
680       test_agg_errmsg16_step, test_agg_errmsg16_final);
681 
682   return SQLITE_OK;
683 }
684 
685 /*
686 ** TCLCMD:  autoinstall_test_functions
687 **
688 ** Invoke this TCL command to use sqlite3_auto_extension() to cause
689 ** the standard set of test functions to be loaded into each new
690 ** database connection.
691 */
692 static int autoinstall_test_funcs(
693   void * clientData,
694   Tcl_Interp *interp,
695   int objc,
696   Tcl_Obj *CONST objv[]
697 ){
698   extern int Md5_Register(sqlite3*);
699   int rc = sqlite3_auto_extension((void*)registerTestFunctions);
700   if( rc==SQLITE_OK ){
701     rc = sqlite3_auto_extension((void*)Md5_Register);
702   }
703   Tcl_SetObjResult(interp, Tcl_NewIntObj(rc));
704   return TCL_OK;
705 }
706 
707 /*
708 ** A bogus step function and finalizer function.
709 */
710 static void tStep(sqlite3_context *a, int b, sqlite3_value **c){}
711 static void tFinal(sqlite3_context *a){}
712 
713 
714 /*
715 ** tclcmd:  abuse_create_function
716 **
717 ** Make various calls to sqlite3_create_function that do not have valid
718 ** parameters.  Verify that the error condition is detected and reported.
719 */
720 static int abuse_create_function(
721   void * clientData,
722   Tcl_Interp *interp,
723   int objc,
724   Tcl_Obj *CONST objv[]
725 ){
726   extern int getDbPointer(Tcl_Interp*, const char*, sqlite3**);
727   sqlite3 *db;
728   int rc;
729   int mxArg;
730 
731   if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
732 
733   rc = sqlite3_create_function(db, "tx", 1, SQLITE_UTF8, 0, tStep,tStep,tFinal);
734   if( rc!=SQLITE_MISUSE ) goto abuse_err;
735 
736   rc = sqlite3_create_function(db, "tx", 1, SQLITE_UTF8, 0, tStep, tStep, 0);
737   if( rc!=SQLITE_MISUSE ) goto abuse_err;
738 
739   rc = sqlite3_create_function(db, "tx", 1, SQLITE_UTF8, 0, tStep, 0, tFinal);
740   if( rc!=SQLITE_MISUSE) goto abuse_err;
741 
742   rc = sqlite3_create_function(db, "tx", 1, SQLITE_UTF8, 0, 0, 0, tFinal);
743   if( rc!=SQLITE_MISUSE ) goto abuse_err;
744 
745   rc = sqlite3_create_function(db, "tx", 1, SQLITE_UTF8, 0, 0, tStep, 0);
746   if( rc!=SQLITE_MISUSE ) goto abuse_err;
747 
748   rc = sqlite3_create_function(db, "tx", -2, SQLITE_UTF8, 0, tStep, 0, 0);
749   if( rc!=SQLITE_MISUSE ) goto abuse_err;
750 
751   rc = sqlite3_create_function(db, "tx", 128, SQLITE_UTF8, 0, tStep, 0, 0);
752   if( rc!=SQLITE_MISUSE ) goto abuse_err;
753 
754   rc = sqlite3_create_function(db, "funcxx"
755        "_123456789_123456789_123456789_123456789_123456789"
756        "_123456789_123456789_123456789_123456789_123456789"
757        "_123456789_123456789_123456789_123456789_123456789"
758        "_123456789_123456789_123456789_123456789_123456789"
759        "_123456789_123456789_123456789_123456789_123456789",
760        1, SQLITE_UTF8, 0, tStep, 0, 0);
761   if( rc!=SQLITE_MISUSE ) goto abuse_err;
762 
763   /* This last function registration should actually work.  Generate
764   ** a no-op function (that always returns NULL) and which has the
765   ** maximum-length function name and the maximum number of parameters.
766   */
767   sqlite3_limit(db, SQLITE_LIMIT_FUNCTION_ARG, 10000);
768   mxArg = sqlite3_limit(db, SQLITE_LIMIT_FUNCTION_ARG, -1);
769   rc = sqlite3_create_function(db, "nullx"
770        "_123456789_123456789_123456789_123456789_123456789"
771        "_123456789_123456789_123456789_123456789_123456789"
772        "_123456789_123456789_123456789_123456789_123456789"
773        "_123456789_123456789_123456789_123456789_123456789"
774        "_123456789_123456789_123456789_123456789_123456789",
775        mxArg, SQLITE_UTF8, 0, tStep, 0, 0);
776   if( rc!=SQLITE_OK ) goto abuse_err;
777 
778   return TCL_OK;
779 
780 abuse_err:
781   Tcl_AppendResult(interp, "sqlite3_create_function abused test failed",
782                    (char*)0);
783   return TCL_ERROR;
784 }
785 
786 /*
787 ** Register commands with the TCL interpreter.
788 */
789 int Sqlitetest_func_Init(Tcl_Interp *interp){
790   static struct {
791      char *zName;
792      Tcl_ObjCmdProc *xProc;
793   } aObjCmd[] = {
794      { "autoinstall_test_functions",    autoinstall_test_funcs },
795      { "abuse_create_function",         abuse_create_function  },
796   };
797   int i;
798   extern int Md5_Register(sqlite3*);
799 
800   for(i=0; i<sizeof(aObjCmd)/sizeof(aObjCmd[0]); i++){
801     Tcl_CreateObjCommand(interp, aObjCmd[i].zName, aObjCmd[i].xProc, 0, 0);
802   }
803   sqlite3_initialize();
804   sqlite3_auto_extension((void*)registerTestFunctions);
805   sqlite3_auto_extension((void*)Md5_Register);
806   return TCL_OK;
807 }
808