/ Check-in [b076e165]
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:Update the rest of the malloc failure test scripts to use the new interface. (CVS 4296)
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: b076e1655d6bae5ae10e6ceee646f502435da66a
User & Date: danielk1977 2007-08-25 13:37:49
Context
2007-08-25
14:39
Use recursive mutexes in pthreads. If the pthreads implementation does not support recursive mutexes, then you cannot compile SQLite with SQLITE_THREADSAFE=1. Ticket #2588. (CVS 4297) check-in: 1668284d user: drh tags: trunk
13:37
Update the rest of the malloc failure test scripts to use the new interface. (CVS 4296) check-in: b076e165 user: danielk1977 tags: trunk
13:09
Fix bugs in the altermalloc.test and incrblob_err.test scripts. (CVS 4295) check-in: e5ec1707 user: danielk1977 tags: trunk
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to src/test8.c.

     9      9   **    May you share freely, never taking more than you give.
    10     10   **
    11     11   *************************************************************************
    12     12   ** Code for testing the virtual table interfaces.  This code
    13     13   ** is not included in the SQLite library.  It is used for automated
    14     14   ** testing of the SQLite library.
    15     15   **
    16         -** $Id: test8.c,v 1.53 2007/08/23 02:47:53 drh Exp $
           16  +** $Id: test8.c,v 1.54 2007/08/25 13:37:49 danielk1977 Exp $
    17     17   */
    18     18   #include "sqliteInt.h"
    19     19   #include "tcl.h"
    20     20   #include <stdlib.h>
    21     21   #include <string.h>
    22     22   
    23     23   #ifndef SQLITE_OMIT_VIRTUALTABLE
