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 ;;; FIXME: add tests for carrying a stat structure around in the
255 ;;; optional argument to SB-POSIX:STAT
257 (deftest stat.error
.1
258 (handler-case (sb-posix:stat
"")
259 (sb-posix:syscall-error
(c)
260 (sb-posix:syscall-errno c
)))
263 (define-eacces-test stat.error
.2
264 (let* ((dir (merge-pathnames
265 (make-pathname :directory
'(:relative
"stat.error.2"))
267 (file (merge-pathnames
268 (make-pathname :name
"unstatable")
270 (sb-posix:mkdir dir
+mode-rwx-all
+)
271 (with-open-file (s file
:direction
:output
)
272 (write "" :stream s
))
273 (sb-posix:chmod dir
0)
276 (sb-posix:syscall-error
(c)
277 (sb-posix:chmod dir
(logior sb-posix
::s-iread sb-posix
::s-iwrite sb-posix
::s-iexec
))
278 (sb-posix:unlink file
)
280 (sb-posix:syscall-errno c
))
282 (sb-posix:chmod dir
(logior sb-posix
::s-iread sb-posix
::s-iwrite sb-posix
::s-iexec
))
283 (sb-posix:unlink file
)
289 (defmacro with-stat-mode
((mode pathname
) &body body
)
290 (let ((stat (gensym)))
291 `(let* ((,stat
(sb-posix:stat
,pathname
))
292 (,mode
(sb-posix::stat-mode
,stat
)))
295 (defmacro with-lstat-mode
((mode pathname
) &body body
)
296 (let ((stat (gensym)))
297 `(let* ((,stat
(sb-posix:lstat
,pathname
))
298 (,mode
(sb-posix::stat-mode
,stat
)))
302 (with-stat-mode (mode *test-directory
*)
303 (sb-posix:s-isreg mode
))
307 (with-stat-mode (mode *test-directory
*)
308 (sb-posix:s-isdir mode
))
312 (with-stat-mode (mode *test-directory
*)
313 (sb-posix:s-ischr mode
))
317 (with-stat-mode (mode *test-directory
*)
318 (sb-posix:s-isblk mode
))
322 (with-stat-mode (mode *test-directory
*)
323 (sb-posix:s-isfifo mode
))
328 (with-stat-mode (mode *test-directory
*)
329 (sb-posix:s-issock mode
))
334 (let ((link-pathname (make-pathname :name
"stat-mode.7"
335 :defaults
*test-directory
*)))
338 (sb-posix:symlink
*test-directory
* link-pathname
)
339 (with-lstat-mode (mode link-pathname
)
340 (sb-posix:s-islnk mode
)))
341 (ignore-errors (sb-posix:unlink link-pathname
))))
345 (let ((pathname (make-pathname :name
"stat-mode.8"
346 :defaults
*test-directory
*)))
349 (with-open-file (out pathname
:direction
:output
)
350 (write-line "test" out
))
351 (with-stat-mode (mode pathname
)
352 (sb-posix:s-isreg mode
)))
353 (ignore-errors (delete-file pathname
))))
356 ;;; see comment in filename's designator definition, in macros.lisp
357 (deftest filename-designator
.1
358 (let ((file (format nil
"~A/[foo].txt" (namestring *test-directory
*))))
359 ;; creat() with a string as argument
360 (let ((fd (sb-posix:creat file sb-posix
:s-iwrite
)))
363 ;; if this test fails, it will probably be with
364 ;; "System call error 2 (No such file or directory)"
365 (let ((*default-pathname-defaults
* *test-directory
*))
366 (sb-posix:unlink
(car (directory "*.txt")))))
370 (let ((name (merge-pathnames "open-test.txt" *test-directory
*)))
374 (sb-posix:creat name
(logior sb-posix
:s-iwrite sb-posix
:s-iread
)))
375 (let ((fd (sb-posix:open name sb-posix
::o-rdonly
)))
376 (ignore-errors (sb-posix:close fd
))
378 (ignore-errors (sb-posix:unlink name
))))
381 (deftest open.error
.1
382 (handler-case (sb-posix:open
*test-directory
* sb-posix
::o-wronly
)
383 (sb-posix:syscall-error
(c)
384 (sb-posix:syscall-errno c
)))
390 #-
(or (and x86-64 linux
) win32
)
392 (let ((fd (sb-posix:open
"/dev/null" sb-posix
::o-nonblock
)))
393 (= (sb-posix:fcntl fd sb-posix
::f-getfl
) sb-posix
::o-nonblock
))
395 ;; On AMD64/Linux O_LARGEFILE is always set, even though the whole
396 ;; flag makes no sense.
399 (let ((fd (sb-posix:open
"/dev/null" sb-posix
::o-nonblock
)))
400 (/= 0 (logand (sb-posix:fcntl fd sb-posix
::f-getfl
)
401 sb-posix
::o-nonblock
)))
405 (let ((dir (sb-posix:opendir
"/")))
406 (unwind-protect (sb-alien:null-alien dir
)
407 (unless (sb-alien:null-alien dir
)
408 (sb-posix:closedir dir
))))
412 (let ((dir (sb-posix:opendir
"/")))
415 (loop for dirent
= (sb-posix:readdir dir
)
416 until
(sb-alien:null-alien dirent
)
417 when
(not (stringp (sb-posix:dirent-name dirent
)))
418 do
(return-from dir-loop nil
)
420 (sb-posix:closedir dir
)))
423 (deftest readdir
/dirent-name
424 (let ((dir (sb-posix:opendir
*current-directory
*)))
426 (equal (sort (loop for entry
= (sb-posix:readdir dir
)
427 until
(sb-alien:null-alien entry
)
428 collect
(sb-posix:dirent-name entry
))
430 (sort (append '("." "..")
432 (let ((string (enough-namestring p
*current-directory
*)))
433 (if (pathname-name p
)
435 (subseq string
0 (1- (length string
))))))
436 (directory (make-pathname
439 :defaults
*current-directory
*))))
441 (sb-posix:closedir dir
)))
446 ;; make sure that we found something
447 (not (sb-posix:getpwuid
0))
452 ;; make sure that we found something
453 (not (sb-posix:getpwnam
"root"))
457 ;; Requires root or special group + plus a sensible thing on the port
458 (deftest cfget
/setispeed
.1
459 (with-open-file (s "/dev/ttyS0")
460 (let* ((termios (sb-posix:tcgetattr s
))
461 (old (sb-posix:cfgetispeed termios
))
462 (new (if (= old sb-posix
:b2400
)
465 (sb-posix:cfsetispeed new termios
)
466 (sb-posix:tcsetattr
0 sb-posix
:tcsadrain termios
)
467 (setf termios
(sb-posix:tcgetattr s
))
468 (= new
(sb-posix:cfgetispeed termios
))))
472 ;; Requires root or special group + a sensible thing on the port
473 (deftest cfget
/setospeed
.1
474 (with-open-file (s "/dev/ttyS0" :direction
:output
:if-exists
:append
)
475 (let* ((termios (sb-posix:tcgetattr s
))
476 (old (sb-posix:cfgetospeed termios
))
477 (new (if (= old sb-posix
:b2400
)
480 (sb-posix:cfsetospeed new termios
)
481 (sb-posix:tcsetattr
0 sb-posix
:tcsadrain termios
)
482 (setf termios
(sb-posix:tcgetattr
0))
483 (= new
(sb-posix:cfgetospeed termios
))))
489 (plusp (sb-posix:time
))
494 (let ((file (merge-pathnames #p
"utimes.1" *test-directory
*))
495 (atime (random (1- (expt 2 31))))
496 (mtime (random (1- (expt 2 31)))))
497 (with-open-file (stream file
499 :if-exists
:supersede
500 :if-does-not-exist
:create
)
501 (princ "Hello, utimes" stream
))
502 (sb-posix:utime file atime mtime
)
503 (let* ((stat (sb-posix:stat file
)))
505 (list (= (sb-posix:stat-atime stat
) atime
)
506 (= (sb-posix:stat-mtime stat
) mtime
))))
513 (let ((link-pathname (make-pathname :name
"readlink.1"
514 :defaults
*test-directory
*)))
515 (sb-posix:symlink
"/" link-pathname
)
517 (sb-posix:readlink link-pathname
)
518 (ignore-errors (sb-posix:unlink link-pathname
))))
521 ;; Same thing, but with a very long link target (which doesn't have
522 ;; to exist). This tests the array adjustment in the wrapper,
523 ;; provided that the target's length is long enough.
525 (let ((target-pathname (make-pathname
526 :name
(make-string 255 :initial-element
#\a)
527 :directory
'(:absolute
)))
528 (link-pathname (make-pathname :name
"readlink.2"
529 :defaults
*test-directory
*)))
530 (sb-posix:symlink target-pathname link-pathname
)
532 (sb-posix:readlink link-pathname
)
533 (ignore-errors (sb-posix:unlink link-pathname
))))
534 #.
(concatenate 'string
"/" (make-string 255 :initial-element
#\a)))
536 ;; The error tests are in the order of exposition from SUSv3.
537 (deftest readlink.error
.1
538 (let* ((subdir-pathname (merge-pathnames
540 :directory
'(:relative
"readlink.error.1"))
542 (link-pathname (make-pathname :name
"readlink.error.1"
543 :defaults subdir-pathname
)))
544 (sb-posix:mkdir subdir-pathname
#o777
)
545 (sb-posix:symlink
"/" link-pathname
)
546 (sb-posix:chmod subdir-pathname
0)
548 (handler-case (sb-posix:readlink link-pathname
)
549 (sb-posix:syscall-error
(c)
550 (sb-posix:syscall-errno c
)))
552 (sb-posix:chmod subdir-pathname
#o777
)
553 (sb-posix:unlink link-pathname
)
554 (sb-posix:rmdir subdir-pathname
))))
556 (deftest readlink.error
.2
557 (let* ((non-link-pathname (make-pathname :name
"readlink.error.2"
558 :defaults
*test-directory
*))
559 (fd (sb-posix:open non-link-pathname sb-posix
::o-creat
)))
561 (handler-case (sb-posix:readlink non-link-pathname
)
562 (sb-posix:syscall-error
(c)
563 (sb-posix:syscall-errno c
)))
566 (sb-posix:unlink non-link-pathname
))))
568 ;; Skipping EIO, ELOOP
569 (deftest readlink.error
.3
570 (let* ((link-pathname (make-pathname :name
"readlink.error.3"
571 :defaults
*test-directory
*))
572 (bogus-pathname (merge-pathnames
575 :directory
'(:relative
"readlink.error.3"))
577 (sb-posix:symlink link-pathname link-pathname
)
579 (handler-case (sb-posix:readlink bogus-pathname
)
580 (sb-posix:syscall-error
(c)
581 (sb-posix:syscall-errno c
)))
582 (ignore-errors (sb-posix:unlink link-pathname
))))
584 ;; Note: PATH_MAX and NAME_MAX need not be defined, and may vary, so
585 ;; failure of this test is not too meaningful.
586 (deftest readlink.error
.4
588 (make-pathname :name
(make-string 257 ;NAME_MAX plus some, maybe
589 :initial-element
#\a))))
590 (handler-case (sb-posix:readlink pathname
)
591 (sb-posix:syscall-error
(c)
592 (sb-posix:syscall-errno c
))))
593 #.sb-posix
:enametoolong
)
594 (deftest readlink.error
.5
595 (let ((string (format nil
"~v{/A~}" 2049 ;PATH_MAX/2 plus some, maybe
597 (handler-case (sb-posix:readlink string
)
598 (sb-posix:syscall-error
(c)
599 (sb-posix:syscall-errno c
))))
600 #.sb-posix
:enametoolong
)
601 (deftest readlink.error
.6
602 (let ((no-such-pathname (make-pathname :name
"readlink.error.6"
603 :defaults
*test-directory
*)))
604 (handler-case (sb-posix:readlink no-such-pathname
)
605 (sb-posix:syscall-error
(c)
606 (sb-posix:syscall-errno c
))))
608 (deftest readlink.error
.7
609 (let* ((non-link-pathname (make-pathname :name
"readlink.error.7"
610 :defaults
*test-directory
*))
611 (impossible-pathname (merge-pathnames
614 '(:relative
"readlink.error.7")
615 :name
"readlink.error.7")
617 (fd (sb-posix:open non-link-pathname sb-posix
::o-creat
)))
619 (handler-case (sb-posix:readlink impossible-pathname
)
620 (sb-posix:syscall-error
(c)
621 (sb-posix:syscall-errno c
)))
624 (sb-posix:unlink non-link-pathname
))))
629 ;; FIXME: something saner, please
630 (equal (sb-unix::posix-getcwd
) (sb-posix:getcwd
))