/ Check-in [7fd4dd95]
Login
SQLite training in Houston TX on 2019-11-05 (details)
Part of the 2019 Tcl Conference

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

Overview
Comment:Add fuzz3.test. For testing the library's response to corrupted database files. (CVS 5685)
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: 7fd4dd95791974b0ef6cf30a186b32df25ee2211
User & Date: danielk1977 2008-09-09 18:28:07
Context
2008-09-10
10:57
Fix some test script details so that the test suite runs with an SQLITE_DEFAULT_AUTOVACUUM=2 build. (CVS 5686) check-in: 300a64b7 user: danielk1977 tags: trunk
2008-09-09
18:28
Add fuzz3.test. For testing the library's response to corrupted database files. (CVS 5685) check-in: 7fd4dd95 user: danielk1977 tags: trunk
12:31
Calling sqlite3_create_function with nArg==(-1) does not override prior calls on the same function name with nArg>=0. Ticket #3345. Add the new -argcount option to the "function" method in the TCL interface. (CVS 5684) check-in: 5aa5b804 user: drh tags: trunk
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to test/all.test.

     6      6   #    May you do good and not evil.
     7      7   #    May you find forgiveness for yourself and forgive others.
     8      8   #    May you share freely, never taking more than you give.
     9      9   #
    10     10   #***********************************************************************
    11     11   # This file runs all tests.
    12     12   #
    13         -# $Id: all.test,v 1.57 2008/07/14 15:11:20 drh Exp $
           13  +# $Id: all.test,v 1.58 2008/09/09 18:28:07 danielk1977 Exp $
    14     14   
    15     15   set testdir [file dirname $argv0]
    16     16   source $testdir/tester.tcl
    17     17   rename finish_test really_finish_test
    18     18   proc finish_test {} {
    19     19     # no-op
    20     20   }
................................................................................
    60     60   lappend EXCLUDE quick.test             ;# Alternate test driver script
    61     61   lappend EXCLUDE veryquick.test         ;# Alternate test driver script
    62     62   lappend EXCLUDE malloc.test            ;# Run seperately later.
    63     63   lappend EXCLUDE misuse.test            ;# Run seperately later.
    64     64   lappend EXCLUDE memleak.test           ;# Alternate test driver script
    65     65   lappend EXCLUDE permutations.test      ;# Run seperately later.
    66     66   lappend EXCLUDE fuzz.test
           67  +lappend EXCLUDE fuzz3.test
    67     68   lappend EXCLUDE soak.test              ;# Takes a very long time (default 1 hr)
    68     69   lappend EXCLUDE fts3.test              ;# Wrapper for muliple fts3*.tests
    69     70   lappend EXCLUDE mallocAll.test         ;# Wrapper for running all malloc tests
    70     71   
    71     72   # Files to include in the test.  If this list is empty then everything
    72     73   # that is not in the EXCLUDE list is run.
    73     74   #

