/ Hex Artifact Content
Login
SQLite training in Houston TX on 2019-11-05 (details)
Part of the 2019 Tcl Conference

Artifact 09b8ad7e52d2f54bce50e31aa7ea51412bb9f70ac13c74e669ddcd8b48b0d98d:


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 23 2d 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: 0a 23 0a 69 66 20 7b 5b 70 65 72 6d 75 74 61 74  .#.if {[permutat
0f10: 69 6f 6e 5d 20 21 3d 20 22 69 6e 6d 65 6d 6f 72  ion] != "inmemor
0f20: 79 5f 6a 6f 75 72 6e 61 6c 22 7d 20 7b 0a 20 20  y_journal"} {.  
0f30: 66 61 75 6c 74 73 69 6d 5f 64 65 6c 65 74 65 5f  faultsim_delete_
0f40: 61 6e 64 5f 72 65 6f 70 65 6e 0a 20 20 66 61 75  and_reopen.  fau
0f50: 6c 74 73 69 6d 5f 73 61 76 65 5f 61 6e 64 5f 63  ltsim_save_and_c
0f60: 6c 6f 73 65 0a 20 20 64 6f 5f 66 61 75 6c 74 73  lose.  do_faults
0f70: 69 6d 5f 74 65 73 74 20 77 61 6c 66 61 75 6c 74  im_test walfault
0f80: 2d 34 20 2d 70 72 65 70 20 7b 0a 20 20 20 20 66  -4 -prep {.    f
0f90: 61 75 6c 74 73 69 6d 5f 72 65 73 74 6f 72 65 5f  aultsim_restore_
0fa0: 61 6e 64 5f 72 65 6f 70 65 6e 0a 20 20 7d 20 2d  and_reopen.  } -
0fb0: 62 6f 64 79 20 7b 0a 20 20 20 20 65 78 65 63 73  body {.    execs
0fc0: 71 6c 20 7b 0a 20 20 20 20 20 20 50 52 41 47 4d  ql {.      PRAGM
0fd0: 41 20 61 75 74 6f 5f 76 61 63 75 75 6d 20 3d 20  A auto_vacuum = 
0fe0: 30 3b 0a 20 20 20 20 20 20 50 52 41 47 4d 41 20  0;.      PRAGMA 
0ff0: 6a 6f 75 72 6e 61 6c 5f 6d 6f 64 65 20 3d 20 57  journal_mode = W
1000: 41 4c 3b 0a 20 20 20 20 20 20 43 52 45 41 54 45  AL;.      CREATE
1010: 20 54 41 42 4c 45 20 74 31 28 61 20 50 52 49 4d   TABLE t1(a PRIM
1020: 41 52 59 20 4b 45 59 2c 20 62 29 3b 0a 20 20 20  ARY KEY, b);.   
1030: 20 20 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 74     INSERT INTO t
1040: 31 20 56 41 4c 55 45 53 28 27 61 27 2c 20 27 62  1 VALUES('a', 'b
1050: 27 29 3b 0a 20 20 20 20 20 20 50 52 41 47 4d 41  ');.      PRAGMA
1060: 20 77 61 6c 5f 63 68 65 63 6b 70 6f 69 6e 74 3b   wal_checkpoint;
1070: 0a 20 20 20 20 20 20 53 45 4c 45 43 54 20 2a 20  .      SELECT * 
1080: 46 52 4f 4d 20 74 31 3b 0a 20 20 20 20 7d 0a 20  FROM t1;.    }. 
1090: 20 7d 20 2d 74 65 73 74 20 7b 0a 20 20 20 20 23   } -test {.    #
10a0: 20 55 70 64 61 74 65 3a 20 54 68 65 20 66 6f 6c   Update: The fol
10b0: 6c 6f 77 69 6e 67 20 63 68 61 6e 67 65 64 20 66  lowing changed f
10c0: 72 6f 6d 20 7b 30 20 7b 77 61 6c 20 30 20 37 20  rom {0 {wal 0 7 
10d0: 37 20 61 20 62 7d 7d 20 61 73 20 61 20 72 65 73  7 a b}} as a res
10e0: 75 6c 74 0a 20 20 20 20 23 20 6f 66 20 50 53 4f  ult.    # of PSO
10f0: 57 20 62 65 69 6e 67 20 73 65 74 20 62 79 20 64  W being set by d
1100: 65 66 61 75 6c 74 2e 0a 20 20 20 20 66 61 75 6c  efault..    faul
1110: 74 73 69 6d 5f 74 65 73 74 5f 72 65 73 75 6c 74  tsim_test_result
1120: 20 7b 30 20 7b 77 61 6c 20 30 20 35 20 35 20 61   {0 {wal 0 5 5 a
1130: 20 62 7d 7d 0a 20 20 20 20 66 61 75 6c 74 73 69   b}}.    faultsi
1140: 6d 5f 69 6e 74 65 67 72 69 74 79 5f 63 68 65 63  m_integrity_chec
1150: 6b 0a 20 20 7d 20 0a 7d 0a 0a 23 2d 2d 2d 2d 2d  k.  } .}..#-----
1160: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1170: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1180: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1190: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
11a0: 2d 2d 2d 2d 2d 0a 23 0a 64 6f 5f 74 65 73 74 20  -----.#.do_test 
11b0: 77 61 6c 66 61 75 6c 74 2d 35 2d 70 72 65 2d 31  walfault-5-pre-1
11c0: 20 7b 0a 20 20 66 61 75 6c 74 73 69 6d 5f 64 65   {.  faultsim_de
11d0: 6c 65 74 65 5f 61 6e 64 5f 72 65 6f 70 65 6e 0a  lete_and_reopen.
11e0: 20 20 65 78 65 63 73 71 6c 20 7b 0a 20 20 20 20    execsql {.    
11f0: 50 52 41 47 4d 41 20 70 61 67 65 5f 73 69 7a 65  PRAGMA page_size
1200: 20 3d 20 35 31 32 3b 0a 20 20 20 20 50 52 41 47   = 512;.    PRAG
1210: 4d 41 20 6a 6f 75 72 6e 61 6c 5f 6d 6f 64 65 20  MA journal_mode 
1220: 3d 20 57 41 4c 3b 0a 20 20 7d 0a 20 20 66 61 75  = WAL;.  }.  fau
1230: 6c 74 73 69 6d 5f 73 61 76 65 5f 61 6e 64 5f 63  ltsim_save_and_c
1240: 6c 6f 73 65 0a 7d 20 7b 7d 0a 64 6f 5f 66 61 75  lose.} {}.do_fau
1250: 6c 74 73 69 6d 5f 74 65 73 74 20 77 61 6c 66 61  ltsim_test walfa
1260: 75 6c 74 2d 35 20 2d 66 61 75 6c 74 73 20 73 68  ult-5 -faults sh
1270: 6d 65 72 72 2a 20 2d 70 72 65 70 20 7b 0a 20 20  merr* -prep {.  
1280: 66 61 75 6c 74 73 69 6d 5f 72 65 73 74 6f 72 65  faultsim_restore
1290: 5f 61 6e 64 5f 72 65 6f 70 65 6e 0a 20 20 65 78  _and_reopen.  ex
12a0: 65 63 73 71 6c 20 7b 20 50 52 41 47 4d 41 20 77  ecsql { PRAGMA w
12b0: 61 6c 5f 61 75 74 6f 63 68 65 63 6b 70 6f 69 6e  al_autocheckpoin
12c0: 74 20 3d 20 30 20 7d 0a 20 20 73 68 6d 66 61 75  t = 0 }.  shmfau
12d0: 6c 74 20 66 69 6c 74 65 72 20 78 53 68 6d 4d 61  lt filter xShmMa
12e0: 70 0a 7d 20 2d 62 6f 64 79 20 7b 0a 20 20 65 78  p.} -body {.  ex
12f0: 65 63 73 71 6c 20 7b 0a 20 20 20 20 43 52 45 41  ecsql {.    CREA
1300: 54 45 20 54 41 42 4c 45 20 74 31 28 78 29 3b 0a  TE TABLE t1(x);.
1310: 20 20 20 20 42 45 47 49 4e 3b 0a 20 20 20 20 20      BEGIN;.     
1320: 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 74 31 20   INSERT INTO t1 
1330: 56 41 4c 55 45 53 28 72 61 6e 64 6f 6d 62 6c 6f  VALUES(randomblo
1340: 62 28 34 30 30 29 29 3b 20 20 20 20 20 20 20 20  b(400));        
1350: 20 20 20 2f 2a 20 31 20 2a 2f 0a 20 20 20 20 20     /* 1 */.     
1360: 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 74 31 20   INSERT INTO t1 
1370: 53 45 4c 45 43 54 20 72 61 6e 64 6f 6d 62 6c 6f  SELECT randomblo
1380: 62 28 34 30 30 29 20 46 52 4f 4d 20 74 31 3b 20  b(400) FROM t1; 
1390: 20 20 20 2f 2a 20 32 20 2a 2f 0a 20 20 20 20 20     /* 2 */.     
13a0: 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 74 31 20   INSERT INTO t1 
13b0: 53 45 4c 45 43 54 20 72 61 6e 64 6f 6d 62 6c 6f  SELECT randomblo
13c0: 62 28 34 30 30 29 20 46 52 4f 4d 20 74 31 3b 20  b(400) FROM t1; 
13d0: 20 20 20 2f 2a 20 34 20 2a 2f 0a 20 20 20 20 20     /* 4 */.     
13e0: 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 74 31 20   INSERT INTO t1 
13f0: 53 45 4c 45 43 54 20 72 61 6e 64 6f 6d 62 6c 6f  SELECT randomblo
1400: 62 28 34 30 30 29 20 46 52 4f 4d 20 74 31 3b 20  b(400) FROM t1; 
1410: 20 20 20 2f 2a 20 38 20 2a 2f 0a 20 20 20 20 20     /* 8 */.     
1420: 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 74 31 20   INSERT INTO t1 
1430: 53 45 4c 45 43 54 20 72 61 6e 64 6f 6d 62 6c 6f  SELECT randomblo
1440: 62 28 34 30 30 29 20 46 52 4f 4d 20 74 31 3b 20  b(400) FROM t1; 
1450: 20 20 20 2f 2a 20 31 36 20 2a 2f 0a 20 20 20 20     /* 16 */.    
1460: 20 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 74 31    INSERT INTO t1
1470: 20 53 45 4c 45 43 54 20 72 61 6e 64 6f 6d 62 6c   SELECT randombl
1480: 6f 62 28 34 30 30 29 20 46 52 4f 4d 20 74 31 3b  ob(400) FROM t1;
1490: 20 20 20 20 2f 2a 20 33 32 20 2a 2f 0a 20 20 20      /* 32 */.   
14a0: 20 20 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 74     INSERT INTO t
14b0: 31 20 53 45 4c 45 43 54 20 72 61 6e 64 6f 6d 62  1 SELECT randomb
14c0: 6c 6f 62 28 34 30 30 29 20 46 52 4f 4d 20 74 31  lob(400) FROM t1
14d0: 3b 20 20 20 20 2f 2a 20 36 34 20 2a 2f 0a 20 20  ;    /* 64 */.  
14e0: 20 20 20 20 49 4e 53 45 52 54 20 49 4e 54 4f 20      INSERT INTO 
14f0: 74 31 20 53 45 4c 45 43 54 20 72 61 6e 64 6f 6d  t1 SELECT random
1500: 62 6c 6f 62 28 34 30 30 29 20 46 52 4f 4d 20 74  blob(400) FROM t
1510: 31 3b 20 20 20 20 2f 2a 20 31 32 38 20 2a 2f 0a  1;    /* 128 */.
1520: 20 20 20 20 20 20 49 4e 53 45 52 54 20 49 4e 54        INSERT INT
1530: 4f 20 74 31 20 53 45 4c 45 43 54 20 72 61 6e 64  O t1 SELECT rand
1540: 6f 6d 62 6c 6f 62 28 34 30 30 29 20 46 52 4f 4d  omblob(400) FROM
1550: 20 74 31 3b 20 20 20 20 2f 2a 20 32 35 36 20 2a   t1;    /* 256 *
1560: 2f 0a 20 20 20 20 20 20 49 4e 53 45 52 54 20 49  /.      INSERT I
1570: 4e 54 4f 20 74 31 20 53 45 4c 45 43 54 20 72 61  NTO t1 SELECT ra
1580: 6e 64 6f 6d 62 6c 6f 62 28 34 30 30 29 20 46 52  ndomblob(400) FR
1590: 4f 4d 20 74 31 3b 20 20 20 20 2f 2a 20 35 31 32  OM t1;    /* 512
15a0: 20 2a 2f 0a 20 20 20 20 20 20 49 4e 53 45 52 54   */.      INSERT
15b0: 20 49 4e 54 4f 20 74 31 20 53 45 4c 45 43 54 20   INTO t1 SELECT 
15c0: 72 61 6e 64 6f 6d 62 6c 6f 62 28 34 30 30 29 20  randomblob(400) 
15d0: 46 52 4f 4d 20 74 31 3b 20 20 20 20 2f 2a 20 31  FROM t1;    /* 1
15e0: 30 32 34 20 2a 2f 0a 20 20 20 20 20 20 49 4e 53  024 */.      INS
15f0: 45 52 54 20 49 4e 54 4f 20 74 31 20 53 45 4c 45  ERT INTO t1 SELE
1600: 43 54 20 72 61 6e 64 6f 6d 62 6c 6f 62 28 34 30  CT randomblob(40
1610: 30 29 20 46 52 4f 4d 20 74 31 3b 20 20 20 20 2f  0) FROM t1;    /
1620: 2a 20 32 30 34 38 20 2a 2f 0a 20 20 20 20 20 20  * 2048 */.      
1630: 49 4e 53 45 52 54 20 49 4e 54 4f 20 74 31 20 53  INSERT INTO t1 S
1640: 45 4c 45 43 54 20 72 61 6e 64 6f 6d 62 6c 6f 62  ELECT randomblob
1650: 28 34 30 30 29 20 46 52 4f 4d 20 74 31 3b 20 20  (400) FROM t1;  
1660: 20 20 2f 2a 20 34 30 39 36 20 2a 2f 0a 20 20 20    /* 4096 */.   
1670: 20 20 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 74     INSERT INTO t
1680: 31 20 53 45 4c 45 43 54 20 72 61 6e 64 6f 6d 62  1 SELECT randomb
1690: 6c 6f 62 28 34 30 30 29 20 46 52 4f 4d 20 74 31  lob(400) FROM t1
16a0: 3b 20 20 20 20 2f 2a 20 38 31 39 32 20 2a 2f 0a  ;    /* 8192 */.
16b0: 20 20 20 20 20 20 49 4e 53 45 52 54 20 49 4e 54        INSERT INT
16c0: 4f 20 74 31 20 53 45 4c 45 43 54 20 72 61 6e 64  O t1 SELECT rand
16d0: 6f 6d 62 6c 6f 62 28 34 30 30 29 20 46 52 4f 4d  omblob(400) FROM
16e0: 20 74 31 3b 20 20 20 20 2f 2a 20 31 36 33 38 34   t1;    /* 16384
16f0: 20 2a 2f 0a 20 20 20 20 43 4f 4d 4d 49 54 3b 0a   */.    COMMIT;.
1700: 20 20 20 20 53 45 4c 45 43 54 20 63 6f 75 6e 74      SELECT count
1710: 28 2a 29 20 46 52 4f 4d 20 74 31 3b 0a 20 20 7d  (*) FROM t1;.  }
1720: 0a 7d 20 2d 74 65 73 74 20 7b 0a 20 20 66 61 75  .} -test {.  fau
1730: 6c 74 73 69 6d 5f 74 65 73 74 5f 72 65 73 75 6c  ltsim_test_resul
1740: 74 20 7b 30 20 31 36 33 38 34 7d 0a 20 20 66 61  t {0 16384}.  fa
1750: 75 6c 74 73 69 6d 5f 69 6e 74 65 67 72 69 74 79  ultsim_integrity
1760: 5f 63 68 65 63 6b 0a 7d 0a 0a 23 2d 2d 2d 2d 2d  _check.}..#-----
1770: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1780: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1790: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
17a0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
17b0: 2d 2d 2d 2d 2d 0a 23 0a 64 6f 5f 74 65 73 74 20  -----.#.do_test 
17c0: 77 61 6c 66 61 75 6c 74 2d 36 2d 70 72 65 2d 31  walfault-6-pre-1
17d0: 20 7b 0a 20 20 66 61 75 6c 74 73 69 6d 5f 64 65   {.  faultsim_de
17e0: 6c 65 74 65 5f 61 6e 64 5f 72 65 6f 70 65 6e 0a  lete_and_reopen.
17f0: 20 20 65 78 65 63 73 71 6c 20 7b 0a 20 20 20 20    execsql {.    
1800: 50 52 41 47 4d 41 20 70 61 67 65 5f 73 69 7a 65  PRAGMA page_size
1810: 20 3d 20 35 31 32 3b 0a 20 20 20 20 50 52 41 47   = 512;.    PRAG
1820: 4d 41 20 6a 6f 75 72 6e 61 6c 5f 6d 6f 64 65 20  MA journal_mode 
1830: 3d 20 57 41 4c 3b 0a 20 20 20 20 50 52 41 47 4d  = WAL;.    PRAGM
1840: 41 20 77 61 6c 5f 61 75 74 6f 63 68 65 63 6b 70  A wal_autocheckp
1850: 6f 69 6e 74 20 3d 20 30 3b 0a 20 20 20 20 43 52  oint = 0;.    CR
1860: 45 41 54 45 20 54 41 42 4c 45 20 74 31 28 78 29  EATE TABLE t1(x)
1870: 3b 0a 20 20 20 20 42 45 47 49 4e 3b 0a 20 20 20  ;.    BEGIN;.   
1880: 20 20 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 74     INSERT INTO t
1890: 31 20 56 41 4c 55 45 53 28 72 61 6e 64 6f 6d 62  1 VALUES(randomb
18a0: 6c 6f 62 28 34 30 30 29 29 3b 20 20 20 20 20 20  lob(400));      
18b0: 20 20 20 20 20 2f 2a 20 31 20 2a 2f 0a 20 20 20       /* 1 */.   
18c0: 20 20 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 74     INSERT INTO t
18d0: 31 20 53 45 4c 45 43 54 20 72 61 6e 64 6f 6d 62  1 SELECT randomb
18e0: 6c 6f 62 28 34 30 30 29 20 46 52 4f 4d 20 74 31  lob(400) FROM t1
18f0: 3b 20 20 20 20 2f 2a 20 32 20 2a 2f 0a 20 20 20  ;    /* 2 */.   
1900: 20 20 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 74     INSERT INTO t
1910: 31 20 53 45 4c 45 43 54 20 72 61 6e 64 6f 6d 62  1 SELECT randomb
1920: 6c 6f 62 28 34 30 30 29 20 46 52 4f 4d 20 74 31  lob(400) FROM t1
1930: 3b 20 20 20 20 2f 2a 20 34 20 2a 2f 0a 20 20 20  ;    /* 4 */.   
1940: 20 20 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 74     INSERT INTO t
1950: 31 20 53 45 4c 45 43 54 20 72 61 6e 64 6f 6d 62  1 SELECT randomb
1960: 6c 6f 62 28 34 30 30 29 20 46 52 4f 4d 20 74 31  lob(400) FROM t1
1970: 3b 20 20 20 20 2f 2a 20 38 20 2a 2f 0a 20 20 20  ;    /* 8 */.   
1980: 20 20 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 74     INSERT INTO t
1990: 31 20 53 45 4c 45 43 54 20 72 61 6e 64 6f 6d 62  1 SELECT randomb
19a0: 6c 6f 62 28 34 30 30 29 20 46 52 4f 4d 20 74 31  lob(400) FROM t1
19b0: 3b 20 20 20 20 2f 2a 20 31 36 20 2a 2f 0a 20 20  ;    /* 16 */.  
19c0: 20 20 20 20 49 4e 53 45 52 54 20 49 4e 54 4f 20      INSERT INTO 
19d0: 74 31 20 53 45 4c 45 43 54 20 72 61 6e 64 6f 6d  t1 SELECT random
19e0: 62 6c 6f 62 28 34 30 30 29 20 46 52 4f 4d 20 74  blob(400) FROM t
19f0: 31 3b 20 20 20 20 2f 2a 20 33 32 20 2a 2f 0a 20  1;    /* 32 */. 
1a00: 20 20 20 20 20 49 4e 53 45 52 54 20 49 4e 54 4f       INSERT INTO
1a10: 20 74 31 20 53 45 4c 45 43 54 20 72 61 6e 64 6f   t1 SELECT rando
1a20: 6d 62 6c 6f 62 28 34 30 30 29 20 46 52 4f 4d 20  mblob(400) FROM 
1a30: 74 31 3b 20 20 20 20 2f 2a 20 36 34 20 2a 2f 0a  t1;    /* 64 */.
1a40: 20 20 20 20 20 20 49 4e 53 45 52 54 20 49 4e 54        INSERT INT
1a50: 4f 20 74 31 20 53 45 4c 45 43 54 20 72 61 6e 64  O t1 SELECT rand
1a60: 6f 6d 62 6c 6f 62 28 34 30 30 29 20 46 52 4f 4d  omblob(400) FROM
1a70: 20 74 31 3b 20 20 20 20 2f 2a 20 31 32 38 20 2a   t1;    /* 128 *
1a80: 2f 0a 20 20 20 20 20 20 49 4e 53 45 52 54 20 49  /.      INSERT I
1a90: 4e 54 4f 20 74 31 20 53 45 4c 45 43 54 20 72 61  NTO t1 SELECT ra
1aa0: 6e 64 6f 6d 62 6c 6f 62 28 34 30 30 29 20 46 52  ndomblob(400) FR
1ab0: 4f 4d 20 74 31 3b 20 20 20 20 2f 2a 20 32 35 36  OM t1;    /* 256
1ac0: 20 2a 2f 0a 20 20 20 20 20 20 49 4e 53 45 52 54   */.      INSERT
1ad0: 20 49 4e 54 4f 20 74 31 20 53 45 4c 45 43 54 20   INTO t1 SELECT 
1ae0: 72 61 6e 64 6f 6d 62 6c 6f 62 28 34 30 30 29 20  randomblob(400) 
1af0: 46 52 4f 4d 20 74 31 3b 20 20 20 20 2f 2a 20 35  FROM t1;    /* 5
1b00: 31 32 20 2a 2f 0a 20 20 20 20 20 20 49 4e 53 45  12 */.      INSE
1b10: 52 54 20 49 4e 54 4f 20 74 31 20 53 45 4c 45 43  RT INTO t1 SELEC
1b20: 54 20 72 61 6e 64 6f 6d 62 6c 6f 62 28 34 30 30  T randomblob(400
1b30: 29 20 46 52 4f 4d 20 74 31 3b 20 20 20 20 2f 2a  ) FROM t1;    /*
1b40: 20 31 30 32 34 20 2a 2f 0a 20 20 20 20 20 20 49   1024 */.      I
1b50: 4e 53 45 52 54 20 49 4e 54 4f 20 74 31 20 53 45  NSERT INTO t1 SE
1b60: 4c 45 43 54 20 72 61 6e 64 6f 6d 62 6c 6f 62 28  LECT randomblob(
1b70: 34 30 30 29 20 46 52 4f 4d 20 74 31 3b 20 20 20  400) FROM t1;   
1b80: 20 2f 2a 20 32 30 34 38 20 2a 2f 0a 20 20 20 20   /* 2048 */.    
1b90: 20 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 74 31    INSERT INTO t1
1ba0: 20 53 45 4c 45 43 54 20 72 61 6e 64 6f 6d 62 6c   SELECT randombl
1bb0: 6f 62 28 34 30 30 29 20 46 52 4f 4d 20 74 31 3b  ob(400) FROM t1;
1bc0: 20 20 20 20 2f 2a 20 34 30 39 36 20 2a 2f 0a 20      /* 4096 */. 
1bd0: 20 20 20 20 20 49 4e 53 45 52 54 20 49 4e 54 4f       INSERT INTO
1be0: 20 74 31 20 53 45 4c 45 43 54 20 72 61 6e 64 6f   t1 SELECT rando
1bf0: 6d 62 6c 6f 62 28 34 30 30 29 20 46 52 4f 4d 20  mblob(400) FROM 
1c00: 74 31 3b 20 20 20 20 2f 2a 20 38 31 39 32 20 2a  t1;    /* 8192 *
1c10: 2f 0a 20 20 20 20 20 20 49 4e 53 45 52 54 20 49  /.      INSERT I
1c20: 4e 54 4f 20 74 31 20 53 45 4c 45 43 54 20 72 61  NTO t1 SELECT ra
1c30: 6e 64 6f 6d 62 6c 6f 62 28 34 30 30 29 20 46 52  ndomblob(400) FR
1c40: 4f 4d 20 74 31 3b 20 20 20 20 2f 2a 20 31 36 33  OM t1;    /* 163
1c50: 38 34 20 2a 2f 0a 20 20 20 20 43 4f 4d 4d 49 54  84 */.    COMMIT
1c60: 3b 0a 20 20 7d 0a 20 20 66 61 75 6c 74 73 69 6d  ;.  }.  faultsim
1c70: 5f 73 61 76 65 5f 61 6e 64 5f 63 6c 6f 73 65 0a  _save_and_close.
1c80: 7d 20 7b 7d 0a 64 6f 5f 66 61 75 6c 74 73 69 6d  } {}.do_faultsim
1c90: 5f 74 65 73 74 20 77 61 6c 66 61 75 6c 74 2d 36  _test walfault-6
1ca0: 20 2d 66 61 75 6c 74 73 20 73 68 6d 65 72 72 2a   -faults shmerr*
1cb0: 20 2d 70 72 65 70 20 7b 0a 20 20 66 61 75 6c 74   -prep {.  fault
1cc0: 73 69 6d 5f 72 65 73 74 6f 72 65 5f 61 6e 64 5f  sim_restore_and_
1cd0: 72 65 6f 70 65 6e 0a 20 20 73 68 6d 66 61 75 6c  reopen.  shmfaul
1ce0: 74 20 66 69 6c 74 65 72 20 78 53 68 6d 4d 61 70  t filter xShmMap
1cf0: 0a 7d 20 2d 62 6f 64 79 20 7b 0a 20 20 65 78 65  .} -body {.  exe
1d00: 63 73 71 6c 20 7b 20 53 45 4c 45 43 54 20 63 6f  csql { SELECT co
1d10: 75 6e 74 28 2a 29 20 46 52 4f 4d 20 74 31 20 7d  unt(*) FROM t1 }
1d20: 0a 7d 20 2d 74 65 73 74 20 7b 0a 20 20 66 61 75  .} -test {.  fau
1d30: 6c 74 73 69 6d 5f 74 65 73 74 5f 72 65 73 75 6c  ltsim_test_resul
1d40: 74 20 7b 30 20 31 36 33 38 34 7d 0a 20 20 66 61  t {0 16384}.  fa
1d50: 75 6c 74 73 69 6d 5f 69 6e 74 65 67 72 69 74 79  ultsim_integrity
1d60: 5f 63 68 65 63 6b 0a 20 20 73 65 74 20 6e 20 5b  _check.  set n [
1d70: 64 62 20 6f 6e 65 20 7b 53 45 4c 45 43 54 20 63  db one {SELECT c
1d80: 6f 75 6e 74 28 2a 29 20 46 52 4f 4d 20 74 31 7d  ount(*) FROM t1}
1d90: 5d 0a 20 20 69 66 20 7b 24 6e 20 21 3d 20 31 36  ].  if {$n != 16
1da0: 33 38 34 20 26 26 20 24 6e 20 21 3d 20 30 7d 20  384 && $n != 0} 
1db0: 7b 20 65 72 72 6f 72 20 22 49 6e 63 6f 72 72 65  { error "Incorre
1dc0: 63 74 20 6e 75 6d 62 65 72 20 6f 66 20 72 6f 77  ct number of row
1dd0: 73 3a 20 24 6e 22 20 7d 0a 7d 0a 0a 23 2d 2d 2d  s: $n" }.}..#---
1de0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1df0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1e00: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1e10: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1e20: 2d 2d 2d 2d 2d 2d 2d 0a 23 0a 64 6f 5f 74 65 73  -------.#.do_tes
1e30: 74 20 77 61 6c 66 61 75 6c 74 2d 37 2d 70 72 65  t walfault-7-pre
1e40: 2d 31 20 7b 0a 20 20 66 61 75 6c 74 73 69 6d 5f  -1 {.  faultsim_
1e50: 64 65 6c 65 74 65 5f 61 6e 64 5f 72 65 6f 70 65  delete_and_reope
1e60: 6e 0a 20 20 65 78 65 63 73 71 6c 20 7b 0a 20 20  n.  execsql {.  
1e70: 20 20 50 52 41 47 4d 41 20 70 61 67 65 5f 73 69    PRAGMA page_si
1e80: 7a 65 20 3d 20 35 31 32 3b 0a 20 20 20 20 50 52  ze = 512;.    PR
1e90: 41 47 4d 41 20 6a 6f 75 72 6e 61 6c 5f 6d 6f 64  AGMA journal_mod
1ea0: 65 20 3d 20 57 41 4c 3b 0a 20 20 20 20 50 52 41  e = WAL;.    PRA
1eb0: 47 4d 41 20 77 61 6c 5f 61 75 74 6f 63 68 65 63  GMA wal_autochec
1ec0: 6b 70 6f 69 6e 74 20 3d 20 30 3b 0a 20 20 20 20  kpoint = 0;.    
1ed0: 43 52 45 41 54 45 20 54 41 42 4c 45 20 74 31 28  CREATE TABLE t1(
1ee0: 78 29 3b 0a 20 20 20 20 42 45 47 49 4e 3b 0a 20  x);.    BEGIN;. 
1ef0: 20 20 20 20 20 49 4e 53 45 52 54 20 49 4e 54 4f       INSERT INTO
1f00: 20 74 31 20 56 41 4c 55 45 53 28 72 61 6e 64 6f   t1 VALUES(rando
1f10: 6d 62 6c 6f 62 28 34 30 30 29 29 3b 20 20 20 20  mblob(400));    
1f20: 20 20 20 20 20 20 20 2f 2a 20 31 20 2a 2f 0a 20         /* 1 */. 
1f30: 20 20 20 20 20 49 4e 53 45 52 54 20 49 4e 54 4f       INSERT INTO
1f40: 20 74 31 20 53 45 4c 45 43 54 20 72 61 6e 64 6f   t1 SELECT rando
1f50: 6d 62 6c 6f 62 28 34 30 30 29 20 46 52 4f 4d 20  mblob(400) FROM 
1f60: 74 31 3b 20 20 20 20 2f 2a 20 32 20 2a 2f 0a 20  t1;    /* 2 */. 
1f70: 20 20 20 20 20 49 4e 53 45 52 54 20 49 4e 54 4f       INSERT INTO
1f80: 20 74 31 20 53 45 4c 45 43 54 20 72 61 6e 64 6f   t1 SELECT rando
1f90: 6d 62 6c 6f 62 28 34 30 30 29 20 46 52 4f 4d 20  mblob(400) FROM 
1fa0: 74 31 3b 20 20 20 20 2f 2a 20 34 20 2a 2f 0a 20  t1;    /* 4 */. 
1fb0: 20 20 20 43 4f 4d 4d 49 54 3b 0a 20 20 7d 0a 20     COMMIT;.  }. 
1fc0: 20 66 61 75 6c 74 73 69 6d 5f 73 61 76 65 5f 61   faultsim_save_a
1fd0: 6e 64 5f 63 6c 6f 73 65 0a 7d 20 7b 7d 0a 64 6f  nd_close.} {}.do
1fe0: 5f 66 61 75 6c 74 73 69 6d 5f 74 65 73 74 20 77  _faultsim_test w
1ff0: 61 6c 66 61 75 6c 74 2d 37 20 2d 70 72 65 70 20  alfault-7 -prep 
2000: 7b 0a 20 20 66 61 75 6c 74 73 69 6d 5f 72 65 73  {.  faultsim_res
2010: 74 6f 72 65 5f 61 6e 64 5f 72 65 6f 70 65 6e 0a  tore_and_reopen.
2020: 7d 20 2d 62 6f 64 79 20 7b 0a 20 20 65 78 65 63  } -body {.  exec
2030: 73 71 6c 20 7b 20 53 45 4c 45 43 54 20 63 6f 75  sql { SELECT cou
2040: 6e 74 28 2a 29 20 46 52 4f 4d 20 74 31 20 7d 0a  nt(*) FROM t1 }.
2050: 7d 20 2d 74 65 73 74 20 7b 0a 20 20 66 61 75 6c  } -test {.  faul
2060: 74 73 69 6d 5f 74 65 73 74 5f 72 65 73 75 6c 74  tsim_test_result
2070: 20 7b 30 20 34 7d 0a 20 20 73 65 74 20 6e 20 5b   {0 4}.  set n [
2080: 64 62 20 6f 6e 65 20 7b 53 45 4c 45 43 54 20 63  db one {SELECT c
2090: 6f 75 6e 74 28 2a 29 20 46 52 4f 4d 20 74 31 7d  ount(*) FROM t1}
20a0: 5d 0a 20 20 69 66 20 7b 24 6e 20 21 3d 20 34 20  ].  if {$n != 4 
20b0: 26 26 20 24 6e 20 21 3d 20 30 7d 20 7b 20 65 72  && $n != 0} { er
20c0: 72 6f 72 20 22 49 6e 63 6f 72 72 65 63 74 20 6e  ror "Incorrect n
20d0: 75 6d 62 65 72 20 6f 66 20 72 6f 77 73 3a 20 24  umber of rows: $
20e0: 6e 22 20 7d 0a 7d 0a 0a 23 2d 2d 2d 2d 2d 2d 2d  n" }.}..#-------
20f0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
2100: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
2110: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
2120: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
2130: 2d 2d 2d 0a 23 0a 64 6f 5f 74 65 73 74 20 77 61  ---.#.do_test wa
2140: 6c 66 61 75 6c 74 2d 38 2d 70 72 65 2d 31 20 7b  lfault-8-pre-1 {
2150: 0a 20 20 66 61 75 6c 74 73 69 6d 5f 64 65 6c 65  .  faultsim_dele
2160: 74 65 5f 61 6e 64 5f 72 65 6f 70 65 6e 0a 20 20  te_and_reopen.  
2170: 65 78 65 63 73 71 6c 20 7b 0a 20 20 20 20 50 52  execsql {.    PR
2180: 41 47 4d 41 20 6a 6f 75 72 6e 61 6c 5f 6d 6f 64  AGMA journal_mod
2190: 65 20 3d 20 57 41 4c 3b 0a 20 20 20 20 43 52 45  e = WAL;.    CRE
21a0: 41 54 45 20 54 41 42 4c 45 20 61 62 63 28 61 20  ATE TABLE abc(a 
21b0: 50 52 49 4d 41 52 59 20 4b 45 59 29 3b 0a 20 20  PRIMARY KEY);.  
21c0: 20 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 61 62    INSERT INTO ab
21d0: 63 20 56 41 4c 55 45 53 28 72 61 6e 64 6f 6d 62  c VALUES(randomb
21e0: 6c 6f 62 28 39 30 30 29 29 3b 0a 20 20 7d 0a 20  lob(900));.  }. 
21f0: 20 66 61 75 6c 74 73 69 6d 5f 73 61 76 65 5f 61   faultsim_save_a
2200: 6e 64 5f 63 6c 6f 73 65 0a 7d 20 7b 7d 0a 64 6f  nd_close.} {}.do
2210: 5f 66 61 75 6c 74 73 69 6d 5f 74 65 73 74 20 77  _faultsim_test w
2220: 61 6c 66 61 75 6c 74 2d 38 20 2d 70 72 65 70 20  alfault-8 -prep 
2230: 7b 0a 20 20 66 61 75 6c 74 73 69 6d 5f 72 65 73  {.  faultsim_res
2240: 74 6f 72 65 5f 61 6e 64 5f 72 65 6f 70 65 6e 0a  tore_and_reopen.
2250: 20 20 65 78 65 63 73 71 6c 20 7b 20 50 52 41 47    execsql { PRAG
2260: 4d 41 20 63 61 63 68 65 5f 73 69 7a 65 20 3d 20  MA cache_size = 
2270: 31 30 20 7d 0a 7d 20 2d 62 6f 64 79 20 7b 0a 20  10 }.} -body {. 
2280: 20 65 78 65 63 73 71 6c 20 7b 0a 20 20 20 20 42   execsql {.    B
2290: 45 47 49 4e 3b 0a 20 20 20 20 20 20 49 4e 53 45  EGIN;.      INSE
22a0: 52 54 20 49 4e 54 4f 20 61 62 63 20 53 45 4c 45  RT INTO abc SELE
22b0: 43 54 20 72 61 6e 64 6f 6d 62 6c 6f 62 28 39 30  CT randomblob(90
22c0: 30 29 20 46 52 4f 4d 20 61 62 63 3b 20 20 20 20  0) FROM abc;    
22d0: 2f 2a 20 31 20 2a 2f 0a 20 20 20 20 20 20 2d 2d  /* 1 */.      --
22e0: 49 4e 53 45 52 54 20 49 4e 54 4f 20 61 62 63 20  INSERT INTO abc 
22f0: 53 45 4c 45 43 54 20 72 61 6e 64 6f 6d 62 6c 6f  SELECT randomblo
2300: 62 28 39 30 30 29 20 46 52 4f 4d 20 61 62 63 3b  b(900) FROM abc;
2310: 20 20 20 20 2f 2a 20 32 20 2a 2f 0a 20 20 20 20      /* 2 */.    
2320: 20 20 2d 2d 49 4e 53 45 52 54 20 49 4e 54 4f 20    --INSERT INTO 
2330: 61 62 63 20 53 45 4c 45 43 54 20 72 61 6e 64 6f  abc SELECT rando
2340: 6d 62 6c 6f 62 28 39 30 30 29 20 46 52 4f 4d 20  mblob(900) FROM 
2350: 61 62 63 3b 20 20 20 20 2f 2a 20 34 20 2a 2f 0a  abc;    /* 4 */.
2360: 20 20 20 20 20 20 2d 2d 49 4e 53 45 52 54 20 49        --INSERT I
2370: 4e 54 4f 20 61 62 63 20 53 45 4c 45 43 54 20 72  NTO abc SELECT r
2380: 61 6e 64 6f 6d 62 6c 6f 62 28 39 30 30 29 20 46  andomblob(900) F
2390: 52 4f 4d 20 61 62 63 3b 20 20 20 20 2f 2a 20 38  ROM abc;    /* 8
23a0: 20 2a 2f 0a 20 20 20 20 52 4f 4c 4c 42 41 43 4b   */.    ROLLBACK
23b0: 3b 0a 20 20 20 20 53 45 4c 45 43 54 20 63 6f 75  ;.    SELECT cou
23c0: 6e 74 28 2a 29 20 46 52 4f 4d 20 61 62 63 3b 0a  nt(*) FROM abc;.
23d0: 20 20 7d 0a 7d 20 2d 74 65 73 74 20 7b 0a 20 20    }.} -test {.  
23e0: 66 61 75 6c 74 73 69 6d 5f 74 65 73 74 5f 72 65  faultsim_test_re
23f0: 73 75 6c 74 20 7b 30 20 31 7d 0a 0a 20 20 66 61  sult {0 1}..  fa
2400: 75 6c 74 73 69 6d 5f 69 6e 74 65 67 72 69 74 79  ultsim_integrity
2410: 5f 63 68 65 63 6b 0a 20 20 63 61 74 63 68 20 7b  _check.  catch {
2420: 20 64 62 20 65 76 61 6c 20 52 4f 4c 4c 42 41 43   db eval ROLLBAC
2430: 4b 20 7d 0a 20 20 66 61 75 6c 74 73 69 6d 5f 69  K }.  faultsim_i
2440: 6e 74 65 67 72 69 74 79 5f 63 68 65 63 6b 0a 0a  ntegrity_check..
2450: 20 20 73 65 74 20 6e 20 5b 64 62 20 6f 6e 65 20    set n [db one 
2460: 7b 53 45 4c 45 43 54 20 63 6f 75 6e 74 28 2a 29  {SELECT count(*)
2470: 20 46 52 4f 4d 20 61 62 63 7d 5d 0a 20 20 69 66   FROM abc}].  if
2480: 20 7b 24 6e 20 21 3d 20 31 7d 20 7b 20 65 72 72   {$n != 1} { err
2490: 6f 72 20 22 49 6e 63 6f 72 72 65 63 74 20 6e 75  or "Incorrect nu
24a0: 6d 62 65 72 20 6f 66 20 72 6f 77 73 3a 20 24 6e  mber of rows: $n
24b0: 22 20 7d 0a 7d 0a 0a 23 2d 2d 2d 2d 2d 2d 2d 2d  " }.}..#--------
24c0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
24d0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
24e0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
24f0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
2500: 2d 2d 0a 23 0a 64 6f 5f 74 65 73 74 20 77 61 6c  --.#.do_test wal
2510: 66 61 75 6c 74 2d 39 2d 70 72 65 2d 31 20 7b 0a  fault-9-pre-1 {.
2520: 20 20 66 61 75 6c 74 73 69 6d 5f 64 65 6c 65 74    faultsim_delet
2530: 65 5f 61 6e 64 5f 72 65 6f 70 65 6e 0a 20 20 65  e_and_reopen.  e
2540: 78 65 63 73 71 6c 20 7b 0a 20 20 20 20 50 52 41  xecsql {.    PRA
2550: 47 4d 41 20 6a 6f 75 72 6e 61 6c 5f 6d 6f 64 65  GMA journal_mode
2560: 20 3d 20 57 41 4c 3b 0a 20 20 20 20 43 52 45 41   = WAL;.    CREA
2570: 54 45 20 54 41 42 4c 45 20 61 62 63 28 61 20 50  TE TABLE abc(a P
2580: 52 49 4d 41 52 59 20 4b 45 59 29 3b 0a 20 20 20  RIMARY KEY);.   
2590: 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 61 62 63   INSERT INTO abc
25a0: 20 56 41 4c 55 45 53 28 72 61 6e 64 6f 6d 62 6c   VALUES(randombl
25b0: 6f 62 28 39 30 30 29 29 3b 0a 20 20 7d 0a 20 20  ob(900));.  }.  
25c0: 66 61 75 6c 74 73 69 6d 5f 73 61 76 65 5f 61 6e  faultsim_save_an
25d0: 64 5f 63 6c 6f 73 65 0a 7d 20 7b 7d 0a 64 6f 5f  d_close.} {}.do_
25e0: 66 61 75 6c 74 73 69 6d 5f 74 65 73 74 20 77 61  faultsim_test wa
25f0: 6c 66 61 75 6c 74 2d 39 20 2d 70 72 65 70 20 7b  lfault-9 -prep {
2600: 0a 20 20 23 69 66 20 7b 24 69 46 61 69 6c 3c 37  .  #if {$iFail<7
2610: 33 7d 20 7b 20 73 65 74 20 69 46 61 69 6c 20 37  3} { set iFail 7
2620: 33 20 7d 0a 20 20 23 69 66 20 7b 24 69 46 61 69  3 }.  #if {$iFai
2630: 6c 3e 37 33 7d 20 7b 20 65 78 69 74 20 7d 0a 20  l>73} { exit }. 
2640: 20 0a 20 20 66 61 75 6c 74 73 69 6d 5f 72 65 73   .  faultsim_res
2650: 74 6f 72 65 5f 61 6e 64 5f 72 65 6f 70 65 6e 0a  tore_and_reopen.
2660: 20 20 65 78 65 63 73 71 6c 20 7b 20 50 52 41 47    execsql { PRAG
2670: 4d 41 20 63 61 63 68 65 5f 73 69 7a 65 20 3d 20  MA cache_size = 
2680: 31 30 20 7d 0a 7d 20 2d 62 6f 64 79 20 7b 0a 20  10 }.} -body {. 
2690: 20 65 78 65 63 73 71 6c 20 7b 0a 20 20 20 20 42   execsql {.    B
26a0: 45 47 49 4e 3b 0a 20 20 20 20 20 20 49 4e 53 45  EGIN;.      INSE
26b0: 52 54 20 49 4e 54 4f 20 61 62 63 20 53 45 4c 45  RT INTO abc SELE
26c0: 43 54 20 72 61 6e 64 6f 6d 62 6c 6f 62 28 39 30  CT randomblob(90
26d0: 30 29 20 46 52 4f 4d 20 61 62 63 3b 20 20 20 20  0) FROM abc;    
26e0: 2f 2a 20 31 20 2a 2f 0a 20 20 20 20 20 20 53 41  /* 1 */.      SA
26f0: 56 45 50 4f 49 4e 54 20 73 70 6f 69 6e 74 3b 0a  VEPOINT spoint;.
2700: 20 20 20 20 20 20 20 20 49 4e 53 45 52 54 20 49          INSERT I
2710: 4e 54 4f 20 61 62 63 20 53 45 4c 45 43 54 20 72  NTO abc SELECT r
2720: 61 6e 64 6f 6d 62 6c 6f 62 28 39 30 30 29 20 46  andomblob(900) F
2730: 52 4f 4d 20 61 62 63 3b 20 20 20 20 2f 2a 20 32  ROM abc;    /* 2
2740: 20 2a 2f 0a 20 20 20 20 20 20 20 20 49 4e 53 45   */.        INSE
2750: 52 54 20 49 4e 54 4f 20 61 62 63 20 53 45 4c 45  RT INTO abc SELE
2760: 43 54 20 72 61 6e 64 6f 6d 62 6c 6f 62 28 39 30  CT randomblob(90
2770: 30 29 20 46 52 4f 4d 20 61 62 63 3b 20 20 20 20  0) FROM abc;    
2780: 2f 2a 20 34 20 2a 2f 0a 20 20 20 20 20 20 20 20  /* 4 */.        
2790: 49 4e 53 45 52 54 20 49 4e 54 4f 20 61 62 63 20  INSERT INTO abc 
27a0: 53 45 4c 45 43 54 20 72 61 6e 64 6f 6d 62 6c 6f  SELECT randomblo
27b0: 62 28 39 30 30 29 20 46 52 4f 4d 20 61 62 63 3b  b(900) FROM abc;
27c0: 20 20 20 20 2f 2a 20 38 20 2a 2f 0a 20 20 20 20      /* 8 */.    
27d0: 20 20 52 4f 4c 4c 42 41 43 4b 20 54 4f 20 73 70    ROLLBACK TO sp
27e0: 6f 69 6e 74 3b 0a 20 20 20 20 43 4f 4d 4d 49 54  oint;.    COMMIT
27f0: 3b 0a 20 20 20 20 53 45 4c 45 43 54 20 63 6f 75  ;.    SELECT cou
2800: 6e 74 28 2a 29 20 46 52 4f 4d 20 61 62 63 3b 0a  nt(*) FROM abc;.
2810: 20 20 7d 0a 7d 20 2d 74 65 73 74 20 7b 0a 20 20    }.} -test {.  
2820: 66 61 75 6c 74 73 69 6d 5f 74 65 73 74 5f 72 65  faultsim_test_re
2830: 73 75 6c 74 20 7b 30 20 32 7d 0a 20 20 66 61 75  sult {0 2}.  fau
2840: 6c 74 73 69 6d 5f 69 6e 74 65 67 72 69 74 79 5f  ltsim_integrity_
2850: 63 68 65 63 6b 0a 0a 20 20 63 61 74 63 68 20 7b  check..  catch {
2860: 20 64 62 20 65 76 61 6c 20 7b 20 52 4f 4c 4c 42   db eval { ROLLB
2870: 41 43 4b 20 54 4f 20 73 70 6f 69 6e 74 20 7d 20  ACK TO spoint } 
2880: 7d 0a 20 20 63 61 74 63 68 20 7b 20 64 62 20 65  }.  catch { db e
2890: 76 61 6c 20 7b 20 43 4f 4d 4d 49 54 20 7d 20 7d  val { COMMIT } }
28a0: 0a 20 20 73 65 74 20 6e 20 5b 64 62 20 6f 6e 65  .  set n [db one
28b0: 20 7b 53 45 4c 45 43 54 20 63 6f 75 6e 74 28 2a   {SELECT count(*
28c0: 29 20 46 52 4f 4d 20 61 62 63 7d 5d 0a 20 20 69  ) FROM abc}].  i
28d0: 66 20 7b 24 6e 20 21 3d 20 31 20 26 26 20 24 6e  f {$n != 1 && $n
28e0: 20 21 3d 20 32 7d 20 7b 20 65 72 72 6f 72 20 22   != 2} { error "
28f0: 49 6e 63 6f 72 72 65 63 74 20 6e 75 6d 62 65 72  Incorrect number
2900: 20 6f 66 20 72 6f 77 73 3a 20 24 6e 22 20 7d 0a   of rows: $n" }.
2910: 7d 0a 0a 64 6f 5f 74 65 73 74 20 77 61 6c 66 61  }..do_test walfa
2920: 75 6c 74 2d 31 30 2d 70 72 65 31 20 7b 0a 20 20  ult-10-pre1 {.  
2930: 66 61 75 6c 74 73 69 6d 5f 64 65 6c 65 74 65 5f  faultsim_delete_
2940: 61 6e 64 5f 72 65 6f 70 65 6e 0a 20 20 65 78 65  and_reopen.  exe
2950: 63 73 71 6c 20 7b 0a 20 20 20 20 50 52 41 47 4d  csql {.    PRAGM
2960: 41 20 6a 6f 75 72 6e 61 6c 5f 6d 6f 64 65 20 3d  A journal_mode =
2970: 20 57 41 4c 3b 0a 20 20 20 20 50 52 41 47 4d 41   WAL;.    PRAGMA
2980: 20 77 61 6c 5f 61 75 74 6f 63 68 65 63 6b 70 6f   wal_autocheckpo
2990: 69 6e 74 20 3d 20 30 3b 0a 20 20 20 20 43 52 45  int = 0;.    CRE
29a0: 41 54 45 20 54 41 42 4c 45 20 7a 28 7a 7a 20 49  ATE TABLE z(zz I
29b0: 4e 54 45 47 45 52 20 50 52 49 4d 41 52 59 20 4b  NTEGER PRIMARY K
29c0: 45 59 2c 20 7a 7a 7a 20 42 4c 4f 42 29 3b 0a 20  EY, zzz BLOB);. 
29d0: 20 20 20 43 52 45 41 54 45 20 49 4e 44 45 58 20     CREATE INDEX 
29e0: 7a 7a 7a 7a 20 4f 4e 20 7a 28 7a 7a 7a 29 3b 0a  zzzz ON z(zzz);.
29f0: 20 20 20 20 49 4e 53 45 52 54 20 49 4e 54 4f 20      INSERT INTO 
2a00: 7a 20 56 41 4c 55 45 53 28 4e 55 4c 4c 2c 20 72  z VALUES(NULL, r
2a10: 61 6e 64 6f 6d 62 6c 6f 62 28 38 30 30 29 29 3b  andomblob(800));
2a20: 0a 20 20 20 20 49 4e 53 45 52 54 20 49 4e 54 4f  .    INSERT INTO
2a30: 20 7a 20 56 41 4c 55 45 53 28 4e 55 4c 4c 2c 20   z VALUES(NULL, 
2a40: 72 61 6e 64 6f 6d 62 6c 6f 62 28 38 30 30 29 29  randomblob(800))
2a50: 3b 0a 20 20 20 20 49 4e 53 45 52 54 20 49 4e 54  ;.    INSERT INT
2a60: 4f 20 7a 20 53 45 4c 45 43 54 20 4e 55 4c 4c 2c  O z SELECT NULL,
2a70: 20 72 61 6e 64 6f 6d 62 6c 6f 62 28 38 30 30 29   randomblob(800)
2a80: 20 46 52 4f 4d 20 7a 3b 0a 20 20 20 20 49 4e 53   FROM z;.    INS
2a90: 45 52 54 20 49 4e 54 4f 20 7a 20 53 45 4c 45 43  ERT INTO z SELEC
2aa0: 54 20 4e 55 4c 4c 2c 20 72 61 6e 64 6f 6d 62 6c  T NULL, randombl
2ab0: 6f 62 28 38 30 30 29 20 46 52 4f 4d 20 7a 3b 0a  ob(800) FROM z;.
2ac0: 20 20 20 20 49 4e 53 45 52 54 20 49 4e 54 4f 20      INSERT INTO 
2ad0: 7a 20 53 45 4c 45 43 54 20 4e 55 4c 4c 2c 20 72  z SELECT NULL, r
2ae0: 61 6e 64 6f 6d 62 6c 6f 62 28 38 30 30 29 20 46  andomblob(800) F
2af0: 52 4f 4d 20 7a 3b 0a 20 20 20 20 49 4e 53 45 52  ROM z;.    INSER
2b00: 54 20 49 4e 54 4f 20 7a 20 53 45 4c 45 43 54 20  T INTO z SELECT 
2b10: 4e 55 4c 4c 2c 20 72 61 6e 64 6f 6d 62 6c 6f 62  NULL, randomblob
2b20: 28 38 30 30 29 20 46 52 4f 4d 20 7a 3b 0a 20 20  (800) FROM z;.  
2b30: 20 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 7a 20    INSERT INTO z 
2b40: 53 45 4c 45 43 54 20 4e 55 4c 4c 2c 20 72 61 6e  SELECT NULL, ran
2b50: 64 6f 6d 62 6c 6f 62 28 38 30 30 29 20 46 52 4f  domblob(800) FRO
2b60: 4d 20 7a 3b 0a 20 20 7d 0a 20 20 66 61 75 6c 74  M z;.  }.  fault
2b70: 73 69 6d 5f 73 61 76 65 5f 61 6e 64 5f 63 6c 6f  sim_save_and_clo
2b80: 73 65 0a 7d 20 7b 7d 0a 64 6f 5f 66 61 75 6c 74  se.} {}.do_fault
2b90: 73 69 6d 5f 74 65 73 74 20 77 61 6c 66 61 75 6c  sim_test walfaul
2ba0: 74 2d 31 30 20 2d 70 72 65 70 20 7b 0a 20 20 66  t-10 -prep {.  f
2bb0: 61 75 6c 74 73 69 6d 5f 72 65 73 74 6f 72 65 5f  aultsim_restore_
2bc0: 61 6e 64 5f 72 65 6f 70 65 6e 0a 20 20 65 78 65  and_reopen.  exe
2bd0: 63 73 71 6c 20 7b 0a 20 20 20 20 50 52 41 47 4d  csql {.    PRAGM
2be0: 41 20 63 61 63 68 65 5f 73 69 7a 65 20 3d 20 31  A cache_size = 1
2bf0: 30 3b 0a 20 20 20 20 42 45 47 49 4e 3b 0a 20 20  0;.    BEGIN;.  
2c00: 20 20 20 20 55 50 44 41 54 45 20 7a 20 53 45 54      UPDATE z SET
2c10: 20 7a 7a 7a 20 3d 20 72 61 6e 64 6f 6d 62 6c 6f   zzz = randomblo
2c20: 62 28 37 39 39 29 3b 0a 20 20 7d 0a 0a 20 20 73  b(799);.  }..  s
2c30: 65 74 20 3a 3a 73 74 6d 74 20 5b 73 71 6c 69 74  et ::stmt [sqlit
2c40: 65 33 5f 70 72 65 70 61 72 65 20 64 62 20 22 53  e3_prepare db "S
2c50: 45 4c 45 43 54 20 7a 7a 7a 20 46 52 4f 4d 20 7a  ELECT zzz FROM z
2c60: 20 57 48 45 52 45 20 7a 7a 20 49 4e 20 28 31 2c   WHERE zz IN (1,
2c70: 20 32 2c 20 33 29 22 20 2d 31 5d 0a 20 20 73 71   2, 3)" -1].  sq
2c80: 6c 69 74 65 33 5f 73 74 65 70 20 24 3a 3a 73 74  lite3_step $::st
2c90: 6d 74 0a 7d 20 2d 62 6f 64 79 20 7b 0a 20 20 65  mt.} -body {.  e
2ca0: 78 65 63 73 71 6c 20 7b 20 49 4e 53 45 52 54 20  xecsql { INSERT 
2cb0: 49 4e 54 4f 20 7a 20 56 41 4c 55 45 53 28 4e 55  INTO z VALUES(NU
2cc0: 4c 4c 2c 20 4e 55 4c 4c 29 20 7d 0a 7d 20 2d 74  LL, NULL) }.} -t
2cd0: 65 73 74 20 7b 0a 20 20 73 71 6c 69 74 65 33 5f  est {.  sqlite3_
2ce0: 66 69 6e 61 6c 69 7a 65 20 24 3a 3a 73 74 6d 74  finalize $::stmt
2cf0: 0a 20 20 66 61 75 6c 74 73 69 6d 5f 69 6e 74 65  .  faultsim_inte
2d00: 67 72 69 74 79 5f 63 68 65 63 6b 0a 0a 20 20 66  grity_check..  f
2d10: 61 75 6c 74 73 69 6d 5f 74 65 73 74 5f 72 65 73  aultsim_test_res
2d20: 75 6c 74 20 7b 30 20 7b 7d 7d 0a 20 20 63 61 74  ult {0 {}}.  cat
2d30: 63 68 20 7b 20 64 62 20 65 76 61 6c 20 7b 20 52  ch { db eval { R
2d40: 4f 4c 4c 42 41 43 4b 20 7d 20 7d 0a 20 20 66 61  OLLBACK } }.  fa
2d50: 75 6c 74 73 69 6d 5f 69 6e 74 65 67 72 69 74 79  ultsim_integrity
2d60: 5f 63 68 65 63 6b 0a 0a 20 20 73 65 74 20 6e 20  _check..  set n 
2d70: 5b 64 62 20 65 76 61 6c 20 7b 53 45 4c 45 43 54  [db eval {SELECT
2d80: 20 63 6f 75 6e 74 28 2a 29 2c 20 73 75 6d 28 6c   count(*), sum(l
2d90: 65 6e 67 74 68 28 7a 7a 7a 29 29 20 46 52 4f 4d  ength(zzz)) FROM
2da0: 20 7a 7d 5d 0a 20 20 69 66 20 7b 24 6e 20 21 3d   z}].  if {$n !=
2db0: 20 22 36 34 20 35 31 32 30 30 22 7d 20 7b 20 65   "64 51200"} { e
2dc0: 72 72 6f 72 20 22 49 6e 63 6f 72 72 65 63 74 20  rror "Incorrect 
2dd0: 64 61 74 61 3a 20 24 6e 22 20 7d 0a 7d 0a 0a 23  data: $n" }.}..#
2de0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
2df0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
2e00: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
2e10: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
2e20: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 23 20 54 65 73  ----------.# Tes
2e30: 74 20 66 61 75 6c 74 20 69 6e 6a 65 63 74 69 6f  t fault injectio
2e40: 6e 20 77 68 69 6c 65 20 63 68 65 63 6b 70 6f 69  n while checkpoi
2e50: 6e 74 69 6e 67 20 61 20 6c 61 72 67 65 20 57 41  nting a large WA
2e60: 4c 20 66 69 6c 65 2c 20 69 66 20 74 68 65 20 0a  L file, if the .
2e70: 23 20 63 68 65 63 6b 70 6f 69 6e 74 20 69 73 20  # checkpoint is 
2e80: 74 68 65 20 66 69 72 73 74 20 6f 70 65 72 61 74  the first operat
2e90: 69 6f 6e 20 72 75 6e 20 61 66 74 65 72 20 6f 70  ion run after op
2ea0: 65 6e 69 6e 67 20 74 68 65 20 64 61 74 61 62 61  ening the databa
2eb0: 73 65 2e 0a 23 20 54 68 69 73 20 6d 65 61 6e 73  se..# This means
2ec0: 20 74 68 61 74 20 73 6f 6d 65 20 6f 66 20 74 68   that some of th
2ed0: 65 20 72 65 71 75 69 72 65 64 20 77 61 6c 2d 69  e required wal-i
2ee0: 6e 64 65 78 20 70 61 67 65 73 20 61 72 65 20 6d  ndex pages are m
2ef0: 61 70 70 65 64 20 61 73 20 70 61 72 74 20 6f 66  apped as part of
2f00: 0a 23 20 74 68 65 20 63 68 65 63 6b 70 6f 69 6e  .# the checkpoin
2f10: 74 20 70 72 6f 63 65 73 73 2c 20 77 68 69 63 68  t process, which
2f20: 20 6d 65 61 6e 73 20 74 68 65 72 65 20 61 72 65   means there are
2f30: 20 61 20 66 65 77 20 6d 6f 72 65 20 6f 70 70 6f   a few more oppo
2f40: 72 74 75 6e 69 74 69 65 73 0a 23 20 66 6f 72 20  rtunities.# for 
2f50: 49 4f 20 65 72 72 6f 72 73 2e 0a 23 0a 23 20 54  IO errors..#.# T
2f60: 6f 20 73 70 65 65 64 20 74 68 69 73 20 75 70 2c  o speed this up,
2f70: 20 49 4f 20 65 72 72 6f 72 73 20 61 72 65 20 6f   IO errors are o
2f80: 6e 6c 79 20 73 69 6d 75 6c 61 74 65 64 20 77 69  nly simulated wi
2f90: 74 68 69 6e 20 78 53 68 6d 4d 61 70 28 29 20 63  thin xShmMap() c
2fa0: 61 6c 6c 73 2e 0a 23 0a 64 6f 5f 74 65 73 74 20  alls..#.do_test 
2fb0: 77 61 6c 66 61 75 6c 74 2d 31 31 2d 70 72 65 2d  walfault-11-pre-
2fc0: 31 20 7b 0a 20 20 73 71 6c 69 74 65 33 20 64 62  1 {.  sqlite3 db
2fd0: 20 74 65 73 74 2e 64 62 0a 20 20 65 78 65 63 73   test.db.  execs
2fe0: 71 6c 20 7b 0a 20 20 20 20 50 52 41 47 4d 41 20  ql {.    PRAGMA 
2ff0: 6a 6f 75 72 6e 61 6c 5f 6d 6f 64 65 20 3d 20 57  journal_mode = W
3000: 41 4c 3b 0a 20 20 20 20 50 52 41 47 4d 41 20 77  AL;.    PRAGMA w
3010: 61 6c 5f 61 75 74 6f 63 68 65 63 6b 70 6f 69 6e  al_autocheckpoin
3020: 74 20 3d 20 30 3b 0a 20 20 20 20 42 45 47 49 4e  t = 0;.    BEGIN
3030: 3b 0a 20 20 20 20 20 20 43 52 45 41 54 45 20 54  ;.      CREATE T
3040: 41 42 4c 45 20 61 62 63 28 61 20 50 52 49 4d 41  ABLE abc(a PRIMA
3050: 52 59 20 4b 45 59 29 3b 0a 20 20 20 20 20 20 49  RY KEY);.      I
3060: 4e 53 45 52 54 20 49 4e 54 4f 20 61 62 63 20 56  NSERT INTO abc V
3070: 41 4c 55 45 53 28 72 61 6e 64 6f 6d 62 6c 6f 62  ALUES(randomblob
3080: 28 31 35 30 30 29 29 3b 0a 20 20 20 20 20 20 49  (1500));.      I
3090: 4e 53 45 52 54 20 49 4e 54 4f 20 61 62 63 20 56  NSERT INTO abc V
30a0: 41 4c 55 45 53 28 72 61 6e 64 6f 6d 62 6c 6f 62  ALUES(randomblob
30b0: 28 31 35 30 30 29 29 3b 0a 20 20 20 20 20 20 49  (1500));.      I
30c0: 4e 53 45 52 54 20 49 4e 54 4f 20 61 62 63 20 53  NSERT INTO abc S
30d0: 45 4c 45 43 54 20 72 61 6e 64 6f 6d 62 6c 6f 62  ELECT randomblob
30e0: 28 31 35 30 30 29 20 46 52 4f 4d 20 61 62 63 3b  (1500) FROM abc;
30f0: 20 20 20 2d 2d 20 20 20 20 34 0a 20 20 20 20 20     --    4.     
3100: 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 61 62 63   INSERT INTO abc
3110: 20 53 45 4c 45 43 54 20 72 61 6e 64 6f 6d 62 6c   SELECT randombl
3120: 6f 62 28 31 35 30 30 29 20 46 52 4f 4d 20 61 62  ob(1500) FROM ab
3130: 63 3b 20 20 20 2d 2d 20 20 20 20 38 0a 20 20 20  c;   --    8.   
3140: 20 20 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 61     INSERT INTO a
3150: 62 63 20 53 45 4c 45 43 54 20 72 61 6e 64 6f 6d  bc SELECT random
3160: 62 6c 6f 62 28 31 35 30 30 29 20 46 52 4f 4d 20  blob(1500) FROM 
3170: 61 62 63 3b 20 20 20 2d 2d 20 20 20 31 36 0a 20  abc;   --   16. 
3180: 20 20 20 20 20 49 4e 53 45 52 54 20 49 4e 54 4f       INSERT INTO
3190: 20 61 62 63 20 53 45 4c 45 43 54 20 72 61 6e 64   abc SELECT rand
31a0: 6f 6d 62 6c 6f 62 28 31 35 30 30 29 20 46 52 4f  omblob(1500) FRO
31b0: 4d 20 61 62 63 3b 20 20 20 2d 2d 20 20 20 33 32  M abc;   --   32
31c0: 0a 20 20 20 20 20 20 49 4e 53 45 52 54 20 49 4e  .      INSERT IN
31d0: 54 4f 20 61 62 63 20 53 45 4c 45 43 54 20 72 61  TO abc SELECT ra
31e0: 6e 64 6f 6d 62 6c 6f 62 28 31 35 30 30 29 20 46  ndomblob(1500) F
31f0: 52 4f 4d 20 61 62 63 3b 20 20 20 2d 2d 20 20 20  ROM abc;   --   
3200: 36 34 0a 20 20 20 20 20 20 49 4e 53 45 52 54 20  64.      INSERT 
3210: 49 4e 54 4f 20 61 62 63 20 53 45 4c 45 43 54 20  INTO abc SELECT 
3220: 72 61 6e 64 6f 6d 62 6c 6f 62 28 31 35 30 30 29  randomblob(1500)
3230: 20 46 52 4f 4d 20 61 62 63 3b 20 20 20 2d 2d 20   FROM abc;   -- 
3240: 20 31 32 38 0a 20 20 20 20 20 20 49 4e 53 45 52   128.      INSER
3250: 54 20 49 4e 54 4f 20 61 62 63 20 53 45 4c 45 43  T INTO abc SELEC
3260: 54 20 72 61 6e 64 6f 6d 62 6c 6f 62 28 31 35 30  T randomblob(150
3270: 30 29 20 46 52 4f 4d 20 61 62 63 3b 20 20 20 2d  0) FROM abc;   -
3280: 2d 20 20 32 35 36 0a 20 20 20 20 20 20 49 4e 53  -  256.      INS
3290: 45 52 54 20 49 4e 54 4f 20 61 62 63 20 53 45 4c  ERT INTO abc SEL
32a0: 45 43 54 20 72 61 6e 64 6f 6d 62 6c 6f 62 28 31  ECT randomblob(1
32b0: 35 30 30 29 20 46 52 4f 4d 20 61 62 63 3b 20 20  500) FROM abc;  
32c0: 20 2d 2d 20 20 35 31 32 0a 20 20 20 20 20 20 49   --  512.      I
32d0: 4e 53 45 52 54 20 49 4e 54 4f 20 61 62 63 20 53  NSERT INTO abc S
32e0: 45 4c 45 43 54 20 72 61 6e 64 6f 6d 62 6c 6f 62  ELECT randomblob
32f0: 28 31 35 30 30 29 20 46 52 4f 4d 20 61 62 63 3b  (1500) FROM abc;
3300: 20 20 20 2d 2d 20 31 30 32 34 0a 20 20 20 20 20     -- 1024.     
3310: 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 61 62 63   INSERT INTO abc
3320: 20 53 45 4c 45 43 54 20 72 61 6e 64 6f 6d 62 6c   SELECT randombl
3330: 6f 62 28 31 35 30 30 29 20 46 52 4f 4d 20 61 62  ob(1500) FROM ab
3340: 63 3b 20 20 20 2d 2d 20 32 30 34 38 0a 20 20 20  c;   -- 2048.   
3350: 20 20 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 61     INSERT INTO a
3360: 62 63 20 53 45 4c 45 43 54 20 72 61 6e 64 6f 6d  bc SELECT random
3370: 62 6c 6f 62 28 31 35 30 30 29 20 46 52 4f 4d 20  blob(1500) FROM 
3380: 61 62 63 3b 20 20 20 2d 2d 20 34 30 39 36 0a 20  abc;   -- 4096. 
3390: 20 20 20 43 4f 4d 4d 49 54 3b 0a 20 20 7d 0a 20     COMMIT;.  }. 
33a0: 20 66 61 75 6c 74 73 69 6d 5f 73 61 76 65 5f 61   faultsim_save_a
33b0: 6e 64 5f 63 6c 6f 73 65 0a 7d 20 7b 7d 0a 64 6f  nd_close.} {}.do
33c0: 5f 66 61 75 6c 74 73 69 6d 5f 74 65 73 74 20 77  _faultsim_test w
33d0: 61 6c 66 61 75 6c 74 2d 31 31 20 2d 66 61 75 6c  alfault-11 -faul
33e0: 74 73 20 73 68 6d 65 72 72 2a 20 2d 70 72 65 70  ts shmerr* -prep
33f0: 20 7b 0a 20 20 63 61 74 63 68 20 7b 20 64 62 32   {.  catch { db2
3400: 20 63 6c 6f 73 65 20 7d 0a 20 20 66 61 75 6c 74   close }.  fault
3410: 73 69 6d 5f 72 65 73 74 6f 72 65 5f 61 6e 64 5f  sim_restore_and_
3420: 72 65 6f 70 65 6e 0a 20 20 73 68 6d 66 61 75 6c  reopen.  shmfaul
3430: 74 20 66 69 6c 74 65 72 20 78 53 68 6d 4d 61 70  t filter xShmMap
3440: 0a 7d 20 2d 62 6f 64 79 20 7b 0a 20 20 64 62 20  .} -body {.  db 
3450: 65 76 61 6c 20 7b 20 53 45 4c 45 43 54 20 63 6f  eval { SELECT co
3460: 75 6e 74 28 2a 29 20 46 52 4f 4d 20 61 62 63 20  unt(*) FROM abc 
3470: 7d 0a 20 20 73 71 6c 69 74 65 33 20 64 62 32 20  }.  sqlite3 db2 
3480: 74 65 73 74 2e 64 62 20 2d 76 66 73 20 73 68 6d  test.db -vfs shm
3490: 66 61 75 6c 74 0a 20 20 64 62 32 20 65 76 61 6c  fault.  db2 eval
34a0: 20 7b 20 50 52 41 47 4d 41 20 77 61 6c 5f 63 68   { PRAGMA wal_ch
34b0: 65 63 6b 70 6f 69 6e 74 20 7d 0a 20 20 73 65 74  eckpoint }.  set
34c0: 20 7b 7d 20 7b 7d 0a 7d 20 2d 74 65 73 74 20 7b   {} {}.} -test {
34d0: 0a 20 20 66 61 75 6c 74 73 69 6d 5f 74 65 73 74  .  faultsim_test
34e0: 5f 72 65 73 75 6c 74 20 7b 30 20 7b 7d 7d 0a 7d  _result {0 {}}.}
34f0: 0a 0a 23 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ..#-------------
3500: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
3510: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
3520: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
3530: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 23 20 54  ------------.# T
3540: 65 73 74 20 74 68 65 20 68 61 6e 64 6c 69 6e 67  est the handling
3550: 20 6f 66 20 74 68 65 20 76 61 72 69 6f 75 73 20   of the various 
3560: 49 4f 2f 4f 4f 4d 2f 53 48 4d 20 65 72 72 6f 72  IO/OOM/SHM error
3570: 73 20 74 68 61 74 20 6d 61 79 20 6f 63 63 75 72  s that may occur
3580: 20 64 75 72 69 6e 67 20 0a 23 20 61 20 6c 6f 67   during .# a log
3590: 20 72 65 63 6f 76 65 72 79 20 6f 70 65 72 61 74   recovery operat
35a0: 69 6f 6e 20 75 6e 64 65 72 74 61 6b 65 6e 20 61  ion undertaken a
35b0: 73 20 70 61 72 74 20 6f 66 20 61 20 63 61 6c 6c  s part of a call
35c0: 20 74 6f 20 0a 23 20 73 71 6c 69 74 65 33 5f 77   to .# sqlite3_w
35d0: 61 6c 5f 63 68 65 63 6b 70 6f 69 6e 74 28 29 2e  al_checkpoint().
35e0: 0a 23 20 0a 64 6f 5f 74 65 73 74 20 77 61 6c 66  .# .do_test walf
35f0: 61 75 6c 74 2d 31 32 2d 70 72 65 2d 31 20 7b 0a  ault-12-pre-1 {.
3600: 20 20 66 61 75 6c 74 73 69 6d 5f 64 65 6c 65 74    faultsim_delet
3610: 65 5f 61 6e 64 5f 72 65 6f 70 65 6e 0a 20 20 65  e_and_reopen.  e
3620: 78 65 63 73 71 6c 20 7b 0a 20 20 20 20 50 52 41  xecsql {.    PRA
3630: 47 4d 41 20 6a 6f 75 72 6e 61 6c 5f 6d 6f 64 65  GMA journal_mode
3640: 20 3d 20 57 41 4c 3b 0a 20 20 20 20 50 52 41 47   = WAL;.    PRAG
3650: 4d 41 20 77 61 6c 5f 61 75 74 6f 63 68 65 63 6b  MA wal_autocheck
3660: 70 6f 69 6e 74 20 3d 20 30 3b 0a 20 20 20 20 42  point = 0;.    B
3670: 45 47 49 4e 3b 0a 20 20 20 20 20 20 43 52 45 41  EGIN;.      CREA
3680: 54 45 20 54 41 42 4c 45 20 61 62 63 28 61 20 50  TE TABLE abc(a P
3690: 52 49 4d 41 52 59 20 4b 45 59 29 3b 0a 20 20 20  RIMARY KEY);.   
36a0: 20 20 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 61     INSERT INTO a
36b0: 62 63 20 56 41 4c 55 45 53 28 72 61 6e 64 6f 6d  bc VALUES(random
36c0: 62 6c 6f 62 28 31 35 30 30 29 29 3b 0a 20 20 20  blob(1500));.   
36d0: 20 20 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 61     INSERT INTO a
36e0: 62 63 20 56 41 4c 55 45 53 28 72 61 6e 64 6f 6d  bc VALUES(random
36f0: 62 6c 6f 62 28 31 35 30 30 29 29 3b 0a 20 20 20  blob(1500));.   
3700: 20 43 4f 4d 4d 49 54 3b 0a 20 20 7d 0a 20 20 66   COMMIT;.  }.  f
3710: 61 75 6c 74 73 69 6d 5f 73 61 76 65 5f 61 6e 64  aultsim_save_and
3720: 5f 63 6c 6f 73 65 0a 7d 20 7b 7d 0a 64 6f 5f 66  _close.} {}.do_f
3730: 61 75 6c 74 73 69 6d 5f 74 65 73 74 20 77 61 6c  aultsim_test wal
3740: 66 61 75 6c 74 2d 31 32 20 2d 70 72 65 70 20 7b  fault-12 -prep {
3750: 0a 20 20 69 66 20 7b 5b 69 6e 66 6f 20 63 6f 6d  .  if {[info com
3760: 6d 61 6e 64 73 20 73 68 6d 66 61 75 6c 74 5d 20  mands shmfault] 
3770: 3d 3d 20 22 22 7d 20 7b 0a 20 20 20 20 74 65 73  == ""} {.    tes
3780: 74 76 66 73 20 73 68 6d 66 61 75 6c 74 20 2d 64  tvfs shmfault -d
3790: 65 66 61 75 6c 74 20 74 72 75 65 0a 20 20 7d 0a  efault true.  }.
37a0: 20 20 66 61 75 6c 74 73 69 6d 5f 72 65 73 74 6f    faultsim_resto
37b0: 72 65 5f 61 6e 64 5f 72 65 6f 70 65 6e 0a 20 20  re_and_reopen.  
37c0: 64 62 20 65 76 61 6c 20 7b 20 53 45 4c 45 43 54  db eval { SELECT
37d0: 20 2a 20 46 52 4f 4d 20 73 71 6c 69 74 65 5f 6d   * FROM sqlite_m
37e0: 61 73 74 65 72 20 7d 0a 20 20 73 68 6d 66 61 75  aster }.  shmfau
37f0: 6c 74 20 73 68 6d 20 74 65 73 74 2e 64 62 20 5b  lt shm test.db [
3800: 73 74 72 69 6e 67 20 72 65 70 65 61 74 20 22 5c  string repeat "\
3810: 30 30 30 22 20 34 30 5d 0a 7d 20 2d 62 6f 64 79  000" 40].} -body
3820: 20 7b 0a 20 20 73 65 74 20 72 63 20 5b 73 71 6c   {.  set rc [sql
3830: 69 74 65 33 5f 77 61 6c 5f 63 68 65 63 6b 70 6f  ite3_wal_checkpo
3840: 69 6e 74 20 64 62 5d 0a 20 20 69 66 20 7b 24 72  int db].  if {$r
3850: 63 20 21 3d 20 22 53 51 4c 49 54 45 5f 4f 4b 22  c != "SQLITE_OK"
3860: 7d 20 7b 20 65 72 72 6f 72 20 5b 73 71 6c 69 74  } { error [sqlit
3870: 65 33 5f 65 72 72 6d 73 67 20 64 62 5d 20 7d 0a  e3_errmsg db] }.
3880: 7d 20 2d 74 65 73 74 20 7b 0a 20 20 64 62 20 63  } -test {.  db c
3890: 6c 6f 73 65 0a 20 20 66 61 75 6c 74 73 69 6d 5f  lose.  faultsim_
38a0: 74 65 73 74 5f 72 65 73 75 6c 74 20 7b 30 20 7b  test_result {0 {
38b0: 7d 7d 0a 7d 0a 0a 23 2d 2d 2d 2d 2d 2d 2d 2d 2d  }}.}..#---------
38c0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
38d0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
38e0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
38f0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
3900: 0a 23 20 54 65 73 74 20 73 69 6d 70 6c 65 20 72  .# Test simple r
3910: 65 63 6f 76 65 72 79 2c 20 72 65 61 64 69 6e 67  ecovery, reading
3920: 20 61 6e 64 20 77 72 69 74 69 6e 67 20 61 20 64   and writing a d
3930: 61 74 61 62 61 73 65 20 66 69 6c 65 20 75 73 69  atabase file usi
3940: 6e 67 20 61 20 0a 23 20 68 65 61 70 2d 6d 65 6d  ng a .# heap-mem
3950: 6f 72 79 20 77 61 6c 2d 69 6e 64 65 78 2e 0a 23  ory wal-index..#
3960: 20 0a 64 6f 5f 74 65 73 74 20 77 61 6c 66 61 75   .do_test walfau
3970: 6c 74 2d 31 33 2d 70 72 65 2d 31 20 7b 0a 20 20  lt-13-pre-1 {.  
3980: 66 61 75 6c 74 73 69 6d 5f 64 65 6c 65 74 65 5f  faultsim_delete_
3990: 61 6e 64 5f 72 65 6f 70 65 6e 0a 20 20 65 78 65  and_reopen.  exe
39a0: 63 73 71 6c 20 7b 0a 20 20 20 20 50 52 41 47 4d  csql {.    PRAGM
39b0: 41 20 6a 6f 75 72 6e 61 6c 5f 6d 6f 64 65 20 3d  A journal_mode =
39c0: 20 57 41 4c 3b 0a 20 20 20 20 50 52 41 47 4d 41   WAL;.    PRAGMA
39d0: 20 77 61 6c 5f 61 75 74 6f 63 68 65 63 6b 70 6f   wal_autocheckpo
39e0: 69 6e 74 20 3d 20 30 3b 0a 20 20 20 20 42 45 47  int = 0;.    BEG
39f0: 49 4e 3b 0a 20 20 20 20 20 20 43 52 45 41 54 45  IN;.      CREATE
3a00: 20 54 41 42 4c 45 20 61 62 63 28 61 20 50 52 49   TABLE abc(a PRI
3a10: 4d 41 52 59 20 4b 45 59 29 3b 0a 20 20 20 20 20  MARY KEY);.     
3a20: 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 61 62 63   INSERT INTO abc
3a30: 20 56 41 4c 55 45 53 28 72 61 6e 64 6f 6d 62 6c   VALUES(randombl
3a40: 6f 62 28 31 35 30 30 29 29 3b 0a 20 20 20 20 20  ob(1500));.     
3a50: 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 61 62 63   INSERT INTO abc
3a60: 20 56 41 4c 55 45 53 28 72 61 6e 64 6f 6d 62 6c   VALUES(randombl
3a70: 6f 62 28 31 35 30 30 29 29 3b 0a 20 20 20 20 43  ob(1500));.    C
3a80: 4f 4d 4d 49 54 3b 0a 20 20 7d 0a 20 20 66 61 75  OMMIT;.  }.  fau
3a90: 6c 74 73 69 6d 5f 73 61 76 65 5f 61 6e 64 5f 63  ltsim_save_and_c
3aa0: 6c 6f 73 65 0a 20 20 64 65 6c 65 74 65 5f 66 69  lose.  delete_fi
3ab0: 6c 65 20 73 76 5f 74 65 73 74 2e 64 62 2d 73 68  le sv_test.db-sh
3ac0: 6d 0a 7d 20 7b 7d 0a 0a 64 6f 5f 66 61 75 6c 74  m.} {}..do_fault
3ad0: 73 69 6d 5f 74 65 73 74 20 77 61 6c 66 61 75 6c  sim_test walfaul
3ae0: 74 2d 31 33 2e 31 20 2d 70 72 65 70 20 7b 0a 20  t-13.1 -prep {. 
3af0: 20 66 61 75 6c 74 73 69 6d 5f 72 65 73 74 6f 72   faultsim_restor
3b00: 65 5f 61 6e 64 5f 72 65 6f 70 65 6e 0a 7d 20 2d  e_and_reopen.} -
3b10: 62 6f 64 79 20 7b 0a 20 20 64 62 20 65 76 61 6c  body {.  db eval
3b20: 20 7b 20 50 52 41 47 4d 41 20 6c 6f 63 6b 69 6e   { PRAGMA lockin
3b30: 67 5f 6d 6f 64 65 20 3d 20 65 78 63 6c 75 73 69  g_mode = exclusi
3b40: 76 65 20 7d 0a 20 20 64 62 20 65 76 61 6c 20 7b  ve }.  db eval {
3b50: 20 53 45 4c 45 43 54 20 63 6f 75 6e 74 28 2a 29   SELECT count(*)
3b60: 20 46 52 4f 4d 20 61 62 63 20 7d 0a 7d 20 2d 74   FROM abc }.} -t
3b70: 65 73 74 20 7b 0a 20 20 66 61 75 6c 74 73 69 6d  est {.  faultsim
3b80: 5f 74 65 73 74 5f 72 65 73 75 6c 74 20 7b 30 20  _test_result {0 
3b90: 32 7d 0a 20 20 69 66 20 7b 5b 66 69 6c 65 20 65  2}.  if {[file e
3ba0: 78 69 73 74 73 20 74 65 73 74 2e 64 62 2d 73 68  xists test.db-sh
3bb0: 6d 5d 7d 20 7b 20 65 72 72 6f 72 20 22 4e 6f 74  m]} { error "Not
3bc0: 20 75 73 69 6e 67 20 68 65 61 70 2d 6d 65 6d 6f   using heap-memo
3bd0: 72 79 20 6d 6f 64 65 22 20 7d 0a 20 20 66 61 75  ry mode" }.  fau
3be0: 6c 74 73 69 6d 5f 69 6e 74 65 67 72 69 74 79 5f  ltsim_integrity_
3bf0: 63 68 65 63 6b 0a 7d 0a 0a 64 6f 5f 66 61 75 6c  check.}..do_faul
3c00: 74 73 69 6d 5f 74 65 73 74 20 77 61 6c 66 61 75  tsim_test walfau
3c10: 6c 74 2d 31 33 2e 32 20 2d 70 72 65 70 20 7b 0a  lt-13.2 -prep {.
3c20: 20 20 66 61 75 6c 74 73 69 6d 5f 72 65 73 74 6f    faultsim_resto
3c30: 72 65 5f 61 6e 64 5f 72 65 6f 70 65 6e 0a 20 20  re_and_reopen.  
3c40: 64 62 20 65 76 61 6c 20 7b 20 50 52 41 47 4d 41  db eval { PRAGMA
3c50: 20 6c 6f 63 6b 69 6e 67 5f 6d 6f 64 65 20 3d 20   locking_mode = 
3c60: 65 78 63 6c 75 73 69 76 65 20 7d 0a 7d 20 2d 62  exclusive }.} -b
3c70: 6f 64 79 20 7b 0a 20 20 64 62 20 65 76 61 6c 20  ody {.  db eval 
3c80: 7b 20 50 52 41 47 4d 41 20 6a 6f 75 72 6e 61 6c  { PRAGMA journal
3c90: 5f 6d 6f 64 65 20 3d 20 64 65 6c 65 74 65 20 7d  _mode = delete }
3ca0: 0a 7d 20 2d 74 65 73 74 20 7b 0a 20 20 66 61 75  .} -test {.  fau
3cb0: 6c 74 73 69 6d 5f 74 65 73 74 5f 72 65 73 75 6c  ltsim_test_resul
3cc0: 74 20 7b 30 20 64 65 6c 65 74 65 7d 0a 20 20 69  t {0 delete}.  i
3cd0: 66 20 7b 5b 66 69 6c 65 20 65 78 69 73 74 73 20  f {[file exists 
3ce0: 74 65 73 74 2e 64 62 2d 73 68 6d 5d 7d 20 7b 20  test.db-shm]} { 
3cf0: 65 72 72 6f 72 20 22 4e 6f 74 20 75 73 69 6e 67  error "Not using
3d00: 20 68 65 61 70 2d 6d 65 6d 6f 72 79 20 6d 6f 64   heap-memory mod
3d10: 65 22 20 7d 0a 20 20 66 61 75 6c 74 73 69 6d 5f  e" }.  faultsim_
3d20: 69 6e 74 65 67 72 69 74 79 5f 63 68 65 63 6b 0a  integrity_check.
3d30: 7d 0a 0a 64 6f 5f 74 65 73 74 20 77 61 6c 66 61  }..do_test walfa
3d40: 75 6c 74 2d 31 33 2d 70 72 65 2d 32 20 7b 0a 20  ult-13-pre-2 {. 
3d50: 20 66 61 75 6c 74 73 69 6d 5f 64 65 6c 65 74 65   faultsim_delete
3d60: 5f 61 6e 64 5f 72 65 6f 70 65 6e 0a 20 20 65 78  _and_reopen.  ex
3d70: 65 63 73 71 6c 20 7b 0a 20 20 20 20 42 45 47 49  ecsql {.    BEGI
3d80: 4e 3b 0a 20 20 20 20 20 20 43 52 45 41 54 45 20  N;.      CREATE 
3d90: 54 41 42 4c 45 20 61 62 63 28 61 20 50 52 49 4d  TABLE abc(a PRIM
3da0: 41 52 59 20 4b 45 59 29 3b 0a 20 20 20 20 20 20  ARY KEY);.      
3db0: 49 4e 53 45 52 54 20 49 4e 54 4f 20 61 62 63 20  INSERT INTO abc 
3dc0: 56 41 4c 55 45 53 28 72 61 6e 64 6f 6d 62 6c 6f  VALUES(randomblo
3dd0: 62 28 31 35 30 30 29 29 3b 0a 20 20 20 20 20 20  b(1500));.      
3de0: 49 4e 53 45 52 54 20 49 4e 54 4f 20 61 62 63 20  INSERT INTO abc 
3df0: 56 41 4c 55 45 53 28 72 61 6e 64 6f 6d 62 6c 6f  VALUES(randomblo
3e00: 62 28 31 35 30 30 29 29 3b 0a 20 20 20 20 43 4f  b(1500));.    CO
3e10: 4d 4d 49 54 3b 0a 20 20 7d 0a 20 20 66 61 75 6c  MMIT;.  }.  faul
3e20: 74 73 69 6d 5f 73 61 76 65 5f 61 6e 64 5f 63 6c  tsim_save_and_cl
3e30: 6f 73 65 0a 7d 20 7b 7d 0a 0a 64 6f 5f 66 61 75  ose.} {}..do_fau
3e40: 6c 74 73 69 6d 5f 74 65 73 74 20 77 61 6c 66 61  ltsim_test walfa
3e50: 75 6c 74 2d 31 33 2e 33 20 2d 70 72 65 70 20 7b  ult-13.3 -prep {
3e60: 0a 20 20 66 61 75 6c 74 73 69 6d 5f 72 65 73 74  .  faultsim_rest
3e70: 6f 72 65 5f 61 6e 64 5f 72 65 6f 70 65 6e 0a 7d  ore_and_reopen.}
3e80: 20 2d 62 6f 64 79 20 7b 0a 20 20 64 62 20 65 76   -body {.  db ev
3e90: 61 6c 20 7b 20 0a 20 20 20 20 50 52 41 47 4d 41  al { .    PRAGMA
3ea0: 20 6c 6f 63 6b 69 6e 67 5f 6d 6f 64 65 20 3d 20   locking_mode = 
3eb0: 65 78 63 6c 75 73 69 76 65 3b 0a 20 20 20 20 50  exclusive;.    P
3ec0: 52 41 47 4d 41 20 6a 6f 75 72 6e 61 6c 5f 6d 6f  RAGMA journal_mo
3ed0: 64 65 20 3d 20 57 41 4c 3b 0a 20 20 20 20 49 4e  de = WAL;.    IN
3ee0: 53 45 52 54 20 49 4e 54 4f 20 61 62 63 20 56 41  SERT INTO abc VA
3ef0: 4c 55 45 53 28 72 61 6e 64 6f 6d 62 6c 6f 62 28  LUES(randomblob(
3f00: 31 35 30 30 29 29 3b 0a 20 20 7d 0a 7d 20 2d 74  1500));.  }.} -t
3f10: 65 73 74 20 7b 0a 20 20 66 61 75 6c 74 73 69 6d  est {.  faultsim
3f20: 5f 74 65 73 74 5f 72 65 73 75 6c 74 20 7b 30 20  _test_result {0 
3f30: 7b 65 78 63 6c 75 73 69 76 65 20 77 61 6c 7d 7d  {exclusive wal}}
3f40: 0a 20 20 69 66 20 7b 5b 66 69 6c 65 20 65 78 69  .  if {[file exi
3f50: 73 74 73 20 74 65 73 74 2e 64 62 2d 73 68 6d 5d  sts test.db-shm]
3f60: 7d 20 7b 20 65 72 72 6f 72 20 22 4e 6f 74 20 75  } { error "Not u
3f70: 73 69 6e 67 20 68 65 61 70 2d 6d 65 6d 6f 72 79  sing heap-memory
3f80: 20 6d 6f 64 65 22 20 7d 0a 20 20 66 61 75 6c 74   mode" }.  fault
3f90: 73 69 6d 5f 69 6e 74 65 67 72 69 74 79 5f 63 68  sim_integrity_ch
3fa0: 65 63 6b 0a 20 20 73 65 74 20 6e 52 6f 77 20 5b  eck.  set nRow [
3fb0: 64 62 20 65 76 61 6c 20 7b 53 45 4c 45 43 54 20  db eval {SELECT 
3fc0: 63 6f 75 6e 74 28 2a 29 20 46 52 4f 4d 20 61 62  count(*) FROM ab
3fd0: 63 7d 5d 0a 20 20 69 66 20 7b 21 28 28 24 6e 52  c}].  if {!(($nR
3fe0: 6f 77 3d 3d 32 20 26 26 20 24 74 65 73 74 72 63  ow==2 && $testrc
3ff0: 29 20 7c 7c 20 24 6e 52 6f 77 3d 3d 33 29 7d 20  ) || $nRow==3)} 
4000: 7b 20 65 72 72 6f 72 20 22 42 61 64 20 64 62 20  { error "Bad db 
4010: 63 6f 6e 74 65 6e 74 22 20 7d 0a 7d 0a 0a 23 2d  content" }.}..#-
4020: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
4030: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
4040: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
4050: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
4060: 2d 2d 2d 2d 2d 2d 2d 2d 0a 23 20 54 65 73 74 20  --------.# Test 
4070: 66 61 75 6c 74 2d 68 61 6e 64 6c 69 6e 67 20 77  fault-handling w
4080: 68 65 6e 20 77 72 61 70 70 69 6e 67 20 61 72 6f  hen wrapping aro
4090: 75 6e 64 20 74 6f 20 74 68 65 20 73 74 61 72 74  und to the start
40a0: 20 6f 66 20 61 20 57 41 4c 20 66 69 6c 65 2e 0a   of a WAL file..
40b0: 23 0a 64 6f 5f 74 65 73 74 20 77 61 6c 66 61 75  #.do_test walfau
40c0: 6c 74 2d 31 34 2d 70 72 65 20 7b 0a 20 20 66 61  lt-14-pre {.  fa
40d0: 75 6c 74 73 69 6d 5f 64 65 6c 65 74 65 5f 61 6e  ultsim_delete_an
40e0: 64 5f 72 65 6f 70 65 6e 0a 20 20 65 78 65 63 73  d_reopen.  execs
40f0: 71 6c 20 7b 0a 20 20 20 20 50 52 41 47 4d 41 20  ql {.    PRAGMA 
4100: 61 75 74 6f 5f 76 61 63 75 75 6d 20 3d 20 30 3b  auto_vacuum = 0;
4110: 0a 20 20 20 20 50 52 41 47 4d 41 20 6a 6f 75 72  .    PRAGMA jour
4120: 6e 61 6c 5f 6d 6f 64 65 20 3d 20 57 41 4c 3b 0a  nal_mode = WAL;.
4130: 20 20 20 20 42 45 47 49 4e 3b 0a 20 20 20 20 20      BEGIN;.     
4140: 20 43 52 45 41 54 45 20 54 41 42 4c 45 20 61 62   CREATE TABLE ab
4150: 63 28 61 20 50 52 49 4d 41 52 59 20 4b 45 59 29  c(a PRIMARY KEY)
4160: 3b 0a 20 20 20 20 20 20 49 4e 53 45 52 54 20 49  ;.      INSERT I
4170: 4e 54 4f 20 61 62 63 20 56 41 4c 55 45 53 28 72  NTO abc VALUES(r
4180: 61 6e 64 6f 6d 62 6c 6f 62 28 31 35 30 30 29 29  andomblob(1500))
4190: 3b 0a 20 20 20 20 20 20 49 4e 53 45 52 54 20 49  ;.      INSERT I
41a0: 4e 54 4f 20 61 62 63 20 56 41 4c 55 45 53 28 72  NTO abc VALUES(r
41b0: 61 6e 64 6f 6d 62 6c 6f 62 28 31 35 30 30 29 29  andomblob(1500))
41c0: 3b 0a 20 20 20 20 43 4f 4d 4d 49 54 3b 0a 20 20  ;.    COMMIT;.  
41d0: 7d 0a 20 20 66 61 75 6c 74 73 69 6d 5f 73 61 76  }.  faultsim_sav
41e0: 65 5f 61 6e 64 5f 63 6c 6f 73 65 0a 7d 20 7b 7d  e_and_close.} {}
41f0: 0a 64 6f 5f 66 61 75 6c 74 73 69 6d 5f 74 65 73  .do_faultsim_tes
4200: 74 20 77 61 6c 66 61 75 6c 74 2d 31 34 20 2d 70  t walfault-14 -p
4210: 72 65 70 20 7b 0a 20 20 66 61 75 6c 74 73 69 6d  rep {.  faultsim
4220: 5f 72 65 73 74 6f 72 65 5f 61 6e 64 5f 72 65 6f  _restore_and_reo
4230: 70 65 6e 0a 7d 20 2d 62 6f 64 79 20 7b 0a 20 20  pen.} -body {.  
4240: 64 62 20 65 76 61 6c 20 7b 20 0a 20 20 20 20 50  db eval { .    P
4250: 52 41 47 4d 41 20 77 61 6c 5f 63 68 65 63 6b 70  RAGMA wal_checkp
4260: 6f 69 6e 74 20 3d 20 66 75 6c 6c 3b 0a 20 20 20  oint = full;.   
4270: 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 61 62 63   INSERT INTO abc
4280: 20 56 41 4c 55 45 53 28 72 61 6e 64 6f 6d 62 6c   VALUES(randombl
4290: 6f 62 28 31 35 30 30 29 29 3b 0a 20 20 7d 0a 7d  ob(1500));.  }.}
42a0: 20 2d 74 65 73 74 20 7b 0a 20 20 66 61 75 6c 74   -test {.  fault
42b0: 73 69 6d 5f 74 65 73 74 5f 72 65 73 75 6c 74 20  sim_test_result 
42c0: 7b 30 20 7b 30 20 39 20 39 7d 7d 0a 20 20 66 61  {0 {0 9 9}}.  fa
42d0: 75 6c 74 73 69 6d 5f 69 6e 74 65 67 72 69 74 79  ultsim_integrity
42e0: 5f 63 68 65 63 6b 0a 20 20 73 65 74 20 6e 52 6f  _check.  set nRo
42f0: 77 20 5b 64 62 20 65 76 61 6c 20 7b 53 45 4c 45  w [db eval {SELE
4300: 43 54 20 63 6f 75 6e 74 28 2a 29 20 46 52 4f 4d  CT count(*) FROM
4310: 20 61 62 63 7d 5d 0a 20 20 69 66 20 7b 21 28 28   abc}].  if {!((
4320: 24 6e 52 6f 77 3d 3d 32 20 26 26 20 24 74 65 73  $nRow==2 && $tes
4330: 74 72 63 29 20 7c 7c 20 24 6e 52 6f 77 3d 3d 33  trc) || $nRow==3
4340: 29 7d 20 7b 20 65 72 72 6f 72 20 22 42 61 64 20  )} { error "Bad 
4350: 64 62 20 63 6f 6e 74 65 6e 74 22 20 7d 0a 7d 0a  db content" }.}.
4360: 0a 23 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  .#--------------
4370: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
4380: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
4390: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
43a0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 23 20 54 65  -----------.# Te
43b0: 73 74 20 66 61 75 6c 74 2d 68 61 6e 64 6c 69 6e  st fault-handlin
43c0: 67 20 77 68 65 6e 20 73 77 69 74 63 68 69 6e 67  g when switching
43d0: 20 6f 75 74 20 6f 66 20 65 78 63 6c 75 73 69 76   out of exclusiv
43e0: 65 2d 6c 6f 63 6b 69 6e 67 20 6d 6f 64 65 2e 0a  e-locking mode..
43f0: 23 0a 64 6f 5f 74 65 73 74 20 77 61 6c 66 61 75  #.do_test walfau
4400: 6c 74 2d 31 35 2d 70 72 65 20 7b 0a 20 20 66 61  lt-15-pre {.  fa
4410: 75 6c 74 73 69 6d 5f 64 65 6c 65 74 65 5f 61 6e  ultsim_delete_an
4420: 64 5f 72 65 6f 70 65 6e 0a 20 20 65 78 65 63 73  d_reopen.  execs
4430: 71 6c 20 7b 0a 20 20 20 20 50 52 41 47 4d 41 20  ql {.    PRAGMA 
4440: 61 75 74 6f 5f 76 61 63 75 75 6d 20 3d 20 30 3b  auto_vacuum = 0;
4450: 0a 20 20 20 20 50 52 41 47 4d 41 20 6a 6f 75 72  .    PRAGMA jour
4460: 6e 61 6c 5f 6d 6f 64 65 20 3d 20 57 41 4c 3b 0a  nal_mode = WAL;.
4470: 20 20 20 20 42 45 47 49 4e 3b 0a 20 20 20 20 20      BEGIN;.     
4480: 20 43 52 45 41 54 45 20 54 41 42 4c 45 20 61 62   CREATE TABLE ab
4490: 63 28 61 20 50 52 49 4d 41 52 59 20 4b 45 59 29  c(a PRIMARY KEY)
44a0: 3b 0a 20 20 20 20 20 20 49 4e 53 45 52 54 20 49  ;.      INSERT I
44b0: 4e 54 4f 20 61 62 63 20 56 41 4c 55 45 53 28 72  NTO abc VALUES(r
44c0: 61 6e 64 6f 6d 62 6c 6f 62 28 31 35 30 30 29 29  andomblob(1500))
44d0: 3b 0a 20 20 20 20 20 20 49 4e 53 45 52 54 20 49  ;.      INSERT I
44e0: 4e 54 4f 20 61 62 63 20 56 41 4c 55 45 53 28 72  NTO abc VALUES(r
44f0: 61 6e 64 6f 6d 62 6c 6f 62 28 31 35 30 30 29 29  andomblob(1500))
4500: 3b 0a 20 20 20 20 43 4f 4d 4d 49 54 3b 0a 20 20  ;.    COMMIT;.  
4510: 7d 0a 20 20 66 61 75 6c 74 73 69 6d 5f 73 61 76  }.  faultsim_sav
4520: 65 5f 61 6e 64 5f 63 6c 6f 73 65 0a 7d 20 7b 7d  e_and_close.} {}
4530: 0a 64 6f 5f 66 61 75 6c 74 73 69 6d 5f 74 65 73  .do_faultsim_tes
4540: 74 20 77 61 6c 66 61 75 6c 74 2d 31 35 20 2d 70  t walfault-15 -p
4550: 72 65 70 20 7b 0a 20 20 66 61 75 6c 74 73 69 6d  rep {.  faultsim
4560: 5f 72 65 73 74 6f 72 65 5f 61 6e 64 5f 72 65 6f  _restore_and_reo
4570: 70 65 6e 0a 20 20 65 78 65 63 73 71 6c 20 7b 0a  pen.  execsql {.
4580: 20 20 20 20 53 45 4c 45 43 54 20 63 6f 75 6e 74      SELECT count
4590: 28 2a 29 20 46 52 4f 4d 20 61 62 63 3b 0a 20 20  (*) FROM abc;.  
45a0: 20 20 50 52 41 47 4d 41 20 6c 6f 63 6b 69 6e 67    PRAGMA locking
45b0: 5f 6d 6f 64 65 20 3d 20 65 78 63 6c 75 73 69 76  _mode = exclusiv
45c0: 65 3b 0a 20 20 20 20 42 45 47 49 4e 3b 0a 20 20  e;.    BEGIN;.  
45d0: 20 20 20 20 49 4e 53 45 52 54 20 49 4e 54 4f 20      INSERT INTO 
45e0: 61 62 63 20 56 41 4c 55 45 53 28 72 61 6e 64 6f  abc VALUES(rando
45f0: 6d 62 6c 6f 62 28 31 35 30 30 29 29 3b 0a 20 20  mblob(1500));.  
4600: 20 20 43 4f 4d 4d 49 54 3b 0a 20 20 7d 0a 7d 20    COMMIT;.  }.} 
4610: 2d 62 6f 64 79 20 7b 0a 20 20 64 62 20 65 76 61  -body {.  db eva
4620: 6c 20 7b 20 0a 20 20 20 20 50 52 41 47 4d 41 20  l { .    PRAGMA 
4630: 6c 6f 63 6b 69 6e 67 5f 6d 6f 64 65 20 3d 20 6e  locking_mode = n
4640: 6f 72 6d 61 6c 3b 0a 20 20 20 20 42 45 47 49 4e  ormal;.    BEGIN
4650: 3b 0a 20 20 20 20 20 20 49 4e 53 45 52 54 20 49  ;.      INSERT I
4660: 4e 54 4f 20 61 62 63 20 56 41 4c 55 45 53 28 72  NTO abc VALUES(r
4670: 61 6e 64 6f 6d 62 6c 6f 62 28 31 35 30 30 29 29  andomblob(1500))
4680: 3b 0a 20 20 20 20 43 4f 4d 4d 49 54 3b 0a 20 20  ;.    COMMIT;.  
4690: 7d 0a 7d 20 2d 74 65 73 74 20 7b 0a 20 20 66 61  }.} -test {.  fa
46a0: 75 6c 74 73 69 6d 5f 69 6e 74 65 67 72 69 74 79  ultsim_integrity
46b0: 5f 63 68 65 63 6b 0a 20 20 73 65 74 20 6e 52 6f  _check.  set nRo
46c0: 77 20 5b 64 62 20 65 76 61 6c 20 7b 53 45 4c 45  w [db eval {SELE
46d0: 43 54 20 63 6f 75 6e 74 28 2a 29 20 46 52 4f 4d  CT count(*) FROM
46e0: 20 61 62 63 7d 5d 0a 20 20 69 66 20 7b 24 6e 52   abc}].  if {$nR
46f0: 6f 77 21 3d 33 20 26 26 20 24 6e 52 6f 77 21 3d  ow!=3 && $nRow!=
4700: 34 7d 20 7b 20 65 72 72 6f 72 20 22 42 61 64 20  4} { error "Bad 
4710: 64 62 20 63 6f 6e 74 65 6e 74 22 20 7d 0a 7d 0a  db content" }.}.
4720: 0a 66 69 6e 69 73 68 5f 74 65 73 74 0a           .finish_test.