Merge pull request #3743 from bluca/test_ws_bindport

Fix ZMQ_LAST_ENDPOINT with WS[S] and use it in tests
This commit is contained in:
Doron Somech 2019-11-28 17:24:35 +02:00 committed by GitHub
commit 85df75584e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 178 additions and 22 deletions

View File

@ -103,12 +103,14 @@ option(WITH_NSS "Use NSS instead of builtin sha1" OFF)
if (NOT DISABLE_WS)
list(APPEND sources
${CMAKE_CURRENT_SOURCE_DIR}/src/ws_address.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/wss_address.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/ws_connecter.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/ws_decoder.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/ws_encoder.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/ws_engine.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/ws_listener.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/ws_address.hpp
${CMAKE_CURRENT_SOURCE_DIR}/src/wss_address.hpp
${CMAKE_CURRENT_SOURCE_DIR}/src/ws_connecter.hpp
${CMAKE_CURRENT_SOURCE_DIR}/src/ws_decoder.hpp
${CMAKE_CURRENT_SOURCE_DIR}/src/ws_encoder.hpp

View File

@ -286,6 +286,8 @@ if HAVE_WS
src_libzmq_la_SOURCES += \
src/ws_address.cpp \
src/ws_address.hpp \
src/wss_address.cpp \
src/wss_address.hpp \
src/ws_connecter.cpp \
src/ws_connecter.hpp \
src/ws_decoder.cpp \

View File

@ -46,6 +46,7 @@ class ctx_t;
class tcp_address_t;
class udp_address_t;
class ws_address_t;
class wss_address_t;
#if defined ZMQ_HAVE_IPC
class ipc_address_t;
#endif
@ -99,6 +100,7 @@ struct address_t
udp_address_t *udp_addr;
#ifdef ZMQ_HAVE_WS
ws_address_t *ws_addr;
wss_address_t *wss_addr;
#endif
#if defined ZMQ_HAVE_IPC
ipc_address_t *ipc_addr;

View File

@ -55,6 +55,7 @@
#include "tipc_listener.hpp"
#include "tcp_connecter.hpp"
#include "ws_address.hpp"
#include "wss_address.hpp"
#include "io_thread.hpp"
#include "session_base.hpp"
#include "config.hpp"
@ -904,13 +905,22 @@ int zmq::socket_base_t::connect (const char *endpoint_uri_)
#ifdef ZMQ_HAVE_WS
#ifdef ZMQ_HAVE_WSS
else if (protocol == protocol_name::ws || protocol == protocol_name::wss) {
if (protocol == protocol_name::wss) {
paddr->resolved.wss_addr = new (std::nothrow) wss_address_t ();
alloc_assert (paddr->resolved.wss_addr);
rc = paddr->resolved.wss_addr->resolve (address.c_str (), false,
options.ipv6);
} else
#else
else if (protocol == protocol_name::ws) {
#endif
paddr->resolved.ws_addr = new (std::nothrow) ws_address_t ();
alloc_assert (paddr->resolved.ws_addr);
rc = paddr->resolved.ws_addr->resolve (address.c_str (), false,
options.ipv6);
{
paddr->resolved.ws_addr = new (std::nothrow) ws_address_t ();
alloc_assert (paddr->resolved.ws_addr);
rc = paddr->resolved.ws_addr->resolve (address.c_str (), false,
options.ipv6);
}
if (rc != 0) {
LIBZMQ_DELETE (paddr);
return -1;

View File

@ -105,6 +105,8 @@ int zmq::ws_address_t::resolve (const char *name_, bool local_, bool ipv6_)
_path = std::string (delim);
else
_path = std::string ("/");
// remove the path, otherwise resolving the port will fail with wildcard
std::string host_port = std::string (name_, delim - name_);
ip_resolver_options_t resolver_opts;
resolver_opts.bindable (local_)
@ -116,7 +118,7 @@ int zmq::ws_address_t::resolve (const char *name_, bool local_, bool ipv6_)
ip_resolver_t resolver (resolver_opts);
return resolver.resolve (&_address, name_);
return resolver.resolve (&_address, host_port.c_str ());
}
int zmq::ws_address_t::to_string (std::string &addr_) const

View File

@ -65,8 +65,10 @@ class ws_address_t
const char *host () const;
const char *path () const;
private:
protected:
ip_addr_t _address;
private:
std::string _host;
std::string _path;
};

View File

@ -39,6 +39,7 @@
#include "tcp.hpp"
#include "address.hpp"
#include "ws_address.hpp"
#include "wss_address.hpp"
#include "session_base.hpp"
#include "ws_engine.hpp"
@ -118,7 +119,12 @@ void zmq::ws_connecter_t::out_event ()
return;
}
create_engine (fd, get_socket_name<ws_address_t> (fd, socket_end_local));
if (_wss)
create_engine (fd,
get_socket_name<wss_address_t> (fd, socket_end_local));
else
create_engine (fd,
get_socket_name<ws_address_t> (fd, socket_end_local));
}
void zmq::ws_connecter_t::timer_event (int id_)

