Ensure that %BIT-POSITION does not read too many words
[sbcl.git] / contrib / sb-posix / posix-tests.lisp
blob0f8f5c55fcd5d951532a98cb3fafb66115b84e4e
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-output"))
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
19 #-win32
20 (logior
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)
25 #-win32
26 `(deftest ,name
27 (block ,name
28 (when (= (sb-posix:geteuid) 0)
29 (return-from ,name (values ,@values)))
30 ,form)
31 ,@values))
33 (deftest chdir.1
34 (sb-posix:chdir *test-directory*)
37 (deftest chdir.2
38 (sb-posix:chdir (namestring *test-directory*))
41 (deftest chdir.3
42 (sb-posix:chdir "/")
45 (deftest chdir.4
46 (sb-posix:chdir #p"/")
49 (deftest chdir.5
50 (sb-posix:chdir *current-directory*)
53 (deftest chdir.6
54 (sb-posix:chdir "/../")
57 (deftest chdir.7
58 (sb-posix:chdir #p"/../")
61 (deftest chdir.8
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"))))
67 (handler-case
68 (sb-posix:chdir (merge-pathnames dne *test-directory*))
69 (sb-posix:syscall-error (c)
70 (sb-posix:syscall-errno c))))
71 #.sb-posix::enoent)
73 (deftest chdir.error.2
74 (handler-case
75 (sb-posix:chdir *this-file*)
76 (sb-posix:syscall-error (c)
77 (sb-posix:syscall-errno c)))
78 #-win32
79 #.sb-posix:enotdir
80 #+win32
81 #.sb-posix:einval)
83 (deftest mkdir.1
84 (let ((dne (make-pathname :directory '(:relative "mkdir.does-not-exist.1"))))
85 (unwind-protect
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*)))))
92 (deftest mkdir.2
93 (let ((dne (make-pathname :directory '(:relative "mkdir.does-not-exist.2"))))
94 (unwind-protect
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
100 (handler-case
101 (sb-posix:mkdir *test-directory* 0)
102 (sb-posix:syscall-error (c)
103 (sb-posix:syscall-errno c)))
104 #.sb-posix::eexist)
106 (deftest mkdir.error.2
107 (handler-case
108 (sb-posix:mkdir #-win32 "/" #+win32 "C:/" 0)
109 (sb-posix:syscall-error (c)
110 ;; The results aren't the most consistent ones across platforms. Darwin
111 ;; likes EISDIR, Windows either EACCESS or EEXIST, others EEXIST.
112 ;; ...let's just accept them all.
113 (when (member (sb-posix:syscall-errno c)
114 (list #.sb-posix:eisdir
115 #.sb-posix:eacces
116 #.sb-posix::eexist)
117 :test #'eql)
118 :ok)))
119 :ok)
121 (define-eacces-test mkdir.error.3
122 (let* ((dir (merge-pathnames
123 (make-pathname :directory '(:relative "mkdir.error.3"))
124 *test-directory*))
125 (dir2 (merge-pathnames
126 (make-pathname :directory '(:relative "does-not-exist"))
127 dir)))
128 (sb-posix:mkdir dir 0)
129 (handler-case
130 (sb-posix:mkdir dir2 0)
131 (sb-posix:syscall-error (c)
132 (sb-posix:rmdir dir)
133 (sb-posix:syscall-errno c))
134 (:no-error (result)
135 (sb-posix:rmdir dir2)
136 (sb-posix:rmdir dir)
137 result)))
138 #.sb-posix::eacces)
140 (deftest rmdir.1
141 (let ((dne (make-pathname :directory '(:relative "rmdir.does-not-exist.1"))))
142 (ensure-directories-exist (merge-pathnames dne *test-directory*))
143 (sb-posix:rmdir (merge-pathnames dne *test-directory*)))
146 (deftest rmdir.2
147 (let ((dne (make-pathname :directory '(:relative "rmdir.does-not-exist.2"))))
148 (ensure-directories-exist (merge-pathnames dne *test-directory*))
149 (sb-posix:rmdir (namestring (merge-pathnames dne *test-directory*))))
152 (deftest rmdir.error.1
153 (let ((dne (make-pathname :directory '(:relative "rmdir.dne.error.1"))))
154 (handler-case
155 (sb-posix:rmdir (merge-pathnames dne *test-directory*))
156 (sb-posix:syscall-error (c)
157 (sb-posix:syscall-errno c))))
158 #.sb-posix::enoent)
160 (deftest rmdir.error.2
161 (handler-case
162 (sb-posix:rmdir *this-file*)
163 (sb-posix:syscall-error (c)
164 (sb-posix:syscall-errno c)))
165 #-win32
166 #.sb-posix::enotdir
167 #+win32
168 #.sb-posix::einval)
170 (deftest rmdir.error.3
171 (handler-case
172 (sb-posix:rmdir #-win32 "/" #+win32 (sb-ext:posix-getenv "windir"))
173 (sb-posix:syscall-error (c)
174 (typep
175 (sb-posix:syscall-errno c)
176 '(member
177 #+(or darwin openbsd)
178 #.sb-posix:eisdir
179 #+win32
180 #.sb-posix::eacces
181 #+win32
182 #.sb-posix::enotempty
183 #+sunos
184 #.sb-posix::einval
185 #-(or darwin openbsd win32 sunos)
186 #.sb-posix::ebusy)))) t)
188 (deftest rmdir.error.4
189 (let* ((dir (ensure-directories-exist
190 (merge-pathnames
191 (make-pathname :directory '(:relative "rmdir.error.4"))
192 *test-directory*)))
193 (file (make-pathname :name "foo" :defaults dir)))
194 (with-open-file (s file :direction :output :if-exists nil)
195 (write "" :stream s))
196 (handler-case
197 (sb-posix:rmdir dir)
198 (sb-posix:syscall-error (c)
199 (delete-file file)
200 (sb-posix:rmdir dir)
201 (let ((errno (sb-posix:syscall-errno c)))
202 ;; documented by POSIX
203 (or (= errno sb-posix::eexist) (= errno sb-posix::enotempty))))))
206 (define-eacces-test rmdir.error.5
207 (let* ((dir (merge-pathnames
208 (make-pathname :directory '(:relative "rmdir.error.5"))
209 *test-directory*))
210 (dir2 (merge-pathnames
211 (make-pathname :directory '(:relative "unremovable"))
212 dir)))
213 (sb-posix:mkdir dir +mode-rwx-all+)
214 (sb-posix:mkdir dir2 +mode-rwx-all+)
215 (sb-posix:chmod dir 0)
216 (handler-case
217 (sb-posix:rmdir dir2)
218 (sb-posix:syscall-error (c)
219 (sb-posix:chmod dir (logior sb-posix::s-iread sb-posix::s-iwrite sb-posix::s-iexec))
220 (sb-posix:rmdir dir2)
221 (sb-posix:rmdir dir)
222 (sb-posix:syscall-errno c))
223 (:no-error (result)
224 (sb-posix:chmod dir (logior sb-posix::s-iread sb-posix::s-iwrite sb-posix::s-iexec))
225 (sb-posix:rmdir dir)
226 result)))
227 #.sb-posix::eacces)
229 #-(or (and darwin x86) win32)
230 (deftest stat.1
231 (let* ((stat (sb-posix:stat *test-directory*))
232 (mode (sb-posix::stat-mode stat)))
233 ;; FIXME: Ugly ::s everywhere
234 (logand mode (logior sb-posix::s-iread sb-posix::s-iwrite sb-posix::s-iexec)))
235 #.(logior sb-posix::s-iread sb-posix::s-iwrite sb-posix::s-iexec))
237 #-(or (and darwin x86) win32)
238 (deftest stat.2
239 (let* ((stat (sb-posix:stat "/"))
240 (mode (sb-posix::stat-mode stat)))
241 ;; it's logically possible for / to be writeable by others... but
242 ;; if it is, either someone is playing with strange security
243 ;; modules or they want to know about it anyway.
244 (logand mode sb-posix::s-iwoth))
247 (deftest stat.3
248 (let* ((now (get-universal-time))
249 ;; FIXME: (encode-universal-time 00 00 00 01 01 1970)
250 (unix-now (- now 2208988800))
251 (stat (sb-posix:stat *test-directory*))
252 (atime (sb-posix::stat-atime stat)))
253 ;; FIXME: breaks if mounted noatime :-(
254 #+nil (< (- atime unix-now) 10)
255 (< (- atime unix-now) 10))
258 #-(or (and darwin x86) win32)
259 (deftest stat.4
260 (let* ((stat (sb-posix:stat (make-pathname :directory '(:absolute :up))))
261 (mode (sb-posix::stat-mode stat)))
262 ;; it's logically possible for / to be writeable by others... but
263 ;; if it is, either someone is playing with strange security
264 ;; modules or they want to know about it anyway.
265 (logand mode sb-posix::s-iwoth))
268 ;; Test that stat can take a second argument.
269 #-win32
270 (deftest stat.5
271 (let* ((stat-1 (sb-posix:stat "/"))
272 (inode-1 (sb-posix:stat-ino stat-1))
273 (stat-2 (sb-posix:stat "/bin/sh"
274 stat-1))
275 (inode-2 (sb-posix:stat-ino stat-2)))
276 (values
277 (eq stat-1 stat-2)
278 (/= inode-1 inode-2)))
282 #+win32
283 (deftest stat.5
284 (let ((f (namestring (merge-pathnames "some.file" *test-directory*))))
285 (close (open f :if-exists :append :if-does-not-exist :create))
286 (let* ((stat-1 (sb-posix:stat "/"))
287 (mode-1 (sb-posix:stat-mode stat-1))
288 (stat-2 (sb-posix:stat f stat-1))
289 (mode-2 (sb-posix:stat-mode stat-2)))
290 (values
291 (eq stat-1 stat-2)
292 (/= mode-1 mode-2))))
296 ;;; FIXME: add tests for carrying a stat structure around in the
297 ;;; optional argument to SB-POSIX:STAT
299 (deftest stat.error.1
300 (handler-case (sb-posix:stat "")
301 (sb-posix:syscall-error (c)
302 (sb-posix:syscall-errno c)))
303 #.sb-posix::enoent)
305 (define-eacces-test stat.error.2
306 (let* ((dir (merge-pathnames
307 (make-pathname :directory '(:relative "stat.error.2"))
308 *test-directory*))
309 (file (merge-pathnames
310 (make-pathname :name "unstatable")
311 dir)))
312 (sb-posix:mkdir dir +mode-rwx-all+)
313 (with-open-file (s file :direction :output)
314 (write "" :stream s))
315 (sb-posix:chmod dir 0)
316 (handler-case
317 (sb-posix:stat file)
318 (sb-posix:syscall-error (c)
319 (sb-posix:chmod dir (logior sb-posix::s-iread sb-posix::s-iwrite sb-posix::s-iexec))
320 (sb-posix:unlink file)
321 (sb-posix:rmdir dir)
322 (sb-posix:syscall-errno c))
323 (:no-error (result)
324 (sb-posix:chmod dir (logior sb-posix::s-iread sb-posix::s-iwrite sb-posix::s-iexec))
325 (sb-posix:unlink file)
326 (sb-posix:rmdir dir)
327 result)))
328 #.sb-posix::eacces)
330 ;;; stat-mode tests
331 (defmacro with-stat-mode ((mode pathname) &body body)
332 (let ((stat (gensym)))
333 `(let* ((,stat (sb-posix:stat ,pathname))
334 (,mode (sb-posix::stat-mode ,stat)))
335 ,@body)))
337 (defmacro with-lstat-mode ((mode pathname) &body body)
338 (let ((stat (gensym)))
339 `(let* ((,stat (sb-posix:lstat ,pathname))
340 (,mode (sb-posix::stat-mode ,stat)))
341 ,@body)))
343 (deftest stat-mode.1
344 (with-stat-mode (mode *test-directory*)
345 (sb-posix:s-isreg mode))
346 nil)
348 #-(and darwin x86)
349 (deftest stat-mode.2
350 (with-stat-mode (mode *test-directory*)
351 (sb-posix:s-isdir mode))
354 (deftest stat-mode.3
355 (with-stat-mode (mode *test-directory*)
356 (sb-posix:s-ischr mode))
357 nil)
359 (deftest stat-mode.4
360 (with-stat-mode (mode *test-directory*)
361 (sb-posix:s-isblk mode))
362 nil)
364 (deftest stat-mode.5
365 (with-stat-mode (mode *test-directory*)
366 (sb-posix:s-isfifo mode))
367 nil)
369 #-(or (and darwin x86) win32)
370 (deftest stat-mode.6
371 (with-stat-mode (mode *test-directory*)
372 (sb-posix:s-issock mode))
373 nil)
375 #-(or (and darwin x86) win32)
376 (deftest stat-mode.7
377 (let ((link-pathname (make-pathname :name "stat-mode.7"
378 :defaults *test-directory*)))
379 (unwind-protect
380 (progn
381 (sb-posix:symlink *test-directory* link-pathname)
382 (with-lstat-mode (mode link-pathname)
383 (sb-posix:s-islnk mode)))
384 (ignore-errors (sb-posix:unlink link-pathname))))
387 #-(and darwin x86)
388 (deftest stat-mode.8
389 (let ((pathname (make-pathname :name "stat-mode.8"
390 :defaults *test-directory*)))
391 (unwind-protect
392 (progn
393 (with-open-file (out pathname :direction :output)
394 (write-line "test" out))
395 (with-stat-mode (mode pathname)
396 (sb-posix:s-isreg mode)))
397 (ignore-errors (delete-file pathname))))
400 ;;; see comment in filename's designator definition, in macros.lisp
401 (deftest filename-designator.1
402 (let ((file (format nil "~A/[foo].txt" (namestring *test-directory*))))
403 ;; creat() with a string as argument
404 (let ((fd (sb-posix:creat file sb-posix:s-iwrite)))
405 #+win32
406 (sb-posix:close fd))
407 ;; if this test fails, it will probably be with
408 ;; "System call error 2 (No such file or directory)"
409 (let ((*default-pathname-defaults* *test-directory*))
410 (sb-posix:unlink (car (directory "*.txt")))))
413 (deftest open.1
414 (let ((name (merge-pathnames "open-test.txt" *test-directory*)))
415 (unwind-protect
416 (progn
417 (sb-posix:close
418 (sb-posix:creat name (logior sb-posix:s-iwrite sb-posix:s-iread)))
419 (let ((fd (sb-posix:open name sb-posix::o-rdonly)))
420 (ignore-errors (sb-posix:close fd))
421 (< fd 0)))
422 (ignore-errors (sb-posix:unlink name))))
423 nil)
425 #-hpux ; fix: cant handle c-vargs
426 (deftest open.error.1
427 (handler-case (sb-posix:open *test-directory* sb-posix::o-wronly)
428 (sb-posix:syscall-error (c)
429 (sb-posix:syscall-errno c)))
430 #-win32
431 #.sb-posix::eisdir
432 #+win32
433 #.sb-posix:eacces)
435 #-(or (and (or x86-64 arm64 alpha) (or linux sunos)) win32)
436 (deftest fcntl.1
437 (let ((fd (sb-posix:open "/dev/null" sb-posix::o-nonblock)))
438 (= (sb-posix:fcntl fd sb-posix::f-getfl) sb-posix::o-nonblock))
440 ;; On AMD64/Linux O_LARGEFILE is always set, even though the whole
441 ;; flag makes no sense.
442 #+(and (or x86-64 arm64 alpha) (or linux sunos))
443 (deftest fcntl.1
444 (let ((fd (sb-posix:open "/dev/null" sb-posix::o-nonblock)))
445 (/= 0 (logand (sb-posix:fcntl fd sb-posix::f-getfl)
446 sb-posix::o-nonblock)))
449 #-(or hpux win32 netbsd) ; fix: cant handle c-vargs
450 (deftest fcntl.flock.1
451 (locally (declare (sb-ext:muffle-conditions sb-ext:compiler-note))
452 (let ((flock (make-instance 'sb-posix:flock
453 :type sb-posix:f-wrlck
454 :whence sb-posix:seek-set
455 :start 0 :len 10))
456 (pathname "fcntl.flock.1")
457 kid-status)
458 (catch 'test
459 (with-open-file (f pathname :direction :output)
460 (write-line "1234567890" f)
461 (assert (zerop (sb-posix:fcntl f sb-posix:f-setlk flock)))
462 (let ((pid (sb-posix:fork)))
463 (if (zerop pid)
464 (progn
465 (multiple-value-bind (nope error)
466 (ignore-errors (sb-posix:fcntl f sb-posix:f-setlk flock))
467 (sb-ext:exit
468 :code
469 (cond ((not (null nope)) 1)
470 ((= (sb-posix:syscall-errno error) sb-posix:eagain)
472 (t 86))
473 :abort t #| don't delete the file |#)))
474 (progn
475 (setf kid-status
476 (sb-posix:wexitstatus
477 (nth-value
478 1 (sb-posix:waitpid pid 0))))
479 (throw 'test nil))))))
480 kid-status))
484 #-(or win32 netbsd)
485 (deftest fcntl.flock.2
486 (locally (declare (sb-ext:muffle-conditions sb-ext:compiler-note))
487 (let ((flock (make-instance 'sb-posix:flock
488 :type sb-posix:f-wrlck
489 :whence sb-posix:seek-set
490 :start 0 :len 10))
491 (pathname "fcntl.flock.2")
492 kid-status)
493 (catch 'test
494 (with-open-file (f pathname :direction :output)
495 (write-line "1234567890" f)
496 (assert (zerop (sb-posix:fcntl f sb-posix:f-setlk flock)))
497 (let ((ppid (sb-posix:getpid))
498 (pid (sb-posix:fork)))
499 (if (zerop pid)
500 (let ((r (sb-posix:fcntl f sb-posix:f-getlk flock)))
501 (sb-ext:exit
502 :code
503 (cond ((not (zerop r)) 1)
504 ((= (sb-posix:flock-pid flock) ppid) 42)
505 (t 86))
506 :abort t #| don't delete the file |#))
507 (progn
508 (setf kid-status
509 (sb-posix:wexitstatus
510 (nth-value
511 1 (sb-posix:waitpid pid 0))))
512 (throw 'test nil))))))
513 kid-status))
516 (deftest read.1
517 (progn
518 (with-open-file (ouf (merge-pathnames "read-test.txt" *test-directory*)
519 :direction :output
520 :if-exists :supersede
521 :if-does-not-exist :create)
522 (write-string "foo" ouf))
523 (let ((fd (sb-posix:open (merge-pathnames "read-test.txt" *test-directory*) sb-posix:o-rdonly)))
524 (unwind-protect
525 (let ((buf (make-array 10 :element-type '(unsigned-byte 8))))
526 (values
527 (sb-posix:read fd (sb-sys:vector-sap buf) 10)
528 (code-char (aref buf 0))
529 (code-char (aref buf 1))
530 (code-char (aref buf 2))))
531 (sb-posix:close fd))))
532 3 #\f #\o #\o)
534 (deftest opendir.1
535 (let ((dir (sb-posix:opendir "/")))
536 (unwind-protect (sb-alien:null-alien dir)
537 (unless (sb-alien:null-alien dir)
538 (sb-posix:closedir dir))))
539 nil)
541 #-(and darwin x86)
542 (deftest readdir.1
543 (let ((dir (sb-posix:opendir "/")))
544 (unwind-protect
545 (block dir-loop
546 (loop for dirent = (sb-posix:readdir dir)
547 until (sb-alien:null-alien dirent)
548 when (not (stringp (sb-posix:dirent-name dirent)))
549 do (return-from dir-loop nil)
550 finally (return t)))
551 (sb-posix:closedir dir)))
554 #-darwin
555 (deftest readdir/dirent-name
556 (let ((dir (sb-posix:opendir *current-directory*)))
557 (unwind-protect
558 (equal (sort (loop for entry = (sb-posix:readdir dir)
559 until (sb-alien:null-alien entry)
560 collect (sb-posix:dirent-name entry))
561 #'string<)
562 (sort (append '("." "..")
563 (mapcar (lambda (p)
564 (let ((string (enough-namestring p *current-directory*)))
565 (if (pathname-name p)
566 string
567 (subseq string 0 (1- (length string))))))
568 (directory (make-pathname
569 :name :wild
570 :type :wild
571 :defaults *current-directory*))))
572 #'string<))
573 (sb-posix:closedir dir)))
577 (deftest write.1
578 (progn
579 (let ((fd (sb-posix:open (merge-pathnames "write-test.txt" *test-directory*)
580 (logior sb-posix:o-creat sb-posix:o-wronly)
581 (logior sb-posix:s-irusr sb-posix:s-iwusr)))
582 (retval nil))
583 (unwind-protect
584 (let ((buf (coerce "foo" 'simple-base-string)))
585 (setf retval (sb-posix:write fd (sb-sys:vector-sap buf) 3)))
586 (sb-posix:close fd))
588 (with-open-file (inf (merge-pathnames "write-test.txt" *test-directory*)
589 :direction :input)
590 (values retval (read-line inf)))))
591 3 "foo")
593 #-(or android win32)
594 (deftest pwent.1
595 ;; make sure that we found something
596 (not (sb-posix:getpwuid 0))
597 nil)
599 #-(or android win32)
600 (deftest pwent.2
601 ;; make sure that we found something
602 (not (sb-posix:getpwnam "root"))
603 nil)
605 #-(or android win32)
606 (deftest pwent.non-existing
607 ;; make sure that we get something sensible, not an error
608 (handler-case (progn (sb-posix:getpwnam "almost-certainly-does-not-exist")
609 nil)
610 (t (cond) t))
611 nil)
613 #-(or android win32)
614 (deftest grent.1
615 ;; make sure that we found something
616 (not (sb-posix:getgrgid 0))
617 nil)
619 #-(or android win32)
620 (deftest grent.2
621 ;; make sure that we found something
622 (not (sb-posix:getgrnam "sys"))
623 nil)
625 #-(or android win32)
626 (deftest grent.non-existing
627 ;; make sure that we get something sensible, not an error
628 (handler-case (progn (sb-posix:getgrnam "almost-certainly-does-not-exist")
629 nil)
630 (t (cond) t))
631 nil)
633 #+nil
634 ;; Requires root or special group + plus a sensible thing on the port
635 (deftest cfget/setispeed.1
636 (with-open-file (s "/dev/ttyS0")
637 (let* ((termios (sb-posix:tcgetattr s))
638 (old (sb-posix:cfgetispeed termios))
639 (new (if (= old sb-posix:b2400)
640 sb-posix:b9600
641 sb-posix:b2400)))
642 (sb-posix:cfsetispeed new termios)
643 (sb-posix:tcsetattr 0 sb-posix:tcsadrain termios)
644 (setf termios (sb-posix:tcgetattr s))
645 (= new (sb-posix:cfgetispeed termios))))
648 #+nil
649 ;; Requires root or special group + a sensible thing on the port
650 (deftest cfget/setospeed.1
651 (with-open-file (s "/dev/ttyS0" :direction :output :if-exists :append)
652 (let* ((termios (sb-posix:tcgetattr s))
653 (old (sb-posix:cfgetospeed termios))
654 (new (if (= old sb-posix:b2400)
655 sb-posix:b9600
656 sb-posix:b2400)))
657 (sb-posix:cfsetospeed new termios)
658 (sb-posix:tcsetattr 0 sb-posix:tcsadrain termios)
659 (setf termios (sb-posix:tcgetattr 0))
660 (= new (sb-posix:cfgetospeed termios))))
664 #-win32
665 (deftest time.1
666 (plusp (sb-posix:time))
669 #-(or (and darwin x86) win32)
670 (deftest utimes.1
671 (let ((file (merge-pathnames #p"utimes.1" *test-directory*))
672 (atime (random (1- (expt 2 31))))
673 (mtime (random (1- (expt 2 31)))))
674 (with-open-file (stream file
675 :direction :output
676 :if-exists :supersede
677 :if-does-not-exist :create)
678 (princ "Hello, utimes" stream))
679 (sb-posix:utime file atime mtime)
680 (let* ((stat (sb-posix:stat file)))
681 (delete-file file)
682 (list (= (sb-posix:stat-atime stat) atime)
683 (= (sb-posix:stat-mtime stat) mtime))))
684 (t t))
686 ;; readlink tests.
687 #-win32
688 (progn
689 (deftest readlink.1
690 (let ((link-pathname (make-pathname :name "readlink.1"
691 :defaults *test-directory*)))
692 (sb-posix:symlink "/" link-pathname)
693 (unwind-protect
694 (sb-posix:readlink link-pathname)
695 (ignore-errors (sb-posix:unlink link-pathname))))
696 "/")
698 ;; Same thing, but with a very long link target (which doesn't have
699 ;; to exist). This tests the array adjustment in the wrapper,
700 ;; provided that the target's length is long enough.
701 #-hpux ; arg2 to readlink is 80, and arg0 is larger than that
702 (deftest readlink.2
703 (let ((target-pathname (make-pathname
704 :name (make-string 255 :initial-element #\a)
705 :directory '(:absolute)))
706 (link-pathname (make-pathname :name "readlink.2"
707 :defaults *test-directory*)))
708 (sb-posix:symlink target-pathname link-pathname)
709 (unwind-protect
710 (sb-posix:readlink link-pathname)
711 (ignore-errors (sb-posix:unlink link-pathname))))
712 #.(concatenate 'string "/" (make-string 255 :initial-element #\a)))
714 ;; The error tests are in the order of exposition from SUSv3.
715 (deftest readlink.error.1
716 (let* ((subdir-pathname (merge-pathnames
717 (make-pathname
718 :directory '(:relative "readlink.error.1"))
719 *test-directory*))
720 (link-pathname (make-pathname :name "readlink.error.1"
721 :defaults subdir-pathname)))
722 (sb-posix:mkdir subdir-pathname #o777)
723 (sb-posix:symlink "/" link-pathname)
724 (sb-posix:chmod subdir-pathname 0)
725 (unwind-protect
726 (handler-case (sb-posix:readlink link-pathname)
727 (sb-posix:syscall-error (c)
728 (sb-posix:syscall-errno c)))
729 (ignore-errors
730 (sb-posix:chmod subdir-pathname #o777)
731 (sb-posix:unlink link-pathname)
732 (sb-posix:rmdir subdir-pathname))))
733 #.sb-posix:eacces)
734 (deftest readlink.error.2
735 (let* ((non-link-pathname (make-pathname :name "readlink.error.2"
736 :defaults *test-directory*))
737 (fd (sb-posix:open non-link-pathname sb-posix::o-creat)))
738 (unwind-protect
739 (handler-case (sb-posix:readlink non-link-pathname)
740 (sb-posix:syscall-error (c)
741 (sb-posix:syscall-errno c)))
742 (ignore-errors
743 (sb-posix:close fd)
744 (sb-posix:unlink non-link-pathname))))
745 #.sb-posix:einval)
746 ;; Skipping EIO, ELOOP
747 (deftest readlink.error.3
748 (let* ((link-pathname (make-pathname :name "readlink.error.3"
749 :defaults *test-directory*))
750 (bogus-pathname (merge-pathnames
751 (make-pathname
752 :name "bogus"
753 :directory '(:relative "readlink.error.3"))
754 *test-directory*)))
755 (sb-posix:symlink link-pathname link-pathname)
756 (unwind-protect
757 (handler-case (sb-posix:readlink bogus-pathname)
758 (sb-posix:syscall-error (c)
759 (sb-posix:syscall-errno c)))
760 (ignore-errors (sb-posix:unlink link-pathname))))
761 #.sb-posix:eloop)
762 ;; Note: PATH_MAX and NAME_MAX need not be defined, and may vary, so
763 ;; failure of this test is not too meaningful.
764 (deftest readlink.error.4
765 (let ((pathname
766 (make-pathname :name (make-string 257 ;NAME_MAX plus some, maybe
767 :initial-element #\a))))
768 (handler-case (sb-posix:readlink pathname)
769 (sb-posix:syscall-error (c)
770 (sb-posix:syscall-errno c))))
771 #.sb-posix:enametoolong)
772 (deftest readlink.error.5
773 (let ((string (format nil "~v{/A~}" 2049 ;PATH_MAX/2 plus some, maybe
774 '(x))))
775 (handler-case (sb-posix:readlink string)
776 (sb-posix:syscall-error (c)
777 (sb-posix:syscall-errno c))))
778 #.sb-posix:enametoolong)
779 (deftest readlink.error.6
780 (let ((no-such-pathname (make-pathname :name "readlink.error.6"
781 :defaults *test-directory*)))
782 (handler-case (sb-posix:readlink no-such-pathname)
783 (sb-posix:syscall-error (c)
784 (sb-posix:syscall-errno c))))
785 #.sb-posix:enoent)
786 (deftest readlink.error.7
787 (let* ((non-link-pathname (make-pathname :name "readlink.error.7"
788 :defaults *test-directory*))
789 (impossible-pathname (merge-pathnames
790 (make-pathname
791 :directory
792 '(:relative "readlink.error.7")
793 :name "readlink.error.7")
794 *test-directory*))
795 (fd (sb-posix:open non-link-pathname sb-posix::o-creat)))
796 (unwind-protect
797 (handler-case (sb-posix:readlink impossible-pathname)
798 (sb-posix:syscall-error (c)
799 (sb-posix:syscall-errno c)))
800 (ignore-errors
801 (sb-posix:close fd)
802 (sb-posix:unlink non-link-pathname))))
803 #.sb-posix:enotdir)
806 (deftest getcwd.1
807 ;; FIXME: something saner, please
808 (equal (sb-unix::posix-getcwd) (sb-posix:getcwd))
811 #-win32
812 (deftest mkstemp.1
813 (multiple-value-bind (fd temp)
814 (sb-posix:mkstemp (make-pathname
815 :name "mkstemp-1"
816 :type "XXXXXX"
817 :defaults *test-directory*))
818 (let ((pathname (sb-ext:parse-native-namestring temp)))
819 (unwind-protect
820 (values (integerp fd) (pathname-name pathname))
821 (delete-file temp))))
822 t "mkstemp-1")
824 ;#-(or win32 sunos hpux)
825 ;;;; mkdtemp is unimplemented on at least Solaris 10
826 #-(or win32 hpux sunos)
827 ;;; But it is implemented on OpenSolaris 2008.11
828 (deftest mkdtemp.1
829 (let ((pathname
830 (sb-ext:parse-native-namestring
831 (sb-posix:mkdtemp (make-pathname
832 :name "mkdtemp-1"
833 :type "XXXXXX"
834 :defaults *test-directory*))
836 *default-pathname-defaults*
837 :as-directory t)))
838 (unwind-protect
839 (values (let* ((xxx (car (last (pathname-directory pathname))))
840 (p (position #\. xxx)))
841 (and p (subseq xxx 0 p)))
842 (pathname-name pathname)
843 (pathname-type pathname))
844 (sb-posix:rmdir pathname)))
845 "mkdtemp-1" nil nil)
847 #-win32
848 (deftest mktemp.1
849 (let ((pathname (sb-ext:parse-native-namestring
850 (sb-posix:mktemp #p"mktemp.XXXXXX"))))
851 (values (equal "mktemp" (pathname-name pathname))
852 (not (equal "XXXXXX" (pathname-type pathname)))))
853 t t)
855 #-win32
856 (deftest mkstemp.null-terminate
857 (let* ((default (make-pathname :directory '(:absolute "tmp")))
858 (filename (namestring (make-pathname :name "mkstemp-1"
859 :type "XXXXXX"
860 :defaults default)))
861 ;; The magic 64 is the filename length that happens to
862 ;; trigger the no null termination bug at least on my
863 ;; machine on a certain build.
864 (n (- 64 (length (sb-ext:string-to-octets filename)))))
865 (multiple-value-bind (fd temp)
866 (sb-posix:mkstemp (make-pathname
867 :name "mkstemp-1"
868 :type (format nil "~AXXXXXX"
869 (make-string n :initial-element #\x))
870 :defaults default))
871 (unwind-protect
872 (values (integerp fd) (subseq temp 0 (position #\. temp)))
873 (delete-file temp))))
874 t "/tmp/mkstemp-1")
876 (deftest envstuff
877 (let ((name1 "ASLIFJLSDKFJKAHGSDKLJH")
878 (name2 "KJHFKLJDSHIUYHBSDNFCBH"))
879 (values (sb-posix:getenv name1)
880 (sb-posix:getenv name1)
881 (progn
882 (sb-posix:putenv (concatenate 'string name1 "=name1,test1"))
883 (sb-ext:gc :full t)
884 (sb-posix:getenv name1))
885 (progn
886 (sb-posix:setenv name1 "name1,test2" 0)
887 (sb-ext:gc :full t)
888 (sb-posix:getenv name1))
889 (progn
890 (sb-posix:setenv name2 "name2,test1" 0)
891 (sb-ext:gc :full t)
892 (sb-posix:getenv name2))
893 (progn
894 (sb-posix:setenv name2 "name2,test2" 1)
895 (sb-ext:gc :full t)
896 (sb-posix:getenv name2))))
899 "name1,test1"
900 "name1,test1"
901 "name2,test1"
902 "name2,test2")