................................................................................
   299    299     echo_vtab *pVtab, 
   300    300     sqlite3 *db 
   301    301   ){
   302    302     int rc = SQLITE_OK;
   303    303   
   304    304     if( pVtab->zTableName ){
   305    305       sqlite3_stmt *pStmt = 0;
   306         -    sqlite3_prepare(db, 
          306  +    rc = sqlite3_prepare(db, 
   307    307           "SELECT sql FROM sqlite_master WHERE type = 'table' AND name = ?",
   308    308           -1, &pStmt, 0);
   309         -    sqlite3_bind_text(pStmt, 1, pVtab->zTableName, -1, 0);
   310         -    if( sqlite3_step(pStmt)==SQLITE_ROW ){
   311         -      int rc2;
   312         -      const char *zCreateTable = (const char *)sqlite3_column_text(pStmt, 0);
   313         -      rc = sqlite3_declare_vtab(db, zCreateTable);
   314         -      rc2 = sqlite3_finalize(pStmt);
   315         -      if( rc==SQLITE_OK ){
   316         -        rc = rc2;
          309  +    if( rc==SQLITE_OK ){
          310  +      sqlite3_bind_text(pStmt, 1, pVtab->zTableName, -1, 0);
          311  +      if( sqlite3_step(pStmt)==SQLITE_ROW ){
          312  +        int rc2;
          313  +        const char *zCreateTable = (const char *)sqlite3_column_text(pStmt, 0);
          314  +        rc = sqlite3_declare_vtab(db, zCreateTable);
          315  +        rc2 = sqlite3_finalize(pStmt);
          316  +        if( rc==SQLITE_OK ){
          317  +          rc = rc2;
          318  +        }
          319  +      } else {
          320  +        rc = sqlite3_finalize(pStmt);
          321  +        if( rc==SQLITE_OK ){ 
          322  +          rc = SQLITE_ERROR;
          323  +        }
   317    324         }
   318         -    } else {
   319         -      rc = sqlite3_finalize(pStmt);
   320         -      if( rc==SQLITE_OK ){ 
   321         -        rc = SQLITE_ERROR;
          325  +      if( rc==SQLITE_OK ){
          326  +        rc = getColumnNames(db, pVtab->zTableName, &pVtab->aCol, &pVtab->nCol);
   322    327         }
   323         -    }
   324         -    if( rc==SQLITE_OK ){
   325         -      rc = getColumnNames(db, pVtab->zTableName, &pVtab->aCol, &pVtab->nCol);
   326         -    }
   327         -    if( rc==SQLITE_OK ){
   328         -      rc = getIndexArray(db, pVtab->zTableName, pVtab->nCol, &pVtab->aIndex);
          328  +      if( rc==SQLITE_OK ){
          329  +        rc = getIndexArray(db, pVtab->zTableName, pVtab->nCol, &pVtab->aIndex);
          330  +      }
   329    331       }
   330    332     }
   331    333   
   332    334     return rc;
   333    335   }
   334    336   
   335    337   /*
................................................................................
   780    782       string_concat(&zQuery, zNew, 1);
   781    783       pIdxInfo->orderByConsumed = 1;
   782    784     }
   783    785   
   784    786     appendToEchoModule(pVtab->interp, "xBestIndex");;
   785    787     appendToEchoModule(pVtab->interp, zQuery);
   786    788   
          789  +  if( !zQuery ){
          790  +    return SQLITE_NOMEM;
          791  +  }
   787    792     pIdxInfo->idxNum = hashString(zQuery);
   788    793     pIdxInfo->idxStr = zQuery;
   789    794     pIdxInfo->needToFreeIdxStr = 1;
   790    795     if (useCost) {
   791    796       pIdxInfo->estimatedCost = cost;
   792    797     } else if( useIdx ){
   793    798       /* Approximation of log2(nRow). */

Changes to test/attachmalloc.test.

     8      8   #    May you share freely, never taking more than you give.
     9      9   #
    10     10   #*************************************************************************
    11     11   # This file implements regression tests for SQLite library.  The
    12     12   # focus of this script is testing the ATTACH statement and
    13     13   # specifically out-of-memory conditions within that command.
    14     14   #
    15         -# $Id: attachmalloc.test,v 1.3 2006/09/04 18:54:14 drh Exp $
           15  +# $Id: attachmalloc.test,v 1.4 2007/08/25 13:37:49 danielk1977 Exp $
    16     16   #
    17     17   
    18     18   set testdir [file dirname $argv0]
    19     19   source $testdir/tester.tcl
    20     20   
    21         -# Only run these tests if memory debugging is turned on.
    22         -#
    23         -if {[info command sqlite_malloc_stat]==""} {
    24         -  puts "Skipping malloc tests: not compiled with -DSQLITE_MEMDEBUG=1"
    25         -  finish_test
    26         -  return
    27         -}
    28         -
    29         -
    30         -# Usage: do_malloc_test <test name> <options...>
    31         -#
    32         -# The first argument, <test number>, is an integer used to name the
    33         -# tests executed by this proc. Options are as follows:
    34         -#
    35         -#     -tclprep          TCL script to run to prepare test.
    36         -#     -sqlprep          SQL script to run to prepare test.
    37         -#     -tclbody          TCL script to run with malloc failure simulation.
    38         -#     -sqlbody          TCL script to run with malloc failure simulation.
    39         -#     -cleanup          TCL script to run after the test.
    40         -#
    41         -# This command runs a series of tests to verify SQLite's ability
    42         -# to handle an out-of-memory condition gracefully. It is assumed
    43         -# that if this condition occurs a malloc() call will return a
    44         -# NULL pointer. Linux, for example, doesn't do that by default. See
    45         -# the "BUGS" section of malloc(3).
    46         -#
    47         -# Each iteration of a loop, the TCL commands in any argument passed
    48         -# to the -tclbody switch, followed by the SQL commands in any argument
    49         -# passed to the -sqlbody switch are executed. Each iteration the
    50         -# Nth call to sqliteMalloc() is made to fail, where N is increased
    51         -# each time the loop runs starting from 1. When all commands execute
    52         -# successfully, the loop ends.
    53         -#
    54         -proc do_malloc_test {tn args} {
    55         -  array set ::mallocopts $args
    56         -
    57         -  set ::go 1
    58         -  for {set ::n 1} {$::go} {incr ::n} {
    59         -
    60         -    do_test $tn.$::n {
    61         -
    62         -      sqlite_malloc_fail 0
    63         -      catch {db close}
    64         -      catch {file delete -force test.db}
    65         -      catch {file delete -force test.db-journal}
    66         -      catch {file delete -force test2.db}
    67         -      catch {file delete -force test2.db-journal}
    68         -      set ::DB [sqlite3 db test.db]
    69         -
    70         -      if {[info exists ::mallocopts(-tclprep)]} {
    71         -        eval $::mallocopts(-tclprep)
    72         -      }
    73         -      if {[info exists ::mallocopts(-sqlprep)]} {
    74         -        execsql $::mallocopts(-sqlprep)
    75         -      }
    76         -
    77         -      sqlite_malloc_fail $::n
    78         -      set ::mallocbody {}
    79         -      if {[info exists ::mallocopts(-tclbody)]} {
    80         -        append ::mallocbody "$::mallocopts(-tclbody)\n"
    81         -      }
    82         -      if {[info exists ::mallocopts(-sqlbody)]} {
    83         -        append ::mallocbody "db eval {$::mallocopts(-sqlbody)}"
    84         -      }
    85         -
    86         -      set v [catch $::mallocbody msg]
    87         -
    88         -      set leftover [lindex [sqlite_malloc_stat] 2]
    89         -      if {$leftover>0} {
    90         -        if {$leftover>1} {puts "\nLeftover: $leftover\nReturn=$v  Message=$msg"}
    91         -        set ::go 0
    92         -        set v {1 1}
    93         -      } else {
    94         -        set v2 [expr {$msg=="" || $msg=="out of memory"}]
    95         -        if {!$v2} {puts "\nError message returned: $msg"}
    96         -        lappend v $v2
    97         -      }
    98         -    } {1 1}
    99         -    sqlite_malloc_fail 0
   100         -
   101         -    if {[info exists ::mallocopts(-cleanup)]} {
   102         -      catch $::mallocopts(-cleanup)
   103         -    }
   104         -  }
   105         -  unset ::mallocopts
   106         -}
           21  +source $testdir/malloc_common.tcl
   107     22   
   108     23   do_malloc_test attachmalloc-1 -tclprep {
   109     24     db close
   110     25     for {set i 2} {$i<=4} {incr i} {
   111     26       file delete -force test$i.db
   112     27       file delete -force test$i.db-journal
   113     28     }

Changes to test/capi3.test.

     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 implements regression tests for SQLite library.  The
    12     12   # focus of this script testing the callback-free C/C++ API.
    13     13   #
    14         -# $Id: capi3.test,v 1.52 2007/08/22 02:56:44 drh Exp $
           14  +# $Id: capi3.test,v 1.53 2007/08/25 13:37:49 danielk1977 Exp $
    15     15   #
    16     16   
    17     17   set testdir [file dirname $argv0]
    18     18   source $testdir/tester.tcl
    19     19   
    20     20   # Return the UTF-16 representation of the supplied UTF-8 string $str.
    21     21   # If $nt is true, append two 0x00 bytes as a nul terminator.
................................................................................
   746    746   set test_number 1
   747    747   foreach {code english} $code2english {
   748    748     do_test capi3-9.$test_number "sqlite3_test_errstr $code" $english
   749    749     incr test_number
   750    750   }
   751    751   
   752    752   # Test the error message when a "real" out of memory occurs.
   753         -if {[info command sqlite_malloc_stat]!=""} {
   754         -set sqlite_malloc_fail 1
   755         -do_test capi3-10-1 {
   756         -  sqlite3 db test.db
   757         -  set DB [sqlite3_connection_pointer db]
   758         -  sqlite_malloc_fail 1
   759         -  catchsql {
   760         -    select * from sqlite_master;
   761         -  }
   762         -} {1 {out of memory}}
   763         -do_test capi3-10-2 {
   764         -  sqlite3_errmsg $::DB
   765         -} {out of memory}
   766         -ifcapable {utf16} {
   767         -  do_test capi3-10-3 {
   768         -    utf8 [sqlite3_errmsg16 $::DB]
          753  +if {[info command sqlite3_memdebug_fail]!=""} {
          754  +  do_test capi3-10-1 {
          755  +    sqlite3 db test.db
          756  +    set DB [sqlite3_connection_pointer db]
          757  +    sqlite3_memdebug_fail 0 0
          758  +    catchsql {
          759  +      select * from sqlite_master;
          760  +    }
          761  +  } {1 {out of memory}}
          762  +  do_test capi3-10-2 {
          763  +    sqlite3_errmsg $::DB
   769    764     } {out of memory}
   770         -}
   771         -db close
   772         -sqlite_malloc_fail 0
          765  +  ifcapable {utf16} {
          766  +    do_test capi3-10-3 {
          767  +      utf8 [sqlite3_errmsg16 $::DB]
          768  +    } {out of memory}
          769  +  }
          770  +  db close
          771  +  sqlite3_memdebug_fail -1 0
   773    772   }
   774    773   
   775    774   # The following tests - capi3-11.* - test that a COMMIT or ROLLBACK
   776    775   # statement issued while there are still outstanding VMs that are part of
   777    776   # the transaction fails.
   778    777   sqlite3 db test.db
   779    778   set DB [sqlite3_connection_pointer db]

Changes to test/capi3c.test.

     9      9   #
    10     10   #***********************************************************************
    11     11   # This file implements regression tests for SQLite library.  
    12     12   #
    13     13   # This is a copy of the capi3.test file that has been adapted to
    14     14   # test the new sqlite3_prepare_v2 interface.
    15     15   #
    16         -# $Id: capi3c.test,v 1.9 2007/07/19 22:30:19 drh Exp $
           16  +# $Id: capi3c.test,v 1.10 2007/08/25 13:37:49 danielk1977 Exp $
    17     17   #
    18     18   
    19     19   set testdir [file dirname $argv0]
    20     20   source $testdir/tester.tcl
    21     21   
    22     22   # Return the UTF-16 representation of the supplied UTF-8 string $str.
    23     23   # If $nt is true, append two 0x00 bytes as a nul terminator.
................................................................................
   741    741   set test_number 1
   742    742   foreach {code english} $code2english {
   743    743     do_test capi3c-9.$test_number "sqlite3_test_errstr $code" $english
   744    744     incr test_number
   745    745   }
   746    746   
   747    747   # Test the error message when a "real" out of memory occurs.
   748         -if {[info command sqlite_malloc_stat]!=""} {
   749         -set sqlite_malloc_fail 1
   750         -do_test capi3c-10-1 {
   751         -  sqlite3 db test.db
   752         -  set DB [sqlite3_connection_pointer db]
   753         -  sqlite_malloc_fail 1
   754         -  catchsql {
   755         -    select * from sqlite_master;
   756         -  }
   757         -} {1 {out of memory}}
   758         -do_test capi3c-10-2 {
   759         -  sqlite3_errmsg $::DB
   760         -} {out of memory}
   761         -ifcapable {utf16} {
   762         -  do_test capi3c-10-3 {
   763         -    utf8 [sqlite3_errmsg16 $::DB]
          748  +if {[info command sqlite3_memdebug_fail]!=""} {
          749  +  do_test capi3c-10-1 {
          750  +    sqlite3 db test.db
          751  +    set DB [sqlite3_connection_pointer db]
          752  +    sqlite3_memdebug_fail 0 0
          753  +    catchsql {
          754  +      select * from sqlite_master;
          755  +    }
          756  +  } {1 {out of memory}}
          757  +  do_test capi3c-10-2 {
          758  +    sqlite3_errmsg $::DB
   764    759     } {out of memory}
   765         -}
   766         -db close
   767         -sqlite_malloc_fail 0
          760  +  ifcapable {utf16} {
          761  +    do_test capi3c-10-3 {
          762  +      utf8 [sqlite3_errmsg16 $::DB]
          763  +    } {out of memory}
          764  +  }
          765  +  db close
          766  +  sqlite3_memdebug_fail -1 0
   768    767   }
   769    768   
   770    769   # The following tests - capi3c-11.* - test that a COMMIT or ROLLBACK
   771    770   # statement issued while there are still outstanding VMs that are part of
   772    771   # the transaction fails.
   773    772   sqlite3 db test.db
   774    773   set DB [sqlite3_connection_pointer db]

Changes to test/fuzz_malloc.test.

     8      8   #    May you find forgiveness for yourself and forgive others.
     9      9   #    May you share freely, never taking more than you give.
    10     10   #
    11     11   #***********************************************************************
    12     12   #
    13     13   # This file tests malloc failures in concert with fuzzy SQL generation.
    14     14   #
    15         -# $Id: fuzz_malloc.test,v 1.5 2007/06/18 12:22:43 drh Exp $
           15  +# $Id: fuzz_malloc.test,v 1.6 2007/08/25 13:37:49 danielk1977 Exp $
    16     16   
    17     17   set testdir [file dirname $argv0]
    18     18   source $testdir/tester.tcl
    19     19   
    20         -# Only run these tests if memory debugging is turned on.
    21         -#
    22         -if {[info command sqlite_malloc_stat]==""} {
    23         -  puts "Skipping fuzz_malloc tests: not compiled with -DSQLITE_MEMDEBUG=1"
    24         -  finish_test
    25         -  return
    26         -}
    27         -
    28         -source $testdir/fuzz_common.tcl
    29     20   source $testdir/malloc_common.tcl
           21  +source $testdir/fuzz_common.tcl
    30     22   
    31     23   if {[info exists ISQUICK]} {
    32     24     set ::REPEATS 20
    33     25   } elseif {[info exists SOAKTEST]} {
    34     26     set ::REPEATS 100
    35     27   } else {
    36     28     set ::REPEATS 40
................................................................................
    44     36   #     -repeats
    45     37   #     
    46     38   proc do_fuzzy_malloc_test {testname args} {
    47     39     set ::fuzzyopts(-repeats) $::REPEATS
    48     40     set ::fuzzyopts(-sqlprep) {}
    49     41     array set ::fuzzyopts $args
    50     42   
    51         -  sqlite_malloc_fail 0
           43  +  sqlite3_memdebug_fail -1 0
    52     44     db close
    53     45     file delete test.db test.db-journal
    54     46     sqlite3 db test.db
    55     47     set ::prep $::fuzzyopts(-sqlprep)
    56     48     execsql $::prep
    57     49     set jj 0
    58     50     for {set ii 0} {$ii < $::fuzzyopts(-repeats)} {incr ii} {
................................................................................
    89     81   set ::TableList  [list abc def ghi]
    90     82   set ::ColumnList [list a b c]
    91     83   
    92     84   do_fuzzy_malloc_test fuzzy_malloc-3 \
    93     85     -template {[Select]}              \
    94     86     -sqlprep $::SQLPREP
    95     87   
    96         -sqlite_malloc_fail 0
           88  +sqlite3_memdebug_fail -1 0
    97     89   finish_test

Changes to test/shared_err.test.

     9      9   #
    10     10   #***********************************************************************
    11     11   #
    12     12   # The focus of the tests in this file are IO errors that occur in a shared
    13     13   # cache context. What happens to connection B if one connection A encounters
    14     14   # an IO-error whilst reading or writing the file-system?
    15     15   #
    16         -# $Id: shared_err.test,v 1.11 2007/04/06 01:04:40 drh Exp $
           16  +# $Id: shared_err.test,v 1.12 2007/08/25 13:37:49 danielk1977 Exp $
    17     17   
    18     18   proc skip {args} {}
    19     19   
    20     20   
    21     21   set testdir [file dirname $argv0]
    22     22   source $testdir/tester.tcl
           23  +source $testdir/malloc_common.tcl
    23     24   db close
    24     25   
    25     26   ifcapable !shared_cache||!subquery {
    26     27     finish_test
    27     28     return
    28     29   }
           30  +
    29     31   set ::enable_shared_cache [sqlite3_enable_shared_cache 1]
    30         -
    31         -# Todo: This is a copy of the [do_malloc_test] proc in malloc.test
    32         -# It would be better if these were consolidated.
    33         -
    34         -# Usage: do_malloc_test <test number> <options...>
    35         -#
    36         -# The first argument, <test number>, is an integer used to name the
    37         -# tests executed by this proc. Options are as follows:
    38         -#
    39         -#     -tclprep          TCL script to run to prepare test.
    40         -#     -sqlprep          SQL script to run to prepare test.
    41         -#     -tclbody          TCL script to run with malloc failure simulation.
    42         -#     -sqlbody          TCL script to run with malloc failure simulation.
    43         -#     -cleanup          TCL script to run after the test.
    44         -#
    45         -# This command runs a series of tests to verify SQLite's ability
    46         -# to handle an out-of-memory condition gracefully. It is assumed
    47         -# that if this condition occurs a malloc() call will return a
    48         -# NULL pointer. Linux, for example, doesn't do that by default. See
    49         -# the "BUGS" section of malloc(3).
    50         -#
    51         -# Each iteration of a loop, the TCL commands in any argument passed
    52         -# to the -tclbody switch, followed by the SQL commands in any argument
    53         -# passed to the -sqlbody switch are executed. Each iteration the
    54         -# Nth call to sqliteMalloc() is made to fail, where N is increased
    55         -# each time the loop runs starting from 1. When all commands execute
    56         -# successfully, the loop ends.
    57         -#
    58         -proc do_malloc_test {tn args} {
    59         -  array unset ::mallocopts 
    60         -  array set ::mallocopts $args
    61         -
    62         -  set ::go 1
    63         -  for {set ::n 1} {$::go && $::n < 50000} {incr ::n} {
    64         -    do_test shared_malloc-$tn.$::n {
    65         -
    66         -      # Remove all traces of database files test.db and test2.db from the files
    67         -      # system. Then open (empty database) "test.db" with the handle [db].
    68         -      # 
    69         -      sqlite_malloc_fail 0
    70         -      catch {db close} 
    71         -      catch {file delete -force test.db}
    72         -      catch {file delete -force test.db-journal}
    73         -      catch {file delete -force test2.db}
    74         -      catch {file delete -force test2.db-journal}
    75         -      catch {sqlite3 db test.db} 
    76         -      set ::DB [sqlite3_connection_pointer db]
    77         -
    78         -      # Execute any -tclprep and -sqlprep scripts.
    79         -      #
    80         -      if {[info exists ::mallocopts(-tclprep)]} {
    81         -        eval $::mallocopts(-tclprep)
    82         -      }
    83         -      if {[info exists ::mallocopts(-sqlprep)]} {
    84         -        execsql $::mallocopts(-sqlprep)
    85         -      }
    86         -
    87         -      # Now set the ${::n}th malloc() to fail and execute the -tclbody and
    88         -      # -sqlbody scripts.
    89         -      #
    90         -      sqlite_malloc_fail $::n
    91         -      set ::mallocbody {}
    92         -      if {[info exists ::mallocopts(-tclbody)]} {
    93         -        append ::mallocbody "$::mallocopts(-tclbody)\n"
    94         -      }
    95         -      if {[info exists ::mallocopts(-sqlbody)]} {
    96         -        append ::mallocbody "db eval {$::mallocopts(-sqlbody)}"
    97         -      }
    98         -      set v [catch $::mallocbody msg]
    99         -
   100         -      set leftover [lindex [sqlite_malloc_stat] 2]
   101         -      if {$leftover>0} {
   102         -        if {$leftover>1} {puts "\nLeftover: $leftover\nReturn=$v  Message=$msg"}
   103         -        set ::go 0
   104         -        if {$v} {
   105         -          puts "\nError message returned: $msg"
   106         -        } else {
   107         -          set v {1 1}
   108         -        }
   109         -      } else {
   110         -        set v2 [expr {$msg=="" || $msg=="out of memory"}]
   111         -        if {!$v2} {puts "\nError message returned: $msg"}
   112         -        lappend v $v2
   113         -      }
   114         -    } {1 1}
   115         -
   116         -    sqlite_malloc_fail 0
   117         -    if {[info exists ::mallocopts(-cleanup)]} {
   118         -      catch [list uplevel #0 $::mallocopts(-cleanup)] msg
   119         -    }
   120         -  }
   121         -  unset ::mallocopts
   122         -}
   123         -
   124     32   
   125     33   do_ioerr_test shared_ioerr-1 -tclprep {
   126     34     sqlite3 db2 test.db
   127     35     execsql {
   128     36       PRAGMA read_uncommitted = 1;
   129     37       CREATE TABLE t1(a,b,c);
   130     38       BEGIN;

Changes to test/vtab_err.test.

     5      5   #
     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   #
    12         -# $Id: vtab_err.test,v 1.5 2007/04/19 11:09:02 danielk1977 Exp $
           12  +# $Id: vtab_err.test,v 1.6 2007/08/25 13:37:49 danielk1977 Exp $
    13     13   
    14     14   set testdir [file dirname $argv0]
    15     15   source $testdir/tester.tcl
    16     16   
    17         -# Only run these tests if memory debugging is turned on.
    18         -#
    19         -if {[info command sqlite_malloc_stat]==""} {
    20         -  puts "Skipping vtab_err tests: not compiled with -DSQLITE_MEMDEBUG=1"
    21         -  finish_test
    22         -  return
    23         -}
           17  +source $testdir/malloc_common.tcl
    24     18   
    25     19   ifcapable !vtab {
    26     20     finish_test
    27     21     return
    28     22   }
    29     23   
    30         -# Usage: do_malloc_test <test number> <options...>
    31         -#
    32         -# The first argument, <test number>, is an integer used to name the
    33         -# tests executed by this proc. Options are as follows:
    34         -#
    35         -#     -tclprep          TCL script to run to prepare test.
    36         -#     -sqlprep          SQL script to run to prepare test.
    37         -#     -tclbody          TCL script to run with malloc failure simulation.
    38         -#     -sqlbody          TCL script to run with malloc failure simulation.
    39         -#     -cleanup          TCL script to run after the test.
    40         -#
    41         -# This command runs a series of tests to verify SQLite's ability
    42         -# to handle an out-of-memory condition gracefully. It is assumed
    43         -# that if this condition occurs a malloc() call will return a
    44         -# NULL pointer. Linux, for example, doesn't do that by default. See
    45         -# the "BUGS" section of malloc(3).
    46         -#
    47         -# Each iteration of a loop, the TCL commands in any argument passed
    48         -# to the -tclbody switch, followed by the SQL commands in any argument
    49         -# passed to the -sqlbody switch are executed. Each iteration the
    50         -# Nth call to sqliteMalloc() is made to fail, where N is increased
    51         -# each time the loop runs starting from 1. When all commands execute
    52         -# successfully, the loop ends.
    53         -#
    54         -proc do_malloc_test {tn args} {
    55         -  array unset ::mallocopts 
    56         -  array set ::mallocopts $args
    57         -
    58         -  set ::go 1
    59         -  for {set ::n 1} {$::go && $::n < 50000} {incr ::n} {
    60         -    do_test $tn.$::n {
    61         -
    62         -      # Remove all traces of database files test.db and test2.db from the files
    63         -      # system. Then open (empty database) "test.db" with the handle [db].
    64         -      # 
    65         -      sqlite_malloc_fail 0
    66         -      catch {db close} 
    67         -      catch {file delete -force test.db}
    68         -      catch {file delete -force test.db-journal}
    69         -      catch {file delete -force test2.db}
    70         -      catch {file delete -force test2.db-journal}
    71         -      catch {sqlite3 db test.db} 
    72         -      set ::DB [sqlite3_connection_pointer db]
    73         -
    74         -      # Execute any -tclprep and -sqlprep scripts.
    75         -      #
    76         -      if {[info exists ::mallocopts(-tclprep)]} {
    77         -        eval $::mallocopts(-tclprep)
    78         -      }
    79         -      if {[info exists ::mallocopts(-sqlprep)]} {
    80         -        execsql $::mallocopts(-sqlprep)
    81         -      }
    82         -
    83         -      # Now set the ${::n}th malloc() to fail and execute the -tclbody and
    84         -      # -sqlbody scripts.
    85         -      #
    86         -      sqlite_malloc_fail $::n
    87         -      set ::mallocbody {}
    88         -      if {[info exists ::mallocopts(-tclbody)]} {
    89         -        append ::mallocbody "$::mallocopts(-tclbody)\n"
    90         -      }
    91         -      if {[info exists ::mallocopts(-sqlbody)]} {
    92         -        append ::mallocbody "db eval {$::mallocopts(-sqlbody)}"
    93         -      }
    94         -      set v [catch $::mallocbody msg]
    95         -
    96         -      # If the test fails (if $v!=0) and the database connection actually
    97         -      # exists, make sure the failure code is SQLITE_NOMEM.
    98         -      if {$v&&[info command db]=="db"&&[info exists ::mallocopts(-sqlbody)]} {
    99         -        if {[db errorcode]!=7 && $msg!="vtable constructor failed: e"} {
   100         -          set v 999
   101         -        }
   102         -      }
   103         -
   104         -      set leftover [lindex [sqlite_malloc_stat] 2]
   105         -      if {$leftover>0} {
   106         -        if {$leftover>1} {puts "\nLeftover: $leftover\nReturn=$v  Message=$msg"}
   107         -        set ::go 0
   108         -        if {$v} {
   109         -          puts "\nError message returned: $msg"
   110         -        } else {
   111         -          set v {1 1}
   112         -        }
   113         -      } else {
   114         -        set v2 [expr {
   115         -          $msg == "" || $msg == "out of memory" || 
   116         -          $msg == "vtable constructor failed: e"
   117         -        }]
   118         -        if {!$v2} {puts "\nError message returned: $msg"}
   119         -        lappend v $v2
   120         -      }
   121         -    } {1 1}
   122         -
   123         -    if {[info exists ::mallocopts(-cleanup)]} {
   124         -      catch [list uplevel #0 $::mallocopts(-cleanup)] msg
   125         -    }
   126         -  }
   127         -  unset ::mallocopts
   128         -}
   129         -
   130     24   unset -nocomplain echo_module_begin_fail
   131     25   do_ioerr_test vtab_err-1 -tclprep {
   132     26     register_echo_module [sqlite3_connection_pointer db]
   133     27   } -sqlbody {
   134     28     BEGIN;
   135     29     CREATE TABLE r(a PRIMARY KEY, b, c);
   136     30     CREATE VIRTUAL TABLE e USING echo(r);
................................................................................
   160     54     BEGIN;
   161     55       CREATE TABLE r2(a, b, c);
   162     56       INSERT INTO r2 SELECT * FROM e;
   163     57       INSERT INTO e SELECT a||'x', b, c FROM r2;
   164     58     COMMIT;
   165     59   } 
   166     60   
   167         -sqlite_malloc_fail 0
           61  +sqlite3_memdebug_fail -1 0
           62  +
   168     63   finish_test