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
|
// disconnect_options_test.h
// Unit tests for the disconnect_options class in the Paho MQTT C++ library.
/*******************************************************************************
* Copyright (c) 2016 Guilherme M. Ferreira <guilherme.maciel.ferreira@gmail.com>
* Copyright (c) 2016-2023 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:
* Guilherme M. Ferreira - initial implementation and documentation
*******************************************************************************/
#define UNIT_TESTS
#include <cstring>
#include "catch2_version.h"
#include "mock_async_client.h"
#include "mqtt/disconnect_options.h"
using namespace mqtt;
/////////////////////////////////////////////////////////////////////////////
static const int DFLT_TIMEOUT = 0;
static const std::string EMPTY_STR;
static constexpr token::Type TOKEN_TYPE = token::Type::DISCONNECT;
static mock_async_client cli;
// ----------------------------------------------------------------------
// Test default constructor
// ----------------------------------------------------------------------
TEST_CASE("disconnect_options dflt constructor", "[options]")
{
disconnect_options opts;
REQUIRE(DFLT_TIMEOUT == (int)opts.get_timeout().count());
REQUIRE(!opts.get_token());
const auto& c_struct = opts.c_struct();
REQUIRE(nullptr == c_struct.onSuccess);
REQUIRE(nullptr == c_struct.onFailure);
REQUIRE(DFLT_TIMEOUT == c_struct.timeout);
}
// ----------------------------------------------------------------------
// Test user constructor
// ----------------------------------------------------------------------
TEST_CASE("disconnect_options user constructor", "[options]")
{
const int TIMEOUT = 10;
auto tok = token::create(TOKEN_TYPE, cli);
disconnect_options opts{TIMEOUT};
opts.set_token(tok, MQTTVERSION_DEFAULT);
const auto& c_struct = opts.c_struct();
REQUIRE(nullptr != c_struct.onSuccess);
REQUIRE(nullptr != c_struct.onFailure);
REQUIRE(TIMEOUT == (int)opts.get_timeout().count());
REQUIRE(tok == opts.get_token());
}
// ----------------------------------------------------------------------
// Test the copy constructor
// ----------------------------------------------------------------------
TEST_CASE("disconnect_options copy ctor", "[options]")
{
constexpr std::chrono::milliseconds TIMEOUT{50};
disconnect_options orgOpts{TIMEOUT};
SECTION("simple options")
{
disconnect_options opts{orgOpts};
REQUIRE(TIMEOUT == opts.get_timeout());
REQUIRE(opts.get_properties().empty());
// Make sure it's a true copy, not linked to the original
orgOpts.set_timeout(0);
REQUIRE(TIMEOUT == opts.get_timeout());
}
SECTION("properties")
{
orgOpts.set_properties({{property::SESSION_EXPIRY_INTERVAL, 42}});
disconnect_options opts{orgOpts};
const auto& copts = opts.c_struct();
const auto& orgCopts = orgOpts.c_struct();
// Make sure we copied the properties
REQUIRE(orgCopts.properties.array != copts.properties.array);
orgOpts.get_properties().clear();
// Check that the properties transferred over
REQUIRE(1 == opts.get_properties().size());
REQUIRE(opts.get_properties().contains(property::SESSION_EXPIRY_INTERVAL));
REQUIRE(
42 == get<uint32_t>(opts.get_properties(), property::SESSION_EXPIRY_INTERVAL)
);
REQUIRE(1 == opts.c_struct().properties.count);
REQUIRE(
1 == MQTTProperties_propertyCount(
const_cast<MQTTProperties*>(&copts.properties),
MQTTPROPERTY_CODE_SESSION_EXPIRY_INTERVAL
)
);
REQUIRE(
42 == MQTTProperties_getNumericValue(
const_cast<MQTTProperties*>(&copts.properties),
MQTTPROPERTY_CODE_SESSION_EXPIRY_INTERVAL
)
);
}
}
// ----------------------------------------------------------------------
// Test the move constructor
// ----------------------------------------------------------------------
TEST_CASE("disconnect_options move_constructor", "[options]")
{
constexpr std::chrono::milliseconds TIMEOUT{50};
disconnect_options orgOpts{TIMEOUT};
SECTION("simple options")
{
disconnect_options opts{std::move(orgOpts)};
REQUIRE(TIMEOUT == opts.get_timeout());
REQUIRE(opts.get_properties().empty());
}
SECTION("properties")
{
orgOpts.set_properties({{property::SESSION_EXPIRY_INTERVAL, 42}});
disconnect_options opts{std::move(orgOpts)};
const auto& copts = opts.c_struct();
REQUIRE(1 == opts.get_properties().size());
REQUIRE(opts.get_properties().contains(property::SESSION_EXPIRY_INTERVAL));
REQUIRE(1 == copts.properties.count);
// Check that the original was moved
REQUIRE(orgOpts.get_properties().empty());
}
}
// ----------------------------------------------------------------------
// Test set timeout
// ----------------------------------------------------------------------
TEST_CASE("disconnect_options set timeout", "[options]")
{
disconnect_options opts;
const auto& c_struct = opts.c_struct();
const int TIMEOUT = 5000; // ms
// Set with integer
opts.set_timeout(TIMEOUT);
REQUIRE(TIMEOUT == (int)opts.get_timeout().count());
REQUIRE(TIMEOUT == c_struct.timeout);
// Set with chrono duration
opts.set_timeout(std::chrono::seconds(2 * TIMEOUT / 1000));
REQUIRE(2 * TIMEOUT == (int)opts.get_timeout().count());
REQUIRE(2 * TIMEOUT == c_struct.timeout);
}
// ----------------------------------------------------------------------
// Test set connect token
// ----------------------------------------------------------------------
TEST_CASE("disconnect_options set token", "[options]")
{
auto tok = token::create(TOKEN_TYPE, cli);
disconnect_options opts;
const auto& c_struct = opts.c_struct();
REQUIRE(nullptr == c_struct.onSuccess);
REQUIRE(nullptr == c_struct.onFailure);
opts.set_token(token_ptr(), MQTTVERSION_DEFAULT);
REQUIRE(nullptr == c_struct.onSuccess);
REQUIRE(nullptr == c_struct.onFailure);
opts.set_token(tok, MQTTVERSION_DEFAULT);
REQUIRE(nullptr != c_struct.onSuccess);
REQUIRE(nullptr != c_struct.onFailure);
REQUIRE(tok == opts.get_token());
}
|