/ Hex Artifact Content
Login

Artifact 33a37ce3ed56a20093ceee778cd2d7109c7085a59f3213d2baede11d952e8e50:


0000: 23 20 32 30 30 39 20 4a 75 6e 65 20 33 0a 23 0a  # 2009 June 3.#.
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 0a 23 20 24 49 64 3a 20 63 6f 72  ***.#.# $Id: cor
0170: 72 75 70 74 44 2e 74 65 73 74 2c 76 20 31 2e 32  ruptD.test,v 1.2
0180: 20 32 30 30 39 2f 30 36 2f 30 35 20 31 37 3a 30   2009/06/05 17:0
0190: 39 3a 31 32 20 64 72 68 20 45 78 70 20 24 0a 0a  9:12 drh Exp $..
01a0: 73 65 74 20 74 65 73 74 64 69 72 20 5b 66 69 6c  set testdir [fil
01b0: 65 20 64 69 72 6e 61 6d 65 20 24 61 72 67 76 30  e dirname $argv0
01c0: 5d 0a 73 6f 75 72 63 65 20 24 74 65 73 74 64 69  ].source $testdi
01d0: 72 2f 74 65 73 74 65 72 2e 74 63 6c 0a 0a 23 20  r/tester.tcl..# 
01e0: 44 6f 20 6e 6f 74 20 75 73 65 20 61 20 63 6f 64  Do not use a cod
01f0: 65 63 20 66 6f 72 20 74 65 73 74 73 20 69 6e 20  ec for tests in 
0200: 74 68 69 73 20 66 69 6c 65 2c 20 61 73 20 74 68  this file, as th
0210: 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20  e database file 
0220: 69 73 0a 23 20 6d 61 6e 69 70 75 6c 61 74 65 64  is.# manipulated
0230: 20 64 69 72 65 63 74 6c 79 20 75 73 69 6e 67 20   directly using 
0240: 74 63 6c 20 73 63 72 69 70 74 73 20 28 75 73 69  tcl scripts (usi
0250: 6e 67 20 74 68 65 20 5b 68 65 78 69 6f 5f 77 72  ng the [hexio_wr
0260: 69 74 65 5d 20 63 6f 6d 6d 61 6e 64 29 2e 0a 23  ite] command)..#
0270: 0a 64 6f 5f 6e 6f 74 5f 75 73 65 5f 63 6f 64 65  .do_not_use_code
0280: 63 0a 0a 23 20 54 68 65 73 65 20 74 65 73 74 73  c..# These tests
0290: 20 64 65 61 6c 20 77 69 74 68 20 63 6f 72 72 75   deal with corru
02a0: 70 74 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  pt database file
02b0: 73 0a 23 0a 64 61 74 61 62 61 73 65 5f 6d 61 79  s.#.database_may
02c0: 5f 62 65 5f 63 6f 72 72 75 70 74 0a 0a 23 2d 2d  _be_corrupt..#--
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 2d 2d 2d 2d  ----------------
0300: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
0310: 2d 2d 2d 2d 2d 2d 2d 2d 0a 23 20 4f 56 45 52 56  --------.# OVERV
0320: 49 45 57 0a 23 0a 23 20 20 20 54 68 69 73 20 74  IEW.#.#   This t
0330: 65 73 74 20 66 69 6c 65 20 61 74 74 65 6d 70 74  est file attempt
0340: 73 20 74 6f 20 76 65 72 69 66 79 20 74 68 61 74  s to verify that
0350: 20 53 51 4c 69 74 65 20 64 6f 65 73 20 6e 6f 74   SQLite does not
0360: 20 72 65 61 64 20 70 61 73 74 20 74 68 65 20 0a   read past the .
0370: 23 20 20 20 65 6e 64 20 6f 66 20 61 6e 79 20 69  #   end of any i
0380: 6e 2d 6d 65 6d 6f 72 79 20 62 75 66 66 65 72 73  n-memory buffers
0390: 20 61 73 20 61 20 72 65 73 75 6c 74 20 6f 66 20   as a result of 
03a0: 63 6f 72 72 75 70 74 65 64 20 64 61 74 61 62 61  corrupted databa
03b0: 73 65 20 70 61 67 65 20 0a 23 20 20 20 69 6d 61  se page .#   ima
03c0: 67 65 73 2e 20 55 73 75 61 6c 6c 79 20 74 68 69  ges. Usually thi
03d0: 73 20 68 61 70 70 65 6e 73 20 62 65 63 61 75 73  s happens becaus
03e0: 65 20 61 20 66 69 65 6c 64 20 77 69 74 68 69 6e  e a field within
03f0: 20 61 20 64 61 74 61 62 61 73 65 20 70 61 67 65   a database page
0400: 0a 23 20 20 20 74 68 61 74 20 63 6f 6e 74 61 69  .#   that contai
0410: 6e 73 20 61 6e 20 6f 66 66 73 65 74 20 74 6f 20  ns an offset to 
0420: 73 6f 6d 65 20 6f 74 68 65 72 20 73 74 72 75 63  some other struc
0430: 74 75 72 65 20 77 69 74 68 69 6e 20 74 68 65 20  ture within the 
0440: 73 61 6d 65 20 70 61 67 65 0a 23 20 20 20 69 73  same page.#   is
0450: 20 73 65 74 20 74 6f 20 74 6f 6f 20 6c 61 72 67   set to too larg
0460: 65 20 61 20 76 61 6c 75 65 2e 20 41 20 64 61 74  e a value. A dat
0470: 61 62 61 73 65 20 70 61 67 65 20 63 6f 6e 74 61  abase page conta
0480: 69 6e 73 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e  ins the followin
0490: 67 0a 23 20 20 20 73 75 63 68 20 66 69 65 6c 64  g.#   such field
04a0: 73 3a 0a 23 0a 23 20 20 20 20 20 31 2e 20 54 68  s:.#.#     1. Th
04b0: 65 20 70 61 67 65 20 68 65 61 64 65 72 20 66 69  e page header fi
04c0: 65 6c 64 20 74 68 61 74 20 63 6f 6e 74 61 69 6e  eld that contain
04d0: 73 20 74 68 65 20 6f 66 66 73 65 74 20 74 6f 20  s the offset to 
04e0: 74 68 65 20 66 69 72 73 74 20 0a 23 20 20 20 20  the first .#    
04f0: 20 20 20 20 66 72 65 65 20 62 6c 6f 63 6b 20 6f      free block o
0500: 66 20 73 70 61 63 65 2e 0a 23 0a 23 20 20 20 20  f space..#.#    
0510: 20 32 2e 20 54 68 65 20 66 69 72 73 74 20 74 77   2. The first tw
0520: 6f 20 62 79 74 65 73 20 6f 66 20 61 6c 6c 20 62  o bytes of all b
0530: 75 74 20 74 68 65 20 6c 61 73 74 20 66 72 65 65  ut the last free
0540: 20 62 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 66 72   block on the fr
0550: 65 65 2d 62 6c 6f 63 6b 0a 23 20 20 20 20 20 20  ee-block.#      
0560: 20 20 6c 69 73 74 20 28 74 68 65 20 6f 66 66 73    list (the offs
0570: 65 74 20 74 6f 20 74 68 65 20 6e 65 78 74 20 66  et to the next f
0580: 72 65 65 20 62 6c 6f 63 6b 29 2e 0a 23 0a 23 20  ree block)..#.# 
0590: 20 20 20 20 33 2e 20 54 68 65 20 70 61 67 65 20      3. The page 
05a0: 68 65 61 64 65 72 20 66 69 65 6c 64 20 63 6f 6e  header field con
05b0: 74 61 69 6e 69 6e 67 20 74 68 65 20 6e 75 6d 62  taining the numb
05c0: 65 72 20 6f 66 20 63 65 6c 6c 73 20 6f 6e 20 74  er of cells on t
05d0: 68 65 20 70 61 67 65 0a 23 20 20 20 20 20 20 20  he page.#       
05e0: 20 28 69 6d 70 6c 69 63 69 74 6c 79 20 64 65 66   (implicitly def
05f0: 69 6e 65 73 20 74 68 65 20 6f 66 66 73 65 74 20  ines the offset 
0600: 74 6f 20 74 68 65 20 66 69 6e 61 6c 20 65 6c 65  to the final ele
0610: 6d 65 6e 74 20 69 6e 20 74 68 65 20 63 65 6c 6c  ment in the cell
0620: 20 6f 66 66 73 65 74 0a 23 20 20 20 20 20 20 20   offset.#       
0630: 20 61 72 72 61 79 2c 20 77 68 69 63 68 20 63 6f   array, which co
0640: 75 6c 64 20 70 6f 74 65 6e 74 69 61 6c 6c 79 20  uld potentially 
0650: 62 65 20 6f 66 66 20 74 68 65 20 65 6e 64 20 6f  be off the end o
0660: 66 20 74 68 65 20 70 61 67 65 29 2e 0a 23 0a 23  f the page)..#.#
0670: 20 20 20 20 20 34 2e 20 54 68 65 20 70 61 67 65       4. The page
0680: 20 68 65 61 64 65 72 20 66 69 65 6c 64 20 63 6f   header field co
0690: 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 6f 66 66  ntaining the off
06a0: 73 65 74 20 74 6f 20 74 68 65 20 73 74 61 72 74  set to the start
06b0: 20 6f 66 20 74 68 65 20 63 65 6c 6c 0a 23 20 20   of the cell.#  
06c0: 20 20 20 20 20 20 63 6f 6e 74 65 6e 74 20 61 72        content ar
06d0: 65 61 2e 0a 23 0a 23 20 20 20 20 20 35 2e 20 54  ea..#.#     5. T
06e0: 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74  he contents of t
06f0: 68 65 20 63 65 6c 6c 20 6f 66 66 73 65 74 20 61  he cell offset a
0700: 72 72 61 79 2e 0a 23 0a 23 20 20 20 20 20 36 2e  rray..#.#     6.
0710: 20 54 68 65 20 66 69 72 73 74 20 66 65 77 20 62   The first few b
0720: 79 74 65 73 20 6f 66 20 65 61 63 68 20 63 65 6c  ytes of each cel
0730: 6c 20 64 65 74 65 72 6d 69 6e 65 20 74 68 65 20  l determine the 
0740: 73 69 7a 65 20 6f 66 20 74 68 65 20 63 65 6c 6c  size of the cell
0750: 0a 23 20 20 20 20 20 20 20 20 73 74 6f 72 65 64  .#        stored
0760: 20 77 69 74 68 69 6e 20 74 68 65 20 70 61 67 65   within the page
0770: 2c 20 61 6e 64 20 68 65 6e 63 65 20 74 68 65 20  , and hence the 
0780: 6f 66 66 73 65 74 20 74 6f 20 74 68 65 20 66 69  offset to the fi
0790: 6e 61 6c 20 62 79 74 65 20 6f 66 0a 23 20 20 20  nal byte of.#   
07a0: 20 20 20 20 20 74 68 65 20 63 65 6c 6c 2e 0a 23       the cell..#
07b0: 0a 23 20 20 20 49 66 20 61 6e 79 20 6f 66 20 74  .#   If any of t
07c0: 68 65 20 61 62 6f 76 65 20 66 69 65 6c 64 73 20  he above fields 
07d0: 61 72 65 20 73 65 74 20 74 6f 20 74 6f 6f 20 6c  are set to too l
07e0: 61 72 67 65 20 61 20 76 61 6c 75 65 2c 20 74 68  arge a value, th
07f0: 65 6e 20 61 20 62 75 66 66 65 72 0a 23 20 20 20  en a buffer.#   
0800: 6f 76 65 72 72 65 61 64 20 6d 61 79 20 6f 63 63  overread may occ
0810: 75 72 2e 20 54 68 69 73 20 74 65 73 74 20 73 63  ur. This test sc
0820: 72 69 70 74 20 63 72 65 61 74 65 73 20 61 6e 64  ript creates and
0830: 20 6f 70 65 72 61 74 65 73 20 6f 6e 20 76 61 72   operates on var
0840: 69 6f 75 73 0a 23 20 20 20 73 74 72 61 74 65 67  ious.#   strateg
0850: 69 63 61 6c 6c 79 20 63 6f 72 72 75 70 74 65 64  ically corrupted
0860: 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 73 20   database files 
0870: 74 6f 20 61 74 74 65 6d 70 74 20 74 6f 20 70 72  to attempt to pr
0880: 6f 76 6f 6b 65 20 73 75 63 68 20 62 75 66 66 65  ovoke such buffe
0890: 72 0a 23 20 20 20 6f 76 65 72 72 65 61 64 73 2e  r.#   overreads.
08a0: 0a 23 0a 23 20 20 20 56 65 72 79 20 6f 66 74 65  .#.#   Very ofte
08b0: 6e 2c 20 61 20 62 75 66 66 65 72 20 6f 76 65 72  n, a buffer over
08c0: 72 65 61 64 20 70 61 73 73 65 73 20 75 6e 6e 6f  read passes unno
08d0: 74 69 63 65 64 2c 20 70 61 72 74 69 63 75 6c 61  ticed, particula
08e0: 72 6c 79 20 69 6e 20 77 6f 72 6b 73 74 61 74 69  rly in workstati
08f0: 6f 6e 0a 23 20 20 20 65 6e 76 69 72 6f 6e 6d 65  on.#   environme
0900: 6e 74 73 2e 20 46 6f 72 20 74 68 69 73 20 72 65  nts. For this re
0910: 61 73 6f 6e 2c 20 74 68 69 73 20 74 65 73 74 20  ason, this test 
0920: 73 63 72 69 70 74 20 73 68 6f 75 6c 64 20 62 65  script should be
0930: 20 72 75 6e 20 75 73 69 6e 67 20 76 61 6c 67 72   run using valgr
0940: 69 6e 64 0a 23 20 20 20 28 6f 72 20 73 69 6d 69  ind.#   (or simi
0950: 6c 61 72 29 20 69 6e 20 6f 72 64 65 72 20 74 6f  lar) in order to
0960: 20 76 65 72 69 66 79 20 74 68 61 74 20 6e 6f 20   verify that no 
0970: 6f 76 65 72 72 65 61 64 73 20 6f 63 63 75 72 2e  overreads occur.
0980: 0a 23 0a 23 20 54 45 53 54 20 50 4c 41 4e 0a 23  .#.# TEST PLAN.#
0990: 20 0a 23 20 20 20 54 65 73 74 20 63 61 73 65 73   .#   Test cases
09a0: 20 63 6f 72 72 75 70 74 44 2d 31 2e 2a 20 61 72   corruptD-1.* ar
09b0: 65 20 77 68 69 74 65 2d 62 6f 78 20 74 65 73 74  e white-box test
09c0: 73 2e 20 54 68 65 79 20 61 74 74 65 6d 70 74 20  s. They attempt 
09d0: 74 6f 20 63 6f 72 72 75 70 74 0a 23 20 20 20 6f  to corrupt.#   o
09e0: 6e 65 20 6f 66 20 74 68 65 20 61 62 6f 76 65 20  ne of the above 
09f0: 66 69 65 6c 64 73 2c 20 74 68 65 6e 20 65 78 65  fields, then exe
0a00: 72 63 69 73 65 20 65 61 63 68 20 70 61 72 74 20  rcise each part 
0a10: 6f 66 20 74 68 65 20 63 6f 64 65 20 69 6e 20 62  of the code in b
0a20: 74 72 65 65 2e 63 0a 23 20 20 20 74 68 61 74 20  tree.c.#   that 
0a30: 75 73 65 73 20 73 61 69 64 20 66 69 65 6c 64 2e  uses said field.
0a40: 0a 23 20 20 20 0a 23 20 20 20 4f 66 66 73 65 74  .#   .#   Offset
0a50: 20 76 61 72 69 61 62 6c 65 73 20 31 2c 20 32 2c   variables 1, 2,
0a60: 20 33 20 61 6e 64 20 34 20 61 72 65 20 61 6c 6c   3 and 4 are all
0a70: 20 63 68 65 63 6b 65 64 20 74 6f 20 6d 61 6b 65   checked to make
0a80: 20 73 75 72 65 20 74 68 65 79 0a 23 20 20 20 77   sure they.#   w
0a90: 69 6c 6c 20 6e 6f 74 20 72 65 73 75 6c 74 20 69  ill not result i
0aa0: 6e 20 62 75 66 66 65 72 20 6f 76 65 72 72 75 6e  n buffer overrun
0ab0: 73 20 61 73 20 70 61 72 74 20 6f 66 20 70 61 67  s as part of pag
0ac0: 65 20 69 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e  e initialization
0ad0: 20 69 6e 0a 23 20 20 20 73 71 6c 69 74 65 33 42   in.#   sqlite3B
0ae0: 74 72 65 65 49 6e 69 74 50 61 67 65 28 29 2e 20  treeInitPage(). 
0af0: 4f 66 66 73 65 74 73 20 35 20 61 6e 64 20 36 20  Offsets 5 and 6 
0b00: 63 61 6e 6e 6f 74 20 62 65 20 74 65 73 74 65 64  cannot be tested
0b10: 20 61 73 20 70 61 72 74 20 6f 66 0a 23 20 20 20   as part of.#   
0b20: 70 61 67 65 20 69 6e 69 74 69 61 6c 69 7a 61 74  page initializat
0b30: 69 6f 6e 2c 20 61 73 20 74 72 79 69 6e 67 20 74  ion, as trying t
0b40: 6f 20 64 6f 20 73 6f 20 63 61 75 73 65 73 20 61  o do so causes a
0b50: 20 70 65 72 66 6f 72 6d 61 6e 63 65 20 68 69 74   performance hit
0b60: 2e 0a 23 0a 0a 64 6f 5f 74 65 73 74 20 63 6f 72  ..#..do_test cor
0b70: 72 75 70 74 44 2d 31 2e 30 20 7b 0a 20 20 65 78  ruptD-1.0 {.  ex
0b80: 65 63 73 71 6c 20 7b 20 0a 20 20 20 20 50 52 41  ecsql { .    PRA
0b90: 47 4d 41 20 61 75 74 6f 5f 76 61 63 75 75 6d 20  GMA auto_vacuum 
0ba0: 3d 20 30 3b 0a 20 20 20 20 50 52 41 47 4d 41 20  = 0;.    PRAGMA 
0bb0: 70 61 67 65 5f 73 69 7a 65 20 3d 20 31 30 32 34  page_size = 1024
0bc0: 3b 0a 20 20 20 20 43 52 45 41 54 45 20 54 41 42  ;.    CREATE TAB
0bd0: 4c 45 20 74 31 28 61 2c 20 62 29 3b 0a 20 20 20  LE t1(a, b);.   
0be0: 20 43 52 45 41 54 45 20 49 4e 44 45 58 20 69 31   CREATE INDEX i1
0bf0: 20 4f 4e 20 74 31 28 61 2c 20 62 29 3b 0a 20 20   ON t1(a, b);.  
0c00: 7d 0a 20 20 66 6f 72 20 7b 73 65 74 20 69 69 20  }.  for {set ii 
0c10: 31 7d 20 7b 24 69 69 20 3c 20 35 30 7d 20 7b 69  1} {$ii < 50} {i
0c20: 6e 63 72 20 69 69 7d 20 7b 0a 20 20 20 20 65 78  ncr ii} {.    ex
0c30: 65 63 73 71 6c 20 7b 20 49 4e 53 45 52 54 20 49  ecsql { INSERT I
0c40: 4e 54 4f 20 74 31 20 56 41 4c 55 45 53 28 24 69  NTO t1 VALUES($i
0c50: 69 2c 20 24 69 69 20 2a 20 24 69 69 29 20 7d 0a  i, $ii * $ii) }.
0c60: 20 20 7d 0a 20 20 65 78 65 63 73 71 6c 20 7b 0a    }.  execsql {.
0c70: 20 20 20 20 44 45 4c 45 54 45 20 46 52 4f 4d 20      DELETE FROM 
0c80: 74 31 20 57 48 45 52 45 20 61 20 3d 20 31 30 3b  t1 WHERE a = 10;
0c90: 0a 20 20 20 20 44 45 4c 45 54 45 20 46 52 4f 4d  .    DELETE FROM
0ca0: 20 74 31 20 57 48 45 52 45 20 61 20 3d 20 32 30   t1 WHERE a = 20
0cb0: 3b 0a 20 20 20 20 44 45 4c 45 54 45 20 46 52 4f  ;.    DELETE FRO
0cc0: 4d 20 74 31 20 57 48 45 52 45 20 61 20 3d 20 33  M t1 WHERE a = 3
0cd0: 30 3b 0a 20 20 20 20 44 45 4c 45 54 45 20 46 52  0;.    DELETE FR
0ce0: 4f 4d 20 74 31 20 57 48 45 52 45 20 61 20 3d 20  OM t1 WHERE a = 
0cf0: 34 30 3b 0a 20 20 7d 0a 20 20 66 6f 72 63 65 63  40;.  }.  forcec
0d00: 6f 70 79 20 74 65 73 74 2e 64 62 20 74 65 73 74  opy test.db test
0d10: 2e 62 75 0a 7d 20 7b 7d 0a 0a 70 72 6f 63 20 69  .bu.} {}..proc i
0d20: 6e 63 72 5f 63 68 61 6e 67 65 5f 63 6f 75 6e 74  ncr_change_count
0d30: 65 72 20 7b 7d 20 7b 0a 20 20 68 65 78 69 6f 5f  er {} {.  hexio_
0d40: 77 72 69 74 65 20 74 65 73 74 2e 64 62 20 32 34  write test.db 24
0d50: 20 5b 0a 20 20 20 20 68 65 78 69 6f 5f 72 65 6e   [.    hexio_ren
0d60: 64 65 72 5f 69 6e 74 33 32 20 5b 65 78 70 72 20  der_int32 [expr 
0d70: 5b 68 65 78 69 6f 5f 67 65 74 5f 69 6e 74 20 5b  [hexio_get_int [
0d80: 68 65 78 69 6f 5f 72 65 61 64 20 74 65 73 74 2e  hexio_read test.
0d90: 64 62 20 32 34 20 34 5d 5d 20 2b 20 31 5d 0a 20  db 24 4]] + 1]. 
0da0: 20 5d 0a 7d 0a 0a 70 72 6f 63 20 72 65 73 74 6f   ].}..proc resto
0db0: 72 65 5f 66 69 6c 65 20 7b 7d 20 7b 0a 20 20 64  re_file {} {.  d
0dc0: 62 20 63 6c 6f 73 65 0a 20 20 66 6f 72 63 65 63  b close.  forcec
0dd0: 6f 70 79 20 74 65 73 74 2e 62 75 20 74 65 73 74  opy test.bu test
0de0: 2e 64 62 0a 20 20 73 71 6c 69 74 65 33 20 64 62  .db.  sqlite3 db
0df0: 20 74 65 73 74 2e 64 62 0a 7d 0a 0a 23 2d 2d 2d   test.db.}..#---
0e00: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
0e10: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
0e20: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
0e30: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
0e40: 2d 2d 2d 2d 2d 2d 0a 23 20 54 68 65 20 66 6f 6c  ------.# The fol
0e50: 6c 6f 77 69 6e 67 20 74 65 73 74 73 2c 20 63 6f  lowing tests, co
0e60: 72 72 75 70 74 44 2d 31 2e 31 2e 2a 2c 20 66 6f  rruptD-1.1.*, fo
0e70: 63 75 73 20 6f 6e 20 74 68 65 20 70 61 67 65 20  cus on the page 
0e80: 68 65 61 64 65 72 20 66 69 65 6c 64 0a 23 20 63  header field.# c
0e90: 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 6f 66  ontaining the of
0ea0: 66 73 65 74 20 6f 66 20 74 68 65 20 66 69 72 73  fset of the firs
0eb0: 74 20 66 72 65 65 20 62 6c 6f 63 6b 20 69 6e 20  t free block in 
0ec0: 61 20 70 61 67 65 2e 20 0a 23 0a 64 6f 5f 74 65  a page. .#.do_te
0ed0: 73 74 20 63 6f 72 72 75 70 74 44 2d 31 2e 31 2e  st corruptD-1.1.
0ee0: 31 20 7b 0a 20 20 69 6e 63 72 5f 63 68 61 6e 67  1 {.  incr_chang
0ef0: 65 5f 63 6f 75 6e 74 65 72 0a 20 20 68 65 78 69  e_counter.  hexi
0f00: 6f 5f 77 72 69 74 65 20 74 65 73 74 2e 64 62 20  o_write test.db 
0f10: 5b 65 78 70 72 20 31 30 32 34 2b 31 5d 20 46 46  [expr 1024+1] FF
0f20: 46 46 0a 20 20 63 61 74 63 68 73 71 6c 20 7b 20  FF.  catchsql { 
0f30: 50 52 41 47 4d 41 20 71 75 69 63 6b 5f 63 68 65  PRAGMA quick_che
0f40: 63 6b 20 7d 0a 7d 20 7b 30 20 7b 7b 2a 2a 2a 20  ck }.} {0 {{*** 
0f50: 69 6e 20 64 61 74 61 62 61 73 65 20 6d 61 69 6e  in database main
0f60: 20 2a 2a 2a 0a 50 61 67 65 20 32 3a 20 66 72 65   ***.Page 2: fre
0f70: 65 20 73 70 61 63 65 20 63 6f 72 72 75 70 74 69  e space corrupti
0f80: 6f 6e 7d 7d 7d 0a 64 6f 5f 74 65 73 74 20 63 6f  on}}}.do_test co
0f90: 72 72 75 70 74 44 2d 31 2e 31 2e 32 20 7b 0a 20  rruptD-1.1.2 {. 
0fa0: 20 69 6e 63 72 5f 63 68 61 6e 67 65 5f 63 6f 75   incr_change_cou
0fb0: 6e 74 65 72 0a 20 20 68 65 78 69 6f 5f 77 72 69  nter.  hexio_wri
0fc0: 74 65 20 74 65 73 74 2e 64 62 20 5b 65 78 70 72  te test.db [expr
0fd0: 20 31 30 32 34 2b 31 5d 20 5b 68 65 78 69 6f 5f   1024+1] [hexio_
0fe0: 72 65 6e 64 65 72 5f 69 6e 74 33 32 20 31 30 32  render_int32 102
0ff0: 31 5d 0a 20 20 63 61 74 63 68 73 71 6c 20 7b 20  1].  catchsql { 
1000: 53 45 4c 45 43 54 20 2a 20 46 52 4f 4d 20 74 31  SELECT * FROM t1
1010: 20 4f 52 44 45 52 20 42 59 20 72 6f 77 69 64 20   ORDER BY rowid 
1020: 7d 0a 7d 20 7b 31 20 7b 64 61 74 61 62 61 73 65  }.} {1 {database
1030: 20 64 69 73 6b 20 69 6d 61 67 65 20 69 73 20 6d   disk image is m
1040: 61 6c 66 6f 72 6d 65 64 7d 7d 0a 0a 23 2d 2d 2d  alformed}}..#---
1050: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1060: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1070: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1080: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1090: 2d 2d 2d 2d 2d 2d 0a 23 20 54 68 65 20 66 6f 6c  ------.# The fol
10a0: 6c 6f 77 69 6e 67 20 74 65 73 74 73 2c 20 63 6f  lowing tests, co
10b0: 72 72 75 70 74 44 2d 31 2e 32 2e 2a 2c 20 66 6f  rruptD-1.2.*, fo
10c0: 63 75 73 20 6f 6e 20 74 68 65 20 6f 66 66 73 65  cus on the offse
10d0: 74 73 20 63 6f 6e 74 61 69 6e 65 64 0a 23 20 69  ts contained.# i
10e0: 6e 20 74 68 65 20 66 69 72 73 74 20 32 20 62 79  n the first 2 by
10f0: 74 65 20 6f 66 20 65 61 63 68 20 66 72 65 65 2d  te of each free-
1100: 62 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 66 72 65  block on the fre
1110: 65 2d 6c 69 73 74 2e 0a 23 0a 64 6f 5f 74 65 73  e-list..#.do_tes
1120: 74 20 63 6f 72 72 75 70 74 44 2d 31 2e 32 2e 31  t corruptD-1.2.1
1130: 20 7b 0a 20 20 72 65 73 74 6f 72 65 5f 66 69 6c   {.  restore_fil
1140: 65 0a 7d 20 7b 7d 0a 64 6f 5f 74 65 73 74 20 63  e.} {}.do_test c
1150: 6f 72 72 75 70 74 44 2d 31 2e 32 2e 32 20 7b 0a  orruptD-1.2.2 {.
1160: 7d 20 7b 7d 0a 0a 23 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 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
11b0: 0a 23 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  .# The following
11c0: 20 74 65 73 74 73 2c 20 63 6f 72 72 75 70 74 44   tests, corruptD
11d0: 2d 31 2e 34 2e 2a 2c 20 2e 2e 2e 0a 23 0a 0a 0a  -1.4.*, ....#...
11e0: 23 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  #---------------
11f0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1200: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1210: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1220: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 23 20 54 68 65  ----------.# The
1230: 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 65 73 74 73   following tests
1240: 2c 20 63 6f 72 72 75 70 74 44 2d 31 2e 35 2e 2a  , corruptD-1.5.*
1250: 2c 20 66 6f 63 75 73 20 6f 6e 20 74 68 65 20 6f  , focus on the o
1260: 66 66 73 65 74 73 20 63 6f 6e 74 61 69 6e 65 64  ffsets contained
1270: 0a 23 20 69 6e 20 74 68 65 20 63 65 6c 6c 20 6f  .# in the cell o
1280: 66 66 73 65 74 20 61 72 72 61 79 2e 0a 23 20 0a  ffset array..# .
1290: 23 20 20 20 64 65 66 72 61 67 6d 65 6e 74 50 61  #   defragmentPa
12a0: 67 65 0a 23 0a 0a 66 69 6e 69 73 68 5f 74 65 73  ge.#..finish_tes
12b0: 74 0a                                            t.