summaryrefslogtreecommitdiff
path: root/test/extension01.test
blob: 4e648ef4f51ac625ed2adddee6514b8f23caa2e1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# 2014-06-16
#
# The author disclaims copyright to this source code.  In place of
# a legal notice, here is a blessing:
#
#    May you do good and not evil.
#    May you find forgiveness for yourself and forgive others.
#    May you share freely, never taking more than you give.
#
#***********************************************************************
# 
# This file implements tests for various small extensions.
#

set testdir [file dirname $argv0]
source $testdir/tester.tcl
set ::testprefix extension01

load_static_extension db fileio
do_test 1.0 {
  forcedelete file1.txt
  set out [open ./file1.txt wb]
  puts -nonewline $out "This is a text file without a line ending"
  close $out
  db eval {
    CREATE TABLE t1(a INTEGER PRIMARY KEY, b TEXT);
    INSERT INTO t1 VALUES(1, readfile('./file1.txt'));
    SELECT * FROM t1;
  }
} {1 {This is a text file without a line ending}}
do_test 1.1 {
  forcedelete file2.txt
  db nullvalue nil
  db eval {
    DELETE FROM t1;
    INSERT INTO t1 VALUES(2, readfile(NULL)),(3, readfile('file2.txt'));
    SELECT a, b, typeof(b) FROM t1;
  }
} {2 nil null 3 nil null}

do_test 1.2 {
  db eval {
    SELECT writefile('./file2.txt', 'A second test line');
  }
} {18}
do_test 1.3 {
  set in [open ./file2.txt rb]
  set x [read $in]
  close $in
  list $x [file size file2.txt]
} {{A second test line} 18}

do_test 1.4 {
  db eval {
    SELECT writefile('./file2.txt', NULL);
  }
} {0}
do_test 1.5 {
  file size ./file2.txt
} {0}

do_test 1.6 {
  if {$::tcl_platform(os) ne "Windows NT"} {
    file attributes ./file2.txt -permissions r--r--r--
  } else {
    file attributes ./file2.txt -readonly 1
  }
  db eval {
    SELECT writefile('./file2.txt', 'Another test');
  }
} {nil}
do_test 1.7 {
  if {$::tcl_platform(os) ne "Windows NT"} {
    file attributes ./file2.txt -permissions rw-r--r--
  } else {
    file attributes ./file2.txt -readonly 0
  }
  db eval {
    SELECT writefile(NULL, 'Another test');
  }
} {nil}

# readfile() must read until EOF rather than trusting the size reported
# by ftell().  That size is -1 for a pipe or FIFO and 0 for many files
# under /proc.
#
forcedelete fifo1
if {$::tcl_platform(platform)=="unix" && ![catch {exec mkfifo fifo1}]} {
  do_test 2.1 {
    exec sh -c {printf hello >fifo1} &
    db eval { SELECT CAST(readfile('fifo1') AS TEXT) }
  } {hello}
  do_test 2.2 {
    exec sh -c {head -c 100000 /dev/zero >fifo1} &
    db eval { SELECT length(readfile('fifo1')) }
  } {100000}
  do_test 2.3 {
    set mx [sqlite3_limit db SQLITE_LIMIT_LENGTH 1000]
    exec sh -c {head -c 100000 /dev/zero >fifo1 2>/dev/null} &
    set res [catchsql { SELECT length(readfile('fifo1')) }]
    sqlite3_limit db SQLITE_LIMIT_LENGTH $mx
    set res
  } {1 {string or blob too big}}
  forcedelete fifo1
}
if {[file readable /proc/self/status]} {
  do_execsql_test 2.4 {
    SELECT length(readfile('/proc/self/status'))>0
  } {1}
}

finish_test