corrupt4.test 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. # 2007 Sept 7
  2. #
  3. # The author disclaims copyright to this source code. In place of
  4. # a legal notice, here is a blessing:
  5. #
  6. # May you do good and not evil.
  7. # May you find forgiveness for yourself and forgive others.
  8. # May you share freely, never taking more than you give.
  9. #
  10. #***********************************************************************
  11. # This file implements regression tests for SQLite library.
  12. #
  13. # This file implements tests to make sure SQLite does not crash or
  14. # segfault if it sees a corrupt database file.
  15. #
  16. # $Id: corrupt4.test,v 1.1 2007/09/07 14:32:07 drh Exp $
  17. set testdir [file dirname $argv0]
  18. source $testdir/tester.tcl
  19. # Do not use a codec for tests in this file, as the database file is
  20. # manipulated directly using tcl scripts (using the [hexio_write] command).
  21. #
  22. do_not_use_codec
  23. # We must have the page_size pragma for these tests to work.
  24. #
  25. ifcapable !pager_pragmas {
  26. finish_test
  27. return
  28. }
  29. # Create a database with a freelist containing at least two pages.
  30. #
  31. do_test corrupt4-1.1 {
  32. set bigstring [string repeat 0123456789 200]
  33. execsql {
  34. PRAGMA auto_vacuum=OFF;
  35. PRAGMA page_size=1024;
  36. CREATE TABLE t1(x);
  37. INSERT INTO t1 VALUES($bigstring);
  38. CREATE TABLE t2(y);
  39. INSERT INTO t2 VALUES(1);
  40. DROP TABLE t1;
  41. }
  42. file size test.db
  43. } [expr {1024*4}]
  44. # Verify that there are two pages on the freelist.
  45. #
  46. do_test corrupt4-1.2 {
  47. execsql {PRAGMA freelist_count}
  48. } {2}
  49. # Get the page number for the trunk of the freelist.
  50. #
  51. set trunkpgno [hexio_get_int [hexio_read test.db 32 4]]
  52. set baseaddr [expr {($trunkpgno-1)*1024}]
  53. # Verify that the trunk of the freelist has exactly one
  54. # leaf.
  55. #
  56. do_test corrupt4-1.3 {
  57. hexio_get_int [hexio_read test.db [expr {$::baseaddr+4}] 4]
  58. } {1}
  59. # Insert a negative number as the number of leaves on the trunk.
  60. # Then try to add a new element to the freelist.
  61. #
  62. do_test corrupt4-1.4 {
  63. hexio_write test.db [expr {$::baseaddr+4}] [hexio_render_int32 -100000000]
  64. db close
  65. sqlite3 db test.db
  66. catchsql {
  67. DROP TABLE t2
  68. }
  69. } {1 {database disk image is malformed}}
  70. finish_test