/ Hex Artifact Content
Login

Artifact efb0d5724893133e71b8d9d90abdb781845a6bb0:


0000: 23 20 32 30 31 30 20 4d 61 79 20 30 33 0a 23 0a  # 2010 May 03.#.
0010: 23 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73  # The author dis
0020: 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74  claims copyright
0030: 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20   to this source 
0040: 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20  code.  In place 
0050: 6f 66 0a 23 20 61 20 6c 65 67 61 6c 20 6e 6f 74  of.# a legal not
0060: 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62  ice, here is a b
0070: 6c 65 73 73 69 6e 67 3a 0a 23 0a 23 20 20 20 20  lessing:.#.#    
0080: 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20  May you do good 
0090: 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 23 20  and not evil..# 
00a0: 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20     May you find 
00b0: 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20  forgiveness for 
00c0: 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72  yourself and for
00d0: 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 23 20 20  give others..#  
00e0: 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20    May you share 
00f0: 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61  freely, never ta
0100: 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79  king more than y
0110: 6f 75 20 67 69 76 65 2e 0a 23 0a 23 2a 2a 2a 2a  ou give..#.#****
0120: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
0130: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
0140: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
0150: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
0160: 2a 2a 2a 0a 23 20 54 68 69 73 20 66 69 6c 65 20  ***.# This file 
0170: 69 6d 70 6c 65 6d 65 6e 74 73 20 72 65 67 72 65  implements regre
0180: 73 73 69 6f 6e 20 74 65 73 74 73 20 66 6f 72 20  ssion tests for 
0190: 53 51 4c 69 74 65 20 6c 69 62 72 61 72 79 2e 20  SQLite library. 
01a0: 20 54 68 65 0a 23 20 66 6f 63 75 73 20 6f 66 20   The.# focus of 
01b0: 74 68 69 73 20 66 69 6c 65 20 69 73 20 74 65 73  this file is tes
01c0: 74 69 6e 67 20 74 68 65 20 6f 70 65 72 61 74 69  ting the operati
01d0: 6f 6e 20 6f 66 20 74 68 65 20 6c 69 62 72 61 72  on of the librar
01e0: 79 20 69 6e 0a 23 20 22 50 52 41 47 4d 41 20 6a  y in.# "PRAGMA j
01f0: 6f 75 72 6e 61 6c 5f 6d 6f 64 65 3d 57 41 4c 22  ournal_mode=WAL"
0200: 20 6d 6f 64 65 2e 0a 23 0a 0a 73 65 74 20 74 65   mode..#..set te
0210: 73 74 64 69 72 20 5b 66 69 6c 65 20 64 69 72 6e  stdir [file dirn
0220: 61 6d 65 20 24 61 72 67 76 30 5d 0a 73 6f 75 72  ame $argv0].sour
0230: 63 65 20 24 74 65 73 74 64 69 72 2f 74 65 73 74  ce $testdir/test
0240: 65 72 2e 74 63 6c 0a 73 6f 75 72 63 65 20 24 74  er.tcl.source $t
0250: 65 73 74 64 69 72 2f 6d 61 6c 6c 6f 63 5f 63 6f  estdir/malloc_co
0260: 6d 6d 6f 6e 2e 74 63 6c 0a 73 6f 75 72 63 65 20  mmon.tcl.source 
0270: 24 74 65 73 74 64 69 72 2f 6c 6f 63 6b 5f 63 6f  $testdir/lock_co
0280: 6d 6d 6f 6e 2e 74 63 6c 0a 0a 69 66 63 61 70 61  mmon.tcl..ifcapa
0290: 62 6c 65 20 21 77 61 6c 20 7b 66 69 6e 69 73 68  ble !wal {finish
02a0: 5f 74 65 73 74 20 3b 20 72 65 74 75 72 6e 20 7d  _test ; return }
02b0: 0a 0a 23 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ..#-------------
02c0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
02d0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
02e0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
02f0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 23 20 54  ------------.# T
0300: 68 69 73 20 74 65 73 74 20 63 61 73 65 2c 20 77  his test case, w
0310: 61 6c 66 61 75 6c 74 2d 31 2d 2a 2c 20 73 69 6d  alfault-1-*, sim
0320: 75 6c 61 74 65 73 20 66 61 75 6c 74 73 20 77 68  ulates faults wh
0330: 69 6c 65 20 65 78 65 63 75 74 69 6e 67 20 61 0a  ile executing a.
0340: 23 0a 23 20 20 20 50 52 41 47 4d 41 20 6a 6f 75  #.#   PRAGMA jou
0350: 72 6e 61 6c 5f 6d 6f 64 65 20 3d 20 57 41 4c 3b  rnal_mode = WAL;
0360: 0a 23 0a 23 20 73 74 61 74 65 6d 65 6e 74 20 69  .#.# statement i
0370: 6d 6d 65 64 69 61 74 65 6c 79 20 61 66 74 65 72  mmediately after
0380: 20 63 72 65 61 74 69 6e 67 20 61 20 6e 65 77 20   creating a new 
0390: 64 61 74 61 62 61 73 65 2e 0a 23 0a 64 6f 5f 74  database..#.do_t
03a0: 65 73 74 20 77 61 6c 66 61 75 6c 74 2d 31 2d 70  est walfault-1-p
03b0: 72 65 2d 31 20 7b 0a 20 20 66 61 75 6c 74 73 69  re-1 {.  faultsi
03c0: 6d 5f 64 65 6c 65 74 65 5f 61 6e 64 5f 72 65 6f  m_delete_and_reo
03d0: 70 65 6e 0a 20 20 66 61 75 6c 74 73 69 6d 5f 73  pen.  faultsim_s
03e0: 61 76 65 5f 61 6e 64 5f 63 6c 6f 73 65 0a 7d 20  ave_and_close.} 
03f0: 7b 7d 0a 64 6f 5f 66 61 75 6c 74 73 69 6d 5f 74  {}.do_faultsim_t
0400: 65 73 74 20 77 61 6c 66 61 75 6c 74 2d 31 20 2d  est walfault-1 -
0410: 70 72 65 70 20 7b 0a 20 20 66 61 75 6c 74 73 69  prep {.  faultsi
0420: 6d 5f 72 65 73 74 6f 72 65 5f 61 6e 64 5f 72 65  m_restore_and_re
0430: 6f 70 65 6e 0a 7d 20 2d 62 6f 64 79 20 7b 0a 20  open.} -body {. 
0440: 20 64 62 20 65 76 61 6c 20 7b 20 50 52 41 47 4d   db eval { PRAGM
0450: 41 20 6d 61 69 6e 2e 6a 6f 75 72 6e 61 6c 5f 6d  A main.journal_m
0460: 6f 64 65 20 3d 20 57 41 4c 20 7d 0a 7d 20 2d 74  ode = WAL }.} -t
0470: 65 73 74 20 7b 0a 0a 20 20 66 61 75 6c 74 73 69  est {..  faultsi
0480: 6d 5f 74 65 73 74 5f 72 65 73 75 6c 74 20 7b 30  m_test_result {0
0490: 20 77 61 6c 7d 0a 0a 20 20 23 20 54 65 73 74 20   wal}..  # Test 
04a0: 74 68 61 74 20 74 68 65 20 63 6f 6e 6e 65 63 74  that the connect
04b0: 69 6f 6e 20 74 68 61 74 20 65 6e 63 6f 75 6e 74  ion that encount
04c0: 65 72 65 64 20 61 6e 20 65 72 72 6f 72 20 61 73  ered an error as
04d0: 20 70 61 72 74 20 6f 66 20 0a 20 20 23 20 22 50   part of .  # "P
04e0: 52 41 47 4d 41 20 6a 6f 75 72 6e 61 6c 5f 6d 6f  RAGMA journal_mo
04f0: 64 65 20 3d 20 57 41 4c 22 20 61 6e 64 20 61 20  de = WAL" and a 
0500: 6e 65 77 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 75  new connection u
0510: 73 65 20 74 68 65 20 73 61 6d 65 0a 20 20 23 20  se the same.  # 
0520: 6a 6f 75 72 6e 61 6c 20 6d 6f 64 65 20 77 68 65  journal mode whe
0530: 6e 20 61 63 63 65 73 73 69 6e 67 20 74 68 65 20  n accessing the 
0540: 64 61 74 61 62 61 73 65 2e 0a 20 20 23 0a 20 20  database..  #.  
0550: 23 20 49 66 20 22 50 52 41 47 4d 41 20 6a 6f 75  # If "PRAGMA jou
0560: 72 6e 61 6c 5f 6d 6f 64 65 22 20 69 73 20 65 78  rnal_mode" is ex
0570: 65 63 75 74 65 64 20 69 6d 6d 65 64 69 61 74 65  ecuted immediate
0580: 6c 79 2c 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 5b  ly, connection [
0590: 64 62 5d 20 28 74 68 65 20 0a 20 20 23 20 6f 6e  db] (the .  # on
05a0: 65 20 74 68 61 74 20 68 69 74 20 74 68 65 20 65  e that hit the e
05b0: 72 72 6f 72 20 69 6e 20 6a 6f 75 72 6e 61 6c 5f  rror in journal_
05c0: 6d 6f 64 65 3d 22 57 41 4c 22 29 20 6d 69 67 68  mode="WAL") migh
05d0: 74 20 72 65 74 75 72 6e 20 22 77 61 6c 22 20 65  t return "wal" e
05e0: 76 65 6e 20 0a 20 20 23 20 69 66 20 69 74 20 66  ven .  # if it f
05f0: 61 69 6c 65 64 20 74 6f 20 73 77 69 74 63 68 20  ailed to switch 
0600: 74 68 65 20 64 61 74 61 62 61 73 65 20 74 6f 20  the database to 
0610: 57 41 4c 20 6d 6f 64 65 2e 20 54 68 69 73 20 69  WAL mode. This i
0620: 73 20 6e 6f 74 20 63 6f 6e 73 69 64 65 72 65 64  s not considered
0630: 20 0a 20 20 23 20 61 20 70 72 6f 62 6c 65 6d 2e   .  # a problem.
0640: 20 57 68 65 6e 20 69 74 20 74 72 69 65 73 20 74   When it tries t
0650: 6f 20 72 65 61 64 20 74 68 65 20 64 61 74 61 62  o read the datab
0660: 61 73 65 2c 20 63 6f 6e 6e 65 63 74 69 6f 6e 20  ase, connection 
0670: 5b 64 62 5d 20 63 6f 72 72 65 63 74 6c 79 20 0a  [db] correctly .
0680: 20 20 23 20 72 65 63 6f 67 6e 69 7a 65 73 20 74    # recognizes t
0690: 68 61 74 20 69 74 20 69 73 20 61 20 72 6f 6c 6c  hat it is a roll
06a0: 62 61 63 6b 20 64 61 74 61 62 61 73 65 20 61 6e  back database an
06b0: 64 20 73 77 69 74 63 68 65 73 20 62 61 63 6b 20  d switches back 
06c0: 74 6f 20 61 20 0a 20 20 23 20 72 6f 6c 6c 62 61  to a .  # rollba
06d0: 63 6b 20 63 6f 6d 70 61 74 69 62 6c 65 20 6a 6f  ck compatible jo
06e0: 75 72 6e 61 6c 20 6d 6f 64 65 2e 0a 20 20 23 0a  urnal mode..  #.
06f0: 20 20 69 66 20 7b 5b 70 65 72 6d 75 74 61 74 69    if {[permutati
0700: 6f 6e 5d 20 21 3d 20 22 69 6e 6d 65 6d 6f 72 79  on] != "inmemory
0710: 5f 6a 6f 75 72 6e 61 6c 22 7d 20 7b 0a 20 20 20  _journal"} {.   
0720: 20 73 65 74 20 6a 6d 20 20 5b 64 62 20 6f 6e 65   set jm  [db one
0730: 20 20 7b 53 45 4c 45 43 54 20 2a 20 46 52 4f 4d    {SELECT * FROM
0740: 20 73 71 6c 69 74 65 5f 6d 61 73 74 65 72 20 3b   sqlite_master ;
0750: 20 50 52 41 47 4d 41 20 6d 61 69 6e 2e 6a 6f 75   PRAGMA main.jou
0760: 72 6e 61 6c 5f 6d 6f 64 65 7d 5d 0a 20 20 20 20  rnal_mode}].    
0770: 73 71 6c 69 74 65 33 20 64 62 32 20 74 65 73 74  sqlite3 db2 test
0780: 2e 64 62 0a 20 20 20 20 73 65 74 20 6a 6d 32 20  .db.    set jm2 
0790: 5b 64 62 32 20 6f 6e 65 20 7b 53 45 4c 45 43 54  [db2 one {SELECT
07a0: 20 2a 20 46 52 4f 4d 20 73 71 6c 69 74 65 5f 6d   * FROM sqlite_m
07b0: 61 73 74 65 72 20 3b 20 50 52 41 47 4d 41 20 6d  aster ; PRAGMA m
07c0: 61 69 6e 2e 6a 6f 75 72 6e 61 6c 5f 6d 6f 64 65  ain.journal_mode
07d0: 7d 5d 0a 20 20 20 20 64 62 32 20 63 6c 6f 73 65  }].    db2 close
07e0: 0a 20 20 0a 20 20 20 20 69 66 20 7b 20 24 6a 6d  .  .    if { $jm
07f0: 21 3d 24 6a 6d 32 20 7d 20 7b 20 65 72 72 6f 72  !=$jm2 } { error
0800: 20 22 4a 6f 75 72 6e 61 6c 20 6d 6f 64 65 73 20   "Journal modes 
0810: 64 6f 20 6e 6f 74 20 6d 61 74 63 68 3a 20 24 6a  do not match: $j
0820: 6d 20 24 6a 6d 32 22 20 7d 0a 20 20 20 20 69 66  m $jm2" }.    if
0830: 20 7b 20 24 74 65 73 74 72 63 3d 3d 30 20 26 26   { $testrc==0 &&
0840: 20 24 6a 6d 21 3d 22 77 61 6c 22 20 7d 20 7b 20   $jm!="wal" } { 
0850: 65 72 72 6f 72 20 22 4a 6f 75 72 6e 61 6c 20 6d  error "Journal m
0860: 6f 64 65 20 69 73 20 6e 6f 74 20 57 41 4c 22 20  ode is not WAL" 
0870: 7d 0a 20 20 7d 0a 7d 0a 0a 23 2d 2d 2d 2d 2d 2d  }.  }.}..#------
0880: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
0890: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
08a0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
08b0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
08c0: 2d 2d 2d 2d 0a 23 20 54 65 73 74 20 63 61 73 65  ----.# Test case
08d0: 20 77 61 6c 66 61 75 6c 74 2d 32 2d 2a 20 74 65   walfault-2-* te
08e0: 73 74 73 20 66 61 75 6c 74 20 69 6e 6a 65 63 74  sts fault inject
08f0: 69 6f 6e 20 64 75 72 69 6e 67 20 72 65 63 6f 76  ion during recov
0900: 65 72 79 20 6f 66 20 61 20 0a 23 20 73 68 6f 72  ery of a .# shor
0910: 74 20 57 41 4c 20 66 69 6c 65 20 28 61 20 64 6f  t WAL file (a do
0920: 7a 65 6e 20 66 72 61 6d 65 73 20 6f 72 20 74 68  zen frames or th
0930: 65 72 65 61 62 6f 75 74 73 29 2e 0a 23 0a 64 6f  ereabouts)..#.do
0940: 5f 74 65 73 74 20 77 61 6c 66 61 75 6c 74 2d 32  _test walfault-2
0950: 2d 70 72 65 2d 31 20 7b 0a 20 20 73 71 6c 69 74  -pre-1 {.  sqlit
0960: 65 33 20 64 62 20 74 65 73 74 2e 64 62 0a 20 20  e3 db test.db.  
0970: 65 78 65 63 73 71 6c 20 7b 0a 20 20 20 20 50 52  execsql {.    PR
0980: 41 47 4d 41 20 6a 6f 75 72 6e 61 6c 5f 6d 6f 64  AGMA journal_mod
0990: 65 20 3d 20 57 41 4c 3b 0a 20 20 20 20 42 45 47  e = WAL;.    BEG
09a0: 49 4e 3b 0a 20 20 20 20 20 20 43 52 45 41 54 45  IN;.      CREATE
09b0: 20 54 41 42 4c 45 20 78 28 79 2c 20 7a 2c 20 55   TABLE x(y, z, U
09c0: 4e 49 51 55 45 28 79 2c 20 7a 29 29 3b 0a 20 20  NIQUE(y, z));.  
09d0: 20 20 20 20 49 4e 53 45 52 54 20 49 4e 54 4f 20      INSERT INTO 
09e0: 78 20 56 41 4c 55 45 53 28 72 61 6e 64 6f 6d 62  x VALUES(randomb
09f0: 6c 6f 62 28 31 30 30 29 2c 20 72 61 6e 64 6f 6d  lob(100), random
0a00: 62 6c 6f 62 28 31 30 30 29 29 3b 0a 20 20 20 20  blob(100));.    
0a10: 43 4f 4d 4d 49 54 3b 0a 20 20 20 20 50 52 41 47  COMMIT;.    PRAG
0a20: 4d 41 20 77 61 6c 5f 63 68 65 63 6b 70 6f 69 6e  MA wal_checkpoin
0a30: 74 3b 0a 0a 20 20 20 20 49 4e 53 45 52 54 20 49  t;..    INSERT I
0a40: 4e 54 4f 20 78 20 53 45 4c 45 43 54 20 72 61 6e  NTO x SELECT ran
0a50: 64 6f 6d 62 6c 6f 62 28 31 30 30 29 2c 20 72 61  domblob(100), ra
0a60: 6e 64 6f 6d 62 6c 6f 62 28 31 30 30 29 20 46 52  ndomblob(100) FR
0a70: 4f 4d 20 78 3b 0a 20 20 20 20 49 4e 53 45 52 54  OM x;.    INSERT
0a80: 20 49 4e 54 4f 20 78 20 53 45 4c 45 43 54 20 72   INTO x SELECT r
0a90: 61 6e 64 6f 6d 62 6c 6f 62 28 31 30 30 29 2c 20  andomblob(100), 
0aa0: 72 61 6e 64 6f 6d 62 6c 6f 62 28 31 30 30 29 20  randomblob(100) 
0ab0: 46 52 4f 4d 20 78 3b 0a 20 20 20 20 49 4e 53 45  FROM x;.    INSE
0ac0: 52 54 20 49 4e 54 4f 20 78 20 53 45 4c 45 43 54  RT INTO x SELECT
0ad0: 20 72 61 6e 64 6f 6d 62 6c 6f 62 28 31 30 30 29   randomblob(100)
0ae0: 2c 20 72 61 6e 64 6f 6d 62 6c 6f 62 28 31 30 30  , randomblob(100
0af0: 29 20 46 52 4f 4d 20 78 3b 0a 20 20 7d 0a 20 20  ) FROM x;.  }.  
0b00: 65 78 65 63 73 71 6c 20 7b 0a 20 20 20 20 53 45  execsql {.    SE
0b10: 4c 45 43 54 20 63 6f 75 6e 74 28 2a 29 20 46 52  LECT count(*) FR
0b20: 4f 4d 20 78 0a 20 20 7d 0a 7d 20 7b 38 7d 0a 64  OM x.  }.} {8}.d
0b30: 6f 5f 74 65 73 74 20 77 61 6c 66 61 75 6c 74 2d  o_test walfault-
0b40: 32 2d 70 72 65 2d 32 20 7b 0a 20 20 66 61 75 6c  2-pre-2 {.  faul
0b50: 74 73 69 6d 5f 73 61 76 65 5f 61 6e 64 5f 63 6c  tsim_save_and_cl
0b60: 6f 73 65 0a 20 20 66 61 75 6c 74 73 69 6d 5f 72  ose.  faultsim_r
0b70: 65 73 74 6f 72 65 5f 61 6e 64 5f 72 65 6f 70 65  estore_and_reope
0b80: 6e 0a 20 20 65 78 65 63 73 71 6c 20 7b 20 53 45  n.  execsql { SE
0b90: 4c 45 43 54 20 63 6f 75 6e 74 28 2a 29 20 46 52  LECT count(*) FR
0ba0: 4f 4d 20 78 20 7d 0a 7d 20 7b 38 7d 0a 64 6f 5f  OM x }.} {8}.do_
0bb0: 66 61 75 6c 74 73 69 6d 5f 74 65 73 74 20 77 61  faultsim_test wa
0bc0: 6c 66 61 75 6c 74 2d 32 20 2d 70 72 65 70 20 7b  lfault-2 -prep {
0bd0: 0a 20 20 66 61 75 6c 74 73 69 6d 5f 72 65 73 74  .  faultsim_rest
0be0: 6f 72 65 5f 61 6e 64 5f 72 65 6f 70 65 6e 0a 7d  ore_and_reopen.}
0bf0: 20 2d 62 6f 64 79 20 7b 0a 20 20 65 78 65 63 73   -body {.  execs
0c00: 71 6c 20 7b 20 53 45 4c 45 43 54 20 63 6f 75 6e  ql { SELECT coun
0c10: 74 28 2a 29 20 46 52 4f 4d 20 78 20 7d 0a 7d 20  t(*) FROM x }.} 
0c20: 2d 74 65 73 74 20 7b 0a 20 20 66 61 75 6c 74 73  -test {.  faults
0c30: 69 6d 5f 74 65 73 74 5f 72 65 73 75 6c 74 20 7b  im_test_result {
0c40: 30 20 38 7d 0a 20 20 66 61 75 6c 74 73 69 6d 5f  0 8}.  faultsim_
0c50: 69 6e 74 65 67 72 69 74 79 5f 63 68 65 63 6b 0a  integrity_check.
0c60: 7d 0a 0a 23 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  }..#------------
0c70: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
0c80: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
0c90: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
0ca0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 23  --------------.#
0cb0: 20 54 65 73 74 20 66 61 75 6c 74 20 69 6e 6a 65   Test fault inje
0cc0: 63 74 69 6f 6e 20 77 68 69 6c 65 20 77 72 69 74  ction while writ
0cd0: 69 6e 67 20 61 6e 64 20 63 68 65 63 6b 70 6f 69  ing and checkpoi
0ce0: 6e 74 69 6e 67 20 61 20 73 6d 61 6c 6c 20 57 41  nting a small WA
0cf0: 4c 20 66 69 6c 65 2e 0a 23 0a 64 6f 5f 74 65 73  L file..#.do_tes
0d00: 74 20 77 61 6c 66 61 75 6c 74 2d 33 2d 70 72 65  t walfault-3-pre
0d10: 2d 31 20 7b 0a 20 20 73 71 6c 69 74 65 33 20 64  -1 {.  sqlite3 d
0d20: 62 20 74 65 73 74 2e 64 62 0a 20 20 65 78 65 63  b test.db.  exec
0d30: 73 71 6c 20 7b 0a 20 20 20 20 50 52 41 47 4d 41  sql {.    PRAGMA
0d40: 20 61 75 74 6f 5f 76 61 63 75 75 6d 20 3d 20 31   auto_vacuum = 1
0d50: 3b 0a 20 20 20 20 50 52 41 47 4d 41 20 6a 6f 75  ;.    PRAGMA jou
0d60: 72 6e 61 6c 5f 6d 6f 64 65 20 3d 20 57 41 4c 3b  rnal_mode = WAL;
0d70: 0a 20 20 20 20 43 52 45 41 54 45 20 54 41 42 4c  .    CREATE TABL
0d80: 45 20 61 62 63 28 61 20 50 52 49 4d 41 52 59 20  E abc(a PRIMARY 
0d90: 4b 45 59 29 3b 0a 20 20 20 20 49 4e 53 45 52 54  KEY);.    INSERT
0da0: 20 49 4e 54 4f 20 61 62 63 20 56 41 4c 55 45 53   INTO abc VALUES
0db0: 28 72 61 6e 64 6f 6d 62 6c 6f 62 28 31 35 30 30  (randomblob(1500
0dc0: 29 29 3b 0a 20 20 7d 0a 20 20 64 62 20 63 6c 6f  ));.  }.  db clo
0dd0: 73 65 0a 20 20 66 61 75 6c 74 73 69 6d 5f 73 61  se.  faultsim_sa
0de0: 76 65 5f 61 6e 64 5f 63 6c 6f 73 65 0a 7d 20 7b  ve_and_close.} {
0df0: 7d 0a 64 6f 5f 66 61 75 6c 74 73 69 6d 5f 74 65  }.do_faultsim_te
0e00: 73 74 20 77 61 6c 66 61 75 6c 74 2d 33 20 2d 70  st walfault-3 -p
0e10: 72 65 70 20 7b 0a 20 20 66 61 75 6c 74 73 69 6d  rep {.  faultsim
0e20: 5f 72 65 73 74 6f 72 65 5f 61 6e 64 5f 72 65 6f  _restore_and_reo
0e30: 70 65 6e 0a 7d 20 2d 62 6f 64 79 20 7b 0a 20 20  pen.} -body {.  
0e40: 64 62 20 65 76 61 6c 20 7b 0a 20 20 20 20 44 45  db eval {.    DE
0e50: 4c 45 54 45 20 46 52 4f 4d 20 61 62 63 3b 0a 20  LETE FROM abc;. 
0e60: 20 20 20 50 52 41 47 4d 41 20 77 61 6c 5f 63 68     PRAGMA wal_ch
0e70: 65 63 6b 70 6f 69 6e 74 3b 0a 20 20 7d 0a 20 20  eckpoint;.  }.  
0e80: 73 65 74 20 7b 7d 20 7b 7d 0a 7d 20 2d 74 65 73  set {} {}.} -tes
0e90: 74 20 7b 0a 20 20 66 61 75 6c 74 73 69 6d 5f 74  t {.  faultsim_t
0ea0: 65 73 74 5f 72 65 73 75 6c 74 20 7b 30 20 7b 7d  est_result {0 {}
0eb0: 7d 0a 7d 0a 0a 0a 23 2d 2d 2d 2d 2d 2d 2d 2d 2d  }.}...#---------
0ec0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
0ed0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
0ee0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
0ef0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
0f00: 2d 0a 23 0a 69 66 20 7b 5b 70 65 72 6d 75 74 61  -.#.if {[permuta
0f10: 74 69 6f 6e 5d 20 21 3d 20 22 69 6e 6d 65 6d 6f  tion] != "inmemo
0f20: 72 79 5f 6a 6f 75 72 6e 61 6c 22 7d 20 7b 0a 20  ry_journal"} {. 
0f30: 20 66 61 75 6c 74 73 69 6d 5f 64 65 6c 65 74 65   faultsim_delete
0f40: 5f 61 6e 64 5f 72 65 6f 70 65 6e 0a 20 20 66 61  _and_reopen.  fa
0f50: 75 6c 74 73 69 6d 5f 73 61 76 65 5f 61 6e 64 5f  ultsim_save_and_
0f60: 63 6c 6f 73 65 0a 20 20 64 6f 5f 66 61 75 6c 74  close.  do_fault
0f70: 73 69 6d 5f 74 65 73 74 20 77 61 6c 66 61 75 6c  sim_test walfaul
0f80: 74 2d 34 20 2d 70 72 65 70 20 7b 0a 20 20 20 20  t-4 -prep {.    
0f90: 66 61 75 6c 74 73 69 6d 5f 72 65 73 74 6f 72 65  faultsim_restore
0fa0: 5f 61 6e 64 5f 72 65 6f 70 65 6e 0a 20 20 7d 20  _and_reopen.  } 
0fb0: 2d 62 6f 64 79 20 7b 0a 20 20 20 20 65 78 65 63  -body {.    exec
0fc0: 73 71 6c 20 7b 0a 20 20 20 20 20 20 50 52 41 47  sql {.      PRAG
0fd0: 4d 41 20 61 75 74 6f 5f 76 61 63 75 75 6d 20 3d  MA auto_vacuum =
0fe0: 20 30 3b 0a 20 20 20 20 20 20 50 52 41 47 4d 41   0;.      PRAGMA
0ff0: 20 6a 6f 75 72 6e 61 6c 5f 6d 6f 64 65 20 3d 20   journal_mode = 
1000: 57 41 4c 3b 0a 20 20 20 20 20 20 43 52 45 41 54  WAL;.      CREAT
1010: 45 20 54 41 42 4c 45 20 74 31 28 61 20 50 52 49  E TABLE t1(a PRI
1020: 4d 41 52 59 20 4b 45 59 2c 20 62 29 3b 0a 20 20  MARY KEY, b);.  
1030: 20 20 20 20 49 4e 53 45 52 54 20 49 4e 54 4f 20      INSERT INTO 
1040: 74 31 20 56 41 4c 55 45 53 28 27 61 27 2c 20 27  t1 VALUES('a', '
1050: 62 27 29 3b 0a 20 20 20 20 20 20 50 52 41 47 4d  b');.      PRAGM
1060: 41 20 77 61 6c 5f 63 68 65 63 6b 70 6f 69 6e 74  A wal_checkpoint
1070: 3b 0a 20 20 20 20 20 20 53 45 4c 45 43 54 20 2a  ;.      SELECT *
1080: 20 46 52 4f 4d 20 74 31 3b 0a 20 20 20 20 7d 0a   FROM t1;.    }.
1090: 20 20 7d 20 2d 74 65 73 74 20 7b 0a 20 20 20 20    } -test {.    
10a0: 66 61 75 6c 74 73 69 6d 5f 74 65 73 74 5f 72 65  faultsim_test_re
10b0: 73 75 6c 74 20 7b 30 20 7b 77 61 6c 20 30 20 37  sult {0 {wal 0 7
10c0: 20 37 20 61 20 62 7d 7d 0a 20 20 20 20 66 61 75   7 a b}}.    fau
10d0: 6c 74 73 69 6d 5f 69 6e 74 65 67 72 69 74 79 5f  ltsim_integrity_
10e0: 63 68 65 63 6b 0a 20 20 7d 20 0a 7d 0a 0a 23 2d  check.  } .}..#-
10f0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1100: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1110: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1120: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1130: 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 23 0a 64 6f 5f 74  ---------.#.do_t
1140: 65 73 74 20 77 61 6c 66 61 75 6c 74 2d 35 2d 70  est walfault-5-p
1150: 72 65 2d 31 20 7b 0a 20 20 66 61 75 6c 74 73 69  re-1 {.  faultsi
1160: 6d 5f 64 65 6c 65 74 65 5f 61 6e 64 5f 72 65 6f  m_delete_and_reo
1170: 70 65 6e 0a 20 20 65 78 65 63 73 71 6c 20 7b 0a  pen.  execsql {.
1180: 20 20 20 20 50 52 41 47 4d 41 20 70 61 67 65 5f      PRAGMA page_
1190: 73 69 7a 65 20 3d 20 35 31 32 3b 0a 20 20 20 20  size = 512;.    
11a0: 50 52 41 47 4d 41 20 6a 6f 75 72 6e 61 6c 5f 6d  PRAGMA journal_m
11b0: 6f 64 65 20 3d 20 57 41 4c 3b 0a 20 20 7d 0a 20  ode = WAL;.  }. 
11c0: 20 66 61 75 6c 74 73 69 6d 5f 73 61 76 65 5f 61   faultsim_save_a
11d0: 6e 64 5f 63 6c 6f 73 65 0a 7d 20 7b 7d 0a 64 6f  nd_close.} {}.do
11e0: 5f 66 61 75 6c 74 73 69 6d 5f 74 65 73 74 20 77  _faultsim_test w
11f0: 61 6c 66 61 75 6c 74 2d 35 20 2d 66 61 75 6c 74  alfault-5 -fault
1200: 73 20 73 68 6d 65 72 72 2a 20 2d 70 72 65 70 20  s shmerr* -prep 
1210: 7b 0a 20 20 66 61 75 6c 74 73 69 6d 5f 72 65 73  {.  faultsim_res
1220: 74 6f 72 65 5f 61 6e 64 5f 72 65 6f 70 65 6e 0a  tore_and_reopen.
1230: 20 20 65 78 65 63 73 71 6c 20 7b 20 50 52 41 47    execsql { PRAG
1240: 4d 41 20 77 61 6c 5f 61 75 74 6f 63 68 65 63 6b  MA wal_autocheck
1250: 70 6f 69 6e 74 20 3d 20 30 20 7d 0a 20 20 73 68  point = 0 }.  sh
1260: 6d 66 61 75 6c 74 20 66 69 6c 74 65 72 20 78 53  mfault filter xS
1270: 68 6d 4d 61 70 0a 7d 20 2d 62 6f 64 79 20 7b 0a  hmMap.} -body {.
1280: 20 20 65 78 65 63 73 71 6c 20 7b 0a 20 20 20 20    execsql {.    
1290: 43 52 45 41 54 45 20 54 41 42 4c 45 20 74 31 28  CREATE TABLE t1(
12a0: 78 29 3b 0a 20 20 20 20 42 45 47 49 4e 3b 0a 20  x);.    BEGIN;. 
12b0: 20 20 20 20 20 49 4e 53 45 52 54 20 49 4e 54 4f       INSERT INTO
12c0: 20 74 31 20 56 41 4c 55 45 53 28 72 61 6e 64 6f   t1 VALUES(rando
12d0: 6d 62 6c 6f 62 28 34 30 30 29 29 3b 20 20 20 20  mblob(400));    
12e0: 20 20 20 20 20 20 20 2f 2a 20 31 20 2a 2f 0a 20         /* 1 */. 
12f0: 20 20 20 20 20 49 4e 53 45 52 54 20 49 4e 54 4f       INSERT INTO
1300: 20 74 31 20 53 45 4c 45 43 54 20 72 61 6e 64 6f   t1 SELECT rando
1310: 6d 62 6c 6f 62 28 34 30 30 29 20 46 52 4f 4d 20  mblob(400) FROM 
1320: 74 31 3b 20 20 20 20 2f 2a 20 32 20 2a 2f 0a 20  t1;    /* 2 */. 
1330: 20 20 20 20 20 49 4e 53 45 52 54 20 49 4e 54 4f       INSERT INTO
1340: 20 74 31 20 53 45 4c 45 43 54 20 72 61 6e 64 6f   t1 SELECT rando
1350: 6d 62 6c 6f 62 28 34 30 30 29 20 46 52 4f 4d 20  mblob(400) FROM 
1360: 74 31 3b 20 20 20 20 2f 2a 20 34 20 2a 2f 0a 20  t1;    /* 4 */. 
1370: 20 20 20 20 20 49 4e 53 45 52 54 20 49 4e 54 4f       INSERT INTO
1380: 20 74 31 20 53 45 4c 45 43 54 20 72 61 6e 64 6f   t1 SELECT rando
1390: 6d 62 6c 6f 62 28 34 30 30 29 20 46 52 4f 4d 20  mblob(400) FROM 
13a0: 74 31 3b 20 20 20 20 2f 2a 20 38 20 2a 2f 0a 20  t1;    /* 8 */. 
13b0: 20 20 20 20 20 49 4e 53 45 52 54 20 49 4e 54 4f       INSERT INTO
13c0: 20 74 31 20 53 45 4c 45 43 54 20 72 61 6e 64 6f   t1 SELECT rando
13d0: 6d 62 6c 6f 62 28 34 30 30 29 20 46 52 4f 4d 20  mblob(400) FROM 
13e0: 74 31 3b 20 20 20 20 2f 2a 20 31 36 20 2a 2f 0a  t1;    /* 16 */.
13f0: 20 20 20 20 20 20 49 4e 53 45 52 54 20 49 4e 54        INSERT INT
1400: 4f 20 74 31 20 53 45 4c 45 43 54 20 72 61 6e 64  O t1 SELECT rand
1410: 6f 6d 62 6c 6f 62 28 34 30 30 29 20 46 52 4f 4d  omblob(400) FROM
1420: 20 74 31 3b 20 20 20 20 2f 2a 20 33 32 20 2a 2f   t1;    /* 32 */
1430: 0a 20 20 20 20 20 20 49 4e 53 45 52 54 20 49 4e  .      INSERT IN
1440: 54 4f 20 74 31 20 53 45 4c 45 43 54 20 72 61 6e  TO t1 SELECT ran
1450: 64 6f 6d 62 6c 6f 62 28 34 30 30 29 20 46 52 4f  domblob(400) FRO
1460: 4d 20 74 31 3b 20 20 20 20 2f 2a 20 36 34 20 2a  M t1;    /* 64 *
1470: 2f 0a 20 20 20 20 20 20 49 4e 53 45 52 54 20 49  /.      INSERT I
1480: 4e 54 4f 20 74 31 20 53 45 4c 45 43 54 20 72 61  NTO t1 SELECT ra
1490: 6e 64 6f 6d 62 6c 6f 62 28 34 30 30 29 20 46 52  ndomblob(400) FR
14a0: 4f 4d 20 74 31 3b 20 20 20 20 2f 2a 20 31 32 38  OM t1;    /* 128
14b0: 20 2a 2f 0a 20 20 20 20 20 20 49 4e 53 45 52 54   */.      INSERT
14c0: 20 49 4e 54 4f 20 74 31 20 53 45 4c 45 43 54 20   INTO t1 SELECT 
14d0: 72 61 6e 64 6f 6d 62 6c 6f 62 28 34 30 30 29 20  randomblob(400) 
14e0: 46 52 4f 4d 20 74 31 3b 20 20 20 20 2f 2a 20 32  FROM t1;    /* 2
14f0: 35 36 20 2a 2f 0a 20 20 20 20 20 20 49 4e 53 45  56 */.      INSE
1500: 52 54 20 49 4e 54 4f 20 74 31 20 53 45 4c 45 43  RT INTO t1 SELEC
1510: 54 20 72 61 6e 64 6f 6d 62 6c 6f 62 28 34 30 30  T randomblob(400
1520: 29 20 46 52 4f 4d 20 74 31 3b 20 20 20 20 2f 2a  ) FROM t1;    /*
1530: 20 35 31 32 20 2a 2f 0a 20 20 20 20 20 20 49 4e   512 */.      IN
1540: 53 45 52 54 20 49 4e 54 4f 20 74 31 20 53 45 4c  SERT INTO t1 SEL
1550: 45 43 54 20 72 61 6e 64 6f 6d 62 6c 6f 62 28 34  ECT randomblob(4
1560: 30 30 29 20 46 52 4f 4d 20 74 31 3b 20 20 20 20  00) FROM t1;    
1570: 2f 2a 20 31 30 32 34 20 2a 2f 0a 20 20 20 20 20  /* 1024 */.     
1580: 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 74 31 20   INSERT INTO t1 
1590: 53 45 4c 45 43 54 20 72 61 6e 64 6f 6d 62 6c 6f  SELECT randomblo
15a0: 62 28 34 30 30 29 20 46 52 4f 4d 20 74 31 3b 20  b(400) FROM t1; 
15b0: 20 20 20 2f 2a 20 32 30 34 38 20 2a 2f 0a 20 20     /* 2048 */.  
15c0: 20 20 20 20 49 4e 53 45 52 54 20 49 4e 54 4f 20      INSERT INTO 
15d0: 74 31 20 53 45 4c 45 43 54 20 72 61 6e 64 6f 6d  t1 SELECT random
15e0: 62 6c 6f 62 28 34 30 30 29 20 46 52 4f 4d 20 74  blob(400) FROM t
15f0: 31 3b 20 20 20 20 2f 2a 20 34 30 39 36 20 2a 2f  1;    /* 4096 */
1600: 0a 20 20 20 20 20 20 49 4e 53 45 52 54 20 49 4e  .      INSERT IN
1610: 54 4f 20 74 31 20 53 45 4c 45 43 54 20 72 61 6e  TO t1 SELECT ran
1620: 64 6f 6d 62 6c 6f 62 28 34 30 30 29 20 46 52 4f  domblob(400) FRO
1630: 4d 20 74 31 3b 20 20 20 20 2f 2a 20 38 31 39 32  M t1;    /* 8192
1640: 20 2a 2f 0a 20 20 20 20 20 20 49 4e 53 45 52 54   */.      INSERT
1650: 20 49 4e 54 4f 20 74 31 20 53 45 4c 45 43 54 20   INTO t1 SELECT 
1660: 72 61 6e 64 6f 6d 62 6c 6f 62 28 34 30 30 29 20  randomblob(400) 
1670: 46 52 4f 4d 20 74 31 3b 20 20 20 20 2f 2a 20 31  FROM t1;    /* 1
1680: 36 33 38 34 20 2a 2f 0a 20 20 20 20 43 4f 4d 4d  6384 */.    COMM
1690: 49 54 3b 0a 20 20 20 20 53 45 4c 45 43 54 20 63  IT;.    SELECT c
16a0: 6f 75 6e 74 28 2a 29 20 46 52 4f 4d 20 74 31 3b  ount(*) FROM t1;
16b0: 0a 20 20 7d 0a 7d 20 2d 74 65 73 74 20 7b 0a 20  .  }.} -test {. 
16c0: 20 66 61 75 6c 74 73 69 6d 5f 74 65 73 74 5f 72   faultsim_test_r
16d0: 65 73 75 6c 74 20 7b 30 20 31 36 33 38 34 7d 0a  esult {0 16384}.
16e0: 20 20 66 61 75 6c 74 73 69 6d 5f 69 6e 74 65 67    faultsim_integ
16f0: 72 69 74 79 5f 63 68 65 63 6b 0a 7d 0a 0a 23 2d  rity_check.}..#-
1700: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1710: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1720: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1730: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1740: 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 23 0a 64 6f 5f 74  ---------.#.do_t
1750: 65 73 74 20 77 61 6c 66 61 75 6c 74 2d 36 2d 70  est walfault-6-p
1760: 72 65 2d 31 20 7b 0a 20 20 66 61 75 6c 74 73 69  re-1 {.  faultsi
1770: 6d 5f 64 65 6c 65 74 65 5f 61 6e 64 5f 72 65 6f  m_delete_and_reo
1780: 70 65 6e 0a 20 20 65 78 65 63 73 71 6c 20 7b 0a  pen.  execsql {.
1790: 20 20 20 20 50 52 41 47 4d 41 20 70 61 67 65 5f      PRAGMA page_
17a0: 73 69 7a 65 20 3d 20 35 31 32 3b 0a 20 20 20 20  size = 512;.    
17b0: 50 52 41 47 4d 41 20 6a 6f 75 72 6e 61 6c 5f 6d  PRAGMA journal_m
17c0: 6f 64 65 20 3d 20 57 41 4c 3b 0a 20 20 20 20 50  ode = WAL;.    P
17d0: 52 41 47 4d 41 20 77 61 6c 5f 61 75 74 6f 63 68  RAGMA wal_autoch
17e0: 65 63 6b 70 6f 69 6e 74 20 3d 20 30 3b 0a 20 20  eckpoint = 0;.  
17f0: 20 20 43 52 45 41 54 45 20 54 41 42 4c 45 20 74    CREATE TABLE t
1800: 31 28 78 29 3b 0a 20 20 20 20 42 45 47 49 4e 3b  1(x);.    BEGIN;
1810: 0a 20 20 20 20 20 20 49 4e 53 45 52 54 20 49 4e  .      INSERT IN
1820: 54 4f 20 74 31 20 56 41 4c 55 45 53 28 72 61 6e  TO t1 VALUES(ran
1830: 64 6f 6d 62 6c 6f 62 28 34 30 30 29 29 3b 20 20  domblob(400));  
1840: 20 20 20 20 20 20 20 20 20 2f 2a 20 31 20 2a 2f           /* 1 */
1850: 0a 20 20 20 20 20 20 49 4e 53 45 52 54 20 49 4e  .      INSERT IN
1860: 54 4f 20 74 31 20 53 45 4c 45 43 54 20 72 61 6e  TO t1 SELECT ran
1870: 64 6f 6d 62 6c 6f 62 28 34 30 30 29 20 46 52 4f  domblob(400) FRO
1880: 4d 20 74 31 3b 20 20 20 20 2f 2a 20 32 20 2a 2f  M t1;    /* 2 */
1890: 0a 20 20 20 20 20 20 49 4e 53 45 52 54 20 49 4e  .      INSERT IN
18a0: 54 4f 20 74 31 20 53 45 4c 45 43 54 20 72 61 6e  TO t1 SELECT ran
18b0: 64 6f 6d 62 6c 6f 62 28 34 30 30 29 20 46 52 4f  domblob(400) FRO
18c0: 4d 20 74 31 3b 20 20 20 20 2f 2a 20 34 20 2a 2f  M t1;    /* 4 */
18d0: 0a 20 20 20 20 20 20 49 4e 53 45 52 54 20 49 4e  .      INSERT IN
18e0: 54 4f 20 74 31 20 53 45 4c 45 43 54 20 72 61 6e  TO t1 SELECT ran
18f0: 64 6f 6d 62 6c 6f 62 28 34 30 30 29 20 46 52 4f  domblob(400) FRO
1900: 4d 20 74 31 3b 20 20 20 20 2f 2a 20 38 20 2a 2f  M t1;    /* 8 */
1910: 0a 20 20 20 20 20 20 49 4e 53 45 52 54 20 49 4e  .      INSERT IN
1920: 54 4f 20 74 31 20 53 45 4c 45 43 54 20 72 61 6e  TO t1 SELECT ran
1930: 64 6f 6d 62 6c 6f 62 28 34 30 30 29 20 46 52 4f  domblob(400) FRO
1940: 4d 20 74 31 3b 20 20 20 20 2f 2a 20 31 36 20 2a  M t1;    /* 16 *
1950: 2f 0a 20 20 20 20 20 20 49 4e 53 45 52 54 20 49  /.      INSERT I
1960: 4e 54 4f 20 74 31 20 53 45 4c 45 43 54 20 72 61  NTO t1 SELECT ra
1970: 6e 64 6f 6d 62 6c 6f 62 28 34 30 30 29 20 46 52  ndomblob(400) FR
1980: 4f 4d 20 74 31 3b 20 20 20 20 2f 2a 20 33 32 20  OM t1;    /* 32 
1990: 2a 2f 0a 20 20 20 20 20 20 49 4e 53 45 52 54 20  */.      INSERT 
19a0: 49 4e 54 4f 20 74 31 20 53 45 4c 45 43 54 20 72  INTO t1 SELECT r
19b0: 61 6e 64 6f 6d 62 6c 6f 62 28 34 30 30 29 20 46  andomblob(400) F
19c0: 52 4f 4d 20 74 31 3b 20 20 20 20 2f 2a 20 36 34  ROM t1;    /* 64
19d0: 20 2a 2f 0a 20 20 20 20 20 20 49 4e 53 45 52 54   */.      INSERT
19e0: 20 49 4e 54 4f 20 74 31 20 53 45 4c 45 43 54 20   INTO t1 SELECT 
19f0: 72 61 6e 64 6f 6d 62 6c 6f 62 28 34 30 30 29 20  randomblob(400) 
1a00: 46 52 4f 4d 20 74 31 3b 20 20 20 20 2f 2a 20 31  FROM t1;    /* 1
1a10: 32 38 20 2a 2f 0a 20 20 20 20 20 20 49 4e 53 45  28 */.      INSE
1a20: 52 54 20 49 4e 54 4f 20 74 31 20 53 45 4c 45 43  RT INTO t1 SELEC
1a30: 54 20 72 61 6e 64 6f 6d 62 6c 6f 62 28 34 30 30  T randomblob(400
1a40: 29 20 46 52 4f 4d 20 74 31 3b 20 20 20 20 2f 2a  ) FROM t1;    /*
1a50: 20 32 35 36 20 2a 2f 0a 20 20 20 20 20 20 49 4e   256 */.      IN
1a60: 53 45 52 54 20 49 4e 54 4f 20 74 31 20 53 45 4c  SERT INTO t1 SEL
1a70: 45 43 54 20 72 61 6e 64 6f 6d 62 6c 6f 62 28 34  ECT randomblob(4
1a80: 30 30 29 20 46 52 4f 4d 20 74 31 3b 20 20 20 20  00) FROM t1;    
1a90: 2f 2a 20 35 31 32 20 2a 2f 0a 20 20 20 20 20 20  /* 512 */.      
1aa0: 49 4e 53 45 52 54 20 49 4e 54 4f 20 74 31 20 53  INSERT INTO t1 S
1ab0: 45 4c 45 43 54 20 72 61 6e 64 6f 6d 62 6c 6f 62  ELECT randomblob
1ac0: 28 34 30 30 29 20 46 52 4f 4d 20 74 31 3b 20 20  (400) FROM t1;  
1ad0: 20 20 2f 2a 20 31 30 32 34 20 2a 2f 0a 20 20 20    /* 1024 */.   
1ae0: 20 20 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 74     INSERT INTO t
1af0: 31 20 53 45 4c 45 43 54 20 72 61 6e 64 6f 6d 62  1 SELECT randomb
1b00: 6c 6f 62 28 34 30 30 29 20 46 52 4f 4d 20 74 31  lob(400) FROM t1
1b10: 3b 20 20 20 20 2f 2a 20 32 30 34 38 20 2a 2f 0a  ;    /* 2048 */.
1b20: 20 20 20 20 20 20 49 4e 53 45 52 54 20 49 4e 54        INSERT INT
1b30: 4f 20 74 31 20 53 45 4c 45 43 54 20 72 61 6e 64  O t1 SELECT rand
1b40: 6f 6d 62 6c 6f 62 28 34 30 30 29 20 46 52 4f 4d  omblob(400) FROM
1b50: 20 74 31 3b 20 20 20 20 2f 2a 20 34 30 39 36 20   t1;    /* 4096 
1b60: 2a 2f 0a 20 20 20 20 20 20 49 4e 53 45 52 54 20  */.      INSERT 
1b70: 49 4e 54 4f 20 74 31 20 53 45 4c 45 43 54 20 72  INTO t1 SELECT r
1b80: 61 6e 64 6f 6d 62 6c 6f 62 28 34 30 30 29 20 46  andomblob(400) F
1b90: 52 4f 4d 20 74 31 3b 20 20 20 20 2f 2a 20 38 31  ROM t1;    /* 81
1ba0: 39 32 20 2a 2f 0a 20 20 20 20 20 20 49 4e 53 45  92 */.      INSE
1bb0: 52 54 20 49 4e 54 4f 20 74 31 20 53 45 4c 45 43  RT INTO t1 SELEC
1bc0: 54 20 72 61 6e 64 6f 6d 62 6c 6f 62 28 34 30 30  T randomblob(400
1bd0: 29 20 46 52 4f 4d 20 74 31 3b 20 20 20 20 2f 2a  ) FROM t1;    /*
1be0: 20 31 36 33 38 34 20 2a 2f 0a 20 20 20 20 43 4f   16384 */.    CO
1bf0: 4d 4d 49 54 3b 0a 20 20 7d 0a 20 20 66 61 75 6c  MMIT;.  }.  faul
1c00: 74 73 69 6d 5f 73 61 76 65 5f 61 6e 64 5f 63 6c  tsim_save_and_cl
1c10: 6f 73 65 0a 7d 20 7b 7d 0a 64 6f 5f 66 61 75 6c  ose.} {}.do_faul
1c20: 74 73 69 6d 5f 74 65 73 74 20 77 61 6c 66 61 75  tsim_test walfau
1c30: 6c 74 2d 36 20 2d 66 61 75 6c 74 73 20 73 68 6d  lt-6 -faults shm
1c40: 65 72 72 2a 20 2d 70 72 65 70 20 7b 0a 20 20 66  err* -prep {.  f
1c50: 61 75 6c 74 73 69 6d 5f 72 65 73 74 6f 72 65 5f  aultsim_restore_
1c60: 61 6e 64 5f 72 65 6f 70 65 6e 0a 20 20 73 68 6d  and_reopen.  shm
1c70: 66 61 75 6c 74 20 66 69 6c 74 65 72 20 78 53 68  fault filter xSh
1c80: 6d 4d 61 70 0a 7d 20 2d 62 6f 64 79 20 7b 0a 20  mMap.} -body {. 
1c90: 20 65 78 65 63 73 71 6c 20 7b 20 53 45 4c 45 43   execsql { SELEC
1ca0: 54 20 63 6f 75 6e 74 28 2a 29 20 46 52 4f 4d 20  T count(*) FROM 
1cb0: 74 31 20 7d 0a 7d 20 2d 74 65 73 74 20 7b 0a 20  t1 }.} -test {. 
1cc0: 20 66 61 75 6c 74 73 69 6d 5f 74 65 73 74 5f 72   faultsim_test_r
1cd0: 65 73 75 6c 74 20 7b 30 20 31 36 33 38 34 7d 0a  esult {0 16384}.
1ce0: 20 20 66 61 75 6c 74 73 69 6d 5f 69 6e 74 65 67    faultsim_integ
1cf0: 72 69 74 79 5f 63 68 65 63 6b 0a 20 20 73 65 74  rity_check.  set
1d00: 20 6e 20 5b 64 62 20 6f 6e 65 20 7b 53 45 4c 45   n [db one {SELE
1d10: 43 54 20 63 6f 75 6e 74 28 2a 29 20 46 52 4f 4d  CT count(*) FROM
1d20: 20 74 31 7d 5d 0a 20 20 69 66 20 7b 24 6e 20 21   t1}].  if {$n !
1d30: 3d 20 31 36 33 38 34 20 26 26 20 24 6e 20 21 3d  = 16384 && $n !=
1d40: 20 30 7d 20 7b 20 65 72 72 6f 72 20 22 49 6e 63   0} { error "Inc
1d50: 6f 72 72 65 63 74 20 6e 75 6d 62 65 72 20 6f 66  orrect number of
1d60: 20 72 6f 77 73 3a 20 24 6e 22 20 7d 0a 7d 0a 0a   rows: $n" }.}..
1d70: 23 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  #---------------
1d80: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1d90: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1da0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1db0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 23 0a 64 6f  -----------.#.do
1dc0: 5f 74 65 73 74 20 77 61 6c 66 61 75 6c 74 2d 37  _test walfault-7
1dd0: 2d 70 72 65 2d 31 20 7b 0a 20 20 66 61 75 6c 74  -pre-1 {.  fault
1de0: 73 69 6d 5f 64 65 6c 65 74 65 5f 61 6e 64 5f 72  sim_delete_and_r
1df0: 65 6f 70 65 6e 0a 20 20 65 78 65 63 73 71 6c 20  eopen.  execsql 
1e00: 7b 0a 20 20 20 20 50 52 41 47 4d 41 20 70 61 67  {.    PRAGMA pag
1e10: 65 5f 73 69 7a 65 20 3d 20 35 31 32 3b 0a 20 20  e_size = 512;.  
1e20: 20 20 50 52 41 47 4d 41 20 6a 6f 75 72 6e 61 6c    PRAGMA journal
1e30: 5f 6d 6f 64 65 20 3d 20 57 41 4c 3b 0a 20 20 20  _mode = WAL;.   
1e40: 20 50 52 41 47 4d 41 20 77 61 6c 5f 61 75 74 6f   PRAGMA wal_auto
1e50: 63 68 65 63 6b 70 6f 69 6e 74 20 3d 20 30 3b 0a  checkpoint = 0;.
1e60: 20 20 20 20 43 52 45 41 54 45 20 54 41 42 4c 45      CREATE TABLE
1e70: 20 74 31 28 78 29 3b 0a 20 20 20 20 42 45 47 49   t1(x);.    BEGI
1e80: 4e 3b 0a 20 20 20 20 20 20 49 4e 53 45 52 54 20  N;.      INSERT 
1e90: 49 4e 54 4f 20 74 31 20 56 41 4c 55 45 53 28 72  INTO t1 VALUES(r
1ea0: 61 6e 64 6f 6d 62 6c 6f 62 28 34 30 30 29 29 3b  andomblob(400));
1eb0: 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 31 20             /* 1 
1ec0: 2a 2f 0a 20 20 20 20 20 20 49 4e 53 45 52 54 20  */.      INSERT 
1ed0: 49 4e 54 4f 20 74 31 20 53 45 4c 45 43 54 20 72  INTO t1 SELECT r
1ee0: 61 6e 64 6f 6d 62 6c 6f 62 28 34 30 30 29 20 46  andomblob(400) F
1ef0: 52 4f 4d 20 74 31 3b 20 20 20 20 2f 2a 20 32 20  ROM t1;    /* 2 
1f00: 2a 2f 0a 20 20 20 20 20 20 49 4e 53 45 52 54 20  */.      INSERT 
1f10: 49 4e 54 4f 20 74 31 20 53 45 4c 45 43 54 20 72  INTO t1 SELECT r
1f20: 61 6e 64 6f 6d 62 6c 6f 62 28 34 30 30 29 20 46  andomblob(400) F
1f30: 52 4f 4d 20 74 31 3b 20 20 20 20 2f 2a 20 34 20  ROM t1;    /* 4 
1f40: 2a 2f 0a 20 20 20 20 43 4f 4d 4d 49 54 3b 0a 20  */.    COMMIT;. 
1f50: 20 7d 0a 20 20 66 61 75 6c 74 73 69 6d 5f 73 61   }.  faultsim_sa
1f60: 76 65 5f 61 6e 64 5f 63 6c 6f 73 65 0a 7d 20 7b  ve_and_close.} {
1f70: 7d 0a 64 6f 5f 66 61 75 6c 74 73 69 6d 5f 74 65  }.do_faultsim_te
1f80: 73 74 20 77 61 6c 66 61 75 6c 74 2d 37 20 2d 70  st walfault-7 -p
1f90: 72 65 70 20 7b 0a 20 20 66 61 75 6c 74 73 69 6d  rep {.  faultsim
1fa0: 5f 72 65 73 74 6f 72 65 5f 61 6e 64 5f 72 65 6f  _restore_and_reo
1fb0: 70 65 6e 0a 7d 20 2d 62 6f 64 79 20 7b 0a 20 20  pen.} -body {.  
1fc0: 65 78 65 63 73 71 6c 20 7b 20 53 45 4c 45 43 54  execsql { SELECT
1fd0: 20 63 6f 75 6e 74 28 2a 29 20 46 52 4f 4d 20 74   count(*) FROM t
1fe0: 31 20 7d 0a 7d 20 2d 74 65 73 74 20 7b 0a 20 20  1 }.} -test {.  
1ff0: 66 61 75 6c 74 73 69 6d 5f 74 65 73 74 5f 72 65  faultsim_test_re
2000: 73 75 6c 74 20 7b 30 20 34 7d 0a 20 20 73 65 74  sult {0 4}.  set
2010: 20 6e 20 5b 64 62 20 6f 6e 65 20 7b 53 45 4c 45   n [db one {SELE
2020: 43 54 20 63 6f 75 6e 74 28 2a 29 20 46 52 4f 4d  CT count(*) FROM
2030: 20 74 31 7d 5d 0a 20 20 69 66 20 7b 24 6e 20 21   t1}].  if {$n !
2040: 3d 20 34 20 26 26 20 24 6e 20 21 3d 20 30 7d 20  = 4 && $n != 0} 
2050: 7b 20 65 72 72 6f 72 20 22 49 6e 63 6f 72 72 65  { error "Incorre
2060: 63 74 20 6e 75 6d 62 65 72 20 6f 66 20 72 6f 77  ct number of row
2070: 73 3a 20 24 6e 22 20 7d 0a 7d 0a 0a 23 2d 2d 2d  s: $n" }.}..#---
2080: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
2090: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
20a0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
20b0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
20c0: 2d 2d 2d 2d 2d 2d 2d 0a 23 0a 64 6f 5f 74 65 73  -------.#.do_tes
20d0: 74 20 77 61 6c 66 61 75 6c 74 2d 38 2d 70 72 65  t walfault-8-pre
20e0: 2d 31 20 7b 0a 20 20 66 61 75 6c 74 73 69 6d 5f  -1 {.  faultsim_
20f0: 64 65 6c 65 74 65 5f 61 6e 64 5f 72 65 6f 70 65  delete_and_reope
2100: 6e 0a 20 20 65 78 65 63 73 71 6c 20 7b 0a 20 20  n.  execsql {.  
2110: 20 20 50 52 41 47 4d 41 20 6a 6f 75 72 6e 61 6c    PRAGMA journal
2120: 5f 6d 6f 64 65 20 3d 20 57 41 4c 3b 0a 20 20 20  _mode = WAL;.   
2130: 20 43 52 45 41 54 45 20 54 41 42 4c 45 20 61 62   CREATE TABLE ab
2140: 63 28 61 20 50 52 49 4d 41 52 59 20 4b 45 59 29  c(a PRIMARY KEY)
2150: 3b 0a 20 20 20 20 49 4e 53 45 52 54 20 49 4e 54  ;.    INSERT INT
2160: 4f 20 61 62 63 20 56 41 4c 55 45 53 28 72 61 6e  O abc VALUES(ran
2170: 64 6f 6d 62 6c 6f 62 28 39 30 30 29 29 3b 0a 20  domblob(900));. 
2180: 20 7d 0a 20 20 66 61 75 6c 74 73 69 6d 5f 73 61   }.  faultsim_sa
2190: 76 65 5f 61 6e 64 5f 63 6c 6f 73 65 0a 7d 20 7b  ve_and_close.} {
21a0: 7d 0a 64 6f 5f 66 61 75 6c 74 73 69 6d 5f 74 65  }.do_faultsim_te
21b0: 73 74 20 77 61 6c 66 61 75 6c 74 2d 38 20 2d 70  st walfault-8 -p
21c0: 72 65 70 20 7b 0a 20 20 66 61 75 6c 74 73 69 6d  rep {.  faultsim
21d0: 5f 72 65 73 74 6f 72 65 5f 61 6e 64 5f 72 65 6f  _restore_and_reo
21e0: 70 65 6e 0a 20 20 65 78 65 63 73 71 6c 20 7b 20  pen.  execsql { 
21f0: 50 52 41 47 4d 41 20 63 61 63 68 65 5f 73 69 7a  PRAGMA cache_siz
2200: 65 20 3d 20 31 30 20 7d 0a 7d 20 2d 62 6f 64 79  e = 10 }.} -body
2210: 20 7b 0a 20 20 65 78 65 63 73 71 6c 20 7b 0a 20   {.  execsql {. 
2220: 20 20 20 42 45 47 49 4e 3b 0a 20 20 20 20 20 20     BEGIN;.      
2230: 49 4e 53 45 52 54 20 49 4e 54 4f 20 61 62 63 20  INSERT INTO abc 
2240: 53 45 4c 45 43 54 20 72 61 6e 64 6f 6d 62 6c 6f  SELECT randomblo
2250: 62 28 39 30 30 29 20 46 52 4f 4d 20 61 62 63 3b  b(900) FROM abc;
2260: 20 20 20 20 2f 2a 20 31 20 2a 2f 0a 20 20 20 20      /* 1 */.    
2270: 20 20 2d 2d 49 4e 53 45 52 54 20 49 4e 54 4f 20    --INSERT INTO 
2280: 61 62 63 20 53 45 4c 45 43 54 20 72 61 6e 64 6f  abc SELECT rando
2290: 6d 62 6c 6f 62 28 39 30 30 29 20 46 52 4f 4d 20  mblob(900) FROM 
22a0: 61 62 63 3b 20 20 20 20 2f 2a 20 32 20 2a 2f 0a  abc;    /* 2 */.
22b0: 20 20 20 20 20 20 2d 2d 49 4e 53 45 52 54 20 49        --INSERT I
22c0: 4e 54 4f 20 61 62 63 20 53 45 4c 45 43 54 20 72  NTO abc SELECT r
22d0: 61 6e 64 6f 6d 62 6c 6f 62 28 39 30 30 29 20 46  andomblob(900) F
22e0: 52 4f 4d 20 61 62 63 3b 20 20 20 20 2f 2a 20 34  ROM abc;    /* 4
22f0: 20 2a 2f 0a 20 20 20 20 20 20 2d 2d 49 4e 53 45   */.      --INSE
2300: 52 54 20 49 4e 54 4f 20 61 62 63 20 53 45 4c 45  RT INTO abc SELE
2310: 43 54 20 72 61 6e 64 6f 6d 62 6c 6f 62 28 39 30  CT randomblob(90
2320: 30 29 20 46 52 4f 4d 20 61 62 63 3b 20 20 20 20  0) FROM abc;    
2330: 2f 2a 20 38 20 2a 2f 0a 20 20 20 20 52 4f 4c 4c  /* 8 */.    ROLL
2340: 42 41 43 4b 3b 0a 20 20 20 20 53 45 4c 45 43 54  BACK;.    SELECT
2350: 20 63 6f 75 6e 74 28 2a 29 20 46 52 4f 4d 20 61   count(*) FROM a
2360: 62 63 3b 0a 20 20 7d 0a 7d 20 2d 74 65 73 74 20  bc;.  }.} -test 
2370: 7b 0a 20 20 66 61 75 6c 74 73 69 6d 5f 74 65 73  {.  faultsim_tes
2380: 74 5f 72 65 73 75 6c 74 20 7b 30 20 31 7d 0a 0a  t_result {0 1}..
2390: 20 20 66 61 75 6c 74 73 69 6d 5f 69 6e 74 65 67    faultsim_integ
23a0: 72 69 74 79 5f 63 68 65 63 6b 0a 20 20 63 61 74  rity_check.  cat
23b0: 63 68 20 7b 20 64 62 20 65 76 61 6c 20 52 4f 4c  ch { db eval ROL
23c0: 4c 42 41 43 4b 20 7d 0a 20 20 66 61 75 6c 74 73  LBACK }.  faults
23d0: 69 6d 5f 69 6e 74 65 67 72 69 74 79 5f 63 68 65  im_integrity_che
23e0: 63 6b 0a 0a 20 20 73 65 74 20 6e 20 5b 64 62 20  ck..  set n [db 
23f0: 6f 6e 65 20 7b 53 45 4c 45 43 54 20 63 6f 75 6e  one {SELECT coun
2400: 74 28 2a 29 20 46 52 4f 4d 20 61 62 63 7d 5d 0a  t(*) FROM abc}].
2410: 20 20 69 66 20 7b 24 6e 20 21 3d 20 31 7d 20 7b    if {$n != 1} {
2420: 20 65 72 72 6f 72 20 22 49 6e 63 6f 72 72 65 63   error "Incorrec
2430: 74 20 6e 75 6d 62 65 72 20 6f 66 20 72 6f 77 73  t number of rows
2440: 3a 20 24 6e 22 20 7d 0a 7d 0a 0a 23 2d 2d 2d 2d  : $n" }.}..#----
2450: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
2460: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
2470: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
2480: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
2490: 2d 2d 2d 2d 2d 2d 0a 23 0a 64 6f 5f 74 65 73 74  ------.#.do_test
24a0: 20 77 61 6c 66 61 75 6c 74 2d 39 2d 70 72 65 2d   walfault-9-pre-
24b0: 31 20 7b 0a 20 20 66 61 75 6c 74 73 69 6d 5f 64  1 {.  faultsim_d
24c0: 65 6c 65 74 65 5f 61 6e 64 5f 72 65 6f 70 65 6e  elete_and_reopen
24d0: 0a 20 20 65 78 65 63 73 71 6c 20 7b 0a 20 20 20  .  execsql {.   
24e0: 20 50 52 41 47 4d 41 20 6a 6f 75 72 6e 61 6c 5f   PRAGMA journal_
24f0: 6d 6f 64 65 20 3d 20 57 41 4c 3b 0a 20 20 20 20  mode = WAL;.    
2500: 43 52 45 41 54 45 20 54 41 42 4c 45 20 61 62 63  CREATE TABLE abc
2510: 28 61 20 50 52 49 4d 41 52 59 20 4b 45 59 29 3b  (a PRIMARY KEY);
2520: 0a 20 20 20 20 49 4e 53 45 52 54 20 49 4e 54 4f  .    INSERT INTO
2530: 20 61 62 63 20 56 41 4c 55 45 53 28 72 61 6e 64   abc VALUES(rand
2540: 6f 6d 62 6c 6f 62 28 39 30 30 29 29 3b 0a 20 20  omblob(900));.  
2550: 7d 0a 20 20 66 61 75 6c 74 73 69 6d 5f 73 61 76  }.  faultsim_sav
2560: 65 5f 61 6e 64 5f 63 6c 6f 73 65 0a 7d 20 7b 7d  e_and_close.} {}
2570: 0a 64 6f 5f 66 61 75 6c 74 73 69 6d 5f 74 65 73  .do_faultsim_tes
2580: 74 20 77 61 6c 66 61 75 6c 74 2d 39 20 2d 70 72  t walfault-9 -pr
2590: 65 70 20 7b 0a 20 20 23 69 66 20 7b 24 69 46 61  ep {.  #if {$iFa
25a0: 69 6c 3c 37 33 7d 20 7b 20 73 65 74 20 69 46 61  il<73} { set iFa
25b0: 69 6c 20 37 33 20 7d 0a 20 20 23 69 66 20 7b 24  il 73 }.  #if {$
25c0: 69 46 61 69 6c 3e 37 33 7d 20 7b 20 65 78 69 74  iFail>73} { exit
25d0: 20 7d 0a 20 20 0a 20 20 66 61 75 6c 74 73 69 6d   }.  .  faultsim
25e0: 5f 72 65 73 74 6f 72 65 5f 61 6e 64 5f 72 65 6f  _restore_and_reo
25f0: 70 65 6e 0a 20 20 65 78 65 63 73 71 6c 20 7b 20  pen.  execsql { 
2600: 50 52 41 47 4d 41 20 63 61 63 68 65 5f 73 69 7a  PRAGMA cache_siz
2610: 65 20 3d 20 31 30 20 7d 0a 7d 20 2d 62 6f 64 79  e = 10 }.} -body
2620: 20 7b 0a 20 20 65 78 65 63 73 71 6c 20 7b 0a 20   {.  execsql {. 
2630: 20 20 20 42 45 47 49 4e 3b 0a 20 20 20 20 20 20     BEGIN;.      
2640: 49 4e 53 45 52 54 20 49 4e 54 4f 20 61 62 63 20  INSERT INTO abc 
2650: 53 45 4c 45 43 54 20 72 61 6e 64 6f 6d 62 6c 6f  SELECT randomblo
2660: 62 28 39 30 30 29 20 46 52 4f 4d 20 61 62 63 3b  b(900) FROM abc;
2670: 20 20 20 20 2f 2a 20 31 20 2a 2f 0a 20 20 20 20      /* 1 */.    
2680: 20 20 53 41 56 45 50 4f 49 4e 54 20 73 70 6f 69    SAVEPOINT spoi
2690: 6e 74 3b 0a 20 20 20 20 20 20 20 20 49 4e 53 45  nt;.        INSE
26a0: 52 54 20 49 4e 54 4f 20 61 62 63 20 53 45 4c 45  RT INTO abc SELE
26b0: 43 54 20 72 61 6e 64 6f 6d 62 6c 6f 62 28 39 30  CT randomblob(90
26c0: 30 29 20 46 52 4f 4d 20 61 62 63 3b 20 20 20 20  0) FROM abc;    
26d0: 2f 2a 20 32 20 2a 2f 0a 20 20 20 20 20 20 20 20  /* 2 */.        
26e0: 49 4e 53 45 52 54 20 49 4e 54 4f 20 61 62 63 20  INSERT INTO abc 
26f0: 53 45 4c 45 43 54 20 72 61 6e 64 6f 6d 62 6c 6f  SELECT randomblo
2700: 62 28 39 30 30 29 20 46 52 4f 4d 20 61 62 63 3b  b(900) FROM abc;
2710: 20 20 20 20 2f 2a 20 34 20 2a 2f 0a 20 20 20 20      /* 4 */.    
2720: 20 20 20 20 49 4e 53 45 52 54 20 49 4e 54 4f 20      INSERT INTO 
2730: 61 62 63 20 53 45 4c 45 43 54 20 72 61 6e 64 6f  abc SELECT rando
2740: 6d 62 6c 6f 62 28 39 30 30 29 20 46 52 4f 4d 20  mblob(900) FROM 
2750: 61 62 63 3b 20 20 20 20 2f 2a 20 38 20 2a 2f 0a  abc;    /* 8 */.
2760: 20 20 20 20 20 20 52 4f 4c 4c 42 41 43 4b 20 54        ROLLBACK T
2770: 4f 20 73 70 6f 69 6e 74 3b 0a 20 20 20 20 43 4f  O spoint;.    CO
2780: 4d 4d 49 54 3b 0a 20 20 20 20 53 45 4c 45 43 54  MMIT;.    SELECT
2790: 20 63 6f 75 6e 74 28 2a 29 20 46 52 4f 4d 20 61   count(*) FROM a
27a0: 62 63 3b 0a 20 20 7d 0a 7d 20 2d 74 65 73 74 20  bc;.  }.} -test 
27b0: 7b 0a 20 20 66 61 75 6c 74 73 69 6d 5f 74 65 73  {.  faultsim_tes
27c0: 74 5f 72 65 73 75 6c 74 20 7b 30 20 32 7d 0a 20  t_result {0 2}. 
27d0: 20 66 61 75 6c 74 73 69 6d 5f 69 6e 74 65 67 72   faultsim_integr
27e0: 69 74 79 5f 63 68 65 63 6b 0a 0a 20 20 63 61 74  ity_check..  cat
27f0: 63 68 20 7b 20 64 62 20 65 76 61 6c 20 7b 20 52  ch { db eval { R
2800: 4f 4c 4c 42 41 43 4b 20 54 4f 20 73 70 6f 69 6e  OLLBACK TO spoin
2810: 74 20 7d 20 7d 0a 20 20 63 61 74 63 68 20 7b 20  t } }.  catch { 
2820: 64 62 20 65 76 61 6c 20 7b 20 43 4f 4d 4d 49 54  db eval { COMMIT
2830: 20 7d 20 7d 0a 20 20 73 65 74 20 6e 20 5b 64 62   } }.  set n [db
2840: 20 6f 6e 65 20 7b 53 45 4c 45 43 54 20 63 6f 75   one {SELECT cou
2850: 6e 74 28 2a 29 20 46 52 4f 4d 20 61 62 63 7d 5d  nt(*) FROM abc}]
2860: 0a 20 20 69 66 20 7b 24 6e 20 21 3d 20 31 20 26  .  if {$n != 1 &
2870: 26 20 24 6e 20 21 3d 20 32 7d 20 7b 20 65 72 72  & $n != 2} { err
2880: 6f 72 20 22 49 6e 63 6f 72 72 65 63 74 20 6e 75  or "Incorrect nu
2890: 6d 62 65 72 20 6f 66 20 72 6f 77 73 3a 20 24 6e  mber of rows: $n
28a0: 22 20 7d 0a 7d 0a 0a 64 6f 5f 74 65 73 74 20 77  " }.}..do_test w
28b0: 61 6c 66 61 75 6c 74 2d 31 30 2d 70 72 65 31 20  alfault-10-pre1 
28c0: 7b 0a 20 20 66 61 75 6c 74 73 69 6d 5f 64 65 6c  {.  faultsim_del
28d0: 65 74 65 5f 61 6e 64 5f 72 65 6f 70 65 6e 0a 20  ete_and_reopen. 
28e0: 20 65 78 65 63 73 71 6c 20 7b 0a 20 20 20 20 50   execsql {.    P
28f0: 52 41 47 4d 41 20 6a 6f 75 72 6e 61 6c 5f 6d 6f  RAGMA journal_mo
2900: 64 65 20 3d 20 57 41 4c 3b 0a 20 20 20 20 50 52  de = WAL;.    PR
2910: 41 47 4d 41 20 77 61 6c 5f 61 75 74 6f 63 68 65  AGMA wal_autoche
2920: 63 6b 70 6f 69 6e 74 20 3d 20 30 3b 0a 20 20 20  ckpoint = 0;.   
2930: 20 43 52 45 41 54 45 20 54 41 42 4c 45 20 7a 28   CREATE TABLE z(
2940: 7a 7a 20 49 4e 54 45 47 45 52 20 50 52 49 4d 41  zz INTEGER PRIMA
2950: 52 59 20 4b 45 59 2c 20 7a 7a 7a 20 42 4c 4f 42  RY KEY, zzz BLOB
2960: 29 3b 0a 20 20 20 20 43 52 45 41 54 45 20 49 4e  );.    CREATE IN
2970: 44 45 58 20 7a 7a 7a 7a 20 4f 4e 20 7a 28 7a 7a  DEX zzzz ON z(zz
2980: 7a 29 3b 0a 20 20 20 20 49 4e 53 45 52 54 20 49  z);.    INSERT I
2990: 4e 54 4f 20 7a 20 56 41 4c 55 45 53 28 4e 55 4c  NTO z VALUES(NUL
29a0: 4c 2c 20 72 61 6e 64 6f 6d 62 6c 6f 62 28 38 30  L, randomblob(80
29b0: 30 29 29 3b 0a 20 20 20 20 49 4e 53 45 52 54 20  0));.    INSERT 
29c0: 49 4e 54 4f 20 7a 20 56 41 4c 55 45 53 28 4e 55  INTO z VALUES(NU
29d0: 4c 4c 2c 20 72 61 6e 64 6f 6d 62 6c 6f 62 28 38  LL, randomblob(8
29e0: 30 30 29 29 3b 0a 20 20 20 20 49 4e 53 45 52 54  00));.    INSERT
29f0: 20 49 4e 54 4f 20 7a 20 53 45 4c 45 43 54 20 4e   INTO z SELECT N
2a00: 55 4c 4c 2c 20 72 61 6e 64 6f 6d 62 6c 6f 62 28  ULL, randomblob(
2a10: 38 30 30 29 20 46 52 4f 4d 20 7a 3b 0a 20 20 20  800) FROM z;.   
2a20: 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 7a 20 53   INSERT INTO z S
2a30: 45 4c 45 43 54 20 4e 55 4c 4c 2c 20 72 61 6e 64  ELECT NULL, rand
2a40: 6f 6d 62 6c 6f 62 28 38 30 30 29 20 46 52 4f 4d  omblob(800) FROM
2a50: 20 7a 3b 0a 20 20 20 20 49 4e 53 45 52 54 20 49   z;.    INSERT I
2a60: 4e 54 4f 20 7a 20 53 45 4c 45 43 54 20 4e 55 4c  NTO z SELECT NUL
2a70: 4c 2c 20 72 61 6e 64 6f 6d 62 6c 6f 62 28 38 30  L, randomblob(80
2a80: 30 29 20 46 52 4f 4d 20 7a 3b 0a 20 20 20 20 49  0) FROM z;.    I
2a90: 4e 53 45 52 54 20 49 4e 54 4f 20 7a 20 53 45 4c  NSERT INTO z SEL
2aa0: 45 43 54 20 4e 55 4c 4c 2c 20 72 61 6e 64 6f 6d  ECT NULL, random
2ab0: 62 6c 6f 62 28 38 30 30 29 20 46 52 4f 4d 20 7a  blob(800) FROM z
2ac0: 3b 0a 20 20 20 20 49 4e 53 45 52 54 20 49 4e 54  ;.    INSERT INT
2ad0: 4f 20 7a 20 53 45 4c 45 43 54 20 4e 55 4c 4c 2c  O z SELECT NULL,
2ae0: 20 72 61 6e 64 6f 6d 62 6c 6f 62 28 38 30 30 29   randomblob(800)
2af0: 20 46 52 4f 4d 20 7a 3b 0a 20 20 7d 0a 20 20 66   FROM z;.  }.  f
2b00: 61 75 6c 74 73 69 6d 5f 73 61 76 65 5f 61 6e 64  aultsim_save_and
2b10: 5f 63 6c 6f 73 65 0a 7d 20 7b 7d 0a 64 6f 5f 66  _close.} {}.do_f
2b20: 61 75 6c 74 73 69 6d 5f 74 65 73 74 20 77 61 6c  aultsim_test wal
2b30: 66 61 75 6c 74 2d 31 30 20 2d 70 72 65 70 20 7b  fault-10 -prep {
2b40: 0a 20 20 66 61 75 6c 74 73 69 6d 5f 72 65 73 74  .  faultsim_rest
2b50: 6f 72 65 5f 61 6e 64 5f 72 65 6f 70 65 6e 0a 20  ore_and_reopen. 
2b60: 20 65 78 65 63 73 71 6c 20 7b 0a 20 20 20 20 50   execsql {.    P
2b70: 52 41 47 4d 41 20 63 61 63 68 65 5f 73 69 7a 65  RAGMA cache_size
2b80: 20 3d 20 31 30 3b 0a 20 20 20 20 42 45 47 49 4e   = 10;.    BEGIN
2b90: 3b 0a 20 20 20 20 20 20 55 50 44 41 54 45 20 7a  ;.      UPDATE z
2ba0: 20 53 45 54 20 7a 7a 7a 20 3d 20 72 61 6e 64 6f   SET zzz = rando
2bb0: 6d 62 6c 6f 62 28 37 39 39 29 3b 0a 20 20 7d 0a  mblob(799);.  }.
2bc0: 0a 20 20 73 65 74 20 3a 3a 73 74 6d 74 20 5b 73  .  set ::stmt [s
2bd0: 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 20 64  qlite3_prepare d
2be0: 62 20 22 53 45 4c 45 43 54 20 7a 7a 7a 20 46 52  b "SELECT zzz FR
2bf0: 4f 4d 20 7a 20 57 48 45 52 45 20 7a 7a 20 49 4e  OM z WHERE zz IN
2c00: 20 28 31 2c 20 32 2c 20 33 29 22 20 2d 31 5d 0a   (1, 2, 3)" -1].
2c10: 20 20 73 71 6c 69 74 65 33 5f 73 74 65 70 20 24    sqlite3_step $
2c20: 3a 3a 73 74 6d 74 0a 7d 20 2d 62 6f 64 79 20 7b  ::stmt.} -body {
2c30: 0a 20 20 65 78 65 63 73 71 6c 20 7b 20 49 4e 53  .  execsql { INS
2c40: 45 52 54 20 49 4e 54 4f 20 7a 20 56 41 4c 55 45  ERT INTO z VALUE
2c50: 53 28 4e 55 4c 4c 2c 20 4e 55 4c 4c 29 20 7d 0a  S(NULL, NULL) }.
2c60: 7d 20 2d 74 65 73 74 20 7b 0a 20 20 73 71 6c 69  } -test {.  sqli
2c70: 74 65 33 5f 66 69 6e 61 6c 69 7a 65 20 24 3a 3a  te3_finalize $::
2c80: 73 74 6d 74 0a 20 20 66 61 75 6c 74 73 69 6d 5f  stmt.  faultsim_
2c90: 69 6e 74 65 67 72 69 74 79 5f 63 68 65 63 6b 0a  integrity_check.
2ca0: 0a 20 20 66 61 75 6c 74 73 69 6d 5f 74 65 73 74  .  faultsim_test
2cb0: 5f 72 65 73 75 6c 74 20 7b 30 20 7b 7d 7d 0a 20  _result {0 {}}. 
2cc0: 20 63 61 74 63 68 20 7b 20 64 62 20 65 76 61 6c   catch { db eval
2cd0: 20 7b 20 52 4f 4c 4c 42 41 43 4b 20 7d 20 7d 0a   { ROLLBACK } }.
2ce0: 20 20 66 61 75 6c 74 73 69 6d 5f 69 6e 74 65 67    faultsim_integ
2cf0: 72 69 74 79 5f 63 68 65 63 6b 0a 0a 20 20 73 65  rity_check..  se
2d00: 74 20 6e 20 5b 64 62 20 65 76 61 6c 20 7b 53 45  t n [db eval {SE
2d10: 4c 45 43 54 20 63 6f 75 6e 74 28 2a 29 2c 20 73  LECT count(*), s
2d20: 75 6d 28 6c 65 6e 67 74 68 28 7a 7a 7a 29 29 20  um(length(zzz)) 
2d30: 46 52 4f 4d 20 7a 7d 5d 0a 20 20 69 66 20 7b 24  FROM z}].  if {$
2d40: 6e 20 21 3d 20 22 36 34 20 35 31 32 30 30 22 7d  n != "64 51200"}
2d50: 20 7b 20 65 72 72 6f 72 20 22 49 6e 63 6f 72 72   { error "Incorr
2d60: 65 63 74 20 64 61 74 61 3a 20 24 6e 22 20 7d 0a  ect data: $n" }.
2d70: 7d 0a 0a 23 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  }..#------------
2d80: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
2d90: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
2da0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
2db0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 23  --------------.#
2dc0: 20 54 65 73 74 20 66 61 75 6c 74 20 69 6e 6a 65   Test fault inje
2dd0: 63 74 69 6f 6e 20 77 68 69 6c 65 20 63 68 65 63  ction while chec
2de0: 6b 70 6f 69 6e 74 69 6e 67 20 61 20 6c 61 72 67  kpointing a larg
2df0: 65 20 57 41 4c 20 66 69 6c 65 2c 20 69 66 20 74  e WAL file, if t
2e00: 68 65 20 0a 23 20 63 68 65 63 6b 70 6f 69 6e 74  he .# checkpoint
2e10: 20 69 73 20 74 68 65 20 66 69 72 73 74 20 6f 70   is the first op
2e20: 65 72 61 74 69 6f 6e 20 72 75 6e 20 61 66 74 65  eration run afte
2e30: 72 20 6f 70 65 6e 69 6e 67 20 74 68 65 20 64 61  r opening the da
2e40: 74 61 62 61 73 65 2e 0a 23 20 54 68 69 73 20 6d  tabase..# This m
2e50: 65 61 6e 73 20 74 68 61 74 20 73 6f 6d 65 20 6f  eans that some o
2e60: 66 20 74 68 65 20 72 65 71 75 69 72 65 64 20 77  f the required w
2e70: 61 6c 2d 69 6e 64 65 78 20 70 61 67 65 73 20 61  al-index pages a
2e80: 72 65 20 6d 61 70 70 65 64 20 61 73 20 70 61 72  re mapped as par
2e90: 74 20 6f 66 0a 23 20 74 68 65 20 63 68 65 63 6b  t of.# the check
2ea0: 70 6f 69 6e 74 20 70 72 6f 63 65 73 73 2c 20 77  point process, w
2eb0: 68 69 63 68 20 6d 65 61 6e 73 20 74 68 65 72 65  hich means there
2ec0: 20 61 72 65 20 61 20 66 65 77 20 6d 6f 72 65 20   are a few more 
2ed0: 6f 70 70 6f 72 74 75 6e 69 74 69 65 73 0a 23 20  opportunities.# 
2ee0: 66 6f 72 20 49 4f 20 65 72 72 6f 72 73 2e 0a 23  for IO errors..#
2ef0: 0a 23 20 54 6f 20 73 70 65 65 64 20 74 68 69 73  .# To speed this
2f00: 20 75 70 2c 20 49 4f 20 65 72 72 6f 72 73 20 61   up, IO errors a
2f10: 72 65 20 6f 6e 6c 79 20 73 69 6d 75 6c 61 74 65  re only simulate
2f20: 64 20 77 69 74 68 69 6e 20 78 53 68 6d 4d 61 70  d within xShmMap
2f30: 28 29 20 63 61 6c 6c 73 2e 0a 23 0a 64 6f 5f 74  () calls..#.do_t
2f40: 65 73 74 20 77 61 6c 66 61 75 6c 74 2d 31 31 2d  est walfault-11-
2f50: 70 72 65 2d 31 20 7b 0a 20 20 73 71 6c 69 74 65  pre-1 {.  sqlite
2f60: 33 20 64 62 20 74 65 73 74 2e 64 62 0a 20 20 65  3 db test.db.  e
2f70: 78 65 63 73 71 6c 20 7b 0a 20 20 20 20 50 52 41  xecsql {.    PRA
2f80: 47 4d 41 20 6a 6f 75 72 6e 61 6c 5f 6d 6f 64 65  GMA journal_mode
2f90: 20 3d 20 57 41 4c 3b 0a 20 20 20 20 50 52 41 47   = WAL;.    PRAG
2fa0: 4d 41 20 77 61 6c 5f 61 75 74 6f 63 68 65 63 6b  MA wal_autocheck
2fb0: 70 6f 69 6e 74 20 3d 20 30 3b 0a 20 20 20 20 42  point = 0;.    B
2fc0: 45 47 49 4e 3b 0a 20 20 20 20 20 20 43 52 45 41  EGIN;.      CREA
2fd0: 54 45 20 54 41 42 4c 45 20 61 62 63 28 61 20 50  TE TABLE abc(a P
2fe0: 52 49 4d 41 52 59 20 4b 45 59 29 3b 0a 20 20 20  RIMARY KEY);.   
2ff0: 20 20 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 61     INSERT INTO a
3000: 62 63 20 56 41 4c 55 45 53 28 72 61 6e 64 6f 6d  bc VALUES(random
3010: 62 6c 6f 62 28 31 35 30 30 29 29 3b 0a 20 20 20  blob(1500));.   
3020: 20 20 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 61     INSERT INTO a
3030: 62 63 20 56 41 4c 55 45 53 28 72 61 6e 64 6f 6d  bc VALUES(random
3040: 62 6c 6f 62 28 31 35 30 30 29 29 3b 0a 20 20 20  blob(1500));.   
3050: 20 20 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 61     INSERT INTO a
3060: 62 63 20 53 45 4c 45 43 54 20 72 61 6e 64 6f 6d  bc SELECT random
3070: 62 6c 6f 62 28 31 35 30 30 29 20 46 52 4f 4d 20  blob(1500) FROM 
3080: 61 62 63 3b 20 20 20 2d 2d 20 20 20 20 34 0a 20  abc;   --    4. 
3090: 20 20 20 20 20 49 4e 53 45 52 54 20 49 4e 54 4f       INSERT INTO
30a0: 20 61 62 63 20 53 45 4c 45 43 54 20 72 61 6e 64   abc SELECT rand
30b0: 6f 6d 62 6c 6f 62 28 31 35 30 30 29 20 46 52 4f  omblob(1500) FRO
30c0: 4d 20 61 62 63 3b 20 20 20 2d 2d 20 20 20 20 38  M abc;   --    8
30d0: 0a 20 20 20 20 20 20 49 4e 53 45 52 54 20 49 4e  .      INSERT IN
30e0: 54 4f 20 61 62 63 20 53 45 4c 45 43 54 20 72 61  TO abc SELECT ra
30f0: 6e 64 6f 6d 62 6c 6f 62 28 31 35 30 30 29 20 46  ndomblob(1500) F
3100: 52 4f 4d 20 61 62 63 3b 20 20 20 2d 2d 20 20 20  ROM abc;   --   
3110: 31 36 0a 20 20 20 20 20 20 49 4e 53 45 52 54 20  16.      INSERT 
3120: 49 4e 54 4f 20 61 62 63 20 53 45 4c 45 43 54 20  INTO abc SELECT 
3130: 72 61 6e 64 6f 6d 62 6c 6f 62 28 31 35 30 30 29  randomblob(1500)
3140: 20 46 52 4f 4d 20 61 62 63 3b 20 20 20 2d 2d 20   FROM abc;   -- 
3150: 20 20 33 32 0a 20 20 20 20 20 20 49 4e 53 45 52    32.      INSER
3160: 54 20 49 4e 54 4f 20 61 62 63 20 53 45 4c 45 43  T INTO abc SELEC
3170: 54 20 72 61 6e 64 6f 6d 62 6c 6f 62 28 31 35 30  T randomblob(150
3180: 30 29 20 46 52 4f 4d 20 61 62 63 3b 20 20 20 2d  0) FROM abc;   -
3190: 2d 20 20 20 36 34 0a 20 20 20 20 20 20 49 4e 53  -   64.      INS
31a0: 45 52 54 20 49 4e 54 4f 20 61 62 63 20 53 45 4c  ERT INTO abc SEL
31b0: 45 43 54 20 72 61 6e 64 6f 6d 62 6c 6f 62 28 31  ECT randomblob(1
31c0: 35 30 30 29 20 46 52 4f 4d 20 61 62 63 3b 20 20  500) FROM abc;  
31d0: 20 2d 2d 20 20 31 32 38 0a 20 20 20 20 20 20 49   --  128.      I
31e0: 4e 53 45 52 54 20 49 4e 54 4f 20 61 62 63 20 53  NSERT INTO abc S
31f0: 45 4c 45 43 54 20 72 61 6e 64 6f 6d 62 6c 6f 62  ELECT randomblob
3200: 28 31 35 30 30 29 20 46 52 4f 4d 20 61 62 63 3b  (1500) FROM abc;
3210: 20 20 20 2d 2d 20 20 32 35 36 0a 20 20 20 20 20     --  256.     
3220: 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 61 62 63   INSERT INTO abc
3230: 20 53 45 4c 45 43 54 20 72 61 6e 64 6f 6d 62 6c   SELECT randombl
3240: 6f 62 28 31 35 30 30 29 20 46 52 4f 4d 20 61 62  ob(1500) FROM ab
3250: 63 3b 20 20 20 2d 2d 20 20 35 31 32 0a 20 20 20  c;   --  512.   
3260: 20 20 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 61     INSERT INTO a
3270: 62 63 20 53 45 4c 45 43 54 20 72 61 6e 64 6f 6d  bc SELECT random
3280: 62 6c 6f 62 28 31 35 30 30 29 20 46 52 4f 4d 20  blob(1500) FROM 
3290: 61 62 63 3b 20 20 20 2d 2d 20 31 30 32 34 0a 20  abc;   -- 1024. 
32a0: 20 20 20 20 20 49 4e 53 45 52 54 20 49 4e 54 4f       INSERT INTO
32b0: 20 61 62 63 20 53 45 4c 45 43 54 20 72 61 6e 64   abc SELECT rand
32c0: 6f 6d 62 6c 6f 62 28 31 35 30 30 29 20 46 52 4f  omblob(1500) FRO
32d0: 4d 20 61 62 63 3b 20 20 20 2d 2d 20 32 30 34 38  M abc;   -- 2048
32e0: 0a 20 20 20 20 20 20 49 4e 53 45 52 54 20 49 4e  .      INSERT IN
32f0: 54 4f 20 61 62 63 20 53 45 4c 45 43 54 20 72 61  TO abc SELECT ra
3300: 6e 64 6f 6d 62 6c 6f 62 28 31 35 30 30 29 20 46  ndomblob(1500) F
3310: 52 4f 4d 20 61 62 63 3b 20 20 20 2d 2d 20 34 30  ROM abc;   -- 40
3320: 39 36 0a 20 20 20 20 43 4f 4d 4d 49 54 3b 0a 20  96.    COMMIT;. 
3330: 20 7d 0a 20 20 66 61 75 6c 74 73 69 6d 5f 73 61   }.  faultsim_sa
3340: 76 65 5f 61 6e 64 5f 63 6c 6f 73 65 0a 7d 20 7b  ve_and_close.} {
3350: 7d 0a 64 6f 5f 66 61 75 6c 74 73 69 6d 5f 74 65  }.do_faultsim_te
3360: 73 74 20 77 61 6c 66 61 75 6c 74 2d 31 31 20 2d  st walfault-11 -
3370: 66 61 75 6c 74 73 20 73 68 6d 65 72 72 2a 20 2d  faults shmerr* -
3380: 70 72 65 70 20 7b 0a 20 20 63 61 74 63 68 20 7b  prep {.  catch {
3390: 20 64 62 32 20 63 6c 6f 73 65 20 7d 0a 20 20 66   db2 close }.  f
33a0: 61 75 6c 74 73 69 6d 5f 72 65 73 74 6f 72 65 5f  aultsim_restore_
33b0: 61 6e 64 5f 72 65 6f 70 65 6e 0a 20 20 73 68 6d  and_reopen.  shm
33c0: 66 61 75 6c 74 20 66 69 6c 74 65 72 20 78 53 68  fault filter xSh
33d0: 6d 4d 61 70 0a 7d 20 2d 62 6f 64 79 20 7b 0a 20  mMap.} -body {. 
33e0: 20 64 62 20 65 76 61 6c 20 7b 20 53 45 4c 45 43   db eval { SELEC
33f0: 54 20 63 6f 75 6e 74 28 2a 29 20 46 52 4f 4d 20  T count(*) FROM 
3400: 61 62 63 20 7d 0a 20 20 73 71 6c 69 74 65 33 20  abc }.  sqlite3 
3410: 64 62 32 20 74 65 73 74 2e 64 62 20 2d 76 66 73  db2 test.db -vfs
3420: 20 73 68 6d 66 61 75 6c 74 0a 20 20 64 62 32 20   shmfault.  db2 
3430: 65 76 61 6c 20 7b 20 50 52 41 47 4d 41 20 77 61  eval { PRAGMA wa
3440: 6c 5f 63 68 65 63 6b 70 6f 69 6e 74 20 7d 0a 20  l_checkpoint }. 
3450: 20 73 65 74 20 7b 7d 20 7b 7d 0a 7d 20 2d 74 65   set {} {}.} -te
3460: 73 74 20 7b 0a 20 20 66 61 75 6c 74 73 69 6d 5f  st {.  faultsim_
3470: 74 65 73 74 5f 72 65 73 75 6c 74 20 7b 30 20 7b  test_result {0 {
3480: 7d 7d 0a 7d 0a 0a 23 2d 2d 2d 2d 2d 2d 2d 2d 2d  }}.}..#---------
3490: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
34a0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
34b0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
34c0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
34d0: 0a 23 20 54 65 73 74 20 74 68 65 20 68 61 6e 64  .# Test the hand
34e0: 6c 69 6e 67 20 6f 66 20 74 68 65 20 76 61 72 69  ling of the vari
34f0: 6f 75 73 20 49 4f 2f 4f 4f 4d 2f 53 48 4d 20 65  ous IO/OOM/SHM e
3500: 72 72 6f 72 73 20 74 68 61 74 20 6d 61 79 20 6f  rrors that may o
3510: 63 63 75 72 20 64 75 72 69 6e 67 20 0a 23 20 61  ccur during .# a
3520: 20 6c 6f 67 20 72 65 63 6f 76 65 72 79 20 6f 70   log recovery op
3530: 65 72 61 74 69 6f 6e 20 75 6e 64 65 72 74 61 6b  eration undertak
3540: 65 6e 20 61 73 20 70 61 72 74 20 6f 66 20 61 20  en as part of a 
3550: 63 61 6c 6c 20 74 6f 20 0a 23 20 73 71 6c 69 74  call to .# sqlit
3560: 65 33 5f 77 61 6c 5f 63 68 65 63 6b 70 6f 69 6e  e3_wal_checkpoin
3570: 74 28 29 2e 0a 23 20 0a 64 6f 5f 74 65 73 74 20  t()..# .do_test 
3580: 77 61 6c 66 61 75 6c 74 2d 31 32 2d 70 72 65 2d  walfault-12-pre-
3590: 31 20 7b 0a 20 20 66 61 75 6c 74 73 69 6d 5f 64  1 {.  faultsim_d
35a0: 65 6c 65 74 65 5f 61 6e 64 5f 72 65 6f 70 65 6e  elete_and_reopen
35b0: 0a 20 20 65 78 65 63 73 71 6c 20 7b 0a 20 20 20  .  execsql {.   
35c0: 20 50 52 41 47 4d 41 20 6a 6f 75 72 6e 61 6c 5f   PRAGMA journal_
35d0: 6d 6f 64 65 20 3d 20 57 41 4c 3b 0a 20 20 20 20  mode = WAL;.    
35e0: 50 52 41 47 4d 41 20 77 61 6c 5f 61 75 74 6f 63  PRAGMA wal_autoc
35f0: 68 65 63 6b 70 6f 69 6e 74 20 3d 20 30 3b 0a 20  heckpoint = 0;. 
3600: 20 20 20 42 45 47 49 4e 3b 0a 20 20 20 20 20 20     BEGIN;.      
3610: 43 52 45 41 54 45 20 54 41 42 4c 45 20 61 62 63  CREATE TABLE abc
3620: 28 61 20 50 52 49 4d 41 52 59 20 4b 45 59 29 3b  (a PRIMARY KEY);
3630: 0a 20 20 20 20 20 20 49 4e 53 45 52 54 20 49 4e  .      INSERT IN
3640: 54 4f 20 61 62 63 20 56 41 4c 55 45 53 28 72 61  TO abc VALUES(ra
3650: 6e 64 6f 6d 62 6c 6f 62 28 31 35 30 30 29 29 3b  ndomblob(1500));
3660: 0a 20 20 20 20 20 20 49 4e 53 45 52 54 20 49 4e  .      INSERT IN
3670: 54 4f 20 61 62 63 20 56 41 4c 55 45 53 28 72 61  TO abc VALUES(ra
3680: 6e 64 6f 6d 62 6c 6f 62 28 31 35 30 30 29 29 3b  ndomblob(1500));
3690: 0a 20 20 20 20 43 4f 4d 4d 49 54 3b 0a 20 20 7d  .    COMMIT;.  }
36a0: 0a 20 20 66 61 75 6c 74 73 69 6d 5f 73 61 76 65  .  faultsim_save
36b0: 5f 61 6e 64 5f 63 6c 6f 73 65 0a 7d 20 7b 7d 0a  _and_close.} {}.
36c0: 64 6f 5f 66 61 75 6c 74 73 69 6d 5f 74 65 73 74  do_faultsim_test
36d0: 20 77 61 6c 66 61 75 6c 74 2d 31 32 20 2d 70 72   walfault-12 -pr
36e0: 65 70 20 7b 0a 20 20 69 66 20 7b 5b 69 6e 66 6f  ep {.  if {[info
36f0: 20 63 6f 6d 6d 61 6e 64 73 20 73 68 6d 66 61 75   commands shmfau
3700: 6c 74 5d 20 3d 3d 20 22 22 7d 20 7b 0a 20 20 20  lt] == ""} {.   
3710: 20 74 65 73 74 76 66 73 20 73 68 6d 66 61 75 6c   testvfs shmfaul
3720: 74 20 2d 64 65 66 61 75 6c 74 20 74 72 75 65 0a  t -default true.
3730: 20 20 7d 0a 20 20 66 61 75 6c 74 73 69 6d 5f 72    }.  faultsim_r
3740: 65 73 74 6f 72 65 5f 61 6e 64 5f 72 65 6f 70 65  estore_and_reope
3750: 6e 0a 20 20 64 62 20 65 76 61 6c 20 7b 20 53 45  n.  db eval { SE
3760: 4c 45 43 54 20 2a 20 46 52 4f 4d 20 73 71 6c 69  LECT * FROM sqli
3770: 74 65 5f 6d 61 73 74 65 72 20 7d 0a 20 20 73 68  te_master }.  sh
3780: 6d 66 61 75 6c 74 20 73 68 6d 20 74 65 73 74 2e  mfault shm test.
3790: 64 62 20 5b 73 74 72 69 6e 67 20 72 65 70 65 61  db [string repea
37a0: 74 20 22 5c 30 30 30 22 20 34 30 5d 0a 7d 20 2d  t "\000" 40].} -
37b0: 62 6f 64 79 20 7b 0a 20 20 73 65 74 20 72 63 20  body {.  set rc 
37c0: 5b 73 71 6c 69 74 65 33 5f 77 61 6c 5f 63 68 65  [sqlite3_wal_che
37d0: 63 6b 70 6f 69 6e 74 20 64 62 5d 0a 20 20 69 66  ckpoint db].  if
37e0: 20 7b 24 72 63 20 21 3d 20 22 53 51 4c 49 54 45   {$rc != "SQLITE
37f0: 5f 4f 4b 22 7d 20 7b 20 65 72 72 6f 72 20 5b 73  _OK"} { error [s
3800: 71 6c 69 74 65 33 5f 65 72 72 6d 73 67 20 64 62  qlite3_errmsg db
3810: 5d 20 7d 0a 7d 20 2d 74 65 73 74 20 7b 0a 20 20  ] }.} -test {.  
3820: 64 62 20 63 6c 6f 73 65 0a 20 20 66 61 75 6c 74  db close.  fault
3830: 73 69 6d 5f 74 65 73 74 5f 72 65 73 75 6c 74 20  sim_test_result 
3840: 7b 30 20 7b 7d 7d 0a 7d 0a 0a 23 2d 2d 2d 2d 2d  {0 {}}.}..#-----
3850: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
3860: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
3870: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
3880: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
3890: 2d 2d 2d 2d 0a 23 20 54 65 73 74 20 73 69 6d 70  ----.# Test simp
38a0: 6c 65 20 72 65 63 6f 76 65 72 79 2c 20 72 65 61  le recovery, rea
38b0: 64 69 6e 67 20 61 6e 64 20 77 72 69 74 69 6e 67  ding and writing
38c0: 20 61 20 64 61 74 61 62 61 73 65 20 66 69 6c 65   a database file
38d0: 20 75 73 69 6e 67 20 61 20 0a 23 20 68 65 61 70   using a .# heap
38e0: 2d 6d 65 6d 6f 72 79 20 77 61 6c 2d 69 6e 64 65  -memory wal-inde
38f0: 78 2e 0a 23 20 0a 64 6f 5f 74 65 73 74 20 77 61  x..# .do_test wa
3900: 6c 66 61 75 6c 74 2d 31 33 2d 70 72 65 2d 31 20  lfault-13-pre-1 
3910: 7b 0a 20 20 66 61 75 6c 74 73 69 6d 5f 64 65 6c  {.  faultsim_del
3920: 65 74 65 5f 61 6e 64 5f 72 65 6f 70 65 6e 0a 20  ete_and_reopen. 
3930: 20 65 78 65 63 73 71 6c 20 7b 0a 20 20 20 20 50   execsql {.    P
3940: 52 41 47 4d 41 20 6a 6f 75 72 6e 61 6c 5f 6d 6f  RAGMA journal_mo
3950: 64 65 20 3d 20 57 41 4c 3b 0a 20 20 20 20 50 52  de = WAL;.    PR
3960: 41 47 4d 41 20 77 61 6c 5f 61 75 74 6f 63 68 65  AGMA wal_autoche
3970: 63 6b 70 6f 69 6e 74 20 3d 20 30 3b 0a 20 20 20  ckpoint = 0;.   
3980: 20 42 45 47 49 4e 3b 0a 20 20 20 20 20 20 43 52   BEGIN;.      CR
3990: 45 41 54 45 20 54 41 42 4c 45 20 61 62 63 28 61  EATE TABLE abc(a
39a0: 20 50 52 49 4d 41 52 59 20 4b 45 59 29 3b 0a 20   PRIMARY KEY);. 
39b0: 20 20 20 20 20 49 4e 53 45 52 54 20 49 4e 54 4f       INSERT INTO
39c0: 20 61 62 63 20 56 41 4c 55 45 53 28 72 61 6e 64   abc VALUES(rand
39d0: 6f 6d 62 6c 6f 62 28 31 35 30 30 29 29 3b 0a 20  omblob(1500));. 
39e0: 20 20 20 20 20 49 4e 53 45 52 54 20 49 4e 54 4f       INSERT INTO
39f0: 20 61 62 63 20 56 41 4c 55 45 53 28 72 61 6e 64   abc VALUES(rand
3a00: 6f 6d 62 6c 6f 62 28 31 35 30 30 29 29 3b 0a 20  omblob(1500));. 
3a10: 20 20 20 43 4f 4d 4d 49 54 3b 0a 20 20 7d 0a 20     COMMIT;.  }. 
3a20: 20 66 61 75 6c 74 73 69 6d 5f 73 61 76 65 5f 61   faultsim_save_a
3a30: 6e 64 5f 63 6c 6f 73 65 0a 20 20 64 65 6c 65 74  nd_close.  delet
3a40: 65 5f 66 69 6c 65 20 73 76 5f 74 65 73 74 2e 64  e_file sv_test.d
3a50: 62 2d 73 68 6d 0a 7d 20 7b 7d 0a 0a 64 6f 5f 66  b-shm.} {}..do_f
3a60: 61 75 6c 74 73 69 6d 5f 74 65 73 74 20 77 61 6c  aultsim_test wal
3a70: 66 61 75 6c 74 2d 31 33 2e 31 20 2d 70 72 65 70  fault-13.1 -prep
3a80: 20 7b 0a 20 20 66 61 75 6c 74 73 69 6d 5f 72 65   {.  faultsim_re
3a90: 73 74 6f 72 65 5f 61 6e 64 5f 72 65 6f 70 65 6e  store_and_reopen
3aa0: 0a 7d 20 2d 62 6f 64 79 20 7b 0a 20 20 64 62 20  .} -body {.  db 
3ab0: 65 76 61 6c 20 7b 20 50 52 41 47 4d 41 20 6c 6f  eval { PRAGMA lo
3ac0: 63 6b 69 6e 67 5f 6d 6f 64 65 20 3d 20 65 78 63  cking_mode = exc
3ad0: 6c 75 73 69 76 65 20 7d 0a 20 20 64 62 20 65 76  lusive }.  db ev
3ae0: 61 6c 20 7b 20 53 45 4c 45 43 54 20 63 6f 75 6e  al { SELECT coun
3af0: 74 28 2a 29 20 46 52 4f 4d 20 61 62 63 20 7d 0a  t(*) FROM abc }.
3b00: 7d 20 2d 74 65 73 74 20 7b 0a 20 20 66 61 75 6c  } -test {.  faul
3b10: 74 73 69 6d 5f 74 65 73 74 5f 72 65 73 75 6c 74  tsim_test_result
3b20: 20 7b 30 20 32 7d 0a 20 20 69 66 20 7b 5b 66 69   {0 2}.  if {[fi
3b30: 6c 65 20 65 78 69 73 74 73 20 74 65 73 74 2e 64  le exists test.d
3b40: 62 2d 73 68 6d 5d 7d 20 7b 20 65 72 72 6f 72 20  b-shm]} { error 
3b50: 22 4e 6f 74 20 75 73 69 6e 67 20 68 65 61 70 2d  "Not using heap-
3b60: 6d 65 6d 6f 72 79 20 6d 6f 64 65 22 20 7d 0a 20  memory mode" }. 
3b70: 20 66 61 75 6c 74 73 69 6d 5f 69 6e 74 65 67 72   faultsim_integr
3b80: 69 74 79 5f 63 68 65 63 6b 0a 7d 0a 0a 64 6f 5f  ity_check.}..do_
3b90: 66 61 75 6c 74 73 69 6d 5f 74 65 73 74 20 77 61  faultsim_test wa
3ba0: 6c 66 61 75 6c 74 2d 31 33 2e 32 20 2d 70 72 65  lfault-13.2 -pre
3bb0: 70 20 7b 0a 20 20 66 61 75 6c 74 73 69 6d 5f 72  p {.  faultsim_r
3bc0: 65 73 74 6f 72 65 5f 61 6e 64 5f 72 65 6f 70 65  estore_and_reope
3bd0: 6e 0a 20 20 64 62 20 65 76 61 6c 20 7b 20 50 52  n.  db eval { PR
3be0: 41 47 4d 41 20 6c 6f 63 6b 69 6e 67 5f 6d 6f 64  AGMA locking_mod
3bf0: 65 20 3d 20 65 78 63 6c 75 73 69 76 65 20 7d 0a  e = exclusive }.
3c00: 7d 20 2d 62 6f 64 79 20 7b 0a 20 20 64 62 20 65  } -body {.  db e
3c10: 76 61 6c 20 7b 20 50 52 41 47 4d 41 20 6a 6f 75  val { PRAGMA jou
3c20: 72 6e 61 6c 5f 6d 6f 64 65 20 3d 20 64 65 6c 65  rnal_mode = dele
3c30: 74 65 20 7d 0a 7d 20 2d 74 65 73 74 20 7b 0a 20  te }.} -test {. 
3c40: 20 66 61 75 6c 74 73 69 6d 5f 74 65 73 74 5f 72   faultsim_test_r
3c50: 65 73 75 6c 74 20 7b 30 20 64 65 6c 65 74 65 7d  esult {0 delete}
3c60: 0a 20 20 69 66 20 7b 5b 66 69 6c 65 20 65 78 69  .  if {[file exi
3c70: 73 74 73 20 74 65 73 74 2e 64 62 2d 73 68 6d 5d  sts test.db-shm]
3c80: 7d 20 7b 20 65 72 72 6f 72 20 22 4e 6f 74 20 75  } { error "Not u
3c90: 73 69 6e 67 20 68 65 61 70 2d 6d 65 6d 6f 72 79  sing heap-memory
3ca0: 20 6d 6f 64 65 22 20 7d 0a 20 20 66 61 75 6c 74   mode" }.  fault
3cb0: 73 69 6d 5f 69 6e 74 65 67 72 69 74 79 5f 63 68  sim_integrity_ch
3cc0: 65 63 6b 0a 7d 0a 0a 64 6f 5f 74 65 73 74 20 77  eck.}..do_test w
3cd0: 61 6c 66 61 75 6c 74 2d 31 33 2d 70 72 65 2d 32  alfault-13-pre-2
3ce0: 20 7b 0a 20 20 66 61 75 6c 74 73 69 6d 5f 64 65   {.  faultsim_de
3cf0: 6c 65 74 65 5f 61 6e 64 5f 72 65 6f 70 65 6e 0a  lete_and_reopen.
3d00: 20 20 65 78 65 63 73 71 6c 20 7b 0a 20 20 20 20    execsql {.    
3d10: 42 45 47 49 4e 3b 0a 20 20 20 20 20 20 43 52 45  BEGIN;.      CRE
3d20: 41 54 45 20 54 41 42 4c 45 20 61 62 63 28 61 20  ATE TABLE abc(a 
3d30: 50 52 49 4d 41 52 59 20 4b 45 59 29 3b 0a 20 20  PRIMARY KEY);.  
3d40: 20 20 20 20 49 4e 53 45 52 54 20 49 4e 54 4f 20      INSERT INTO 
3d50: 61 62 63 20 56 41 4c 55 45 53 28 72 61 6e 64 6f  abc VALUES(rando
3d60: 6d 62 6c 6f 62 28 31 35 30 30 29 29 3b 0a 20 20  mblob(1500));.  
3d70: 20 20 20 20 49 4e 53 45 52 54 20 49 4e 54 4f 20      INSERT INTO 
3d80: 61 62 63 20 56 41 4c 55 45 53 28 72 61 6e 64 6f  abc VALUES(rando
3d90: 6d 62 6c 6f 62 28 31 35 30 30 29 29 3b 0a 20 20  mblob(1500));.  
3da0: 20 20 43 4f 4d 4d 49 54 3b 0a 20 20 7d 0a 20 20    COMMIT;.  }.  
3db0: 66 61 75 6c 74 73 69 6d 5f 73 61 76 65 5f 61 6e  faultsim_save_an
3dc0: 64 5f 63 6c 6f 73 65 0a 7d 20 7b 7d 0a 0a 64 6f  d_close.} {}..do
3dd0: 5f 66 61 75 6c 74 73 69 6d 5f 74 65 73 74 20 77  _faultsim_test w
3de0: 61 6c 66 61 75 6c 74 2d 31 33 2e 33 20 2d 70 72  alfault-13.3 -pr
3df0: 65 70 20 7b 0a 20 20 66 61 75 6c 74 73 69 6d 5f  ep {.  faultsim_
3e00: 72 65 73 74 6f 72 65 5f 61 6e 64 5f 72 65 6f 70  restore_and_reop
3e10: 65 6e 0a 7d 20 2d 62 6f 64 79 20 7b 0a 20 20 64  en.} -body {.  d
3e20: 62 20 65 76 61 6c 20 7b 20 0a 20 20 20 20 50 52  b eval { .    PR
3e30: 41 47 4d 41 20 6c 6f 63 6b 69 6e 67 5f 6d 6f 64  AGMA locking_mod
3e40: 65 20 3d 20 65 78 63 6c 75 73 69 76 65 3b 0a 20  e = exclusive;. 
3e50: 20 20 20 50 52 41 47 4d 41 20 6a 6f 75 72 6e 61     PRAGMA journa
3e60: 6c 5f 6d 6f 64 65 20 3d 20 57 41 4c 3b 0a 20 20  l_mode = WAL;.  
3e70: 20 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 61 62    INSERT INTO ab
3e80: 63 20 56 41 4c 55 45 53 28 72 61 6e 64 6f 6d 62  c VALUES(randomb
3e90: 6c 6f 62 28 31 35 30 30 29 29 3b 0a 20 20 7d 0a  lob(1500));.  }.
3ea0: 7d 20 2d 74 65 73 74 20 7b 0a 20 20 66 61 75 6c  } -test {.  faul
3eb0: 74 73 69 6d 5f 74 65 73 74 5f 72 65 73 75 6c 74  tsim_test_result
3ec0: 20 7b 30 20 7b 65 78 63 6c 75 73 69 76 65 20 77   {0 {exclusive w
3ed0: 61 6c 7d 7d 0a 20 20 69 66 20 7b 5b 66 69 6c 65  al}}.  if {[file
3ee0: 20 65 78 69 73 74 73 20 74 65 73 74 2e 64 62 2d   exists test.db-
3ef0: 73 68 6d 5d 7d 20 7b 20 65 72 72 6f 72 20 22 4e  shm]} { error "N
3f00: 6f 74 20 75 73 69 6e 67 20 68 65 61 70 2d 6d 65  ot using heap-me
3f10: 6d 6f 72 79 20 6d 6f 64 65 22 20 7d 0a 20 20 66  mory mode" }.  f
3f20: 61 75 6c 74 73 69 6d 5f 69 6e 74 65 67 72 69 74  aultsim_integrit
3f30: 79 5f 63 68 65 63 6b 0a 20 20 73 65 74 20 6e 52  y_check.  set nR
3f40: 6f 77 20 5b 64 62 20 65 76 61 6c 20 7b 53 45 4c  ow [db eval {SEL
3f50: 45 43 54 20 63 6f 75 6e 74 28 2a 29 20 46 52 4f  ECT count(*) FRO
3f60: 4d 20 61 62 63 7d 5d 0a 20 20 69 66 20 7b 21 28  M abc}].  if {!(
3f70: 28 24 6e 52 6f 77 3d 3d 32 20 26 26 20 24 74 65  ($nRow==2 && $te
3f80: 73 74 72 63 29 20 7c 7c 20 24 6e 52 6f 77 3d 3d  strc) || $nRow==
3f90: 33 29 7d 20 7b 20 65 72 72 6f 72 20 22 42 61 64  3)} { error "Bad
3fa0: 20 64 62 20 63 6f 6e 74 65 6e 74 22 20 7d 0a 7d   db content" }.}
3fb0: 0a 0a 23 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ..#-------------
3fc0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
3fd0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
3fe0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
3ff0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 23 20 54  ------------.# T
4000: 65 73 74 20 66 61 75 6c 74 2d 68 61 6e 64 6c 69  est fault-handli
4010: 6e 67 20 77 68 65 6e 20 77 72 61 70 70 69 6e 67  ng when wrapping
4020: 20 61 72 6f 75 6e 64 20 74 6f 20 74 68 65 20 73   around to the s
4030: 74 61 72 74 20 6f 66 20 61 20 57 41 4c 20 66 69  tart of a WAL fi
4040: 6c 65 2e 0a 23 0a 64 6f 5f 74 65 73 74 20 77 61  le..#.do_test wa
4050: 6c 66 61 75 6c 74 2d 31 34 2d 70 72 65 20 7b 0a  lfault-14-pre {.
4060: 20 20 66 61 75 6c 74 73 69 6d 5f 64 65 6c 65 74    faultsim_delet
4070: 65 5f 61 6e 64 5f 72 65 6f 70 65 6e 0a 20 20 65  e_and_reopen.  e
4080: 78 65 63 73 71 6c 20 7b 0a 20 20 20 20 50 52 41  xecsql {.    PRA
4090: 47 4d 41 20 61 75 74 6f 5f 76 61 63 75 75 6d 20  GMA auto_vacuum 
40a0: 3d 20 30 3b 0a 20 20 20 20 50 52 41 47 4d 41 20  = 0;.    PRAGMA 
40b0: 6a 6f 75 72 6e 61 6c 5f 6d 6f 64 65 20 3d 20 57  journal_mode = W
40c0: 41 4c 3b 0a 20 20 20 20 42 45 47 49 4e 3b 0a 20  AL;.    BEGIN;. 
40d0: 20 20 20 20 20 43 52 45 41 54 45 20 54 41 42 4c       CREATE TABL
40e0: 45 20 61 62 63 28 61 20 50 52 49 4d 41 52 59 20  E abc(a PRIMARY 
40f0: 4b 45 59 29 3b 0a 20 20 20 20 20 20 49 4e 53 45  KEY);.      INSE
4100: 52 54 20 49 4e 54 4f 20 61 62 63 20 56 41 4c 55  RT INTO abc VALU
4110: 45 53 28 72 61 6e 64 6f 6d 62 6c 6f 62 28 31 35  ES(randomblob(15
4120: 30 30 29 29 3b 0a 20 20 20 20 20 20 49 4e 53 45  00));.      INSE
4130: 52 54 20 49 4e 54 4f 20 61 62 63 20 56 41 4c 55  RT INTO abc VALU
4140: 45 53 28 72 61 6e 64 6f 6d 62 6c 6f 62 28 31 35  ES(randomblob(15
4150: 30 30 29 29 3b 0a 20 20 20 20 43 4f 4d 4d 49 54  00));.    COMMIT
4160: 3b 0a 20 20 7d 0a 20 20 66 61 75 6c 74 73 69 6d  ;.  }.  faultsim
4170: 5f 73 61 76 65 5f 61 6e 64 5f 63 6c 6f 73 65 0a  _save_and_close.
4180: 7d 20 7b 7d 0a 64 6f 5f 66 61 75 6c 74 73 69 6d  } {}.do_faultsim
4190: 5f 74 65 73 74 20 77 61 6c 66 61 75 6c 74 2d 31  _test walfault-1
41a0: 34 20 2d 70 72 65 70 20 7b 0a 20 20 66 61 75 6c  4 -prep {.  faul
41b0: 74 73 69 6d 5f 72 65 73 74 6f 72 65 5f 61 6e 64  tsim_restore_and
41c0: 5f 72 65 6f 70 65 6e 0a 7d 20 2d 62 6f 64 79 20  _reopen.} -body 
41d0: 7b 0a 20 20 64 62 20 65 76 61 6c 20 7b 20 0a 20  {.  db eval { . 
41e0: 20 20 20 50 52 41 47 4d 41 20 77 61 6c 5f 63 68     PRAGMA wal_ch
41f0: 65 63 6b 70 6f 69 6e 74 20 3d 20 66 75 6c 6c 3b  eckpoint = full;
4200: 0a 20 20 20 20 49 4e 53 45 52 54 20 49 4e 54 4f  .    INSERT INTO
4210: 20 61 62 63 20 56 41 4c 55 45 53 28 72 61 6e 64   abc VALUES(rand
4220: 6f 6d 62 6c 6f 62 28 31 35 30 30 29 29 3b 0a 20  omblob(1500));. 
4230: 20 7d 0a 7d 20 2d 74 65 73 74 20 7b 0a 20 20 66   }.} -test {.  f
4240: 61 75 6c 74 73 69 6d 5f 74 65 73 74 5f 72 65 73  aultsim_test_res
4250: 75 6c 74 20 7b 30 20 7b 30 20 31 30 20 31 30 7d  ult {0 {0 10 10}
4260: 7d 0a 20 20 66 61 75 6c 74 73 69 6d 5f 69 6e 74  }.  faultsim_int
4270: 65 67 72 69 74 79 5f 63 68 65 63 6b 0a 20 20 73  egrity_check.  s
4280: 65 74 20 6e 52 6f 77 20 5b 64 62 20 65 76 61 6c  et nRow [db eval
4290: 20 7b 53 45 4c 45 43 54 20 63 6f 75 6e 74 28 2a   {SELECT count(*
42a0: 29 20 46 52 4f 4d 20 61 62 63 7d 5d 0a 20 20 69  ) FROM abc}].  i
42b0: 66 20 7b 21 28 28 24 6e 52 6f 77 3d 3d 32 20 26  f {!(($nRow==2 &
42c0: 26 20 24 74 65 73 74 72 63 29 20 7c 7c 20 24 6e  & $testrc) || $n
42d0: 52 6f 77 3d 3d 33 29 7d 20 7b 20 65 72 72 6f 72  Row==3)} { error
42e0: 20 22 42 61 64 20 64 62 20 63 6f 6e 74 65 6e 74   "Bad db content
42f0: 22 20 7d 0a 7d 0a 0a 66 69 6e 69 73 68 5f 74 65  " }.}..finish_te
4300: 73 74 0a                                         st.