| 1 |
/* |
| 2 |
* Licensed to the Apache Software Foundation (ASF) under one |
| 3 |
* or more contributor license agreements. See the NOTICE file |
| 4 |
* distributed with this work for additional information |
| 5 |
* regarding copyright ownership. The ASF licenses this file |
| 6 |
* to you under the Apache License, Version 2.0 (the |
| 7 |
* "License"); you may not use this file except in compliance |
| 8 |
* with the License. You may obtain a copy of the License at |
| 9 |
* |
| 10 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 11 |
* |
| 12 |
* Unless required by applicable law or agreed to in writing, |
| 13 |
* software distributed under the License is distributed on an |
| 14 |
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 |
* KIND, either express or implied. See the License for the |
| 16 |
* specific language governing permissions and limitations |
| 17 |
* under the License. |
| 18 |
*/ |
| 19 |
|
| 20 |
#include "test_bits.hpp" |
| 21 |
|
| 22 |
#include "proton/connect_config.hpp" |
| 23 |
#include "proton/connection.hpp" |
| 24 |
#include "proton/connection_options.hpp" |
| 25 |
#include "proton/container.hpp" |
| 26 |
#include "proton/error_condition.hpp" |
| 27 |
#include "proton/listener.hpp" |
| 28 |
#include "proton/listen_handler.hpp" |
| 29 |
#include "proton/messaging_handler.hpp" |
| 30 |
#include "proton/transport.hpp" |
| 31 |
#include "proton/ssl.hpp" |
| 32 |
#include "proton/sasl.hpp" |
| 33 |
|
| 34 |
// The C++ API lacks a way to test for presence of extended SASL or SSL support. |
| 35 |
#include "proton/sasl.h" |
| 36 |
#include "proton/ssl.h" |
| 37 |
|
| 38 |
#include <sstream> |
| 39 |
#include <fstream> |
| 40 |
#include <cstdio> |
| 41 |
#include <cstdlib> |
| 42 |
|
| 43 |
// Used to inspect items from the proton-c level of the API |
| 44 |
#include "proton/internal/object.hpp" |
| 45 |
#include "proton/returned.hpp" |
| 46 |
#include <proton/transport.h> |
| 47 |
#include <proton/connection.h> |
| 48 |
|
| 49 |
#include <json/version.h> |
| 50 |
|
| 51 |
// Windows has a different set of APIs for setting the environment |
| 52 |
#ifdef _WIN32 |
| 53 |
#include <string.h> |
| 54 |
namespace { |
| 55 |
void setenv(const char* var, const char* value, int) { |
| 56 |
int vlen = strlen(var); |
| 57 |
int len = vlen+strlen(value)+1; |
| 58 |
char* buff = new char[len+1]; |
| 59 |
strcpy(buff, var); |
| 60 |
strcpy(buff+vlen, "="); |
| 61 |
strcpy(buff+vlen+1, value); |
| 62 |
_putenv(buff); |
| 63 |
delete [] buff; |
| 64 |
} |
| 65 |
|
| 66 |
void unsetenv(const char* var) { |
| 67 |
int len = strlen(var); |
| 68 |
char* buff = new char[len+1]; |
| 69 |
strcpy(buff, var); |
| 70 |
strcpy(buff+len, "="); |
| 71 |
_putenv(buff); |
| 72 |
delete [] buff; |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
#endif |
| 77 |
namespace { |
| 78 |
|
| 79 |
using namespace std; |
| 80 |
using namespace proton; |
| 81 |
using proton::error_condition; |
| 82 |
|
| 83 |
string configure(connection_options& opts, const string& config) { |
| 84 |
istringstream is(config); |
| 85 |
return connect_config::parse(is, opts); |
| 86 |
} |
| 87 |
|
| 88 |
void test_default_file() { |
| 89 |
// Default file locations in order of preference. |
| 90 |
::setenv("MESSAGING_CONNECT_FILE", "environment", 1); |
| 91 |
ofstream("connect.json") << "{ \"host\": \"current\" }" << endl; |
| 92 |
::setenv("HOME", "testdata", 1); |
| 93 |
ofstream("testdata/.config/messaging/connect.json") << "{ \"host\": \".config\" }" << endl; |
| 94 |
ASSERT_EQUAL("environment", connect_config::default_file()); |
| 95 |
::unsetenv("MESSAGING_CONNECT_FILE"); |
| 96 |
ASSERT_EQUAL("connect.json", connect_config::default_file()); |
| 97 |
remove("connect.json"); |
| 98 |
ASSERT_EQUAL("testdata/.config/messaging/connect.json", connect_config::default_file()); |
| 99 |
remove("testdata/.config/messaging/connect.json"); |
| 100 |
|
| 101 |
// We can't fully test prefix and /etc locations, we have no control. |
| 102 |
try { |
| 103 |
ASSERT_SUBSTRING("/etc/messaging", connect_config::default_file()); |
| 104 |
} catch (...) {} // OK if not there |
| 105 |
} |
| 106 |
|
| 107 |
void test_addr() { |
| 108 |
connection_options opts; |
| 109 |
ASSERT_EQUAL("foo:bar", configure(opts, "{ \"host\":\"foo\", \"port\":\"bar\" }")); |
| 110 |
ASSERT_EQUAL("foo:1234", configure(opts, "{ \"host\":\"foo\", \"port\":\"1234\" }")); |
| 111 |
ASSERT_EQUAL("localhost:amqps", configure(opts, "{}")); |
| 112 |
ASSERT_EQUAL("localhost:amqp", configure(opts, "{\"scheme\":\"amqp\"}")); |
| 113 |
ASSERT_EQUAL("foo:bar", configure(opts, "{ \"host\":\"foo\", /* inline comment */\"port\":\"bar\" // end of line comment\n}")); |
| 114 |
|
| 115 |
ASSERT_THROWS_MSG(error, "'scheme' must be", configure(opts, "{\"scheme\":\"bad\"}")); |
| 116 |
ASSERT_THROWS_MSG(error, "'scheme' expected string, found boolean", configure(opts, "{\"scheme\":true}")); |
| 117 |
ASSERT_THROWS_MSG(error, "'port' expected string or uint, found boolean", configure(opts, "{\"port\":true}")); |
| 118 |
ASSERT_THROWS_MSG(error, "'host' expected string, found boolean", configure(opts, "{\"host\":true}")); |
| 119 |
} |
| 120 |
|
| 121 |
// Hack to write strings with embedded '"' and newlines |
| 122 |
#define RAW_STRING(...) #__VA_ARGS__ |
| 123 |
|
| 124 |
void test_invalid_config() { |
| 125 |
connection_options opts; |
| 126 |
ASSERT_THROWS_MSG(proton::error, "expected string", configure(opts, RAW_STRING({ "scheme":true}))); |
| 127 |
ASSERT_THROWS_MSG(proton::error, "expected object", configure(opts, RAW_STRING({ "tls":""}))); |
| 128 |
ASSERT_THROWS_MSG(proton::error, "expected object", configure(opts, RAW_STRING({ "sasl":true}))); |
| 129 |
ASSERT_THROWS_MSG(proton::error, "expected boolean", configure(opts, RAW_STRING({ "sasl": { "enable":""}}))); |
| 130 |
ASSERT_THROWS_MSG(proton::error, "expected boolean", configure(opts, RAW_STRING({ "tls": { "verify":""}}))); |
| 131 |
} |
| 132 |
|
| 133 |
void test_invalid_json() { |
| 134 |
connection_options opts; |
| 135 |
// ancient versions of jsoncpp use a generic error message for parse errors |
| 136 |
// in the exception, and print the detailed message to stderr |
| 137 |
// https://github.com/open-source-parsers/jsoncpp/commit/6b10ce8c0d07ea07861e82f65f17d9fd6abd658d |
| 138 |
if (std::make_tuple(JSONCPP_VERSION_MAJOR, JSONCPP_VERSION_MINOR) < std::make_tuple(1, 7)) { |
| 139 |
ASSERT_THROWS_MSG(proton::error, "reader error", configure(opts, "{")); |
| 140 |
ASSERT_THROWS_MSG(proton::error, "reader error", configure(opts, "")); |
| 141 |
ASSERT_THROWS_MSG(proton::error, "reader error", configure(opts, RAW_STRING({ "user" : "x" "host" : "y"}))); |
| 142 |
} else { |
| 143 |
ASSERT_THROWS_MSG(proton::error, "Missing '}'", configure(opts, "{")); |
| 144 |
ASSERT_THROWS_MSG(proton::error, "Syntax error", configure(opts, "")); |
| 145 |
ASSERT_THROWS_MSG(proton::error, "Missing ','", configure(opts, RAW_STRING({ "user":"x" "host":"y"}))); |
| 146 |
} |
| 147 |
} |
| 148 |
|
| 149 |
// Extra classes to resolve clash of on_error in both messaging_handler and listen_handler |
| 150 |
class messaging_handler : public proton::messaging_handler { |
| 151 |
virtual void on_messaging_error(const error_condition&) = 0; |
| 152 |
|
| 153 |
void on_error(const error_condition& c) override { |
| 154 |
on_messaging_error(c); |
| 155 |
} |
| 156 |
}; |
| 157 |
|
| 158 |
class listen_handler : public proton::listen_handler { |
| 159 |
virtual void on_listen_error(listener& , const string&) = 0; |
| 160 |
|
| 161 |
void on_error(listener& l, const string& s) override { |
| 162 |
on_listen_error(l, s); |
| 163 |
} |
| 164 |
}; |
| 165 |
|
| 166 |
class test_handler : public messaging_handler, public listen_handler { |
| 167 |
bool opened_; |
| 168 |
connection_options connection_options_; |
| 169 |
listener listener_; |
| 170 |
|
| 171 |
void on_open(listener& l) override { |
| 172 |
on_listener_start(l.container()); |
| 173 |
} |
| 174 |
|
| 175 |
connection_options on_accept(listener& l) override { |
| 176 |
return connection_options_; |
| 177 |
} |
| 178 |
|
| 179 |
void on_container_start(container& c) override { |
| 180 |
listener_ = c.listen("//:0", *this); |
| 181 |
} |
| 182 |
|
| 183 |
void on_connection_open(connection& c) override { |
| 184 |
if (!c.active()) { // Server side |
| 185 |
opened_ = true; |
| 186 |
check_connection(c); |
| 187 |
listener_.stop(); |
| 188 |
c.close(); |
| 189 |
} |
| 190 |
} |
| 191 |
|
| 192 |
void on_messaging_error(const error_condition& e) override { |
| 193 |
FAIL("unexpected error " << e); |
| 194 |
} |
| 195 |
|
| 196 |
void on_listen_error(listener&, const string& s) override { |
| 197 |
FAIL("unexpected listen error " << s); |
| 198 |
} |
| 199 |
|
| 200 |
virtual void check_connection(connection& c) {} |
| 201 |
virtual void on_listener_start(container& c) = 0; |
| 202 |
|
| 203 |
protected: |
| 204 |
string config_with_port(const string& bare_config) { |
| 205 |
ostringstream ss; |
| 206 |
ss << "{" << "\"port\":" << listener_.port() << ", " << bare_config << "}"; |
| 207 |
return ss.str(); |
| 208 |
} |
| 209 |
|
| 210 |
void connect(container& c, const string& bare_config) { |
| 211 |
connection_options opts; |
| 212 |
c.connect(configure(opts, config_with_port(bare_config)), opts); |
| 213 |
} |
| 214 |
|
| 215 |
void stop_listener() { |
| 216 |
listener_.stop(); |
| 217 |
} |
| 218 |
|
| 219 |
public: |
| 220 |
test_handler(const connection_options& listen_opts = connection_options()) : |
| 221 |
opened_(false), connection_options_(listen_opts) {} |
| 222 |
|
| 223 |
void run() { |
| 224 |
container(*this).run(); |
| 225 |
ASSERT(opened_); |
| 226 |
} |
| 227 |
}; |
| 228 |
|
| 229 |
class test_almost_default_connect : public test_handler { |
| 230 |
public: |
| 231 |
|
| 232 |
void on_listener_start(container& c) override { |
| 233 |
ofstream os("connect.json"); |
| 234 |
ASSERT(os << config_with_port(RAW_STRING("scheme":"amqp"))); |
| 235 |
os.close(); |
| 236 |
c.connect(); |
| 237 |
} |
| 238 |
|
| 239 |
void check_connection(connection& c) override { |
| 240 |
ASSERT_EQUAL("localhost", c.virtual_host()); |
| 241 |
} |
| 242 |
}; |
| 243 |
|
| 244 |
// Hack to use protected pn_object member of proton::object |
| 245 |
template <class P, class T> |
| 246 |
class internal_access_of: public P { |
| 247 |
|
| 248 |
public: |
| 249 |
internal_access_of(const P& t) : P(t) {} |
| 250 |
T* pn_object() { return proton::internal::object<T>::pn_object(); } |
| 251 |
}; |
| 252 |
|
| 253 |
class test_default_connect : public test_handler { |
| 254 |
public: |
| 255 |
|
| 256 |
void on_listener_start(container& c) override { |
| 257 |
c.connect(); |
| 258 |
} |
| 259 |
|
| 260 |
// on_transport_open will be called whatever happens to the connection whether it's |
| 261 |
// connected or rejected due to error. |
| 262 |
// |
| 263 |
// We use a hacky way to get to the underlying C objects to check what's in them |
| 264 |
void on_transport_open(proton::transport& transport) override { |
| 265 |
proton::connection connection = transport.connection(); |
| 266 |
internal_access_of<proton::connection, pn_connection_t> ic(connection); |
| 267 |
pn_connection_t* c = ic.pn_object(); |
| 268 |
ASSERT_EQUAL(std::string("localhost"), pn_connection_get_hostname(c)); |
| 269 |
} |
| 270 |
|
| 271 |
void on_transport_error(proton::transport& t) override { |
| 272 |
ASSERT_SUBSTRING("localhost:5671", t.error().description()); |
| 273 |
t.connection().container().stop(); |
| 274 |
} |
| 275 |
|
| 276 |
void run() { |
| 277 |
container(*this).run(); |
| 278 |
} |
| 279 |
}; |
| 280 |
|
| 281 |
class test_host_user_pass : public test_handler { |
| 282 |
public: |
| 283 |
|
| 284 |
void on_listener_start(proton::container & c) override { |
| 285 |
connect(c, RAW_STRING("scheme":"amqp", "host":"127.0.0.1", "user":"user@proton", "password":"password")); |
| 286 |
} |
| 287 |
|
| 288 |
void check_connection(connection& c) override { |
| 289 |
ASSERT_EQUAL("127.0.0.1", c.virtual_host()); |
| 290 |
if (pn_sasl_extended()) { |
| 291 |
ASSERT_EQUAL("user@proton", c.user()); |
| 292 |
} else { |
| 293 |
ASSERT_EQUAL("anonymous", c.user()); |
| 294 |
} |
| 295 |
} |
| 296 |
}; |
| 297 |
|
| 298 |
class test_tls : public test_handler { |
| 299 |
static connection_options make_opts() { |
| 300 |
ssl_certificate cert("testdata/certs/server-certificate.pem", |
| 301 |
"testdata/certs/server-private-key.pem", |
| 302 |
"server-password"); |
| 303 |
connection_options opts; |
| 304 |
opts.ssl_server_options(ssl_server_options(cert)); |
| 305 |
return opts; |
| 306 |
} |
| 307 |
|
| 308 |
public: |
| 309 |
|
| 310 |
test_tls() : test_handler(make_opts()) {} |
| 311 |
|
| 312 |
void on_listener_start(proton::container & c) override { |
| 313 |
connect(c, RAW_STRING("scheme":"amqps", "tls": { "verify":false })); |
| 314 |
} |
| 315 |
}; |
| 316 |
|
| 317 |
class test_tls_default_fail : public test_handler { |
| 318 |
static connection_options make_opts() { |
| 319 |
ssl_certificate cert("testdata/certs/server-certificate.pem", |
| 320 |
"testdata/certs/server-private-key.pem", |
| 321 |
"server-password"); |
| 322 |
connection_options opts; |
| 323 |
opts.ssl_server_options(ssl_server_options(cert)); |
| 324 |
return opts; |
| 325 |
} |
| 326 |
bool failed_; |
| 327 |
|
| 328 |
public: |
| 329 |
|
| 330 |
test_tls_default_fail() : test_handler(make_opts()), failed_(false) {} |
| 331 |
|
| 332 |
void on_listener_start(proton::container& c) override { |
| 333 |
connect(c, RAW_STRING("scheme":"amqps")); |
| 334 |
} |
| 335 |
|
| 336 |
void on_messaging_error(const proton::error_condition& c) override { |
| 337 |
if (failed_) return; |
| 338 |
|
| 339 |
ASSERT_SUBSTRING("verify failed", c.description()); |
| 340 |
failed_ = true; |
| 341 |
stop_listener(); |
| 342 |
} |
| 343 |
|
| 344 |
void run() { |
| 345 |
container(*this).run(); |
| 346 |
ASSERT(failed_); |
| 347 |
} |
| 348 |
}; |
| 349 |
|
| 350 |
class test_tls_external : public test_handler { |
| 351 |
|
| 352 |
static connection_options make_opts() { |
| 353 |
ssl_certificate cert("testdata/certs/server-certificate-lh.pem", |
| 354 |
"testdata/certs/server-private-key-lh.pem", |
| 355 |
"server-password"); |
| 356 |
connection_options opts; |
| 357 |
opts.ssl_server_options(ssl_server_options(cert, |
| 358 |
"testdata/certs/ca-certificate.pem", |
| 359 |
"testdata/certs/ca-certificate.pem", |
| 360 |
ssl::VERIFY_PEER)); |
| 361 |
return opts; |
| 362 |
} |
| 363 |
|
| 364 |
public: |
| 365 |
|
| 366 |
test_tls_external() : test_handler(make_opts()) {} |
| 367 |
|
| 368 |
void on_listener_start(container& c) override { |
| 369 |
connect(c, RAW_STRING( |
| 370 |
"scheme":"amqps", |
| 371 |
"sasl":{ "mechanisms": "EXTERNAL" }, |
| 372 |
"tls": { |
| 373 |
"cert":"testdata/certs/client-certificate.pem", |
| 374 |
"key":"testdata/certs/client-private-key-no-password.pem", |
| 375 |
"ca":"testdata/certs/ca-certificate.pem", |
| 376 |
"verify":true })); |
| 377 |
} |
| 378 |
}; |
| 379 |
|
| 380 |
class test_tls_plain : public test_handler { |
| 381 |
|
| 382 |
static connection_options make_opts() { |
| 383 |
ssl_certificate cert("testdata/certs/server-certificate-lh.pem", |
| 384 |
"testdata/certs/server-private-key-lh.pem", |
| 385 |
"server-password"); |
| 386 |
connection_options opts; |
| 387 |
opts.ssl_server_options(ssl_server_options(cert, |
| 388 |
"testdata/certs/ca-certificate.pem", |
| 389 |
"testdata/certs/ca-certificate.pem", |
| 390 |
ssl::VERIFY_PEER)); |
| 391 |
return opts; |
| 392 |
} |
| 393 |
|
| 394 |
public: |
| 395 |
|
| 396 |
test_tls_plain() : test_handler(make_opts()) {} |
| 397 |
|
| 398 |
void on_listener_start(container& c) override { |
| 399 |
connect(c, RAW_STRING( |
| 400 |
"scheme":"amqps", "user":"user@proton", "password": "password", |
| 401 |
"sasl":{ "mechanisms": "PLAIN" }, |
| 402 |
"tls": { |
| 403 |
"cert":"testdata/certs/client-certificate.pem", |
| 404 |
"key":"testdata/certs/client-private-key-no-password.pem", |
| 405 |
"ca":"testdata/certs/ca-certificate.pem", |
| 406 |
"verify":true })); |
| 407 |
} |
| 408 |
}; |
| 409 |
|
| 410 |
} // namespace |
| 411 |
|
| 412 |
|
| 413 |
int main(int argc, char** argv) { |
| 414 |
int failed = 0; |
| 415 |
RUN_ARGV_TEST(failed, test_default_file()); |
| 416 |
RUN_ARGV_TEST(failed, test_addr()); |
| 417 |
RUN_ARGV_TEST(failed, test_invalid_config()); |
| 418 |
RUN_ARGV_TEST(failed, test_invalid_json()); |
| 419 |
RUN_ARGV_TEST(failed, test_default_connect().run()); |
| 420 |
RUN_ARGV_TEST(failed, test_almost_default_connect().run()); |
| 421 |
|
| 422 |
bool have_sasl = pn_sasl_extended() && getenv("PN_SASL_CONFIG_PATH"); |
| 423 |
pn_ssl_domain_t *have_ssl = pn_ssl_domain(PN_SSL_MODE_SERVER); |
| 424 |
|
| 425 |
if (have_sasl) { |
| 426 |
RUN_ARGV_TEST(failed, test_host_user_pass().run()); |
| 427 |
} |
| 428 |
#ifndef _WIN32 |
| 429 |
if (have_ssl) { |
| 430 |
pn_ssl_domain_free(have_ssl); |
| 431 |
RUN_ARGV_TEST(failed, test_tls().run()); |
| 432 |
RUN_ARGV_TEST(failed, test_tls_default_fail().run()); |
| 433 |
RUN_ARGV_TEST(failed, test_tls_external().run()); |
| 434 |
if (have_sasl) { |
| 435 |
RUN_ARGV_TEST(failed, test_tls_plain().run()); |
| 436 |
} |
| 437 |
} else { |
| 438 |
std::cout << "SKIP: TLS tests, not available" << std::endl; |
| 439 |
} |
| 440 |
#else |
| 441 |
std::cout << "SKIP: TLS tests, expected to fail on windows" << std::endl; |
| 442 |
#endif |
| 443 |
return failed; |
| 444 |
} |