ipc.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /*
  2. Copyright (c) 2012 Martin Sustrik All rights reserved.
  3. Copyright 2017 Garrett D'Amore <garrett@damore.org>
  4. Permission is hereby granted, free of charge, to any person obtaining a copy
  5. of this software and associated documentation files (the "Software"),
  6. to deal in the Software without restriction, including without limitation
  7. the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. and/or sell copies of the Software, and to permit persons to whom
  9. the Software is furnished to do so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included
  11. in all copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  15. THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  17. FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  18. IN THE SOFTWARE.
  19. */
  20. #include "../src/nn.h"
  21. #include "../src/pair.h"
  22. #include "../src/pubsub.h"
  23. #include "../src/ipc.h"
  24. #include "testutil.h"
  25. /* Tests IPC transport. */
  26. #define SOCKET_ADDRESS "ipc://test.ipc"
  27. int main ()
  28. {
  29. #ifndef NN_HAVE_WSL
  30. int sb;
  31. int sc;
  32. int i;
  33. int s1, s2;
  34. void * dummy_buf;
  35. int rc;
  36. int opt;
  37. size_t opt_sz = sizeof (opt);
  38. int size;
  39. char * buf;
  40. /* Try closing a IPC socket while it not connected. */
  41. sc = test_socket (AF_SP, NN_PAIR);
  42. test_connect (sc, SOCKET_ADDRESS);
  43. test_close (sc);
  44. /* Open the socket anew. */
  45. sc = test_socket (AF_SP, NN_PAIR);
  46. test_connect (sc, SOCKET_ADDRESS);
  47. /* Leave enough time for at least one re-connect attempt. */
  48. nn_sleep (200);
  49. sb = test_socket (AF_SP, NN_PAIR);
  50. test_bind (sb, SOCKET_ADDRESS);
  51. /* Ping-pong test. */
  52. for (i = 0; i != 1; ++i) {
  53. test_send (sc, "0123456789012345678901234567890123456789");
  54. test_recv (sb, "0123456789012345678901234567890123456789");
  55. test_send (sb, "0123456789012345678901234567890123456789");
  56. test_recv (sc, "0123456789012345678901234567890123456789");
  57. }
  58. /* Batch transfer test. */
  59. for (i = 0; i != 100; ++i) {
  60. test_send (sc, "XYZ");
  61. }
  62. for (i = 0; i != 100; ++i) {
  63. test_recv (sb, "XYZ");
  64. }
  65. /* Send something large enough to trigger overlapped I/O on Windows. */
  66. size = 10000;
  67. buf = malloc (size);
  68. if (!buf){
  69. fprintf (stderr, "Out of memory");
  70. exit(1);
  71. }
  72. for (i = 0; i < size; ++i) {
  73. buf[i] = 48 + i % 10;
  74. }
  75. buf[size-1] = '\0';
  76. test_send (sc, buf);
  77. test_recv (sb, buf);
  78. free (buf);
  79. test_close (sc);
  80. test_close (sb);
  81. /* Test whether connection rejection is handled decently. */
  82. sb = test_socket (AF_SP, NN_PAIR);
  83. test_bind (sb, SOCKET_ADDRESS);
  84. s1 = test_socket (AF_SP, NN_PAIR);
  85. test_connect (s1, SOCKET_ADDRESS);
  86. s2 = test_socket (AF_SP, NN_PAIR);
  87. test_connect (s2, SOCKET_ADDRESS);
  88. nn_sleep (100);
  89. test_close (s2);
  90. test_close (s1);
  91. test_close (sb);
  92. /* On Windows, CreateNamedPipeA does not run exclusively.
  93. We should look at fixing this, but it will require
  94. changing the usock code for Windows. In the meantime just
  95. disable this test on Windows. */
  96. #if !defined(NN_HAVE_WINDOWS)
  97. /* Test two sockets binding to the same address. */
  98. sb = test_socket (AF_SP, NN_PAIR);
  99. test_bind (sb, SOCKET_ADDRESS);
  100. s1 = test_socket (AF_SP, NN_PAIR);
  101. rc = nn_bind (s1, SOCKET_ADDRESS);
  102. nn_assert (rc < 0);
  103. errno_assert (nn_errno () == EADDRINUSE);
  104. sc = test_socket (AF_SP, NN_PAIR);
  105. test_connect (sc, SOCKET_ADDRESS);
  106. nn_sleep (100);
  107. test_send (sb, "ABC");
  108. test_recv (sc, "ABC");
  109. test_close (sb);
  110. test_close (sc);
  111. test_close (s1);
  112. #endif
  113. /* Test NN_RCVMAXSIZE limit */
  114. sb = test_socket (AF_SP, NN_PAIR);
  115. test_bind (sb, SOCKET_ADDRESS);
  116. s1 = test_socket (AF_SP, NN_PAIR);
  117. test_connect (s1, SOCKET_ADDRESS);
  118. opt = 4;
  119. rc = nn_setsockopt (sb, NN_SOL_SOCKET, NN_RCVMAXSIZE, &opt, opt_sz);
  120. nn_assert (rc == 0);
  121. nn_sleep (100);
  122. test_send (s1, "ABCD");
  123. test_recv (sb, "ABCD");
  124. test_send (s1, "ABCDE");
  125. /* Without sleep nn_recv returns EAGAIN even for string
  126. of acceptable size, so false positives are possible. */
  127. nn_sleep (100);
  128. rc = nn_recv (sb, &dummy_buf, NN_MSG, NN_DONTWAIT);
  129. nn_assert (rc < 0);
  130. errno_assert (nn_errno () == EAGAIN);
  131. test_close (sb);
  132. test_close (s1);
  133. /* Test that NN_RCVMAXSIZE can be -1, but not lower */
  134. sb = test_socket (AF_SP, NN_PAIR);
  135. opt = -1;
  136. rc = nn_setsockopt (sb, NN_SOL_SOCKET, NN_RCVMAXSIZE, &opt, opt_sz);
  137. nn_assert (rc >= 0);
  138. opt = -2;
  139. rc = nn_setsockopt (sb, NN_SOL_SOCKET, NN_RCVMAXSIZE, &opt, opt_sz);
  140. nn_assert (rc < 0);
  141. errno_assert (nn_errno () == EINVAL);
  142. test_close (sb);
  143. /* Test closing a socket that is waiting to connect. */
  144. sc = test_socket (AF_SP, NN_PAIR);
  145. test_connect (sc, SOCKET_ADDRESS);
  146. nn_sleep (100);
  147. test_close (sc);
  148. #endif /* NN_HAVE_WSL */
  149. return 0;
  150. }