View File

@ -42,6 +42,7 @@
#include "socket_base.hpp"
#include "address.hpp"
#include "ws_engine.hpp"
#include "wss_address.hpp"
#include "session_base.hpp"
#ifdef ZMQ_HAVE_WSS
@ -123,7 +124,10 @@ void zmq::ws_listener_t::in_event ()
std::string zmq::ws_listener_t::get_socket_name (zmq::fd_t fd_,
socket_end_t socket_end_) const
{
return zmq::get_socket_name<tcp_address_t> (fd_, socket_end_);
if (_wss)
return zmq::get_socket_name<wss_address_t> (fd_, socket_end_);
else
return zmq::get_socket_name<ws_address_t> (fd_, socket_end_);
}
int zmq::ws_listener_t::create_socket (const char *addr_)
@ -206,7 +210,10 @@ int zmq::ws_listener_t::set_local_address (const char *addr_)
if (rc != 0)
return -1;
if (create_socket (addr_) == -1)
// remove the path, otherwise resolving the port will fail with wildcard
const char *delim = strrchr (addr_, '/');
std::string host_port = std::string (addr_, delim - addr_);
if (create_socket (host_port.c_str ()) == -1)
return -1;
}

53
src/wss_address.cpp Normal file
View File

@ -0,0 +1,53 @@
/*
Copyright (c) 2019 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/>.
*/
#include "precompiled.hpp"
#include <string>
#include <sstream>
#include "wss_address.hpp"
zmq::wss_address_t::wss_address_t () : ws_address_t ()
{
}
zmq::wss_address_t::wss_address_t (const sockaddr *sa_, socklen_t sa_len_) :
ws_address_t (sa_, sa_len_)
{
}
int zmq::wss_address_t::to_string (std::string &addr_) const
{
std::ostringstream os;
os << std::string ("wss://") << host () << std::string (":")
<< _address.port ();
addr_ = os.str ();
return 0;
}

47
src/wss_address.hpp Normal file
View File

@ -0,0 +1,47 @@
/*
Copyright (c) 2019 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/>.
*/
#ifndef __ZMQ_WSS_ADDRESS_HPP_INCLUDED__
#define __ZMQ_WSS_ADDRESS_HPP_INCLUDED__
#include "ws_address.hpp"
namespace zmq
{
class wss_address_t : public ws_address_t
{
public:
wss_address_t ();
wss_address_t (const sockaddr *sa_, socklen_t sa_len_);
// The opposite to resolve()
int to_string (std::string &addr_) const;
};
}
#endif

View File

