xref: /sqlite-3.40.0/test/mutex2.test (revision 18472fa7)
1c8d75674Sdrh# 2008 July 7
2c8d75674Sdrh#
3c8d75674Sdrh# The author disclaims copyright to this source code.  In place of
4c8d75674Sdrh# a legal notice, here is a blessing:
5c8d75674Sdrh#
6c8d75674Sdrh#    May you do good and not evil.
7c8d75674Sdrh#    May you find forgiveness for yourself and forgive others.
8c8d75674Sdrh#    May you share freely, never taking more than you give.
9c8d75674Sdrh#
10c8d75674Sdrh#***********************************************************************
11c8d75674Sdrh#
12c8d75674Sdrh# Test scripts for deliberate failures of mutex routines.
13c8d75674Sdrh#
14*18472fa7Sdrh# $Id: mutex2.test,v 1.9 2008/10/07 15:25:49 drh Exp $
15c8d75674Sdrh
16c8d75674Sdrhset testdir [file dirname $argv0]
17c8d75674Sdrhsource $testdir/tester.tcl
18*18472fa7Sdrhifcapable !mutex {
19*18472fa7Sdrh  finish_test
20*18472fa7Sdrh  return
21*18472fa7Sdrh}
22c8d75674Sdrh
23c8d75674Sdrh# deinitialize
24c8d75674Sdrh#
25c8d75674Sdrhcatch {db close}
26c8d75674Sdrhsqlite3_reset_auto_extension
27c8d75674Sdrhsqlite3_shutdown
28c8d75674Sdrhinstall_mutex_counters 1
29c8d75674Sdrh
30c8d75674Sdrh# Fix the mutex subsystem so that it will not initialize.  In other words,
31c8d75674Sdrh# make it so that sqlite3_initialize() always fails.
32c8d75674Sdrh#
33c8d75674Sdrhdo_test mutex2-1.1 {
34c8d75674Sdrh  set ::disable_mutex_init 10
35c8d75674Sdrh  sqlite3_initialize
36c8d75674Sdrh} {SQLITE_IOERR}
37c8d75674Sdrhdo_test mutex2-1.1 {
38c8d75674Sdrh  set ::disable_mutex_init 7
39c8d75674Sdrh  sqlite3_initialize
40c8d75674Sdrh} {SQLITE_NOMEM}
41c8d75674Sdrh
42c8d75674Sdrhproc utf16 {str} {
43c8d75674Sdrh  set r [encoding convertto unicode $str]
44c8d75674Sdrh  append r "\x00\x00"
45c8d75674Sdrh  return $r
46c8d75674Sdrh}
47c8d75674Sdrh
48c8d75674Sdrh# Now that sqlite3_initialize() is failing, try to run various APIs that
49c8d75674Sdrh# require that SQLite be initialized.  Verify that they fail.
50c8d75674Sdrh#
51c8d75674Sdrhdo_test mutex2-2.1 {
52c8d75674Sdrh  set ::disable_mutex_init 7
53c8d75674Sdrh  set rc [catch {sqlite db test.db} msg]
54c8d75674Sdrh  lappend rc $msg
55ff1590eeSdrh} {1 {}}
56c8d75674Sdrhifcapable utf16 {
57c8d75674Sdrh  do_test mutex2-2.2 {
58c8d75674Sdrh    set db2 [sqlite3_open16 [utf16 test.db] {}]
59c8d75674Sdrh  } {0}
60c8d75674Sdrh  do_test mutex2-2.3 {
61c8d75674Sdrh    sqlite3_complete16 [utf16 {SELECT * FROM t1;}]
62c8d75674Sdrh  } {7}
63c8d75674Sdrh}
64c8d75674Sdrhdo_test mutex2-2.4 {
65c8d75674Sdrh  sqlite3_mprintf_int {This is a test %d,%d,%d} 1 2 3
66ff1590eeSdrh} {}
677830cd41Sdrhifcapable load_ext {
68c8d75674Sdrh  do_test mutex2-2.5 {
69c8d75674Sdrh    sqlite3_auto_extension_sqr
70c8d75674Sdrh  } {7}
717830cd41Sdrh}
72c8d75674Sdrhdo_test mutex2-2.6 {
73c8d75674Sdrh  sqlite3_reset_auto_extension
74c8d75674Sdrh} {}
75c8d75674Sdrhdo_test mutex2-2.7 {
76c8d75674Sdrh  sqlite3_malloc 10000
778278ce79Sdrh} {0}
78c8d75674Sdrhdo_test mutex2-2.8 {
79c8d75674Sdrh  sqlite3_realloc 0 10000
808278ce79Sdrh} {0}
81b27475baSdanielk1977ifcapable threadsafe {
82c8d75674Sdrh  do_test mutex2-2.9 {
83c8d75674Sdrh    alloc_dealloc_mutex
84c8d75674Sdrh  } {0}
85b27475baSdanielk1977}
86c8d75674Sdrhdo_test mutex2-2.10 {
87c8d75674Sdrh  vfs_initfail_test
88c8d75674Sdrh} {}
89c8d75674Sdrh
90c8d75674Sdrh# Restore the system to a functional state
91c8d75674Sdrh#
92c8d75674Sdrhinstall_mutex_counters 0
93c8d75674Sdrhset disable_mutex_init 0
94c8d75674Sdrhautoinstall_test_functions
958a42cbd3Sdrh
968a42cbd3Sdrh# Mutex allocation works now.
978a42cbd3Sdrh#
988a42cbd3Sdrh
998a42cbd3Sdrhdo_test mutex2-3.1 {
1008a42cbd3Sdrh  set ptr [alloc_dealloc_mutex]
1018a42cbd3Sdrh  expr {$ptr!=0}
1028a42cbd3Sdrh} {1}
1038a42cbd3Sdrh
1048a42cbd3Sdrh
105c8d75674Sdrhfinish_test
106