diff options
author | drh <drh@noemail.net> | 2015-09-24 14:26:51 +0000 |
---|---|---|
committer | drh <drh@noemail.net> | 2015-09-24 14:26:51 +0000 |
commit | 9ec0efd946b8015f8b3ba3b726bc39f0b15d306c (patch) | |
tree | b4764dfa9ca1b2d064f80f3d13056f2b5bbeba61 /test/fuzzcheck.c | |
parent | 438b881540c83370029c73c8a1d7eba78979c491 (diff) | |
parent | 22c733da644bcf8d614b7caf058c7a2b51286ab8 (diff) | |
download | sqlite-9ec0efd946b8015f8b3ba3b726bc39f0b15d306c.tar.gz sqlite-9ec0efd946b8015f8b3ba3b726bc39f0b15d306c.zip |
Merge all the latest trunk enhancements into the sessions branch.
FossilOrigin-Name: c91065f8edb1e54076791716fc20d3fcfe3070dc
Diffstat (limited to 'test/fuzzcheck.c')
-rw-r--r-- | test/fuzzcheck.c | 96 |
1 files changed, 95 insertions, 1 deletions
diff --git a/test/fuzzcheck.c b/test/fuzzcheck.c index c678e2af7..4597891c3 100644 --- a/test/fuzzcheck.c +++ b/test/fuzzcheck.c @@ -297,6 +297,38 @@ static void readfileFunc( } /* +** Implementation of the "writefile(X,Y)" SQL function. The argument Y +** is written into file X. The number of bytes written is returned. Or +** NULL is returned if something goes wrong, such as being unable to open +** file X for writing. +*/ +static void writefileFunc( + sqlite3_context *context, + int argc, + sqlite3_value **argv +){ + FILE *out; + const char *z; + sqlite3_int64 rc; + const char *zFile; + + (void)argc; + zFile = (const char*)sqlite3_value_text(argv[0]); + if( zFile==0 ) return; + out = fopen(zFile, "wb"); + if( out==0 ) return; + z = (const char*)sqlite3_value_blob(argv[1]); + if( z==0 ){ + rc = 0; + }else{ + rc = fwrite(z, 1, sqlite3_value_bytes(argv[1]), out); + } + fclose(out); + sqlite3_result_int64(context, rc); +} + + +/* ** Load a list of Blob objects from the database */ static void blobListLoadFromDb( @@ -751,6 +783,8 @@ static void showHelp(void){ "Options:\n" " --cell-size-check Set the PRAGMA cell_size_check=ON\n" " --dbid N Use only the database where dbid=N\n" +" --export-db DIR Write databases to files(s) in DIR. Works with --dbid\n" +" --export-sql DIR Write SQL to file(s) in DIR. Also works with --sqlid\n" " --help Show this help text\n" " -q Reduced output\n" " --quiet Reduced output\n" @@ -763,7 +797,7 @@ static void showHelp(void){ " --rebuild Rebuild and vacuum the database file\n" " --result-trace Show the results of each SQL command\n" " --sqlid N Use only SQL where sqlid=N\n" -" --timeline N Abort if any single test case needs more than N seconds\n" +" --timeout N Abort if any single test case needs more than N seconds\n" " -v Increased output\n" " --verbose Increased output\n" ); @@ -799,6 +833,8 @@ int main(int argc, char **argv){ int sqlFuzz = 0; /* True for SQL fuzz testing. False for DB fuzz */ int iTimeout = 120; /* Default 120-second timeout */ int nMem = 0; /* Memory limit */ + char *zExpDb = 0; /* Write Databases to files in this directory */ + char *zExpSql = 0; /* Write SQL to files in this directory */ iBegin = timeOfDay(); #ifdef __unix__ @@ -818,6 +854,14 @@ int main(int argc, char **argv){ if( i>=argc-1 ) fatalError("missing arguments on %s", argv[i]); onlyDbid = integerValue(argv[++i]); }else + if( strcmp(z,"export-db")==0 ){ + if( i>=argc-1 ) fatalError("missing arguments on %s", argv[i]); + zExpDb = argv[++i]; + }else + if( strcmp(z,"export-sql")==0 ){ + if( i>=argc-1 ) fatalError("missing arguments on %s", argv[i]); + zExpSql = argv[++i]; + }else if( strcmp(z,"help")==0 ){ showHelp(); return 0; @@ -943,6 +987,50 @@ int main(int argc, char **argv){ sqlite3_close(db); return 0; } + if( zExpDb!=0 || zExpSql!=0 ){ + sqlite3_create_function(db, "writefile", 2, SQLITE_UTF8, 0, + writefileFunc, 0, 0); + if( zExpDb!=0 ){ + const char *zExDb = + "SELECT writefile(printf('%s/db%06d.db',?1,dbid),dbcontent)," + " dbid, printf('%s/db%06d.db',?1,dbid), length(dbcontent)" + " FROM db WHERE ?2<0 OR dbid=?2;"; + rc = sqlite3_prepare_v2(db, zExDb, -1, &pStmt, 0); + if( rc ) fatalError("cannot prepare statement [%s]: %s", + zExDb, sqlite3_errmsg(db)); + sqlite3_bind_text64(pStmt, 1, zExpDb, strlen(zExpDb), + SQLITE_STATIC, SQLITE_UTF8); + sqlite3_bind_int(pStmt, 2, onlyDbid); + while( sqlite3_step(pStmt)==SQLITE_ROW ){ + printf("write db-%d (%d bytes) into %s\n", + sqlite3_column_int(pStmt,1), + sqlite3_column_int(pStmt,3), + sqlite3_column_text(pStmt,2)); + } + sqlite3_finalize(pStmt); + } + if( zExpSql!=0 ){ + const char *zExSql = + "SELECT writefile(printf('%s/sql%06d.txt',?1,sqlid),sqltext)," + " sqlid, printf('%s/sql%06d.txt',?1,sqlid), length(sqltext)" + " FROM xsql WHERE ?2<0 OR sqlid=?2;"; + rc = sqlite3_prepare_v2(db, zExSql, -1, &pStmt, 0); + if( rc ) fatalError("cannot prepare statement [%s]: %s", + zExSql, sqlite3_errmsg(db)); + sqlite3_bind_text64(pStmt, 1, zExpSql, strlen(zExpSql), + SQLITE_STATIC, SQLITE_UTF8); + sqlite3_bind_int(pStmt, 2, onlySqlid); + while( sqlite3_step(pStmt)==SQLITE_ROW ){ + printf("write sql-%d (%d bytes) into %s\n", + sqlite3_column_int(pStmt,1), + sqlite3_column_int(pStmt,3), + sqlite3_column_text(pStmt,2)); + } + sqlite3_finalize(pStmt); + } + sqlite3_close(db); + return 0; + } /* Load all SQL script content and all initial database images from the ** source db @@ -1039,6 +1127,12 @@ int main(int argc, char **argv){ } rc = sqlite3_open_v2("main.db", &db, openFlags, zVfs); if( rc ) fatalError("cannot open inmem database"); +#ifdef SQLITE_ENABLE_JSON1 + { + extern int sqlite3_json_init(sqlite3*); + sqlite3_json_init(db); + } +#endif if( cellSzCkFlag ) runSql(db, "PRAGMA cell_size_check=ON", runFlags); setAlarm(iTimeout); #ifndef SQLITE_OMIT_PROGRESS_CALLBACK |