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