test_term_endpoint.cpp 6.24 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 "testutil.hpp"
31
#include "testutil_unity.hpp"
32

33 34 35 36
void setUp ()
{
    setup_test_context ();
}
37

38
void tearDown ()
39
{
40 41 42 43 44 45 46 47
    teardown_test_context ();
}

/* Use the worst case filename size for the buffer (+1 for trailing NUL), this
 * is larger than MAX_SOCKET_STRING, which is not large enough for IPC */
#define BUF_SIZE (FILENAME_MAX + 1)

const char *ep_wc_tcp = "tcp://127.0.0.1:*";
48
#if !defined ZMQ_HAVE_WINDOWS && !defined ZMQ_HAVE_OPENVMS
49
const char *ep_wc_ipc = "ipc://*";
50
#endif
Ilya Kulakov's avatar
Ilya Kulakov committed
51
#if defined ZMQ_HAVE_VMCI
52
const char *ep_wc_vmci = "vmci://*:*";
Ilya Kulakov's avatar
Ilya Kulakov committed
53
#endif
54

55 56 57 58
void test_send_after_unbind_fails ()
{
    char my_endpoint[BUF_SIZE];

59
    //  Create infrastructure.
60 61 62 63 64
    void *push = test_context_socket (ZMQ_PUSH);
    bind_loopback_ipv4 (push, my_endpoint, BUF_SIZE);

    void *pull = test_context_socket (ZMQ_PULL);
    TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (pull, my_endpoint));
65

66
    //  Pass one message through to ensure the connection is established
67 68
    send_string_expect_success (push, "ABC", 0);
    recv_string_expect_success (pull, "ABC", 0);
69

70
    //  Unbind the listening endpoint
71
    TEST_ASSERT_SUCCESS_ERRNO (zmq_unbind (push, my_endpoint));
72

73
    //  Allow unbind to settle
74
    msleep (SETTLE_TIME);
75

76
    //  Check that sending would block (there's no outbound connection)
77
    TEST_ASSERT_FAILURE_ERRNO (EAGAIN, zmq_send (push, "ABC", 3, ZMQ_DONTWAIT));
78

79
    //  Clean up
80 81 82
    test_context_socket_close (pull);
    test_context_socket_close (push);
}
83

84 85
void test_send_after_disconnect_fails ()
{
86
    //  Create infrastructure
87 88 89 90 91 92
    void *pull = test_context_socket (ZMQ_PULL);
    char my_endpoint[BUF_SIZE];
    bind_loopback_ipv4 (pull, my_endpoint, BUF_SIZE);

    void *push = test_context_socket (ZMQ_PUSH);
    TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (push, my_endpoint));
93 94

    //  Pass one message through to ensure the connection is established.
95 96
    send_string_expect_success (push, "ABC", 0);
    recv_string_expect_success (pull, "ABC", 0);
97

98
    //  Disconnect the bound endpoint
99
    TEST_ASSERT_SUCCESS_ERRNO (zmq_disconnect (push, my_endpoint));
100

101
    //  Allow disconnect to settle
102
    msleep (SETTLE_TIME);
103 104

    //  Check that sending would block (there's no inbound connections).
105
    TEST_ASSERT_FAILURE_ERRNO (EAGAIN, zmq_send (push, "ABC", 3, ZMQ_DONTWAIT));
106

107 108 109 110
    //  Clean up
    test_context_socket_close (pull);
    test_context_socket_close (push);
}
111

