1.0.15.37: Windows fixes.
[sbcl.git] / contrib / sb-posix / posix-tests.lisp
1 (defpackage "SB-POSIX-TESTS"
2   (:use "COMMON-LISP" "SB-RT"))
3
4 (in-package "SB-POSIX-TESTS")
5
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*)))))
11
12 (defvar *current-directory* *default-pathname-defaults*)
13
14 (defvar *this-file* *load-truename*)
15
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))))
23
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))
32 \f
33 (deftest chdir.1
34   (sb-posix:chdir *test-directory*)
35   0)
36
37 (deftest chdir.2
38     (sb-posix:chdir (namestring *test-directory*))
39   0)
40
41 (deftest chdir.3
42     (sb-posix:chdir "/")
43   0)
44
45 (deftest chdir.4
46     (sb-posix:chdir #p"/")
47   0)
48
49 (deftest chdir.5
50     (sb-posix:chdir *current-directory*)
51   0)
52
53 (deftest chdir.6
54   (sb-posix:chdir "/../")
55   0)
56
57 (deftest chdir.7
58   (sb-posix:chdir #p"/../")
59   0)
60
61 (deftest chdir.8
62   (sb-posix:chdir (make-pathname :directory '(:absolute :up)))
63   0)
64
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)
72
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)
82 \f
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*)))))
90   0)
91
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*)))))
97   0)
98
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)
105
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)
115
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)
134 \f
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*)))
139   0)
140
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*))))
145   0)
146
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)
154
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)
164
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)
174
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))))))
191   t)
192
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)
215 \f
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))
222
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))
231   0)
232
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))
242   t)
243
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))
252   0)
253
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)))
265   t
266   t)
267
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)))
278   t
279   t)
280
281 ;;; FIXME: add tests for carrying a stat structure around in the
282 ;;; optional argument to SB-POSIX:STAT
283
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)
289
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)
314 \f
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)))
321
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)))
327
328 (deftest stat-mode.1
329   (with-stat-mode (mode *test-directory*)
330     (sb-posix:s-isreg mode))
331   nil)
332
333 (deftest stat-mode.2
334   (with-stat-mode (mode *test-directory*)
335     (sb-posix:s-isdir mode))
336   t)
337
338 (deftest stat-mode.3
339   (with-stat-mode (mode *test-directory*)
340     (sb-posix:s-ischr mode))
341   nil)
342
343 (deftest stat-mode.4
344   (with-stat-mode (mode *test-directory*)
345     (sb-posix:s-isblk mode))
346   nil)
347
348 (deftest stat-mode.5
349   (with-stat-mode (mode *test-directory*)
350     (sb-posix:s-isfifo mode))
351   nil)
352
353 #-win32
354 (deftest stat-mode.6
355   (with-stat-mode (mode *test-directory*)
356     (sb-posix:s-issock mode))
357   nil)
358
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))))
369   t)
370
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))))
381   t)
382 \f
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")))))
394   0)
395 \f
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)
407
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)))
412   #-win32
413   #.sb-posix::eisdir
414   #+win32
415   #.sb-posix:eacces)
416
417 #-(or (and x86-64 linux) win32)
418 (deftest fcntl.1
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))
421   t)
422 ;; On AMD64/Linux O_LARGEFILE is always set, even though the whole
423 ;; flag makes no sense.
424 #+(and x86-64 linux)
425 (deftest fcntl.1
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)))
429   t)
430
431 (deftest fcntl.flock.1
432     (locally (declare (sb-ext:muffle-conditions sb-ext:compiler-note))
433       (let ((flock (make-instance 'sb-posix:flock
434                       :type sb-posix:f-wrlck
435                       :whence sb-posix:seek-set
436                       :start 0 :len 10))
437             (pathname "fcntl.flock.1")
438             kid-status)
439         (catch 'test
440           (with-open-file (f pathname :direction :output)
441             (write-line "1234567890" f)
442             (assert (zerop (sb-posix:fcntl f sb-posix:f-setlk flock)))
443             (let ((pid (sb-posix:fork)))
444               (if (zerop pid)
445                   (progn
446                     (multiple-value-bind (nope error)
447                         (ignore-errors (sb-posix:fcntl f sb-posix:f-setlk flock))
448                       (sb-ext:quit
449                        :unix-status
450                        (cond ((not (null nope)) 1)
451                              ((= (sb-posix:syscall-errno error) sb-posix:eagain)
452                               42)
453                              (t 86))
454                        :recklessly-p t #| don't delete the file |#)))
455                   (progn
456                     (setf kid-status
457                           (sb-posix:wexitstatus
458                            (nth-value
459                             1 (sb-posix:waitpid pid 0))))
460                     (throw 'test nil))))))
461         kid-status))
462   42)
463
464
465 (deftest fcntl.flock.2
466     (locally (declare (sb-ext:muffle-conditions sb-ext:compiler-note))
467       (let ((flock (make-instance 'sb-posix:flock
468                       :type sb-posix:f-wrlck
469                       :whence sb-posix:seek-set
470                       :start 0 :len 10))
471             (pathname "fcntl.flock.2")
472             kid-status)
473         (catch 'test
474           (with-open-file (f pathname :direction :output)
475             (write-line "1234567890" f)
476             (assert (zerop (sb-posix:fcntl f sb-posix:f-setlk flock)))
477             (let ((ppid (sb-posix:getpid))
478                   (pid (sb-posix:fork)))
479               (if (zerop pid)
480                   (let ((r (sb-posix:fcntl f sb-posix:f-getlk flock)))
481                     (sb-ext:quit
482                      :unix-status
483                      (cond ((not (zerop r)) 1)
484                            ((= (sb-posix:flock-pid flock) ppid) 42)
485                            (t 86))
486                      :recklessly-p t #| don't delete the file |#))
487                   (progn
488                     (setf kid-status
489                           (sb-posix:wexitstatus
490                            (nth-value
491                             1 (sb-posix:waitpid pid 0))))
492                     (throw 'test nil))))))
493         kid-status))
494   42)
495
496 (deftest opendir.1
497   (let ((dir (sb-posix:opendir "/")))
498     (unwind-protect (sb-alien:null-alien dir)
499       (unless (sb-alien:null-alien dir)
500         (sb-posix:closedir dir))))
501   nil)
502
503 (deftest readdir.1
504   (let ((dir (sb-posix:opendir "/")))
505     (unwind-protect
506        (block dir-loop
507          (loop for dirent = (sb-posix:readdir dir)
508                until (sb-alien:null-alien dirent)
509                when (not (stringp (sb-posix:dirent-name dirent)))
510                  do (return-from dir-loop nil)
511                finally (return t)))
512       (sb-posix:closedir dir)))
513   t)
514
515 (deftest readdir/dirent-name
516     (let ((dir (sb-posix:opendir *current-directory*)))
517       (unwind-protect
518            (equal (sort (loop for entry = (sb-posix:readdir dir)
519                            until (sb-alien:null-alien entry)
520                            collect (sb-posix:dirent-name entry))
521                         #'string<)
522                   (sort (append '("." "..")
523                                 (mapcar (lambda (p)
524                                           (let ((string (enough-namestring p *current-directory*)))
525                                             (if (pathname-name p)
526                                                 string
527                                                 (subseq string 0 (1- (length string))))))
528                                         (directory (make-pathname
529                                                     :name :wild
530                                                     :type :wild
531                                                     :defaults *current-directory*))))
532                         #'string<))
533         (sb-posix:closedir dir)))
534   t)
535
536 #-win32
537 (deftest pwent.1
538   ;; make sure that we found something
539   (not (sb-posix:getpwuid 0))
540   nil)
541
542 #-win32
543 (deftest pwent.2
544   ;; make sure that we found something
545   (not (sb-posix:getpwnam "root"))
546   nil)
547
548 #-win32
549 (deftest pwent.non-existing
550     ;; make sure that we get something sensible, not an error
551     (handler-case (progn (sb-posix:getpwnam "almost-certainly-does-not-exist")
552                          nil)
553       (t (cond) t))
554   nil)
555
556 #-win32
557 (deftest grent.1
558   ;; make sure that we found something
559   (not (sb-posix:getgrgid 0))
560   nil)
561
562 #-win32
563 (deftest grent.2
564   ;; make sure that we found something
565   (not (sb-posix:getgrnam "sys"))
566   nil)
567
568 #-win32
569 (deftest grent.non-existing
570     ;; make sure that we get something sensible, not an error
571     (handler-case (progn (sb-posix:getgrnam "almost-certainly-does-not-exist")
572                          nil)
573       (t (cond) t))
574   nil)
575
576 #+nil
577 ;; Requires root or special group + plus a sensible thing on the port
578 (deftest cfget/setispeed.1
579     (with-open-file (s "/dev/ttyS0")
580       (let* ((termios (sb-posix:tcgetattr s))
581              (old (sb-posix:cfgetispeed termios))
582              (new (if (= old sb-posix:b2400)
583                       sb-posix:b9600
584                       sb-posix:b2400)))
585         (sb-posix:cfsetispeed new termios)
586         (sb-posix:tcsetattr 0 sb-posix:tcsadrain termios)
587         (setf termios (sb-posix:tcgetattr s))
588         (= new (sb-posix:cfgetispeed termios))))
589   t)
590
591 #+nil
592 ;; Requires root or special group + a sensible thing on the port
593 (deftest cfget/setospeed.1
594     (with-open-file (s "/dev/ttyS0" :direction :output :if-exists :append)
595       (let* ((termios (sb-posix:tcgetattr s))
596              (old (sb-posix:cfgetospeed termios))
597              (new (if (= old sb-posix:b2400)
598                       sb-posix:b9600
599                       sb-posix:b2400)))
600         (sb-posix:cfsetospeed new termios)
601         (sb-posix:tcsetattr 0 sb-posix:tcsadrain termios)
602         (setf termios (sb-posix:tcgetattr 0))
603         (= new (sb-posix:cfgetospeed termios))))
604   t)
605
606
607 #-win32
608 (deftest time.1
609     (plusp (sb-posix:time))
610   t)
611
612 #-win32
613 (deftest utimes.1
614     (let ((file (merge-pathnames #p"utimes.1" *test-directory*))
615           (atime (random (1- (expt 2 31))))
616           (mtime (random (1- (expt 2 31)))))
617       (with-open-file (stream file
618                        :direction :output
619                        :if-exists :supersede
620                        :if-does-not-exist :create)
621         (princ "Hello, utimes" stream))
622       (sb-posix:utime file atime mtime)
623       (let* ((stat (sb-posix:stat file)))
624         (delete-file file)
625         (list (= (sb-posix:stat-atime stat) atime)
626               (= (sb-posix:stat-mtime stat) mtime))))
627   (t t))
628 \f
629 ;; readlink tests.
630 #-win32
631 (progn
632   (deftest readlink.1
633       (let ((link-pathname (make-pathname :name "readlink.1"
634                                           :defaults *test-directory*)))
635         (sb-posix:symlink "/" link-pathname)
636         (unwind-protect
637              (sb-posix:readlink link-pathname)
638           (ignore-errors (sb-posix:unlink link-pathname))))
639     "/")
640
641   ;; Same thing, but with a very long link target (which doesn't have
642   ;; to exist).  This tests the array adjustment in the wrapper,
643   ;; provided that the target's length is long enough.
644   (deftest readlink.2
645       (let ((target-pathname (make-pathname
646                               :name (make-string 255 :initial-element #\a)
647                               :directory '(:absolute)))
648             (link-pathname (make-pathname :name "readlink.2"
649                                           :defaults *test-directory*)))
650         (sb-posix:symlink target-pathname link-pathname)
651         (unwind-protect
652              (sb-posix:readlink link-pathname)
653           (ignore-errors (sb-posix:unlink link-pathname))))
654     #.(concatenate 'string "/" (make-string 255 :initial-element #\a)))
655
656   ;; The error tests are in the order of exposition from SUSv3.
657   (deftest readlink.error.1
658       (let* ((subdir-pathname (merge-pathnames
659                                (make-pathname
660                                 :directory '(:relative "readlink.error.1"))
661                                *test-directory*))
662              (link-pathname (make-pathname :name "readlink.error.1"
663                                            :defaults subdir-pathname)))
664         (sb-posix:mkdir subdir-pathname #o777)
665         (sb-posix:symlink "/" link-pathname)
666         (sb-posix:chmod subdir-pathname 0)
667         (unwind-protect
668              (handler-case (sb-posix:readlink link-pathname)
669                (sb-posix:syscall-error (c)
670                  (sb-posix:syscall-errno c)))
671           (ignore-errors
672             (sb-posix:chmod subdir-pathname #o777)
673             (sb-posix:unlink link-pathname)
674             (sb-posix:rmdir subdir-pathname))))
675     #.sb-posix:eacces)
676   (deftest readlink.error.2
677       (let* ((non-link-pathname (make-pathname :name "readlink.error.2"
678                                                :defaults *test-directory*))
679              (fd (sb-posix:open non-link-pathname sb-posix::o-creat)))
680         (unwind-protect
681              (handler-case (sb-posix:readlink non-link-pathname)
682                (sb-posix:syscall-error (c)
683                  (sb-posix:syscall-errno c)))
684           (ignore-errors
685             (sb-posix:close fd)
686             (sb-posix:unlink non-link-pathname))))
687     #.sb-posix:einval)
688   ;; Skipping EIO, ELOOP
689   (deftest readlink.error.3
690       (let* ((link-pathname (make-pathname :name "readlink.error.3"
691                                            :defaults *test-directory*))
692              (bogus-pathname (merge-pathnames
693                               (make-pathname
694                                :name "bogus"
695                                :directory '(:relative "readlink.error.3"))
696                                *test-directory*)))
697         (sb-posix:symlink link-pathname link-pathname)
698         (unwind-protect
699              (handler-case (sb-posix:readlink bogus-pathname)
700                (sb-posix:syscall-error (c)
701                  (sb-posix:syscall-errno c)))
702           (ignore-errors (sb-posix:unlink link-pathname))))
703     #.sb-posix:eloop)
704   ;; Note: PATH_MAX and NAME_MAX need not be defined, and may vary, so
705   ;; failure of this test is not too meaningful.
706   (deftest readlink.error.4
707       (let ((pathname
708              (make-pathname :name (make-string 257 ;NAME_MAX plus some, maybe
709                                                :initial-element #\a))))
710         (handler-case (sb-posix:readlink pathname)
711           (sb-posix:syscall-error (c)
712             (sb-posix:syscall-errno c))))
713     #.sb-posix:enametoolong)
714   (deftest readlink.error.5
715       (let ((string (format nil "~v{/A~}" 2049 ;PATH_MAX/2 plus some, maybe
716                                           '(x))))
717         (handler-case (sb-posix:readlink string)
718           (sb-posix:syscall-error (c)
719             (sb-posix:syscall-errno c))))
720     #.sb-posix:enametoolong)
721     (deftest readlink.error.6
722       (let ((no-such-pathname (make-pathname :name "readlink.error.6"
723                                              :defaults *test-directory*)))
724         (handler-case (sb-posix:readlink no-such-pathname)
725           (sb-posix:syscall-error (c)
726             (sb-posix:syscall-errno c))))
727     #.sb-posix:enoent)
728   (deftest readlink.error.7
729       (let* ((non-link-pathname (make-pathname :name "readlink.error.7"
730                                                :defaults *test-directory*))
731              (impossible-pathname (merge-pathnames
732                                    (make-pathname
733                                     :directory
734                                     '(:relative "readlink.error.7")
735                                     :name "readlink.error.7")
736                                    *test-directory*))
737              (fd (sb-posix:open non-link-pathname sb-posix::o-creat)))
738         (unwind-protect
739              (handler-case (sb-posix:readlink impossible-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:enotdir)
746   )
747
748 (deftest getcwd.1
749     ;; FIXME: something saner, please
750     (equal (sb-unix::posix-getcwd) (sb-posix:getcwd))
751   t)
752
753 #-win32
754 (deftest mkstemp.1
755     (multiple-value-bind (fd temp)
756         (sb-posix:mkstemp (make-pathname
757                            :name "mkstemp-1"
758                            :type "XXXXXX"
759                            :defaults *test-directory*))
760       (let ((pathname (sb-ext:parse-native-namestring temp)))
761         (unwind-protect
762              (values (integerp fd) (pathname-name pathname))
763           (delete-file temp))))
764   t "mkstemp-1")
765
766 #-win32
767 (deftest mkdtemp.1
768     (let ((pathname
769            (sb-ext:parse-native-namestring
770             (sb-posix:mkdtemp (make-pathname
771                                :name "mkdtemp-1"
772                                :type "XXXXXX"
773                                :defaults *test-directory*))
774             nil
775             *default-pathname-defaults*
776             :as-directory t)))
777       (unwind-protect
778            (values (let* ((xxx (car (last (pathname-directory pathname))))
779                           (p (position #\. xxx)))
780                      (and p (subseq xxx 0 p)))
781                    (pathname-name pathname)
782                    (pathname-type pathname))
783         (sb-posix:rmdir pathname)))
784   "mkdtemp-1" nil nil)
785
786 #-win32
787 (deftest mktemp.1
788     (let ((pathname (sb-ext:parse-native-namestring
789                      (sb-posix:mktemp #p"mktemp.XXXXXX"))))
790       (values (equal "mktemp" (pathname-name pathname))
791               (not (equal "XXXXXX" (pathname-type pathname)))))
792   t t)