File: test_term_endpoint.cpp

package info (click to toggle)
zeromq3 4.3.5-1
  • links: PTS
  • area: main
  • in suites: forky, sid, trixie
  • size: 6,548 kB
  • sloc: cpp: 56,475; ansic: 4,968; makefile: 1,607; sh: 1,400; xml: 196; python: 40
file content (162 lines) | stat: -rw-r--r-- 5,161 bytes parent folder | download
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/* SPDX-License-Identifier: MPL-2.0 */

#include "testutil.hpp"
#include "testutil_unity.hpp"

SETUP_TEARDOWN_TESTCONTEXT

/* 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:*";
#if !defined ZMQ_HAVE_WINDOWS && !defined ZMQ_HAVE_OPENVMS
const char *ep_wc_ipc = "ipc://*";
#endif
#if defined ZMQ_HAVE_VMCI
const char *ep_wc_vmci = "vmci://*:*";
#endif

void test_send_after_unbind_fails ()
{
    char my_endpoint[BUF_SIZE];

    //  Create infrastructure.
    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));

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

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

    //  Allow unbind to settle
    msleep (SETTLE_TIME);

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

    //  Clean up
    test_context_socket_close (pull);
    test_context_socket_close (push);
}

void test_send_after_disconnect_fails ()
{
    //  Create infrastructure
    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));

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

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

    //  Allow disconnect to settle
    msleep (SETTLE_TIME);

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

    //  Clean up
    test_context_socket_close (pull);
    test_context_socket_close (push);
}

void test_unbind_via_last_endpoint ()
{
    //  Create infrastructure (wild-card binding)
    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);

#if !defined ZMQ_HAVE_WINDOWS && !defined ZMQ_HAVE_OPENVMS
    TEST_ASSERT_SUCCESS_ERRNO (zmq_bind (pull, ep_wc_ipc));
#endif
#if defined ZMQ_HAVE_VMCI
    void *req = test_context_socket (ZMQ_REQ);
    int rc = zmq_bind (req, ep_wc_vmci);
    if (rc < 0 && errno == EAFNOSUPPORT)
        TEST_IGNORE_MESSAGE ("VMCI not supported");
    TEST_ASSERT_SUCCESS_ERRNO (rc);
#endif

    // Unbind sockets binded by wild-card address
    TEST_ASSERT_SUCCESS_ERRNO (zmq_unbind (push, my_endpoint));

    size_t buf_size = 0;
    (void) buf_size;

#if !defined ZMQ_HAVE_WINDOWS && !defined ZMQ_HAVE_OPENVMS
    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));
#endif
#if defined ZMQ_HAVE_VMCI
    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));
#endif

    //  Clean up
    test_context_socket_close (pull);
    test_context_socket_close (push);
}

void test_wildcard_unbind_fails ()
{
    //  Create infrastructure (wild-card binding)
    void *push = test_context_socket (ZMQ_PUSH);
    TEST_ASSERT_SUCCESS_ERRNO (zmq_bind (push, ep_wc_tcp));
    void *pull = test_context_socket (ZMQ_PULL);
#if !defined ZMQ_HAVE_WINDOWS && !defined ZMQ_HAVE_OPENVMS
    TEST_ASSERT_SUCCESS_ERRNO (zmq_bind (pull, ep_wc_ipc));
#endif
#if defined ZMQ_HAVE_VMCI
    void *req = test_context_socket (ZMQ_REQ);
    int rc = zmq_bind (req, ep_wc_vmci);
    if (rc < 0 && errno == EAFNOSUPPORT)
        TEST_IGNORE_MESSAGE ("VMCI not supported");
    TEST_ASSERT_SUCCESS_ERRNO (rc);
#endif

    // Sockets binded by wild-card address can't be unbinded by wild-card address
    TEST_ASSERT_FAILURE_ERRNO (ENOENT, zmq_unbind (push, ep_wc_tcp));
#if !defined ZMQ_HAVE_WINDOWS && !defined ZMQ_HAVE_OPENVMS
    TEST_ASSERT_FAILURE_ERRNO (ENOENT, zmq_unbind (pull, ep_wc_ipc));
#endif
#if defined ZMQ_HAVE_VMCI
    TEST_ASSERT_FAILURE_ERRNO (ENOENT, zmq_unbind (req, ep_wc_vmci));
#endif

    //  Clean up
    test_context_socket_close (pull);
    test_context_socket_close (push);
}

int main ()
{
    setup_test_environment ();

    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 ();
}