Added test/fuzz3.test.

            1  +# 2007 May 10
            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  +# This file implements regression tests for SQLite library.  The focus
           12  +# of this file is checking the libraries response to subtly corrupting
           13  +# the database file by changing the values of pseudo-randomly selected
           14  +# bytes.
           15  +#
           16  +# $Id: fuzz3.test,v 1.1 2008/09/09 18:28:07 danielk1977 Exp $
           17  +
           18  +set testdir [file dirname $argv0]
           19  +source $testdir/tester.tcl
           20  +
           21  +
           22  +expr srand(123)
           23  +
           24  +proc rstring {n} {
           25  +  set str s
           26  +  while {[string length $str] < $n} {
           27  +    append str [expr rand()]
           28  +  }
           29  +  return [string range $str 0 $n]
           30  +}
           31  +
           32  +# Return a randomly generated SQL literal.
           33  +#
           34  +proc rvalue {} {
           35  +  switch -- [expr int(rand()*5)] {
           36  +    0 { # SQL NULL value.
           37  +      return NULL 
           38  +    }
           39  +    1 { # Integer value.
           40  +      return [expr int(rand()*1024)] 
           41  +    }
           42  +    2 { # Real value.
           43  +      return [expr rand()] 
           44  +    }
           45  +    3 { # String value.
           46  +      set n [expr int(rand()*2500)]
           47  +      return "'[rstring $n]'"
           48  +    }
           49  +    4 { # Blob value.
           50  +      set n [expr int(rand()*2500)]
           51  +      return "CAST('[rstring $n]' AS BLOB)"
           52  +    }
           53  +  }
           54  +}
           55  +
           56  +proc db_checksum {} {
           57  +  set    cksum [execsql { SELECT md5sum(a, b, c) FROM t1 }]
           58  +  append cksum [execsql { SELECT md5sum(d, e, f) FROM t2 }]
           59  +  set cksum
           60  +}
           61  +
           62  +# Modify a single byte in the file 'test.db' using tcl IO commands. The
           63  +# argument value, which must be an integer, determines both the offset of
           64  +# the byte that is modified, and the value that it is set to. The lower
           65  +# 8 bits of iMod determine the new byte value. The offset of the byte
           66  +# modified is the value of ($iMod >> 8).
           67  +#
           68  +# The return value is the iMod value required to restore the file
           69  +# to its original state. The command:
           70  +#
           71  +#   modify_database [modify_database $x]
           72  +#
           73  +# leaves the file in the same state as it was in at the start of the
           74  +# command (assuming that the file is at least ($x>>8) bytes in size).
           75  +#
           76  +proc modify_database {iMod} {
           77  +  set blob [binary format c [expr {$iMod&0xFF}]]
           78  +  set offset [expr {$iMod>>8}]
           79  +
           80  +  set fd [open test.db r+]
           81  +  fconfigure $fd -encoding binary -translation binary
           82  +  seek $fd $offset
           83  +  set old_blob [read $fd 1]
           84  +  seek $fd $offset
           85  +  puts -nonewline $fd $blob
           86  +  close $fd
           87  +
           88  +  binary scan $old_blob c iOld
           89  +  return [expr {($offset<<8) + ($iOld&0xFF)}]
           90  +}
           91  +
           92  +proc purge_pcache {} {
           93  +  ifcapable !memorymanage {
           94  +    db close
           95  +    sqlite3 db test.db
           96  +  } else {
           97  +    sqlite3_release_memory 10000000
           98  +  }
           99  +  if {[lindex [pcache_stats] 1] != 0} {
          100  +    error "purge_pcache failed: [pcache_stats]"
          101  +  }
          102  +}
          103  +
          104  +# This block creates a database to work with. 
          105  +#
          106  +do_test fuzz3-1 {
          107  +  execsql {
          108  +    BEGIN;
          109  +    CREATE TABLE t1(a, b, c);
          110  +    CREATE TABLE t2(d, e, f);
          111  +    CREATE INDEX i1 ON t1(a, b, c);
          112  +    CREATE INDEX i2 ON t2(d, e, f);
          113  +  }
          114  +  for {set i 0} {$i < 50} {incr i} {
          115  +    execsql "INSERT INTO t1 VALUES([rvalue], [rvalue], [rvalue])"
          116  +    execsql "INSERT INTO t2 VALUES([rvalue], [rvalue], [rvalue])"
          117  +  }
          118  +  execsql COMMIT
          119  +} {}
          120  +
          121  +set ::cksum [db_checksum]
          122  +do_test fuzz3-2 {
          123  +  db_checksum
          124  +} $::cksum
          125  +
          126  +for {set ii 0} {$ii < 10000} {incr ii} {
          127  +  purge_pcache
          128  +
          129  +  # Randomly modify a single byte of the database file somewhere within
          130  +  # the first 100KB of the file.
          131  +  set iNew [expr int(rand()*5*1024*256)]
          132  +  set iOld [modify_database $iNew]
          133  +
          134  +  set iTest 2
          135  +  foreach sql {
          136  +    {SELECT * FROM t1}                 
          137  +    {SELECT * FROM t2}                 
          138  +    {SELECT * FROM t1 ORDER BY a}      
          139  +    {SELECT * FROM t2 ORDER BY d}      
          140  +    {SELECT * FROM t1 WHERE a = (SELECT a FROM t1 WHERE rowid=25)} 
          141  +    {SELECT * FROM t2 WHERE d = (SELECT d FROM t2 WHERE rowid=1)}  
          142  +    {SELECT * FROM t2 WHERE d = (SELECT d FROM t2 WHERE rowid=50)} 
          143  +    {PRAGMA integrity_check}           
          144  +  } {
          145  +    do_test fuzz3-$ii.$iNew.[incr iTest] {
          146  +      foreach {rc msg} [catchsql $sql] {}
          147  +      if {$rc == 0 
          148  +       || $msg eq "database disk image is malformed"
          149  +       || $msg eq "file is encrypted or is not a database"
          150  +       || [string match "malformed database schema*" $msg]
          151  +      } {
          152  +        set msg ok
          153  +      }
          154  +      set msg
          155  +    } {ok}
          156  +  }
          157  +
          158  +  # Restore the original database file content. Test that the correct 
          159  +  # checksum is now returned.
          160  +  #
          161  +  purge_pcache
          162  +  modify_database $iOld
          163  +  do_test fuzz3-$ii.$iNew.[incr iTest] {
          164  +    db_checksum
          165  +  } $::cksum
          166  +}
          167  +
          168  +finish_test
          169  +

Changes to test/quick.test.

     2      2   #    May you do good and not evil.
     3      3   #    May you find forgiveness for yourself and forgive others.
     4      4   #    May you share freely, never taking more than you give.
     5      5   #
     6      6   #***********************************************************************
     7      7   # This file runs all tests.
     8      8   #
     9         -# $Id: quick.test,v 1.86 2008/08/28 08:31:48 danielk1977 Exp $
            9  +# $Id: quick.test,v 1.87 2008/09/09 18:28:07 danielk1977 Exp $
    10     10   
    11     11   proc lshift {lvar} {
    12     12     upvar $lvar l
    13     13     set ret [lindex $l 0]
    14     14     set l [lrange $l 1 end]
    15     15     return $ret
    16     16   }
................................................................................
    50     50     crash4.test
    51     51     crash5.test
    52     52     crash6.test
    53     53     crash7.test
    54     54     delete3.test
    55     55     fts3.test
    56     56     fuzz.test
           57  +  fuzz3.test
    57     58     fuzz_malloc.test
    58     59     in2.test
    59     60     loadext.test
    60     61     memleak.test
    61     62     misc7.test
    62     63     misuse.test
    63     64     mutex2.test