1
0

test-udp-options.c 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. TEST_IMPL(udp_options) {
  27. static int invalid_ttls[] = { -1, 0, 256 };
  28. struct sockaddr_in addr;
  29. uv_loop_t* loop;
  30. uv_udp_t h;
  31. int i, r;
  32. ASSERT(0 == uv_ip4_addr("0.0.0.0", TEST_PORT, &addr));
  33. loop = uv_default_loop();
  34. r = uv_udp_init(loop, &h);
  35. ASSERT(r == 0);
  36. uv_unref((uv_handle_t*)&h); /* don't keep the loop alive */
  37. r = uv_udp_bind(&h, (const struct sockaddr*) &addr, 0);
  38. ASSERT(r == 0);
  39. r = uv_udp_set_broadcast(&h, 1);
  40. r |= uv_udp_set_broadcast(&h, 1);
  41. r |= uv_udp_set_broadcast(&h, 0);
  42. r |= uv_udp_set_broadcast(&h, 0);
  43. ASSERT(r == 0);
  44. /* values 1-255 should work */
  45. for (i = 1; i <= 255; i++) {
  46. r = uv_udp_set_ttl(&h, i);
  47. ASSERT(r == 0);
  48. }
  49. for (i = 0; i < (int) ARRAY_SIZE(invalid_ttls); i++) {
  50. r = uv_udp_set_ttl(&h, invalid_ttls[i]);
  51. ASSERT(r == UV_EINVAL);
  52. }
  53. r = uv_udp_set_multicast_loop(&h, 1);
  54. r |= uv_udp_set_multicast_loop(&h, 1);
  55. r |= uv_udp_set_multicast_loop(&h, 0);
  56. r |= uv_udp_set_multicast_loop(&h, 0);
  57. ASSERT(r == 0);
  58. /* values 0-255 should work */
  59. for (i = 0; i <= 255; i++) {
  60. r = uv_udp_set_multicast_ttl(&h, i);
  61. ASSERT(r == 0);
  62. }
  63. /* anything >255 should fail */
  64. r = uv_udp_set_multicast_ttl(&h, 256);
  65. ASSERT(r == UV_EINVAL);
  66. /* don't test ttl=-1, it's a valid value on some platforms */
  67. r = uv_run(loop, UV_RUN_DEFAULT);
  68. ASSERT(r == 0);
  69. MAKE_VALGRIND_HAPPY();
  70. return 0;
  71. }