test-udp-options.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. 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, uv_ip4_addr("0.0.0.0", TEST_PORT), 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. ASSERT(r == 0);
  46. }
  47. for (i = 0; i < (int) ARRAY_SIZE(invalid_ttls); i++) {
  48. r = uv_udp_set_ttl(&h, invalid_ttls[i]);
  49. ASSERT(r == -1);
  50. ASSERT(uv_last_error(loop).code == UV_EINVAL);
  51. }
  52. r = uv_udp_set_multicast_loop(&h, 1);
  53. r |= uv_udp_set_multicast_loop(&h, 1);
  54. r |= uv_udp_set_multicast_loop(&h, 0);
  55. r |= uv_udp_set_multicast_loop(&h, 0);
  56. ASSERT(r == 0);
  57. /* values 0-255 should work */
  58. for (i = 0; i <= 255; i++) {
  59. r = uv_udp_set_multicast_ttl(&h, i);
  60. ASSERT(r == 0);
  61. }
  62. /* anything >255 should fail */
  63. r = uv_udp_set_multicast_ttl(&h, 256);
  64. ASSERT(r == -1);
  65. ASSERT(uv_last_error(loop).code == 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. }