/ Check-in [1f583c53]
Login

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

Overview
Comment:Add the "decode_hexdb" TCL command to testfixture. Add the dbfuzz001.test module to demonstration how to use decode_hexdb to deserialize a dbtotxt database description for use in a corruption test.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 1f583c53f3b7318c69f6e235934d97ef9493278feeab0837217076d7d071c35b
User & Date: drh 2018-12-13 20:49:43
Context
2018-12-13
21:05
Fix a problem in sqlite3BtreeDelete() in which deleting an entry from a corrupt database can leave a btree page with zero cells. check-in: 682053d1 user: drh tags: trunk
20:49
Add the "decode_hexdb" TCL command to testfixture. Add the dbfuzz001.test module to demonstration how to use decode_hexdb to deserialize a dbtotxt database description for use in a corruption test. check-in: 1f583c53 user: drh tags: trunk
18:59
Fix the CLI to keep proper track of input line numbers for use in error messages, even after processing in-line hex database inputs using ".open --hexdb". check-in: 7ffa9858 user: drh tags: trunk
Changes
Hide Diffs Side-by-Side Diffs Show Whitespace Changes Patch

Changes to src/test1.c.

  7636   7636         zDb = Tcl_GetString(objv[2]);
  7637   7637       }
  7638   7638       rc = sqlite3_mmap_warm(db, zDb);
  7639   7639       Tcl_SetObjResult(interp, Tcl_NewStringObj(sqlite3ErrName(rc), -1));
  7640   7640       return TCL_OK;
  7641   7641     }
  7642   7642   }
         7643  +
         7644  +/*
         7645  +** Usage:  decode_hexdb TEXT
         7646  +**
         7647  +** Example:   db deserialize [decode_hexdb $output_of_dbtotxt]
         7648  +**
         7649  +** This routine returns a byte-array for an SQLite database file that
         7650  +** is constructed from a text input which is the output of the "dbtotxt"
         7651  +** utility.
         7652  +*/
         7653  +static int SQLITE_TCLAPI test_decode_hexdb(
         7654  +  void * clientData,
         7655  +  Tcl_Interp *interp,
         7656  +  int objc,
         7657  +  Tcl_Obj *CONST objv[]
         7658  +){
         7659  +  const char *zIn = 0;
         7660  +  unsigned char *a = 0;
         7661  +  int n = 0;
         7662  +  int lineno = 0;
         7663  +  int i, iNext;
         7664  +  int iOffset = 0;
         7665  +  int j, k;
         7666  +  int rc;
         7667  +  unsigned char x[16];
         7668  +  if( objc!=2 ){
         7669  +    Tcl_WrongNumArgs(interp, 1, objv, "HEXDB");
         7670  +    return TCL_ERROR;
         7671  +  }
         7672  +  zIn = Tcl_GetString(objv[1]);
         7673  +  for(i=0; zIn[i]; i=iNext){
         7674  +    lineno++;
         7675  +    for(iNext=i; zIn[iNext] && zIn[iNext]!='\n'; iNext++){}
         7676  +    if( zIn[iNext]=='\n' ) iNext++;
         7677  +    while( zIn[i]==' ' || zIn[i]=='\t' ){ i++; }
         7678  +    if( a==0 ){
         7679  +      int pgsz;
         7680  +      rc = sscanf(zIn+i, "| size %d pagesize %d", &n, &pgsz);
         7681  +      if( rc!=2 ) continue;
         7682  +      if( n<512 ){
         7683  +        Tcl_AppendResult(interp, "bad 'size' field", (void*)0);
         7684  +        return TCL_ERROR;
         7685  +      }
         7686  +      a = malloc( n );
         7687  +      if( a==0 ){
         7688  +        Tcl_AppendResult(interp, "out of memory", (void*)0);
         7689  +        return TCL_ERROR;
         7690  +      }
         7691  +      memset(a, 0, n);
         7692  +      continue;
         7693  +    }
         7694  +    rc = sscanf(zIn+i, "| page %d offset %d", &j, &k);
         7695  +    if( rc==2 ){
         7696  +      iOffset = k;
         7697  +      continue;
         7698  +    }
         7699  +    rc = sscanf(zIn+i,"| %d: %hhx %hhx %hhx %hhx %hhx %hhx %hhx %hhx"
         7700  +                      "  %hhx %hhx %hhx %hhx %hhx %hhx %hhx %hhx",
         7701  +                &j, &x[0], &x[1], &x[2], &x[3], &x[4], &x[5], &x[6], &x[7],
         7702  +                &x[8], &x[9], &x[10], &x[11], &x[12], &x[13], &x[14], &x[15]);
         7703  +    if( rc==17 ){
         7704  +      k = iOffset+j;
         7705  +      if( k+16<=n ){
         7706  +        memcpy(a+k, x, 16);
         7707  +      }
         7708  +      continue;
         7709  +    }
         7710  +  }
         7711  +  Tcl_SetObjResult(interp, Tcl_NewByteArrayObj(a, n));
         7712  +  free(a);
         7713  +  return TCL_OK;
         7714  +}
         7715  +
  7643   7716   
  7644   7717   /*
  7645   7718   ** Register commands with the TCL interpreter.
  7646   7719   */
  7647   7720   int Sqlitetest1_Init(Tcl_Interp *interp){
  7648   7721     extern int sqlite3_search_count;
  7649   7722     extern int sqlite3_found_count;
................................................................................
  7916   7989        { "sqlite3_snapshot_open_blob", test_snapshot_open_blob, 0 },
  7917   7990        { "sqlite3_snapshot_cmp_blob", test_snapshot_cmp_blob, 0 },
  7918   7991   #endif
  7919   7992        { "sqlite3_delete_database", test_delete_database,    0 },
  7920   7993        { "atomic_batch_write",      test_atomic_batch_write, 0 },
  7921   7994        { "sqlite3_mmap_warm",       test_mmap_warm,          0 },
  7922   7995        { "sqlite3_config_sorterref", test_config_sorterref,   0 },
         7996  +     { "decode_hexdb",             test_decode_hexdb,       0 },
  7923   7997     };
  7924   7998     static int bitmask_size = sizeof(Bitmask)*8;
  7925   7999     static int longdouble_size = sizeof(LONGDOUBLE_TYPE);
  7926   8000     int i;
  7927   8001     extern int sqlite3_sync_count, sqlite3_fullsync_count;
  7928   8002     extern int sqlite3_opentemp_count;
  7929   8003     extern int sqlite3_like_count;

