1 (defpackage "SB-POSIX-TESTS"
2 (:use "COMMON-LISP" "SB-RT"))
4 (in-package "SB-POSIX-TESTS")
6 (defvar *test-directory*
7 (ensure-directories-exist
8 (merge-pathnames (make-pathname :directory '(:relative "test-lab"))
9 (make-pathname :directory
10 (pathname-directory *load-truename*)))))
12 (defvar *current-directory* *default-pathname-defaults*)
14 (defvar *this-file* *load-truename*)
16 (eval-when (:compile-toplevel :load-toplevel)
17 (defconstant +mode-rwx-all+ (logior sb-posix::s-irusr sb-posix::s-iwusr sb-posix::s-ixusr
18 sb-posix::s-irgrp sb-posix::s-iwgrp sb-posix::s-ixgrp
19 sb-posix::s-iroth sb-posix::s-iwoth sb-posix::s-ixoth)))
21 (defmacro define-eacces-test (name form &rest values)
24 (when (= (sb-posix:geteuid) 0)
25 (return-from ,name (values ,@values)))
30 (sb-posix:chdir *test-directory*)
34 (sb-posix:chdir (namestring *test-directory*))
42 (sb-posix:chdir #p"/")
46 (sb-posix:chdir *current-directory*)
50 (sb-posix:chdir "/../")
54 (sb-posix:chdir #p"/../")
58 (sb-posix:chdir (make-pathname :directory '(:absolute :up)))
61 (deftest chdir.error.1
62 (let ((dne (make-pathname :directory '(:relative "chdir.does-not-exist"))))
64 (sb-posix:chdir (merge-pathnames dne *test-directory*))
65 (sb-posix:syscall-error (c)
66 (sb-posix:syscall-errno c))))
69 (deftest chdir.error.2
71 (sb-posix:chdir *this-file*)
72 (sb-posix:syscall-error (c)
73 (sb-posix:syscall-errno c)))
77 (let ((dne (make-pathname :directory '(:relative "mkdir.does-not-exist.1"))))
79 (sb-posix:mkdir (merge-pathnames dne *test-directory*) 0)
80 ;; FIXME: no delete-directory in CL, but using our own operators
81 ;; is probably not ideal
82 (ignore-errors (sb-posix:rmdir (merge-pathnames dne *test-directory*)))))
86 (let ((dne (make-pathname :directory '(:relative "mkdir.does-not-exist.2"))))
88 (sb-posix:mkdir (namestring (merge-pathnames dne *test-directory*)) 0)
89 (ignore-errors (sb-posix:rmdir (merge-pathnames dne *test-directory*)))))
92 (deftest mkdir.error.1
94 (sb-posix:mkdir *test-directory* 0)
95 (sb-posix:syscall-error (c)
96 (sb-posix:syscall-errno c)))
99 (deftest mkdir.error.2
101 (sb-posix:mkdir "/" 0)
102 (sb-posix:syscall-error (c)
103 (sb-posix:syscall-errno c)))
106 (define-eacces-test mkdir.error.3
107 (let* ((dir (merge-pathnames
108 (make-pathname :directory '(:relative "mkdir.error.3"))
110 (dir2 (merge-pathnames
111 (make-pathname :directory '(:relative "does-not-exist"))
113 (sb-posix:mkdir dir 0)
115 (sb-posix:mkdir dir2 0)
116 (sb-posix:syscall-error (c)
118 (sb-posix:syscall-errno c))
120 (sb-posix:rmdir dir2)
126 (let ((dne (make-pathname :directory '(:relative "rmdir.does-not-exist.1"))))
127 (ensure-directories-exist (merge-pathnames dne *test-directory*))
128 (sb-posix:rmdir (merge-pathnames dne *test-directory*)))
132 (let ((dne (make-pathname :directory '(:relative "rmdir.does-not-exist.2"))))
133 (ensure-directories-exist (merge-pathnames dne *test-directory*))
134 (sb-posix:rmdir (namestring (merge-pathnames dne *test-directory*))))
137 (deftest rmdir.error.1
138 (let ((dne (make-pathname :directory '(:relative "rmdir.dne.error.1"))))
140 (sb-posix:rmdir (merge-pathnames dne *test-directory*))
141 (sb-posix:syscall-error (c)
142 (sb-posix:syscall-errno c))))
145 (deftest rmdir.error.2
147 (sb-posix:rmdir *this-file*)
148 (sb-posix:syscall-error (c)
149 (sb-posix:syscall-errno c)))
152 (deftest rmdir.error.3
155 (sb-posix:syscall-error (c)
156 (sb-posix:syscall-errno c)))
159 (deftest rmdir.error.4
160 (let* ((dir (ensure-directories-exist
162 (make-pathname :directory '(:relative "rmdir.error.4"))
164 (file (make-pathname :name "foo" :defaults dir)))
165 (with-open-file (s file :direction :output)
166 (write "" :stream s))
169 (sb-posix:syscall-error (c)
172 (let ((errno (sb-posix:syscall-errno c)))
173 ;; documented by POSIX
174 (or (= errno sb-posix::eexist) (= errno sb-posix::enotempty))))))
177 (define-eacces-test rmdir.error.5
178 (let* ((dir (merge-pathnames
179 (make-pathname :directory '(:relative "rmdir.error.5"))
181 (dir2 (merge-pathnames
182 (make-pathname :directory '(:relative "unremovable"))
184 (sb-posix:mkdir dir +mode-rwx-all+)
185 (sb-posix:mkdir dir2 +mode-rwx-all+)
186 (sb-posix:chmod dir 0)
188 (sb-posix:rmdir dir2)
189 (sb-posix:syscall-error (c)
190 (sb-posix:chmod dir (logior sb-posix::s-iread sb-posix::s-iwrite sb-posix::s-iexec))
191 (sb-posix:rmdir dir2)
193 (sb-posix:syscall-errno c))
195 (sb-posix:chmod dir (logior sb-posix::s-iread sb-posix::s-iwrite sb-posix::s-iexec))
201 (let* ((stat (sb-posix:stat *test-directory*))
202 (mode (sb-posix::stat-mode stat)))
203 ;; FIXME: Ugly ::s everywhere
204 (logand mode (logior sb-posix::s-iread sb-posix::s-iwrite sb-posix::s-iexec)))
205 #.(logior sb-posix::s-iread sb-posix::s-iwrite sb-posix::s-iexec))
208 (let* ((stat (sb-posix:stat "/"))
209 (mode (sb-posix::stat-mode stat)))
210 ;; it's logically possible for / to be writeable by others... but
211 ;; if it is, either someone is playing with strange security
212 ;; modules or they want to know about it anyway.
213 (logand mode sb-posix::s-iwoth))
217 (let* ((now (get-universal-time))
218 ;; FIXME: (encode-universal-time 00 00 00 01 01 1970)
219 (unix-now (- now 2208988800))
220 (stat (sb-posix:stat *test-directory*))
221 (atime (sb-posix::stat-atime stat)))
222 ;; FIXME: breaks if mounted noatime :-(
223 (< (- atime unix-now) 10))
227 (let* ((stat (sb-posix:stat (make-pathname :directory '(:absolute :up))))
228 (mode (sb-posix::stat-mode stat)))
229 ;; it's logically possible for / to be writeable by others... but
230 ;; if it is, either someone is playing with strange security
231 ;; modules or they want to know about it anyway.
232 (logand mode sb-posix::s-iwoth))
235 ;;; FIXME: add tests for carrying a stat structure around in the
236 ;;; optional argument to SB-POSIX:STAT
238 (deftest stat.error.1
239 (handler-case (sb-posix:stat "")
240 (sb-posix:syscall-error (c)
241 (sb-posix:syscall-errno c)))
244 (define-eacces-test stat.error.2
245 (let* ((dir (merge-pathnames
246 (make-pathname :directory '(:relative "stat.error.2"))
248 (file (merge-pathnames
249 (make-pathname :name "unstatable")
251 (sb-posix:mkdir dir +mode-rwx-all+)
252 (with-open-file (s file :direction :output)
253 (write "" :stream s))
254 (sb-posix:chmod dir 0)
257 (sb-posix:syscall-error (c)
258 (sb-posix:chmod dir (logior sb-posix::s-iread sb-posix::s-iwrite sb-posix::s-iexec))
259 (sb-posix:unlink file)
261 (sb-posix:syscall-errno c))
263 (sb-posix:chmod dir (logior sb-posix::s-iread sb-posix::s-iwrite sb-posix::s-iexec))
264 (sb-posix:unlink file)
270 (defmacro with-stat-mode ((mode pathname) &body body)
271 (let ((stat (gensym)))
272 `(let* ((,stat (sb-posix:stat ,pathname))
273 (,mode (sb-posix::stat-mode ,stat)))
276 (defmacro with-lstat-mode ((mode pathname) &body body)
277 (let ((stat (gensym)))
278 `(let* ((,stat (sb-posix:lstat ,pathname))
279 (,mode (sb-posix::stat-mode ,stat)))
283 (with-stat-mode (mode *test-directory*)
284 (sb-posix:s-isreg mode))
288 (with-stat-mode (mode *test-directory*)
289 (sb-posix:s-isdir mode))
293 (with-stat-mode (mode *test-directory*)
294 (sb-posix:s-ischr mode))
298 (with-stat-mode (mode *test-directory*)
299 (sb-posix:s-isblk mode))
303 (with-stat-mode (mode *test-directory*)
304 (sb-posix:s-isfifo mode))
308 (with-stat-mode (mode *test-directory*)
309 (sb-posix:s-issock mode))
313 (let ((link-pathname (make-pathname :name "stat-mode.7"
314 :defaults *test-directory*)))
317 (sb-posix:symlink *test-directory* link-pathname)
318 (with-lstat-mode (mode link-pathname)
319 (sb-posix:s-islnk mode)))
320 (ignore-errors (sb-posix:unlink link-pathname))))
324 (let ((pathname (make-pathname :name "stat-mode.8"
325 :defaults *test-directory*)))
328 (with-open-file (out pathname :direction :output)
329 (write-line "test" out))
330 (with-stat-mode (mode pathname)
331 (sb-posix:s-isreg mode)))
332 (ignore-errors (delete-file pathname))))
335 ;;; see comment in filename's designator definition, in macros.lisp
336 (deftest filename-designator.1
337 (let ((file (format nil "~A/[foo].txt" (namestring *test-directory*))))
338 ;; creat() with a string as argument
339 (sb-posix:creat file 0)
340 ;; if this test fails, it will probably be with
341 ;; "System call error 2 (No such file or directory)"
342 (let ((*default-pathname-defaults* *test-directory*))
343 (sb-posix:unlink (car (directory "*.txt")))))
347 (let ((fd (sb-posix:open *test-directory* sb-posix::o-rdonly)))
348 (ignore-errors (sb-posix:close fd))
352 (deftest open.error.1
353 (handler-case (sb-posix:open *test-directory* sb-posix::o-wronly)
354 (sb-posix:syscall-error (c)
355 (sb-posix:syscall-errno c)))
359 (let ((fd (sb-posix:open "/dev/null" sb-posix::o-nonblock)))
360 (= (sb-posix:fcntl fd sb-posix::f-getfl) sb-posix::o-nonblock))
365 (let ((dir (sb-posix:opendir "/")))
366 (unwind-protect (sb-alien:null-alien dir)
367 (unless (sb-alien:null-alien dir)
368 (sb-posix:closedir dir))))
372 (let ((dir (sb-posix:opendir "/")))
375 (loop for dirent = (sb-posix:readdir dir)
376 until (sb-alien:null-alien dirent)
377 when (not (stringp (sb-posix:dirent-name dirent)))
378 do (return-from dir-loop nil)
380 (sb-posix:closedir dir)))