msg.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /*
  2. Copyright (c) 2013 Martin Sustrik All rights reserved.
  3. Copyright 2016 Franklin "Snaipe" Mathieu <franklinmathieu@gmail.com>
  4. Copyright 2017 Garrett D'Amore <garrett@damore.org>
  5. Permission is hereby granted, free of charge, to any person obtaining a copy
  6. of this software and associated documentation files (the "Software"),
  7. to deal in the Software without restriction, including without limitation
  8. the rights to use, copy, modify, merge, publish, distribute, sublicense,
  9. and/or sell copies of the Software, and to permit persons to whom
  10. the Software is furnished to do so, subject to the following conditions:
  11. The above copyright notice and this permission notice shall be included
  12. in all copies or substantial portions of the Software.
  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
  16. THE 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 "../src/nn.h"
  22. #include "../src/pair.h"
  23. #include "testutil.h"
  24. #include <string.h>
  25. #define SOCKET_ADDRESS "inproc://a"
  26. char longdata[1 << 20];
  27. int main (int argc, const char *argv[])
  28. {
  29. int rc;
  30. int sb;
  31. int sc;
  32. unsigned char *buf1, *buf2;
  33. int i;
  34. struct nn_iovec iov;
  35. struct nn_msghdr hdr;
  36. char socket_address_tcp[128];
  37. test_addr_from(socket_address_tcp, "tcp", "127.0.0.1",
  38. get_test_port(argc, argv));
  39. sb = test_socket (AF_SP, NN_PAIR);
  40. test_bind (sb, SOCKET_ADDRESS);
  41. sc = test_socket (AF_SP, NN_PAIR);
  42. test_connect (sc, SOCKET_ADDRESS);
  43. buf1 = nn_allocmsg (256, 0);
  44. alloc_assert (buf1);
  45. for (i = 0; i != 256; ++i)
  46. buf1 [i] = (unsigned char) i;
  47. rc = nn_send (sc, &buf1, NN_MSG, 0);
  48. errno_assert (rc >= 0);
  49. nn_assert (rc == 256);
  50. buf2 = NULL;
  51. rc = nn_recv (sb, &buf2, NN_MSG, 0);
  52. errno_assert (rc >= 0);
  53. nn_assert (rc == 256);
  54. nn_assert (buf2);
  55. for (i = 0; i != 256; ++i)
  56. nn_assert (buf2 [i] == (unsigned char) i);
  57. rc = nn_freemsg (buf2);
  58. errno_assert (rc == 0);
  59. buf1 = nn_allocmsg (256, 0);
  60. alloc_assert (buf1);
  61. for (i = 0; i != 256; ++i)
  62. buf1 [i] = (unsigned char) i;
  63. iov.iov_base = &buf1;
  64. iov.iov_len = NN_MSG;
  65. memset (&hdr, 0, sizeof (hdr));
  66. hdr.msg_iov = &iov;
  67. hdr.msg_iovlen = 1;
  68. rc = nn_sendmsg (sc, &hdr, 0);
  69. errno_assert (rc >= 0);
  70. nn_assert (rc == 256);
  71. buf2 = NULL;
  72. iov.iov_base = &buf2;
  73. iov.iov_len = NN_MSG;
  74. memset (&hdr, 0, sizeof (hdr));
  75. hdr.msg_iov = &iov;
  76. hdr.msg_iovlen = 1;
  77. rc = nn_recvmsg (sb, &hdr, 0);
  78. errno_assert (rc >= 0);
  79. nn_assert (rc == 256);
  80. nn_assert (buf2);
  81. for (i = 0; i != 256; ++i)
  82. nn_assert (buf2 [i] == (unsigned char) i);
  83. rc = nn_freemsg (buf2);
  84. errno_assert (rc == 0);
  85. test_close (sc);
  86. test_close (sb);
  87. /* Test receiving of large message */
  88. sb = test_socket (AF_SP, NN_PAIR);
  89. test_bind (sb, socket_address_tcp);
  90. sc = test_socket (AF_SP, NN_PAIR);
  91. test_connect (sc, socket_address_tcp);
  92. for (i = 0; i < (int) sizeof (longdata); ++i)
  93. longdata[i] = '0' + (i % 10);
  94. longdata [sizeof (longdata) - 1] = 0;
  95. test_send (sb, longdata);
  96. rc = nn_recv (sc, &buf2, NN_MSG, 0);
  97. errno_assert (rc >= 0);
  98. nn_assert (rc == sizeof (longdata) - 1);
  99. nn_assert (buf2);
  100. for (i = 0; i < (int) sizeof (longdata) - 1; ++i)
  101. nn_assert (buf2 [i] == longdata [i]);
  102. rc = nn_freemsg (buf2);
  103. errno_assert (rc == 0);
  104. test_close (sc);
  105. test_close (sb);
  106. /* Test reallocmsg */
  107. buf1 = nn_allocmsg (8, 0);
  108. alloc_assert (buf1);
  109. buf2 = nn_reallocmsg (buf1, 1);
  110. nn_assert (buf2 == buf1);
  111. buf1 = nn_reallocmsg (buf2, 100);
  112. nn_assert (buf1 != buf2);
  113. nn_assert (buf1 != 0);
  114. nn_freemsg (buf1);
  115. return 0;
  116. }