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 (< (- atime unix-now) 10))
245 (let* ((stat (sb-posix:stat (make-pathname :directory '(:absolute :up))))
246 (mode (sb-posix::stat-mode stat)))
247 ;; it's logically possible for / to be writeable by others... but
248 ;; if it is, either someone is playing with strange security
249 ;; modules or they want to know about it anyway.
250 (logand mode sb-posix::s-iwoth))
253 ;;; FIXME: add tests for carrying a stat structure around in the
254 ;;; optional argument to SB-POSIX:STAT
256 (deftest stat.error.1
257 (handler-case (sb-posix:stat "")
258 (sb-posix:syscall-error (c)
259 (sb-posix:syscall-errno c)))
262 (define-eacces-test stat.error.2
263 (let* ((dir (merge-pathnames
264 (make-pathname :directory '(:relative "stat.error.2"))
266 (file (merge-pathnames
267 (make-pathname :name "unstatable")
269 (sb-posix:mkdir dir +mode-rwx-all+)
270 (with-open-file (s file :direction :output)
271 (write "" :stream s))
272 (sb-posix:chmod dir 0)
275 (sb-posix:syscall-error (c)
276 (sb-posix:chmod dir (logior sb-posix::s-iread sb-posix::s-iwrite sb-posix::s-iexec))
277 (sb-posix:unlink file)
279 (sb-posix:syscall-errno c))
281 (sb-posix:chmod dir (logior sb-posix::s-iread sb-posix::s-iwrite sb-posix::s-iexec))
282 (sb-posix:unlink file)
288 (defmacro with-stat-mode ((mode pathname) &body body)
289 (let ((stat (gensym)))
290 `(let* ((,stat (sb-posix:stat ,pathname))
291 (,mode (sb-posix::stat-mode ,stat)))
294 (defmacro with-lstat-mode ((mode pathname) &body body)
295 (let ((stat (gensym)))
296 `(let* ((,stat (sb-posix:lstat ,pathname))
297 (,mode (sb-posix::stat-mode ,stat)))
301 (with-stat-mode (mode *test-directory*)
302 (sb-posix:s-isreg mode))
306 (with-stat-mode (mode *test-directory*)
307 (sb-posix:s-isdir mode))
311 (with-stat-mode (mode *test-directory*)
312 (sb-posix:s-ischr mode))
316 (with-stat-mode (mode *test-directory*)
317 (sb-posix:s-isblk mode))
321 (with-stat-mode (mode *test-directory*)
322 (sb-posix:s-isfifo mode))
327 (with-stat-mode (mode *test-directory*)
328 (sb-posix:s-issock mode))
333 (let ((link-pathname (make-pathname :name "stat-mode.7"
334 :defaults *test-directory*)))
337 (sb-posix:symlink *test-directory* link-pathname)
338 (with-lstat-mode (mode link-pathname)
339 (sb-posix:s-islnk mode)))
340 (ignore-errors (sb-posix:unlink link-pathname))))
344 (let ((pathname (make-pathname :name "stat-mode.8"
345 :defaults *test-directory*)))
348 (with-open-file (out pathname :direction :output)
349 (write-line "test" out))
350 (with-stat-mode (mode pathname)
351 (sb-posix:s-isreg mode)))
352 (ignore-errors (delete-file pathname))))
355 ;;; see comment in filename's designator definition, in macros.lisp
356 (deftest filename-designator.1
357 (let ((file (format nil "~A/[foo].txt" (namestring *test-directory*))))
358 ;; creat() with a string as argument
359 (let ((fd (sb-posix:creat file sb-posix:s-iwrite)))
362 ;; if this test fails, it will probably be with
363 ;; "System call error 2 (No such file or directory)"
364 (let ((*default-pathname-defaults* *test-directory*))
365 (sb-posix:unlink (car (directory "*.txt")))))
369 (let ((name (merge-pathnames "open-test.txt" *test-directory*)))
373 (sb-posix:creat name (logior sb-posix:s-iwrite sb-posix:s-iread)))
374 (let ((fd (sb-posix:open name sb-posix::o-rdonly)))
375 (ignore-errors (sb-posix:close fd))
377 (ignore-errors (sb-posix:unlink name))))
380 (deftest open.error.1
381 (handler-case (sb-posix:open *test-directory* sb-posix::o-wronly)
382 (sb-posix:syscall-error (c)
383 (sb-posix:syscall-errno c)))
389 #-(or (and x86-64 linux) win32)
391 (let ((fd (sb-posix:open "/dev/null" sb-posix::o-nonblock)))
392 (= (sb-posix:fcntl fd sb-posix::f-getfl) sb-posix::o-nonblock))
394 ;; On AMD64/Linux O_LARGEFILE is always set, even though the whole
395 ;; flag makes no sense.
398 (let ((fd (sb-posix:open "/dev/null" sb-posix::o-nonblock)))
399 (/= 0 (logand (sb-posix:fcntl fd sb-posix::f-getfl)
400 sb-posix::o-nonblock)))
404 (let ((dir (sb-posix:opendir "/")))
405 (unwind-protect (sb-alien:null-alien dir)
406 (unless (sb-alien:null-alien dir)
407 (sb-posix:closedir dir))))
411 (let ((dir (sb-posix:opendir "/")))
414 (loop for dirent = (sb-posix:readdir dir)
415 until (sb-alien:null-alien dirent)
416 when (not (stringp (sb-posix:dirent-name dirent)))
417 do (return-from dir-loop nil)
419 (sb-posix:closedir dir)))
422 (deftest readdir/dirent-name
423 (let ((dir (sb-posix:opendir *current-directory*)))
425 (equal (sort (loop for entry = (sb-posix:readdir dir)
426 until (sb-alien:null-alien entry)
427 collect (sb-posix:dirent-name entry))
429 (sort (append '("." "..")
431 (let ((string (enough-namestring p *current-directory*)))
432 (if (pathname-name p)
434 (subseq string 0 (1- (length string))))))
435 (directory (make-pathname
438 :defaults *current-directory*))))
440 (sb-posix:closedir dir)))
445 ;; make sure that we found something
446 (not (sb-posix:getpwuid 0))
451 ;; make sure that we found something
452 (not (sb-posix:getpwnam "root"))
456 ;; Requires root or special group + plus a sensible thing on the port
457 (deftest cfget/setispeed.1
458 (with-open-file (s "/dev/ttyS0")
459 (let* ((termios (sb-posix:tcgetattr s))
460 (old (sb-posix:cfgetispeed termios))
461 (new (if (= old sb-posix:b2400)
464 (sb-posix:cfsetispeed new termios)
465 (sb-posix:tcsetattr 0 sb-posix:tcsadrain termios)
466 (setf termios (sb-posix:tcgetattr s))
467 (= new (sb-posix:cfgetispeed termios))))
471 ;; Requires root or special group + a sensible thing on the port
472 (deftest cfget/setospeed.1
473 (with-open-file (s "/dev/ttyS0" :direction :output :if-exists :append)
474 (let* ((termios (sb-posix:tcgetattr s))
475 (old (sb-posix:cfgetospeed termios))
476 (new (if (= old sb-posix:b2400)
479 (sb-posix:cfsetospeed new termios)
480 (sb-posix:tcsetattr 0 sb-posix:tcsadrain termios)
481 (setf termios (sb-posix:tcgetattr 0))
482 (= new (sb-posix:cfgetospeed termios))))
488 (plusp (sb-posix:time))
493 (let ((file (merge-pathnames #p"utime.1" *test-directory*))
494 (atime (random (1- (expt 2 31))))
495 (mtime (random (1- (expt 2 31)))))
496 (with-open-file (stream file
498 :if-exists :supersede
499 :if-does-not-exist :create)
500 (princ "Hello, utime" stream))
501 (sb-posix:utime file atime mtime)
502 (let* ((stat (sb-posix:stat file)))
504 (list (= (sb-posix:stat-atime stat) atime)
505 (= (sb-posix:stat-mtime stat) mtime))))
510 (let ((file (merge-pathnames #p"utimes.1" *test-directory*))
511 (atime (random (1- (expt 2 31))))
512 (mtime (random (1- (expt 2 31)))))
513 (with-open-file (stream file
515 :if-exists :supersede
516 :if-does-not-exist :create)
517 (princ "Hello, utimes" stream))
518 (sb-posix:utime file atime mtime)
519 (let* ((stat (sb-posix:stat file)))
521 (list (= (sb-posix:stat-atime stat) atime)
522 (= (sb-posix:stat-mtime stat) mtime))))