xref: /sqlite-3.40.0/test/misc7.test (revision f8a78464)
1# 2006 September 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.
12#
13# $Id: misc7.test,v 1.29 2009/07/16 18:21:18 drh Exp $
14
15set testdir [file dirname $argv0]
16source $testdir/tester.tcl
17
18do_test misc7-1-misuse {
19  c_misuse_test
20} {}
21
22do_test misc7-2 {
23  c_realloc_test
24} {}
25
26do_test misc7-3 {
27  c_collation_test
28} {}
29
30# Try to open a directory:
31#
32do_test misc7-4 {
33  delete_file mydir
34  file mkdir mydir
35  set rc [catch {
36    sqlite3 db2 ./mydir
37  } msg]
38  list $rc $msg
39} {1 {unable to open database file}}
40
41# Try to open a file with a directory where its journal file should be.
42#
43do_test misc7-5 {
44  delete_file mydir
45  file mkdir mydir-journal
46  sqlite3 db2 ./mydir
47  catchsql {
48    CREATE TABLE abc(a, b, c);
49  } db2
50} {1 {unable to open database file}}
51db2 close
52
53#--------------------------------------------------------------------
54# The following tests, misc7-6.* test the libraries behaviour when
55# it cannot open a file. To force this condition, we use up all the
56# file-descriptors before running sqlite. This probably only works
57# on unix.
58#
59
60proc use_up_files {} {
61  set ret [list]
62  catch {
63    while 1 { lappend ret [open test.db] }
64  }
65  return $ret
66}
67
68proc do_fileopen_test {prefix sql} {
69  set fd_list [use_up_files]
70  set ::go 1
71  set ::n 1
72  set ::sql $sql
73  while {$::go} {
74    catch {db close}
75    do_test ${prefix}.${::n} {
76      set rc [catch {
77        sqlite db test.db
78        db eval $::sql
79      } msg]
80      if {$rc == 0} {set ::go 0}
81
82      expr {$rc == 0 || ($rc == 1 && [string first unable $msg]==0)}
83    } 1
84
85    close [lindex $fd_list 0]
86    set fd_list [lrange $fd_list 1 end]
87    incr ::n
88  }
89  foreach fd $fd_list {
90    close $fd
91  }
92  db close
93}
94
95execsql { CREATE TABLE abc(a PRIMARY KEY, b, c); }
96db close
97
98if {$tcl_platform(platform)!="windows"} {
99  do_fileopen_test misc7-6.1 {
100    BEGIN;
101    INSERT INTO abc VALUES(1, 2, 3);
102    INSERT INTO abc VALUES(2, 3, 4);
103    INSERT INTO abc SELECT a+2, b, c FROM abc;
104    COMMIT;
105  }
106
107  do_fileopen_test misc7-6.2 {
108    PRAGMA temp.cache_size = 1000;
109  }
110}
111
112#
113# End of tests for out-of-file-descriptors condition.
114#--------------------------------------------------------------------
115
116sqlite3 db test.db
117execsql {
118  DELETE FROM abc;
119  INSERT INTO abc VALUES(1, 2, 3);
120  INSERT INTO abc VALUES(2, 3, 4);
121  INSERT INTO abc SELECT a+2, b, c FROM abc;
122}
123
124
125#--------------------------------------------------------------------
126# Test that the sqlite3_busy_timeout call seems to delay approximately
127# the right amount of time.
128#
129do_test misc7-7.0 {
130  sqlite3 db2 test.db
131  sqlite3_busy_timeout [sqlite3_connection_pointer db] 2000
132  execsql {
133    BEGIN EXCLUSIVE;
134  } db2
135
136  # Now db2 has an exclusive lock on the database file, and db has
137  # a busy-timeout of 2000 milliseconds. So check that trying to
138  # access the database using connection db delays for at least 1500 ms.
139  #
140  set tm [time {
141    set result [catchsql {
142        SELECT * FROM sqlite_master;
143      } db]
144  }]
145  set delay [lindex $tm 0]  ;# In microseconds
146  lappend result [expr {$delay>1500000 && $delay<4000000}]
147} {1 {database is locked} 1}
148db2 close
149
150#--------------------------------------------------------------------
151# Test that nothing goes horribly wrong when attaching a database
152# after the omit_readlock pragma has been exercised.
153#
154# Note:  The PRAGMA omit_readlock was an early hack to disable the
155# fcntl() calls for read-only databases so that read-only databases could
156# be read on broken NFS systems.  That pragma has now been removed.
157# (Use the unix-none VFS as a replacement, if needed.)  But these tests
158# do not really depend on omit_readlock, so we left them in place.
159#
160do_test misc7-7.1 {
161  forcedelete test2.db
162  forcedelete test2.db-journal
163  execsql {
164    PRAGMA omit_readlock = 1;
165    ATTACH 'test2.db' AS aux;
166    CREATE TABLE aux.hello(world);
167    SELECT name FROM aux.sqlite_master;
168  }
169} {hello}
170do_test misc7-7.2 {
171  execsql {
172    DETACH aux;
173  }
174} {}
175do_test misc7-7.3 {
176  db close
177  sqlite3 db test.db -readonly 1
178  execsql {
179    PRAGMA omit_readlock = 1;
180    ATTACH 'test2.db' AS aux;
181    SELECT name FROM aux.sqlite_master;
182    SELECT name FROM aux.sqlite_master;
183  }
184} {hello hello}
185do_test misc7-7.3 {
186  db close
187  sqlite3 db test.db
188  set ::DB [sqlite3_connection_pointer db]
189  list
190} {}
191
192# Test the UTF-16 version of the "out of memory" message (used when
193# malloc fails during sqlite3_open() ).
194#
195ifcapable utf16 {
196  do_test misc7-8 {
197    encoding convertfrom unicode [sqlite3_errmsg16 0x00000000]
198  } {out of memory}
199}
200
201do_test misc7-9 {
202  execsql {
203    SELECT *
204    FROM (SELECT name+1 AS one FROM sqlite_master LIMIT 1 OFFSET 1)
205    WHERE one LIKE 'hello%';
206  }
207} {}
208
209#--------------------------------------------------------------------
210# Improve coverage for vtab code.
211#
212ifcapable vtab {
213  # Run some debug code to improve reported coverage
214  #
215
216  # set sqlite_where_trace 1
217  do_test misc7-10 {
218    register_echo_module [sqlite3_connection_pointer db]
219    execsql {
220      CREATE VIRTUAL TABLE t1 USING echo(abc);
221      SELECT a FROM t1 WHERE a = 1 ORDER BY b;
222    }
223  } {1}
224  set sqlite_where_trace 0
225
226  # Specify an ORDER BY clause that cannot be indexed.
227  do_test misc7-11 {
228    execsql {
229      SELECT t1.a, t2.a FROM t1, t1 AS t2 ORDER BY 2 LIMIT 1;
230    }
231  } {1 1}
232
233  # The whole point of this is to test an error code other than
234  # SQLITE_NOMEM from the vtab xBestIndex callback.
235  #
236  do_ioerr_test misc7-12 -tclprep {
237    sqlite3 db2 test.db
238    register_echo_module [sqlite3_connection_pointer db2]
239    db2 eval {
240      CREATE TABLE abc(a PRIMARY KEY, b, c);
241      INSERT INTO abc VALUES(1, 2, 3);
242      CREATE VIRTUAL TABLE t1 USING echo(abc);
243    }
244    db2 close
245  } -tclbody {
246    register_echo_module [sqlite3_connection_pointer db]
247    execsql {SELECT * FROM t1 WHERE a = 1;}
248  }
249
250  # The case where the virtual table module returns a very large number
251  # as the cost of a scan (greater than SQLITE_BIG_DOUBLE in the code).
252  #
253  do_test misc7-13 {
254    sqlite3 db test.db
255    register_echo_module [sqlite3_connection_pointer db]
256    set ::echo_module_cost 2.0e+99
257    execsql {SELECT * FROM t1 WHERE a = 1;}
258  } {1 2 3}
259  unset ::echo_module_cost
260}
261
262db close
263forcedelete test.db
264forcedelete test.db-journal
265sqlite3 db test.db
266
267ifcapable explain {
268  do_execsql_test misc7-14.1 {
269    CREATE TABLE abc(a PRIMARY KEY, b, c);
270    EXPLAIN QUERY PLAN SELECT * FROM abc AS t2 WHERE rowid = 1;
271  } {
272    0 0 0 {SEARCH TABLE abc AS t2 USING INTEGER PRIMARY KEY (rowid=?) (~1 rows)}
273  }
274  do_execsql_test misc7-14.2 {
275    EXPLAIN QUERY PLAN SELECT * FROM abc AS t2 WHERE a = 1;
276  } {0 0 0
277     {SEARCH TABLE abc AS t2 USING INDEX sqlite_autoindex_abc_1 (a=?) (~1 rows)}
278  }
279  do_execsql_test misc7-14.3 {
280    EXPLAIN QUERY PLAN SELECT * FROM abc AS t2 ORDER BY a;
281  } {0 0 0
282     {SCAN TABLE abc AS t2 USING INDEX sqlite_autoindex_abc_1 (~1000000 rows)}
283  }
284}
285
286db close
287forcedelete test.db
288forcedelete test.db-journal
289sqlite3 db test.db
290
291#--------------------------------------------------------------------
292# This is all to force the pager_remove_from_stmt_list() function
293# (inside pager.c) to remove a pager from the middle of the
294# statement-list.
295#
296do_test misc7-15.1 {
297  execsql {
298    PRAGMA cache_size = 10;
299    BEGIN;
300    CREATE TABLE abc(a PRIMARY KEY, b, c);
301    INSERT INTO abc
302    VALUES(randstr(100,100), randstr(100,100), randstr(100,100));
303    INSERT INTO abc SELECT
304            randstr(100,100), randstr(100,100), randstr(100,100) FROM abc;
305    INSERT INTO abc SELECT
306            randstr(100,100), randstr(100,100), randstr(100,100) FROM abc;
307    INSERT INTO abc SELECT
308            randstr(100,100), randstr(100,100), randstr(100,100) FROM abc;
309    INSERT INTO abc SELECT
310            randstr(100,100), randstr(100,100), randstr(100,100) FROM abc;
311    INSERT INTO abc SELECT
312            randstr(100,100), randstr(100,100), randstr(100,100) FROM abc;
313    INSERT INTO abc SELECT
314            randstr(100,100), randstr(100,100), randstr(100,100) FROM abc;
315    INSERT INTO abc SELECT
316            randstr(100,100), randstr(100,100), randstr(100,100) FROM abc;
317    INSERT INTO abc SELECT
318            randstr(100,100), randstr(100,100), randstr(100,100) FROM abc;
319    COMMIT;
320  }
321  expr {[file size test.db]>10240}
322} {1}
323do_test misc7-15.2 {
324  execsql {
325    DELETE FROM abc WHERE rowid > 12;
326    INSERT INTO abc SELECT
327            randstr(100,100), randstr(100,100), randstr(100,100) FROM abc;
328  }
329} {}
330
331db close
332forcedelete test.db
333forcedelete test.db-journal
334sqlite3 db test.db
335
336do_ioerr_test misc7-16 -sqlprep {
337   PRAGMA cache_size = 10;
338   PRAGMA default_cache_size = 10;
339   CREATE TABLE t3(a, b, UNIQUE(a, b));
340   INSERT INTO t3 VALUES( randstr(100, 100), randstr(100, 100) );
341   INSERT INTO t3 SELECT randstr(100, 100), randstr(100, 100) FROM t3;
342   INSERT INTO t3 SELECT randstr(100, 100), randstr(100, 100) FROM t3;
343   INSERT INTO t3 SELECT randstr(100, 100), randstr(100, 100) FROM t3;
344   INSERT INTO t3 SELECT randstr(100, 100), randstr(100, 100) FROM t3;
345   INSERT INTO t3 SELECT randstr(100, 100), randstr(100, 100) FROM t3;
346   UPDATE t3
347   SET b = 'hello world'
348   WHERE rowid >= (SELECT max(rowid)-1 FROM t3);
349} -tclbody {
350  set rc [catch {db eval {
351    BEGIN;
352      PRAGMA cache_size = 10;
353      INSERT INTO t3 VALUES( randstr(100, 100), randstr(100, 100) );
354      UPDATE t3 SET a = b;
355    COMMIT;
356  }} msg]
357
358  if {!$rc || ($rc && [string first "columns" $msg]==0)} {
359    set msg
360  } else {
361    error $msg
362  }
363}
364
365sqlite3 db test.db
366
367do_test misc7-16.X {
368  execsql {
369    SELECT count(*) FROM t3;
370  }
371} {32}
372
373#----------------------------------------------------------------------
374# Test the situation where a hot-journal is discovered but write-access
375# to it is denied. This should return SQLITE_BUSY.
376#
377# These tests do not work on windows due to restrictions in the
378# windows file system.
379#
380if {$tcl_platform(platform)!="windows" && $tcl_platform(platform)!="os2"} {
381
382  # Some network filesystems (ex: AFP) do not support setting read-only
383  # permissions.  Only run these tests if full unix permission setting
384  # capabilities are supported.
385  #
386  file attributes test.db -permissions rw-r--r--
387  if {[file attributes test.db -permissions]==0644} {
388
389    do_test misc7-17.1 {
390      execsql {
391        BEGIN;
392        DELETE FROM t3 WHERE (oid%3)==0;
393      }
394      forcecopy test.db bak.db
395      forcecopy test.db-journal bak.db-journal
396      execsql {
397        COMMIT;
398      }
399
400      db close
401      forcecopy bak.db test.db
402      forcecopy bak.db-journal test.db-journal
403      sqlite3 db test.db
404
405      catch {file attributes test.db-journal -permissions r--------}
406      catch {file attributes test.db-journal -readonly 1}
407      catchsql {
408        SELECT count(*) FROM t3;
409      }
410    } {1 {unable to open database file}}
411    do_test misc7-17.2 {
412      # Note that the -readonly flag must be cleared before the -permissions
413      # are set. Otherwise, when using tcl 8.5 on mac, the fact that the
414      # -readonly flag is set causes the attempt to set the permissions
415      # to fail.
416      catch {file attributes test.db-journal -readonly 0}
417      catch {file attributes test.db-journal -permissions rw-------}
418      catchsql {
419        SELECT count(*) FROM t3;
420      }
421    } {0 32}
422
423    # sqlite3_test_control_pending_page [expr ($::sqlite_pending_byte / 1024) + 1]
424    set ::pending_byte_page [expr ($::sqlite_pending_byte / 1024) + 1]
425    sqlite3_test_control_pending_byte $::sqlite_pending_byte
426    do_test misc7-17.3 {
427      db eval {
428        pragma writable_schema = true;
429        UPDATE sqlite_master
430          SET rootpage = $pending_byte_page
431          WHERE type = 'table' AND name = 't3';
432      }
433      execsql {
434        SELECT rootpage FROM sqlite_master WHERE type = 'table' AND name = 't3';
435      }
436    } $::pending_byte_page
437
438    do_test misc7-17.4 {
439      db close
440      sqlite3 db test.db
441      catchsql {
442        SELECT count(*) FROM t3;
443      }
444    } {1 {database disk image is malformed}}
445  }
446}
447
448# Ticket #2470
449#
450do_test misc7-18.1 {
451  execsql {
452    CREATE TABLE table_1 (col_10);
453    CREATE TABLE table_2 (
454      col_1, col_2, col_3, col_4, col_5,
455      col_6, col_7, col_8, col_9, col_10
456    );
457    SELECT a.col_10
458    FROM
459      (SELECT table_1.col_10 AS col_10 FROM table_1) a,
460      (SELECT table_1.col_10, table_2.col_9 AS qcol_9
461         FROM table_1, table_2
462        GROUP BY table_1.col_10, qcol_9);
463  }
464} {}
465
466# Testing boundary conditions on sqlite3_status()
467#
468do_test misc7-19.1 {
469  sqlite3_status -1 0
470} {21 0 0}
471do_test misc7-19.2 {
472  sqlite3_status 1000 0
473} {21 0 0}
474
475
476# sqlite3_global_recover() is a no-op.  But we might as well test it
477# if only to get the test coverage.
478#
479do_test misc7-20.1 {
480  sqlite3_global_recover
481} {SQLITE_OK}
482
483# Try to open a really long file name.
484#
485do_test misc7-21.1 {
486  set zFile [file join [get_pwd] "[string repeat abcde 104].db"]
487  set rc [catch {sqlite3 db2 $zFile} msg]
488  list $rc $msg
489} {1 {unable to open database file}}
490
491
492db close
493forcedelete test.db
494
495finish_test
496