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
+
18 (logior sb-posix
::s-irusr sb-posix
::s-iwusr sb-posix
::s-ixusr
21 sb-posix
::s-irgrp sb-posix
::s-iwgrp sb-posix
::s-ixgrp
22 sb-posix
::s-iroth sb-posix
::s-iwoth sb-posix
::s-ixoth
))))
24 (defmacro define-eacces-test
(name form
&rest values
)
28 (when (= (sb-posix:geteuid
) 0)
29 (return-from ,name
(values ,@values
)))
34 (sb-posix:chdir
*test-directory
*)
38 (sb-posix:chdir
(namestring *test-directory
*))
46 (sb-posix:chdir
#p
"/")
50 (sb-posix:chdir
*current-directory
*)
54 (sb-posix:chdir
"/../")
58 (sb-posix:chdir
#p
"/../")
62 (sb-posix:chdir
(make-pathname :directory
'(:absolute
:up
)))
65 (deftest chdir.error
.1
66 (let ((dne (make-pathname :directory
'(:relative
"chdir.does-not-exist"))))
68 (sb-posix:chdir
(merge-pathnames dne
*test-directory
*))
69 (sb-posix:syscall-error
(c)
70 (sb-posix:syscall-errno c
))))
73 (deftest chdir.error
.2
75 (sb-posix:chdir
*this-file
*)
76 (sb-posix:syscall-error
(c)
77 (sb-posix:syscall-errno c
)))
84 (let ((dne (make-pathname :directory
'(:relative
"mkdir.does-not-exist.1"))))
86 (sb-posix:mkdir
(merge-pathnames dne
*test-directory
*) 0)
87 ;; FIXME: no delete-directory in CL, but using our own operators
88 ;; is probably not ideal
89 (ignore-errors (sb-posix:rmdir
(merge-pathnames dne
*test-directory
*)))))
93 (let ((dne (make-pathname :directory
'(:relative
"mkdir.does-not-exist.2"))))
95 (sb-posix:mkdir
(namestring (merge-pathnames dne
*test-directory
*)) 0)
96 (ignore-errors (sb-posix:rmdir
(merge-pathnames dne
*test-directory
*)))))
99 (deftest mkdir.error
.1
101 (sb-posix:mkdir
*test-directory
* 0)
102 (sb-posix:syscall-error
(c)
103 (sb-posix:syscall-errno c
)))
106 (deftest mkdir.error
.2
108 (sb-posix:mkdir
#-win32
"/" #+win32
"C:/" 0)
109 (sb-posix:syscall-error
(c)
110 (sb-posix:syscall-errno c
)))
116 (define-eacces-test mkdir.error
.3
117 (let* ((dir (merge-pathnames
118 (make-pathname :directory
'(:relative
"mkdir.error.3"))
120 (dir2 (merge-pathnames
121 (make-pathname :directory
'(:relative
"does-not-exist"))
123 (sb-posix:mkdir dir
0)
125 (sb-posix:mkdir dir2
0)
126 (sb-posix:syscall-error
(c)
128 (sb-posix:syscall-errno c
))
130 (sb-posix:rmdir dir2
)
136 (let ((dne (make-pathname :directory
'(:relative
"rmdir.does-not-exist.1"))))
137 (ensure-directories-exist (merge-pathnames dne
*test-directory
*))
138 (sb-posix:rmdir
(merge-pathnames dne
*test-directory
*)))
142 (let ((dne (make-pathname :directory
'(:relative
"rmdir.does-not-exist.2"))))
143 (ensure-directories-exist (merge-pathnames dne
*test-directory
*))
144 (sb-posix:rmdir
(namestring (merge-pathnames dne
*test-directory
*))))
147 (deftest rmdir.error
.1
148 (let ((dne (make-pathname :directory
'(:relative
"rmdir.dne.error.1"))))
150 (sb-posix:rmdir
(merge-pathnames dne
*test-directory
*))
151 (sb-posix:syscall-error
(c)
152 (sb-posix:syscall-errno c
))))
155 (deftest rmdir.error
.2
157 (sb-posix:rmdir
*this-file
*)
158 (sb-posix:syscall-error
(c)
159 (sb-posix:syscall-errno c
)))
165 (deftest rmdir.error
.3
167 (sb-posix:rmdir
#-win32
"/" #+win32
"C:/")
168 (sb-posix:syscall-error
(c)
169 (sb-posix:syscall-errno c
)))
175 (deftest rmdir.error
.4
176 (let* ((dir (ensure-directories-exist
178 (make-pathname :directory
'(:relative
"rmdir.error.4"))
180 (file (make-pathname :name
"foo" :defaults dir
)))
181 (with-open-file (s file
:direction
:output
:if-exists nil
)
182 (write "" :stream s
))
185 (sb-posix:syscall-error
(c)
188 (let ((errno (sb-posix:syscall-errno c
)))
189 ;; documented by POSIX
190 (or (= errno sb-posix
::eexist
) (= errno sb-posix
::enotempty
))))))
193 (define-eacces-test rmdir.error
.5
194 (let* ((dir (merge-pathnames
195 (make-pathname :directory
'(:relative
"rmdir.error.5"))
197 (dir2 (merge-pathnames
198 (make-pathname :directory
'(:relative
"unremovable"))
200 (sb-posix:mkdir dir
+mode-rwx-all
+)
201 (sb-posix:mkdir dir2
+mode-rwx-all
+)
202 (sb-posix:chmod dir
0)
204 (sb-posix:rmdir dir2
)
205 (sb-posix:syscall-error
(c)
206 (sb-posix:chmod dir
(logior sb-posix
::s-iread sb-posix
::s-iwrite sb-posix
::s-iexec
))
207 (sb-posix:rmdir dir2
)
209 (sb-posix:syscall-errno c
))
211 (sb-posix:chmod dir
(logior sb-posix
::s-iread sb-posix
::s-iwrite sb-posix
::s-iexec
))
217 (let* ((stat (sb-posix:stat
*test-directory
*))
218 (mode (sb-posix::stat-mode stat
)))
219 ;; FIXME: Ugly ::s everywhere
220 (logand mode
(logior sb-posix
::s-iread sb-posix
::s-iwrite sb-posix
::s-iexec
)))
221 #.
(logior sb-posix
::s-iread sb-posix
::s-iwrite sb-posix
::s-iexec
))
225 (let* ((stat (sb-posix:stat
"/"))
226 (mode (sb-posix::stat-mode stat
)))
227 ;; it's logically possible for / to be writeable by others... but
228 ;; if it is, either someone is playing with strange security
229 ;; modules or they want to know about it anyway.
230 (logand mode sb-posix
::s-iwoth
))
234 (let* ((now (get-universal-time))
235 ;; FIXME: (encode-universal-time 00 00 00 01 01 1970)
236 (unix-now (- now
2208988800))
237 (stat (sb-posix:stat
*test-directory
*))
238 (atime (sb-posix::stat-atime stat
)))
239 ;; FIXME: breaks if mounted noatime :-(
240 #+nil
(< (- atime unix-now
) 10)
241 (< (- atime unix-now
) 10))
246 (let* ((stat (sb-posix:stat
(make-pathname :directory
'(:absolute
:up
))))
247 (mode (sb-posix::stat-mode stat
)))
248 ;; it's logically possible for / to be writeable by others... but
249 ;; if it is, either someone is playing with strange security
250 ;; modules or they want to know about it anyway.
251 (logand mode sb-posix
::s-iwoth
))
254 ;; Test that stat can take a second argument.
257 (let* ((stat-1 (sb-posix:stat
"/"))
258 (inode-1 (sb-posix:stat-ino stat-1
))
259 (stat-2 (sb-posix:stat
"/bin/sh"
261 (inode-2 (sb-posix:stat-ino stat-2
)))
264 (/= inode-1 inode-2
)))
270 (let* ((stat-1 (sb-posix:stat
"/"))
271 (mode-1 (sb-posix:stat-mode stat-1
))
272 (stat-2 (sb-posix:stat
"C:\\CONFIG.SYS"
274 (mode-2 (sb-posix:stat-mode stat-2
)))
281 ;;; FIXME: add tests for carrying a stat structure around in the
282 ;;; optional argument to SB-POSIX:STAT
284 (deftest stat.error
.1
285 (handler-case (sb-posix:stat
"")
286 (sb-posix:syscall-error
(c)
287 (sb-posix:syscall-errno c
)))
290 (define-eacces-test stat.error
.2
291 (let* ((dir (merge-pathnames
292 (make-pathname :directory
'(:relative
"stat.error.2"))
294 (file (merge-pathnames
295 (make-pathname :name
"unstatable")
297 (sb-posix:mkdir dir
+mode-rwx-all
+)
298 (with-open-file (s file
:direction
:output
)
299 (write "" :stream s
))
300 (sb-posix:chmod dir
0)
303 (sb-posix:syscall-error
(c)
304 (sb-posix:chmod dir
(logior sb-posix
::s-iread sb-posix
::s-iwrite sb-posix
::s-iexec
))
305 (sb-posix:unlink file
)
307 (sb-posix:syscall-errno c
))
309 (sb-posix:chmod dir
(logior sb-posix
::s-iread sb-posix
::s-iwrite sb-posix
::s-iexec
))
310 (sb-posix:unlink file
)
316 (defmacro with-stat-mode
((mode pathname
) &body body
)
317 (let ((stat (gensym)))
318 `(let* ((,stat
(sb-posix:stat
,pathname
))
319 (,mode
(sb-posix::stat-mode
,stat
)))
322 (defmacro with-lstat-mode
((mode pathname
) &body body
)
323 (let ((stat (gensym)))
324 `(let* ((,stat
(sb-posix:lstat
,pathname
))
325 (,mode
(sb-posix::stat-mode
,stat
)))
329 (with-stat-mode (mode *test-directory
*)
330 (sb-posix:s-isreg mode
))
334 (with-stat-mode (mode *test-directory
*)
335 (sb-posix:s-isdir mode
))
339 (with-stat-mode (mode *test-directory
*)
340 (sb-posix:s-ischr mode
))
344 (with-stat-mode (mode *test-directory
*)
345 (sb-posix:s-isblk mode
))
349 (with-stat-mode (mode *test-directory
*)
350 (sb-posix:s-isfifo mode
))
355 (with-stat-mode (mode *test-directory
*)
356 (sb-posix:s-issock mode
))
361 (let ((link-pathname (make-pathname :name
"stat-mode.7"
362 :defaults
*test-directory
*)))
365 (sb-posix:symlink
*test-directory
* link-pathname
)
366 (with-lstat-mode (mode link-pathname
)
367 (sb-posix:s-islnk mode
)))
368 (ignore-errors (sb-posix:unlink link-pathname
))))
372 (let ((pathname (make-pathname :name
"stat-mode.8"
373 :defaults
*test-directory
*)))
376 (with-open-file (out pathname
:direction
:output
)
377 (write-line "test" out
))
378 (with-stat-mode (mode pathname
)
379 (sb-posix:s-isreg mode
)))
380 (ignore-errors (delete-file pathname
))))
383 ;;; see comment in filename's designator definition, in macros.lisp
384 (deftest filename-designator
.1
385 (let ((file (format nil
"~A/[foo].txt" (namestring *test-directory
*))))
386 ;; creat() with a string as argument
387 (let ((fd (sb-posix:creat file sb-posix
:s-iwrite
)))
390 ;; if this test fails, it will probably be with
391 ;; "System call error 2 (No such file or directory)"
392 (let ((*default-pathname-defaults
* *test-directory
*))
393 (sb-posix:unlink
(car (directory "*.txt")))))
397 (let ((name (merge-pathnames "open-test.txt" *test-directory
*)))
401 (sb-posix:creat name
(logior sb-posix
:s-iwrite sb-posix
:s-iread
)))
402 (let ((fd (sb-posix:open name sb-posix
::o-rdonly
)))
403 (ignore-errors (sb-posix:close fd
))
405 (ignore-errors (sb-posix:unlink name
))))
408 (deftest open.error
.1
409 (handler-case (sb-posix:open
*test-directory
* sb-posix
::o-wronly
)
410 (sb-posix:syscall-error
(c)
411 (sb-posix:syscall-errno c
)))
417 #-
(or (and x86-64 linux
) win32
)
419 (let ((fd (sb-posix:open
"/dev/null" sb-posix
::o-nonblock
)))
420 (= (sb-posix:fcntl fd sb-posix
::f-getfl
) sb-posix
::o-nonblock
))
422 ;; On AMD64/Linux O_LARGEFILE is always set, even though the whole
423 ;; flag makes no sense.
426 (let ((fd (sb-posix:open
"/dev/null" sb-posix
::o-nonblock
)))
427 (/= 0 (logand (sb-posix:fcntl fd sb-posix
::f-getfl
)
428 sb-posix
::o-nonblock
)))
432 (deftest fcntl.flock
.1
433 (locally (declare (sb-ext:muffle-conditions sb-ext
:compiler-note
))
434 (let ((flock (make-instance 'sb-posix
:flock
435 :type sb-posix
:f-wrlck
436 :whence sb-posix
:seek-set
438 (pathname "fcntl.flock.1")
441 (with-open-file (f pathname
:direction
:output
)
442 (write-line "1234567890" f
)
443 (assert (zerop (sb-posix:fcntl f sb-posix
:f-setlk flock
)))
444 (let ((pid (sb-posix:fork
)))
447 (multiple-value-bind (nope error
)
448 (ignore-errors (sb-posix:fcntl f sb-posix
:f-setlk flock
))
451 (cond ((not (null nope
)) 1)
452 ((= (sb-posix:syscall-errno error
) sb-posix
:eagain
)
455 :recklessly-p t
#| don
't delete the file |
#)))
458 (sb-posix:wexitstatus
460 1 (sb-posix:waitpid pid
0))))
461 (throw 'test nil
))))))
467 (deftest fcntl.flock
.2
468 (locally (declare (sb-ext:muffle-conditions sb-ext
:compiler-note
))
469 (let ((flock (make-instance 'sb-posix
:flock
470 :type sb-posix
:f-wrlck
471 :whence sb-posix
:seek-set
473 (pathname "fcntl.flock.2")
476 (with-open-file (f pathname
:direction
:output
)
477 (write-line "1234567890" f
)
478 (assert (zerop (sb-posix:fcntl f sb-posix
:f-setlk flock
)))
479 (let ((ppid (sb-posix:getpid
))
480 (pid (sb-posix:fork
)))
482 (let ((r (sb-posix:fcntl f sb-posix
:f-getlk flock
)))
485 (cond ((not (zerop r
)) 1)
486 ((= (sb-posix:flock-pid flock
) ppid
) 42)
488 :recklessly-p t
#| don
't delete the file |
#))
491 (sb-posix:wexitstatus
493 1 (sb-posix:waitpid pid
0))))
494 (throw 'test nil
))))))
499 (let ((dir (sb-posix:opendir
"/")))
500 (unwind-protect (sb-alien:null-alien dir
)
501 (unless (sb-alien:null-alien dir
)
502 (sb-posix:closedir dir
))))
506 (let ((dir (sb-posix:opendir
"/")))
509 (loop for dirent
= (sb-posix:readdir dir
)
510 until
(sb-alien:null-alien dirent
)
511 when
(not (stringp (sb-posix:dirent-name dirent
)))
512 do
(return-from dir-loop nil
)
514 (sb-posix:closedir dir
)))
517 (deftest readdir
/dirent-name
518 (let ((dir (sb-posix:opendir
*current-directory
*)))
520 (equal (sort (loop for entry
= (sb-posix:readdir dir
)
521 until
(sb-alien:null-alien entry
)
522 collect
(sb-posix:dirent-name entry
))
524 (sort (append '("." "..")
526 (let ((string (enough-namestring p
*current-directory
*)))
527 (if (pathname-name p
)
529 (subseq string
0 (1- (length string
))))))
530 (directory (make-pathname
533 :defaults
*current-directory
*))))
535 (sb-posix:closedir dir
)))
540 ;; make sure that we found something
541 (not (sb-posix:getpwuid
0))
546 ;; make sure that we found something
547 (not (sb-posix:getpwnam
"root"))
551 (deftest pwent.non-existing
552 ;; make sure that we get something sensible, not an error
553 (handler-case (progn (sb-posix:getpwnam
"almost-certainly-does-not-exist")
560 ;; make sure that we found something
561 (not (sb-posix:getgrgid
0))
566 ;; make sure that we found something
567 (not (sb-posix:getgrnam
"sys"))
571 (deftest grent.non-existing
572 ;; make sure that we get something sensible, not an error
573 (handler-case (progn (sb-posix:getgrnam
"almost-certainly-does-not-exist")
579 ;; Requires root or special group + plus a sensible thing on the port
580 (deftest cfget
/setispeed
.1
581 (with-open-file (s "/dev/ttyS0")
582 (let* ((termios (sb-posix:tcgetattr s
))
583 (old (sb-posix:cfgetispeed termios
))
584 (new (if (= old sb-posix
:b2400
)
587 (sb-posix:cfsetispeed new termios
)
588 (sb-posix:tcsetattr
0 sb-posix
:tcsadrain termios
)
589 (setf termios
(sb-posix:tcgetattr s
))
590 (= new
(sb-posix:cfgetispeed termios
))))
594 ;; Requires root or special group + a sensible thing on the port
595 (deftest cfget
/setospeed
.1
596 (with-open-file (s "/dev/ttyS0" :direction
:output
:if-exists
:append
)
597 (let* ((termios (sb-posix:tcgetattr s
))
598 (old (sb-posix:cfgetospeed termios
))
599 (new (if (= old sb-posix
:b2400
)
602 (sb-posix:cfsetospeed new termios
)
603 (sb-posix:tcsetattr
0 sb-posix
:tcsadrain termios
)
604 (setf termios
(sb-posix:tcgetattr
0))
605 (= new
(sb-posix:cfgetospeed termios
))))
611 (plusp (sb-posix:time
))
616 (let ((file (merge-pathnames #p
"utimes.1" *test-directory
*))
617 (atime (random (1- (expt 2 31))))
618 (mtime (random (1- (expt 2 31)))))
619 (with-open-file (stream file
621 :if-exists
:supersede
622 :if-does-not-exist
:create
)
623 (princ "Hello, utimes" stream
))
624 (sb-posix:utime file atime mtime
)
625 (let* ((stat (sb-posix:stat file
)))
627 (list (= (sb-posix:stat-atime stat
) atime
)
628 (= (sb-posix:stat-mtime stat
) mtime
))))
635 (let ((link-pathname (make-pathname :name
"readlink.1"
636 :defaults
*test-directory
*)))
637 (sb-posix:symlink
"/" link-pathname
)
639 (sb-posix:readlink link-pathname
)
640 (ignore-errors (sb-posix:unlink link-pathname
))))
643 ;; Same thing, but with a very long link target (which doesn't have
644 ;; to exist). This tests the array adjustment in the wrapper,
645 ;; provided that the target's length is long enough.
647 (let ((target-pathname (make-pathname
648 :name
(make-string 255 :initial-element
#\a)
649 :directory
'(:absolute
)))
650 (link-pathname (make-pathname :name
"readlink.2"
651 :defaults
*test-directory
*)))
652 (sb-posix:symlink target-pathname link-pathname
)
654 (sb-posix:readlink link-pathname
)
655 (ignore-errors (sb-posix:unlink link-pathname
))))
656 #.
(concatenate 'string
"/" (make-string 255 :initial-element
#\a)))
658 ;; The error tests are in the order of exposition from SUSv3.
659 (deftest readlink.error
.1
660 (let* ((subdir-pathname (merge-pathnames
662 :directory
'(:relative
"readlink.error.1"))
664 (link-pathname (make-pathname :name
"readlink.error.1"
665 :defaults subdir-pathname
)))
666 (sb-posix:mkdir subdir-pathname
#o777
)
667 (sb-posix:symlink
"/" link-pathname
)
668 (sb-posix:chmod subdir-pathname
0)
670 (handler-case (sb-posix:readlink link-pathname
)
671 (sb-posix:syscall-error
(c)
672 (sb-posix:syscall-errno c
)))
674 (sb-posix:chmod subdir-pathname
#o777
)
675 (sb-posix:unlink link-pathname
)
676 (sb-posix:rmdir subdir-pathname
))))
678 (deftest readlink.error
.2
679 (let* ((non-link-pathname (make-pathname :name
"readlink.error.2"
680 :defaults
*test-directory
*))
681 (fd (sb-posix:open non-link-pathname sb-posix
::o-creat
)))
683 (handler-case (sb-posix:readlink non-link-pathname
)
684 (sb-posix:syscall-error
(c)
685 (sb-posix:syscall-errno c
)))
688 (sb-posix:unlink non-link-pathname
))))
690 ;; Skipping EIO, ELOOP
691 (deftest readlink.error
.3
692 (let* ((link-pathname (make-pathname :name
"readlink.error.3"
693 :defaults
*test-directory
*))
694 (bogus-pathname (merge-pathnames
697 :directory
'(:relative
"readlink.error.3"))
699 (sb-posix:symlink link-pathname link-pathname
)
701 (handler-case (sb-posix:readlink bogus-pathname
)
702 (sb-posix:syscall-error
(c)
703 (sb-posix:syscall-errno c
)))
704 (ignore-errors (sb-posix:unlink link-pathname
))))
706 ;; Note: PATH_MAX and NAME_MAX need not be defined, and may vary, so
707 ;; failure of this test is not too meaningful.
708 (deftest readlink.error
.4
710 (make-pathname :name
(make-string 257 ;NAME_MAX plus some, maybe
711 :initial-element
#\a))))
712 (handler-case (sb-posix:readlink pathname
)
713 (sb-posix:syscall-error
(c)
714 (sb-posix:syscall-errno c
))))
715 #.sb-posix
:enametoolong
)
716 (deftest readlink.error
.5
717 (let ((string (format nil
"~v{/A~}" 2049 ;PATH_MAX/2 plus some, maybe
719 (handler-case (sb-posix:readlink string
)
720 (sb-posix:syscall-error
(c)
721 (sb-posix:syscall-errno c
))))
722 #.sb-posix
:enametoolong
)
723 (deftest readlink.error
.6
724 (let ((no-such-pathname (make-pathname :name
"readlink.error.6"
725 :defaults
*test-directory
*)))
726 (handler-case (sb-posix:readlink no-such-pathname
)
727 (sb-posix:syscall-error
(c)
728 (sb-posix:syscall-errno c
))))
730 (deftest readlink.error
.7
731 (let* ((non-link-pathname (make-pathname :name
"readlink.error.7"
732 :defaults
*test-directory
*))
733 (impossible-pathname (merge-pathnames
736 '(:relative
"readlink.error.7")
737 :name
"readlink.error.7")
739 (fd (sb-posix:open non-link-pathname sb-posix
::o-creat
)))
741 (handler-case (sb-posix:readlink impossible-pathname
)
742 (sb-posix:syscall-error
(c)
743 (sb-posix:syscall-errno c
)))
746 (sb-posix:unlink non-link-pathname
))))
751 ;; FIXME: something saner, please
752 (equal (sb-unix::posix-getcwd
) (sb-posix:getcwd
))
757 (multiple-value-bind (fd temp
)
758 (sb-posix:mkstemp
(make-pathname
761 :defaults
*test-directory
*))
762 (let ((pathname (sb-ext:parse-native-namestring temp
)))
764 (values (integerp fd
) (pathname-name pathname
))
765 (delete-file temp
))))
771 (sb-ext:parse-native-namestring
772 (sb-posix:mkdtemp
(make-pathname
775 :defaults
*test-directory
*))
777 *default-pathname-defaults
*
780 (values (let* ((xxx (car (last (pathname-directory pathname
))))
781 (p (position #\. xxx
)))
782 (and p
(subseq xxx
0 p
)))
783 (pathname-name pathname
)
784 (pathname-type pathname
))
785 (sb-posix:rmdir pathname
)))
790 (let ((pathname (sb-ext:parse-native-namestring
791 (sb-posix:mktemp
#p
"mktemp.XXXXXX"))))
792 (values (equal "mktemp" (pathname-name pathname
))
793 (not (equal "XXXXXX" (pathname-type pathname
)))))