This is an automated email from the ASF dual-hosted git repository. cliffjansen pushed a commit to branch main in repository https://gitbox.apache.org/repos/asf/qpid-proton.git
commit db52dcbf8070caed7d364ff8e16d1b549c676ee4 Author: Ahmad Fatoum <[email protected]> AuthorDate: Sun Oct 27 21:09:34 2024 +0100 PROTON-2594: [C++] connect_config_test: factor out some definitions into header The test_handler implementation in connect_config_test can be useful for further tests as well such as the incoming pkcs11_test. We don't want to add the PKCS#11 test here, because the test is not applicable to all platforms and it would overly complicate the existing tests, so let's share code via a common header. --- cpp/src/connect_config_test.cpp | 100 +----------------------------- cpp/src/test_handler.hpp | 133 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 135 insertions(+), 98 deletions(-) diff --git a/cpp/src/connect_config_test.cpp b/cpp/src/connect_config_test.cpp index 64fb9affb..4fdaf1ae7 100644 --- a/cpp/src/connect_config_test.cpp +++ b/cpp/src/connect_config_test.cpp @@ -24,9 +24,6 @@ #include "proton/connection_options.hpp" #include "proton/container.hpp" #include "proton/error_condition.hpp" -#include "proton/listener.hpp" -#include "proton/listen_handler.hpp" -#include "proton/messaging_handler.hpp" #include "proton/transport.hpp" #include "proton/ssl.hpp" #include "proton/sasl.hpp" @@ -35,6 +32,8 @@ #include "proton/sasl.h" #include "proton/ssl.h" +#include "test_handler.hpp" + #include <sstream> #include <fstream> #include <cstdio> @@ -78,12 +77,6 @@ namespace { using namespace std; using namespace proton; -using proton::error_condition; - -string configure(connection_options& opts, const string& config) { - istringstream is(config); - return connect_config::parse(is, opts); -} void test_default_file() { // Default file locations in order of preference. @@ -146,86 +139,6 @@ void test_invalid_json() { } } -// Extra classes to resolve clash of on_error in both messaging_handler and listen_handler -class messaging_handler : public proton::messaging_handler { - virtual void on_messaging_error(const error_condition&) = 0; - - void on_error(const error_condition& c) override { - on_messaging_error(c); - } -}; - -class listen_handler : public proton::listen_handler { - virtual void on_listen_error(listener& , const string&) = 0; - - void on_error(listener& l, const string& s) override { - on_listen_error(l, s); - } -}; - -class test_handler : public messaging_handler, public listen_handler { - bool opened_; - connection_options connection_options_; - listener listener_; - - void on_open(listener& l) override { - on_listener_start(l.container()); - } - - connection_options on_accept(listener& l) override { - return connection_options_; - } - - void on_container_start(container& c) override { - listener_ = c.listen("//:0", *this); - } - - void on_connection_open(connection& c) override { - if (!c.active()) { // Server side - opened_ = true; - check_connection(c); - listener_.stop(); - c.close(); - } - } - - void on_messaging_error(const error_condition& e) override { - FAIL("unexpected error " << e); - } - - void on_listen_error(listener&, const string& s) override { - FAIL("unexpected listen error " << s); - } - - virtual void check_connection(connection& c) {} - virtual void on_listener_start(container& c) = 0; - - protected: - string config_with_port(const string& bare_config) { - ostringstream ss; - ss << "{" << "\"port\":" << listener_.port() << ", " << bare_config << "}"; - return ss.str(); - } - - void connect(container& c, const string& bare_config) { - connection_options opts; - c.connect(configure(opts, config_with_port(bare_config)), opts); - } - - void stop_listener() { - listener_.stop(); - } - - public: - test_handler(const connection_options& listen_opts = connection_options()) : - opened_(false), connection_options_(listen_opts) {} - - void run() { - container(*this).run(); - ASSERT(opened_); - } -}; - class test_almost_default_connect : public test_handler { public: @@ -241,15 +154,6 @@ class test_almost_default_connect : public test_handler { } }; -// Hack to use protected pn_object member of proton::object -template <class P, class T> -class internal_access_of: public P { - -public: - internal_access_of(const P& t) : P(t) {} - T* pn_object() { return proton::internal::object<T>::pn_object(); } -}; - class test_default_connect : public test_handler { public: diff --git a/cpp/src/test_handler.hpp b/cpp/src/test_handler.hpp new file mode 100644 index 000000000..8e6781b78 --- /dev/null +++ b/cpp/src/test_handler.hpp @@ -0,0 +1,133 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +#include "test_bits.hpp" + +#include "proton/connect_config.hpp" +#include "proton/connection.hpp" +#include "proton/connection_options.hpp" +#include "proton/container.hpp" +#include "proton/error_condition.hpp" +#include "proton/listener.hpp" +#include "proton/listen_handler.hpp" +#include "proton/messaging_handler.hpp" + +#include <string> + +namespace { + +using namespace std; +using namespace proton; +using proton::error_condition; + +static inline string configure(connection_options& opts, const string& config) { + istringstream is(config); + return connect_config::parse(is, opts); +} + +// Extra classes to resolve clash of on_error in both messaging_handler and listen_handler +class messaging_handler : public proton::messaging_handler { + virtual void on_messaging_error(const error_condition&) = 0; + + void on_error(const error_condition& c) override { + on_messaging_error(c); + } +}; + +class listen_handler : public proton::listen_handler { + virtual void on_listen_error(listener& , const string&) = 0; + + void on_error(listener& l, const string& s) override { + on_listen_error(l, s); + } +}; + +class test_handler : public messaging_handler, public listen_handler { + bool opened_; + connection_options connection_options_; + listener listener_; + + void on_open(listener& l) override { + on_listener_start(l.container()); + } + + connection_options on_accept(listener& l) override { + return connection_options_; + } + + void on_container_start(container& c) override { + listener_ = c.listen("//:0", *this); + } + + void on_connection_open(connection& c) override { + if (!c.active()) { // Server side + opened_ = true; + check_connection(c); + listener_.stop(); + c.close(); + } + } + + void on_messaging_error(const error_condition& e) override { + FAIL("unexpected error " << e); + } + + void on_listen_error(listener&, const string& s) override { + FAIL("unexpected listen error " << s); + } + + virtual void check_connection(connection& c) {} + virtual void on_listener_start(container& c) = 0; + + protected: + string config_with_port(const string& bare_config) { + ostringstream ss; + ss << "{" << "\"port\":" << listener_.port() << ", " << bare_config << "}"; + return ss.str(); + } + + void connect(container& c, const string& bare_config) { + connection_options opts; + c.connect(configure(opts, config_with_port(bare_config)), opts); + } + + void stop_listener() { + listener_.stop(); + } + + public: + test_handler(const connection_options& listen_opts = connection_options()) : + opened_(false), connection_options_(listen_opts) {} + + void run() { + container(*this).run(); + ASSERT(opened_); + } +}; + +// Hack to use protected pn_object member of proton::object +template <class P, class T> +class internal_access_of: public P { + +public: + internal_access_of(const P& t) : P(t) {} + T* pn_object() { return proton::internal::object<T>::pn_object(); } +}; + +} --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
