drh | b3226e8 | 2007-08-13 12:39:03 +0000 | [diff] [blame] | 1 | # 2007 Aug 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 | # This file tests aspects of the malloc failure while parsing |
| 13 | # CREATE TABLE statements in auto_vacuum mode. |
| 14 | # |
danielk1977 | ae72d98 | 2007-10-03 08:46:44 +0000 | [diff] [blame^] | 15 | # $Id: mallocC.test,v 1.7 2007/10/03 08:46:45 danielk1977 Exp $ |
drh | b3226e8 | 2007-08-13 12:39:03 +0000 | [diff] [blame] | 16 | |
| 17 | set testdir [file dirname $argv0] |
| 18 | source $testdir/tester.tcl |
| 19 | |
| 20 | # Only run these tests if memory debugging is turned on. |
| 21 | # |
danielk1977 | 4152e67 | 2007-09-12 17:01:45 +0000 | [diff] [blame] | 22 | ifcapable !memdebug||!compound { |
drh | 5a3032b | 2007-09-03 16:12:09 +0000 | [diff] [blame] | 23 | puts "Skipping mallocC tests: not compiled with -DSQLITE_MEMDEBUG..." |
drh | ed138fb | 2007-08-22 22:04:37 +0000 | [diff] [blame] | 24 | finish_test |
| 25 | return |
drh | b3226e8 | 2007-08-13 12:39:03 +0000 | [diff] [blame] | 26 | } |
| 27 | |
| 28 | # Generate a checksum based on the contents of the database. If the |
| 29 | # checksum of two databases is the same, and the integrity-check passes |
| 30 | # for both, the two databases are identical. |
| 31 | # |
| 32 | proc cksum {db} { |
| 33 | set ret [list] |
| 34 | ifcapable tempdb { |
| 35 | set sql { |
| 36 | SELECT name FROM sqlite_master WHERE type = 'table' UNION |
| 37 | SELECT name FROM sqlite_temp_master WHERE type = 'table' UNION |
| 38 | SELECT 'sqlite_master' UNION |
| 39 | SELECT 'sqlite_temp_master' |
| 40 | } |
| 41 | } else { |
| 42 | set sql { |
| 43 | SELECT name FROM sqlite_master WHERE type = 'table' UNION |
| 44 | SELECT 'sqlite_master' |
| 45 | } |
| 46 | } |
| 47 | set tbllist [$db eval $sql] |
| 48 | set txt {} |
| 49 | foreach tbl $tbllist { |
| 50 | append txt [$db eval "SELECT * FROM $tbl"] |
| 51 | } |
| 52 | # puts txt=$txt |
| 53 | return [md5 $txt] |
| 54 | } |
| 55 | |
| 56 | proc do_mallocC_test {tn args} { |
| 57 | array set ::mallocopts $args |
| 58 | set sum [cksum db] |
| 59 | |
| 60 | for {set ::n 1} {true} {incr ::n} { |
| 61 | |
| 62 | # Run the SQL. Malloc number $::n is set to fail. A malloc() failure |
| 63 | # may or may not be reported. |
danielk1977 | a1644fd | 2007-08-29 12:31:25 +0000 | [diff] [blame] | 64 | sqlite3_memdebug_fail $::n -repeat 1 |
drh | b403574 | 2007-08-13 12:58:18 +0000 | [diff] [blame] | 65 | do_test mallocC-$tn.$::n.1 { |
drh | b3226e8 | 2007-08-13 12:39:03 +0000 | [diff] [blame] | 66 | set res [catchsql [string trim $::mallocopts(-sql)]] |
| 67 | set rc [expr { |
| 68 | 0==[string compare $res {1 {out of memory}}] || |
danielk1977 | ae72d98 | 2007-10-03 08:46:44 +0000 | [diff] [blame^] | 69 | [db errorcode] == 3082 || |
drh | b3226e8 | 2007-08-13 12:39:03 +0000 | [diff] [blame] | 70 | 0==[lindex $res 0] |
| 71 | }] |
| 72 | if {$rc!=1} { |
| 73 | puts "Error: $res" |
| 74 | } |
| 75 | set rc |
| 76 | } {1} |
| 77 | |
| 78 | # If $::n is greater than the number of malloc() calls required to |
| 79 | # execute the SQL, then this test is finished. Break out of the loop. |
danielk1977 | a1644fd | 2007-08-29 12:31:25 +0000 | [diff] [blame] | 80 | set nFail [sqlite3_memdebug_fail -1] |
drh | ed138fb | 2007-08-22 22:04:37 +0000 | [diff] [blame] | 81 | if {$nFail==0} { |
drh | b3226e8 | 2007-08-13 12:39:03 +0000 | [diff] [blame] | 82 | break |
| 83 | } |
| 84 | |
| 85 | # Recover from the malloc failure. |
| 86 | # |
| 87 | # Update: The new malloc() failure handling means that a transaction may |
| 88 | # still be active even if a malloc() has failed. But when these tests were |
| 89 | # written this was not the case. So do a manual ROLLBACK here so that the |
| 90 | # tests pass. |
drh | b403574 | 2007-08-13 12:58:18 +0000 | [diff] [blame] | 91 | do_test mallocC-$tn.$::n.2 { |
drh | b3226e8 | 2007-08-13 12:39:03 +0000 | [diff] [blame] | 92 | catch { |
| 93 | execsql { |
| 94 | ROLLBACK; |
| 95 | } |
| 96 | } |
| 97 | expr 0 |
| 98 | } {0} |
| 99 | |
| 100 | # Checksum the database. |
drh | b403574 | 2007-08-13 12:58:18 +0000 | [diff] [blame] | 101 | #do_test mallocC-$tn.$::n.3 { |
drh | b3226e8 | 2007-08-13 12:39:03 +0000 | [diff] [blame] | 102 | # cksum db |
| 103 | #} $sum |
| 104 | |
drh | b403574 | 2007-08-13 12:58:18 +0000 | [diff] [blame] | 105 | #integrity_check mallocC-$tn.$::n.4 |
drh | b3226e8 | 2007-08-13 12:39:03 +0000 | [diff] [blame] | 106 | if {$::nErr>1} return |
| 107 | } |
| 108 | unset ::mallocopts |
| 109 | } |
| 110 | |
danielk1977 | ae72d98 | 2007-10-03 08:46:44 +0000 | [diff] [blame^] | 111 | sqlite3_extended_result_codes db 1 |
| 112 | |
drh | b3226e8 | 2007-08-13 12:39:03 +0000 | [diff] [blame] | 113 | execsql { |
| 114 | PRAGMA auto_vacuum=1; |
| 115 | CREATE TABLE t0(a, b, c); |
| 116 | } |
| 117 | do_mallocC_test 1 -sql { |
| 118 | BEGIN; |
| 119 | -- Allocate 32 new root pages. This will exercise the 'extract specific |
| 120 | -- page from the freelist' code when in auto-vacuum mode (see the |
| 121 | -- allocatePage() routine in btree.c). |
| 122 | CREATE TABLE t1(a UNIQUE, b UNIQUE, c UNIQUE); |
| 123 | CREATE TABLE t2(a UNIQUE, b UNIQUE, c UNIQUE); |
| 124 | CREATE TABLE t3(a UNIQUE, b UNIQUE, c UNIQUE); |
| 125 | CREATE TABLE t4(a UNIQUE, b UNIQUE, c UNIQUE); |
| 126 | CREATE TABLE t5(a UNIQUE, b UNIQUE, c UNIQUE); |
| 127 | CREATE TABLE t6(a UNIQUE, b UNIQUE, c UNIQUE); |
| 128 | CREATE TABLE t7(a UNIQUE, b UNIQUE, c UNIQUE); |
| 129 | CREATE TABLE t8(a UNIQUE, b UNIQUE, c UNIQUE); |
| 130 | |
| 131 | ROLLBACK; |
| 132 | } |
| 133 | |
| 134 | finish_test |