00-socket.test 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  1. ;;;; 00-socket.test --- test socket functions -*- scheme -*-
  2. ;;;;
  3. ;;;; Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010,
  4. ;;;; 2011, 2012, 2013, 2014, 2017 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. ;; This test runs early, so that we can fork before any threads are
  20. ;; created in other tests.
  21. (define-module (test-suite test-socket)
  22. #:use-module (rnrs bytevectors)
  23. #:use-module (srfi srfi-26)
  24. #:use-module (test-suite lib))
  25. ;;;
  26. ;;; inet-ntop
  27. ;;;
  28. (if (defined? 'inet-ntop)
  29. (with-test-prefix "inet-ntop"
  30. (with-test-prefix "ipv6"
  31. (pass-if "0"
  32. (string? (inet-ntop AF_INET6 0)))
  33. (pass-if "2^128-1"
  34. (string? (inet-ntop AF_INET6 (1- (ash 1 128)))))
  35. (pass-if-exception "-1" exception:out-of-range
  36. (inet-ntop AF_INET6 -1))
  37. (pass-if-exception "2^128" exception:out-of-range
  38. (inet-ntop AF_INET6 (ash 1 128)))
  39. (pass-if-exception "2^1024" exception:out-of-range
  40. (inet-ntop AF_INET6 (ash 1 1024))))))
  41. ;;;
  42. ;;; inet-pton
  43. ;;;
  44. (if (defined? 'inet-pton)
  45. (with-test-prefix "inet-pton"
  46. (with-test-prefix "ipv6"
  47. (pass-if "00:00:00:00:00:00:00:00"
  48. (eqv? 0 (inet-pton AF_INET6 "00:00:00:00:00:00:00:00")))
  49. (pass-if "0:0:0:0:0:0:0:1"
  50. (eqv? 1 (inet-pton AF_INET6 "0:0:0:0:0:0:0:1")))
  51. (pass-if "::1"
  52. (eqv? 1 (inet-pton AF_INET6 "::1")))
  53. (pass-if "FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF"
  54. (eqv? #xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
  55. (inet-pton AF_INET6
  56. "FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF")))
  57. (pass-if "F000:0000:0000:0000:0000:0000:0000:0000"
  58. (eqv? #xF0000000000000000000000000000000
  59. (inet-pton AF_INET6
  60. "F000:0000:0000:0000:0000:0000:0000:0000")))
  61. (pass-if "0F00:0000:0000:0000:0000:0000:0000:0000"
  62. (eqv? #x0F000000000000000000000000000000
  63. (inet-pton AF_INET6
  64. "0F00:0000:0000:0000:0000:0000:0000:0000")))
  65. (pass-if "0000:0000:0000:0000:0000:0000:0000:00F0"
  66. (eqv? #xF0
  67. (inet-pton AF_INET6
  68. "0000:0000:0000:0000:0000:0000:0000:00F0"))))))
  69. (if (defined? 'inet-ntop)
  70. (with-test-prefix "inet-ntop"
  71. (with-test-prefix "ipv4"
  72. (pass-if "127.0.0.1"
  73. (equal? "127.0.0.1" (inet-ntop AF_INET INADDR_LOOPBACK))))
  74. (if (defined? 'AF_INET6)
  75. (with-test-prefix "ipv6"
  76. (pass-if "FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF"
  77. (string-ci=? "FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF"
  78. (inet-ntop AF_INET6 (- (expt 2 128) 1))))
  79. (pass-if "::1"
  80. (equal? "::1" (inet-ntop AF_INET6 1)))))))
  81. ;;;
  82. ;;; make-socket-address
  83. ;;;
  84. (with-test-prefix "make-socket-address"
  85. (if (defined? 'AF_INET)
  86. (pass-if "AF_INET"
  87. (let ((sa (make-socket-address AF_INET 123456 80)))
  88. (and (= (sockaddr:fam sa) AF_INET)
  89. (= (sockaddr:addr sa) 123456)
  90. (= (sockaddr:port sa) 80)))))
  91. (if (defined? 'AF_INET6)
  92. (pass-if "AF_INET6"
  93. ;; Since the platform doesn't necessarily support `scopeid', we won't
  94. ;; test it.
  95. (let ((sa* (make-socket-address AF_INET6 123456 80 1))
  96. (sa+ (make-socket-address AF_INET6 123456 80)))
  97. (and (= (sockaddr:fam sa*) (sockaddr:fam sa+) AF_INET6)
  98. (= (sockaddr:addr sa*) (sockaddr:addr sa+) 123456)
  99. (= (sockaddr:port sa*) (sockaddr:port sa+) 80)
  100. (= (sockaddr:flowinfo sa*) 1)))))
  101. (if (defined? 'AF_UNIX)
  102. (pass-if "AF_UNIX"
  103. (let ((sa (make-socket-address AF_UNIX "/tmp/unix-socket")))
  104. (and (= (sockaddr:fam sa) AF_UNIX)
  105. (string=? (sockaddr:path sa) "/tmp/unix-socket"))))))
  106. ;;;
  107. ;;; AF_UNIX sockets and `make-socket-address'
  108. ;;;
  109. (define %tmpdir
  110. ;; Honor `$TMPDIR', which tmpnam(3) doesn't do.
  111. (or (getenv "TMPDIR") "/tmp"))
  112. (define %curdir
  113. ;; Remember the current working directory.
  114. (getcwd))
  115. ;; Temporarily cd to %TMPDIR. The goal is to work around path name
  116. ;; limitations, which can lead to exceptions like:
  117. ;;
  118. ;; (misc-error "scm_to_sockaddr"
  119. ;; "unix address path too long: ~A"
  120. ;; ("/tmp/nix-build-fb7bph4ifh0vr3ihigm702dzffdnapfj-guile-coverage-1.9.5.drv-0/guile-test-socket-1258553296-77619")
  121. ;; #f)
  122. (chdir %tmpdir)
  123. (define (temp-file-path)
  124. ;; Return a temporary file name, assuming the current directory is %TMPDIR.
  125. (string-append "guile-test-socket-"
  126. (number->string (current-time)) "-"
  127. (number->string (random 100000))))
  128. (define (primitive-fork-if-available)
  129. (if (not (provided? 'fork))
  130. -1
  131. (primitive-fork)))
  132. (if (defined? 'AF_UNIX)
  133. (with-test-prefix "AF_UNIX/SOCK_DGRAM"
  134. ;; testing `bind' and `sendto' and datagram sockets
  135. (let ((server-socket (socket AF_UNIX SOCK_DGRAM 0))
  136. (server-bound? #f)
  137. (path (temp-file-path)))
  138. (pass-if "bind"
  139. (catch 'system-error
  140. (lambda ()
  141. (bind server-socket AF_UNIX path)
  142. (set! server-bound? #t)
  143. #t)
  144. (lambda args
  145. (let ((errno (system-error-errno args)))
  146. (cond ((= errno EADDRINUSE) (throw 'unresolved))
  147. (else (apply throw args)))))))
  148. (pass-if "bind/sockaddr"
  149. (let* ((sock (socket AF_UNIX SOCK_STREAM 0))
  150. (path (temp-file-path))
  151. (sockaddr (make-socket-address AF_UNIX path)))
  152. (catch 'system-error
  153. (lambda ()
  154. (bind sock sockaddr)
  155. (false-if-exception (delete-file path))
  156. #t)
  157. (lambda args
  158. (let ((errno (system-error-errno args)))
  159. (cond ((= errno EADDRINUSE) (throw 'unresolved))
  160. (else (apply throw args))))))))
  161. (pass-if "sendto"
  162. (if (not server-bound?)
  163. (throw 'unresolved)
  164. (let ((client (socket AF_UNIX SOCK_DGRAM 0))
  165. (message (string->utf8 "hello")))
  166. (> (sendto client message AF_UNIX path) 0))))
  167. (pass-if "sendto/sockaddr"
  168. (if (not server-bound?)
  169. (throw 'unresolved)
  170. (let ((client (socket AF_UNIX SOCK_DGRAM 0))
  171. (message (string->utf8 "hello"))
  172. (sockaddr (make-socket-address AF_UNIX path)))
  173. (> (sendto client message sockaddr) 0))))
  174. (false-if-exception (delete-file path)))))
  175. (if (defined? 'AF_UNIX)
  176. (with-test-prefix "AF_UNIX/SOCK_STREAM"
  177. ;; testing `bind', `listen' and `connect' on stream-oriented sockets
  178. (let ((server-socket (socket AF_UNIX SOCK_STREAM 0))
  179. (server-bound? #f)
  180. (server-listening? #f)
  181. (server-pid #f)
  182. (path (temp-file-path)))
  183. (pass-if "bind"
  184. (catch 'system-error
  185. (lambda ()
  186. (bind server-socket AF_UNIX path)
  187. (set! server-bound? #t)
  188. #t)
  189. (lambda args
  190. (let ((errno (system-error-errno args)))
  191. (cond ((= errno EADDRINUSE) (throw 'unresolved))
  192. (else (apply throw args)))))))
  193. (pass-if "bind/sockaddr"
  194. (let* ((sock (socket AF_UNIX SOCK_STREAM 0))
  195. (path (temp-file-path))
  196. (sockaddr (make-socket-address AF_UNIX path)))
  197. (catch 'system-error
  198. (lambda ()
  199. (bind sock sockaddr)
  200. (false-if-exception (delete-file path))
  201. #t)
  202. (lambda args
  203. (let ((errno (system-error-errno args)))
  204. (cond ((= errno EADDRINUSE) (throw 'unresolved))
  205. (else (apply throw args))))))))
  206. (pass-if "listen"
  207. (if (not server-bound?)
  208. (throw 'unresolved)
  209. (begin
  210. (listen server-socket 123)
  211. (set! server-listening? #t)
  212. #t)))
  213. (force-output (current-output-port))
  214. (force-output (current-error-port))
  215. (if server-listening?
  216. (let ((pid (primitive-fork-if-available)))
  217. ;; Spawn a server process.
  218. (case pid
  219. ((-1) (throw 'unresolved))
  220. ((0) ;; the kid: serve two connections and exit
  221. (let serve ((conn
  222. (false-if-exception (accept server-socket)))
  223. (count 1))
  224. (if (not conn)
  225. (exit 1)
  226. (if (> count 0)
  227. (serve (false-if-exception (accept server-socket))
  228. (- count 1)))))
  229. (exit 0))
  230. (else ;; the parent
  231. (set! server-pid pid)
  232. #t))))
  233. (pass-if "connect"
  234. (if (not server-pid)
  235. (throw 'unresolved)
  236. (let ((s (socket AF_UNIX SOCK_STREAM 0)))
  237. (connect s AF_UNIX path)
  238. #t)))
  239. (pass-if "connect/sockaddr"
  240. (if (not server-pid)
  241. (throw 'unresolved)
  242. (let ((s (socket AF_UNIX SOCK_STREAM 0)))
  243. (connect s (make-socket-address AF_UNIX path))
  244. #t)))
  245. (pass-if "accept"
  246. (if (not server-pid)
  247. (throw 'unresolved)
  248. (let ((status (cdr (waitpid server-pid))))
  249. (eqv? 0 (status:exit-val status)))))
  250. (false-if-exception (delete-file path))
  251. #t)
  252. ;; Testing `send', `recv!' & co. on stream-oriented sockets (with
  253. ;; a bit of duplication with the above.)
  254. (let ((server-socket (socket AF_UNIX SOCK_STREAM 0))
  255. (server-bound? #f)
  256. (server-listening? #f)
  257. (server-pid #f)
  258. (message "hello, world!")
  259. (path (temp-file-path)))
  260. (define (sub-bytevector bv len)
  261. (let ((c (make-bytevector len)))
  262. (bytevector-copy! bv 0 c 0 len)
  263. c))
  264. (pass-if "bind (bis)"
  265. (catch 'system-error
  266. (lambda ()
  267. (bind server-socket AF_UNIX path)
  268. (set! server-bound? #t)
  269. #t)
  270. (lambda args
  271. (let ((errno (system-error-errno args)))
  272. (cond ((= errno EADDRINUSE) (throw 'unresolved))
  273. (else (apply throw args)))))))
  274. (pass-if "listen (bis)"
  275. (if (not server-bound?)
  276. (throw 'unresolved)
  277. (begin
  278. (listen server-socket 123)
  279. (set! server-listening? #t)
  280. #t)))
  281. (force-output (current-output-port))
  282. (force-output (current-error-port))
  283. (if server-listening?
  284. (let ((pid (primitive-fork-if-available)))
  285. ;; Spawn a server process.
  286. (case pid
  287. ((-1) (throw 'unresolved))
  288. ((0) ;; the kid: send MESSAGE and exit
  289. (exit
  290. (false-if-exception
  291. (let ((conn (car (accept server-socket)))
  292. (bv (string->utf8 message)))
  293. (= (bytevector-length bv)
  294. (send conn bv))))))
  295. (else ;; the parent
  296. (set! server-pid pid)
  297. #t))))
  298. (pass-if "recv!"
  299. (if (not server-pid)
  300. (throw 'unresolved)
  301. (let ((s (socket AF_UNIX SOCK_STREAM 0)))
  302. (connect s AF_UNIX path)
  303. (let* ((buf (make-bytevector 123))
  304. (received (recv! s buf)))
  305. (string=? (utf8->string (sub-bytevector buf received))
  306. message)))))
  307. (pass-if "accept (bis)"
  308. (if (not server-pid)
  309. (throw 'unresolved)
  310. (let ((status (cdr (waitpid server-pid))))
  311. (eqv? 0 (status:exit-val status)))))
  312. (false-if-exception (delete-file path))
  313. #t)))
  314. (if (defined? 'AF_INET6)
  315. (with-test-prefix "AF_INET6/SOCK_STREAM"
  316. ;; testing `bind', `listen' and `connect' on stream-oriented sockets
  317. (let ((server-socket
  318. ;; Some platforms don't support this protocol/family combination.
  319. (false-if-exception (socket AF_INET6 SOCK_STREAM 0)))
  320. (server-bound? #f)
  321. (server-listening? #f)
  322. (server-pid #f)
  323. (ipv6-addr 1) ; ::1
  324. (server-port 8889)
  325. (client-port 9998))
  326. (pass-if "bind"
  327. (if (not server-socket)
  328. (throw 'unresolved))
  329. (catch 'system-error
  330. (lambda ()
  331. (bind server-socket AF_INET6 ipv6-addr server-port)
  332. (set! server-bound? #t)
  333. #t)
  334. (lambda args
  335. (let ((errno (system-error-errno args)))
  336. (cond ((= errno EADDRINUSE) (throw 'unresolved))
  337. ;; On Linux-based systems, when `ipv6' support is
  338. ;; missing (for instance, `ipv6' is loaded and
  339. ;; /proc/sys/net/ipv6/conf/all/disable_ipv6 is set
  340. ;; to 1), the socket call above succeeds but
  341. ;; bind(2) fails like this.
  342. ((= errno EADDRNOTAVAIL) (throw 'unresolved))
  343. (else (apply throw args)))))))
  344. (pass-if "bind/sockaddr"
  345. (let* ((sock (false-if-exception (socket AF_INET6 SOCK_STREAM 0)))
  346. (sockaddr (make-socket-address AF_INET6 ipv6-addr client-port)))
  347. (if (not sock)
  348. (throw 'unresolved))
  349. (catch 'system-error
  350. (lambda ()
  351. (bind sock sockaddr)
  352. #t)
  353. (lambda args
  354. (let ((errno (system-error-errno args)))
  355. (cond ((= errno EADDRINUSE) (throw 'unresolved))
  356. ((= errno EADDRNOTAVAIL) (throw 'unresolved))
  357. (else (apply throw args))))))))
  358. (pass-if "listen"
  359. (if (not server-bound?)
  360. (throw 'unresolved)
  361. (begin
  362. (listen server-socket 123)
  363. (set! server-listening? #t)
  364. #t)))
  365. (force-output (current-output-port))
  366. (force-output (current-error-port))
  367. (if server-listening?
  368. (let ((pid (primitive-fork-if-available)))
  369. ;; Spawn a server process.
  370. (case pid
  371. ((-1) (throw 'unresolved))
  372. ((0) ;; the kid: serve two connections and exit
  373. (let serve ((conn
  374. (false-if-exception (accept server-socket)))
  375. (count 1))
  376. (if (not conn)
  377. (exit 1)
  378. (if (> count 0)
  379. (serve (false-if-exception (accept server-socket))
  380. (- count 1)))))
  381. (exit 0))
  382. (else ;; the parent
  383. (set! server-pid pid)
  384. #t))))
  385. (pass-if "connect"
  386. (if (not server-pid)
  387. (throw 'unresolved)
  388. (let ((s (socket AF_INET6 SOCK_STREAM 0)))
  389. (connect s AF_INET6 ipv6-addr server-port)
  390. #t)))
  391. (pass-if "connect/sockaddr"
  392. (if (not server-pid)
  393. (throw 'unresolved)
  394. (let ((s (socket AF_INET6 SOCK_STREAM 0)))
  395. (connect s (make-socket-address AF_INET6 ipv6-addr server-port))
  396. #t)))
  397. (pass-if "accept"
  398. (if (not server-pid)
  399. (throw 'unresolved)
  400. (let ((status (cdr (waitpid server-pid))))
  401. (eqv? 0 (status:exit-val status)))))
  402. #t)))
  403. ;; Switch back to the previous directory.
  404. (false-if-exception (chdir %curdir))