blob: 83f9730df5e28760d3c8095094963b6d79bdb8e3 [file] [log] [blame]
drh984bfaa2008-03-19 16:08:53 +00001/*
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**
shane2a5fc4d2008-07-31 01:47:11 +000015** $Id: test_func.c,v 1.9 2008/07/31 01:47:11 shane Exp $
drh984bfaa2008-03-19 16:08:53 +000016*/
17#include "sqlite3.h"
18#include "tcl.h"
19#include <stdlib.h>
20#include <string.h>
21#include <assert.h>
22
23
24/*
25** Allocate nByte bytes of space using sqlite3_malloc(). If the
26** allocation fails, call sqlite3_result_error_nomem() to notify
27** the database handle that malloc() has failed.
28*/
29static void *testContextMalloc(sqlite3_context *context, int nByte){
30 char *z = sqlite3_malloc(nByte);
31 if( !z && nByte>0 ){
32 sqlite3_result_error_nomem(context);
33 }
34 return z;
35}
36
37/*
38** This function generates a string of random characters. Used for
39** generating test data.
40*/
41static void randStr(sqlite3_context *context, int argc, sqlite3_value **argv){
42 static const unsigned char zSrc[] =
43 "abcdefghijklmnopqrstuvwxyz"
44 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
45 "0123456789"
46 ".-!,:*^+=_|?/<> ";
47 int iMin, iMax, n, r, i;
48 unsigned char zBuf[1000];
49
50 /* It used to be possible to call randstr() with any number of arguments,
51 ** but now it is registered with SQLite as requiring exactly 2.
52 */
53 assert(argc==2);
54
55 iMin = sqlite3_value_int(argv[0]);
56 if( iMin<0 ) iMin = 0;
57 if( iMin>=sizeof(zBuf) ) iMin = sizeof(zBuf)-1;
58 iMax = sqlite3_value_int(argv[1]);
59 if( iMax<iMin ) iMax = iMin;
60 if( iMax>=sizeof(zBuf) ) iMax = sizeof(zBuf)-1;
61 n = iMin;
62 if( iMax>iMin ){
63 sqlite3_randomness(sizeof(r), &r);
64 r &= 0x7fffffff;
65 n += r%(iMax + 1 - iMin);
66 }
67 assert( n<sizeof(zBuf) );
68 sqlite3_randomness(n, zBuf);
69 for(i=0; i<n; i++){
70 zBuf[i] = zSrc[zBuf[i]%(sizeof(zSrc)-1)];
71 }
72 zBuf[n] = 0;
73 sqlite3_result_text(context, (char*)zBuf, n, SQLITE_TRANSIENT);
74}
75
76/*
77** The following two SQL functions are used to test returning a text
78** result with a destructor. Function 'test_destructor' takes one argument
79** and returns the same argument interpreted as TEXT. A destructor is
80** passed with the sqlite3_result_text() call.
81**
82** SQL function 'test_destructor_count' returns the number of outstanding
83** allocations made by 'test_destructor';
84**
85** WARNING: Not threadsafe.
86*/
87static int test_destructor_count_var = 0;
88static void destructor(void *p){
89 char *zVal = (char *)p;
90 assert(zVal);
91 zVal--;
92 sqlite3_free(zVal);
93 test_destructor_count_var--;
94}
95static void test_destructor(
96 sqlite3_context *pCtx,
97 int nArg,
98 sqlite3_value **argv
99){
100 char *zVal;
101 int len;
102
103 test_destructor_count_var++;
104 assert( nArg==1 );
105 if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return;
106 len = sqlite3_value_bytes(argv[0]);
107 zVal = testContextMalloc(pCtx, len+3);
108 if( !zVal ){
109 return;
110 }
111 zVal[len+1] = 0;
112 zVal[len+2] = 0;
113 zVal++;
114 memcpy(zVal, sqlite3_value_text(argv[0]), len);
115 sqlite3_result_text(pCtx, zVal, -1, destructor);
116}
shane2a5fc4d2008-07-31 01:47:11 +0000117#ifndef SQLITE_OMIT_UTF16
drhda84ca82008-03-19 16:35:24 +0000118static void test_destructor16(
119 sqlite3_context *pCtx,
120 int nArg,
121 sqlite3_value **argv
122){
123 char *zVal;
124 int len;
125
126 test_destructor_count_var++;
127 assert( nArg==1 );
128 if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return;
129 len = sqlite3_value_bytes16(argv[0]);
130 zVal = testContextMalloc(pCtx, len+3);
131 if( !zVal ){
132 return;
133 }
134 zVal[len+1] = 0;
135 zVal[len+2] = 0;
136 zVal++;
137 memcpy(zVal, sqlite3_value_text16(argv[0]), len);
138 sqlite3_result_text16(pCtx, zVal, -1, destructor);
139}
shane2a5fc4d2008-07-31 01:47:11 +0000140#endif
drh984bfaa2008-03-19 16:08:53 +0000141static void test_destructor_count(
142 sqlite3_context *pCtx,
143 int nArg,
144 sqlite3_value **argv
145){
146 sqlite3_result_int(pCtx, test_destructor_count_var);
147}
148
149/*
150** Routines for testing the sqlite3_get_auxdata() and sqlite3_set_auxdata()
151** interface.
152**
153** The test_auxdata() SQL function attempts to register each of its arguments
154** as auxiliary data. If there are no prior registrations of aux data for
155** that argument (meaning the argument is not a constant or this is its first
156** call) then the result for that argument is 0. If there is a prior
157** registration, the result for that argument is 1. The overall result
158** is the individual argument results separated by spaces.
159*/
160static void free_test_auxdata(void *p) {sqlite3_free(p);}
161static void test_auxdata(
162 sqlite3_context *pCtx,
163 int nArg,
164 sqlite3_value **argv
165){
166 int i;
167 char *zRet = testContextMalloc(pCtx, nArg*2);
168 if( !zRet ) return;
169 memset(zRet, 0, nArg*2);
170 for(i=0; i<nArg; i++){
171 char const *z = (char*)sqlite3_value_text(argv[i]);
172 if( z ){
173 int n;
174 char *zAux = sqlite3_get_auxdata(pCtx, i);
175 if( zAux ){
176 zRet[i*2] = '1';
177 assert( strcmp(zAux,z)==0 );
178 }else {
179 zRet[i*2] = '0';
180 }
181 n = strlen(z) + 1;
182 zAux = testContextMalloc(pCtx, n);
183 if( zAux ){
184 memcpy(zAux, z, n);
185 sqlite3_set_auxdata(pCtx, i, zAux, free_test_auxdata);
186 }
187 zRet[i*2+1] = ' ';
188 }
189 }
190 sqlite3_result_text(pCtx, zRet, 2*nArg-1, free_test_auxdata);
191}
192
193/*
194** A function to test error reporting from user functions. This function
drh00e087b2008-04-10 17:14:07 +0000195** returns a copy of its first argument as the error message. If the
196** second argument exists, it becomes the error code.
drh984bfaa2008-03-19 16:08:53 +0000197*/
198static void test_error(
199 sqlite3_context *pCtx,
200 int nArg,
201 sqlite3_value **argv
202){
drh00e087b2008-04-10 17:14:07 +0000203 sqlite3_result_error(pCtx, (char*)sqlite3_value_text(argv[0]), -1);
204 if( nArg==2 ){
205 sqlite3_result_error_code(pCtx, sqlite3_value_int(argv[1]));
206 }
drh984bfaa2008-03-19 16:08:53 +0000207}
208
drh191b54c2008-04-15 12:14:21 +0000209/*
210** This function takes two arguments. It performance UTF-8/16 type
211** conversions on the first argument then returns a copy of the second
212** argument.
213**
214** This function is used in cases such as the following:
215**
216** SELECT test_isolation(x,x) FROM t1;
217**
218** We want to verify that the type conversions that occur on the
219** first argument do not invalidate the second argument.
220*/
221static void test_isolation(
222 sqlite3_context *pCtx,
223 int nArg,
224 sqlite3_value **argv
225){
226#ifndef SQLITE_OMIT_UTF16
227 sqlite3_value_text16(argv[0]);
228 sqlite3_value_text(argv[0]);
229 sqlite3_value_text16(argv[0]);
230 sqlite3_value_text(argv[0]);
231#endif
232 sqlite3_result_value(pCtx, argv[1]);
233}
234
drha3460582008-07-11 21:02:53 +0000235/*
236** Invoke an SQL statement recursively. The function result is the
237** first column of the first row of the result set.
238*/
239static void test_eval(
240 sqlite3_context *pCtx,
241 int nArg,
242 sqlite3_value **argv
243){
244 sqlite3_stmt *pStmt;
245 int rc;
246 sqlite3 *db = sqlite3_context_db_handle(pCtx);
247 const char *zSql;
248
249 zSql = (char*)sqlite3_value_text(argv[0]);
250 rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
251 if( rc==SQLITE_OK ){
252 rc = sqlite3_step(pStmt);
253 if( rc==SQLITE_ROW ){
254 sqlite3_result_value(pCtx, sqlite3_column_value(pStmt, 0));
255 }
256 rc = sqlite3_finalize(pStmt);
257 }
258 if( rc ){
259 char *zErr;
260 assert( pStmt==0 );
261 zErr = sqlite3_mprintf("sqlite3_prepare_v2() error: %s",sqlite3_errmsg(db));
262 sqlite3_result_text(pCtx, zErr, -1, sqlite3_free);
263 sqlite3_result_error_code(pCtx, rc);
264 }
265}
266
drh191b54c2008-04-15 12:14:21 +0000267
drh984bfaa2008-03-19 16:08:53 +0000268static int registerTestFunctions(sqlite3 *db){
269 static const struct {
270 char *zName;
271 signed char nArg;
272 unsigned char eTextRep; /* 1: UTF-16. 0: UTF-8 */
273 void (*xFunc)(sqlite3_context*,int,sqlite3_value **);
274 } aFuncs[] = {
275 { "randstr", 2, SQLITE_UTF8, randStr },
276 { "test_destructor", 1, SQLITE_UTF8, test_destructor},
shane2a5fc4d2008-07-31 01:47:11 +0000277#ifndef SQLITE_OMIT_UTF16
drhda84ca82008-03-19 16:35:24 +0000278 { "test_destructor16", 1, SQLITE_UTF8, test_destructor16},
shane2a5fc4d2008-07-31 01:47:11 +0000279#endif
drh984bfaa2008-03-19 16:08:53 +0000280 { "test_destructor_count", 0, SQLITE_UTF8, test_destructor_count},
281 { "test_auxdata", -1, SQLITE_UTF8, test_auxdata},
282 { "test_error", 1, SQLITE_UTF8, test_error},
drh00e087b2008-04-10 17:14:07 +0000283 { "test_error", 2, SQLITE_UTF8, test_error},
drha3460582008-07-11 21:02:53 +0000284 { "test_eval", 1, SQLITE_UTF8, test_eval},
drh191b54c2008-04-15 12:14:21 +0000285 { "test_isolation", 2, SQLITE_UTF8, test_isolation},
drh984bfaa2008-03-19 16:08:53 +0000286 };
287 int i;
drh81cc3542008-03-19 19:01:21 +0000288 extern int Md5_Register(sqlite3*);
drh984bfaa2008-03-19 16:08:53 +0000289
290 for(i=0; i<sizeof(aFuncs)/sizeof(aFuncs[0]); i++){
291 sqlite3_create_function(db, aFuncs[i].zName, aFuncs[i].nArg,
292 aFuncs[i].eTextRep, 0, aFuncs[i].xFunc, 0, 0);
293 }
drh81cc3542008-03-19 19:01:21 +0000294 Md5_Register(db);
drh984bfaa2008-03-19 16:08:53 +0000295 return SQLITE_OK;
296}
297
298/*
299** TCLCMD: autoinstall_test_functions
300**
301** Invoke this TCL command to use sqlite3_auto_extension() to cause
302** the standard set of test functions to be loaded into each new
303** database connection.
304*/
305static int autoinstall_test_funcs(
306 void * clientData,
307 Tcl_Interp *interp,
308 int objc,
309 Tcl_Obj *CONST objv[]
310){
311 sqlite3_auto_extension((void*)registerTestFunctions);
312 return TCL_OK;
313}
314
drh24b58dd2008-07-07 14:50:14 +0000315/*
316** A bogus step function and finalizer function.
317*/
318static void tStep(sqlite3_context *a, int b, sqlite3_value **c){}
319static void tFinal(sqlite3_context *a){}
320
321
322/*
323** tclcmd: abuse_create_function
324**
325** Make various calls to sqlite3_create_function that do not have valid
326** parameters. Verify that the error condition is detected and reported.
327*/
328static int abuse_create_function(
329 void * clientData,
330 Tcl_Interp *interp,
331 int objc,
332 Tcl_Obj *CONST objv[]
333){
334 extern int getDbPointer(Tcl_Interp*, const char*, sqlite3**);
335 sqlite3 *db;
336 int rc;
337 int mxArg;
338
339 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
340
341 rc = sqlite3_create_function(db, "tx", 1, SQLITE_UTF8, 0, tStep,tStep,tFinal);
342 if( rc!=SQLITE_ERROR ) goto abuse_err;
343 if( sqlite3_errcode(db)!=SQLITE_ERROR ) goto abuse_err;
344 if( strcmp(sqlite3_errmsg(db), "bad parameters")!=0 ) goto abuse_err;
345
346 rc = sqlite3_create_function(db, "tx", 1, SQLITE_UTF8, 0, tStep, tStep, 0);
347 if( rc!=SQLITE_ERROR ) goto abuse_err;
348 if( sqlite3_errcode(db)!=SQLITE_ERROR ) goto abuse_err;
349 if( strcmp(sqlite3_errmsg(db), "bad parameters")!=0 ) goto abuse_err;
350
351 rc = sqlite3_create_function(db, "tx", 1, SQLITE_UTF8, 0, tStep, 0, tFinal);
352 if( rc!=SQLITE_ERROR ) goto abuse_err;
353 if( sqlite3_errcode(db)!=SQLITE_ERROR ) goto abuse_err;
354 if( strcmp(sqlite3_errmsg(db), "bad parameters")!=0 ) goto abuse_err;
355
356 rc = sqlite3_create_function(db, "tx", 1, SQLITE_UTF8, 0, 0, 0, tFinal);
357 if( rc!=SQLITE_ERROR ) goto abuse_err;
358 if( sqlite3_errcode(db)!=SQLITE_ERROR ) goto abuse_err;
359 if( strcmp(sqlite3_errmsg(db), "bad parameters")!=0 ) goto abuse_err;
360
361 rc = sqlite3_create_function(db, "tx", 1, SQLITE_UTF8, 0, 0, tStep, 0);
362 if( rc!=SQLITE_ERROR ) goto abuse_err;
363 if( sqlite3_errcode(db)!=SQLITE_ERROR ) goto abuse_err;
364 if( strcmp(sqlite3_errmsg(db), "bad parameters")!=0 ) goto abuse_err;
365
366 rc = sqlite3_create_function(db, "tx", -2, SQLITE_UTF8, 0, tStep, 0, 0);
367 if( rc!=SQLITE_ERROR ) goto abuse_err;
368 if( sqlite3_errcode(db)!=SQLITE_ERROR ) goto abuse_err;
369 if( strcmp(sqlite3_errmsg(db), "bad parameters")!=0 ) goto abuse_err;
370
371 rc = sqlite3_create_function(db, "tx", 128, SQLITE_UTF8, 0, tStep, 0, 0);
372 if( rc!=SQLITE_ERROR ) goto abuse_err;
373 if( sqlite3_errcode(db)!=SQLITE_ERROR ) goto abuse_err;
374 if( strcmp(sqlite3_errmsg(db), "bad parameters")!=0 ) goto abuse_err;
375
376 rc = sqlite3_create_function(db, "funcxx"
377 "_123456789_123456789_123456789_123456789_123456789"
378 "_123456789_123456789_123456789_123456789_123456789"
379 "_123456789_123456789_123456789_123456789_123456789"
380 "_123456789_123456789_123456789_123456789_123456789"
381 "_123456789_123456789_123456789_123456789_123456789",
382 1, SQLITE_UTF8, 0, tStep, 0, 0);
383 if( rc!=SQLITE_ERROR ) goto abuse_err;
384 if( sqlite3_errcode(db)!=SQLITE_ERROR ) goto abuse_err;
385 if( strcmp(sqlite3_errmsg(db), "bad parameters")!=0 ) goto abuse_err;
386
387 /* This last function registration should actually work. Generate
388 ** a no-op function (that always returns NULL) and which has the
389 ** maximum-length function name and the maximum number of parameters.
390 */
391 sqlite3_limit(db, SQLITE_LIMIT_FUNCTION_ARG, 10000);
392 mxArg = sqlite3_limit(db, SQLITE_LIMIT_FUNCTION_ARG, -1);
393 rc = sqlite3_create_function(db, "nullx"
394 "_123456789_123456789_123456789_123456789_123456789"
395 "_123456789_123456789_123456789_123456789_123456789"
396 "_123456789_123456789_123456789_123456789_123456789"
397 "_123456789_123456789_123456789_123456789_123456789"
398 "_123456789_123456789_123456789_123456789_123456789",
399 mxArg, SQLITE_UTF8, 0, tStep, 0, 0);
400 if( rc!=SQLITE_OK ) goto abuse_err;
401
402 return TCL_OK;
403
404abuse_err:
405 Tcl_AppendResult(interp, "sqlite3_create_function abused test failed",
406 (char*)0);
407 return TCL_ERROR;
408}
409
drh984bfaa2008-03-19 16:08:53 +0000410
411
412/*
413** Register commands with the TCL interpreter.
414*/
415int Sqlitetest_func_Init(Tcl_Interp *interp){
416 static struct {
417 char *zName;
418 Tcl_ObjCmdProc *xProc;
419 } aObjCmd[] = {
420 { "autoinstall_test_functions", autoinstall_test_funcs },
drh24b58dd2008-07-07 14:50:14 +0000421 { "abuse_create_function", abuse_create_function },
drh984bfaa2008-03-19 16:08:53 +0000422 };
423 int i;
424 for(i=0; i<sizeof(aObjCmd)/sizeof(aObjCmd[0]); i++){
425 Tcl_CreateObjCommand(interp, aObjCmd[i].zName, aObjCmd[i].xProc, 0, 0);
426 }
danielk197700f0faf2008-07-08 14:17:35 +0000427 sqlite3_initialize();
drh984bfaa2008-03-19 16:08:53 +0000428 sqlite3_auto_extension((void*)registerTestFunctions);
429 return TCL_OK;
430}