Added test/dbfuzz001.test.

            1  +# 2012-12-13
            2  +#
            3  +# The author disclaims copyright to this source code.  In place of
            4  +# a legal notice, here is a blessing:
            5  +#
            6  +#    May you do good and not evil.
            7  +#    May you find forgiveness for yourself and forgive others.
            8  +#    May you share freely, never taking more than you give.
            9  +#
           10  +#***********************************************************************
           11  +#
           12  +# Test cases for corrupt database files.
           13  +
           14  +set testdir [file dirname $argv0]
           15  +source $testdir/tester.tcl
           16  +
           17  +ifcapable !deserialize {
           18  +  finish_test
           19  +  return
           20  +}
           21  +
           22  +do_test dbfuzz001-100 {
           23  +  sqlite3 db {}
           24  +  db deserialize [decode_hexdb {
           25  +    | size 5632 pagesize 512 filename c4.db
           26  +    | page 1 offset 0
           27  +    |      0: 53 51 4c 69 74 65 20 66 6f 72 6d 61 74 20 33 00   SQLite format 3.
           28  +    |     16: 02 00 01 01 00 40 20 20 00 00 00 02 00 00 00 0b   .....@  ........
           29  +    |     32: 00 00 00 06 00 00 00 01 00 00 00 28 00 00 00 04   ...........(....
           30  +    |     48: 00 00 00 00 00 00 00 00 00 00 00 01 00 00 00 00   ................
           31  +    |     80: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 02   ................
           32  +    |     96: 00 2e 30 38 0d 00 00 00 06 01 06 00 01 da 01 b0   ..08............
           33  +    |    112: 01 56 01 86 01 2a 01 06 00 00 00 00 00 00 00 00   .V...*..........
           34  +    |    256: 00 00 00 00 00 00 22 07 06 17 11 11 01 31 74 61   ......"......1ta
           35  +    |    272: 62 6c 65 74 34 74 34 07 43 52 45 41 54 45 20 54   blet4t4.CREATE T
           36  +    |    288: 41 42 4c 45 20 74 34 28 78 29 2a 06 06 17 13 11   ABLE t4(x)*.....
           37  +    |    304: 01 3f 69 6e 64 65 78 00 00 00 00 00 00 00 00 00   .?index.........
           38  +    |    336: 20 74 33 28 78 29 2e 04 06 17 15 11 01 45 69 6e    t3(x).......Ein
           39  +    |    352: 64 65 78 74 32 63 64 74 32 05 43 52 45 41 54 45   dext2cdt2.CREATE
           40  +    |    368: 20 49 4e 44 45 58 20 74 32 63 64 20 4f 4e 20 74    INDEX t2cd ON t
           41  +    |    384: 32 28 63 2c 64 29 28 05 06 17 11 11 01 3d 74 61   2(c,d)(......=ta
           42  +    |    400: 62 6c 65 74 33 74 33 04 43 52 45 41 54 45 20 54   blet3t3.CREATE T
           43  +    |    416: 41 42 4c 45 20 74 33 28 63 2c 78 2c 65 2c 66 29   ABLE t3(c,x,e,f)
           44  +    |    432: 28 02 06 17 11 11 01 3d 74 61 62 6c 65 74 32 74   (......=tablet2t
           45  +    |    448: 32 03 43 52 45 41 54 45 20 54 41 42 4c 45 20 74   2.CREATE TABLE t
           46  +    |    464: 32 28 63 2c 64 2c 65 2c 66 29 24 01 06 17 11 11   2(c,d,e,f)$.....
           47  +    |    480: 01 35 74 61 62 6c 65 74 31 74 31 02 43 52 45 41   .5tablet1t1.CREA
           48  +    |    496: 54 45 20 54 41 42 4c 45 20 74 31 28 61 2c 62 29   TE TABLE t1(a,b)
           49  +    | page 2 offset 512
           50  +    |      0: 0d 00 00 00 00 02 00 00 00 00 00 00 00 00 00 00   ................
           51  +    | page 3 offset 1024
           52  +    |      0: 0d 00 00 00 00 02 00 00 00 00 00 00 00 00 00 00   ................
           53  +    | page 4 offset 1536
           54  +    |      0: 05 00 00 00 03 01 f1 00 00 00 00 0b 01 fb 01 f6   ................
           55  +    |     16: 01 f1 00 16 00 00 09 06 05 01 01 01 01 04 04 03   ................
           56  +    |     32: 03 07 05 05 01 01 09 09 02 02 19 04 05 17 17 17   ................
           57  +    |     48: 17 73 65 76 65 6e 65 69 67 68 74 65 69 67 68 74   .seveneighteight
           58  +    |     64: 73 65 76 65 6e 25 03 05 07 07 07 07 40 14 00 00   seven%......@...
           59  +    |     80: 00 00 00 00 40 18 00 00 00 00 00 00 40 18 00 00   ....@.......@...
           60  +    |     96: 00 00 00 00 40 14 00 00 00 00 00 00 09 02 05 01   ....@...........
           61  +    |    112: 01 01 01 03 04 04 03 07 01 05 09 01 01 09 02 02   ................
           62  +    |    352: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 1a   ................
           63  +    |    496: 00 00 00 00 0a 3e 00 00 00 09 21 00 00 00 08 06   .....>....!.....
           64  +    | page 5 offset 2048
           65  +    |      0: 0a 00 00 00 00 02 00 00 00 00 00 00 00 00 00 00   ................
           66  +    | page 7 offset 3072
           67  +    |      0: 0d 00 00 00 08 01 c2 00 01 fb 01 f6 01 f1 01 ec   ................
           68  +    |     16: 01 e0 01 d4 01 cb 01 c2 00 00 00 00 00 00 00 00   ................
           69  +    |     96: 00 00 00 00 13 00 00 00 00 00 00 00 00 00 00 00   ................
           70  +    |    224: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 02   ................
           71  +    |    288: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 03   ................
           72  +    |    448: 00 00 07 08 02 17 65 69 67 68 74 07 07 02 17 65   ......eight....e
           73  +    |    464: 69 67 68 74 0a 06 02 07 40 18 00 00 00 00 00 00   ight....@.......
           74  +    |    480: 0a 05 02 07 40 18 00 00 00 00 00 00 03 04 02 01   ....@...........
           75  +    |    496: 04 03 03 02 01 04 03 02 02 01 02 03 01 02 01 02   ................
           76  +    | page 8 offset 3584
           77  +    |      0: 0d 00 21 00 01 00 16 00 00 16 00 16 00 16 00 16   ..!.............
           78  +    |     16: 00 16 00 16 00 00 09 06 05 01 01 01 01 04 04 03   ................
           79  +    |     32: 03 00 00 00 5f 01 09 09 02 02 00 00 00 56 17 17   ...._........V..
           80  +    |     48: 17 73 65 76 65 6e 65 69 67 68 74 65 69 67 68 74   .seveneighteight
           81  +    |     64: 73 65 76 65 6e 00 00 00 3b 07 07 07 40 14 00 00   seven...;...@...
           82  +    |     80: 00 00 00 00 40 18 00 00 00 00 00 00 40 18 00 00   ....@.......@...
           83  +    |     96: 00 00 00 00 40 14 00 00 00 00 00 00 00 00 00 14   ....@...........
           84  +    |    112: 01 01 01 03 04 04 03 00 00 00 09 01 01 09 02 02   ................
           85  +    |    352: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 1a   ................
           86  +    | page 9 offset 4096
           87  +    |      0: 0d 00 00 00 1b 00 47 00 01 d9 01 be 01 af 01 a0   ......G.........
           88  +    |     16: 01 91 01 82 01 73 01 64 01 55 01 46 01 37 01 28   .....s.d.U.F.7.(
           89  +    |     32: 01 19 01 0a 00 fb 00 ec 00 dd 00 ce 00 bf 00 b0   ................
           90  +    |     48: 00 a1 00 92 00 83 00 74 00 65 00 56 00 47 00 00   .......t.e.V.G..
           91  +    |     64: 00 00 00 00 00 00 00 0d 21 00 00 48 01 54 00 01   ........!..H.T..
           92  +    |     80: f7 01 ec 01 c5 01 0d 20 00 00 48 01 54 00 01 f7   ....... ..H.T...
           93  +    |     96: 01 ec 01 c5 01 0d 1f 00 00 48 01 54 00 01 f7 01   .........H.T....
           94  +    |    112: ec 01 c5 01 0d 1e 00 00 48 01 54 00 01 f7 01 ec   ........H.T.....
           95  +    |    128: 01 c5 01 0d 1d 00 00 48 01 54 00 01 f7 01 ec 01   .......H.T......
           96  +    |    144: c5 01 0d 1c 00 00 48 01 54 00 01 f7 01 ec 01 c5   ......H.T.......
           97  +    |    160: 01 0d 1b 00 00 48 01 54 00 01 f7 01 ec 01 c5 01   .....H.T........
           98  +    |    176: 0d 1a 00 00 48 01 54 00 01 f7 01 ec 01 c5 01 0d   ....H.T.........
           99  +    |    192: 19 00 00 48 01 54 00 01 f7 01 ec 01 c5 01 0d 18   ...H.T..........
          100  +    |    208: 00 00 48 01 54 00 01 f7 01 ec 01 c5 01 0d 17 00   ..H.T...........
          101  +    |    224: 00 48 01 54 00 01 f7 01 ec 01 c5 01 0d 16 00 00   .H.T............
          102  +    |    240: 48 01 54 00 01 f7 01 ec 01 c5 01 0d 15 00 00 48   H.T............H
          103  +    |    256: 01 54 00 01 f7 01 ec 01 c5 01 0d 14 00 00 48 01   .T............H.
          104  +    |    272: 54 00 01 f7 01 ec 01 c5 01 0d 13 00 00 48 01 54   T............H.T
          105  +    |    288: 00 01 f7 01 ec 01 c5 01 0d 12 00 00 48 01 54 00   ............H.T.
          106  +    |    304: 01 f7 01 ec 01 c5 01 0d 11 00 00 48 01 54 00 01   ...........H.T..
          107  +    |    320: f7 01 ec 01 c5 01 0d 10 00 00 48 01 54 00 01 f7   ..........H.T...
          108  +    |    336: 01 ec 01 c5 01 0d 0f 00 00 48 01 54 00 01 f7 01   .........H.T....
          109  +    |    352: ec 01 c5 01 0d 0e 00 00 48 01 54 00 01 f7 01 ec   ........H.T.....
          110  +    |    368: 01 c5 01 0d 0d 00 00 48 01 54 00 01 f7 01 ec 01   .......H.T......
          111  +    |    384: c5 01 0d 0c 00 00 48 01 54 00 01 f7 01 ec 01 c5   ......H.T.......
          112  +    |    400: 01 0d 0b 00 00 48 01 54 00 01 f7 01 ec 01 c5 01   .....H.T........
          113  +    |    416: 0d 0a 00 00 48 01 54 00 01 f7 01 ec 01 c5 01 0d   ....H.T.........
          114  +    |    432: 09 00 00 48 01 54 00 01 f7 01 ec 01 c5 01 19 08   ...H.T..........
          115  +    |    448: 05 17 17 17 17 65 69 67 68 74 65 69 67 68 74 73   .....eighteights
          116  +    |    464: 65 76 65 6e 73 65 76 65 6e 25 07 05 07 07 07 07   evenseven%......
          117  +    |    480: 40 18 00 00 00 00 00 00 40 18 00 00 00 00 00 00   @.......@.......
          118  +    |    496: 40 14 00 00 00 00 00 00 40 14 00 00 00 00 00 00   @.......@.......
          119  +    | page 10 offset 4608
          120  +    |      0: 0d 00 00 00 1d 00 4d 00 01 f1 01 e2 01 d3 01 c4   ......M.........
          121  +    |     16: 01 b5 01 a6 01 97 01 88 01 79 01 6a 01 5b 01 4c   .........y.j.[.L
          122  +    |     32: 01 3d 01 2e 01 1f 01 10 01 01 00 f2 00 e3 00 d4   .=..............
          123  +    |     48: 00 c5 00 b6 00 a7 00 98 00 89 00 7a 00 6b 00 5c   ...........z.k.\
          124  +    |     64: 00 4d 00 00 00 00 00 00 00 00 00 00 00 0d 3e 00   .M............>.
          125  +    |     80: 00 48 01 54 00 01 f7 01 ec 01 c5 01 0d 3d 00 00   .H.T.........=..
          126  +    |     96: 48 01 54 00 01 f7 01 ec 01 c5 01 0d 3c 00 00 48   H.T.........<..H
          127  +    |    112: 01 54 00 01 f7 01 ec 01 c5 01 0d 3b 00 00 48 01   .T.........;..H.
          128  +    |    128: 54 00 01 f7 01 ec 01 c5 01 0d 3a 00 00 48 01 54   T.........:..H.T
          129  +    |    144: 00 01 f7 01 ec 01 c5 01 0d 39 00 00 48 01 54 00   .........9..H.T.
          130  +    |    160: 01 f7 01 ec 01 c5 01 0d 38 00 00 48 01 54 00 01   ........8..H.T..
          131  +    |    176: f7 01 ec 01 c5 01 0d 37 00 00 48 01 54 00 01 f7   .......7..H.T...
          132  +    |    192: 01 ec 01 c5 01 0d 36 00 00 48 01 54 00 01 f7 01   ......6..H.T....
          133  +    |    208: ec 01 c5 01 0d 35 00 00 48 01 54 00 01 f7 01 ec   .....5..H.T.....
          134  +    |    224: 01 c5 01 0d 34 00 00 48 01 54 00 01 f7 01 ec 01   ....4..H.T......
          135  +    |    240: c5 01 0d 33 00 00 48 01 54 00 01 f7 01 ec 01 c5   ...3..H.T.......
          136  +    |    256: 01 0d 32 00 00 48 01 54 00 01 f7 01 ec 01 c5 01   ..2..H.T........
          137  +    |    272: 0d 31 00 00 48 01 54 00 01 f7 01 ec 01 c5 01 0d   .1..H.T.........
          138  +    |    288: 30 00 00 48 01 54 00 01 f7 01 ec 01 c5 01 0d 2f   0..H.T........./
          139  +    |    304: 00 00 48 01 54 00 01 f7 01 ec 01 c5 01 0d 2e 00   ..H.T...........
          140  +    |    320: 00 48 01 54 00 01 f7 01 ec 01 c5 01 0d 2d 00 00   .H.T.........-..
          141  +    |    336: 48 01 54 00 01 f7 01 ec 01 c5 01 0d 2c 00 00 48   H.T.........,..H
          142  +    |    352: 01 54 00 01 f7 01 ec 01 c5 01 0d 2b 00 00 48 01   .T.........+..H.
          143  +    |    368: 54 00 01 f7 01 ec 01 c5 01 0d 2a 00 00 48 01 54   T.........*..H.T
          144  +    |    384: 00 01 f7 01 ec 01 c5 01 0d 29 00 00 48 01 54 00   .........)..H.T.
          145  +    |    400: 01 f7 01 ec 01 c5 01 0d 28 00 00 48 01 54 00 01   ........(..H.T..
          146  +    |    416: f7 01 ec 01 c5 01 0d 27 00 00 48 01 54 00 01 f7   .......'..H.T...
          147  +    |    432: 01 ec 01 c5 01 0d 26 00 00 48 01 54 00 01 f7 01   ......&..H.T....
          148  +    |    448: ec 01 c5 01 0d 25 00 00 48 01 54 00 01 f7 01 ec   .....%..H.T.....
          149  +    |    464: 01 c5 01 0d 24 00 00 48 01 54 00 01 f7 01 ec 01   ....$..H.T......
          150  +    |    480: c5 01 0d 23 00 00 48 01 54 00 01 f7 01 ec 01 c5   ...#..H.T.......
          151  +    |    496: 01 0d 22 00 00 48 01 54 00 01 f7 01 ec 01 c5 01   .."..H.T........
          152  +    | page 11 offset 5120
          153  +    |      0: 0d 00 00 00 0a 01 6a 00 01 f1 01 e2 01 d3 01 c4   ......j.........
          154  +    |     16: 01 b5 01 a6 01 97 01 88 01 79 01 6a 00 00 00 00   .........y.j....
          155  +    |    352: 00 00 00 00 00 00 00 00 00 00 0d 48 00 00 48 01   ...........H..H.
          156  +    |    368: 54 00 01 f7 01 ec 01 c5 01 0d 47 00 00 48 01 54   T.........G..H.T
          157  +    |    384: 00 01 f7 01 ec 01 c5 01 0d 46 00 00 48 01 54 00   .........F..H.T.
          158  +    |    400: 01 f7 01 ec 01 c5 01 0d 45 00 00 48 01 54 00 01   ........E..H.T..
          159  +    |    416: f7 01 ec 01 c5 01 0d 44 00 00 48 01 54 00 01 f7   .......D..H.T...
          160  +    |    432: 01 ec 01 c5 01 0d 43 00 00 48 01 54 00 01 f7 01   ......C..H.T....
          161  +    |    448: ec 01 c5 01 0d 42 00 00 48 01 54 00 01 f7 01 ec   .....B..H.T.....
          162  +    |    464: 01 c5 01 0d 41 00 00 48 01 54 00 01 f7 01 ec 01   ....A..H.T......
          163  +    |    480: c5 01 0d 40 00 00 48 01 54 00 01 f7 01 ec 01 c5   ...@..H.T.......
          164  +    |    496: 01 0d 3f 00 00 48 01 54 00 01 f7 01 ec 01 c5 01   ..?..H.T........
          165  +    | end c4.db
          166  +  }]
          167  +  db eval {PRAGMA integrity_check}
          168  +} {/Fragmentation of 384 bytes reported as 0 on page 8/}
          169  +
          170  +finish_test