00-socket.test 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  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, 2021 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. ;;; setsockopt
  108. ;;;
  109. (with-test-prefix "setsockopt AF_INET"
  110. (if (and (defined? 'AF_INET) (defined? 'TCP_NODELAY))
  111. (pass-if "IPPROTO_TCP TCP_NODELAY"
  112. (let ((sock (socket AF_INET SOCK_STREAM 0)))
  113. (setsockopt sock IPPROTO_TCP TCP_NODELAY 1)
  114. (eqv? 1 (getsockopt sock IPPROTO_TCP TCP_NODELAY))))))
  115. ;;;
  116. ;;; AF_UNIX sockets and `make-socket-address'
  117. ;;;
  118. (define %tmpdir
  119. ;; Honor `$TMPDIR', which tmpnam(3) doesn't do.
  120. (or (getenv "TMPDIR") "/tmp"))
  121. (define %curdir
  122. ;; Remember the current working directory.
  123. (getcwd))
  124. ;; Temporarily cd to %TMPDIR. The goal is to work around path name
  125. ;; limitations, which can lead to exceptions like:
  126. ;;
  127. ;; (misc-error "scm_to_sockaddr"
  128. ;; "unix address path too long: ~A"
  129. ;; ("/tmp/nix-build-fb7bph4ifh0vr3ihigm702dzffdnapfj-guile-coverage-1.9.5.drv-0/guile-test-socket-1258553296-77619")
  130. ;; #f)
  131. (false-if-exception (chdir %tmpdir))
  132. (define (temp-file-path)
  133. ;; Return a temporary file name, assuming the current directory is %TMPDIR.
  134. (string-append "guile-test-socket-"
  135. (number->string (current-time)) "-"
  136. (number->string (random 100000))))
  137. (define (primitive-fork-if-available)
  138. (if (not (provided? 'fork))
  139. -1
  140. (primitive-fork)))
  141. (if (defined? 'AF_UNIX)
  142. (with-test-prefix "AF_UNIX/SOCK_DGRAM"
  143. ;; testing `bind' and `sendto' and datagram sockets
  144. (let ((server-socket (socket AF_UNIX SOCK_DGRAM 0))
  145. (server-bound? #f)
  146. (path (temp-file-path)))
  147. (pass-if "bind"
  148. (catch 'system-error
  149. (lambda ()
  150. (bind server-socket AF_UNIX path)
  151. (set! server-bound? #t)
  152. #t)
  153. (lambda args
  154. (let ((errno (system-error-errno args)))
  155. (cond ((= errno EADDRINUSE) (throw 'unresolved))
  156. (else (apply throw args)))))))
  157. (pass-if "bind/sockaddr"
  158. (let* ((sock (socket AF_UNIX SOCK_STREAM 0))
  159. (path (temp-file-path))
  160. (sockaddr (make-socket-address AF_UNIX path)))
  161. (catch 'system-error
  162. (lambda ()
  163. (bind sock sockaddr)
  164. (false-if-exception (delete-file path))
  165. #t)
  166. (lambda args
  167. (let ((errno (system-error-errno args)))
  168. (cond ((= errno EADDRINUSE) (throw 'unresolved))
  169. (else (apply throw args))))))))
  170. (pass-if "sendto"
  171. (if (not server-bound?)
  172. (throw 'unresolved)
  173. (let ((client (socket AF_UNIX SOCK_DGRAM 0))
  174. (message (string->utf8 "hello")))
  175. (> (sendto client message AF_UNIX path) 0))))
  176. (pass-if "sendto/sockaddr"
  177. (if (not server-bound?)
  178. (throw 'unresolved)
  179. (let ((client (socket AF_UNIX SOCK_DGRAM 0))
  180. (message (string->utf8 "hello"))
  181. (sockaddr (make-socket-address AF_UNIX path)))
  182. (> (sendto client message sockaddr) 0))))
  183. (false-if-exception (delete-file path)))))
  184. (if (defined? 'AF_UNIX)
  185. (with-test-prefix "AF_UNIX/SOCK_STREAM"
  186. ;; testing `bind', `listen' and `connect' on stream-oriented sockets
  187. (let ((server-socket (socket AF_UNIX SOCK_STREAM 0))
  188. (server-bound? #f)
  189. (server-listening? #f)
  190. (server-pid #f)
  191. (path (temp-file-path)))
  192. (pass-if "bind"
  193. (catch 'system-error
  194. (lambda ()
  195. (bind server-socket AF_UNIX path)
  196. (set! server-bound? #t)
  197. #t)
  198. (lambda args
  199. (let ((errno (system-error-errno args)))
  200. (cond ((= errno EADDRINUSE) (throw 'unresolved))
  201. (else (apply throw args)))))))
  202. (pass-if "bind/sockaddr"
  203. (let* ((sock (socket AF_UNIX SOCK_STREAM 0))
  204. (path (temp-file-path))
  205. (sockaddr (make-socket-address AF_UNIX path)))
  206. (catch 'system-error
  207. (lambda ()
  208. (bind sock sockaddr)
  209. (false-if-exception (delete-file path))
  210. #t)
  211. (lambda args
  212. (let ((errno (system-error-errno args)))
  213. (cond ((= errno EADDRINUSE) (throw 'unresolved))
  214. (else (apply throw args))))))))
  215. (pass-if "listen"
  216. (if (not server-bound?)
  217. (throw 'unresolved)
  218. (begin
  219. (listen server-socket 123)
  220. (set! server-listening? #t)
  221. #t)))
  222. (force-output (current-output-port))
  223. (force-output (current-error-port))
  224. (when server-listening?
  225. (let ((pid (primitive-fork-if-available)))
  226. ;; Spawn a server process.
  227. (case pid
  228. ((-1) ;; fork not available
  229. #f)
  230. ((0) ;; the kid: serve two connections and exit
  231. (let serve ((conn
  232. (false-if-exception (accept server-socket)))
  233. (count 1))
  234. (if (not conn)
  235. (exit 1)
  236. (if (> count 0)
  237. (serve (false-if-exception (accept server-socket))
  238. (- count 1)))))
  239. (exit 0))
  240. (else ;; the parent
  241. (set! server-pid pid)
  242. #t))))
  243. (pass-if "connect"
  244. (if (not server-pid)
  245. (throw 'unresolved)
  246. (let ((s (socket AF_UNIX SOCK_STREAM 0)))
  247. (connect s AF_UNIX path)
  248. #t)))
  249. (pass-if "connect/sockaddr"
  250. (if (not server-pid)
  251. (throw 'unresolved)
  252. (let ((s (socket AF_UNIX SOCK_STREAM 0)))
  253. (connect s (make-socket-address AF_UNIX path))
  254. #t)))
  255. (pass-if "accept"
  256. (if (not server-pid)
  257. (throw 'unresolved)
  258. (let ((status (cdr (waitpid server-pid))))
  259. (eqv? 0 (status:exit-val status)))))
  260. (false-if-exception (delete-file path))
  261. #t)
  262. ;; Testing `send', `recv!' & co. on stream-oriented sockets (with
  263. ;; a bit of duplication with the above.)
  264. (let ((server-socket (socket AF_UNIX SOCK_STREAM 0))
  265. (server-bound? #f)
  266. (server-listening? #f)
  267. (server-pid #f)
  268. (message "hello, world!")
  269. (path (temp-file-path)))
  270. (define (sub-bytevector bv len)
  271. (let ((c (make-bytevector len)))
  272. (bytevector-copy! bv 0 c 0 len)
  273. c))
  274. (pass-if "bind (bis)"
  275. (catch 'system-error
  276. (lambda ()
  277. (bind server-socket AF_UNIX path)
  278. (set! server-bound? #t)
  279. #t)
  280. (lambda args
  281. (let ((errno (system-error-errno args)))
  282. (cond ((= errno EADDRINUSE) (throw 'unresolved))
  283. (else (apply throw args)))))))
  284. (pass-if "listen (bis)"
  285. (if (not server-bound?)
  286. (throw 'unresolved)
  287. (begin
  288. (listen server-socket 123)
  289. (set! server-listening? #t)
  290. #t)))
  291. (force-output (current-output-port))
  292. (force-output (current-error-port))
  293. (if server-listening?
  294. (let ((pid (primitive-fork-if-available)))
  295. ;; Spawn a server process.
  296. (case pid
  297. ((-1)
  298. #f)
  299. ((0) ;; the kid: send MESSAGE and exit
  300. (exit
  301. (false-if-exception
  302. (let ((conn (car (accept server-socket)))
  303. (bv (string->utf8 message)))
  304. (= (bytevector-length bv)
  305. (send conn bv))))))
  306. (else ;; the parent
  307. (set! server-pid pid)
  308. #t))))
  309. (pass-if "recv!"
  310. (if (not server-pid)
  311. (throw 'unresolved)
  312. (let ((s (socket AF_UNIX SOCK_STREAM 0)))
  313. (connect s AF_UNIX path)
  314. (let* ((buf (make-bytevector 123))
  315. (received (recv! s buf)))
  316. (string=? (utf8->string (sub-bytevector buf received))
  317. message)))))
  318. (pass-if "accept (bis)"
  319. (if (not server-pid)
  320. (throw 'unresolved)
  321. (let ((status (cdr (waitpid server-pid))))
  322. (eqv? 0 (status:exit-val status)))))
  323. (false-if-exception (delete-file path))
  324. #t)))
  325. (if (defined? 'AF_INET6)
  326. (with-test-prefix "AF_INET6/SOCK_STREAM"
  327. ;; testing `bind', `listen' and `connect' on stream-oriented sockets
  328. (let ((server-socket
  329. ;; Some platforms don't support this protocol/family combination.
  330. (false-if-exception (socket AF_INET6 SOCK_STREAM 0)))
  331. (server-bound? #f)
  332. (server-listening? #f)
  333. (server-pid #f)
  334. (ipv6-addr 1) ; ::1
  335. (server-port 8889)
  336. (client-port 9998))
  337. (pass-if "bind"
  338. (if (not server-socket)
  339. (throw 'unresolved))
  340. (catch 'system-error
  341. (lambda ()
  342. (bind server-socket AF_INET6 ipv6-addr server-port)
  343. (set! server-bound? #t)
  344. #t)
  345. (lambda args
  346. (let ((errno (system-error-errno args)))
  347. (cond ((= errno EADDRINUSE) (throw 'unresolved))
  348. ;; On Linux-based systems, when `ipv6' support is
  349. ;; missing (for instance, `ipv6' is loaded and
  350. ;; /proc/sys/net/ipv6/conf/all/disable_ipv6 is set
  351. ;; to 1), the socket call above succeeds but
  352. ;; bind(2) fails like this.
  353. ((= errno EADDRNOTAVAIL) (throw 'unresolved))
  354. (else (apply throw args)))))))
  355. (pass-if "bind/sockaddr"
  356. (let* ((sock (false-if-exception (socket AF_INET6 SOCK_STREAM 0)))
  357. (sockaddr (make-socket-address AF_INET6 ipv6-addr client-port)))
  358. (if (not sock)
  359. (throw 'unresolved))
  360. (catch 'system-error
  361. (lambda ()
  362. (bind sock sockaddr)
  363. #t)
  364. (lambda args
  365. (let ((errno (system-error-errno args)))
  366. (cond ((= errno EADDRINUSE) (throw 'unresolved))
  367. ((= errno EADDRNOTAVAIL) (throw 'unresolved))
  368. (else (apply throw args))))))))
  369. (pass-if "listen"
  370. (if (not server-bound?)
  371. (throw 'unresolved)
  372. (begin
  373. (listen server-socket 123)
  374. (set! server-listening? #t)
  375. #t)))
  376. (force-output (current-output-port))
  377. (force-output (current-error-port))
  378. (if server-listening?
  379. (let ((pid (primitive-fork-if-available)))
  380. ;; Spawn a server process.
  381. (case pid
  382. ((-1)
  383. #f)
  384. ((0) ;; the kid: serve two connections and exit
  385. (let serve ((conn
  386. (false-if-exception (accept server-socket)))
  387. (count 1))
  388. (if (not conn)
  389. (exit 1)
  390. (if (> count 0)
  391. (serve (false-if-exception (accept server-socket))
  392. (- count 1)))))
  393. (exit 0))
  394. (else ;; the parent
  395. (set! server-pid pid)
  396. #t))))
  397. (pass-if "connect"
  398. (if (not server-pid)
  399. (throw 'unresolved)
  400. (let ((s (socket AF_INET6 SOCK_STREAM 0)))
  401. (connect s AF_INET6 ipv6-addr server-port)
  402. #t)))
  403. (pass-if "connect/sockaddr"
  404. (if (not server-pid)
  405. (throw 'unresolved)
  406. (let ((s (socket AF_INET6 SOCK_STREAM 0)))
  407. (connect s (make-socket-address AF_INET6 ipv6-addr server-port))
  408. #t)))
  409. (pass-if "accept"
  410. (if (not server-pid)
  411. (throw 'unresolved)
  412. (let ((status (cdr (waitpid server-pid))))
  413. (eqv? 0 (status:exit-val status)))))
  414. #t)))
  415. ;; Switch back to the previous directory.
  416. (false-if-exception (chdir %curdir))