test-udp-bind.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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_bind) {
  27. struct sockaddr_in addr;
  28. uv_loop_t* loop;
  29. uv_udp_t h1, h2;
  30. int r;
  31. ASSERT(0 == uv_ip4_addr("0.0.0.0", TEST_PORT, &addr));
  32. loop = uv_default_loop();
  33. r = uv_udp_init(loop, &h1);
  34. ASSERT(r == 0);
  35. r = uv_udp_init(loop, &h2);
  36. ASSERT(r == 0);
  37. r = uv_udp_bind(&h1, (const struct sockaddr*) &addr, 0);
  38. ASSERT(r == 0);
  39. r = uv_udp_bind(&h2, (const struct sockaddr*) &addr, 0);
  40. ASSERT(r == UV_EADDRINUSE);
  41. uv_close((uv_handle_t*) &h1, NULL);
  42. uv_close((uv_handle_t*) &h2, NULL);
  43. r = uv_run(loop, UV_RUN_DEFAULT);
  44. ASSERT(r == 0);
  45. MAKE_VALGRIND_HAPPY();
  46. return 0;
  47. }
  48. TEST_IMPL(udp_bind_reuseaddr) {
  49. struct sockaddr_in addr;
  50. uv_loop_t* loop;
  51. uv_udp_t h1, h2;
  52. int r;
  53. ASSERT(0 == uv_ip4_addr("0.0.0.0", TEST_PORT, &addr));
  54. loop = uv_default_loop();
  55. r = uv_udp_init(loop, &h1);
  56. ASSERT(r == 0);
  57. r = uv_udp_init(loop, &h2);
  58. ASSERT(r == 0);
  59. r = uv_udp_bind(&h1, (const struct sockaddr*) &addr, UV_UDP_REUSEADDR);
  60. ASSERT(r == 0);
  61. r = uv_udp_bind(&h2, (const struct sockaddr*) &addr, UV_UDP_REUSEADDR);
  62. ASSERT(r == 0);
  63. uv_close((uv_handle_t*) &h1, NULL);
  64. uv_close((uv_handle_t*) &h2, NULL);
  65. r = uv_run(loop, UV_RUN_DEFAULT);
  66. ASSERT(r == 0);
  67. MAKE_VALGRIND_HAPPY();
  68. return 0;
  69. }