drh | 984bfaa | 2008-03-19 16:08:53 +0000 | [diff] [blame] | 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. |
drh | 984bfaa | 2008-03-19 16:08:53 +0000 | [diff] [blame] | 14 | */ |
| 15 | #include "sqlite3.h" |
| 16 | #include "tcl.h" |
| 17 | #include <stdlib.h> |
| 18 | #include <string.h> |
| 19 | #include <assert.h> |
| 20 | |
| 21 | |
| 22 | /* |
| 23 | ** Allocate nByte bytes of space using sqlite3_malloc(). If the |
| 24 | ** allocation fails, call sqlite3_result_error_nomem() to notify |
| 25 | ** the database handle that malloc() has failed. |
| 26 | */ |
| 27 | static void *testContextMalloc(sqlite3_context *context, int nByte){ |
| 28 | char *z = sqlite3_malloc(nByte); |
| 29 | if( !z && nByte>0 ){ |
| 30 | sqlite3_result_error_nomem(context); |
| 31 | } |
| 32 | return z; |
| 33 | } |
| 34 | |
| 35 | /* |
| 36 | ** This function generates a string of random characters. Used for |
| 37 | ** generating test data. |
| 38 | */ |
| 39 | static void randStr(sqlite3_context *context, int argc, sqlite3_value **argv){ |
| 40 | static const unsigned char zSrc[] = |
| 41 | "abcdefghijklmnopqrstuvwxyz" |
| 42 | "ABCDEFGHIJKLMNOPQRSTUVWXYZ" |
| 43 | "0123456789" |
| 44 | ".-!,:*^+=_|?/<> "; |
| 45 | int iMin, iMax, n, r, i; |
| 46 | unsigned char zBuf[1000]; |
| 47 | |
| 48 | /* It used to be possible to call randstr() with any number of arguments, |
| 49 | ** but now it is registered with SQLite as requiring exactly 2. |
| 50 | */ |
| 51 | assert(argc==2); |
| 52 | |
| 53 | iMin = sqlite3_value_int(argv[0]); |
| 54 | if( iMin<0 ) iMin = 0; |
| 55 | if( iMin>=sizeof(zBuf) ) iMin = sizeof(zBuf)-1; |
| 56 | iMax = sqlite3_value_int(argv[1]); |
| 57 | if( iMax<iMin ) iMax = iMin; |
| 58 | if( iMax>=sizeof(zBuf) ) iMax = sizeof(zBuf)-1; |
| 59 | n = iMin; |
| 60 | if( iMax>iMin ){ |
| 61 | sqlite3_randomness(sizeof(r), &r); |
| 62 | r &= 0x7fffffff; |
| 63 | n += r%(iMax + 1 - iMin); |
| 64 | } |
| 65 | assert( n<sizeof(zBuf) ); |
| 66 | sqlite3_randomness(n, zBuf); |
| 67 | for(i=0; i<n; i++){ |
| 68 | zBuf[i] = zSrc[zBuf[i]%(sizeof(zSrc)-1)]; |
| 69 | } |
| 70 | zBuf[n] = 0; |
| 71 | sqlite3_result_text(context, (char*)zBuf, n, SQLITE_TRANSIENT); |
| 72 | } |
| 73 | |
| 74 | /* |
| 75 | ** The following two SQL functions are used to test returning a text |
| 76 | ** result with a destructor. Function 'test_destructor' takes one argument |
| 77 | ** and returns the same argument interpreted as TEXT. A destructor is |
| 78 | ** passed with the sqlite3_result_text() call. |
| 79 | ** |
| 80 | ** SQL function 'test_destructor_count' returns the number of outstanding |
| 81 | ** allocations made by 'test_destructor'; |
| 82 | ** |
| 83 | ** WARNING: Not threadsafe. |
| 84 | */ |
| 85 | static int test_destructor_count_var = 0; |
| 86 | static void destructor(void *p){ |
| 87 | char *zVal = (char *)p; |
| 88 | assert(zVal); |
| 89 | zVal--; |
| 90 | sqlite3_free(zVal); |
| 91 | test_destructor_count_var--; |
| 92 | } |
| 93 | static void test_destructor( |
| 94 | sqlite3_context *pCtx, |
| 95 | int nArg, |
| 96 | sqlite3_value **argv |
| 97 | ){ |
| 98 | char *zVal; |
| 99 | int len; |
| 100 | |
| 101 | test_destructor_count_var++; |
| 102 | assert( nArg==1 ); |
| 103 | if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return; |
| 104 | len = sqlite3_value_bytes(argv[0]); |
| 105 | zVal = testContextMalloc(pCtx, len+3); |
| 106 | if( !zVal ){ |
| 107 | return; |
| 108 | } |
| 109 | zVal[len+1] = 0; |
| 110 | zVal[len+2] = 0; |
| 111 | zVal++; |
| 112 | memcpy(zVal, sqlite3_value_text(argv[0]), len); |
| 113 | sqlite3_result_text(pCtx, zVal, -1, destructor); |
| 114 | } |
shane | 2a5fc4d | 2008-07-31 01:47:11 +0000 | [diff] [blame] | 115 | #ifndef SQLITE_OMIT_UTF16 |
drh | da84ca8 | 2008-03-19 16:35:24 +0000 | [diff] [blame] | 116 | static void test_destructor16( |
| 117 | sqlite3_context *pCtx, |
| 118 | int nArg, |
| 119 | sqlite3_value **argv |
| 120 | ){ |
| 121 | char *zVal; |
| 122 | int len; |
| 123 | |
| 124 | test_destructor_count_var++; |
| 125 | assert( nArg==1 ); |
| 126 | if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return; |
| 127 | len = sqlite3_value_bytes16(argv[0]); |
| 128 | zVal = testContextMalloc(pCtx, len+3); |
| 129 | if( !zVal ){ |
| 130 | return; |
| 131 | } |
| 132 | zVal[len+1] = 0; |
| 133 | zVal[len+2] = 0; |
| 134 | zVal++; |
| 135 | memcpy(zVal, sqlite3_value_text16(argv[0]), len); |
| 136 | sqlite3_result_text16(pCtx, zVal, -1, destructor); |
| 137 | } |
shane | 2a5fc4d | 2008-07-31 01:47:11 +0000 | [diff] [blame] | 138 | #endif |
drh | 984bfaa | 2008-03-19 16:08:53 +0000 | [diff] [blame] | 139 | static void test_destructor_count( |
| 140 | sqlite3_context *pCtx, |
| 141 | int nArg, |
| 142 | sqlite3_value **argv |
| 143 | ){ |
| 144 | sqlite3_result_int(pCtx, test_destructor_count_var); |
| 145 | } |
| 146 | |
| 147 | /* |
danielk1977 | 238746a | 2009-03-19 18:51:06 +0000 | [diff] [blame] | 148 | ** The following aggregate function, test_agg_errmsg16(), takes zero |
| 149 | ** arguments. It returns the text value returned by the sqlite3_errmsg16() |
| 150 | ** API function. |
| 151 | */ |
| 152 | void sqlite3BeginBenignMalloc(void); |
| 153 | void sqlite3EndBenignMalloc(void); |
| 154 | static void test_agg_errmsg16_step(sqlite3_context *a, int b,sqlite3_value **c){ |
| 155 | } |
| 156 | static void test_agg_errmsg16_final(sqlite3_context *ctx){ |
danielk1977 | 257d9dc | 2009-07-22 07:27:56 +0000 | [diff] [blame] | 157 | #ifndef SQLITE_OMIT_UTF16 |
danielk1977 | 238746a | 2009-03-19 18:51:06 +0000 | [diff] [blame] | 158 | const void *z; |
| 159 | sqlite3 * db = sqlite3_context_db_handle(ctx); |
| 160 | sqlite3_aggregate_context(ctx, 2048); |
| 161 | sqlite3BeginBenignMalloc(); |
| 162 | z = sqlite3_errmsg16(db); |
| 163 | sqlite3EndBenignMalloc(); |
| 164 | sqlite3_result_text16(ctx, z, -1, SQLITE_TRANSIENT); |
danielk1977 | 257d9dc | 2009-07-22 07:27:56 +0000 | [diff] [blame] | 165 | #endif |
danielk1977 | 238746a | 2009-03-19 18:51:06 +0000 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | /* |
drh | 984bfaa | 2008-03-19 16:08:53 +0000 | [diff] [blame] | 169 | ** Routines for testing the sqlite3_get_auxdata() and sqlite3_set_auxdata() |
| 170 | ** interface. |
| 171 | ** |
| 172 | ** The test_auxdata() SQL function attempts to register each of its arguments |
| 173 | ** as auxiliary data. If there are no prior registrations of aux data for |
| 174 | ** that argument (meaning the argument is not a constant or this is its first |
| 175 | ** call) then the result for that argument is 0. If there is a prior |
| 176 | ** registration, the result for that argument is 1. The overall result |
| 177 | ** is the individual argument results separated by spaces. |
| 178 | */ |
| 179 | static void free_test_auxdata(void *p) {sqlite3_free(p);} |
| 180 | static void test_auxdata( |
| 181 | sqlite3_context *pCtx, |
| 182 | int nArg, |
| 183 | sqlite3_value **argv |
| 184 | ){ |
| 185 | int i; |
| 186 | char *zRet = testContextMalloc(pCtx, nArg*2); |
| 187 | if( !zRet ) return; |
| 188 | memset(zRet, 0, nArg*2); |
| 189 | for(i=0; i<nArg; i++){ |
| 190 | char const *z = (char*)sqlite3_value_text(argv[i]); |
| 191 | if( z ){ |
| 192 | int n; |
| 193 | char *zAux = sqlite3_get_auxdata(pCtx, i); |
| 194 | if( zAux ){ |
| 195 | zRet[i*2] = '1'; |
| 196 | assert( strcmp(zAux,z)==0 ); |
| 197 | }else { |
| 198 | zRet[i*2] = '0'; |
| 199 | } |
| 200 | n = strlen(z) + 1; |
| 201 | zAux = testContextMalloc(pCtx, n); |
| 202 | if( zAux ){ |
| 203 | memcpy(zAux, z, n); |
| 204 | sqlite3_set_auxdata(pCtx, i, zAux, free_test_auxdata); |
| 205 | } |
| 206 | zRet[i*2+1] = ' '; |
| 207 | } |
| 208 | } |
| 209 | sqlite3_result_text(pCtx, zRet, 2*nArg-1, free_test_auxdata); |
| 210 | } |
| 211 | |
| 212 | /* |
| 213 | ** A function to test error reporting from user functions. This function |
drh | 00e087b | 2008-04-10 17:14:07 +0000 | [diff] [blame] | 214 | ** returns a copy of its first argument as the error message. If the |
| 215 | ** second argument exists, it becomes the error code. |
drh | 984bfaa | 2008-03-19 16:08:53 +0000 | [diff] [blame] | 216 | */ |
| 217 | static void test_error( |
| 218 | sqlite3_context *pCtx, |
| 219 | int nArg, |
| 220 | sqlite3_value **argv |
| 221 | ){ |
drh | 00e087b | 2008-04-10 17:14:07 +0000 | [diff] [blame] | 222 | sqlite3_result_error(pCtx, (char*)sqlite3_value_text(argv[0]), -1); |
| 223 | if( nArg==2 ){ |
| 224 | sqlite3_result_error_code(pCtx, sqlite3_value_int(argv[1])); |
| 225 | } |
drh | 984bfaa | 2008-03-19 16:08:53 +0000 | [diff] [blame] | 226 | } |
| 227 | |
drh | 191b54c | 2008-04-15 12:14:21 +0000 | [diff] [blame] | 228 | /* |
drh | 1a4e316 | 2008-08-26 14:42:14 +0000 | [diff] [blame] | 229 | ** Implementation of the counter(X) function. If X is an integer |
| 230 | ** constant, then the first invocation will return X. The second X+1. |
| 231 | ** and so forth. Can be used (for example) to provide a sequence number |
| 232 | ** in a result set. |
| 233 | */ |
| 234 | static void counterFunc( |
| 235 | sqlite3_context *pCtx, /* Function context */ |
| 236 | int nArg, /* Number of function arguments */ |
| 237 | sqlite3_value **argv /* Values for all function arguments */ |
| 238 | ){ |
drh | a85f7e3 | 2008-08-28 02:26:07 +0000 | [diff] [blame] | 239 | int *pCounter = (int*)sqlite3_get_auxdata(pCtx, 0); |
drh | 1a4e316 | 2008-08-26 14:42:14 +0000 | [diff] [blame] | 240 | if( pCounter==0 ){ |
| 241 | pCounter = sqlite3_malloc( sizeof(*pCounter) ); |
| 242 | if( pCounter==0 ){ |
| 243 | sqlite3_result_error_nomem(pCtx); |
| 244 | return; |
| 245 | } |
| 246 | *pCounter = sqlite3_value_int(argv[0]); |
| 247 | sqlite3_set_auxdata(pCtx, 0, pCounter, sqlite3_free); |
| 248 | }else{ |
| 249 | ++*pCounter; |
| 250 | } |
| 251 | sqlite3_result_int(pCtx, *pCounter); |
| 252 | } |
| 253 | |
| 254 | |
| 255 | /* |
drh | 191b54c | 2008-04-15 12:14:21 +0000 | [diff] [blame] | 256 | ** This function takes two arguments. It performance UTF-8/16 type |
| 257 | ** conversions on the first argument then returns a copy of the second |
| 258 | ** argument. |
| 259 | ** |
| 260 | ** This function is used in cases such as the following: |
| 261 | ** |
| 262 | ** SELECT test_isolation(x,x) FROM t1; |
| 263 | ** |
| 264 | ** We want to verify that the type conversions that occur on the |
| 265 | ** first argument do not invalidate the second argument. |
| 266 | */ |
| 267 | static void test_isolation( |
| 268 | sqlite3_context *pCtx, |
| 269 | int nArg, |
| 270 | sqlite3_value **argv |
| 271 | ){ |
| 272 | #ifndef SQLITE_OMIT_UTF16 |
| 273 | sqlite3_value_text16(argv[0]); |
| 274 | sqlite3_value_text(argv[0]); |
| 275 | sqlite3_value_text16(argv[0]); |
| 276 | sqlite3_value_text(argv[0]); |
| 277 | #endif |
| 278 | sqlite3_result_value(pCtx, argv[1]); |
| 279 | } |
| 280 | |
drh | a346058 | 2008-07-11 21:02:53 +0000 | [diff] [blame] | 281 | /* |
| 282 | ** Invoke an SQL statement recursively. The function result is the |
| 283 | ** first column of the first row of the result set. |
| 284 | */ |
| 285 | static void test_eval( |
| 286 | sqlite3_context *pCtx, |
| 287 | int nArg, |
| 288 | sqlite3_value **argv |
| 289 | ){ |
| 290 | sqlite3_stmt *pStmt; |
| 291 | int rc; |
| 292 | sqlite3 *db = sqlite3_context_db_handle(pCtx); |
| 293 | const char *zSql; |
| 294 | |
| 295 | zSql = (char*)sqlite3_value_text(argv[0]); |
| 296 | rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0); |
| 297 | if( rc==SQLITE_OK ){ |
| 298 | rc = sqlite3_step(pStmt); |
| 299 | if( rc==SQLITE_ROW ){ |
| 300 | sqlite3_result_value(pCtx, sqlite3_column_value(pStmt, 0)); |
| 301 | } |
| 302 | rc = sqlite3_finalize(pStmt); |
| 303 | } |
| 304 | if( rc ){ |
| 305 | char *zErr; |
| 306 | assert( pStmt==0 ); |
| 307 | zErr = sqlite3_mprintf("sqlite3_prepare_v2() error: %s",sqlite3_errmsg(db)); |
| 308 | sqlite3_result_text(pCtx, zErr, -1, sqlite3_free); |
| 309 | sqlite3_result_error_code(pCtx, rc); |
| 310 | } |
| 311 | } |
| 312 | |
drh | 191b54c | 2008-04-15 12:14:21 +0000 | [diff] [blame] | 313 | |
drh | 7c95b0f | 2009-10-23 18:15:46 +0000 | [diff] [blame] | 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 | */ |
dan | 93a696f | 2010-01-07 11:27:30 +0000 | [diff] [blame] | 344 | #ifndef SQLITE_OMIT_UTF16 |
drh | 7c95b0f | 2009-10-23 18:15:46 +0000 | [diff] [blame] | 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 | } |
dan | 93a696f | 2010-01-07 11:27:30 +0000 | [diff] [blame] | 364 | #endif |
drh | 7c95b0f | 2009-10-23 18:15:46 +0000 | [diff] [blame] | 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 | */ |
dan | 93a696f | 2010-01-07 11:27:30 +0000 | [diff] [blame] | 398 | #ifndef SQLITE_OMIT_UTF16 |
drh | 7c95b0f | 2009-10-23 18:15:46 +0000 | [diff] [blame] | 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 | } |
dan | 93a696f | 2010-01-07 11:27:30 +0000 | [diff] [blame] | 418 | #endif |
drh | 7c95b0f | 2009-10-23 18:15:46 +0000 | [diff] [blame] | 419 | |
drh | 984bfaa | 2008-03-19 16:08:53 +0000 | [diff] [blame] | 420 | static int registerTestFunctions(sqlite3 *db){ |
| 421 | static const struct { |
| 422 | char *zName; |
| 423 | signed char nArg; |
| 424 | unsigned char eTextRep; /* 1: UTF-16. 0: UTF-8 */ |
| 425 | void (*xFunc)(sqlite3_context*,int,sqlite3_value **); |
| 426 | } aFuncs[] = { |
| 427 | { "randstr", 2, SQLITE_UTF8, randStr }, |
| 428 | { "test_destructor", 1, SQLITE_UTF8, test_destructor}, |
shane | 2a5fc4d | 2008-07-31 01:47:11 +0000 | [diff] [blame] | 429 | #ifndef SQLITE_OMIT_UTF16 |
drh | da84ca8 | 2008-03-19 16:35:24 +0000 | [diff] [blame] | 430 | { "test_destructor16", 1, SQLITE_UTF8, test_destructor16}, |
drh | 7c95b0f | 2009-10-23 18:15:46 +0000 | [diff] [blame] | 431 | { "hex_to_utf16be", 1, SQLITE_UTF8, testHexToUtf16be}, |
| 432 | { "hex_to_utf16le", 1, SQLITE_UTF8, testHexToUtf16le}, |
shane | 2a5fc4d | 2008-07-31 01:47:11 +0000 | [diff] [blame] | 433 | #endif |
drh | 7c95b0f | 2009-10-23 18:15:46 +0000 | [diff] [blame] | 434 | { "hex_to_utf8", 1, SQLITE_UTF8, testHexToUtf8}, |
drh | 984bfaa | 2008-03-19 16:08:53 +0000 | [diff] [blame] | 435 | { "test_destructor_count", 0, SQLITE_UTF8, test_destructor_count}, |
| 436 | { "test_auxdata", -1, SQLITE_UTF8, test_auxdata}, |
| 437 | { "test_error", 1, SQLITE_UTF8, test_error}, |
drh | 00e087b | 2008-04-10 17:14:07 +0000 | [diff] [blame] | 438 | { "test_error", 2, SQLITE_UTF8, test_error}, |
drh | a346058 | 2008-07-11 21:02:53 +0000 | [diff] [blame] | 439 | { "test_eval", 1, SQLITE_UTF8, test_eval}, |
drh | 191b54c | 2008-04-15 12:14:21 +0000 | [diff] [blame] | 440 | { "test_isolation", 2, SQLITE_UTF8, test_isolation}, |
drh | 1a4e316 | 2008-08-26 14:42:14 +0000 | [diff] [blame] | 441 | { "test_counter", 1, SQLITE_UTF8, counterFunc}, |
drh | 984bfaa | 2008-03-19 16:08:53 +0000 | [diff] [blame] | 442 | }; |
| 443 | int i; |
| 444 | |
| 445 | for(i=0; i<sizeof(aFuncs)/sizeof(aFuncs[0]); i++){ |
| 446 | sqlite3_create_function(db, aFuncs[i].zName, aFuncs[i].nArg, |
| 447 | aFuncs[i].eTextRep, 0, aFuncs[i].xFunc, 0, 0); |
| 448 | } |
danielk1977 | 238746a | 2009-03-19 18:51:06 +0000 | [diff] [blame] | 449 | |
| 450 | sqlite3_create_function(db, "test_agg_errmsg16", 0, SQLITE_ANY, 0, 0, |
| 451 | test_agg_errmsg16_step, test_agg_errmsg16_final); |
| 452 | |
drh | 984bfaa | 2008-03-19 16:08:53 +0000 | [diff] [blame] | 453 | return SQLITE_OK; |
| 454 | } |
| 455 | |
| 456 | /* |
| 457 | ** TCLCMD: autoinstall_test_functions |
| 458 | ** |
| 459 | ** Invoke this TCL command to use sqlite3_auto_extension() to cause |
| 460 | ** the standard set of test functions to be loaded into each new |
| 461 | ** database connection. |
| 462 | */ |
| 463 | static int autoinstall_test_funcs( |
| 464 | void * clientData, |
| 465 | Tcl_Interp *interp, |
| 466 | int objc, |
| 467 | Tcl_Obj *CONST objv[] |
| 468 | ){ |
drh | 65aa957 | 2008-08-27 15:21:33 +0000 | [diff] [blame] | 469 | extern int Md5_Register(sqlite3*); |
drh | 701bb3b | 2008-08-02 03:50:39 +0000 | [diff] [blame] | 470 | int rc = sqlite3_auto_extension((void*)registerTestFunctions); |
drh | 65aa957 | 2008-08-27 15:21:33 +0000 | [diff] [blame] | 471 | if( rc==SQLITE_OK ){ |
| 472 | rc = sqlite3_auto_extension((void*)Md5_Register); |
| 473 | } |
drh | 701bb3b | 2008-08-02 03:50:39 +0000 | [diff] [blame] | 474 | Tcl_SetObjResult(interp, Tcl_NewIntObj(rc)); |
drh | 984bfaa | 2008-03-19 16:08:53 +0000 | [diff] [blame] | 475 | return TCL_OK; |
| 476 | } |
| 477 | |
drh | 24b58dd | 2008-07-07 14:50:14 +0000 | [diff] [blame] | 478 | /* |
| 479 | ** A bogus step function and finalizer function. |
| 480 | */ |
| 481 | static void tStep(sqlite3_context *a, int b, sqlite3_value **c){} |
| 482 | static void tFinal(sqlite3_context *a){} |
| 483 | |
| 484 | |
| 485 | /* |
| 486 | ** tclcmd: abuse_create_function |
| 487 | ** |
| 488 | ** Make various calls to sqlite3_create_function that do not have valid |
| 489 | ** parameters. Verify that the error condition is detected and reported. |
| 490 | */ |
| 491 | static int abuse_create_function( |
| 492 | void * clientData, |
| 493 | Tcl_Interp *interp, |
| 494 | int objc, |
| 495 | Tcl_Obj *CONST objv[] |
| 496 | ){ |
| 497 | extern int getDbPointer(Tcl_Interp*, const char*, sqlite3**); |
| 498 | sqlite3 *db; |
| 499 | int rc; |
| 500 | int mxArg; |
| 501 | |
| 502 | if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; |
| 503 | |
| 504 | rc = sqlite3_create_function(db, "tx", 1, SQLITE_UTF8, 0, tStep,tStep,tFinal); |
drh | dff6c17 | 2009-05-07 13:43:49 +0000 | [diff] [blame] | 505 | if( rc!=SQLITE_MISUSE ) goto abuse_err; |
drh | 24b58dd | 2008-07-07 14:50:14 +0000 | [diff] [blame] | 506 | |
| 507 | rc = sqlite3_create_function(db, "tx", 1, SQLITE_UTF8, 0, tStep, tStep, 0); |
drh | dff6c17 | 2009-05-07 13:43:49 +0000 | [diff] [blame] | 508 | if( rc!=SQLITE_MISUSE ) goto abuse_err; |
drh | 24b58dd | 2008-07-07 14:50:14 +0000 | [diff] [blame] | 509 | |
| 510 | rc = sqlite3_create_function(db, "tx", 1, SQLITE_UTF8, 0, tStep, 0, tFinal); |
drh | dff6c17 | 2009-05-07 13:43:49 +0000 | [diff] [blame] | 511 | if( rc!=SQLITE_MISUSE) goto abuse_err; |
drh | 24b58dd | 2008-07-07 14:50:14 +0000 | [diff] [blame] | 512 | |
| 513 | rc = sqlite3_create_function(db, "tx", 1, SQLITE_UTF8, 0, 0, 0, tFinal); |
drh | dff6c17 | 2009-05-07 13:43:49 +0000 | [diff] [blame] | 514 | if( rc!=SQLITE_MISUSE ) goto abuse_err; |
drh | 24b58dd | 2008-07-07 14:50:14 +0000 | [diff] [blame] | 515 | |
| 516 | rc = sqlite3_create_function(db, "tx", 1, SQLITE_UTF8, 0, 0, tStep, 0); |
drh | dff6c17 | 2009-05-07 13:43:49 +0000 | [diff] [blame] | 517 | if( rc!=SQLITE_MISUSE ) goto abuse_err; |
drh | 24b58dd | 2008-07-07 14:50:14 +0000 | [diff] [blame] | 518 | |
| 519 | rc = sqlite3_create_function(db, "tx", -2, SQLITE_UTF8, 0, tStep, 0, 0); |
drh | dff6c17 | 2009-05-07 13:43:49 +0000 | [diff] [blame] | 520 | if( rc!=SQLITE_MISUSE ) goto abuse_err; |
drh | 24b58dd | 2008-07-07 14:50:14 +0000 | [diff] [blame] | 521 | |
| 522 | rc = sqlite3_create_function(db, "tx", 128, SQLITE_UTF8, 0, tStep, 0, 0); |
drh | dff6c17 | 2009-05-07 13:43:49 +0000 | [diff] [blame] | 523 | if( rc!=SQLITE_MISUSE ) goto abuse_err; |
drh | 24b58dd | 2008-07-07 14:50:14 +0000 | [diff] [blame] | 524 | |
| 525 | rc = sqlite3_create_function(db, "funcxx" |
| 526 | "_123456789_123456789_123456789_123456789_123456789" |
| 527 | "_123456789_123456789_123456789_123456789_123456789" |
| 528 | "_123456789_123456789_123456789_123456789_123456789" |
| 529 | "_123456789_123456789_123456789_123456789_123456789" |
| 530 | "_123456789_123456789_123456789_123456789_123456789", |
| 531 | 1, SQLITE_UTF8, 0, tStep, 0, 0); |
drh | dff6c17 | 2009-05-07 13:43:49 +0000 | [diff] [blame] | 532 | if( rc!=SQLITE_MISUSE ) goto abuse_err; |
drh | 24b58dd | 2008-07-07 14:50:14 +0000 | [diff] [blame] | 533 | |
| 534 | /* This last function registration should actually work. Generate |
| 535 | ** a no-op function (that always returns NULL) and which has the |
| 536 | ** maximum-length function name and the maximum number of parameters. |
| 537 | */ |
| 538 | sqlite3_limit(db, SQLITE_LIMIT_FUNCTION_ARG, 10000); |
| 539 | mxArg = sqlite3_limit(db, SQLITE_LIMIT_FUNCTION_ARG, -1); |
| 540 | rc = sqlite3_create_function(db, "nullx" |
| 541 | "_123456789_123456789_123456789_123456789_123456789" |
| 542 | "_123456789_123456789_123456789_123456789_123456789" |
| 543 | "_123456789_123456789_123456789_123456789_123456789" |
| 544 | "_123456789_123456789_123456789_123456789_123456789" |
| 545 | "_123456789_123456789_123456789_123456789_123456789", |
| 546 | mxArg, SQLITE_UTF8, 0, tStep, 0, 0); |
| 547 | if( rc!=SQLITE_OK ) goto abuse_err; |
| 548 | |
| 549 | return TCL_OK; |
| 550 | |
| 551 | abuse_err: |
| 552 | Tcl_AppendResult(interp, "sqlite3_create_function abused test failed", |
| 553 | (char*)0); |
| 554 | return TCL_ERROR; |
| 555 | } |
| 556 | |
drh | 984bfaa | 2008-03-19 16:08:53 +0000 | [diff] [blame] | 557 | /* |
| 558 | ** Register commands with the TCL interpreter. |
| 559 | */ |
| 560 | int Sqlitetest_func_Init(Tcl_Interp *interp){ |
| 561 | static struct { |
| 562 | char *zName; |
| 563 | Tcl_ObjCmdProc *xProc; |
| 564 | } aObjCmd[] = { |
| 565 | { "autoinstall_test_functions", autoinstall_test_funcs }, |
drh | 24b58dd | 2008-07-07 14:50:14 +0000 | [diff] [blame] | 566 | { "abuse_create_function", abuse_create_function }, |
drh | 984bfaa | 2008-03-19 16:08:53 +0000 | [diff] [blame] | 567 | }; |
| 568 | int i; |
drh | 65aa957 | 2008-08-27 15:21:33 +0000 | [diff] [blame] | 569 | extern int Md5_Register(sqlite3*); |
| 570 | |
drh | 984bfaa | 2008-03-19 16:08:53 +0000 | [diff] [blame] | 571 | for(i=0; i<sizeof(aObjCmd)/sizeof(aObjCmd[0]); i++){ |
| 572 | Tcl_CreateObjCommand(interp, aObjCmd[i].zName, aObjCmd[i].xProc, 0, 0); |
| 573 | } |
danielk1977 | 00f0faf | 2008-07-08 14:17:35 +0000 | [diff] [blame] | 574 | sqlite3_initialize(); |
drh | 984bfaa | 2008-03-19 16:08:53 +0000 | [diff] [blame] | 575 | sqlite3_auto_extension((void*)registerTestFunctions); |
drh | 65aa957 | 2008-08-27 15:21:33 +0000 | [diff] [blame] | 576 | sqlite3_auto_extension((void*)Md5_Register); |
drh | 984bfaa | 2008-03-19 16:08:53 +0000 | [diff] [blame] | 577 | return TCL_OK; |
| 578 | } |