test_func.c 21 KB

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