Commit | Line | Data |
---|---|---|
55ae00ea LC |
1 | ;;;; net-db.test --- Test suite for `net-db' -*- mode: scheme; coding: utf-8; -*- |
2 | ;;;; Ludovic Courtès <ludo@gnu.org> | |
3 | ;;;; | |
1ba05158 | 4 | ;;;; Copyright (C) 2010, 2011, 2012 Free Software Foundation, Inc. |
55ae00ea LC |
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 | ||
20 | (define-module (test-suite test-net-db) | |
21 | #:use-module (srfi srfi-1) | |
22 | #:use-module (test-suite lib)) | |
23 | ||
24 | (if (provided? 'net-db) | |
25 | (with-test-prefix "getaddrinfo" | |
26 | ||
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?) | |
31 | (and ok? | |
32 | (= (sockaddr:addr sa) INADDR_LOOPBACK))) | |
33 | #t | |
34 | (map addrinfo:addr ai))))) | |
35 | ||
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?) | |
41 | (and ok? | |
42 | (= (sockaddr:addr sa) INADDR_LOOPBACK) | |
43 | (= (sockaddr:port sa) 80))) | |
44 | #t | |
45 | (map addrinfo:addr ai))))) | |
46 | ||
47 | (pass-if "port 80" | |
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))) | |
52 | (and ok? | |
55ae00ea | 53 | (= (sockaddr:port sa) 80)))) |
3ea159af LC |
54 | #t |
55 | ai)))) | |
56 | ||
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))) | |
63 | (and ok? | |
64 | (= (sockaddr:port sa) 80)))) | |
55ae00ea LC |
65 | #t |
66 | ai)))) | |
67 | ||
68 | (pass-if "no name" | |
69 | (catch 'getaddrinfo-error | |
70 | (lambda () | |
fb61f4b8 AW |
71 | (pk "getaddrinfo for \"does-not-exist\" succeeded!" |
72 | (getaddrinfo "does-not-exist")) | |
73 | (throw 'unresolved)) | |
55ae00ea | 74 | (lambda (key errcode) |
2fe5eb56 | 75 | ;; In some cases (e.g., in a chroot without |
66d86131 LC |
76 | ;; /etc/{hosts,resolv.conf}), this can result in |
77 | ;; `EAI_EAGAIN' (glibc 2.11), or `EAI_NODATA' (glibc 2.12). | |
2fe5eb56 | 78 | (and (or (= errcode EAI_NONAME) |
66d86131 LC |
79 | (and (defined? 'EAI_NODATA) ; GNU extension |
80 | (= errcode EAI_NODATA)) | |
81 | (= errcode EAI_AGAIN) | |
82 | (begin | |
83 | (format #t "unexpected error code: ~a ~s~%" | |
84 | errcode (gai-strerror errcode)) | |
85 | #f)) | |
55ae00ea LC |
86 | (string? (gai-strerror errcode)))))) |
87 | ||
88 | (pass-if "wrong service name" | |
89 | (catch 'getaddrinfo-error | |
90 | (lambda () | |
91 | (getaddrinfo "127.0.0.1" "does-not-exist" AI_NUMERICHOST) | |
2b386ab0 LC |
92 | |
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 | |
97 | ;; for details. | |
98 | (if (string-contains %host-type "darwin9.2") | |
99 | (throw 'unresolved) | |
100 | #f)) | |
55ae00ea | 101 | (lambda (key errcode) |
25bc75c4 | 102 | ;; According to POSIX, both error codes are valid (glibc 2.11 |
1ba05158 LC |
103 | ;; chooses `EAI_SERVICE'; Darwin 8.11.0 chooses the non-POSIX |
104 | ;; `EAI_NODATA', and more recent Darwin versions choose | |
105 | ;; `EAI_NONAME'.) | |
25bc75c4 | 106 | (and (or (= errcode EAI_SERVICE) |
1ba05158 LC |
107 | (= errcode EAI_NONAME) |
108 | (and (defined? 'EAI_NODATA) | |
109 | (= errcode EAI_NODATA))) | |
55ae00ea | 110 | (string? (gai-strerror errcode)))))))) |