sqlite
[Ported from github.com/sqlite/sqlite] Fix bugs in and improve the interface to the ".check" command of the CLI.
Brought by drakestapleton
sqlite / test / extension01.test
| 1 | # 2014-06-16 |
| 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 | # |
| 12 | # This file implements tests for various small extensions. |
| 13 | # |
| 14 | |
| 15 | set testdir [file dirname $argv0] |
| 16 | source $testdir/tester.tcl |
| 17 | set ::testprefix extension01 |
| 18 | |
| 19 | load_static_extension db fileio |
| 20 | do_test 1.0 { |
| 21 | forcedelete file1.txt |
| 22 | set out [open ./file1.txt wb] |
| 23 | puts -nonewline $out "This is a text file without a line ending" |
| 24 | close $out |
| 25 | db eval { |
| 26 | CREATE TABLE t1(a INTEGER PRIMARY KEY, b TEXT); |
| 27 | INSERT INTO t1 VALUES(1, readfile('./file1.txt')); |
| 28 | SELECT * FROM t1; |
| 29 | } |
| 30 | } {1 {This is a text file without a line ending}} |
| 31 | do_test 1.1 { |
| 32 | forcedelete file2.txt |
| 33 | db nullvalue nil |
| 34 | db eval { |
| 35 | DELETE FROM t1; |
| 36 | INSERT INTO t1 VALUES(2, readfile(NULL)),(3, readfile('file2.txt')); |
| 37 | SELECT a, b, typeof(b) FROM t1; |
| 38 | } |
| 39 | } {2 nil null 3 nil null} |
| 40 | |
| 41 | do_test 1.2 { |
| 42 | db eval { |
| 43 | SELECT writefile('./file2.txt', 'A second test line'); |
| 44 | } |
| 45 | } {18} |
| 46 | do_test 1.3 { |
| 47 | set in [open ./file2.txt rb] |
| 48 | set x [read $in] |
| 49 | close $in |
| 50 | list $x [file size file2.txt] |
| 51 | } {{A second test line} 18} |
| 52 | |
| 53 | do_test 1.4 { |
| 54 | db eval { |
| 55 | SELECT writefile('./file2.txt', NULL); |
| 56 | } |
| 57 | } {0} |
| 58 | do_test 1.5 { |
| 59 | file size ./file2.txt |
| 60 | } {0} |
| 61 | |
| 62 | do_test 1.6 { |
| 63 | if {$::tcl_platform(os) ne "Windows NT"} { |
| 64 | file attributes ./file2.txt -permissions r--r--r-- |
| 65 | } else { |
| 66 | file attributes ./file2.txt -readonly 1 |
| 67 | } |
| 68 | db eval { |
| 69 | SELECT writefile('./file2.txt', 'Another test'); |
| 70 | } |
| 71 | } {nil} |
| 72 | do_test 1.7 { |
| 73 | if {$::tcl_platform(os) ne "Windows NT"} { |
| 74 | file attributes ./file2.txt -permissions rw-r--r-- |
| 75 | } else { |
| 76 | file attributes ./file2.txt -readonly 0 |
| 77 | } |
| 78 | db eval { |
| 79 | SELECT writefile(NULL, 'Another test'); |
| 80 | } |
| 81 | } {nil} |
| 82 | |
| 83 | # readfile() must read until EOF rather than trusting the size reported |
| 84 | # by ftell(). That size is -1 for a pipe or FIFO and 0 for many files |
| 85 | # under /proc. |
| 86 | # |
| 87 | forcedelete fifo1 |
| 88 | if {$::tcl_platform(platform)=="unix" && ![catch {exec mkfifo fifo1}]} { |
| 89 | do_test 2.1 { |
| 90 | exec sh -c {printf hello >fifo1} & |
| 91 | db eval { SELECT CAST(readfile('fifo1') AS TEXT) } |
| 92 | } {hello} |
| 93 | do_test 2.2 { |
| 94 | exec sh -c {head -c 100000 /dev/zero >fifo1} & |
| 95 | db eval { SELECT length(readfile('fifo1')) } |
| 96 | } {100000} |
| 97 | do_test 2.3 { |
| 98 | set mx [sqlite3_limit db SQLITE_LIMIT_LENGTH 1000] |
| 99 | exec sh -c {head -c 100000 /dev/zero >fifo1 2>/dev/null} & |
| 100 | set res [catchsql { SELECT length(readfile('fifo1')) }] |
| 101 | sqlite3_limit db SQLITE_LIMIT_LENGTH $mx |
| 102 | set res |
| 103 | } {1 {string or blob too big}} |
| 104 | forcedelete fifo1 |
| 105 | } |
| 106 | if {[file readable /proc/self/status]} { |
| 107 | do_execsql_test 2.4 { |
| 108 | SELECT length(readfile('/proc/self/status'))>0 |
| 109 | } {1} |
| 110 | } |
| 111 | |
| 112 | finish_test |