| 1 |
;;;; net-db.test --- Test suite for `net-db' -*- mode: scheme; coding: utf-8; -*- |
| 2 |
;;;; Ludovic Courtès <[email protected]> |
| 3 |
;;;; |
| 4 |
;;;; Copyright (C) 2010, 2011, 2012 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 |
|
| 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? |
| 53 |
(= (sockaddr:port sa) 80)))) |
| 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)))) |
| 65 |
#t |
| 66 |
ai)))) |
| 67 |
|
| 68 |
(pass-if "no name" |
| 69 |
(catch 'getaddrinfo-error |
| 70 |
(lambda () |
| 71 |
(pk "getaddrinfo for \"does-not-exist\" succeeded!" |
| 72 |
(getaddrinfo "does-not-exist")) |
| 73 |
(throw 'unresolved)) |
| 74 |
(lambda (key errcode) |
| 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)) |
| 81 |
(= errcode EAI_AGAIN) |
| 82 |
(begin |
| 83 |
(format #t "unexpected error code: ~a ~s~%" |
| 84 |
errcode (gai-strerror errcode)) |
| 85 |
#f)) |
| 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) |
| 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)) |
| 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 |
| 105 |
;; `EAI_NONAME'.) |
| 106 |
(and (or (= errcode EAI_SERVICE) |
| 107 |
(= errcode EAI_NONAME) |
| 108 |
(and (defined? 'EAI_NODATA) |
| 109 |
(= errcode EAI_NODATA))) |
| 110 |
(string? (gai-strerror errcode)))))))) |