112 113
void test_unbind_via_last_endpoint ()
{
114
    //  Create infrastructure (wild-card binding)
115 116 117 118 119 120
    void *push = test_context_socket (ZMQ_PUSH);
    char my_endpoint[BUF_SIZE];
    bind_loopback_ipv4 (push, my_endpoint, BUF_SIZE);

    void *pull = test_context_socket (ZMQ_PULL);

121
#if !defined ZMQ_HAVE_WINDOWS && !defined ZMQ_HAVE_OPENVMS
122
    TEST_ASSERT_SUCCESS_ERRNO (zmq_bind (pull, ep_wc_ipc));
123
#endif
Ilya Kulakov's avatar
Ilya Kulakov committed
124
#if defined ZMQ_HAVE_VMCI
125 126
    void *req = test_context_socket (ZMQ_REQ);
    TEST_ASSERT_SUCCESS_ERRNO (zmq_bind (req, ep_wc_vmci));
Ilya Kulakov's avatar
Ilya Kulakov committed
127
#endif
128 129

    // Unbind sockets binded by wild-card address
130 131 132 133 134
    TEST_ASSERT_SUCCESS_ERRNO (zmq_unbind (push, my_endpoint));

    size_t buf_size = 0;
    (void) buf_size;

135
#if !defined ZMQ_HAVE_WINDOWS && !defined ZMQ_HAVE_OPENVMS
136 137 138 139
    buf_size = sizeof (my_endpoint);
    TEST_ASSERT_SUCCESS_ERRNO (
      zmq_getsockopt (pull, ZMQ_LAST_ENDPOINT, my_endpoint, &buf_size));
    TEST_ASSERT_SUCCESS_ERRNO (zmq_unbind (pull, my_endpoint));
140
#endif
Ilya Kulakov's avatar
Ilya Kulakov committed
141
#if defined ZMQ_HAVE_VMCI
142 143 144 145
    buf_size = sizeof (my_endpoint);
    TEST_ASSERT_SUCCESS_ERRNO (
      zmq_getsockopt (req, ZMQ_LAST_ENDPOINT, my_endpoint, &buf_size));
    TEST_ASSERT_SUCCESS_ERRNO (zmq_unbind (req, my_endpoint));
Ilya Kulakov's avatar
Ilya Kulakov committed
146
#endif
147

148 149 150 151
    //  Clean up
    test_context_socket_close (pull);
    test_context_socket_close (push);
}
152

153 154
void test_wildcard_unbind_fails ()
{
155
    //  Create infrastructure (wild-card binding)
156 157 158
    void *push = test_context_socket (ZMQ_PUSH);
    TEST_ASSERT_SUCCESS_ERRNO (zmq_bind (push, ep_wc_tcp));
    void *pull = test_context_socket (ZMQ_PULL);
159
#if !defined ZMQ_HAVE_WINDOWS && !defined ZMQ_HAVE_OPENVMS
160
    TEST_ASSERT_SUCCESS_ERRNO (zmq_bind (pull, ep_wc_ipc));
161
#endif
Ilya Kulakov's avatar
Ilya Kulakov committed
162
#if defined ZMQ_HAVE_VMCI
163 164
    void *req = test_context_socket (ZMQ_REQ);
    TEST_ASSERT_SUCCESS_ERRNO (zmq_bind (req, ep_wc_vmci));
Ilya Kulakov's avatar
Ilya Kulakov committed
165
#endif
166 167

    // Sockets binded by wild-card address can't be unbinded by wild-card address
168
    TEST_ASSERT_FAILURE_ERRNO (ENOENT, zmq_unbind (push, ep_wc_tcp));
169
#if !defined ZMQ_HAVE_WINDOWS && !defined ZMQ_HAVE_OPENVMS
170
    TEST_ASSERT_FAILURE_ERRNO (ENOENT, zmq_unbind (pull, ep_wc_ipc));
171
#endif
Ilya Kulakov's avatar
Ilya Kulakov committed
172
#if defined ZMQ_HAVE_VMCI
173
    TEST_ASSERT_FAILURE_ERRNO (ENOENT, zmq_unbind (req, ep_wc_vmci));
Ilya Kulakov's avatar
Ilya Kulakov committed
174
#endif
175

176 177 178 179 180 181 182 183
    //  Clean up
    test_context_socket_close (pull);
    test_context_socket_close (push);
}

int main ()
{
    setup_test_environment ();
184

185 186 187 188 189 190
    UNITY_BEGIN ();
    RUN_TEST (test_send_after_unbind_fails);
    RUN_TEST (test_send_after_disconnect_fails);
    RUN_TEST (test_unbind_via_last_endpoint);
    RUN_TEST (test_wildcard_unbind_fails);
    return UNITY_END ();
191
}