AryaWu/sqlite
0
1# 2009 January 302#3# The author disclaims copyright to this source code. In place of4# 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. The12# focus of this file is testing the handling of OOM errors by the13# sqlite3_backup_XXX APIs.14#15# $Id: backup_malloc.test,v 1.2 2009/02/04 22:46:47 drh Exp $16 17set testdir [file dirname $argv0]18source $testdir/tester.tcl19 20source $testdir/malloc_common.tcl21 22do_malloc_test backup_malloc-1 -tclprep {23 execsql {24 PRAGMA cache_size = 10;25 BEGIN;26 CREATE TABLE t1(a, b);27 INSERT INTO t1 VALUES(1, randstr(1000,1000));28 INSERT INTO t1 SELECT a+ 1, randstr(1000,1000) FROM t1;29 INSERT INTO t1 SELECT a+ 2, randstr(1000,1000) FROM t1;30 INSERT INTO t1 SELECT a+ 4, randstr(1000,1000) FROM t1;31 INSERT INTO t1 SELECT a+ 8, randstr(1000,1000) FROM t1;32 INSERT INTO t1 SELECT a+16, randstr(1000,1000) FROM t1;33 INSERT INTO t1 SELECT a+32, randstr(1000,1000) FROM t1;34 INSERT INTO t1 SELECT a+64, randstr(1000,1000) FROM t1;35 CREATE INDEX i1 ON t1(b);36 COMMIT;37 }38 sqlite3 db2 test2.db39 execsql { PRAGMA cache_size = 10 } db240} -tclbody {41 42 # Create a backup object.43 #44 set rc [catch {sqlite3_backup B db2 main db main}]45 if {$rc && [sqlite3_errcode db2] == "SQLITE_NOMEM"} {46 error "out of memory"47 }48 49 # Run the backup process some.50 #51 set rc [B step 50]52 if {$rc == "SQLITE_NOMEM" || $rc == "SQLITE_IOERR_NOMEM"} {53 error "out of memory"54 }55 56 # Update the database.57 #58 execsql { UPDATE t1 SET a = a + 1 }59 60 # Finish doing the backup.61 #62 set rc [B step 5000]63 if {$rc == "SQLITE_NOMEM" || $rc == "SQLITE_IOERR_NOMEM"} {64 error "out of memory"65 }66 67 # Finalize the backup.68 B finish69} -cleanup {70 catch { B finish }71 catch { db2 close }72}73 74do_malloc_test backup_malloc-2 -tclprep {75 sqlite3 db2 test2.db76} -tclbody {77 set rc [catch {sqlite3_backup B db2 temp db main}]78 set errcode [sqlite3_errcode db2]79 if {$rc && ($errcode == "SQLITE_NOMEM" || $errcode == "SQLITE_IOERR_NOMEM")} {80 error "out of memory"81 }82} -cleanup {83 catch { B finish }84 db2 close85}86 87reset_db88do_execsql_test 3.0 {89 PRAGMA page_size = 16384;90 BEGIN;91 CREATE TABLE t1(a, b);92 INSERT INTO t1 VALUES(1, 2);93 COMMIT;94}95 96do_faultsim_test 3 -faults oom* -prep {97 catch { db close }98 catch { db2 close }99 100 forcedelete test2.db101 sqlite3 db2 test2.db102 sqlite3 db test.db103 sqlite3_backup B db2 main db main104} -body {105 106 set rc [B step 50]107 if {$rc == "SQLITE_NOMEM" || $rc == "SQLITE_IOERR_NOMEM"} {108 error "out of memory"109 }110 111} -test {112 faultsim_test_result {0 {}} 113 faultsim_integrity_check114 115 # Finalize the backup.116 catch { B finish }117}118 119finish_test120 