test_setsockopt.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /*
  2. Copyright (c) 2007-2016 Contributors as noted in the AUTHORS file
  3. This file is part of libzmq, the ZeroMQ core engine in C++.
  4. libzmq is free software; you can redistribute it and/or modify it under
  5. the terms of the GNU Lesser General Public License (LGPL) as published
  6. by the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. As a special exception, the Contributors give you permission to link
  9. this library with independent modules to produce an executable,
  10. regardless of the license terms of these independent modules, and to
  11. copy and distribute the resulting executable under terms of your choice,
  12. provided that you also meet, for each linked independent module, the
  13. terms and conditions of the license of that module. An independent
  14. module is a module which is not derived from or based on this library.
  15. If you modify this library, you must extend this exception to your
  16. version of the library.
  17. libzmq is distributed in the hope that it will be useful, but WITHOUT
  18. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  19. FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
  20. License for more details.
  21. You should have received a copy of the GNU Lesser General Public License
  22. along with this program. If not, see <http://www.gnu.org/licenses/>.
  23. */
  24. #include "testutil.hpp"
  25. #include "testutil_unity.hpp"
  26. #include <string.h>
  27. SETUP_TEARDOWN_TESTCONTEXT
  28. void test_setsockopt_tcp_recv_buffer ()
  29. {
  30. void *socket = test_context_socket (ZMQ_PUSH);
  31. int val = 0;
  32. size_t placeholder = sizeof (val);
  33. TEST_ASSERT_SUCCESS_ERRNO (
  34. zmq_getsockopt (socket, ZMQ_RCVBUF, &val, &placeholder));
  35. TEST_ASSERT_EQUAL_INT (-1, val);
  36. val = 16384;
  37. TEST_ASSERT_SUCCESS_ERRNO (
  38. zmq_setsockopt (socket, ZMQ_RCVBUF, &val, sizeof (val)));
  39. TEST_ASSERT_EQUAL_INT (16384, val);
  40. TEST_ASSERT_SUCCESS_ERRNO (
  41. zmq_getsockopt (socket, ZMQ_RCVBUF, &val, &placeholder));
  42. TEST_ASSERT_EQUAL_INT (16384, val);
  43. test_context_socket_close (socket);
  44. }
  45. void test_setsockopt_tcp_send_buffer ()
  46. {
  47. void *socket = test_context_socket (ZMQ_PUSH);
  48. int val = 0;
  49. size_t placeholder = sizeof (val);
  50. TEST_ASSERT_SUCCESS_ERRNO (
  51. zmq_getsockopt (socket, ZMQ_SNDBUF, &val, &placeholder));
  52. TEST_ASSERT_EQUAL_INT (-1, val);
  53. val = 16384;
  54. TEST_ASSERT_SUCCESS_ERRNO (
  55. zmq_setsockopt (socket, ZMQ_SNDBUF, &val, sizeof (val)));
  56. TEST_ASSERT_EQUAL_INT (16384, val);
  57. TEST_ASSERT_SUCCESS_ERRNO (
  58. zmq_getsockopt (socket, ZMQ_SNDBUF, &val, &placeholder));
  59. TEST_ASSERT_EQUAL_INT (16384, val);
  60. test_context_socket_close (socket);
  61. }
  62. void test_setsockopt_use_fd ()
  63. {
  64. void *socket = test_context_socket (ZMQ_PUSH);
  65. int val = 0;
  66. size_t placeholder = sizeof (val);
  67. TEST_ASSERT_SUCCESS_ERRNO (
  68. zmq_getsockopt (socket, ZMQ_USE_FD, &val, &placeholder));
  69. TEST_ASSERT_EQUAL_INT (-1, val);
  70. val = 3;
  71. TEST_ASSERT_SUCCESS_ERRNO (
  72. zmq_setsockopt (socket, ZMQ_USE_FD, &val, sizeof (val)));
  73. TEST_ASSERT_EQUAL_INT (3, val);
  74. TEST_ASSERT_SUCCESS_ERRNO (
  75. zmq_getsockopt (socket, ZMQ_USE_FD, &val, &placeholder));
  76. TEST_ASSERT_EQUAL_INT (3, val);
  77. test_context_socket_close (socket);
  78. }
  79. #define BOUNDDEVBUFSZ 16
  80. void test_setsockopt_bindtodevice ()
  81. {
  82. void *socket = test_context_socket (ZMQ_PUSH);
  83. #ifdef ZMQ_BINDTODEVICE
  84. char devname[BOUNDDEVBUFSZ];
  85. size_t buflen = BOUNDDEVBUFSZ;
  86. TEST_ASSERT_SUCCESS_ERRNO (
  87. zmq_getsockopt (socket, ZMQ_BINDTODEVICE, devname, &buflen));
  88. TEST_ASSERT_EQUAL_INT8 ('\0', devname[0]);
  89. TEST_ASSERT_EQUAL_UINT (1, buflen);
  90. sprintf (devname, "testdev");
  91. buflen = strlen (devname);
  92. TEST_ASSERT_SUCCESS_ERRNO (
  93. zmq_setsockopt (socket, ZMQ_BINDTODEVICE, devname, buflen));
  94. buflen = BOUNDDEVBUFSZ;
  95. memset (devname, 0, buflen);
  96. TEST_ASSERT_SUCCESS_ERRNO (
  97. zmq_getsockopt (socket, ZMQ_BINDTODEVICE, devname, &buflen));
  98. TEST_ASSERT_EQUAL_STRING_LEN ("testdev", devname, buflen);
  99. #endif
  100. test_context_socket_close (socket);
  101. }
  102. int main ()
  103. {
  104. setup_test_environment ();
  105. UNITY_BEGIN ();
  106. RUN_TEST (test_setsockopt_tcp_recv_buffer);
  107. RUN_TEST (test_setsockopt_tcp_send_buffer);
  108. RUN_TEST (test_setsockopt_use_fd);
  109. RUN_TEST (test_setsockopt_bindtodevice);
  110. return UNITY_END ();
  111. }