1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 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 157 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 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408
|
// test_ssl_options.cpp
//
// Unit tests for the ssl_options class in the Paho MQTT C++ library.
//
/*******************************************************************************
* Copyright (c) 2016-2020 Frank Pagliughi <fpagliughi@mindspring.com>
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* and Eclipse Distribution License v1.0 which accompany this distribution.
*
* The Eclipse Public License is available at
* http://www.eclipse.org/legal/epl-v20.html
* and the Eclipse Distribution License is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*
* Contributors:
* Frank Pagliughi - initial implementation and documentation
* Guilherme M. Ferreira - add more test cases
* Frank Pagliughi - Converted to Catch2
*******************************************************************************/
#define UNIT_TESTS
#include "catch2_version.h"
#include "mqtt/ssl_options.h"
using namespace std::chrono;
using namespace mqtt;
/////////////////////////////////////////////////////////////////////////////
// C struct signature/eyecatcher
static const char* CSIG = "MQTS";
static const size_t CSIG_LEN = std::strlen(CSIG);
static const bool DFLT_SERVER_CERT = true;
static const std::string EMPTY_STR;
static const std::string TRUST_STORE{"trust store"};
static const std::string KEY_STORE{"key store"};
static const std::string PRIVATE_KEY{"private key"};
static const std::string PRIVATE_KEY_PASSWORD{"private key password"};
static const std::string CA_PATH{"ca path"};
static const std::string ENABLED_CIPHER_SUITES{"cipher suite"};
static const bool SERVER_CERT{false};
static const std::string ALPN0{"mqtt"};
static const std::string ALPN1{"x-iot-mqtt"};
static mqtt::ssl_options orgOpts{TRUST_STORE, KEY_STORE, PRIVATE_KEY,
PRIVATE_KEY_PASSWORD, CA_PATH, ENABLED_CIPHER_SUITES,
SERVER_CERT, {ALPN0, ALPN1}};
// ----------------------------------------------------------------------
// Test the default constructor
// ----------------------------------------------------------------------
TEST_CASE("ssl_options dflt constructor", "[options]")
{
mqtt::ssl_options opts;
REQUIRE(opts.get_trust_store().empty());
REQUIRE(opts.get_key_store().empty());
REQUIRE(opts.get_private_key().empty());
REQUIRE(opts.get_private_key_password().empty());
REQUIRE(opts.get_ca_path().empty());
REQUIRE(DFLT_SERVER_CERT == opts.get_enable_server_cert_auth());
REQUIRE(opts.get_alpn_protos().empty());
// Make sure the empty string represents a nullptr for C library
const auto& c_struct = opts.c_struct();
REQUIRE(!memcmp(&c_struct.struct_id, CSIG, CSIG_LEN));
REQUIRE(c_struct.trustStore == nullptr);
REQUIRE(c_struct.keyStore == nullptr);
REQUIRE(c_struct.privateKey == nullptr);
REQUIRE(c_struct.privateKeyPassword == nullptr);
REQUIRE(c_struct.CApath == nullptr);
REQUIRE(c_struct.enabledCipherSuites == nullptr);
// REQUIRE(DFLT_SERVER_CERT == c_struct.enableServerCertAuth != 0);
REQUIRE(c_struct.protos == nullptr);
REQUIRE(c_struct.protos_len == 0);
}
// ----------------------------------------------------------------------
// Test the constructor that takes user arguments
// ----------------------------------------------------------------------
TEST_CASE("ssl_options user constructor", "[options]")
{
mqtt::ssl_options opts{TRUST_STORE, KEY_STORE, PRIVATE_KEY,
PRIVATE_KEY_PASSWORD, CA_PATH, ENABLED_CIPHER_SUITES,
SERVER_CERT, {ALPN0, ALPN1}};
REQUIRE(TRUST_STORE == opts.get_trust_store());
REQUIRE(KEY_STORE == opts.get_key_store());
REQUIRE(PRIVATE_KEY == opts.get_private_key());
REQUIRE(PRIVATE_KEY_PASSWORD == opts.get_private_key_password());
REQUIRE(CA_PATH == opts.get_ca_path());
REQUIRE(ENABLED_CIPHER_SUITES == opts.get_enabled_cipher_suites());
REQUIRE(SERVER_CERT == opts.get_enable_server_cert_auth());
auto protos = opts.get_alpn_protos();
REQUIRE(2 == protos.size());
REQUIRE(ALPN0 == protos[0]);
REQUIRE(ALPN1 == protos[1]);
// Check the underlying C struct
const auto& c_struct = opts.c_struct();
REQUIRE(!memcmp(&c_struct.struct_id, CSIG, CSIG_LEN));
REQUIRE(!strcmp(c_struct.trustStore, TRUST_STORE.c_str()));
REQUIRE(!strcmp(c_struct.keyStore, KEY_STORE.c_str()));
REQUIRE(!strcmp(c_struct.privateKey, PRIVATE_KEY.c_str()));
REQUIRE(!strcmp(c_struct.privateKeyPassword, PRIVATE_KEY_PASSWORD.c_str()));
REQUIRE(!strcmp(c_struct.CApath, CA_PATH.c_str()));
REQUIRE(!strcmp(c_struct.enabledCipherSuites, ENABLED_CIPHER_SUITES.c_str()));
// REQUIRE(SERVER_CERT == c_struct.enableServerCertAuth != 0);
auto n0 = ALPN0.length();
auto n1 = ALPN1.length();
REQUIRE(c_struct.protos_len == n0 + n1 + 2);
REQUIRE(c_struct.protos[0] == n0);
REQUIRE(memcmp(c_struct.protos + 1, ALPN0.data(), n0) == 0);
REQUIRE(c_struct.protos[n0 + 1] == n1);
REQUIRE(memcmp(c_struct.protos + n0 + 2, ALPN1.data(), n1) == 0);
}
// ----------------------------------------------------------------------
// Test the copy constructor
// ----------------------------------------------------------------------
TEST_CASE("ssl_options copy constructor", "[options]")
{
mqtt::ssl_options org{TRUST_STORE, KEY_STORE, PRIVATE_KEY,
PRIVATE_KEY_PASSWORD, CA_PATH, ENABLED_CIPHER_SUITES,
SERVER_CERT, {ALPN0, ALPN1}};
mqtt::ssl_options opts{org};
REQUIRE(TRUST_STORE == opts.get_trust_store());
REQUIRE(KEY_STORE == opts.get_key_store());
REQUIRE(PRIVATE_KEY == opts.get_private_key());
REQUIRE(PRIVATE_KEY_PASSWORD == opts.get_private_key_password());
REQUIRE(CA_PATH == opts.get_ca_path());
REQUIRE(ENABLED_CIPHER_SUITES == opts.get_enabled_cipher_suites());
REQUIRE(SERVER_CERT == opts.get_enable_server_cert_auth());
auto protos = opts.get_alpn_protos();
REQUIRE(2 == protos.size());
REQUIRE(ALPN0 == protos[0]);
REQUIRE(ALPN1 == protos[1]);
// Check the underlying C struct
const auto& c_struct = opts.c_struct();
REQUIRE(!memcmp(&c_struct.struct_id, CSIG, CSIG_LEN));
REQUIRE(!strcmp(c_struct.trustStore, TRUST_STORE.c_str()));
REQUIRE(!strcmp(c_struct.keyStore, KEY_STORE.c_str()));
REQUIRE(!strcmp(c_struct.privateKey, PRIVATE_KEY.c_str()));
REQUIRE(!strcmp(c_struct.privateKeyPassword, PRIVATE_KEY_PASSWORD.c_str()));
REQUIRE(!strcmp(c_struct.CApath, CA_PATH.c_str()));
REQUIRE(!strcmp(c_struct.enabledCipherSuites, ENABLED_CIPHER_SUITES.c_str()));
// REQUIRE(SERVER_CERT == c_struct.enableServerCertAuth != 0);
// Make sure it's a true copy, not linked to the original
org.set_trust_store(EMPTY_STR);
org.set_key_store(EMPTY_STR);
org.set_private_key(EMPTY_STR);
org.set_private_key_password(EMPTY_STR);
org.set_ca_path(EMPTY_STR);
org.set_enabled_cipher_suites(EMPTY_STR);
org.set_enable_server_cert_auth(!SERVER_CERT);
REQUIRE(TRUST_STORE == opts.get_trust_store());
REQUIRE(KEY_STORE == opts.get_key_store());
REQUIRE(PRIVATE_KEY == opts.get_private_key());
REQUIRE(PRIVATE_KEY_PASSWORD == opts.get_private_key_password());
REQUIRE(CA_PATH == opts.get_ca_path());
REQUIRE(ENABLED_CIPHER_SUITES == opts.get_enabled_cipher_suites());
REQUIRE(SERVER_CERT == opts.get_enable_server_cert_auth());
auto n0 = ALPN0.length();
auto n1 = ALPN1.length();
REQUIRE(c_struct.protos_len == n0 + n1 + 2);
REQUIRE(c_struct.protos[0] == n0);
REQUIRE(memcmp(c_struct.protos + 1, ALPN0.data(), n0) == 0);
REQUIRE(c_struct.protos[n0 + 1] == n1);
REQUIRE(memcmp(c_struct.protos + n0 + 2, ALPN1.data(), n1) == 0);
}
// ----------------------------------------------------------------------
// Test the move constructor
// ----------------------------------------------------------------------
TEST_CASE("ssl_options move constructor", "[options]")
{
mqtt::ssl_options org{orgOpts};
mqtt::ssl_options opts(std::move(org));
REQUIRE(TRUST_STORE == opts.get_trust_store());
REQUIRE(KEY_STORE == opts.get_key_store());
REQUIRE(PRIVATE_KEY == opts.get_private_key());
REQUIRE(PRIVATE_KEY_PASSWORD == opts.get_private_key_password());
REQUIRE(CA_PATH == opts.get_ca_path());
REQUIRE(ENABLED_CIPHER_SUITES == opts.get_enabled_cipher_suites());
REQUIRE(SERVER_CERT == opts.get_enable_server_cert_auth());
auto protos = opts.get_alpn_protos();
REQUIRE(2 == protos.size());
REQUIRE(ALPN0 == protos[0]);
REQUIRE(ALPN1 == protos[1]);
// Check that the original was moved
REQUIRE(org.get_trust_store().empty());
REQUIRE(org.get_key_store().empty());
REQUIRE(org.get_private_key().empty());
REQUIRE(org.get_private_key_password().empty());
REQUIRE(org.get_ca_path().empty());
REQUIRE(org.get_enabled_cipher_suites().empty());
REQUIRE(org.get_alpn_protos().empty());
// Check the underlying C struct
const auto& c_struct = opts.c_struct();
auto n0 = ALPN0.length();
auto n1 = ALPN1.length();
REQUIRE(c_struct.protos_len == n0 + n1 + 2);
REQUIRE(c_struct.protos[0] == n0);
REQUIRE(memcmp(c_struct.protos + 1, ALPN0.data(), n0) == 0);
REQUIRE(c_struct.protos[n0 + 1] == n1);
REQUIRE(memcmp(c_struct.protos + n0 + 2, ALPN1.data(), n1) == 0);
}
// ----------------------------------------------------------------------
// Test the copy assignment operator=(const&)
// ----------------------------------------------------------------------
TEST_CASE("ssl_options copy assignment", "[options]")
{
mqtt::ssl_options org{orgOpts};
mqtt::ssl_options opts;
opts = orgOpts;
REQUIRE(TRUST_STORE == opts.get_trust_store());
REQUIRE(KEY_STORE == opts.get_key_store());
REQUIRE(PRIVATE_KEY == opts.get_private_key());
REQUIRE(PRIVATE_KEY_PASSWORD == opts.get_private_key_password());
REQUIRE(CA_PATH == opts.get_ca_path());
REQUIRE(ENABLED_CIPHER_SUITES == opts.get_enabled_cipher_suites());
REQUIRE(SERVER_CERT == opts.get_enable_server_cert_auth());
// Make sure it's a true copy, not linked to the original
org.set_trust_store(EMPTY_STR);
org.set_key_store(EMPTY_STR);
org.set_private_key(EMPTY_STR);
org.set_private_key_password(EMPTY_STR);
org.set_ca_path(EMPTY_STR);
org.set_enabled_cipher_suites(EMPTY_STR);
org.set_enable_server_cert_auth(!SERVER_CERT);
org.set_alpn_protos({});
REQUIRE(TRUST_STORE == opts.get_trust_store());
REQUIRE(KEY_STORE == opts.get_key_store());
REQUIRE(PRIVATE_KEY == opts.get_private_key());
REQUIRE(PRIVATE_KEY_PASSWORD == opts.get_private_key_password());
REQUIRE(CA_PATH == opts.get_ca_path());
REQUIRE(ENABLED_CIPHER_SUITES == opts.get_enabled_cipher_suites());
REQUIRE(SERVER_CERT == opts.get_enable_server_cert_auth());
// Self assignment should cause no harm
opts = opts;
REQUIRE(TRUST_STORE == opts.get_trust_store());
REQUIRE(KEY_STORE == opts.get_key_store());
REQUIRE(PRIVATE_KEY == opts.get_private_key());
REQUIRE(PRIVATE_KEY_PASSWORD == opts.get_private_key_password());
REQUIRE(CA_PATH == opts.get_ca_path());
REQUIRE(ENABLED_CIPHER_SUITES == opts.get_enabled_cipher_suites());
REQUIRE(SERVER_CERT == opts.get_enable_server_cert_auth());
}
// ----------------------------------------------------------------------
// Test the move assignment, operator=(&&)
// ----------------------------------------------------------------------
TEST_CASE("ssl_options move assignment", "[options]")
{
mqtt::ssl_options org{orgOpts};
mqtt::ssl_options opts;
opts = std::move(org);
REQUIRE(TRUST_STORE == opts.get_trust_store());
REQUIRE(KEY_STORE == opts.get_key_store());
REQUIRE(PRIVATE_KEY == opts.get_private_key());
REQUIRE(PRIVATE_KEY_PASSWORD == opts.get_private_key_password());
REQUIRE(CA_PATH == opts.get_ca_path());
REQUIRE(ENABLED_CIPHER_SUITES == opts.get_enabled_cipher_suites());
REQUIRE(SERVER_CERT == opts.get_enable_server_cert_auth());
auto protos = opts.get_alpn_protos();
REQUIRE(2 == protos.size());
REQUIRE(ALPN0 == protos[0]);
REQUIRE(ALPN1 == protos[1]);
// Check that the original was moved
REQUIRE(org.get_trust_store().empty());
REQUIRE(org.get_key_store().empty());
REQUIRE(org.get_private_key().empty());
REQUIRE(org.get_private_key_password().empty());
REQUIRE(org.get_ca_path().empty());
REQUIRE(org.get_enabled_cipher_suites().empty());
REQUIRE(org.get_alpn_protos().empty());
// Self assignment should cause no harm
// (clang++ is smart enough to warn about this)
#if !defined(__clang__)
opts = std::move(opts);
REQUIRE(TRUST_STORE == opts.get_trust_store());
REQUIRE(KEY_STORE == opts.get_key_store());
REQUIRE(PRIVATE_KEY == opts.get_private_key());
REQUIRE(PRIVATE_KEY_PASSWORD == opts.get_private_key_password());
REQUIRE(CA_PATH == opts.get_ca_path());
REQUIRE(ENABLED_CIPHER_SUITES == opts.get_enabled_cipher_suites());
REQUIRE(SERVER_CERT == opts.get_enable_server_cert_auth());
#endif
}
// ----------------------------------------------------------------------
// Test set/get of the user and password.
// ----------------------------------------------------------------------
TEST_CASE("ssl_options set user", "[options]")
{
mqtt::ssl_options opts;
opts.set_trust_store(TRUST_STORE);
opts.set_key_store(KEY_STORE);
opts.set_private_key(PRIVATE_KEY);
opts.set_private_key_password(PRIVATE_KEY_PASSWORD);
opts.set_ca_path(CA_PATH);
opts.set_enabled_cipher_suites(ENABLED_CIPHER_SUITES);
opts.set_enable_server_cert_auth(SERVER_CERT);
opts.set_alpn_protos({ALPN0, ALPN1});
REQUIRE(TRUST_STORE == opts.get_trust_store());
REQUIRE(KEY_STORE == opts.get_key_store());
REQUIRE(PRIVATE_KEY == opts.get_private_key());
REQUIRE(PRIVATE_KEY_PASSWORD == opts.get_private_key_password());
REQUIRE(CA_PATH == opts.get_ca_path());
REQUIRE(ENABLED_CIPHER_SUITES == opts.get_enabled_cipher_suites());
REQUIRE(SERVER_CERT == opts.get_enable_server_cert_auth());
auto protos = opts.get_alpn_protos();
REQUIRE(2 == protos.size());
REQUIRE(ALPN0 == protos[0]);
REQUIRE(ALPN1 == protos[1]);
}
// ----------------------------------------------------------------------
// Test if empty strings gives nullptr opts
// ----------------------------------------------------------------------
TEST_CASE("ssl_options set empty strings", "[options]")
{
mqtt::ssl_options opts{orgOpts};
opts.set_trust_store(EMPTY_STR);
opts.set_key_store(EMPTY_STR);
opts.set_private_key(EMPTY_STR);
opts.set_private_key_password(EMPTY_STR);
opts.set_ca_path(EMPTY_STR);
opts.set_enabled_cipher_suites(EMPTY_STR);
opts.set_alpn_protos({});
// Make sure the empty string represents a nullptr for C library
const auto& c_struct = opts.c_struct();
REQUIRE(!memcmp(&c_struct.struct_id, CSIG, CSIG_LEN));
REQUIRE(c_struct.trustStore == nullptr);
REQUIRE(c_struct.keyStore == nullptr);
REQUIRE(c_struct.privateKey == nullptr);
REQUIRE(c_struct.privateKeyPassword == nullptr);
REQUIRE(c_struct.CApath == nullptr);
REQUIRE(c_struct.enabledCipherSuites == nullptr);
REQUIRE(c_struct.protos == nullptr);
REQUIRE(c_struct.protos_len == 0);
}
// ----------------------------------------------------------------------
// Test if empty strings gives nullptr opts
// ----------------------------------------------------------------------
TEST_CASE("ssl_options test error handler", "[options]")
{
mqtt::ssl_options opts{orgOpts};
orgOpts.set_error_handler([](const std::string& msg) {
std::cerr << "SSL Error: " << msg << std::endl;
});
}
|