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+ (logior sb-posix::s-irusr sb-posix::s-iwusr sb-posix::s-ixusr
18 sb-posix::s-irgrp sb-posix::s-iwgrp sb-posix::s-ixgrp
19 sb-posix::s-iroth sb-posix::s-iwoth sb-posix::s-ixoth)))
22 (sb-posix:chdir *test-directory*)
26 (sb-posix:chdir (namestring *test-directory*))
34 (sb-posix:chdir #p"/")
38 (sb-posix:chdir *current-directory*)
42 (sb-posix:chdir "/../")
46 (sb-posix:chdir #p"/../")
50 (sb-posix:chdir (make-pathname :directory '(:absolute :up)))
53 (deftest chdir.error.1
54 (let ((dne (make-pathname :directory '(:relative "chdir.does-not-exist"))))
56 (sb-posix:chdir (merge-pathnames dne *test-directory*))
57 (sb-posix:syscall-error (c)
58 (sb-posix:syscall-errno c))))
61 (deftest chdir.error.2
63 (sb-posix:chdir *this-file*)
64 (sb-posix:syscall-error (c)
65 (sb-posix:syscall-errno c)))
69 (let ((dne (make-pathname :directory '(:relative "mkdir.does-not-exist.1"))))
71 (sb-posix:mkdir (merge-pathnames dne *test-directory*) 0)
72 ;; FIXME: no delete-directory in CL, but using our own operators
73 ;; is probably not ideal
74 (ignore-errors (sb-posix:rmdir (merge-pathnames dne *test-directory*)))))
78 (let ((dne (make-pathname :directory '(:relative "mkdir.does-not-exist.2"))))
80 (sb-posix:mkdir (namestring (merge-pathnames dne *test-directory*)) 0)
81 (ignore-errors (sb-posix:rmdir (merge-pathnames dne *test-directory*)))))
84 (deftest mkdir.error.1
86 (sb-posix:mkdir *test-directory* 0)
87 (sb-posix:syscall-error (c)
88 (sb-posix:syscall-errno c)))
91 (deftest mkdir.error.2
93 (sb-posix:mkdir "/" 0)
94 (sb-posix:syscall-error (c)
95 (sb-posix:syscall-errno c)))
98 (deftest mkdir.error.3
100 (sb-posix:mkdir "/almost-certainly-does-not-exist" 0)
101 (sb-posix:syscall-error (c)
102 (sb-posix:syscall-errno c)))
106 (let ((dne (make-pathname :directory '(:relative "rmdir.does-not-exist.1"))))
107 (ensure-directories-exist (merge-pathnames dne *test-directory*))
108 (sb-posix:rmdir (merge-pathnames dne *test-directory*)))
112 (let ((dne (make-pathname :directory '(:relative "rmdir.does-not-exist.2"))))
113 (ensure-directories-exist (merge-pathnames dne *test-directory*))
114 (sb-posix:rmdir (namestring (merge-pathnames dne *test-directory*))))
117 (deftest rmdir.error.1
118 (let ((dne (make-pathname :directory '(:relative "rmdir.dne.error.1"))))
120 (sb-posix:rmdir (merge-pathnames dne *test-directory*))
121 (sb-posix:syscall-error (c)
122 (sb-posix:syscall-errno c))))
125 (deftest rmdir.error.2
127 (sb-posix:rmdir *this-file*)
128 (sb-posix:syscall-error (c)
129 (sb-posix:syscall-errno c)))
132 #-sunos ; Apparently gives EINVAL on SunOS 8, which doesn't make sense
133 (deftest rmdir.error.3
136 (sb-posix:syscall-error (c)
137 (sb-posix:syscall-errno c)))
140 (deftest rmdir.error.4
141 (let* ((dir (ensure-directories-exist
143 (make-pathname :directory '(:relative "rmdir.error.4"))
145 (file (make-pathname :name "foo" :defaults dir)))
146 (with-open-file (s file :direction :output)
147 (write "" :stream s))
150 (sb-posix:syscall-error (c)
153 (let ((errno (sb-posix:syscall-errno c)))
154 ;; documented by POSIX
155 (or (= errno sb-posix::eexist) (= errno sb-posix::enotempty))))))
158 (deftest rmdir.error.5
159 (let* ((dir (merge-pathnames
160 (make-pathname :directory '(:relative "rmdir.error.5"))
162 (dir2 (merge-pathnames
163 (make-pathname :directory '(:relative "unremovable"))
165 (sb-posix:mkdir dir +mode-rwx-all+)
166 (sb-posix:mkdir dir2 +mode-rwx-all+)
167 (sb-posix:chmod dir 0)
169 (sb-posix:rmdir dir2)
170 (sb-posix:syscall-error (c)
171 (sb-posix:chmod dir (logior sb-posix::s-iread sb-posix::s-iwrite sb-posix::s-iexec))
172 (sb-posix:rmdir dir2)
174 (sb-posix:syscall-errno c))))
178 (let* ((stat (sb-posix:stat *test-directory*))
179 (mode (sb-posix::stat-mode stat)))
180 ;; FIXME: Ugly ::s everywhere
181 (logand mode (logior sb-posix::s-iread sb-posix::s-iwrite sb-posix::s-iexec)))
182 #.(logior sb-posix::s-iread sb-posix::s-iwrite sb-posix::s-iexec))
185 (let* ((stat (sb-posix:stat "/"))
186 (mode (sb-posix::stat-mode stat)))
187 ;; it's logically possible for / to be writeable by others... but
188 ;; if it is, either someone is playing with strange security
189 ;; modules or they want to know about it anyway.
190 (logand mode sb-posix::s-iwoth))
194 (let* ((now (get-universal-time))
195 ;; FIXME: (encode-universal-time 00 00 00 01 01 1970)
196 (unix-now (- now 2208988800))
197 (stat (sb-posix:stat *test-directory*))
198 (atime (sb-posix::stat-atime stat)))
199 ;; FIXME: breaks if mounted noatime :-(
200 (< (- atime unix-now) 10))
204 (let* ((stat (sb-posix:stat (make-pathname :directory '(:absolute :up))))
205 (mode (sb-posix::stat-mode stat)))
206 ;; it's logically possible for / to be writeable by others... but
207 ;; if it is, either someone is playing with strange security
208 ;; modules or they want to know about it anyway.
209 (logand mode sb-posix::s-iwoth))
212 ;;; FIXME: add tests for carrying a stat structure around in the
213 ;;; optional argument to SB-POSIX:STAT
215 (deftest stat.error.1
216 (handler-case (sb-posix:stat "")
217 (sb-posix:syscall-error (c)
218 (sb-posix:syscall-errno c)))
221 (deftest stat.error.2
222 (let* ((dir (merge-pathnames
223 (make-pathname :directory '(:relative "stat.error.2"))
225 (file (merge-pathnames
226 (make-pathname :name "unstatable")
228 (sb-posix:mkdir dir +mode-rwx-all+)
229 (with-open-file (s file :direction :output)
230 (write "" :stream s))
231 (sb-posix:chmod dir 0)
234 (sb-posix:syscall-error (c)
235 (sb-posix:chmod dir (logior sb-posix::s-iread sb-posix::s-iwrite sb-posix::s-iexec))
236 (sb-posix:unlink file)
238 (sb-posix:syscall-errno c))))
242 (defmacro with-stat-mode ((mode pathname) &body body)
243 (let ((stat (gensym)))
244 `(let* ((,stat (sb-posix:stat ,pathname))
245 (,mode (sb-posix::stat-mode ,stat)))
248 (defmacro with-lstat-mode ((mode pathname) &body body)
249 (let ((stat (gensym)))
250 `(let* ((,stat (sb-posix:lstat ,pathname))
251 (,mode (sb-posix::stat-mode ,stat)))
255 (with-stat-mode (mode *test-directory*)
256 (sb-posix:s-isreg mode))
260 (with-stat-mode (mode *test-directory*)
261 (sb-posix:s-isdir mode))
265 (with-stat-mode (mode *test-directory*)
266 (sb-posix:s-ischr mode))
270 (with-stat-mode (mode *test-directory*)
271 (sb-posix:s-isblk mode))
275 (with-stat-mode (mode *test-directory*)
276 (sb-posix:s-isfifo mode))
280 (with-stat-mode (mode *test-directory*)
281 (sb-posix:s-issock mode))
285 (let ((link-pathname (make-pathname :name "stat-mode.7"
286 :defaults *test-directory*)))
289 (sb-posix:symlink *test-directory* link-pathname)
290 (with-lstat-mode (mode link-pathname)
291 (sb-posix:s-islnk mode)))
292 (ignore-errors (sb-posix:unlink link-pathname))))
296 (let ((pathname (make-pathname :name "stat-mode.8"
297 :defaults *test-directory*)))
300 (with-open-file (out pathname :direction :output)
301 (write-line "test" out))
302 (with-stat-mode (mode pathname)
303 (sb-posix:s-isreg mode)))
304 (ignore-errors (delete-file pathname))))
307 ;;; see comment in filename's designator definition, in macros.lisp
308 (deftest filename-designator.1
309 (let ((file (format nil "~A/[foo].txt" (namestring *test-directory*))))
310 ;; creat() with a string as argument
311 (sb-posix:creat file 0)
312 ;; if this test fails, it will probably be with
313 ;; "System call error 2 (No such file or directory)"
314 (let ((*default-pathname-defaults* *test-directory*))
315 (sb-posix:unlink (car (directory "*.txt")))))
319 (let ((fd (sb-posix:open *test-directory* sb-posix::o-rdonly)))
320 (ignore-errors (sb-posix:close fd))
324 (deftest open.error.1
325 (handler-case (sb-posix:open *test-directory* sb-posix::o-wronly)
326 (sb-posix:syscall-error (c)
327 (sb-posix:syscall-errno c)))