net-db.test 4.7 KB

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