1.0.37.52: better MAKE-HASH-TABLE :WEAKNESS documentation
[sbcl/pkhuong.git] / contrib / sb-posix / posix-tests.lisp
blob9739323cc3789297b7f60fae302cd0de0e27a8e5
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
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 (sb-posix:syscall-errno c)))
111 #-win32
112 #.sb-posix::eexist
113 #+win32
114 #.sb-posix:eacces)
116 (define-eacces-test mkdir.error.3
117 (let* ((dir (merge-pathnames
118 (make-pathname :directory '(:relative "mkdir.error.3"))
119 *test-directory*))
120 (dir2 (merge-pathnames
121 (make-pathname :directory '(:relative "does-not-exist"))
122 dir)))
123 (sb-posix:mkdir dir 0)
124 (handler-case
125 (sb-posix:mkdir dir2 0)
126 (sb-posix:syscall-error (c)
127 (sb-posix:rmdir dir)
128 (sb-posix:syscall-errno c))
129 (:no-error (result)
130 (sb-posix:rmdir dir2)
131 (sb-posix:rmdir dir)
132 result)))
133 #.sb-posix::eacces)
135 (deftest rmdir.1
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*)))
141 (deftest rmdir.2
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"))))
149 (handler-case
150 (sb-posix:rmdir (merge-pathnames dne *test-directory*))
151 (sb-posix:syscall-error (c)
152 (sb-posix:syscall-errno c))))
153 #.sb-posix::enoent)
155 (deftest rmdir.error.2
156 (handler-case
157 (sb-posix:rmdir *this-file*)
158 (sb-posix:syscall-error (c)
159 (sb-posix:syscall-errno c)))
160 #-win32
161 #.sb-posix::enotdir
162 #+win32
163 #.sb-posix::einval)
165 (deftest rmdir.error.3
166 (handler-case
167 (sb-posix:rmdir #-win32 "/" #+win32 "C:/")
168 (sb-posix:syscall-error (c)
169 (sb-posix:syscall-errno c)))
170 #-win32
171 #.sb-posix::ebusy
172 #+win32
173 #.sb-posix::eacces)
175 (deftest rmdir.error.4
176 (let* ((dir (ensure-directories-exist
177 (merge-pathnames
178 (make-pathname :directory '(:relative "rmdir.error.4"))
179 *test-directory*)))
180 (file (make-pathname :name "foo" :defaults dir)))
181 (with-open-file (s file :direction :output :if-exists nil)
182 (write "" :stream s))
183 (handler-case
184 (sb-posix:rmdir dir)
185 (sb-posix:syscall-error (c)
186 (delete-file file)
187 (sb-posix:rmdir dir)
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"))
196 *test-directory*))
197 (dir2 (merge-pathnames
198 (make-pathname :directory '(:relative "unremovable"))
199 dir)))
200 (sb-posix:mkdir dir +mode-rwx-all+)
201 (sb-posix:mkdir dir2 +mode-rwx-all+)
202 (sb-posix:chmod dir 0)
203 (handler-case
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)
208 (sb-posix:rmdir dir)
209 (sb-posix:syscall-errno c))
210 (:no-error (result)
211 (sb-posix:chmod dir (logior sb-posix::s-iread sb-posix::s-iwrite sb-posix::s-iexec))
212 (sb-posix:rmdir dir)
213 result)))
214 #.sb-posix::eacces)
216 (deftest stat.1
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))
223 #-win32
224 (deftest stat.2
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))
233 (deftest stat.3
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))
244 #-win32
245 (deftest stat.4
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.
255 #-win32
256 (deftest stat.5
257 (let* ((stat-1 (sb-posix:stat "/"))
258 (inode-1 (sb-posix:stat-ino stat-1))
259 (stat-2 (sb-posix:stat "/bin/sh"
260 stat-1))
261 (inode-2 (sb-posix:stat-ino stat-2)))
262 (values
263 (eq stat-1 stat-2)
264 (/= inode-1 inode-2)))
268 #+win32
269 (deftest stat.5
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"
273 stat-1))
274 (mode-2 (sb-posix:stat-mode stat-2)))
275 (values
276 (eq stat-1 stat-2)
277 (/= mode-1 mode-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)))
288 #.sb-posix::enoent)
290 (define-eacces-test stat.error.2
291 (let* ((dir (merge-pathnames
292 (make-pathname :directory '(:relative "stat.error.2"))
293 *test-directory*))
294 (file (merge-pathnames
295 (make-pathname :name "unstatable")
296 dir)))
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)
301 (handler-case
302 (sb-posix:stat file)
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)
306 (sb-posix:rmdir dir)
307 (sb-posix:syscall-errno c))
308 (:no-error (result)
309 (sb-posix:chmod dir (logior sb-posix::s-iread sb-posix::s-iwrite sb-posix::s-iexec))
310 (sb-posix:unlink file)
311 (sb-posix:rmdir dir)
312 result)))
313 #.sb-posix::eacces)
315 ;;; stat-mode tests
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)))
320 ,@body)))
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)))
326 ,@body)))
328 (deftest stat-mode.1
329 (with-stat-mode (mode *test-directory*)
330 (sb-posix:s-isreg mode))
331 nil)
333 (deftest stat-mode.2
334 (with-stat-mode (mode *test-directory*)
335 (sb-posix:s-isdir mode))
338 (deftest stat-mode.3
339 (with-stat-mode (mode *test-directory*)
340 (sb-posix:s-ischr mode))
341 nil)
343 (deftest stat-mode.4
344 (with-stat-mode (mode *test-directory*)
345 (sb-posix:s-isblk mode))
346 nil)
348 (deftest stat-mode.5
349 (with-stat-mode (mode *test-directory*)
350 (sb-posix:s-isfifo mode))
351 nil)
353 #-win32
354 (deftest stat-mode.6
355 (with-stat-mode (mode *test-directory*)
356 (sb-posix:s-issock mode))
357 nil)
359 #-win32
360 (deftest stat-mode.7
361 (let ((link-pathname (make-pathname :name "stat-mode.7"
362 :defaults *test-directory*)))
363 (unwind-protect
364 (progn
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))))
371 (deftest stat-mode.8
372 (let ((pathname (make-pathname :name "stat-mode.8"
373 :defaults *test-directory*)))
374 (unwind-protect
375 (progn
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)))
388 #+win32
389 (sb-posix:close fd))
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")))))
396 (deftest open.1
397 (let ((name (merge-pathnames "open-test.txt" *test-directory*)))
398 (unwind-protect
399 (progn
400 (sb-posix:close
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))
404 (< fd 0)))
405 (ignore-errors (sb-posix:unlink name))))
406 nil)
408 #-hpux ; fix: cant handle c-vargs
409 (deftest open.error.1
410 (handler-case (sb-posix:open *test-directory* sb-posix::o-wronly)
411 (sb-posix:syscall-error (c)
412 (sb-posix:syscall-errno c)))
413 #-win32
414 #.sb-posix::eisdir
415 #+win32
416 #.sb-posix:eacces)
418 #-(or (and x86-64 (or linux sunos)) win32)
419 (deftest fcntl.1
420 (let ((fd (sb-posix:open "/dev/null" sb-posix::o-nonblock)))
421 (= (sb-posix:fcntl fd sb-posix::f-getfl) sb-posix::o-nonblock))
423 ;; On AMD64/Linux O_LARGEFILE is always set, even though the whole
424 ;; flag makes no sense.
425 #+(and x86-64 (or linux sunos))
426 (deftest fcntl.1
427 (let ((fd (sb-posix:open "/dev/null" sb-posix::o-nonblock)))
428 (/= 0 (logand (sb-posix:fcntl fd sb-posix::f-getfl)
429 sb-posix::o-nonblock)))
432 #-(or hpux win32) ; fix: cant handle c-vargs
433 (deftest fcntl.flock.1
434 (locally (declare (sb-ext:muffle-conditions sb-ext:compiler-note))
435 (let ((flock (make-instance 'sb-posix:flock
436 :type sb-posix:f-wrlck
437 :whence sb-posix:seek-set
438 :start 0 :len 10))
439 (pathname "fcntl.flock.1")
440 kid-status)
441 (catch 'test
442 (with-open-file (f pathname :direction :output)
443 (write-line "1234567890" f)
444 (assert (zerop (sb-posix:fcntl f sb-posix:f-setlk flock)))
445 (let ((pid (sb-posix:fork)))
446 (if (zerop pid)
447 (progn
448 (multiple-value-bind (nope error)
449 (ignore-errors (sb-posix:fcntl f sb-posix:f-setlk flock))
450 (sb-ext:quit
451 :unix-status
452 (cond ((not (null nope)) 1)
453 ((= (sb-posix:syscall-errno error) sb-posix:eagain)
455 (t 86))
456 :recklessly-p t #| don't delete the file |#)))
457 (progn
458 (setf kid-status
459 (sb-posix:wexitstatus
460 (nth-value
461 1 (sb-posix:waitpid pid 0))))
462 (throw 'test nil))))))
463 kid-status))
467 #-win32
468 (deftest fcntl.flock.2
469 (locally (declare (sb-ext:muffle-conditions sb-ext:compiler-note))
470 (let ((flock (make-instance 'sb-posix:flock
471 :type sb-posix:f-wrlck
472 :whence sb-posix:seek-set
473 :start 0 :len 10))
474 (pathname "fcntl.flock.2")
475 kid-status)
476 (catch 'test
477 (with-open-file (f pathname :direction :output)
478 (write-line "1234567890" f)
479 (assert (zerop (sb-posix:fcntl f sb-posix:f-setlk flock)))
480 (let ((ppid (sb-posix:getpid))
481 (pid (sb-posix:fork)))
482 (if (zerop pid)
483 (let ((r (sb-posix:fcntl f sb-posix:f-getlk flock)))
484 (sb-ext:quit
485 :unix-status
486 (cond ((not (zerop r)) 1)
487 ((= (sb-posix:flock-pid flock) ppid) 42)
488 (t 86))
489 :recklessly-p t #| don't delete the file |#))
490 (progn
491 (setf kid-status
492 (sb-posix:wexitstatus
493 (nth-value
494 1 (sb-posix:waitpid pid 0))))
495 (throw 'test nil))))))
496 kid-status))
499 (deftest opendir.1
500 (let ((dir (sb-posix:opendir "/")))
501 (unwind-protect (sb-alien:null-alien dir)
502 (unless (sb-alien:null-alien dir)
503 (sb-posix:closedir dir))))
504 nil)
506 (deftest readdir.1
507 (let ((dir (sb-posix:opendir "/")))
508 (unwind-protect
509 (block dir-loop
510 (loop for dirent = (sb-posix:readdir dir)
511 until (sb-alien:null-alien dirent)
512 when (not (stringp (sb-posix:dirent-name dirent)))
513 do (return-from dir-loop nil)
514 finally (return t)))
515 (sb-posix:closedir dir)))
518 #-darwin
519 (deftest readdir/dirent-name
520 (let ((dir (sb-posix:opendir *current-directory*)))
521 (unwind-protect
522 (equal (sort (loop for entry = (sb-posix:readdir dir)
523 until (sb-alien:null-alien entry)
524 collect (sb-posix:dirent-name entry))
525 #'string<)
526 (sort (append '("." "..")
527 (mapcar (lambda (p)
528 (let ((string (enough-namestring p *current-directory*)))
529 (if (pathname-name p)
530 string
531 (subseq string 0 (1- (length string))))))
532 (directory (make-pathname
533 :name :wild
534 :type :wild
535 :defaults *current-directory*))))
536 #'string<))
537 (sb-posix:closedir dir)))
540 #-win32
541 (deftest pwent.1
542 ;; make sure that we found something
543 (not (sb-posix:getpwuid 0))
544 nil)
546 #-win32
547 (deftest pwent.2
548 ;; make sure that we found something
549 (not (sb-posix:getpwnam "root"))
550 nil)
552 #-win32
553 (deftest pwent.non-existing
554 ;; make sure that we get something sensible, not an error
555 (handler-case (progn (sb-posix:getpwnam "almost-certainly-does-not-exist")
556 nil)
557 (t (cond) t))
558 nil)
560 #-win32
561 (deftest grent.1
562 ;; make sure that we found something
563 (not (sb-posix:getgrgid 0))
564 nil)
566 #-win32
567 (deftest grent.2
568 ;; make sure that we found something
569 (not (sb-posix:getgrnam "sys"))
570 nil)
572 #-win32
573 (deftest grent.non-existing
574 ;; make sure that we get something sensible, not an error
575 (handler-case (progn (sb-posix:getgrnam "almost-certainly-does-not-exist")
576 nil)
577 (t (cond) t))
578 nil)
580 #+nil
581 ;; Requires root or special group + plus a sensible thing on the port
582 (deftest cfget/setispeed.1
583 (with-open-file (s "/dev/ttyS0")
584 (let* ((termios (sb-posix:tcgetattr s))
585 (old (sb-posix:cfgetispeed termios))
586 (new (if (= old sb-posix:b2400)
587 sb-posix:b9600
588 sb-posix:b2400)))
589 (sb-posix:cfsetispeed new termios)
590 (sb-posix:tcsetattr 0 sb-posix:tcsadrain termios)
591 (setf termios (sb-posix:tcgetattr s))
592 (= new (sb-posix:cfgetispeed termios))))
595 #+nil
596 ;; Requires root or special group + a sensible thing on the port
597 (deftest cfget/setospeed.1
598 (with-open-file (s "/dev/ttyS0" :direction :output :if-exists :append)
599 (let* ((termios (sb-posix:tcgetattr s))
600 (old (sb-posix:cfgetospeed termios))
601 (new (if (= old sb-posix:b2400)
602 sb-posix:b9600
603 sb-posix:b2400)))
604 (sb-posix:cfsetospeed new termios)
605 (sb-posix:tcsetattr 0 sb-posix:tcsadrain termios)
606 (setf termios (sb-posix:tcgetattr 0))
607 (= new (sb-posix:cfgetospeed termios))))
611 #-win32
612 (deftest time.1
613 (plusp (sb-posix:time))
616 #-win32
617 (deftest utimes.1
618 (let ((file (merge-pathnames #p"utimes.1" *test-directory*))
619 (atime (random (1- (expt 2 31))))
620 (mtime (random (1- (expt 2 31)))))
621 (with-open-file (stream file
622 :direction :output
623 :if-exists :supersede
624 :if-does-not-exist :create)
625 (princ "Hello, utimes" stream))
626 (sb-posix:utime file atime mtime)
627 (let* ((stat (sb-posix:stat file)))
628 (delete-file file)
629 (list (= (sb-posix:stat-atime stat) atime)
630 (= (sb-posix:stat-mtime stat) mtime))))
631 (t t))
633 ;; readlink tests.
634 #-win32
635 (progn
636 (deftest readlink.1
637 (let ((link-pathname (make-pathname :name "readlink.1"
638 :defaults *test-directory*)))
639 (sb-posix:symlink "/" link-pathname)
640 (unwind-protect
641 (sb-posix:readlink link-pathname)
642 (ignore-errors (sb-posix:unlink link-pathname))))
643 "/")
645 ;; Same thing, but with a very long link target (which doesn't have
646 ;; to exist). This tests the array adjustment in the wrapper,
647 ;; provided that the target's length is long enough.
648 #-hpux ; arg2 to readlink is 80, and arg0 is larger than that
649 (deftest readlink.2
650 (let ((target-pathname (make-pathname
651 :name (make-string 255 :initial-element #\a)
652 :directory '(:absolute)))
653 (link-pathname (make-pathname :name "readlink.2"
654 :defaults *test-directory*)))
655 (sb-posix:symlink target-pathname link-pathname)
656 (unwind-protect
657 (sb-posix:readlink link-pathname)
658 (ignore-errors (sb-posix:unlink link-pathname))))
659 #.(concatenate 'string "/" (make-string 255 :initial-element #\a)))
661 ;; The error tests are in the order of exposition from SUSv3.
662 (deftest readlink.error.1
663 (let* ((subdir-pathname (merge-pathnames
664 (make-pathname
665 :directory '(:relative "readlink.error.1"))
666 *test-directory*))
667 (link-pathname (make-pathname :name "readlink.error.1"
668 :defaults subdir-pathname)))
669 (sb-posix:mkdir subdir-pathname #o777)
670 (sb-posix:symlink "/" link-pathname)
671 (sb-posix:chmod subdir-pathname 0)
672 (unwind-protect
673 (handler-case (sb-posix:readlink link-pathname)
674 (sb-posix:syscall-error (c)
675 (sb-posix:syscall-errno c)))
676 (ignore-errors
677 (sb-posix:chmod subdir-pathname #o777)
678 (sb-posix:unlink link-pathname)
679 (sb-posix:rmdir subdir-pathname))))
680 #.sb-posix:eacces)
681 (deftest readlink.error.2
682 (let* ((non-link-pathname (make-pathname :name "readlink.error.2"
683 :defaults *test-directory*))
684 (fd (sb-posix:open non-link-pathname sb-posix::o-creat)))
685 (unwind-protect
686 (handler-case (sb-posix:readlink non-link-pathname)
687 (sb-posix:syscall-error (c)
688 (sb-posix:syscall-errno c)))
689 (ignore-errors
690 (sb-posix:close fd)
691 (sb-posix:unlink non-link-pathname))))
692 #.sb-posix:einval)
693 ;; Skipping EIO, ELOOP
694 (deftest readlink.error.3
695 (let* ((link-pathname (make-pathname :name "readlink.error.3"
696 :defaults *test-directory*))
697 (bogus-pathname (merge-pathnames
698 (make-pathname
699 :name "bogus"
700 :directory '(:relative "readlink.error.3"))
701 *test-directory*)))
702 (sb-posix:symlink link-pathname link-pathname)
703 (unwind-protect
704 (handler-case (sb-posix:readlink bogus-pathname)
705 (sb-posix:syscall-error (c)
706 (sb-posix:syscall-errno c)))
707 (ignore-errors (sb-posix:unlink link-pathname))))
708 #.sb-posix:eloop)
709 ;; Note: PATH_MAX and NAME_MAX need not be defined, and may vary, so
710 ;; failure of this test is not too meaningful.
711 (deftest readlink.error.4
712 (let ((pathname
713 (make-pathname :name (make-string 257 ;NAME_MAX plus some, maybe
714 :initial-element #\a))))
715 (handler-case (sb-posix:readlink pathname)
716 (sb-posix:syscall-error (c)
717 (sb-posix:syscall-errno c))))
718 #.sb-posix:enametoolong)
719 (deftest readlink.error.5
720 (let ((string (format nil "~v{/A~}" 2049 ;PATH_MAX/2 plus some, maybe
721 '(x))))
722 (handler-case (sb-posix:readlink string)
723 (sb-posix:syscall-error (c)
724 (sb-posix:syscall-errno c))))
725 #.sb-posix:enametoolong)
726 (deftest readlink.error.6
727 (let ((no-such-pathname (make-pathname :name "readlink.error.6"
728 :defaults *test-directory*)))
729 (handler-case (sb-posix:readlink no-such-pathname)
730 (sb-posix:syscall-error (c)
731 (sb-posix:syscall-errno c))))
732 #.sb-posix:enoent)
733 (deftest readlink.error.7
734 (let* ((non-link-pathname (make-pathname :name "readlink.error.7"
735 :defaults *test-directory*))
736 (impossible-pathname (merge-pathnames
737 (make-pathname
738 :directory
739 '(:relative "readlink.error.7")
740 :name "readlink.error.7")
741 *test-directory*))
742 (fd (sb-posix:open non-link-pathname sb-posix::o-creat)))
743 (unwind-protect
744 (handler-case (sb-posix:readlink impossible-pathname)
745 (sb-posix:syscall-error (c)
746 (sb-posix:syscall-errno c)))
747 (ignore-errors
748 (sb-posix:close fd)
749 (sb-posix:unlink non-link-pathname))))
750 #.sb-posix:enotdir)
753 (deftest getcwd.1
754 ;; FIXME: something saner, please
755 (equal (sb-unix::posix-getcwd) (sb-posix:getcwd))
758 #-win32
759 (deftest mkstemp.1
760 (multiple-value-bind (fd temp)
761 (sb-posix:mkstemp (make-pathname
762 :name "mkstemp-1"
763 :type "XXXXXX"
764 :defaults *test-directory*))
765 (let ((pathname (sb-ext:parse-native-namestring temp)))
766 (unwind-protect
767 (values (integerp fd) (pathname-name pathname))
768 (delete-file temp))))
769 t "mkstemp-1")
771 ;#-(or win32 sunos hpux)
772 ;;;; mkdtemp is unimplemented on at least Solaris 10
773 #-(or win32 hpux)
774 ;;; But it is implemented on OpenSolaris 2008.11
775 (deftest mkdtemp.1
776 (let ((pathname
777 (sb-ext:parse-native-namestring
778 (sb-posix:mkdtemp (make-pathname
779 :name "mkdtemp-1"
780 :type "XXXXXX"
781 :defaults *test-directory*))
783 *default-pathname-defaults*
784 :as-directory t)))
785 (unwind-protect
786 (values (let* ((xxx (car (last (pathname-directory pathname))))
787 (p (position #\. xxx)))
788 (and p (subseq xxx 0 p)))
789 (pathname-name pathname)
790 (pathname-type pathname))
791 (sb-posix:rmdir pathname)))
792 "mkdtemp-1" nil nil)
794 #-win32
795 (deftest mktemp.1
796 (let ((pathname (sb-ext:parse-native-namestring
797 (sb-posix:mktemp #p"mktemp.XXXXXX"))))
798 (values (equal "mktemp" (pathname-name pathname))
799 (not (equal "XXXXXX" (pathname-type pathname)))))
800 t t)
802 #-win32
803 (deftest mkstemp.null-terminate
804 (let* ((default (make-pathname :directory '(:absolute "tmp")))
805 (filename (namestring (make-pathname :name "mkstemp-1"
806 :type "XXXXXX"
807 :defaults default)))
808 ;; The magic 64 is the filename length that happens to
809 ;; trigger the no null termination bug at least on my
810 ;; machine on a certain build.
811 (n (- 64 (length (sb-ext:string-to-octets filename)))))
812 (multiple-value-bind (fd temp)
813 (sb-posix:mkstemp (make-pathname
814 :name "mkstemp-1"
815 :type (format nil "~AXXXXXX"
816 (make-string n :initial-element #\x))
817 :defaults default))
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 (deftest envstuff
825 (let ((name1 "ASLIFJLSDKFJKAHGSDKLJH")
826 (name2 "KJHFKLJDSHIUYHBSDNFCBH"))
827 (values (sb-posix:getenv name1)
828 (sb-posix:getenv name1)
829 (progn
830 (sb-posix:putenv (concatenate 'string name1 "=name1,test1"))
831 (sb-ext:gc :full t)
832 (sb-posix:getenv name1))
833 (progn
834 (sb-posix:setenv name1 "name1,test2" 0)
835 (sb-ext:gc :full t)
836 (sb-posix:getenv name1))
837 (progn
838 (sb-posix:setenv name2 "name2,test1" 0)
839 (sb-ext:gc :full t)
840 (sb-posix:getenv name2))
841 (progn
842 (sb-posix:setenv name2 "name2,test2" 1)
843 (sb-ext:gc :full t)
844 (sb-posix:getenv name2))))
847 "name1,test1"
848 "name1,test1"
849 "name2,test1"
850 "name2,test2")