1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#pragma once
/*
Copyright (c) 2018 Contributors as noted in the AUTHORS file
This file is part of libzmq, the ZeroMQ core engine in C++.
libzmq is free software; you can redistribute it and/or modify it under
the terms of the GNU Lesser General Public License (LGPL) as published
by the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
As a special exception, the Contributors give you permission to link
this library with independent modules to produce an executable,
regardless of the license terms of these independent modules, and to
copy and distribute the resulting executable under terms of your choice,
provided that you also meet, for each linked independent module, the
terms and conditions of the license of that module. An independent
module is a module which is not derived from or based on this library.
If you modify this library, you must extend this exception to your
version of the library.
libzmq is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "../include/zmq.h"
#include <unity.h>
#include <string.h>
#include <stdio.h>
#if defined(_MSC_VER) && _MSC_VER <= 1800
#define snprintf _snprintf
#endif
int test_assert_success_message_errno_helper (int rc,
const char *msg,
const char *expr)
{
if (rc == -1) {
char buffer[512];
buffer[sizeof (buffer) - 1] =
0; // to ensure defined behavior with VC++ <= 2013
snprintf (buffer, sizeof (buffer) - 1,
"%s failed%s%s%s, errno = %i (%s)", expr,
msg ? " (additional info: " : "", msg ? msg : "",
msg ? ")" : "", zmq_errno (), zmq_strerror (zmq_errno ()));
TEST_FAIL_MESSAGE (buffer);
}
return rc;
}
#define TEST_ASSERT_SUCCESS_MESSAGE_ERRNO(expr, msg) \
test_assert_success_message_errno_helper (expr, msg, #expr)
#define TEST_ASSERT_SUCCESS_ERRNO(expr) \
test_assert_success_message_errno_helper (expr, NULL, #expr)
void send_string_expect_success (void *socket, const char *str, int flags)
{
const size_t len = str ? strlen (str) : 0;
const int rc = zmq_send (socket, str, len, flags);
TEST_ASSERT_EQUAL_INT ((int) len, rc);
}
void recv_string_expect_success (void *socket, const char *str, int flags)
{
const size_t len = str ? strlen (str) : 0;
char buffer[255];
TEST_ASSERT_LESS_OR_EQUAL_MESSAGE (sizeof (buffer), len,
"recv_string_expect_success cannot be "
"used for strings longer than 255 "
"characters");
const int rc =
TEST_ASSERT_SUCCESS_ERRNO (zmq_recv (socket, buffer, sizeof (buffer), 0));
TEST_ASSERT_EQUAL_INT ((int) len, rc);
if (str)
TEST_ASSERT_EQUAL_STRING_LEN (str, buffer, len);
}
// do not call from tests directly, use setup_test_context, get_test_context and teardown_test_context only
void *internal_manage_test_context (bool init, bool clear)
{
static void *test_context = NULL;
if (clear) {
TEST_ASSERT_NOT_NULL (test_context);
TEST_ASSERT_SUCCESS_ERRNO (zmq_ctx_term (test_context));
test_context = NULL;
} else {
if (init) {
TEST_ASSERT_NULL (test_context);
test_context = zmq_ctx_new ();
TEST_ASSERT_NOT_NULL (test_context);
}
}
return test_context;
}
#define MAX_TEST_SOCKETS 128
void internal_manage_test_sockets (void *socket, bool add)
{
static void *test_sockets[MAX_TEST_SOCKETS];
static size_t test_socket_count = 0;
if (!socket) {
assert (!add);
// force-close all sockets
if (test_socket_count) {
for (size_t i = 0; i < test_socket_count; ++i) {
close_zero_linger (test_sockets[i]);
}
fprintf (stderr,
"WARNING: Forced closure of %i sockets, this is an "
"implementation error unless the test case failed\n",
(int) test_socket_count);
test_socket_count = 0;
}
} else {
if (add) {
++test_socket_count;
TEST_ASSERT_LESS_THAN_MESSAGE (MAX_TEST_SOCKETS, test_socket_count,
"MAX_TEST_SOCKETS must be "
"increased, or you cannot use the "
"test context");
test_sockets[test_socket_count - 1] = socket;
} else {
bool found = false;
for (size_t i = 0; i < test_socket_count; ++i) {
if (test_sockets[i] == socket) {
found = true;
}
if (found) {
if (i < test_socket_count)
test_sockets[i] = test_sockets[i + 1];
}
}
TEST_ASSERT_TRUE (found);
--test_socket_count;
}
}
}
void setup_test_context ()
{
internal_manage_test_context (true, false);
}
void *get_test_context ()
{
return internal_manage_test_context (false, false);
}
void teardown_test_context ()
{
// this condition allows an explicit call to teardown_test_context from a
// test. if this is never used, it should probably be removed, to detect
// misuses
if (get_test_context ()) {
internal_manage_test_sockets (NULL, false);
internal_manage_test_context (false, true);
}
}
void *test_context_socket (int type)
{
void *const socket = zmq_socket (get_test_context (), type);
TEST_ASSERT_NOT_NULL (socket);
internal_manage_test_sockets (socket, true);
return socket;
}
void *test_context_socket_close (void *socket)
{
TEST_ASSERT_SUCCESS_ERRNO (zmq_close (socket));
internal_manage_test_sockets (socket, false);
return socket;
}