testutil_unity.hpp 8.61 KB
Newer Older
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
#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

43 44 45
int test_assert_success_message_errno_helper (int rc_,
                                              const char *msg_,
                                              const char *expr_)
46
{
47
    if (rc_ == -1) {
48 49 50 51
        char buffer[512];
        buffer[sizeof (buffer) - 1] =
          0; // to ensure defined behavior with VC++ <= 2013
        snprintf (buffer, sizeof (buffer) - 1,
52 53 54
                  "%s failed%s%s%s, errno = %i (%s)", expr_,
                  msg_ ? " (additional info: " : "", msg_ ? msg_ : "",
                  msg_ ? ")" : "", zmq_errno (), zmq_strerror (zmq_errno ()));
55 56
        TEST_FAIL_MESSAGE (buffer);
    }
57
    return rc_;
58 59
}

60 61 62
int test_assert_success_message_raw_errno_helper (int rc_,
                                                  const char *msg_,
                                                  const char *expr_)
63
{
64
    if (rc_ == -1) {
65 66 67 68 69 70 71 72 73 74
#if defined ZMQ_HAVE_WINDOWS
        int current_errno = WSAGetLastError ();
#else
        int current_errno = errno;
#endif

        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",
75 76
                  expr_, msg_ ? " (additional info: " : "", msg_ ? msg_ : "",
                  msg_ ? ")" : "", current_errno);
77 78
        TEST_FAIL_MESSAGE (buffer);
    }
79
    return rc_;
80 81
}

82 83 84 85 86 87
#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)

88 89 90
#define TEST_ASSERT_SUCCESS_RAW_ERRNO(expr)                                    \
    test_assert_success_message_raw_errno_helper (expr, NULL, #expr)

91 92 93 94 95 96 97
#define TEST_ASSERT_FAILURE_ERRNO(error_code, expr)                            \
    {                                                                          \
        int rc = (expr);                                                       \
        TEST_ASSERT_EQUAL_INT (-1, rc);                                        \
        TEST_ASSERT_EQUAL_INT (error_code, errno);                             \
    }

98
void send_string_expect_success (void *socket_, const char *str_, int flags_)
99
{
100 101
    const size_t len = str_ ? strlen (str_) : 0;
    const int rc = zmq_send (socket_, str_, len, flags_);
102 103 104
    TEST_ASSERT_EQUAL_INT ((int) len, rc);
}

105
void recv_string_expect_success (void *socket_, const char *str_, int flags_)
106
{
107
    const size_t len = str_ ? strlen (str_) : 0;
108 109 110 111 112 113
    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");

114
    const int rc = TEST_ASSERT_SUCCESS_ERRNO (
115
      zmq_recv (socket_, buffer, sizeof (buffer), flags_));
116
    TEST_ASSERT_EQUAL_INT ((int) len, rc);
117 118
    if (str_)
        TEST_ASSERT_EQUAL_STRING_LEN (str_, buffer, len);
119
}
120 121

// do not call from tests directly, use setup_test_context, get_test_context and teardown_test_context only
122
void *internal_manage_test_context (bool init_, bool clear_)
123 124
{
    static void *test_context = NULL;
125
    if (clear_) {
126 127 128 129
        TEST_ASSERT_NOT_NULL (test_context);
        TEST_ASSERT_SUCCESS_ERRNO (zmq_ctx_term (test_context));
        test_context = NULL;
    } else {
130
        if (init_) {
131 132 133 134 135 136 137 138 139 140
            TEST_ASSERT_NULL (test_context);
            test_context = zmq_ctx_new ();
            TEST_ASSERT_NOT_NULL (test_context);
        }
    }
    return test_context;
}

#define MAX_TEST_SOCKETS 128

141
void internal_manage_test_sockets (void *socket_, bool add_)
142 143 144
{
    static void *test_sockets[MAX_TEST_SOCKETS];
    static size_t test_socket_count = 0;
145 146
    if (!socket_) {
        assert (!add_);
147 148 149 150 151 152 153 154 155 156 157 158 159

        // 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 {
160
        if (add_) {
161 162 163 164 165
            ++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");
166
            test_sockets[test_socket_count - 1] = socket_;
167 168 169
        } else {
            bool found = false;
            for (size_t i = 0; i < test_socket_count; ++i) {
170
                if (test_sockets[i] == socket_) {
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
                    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);
    }
}

205
void *test_context_socket (int type_)
206
{
207
    void *const socket = zmq_socket (get_test_context (), type_);
208 209 210 211 212
    TEST_ASSERT_NOT_NULL (socket);
    internal_manage_test_sockets (socket, true);
    return socket;
}

213
void *test_context_socket_close (void *socket_)
214
{
215 216 217
    TEST_ASSERT_SUCCESS_ERRNO (zmq_close (socket_));
    internal_manage_test_sockets (socket_, false);
    return socket_;
218
}
219

220
void *test_context_socket_close_zero_linger (void *socket_)
221 222
{
    const int linger = 0;
223
    int rc = zmq_setsockopt (socket_, ZMQ_LINGER, &linger, sizeof (linger));
224
    TEST_ASSERT_TRUE (rc == 0 || zmq_errno () == ETERM);
225
    return test_context_socket_close (socket_);
226 227
}

228 229 230 231
void test_bind (void *socket_,
                const char *bind_address_,
                char *my_endpoint_,
                size_t len_)
232
{
233
    TEST_ASSERT_SUCCESS_ERRNO (zmq_bind (socket_, bind_address_));
234
    TEST_ASSERT_SUCCESS_ERRNO (
235
      zmq_getsockopt (socket_, ZMQ_LAST_ENDPOINT, my_endpoint_, &len_));
236 237
}

238
void bind_loopback (void *socket_, int ipv6_, char *my_endpoint_, size_t len_)
239
{
240
    if (ipv6_ && !is_ipv6_available ()) {
241 242 243 244
        TEST_IGNORE_MESSAGE ("ipv6 is not available");
    }

    TEST_ASSERT_SUCCESS_ERRNO (
245
      zmq_setsockopt (socket_, ZMQ_IPV6, &ipv6_, sizeof (int)));
246

247 248
    test_bind (socket_, ipv6_ ? "tcp://[::1]:*" : "tcp://127.0.0.1:*",
               my_endpoint_, len_);
249
}
250

251
void bind_loopback_ipv4 (void *socket_, char *my_endpoint_, size_t len_)
252
{
253
    bind_loopback (socket_, false, my_endpoint_, len_);
254
}