posix.test 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. ;;;; posix.test --- Test suite for Guile POSIX functions. -*- scheme -*-
  2. ;;;;
  3. ;;;; Copyright 2003-2004,2006-2007,2010,2012,2015,2017-2019
  4. ;;;; Free Software Foundation, Inc.
  5. ;;;;
  6. ;;;; This library is free software; you can redistribute it and/or
  7. ;;;; modify it under the terms of the GNU Lesser General Public
  8. ;;;; License as published by the Free Software Foundation; either
  9. ;;;; version 3 of the License, or (at your option) any later version.
  10. ;;;;
  11. ;;;; This library is distributed in the hope that it will be useful,
  12. ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. ;;;; Lesser General Public License for more details.
  15. ;;;;
  16. ;;;; You should have received a copy of the GNU Lesser General Public
  17. ;;;; License along with this library; if not, write to the Free Software
  18. ;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. (define-module (test-suite test-posix)
  20. :use-module (test-suite lib))
  21. ;; FIXME: The following exec tests are disabled since on an i386 debian with
  22. ;; glibc 2.3.2 they seem to interact badly with threads.test, the latter
  23. ;; dies with signal 32 (one of the SIGRTs). Don't know how or why, or who's
  24. ;; at fault (though it seems to happen with or without the recent memory
  25. ;; leak fix in these error cases).
  26. ;;
  27. ;; execl
  28. ;;
  29. ;; (with-test-prefix "execl"
  30. ;; (pass-if-exception "./nosuchprog" '(system-error . ".*")
  31. ;; (execl "./nosuchprog" "./nosuchprog" "some arg")))
  32. ;;
  33. ;; execlp
  34. ;;
  35. ;; (with-test-prefix "execlp"
  36. ;; (pass-if-exception "./nosuchprog" '(system-error . ".*")
  37. ;; (execlp "./nosuchprog" "./nosuchprog" "some arg")))
  38. ;;
  39. ;; execle
  40. ;;
  41. ;; (with-test-prefix "execle"
  42. ;; (pass-if-exception "./nosuchprog" '(system-error . ".*")
  43. ;; (execle "./nosuchprog" '() "./nosuchprog" "some arg"))
  44. ;; (pass-if-exception "./nosuchprog" '(system-error . ".*")
  45. ;; (execle "./nosuchprog" '("FOO=1" "BAR=2") "./nosuchprog" "some arg")))
  46. ;;
  47. ;; mkstemp!
  48. ;;
  49. (with-test-prefix "mkstemp!"
  50. ;; the temporary names used in the tests here are kept to 8 characters so
  51. ;; they'll work on a DOS 8.3 file system
  52. (define (string-copy str)
  53. (list->string (string->list str)))
  54. (pass-if-exception "number arg" exception:wrong-type-arg
  55. (mkstemp! 123))
  56. (pass-if "filename string modified"
  57. (let* ((template "T-XXXXXX")
  58. (str (string-copy template))
  59. (port (mkstemp! str))
  60. (result (not (string=? str template))))
  61. (close-port port)
  62. (delete-file str)
  63. result))
  64. (pass-if "binary mode honored"
  65. (let* ((template "T-XXXXXX")
  66. (str (string-copy template))
  67. (outport (mkstemp! str "wb")))
  68. (display "\n" outport)
  69. (close-port outport)
  70. (let* ((inport (open-input-file str #:binary #t))
  71. (char1 (read-char inport))
  72. (char2 (read-char inport))
  73. (result (and (char=? char1 #\newline)
  74. (eof-object? char2))))
  75. (close-port inport)
  76. (delete-file str)
  77. result))))
  78. ;;
  79. ;; putenv
  80. ;;
  81. (with-test-prefix "putenv"
  82. (pass-if "something"
  83. (putenv "FOO=something")
  84. (equal? "something" (getenv "FOO")))
  85. (pass-if "replacing"
  86. (putenv "FOO=one")
  87. (putenv "FOO=two")
  88. (equal? "two" (getenv "FOO")))
  89. (pass-if "empty"
  90. (putenv "FOO=")
  91. (equal? "" (getenv "FOO")))
  92. (pass-if "removing"
  93. (putenv "FOO=bar")
  94. (putenv "FOO")
  95. (not (getenv "FOO")))
  96. (pass-if "modifying string doesn't change env"
  97. (let ((s (string-copy "FOO=bar")))
  98. (putenv s)
  99. (string-set! s 5 #\x)
  100. (equal? "bar" (getenv "FOO")))))
  101. ;;
  102. ;; setenv
  103. ;;
  104. (with-test-prefix "setenv"
  105. (pass-if "something"
  106. (setenv "FOO" "something")
  107. (equal? "something" (getenv "FOO")))
  108. (pass-if "replacing"
  109. (setenv "FOO" "one")
  110. (setenv "FOO" "two")
  111. (equal? "two" (getenv "FOO")))
  112. (pass-if "empty"
  113. (setenv "FOO" "")
  114. (equal? "" (getenv "FOO")))
  115. (pass-if "removing"
  116. (setenv "FOO" "something")
  117. (setenv "FOO" #f)
  118. (not (getenv "FOO"))))
  119. ;;
  120. ;; unsetenv
  121. ;;
  122. (with-test-prefix "unsetenv"
  123. (pass-if "something"
  124. (putenv "FOO=something")
  125. (unsetenv "FOO")
  126. (not (getenv "FOO")))
  127. (pass-if "empty"
  128. (putenv "FOO=")
  129. (unsetenv "FOO")
  130. (not (getenv "FOO"))))
  131. ;;
  132. ;; ttyname
  133. ;;
  134. (with-test-prefix "ttyname"
  135. (pass-if-exception "non-tty argument" exception:system-error
  136. ;; This used to crash in 1.8.1 and earlier.
  137. (let ((file (false-if-exception
  138. (open-output-file "/dev/null"))))
  139. (if (not file)
  140. (throw 'unsupported)
  141. (ttyname file)))))
  142. ;;
  143. ;; utimes
  144. ;;
  145. (with-test-prefix "utime"
  146. (pass-if "valid argument (second resolution)"
  147. (let ((file "posix.test-utime"))
  148. (dynamic-wind
  149. (lambda ()
  150. (close-port (open-output-file file)))
  151. (lambda ()
  152. (let* ((accessed (+ (current-time) 3600))
  153. (modified (- accessed 1000)))
  154. (utime file accessed modified)
  155. (let ((info (stat file)))
  156. (and (= (stat:atime info) accessed)
  157. (= (stat:mtime info) modified)))))
  158. (lambda ()
  159. (delete-file file)))))
  160. (pass-if-equal "AT_SYMLINK_NOFOLLOW"
  161. '(1 1)
  162. (if (defined? 'AT_SYMLINK_NOFOLLOW)
  163. (let ((file "posix.test-utime"))
  164. (dynamic-wind
  165. (lambda ()
  166. (symlink "/dev/null" file))
  167. (lambda ()
  168. (utime file 1 1 0 0 AT_SYMLINK_NOFOLLOW)
  169. (let ((info (lstat file)))
  170. (list (stat:atime info) (stat:mtime info))))
  171. (lambda ()
  172. (delete-file file))))
  173. (throw 'unsupported))))
  174. ;;
  175. ;; affinity
  176. ;;
  177. (with-test-prefix "affinity"
  178. (pass-if "getaffinity"
  179. (if (defined? 'getaffinity)
  180. (> (bitvector-length (getaffinity (getpid))) 0)
  181. (throw 'unresolved)))
  182. (pass-if "setaffinity"
  183. (if (and (defined? 'setaffinity) (defined? 'getaffinity))
  184. (catch 'system-error
  185. (lambda ()
  186. (let ((mask (getaffinity (getpid))))
  187. (setaffinity (getpid) mask)
  188. (equal? mask (getaffinity (getpid)))))
  189. (lambda args
  190. ;; On some platforms such as sh4-linux-gnu, 'setaffinity'
  191. ;; returns ENOSYS.
  192. (let ((errno (system-error-errno args)))
  193. (if (= errno ENOSYS)
  194. (throw 'unresolved)
  195. (apply throw args)))))
  196. (throw 'unresolved))))
  197. ;;
  198. ;; system*
  199. ;;
  200. (with-test-prefix "system*"
  201. (pass-if "http://bugs.gnu.org/13166"
  202. ;; With Guile up to 2.0.7 included, the child process launched by
  203. ;; `system*' would remain alive after an `execvp' failure.
  204. (let ((me (getpid)))
  205. (and (not (zero? (system* "something-that-does-not-exist")))
  206. (= me (getpid))))))
  207. ;;
  208. ;; crypt
  209. ;;
  210. (with-test-prefix "crypt"
  211. (pass-if "basic usage"
  212. (if (not (defined? 'crypt))
  213. (throw 'unsupported)
  214. (string? (crypt "pass" "abcdefg"))))
  215. (pass-if "crypt invalid salt on glibc"
  216. (begin
  217. (unless (defined? 'crypt)
  218. (throw 'unsupported))
  219. (unless (string-contains %host-type "-gnu")
  220. (throw 'unresolved))
  221. (catch 'system-error
  222. (lambda ()
  223. ;; This used to deadlock on glibc while trying to throw to
  224. ;; 'system-error'. This test uses the special
  225. ;; interpretation of the salt that glibc does;
  226. ;; specifically, we pass a salt that's probably
  227. ;; syntactically invalid here. Note, whether it's invalid
  228. ;; or not is system-defined, so it's possible it just works.
  229. (string? (crypt "pass" "$X$abc")))
  230. (lambda _ #t)))))