xref: /sqlite-3.40.0/test/instrfault.test (revision a369d98b)
1# 2016 November 4
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.  The
12# focus of this file is testing OOM error handling within the built-in
13# INSTR() function.
14#
15
16
17set testdir [file dirname $argv0]
18source $testdir/tester.tcl
19set testprefix instrfault
20
21# Use big NEEDLE and HAYSTACK strings. Strings so large they cannot
22# use lookaside buffers.
23#
24set ::NEEDLE [string repeat "abcdefghijklmnopqrstuvwxyz" 10]
25set ::HAYSTACK "[string repeat 123 10]$NEEDLE[string repeat 456 10]"
26
27foreach {enc} {
28  utf8
29  utf16
30} {
31  reset_db
32  sqlite3_db_config_lookaside db 0 0 0
33
34  execsql "PRAGMA encoding = $enc"
35  do_execsql_test 1.$enc.1 {
36    CREATE TABLE t1(n, h);
37    INSERT INTO t1 VALUES($::NEEDLE, $::HAYSTACK);
38  } {}
39
40  do_faultsim_test 1.$enc.1 -faults oom-t* -prep {
41    execsql { SELECT instr(h, n) FROM t1 }
42  } -body {
43    execsql { SELECT instr(h, n) FROM t1 }
44  } -test {
45    faultsim_test_result {0 31}
46  }
47
48  do_faultsim_test 1.$enc.2 -faults oom-t* -prep {
49    execsql { SELECT instr($::HAYSTACK, $::NEEDLE) FROM t1 }
50  } -body {
51    execsql { SELECT instr($::HAYSTACK, $::NEEDLE) FROM t1 }
52  } -test {
53    faultsim_test_result {0 31}
54  }
55
56  do_faultsim_test 1.$enc.3 -faults oom-t* -prep {
57    set ::stmt [sqlite3_prepare_v2 db "SELECT instr(?, ?)" -1 dummy]
58    sqlite3_bind_text $::stmt 1 $::HAYSTACK [string length $::HAYSTACK]
59    sqlite3_bind_text $::stmt 2 $::NEEDLE [string length $::NEEDLE]
60  } -body {
61    set rc [sqlite3_step $::stmt]
62    if {$rc=="SQLITE_NOMEM"} { error "out of memory" }
63    sqlite3_column_int $::stmt 0
64  } -test {
65    faultsim_test_result {0 31}
66    sqlite3_finalize $::stmt
67  }
68
69  do_faultsim_test 1.$enc.4 -faults oom-t* -prep {
70    set ::stmt [sqlite3_prepare_v2 db "SELECT instr(?, ?)" -1 dummy]
71    sqlite3_bind_blob $::stmt 1 $::HAYSTACK [string length $::HAYSTACK]
72    sqlite3_bind_blob $::stmt 2 $::NEEDLE [string length $::NEEDLE]
73  } -body {
74    set rc [sqlite3_step $::stmt]
75    if {$rc=="SQLITE_NOMEM"} { error "out of memory" }
76    sqlite3_column_int $::stmt 0
77  } -test {
78    faultsim_test_result {0 31}
79    sqlite3_finalize $::stmt
80  }
81
82  do_execsql_test 1.$enc.5.0 {
83    CREATE TABLE h1(a, b);
84    INSERT INTO h1 VALUES('abcdefg%200hijkl', randomblob(200));
85    INSERT INTO h1 SELECT b, a FROM h1;
86  }
87  do_faultsim_test 1.$enc.5 -faults oom-t* -body {
88    execsql { SELECT rowid FROM h1 WHERE instr(a,b) }
89  } -test {}
90}
91
92finish_test
93