test-udp-options.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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 int udp_options_test(const struct sockaddr* addr) {
  27. static int invalid_ttls[] = { -1, 0, 256 };
  28. uv_loop_t* loop;
  29. uv_udp_t h;
  30. int i, r;
  31. loop = uv_default_loop();
  32. r = uv_udp_init(loop, &h);
  33. ASSERT(r == 0);
  34. uv_unref((uv_handle_t*)&h); /* don't keep the loop alive */
  35. r = uv_udp_bind(&h, addr, 0);
  36. ASSERT(r == 0);
  37. r = uv_udp_set_broadcast(&h, 1);
  38. r |= uv_udp_set_broadcast(&h, 1);
  39. r |= uv_udp_set_broadcast(&h, 0);
  40. r |= uv_udp_set_broadcast(&h, 0);
  41. ASSERT(r == 0);
  42. /* values 1-255 should work */
  43. for (i = 1; i <= 255; i++) {
  44. r = uv_udp_set_ttl(&h, i);
  45. #if defined(__MVS__)
  46. if (addr->sa_family == AF_INET6)
  47. ASSERT(r == 0);
  48. else
  49. ASSERT(r == UV_ENOTSUP);
  50. #else
  51. ASSERT(r == 0);
  52. #endif
  53. }
  54. for (i = 0; i < (int) ARRAY_SIZE(invalid_ttls); i++) {
  55. r = uv_udp_set_ttl(&h, invalid_ttls[i]);
  56. ASSERT(r == UV_EINVAL);
  57. }
  58. r = uv_udp_set_multicast_loop(&h, 1);
  59. r |= uv_udp_set_multicast_loop(&h, 1);
  60. r |= uv_udp_set_multicast_loop(&h, 0);
  61. r |= uv_udp_set_multicast_loop(&h, 0);
  62. ASSERT(r == 0);
  63. /* values 0-255 should work */
  64. for (i = 0; i <= 255; i++) {
  65. r = uv_udp_set_multicast_ttl(&h, i);
  66. ASSERT(r == 0);
  67. }
  68. /* anything >255 should fail */
  69. r = uv_udp_set_multicast_ttl(&h, 256);
  70. ASSERT(r == UV_EINVAL);
  71. /* don't test ttl=-1, it's a valid value on some platforms */
  72. r = uv_run(loop, UV_RUN_DEFAULT);
  73. ASSERT(r == 0);
  74. MAKE_VALGRIND_HAPPY();
  75. return 0;
  76. }
  77. TEST_IMPL(udp_options) {
  78. struct sockaddr_in addr;
  79. ASSERT(0 == uv_ip4_addr("0.0.0.0", TEST_PORT, &addr));
  80. return udp_options_test((const struct sockaddr*) &addr);
  81. }
  82. TEST_IMPL(udp_options6) {
  83. struct sockaddr_in6 addr;
  84. if (!can_ipv6())
  85. RETURN_SKIP("IPv6 not supported");
  86. ASSERT(0 == uv_ip6_addr("::", TEST_PORT, &addr));
  87. return udp_options_test((const struct sockaddr*) &addr);
  88. }
  89. TEST_IMPL(udp_no_autobind) {
  90. uv_loop_t* loop;
  91. uv_udp_t h;
  92. uv_udp_t h2;
  93. loop = uv_default_loop();
  94. /* Test a lazy initialized socket. */
  95. ASSERT(0 == uv_udp_init(loop, &h));
  96. ASSERT(UV_EBADF == uv_udp_set_multicast_ttl(&h, 32));
  97. ASSERT(UV_EBADF == uv_udp_set_broadcast(&h, 1));
  98. #if defined(__MVS__)
  99. ASSERT(UV_ENOTSUP == uv_udp_set_ttl(&h, 1));
  100. #else
  101. ASSERT(UV_EBADF == uv_udp_set_ttl(&h, 1));
  102. #endif
  103. ASSERT(UV_EBADF == uv_udp_set_multicast_loop(&h, 1));
  104. ASSERT(UV_EBADF == uv_udp_set_multicast_interface(&h, "0.0.0.0"));
  105. uv_close((uv_handle_t*) &h, NULL);
  106. /* Test a non-lazily initialized socket. */
  107. ASSERT(0 == uv_udp_init_ex(loop, &h2, AF_INET));
  108. ASSERT(0 == uv_udp_set_multicast_ttl(&h2, 32));
  109. ASSERT(0 == uv_udp_set_broadcast(&h2, 1));
  110. #if defined(__MVS__)
  111. /* zOS only supports setting ttl for IPv6 sockets. */
  112. ASSERT(UV_ENOTSUP == uv_udp_set_ttl(&h2, 1));
  113. #else
  114. ASSERT(0 == uv_udp_set_ttl(&h2, 1));
  115. #endif
  116. ASSERT(0 == uv_udp_set_multicast_loop(&h2, 1));
  117. ASSERT(0 == uv_udp_set_multicast_interface(&h2, "0.0.0.0"));
  118. uv_close((uv_handle_t*) &h2, NULL);
  119. ASSERT(0 == uv_run(loop, UV_RUN_DEFAULT));
  120. MAKE_VALGRIND_HAPPY();
  121. return 0;
  122. }