misc7.test 14 KB

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