test_setsockopt.cpp 3.75 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 31
#include "testutil.hpp"

32
void test_setsockopt_tcp_recv_buffer (void)
33 34
{
    int rc;
35 36
    void *ctx = zmq_ctx_new ();
    void *socket = zmq_socket (ctx, ZMQ_PUSH);
37 38

    int val = 0;
39
    size_t placeholder = sizeof (val);
40

41 42 43
    rc = zmq_getsockopt (socket, ZMQ_RCVBUF, &val, &placeholder);
    assert (rc == 0);
    assert (val == 8192);
44

45 46 47
    rc = zmq_setsockopt (socket, ZMQ_RCVBUF, &val, sizeof (val));
    assert (rc == 0);
    assert (val == 8192);
48

49 50 51
    rc = zmq_getsockopt (socket, ZMQ_RCVBUF, &val, &placeholder);
    assert (rc == 0);
    assert (val == 8192);
52 53 54

    val = 16384;

55 56 57
    rc = zmq_setsockopt (socket, ZMQ_RCVBUF, &val, sizeof (val));
    assert (rc == 0);
    assert (val == 16384);
58

59 60 61
    rc = zmq_getsockopt (socket, ZMQ_RCVBUF, &val, &placeholder);
    assert (rc == 0);
    assert (val == 16384);
62

63 64
    zmq_close (socket);
    zmq_ctx_term (ctx);
65 66
}

67
void test_setsockopt_tcp_send_buffer (void)
68 69
{
    int rc;
70 71
    void *ctx = zmq_ctx_new ();
    void *socket = zmq_socket (ctx, ZMQ_PUSH);
72 73

    int val = 0;
74
    size_t placeholder = sizeof (val);
75

76 77 78
    rc = zmq_getsockopt (socket, ZMQ_SNDBUF, &val, &placeholder);
    assert (rc == 0);
    assert (val == 8192);
79

80 81 82
    rc = zmq_setsockopt (socket, ZMQ_SNDBUF, &val, sizeof (val));
    assert (rc == 0);
    assert (val == 8192);
83

84 85 86
    rc = zmq_getsockopt (socket, ZMQ_SNDBUF, &val, &placeholder);
    assert (rc == 0);
    assert (val == 8192);
87 88 89

    val = 16384;

90 91 92
    rc = zmq_setsockopt (socket, ZMQ_SNDBUF, &val, sizeof (val));
    assert (rc == 0);
    assert (val == 16384);
93

94 95 96
    rc = zmq_getsockopt (socket, ZMQ_SNDBUF, &val, &placeholder);
    assert (rc == 0);
    assert (val == 16384);
97

98 99
    zmq_close (socket);
    zmq_ctx_term (ctx);
100 101
}

102
void test_setsockopt_use_fd ()
103 104
{
    int rc;
105 106
    void *ctx = zmq_ctx_new ();
    void *socket = zmq_socket (ctx, ZMQ_PUSH);
107 108

    int val = 0;
109
    size_t placeholder = sizeof (val);
110

111
    rc = zmq_getsockopt (socket, ZMQ_USE_FD, &val, &placeholder);
112 113 114 115 116
    assert(rc == 0);
    assert(val == -1);

    val = 3;

117
    rc = zmq_setsockopt (socket, ZMQ_USE_FD, &val, sizeof(val));
118 119 120
    assert(rc == 0);
    assert(val == 3);

121
    rc = zmq_getsockopt (socket, ZMQ_USE_FD, &val, &placeholder);
122 123 124
    assert(rc == 0);
    assert(val == 3);

125 126
    zmq_close (socket);
    zmq_ctx_term (ctx);
127 128
}

129
int main (void)
130
{
131 132 133
    test_setsockopt_tcp_recv_buffer ();
    test_setsockopt_tcp_send_buffer ();
    test_setsockopt_use_fd ();
134
}