test_setsockopt.cpp 4.37 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
/*
    Copyright (c) 2007-2016 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/>.
*/

30
#include "testutil.hpp"
31
#include "testutil_unity.hpp"
32

33 34
#include <string.h>

35
SETUP_TEARDOWN_TESTCONTEXT
36 37 38 39

void test_setsockopt_tcp_recv_buffer ()
{
    void *socket = test_context_socket (ZMQ_PUSH);
40 41

    int val = 0;
42
    size_t placeholder = sizeof (val);
43

44 45 46
    TEST_ASSERT_SUCCESS_ERRNO (
      zmq_getsockopt (socket, ZMQ_RCVBUF, &val, &placeholder));
    TEST_ASSERT_EQUAL_INT (-1, val);
47 48 49

    val = 16384;

50 51 52
    TEST_ASSERT_SUCCESS_ERRNO (
      zmq_setsockopt (socket, ZMQ_RCVBUF, &val, sizeof (val)));
    TEST_ASSERT_EQUAL_INT (16384, val);
53

54 55 56
    TEST_ASSERT_SUCCESS_ERRNO (
      zmq_getsockopt (socket, ZMQ_RCVBUF, &val, &placeholder));
    TEST_ASSERT_EQUAL_INT (16384, val);
57

58
    test_context_socket_close (socket);
59 60
}

61
void 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 69 70
    TEST_ASSERT_SUCCESS_ERRNO (
      zmq_getsockopt (socket, ZMQ_SNDBUF, &val, &placeholder));
    TEST_ASSERT_EQUAL_INT (-1, val);
71 72 73

    val = 16384;

74 75 76
    TEST_ASSERT_SUCCESS_ERRNO (
      zmq_setsockopt (socket, ZMQ_SNDBUF, &val, sizeof (val)));
    TEST_ASSERT_EQUAL_INT (16384, val);
77

78 79 80
    TEST_ASSERT_SUCCESS_ERRNO (
      zmq_getsockopt (socket, ZMQ_SNDBUF, &val, &placeholder));
    TEST_ASSERT_EQUAL_INT (16384, val);
81

82
    test_context_socket_close (socket);
83 84
}

85
void 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 93 94
    TEST_ASSERT_SUCCESS_ERRNO (
      zmq_getsockopt (socket, ZMQ_USE_FD, &val, &placeholder));
    TEST_ASSERT_EQUAL_INT (-1, val);
95 96 97

    val = 3;

98 99 100
    TEST_ASSERT_SUCCESS_ERRNO (
      zmq_setsockopt (socket, ZMQ_USE_FD, &val, sizeof (val)));
    TEST_ASSERT_EQUAL_INT (3, val);
101

102 103 104
    TEST_ASSERT_SUCCESS_ERRNO (
      zmq_getsockopt (socket, ZMQ_USE_FD, &val, &placeholder));
    TEST_ASSERT_EQUAL_INT (3, val);
105

106
    test_context_socket_close (socket);
107 108
}

109 110 111
#define BOUNDDEVBUFSZ 16
void test_setsockopt_bindtodevice ()
{
112
    void *socket = test_context_socket (ZMQ_PUSH);
113

114
#ifdef ZMQ_BINDTODEVICE
115 116 117
    char devname[BOUNDDEVBUFSZ];
    size_t buflen = BOUNDDEVBUFSZ;

118 119 120 121
    TEST_ASSERT_SUCCESS_ERRNO (
      zmq_getsockopt (socket, ZMQ_BINDTODEVICE, devname, &buflen));
    TEST_ASSERT_EQUAL_INT8 ('\0', devname[0]);
    TEST_ASSERT_EQUAL_UINT (1, buflen);
122

123 124
    sprintf (devname, "testdev");
    buflen = strlen (devname);
125

126 127
    TEST_ASSERT_SUCCESS_ERRNO (
      zmq_setsockopt (socket, ZMQ_BINDTODEVICE, devname, buflen));
128 129

    buflen = BOUNDDEVBUFSZ;
130
    memset (devname, 0, buflen);
131

132 133 134
    TEST_ASSERT_SUCCESS_ERRNO (
      zmq_getsockopt (socket, ZMQ_BINDTODEVICE, devname, &buflen));
    TEST_ASSERT_EQUAL_STRING_LEN ("testdev", devname, buflen);
135
#endif
136

137
    test_context_socket_close (socket);
138 139
}

140
int main ()
141
{
142 143 144 145 146 147 148 149
    setup_test_environment ();

    UNITY_BEGIN ();
    RUN_TEST (test_setsockopt_tcp_recv_buffer);
    RUN_TEST (test_setsockopt_tcp_send_buffer);
    RUN_TEST (test_setsockopt_use_fd);
    RUN_TEST (test_setsockopt_bindtodevice);
    return UNITY_END ();
150
}