1 ;;;; net-db.test --- Test suite for `net-db' -*- mode: scheme; coding: utf-8; -*-
2 ;;;; Ludovic Courtès <ludo@gnu.org>
4 ;;;; Copyright (C) 2010, 2011, 2012 Free Software Foundation, Inc.
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.
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.
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
20 (define-module (test-suite test-net-db)
21 #:use-module (srfi srfi-1)
22 #:use-module (test-suite lib))
24 (if (provided? 'net-db)
25 (with-test-prefix "getaddrinfo"
27 (pass-if "127.0.0.1, any service"
28 (let ((ai (getaddrinfo "127.0.0.1" #f AI_NUMERICHOST)))
29 (and (> (length ai) 0)
30 (fold (lambda (sa ok?)
32 (= (sockaddr:addr sa) INADDR_LOOPBACK)))
34 (map addrinfo:addr ai)))))
36 (pass-if "127.0.0.1:80"
37 (let ((ai (getaddrinfo "127.0.0.1" "80"
38 (logior AI_NUMERICHOST AI_NUMERICSERV))))
39 (and (> (length ai) 0)
40 (fold (lambda (sa ok?)
42 (= (sockaddr:addr sa) INADDR_LOOPBACK)
43 (= (sockaddr:port sa) 80)))
45 (map addrinfo:addr ai)))))
48 (let ((ai (getaddrinfo #f "80" (logior AI_ADDRCONFIG AI_NUMERICSERV))))
49 (and (> (length ai) 0)
50 (fold (lambda (ai ok?)
51 (let ((sa (addrinfo:addr ai)))
53 (= (sockaddr:port sa) 80))))
57 (pass-if "port 80 with family and socket type"
58 (let ((ai (getaddrinfo #f "80" (logior AI_ADDRCONFIG AI_NUMERICSERV)
59 AF_UNSPEC SOCK_STREAM)))
60 (and (> (length ai) 0)
61 (fold (lambda (ai ok?)
62 (let ((sa (addrinfo:addr ai)))
64 (= (sockaddr:port sa) 80))))
69 (catch 'getaddrinfo-error
71 (pk "getaddrinfo for \"does-not-exist\" succeeded!"
72 (getaddrinfo "does-not-exist"))
75 ;; In some cases (e.g., in a chroot without
76 ;; /etc/{hosts,resolv.conf}), this can result in
77 ;; `EAI_EAGAIN' (glibc 2.11), or `EAI_NODATA' (glibc 2.12).
78 (and (or (= errcode EAI_NONAME)
79 (and (defined? 'EAI_NODATA) ; GNU extension
80 (= errcode EAI_NODATA))
83 (format #t "unexpected error code: ~a ~s~%"
84 errcode (gai-strerror errcode))
86 (string? (gai-strerror errcode))))))
88 (pass-if "wrong service name"
89 (catch 'getaddrinfo-error
91 (getaddrinfo "127.0.0.1" "does-not-exist" AI_NUMERICHOST)
93 ;; XXX: The call above unexpectedly suceeds on
94 ;; `i386-apple-darwin9.2.2', but not on `i386-apple-darwin9.6.0'.
95 ;; For now we just skip it until a better solution is found. See
96 ;; http://lists.gnu.org/archive/html/bug-gnulib/2010-02/msg00061.html
98 (if (string-contains %host-type "darwin9.2")
101 (lambda (key errcode)
102 ;; According to POSIX, both error codes are valid (glibc 2.11
103 ;; chooses `EAI_SERVICE'; Darwin 8.11.0 chooses the non-POSIX
104 ;; `EAI_NODATA', and more recent Darwin versions choose
106 (and (or (= errcode EAI_SERVICE)
107 (= errcode EAI_NONAME)
108 (and (defined? 'EAI_NODATA)
109 (= errcode EAI_NODATA)))
110 (string? (gai-strerror errcode))))))))