test-udp-dgram-too-big.c 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. #define CHECK_HANDLE(handle) \
  27. ASSERT((uv_udp_t*)(handle) == &handle_)
  28. #define CHECK_REQ(req) \
  29. ASSERT((req) == &req_);
  30. static uv_udp_t handle_;
  31. static uv_udp_send_t req_;
  32. static int send_cb_called;
  33. static int close_cb_called;
  34. static void close_cb(uv_handle_t* handle) {
  35. CHECK_HANDLE(handle);
  36. close_cb_called++;
  37. }
  38. static void send_cb(uv_udp_send_t* req, int status) {
  39. CHECK_REQ(req);
  40. CHECK_HANDLE(req->handle);
  41. ASSERT(status == UV_EMSGSIZE);
  42. uv_close((uv_handle_t*)req->handle, close_cb);
  43. send_cb_called++;
  44. }
  45. TEST_IMPL(udp_dgram_too_big) {
  46. char dgram[65536]; /* 64K MTU is unlikely, even on localhost */
  47. struct sockaddr_in addr;
  48. uv_buf_t buf;
  49. int r;
  50. memset(dgram, 42, sizeof dgram); /* silence valgrind */
  51. r = uv_udp_init(uv_default_loop(), &handle_);
  52. ASSERT(r == 0);
  53. buf = uv_buf_init(dgram, sizeof dgram);
  54. ASSERT(0 == uv_ip4_addr("127.0.0.1", TEST_PORT, &addr));
  55. r = uv_udp_send(&req_,
  56. &handle_,
  57. &buf,
  58. 1,
  59. (const struct sockaddr*) &addr,
  60. send_cb);
  61. ASSERT(r == 0);
  62. ASSERT(close_cb_called == 0);
  63. ASSERT(send_cb_called == 0);
  64. uv_run(uv_default_loop(), UV_RUN_DEFAULT);
  65. ASSERT(send_cb_called == 1);
  66. ASSERT(close_cb_called == 1);
  67. MAKE_VALGRIND_HAPPY();
  68. return 0;
  69. }