blob: 848e46e4c2f6fb752c16c5c3087fd77d3d745918 [file] [log] [blame]
dana2bfa042016-11-04 12:05:29 +00001# 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
dan3043b532016-12-30 17:40:14 +000032 sqlite3_db_config_lookaside db 0 0 0
33
dana2bfa042016-11-04 12:05:29 +000034 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 }
dan3043b532016-12-30 17:40:14 +000068
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]
dana369d982020-01-17 15:45:59 +000072 sqlite3_bind_blob $::stmt 2 $::NEEDLE [string length $::NEEDLE]
dan3043b532016-12-30 17:40:14 +000073 } -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 {}
dana2bfa042016-11-04 12:05:29 +000090}
91
92finish_test