test-getnameinfo.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /* Copyright Joyent, Inc. and other Node contributors. All rights reserved.
  2. *
  3. * Permission is hereby granted, free of charge, to any person obtaining a copy
  4. * of this software and associated documentation files (the "Software"), to
  5. * deal in the Software without restriction, including without limitation the
  6. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  7. * sell copies of the Software, and to permit persons to whom the Software is
  8. * furnished to do so, subject to the following conditions:
  9. *
  10. * The above copyright notice and this permission notice shall be included in
  11. * all copies or substantial portions of the Software.
  12. *
  13. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  18. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  19. * IN THE SOFTWARE.
  20. */
  21. #include "uv.h"
  22. #include "task.h"
  23. #include <stdio.h>
  24. #include <stdlib.h>
  25. #include <string.h>
  26. static const char* address_ip4 = "127.0.0.1";
  27. static const char* address_ip6 = "::1";
  28. static const int port = 80;
  29. static struct sockaddr_in addr4;
  30. static struct sockaddr_in6 addr6;
  31. static uv_getnameinfo_t req;
  32. static void getnameinfo_req(uv_getnameinfo_t* handle,
  33. int status,
  34. const char* hostname,
  35. const char* service) {
  36. ASSERT(handle != NULL);
  37. ASSERT(status == 0);
  38. ASSERT(hostname != NULL);
  39. ASSERT(service != NULL);
  40. }
  41. TEST_IMPL(getnameinfo_basic_ip4) {
  42. int r;
  43. r = uv_ip4_addr(address_ip4, port, &addr4);
  44. ASSERT(r == 0);
  45. r = uv_getnameinfo(uv_default_loop(),
  46. &req,
  47. &getnameinfo_req,
  48. (const struct sockaddr*)&addr4,
  49. 0);
  50. ASSERT(r == 0);
  51. uv_run(uv_default_loop(), UV_RUN_DEFAULT);
  52. MAKE_VALGRIND_HAPPY();
  53. return 0;
  54. }
  55. TEST_IMPL(getnameinfo_basic_ip4_sync) {
  56. ASSERT(0 == uv_ip4_addr(address_ip4, port, &addr4));
  57. ASSERT(0 == uv_getnameinfo(uv_default_loop(),
  58. &req,
  59. NULL,
  60. (const struct sockaddr*)&addr4,
  61. 0));
  62. ASSERT(req.host[0] != '\0');
  63. ASSERT(req.service[0] != '\0');
  64. MAKE_VALGRIND_HAPPY();
  65. return 0;
  66. }
  67. TEST_IMPL(getnameinfo_basic_ip6) {
  68. int r;
  69. r = uv_ip6_addr(address_ip6, port, &addr6);
  70. ASSERT(r == 0);
  71. r = uv_getnameinfo(uv_default_loop(),
  72. &req,
  73. &getnameinfo_req,
  74. (const struct sockaddr*)&addr6,
  75. 0);
  76. ASSERT(r == 0);
  77. uv_run(uv_default_loop(), UV_RUN_DEFAULT);
  78. MAKE_VALGRIND_HAPPY();
  79. return 0;
  80. }