@ -27,6 +27,7 @@
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <string.h>
#include "testutil.hpp"
#include "testutil_unity.hpp"
@ -34,12 +35,16 @@ SETUP_TEARDOWN_TESTCONTEXT
void test_roundtrip ()
{
char connect_address[MAX_SOCKET_STRING + strlen ("/roundtrip")];
size_t addr_length = sizeof (connect_address);
void *sb = test_context_socket (ZMQ_REP);
TEST_ASSERT_SUCCESS_ERRNO (zmq_bind (sb, "ws://*:5556/roundtrip"));
TEST_ASSERT_SUCCESS_ERRNO (zmq_bind (sb, "ws://*:*/roundtrip"));
TEST_ASSERT_SUCCESS_ERRNO (
zmq_getsockopt (sb, ZMQ_LAST_ENDPOINT, connect_address, &addr_length));
strcat (connect_address, "/roundtrip");
void *sc = test_context_socket (ZMQ_REQ);
TEST_ASSERT_SUCCESS_ERRNO (
zmq_connect (sc, "ws://127.0.0.1:5556/roundtrip"));
TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (sc, connect_address));
bounce (sb, sc);
@ -49,11 +54,16 @@ void test_roundtrip ()
void test_short_message ()
{
char connect_address[MAX_SOCKET_STRING + strlen ("/short")];
size_t addr_length = sizeof (connect_address);
void *sb = test_context_socket (ZMQ_REP);
TEST_ASSERT_SUCCESS_ERRNO (zmq_bind (sb, "ws://*:5557/short"));
TEST_ASSERT_SUCCESS_ERRNO (zmq_bind (sb, "ws://*:*/short"));
TEST_ASSERT_SUCCESS_ERRNO (
zmq_getsockopt (sb, ZMQ_LAST_ENDPOINT, connect_address, &addr_length));
strcat (connect_address, "/short");
void *sc = test_context_socket (ZMQ_REQ);
TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (sc, "ws://127.0.0.1:5557/short"));
TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (sc, connect_address));
zmq_msg_t msg;
TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_init_size (&msg, 255));
@ -78,11 +88,16 @@ void test_short_message ()
void test_large_message ()
{
char connect_address[MAX_SOCKET_STRING + strlen ("/large")];
size_t addr_length = sizeof (connect_address);
void *sb = test_context_socket (ZMQ_REP);
TEST_ASSERT_SUCCESS_ERRNO (zmq_bind (sb, "ws://*:5557/large"));
TEST_ASSERT_SUCCESS_ERRNO (zmq_bind (sb, "ws://*:*/large"));
TEST_ASSERT_SUCCESS_ERRNO (
zmq_getsockopt (sb, ZMQ_LAST_ENDPOINT, connect_address, &addr_length));
strcat (connect_address, "/short");
void *sc = test_context_socket (ZMQ_REQ);
TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (sc, "ws://127.0.0.1:5557/large"));
TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (sc, connect_address));
zmq_msg_t msg;
TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_init_size (&msg, 65536));
@ -108,6 +123,8 @@ void test_large_message ()
void test_curve ()
{
char connect_address[MAX_SOCKET_STRING + strlen ("/roundtrip")];
size_t addr_length = sizeof (connect_address);
char client_public[41];
char client_secret[41];
char server_public[41];
@ -124,7 +141,10 @@ void test_curve ()
zmq_setsockopt (server, ZMQ_CURVE_SERVER, &as_server, sizeof (int)));
TEST_ASSERT_SUCCESS_ERRNO (
zmq_setsockopt (server, ZMQ_CURVE_SECRETKEY, server_secret, 41));
TEST_ASSERT_SUCCESS_ERRNO (zmq_bind (server, "ws://*:5556/roundtrip"));
TEST_ASSERT_SUCCESS_ERRNO (zmq_bind (server, "ws://*:*/roundtrip"));
TEST_ASSERT_SUCCESS_ERRNO (zmq_getsockopt (server, ZMQ_LAST_ENDPOINT,
connect_address, &addr_length));
strcat (connect_address, "/roundtrip");
void *client = test_context_socket (ZMQ_REQ);
@ -134,8 +154,7 @@ void test_curve ()
zmq_setsockopt (client, ZMQ_CURVE_PUBLICKEY, client_public, 41));
TEST_ASSERT_SUCCESS_ERRNO (
zmq_setsockopt (client, ZMQ_CURVE_SECRETKEY, client_secret, 41));
TEST_ASSERT_SUCCESS_ERRNO (
zmq_connect (client, "ws://127.0.0.1:5556/roundtrip"));
TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (client, connect_address));
bounce (server, client);

View File

@ -124,16 +124,20 @@ const char *cert =
void test_roundtrip ()
{
char connect_address[MAX_SOCKET_STRING + strlen ("/roundtrip")];
size_t addr_length = sizeof (connect_address);
void *sb = test_context_socket (ZMQ_REP);
zmq_setsockopt (sb, ZMQ_WSS_CERT_PEM, cert, strlen (cert));
zmq_setsockopt (sb, ZMQ_WSS_KEY_PEM, key, strlen (key));
TEST_ASSERT_SUCCESS_ERRNO (zmq_bind (sb, "wss://*:5556/roundtrip"));
TEST_ASSERT_SUCCESS_ERRNO (zmq_bind (sb, "wss://*:*/roundtrip"));
TEST_ASSERT_SUCCESS_ERRNO (
zmq_getsockopt (sb, ZMQ_LAST_ENDPOINT, connect_address, &addr_length));
strcat (connect_address, "/roundtrip");
void *sc = test_context_socket (ZMQ_REQ);
zmq_setsockopt (sc, ZMQ_WSS_TRUST_PEM, cert, strlen (cert));
zmq_setsockopt (sc, ZMQ_WSS_HOSTNAME, "zeromq.org", strlen ("zeromq.org"));
TEST_ASSERT_SUCCESS_ERRNO (
zmq_connect (sc, "wss://127.0.0.1:5556/roundtrip"));
TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (sc, connect_address));
bounce (sb, sc);