summaryrefslogtreecommitdiff
path: root/test-suite/tests/filesys.test
blob: 204f3414cdf0a9e5739a5d0dbc2d232c9d6032ad (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
;;;; filesys.test --- test file system functions -*- scheme -*-
;;;; 
;;;; Copyright (C) 2004, 2006, 2013, 2019, 2021 Free Software Foundation, Inc.
;;;; Copyright (C) 2021 Maxime Devos <maximedevos@telenet.be>
;;;; 
;;;; This library is free software; you can redistribute it and/or
;;;; modify it under the terms of the GNU Lesser General Public
;;;; License as published by the Free Software Foundation; either
;;;; version 3 of the License, or (at your option) any later version.
;;;; 
;;;; This library is distributed in the hope that it will be useful,
;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
;;;; Lesser General Public License for more details.
;;;; 
;;;; You should have received a copy of the GNU Lesser General Public
;;;; License along with this library; if not, write to the Free Software
;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA

(define-module (test-suite test-filesys)
  #:use-module (test-suite lib)
  #:use-module (test-suite guile-test)
  #:use-module (ice-9 threads)
  #:use-module (ice-9 match)
  #:use-module (rnrs io ports)
  #:use-module (rnrs bytevectors))

(define (test-file)
  (data-file-name "filesys-test.tmp"))
(define (test-symlink)
  (data-file-name "filesys-test-link.tmp"))
(define (test-directory)
  (data-file-name "filesys-test-dir.tmp"))
(define (test-directory2)
  (data-file-name "filesys-test-dir2.tmp"))


;;;
;;; copy-file
;;;

(with-test-prefix "copy-file"

  ;; return next prospective file descriptor number
  (define (next-fd)
    (let ((fd (dup 0)))
      (close fd)
      fd))

  ;; in guile 1.6.4 and earlier, copy-file didn't close the input fd when
  ;; the output could not be opened
  (pass-if "fd leak when dest unwritable"
    (let ((old-next (next-fd)))
      (false-if-exception (copy-file "/dev/null" "no/such/dir/foo"))
      (= old-next (next-fd)))))

;;;
;;; lstat
;;;

(with-test-prefix "lstat"

  (pass-if "normal file"
    (call-with-output-file (test-file)
      (lambda (port)
	(display "hello" port)))
    (eqv? 5 (stat:size (lstat (test-file)))))

  (call-with-output-file (test-file)
    (lambda (port)
      (display "hello" port)))
  (false-if-exception (delete-file (test-symlink)))
  (if (not (false-if-exception
	    (begin (symlink (test-file) (test-symlink)) #t)))
      (display "cannot create symlink, lstat test skipped\n")
      (pass-if "symlink"
	;; not much to test, except that it works
	(->bool (lstat (test-symlink))))))

;;;
;;; opendir and friends
;;;

(with-test-prefix "opendir"

  (with-test-prefix "root directory"
    (let ((d (opendir "/")))
      (pass-if "not empty"
	(string? (readdir d)))
      (pass-if "all entries are strings"
	(let more ()
	  (let ((f (readdir d)))
	    (cond ((string? f)
		   (more))
		  ((eof-object? f)
		   #t)
		  (else
		   #f)))))
      (closedir d))))

;;;
;;; stat
;;;

(with-test-prefix "stat"

  (with-test-prefix "filename"

    (pass-if "size"
      (call-with-output-file (test-file)
	(lambda (port)
	  (display "hello" port)))
      (eqv? 5 (stat:size (stat (test-file))))))

  (with-test-prefix "file descriptor"

    (pass-if "size"
      (call-with-output-file (test-file)
	(lambda (port)
	  (display "hello" port)))
      (let* ((fd (open-fdes (test-file) O_RDONLY))
	     (st (stat fd)))
	(close-fdes fd)
	(eqv? 5 (stat:size st)))))

  (with-test-prefix "port"

    (pass-if "size"
      (call-with-output-file (test-file)
	(lambda (port)
	  (display "hello" port)))
      (let* ((port (open-file (test-file) "r+"))
	     (st   (stat port)))
	(close-port port)
	(eqv? 5 (stat:size st))))))

(with-test-prefix "sendfile"

  (let* ((file (search-path %load-path "ice-9/boot-9.scm"))
         (len  (stat:size (stat file)))
         (ref  (call-with-input-file file get-bytevector-all)))

    (pass-if-equal "file" (cons len ref)
      (let* ((result (call-with-input-file file
                       (lambda (input)
                         (call-with-output-file (test-file)
                           (lambda (output)
                             (sendfile output input len 0))))))
             (out (call-with-input-file (test-file) get-bytevector-all)))
        (cons result out)))

    (pass-if-equal "file with offset"
        (cons (- len 777) (call-with-input-file file
                            (lambda (input)
                              (seek input 777 SEEK_SET)
                              (get-bytevector-all input))))
      (let* ((result (call-with-input-file file
                       (lambda (input)
                         (call-with-output-file (test-file)
                           (lambda (output)
                             (sendfile output input (- len 777) 777))))))
             (out (call-with-input-file (test-file) get-bytevector-all)))
        (cons result out)))

    (pass-if-equal "file with offset past the end"
        (cons (- len 777) (call-with-input-file file
                            (lambda (input)
                              (seek input 777 SEEK_SET)
                              (get-bytevector-all input))))
      (let* ((result (call-with-input-file file
                       (lambda (input)
                         (call-with-output-file (test-file)
                           (lambda (output)
                             (sendfile output input len 777))))))
             (out (call-with-input-file (test-file) get-bytevector-all)))
        (cons result out)))

    (pass-if-equal "file with offset near the end"
        (cons 77 (call-with-input-file file
                   (lambda (input)
                     (seek input (- len 77) SEEK_SET)
                     (get-bytevector-all input))))
      (let* ((result (call-with-input-file file
                       (lambda (input)
                         (call-with-output-file (test-file)
                           (lambda (output)
                             (sendfile output input len (- len 77)))))))
             (out (call-with-input-file (test-file) get-bytevector-all)))
        (cons result out)))

    (pass-if-equal "pipe" (cons len ref)
      (if (provided? 'threads)
          (let* ((in+out (pipe))
                 (child  (call-with-new-thread
                          (lambda ()
                            (call-with-input-file file
                              (lambda (input)
                                (let ((result (sendfile (cdr in+out)
                                                        (fileno input)
                                                        len 0)))
                                  (close-port (cdr in+out))
                                  result)))))))
            (let ((out (get-bytevector-all (car in+out))))
              (close-port (car in+out))
              (cons (join-thread child) out)))
          (throw 'unresolved)))

    (pass-if-equal "pipe with offset"
        (cons (- len 777) (call-with-input-file file
                            (lambda (input)
                              (seek input 777 SEEK_SET)
                              (get-bytevector-all input))))
      (if (provided? 'threads)
          (let* ((in+out (pipe))
                 (child  (call-with-new-thread
                          (lambda ()
                            (call-with-input-file file
                              (lambda (input)
                                (let ((result (sendfile (cdr in+out)
                                                        (fileno input)
                                                        (- len 777)
                                                        777)))
                                  (close-port (cdr in+out))
                                  result)))))))
            (let ((out (get-bytevector-all (car in+out))))
              (close-port (car in+out))
              (cons (join-thread child) out)))
          (throw 'unresolved)))))

(with-test-prefix "basename"

  (pass-if-equal "/" "/" (basename "/"))
  (pass-if-equal "//" "/" (basename "//"))
  (pass-if-equal "a/b/c" "c" (basename "a/b/c")))

(delete-file (test-file))
(when (file-exists? (test-symlink))
  (delete-file (test-symlink)))


(with-test-prefix "mkdtemp"

  (pass-if-exception "number arg" exception:wrong-type-arg
    (if (not (defined? 'mkdtemp))
        (throw 'unresolved)
        (mkdtemp 123)))

  (pass-if "template prefix is preserved"
    (if (not (defined? 'mkdtemp))
        (throw 'unresolved)
        (let* ((template "T-XXXXXX")
               (name (mkdtemp template)))
          (false-if-exception (rmdir name))
          (and
           (string? name)
           (string-contains name "T-")
           (= (string-length name) 8)))))

  (pass-if-exception "invalid template" exception:system-error
    (if (not (defined? 'mkdtemp))
        (throw 'unresolved)
        (mkdtemp "T-AAAAAA")))

  (pass-if "directory created"
    (if (not (defined? 'mkdtemp))
        (throw 'unresolved)
        (let* ((template "T-XXXXXX")
               (name (mkdtemp template)))
          (let* ((_stat    (stat name))
                 (result   (eqv? 'directory (stat:type _stat))))
            (false-if-exception (rmdir name))
            result)))))

;;;
;;; chmodat
;;;

(with-test-prefix "chmodat"
  (call-with-output-file (test-file) (const #f))
  (chmod (test-file) #o000)

  (pass-if-equal "regular file"
      #o300
    (unless (defined? 'chmodat)
      (throw 'unsupported))
    (call-with-port
     (open (dirname (test-file)) O_RDONLY)
     (lambda (port)
       (chmodat port (test-file) #o300)))
    (stat:perms (stat (test-file))))

  (chmod (test-file) #o000)

  (pass-if-equal "regular file, AT_SYMLINK_NOFOLLOW"
      #o300
    (unless (and (defined? 'chmodat)
                 (defined? 'AT_SYMLINK_NOFOLLOW))
      (throw 'unsupported))
    (call-with-port
     (open (dirname (test-file)) O_RDONLY)
     (lambda (port)
       (catch 'system-error
         (lambda ()
           (chmodat port (basename (test-file)) #o300 AT_SYMLINK_NOFOLLOW))
         (lambda args
           (close-port port)
           ;; AT_SYMLINK_NOFOLLOW is not supported on Linux (at least Linux
           ;; 5.11.2 with the btrfs file system), even for regular files.
           (if (= ENOTSUP (system-error-errno args))
               (begin
                 (display "fchmodat doesn't support AT_SYMLINK_NOFOLLOW\n")
                 (throw 'unresolved))
               (apply throw args))))))
    (stat:perms (stat (test-file))))

  (pass-if-exception "not a port" exception:wrong-type-arg
    (chmodat "bogus" (test-file) #o300))

  (pass-if-exception "not a file port" exception:wrong-type-arg
    (chmodat (open-input-string "") (test-file) #o300))

  (pass-if-exception "closed port" exception:wrong-type-arg
    (chmodat (call-with-port (open "." O_RDONLY) identity) (test-file) #o300))

  (delete-file (test-file)))

(with-test-prefix "chdir"
  (pass-if-equal "current directory" (getcwd)
    (begin (chdir ".") (getcwd)))
  (define file (search-path %load-path "ice-9/boot-9.scm"))


  (pass-if-equal "test directory" (dirname file)
    (let ((olddir (getcwd))
          (dir #f))
      (chdir (dirname file))
      (set! dir (getcwd))
      (chdir olddir)
      dir))

  (pass-if-equal "test directory, via port" (dirname file)
    (unless (provided? 'chdir-port)
      (throw 'unresolved))
    (let ((olddir (getcwd))
          (port (open (dirname file) O_RDONLY))
          (dir #f))
      (chdir port)
      (set! dir (getcwd))
      (chdir olddir)
      dir))

  (pass-if-exception "closed port"  exception:wrong-type-arg
    (unless (provided? 'chdir-port)
      (throw 'unresolved))
    (let ((port (open (dirname file) O_RDONLY))
          (olddir (getcwd)))
      (close-port port)
      (chdir port)
      (chdir olddir))) ; should not be reached

  (pass-if-exception "not a port or file name" exception:wrong-type-arg
    (chdir '(stuff)))

  (pass-if-exception "non-file port" exception:wrong-type-arg
    (chdir (open-input-string ""))))

(with-test-prefix "readlink"
  (false-if-exception (delete-file (test-symlink)))
  (false-if-exception (delete-file (test-file)))
  (call-with-output-file (test-file)
    (lambda (port)
      (display "hello" port)))
  (if (not (false-if-exception
	    (begin (symlink (test-file) (test-symlink)) #t)))
      (display "cannot create symlink, some readlink tests skipped\n")
      (let ()
        (pass-if-equal "file name of symlink" (test-file)
          (readlink (test-symlink)))

        (pass-if-equal "port representing a symlink" (test-file)
          (let ()
            (unless (and (provided? 'readlink-port)
                         (defined? 'O_NOFOLLOW)
                         (defined? 'O_PATH)
                         (not (= 0 O_NOFOLLOW))
                         (not (= 0 O_PATH)))
              (throw 'unsupported))
            (define port (open (test-symlink) (logior O_NOFOLLOW O_PATH)))
            (define points-to (false-if-exception (readlink port)))
            (close-port port)
            points-to))

        (pass-if-exception "not a port or file name" exception:wrong-type-arg
          (readlink '(stuff)))))

  (pass-if-equal "port representing a regular file" EINVAL
    (call-with-input-file (test-file)
      (lambda (port)
        (unless (provided? 'readlink-port)
          (throw 'unsupported))
        (catch 'system-error
          (lambda ()
            (readlink port)
            (close-port port) ; should be unreachable
            #f)
          (lambda args
            (close-port port)
            ;; At least Linux 5.10.46 returns ENOENT instead of EINVAL.
            ;; Possibly surprising, but it is documented in some man
            ;; pages and it doesn't appear to be an accident:
            ;; <https://elixir.bootlin.com/linux/v5.10.46/source/fs/stat.c#L419>.
            (define error (system-error-errno args))
            (if (= error ENOENT)
                EINVAL
                error))))))

  (pass-if-exception "non-file port" exception:wrong-type-arg
    (readlink (open-input-string "")))

  (pass-if-exception "closed port" exception:wrong-type-arg
    (let ((port (open-file (test-file) "r")))
      (close-port port)
      (readlink port)))

  (false-if-exception (delete-file (test-symlink)))
  (false-if-exception (delete-file (test-file))))

(with-test-prefix "symlinkat"
  (pass-if-equal "create" (test-file)
    (unless (defined? 'symlinkat)
      (throw 'unsupported))
    (call-with-port
     (open "." O_RDONLY)
     (lambda (port)
       (symlinkat port (test-file) (test-symlink))
       (readlink (test-symlink)))))
  (false-if-exception (delete-file (test-symlink)))

  (pass-if-exception "not a port" exception:wrong-type-arg
    (unless (defined? 'symlinkat)
      (throw 'unsupported))
    (symlinkat "bogus" (test-file) (test-symlink)))

  (pass-if-exception "not a file port" exception:wrong-type-arg
    (unless (defined? 'symlinkat)
      (throw 'unsupported))
    (symlinkat (open-input-string "") (test-file) (test-symlink)))

  (pass-if-exception "closed port" exception:wrong-type-arg
    (unless (defined? 'symlinkat)
      (throw 'unsupported))
    (symlinkat (call-with-port (open "." O_RDONLY) identity)
               (test-file) (test-symlink))))

(with-test-prefix "mkdirat"
  (define (skip-if-unsupported)
    (unless (defined? 'mkdirat)
      (throw 'unsupported)))
  (define (maybe-delete-directory)
    (when (file-exists? (test-directory))
      (rmdir (test-directory))))
  (maybe-delete-directory)

  (pass-if-equal "create" 'directory
    (skip-if-unsupported)
    (call-with-port
     (open "." O_RDONLY)
     (lambda (port)
       (mkdirat port (test-directory))
       (stat:type (stat (test-directory))))))
  (maybe-delete-directory)

  (pass-if-equal "explicit perms" (logand #o111 (lognot (umask)))
    (skip-if-unsupported)
    (call-with-port
     (open "." O_RDONLY)
     (lambda (port)
       (mkdirat port (test-directory) #o111)
       (stat:perms (stat (test-directory))))))
  (maybe-delete-directory)

  (pass-if-equal "create, implicit perms" (logand #o777 (lognot (umask)))
    (skip-if-unsupported)
    (call-with-port
     (open "." O_RDONLY)
     (lambda (port)
       (mkdirat port (test-directory))
       (stat:perms (stat (test-directory))))))
  (maybe-delete-directory))

(with-test-prefix "rename-file-at"
  (define (skip-if-unsupported)
    (unless (defined? 'rename-file-at)
      (throw 'unsupported)))
  (pass-if-equal "current working directory" '(#f "hello")
    (skip-if-unsupported)
    ;; Create a file in the test directory
    (call-with-output-file "filesys-test-a.tmp"
      (lambda (port) (display "hello" port)))
    ;; Try to rename it
    (rename-file-at #f "filesys-test-a.tmp" #f "filesys-test-b.tmp")
    ;; Verify it exists under the new name, and not under the old name
    (list (file-exists? "filesys-test-a.tmp")
          (call-with-input-file "filesys-test-b.tmp" get-string-all)))

  (false-if-exception (delete-file "filesys-test-a.tmp"))
  (false-if-exception (delete-file "filesys-test-b.tmp"))

  (pass-if-equal "two ports" '(#f "hello")
    (skip-if-unsupported)
    (mkdir (test-directory))
    (mkdir (test-directory2))
    ;; Create a file in the first directory
    (call-with-output-file (in-vicinity (test-directory) "a")
      (lambda (port) (display "hello" port)))
    (let ((port1 (open (test-directory) O_RDONLY))
          (port2 (open (test-directory2) O_RDONLY)))
      ;; Try to rename it
      (rename-file-at port1 "a" port2 "b")
      (close-port port1)
      (close-port port2)
      ;; Verify it exists under the new name, and not under the old name
      (list (file-exists? (in-vicinity (test-directory) "a"))
            (call-with-input-file (in-vicinity (test-directory2) "b")
              get-string-all))))
  (false-if-exception (delete-file (in-vicinity (test-directory) "a")))
  (false-if-exception (delete-file (in-vicinity (test-directory2) "b")))
  (false-if-exception (rmdir (test-directory)))
  (false-if-exception (rmdir (test-directory2)))

  (pass-if-equal "port and current working directory" '(#f "hello")
    (skip-if-unsupported)
    (mkdir (test-directory))
    ;; Create a file in (test-directory)
    (call-with-output-file (in-vicinity (test-directory) "a")
      (lambda (port) (display "hello" port)))
    (let ((port (open (test-directory) O_RDONLY)))
      ;; Try to rename it
      (rename-file-at port "a" #f (basename (test-file)))
      (close-port port)
      ;; Verify it exists under the new name, and not under the old name.
      (list (file-exists? (in-vicinity (test-directory) "a"))
            (call-with-input-file (test-file) get-string-all))))
  (false-if-exception (delete-file (in-vicinity (test-directory) "a")))
  (false-if-exception (rmdir (test-directory)))
  (false-if-exception (delete-file (test-file)))

  (pass-if-equal "current working directory and port" '(#f "hello")
    (skip-if-unsupported)
    (mkdir (test-directory))
    ;; Create a file in the working directory
    (call-with-output-file (test-file)
      (lambda (port) (display "hello" port)))
    (let ((port (open (test-directory) O_RDONLY)))
      ;; Try to rename it
      (rename-file-at #f (basename (test-file)) port "b")
      (close-port port)
      ;; Verify it exists under the new name, and not under the old name.
      (list (file-exists? (test-file))
            (call-with-input-file (in-vicinity (test-directory) "b")
              get-string-all))))

  (false-if-exception (delete-file (in-vicinity (test-directory) "b")))
  (false-if-exception (delete-file (test-file)))
  (false-if-exception (rmdir (test-directory)))

  (pass-if-exception "not a file port (1)" exception:wrong-type-arg
    (skip-if-unsupported)
    (rename-file-at (open-input-string "") "some" #f "thing"))

  (pass-if-exception "not a file port (2)" exception:wrong-type-arg
    (skip-if-unsupported)
    (rename-file-at #f "some" (open-input-string "") "thing"))

  (pass-if-exception "closed port (1)" exception:wrong-type-arg
    (skip-if-unsupported)
    (rename-file-at (call-with-port (open "." O_RDONLY) identity)
                    "some" #f "thing"))

  (pass-if-exception "closed port (2)" exception:wrong-type-arg
    (skip-if-unsupported)
    (rename-file-at #f "some" (call-with-port (open "." O_RDONLY) identity)
                    "thing"))

  (pass-if-exception "not a string (1)" exception:wrong-type-arg
    (skip-if-unsupported)
    (rename-file-at #f 'what #f "thing"))

  (pass-if-exception "not a string (2)" exception:wrong-type-arg
    (skip-if-unsupported)
    (rename-file-at #f "some" #f 'what)))