mirror of
https://github.com/eclipse-paho/paho.mqtt.cpp.git
synced 2025-09-15 12:58:39 +08:00
Converted message unit test from CppUnit to Catch2
This commit is contained in:
parent
75e4701a10
commit
5224a78d3c
@ -74,7 +74,6 @@ private:
|
||||
|
||||
/** The client has special access. */
|
||||
friend class async_client;
|
||||
friend class message_test;
|
||||
|
||||
/**
|
||||
* Set the dup flag in the underlying message
|
||||
@ -218,6 +217,12 @@ public:
|
||||
* @return A reference to this message.
|
||||
*/
|
||||
message& operator=(message&& rhs);
|
||||
/**
|
||||
* Expose the underlying C struct for the unit tests.
|
||||
*/
|
||||
#if defined(UNIT_TESTS)
|
||||
const MQTTAsync_message& c_struct() const { return msg_; }
|
||||
#endif
|
||||
/**
|
||||
* Sets the topic string.
|
||||
* @param topic The topic on which the message is published.
|
||||
|
||||
@ -1,370 +0,0 @@
|
||||
// message_test.h
|
||||
// Unit tests for the message class in the Paho MQTT C++ library.
|
||||
|
||||
/*******************************************************************************
|
||||
* Copyright (c) 2016 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 v1.0
|
||||
* and Eclipse Distribution License v1.0 which accompany this distribution.
|
||||
*
|
||||
* The Eclipse Public License is available at
|
||||
* http://www.eclipse.org/legal/epl-v10.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
|
||||
*******************************************************************************/
|
||||
|
||||
#ifndef __mqtt_message_test_h
|
||||
#define __mqtt_message_test_h
|
||||
|
||||
#include <cppunit/ui/text/TestRunner.h>
|
||||
#include <cppunit/extensions/HelperMacros.h>
|
||||
|
||||
#include "mqtt/message.h"
|
||||
#include <cstring>
|
||||
|
||||
namespace mqtt {
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
class message_test : public CppUnit::TestFixture
|
||||
{
|
||||
CPPUNIT_TEST_SUITE( message_test );
|
||||
|
||||
CPPUNIT_TEST( test_dflt_constructor );
|
||||
CPPUNIT_TEST( test_buf_len_constructor );
|
||||
CPPUNIT_TEST( test_buf_constructor );
|
||||
CPPUNIT_TEST( test_string_constructor );
|
||||
CPPUNIT_TEST( test_string_qos_constructor );
|
||||
CPPUNIT_TEST( test_c_struct_constructor );
|
||||
CPPUNIT_TEST( test_copy_constructor );
|
||||
CPPUNIT_TEST( test_move_constructor );
|
||||
CPPUNIT_TEST( test_copy_assignment );
|
||||
CPPUNIT_TEST( test_move_assignment );
|
||||
CPPUNIT_TEST( test_validate_qos );
|
||||
|
||||
CPPUNIT_TEST_SUITE_END();
|
||||
|
||||
const std::string EMPTY_STR;
|
||||
const int DFLT_QOS = 0;
|
||||
const bool DFLT_RETAINED = false;
|
||||
const bool DFLT_DUP = false;
|
||||
|
||||
const std::string TOPIC { "hello" };
|
||||
const char* BUF = "Hello there";
|
||||
const size_t N = std::strlen(BUF);
|
||||
const std::string PAYLOAD = std::string(BUF);
|
||||
const int QOS = 1;
|
||||
|
||||
mqtt::message orgMsg;
|
||||
|
||||
public:
|
||||
void setUp() {
|
||||
orgMsg = mqtt::message(TOPIC, PAYLOAD, QOS, true);
|
||||
}
|
||||
void tearDown() {}
|
||||
|
||||
// ----------------------------------------------------------------------
|
||||
// Test the default constructor
|
||||
// ----------------------------------------------------------------------
|
||||
|
||||
void test_dflt_constructor() {
|
||||
mqtt::message msg;
|
||||
CPPUNIT_ASSERT_EQUAL(EMPTY_STR, msg.get_payload_str());
|
||||
CPPUNIT_ASSERT_EQUAL(DFLT_QOS, msg.get_qos());
|
||||
CPPUNIT_ASSERT_EQUAL(DFLT_RETAINED, msg.is_retained());
|
||||
CPPUNIT_ASSERT_EQUAL(DFLT_DUP, msg.is_duplicate());
|
||||
|
||||
const auto& c_struct = msg.msg_;
|
||||
|
||||
CPPUNIT_ASSERT_EQUAL(0, c_struct.payloadlen);
|
||||
CPPUNIT_ASSERT(nullptr == c_struct.payload);
|
||||
CPPUNIT_ASSERT_EQUAL(DFLT_QOS, c_struct.qos);
|
||||
CPPUNIT_ASSERT_EQUAL(DFLT_RETAINED, c_struct.retained != 0);
|
||||
CPPUNIT_ASSERT_EQUAL(DFLT_DUP, c_struct.dup != 0);
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------
|
||||
// Test the raw buffer (void*) and length constructor
|
||||
// ----------------------------------------------------------------------
|
||||
|
||||
void test_buf_len_constructor() {
|
||||
mqtt::message msg(TOPIC, BUF, N);
|
||||
|
||||
CPPUNIT_ASSERT_EQUAL(TOPIC, msg.get_topic());
|
||||
CPPUNIT_ASSERT_EQUAL(PAYLOAD, msg.get_payload_str());
|
||||
CPPUNIT_ASSERT_EQUAL(DFLT_QOS, msg.get_qos());
|
||||
CPPUNIT_ASSERT_EQUAL(DFLT_RETAINED, msg.is_retained());
|
||||
CPPUNIT_ASSERT_EQUAL(DFLT_DUP, msg.is_duplicate());
|
||||
|
||||
const auto& c_struct = msg.msg_;
|
||||
|
||||
CPPUNIT_ASSERT_EQUAL(int(N), c_struct.payloadlen);
|
||||
CPPUNIT_ASSERT(!memcmp(BUF, c_struct.payload, N));
|
||||
CPPUNIT_ASSERT_EQUAL(DFLT_QOS, c_struct.qos);
|
||||
CPPUNIT_ASSERT_EQUAL(DFLT_RETAINED, c_struct.retained != 0);
|
||||
CPPUNIT_ASSERT_EQUAL(DFLT_DUP, c_struct.dup != 0);
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------
|
||||
// Test the raw buffer (void*) constructor
|
||||
// ----------------------------------------------------------------------
|
||||
|
||||
void test_buf_constructor() {
|
||||
mqtt::message msg(TOPIC, BUF, N, QOS, true);
|
||||
|
||||
CPPUNIT_ASSERT_EQUAL(TOPIC, msg.get_topic());
|
||||
CPPUNIT_ASSERT_EQUAL(PAYLOAD, msg.get_payload_str());
|
||||
CPPUNIT_ASSERT_EQUAL(QOS, msg.get_qos());
|
||||
CPPUNIT_ASSERT(msg.is_retained());
|
||||
CPPUNIT_ASSERT_EQUAL(DFLT_DUP, msg.is_duplicate());
|
||||
|
||||
const auto& c_struct = msg.msg_;
|
||||
|
||||
CPPUNIT_ASSERT_EQUAL(int(N), c_struct.payloadlen);
|
||||
CPPUNIT_ASSERT(!memcmp(BUF, c_struct.payload, N));
|
||||
CPPUNIT_ASSERT_EQUAL(QOS, c_struct.qos);
|
||||
CPPUNIT_ASSERT(c_struct.retained != 0);
|
||||
CPPUNIT_ASSERT_EQUAL(DFLT_DUP, c_struct.dup != 0);
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------
|
||||
// Test the string buffer constructor
|
||||
// ----------------------------------------------------------------------
|
||||
|
||||
void test_string_constructor() {
|
||||
mqtt::message msg(TOPIC, PAYLOAD);
|
||||
|
||||
CPPUNIT_ASSERT_EQUAL(TOPIC, msg.get_topic());
|
||||
CPPUNIT_ASSERT_EQUAL(PAYLOAD, msg.get_payload_str());
|
||||
CPPUNIT_ASSERT_EQUAL(DFLT_QOS, msg.get_qos());
|
||||
CPPUNIT_ASSERT(!msg.is_retained());
|
||||
CPPUNIT_ASSERT_EQUAL(DFLT_DUP, msg.is_duplicate());
|
||||
|
||||
const auto& c_struct = msg.msg_;
|
||||
|
||||
CPPUNIT_ASSERT_EQUAL(int(PAYLOAD.size()), c_struct.payloadlen);
|
||||
CPPUNIT_ASSERT(!memcmp(PAYLOAD.data(), c_struct.payload, PAYLOAD.size()));
|
||||
CPPUNIT_ASSERT_EQUAL(DFLT_QOS, c_struct.qos);
|
||||
CPPUNIT_ASSERT_EQUAL(DFLT_RETAINED, c_struct.retained != 0);
|
||||
CPPUNIT_ASSERT_EQUAL(DFLT_DUP, c_struct.dup != 0);
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------
|
||||
// Test the string buffer with QoS constructor
|
||||
// ----------------------------------------------------------------------
|
||||
|
||||
void test_string_qos_constructor() {
|
||||
mqtt::message msg(TOPIC, PAYLOAD, QOS, true);
|
||||
|
||||
CPPUNIT_ASSERT_EQUAL(TOPIC, msg.get_topic());
|
||||
CPPUNIT_ASSERT_EQUAL(PAYLOAD, msg.get_payload_str());
|
||||
CPPUNIT_ASSERT_EQUAL(QOS, msg.get_qos());
|
||||
CPPUNIT_ASSERT(msg.is_retained());
|
||||
CPPUNIT_ASSERT_EQUAL(DFLT_DUP, msg.is_duplicate());
|
||||
|
||||
const auto& c_struct = msg.msg_;
|
||||
|
||||
CPPUNIT_ASSERT_EQUAL(int(PAYLOAD.size()), c_struct.payloadlen);
|
||||
CPPUNIT_ASSERT(!memcmp(PAYLOAD.data(), c_struct.payload, PAYLOAD.size()));
|
||||
CPPUNIT_ASSERT_EQUAL(QOS, c_struct.qos);
|
||||
CPPUNIT_ASSERT(c_struct.retained != 0);
|
||||
CPPUNIT_ASSERT_EQUAL(DFLT_DUP, c_struct.dup != 0);
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------
|
||||
// Test the initialization by C struct
|
||||
// ----------------------------------------------------------------------
|
||||
|
||||
void test_c_struct_constructor() {
|
||||
MQTTAsync_message c_msg = MQTTAsync_message_initializer;
|
||||
|
||||
c_msg.payload = const_cast<char*>(BUF);
|
||||
c_msg.payloadlen = N;
|
||||
c_msg.qos = QOS;
|
||||
c_msg.retained = 1;
|
||||
c_msg.dup = 1;
|
||||
|
||||
mqtt::message msg(TOPIC, c_msg);
|
||||
|
||||
CPPUNIT_ASSERT_EQUAL(TOPIC, msg.get_topic());
|
||||
CPPUNIT_ASSERT_EQUAL(PAYLOAD, msg.get_payload_str());
|
||||
CPPUNIT_ASSERT_EQUAL(QOS, msg.get_qos());
|
||||
CPPUNIT_ASSERT(msg.is_retained());
|
||||
CPPUNIT_ASSERT(msg.is_duplicate());
|
||||
|
||||
const auto& c_struct = msg.msg_;
|
||||
|
||||
CPPUNIT_ASSERT_EQUAL(int(N), c_struct.payloadlen);
|
||||
CPPUNIT_ASSERT(!memcmp(BUF, c_struct.payload, N));
|
||||
CPPUNIT_ASSERT_EQUAL(QOS, c_struct.qos);
|
||||
CPPUNIT_ASSERT(c_struct.retained != 0);
|
||||
CPPUNIT_ASSERT(c_struct.dup != 0);
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------
|
||||
// Test the copy constructor
|
||||
// ----------------------------------------------------------------------
|
||||
|
||||
void test_copy_constructor() {
|
||||
mqtt::message msg(orgMsg);
|
||||
|
||||
CPPUNIT_ASSERT_EQUAL(TOPIC, msg.get_topic());
|
||||
CPPUNIT_ASSERT_EQUAL(PAYLOAD, msg.get_payload_str());
|
||||
CPPUNIT_ASSERT_EQUAL(QOS, msg.get_qos());
|
||||
CPPUNIT_ASSERT(msg.is_retained());
|
||||
|
||||
const auto& c_struct = msg.msg_;
|
||||
|
||||
CPPUNIT_ASSERT_EQUAL(int(PAYLOAD.size()), c_struct.payloadlen);
|
||||
CPPUNIT_ASSERT(!memcmp(PAYLOAD.data(), c_struct.payload, PAYLOAD.size()));
|
||||
CPPUNIT_ASSERT_EQUAL(QOS, c_struct.qos);
|
||||
CPPUNIT_ASSERT(c_struct.retained != 0);
|
||||
CPPUNIT_ASSERT_EQUAL(DFLT_DUP, c_struct.dup != 0);
|
||||
|
||||
// Make sure it's a true copy, not linked to the original
|
||||
orgMsg.set_payload(EMPTY_STR);
|
||||
orgMsg.set_qos(DFLT_QOS);
|
||||
orgMsg.set_retained(false);
|
||||
|
||||
CPPUNIT_ASSERT_EQUAL(PAYLOAD, msg.get_payload_str());
|
||||
CPPUNIT_ASSERT_EQUAL(QOS, msg.get_qos());
|
||||
CPPUNIT_ASSERT(msg.is_retained());
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------
|
||||
// Test the move constructor
|
||||
// ----------------------------------------------------------------------
|
||||
|
||||
void test_move_constructor() {
|
||||
mqtt::message msg(std::move(orgMsg));
|
||||
|
||||
CPPUNIT_ASSERT_EQUAL(TOPIC, msg.get_topic());
|
||||
CPPUNIT_ASSERT_EQUAL(PAYLOAD, msg.get_payload_str());
|
||||
CPPUNIT_ASSERT_EQUAL(QOS, msg.get_qos());
|
||||
CPPUNIT_ASSERT(msg.is_retained());
|
||||
|
||||
const auto& c_struct = msg.msg_;
|
||||
|
||||
CPPUNIT_ASSERT_EQUAL(int(PAYLOAD.size()), c_struct.payloadlen);
|
||||
CPPUNIT_ASSERT(!memcmp(PAYLOAD.data(), c_struct.payload, PAYLOAD.size()));
|
||||
CPPUNIT_ASSERT_EQUAL(QOS, c_struct.qos);
|
||||
CPPUNIT_ASSERT(c_struct.retained != 0);
|
||||
CPPUNIT_ASSERT_EQUAL(DFLT_DUP, c_struct.dup != 0);
|
||||
|
||||
// Check that the original was moved
|
||||
CPPUNIT_ASSERT_EQUAL(size_t(0), orgMsg.get_payload().size());
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------
|
||||
// Test the copy assignment operator=(const&)
|
||||
// ----------------------------------------------------------------------
|
||||
|
||||
void test_copy_assignment() {
|
||||
mqtt::message msg;
|
||||
|
||||
msg = orgMsg;
|
||||
|
||||
CPPUNIT_ASSERT_EQUAL(TOPIC, msg.get_topic());
|
||||
CPPUNIT_ASSERT_EQUAL(PAYLOAD, msg.get_payload_str());
|
||||
CPPUNIT_ASSERT_EQUAL(QOS, msg.get_qos());
|
||||
CPPUNIT_ASSERT(msg.is_retained());
|
||||
|
||||
const auto& c_struct = msg.msg_;
|
||||
|
||||
CPPUNIT_ASSERT_EQUAL(int(PAYLOAD.size()), c_struct.payloadlen);
|
||||
CPPUNIT_ASSERT(!memcmp(PAYLOAD.data(), c_struct.payload, PAYLOAD.size()));
|
||||
CPPUNIT_ASSERT_EQUAL(QOS, c_struct.qos);
|
||||
CPPUNIT_ASSERT(c_struct.retained != 0);
|
||||
CPPUNIT_ASSERT_EQUAL(DFLT_DUP, c_struct.dup != 0);
|
||||
|
||||
// Make sure it's a true copy, not linked to the original
|
||||
orgMsg.set_payload(EMPTY_STR);
|
||||
orgMsg.set_qos(DFLT_QOS);
|
||||
orgMsg.set_retained(false);
|
||||
|
||||
CPPUNIT_ASSERT_EQUAL(TOPIC, msg.get_topic());
|
||||
CPPUNIT_ASSERT_EQUAL(PAYLOAD, msg.get_payload_str());
|
||||
CPPUNIT_ASSERT_EQUAL(QOS, msg.get_qos());
|
||||
CPPUNIT_ASSERT(msg.is_retained());
|
||||
|
||||
// Self assignment should cause no harm
|
||||
msg = msg;
|
||||
|
||||
CPPUNIT_ASSERT_EQUAL(PAYLOAD, msg.get_payload_str());
|
||||
CPPUNIT_ASSERT_EQUAL(QOS, msg.get_qos());
|
||||
CPPUNIT_ASSERT(msg.is_retained());
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------
|
||||
// Test the move assignment, operator=(&&)
|
||||
// ----------------------------------------------------------------------
|
||||
|
||||
void test_move_assignment() {
|
||||
mqtt::message msg;
|
||||
msg = std::move(orgMsg);
|
||||
|
||||
CPPUNIT_ASSERT_EQUAL(TOPIC, msg.get_topic());
|
||||
CPPUNIT_ASSERT_EQUAL(PAYLOAD, msg.get_payload_str());
|
||||
CPPUNIT_ASSERT_EQUAL(QOS, msg.get_qos());
|
||||
CPPUNIT_ASSERT(msg.is_retained());
|
||||
|
||||
const auto& c_struct = msg.msg_;
|
||||
|
||||
CPPUNIT_ASSERT_EQUAL(int(PAYLOAD.size()), c_struct.payloadlen);
|
||||
CPPUNIT_ASSERT(!memcmp(PAYLOAD.data(), c_struct.payload, PAYLOAD.size()));
|
||||
CPPUNIT_ASSERT_EQUAL(QOS, c_struct.qos);
|
||||
CPPUNIT_ASSERT(c_struct.retained != 0);
|
||||
CPPUNIT_ASSERT_EQUAL(DFLT_DUP, c_struct.dup != 0);
|
||||
|
||||
// Check that the original was moved
|
||||
CPPUNIT_ASSERT_EQUAL(size_t(0), orgMsg.get_payload().size());
|
||||
|
||||
// Self assignment should cause no harm
|
||||
// (clang++ is smart enough to warn about this)
|
||||
#if !defined(__clang__)
|
||||
msg = std::move(msg);
|
||||
CPPUNIT_ASSERT_EQUAL(PAYLOAD, msg.get_payload_str());
|
||||
CPPUNIT_ASSERT_EQUAL(QOS, msg.get_qos());
|
||||
CPPUNIT_ASSERT(msg.is_retained());
|
||||
#endif
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------
|
||||
// Test the validate_qos()
|
||||
// ----------------------------------------------------------------------
|
||||
|
||||
void test_validate_qos() {
|
||||
try {
|
||||
mqtt::message::validate_qos(-1);
|
||||
CPPUNIT_FAIL("message shouldn't accept QoS <min");
|
||||
}
|
||||
catch (const mqtt::exception& ex) {}
|
||||
|
||||
for (int i=0; i<=2; ++i) {
|
||||
try {
|
||||
mqtt::message::validate_qos(0);
|
||||
}
|
||||
catch (...) {
|
||||
CPPUNIT_FAIL("mesage should accept valid QoS: "+std::to_string(i));
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
mqtt::message::validate_qos(3);
|
||||
CPPUNIT_FAIL("message shouldn't accept QoS >max");
|
||||
}
|
||||
catch (const mqtt::exception& ex) {}
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////////
|
||||
// end namespace mqtt
|
||||
}
|
||||
|
||||
#endif // __mqtt_message_test_h
|
||||
|
||||
@ -21,7 +21,6 @@
|
||||
#include "async_client_test.h"
|
||||
#include "async_client_v3_test.h"
|
||||
#include "client_test.h"
|
||||
#include "message_test.h"
|
||||
#include "will_options_test.h"
|
||||
#include "ssl_options_test.h"
|
||||
#include "connect_options_test.h"
|
||||
@ -49,7 +48,6 @@ int main(int argc, char* argv[])
|
||||
CPPUNIT_TEST_SUITE_REGISTRATION( mqtt::disconnect_options_test );
|
||||
CPPUNIT_TEST_SUITE_REGISTRATION( mqtt::response_options_test );
|
||||
|
||||
CPPUNIT_TEST_SUITE_REGISTRATION( mqtt::message_test );
|
||||
CPPUNIT_TEST_SUITE_REGISTRATION( mqtt::delivery_response_options_test );
|
||||
CPPUNIT_TEST_SUITE_REGISTRATION( mqtt::iclient_persistence_test );
|
||||
CPPUNIT_TEST_SUITE_REGISTRATION( mqtt::token_test );
|
||||
|
||||
@ -29,7 +29,8 @@ find_package(Catch2 REQUIRED)
|
||||
# --- Executables ---
|
||||
|
||||
add_executable(unit_tests unit_tests.cpp
|
||||
test_properties.cpp
|
||||
test_message.cpp
|
||||
test_properties.cpp
|
||||
test_string_collection.cpp
|
||||
test_topic.cpp
|
||||
)
|
||||
|
||||
336
test/unit/test_message.cpp
Normal file
336
test/unit/test_message.cpp
Normal file
@ -0,0 +1,336 @@
|
||||
// message_test.cpp
|
||||
//
|
||||
// Unit tests for the message 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 v1.0
|
||||
* and Eclipse Distribution License v1.0 which accompany this distribution.
|
||||
*
|
||||
* The Eclipse Public License is available at
|
||||
* http://www.eclipse.org/legal/epl-v10.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
|
||||
*******************************************************************************/
|
||||
|
||||
#define UNIT_TESTS
|
||||
|
||||
#include <cstring>
|
||||
#include "catch2/catch.hpp"
|
||||
#include "mqtt/message.h"
|
||||
|
||||
using namespace mqtt;
|
||||
|
||||
static const std::string EMPTY_STR;
|
||||
static const int DFLT_QOS = 0;
|
||||
static const bool DFLT_RETAINED = false;
|
||||
static const bool DFLT_DUP = false;
|
||||
|
||||
static const std::string TOPIC { "hello" };
|
||||
static const char* BUF = "Hello there";
|
||||
static const size_t N = std::strlen(BUF);
|
||||
static const std::string PAYLOAD = std::string(BUF);
|
||||
static const int QOS = 1;
|
||||
|
||||
// --------------------------------------------------------------------------
|
||||
// Test the default constructor
|
||||
// --------------------------------------------------------------------------
|
||||
|
||||
TEST_CASE("default constructor", "[message]")
|
||||
{
|
||||
mqtt::message msg;
|
||||
|
||||
REQUIRE(EMPTY_STR == msg.get_payload_str());
|
||||
REQUIRE(DFLT_QOS == msg.get_qos());
|
||||
REQUIRE(DFLT_RETAINED == msg.is_retained());
|
||||
REQUIRE(DFLT_DUP == msg.is_duplicate());
|
||||
|
||||
const auto& c_struct = msg.c_struct();
|
||||
|
||||
REQUIRE(0 == c_struct.payloadlen);
|
||||
REQUIRE(nullptr == c_struct.payload);
|
||||
REQUIRE(DFLT_QOS == c_struct.qos);
|
||||
REQUIRE(DFLT_RETAINED == (c_struct.retained != 0));
|
||||
REQUIRE(DFLT_DUP == (c_struct.dup != 0));
|
||||
}
|
||||
|
||||
// --------------------------------------------------------------------------
|
||||
// Test the raw buffer (void*) and length constructor
|
||||
// --------------------------------------------------------------------------
|
||||
|
||||
TEST_CASE("buf len constructor", "[message]")
|
||||
{
|
||||
mqtt::message msg(TOPIC, BUF, N);
|
||||
|
||||
REQUIRE(TOPIC == msg.get_topic());
|
||||
REQUIRE(PAYLOAD == msg.get_payload_str());
|
||||
REQUIRE(DFLT_QOS == msg.get_qos());
|
||||
REQUIRE(DFLT_RETAINED == msg.is_retained());
|
||||
REQUIRE(DFLT_DUP == msg.is_duplicate());
|
||||
|
||||
const auto& c_struct = msg.c_struct();
|
||||
|
||||
REQUIRE(int(N) == c_struct.payloadlen);
|
||||
REQUIRE(0 == memcmp(BUF, c_struct.payload, N));
|
||||
REQUIRE(DFLT_QOS == c_struct.qos);
|
||||
REQUIRE(DFLT_RETAINED == (c_struct.retained != 0));
|
||||
REQUIRE(DFLT_DUP == (c_struct.dup != 0));
|
||||
}
|
||||
|
||||
// --------------------------------------------------------------------------
|
||||
// Test the raw buffer (void*) constructor
|
||||
// --------------------------------------------------------------------------
|
||||
|
||||
TEST_CASE("buf constructor", "[message]")
|
||||
{
|
||||
mqtt::message msg(TOPIC, BUF, N, QOS, true);
|
||||
|
||||
REQUIRE(TOPIC == msg.get_topic());
|
||||
REQUIRE(PAYLOAD == msg.get_payload_str());
|
||||
REQUIRE(QOS == msg.get_qos());
|
||||
REQUIRE(msg.is_retained());
|
||||
REQUIRE(DFLT_DUP == msg.is_duplicate());
|
||||
|
||||
const auto& c_struct = msg.c_struct();
|
||||
|
||||
REQUIRE(int(N) == c_struct.payloadlen);
|
||||
REQUIRE(0 == memcmp(BUF, c_struct.payload, N));
|
||||
REQUIRE(QOS == c_struct.qos);
|
||||
REQUIRE(c_struct.retained != 0);
|
||||
REQUIRE(DFLT_DUP == (c_struct.dup != 0));
|
||||
}
|
||||
|
||||
// --------------------------------------------------------------------------
|
||||
// Test the string buffer constructor
|
||||
// --------------------------------------------------------------------------
|
||||
|
||||
TEST_CASE("string constructor", "[message]")
|
||||
{
|
||||
mqtt::message msg(TOPIC, PAYLOAD);
|
||||
|
||||
REQUIRE(TOPIC == msg.get_topic());
|
||||
REQUIRE(PAYLOAD == msg.get_payload_str());
|
||||
REQUIRE(DFLT_QOS == msg.get_qos());
|
||||
REQUIRE(!msg.is_retained());
|
||||
REQUIRE(DFLT_DUP == msg.is_duplicate());
|
||||
|
||||
const auto& c_struct = msg.c_struct();
|
||||
|
||||
REQUIRE(int(PAYLOAD.size()) == c_struct.payloadlen);
|
||||
REQUIRE(0 == memcmp(PAYLOAD.data(), c_struct.payload, PAYLOAD.size()));
|
||||
REQUIRE(DFLT_QOS == c_struct.qos);
|
||||
REQUIRE(DFLT_RETAINED == (c_struct.retained != 0));
|
||||
REQUIRE(DFLT_DUP == (c_struct.dup != 0));
|
||||
}
|
||||
|
||||
// --------------------------------------------------------------------------
|
||||
// Test the string buffer with QoS constructor
|
||||
// --------------------------------------------------------------------------
|
||||
|
||||
TEST_CASE("string qos constructor", "[message]")
|
||||
{
|
||||
mqtt::message msg(TOPIC, PAYLOAD, QOS, true);
|
||||
|
||||
REQUIRE(TOPIC == msg.get_topic());
|
||||
REQUIRE(PAYLOAD == msg.get_payload_str());
|
||||
REQUIRE(QOS == msg.get_qos());
|
||||
REQUIRE(msg.is_retained());
|
||||
REQUIRE(DFLT_DUP == msg.is_duplicate());
|
||||
|
||||
const auto& c_struct = msg.c_struct();
|
||||
|
||||
REQUIRE(int(PAYLOAD.size()) == c_struct.payloadlen);
|
||||
REQUIRE(0 == memcmp(PAYLOAD.data(), c_struct.payload, PAYLOAD.size()));
|
||||
REQUIRE(QOS == c_struct.qos);
|
||||
REQUIRE(c_struct.retained != 0);
|
||||
REQUIRE(DFLT_DUP == (c_struct.dup != 0));
|
||||
}
|
||||
|
||||
// --------------------------------------------------------------------------
|
||||
// Test the initialization by C struct
|
||||
// --------------------------------------------------------------------------
|
||||
|
||||
TEST_CASE("c struct constructor", "[message]")
|
||||
{
|
||||
MQTTAsync_message c_msg = MQTTAsync_message_initializer;
|
||||
|
||||
c_msg.payload = const_cast<char*>(BUF);
|
||||
c_msg.payloadlen = N;
|
||||
c_msg.qos = QOS;
|
||||
c_msg.retained = 1;
|
||||
c_msg.dup = 1;
|
||||
|
||||
mqtt::message msg(TOPIC, c_msg);
|
||||
|
||||
REQUIRE(TOPIC == msg.get_topic());
|
||||
REQUIRE(PAYLOAD == msg.get_payload_str());
|
||||
REQUIRE(QOS == msg.get_qos());
|
||||
REQUIRE(msg.is_retained());
|
||||
REQUIRE(msg.is_duplicate());
|
||||
|
||||
const auto& c_struct = msg.c_struct();
|
||||
|
||||
REQUIRE(int(N) == c_struct.payloadlen);
|
||||
REQUIRE(0 == memcmp(BUF, c_struct.payload, N));
|
||||
REQUIRE(QOS == c_struct.qos);
|
||||
REQUIRE(c_struct.retained != 0);
|
||||
REQUIRE(c_struct.dup != 0);
|
||||
}
|
||||
|
||||
// --------------------------------------------------------------------------
|
||||
// Test the copy constructor
|
||||
// --------------------------------------------------------------------------
|
||||
|
||||
TEST_CASE("copy constructor", "[message]")
|
||||
{
|
||||
auto orgMsg = mqtt::message(TOPIC, PAYLOAD, QOS, true);
|
||||
|
||||
mqtt::message msg(orgMsg);
|
||||
|
||||
REQUIRE(TOPIC == msg.get_topic());
|
||||
REQUIRE(PAYLOAD == msg.get_payload_str());
|
||||
REQUIRE(QOS == msg.get_qos());
|
||||
REQUIRE(msg.is_retained());
|
||||
|
||||
const auto& c_struct = msg.c_struct();
|
||||
|
||||
REQUIRE(int(PAYLOAD.size()) == c_struct.payloadlen);
|
||||
REQUIRE(0 == memcmp(PAYLOAD.data(), c_struct.payload, PAYLOAD.size()));
|
||||
REQUIRE(QOS == c_struct.qos);
|
||||
REQUIRE(c_struct.retained != 0);
|
||||
REQUIRE(DFLT_DUP == (c_struct.dup != 0));
|
||||
|
||||
// Make sure it's a true copy, not linked to the original
|
||||
orgMsg.set_payload(EMPTY_STR);
|
||||
orgMsg.set_qos(DFLT_QOS);
|
||||
orgMsg.set_retained(false);
|
||||
|
||||
REQUIRE(PAYLOAD == msg.get_payload_str());
|
||||
REQUIRE(QOS == msg.get_qos());
|
||||
REQUIRE(msg.is_retained());
|
||||
}
|
||||
|
||||
// --------------------------------------------------------------------------
|
||||
// Test the move constructor
|
||||
// --------------------------------------------------------------------------
|
||||
|
||||
TEST_CASE("move constructor", "[message]")
|
||||
{
|
||||
auto orgMsg = mqtt::message(TOPIC, PAYLOAD, QOS, true);
|
||||
|
||||
mqtt::message msg(std::move(orgMsg));
|
||||
|
||||
REQUIRE(TOPIC == msg.get_topic());
|
||||
REQUIRE(PAYLOAD == msg.get_payload_str());
|
||||
REQUIRE(QOS == msg.get_qos());
|
||||
REQUIRE(msg.is_retained());
|
||||
|
||||
const auto& c_struct = msg.c_struct();
|
||||
|
||||
REQUIRE(int(PAYLOAD.size()) == c_struct.payloadlen);
|
||||
REQUIRE(0 == memcmp(PAYLOAD.data(), c_struct.payload, PAYLOAD.size()));
|
||||
REQUIRE(QOS == c_struct.qos);
|
||||
REQUIRE(c_struct.retained != 0);
|
||||
REQUIRE(DFLT_DUP == (c_struct.dup != 0));
|
||||
|
||||
// Check that the original was moved
|
||||
REQUIRE(size_t(0) == orgMsg.get_payload().size());
|
||||
}
|
||||
|
||||
// --------------------------------------------------------------------------
|
||||
// Test the copy assignment operator=(const&)
|
||||
// --------------------------------------------------------------------------
|
||||
|
||||
TEST_CASE("copy assignment", "[message]")
|
||||
{
|
||||
auto orgMsg = mqtt::message(TOPIC, PAYLOAD, QOS, true);
|
||||
|
||||
mqtt::message msg;
|
||||
msg = orgMsg;
|
||||
|
||||
REQUIRE(TOPIC == msg.get_topic());
|
||||
REQUIRE(PAYLOAD == msg.get_payload_str());
|
||||
REQUIRE(QOS == msg.get_qos());
|
||||
REQUIRE(msg.is_retained());
|
||||
|
||||
const auto& c_struct = msg.c_struct();
|
||||
|
||||
REQUIRE(int(PAYLOAD.size()) == c_struct.payloadlen);
|
||||
REQUIRE(0 == memcmp(PAYLOAD.data(), c_struct.payload, PAYLOAD.size()));
|
||||
REQUIRE(QOS == c_struct.qos);
|
||||
REQUIRE(c_struct.retained != 0);
|
||||
REQUIRE(DFLT_DUP == (c_struct.dup != 0));
|
||||
|
||||
// Make sure it's a true copy, not linked to the original
|
||||
orgMsg.set_payload(EMPTY_STR);
|
||||
orgMsg.set_qos(DFLT_QOS);
|
||||
orgMsg.set_retained(false);
|
||||
|
||||
REQUIRE(TOPIC == msg.get_topic());
|
||||
REQUIRE(PAYLOAD == msg.get_payload_str());
|
||||
REQUIRE(QOS == msg.get_qos());
|
||||
REQUIRE(msg.is_retained());
|
||||
|
||||
// Self assignment should cause no harm
|
||||
msg = msg;
|
||||
|
||||
REQUIRE(PAYLOAD == msg.get_payload_str());
|
||||
REQUIRE(QOS == msg.get_qos());
|
||||
REQUIRE(msg.is_retained());
|
||||
}
|
||||
|
||||
// --------------------------------------------------------------------------
|
||||
// Test the move assignment, operator=(&&)
|
||||
// --------------------------------------------------------------------------
|
||||
|
||||
TEST_CASE("move assignment", "[message]")
|
||||
{
|
||||
auto orgMsg = mqtt::message(TOPIC, PAYLOAD, QOS, true);
|
||||
|
||||
mqtt::message msg;
|
||||
msg = std::move(orgMsg);
|
||||
|
||||
REQUIRE(TOPIC == msg.get_topic());
|
||||
REQUIRE(PAYLOAD == msg.get_payload_str());
|
||||
REQUIRE(QOS == msg.get_qos());
|
||||
REQUIRE(msg.is_retained());
|
||||
|
||||
const auto& c_struct = msg.c_struct();
|
||||
|
||||
REQUIRE(int(PAYLOAD.size()) == c_struct.payloadlen);
|
||||
REQUIRE(0 == memcmp(PAYLOAD.data(), c_struct.payload, PAYLOAD.size()));
|
||||
REQUIRE(QOS == c_struct.qos);
|
||||
REQUIRE(c_struct.retained != 0);
|
||||
REQUIRE(DFLT_DUP == (c_struct.dup != 0));
|
||||
|
||||
// Check that the original was moved
|
||||
REQUIRE(size_t(0) == orgMsg.get_payload().size());
|
||||
|
||||
// Self assignment should cause no harm
|
||||
// (clang++ is smart enough to warn about this)
|
||||
#if !defined(__clang__)
|
||||
msg = std::move(msg);
|
||||
REQUIRE(PAYLOAD == msg.get_payload_str());
|
||||
REQUIRE(QOS == msg.get_qos());
|
||||
REQUIRE(msg.is_retained());
|
||||
#endif
|
||||
}
|
||||
|
||||
// --------------------------------------------------------------------------
|
||||
// Test the validate_qos()
|
||||
// --------------------------------------------------------------------------
|
||||
|
||||
TEST_CASE("validate qos", "[message]")
|
||||
{
|
||||
REQUIRE_THROWS_AS(mqtt::message::validate_qos(-1), mqtt::exception);
|
||||
REQUIRE_THROWS_AS(mqtt::message::validate_qos(3), mqtt::exception);
|
||||
|
||||
REQUIRE_NOTHROW(mqtt::message::validate_qos(0));
|
||||
}
|
||||
|
||||
@ -20,6 +20,8 @@
|
||||
* Frank Pagliughi - initial implementation and documentation
|
||||
*******************************************************************************/
|
||||
|
||||
#define UNIT_TESTS
|
||||
|
||||
#include <vector>
|
||||
#include <cstring>
|
||||
#include "catch2/catch.hpp"
|
||||
@ -27,8 +29,8 @@
|
||||
|
||||
using namespace mqtt;
|
||||
|
||||
const string STR { "Some random string" };
|
||||
const std::vector<string> VEC { "test0", "test1", "test2" };
|
||||
static const string STR { "Some random string" };
|
||||
static const std::vector<string> VEC { "test0", "test1", "test2" };
|
||||
|
||||
// ----------------------------------------------------------------------
|
||||
// Test the default constructor
|
||||
|
||||
@ -17,6 +17,8 @@
|
||||
*
|
||||
*******************************************************************************/
|
||||
|
||||
#define UNIT_TESTS
|
||||
|
||||
#include <algorithm>
|
||||
#include <memory>
|
||||
#include <stdexcept>
|
||||
@ -28,21 +30,21 @@ using namespace mqtt;
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
const int DFLT_QOS = message::DFLT_QOS;
|
||||
const bool DFLT_RETAINED = message::DFLT_RETAINED;
|
||||
static const int DFLT_QOS = message::DFLT_QOS;
|
||||
static const bool DFLT_RETAINED = message::DFLT_RETAINED;
|
||||
|
||||
const std::string TOPIC { "my/topic/name" };
|
||||
const int QOS = 1;
|
||||
const bool RETAINED = true;
|
||||
static const std::string TOPIC { "my/topic/name" };
|
||||
static const int QOS = 1;
|
||||
static const bool RETAINED = true;
|
||||
|
||||
const int BAD_LOW_QOS = -1;
|
||||
const int BAD_HIGH_QOS = 3;
|
||||
static const int BAD_LOW_QOS = -1;
|
||||
static const int BAD_HIGH_QOS = 3;
|
||||
|
||||
const char* BUF = "Hello there";
|
||||
const size_t N = std::strlen(BUF);
|
||||
const binary PAYLOAD { BUF };
|
||||
static const char* BUF = "Hello there";
|
||||
static const size_t N = std::strlen(BUF);
|
||||
static const binary PAYLOAD { BUF };
|
||||
|
||||
mqtt::test::mock_async_client cli;
|
||||
static mqtt::test::mock_async_client cli;
|
||||
|
||||
// ----------------------------------------------------------------------
|
||||
// Constructors
|
||||
|
||||
Loading…
Reference in New Issue
Block a user