/ Check-in [760af445]
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Add the --export-sql and --export-db options to the fuzzcheck utility program.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: 760af4455115669b934c3115d45cffe89c085faf
User & Date: drh 2015-09-22 18:51:17
Context
2015-09-23
01:10
Do not allow a comma at the end of a JSON array or object. check-in: 7c7a3f3e user: drh tags: trunk
2015-09-22
18:51
Add the --export-sql and --export-db options to the fuzzcheck utility program. check-in: 760af445 user: drh tags: trunk
17:46
Fix a typo in the --help screen of the fuzzcheck utility. check-in: b6ae61fe user: drh tags: trunk
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to test/fuzzcheck.c.

   291    291     if( pBuf && 1==fread(pBuf, nIn, 1, in) ){
   292    292       sqlite3_result_blob(context, pBuf, nIn, sqlite3_free);
   293    293     }else{
   294    294       sqlite3_free(pBuf);
   295    295     }
   296    296     fclose(in);
   297    297   }
          298  +
          299  +/*
          300  +** Implementation of the "writefile(X,Y)" SQL function.  The argument Y
          301  +** is written into file X.  The number of bytes written is returned.  Or
          302  +** NULL is returned if something goes wrong, such as being unable to open
          303  +** file X for writing.
          304  +*/
          305  +static void writefileFunc(
          306  +  sqlite3_context *context,
          307  +  int argc,
          308  +  sqlite3_value **argv
          309  +){
          310  +  FILE *out;
          311  +  const char *z;
          312  +  sqlite3_int64 rc;
          313  +  const char *zFile;
          314  +
          315  +  (void)argc;
          316  +  zFile = (const char*)sqlite3_value_text(argv[0]);
          317  +  if( zFile==0 ) return;
          318  +  out = fopen(zFile, "wb");
          319  +  if( out==0 ) return;
          320  +  z = (const char*)sqlite3_value_blob(argv[1]);
          321  +  if( z==0 ){
          322  +    rc = 0;
          323  +  }else{
          324  +    rc = fwrite(z, 1, sqlite3_value_bytes(argv[1]), out);
          325  +  }
          326  +  fclose(out);
          327  +  sqlite3_result_int64(context, rc);
          328  +}
          329  +
   298    330   
   299    331   /*
   300    332   ** Load a list of Blob objects from the database
   301    333   */
   302    334   static void blobListLoadFromDb(
   303    335     sqlite3 *db,             /* Read from this database */
   304    336     const char *zSql,        /* Query used to extract the blobs */
................................................................................
   747    779     printf("Usage: %s [options] SOURCE-DB ?ARGS...?\n", g.zArgv0);
   748    780     printf(
   749    781   "Read databases and SQL scripts from SOURCE-DB and execute each script against\n"
   750    782   "each database, checking for crashes and memory leaks.\n"
   751    783   "Options:\n"
   752    784   "  --cell-size-check     Set the PRAGMA cell_size_check=ON\n"
   753    785   "  --dbid N              Use only the database where dbid=N\n"
          786  +"  --export-db DIR       Write databases to files(s) in DIR. Works with --dbid\n"
          787  +"  --export-sql DIR      Write SQL to file(s) in DIR. Also works with --sqlid\n"
   754    788   "  --help                Show this help text\n"
   755    789   "  -q                    Reduced output\n"
   756    790   "  --quiet               Reduced output\n"
   757    791   "  --limit-mem N         Limit memory used by test SQLite instance to N bytes\n"
   758    792   "  --limit-vdbe          Panic if an sync SQL runs for more than 100,000 cycles\n"
   759    793   "  --load-sql ARGS...    Load SQL scripts fro files into SOURCE-DB\n"
   760    794   "  --load-db ARGS...     Load template databases from files into SOURCE_DB\n"
................................................................................
   795    829     int nTest = 0;               /* Total number of tests performed */
   796    830     char *zDbName = "";          /* Appreviated name of a source database */
   797    831     const char *zFailCode = 0;   /* Value of the TEST_FAILURE environment variable */
   798    832     int cellSzCkFlag = 0;        /* --cell-size-check */
   799    833     int sqlFuzz = 0;             /* True for SQL fuzz testing. False for DB fuzz */
   800    834     int iTimeout = 120;          /* Default 120-second timeout */
   801    835     int nMem = 0;                /* Memory limit */
          836  +  char *zExpDb = 0;            /* Write Databases to files in this directory */
          837  +  char *zExpSql = 0;           /* Write SQL to files in this directory */
   802    838   
   803    839     iBegin = timeOfDay();
   804    840   #ifdef __unix__
   805    841     signal(SIGALRM, timeoutHandler);
   806    842   #endif
   807    843     g.zArgv0 = argv[0];
   808    844     zFailCode = getenv("TEST_FAILURE");
................................................................................
   813    849         if( z[0]=='-' ) z++;
   814    850         if( strcmp(z,"cell-size-check")==0 ){
   815    851           cellSzCkFlag = 1;
   816    852         }else
   817    853         if( strcmp(z,"dbid")==0 ){
   818    854           if( i>=argc-1 ) fatalError("missing arguments on %s", argv[i]);
   819    855           onlyDbid = integerValue(argv[++i]);
          856  +      }else
          857  +      if( strcmp(z,"export-db")==0 ){
          858  +        if( i>=argc-1 ) fatalError("missing arguments on %s", argv[i]);
          859  +        zExpDb = argv[++i];
          860  +      }else
          861  +      if( strcmp(z,"export-sql")==0 ){
          862  +        if( i>=argc-1 ) fatalError("missing arguments on %s", argv[i]);
          863  +        zExpSql = argv[++i];
   820    864         }else
   821    865         if( strcmp(z,"help")==0 ){
   822    866           showHelp();
   823    867           return 0;
   824    868         }else
   825    869         if( strcmp(z,"limit-mem")==0 ){
   826    870           if( i>=argc-1 ) fatalError("missing arguments on %s", argv[i]);
................................................................................
   936    980           rc = sqlite3_reset(pStmt);
   937    981           if( rc ) fatalError("insert failed for %s", argv[i]);
   938    982         }
   939    983         sqlite3_finalize(pStmt);
   940    984         rc = sqlite3_exec(db, "COMMIT", 0, 0, 0);
   941    985         if( rc ) fatalError("cannot commit the transaction: %s", sqlite3_errmsg(db));
   942    986         rebuild_database(db);
          987  +      sqlite3_close(db);
          988  +      return 0;
          989  +    }
          990  +    if( zExpDb!=0 || zExpSql!=0 ){
          991  +      sqlite3_create_function(db, "writefile", 2, SQLITE_UTF8, 0,
          992  +                              writefileFunc, 0, 0);
          993  +      if( zExpDb!=0 ){
          994  +        const char *zExDb = 
          995  +          "SELECT writefile(printf('%s/db%06d.db',?1,dbid),dbcontent),"
          996  +          "       dbid, printf('%s/db%06d.db',?1,dbid), length(dbcontent)"
          997  +          "  FROM db WHERE ?2<0 OR dbid=?2;";
          998  +        rc = sqlite3_prepare_v2(db, zExDb, -1, &pStmt, 0);
          999  +        if( rc ) fatalError("cannot prepare statement [%s]: %s",
         1000  +                            zExDb, sqlite3_errmsg(db));
         1001  +        sqlite3_bind_text64(pStmt, 1, zExpDb, strlen(zExpDb),
         1002  +                            SQLITE_STATIC, SQLITE_UTF8);
         1003  +        sqlite3_bind_int(pStmt, 2, onlyDbid);
         1004  +        while( sqlite3_step(pStmt)==SQLITE_ROW ){
         1005  +          printf("write db-%d (%d bytes) into %s\n",
         1006  +             sqlite3_column_int(pStmt,1),
         1007  +             sqlite3_column_int(pStmt,3),
         1008  +             sqlite3_column_text(pStmt,2));
         1009  +        }
         1010  +        sqlite3_finalize(pStmt);
         1011  +      }
         1012  +      if( zExpSql!=0 ){
         1013  +        const char *zExSql = 
         1014  +          "SELECT writefile(printf('%s/sql%06d.txt',?1,sqlid),sqltext),"
         1015  +          "       sqlid, printf('%s/sql%06d.txt',?1,sqlid), length(sqltext)"
         1016  +          "  FROM xsql WHERE ?2<0 OR sqlid=?2;";
         1017  +        rc = sqlite3_prepare_v2(db, zExSql, -1, &pStmt, 0);
         1018  +        if( rc ) fatalError("cannot prepare statement [%s]: %s",
         1019  +                            zExSql, sqlite3_errmsg(db));
         1020  +        sqlite3_bind_text64(pStmt, 1, zExpSql, strlen(zExpSql),
         1021  +                            SQLITE_STATIC, SQLITE_UTF8);
         1022  +        sqlite3_bind_int(pStmt, 2, onlySqlid);
         1023  +        while( sqlite3_step(pStmt)==SQLITE_ROW ){
         1024  +          printf("write sql-%d (%d bytes) into %s\n",
         1025  +             sqlite3_column_int(pStmt,1),
         1026  +             sqlite3_column_int(pStmt,3),
         1027  +             sqlite3_column_text(pStmt,2));
         1028  +        }
         1029  +        sqlite3_finalize(pStmt);
         1030  +      }
   943   1031         sqlite3_close(db);
   944   1032         return 0;
   945   1033       }
   946   1034     
   947   1035       /* Load all SQL script content and all initial database images from the
   948   1036       ** source db
   949   1037       */