Skip to content

Commit

Permalink
PROTON-2594: [C++] connect_config_test: factor out some definitions i…
Browse files Browse the repository at this point in the history
…nto 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.
  • Loading branch information
a3f committed Nov 14, 2024
1 parent 2b84656 commit 2dce899
Show file tree
Hide file tree
Showing 2 changed files with 135 additions and 98 deletions.
100 changes: 2 additions & 98 deletions cpp/src/connect_config_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -35,6 +32,8 @@
#include "proton/sasl.h"
#include "proton/ssl.h"

#include "test_handler.hpp"

#include <sstream>
#include <fstream>
#include <cstdio>
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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:

Expand All @@ -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:

Expand Down
133 changes: 133 additions & 0 deletions cpp/src/test_handler.hpp
Original file line number Diff line number Diff line change
@@ -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(); }
};

}

0 comments on commit 2dce899

Please sign in to comment.