test_unbind_wildcard.cpp 5.65 KB
Newer Older
Dylan Cali's avatar
Dylan Cali committed
1
/*
2
    Copyright (c) 2007-2017 Contributors as noted in the AUTHORS file
Dylan Cali's avatar
Dylan Cali committed
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23

    This file is part of 0MQ.

    0MQ is free software; you can redistribute it and/or modify it under
    the terms of the GNU Lesser General Public License as published by
    the Free Software Foundation; either version 3 of the License, or
    (at your option) any later version.

    0MQ 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/>.
*/

#include "testutil.hpp"

int main (void)
{
24
    setup_test_environment ();
Dylan Cali's avatar
Dylan Cali committed
25 26
    void *ctx = zmq_ctx_new ();
    assert (ctx);
27
    int ipv6 = is_ipv6_available ();
Dylan Cali's avatar
Dylan Cali committed
28

29
    /* Address wildcard, IPv6 disabled */
Dylan Cali's avatar
Dylan Cali committed
30 31
    void *sb = zmq_socket (ctx, ZMQ_REP);
    assert (sb);
32 33 34
    void *sc = zmq_socket (ctx, ZMQ_REQ);
    assert (sc);

35
    int rc = zmq_bind (sb, "tcp://*:*");
Dylan Cali's avatar
Dylan Cali committed
36 37
    assert (rc == 0);

38
    char bindEndpoint[256];
39
    char connectEndpoint[256];
40
    size_t endpoint_len = sizeof (bindEndpoint);
41
    rc = zmq_getsockopt (sb, ZMQ_LAST_ENDPOINT, bindEndpoint, &endpoint_len);
Dylan Cali's avatar
Dylan Cali committed
42 43
    assert (rc == 0);

44
    //  Apparently Windows can't connect to 0.0.0.0. A better fix would be welcome.
45
#ifdef ZMQ_HAVE_WINDOWS
46
    sprintf (connectEndpoint, "tcp://127.0.0.1:%s",
47
             strrchr (bindEndpoint, ':') + 1);
48
#else
49
    strcpy (connectEndpoint, bindEndpoint);
50 51 52
#endif

    rc = zmq_connect (sc, connectEndpoint);
53 54 55 56
    assert (rc == 0);

    bounce (sb, sc);

57
    rc = zmq_disconnect (sc, connectEndpoint);
58
    assert (rc == 0);
59
    rc = zmq_unbind (sb, bindEndpoint);
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
    assert (rc == 0);

    rc = zmq_close (sc);
    assert (rc == 0);
    rc = zmq_close (sb);
    assert (rc == 0);

    /* Address wildcard, IPv6 enabled */
    sb = zmq_socket (ctx, ZMQ_REP);
    assert (sb);
    sc = zmq_socket (ctx, ZMQ_REQ);
    assert (sc);

    rc = zmq_setsockopt (sb, ZMQ_IPV6, &ipv6, sizeof (int));
    assert (rc == 0);
    rc = zmq_setsockopt (sc, ZMQ_IPV6, &ipv6, sizeof (int));
    assert (rc == 0);

78
    rc = zmq_bind (sb, "tcp://*:*");
79
    assert (rc == 0);
80

81
    endpoint_len = sizeof (bindEndpoint);
82
    memset (bindEndpoint, 0, endpoint_len);
83
    rc = zmq_getsockopt (sb, ZMQ_LAST_ENDPOINT, bindEndpoint, &endpoint_len);
84
    assert (rc == 0);
85

86 87
#ifdef ZMQ_HAVE_WINDOWS
    if (ipv6)
88
        sprintf (connectEndpoint, "tcp://[::1]:%s",
89
                 strrchr (bindEndpoint, ':') + 1);
90 91
    else
        sprintf (connectEndpoint, "tcp://127.0.0.1:%s",
92
                 strrchr (bindEndpoint, ':') + 1);
93
#else
94
    strcpy (connectEndpoint, bindEndpoint);
95
#endif
96

97
    rc = zmq_connect (sc, connectEndpoint);
98 99 100 101
    assert (rc == 0);

    bounce (sb, sc);

102
    rc = zmq_disconnect (sc, connectEndpoint);
103
    assert (rc == 0);
104
    rc = zmq_unbind (sb, bindEndpoint);
Dylan Cali's avatar
Dylan Cali committed
105 106
    assert (rc == 0);

107 108
    rc = zmq_close (sc);
    assert (rc == 0);
109 110 111
    rc = zmq_close (sb);
    assert (rc == 0);

112 113 114 115 116 117 118 119 120
    /* Port wildcard, IPv4 address, IPv6 disabled */
    sb = zmq_socket (ctx, ZMQ_REP);
    assert (sb);
    sc = zmq_socket (ctx, ZMQ_REQ);
    assert (sc);

    rc = zmq_bind (sb, "tcp://127.0.0.1:*");
    assert (rc == 0);

121
    char endpoint[256];
122
    endpoint_len = sizeof (endpoint);
123
    memset (endpoint, 0, endpoint_len);
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
    rc = zmq_getsockopt (sb, ZMQ_LAST_ENDPOINT, endpoint, &endpoint_len);
    assert (rc == 0);

    rc = zmq_connect (sc, endpoint);
    assert (rc == 0);

    bounce (sb, sc);

    rc = zmq_disconnect (sc, endpoint);
    assert (rc == 0);
    rc = zmq_unbind (sb, endpoint);
    assert (rc == 0);

    rc = zmq_close (sc);
    assert (rc == 0);
    rc = zmq_close (sb);
    assert (rc == 0);

    /* Port wildcard, IPv4 address, IPv6 enabled */
    sb = zmq_socket (ctx, ZMQ_REP);
    assert (sb);
    sc = zmq_socket (ctx, ZMQ_REQ);
    assert (sc);

    rc = zmq_setsockopt (sb, ZMQ_IPV6, &ipv6, sizeof (int));
    assert (rc == 0);
    rc = zmq_setsockopt (sc, ZMQ_IPV6, &ipv6, sizeof (int));
    assert (rc == 0);

    rc = zmq_bind (sb, "tcp://127.0.0.1:*");
    assert (rc == 0);

    endpoint_len = sizeof (endpoint);
157
    memset (endpoint, 0, endpoint_len);
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
    rc = zmq_getsockopt (sb, ZMQ_LAST_ENDPOINT, endpoint, &endpoint_len);
    assert (rc == 0);

    rc = zmq_connect (sc, endpoint);
    assert (rc == 0);

    bounce (sb, sc);

    rc = zmq_disconnect (sc, endpoint);
    assert (rc == 0);
    rc = zmq_unbind (sb, endpoint);
    assert (rc == 0);

    rc = zmq_close (sc);
    assert (rc == 0);
    rc = zmq_close (sb);
    assert (rc == 0);

    if (ipv6) {
        /* Port wildcard, IPv6 address, IPv6 enabled */
        sb = zmq_socket (ctx, ZMQ_REP);
        assert (sb);
        sc = zmq_socket (ctx, ZMQ_REQ);
        assert (sc);

        rc = zmq_setsockopt (sb, ZMQ_IPV6, &ipv6, sizeof (int));
        assert (rc == 0);
        rc = zmq_setsockopt (sc, ZMQ_IPV6, &ipv6, sizeof (int));
        assert (rc == 0);

        rc = zmq_bind (sb, "tcp://[::1]:*");
        assert (rc == 0);

        endpoint_len = sizeof (endpoint);
192
        memset (endpoint, 0, endpoint_len);
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
        rc = zmq_getsockopt (sb, ZMQ_LAST_ENDPOINT, endpoint, &endpoint_len);
        assert (rc == 0);

        rc = zmq_connect (sc, endpoint);
        assert (rc == 0);

        bounce (sb, sc);

        rc = zmq_disconnect (sc, endpoint);
        assert (rc == 0);
        rc = zmq_unbind (sb, endpoint);
        assert (rc == 0);

        rc = zmq_close (sc);
        assert (rc == 0);
        rc = zmq_close (sb);
        assert (rc == 0);
    }

Dylan Cali's avatar
Dylan Cali committed
212 213
    rc = zmq_ctx_term (ctx);
    assert (rc == 0);
Dylan Cali's avatar
Dylan Cali committed
214 215 216

    return 0;
}