check.scm 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. ; Copyright (c) 1993-2007 by Richard Kelsey and Jonathan Rees. See file COPYING.
  2. ; A test suite for the POSIX interface.
  3. ; ,config ,load =scheme48/debug/test.scm
  4. ; ,exec ,load =scheme48/posix/check.scm
  5. ; ,exec (done)
  6. (load-package 'testing)
  7. (config '(run
  8. (define-structure posix-test (export)
  9. (open scheme testing sort threads
  10. util ; every
  11. posix-files
  12. posix-time
  13. posix-users
  14. posix-i/o
  15. posix-process-data
  16. posix-processes
  17. os-strings)
  18. (begin
  19. (define-syntax xtest
  20. (syntax-rules ()
  21. ((xtest stuff ...) #f)))
  22. ; 1. get the process ID
  23. ; 2. make a /tmp/s48-posix-test-<pid> directory
  24. ; 3. go there and make files, etc.
  25. (define initial-wd (working-directory))
  26. (define directory-name
  27. (string-append "/tmp/s48-posix-test-"
  28. (number->string (process-id->integer (get-process-id)))))
  29. (test "file-mode-predicates" equal? '((#t #f)
  30. (#t #f)
  31. (#t #f #t)
  32. (#t #t #f)
  33. (#t #t #t #t))
  34. (let ((mode0 (file-mode set-uid owner-read group-write other-exec))
  35. (mode1 (file-mode set-uid))
  36. (mode2 (file-mode owner-read group-write))
  37. (mode3 (file-mode set-uid other-exec)))
  38. (list (list (file-mode? mode0)
  39. (file-mode? 'mode0))
  40. (list (file-mode=? mode0 mode0)
  41. (file-mode=? mode0 mode1))
  42. (list (file-mode<=? mode0 mode0)
  43. (file-mode<=? mode0 mode1)
  44. (file-mode<=? mode1 mode0))
  45. (list (file-mode>=? mode0 mode0)
  46. (file-mode>=? mode0 mode1)
  47. (file-mode>=? mode1 mode0))
  48. (map (lambda (x)
  49. (file-mode=? x
  50. (integer->file-mode
  51. (file-mode->integer x))))
  52. (list mode0 mode1 mode2 mode3)))))
  53. (test "file-modes" equal? '((#o4421 #o4000)
  54. (#o0000 #o4000 #o4421)
  55. (#o0420)
  56. (1 2 4 8 16 32 64 128 256 1024 2048)
  57. (7 56 448 73 146 292 511))
  58. (let ((mode0 (file-mode set-uid owner-read group-write other-exec))
  59. (mode1 (file-mode set-uid))
  60. (mode2 (file-mode owner-read group-write))
  61. (mode3 (file-mode set-uid other-exec)))
  62. (map (lambda (l)
  63. (map file-mode->integer l))
  64. (list (list mode0 mode1)
  65. (list (file-mode+)
  66. (file-mode+ mode1)
  67. (file-mode+ mode1 mode2 mode3))
  68. (list (file-mode- mode0 mode3))
  69. (list (file-mode other-exec)
  70. (file-mode other-write)
  71. (file-mode other-read)
  72. (file-mode group-exec)
  73. (file-mode group-write)
  74. (file-mode group-read)
  75. (file-mode owner-exec)
  76. (file-mode owner-write)
  77. (file-mode owner-read)
  78. (file-mode set-gid)
  79. (file-mode set-uid))
  80. (list (file-mode other)
  81. (file-mode group)
  82. (file-mode owner)
  83. (file-mode exec)
  84. (file-mode write)
  85. (file-mode read)
  86. (file-mode all))))))
  87. (test "make-directory" eq? (file-type directory)
  88. (begin
  89. (make-directory directory-name (integer->file-mode #o700))
  90. (file-info-type (get-file-info directory-name))))
  91. (test "time" equal? '((#t #t #f #f)
  92. (#t #f #f #t) ; =
  93. (#f #f #t #f) ; <
  94. (#f #t #f #f) ; >
  95. (#t #f #t #t) ; <=
  96. (#t #t #f #t) ; >=
  97. (#t #f)
  98. (#t #f #f #t) ; =
  99. #t)
  100. (begin
  101. (sleep 3000) ; three seconds
  102. (let ((now (current-time))
  103. (dir-time (file-info-last-modification
  104. (get-file-info directory-name))))
  105. (let ((times1 (list now now dir-time dir-time))
  106. (times2 (list now dir-time now dir-time)))
  107. (list (list (time? now)
  108. (time? dir-time)
  109. (time? 'now)
  110. (time? 20))
  111. (map time=? times1 times2)
  112. (map time<? times1 times2)
  113. (map time>? times1 times2)
  114. (map time<=? times1 times2)
  115. (map time>=? times1 times2)
  116. (list (= (time-seconds now)
  117. (time-seconds now))
  118. (= (time-seconds now)
  119. (time-seconds dir-time)))
  120. (map time=?
  121. times1
  122. (map (lambda (t)
  123. (make-time (time-seconds t)))
  124. times2))
  125. (string? (time->string now)))))))
  126. (test "set-working-directory!" string=? directory-name
  127. (begin
  128. (set-working-directory! directory-name)
  129. (os-string->string (working-directory))))
  130. (test "i/o-flags" equal? '(#f #f #t #f #f #f #f #t)
  131. (let* ((out (open-file "file0"
  132. (file-options create write-only)
  133. (integer->file-mode #o700)))
  134. (flags (i/o-flags out)))
  135. (display "123456" out)
  136. (newline out)
  137. (close-output-port out)
  138. (list (file-options-on? flags (file-options append))
  139. (file-options-on? flags (file-options synchronized-data))
  140. (file-options-on? flags (file-options nonblocking))
  141. (file-options-on? flags (file-options synchronized-read))
  142. (file-options-on? flags (file-options synchronized))
  143. (file-options-on? flags (file-options read-only))
  144. (file-options-on? flags (file-options read-write))
  145. (file-options-on? flags (file-options write-only)))))
  146. (test "append mode" equal? '(7 14)
  147. (let* ((old-size (file-info-size (get-file-info "file0")))
  148. (out (open-file "file0"
  149. (file-options append write-only))))
  150. (display "123456" out)
  151. (newline out)
  152. (close-output-port out)
  153. (list old-size
  154. (file-info-size (get-file-info "file0")))))
  155. (test "file times" equal? '(#t #f #t)
  156. (let ((old-info (get-file-info "file0")))
  157. (sleep 3000) ; three seconds
  158. (let ((in (open-file "file0"
  159. (file-options read-only))))
  160. (read-char in)
  161. (close-input-port in))
  162. (let ((new-info (get-file-info "file0")))
  163. (list (time=? (file-info-last-modification old-info)
  164. (file-info-last-modification new-info))
  165. (time=? (file-info-last-access old-info)
  166. (file-info-last-access new-info))
  167. (time<? (file-info-last-access old-info)
  168. (file-info-last-access new-info))))))
  169. (test "link" equal? '(1 2)
  170. (let ((old-link-count (file-info-link-count (get-file-info "file0"))))
  171. (link "file0" "link-to-file0")
  172. (list old-link-count
  173. (file-info-link-count (get-file-info "file0")))))
  174. (test "rename" eq? #t
  175. (let ((inode (file-info-inode (get-file-info "file0"))))
  176. (rename "file0" "file1")
  177. (= inode
  178. (file-info-inode (get-file-info "file1")))))
  179. (test "listings0" equal? '("file1" "link-to-file0")
  180. (let ((directory (open-directory-stream directory-name)))
  181. (let loop ((names '()))
  182. (let ((next (read-directory-stream directory)))
  183. (if next
  184. (loop (cons next names))
  185. (begin
  186. (close-directory-stream directory)
  187. (sort-list (map os-string->string names) string<=?)))))))
  188. (test "listings1" equal? '("file1" "link-to-file0")
  189. (sort-list (map os-string->string (list-directory ".")) string<=?))
  190. (test "unlink" = 1
  191. (begin
  192. (unlink "link-to-file0")
  193. (file-info-link-count (get-file-info "file1"))))
  194. (test "umask" equal? '(#o012 #o765)
  195. (let* ((old-mask (set-file-creation-mask! (integer->file-mode #o012)))
  196. (out (open-file "umask-file"
  197. (file-options create write-only)
  198. (integer->file-mode #o777))))
  199. (display "123456" out)
  200. (newline out)
  201. (close-output-port out)
  202. (let* ((my-mask (set-file-creation-mask! old-mask))
  203. (file-mode (file-info-mode (get-file-info "umask-file"))))
  204. (list (file-mode->integer my-mask)
  205. (file-mode->integer file-mode)))))
  206. ; This assumes that we are not running as root and that / is owned by root.
  207. (test "users & groups" equal? '(#t #f #t #f "root")
  208. (let ((my-info (get-file-info directory-name))
  209. (root-info (get-file-info "/")))
  210. (let ((my-user (user-id->user-info (file-info-owner my-info)))
  211. (root-user (user-id->user-info (file-info-owner root-info)))
  212. (my-group (group-id->group-info (file-info-group my-info)))
  213. (root-group (group-id->group-info (file-info-group root-info))))
  214. (let ((my-other-user (name->user-info (user-info-name my-user)))
  215. (my-other-group (name->group-info (group-info-name my-group))))
  216. (list (user-id=? (file-info-owner my-info)
  217. (user-info-id my-user))
  218. (user-id=? (file-info-owner root-info)
  219. (user-info-id my-user))
  220. (group-id=? (file-info-group my-info)
  221. (group-info-id my-group))
  222. (group-id=? (file-info-group root-info)
  223. (group-info-id my-group))
  224. (os-string->string (user-info-name root-user)))))))
  225. (test "environment" equal? '(#t #t #f)
  226. (let ((env (reverse (environment-alist))))
  227. (list (if (null? env)
  228. #t
  229. (string=? (os-string->string (cdar env))
  230. (lookup-environment-variable->string (caar env))))
  231. (every (lambda (x)
  232. (and (pair? x)
  233. (os-string? (car x))
  234. (os-string? (cdr x))))
  235. env)
  236. (lookup-environment-variable->string "="))))
  237. ; This should be last, because it removes the directory.
  238. (test "rmdir" equal? '(#t #f)
  239. (let ((before (accessible? directory-name (access-mode exists))))
  240. (for-each unlink (list-directory "."))
  241. (set-working-directory! initial-wd)
  242. (remove-directory directory-name)
  243. (list before
  244. (accessible? directory-name (access-mode exists)))))
  245. ))))
  246. (load-package 'posix-test)
  247. (config '(run
  248. (define-structure regexp-test (export)
  249. (open scheme testing
  250. regexps)
  251. (begin
  252. (test "any-match?" equal? '(#t #f #t)
  253. (list
  254. (any-match? (text "abc") "abc")
  255. (any-match? (text "abc") "abx")
  256. (any-match? (text "abc") "xxabcxx")))
  257. (test "exact-match" equal? '(#t #f #f)
  258. (list
  259. (exact-match? (text "abc") "abc")
  260. (exact-match? (text "abc") "abx")
  261. (exact-match? (text "abc") "xxabcxx")))
  262. (define (pair-match exp string)
  263. (let ((res (match exp string)))
  264. (and res
  265. (cons (list (match-start res)
  266. (match-end res))
  267. (map (lambda (p)
  268. (cons (car p)
  269. (list (match-start (cdr p))
  270. (match-end (cdr p)))))
  271. (match-submatches res))))))
  272. (test "match" equal? '(((0 3))
  273. #f
  274. ((2 5))
  275. ((3 9) (foo 5 7))
  276. ((3 6) (bar 4 6)))
  277. (list
  278. (pair-match (text "abc") "abc")
  279. (pair-match (text "abc") "abx")
  280. (pair-match (text "abc") "xxabcxx")
  281. (pair-match (sequence (text "ab")
  282. (submatch 'foo (text "cd"))
  283. (text "ef"))
  284. "xxxabcdefxx")
  285. (pair-match (sequence (set "a")
  286. (one-of (submatch 'foo (text "bc"))
  287. (submatch 'bar (text "BC"))))
  288. "xxxaBCd")))
  289. ))))
  290. (load-package 'regexp-test)
  291. ; All done.
  292. (if (in 'testing '(run (lost?)))
  293. (display "Some tests failed.")
  294. (display "All tests succeeded."))
  295. (newline)
  296. (define (done)
  297. (exit (if (in 'testing '(run (lost?))) 1 0)))