1 ;;;; filesys.test --- test file system functions -*- scheme -*-
3 ;;;; Copyright (C) 2004, 2006, 2013 Free Software Foundation, Inc.
5 ;;;; This library is free software; you can redistribute it and/or
6 ;;;; modify it under the terms of the GNU Lesser General Public
7 ;;;; License as published by the Free Software Foundation; either
8 ;;;; version 3 of the License, or (at your option) any later version.
10 ;;;; This library is distributed in the hope that it will be useful,
11 ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 ;;;; Lesser General Public License for more details.
15 ;;;; You should have received a copy of the GNU Lesser General Public
16 ;;;; License along with this library; if not, write to the Free Software
17 ;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 (define-module (test-suite test-filesys)
20 #:use-module (test-suite lib)
21 #:use-module (test-suite guile-test)
22 #:use-module (ice-9 match)
23 #:use-module (rnrs io ports)
24 #:use-module (rnrs bytevectors))
27 (data-file-name "filesys-test.tmp"))
28 (define (test-symlink)
29 (data-file-name "filesys-test-link.tmp"))
36 (with-test-prefix "copy-file"
38 ;; return next prospective file descriptor number
44 ;; in guile 1.6.4 and earlier, copy-file didn't close the input fd when
45 ;; the output could not be opened
46 (pass-if "fd leak when dest unwritable"
47 (let ((old-next (next-fd)))
48 (false-if-exception (copy-file "/dev/null" "no/such/dir/foo"))
49 (= old-next (next-fd)))))
55 (with-test-prefix "lstat"
57 (pass-if "normal file"
58 (call-with-output-file (test-file)
60 (display "hello" port)))
61 (eqv? 5 (stat:size (lstat (test-file)))))
63 (call-with-output-file (test-file)
65 (display "hello" port)))
66 (false-if-exception (delete-file (test-symlink)))
67 (if (not (false-if-exception
68 (begin (symlink (test-file) (test-symlink)) #t)))
69 (display "cannot create symlink, lstat test skipped\n")
71 ;; not much to test, except that it works
72 (->bool (lstat (test-symlink))))))
75 ;;; opendir and friends
78 (with-test-prefix "opendir"
80 (with-test-prefix "root directory"
81 (let ((d (opendir "/")))
83 (string? (readdir d)))
84 (pass-if "all entries are strings"
86 (let ((f (readdir d)))
99 (with-test-prefix "stat"
101 (with-test-prefix "filename"
104 (call-with-output-file (test-file)
106 (display "hello" port)))
107 (eqv? 5 (stat:size (stat (test-file))))))
109 (with-test-prefix "file descriptor"
112 (call-with-output-file (test-file)
114 (display "hello" port)))
115 (let* ((fd (open-fdes (test-file) O_RDONLY))
118 (eqv? 5 (stat:size st)))))
120 (with-test-prefix "port"
123 (call-with-output-file (test-file)
125 (display "hello" port)))
126 (let* ((port (open-file (test-file) "r+"))
129 (eqv? 5 (stat:size st))))))
131 (with-test-prefix "sendfile"
133 (let* ((file (search-path %load-path "ice-9/boot-9.scm"))
134 (len (stat:size (stat file)))
135 (ref (call-with-input-file file get-bytevector-all)))
137 (pass-if-equal "file" (cons len ref)
138 (cons (call-with-input-file file
140 (call-with-output-file (test-file)
142 (sendfile output input len 0)))))
143 (call-with-input-file (test-file) get-bytevector-all)))
145 (pass-if-equal "file with offset"
146 (cons (- len 777) (call-with-input-file file
148 (seek input 777 SEEK_SET)
149 (get-bytevector-all input))))
150 (cons (call-with-input-file file
152 (call-with-output-file (test-file)
154 (sendfile output input (- len 777) 777)))))
155 (call-with-input-file (test-file) get-bytevector-all)))
157 (pass-if-equal "file with offset past the end" (- len 777)
158 (call-with-input-file file
160 (call-with-output-file (test-file)
162 (sendfile output input len 777))))))
164 (pass-if-equal "file with offset near the end" 77
165 (call-with-input-file file
167 (call-with-output-file (test-file)
169 (sendfile output input len (- len 77)))))))
171 (pass-if-equal "pipe" (cons len ref)
172 (if (provided? 'threads)
173 (let* ((in+out (pipe))
174 (child (call-with-new-thread
176 (call-with-input-file file
178 (let ((result (sendfile (cdr in+out)
181 (close-port (cdr in+out))
183 (let ((out (get-bytevector-all (car in+out))))
184 (close-port (car in+out))
185 (cons (join-thread child) out)))
186 (throw 'unresolved)))
188 (pass-if-equal "pipe with offset"
189 (cons (- len 777) (call-with-input-file file
191 (seek input 777 SEEK_SET)
192 (get-bytevector-all input))))
193 (if (provided? 'threads)
194 (let* ((in+out (pipe))
195 (child (call-with-new-thread
197 (call-with-input-file file
199 (let ((result (sendfile (cdr in+out)
203 (close-port (cdr in+out))
205 (let ((out (get-bytevector-all (car in+out))))
206 (close-port (car in+out))
207 (cons (join-thread child) out)))
208 (throw 'unresolved)))))
210 (delete-file (test-file))
211 (delete-file (test-symlink))