test_term_endpoint.cpp 6.81 KB
Newer Older
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 7 8
    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
9 10
    (at your option) any later version.

11 12 13 14 15 16 17 18 19 20 21 22 23 24
    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.
25 26 27 28

    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/>.
*/
29

30
#include <stdio.h>
31
#include "testutil.hpp"
32

33
/* Use the worst case filename size for the buffer (+1 for trailing NUL) */
34
#define BUF_SIZE (FILENAME_MAX + 1)
35

36
int main (void)
37
{
38
    setup_test_environment ();
39
    int rc;
40 41
    char buf[BUF_SIZE];
    size_t buf_size;
42
    const char *ep_wc_tcp = "tcp://127.0.0.1:*";
43
#if !defined ZMQ_HAVE_WINDOWS && !defined ZMQ_HAVE_OPENVMS
44
    const char *ep_wc_ipc = "ipc://*";
45
#endif
Ilya Kulakov's avatar
Ilya Kulakov committed
46 47 48
#if defined ZMQ_HAVE_VMCI
    const char *ep_wc_vmci = "vmci://*:*";
#endif
49 50

    //  Create infrastructure.
51
    void *ctx = zmq_ctx_new ();
52 53 54
    assert (ctx);
    void *push = zmq_socket (ctx, ZMQ_PUSH);
    assert (push);
55 56
    rc = zmq_bind (push, ep_wc_tcp);
    assert (rc == 0);
57
    buf_size = sizeof (buf);
58
    rc = zmq_getsockopt (push, ZMQ_LAST_ENDPOINT, buf, &buf_size);
59 60 61
    assert (rc == 0);
    void *pull = zmq_socket (ctx, ZMQ_PULL);
    assert (pull);
62
    rc = zmq_connect (pull, buf);
63 64
    assert (rc == 0);

65
    //  Pass one message through to ensure the connection is established
66 67 68 69 70
    rc = zmq_send (push, "ABC", 3, 0);
    assert (rc == 3);
    rc = zmq_recv (pull, buf, sizeof (buf), 0);
    assert (rc == 3);

71
    //  Unbind the listening endpoint
72
    buf_size = sizeof (buf);
73 74 75
    rc = zmq_getsockopt (push, ZMQ_LAST_ENDPOINT, buf, &buf_size);
    assert (rc == 0);
    rc = zmq_unbind (push, buf);
76 77
    assert (rc == 0);

78
    //  Allow unbind to settle
79
    msleep (SETTLE_TIME);
80

81
    //  Check that sending would block (there's no outbound connection)
82 83 84
    rc = zmq_send (push, "ABC", 3, ZMQ_DONTWAIT);
    assert (rc == -1 && zmq_errno () == EAGAIN);

85
    //  Clean up
86 87 88 89
    rc = zmq_close (pull);
    assert (rc == 0);
    rc = zmq_close (push);
    assert (rc == 0);
90
    rc = zmq_ctx_term (ctx);
91 92
    assert (rc == 0);

93
    //  Create infrastructure
94
    ctx = zmq_ctx_new ();
95 96 97
    assert (ctx);
    pull = zmq_socket (ctx, ZMQ_PULL);
    assert (pull);
98 99
    rc = zmq_bind (pull, ep_wc_tcp);
    assert (rc == 0);
100
    buf_size = sizeof (buf);
101 102 103 104 105
    rc = zmq_getsockopt (pull, ZMQ_LAST_ENDPOINT, buf, &buf_size);
    assert (rc == 0);
    push = zmq_socket (ctx, ZMQ_PUSH);
    assert (push);
    rc = zmq_connect (push, buf);
106 107 108 109 110 111 112 113
    assert (rc == 0);

    //  Pass one message through to ensure the connection is established.
    rc = zmq_send (push, "ABC", 3, 0);
    assert (rc == 3);
    rc = zmq_recv (pull, buf, sizeof (buf), 0);
    assert (rc == 3);

114
    //  Disconnect the bound endpoint
115
    buf_size = sizeof (buf);
116 117 118
    rc = zmq_getsockopt (pull, ZMQ_LAST_ENDPOINT, buf, &buf_size);
    assert (rc == 0);
    rc = zmq_disconnect (push, buf);
119 120
    assert (rc == 0);

121
    //  Allow disconnect to settle
122
    msleep (SETTLE_TIME);
123 124 125 126 127 128 129 130 131 132

    //  Check that sending would block (there's no inbound connections).
    rc = zmq_send (push, "ABC", 3, ZMQ_DONTWAIT);
    assert (rc == -1 && zmq_errno () == EAGAIN);

    //  Clean up.
    rc = zmq_close (pull);
    assert (rc == 0);
    rc = zmq_close (push);
    assert (rc == 0);
133
    rc = zmq_ctx_term (ctx);
134 135
    assert (rc == 0);

136 137 138 139 140 141 142
    //  Create infrastructure (wild-card binding)
    ctx = zmq_ctx_new ();
    assert (ctx);
    push = zmq_socket (ctx, ZMQ_PUSH);
    assert (push);
    rc = zmq_bind (push, ep_wc_tcp);
    assert (rc == 0);
143 144
    pull = zmq_socket (ctx, ZMQ_PULL);
    assert (pull);
145
#if !defined ZMQ_HAVE_WINDOWS && !defined ZMQ_HAVE_OPENVMS
146 147
    rc = zmq_bind (pull, ep_wc_ipc);
    assert (rc == 0);
148
#endif
Ilya Kulakov's avatar
Ilya Kulakov committed
149 150 151 152 153 154
#if defined ZMQ_HAVE_VMCI
    void *req = zmq_socket (ctx, ZMQ_REQ);
    assert (req);
    rc = zmq_bind (req, ep_wc_vmci);
    assert (rc == 0);
#endif
155 156

    // Unbind sockets binded by wild-card address
157
    buf_size = sizeof (buf);
158
    rc = zmq_getsockopt (push, ZMQ_LAST_ENDPOINT, buf, &buf_size);
159 160 161
    assert (rc == 0);
    rc = zmq_unbind (push, buf);
    assert (rc == 0);
162
#if !defined ZMQ_HAVE_WINDOWS && !defined ZMQ_HAVE_OPENVMS
163
    buf_size = sizeof (buf);
164
    rc = zmq_getsockopt (pull, ZMQ_LAST_ENDPOINT, buf, &buf_size);
165 166 167
    assert (rc == 0);
    rc = zmq_unbind (pull, buf);
    assert (rc == 0);
168
#endif
Ilya Kulakov's avatar
Ilya Kulakov committed
169
#if defined ZMQ_HAVE_VMCI
170
    buf_size = sizeof (buf);
171
    rc = zmq_getsockopt (req, ZMQ_LAST_ENDPOINT, buf, &buf_size);
Ilya Kulakov's avatar
Ilya Kulakov committed
172
    assert (rc == 0);
173
    rc = zmq_unbind (req, buf);
Ilya Kulakov's avatar
Ilya Kulakov committed
174 175
    assert (rc == 0);
#endif
176

177 178 179 180 181 182 183 184
    //  Clean up.
    rc = zmq_close (pull);
    assert (rc == 0);
    rc = zmq_close (push);
    assert (rc == 0);
    rc = zmq_ctx_term (ctx);
    assert (rc == 0);

185 186 187 188 189 190 191
    //  Create infrastructure (wild-card binding)
    ctx = zmq_ctx_new ();
    assert (ctx);
    push = zmq_socket (ctx, ZMQ_PUSH);
    assert (push);
    rc = zmq_bind (push, ep_wc_tcp);
    assert (rc == 0);
192 193 194
    pull = zmq_socket (ctx, ZMQ_PULL);
    assert (pull);
#if !defined ZMQ_HAVE_WINDOWS && !defined ZMQ_HAVE_OPENVMS
195 196
    rc = zmq_bind (pull, ep_wc_ipc);
    assert (rc == 0);
197
#endif
Ilya Kulakov's avatar
Ilya Kulakov committed
198 199 200 201 202 203
#if defined ZMQ_HAVE_VMCI
    req = zmq_socket (ctx, ZMQ_REQ);
    assert (req);
    rc = zmq_bind (req, ep_wc_vmci);
    assert (rc == 0);
#endif
204 205 206 207

    // Sockets binded by wild-card address can't be unbinded by wild-card address
    rc = zmq_unbind (push, ep_wc_tcp);
    assert (rc == -1 && zmq_errno () == ENOENT);
208
#if !defined ZMQ_HAVE_WINDOWS && !defined ZMQ_HAVE_OPENVMS
209 210
    rc = zmq_unbind (pull, ep_wc_ipc);
    assert (rc == -1 && zmq_errno () == ENOENT);
211
#endif
Ilya Kulakov's avatar
Ilya Kulakov committed
212 213 214 215
#if defined ZMQ_HAVE_VMCI
    rc = zmq_unbind (req, ep_wc_vmci);
    assert (rc == -1 && zmq_errno () == ENOENT);
#endif
216

217 218 219 220 221 222 223 224
    //  Clean up.
    rc = zmq_close (pull);
    assert (rc == 0);
    rc = zmq_close (push);
    assert (rc == 0);
    rc = zmq_ctx_term (ctx);
    assert (rc == 0);

225 226
    return 0;
}