OLD | NEW |
(Empty) | |
| 1 /* |
| 2 ** 2017-02-07 |
| 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 ** |
| 13 ** This program implements an SQLite database self-verification utility. |
| 14 ** Usage: |
| 15 ** |
| 16 ** dbselftest DATABASE ... |
| 17 ** |
| 18 ** This program reads the "selftest" table in DATABASE, in rowid order, |
| 19 ** and runs each of the tests described there, reporting results at the |
| 20 ** end. |
| 21 ** |
| 22 ** The intent of this program is to have a set of test database files that |
| 23 ** can be run using future versions of SQLite in order to verify that |
| 24 ** legacy database files continue to be readable. In other words, the |
| 25 ** intent is to confirm that there have been no breaking changes in the |
| 26 ** file format. The program can also be used to verify that database files |
| 27 ** are fully compatible between different architectures. |
| 28 ** |
| 29 ** The selftest table looks like this: |
| 30 ** |
| 31 ** CREATE TABLE selftest ( |
| 32 ** id INTEGER PRIMARY KEY, -- Run tests in ascending order |
| 33 ** op TEXT, -- "test", "regexp", "print", etc. |
| 34 ** cmdtxt TEXT, -- Usually the SQL to be run |
| 35 ** expected TEXT -- Expected results |
| 36 ** ); |
| 37 ** |
| 38 */ |
| 39 #include <assert.h> |
| 40 #include <string.h> |
| 41 #include <stdarg.h> |
| 42 #include <stdio.h> |
| 43 #include <stdlib.h> |
| 44 #include "sqlite3.h" |
| 45 |
| 46 static const char zHelp[] = |
| 47 "Usage: dbselftest [OPTIONS] DBFILE ...\n" |
| 48 "\n" |
| 49 " --init Create the selftest table\n" |
| 50 " -q Suppress most output. Errors only\n" |
| 51 " -v Show extra output\n" |
| 52 ; |
| 53 |
| 54 |
| 55 /****************************************************************************** |
| 56 ** The following code from ext/misc/sha1.c |
| 57 ** |
| 58 ** Context for the SHA1 hash |
| 59 */ |
| 60 typedef struct SHA1Context SHA1Context; |
| 61 struct SHA1Context { |
| 62 unsigned int state[5]; |
| 63 unsigned int count[2]; |
| 64 unsigned char buffer[64]; |
| 65 }; |
| 66 |
| 67 |
| 68 #if __GNUC__ && (defined(__i386__) || defined(__x86_64__)) |
| 69 /* |
| 70 * GCC by itself only generates left rotates. Use right rotates if |
| 71 * possible to be kinder to dinky implementations with iterative rotate |
| 72 * instructions. |
| 73 */ |
| 74 #define SHA_ROT(op, x, k) \ |
| 75 ({ unsigned int y; asm(op " %1,%0" : "=r" (y) : "I" (k), "0" (x)); y; }) |
| 76 #define rol(x,k) SHA_ROT("roll", x, k) |
| 77 #define ror(x,k) SHA_ROT("rorl", x, k) |
| 78 |
| 79 #else |
| 80 /* Generic C equivalent */ |
| 81 #define SHA_ROT(x,l,r) ((x) << (l) | (x) >> (r)) |
| 82 #define rol(x,k) SHA_ROT(x,k,32-(k)) |
| 83 #define ror(x,k) SHA_ROT(x,32-(k),k) |
| 84 #endif |
| 85 |
| 86 |
| 87 #define blk0le(i) (block[i] = (ror(block[i],8)&0xFF00FF00) \ |
| 88 |(rol(block[i],8)&0x00FF00FF)) |
| 89 #define blk0be(i) block[i] |
| 90 #define blk(i) (block[i&15] = rol(block[(i+13)&15]^block[(i+8)&15] \ |
| 91 ^block[(i+2)&15]^block[i&15],1)) |
| 92 |
| 93 /* |
| 94 * (R0+R1), R2, R3, R4 are the different operations (rounds) used in SHA1 |
| 95 * |
| 96 * Rl0() for little-endian and Rb0() for big-endian. Endianness is |
| 97 * determined at run-time. |
| 98 */ |
| 99 #define Rl0(v,w,x,y,z,i) \ |
| 100 z+=((w&(x^y))^y)+blk0le(i)+0x5A827999+rol(v,5);w=ror(w,2); |
| 101 #define Rb0(v,w,x,y,z,i) \ |
| 102 z+=((w&(x^y))^y)+blk0be(i)+0x5A827999+rol(v,5);w=ror(w,2); |
| 103 #define R1(v,w,x,y,z,i) \ |
| 104 z+=((w&(x^y))^y)+blk(i)+0x5A827999+rol(v,5);w=ror(w,2); |
| 105 #define R2(v,w,x,y,z,i) \ |
| 106 z+=(w^x^y)+blk(i)+0x6ED9EBA1+rol(v,5);w=ror(w,2); |
| 107 #define R3(v,w,x,y,z,i) \ |
| 108 z+=(((w|x)&y)|(w&x))+blk(i)+0x8F1BBCDC+rol(v,5);w=ror(w,2); |
| 109 #define R4(v,w,x,y,z,i) \ |
| 110 z+=(w^x^y)+blk(i)+0xCA62C1D6+rol(v,5);w=ror(w,2); |
| 111 |
| 112 /* |
| 113 * Hash a single 512-bit block. This is the core of the algorithm. |
| 114 */ |
| 115 void SHA1Transform(unsigned int state[5], const unsigned char buffer[64]){ |
| 116 unsigned int qq[5]; /* a, b, c, d, e; */ |
| 117 static int one = 1; |
| 118 unsigned int block[16]; |
| 119 memcpy(block, buffer, 64); |
| 120 memcpy(qq,state,5*sizeof(unsigned int)); |
| 121 |
| 122 #define a qq[0] |
| 123 #define b qq[1] |
| 124 #define c qq[2] |
| 125 #define d qq[3] |
| 126 #define e qq[4] |
| 127 |
| 128 /* Copy p->state[] to working vars */ |
| 129 /* |
| 130 a = state[0]; |
| 131 b = state[1]; |
| 132 c = state[2]; |
| 133 d = state[3]; |
| 134 e = state[4]; |
| 135 */ |
| 136 |
| 137 /* 4 rounds of 20 operations each. Loop unrolled. */ |
| 138 if( 1 == *(unsigned char*)&one ){ |
| 139 Rl0(a,b,c,d,e, 0); Rl0(e,a,b,c,d, 1); Rl0(d,e,a,b,c, 2); Rl0(c,d,e,a,b, 3); |
| 140 Rl0(b,c,d,e,a, 4); Rl0(a,b,c,d,e, 5); Rl0(e,a,b,c,d, 6); Rl0(d,e,a,b,c, 7); |
| 141 Rl0(c,d,e,a,b, 8); Rl0(b,c,d,e,a, 9); Rl0(a,b,c,d,e,10); Rl0(e,a,b,c,d,11); |
| 142 Rl0(d,e,a,b,c,12); Rl0(c,d,e,a,b,13); Rl0(b,c,d,e,a,14); Rl0(a,b,c,d,e,15); |
| 143 }else{ |
| 144 Rb0(a,b,c,d,e, 0); Rb0(e,a,b,c,d, 1); Rb0(d,e,a,b,c, 2); Rb0(c,d,e,a,b, 3); |
| 145 Rb0(b,c,d,e,a, 4); Rb0(a,b,c,d,e, 5); Rb0(e,a,b,c,d, 6); Rb0(d,e,a,b,c, 7); |
| 146 Rb0(c,d,e,a,b, 8); Rb0(b,c,d,e,a, 9); Rb0(a,b,c,d,e,10); Rb0(e,a,b,c,d,11); |
| 147 Rb0(d,e,a,b,c,12); Rb0(c,d,e,a,b,13); Rb0(b,c,d,e,a,14); Rb0(a,b,c,d,e,15); |
| 148 } |
| 149 R1(e,a,b,c,d,16); R1(d,e,a,b,c,17); R1(c,d,e,a,b,18); R1(b,c,d,e,a,19); |
| 150 R2(a,b,c,d,e,20); R2(e,a,b,c,d,21); R2(d,e,a,b,c,22); R2(c,d,e,a,b,23); |
| 151 R2(b,c,d,e,a,24); R2(a,b,c,d,e,25); R2(e,a,b,c,d,26); R2(d,e,a,b,c,27); |
| 152 R2(c,d,e,a,b,28); R2(b,c,d,e,a,29); R2(a,b,c,d,e,30); R2(e,a,b,c,d,31); |
| 153 R2(d,e,a,b,c,32); R2(c,d,e,a,b,33); R2(b,c,d,e,a,34); R2(a,b,c,d,e,35); |
| 154 R2(e,a,b,c,d,36); R2(d,e,a,b,c,37); R2(c,d,e,a,b,38); R2(b,c,d,e,a,39); |
| 155 R3(a,b,c,d,e,40); R3(e,a,b,c,d,41); R3(d,e,a,b,c,42); R3(c,d,e,a,b,43); |
| 156 R3(b,c,d,e,a,44); R3(a,b,c,d,e,45); R3(e,a,b,c,d,46); R3(d,e,a,b,c,47); |
| 157 R3(c,d,e,a,b,48); R3(b,c,d,e,a,49); R3(a,b,c,d,e,50); R3(e,a,b,c,d,51); |
| 158 R3(d,e,a,b,c,52); R3(c,d,e,a,b,53); R3(b,c,d,e,a,54); R3(a,b,c,d,e,55); |
| 159 R3(e,a,b,c,d,56); R3(d,e,a,b,c,57); R3(c,d,e,a,b,58); R3(b,c,d,e,a,59); |
| 160 R4(a,b,c,d,e,60); R4(e,a,b,c,d,61); R4(d,e,a,b,c,62); R4(c,d,e,a,b,63); |
| 161 R4(b,c,d,e,a,64); R4(a,b,c,d,e,65); R4(e,a,b,c,d,66); R4(d,e,a,b,c,67); |
| 162 R4(c,d,e,a,b,68); R4(b,c,d,e,a,69); R4(a,b,c,d,e,70); R4(e,a,b,c,d,71); |
| 163 R4(d,e,a,b,c,72); R4(c,d,e,a,b,73); R4(b,c,d,e,a,74); R4(a,b,c,d,e,75); |
| 164 R4(e,a,b,c,d,76); R4(d,e,a,b,c,77); R4(c,d,e,a,b,78); R4(b,c,d,e,a,79); |
| 165 |
| 166 /* Add the working vars back into context.state[] */ |
| 167 state[0] += a; |
| 168 state[1] += b; |
| 169 state[2] += c; |
| 170 state[3] += d; |
| 171 state[4] += e; |
| 172 |
| 173 #undef a |
| 174 #undef b |
| 175 #undef c |
| 176 #undef d |
| 177 #undef e |
| 178 } |
| 179 |
| 180 |
| 181 /* Initialize a SHA1 context */ |
| 182 static void hash_init(SHA1Context *p){ |
| 183 /* SHA1 initialization constants */ |
| 184 p->state[0] = 0x67452301; |
| 185 p->state[1] = 0xEFCDAB89; |
| 186 p->state[2] = 0x98BADCFE; |
| 187 p->state[3] = 0x10325476; |
| 188 p->state[4] = 0xC3D2E1F0; |
| 189 p->count[0] = p->count[1] = 0; |
| 190 } |
| 191 |
| 192 /* Add new content to the SHA1 hash */ |
| 193 static void hash_step( |
| 194 SHA1Context *p, /* Add content to this context */ |
| 195 const unsigned char *data, /* Data to be added */ |
| 196 unsigned int len /* Number of bytes in data */ |
| 197 ){ |
| 198 unsigned int i, j; |
| 199 |
| 200 j = p->count[0]; |
| 201 if( (p->count[0] += len << 3) < j ){ |
| 202 p->count[1] += (len>>29)+1; |
| 203 } |
| 204 j = (j >> 3) & 63; |
| 205 if( (j + len) > 63 ){ |
| 206 (void)memcpy(&p->buffer[j], data, (i = 64-j)); |
| 207 SHA1Transform(p->state, p->buffer); |
| 208 for(; i + 63 < len; i += 64){ |
| 209 SHA1Transform(p->state, &data[i]); |
| 210 } |
| 211 j = 0; |
| 212 }else{ |
| 213 i = 0; |
| 214 } |
| 215 (void)memcpy(&p->buffer[j], &data[i], len - i); |
| 216 } |
| 217 |
| 218 /* Compute a string using sqlite3_vsnprintf() and hash it */ |
| 219 static void hash_step_vformat( |
| 220 SHA1Context *p, /* Add content to this context */ |
| 221 const char *zFormat, |
| 222 ... |
| 223 ){ |
| 224 va_list ap; |
| 225 int n; |
| 226 char zBuf[50]; |
| 227 va_start(ap, zFormat); |
| 228 sqlite3_vsnprintf(sizeof(zBuf),zBuf,zFormat,ap); |
| 229 va_end(ap); |
| 230 n = (int)strlen(zBuf); |
| 231 hash_step(p, (unsigned char*)zBuf, n); |
| 232 } |
| 233 |
| 234 |
| 235 /* Add padding and compute the message digest. Render the |
| 236 ** message digest as lower-case hexadecimal and put it into |
| 237 ** zOut[]. zOut[] must be at least 41 bytes long. */ |
| 238 static void hash_finish( |
| 239 SHA1Context *p, /* The SHA1 context to finish and render */ |
| 240 char *zOut /* Store hexadecimal hash here */ |
| 241 ){ |
| 242 unsigned int i; |
| 243 unsigned char finalcount[8]; |
| 244 unsigned char digest[20]; |
| 245 static const char zEncode[] = "0123456789abcdef"; |
| 246 |
| 247 for (i = 0; i < 8; i++){ |
| 248 finalcount[i] = (unsigned char)((p->count[(i >= 4 ? 0 : 1)] |
| 249 >> ((3-(i & 3)) * 8) ) & 255); /* Endian independent */ |
| 250 } |
| 251 hash_step(p, (const unsigned char *)"\200", 1); |
| 252 while ((p->count[0] & 504) != 448){ |
| 253 hash_step(p, (const unsigned char *)"\0", 1); |
| 254 } |
| 255 hash_step(p, finalcount, 8); /* Should cause a SHA1Transform() */ |
| 256 for (i = 0; i < 20; i++){ |
| 257 digest[i] = (unsigned char)((p->state[i>>2] >> ((3-(i & 3)) * 8) ) & 255); |
| 258 } |
| 259 for(i=0; i<20; i++){ |
| 260 zOut[i*2] = zEncode[(digest[i]>>4)&0xf]; |
| 261 zOut[i*2+1] = zEncode[digest[i] & 0xf]; |
| 262 } |
| 263 zOut[i*2]= 0; |
| 264 } |
| 265 |
| 266 /* |
| 267 ** Implementation of the sha1(X) function. |
| 268 ** |
| 269 ** Return a lower-case hexadecimal rendering of the SHA1 hash of the |
| 270 ** argument X. If X is a BLOB, it is hashed as is. For all other |
| 271 ** types of input, X is converted into a UTF-8 string and the string |
| 272 ** is hash without the trailing 0x00 terminator. The hash of a NULL |
| 273 ** value is NULL. |
| 274 */ |
| 275 static void sha1Func( |
| 276 sqlite3_context *context, |
| 277 int argc, |
| 278 sqlite3_value **argv |
| 279 ){ |
| 280 SHA1Context cx; |
| 281 int eType = sqlite3_value_type(argv[0]); |
| 282 int nByte = sqlite3_value_bytes(argv[0]); |
| 283 char zOut[44]; |
| 284 |
| 285 assert( argc==1 ); |
| 286 if( eType==SQLITE_NULL ) return; |
| 287 hash_init(&cx); |
| 288 if( eType==SQLITE_BLOB ){ |
| 289 hash_step(&cx, sqlite3_value_blob(argv[0]), nByte); |
| 290 }else{ |
| 291 hash_step(&cx, sqlite3_value_text(argv[0]), nByte); |
| 292 } |
| 293 hash_finish(&cx, zOut); |
| 294 sqlite3_result_text(context, zOut, 40, SQLITE_TRANSIENT); |
| 295 } |
| 296 |
| 297 /* |
| 298 ** Run a prepared statement and compute the SHA1 hash on the |
| 299 ** result rows. |
| 300 */ |
| 301 static void sha1RunStatement(SHA1Context *pCtx, sqlite3_stmt *pStmt){ |
| 302 int nCol = sqlite3_column_count(pStmt); |
| 303 const char *z = sqlite3_sql(pStmt); |
| 304 int n = (int)strlen(z); |
| 305 |
| 306 hash_step_vformat(pCtx,"S%d:",n); |
| 307 hash_step(pCtx,(unsigned char*)z,n); |
| 308 |
| 309 /* Compute a hash over the result of the query */ |
| 310 while( SQLITE_ROW==sqlite3_step(pStmt) ){ |
| 311 int i; |
| 312 hash_step(pCtx,(const unsigned char*)"R",1); |
| 313 for(i=0; i<nCol; i++){ |
| 314 switch( sqlite3_column_type(pStmt,i) ){ |
| 315 case SQLITE_NULL: { |
| 316 hash_step(pCtx, (const unsigned char*)"N",1); |
| 317 break; |
| 318 } |
| 319 case SQLITE_INTEGER: { |
| 320 sqlite3_uint64 u; |
| 321 int j; |
| 322 unsigned char x[9]; |
| 323 sqlite3_int64 v = sqlite3_column_int64(pStmt,i); |
| 324 memcpy(&u, &v, 8); |
| 325 for(j=8; j>=1; j--){ |
| 326 x[j] = u & 0xff; |
| 327 u >>= 8; |
| 328 } |
| 329 x[0] = 'I'; |
| 330 hash_step(pCtx, x, 9); |
| 331 break; |
| 332 } |
| 333 case SQLITE_FLOAT: { |
| 334 sqlite3_uint64 u; |
| 335 int j; |
| 336 unsigned char x[9]; |
| 337 double r = sqlite3_column_double(pStmt,i); |
| 338 memcpy(&u, &r, 8); |
| 339 for(j=8; j>=1; j--){ |
| 340 x[j] = u & 0xff; |
| 341 u >>= 8; |
| 342 } |
| 343 x[0] = 'F'; |
| 344 hash_step(pCtx,x,9); |
| 345 break; |
| 346 } |
| 347 case SQLITE_TEXT: { |
| 348 int n2 = sqlite3_column_bytes(pStmt, i); |
| 349 const unsigned char *z2 = sqlite3_column_text(pStmt, i); |
| 350 hash_step_vformat(pCtx,"T%d:",n2); |
| 351 hash_step(pCtx, z2, n2); |
| 352 break; |
| 353 } |
| 354 case SQLITE_BLOB: { |
| 355 int n2 = sqlite3_column_bytes(pStmt, i); |
| 356 const unsigned char *z2 = sqlite3_column_blob(pStmt, i); |
| 357 hash_step_vformat(pCtx,"B%d:",n2); |
| 358 hash_step(pCtx, z2, n2); |
| 359 break; |
| 360 } |
| 361 } |
| 362 } |
| 363 } |
| 364 } |
| 365 |
| 366 /* |
| 367 ** Run one or more statements of SQL. Compute a SHA1 hash of the output. |
| 368 */ |
| 369 static int sha1Exec( |
| 370 sqlite3 *db, /* Run against this database connection */ |
| 371 const char *zSql, /* The SQL to be run */ |
| 372 char *zOut /* Store the SHA1 hash as hexadecimal in this buffer */ |
| 373 ){ |
| 374 sqlite3_stmt *pStmt = 0; /* A prepared statement */ |
| 375 int rc; /* Result of an API call */ |
| 376 SHA1Context cx; /* The SHA1 hash context */ |
| 377 |
| 378 hash_init(&cx); |
| 379 while( zSql[0] ){ |
| 380 rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, &zSql); |
| 381 if( rc ){ |
| 382 sqlite3_finalize(pStmt); |
| 383 return rc; |
| 384 } |
| 385 sha1RunStatement(&cx, pStmt); |
| 386 sqlite3_finalize(pStmt); |
| 387 } |
| 388 hash_finish(&cx, zOut); |
| 389 return SQLITE_OK; |
| 390 } |
| 391 |
| 392 /* |
| 393 ** Implementation of the sha1_query(SQL) function. |
| 394 ** |
| 395 ** This function compiles and runs the SQL statement(s) given in the |
| 396 ** argument. The results are hashed using SHA1 and that hash is returned. |
| 397 ** |
| 398 ** The original SQL text is included as part of the hash. |
| 399 ** |
| 400 ** The hash is not just a concatenation of the outputs. Each query |
| 401 ** is delimited and each row and value within the query is delimited, |
| 402 ** with all values being marked with their datatypes. |
| 403 */ |
| 404 static void sha1QueryFunc( |
| 405 sqlite3_context *context, |
| 406 int argc, |
| 407 sqlite3_value **argv |
| 408 ){ |
| 409 sqlite3 *db = sqlite3_context_db_handle(context); |
| 410 const char *zSql = (const char*)sqlite3_value_text(argv[0]); |
| 411 sqlite3_stmt *pStmt = 0; |
| 412 int rc; |
| 413 SHA1Context cx; |
| 414 char zOut[44]; |
| 415 |
| 416 assert( argc==1 ); |
| 417 if( zSql==0 ) return; |
| 418 hash_init(&cx); |
| 419 while( zSql[0] ){ |
| 420 rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, &zSql); |
| 421 if( rc ){ |
| 422 char *zMsg = sqlite3_mprintf("error SQL statement [%s]: %s", |
| 423 zSql, sqlite3_errmsg(db)); |
| 424 sqlite3_finalize(pStmt); |
| 425 sqlite3_result_error(context, zMsg, -1); |
| 426 sqlite3_free(zMsg); |
| 427 return; |
| 428 } |
| 429 if( !sqlite3_stmt_readonly(pStmt) ){ |
| 430 char *zMsg = sqlite3_mprintf("non-query: [%s]", sqlite3_sql(pStmt)); |
| 431 sqlite3_finalize(pStmt); |
| 432 sqlite3_result_error(context, zMsg, -1); |
| 433 sqlite3_free(zMsg); |
| 434 return; |
| 435 } |
| 436 sha1RunStatement(&cx, pStmt); |
| 437 sqlite3_finalize(pStmt); |
| 438 } |
| 439 hash_finish(&cx, zOut); |
| 440 sqlite3_result_text(context, zOut, 40, SQLITE_TRANSIENT); |
| 441 } |
| 442 /* End of ext/misc/sha1.c |
| 443 ******************************************************************************/ |
| 444 |
| 445 /* How much output to display */ |
| 446 #define VOLUME_MIN 0 |
| 447 #define VOLUME_OFF 0 |
| 448 #define VOLUME_ERROR_ONLY 1 |
| 449 #define VOLUME_LOW 2 |
| 450 #define VOLUME_ECHO 3 |
| 451 #define VOLUME_VERBOSE 4 |
| 452 #define VOLUME_MAX 4 |
| 453 |
| 454 /* A string accumulator |
| 455 */ |
| 456 typedef struct Str { |
| 457 char *z; /* Accumulated text */ |
| 458 int n; /* Bytes of z[] used so far */ |
| 459 int nAlloc; /* Bytes allocated for z[] */ |
| 460 } Str; |
| 461 |
| 462 /* Append text to the Str object |
| 463 */ |
| 464 static void strAppend(Str *p, const char *z){ |
| 465 int n = (int)strlen(z); |
| 466 if( p->n+n >= p->nAlloc ){ |
| 467 p->nAlloc += p->n+n + 100; |
| 468 p->z = sqlite3_realloc(p->z, p->nAlloc); |
| 469 if( z==0 ){ |
| 470 printf("Could not allocate %d bytes\n", p->nAlloc); |
| 471 exit(1); |
| 472 } |
| 473 } |
| 474 memcpy(p->z+p->n, z, n+1); |
| 475 p->n += n; |
| 476 } |
| 477 |
| 478 /* This is an sqlite3_exec() callback that will capture all |
| 479 ** output in a Str. |
| 480 ** |
| 481 ** Columns are separated by ",". Rows are separated by "|". |
| 482 */ |
| 483 static int execCallback(void *pStr, int argc, char **argv, char **colv){ |
| 484 int i; |
| 485 Str *p = (Str*)pStr; |
| 486 if( p->n ) strAppend(p, "|"); |
| 487 for(i=0; i<argc; i++){ |
| 488 const char *z = (const char*)argv[i]; |
| 489 if( z==0 ) z = "NULL"; |
| 490 if( i>0 ) strAppend(p, ","); |
| 491 strAppend(p, z); |
| 492 } |
| 493 return 0; |
| 494 } |
| 495 |
| 496 /* |
| 497 ** Run an SQL statement constructing using sqlite3_vmprintf(). |
| 498 ** Return the number of errors. |
| 499 */ |
| 500 static int runSql(sqlite3 *db, const char *zFormat, ...){ |
| 501 char *zSql; |
| 502 char *zErr = 0; |
| 503 int rc; |
| 504 int nErr = 0; |
| 505 va_list ap; |
| 506 |
| 507 va_start(ap, zFormat); |
| 508 zSql = sqlite3_vmprintf(zFormat, ap); |
| 509 va_end(ap); |
| 510 if( zSql==0 ){ |
| 511 printf("Out of memory\n"); |
| 512 exit(1); |
| 513 } |
| 514 rc = sqlite3_exec(db, zSql, 0, 0, &zErr); |
| 515 if( rc || zErr ){ |
| 516 printf("SQL error in [%s]: code=%d: %s\n", zSql, rc, zErr); |
| 517 nErr++; |
| 518 } |
| 519 sqlite3_free(zSql); |
| 520 return nErr; |
| 521 } |
| 522 |
| 523 /* |
| 524 ** Generate a prepared statement using a formatted string. |
| 525 */ |
| 526 static sqlite3_stmt *prepareSql(sqlite3 *db, const char *zFormat, ...){ |
| 527 char *zSql; |
| 528 int rc; |
| 529 sqlite3_stmt *pStmt = 0; |
| 530 va_list ap; |
| 531 |
| 532 va_start(ap, zFormat); |
| 533 zSql = sqlite3_vmprintf(zFormat, ap); |
| 534 va_end(ap); |
| 535 if( zSql==0 ){ |
| 536 printf("Out of memory\n"); |
| 537 exit(1); |
| 538 } |
| 539 rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0); |
| 540 if( rc ){ |
| 541 printf("SQL error in [%s]: code=%d: %s\n", zSql, rc, sqlite3_errmsg(db)); |
| 542 sqlite3_finalize(pStmt); |
| 543 pStmt = 0; |
| 544 } |
| 545 sqlite3_free(zSql); |
| 546 return pStmt; |
| 547 } |
| 548 |
| 549 /* |
| 550 ** Construct the standard selftest configuration for the database. |
| 551 */ |
| 552 static int buildSelftestTable(sqlite3 *db){ |
| 553 int rc; |
| 554 sqlite3_stmt *pStmt; |
| 555 int tno = 110; |
| 556 char *zSql; |
| 557 char zHash[50]; |
| 558 |
| 559 rc = runSql(db, |
| 560 "CREATE TABLE IF NOT EXISTS selftest(\n" |
| 561 " tno INTEGER PRIMARY KEY, -- test number\n" |
| 562 " op TEXT, -- what kind of test\n" |
| 563 " sql TEXT, -- SQL text for the test\n" |
| 564 " ans TEXT -- expected answer\n" |
| 565 ");" |
| 566 "INSERT INTO selftest" |
| 567 " VALUES(100,'memo','Hashes generated using --init',NULL);" |
| 568 ); |
| 569 if( rc ) return 1; |
| 570 tno = 110; |
| 571 zSql = "SELECT type,name,tbl_name,sql FROM sqlite_master ORDER BY name"; |
| 572 sha1Exec(db, zSql, zHash); |
| 573 rc = runSql(db, |
| 574 "INSERT INTO selftest(tno,op,sql,ans)" |
| 575 " VALUES(%d,'sha1',%Q,%Q)", tno, zSql, zHash); |
| 576 tno += 10; |
| 577 pStmt = prepareSql(db, |
| 578 "SELECT lower(name) FROM sqlite_master" |
| 579 " WHERE type='table' AND sql NOT GLOB 'CREATE VIRTUAL*'" |
| 580 " AND name<>'selftest'" |
| 581 " ORDER BY 1"); |
| 582 if( pStmt==0 ) return 1; |
| 583 while( SQLITE_ROW==sqlite3_step(pStmt) ){ |
| 584 zSql = sqlite3_mprintf("SELECT * FROM \"%w\" NOT INDEXED", |
| 585 sqlite3_column_text(pStmt, 0)); |
| 586 if( zSql==0 ){ |
| 587 printf("Of of memory\n"); |
| 588 exit(1); |
| 589 } |
| 590 sha1Exec(db, zSql, zHash); |
| 591 rc = runSql(db, |
| 592 "INSERT INTO selftest(tno,op,sql,ans)" |
| 593 " VALUES(%d,'sha1',%Q,%Q)", tno, zSql, zHash); |
| 594 tno += 10; |
| 595 sqlite3_free(zSql); |
| 596 if( rc ) break; |
| 597 } |
| 598 sqlite3_finalize(pStmt); |
| 599 if( rc ) return 1; |
| 600 rc = runSql(db, |
| 601 "INSERT INTO selftest(tno,op,sql,ans)" |
| 602 " VALUES(%d,'run','PRAGMA integrity_check','ok');", tno); |
| 603 if( rc ) return 1; |
| 604 return rc; |
| 605 } |
| 606 |
| 607 /* |
| 608 ** Return true if the named table exists |
| 609 */ |
| 610 static int tableExists(sqlite3 *db, const char *zTab){ |
| 611 return sqlite3_table_column_metadata(db, "main", zTab, 0, 0, 0, 0, 0, 0) |
| 612 == SQLITE_OK; |
| 613 } |
| 614 |
| 615 /* |
| 616 ** Default selftest table content, for use when there is no selftest table |
| 617 */ |
| 618 static char *azDefaultTest[] = { |
| 619 0, 0, 0, 0, |
| 620 "0", "memo", "Missing SELFTEST table - default checks only", "", |
| 621 "1", "run", "PRAGMA integrity_check", "ok" |
| 622 }; |
| 623 |
| 624 int main(int argc, char **argv){ |
| 625 int eVolume = VOLUME_LOW; /* How much output to display */ |
| 626 const char **azDb = 0; /* Name of the database file */ |
| 627 int nDb = 0; /* Number of database files to check */ |
| 628 int doInit = 0; /* True if --init is present */ |
| 629 sqlite3 *db = 0; /* Open database connection */ |
| 630 int rc; /* Return code from API calls */ |
| 631 char *zErrMsg = 0; /* An error message return */ |
| 632 char **azTest; /* Content of the selftest table */ |
| 633 int nRow = 0, nCol = 0; /* Rows and columns in azTest[] */ |
| 634 int i; /* Loop counter */ |
| 635 int nErr = 0; /* Number of errors */ |
| 636 int iDb; /* Loop counter for databases */ |
| 637 Str str; /* Result accumulator */ |
| 638 int nTest = 0; /* Number of tests run */ |
| 639 |
| 640 for(i=1; i<argc; i++){ |
| 641 const char *z = argv[i]; |
| 642 if( z[0]=='-' ){ |
| 643 if( z[1]=='-' ) z++; |
| 644 if( strcmp(z, "-help")==0 ){ |
| 645 printf("%s", zHelp); |
| 646 return 0; |
| 647 }else |
| 648 if( strcmp(z, "-init")==0 ){ |
| 649 doInit = 1; |
| 650 }else |
| 651 if( strcmp(z, "-a")==0 ){ |
| 652 if( eVolume>VOLUME_MIN) eVolume--; |
| 653 }else |
| 654 if( strcmp(z, "-v")==0 ){ |
| 655 if( eVolume<VOLUME_MAX) eVolume++; |
| 656 }else |
| 657 { |
| 658 printf("unknown option: \"%s\"\nUse --help for more information\n", |
| 659 argv[i]); |
| 660 return 1; |
| 661 } |
| 662 }else{ |
| 663 nDb++; |
| 664 azDb = sqlite3_realloc(azDb, nDb*sizeof(azDb[0])); |
| 665 if( azDb==0 ){ |
| 666 printf("out of memory\n"); |
| 667 exit(1); |
| 668 } |
| 669 azDb[nDb-1] = argv[i]; |
| 670 } |
| 671 } |
| 672 if( nDb==0 ){ |
| 673 printf("No databases specified. Use --help for more info\n"); |
| 674 return 1; |
| 675 } |
| 676 if( eVolume>=VOLUME_LOW ){ |
| 677 printf("SQLite %s\n", sqlite3_sourceid()); |
| 678 } |
| 679 memset(&str, 0, sizeof(str)); |
| 680 strAppend(&str, "\n"); |
| 681 for(iDb=0; iDb<nDb; iDb++, sqlite3_close(db)){ |
| 682 rc = sqlite3_open_v2(azDb[iDb], &db, |
| 683 doInit ? SQLITE_OPEN_READWRITE : SQLITE_OPEN_READONLY, 0); |
| 684 if( rc ){ |
| 685 printf("Cannot open \"%s\": %s\n", azDb[iDb], sqlite3_errmsg(db)); |
| 686 return 1; |
| 687 } |
| 688 rc = sqlite3_create_function(db, "sha1", 1, SQLITE_UTF8, 0, |
| 689 sha1Func, 0, 0); |
| 690 if( rc==SQLITE_OK ){ |
| 691 rc = sqlite3_create_function(db, "sha1_query", 1, SQLITE_UTF8, 0, |
| 692 sha1QueryFunc, 0, 0); |
| 693 } |
| 694 if( rc ){ |
| 695 printf("Initialization error: %s\n", sqlite3_errmsg(db)); |
| 696 sqlite3_close(db); |
| 697 return 1; |
| 698 } |
| 699 if( doInit && !tableExists(db, "selftest") ){ |
| 700 buildSelftestTable(db); |
| 701 } |
| 702 if( !tableExists(db, "selftest") ){ |
| 703 azTest = azDefaultTest; |
| 704 nCol = 4; |
| 705 nRow = 2; |
| 706 }else{ |
| 707 rc = sqlite3_get_table(db, |
| 708 "SELECT tno,op,sql,ans FROM selftest ORDER BY tno", |
| 709 &azTest, &nRow, &nCol, &zErrMsg); |
| 710 if( rc || zErrMsg ){ |
| 711 printf("Error querying selftest: %s\n", zErrMsg); |
| 712 sqlite3_free_table(azTest); |
| 713 continue; |
| 714 } |
| 715 } |
| 716 for(i=1; i<=nRow; i++){ |
| 717 int tno = atoi(azTest[i*nCol]); |
| 718 const char *zOp = azTest[i*nCol+1]; |
| 719 const char *zSql = azTest[i*nCol+2]; |
| 720 const char *zAns = azTest[i*nCol+3]; |
| 721 |
| 722 if( eVolume>=VOLUME_ECHO ){ |
| 723 char *zQuote = sqlite3_mprintf("%q", zSql); |
| 724 printf("%d: %s %s\n", tno, zOp, zSql); |
| 725 sqlite3_free(zQuote); |
| 726 } |
| 727 if( strcmp(zOp,"memo")==0 ){ |
| 728 if( eVolume>=VOLUME_LOW ){ |
| 729 printf("%s: %s\n", azDb[iDb], zSql); |
| 730 } |
| 731 }else |
| 732 if( strcmp(zOp,"sha1")==0 ){ |
| 733 char zOut[44]; |
| 734 rc = sha1Exec(db, zSql, zOut); |
| 735 nTest++; |
| 736 if( eVolume>=VOLUME_VERBOSE ){ |
| 737 printf("Result: %s\n", zOut); |
| 738 } |
| 739 if( rc ){ |
| 740 nErr++; |
| 741 if( eVolume>=VOLUME_ERROR_ONLY ){ |
| 742 printf("%d: error-code-%d: %s\n", tno, rc, sqlite3_errmsg(db)); |
| 743 } |
| 744 }else if( strcmp(zAns,zOut)!=0 ){ |
| 745 nErr++; |
| 746 if( eVolume>=VOLUME_ERROR_ONLY ){ |
| 747 printf("%d: Expected: [%s]\n", tno, zAns); |
| 748 printf("%d: Got: [%s]\n", tno, zOut); |
| 749 } |
| 750 } |
| 751 }else |
| 752 if( strcmp(zOp,"run")==0 ){ |
| 753 str.n = 0; |
| 754 str.z[0] = 0; |
| 755 zErrMsg = 0; |
| 756 rc = sqlite3_exec(db, zSql, execCallback, &str, &zErrMsg); |
| 757 nTest++; |
| 758 if( eVolume>=VOLUME_VERBOSE ){ |
| 759 printf("Result: %s\n", str.z); |
| 760 } |
| 761 if( rc || zErrMsg ){ |
| 762 nErr++; |
| 763 if( eVolume>=VOLUME_ERROR_ONLY ){ |
| 764 printf("%d: error-code-%d: %s\n", tno, rc, zErrMsg); |
| 765 } |
| 766 sqlite3_free(zErrMsg); |
| 767 }else if( strcmp(zAns,str.z)!=0 ){ |
| 768 nErr++; |
| 769 if( eVolume>=VOLUME_ERROR_ONLY ){ |
| 770 printf("%d: Expected: [%s]\n", tno, zAns); |
| 771 printf("%d: Got: [%s]\n", tno, str.z); |
| 772 } |
| 773 } |
| 774 }else |
| 775 { |
| 776 printf("Unknown operation \"%s\" on selftest line %d\n", zOp, tno); |
| 777 return 1; |
| 778 } |
| 779 } |
| 780 if( azTest!=azDefaultTest ) sqlite3_free_table(azTest); |
| 781 } |
| 782 if( eVolume>=VOLUME_LOW || (nErr>0 && eVolume>=VOLUME_ERROR_ONLY) ){ |
| 783 printf("%d errors out of %d tests on %d databases\n", nErr, nTest, nDb); |
| 784 } |
| 785 return nErr; |
| 786 } |
OLD | NEW |