diff --git a/.github/build-arduino.sh b/.github/build-arduino.sh index 2ec48e3..551fe8b 100644 --- a/.github/build-arduino.sh +++ b/.github/build-arduino.sh @@ -17,7 +17,7 @@ arduino-cli config set library.enable_unsafe_install true arduino-cli core update-index #arduino-cli core search # Install Arduino cores -#arduino-cli core install arduino:avr +arduino-cli core install arduino:avr arduino-cli core install esp8266:esp8266 arduino-cli core install esp32:esp32 #arduino-cli board listall @@ -27,6 +27,15 @@ arduino-cli lib install --git-url https://github.com/ennui2342/arduino-sram # Link the project to the Arduino library ln -s $GITHUB_WORKSPACE $HOME/Arduino/libraries/CI_Test_Library +# Compile all *.ino files for the Arduino +for f in **/*.ino ; do + if [[ "$f" != *mqtt_esp8266.ino && "$f" != *mqtt_large_message.ino ]]; then + echo "################################################################" + echo "Arduino Uno compiling file ${f}" + arduino-cli compile -b arduino:avr:uno $f + fi +done + # Compile all *.ino files for the ESP32 for f in **/*.ino ; do if [[ "$f" != *"mqtt_stream.ino" ]]; then diff --git a/.github/clang-lint.sh b/.github/clang-lint.sh index 3be3e66..2018fd2 100755 --- a/.github/clang-lint.sh +++ b/.github/clang-lint.sh @@ -12,10 +12,10 @@ cd $GITHUB_WORKSPACE sudo apt-get -y install clang-format-19 # Check clang-format output for f in **/*.{h,c,hpp,cpp,ino} ; do - if [ -f "$f" ] && [[ "$f" != "tests/"* ]]; then + #if [ -f "$f" ] && [[ "$f" != "tests/"* ]]; then + if [ -f "$f" ]; then echo "################################################################" echo "Checking file ${f}" diff $f <(clang-format-19 -assume-filename=main.cpp $f) 1>&2 - echo -e "################################################################\n" fi done diff --git a/examples/mqtt_auth/mqtt_auth.ino b/examples/mqtt_auth/mqtt_auth.ino index 1b273f3..cdeb795 100755 --- a/examples/mqtt_auth/mqtt_auth.ino +++ b/examples/mqtt_auth/mqtt_auth.ino @@ -16,7 +16,7 @@ byte mac[] = {0xDE, 0xED, 0xBA, 0xFE, 0xFE, 0xED}; IPAddress ip(172, 16, 0, 100); IPAddress server(172, 16, 0, 2); -void callback(char* topic, byte* payload, unsigned int length) { +void callback(char* topic, uint8_t* payload, size_t length) { // handle message arrived } diff --git a/examples/mqtt_basic/mqtt_basic.ino b/examples/mqtt_basic/mqtt_basic.ino index 303ad43..f790aab 100755 --- a/examples/mqtt_basic/mqtt_basic.ino +++ b/examples/mqtt_basic/mqtt_basic.ino @@ -21,11 +21,11 @@ byte mac[] = {0xDE, 0xED, 0xBA, 0xFE, 0xFE, 0xED}; IPAddress ip(172, 16, 0, 100); IPAddress server(172, 16, 0, 2); -void callback(char* topic, byte* payload, unsigned int length) { +void callback(char* topic, uint8_t* payload, size_t length) { Serial.print("Message arrived ["); Serial.print(topic); Serial.print("] "); - for (int i = 0; i < length; i++) { + for (size_t i = 0; i < length; i++) { Serial.print((char)payload[i]); } Serial.println(); diff --git a/examples/mqtt_esp8266/mqtt_esp8266.ino b/examples/mqtt_esp8266/mqtt_esp8266.ino index 0f66326..b41192a 100644 --- a/examples/mqtt_esp8266/mqtt_esp8266.ino +++ b/examples/mqtt_esp8266/mqtt_esp8266.ino @@ -66,11 +66,11 @@ void setup_wifi() { Serial.println(WiFi.localIP()); } -void callback(char* topic, byte* payload, unsigned int length) { +void callback(char* topic, uint8_t* payload, size_t length) { Serial.print("Message arrived ["); Serial.print(topic); Serial.print("] "); - for (int i = 0; i < length; i++) { + for (size_t i = 0; i < length; i++) { Serial.print((char)payload[i]); } Serial.println(); diff --git a/examples/mqtt_large_message/mqtt_large_message.ino b/examples/mqtt_large_message/mqtt_large_message.ino index 66a781f..48049c3 100644 --- a/examples/mqtt_large_message/mqtt_large_message.ino +++ b/examples/mqtt_large_message/mqtt_large_message.ino @@ -69,11 +69,11 @@ void setup_wifi() { Serial.println(WiFi.localIP()); } -void callback(char* topic, byte* payload, unsigned int length) { +void callback(char* topic, uint8_t* payload, size_t length) { Serial.print("Message arrived ["); Serial.print(topic); Serial.print("] "); - for (int i = 0; i < length; i++) { + for (size_t i = 0; i < length; i++) { Serial.print((char)payload[i]); } Serial.println(); @@ -90,8 +90,8 @@ void callback(char* topic, byte* payload, unsigned int length) { if (bottleCount > 0) { // Work out how big our resulting message will be - int msgLen = 0; - for (int i = bottleCount; i > 0; i--) { + size_t msgLen = 0; + for (size_t i = bottleCount; i > 0; i--) { String numBottles(i); msgLen += 2 * numBottles.length(); if (i == 1) { diff --git a/examples/mqtt_publish_in_callback/mqtt_publish_in_callback.ino b/examples/mqtt_publish_in_callback/mqtt_publish_in_callback.ino index 9cb81b1..733bea3 100644 --- a/examples/mqtt_publish_in_callback/mqtt_publish_in_callback.ino +++ b/examples/mqtt_publish_in_callback/mqtt_publish_in_callback.ino @@ -23,13 +23,13 @@ IPAddress ip(172, 16, 0, 100); IPAddress server(172, 16, 0, 2); // Callback function header -void callback(char* topic, byte* payload, unsigned int length); +void callback(char* topic, uint8_t* payload, size_t length); EthernetClient ethClient; PubSubClient client(server, 1883, callback, ethClient); // Callback function -void callback(char* topic, byte* payload, unsigned int length) { +void callback(char* topic, uint8_t* payload, size_t length) { // In order to republish this payload, a copy must be made // as the orignal payload buffer will be overwritten whilst // constructing the PUBLISH packet. diff --git a/examples/mqtt_reconnect_nonblocking/mqtt_reconnect_nonblocking.ino b/examples/mqtt_reconnect_nonblocking/mqtt_reconnect_nonblocking.ino index 7db5d65..9d5c600 100644 --- a/examples/mqtt_reconnect_nonblocking/mqtt_reconnect_nonblocking.ino +++ b/examples/mqtt_reconnect_nonblocking/mqtt_reconnect_nonblocking.ino @@ -16,7 +16,7 @@ byte mac[] = {0xDE, 0xED, 0xBA, 0xFE, 0xFE, 0xED}; IPAddress ip(172, 16, 0, 100); IPAddress server(172, 16, 0, 2); -void callback(char* topic, byte* payload, unsigned int length) { +void callback(char* topic, uint8_t* payload, size_t length) { // handle message arrived } @@ -46,7 +46,7 @@ void setup() { void loop() { if (!client.connected()) { - long now = millis(); + unsigned long now = millis(); if (now - lastReconnectAttempt > 5000) { lastReconnectAttempt = now; // Attempt to reconnect diff --git a/examples/mqtt_stream/mqtt_stream.ino b/examples/mqtt_stream/mqtt_stream.ino index c784ce4..5e620a0 100644 --- a/examples/mqtt_stream/mqtt_stream.ino +++ b/examples/mqtt_stream/mqtt_stream.ino @@ -21,11 +21,11 @@ IPAddress server(172, 16, 0, 2); SRAM sram(4, SRAM_1024); -void callback(char* topic, byte* payload, unsigned int length) { +void callback(char* topic, uint8_t* payload, size_t length) { sram.seek(1); // do something with the message - for (uint8_t i = 0; i < length; i++) { + for (size_t i = 0; i < length; i++) { Serial.write(sram.read()); } Serial.println(); diff --git a/src/PubSubClient.cpp b/src/PubSubClient.cpp index 2a078f0..c96dd8b 100755 --- a/src/PubSubClient.cpp +++ b/src/PubSubClient.cpp @@ -133,8 +133,7 @@ bool PubSubClient::connect(const char* id, const char* user, const char* pass, c if (result == 1) { nextMsgId = 1; // Leave room in the buffer for header and variable length field - uint16_t length = MQTT_MAX_HEADER_SIZE; - unsigned int j; + size_t length = MQTT_MAX_HEADER_SIZE; #if MQTT_VERSION == MQTT_VERSION_3_1 uint8_t d[9] = {0x00, 0x06, 'M', 'Q', 'I', 's', 'd', 'p', MQTT_VERSION}; @@ -143,7 +142,7 @@ bool PubSubClient::connect(const char* id, const char* user, const char* pass, c uint8_t d[7] = {0x00, 0x04, 'M', 'Q', 'T', 'T', MQTT_VERSION}; #define MQTT_HEADER_VERSION_LENGTH 7 #endif - for (j = 0; j < MQTT_HEADER_VERSION_LENGTH; j++) { + for (size_t j = 0; j < MQTT_HEADER_VERSION_LENGTH; j++) { this->buffer[length++] = d[j]; } @@ -193,7 +192,7 @@ bool PubSubClient::connect(const char* id, const char* user, const char* pass, c while (!_client->available()) { yield(); unsigned long t = millis(); - if (t - lastInActivity >= ((int32_t)this->socketTimeout * 1000UL)) { + if (t - lastInActivity >= static_cast(this->socketTimeout * 1000UL)) { DEBUG_PSC_PRINTF("connect aborting due to timeout\n"); _state = MQTT_CONNECTION_TIMEOUT; _client->stop(); @@ -225,11 +224,11 @@ bool PubSubClient::connect(const char* id, const char* user, const char* pass, c // reads a byte into result bool PubSubClient::readByte(uint8_t* result) { - uint32_t previousMillis = millis(); + unsigned long previousMillis = millis(); while (!_client->available()) { yield(); - uint32_t currentMillis = millis(); - if (currentMillis - previousMillis >= ((int32_t)this->socketTimeout * 1000)) { + unsigned long currentMillis = millis(); + if (currentMillis - previousMillis >= static_cast(this->socketTimeout * 1000UL)) { return false; } } @@ -330,7 +329,7 @@ bool PubSubClient::loop() { } if (_client->available()) { uint8_t llen; - uint16_t len = readPacket(&llen); + uint32_t len = readPacket(&llen); uint16_t msgId = 0; uint8_t* payload; if (len > 0) { @@ -386,23 +385,22 @@ bool PubSubClient::publish(const char* topic, const char* payload, bool retained return publish(topic, (const uint8_t*)payload, payload ? strnlen(payload, this->bufferSize) : 0, retained); } -bool PubSubClient::publish(const char* topic, const uint8_t* payload, unsigned int plength) { +bool PubSubClient::publish(const char* topic, const uint8_t* payload, size_t plength) { return publish(topic, payload, plength, false); } -bool PubSubClient::publish(const char* topic, const uint8_t* payload, unsigned int plength, bool retained) { +bool PubSubClient::publish(const char* topic, const uint8_t* payload, size_t plength, bool retained) { if (connected()) { if (this->bufferSize < MQTT_MAX_HEADER_SIZE + 2 + strnlen(topic, this->bufferSize) + plength) { // Too long return false; } // Leave room in the buffer for header and variable length field - uint16_t length = MQTT_MAX_HEADER_SIZE; + size_t length = MQTT_MAX_HEADER_SIZE; length = writeString(topic, this->buffer, length); // Add payload - unsigned int i; - for (i = 0; i < plength; i++) { + for (size_t i = 0; i < plength; i++) { this->buffer[length++] = payload[i]; } @@ -420,16 +418,15 @@ bool PubSubClient::publish_P(const char* topic, const char* payload, bool retain return publish_P(topic, (const uint8_t*)payload, payload ? strnlen_P(payload, MQTT_MAX_POSSIBLE_PACKET_SIZE) : 0, retained); } -bool PubSubClient::publish_P(const char* topic, const uint8_t* payload, unsigned int plength, bool retained) { +bool PubSubClient::publish_P(const char* topic, const uint8_t* payload, size_t plength, bool retained) { uint8_t llen = 0; uint8_t digit; - unsigned int rc = 0; - uint16_t tlen; - unsigned int pos = 0; - unsigned int i; + size_t rc = 0; + size_t tlen; + size_t pos = 0; uint8_t header; - unsigned int len; - unsigned int expectedLength; + size_t len; + size_t expectedLength; if (!connected()) { return false; @@ -457,8 +454,8 @@ bool PubSubClient::publish_P(const char* topic, const uint8_t* payload, unsigned rc += _client->write(this->buffer, pos); - for (i = 0; i < plength; i++) { - rc += _client->write((char)pgm_read_byte_near(payload + i)); + for (size_t i = 0; i < plength; i++) { + rc += _client->write((uint8_t)pgm_read_byte_near(payload + i)); } lastOutActivity = millis(); @@ -468,17 +465,17 @@ bool PubSubClient::publish_P(const char* topic, const uint8_t* payload, unsigned return (rc == expectedLength); } -bool PubSubClient::beginPublish(const char* topic, unsigned int plength, bool retained) { +bool PubSubClient::beginPublish(const char* topic, size_t plength, bool retained) { if (connected()) { // Send the header and variable length field - uint16_t length = MQTT_MAX_HEADER_SIZE; + size_t length = MQTT_MAX_HEADER_SIZE; length = writeString(topic, this->buffer, length); uint8_t header = MQTTPUBLISH; if (retained) { header |= 1; } size_t hlen = buildHeader(header, this->buffer, plength + length - MQTT_MAX_HEADER_SIZE); - uint16_t rc = _client->write(this->buffer + (MQTT_MAX_HEADER_SIZE - hlen), length - (MQTT_MAX_HEADER_SIZE - hlen)); + size_t rc = _client->write(this->buffer + (MQTT_MAX_HEADER_SIZE - hlen), length - (MQTT_MAX_HEADER_SIZE - hlen)); lastOutActivity = millis(); return (rc == (length - (MQTT_MAX_HEADER_SIZE - hlen))); } @@ -499,37 +496,39 @@ size_t PubSubClient::write(const uint8_t* buffer, size_t size) { return _client->write(buffer, size); } -size_t PubSubClient::buildHeader(uint8_t header, uint8_t* buf, uint16_t length) { +size_t PubSubClient::buildHeader(uint8_t header, uint8_t* buf, size_t length) { uint8_t lenBuf[4]; uint8_t llen = 0; uint8_t digit; - uint8_t pos = 0; - uint16_t len = length; + size_t len = length; do { digit = len & 127; // digit = len % 128 len >>= 7; // len = len / 128 if (len > 0) { digit |= 0x80; } - lenBuf[pos++] = digit; - llen++; - } while (len > 0); + lenBuf[llen++] = digit; + } while (len > 0 && llen < 4); + + if (len > 0) { + DEBUG_PSC_PRINTF("length too big %u, left %u, should be 0\r\n", length, len); + } buf[4 - llen] = header; - for (int i = 0; i < llen; i++) { + for (uint8_t i = 0; i < llen; i++) { buf[MQTT_MAX_HEADER_SIZE - llen + i] = lenBuf[i]; } return llen + 1; // Full header size is variable length bit plus the 1-byte fixed header } -bool PubSubClient::write(uint8_t header, uint8_t* buf, uint16_t length) { - uint16_t rc; - uint8_t hlen = buildHeader(header, buf, length); +bool PubSubClient::write(uint8_t header, uint8_t* buf, size_t length) { + size_t rc; + size_t hlen = buildHeader(header, buf, length); #ifdef MQTT_MAX_TRANSFER_SIZE uint8_t* writeBuf = buf + (MQTT_MAX_HEADER_SIZE - hlen); - uint16_t bytesRemaining = length + hlen; // Match the length type - uint16_t bytesToWrite; + size_t bytesRemaining = length + hlen; // Match the length type + size_t bytesToWrite; bool result = true; while ((bytesRemaining > 0) && result) { yield(); @@ -572,7 +571,7 @@ bool PubSubClient::subscribe(const char* topic, uint8_t qos) { } this->buffer[length++] = (nextMsgId >> 8); this->buffer[length++] = (nextMsgId & 0xFF); - length = writeString((char*)topic, this->buffer, length); + length = writeString(topic, this->buffer, length); this->buffer[length++] = qos; return write(MQTTSUBSCRIBE | MQTTQOS1, this->buffer, length - MQTT_MAX_HEADER_SIZE); } @@ -613,12 +612,12 @@ void PubSubClient::disconnect() { lastInActivity = lastOutActivity = millis(); } -uint16_t PubSubClient::writeString(const char* string, uint8_t* buf, uint16_t pos) { +size_t PubSubClient::writeString(const char* string, uint8_t* buf, size_t pos) { const char* idp = string; uint16_t i = 0; pos += 2; while (*idp) { - buf[pos++] = *idp++; + buf[pos++] = (uint8_t)*idp++; i++; } buf[pos - i - 2] = (i >> 8); @@ -683,7 +682,7 @@ int PubSubClient::state() { return this->_state; } -bool PubSubClient::setBufferSize(uint16_t size) { +bool PubSubClient::setBufferSize(size_t size) { if (size == 0) { // Cannot set it back to 0 return false; @@ -702,7 +701,7 @@ bool PubSubClient::setBufferSize(uint16_t size) { return (this->buffer != NULL); } -uint16_t PubSubClient::getBufferSize() { +size_t PubSubClient::getBufferSize() { return this->bufferSize; } diff --git a/src/PubSubClient.h b/src/PubSubClient.h index 5752f56..cb67ac7 100755 --- a/src/PubSubClient.h +++ b/src/PubSubClient.h @@ -111,9 +111,9 @@ * @brief Define the signature required by any callback function. * @note The parameters are TOPIC, PAYLOAD, and LENGTH, respectively. */ -#define MQTT_CALLBACK_SIGNATURE std::function callback +#define MQTT_CALLBACK_SIGNATURE std::function callback #else -#define MQTT_CALLBACK_SIGNATURE void (*callback)(char*, uint8_t*, unsigned int) +#define MQTT_CALLBACK_SIGNATURE void (*callback)(char*, uint8_t*, size_t) #endif #define CHECK_STRING_LENGTH(l, s) \ @@ -136,7 +136,7 @@ class PubSubClient : public Print { private: Client* _client; uint8_t* buffer; - uint16_t bufferSize; + size_t bufferSize; uint16_t keepAlive; uint16_t socketTimeout; uint16_t nextMsgId; @@ -147,13 +147,13 @@ class PubSubClient : public Print { uint32_t readPacket(uint8_t*); bool readByte(uint8_t* result); bool readByte(uint8_t* result, uint16_t* index); - bool write(uint8_t header, uint8_t* buf, uint16_t length); - uint16_t writeString(const char* string, uint8_t* buf, uint16_t pos); + bool write(uint8_t header, uint8_t* buf, size_t length); + size_t writeString(const char* string, uint8_t* buf, size_t pos); // Build up the header ready to send // Returns the size of the header // Note: the header is built at the end of the first MQTT_MAX_HEADER_SIZE bytes, so will start // (MQTT_MAX_HEADER_SIZE - ) bytes into the buffer - size_t buildHeader(uint8_t header, uint8_t* buf, uint16_t length); + size_t buildHeader(uint8_t header, uint8_t* buf, size_t length); IPAddress ip; const char* domain; uint16_t port; @@ -367,13 +367,13 @@ class PubSubClient : public Print { * @return true If the buffer was resized. * false If the buffer could not be resized. */ - bool setBufferSize(uint16_t size); + bool setBufferSize(size_t size); /** * @brief Gets the current size of the internal buffer. * @return The size of the internal buffer. */ - uint16_t getBufferSize(); + size_t getBufferSize(); /** * @brief Connects the client. @@ -464,7 +464,7 @@ class PubSubClient : public Print { * @return true If the publish succeeded. * false If the publish failed, either connection lost or message too large. */ - bool publish(const char* topic, const uint8_t* payload, unsigned int plength); + bool publish(const char* topic, const uint8_t* payload, size_t plength); /** * @brief Publishes a message to the specified topic. @@ -475,7 +475,7 @@ class PubSubClient : public Print { * @return true If the publish succeeded. * false If the publish failed, either connection lost or message too large. */ - bool publish(const char* topic, const uint8_t* payload, unsigned int plength, bool retained); + bool publish(const char* topic, const uint8_t* payload, size_t plength, bool retained); /** * @brief Publishes a message stored in PROGMEM to the specified topic. @@ -496,7 +496,7 @@ class PubSubClient : public Print { * @return true If the publish succeeded. * false If the publish failed, either connection lost or message too large. */ - bool publish_P(const char* topic, const uint8_t* payload, unsigned int plength, bool retained); + bool publish_P(const char* topic, const uint8_t* payload, size_t plength, bool retained); /** * @brief Start to publish a message. @@ -512,7 +512,7 @@ class PubSubClient : public Print { * @return true If the publish succeeded. * false If the publish failed, either connection lost or message too large. */ - bool beginPublish(const char* topic, unsigned int plength, bool retained); + bool beginPublish(const char* topic, size_t plength, bool retained); /** * @brief Finish sending a message that was started with a call to beginPublish. diff --git a/tests/Makefile b/tests/Makefile index 8fa6e76..2c9dd30 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -7,6 +7,9 @@ SHIM_FILES=${SRC_PATH}/lib/*.cpp PSC_FILE=../src/PubSubClient.cpp CC=g++ CFLAGS=-std=c++11 -I${SRC_PATH}/lib -I../src +CFLAGS+=-pedantic -Werror -Wall -Wextra -Wcast-align -Wcast-qual -Wctor-dtor-privacy -Wdisabled-optimization -Wformat=2 +CFLAGS+=-Winit-self -Wmissing-declarations -Wmissing-include-dirs -Wno-unused -Wnull-dereference -Woverloaded-virtual +CFLAGS+=-Wredundant-decls -Wreorder -Wsign-conversion -Wsign-promo -Wstrict-overflow=5 -Wswitch-default -Wundef all: $(TEST_BIN) diff --git a/tests/src/connect_spec.cpp b/tests/src/connect_spec.cpp index e8545c4..cbba389 100644 --- a/tests/src/connect_spec.cpp +++ b/tests/src/connect_spec.cpp @@ -1,23 +1,41 @@ +#include "BDDTest.h" +#include "Buffer.h" #include "PubSubClient.h" #include "ShimClient.h" -#include "Buffer.h" -#include "BDDTest.h" #include "trace.h" - -byte server[] = { 172, 16, 0, 2 }; - -void callback(char* topic, byte* payload, unsigned int length) { - // handle message arrived +byte server[] = {172, 16, 0, 2}; + +// function declarations +void callback(char* topic, uint8_t* payload, size_t length); +int test_connect_fails_no_network(); +int test_connect_fails_on_no_response(); +int test_connect_properly_formatted(); +int test_connect_properly_formatted_hostname(); +int test_connect_fails_on_bad_rc(); +int test_connect_non_clean_session(); +int test_connect_accepts_username_password(); +int test_connect_accepts_username_no_password(); +int test_connect_accepts_username_blank_password(); +int test_connect_ignores_password_no_username(); +int test_connect_with_will(); +int test_connect_with_will_username_password(); +int test_connect_disconnect_connect(); +int test_connect_custom_keepalive(); + +void callback(char* topic, uint8_t* payload, size_t length) { + // handle message arrived + topic[0]; + payload[0]; + length; } - int test_connect_fails_no_network() { IT("fails to connect if underlying client doesn't connect"); ShimClient shimClient; shimClient.setAllowConnect(false); PubSubClient client(server, 1883, callback, shimClient); - int rc = client.connect((char*)"client_test1"); + bool rc = client.connect("client_test1"); IS_FALSE(rc); int state = client.state(); IS_TRUE(state == MQTT_CONNECT_FAILED); @@ -29,7 +47,7 @@ int test_connect_fails_on_no_response() { ShimClient shimClient; shimClient.setAllowConnect(true); PubSubClient client(server, 1883, callback, shimClient); - int rc = client.connect((char*)"client_test1"); + bool rc = client.connect("client_test1"); IS_FALSE(rc); int state = client.state(); IS_TRUE(state == MQTT_CONNECTION_TIMEOUT); @@ -41,19 +59,20 @@ int test_connect_properly_formatted() { ShimClient shimClient; shimClient.setAllowConnect(true); - byte expectServer[] = { 172, 16, 0, 2 }; - shimClient.expectConnect(expectServer,1883); - byte connect[] = {0x10,0x18,0x0,0x4,0x4d,0x51,0x54,0x54,0x4,0x2,0x0,0xf,0x0,0xc,0x63,0x6c,0x69,0x65,0x6e,0x74,0x5f,0x74,0x65,0x73,0x74,0x31}; - byte connack[] = { 0x20, 0x02, 0x00, 0x00 }; + byte expectServer[] = {172, 16, 0, 2}; + shimClient.expectConnect(expectServer, 1883); + byte connect[] = {0x10, 0x18, 0x0, 0x4, 0x4d, 0x51, 0x54, 0x54, 0x4, 0x2, 0x0, 0xf, 0x0, + 0xc, 0x63, 0x6c, 0x69, 0x65, 0x6e, 0x74, 0x5f, 0x74, 0x65, 0x73, 0x74, 0x31}; + byte connack[] = {0x20, 0x02, 0x00, 0x00}; - shimClient.expect(connect,26); - shimClient.respond(connack,4); + shimClient.expect(connect, 26); + shimClient.respond(connack, 4); PubSubClient client(server, 1883, callback, shimClient); int state = client.state(); IS_TRUE(state == MQTT_DISCONNECTED); - int rc = client.connect((char*)"client_test1"); + bool rc = client.connect("client_test1"); IS_TRUE(rc); IS_FALSE(shimClient.error()); @@ -68,28 +87,27 @@ int test_connect_properly_formatted_hostname() { ShimClient shimClient; shimClient.setAllowConnect(true); - shimClient.expectConnect((char* const)"localhost",1883); - byte connack[] = { 0x20, 0x02, 0x00, 0x00 }; - shimClient.respond(connack,4); + shimClient.expectConnect("localhost", 1883); + byte connack[] = {0x20, 0x02, 0x00, 0x00}; + shimClient.respond(connack, 4); - PubSubClient client((char* const)"localhost", 1883, callback, shimClient); - int rc = client.connect((char*)"client_test1"); + PubSubClient client("localhost", 1883, callback, shimClient); + bool rc = client.connect("client_test1"); IS_TRUE(rc); IS_FALSE(shimClient.error()); END_IT } - int test_connect_fails_on_bad_rc() { IT("fails to connect if a bad return code is received"); ShimClient shimClient; shimClient.setAllowConnect(true); - byte connack[] = { 0x20, 0x02, 0x00, 0x01 }; - shimClient.respond(connack,4); + byte connack[] = {0x20, 0x02, 0x00, 0x01}; + shimClient.respond(connack, 4); PubSubClient client(server, 1883, callback, shimClient); - int rc = client.connect((char*)"client_test1"); + bool rc = client.connect("client_test1"); IS_FALSE(rc); int state = client.state(); @@ -103,19 +121,20 @@ int test_connect_non_clean_session() { ShimClient shimClient; shimClient.setAllowConnect(true); - byte expectServer[] = { 172, 16, 0, 2 }; - shimClient.expectConnect(expectServer,1883); - byte connect[] = {0x10,0x18,0x0,0x4,0x4d,0x51,0x54,0x54,0x4,0x0,0x0,0xf,0x0,0xc,0x63,0x6c,0x69,0x65,0x6e,0x74,0x5f,0x74,0x65,0x73,0x74,0x31}; - byte connack[] = { 0x20, 0x02, 0x00, 0x00 }; + byte expectServer[] = {172, 16, 0, 2}; + shimClient.expectConnect(expectServer, 1883); + byte connect[] = {0x10, 0x18, 0x0, 0x4, 0x4d, 0x51, 0x54, 0x54, 0x4, 0x0, 0x0, 0xf, 0x0, + 0xc, 0x63, 0x6c, 0x69, 0x65, 0x6e, 0x74, 0x5f, 0x74, 0x65, 0x73, 0x74, 0x31}; + byte connack[] = {0x20, 0x02, 0x00, 0x00}; - shimClient.expect(connect,26); - shimClient.respond(connack,4); + shimClient.expect(connect, 26); + shimClient.respond(connack, 4); PubSubClient client(server, 1883, callback, shimClient); int state = client.state(); IS_TRUE(state == MQTT_DISCONNECTED); - int rc = client.connect((char*)"client_test1",0,0,0,0,0,0,0); + bool rc = client.connect("client_test1", 0, 0, 0, 0, 0, 0, 0); IS_TRUE(rc); IS_FALSE(shimClient.error()); @@ -130,13 +149,14 @@ int test_connect_accepts_username_password() { ShimClient shimClient; shimClient.setAllowConnect(true); - byte connect[] = { 0x10,0x24,0x0,0x4,0x4d,0x51,0x54,0x54,0x4,0xc2,0x0,0xf,0x0,0xc,0x63,0x6c,0x69,0x65,0x6e,0x74,0x5f,0x74,0x65,0x73,0x74,0x31,0x0,0x4,0x75,0x73,0x65,0x72,0x0,0x4,0x70,0x61,0x73,0x73}; - byte connack[] = { 0x20, 0x02, 0x00, 0x00 }; - shimClient.expect(connect,0x26); - shimClient.respond(connack,4); + byte connect[] = {0x10, 0x24, 0x0, 0x4, 0x4d, 0x51, 0x54, 0x54, 0x4, 0xc2, 0x0, 0xf, 0x0, 0xc, 0x63, 0x6c, 0x69, 0x65, 0x6e, + 0x74, 0x5f, 0x74, 0x65, 0x73, 0x74, 0x31, 0x0, 0x4, 0x75, 0x73, 0x65, 0x72, 0x0, 0x4, 0x70, 0x61, 0x73, 0x73}; + byte connack[] = {0x20, 0x02, 0x00, 0x00}; + shimClient.expect(connect, 0x26); + shimClient.respond(connack, 4); PubSubClient client(server, 1883, callback, shimClient); - int rc = client.connect((char*)"client_test1",(char*)"user",(char*)"pass"); + bool rc = client.connect("client_test1", "user", "pass"); IS_TRUE(rc); IS_FALSE(shimClient.error()); @@ -148,30 +168,33 @@ int test_connect_accepts_username_no_password() { ShimClient shimClient; shimClient.setAllowConnect(true); - byte connect[] = { 0x10,0x1e,0x0,0x4,0x4d,0x51,0x54,0x54,0x4,0x82,0x0,0xf,0x0,0xc,0x63,0x6c,0x69,0x65,0x6e,0x74,0x5f,0x74,0x65,0x73,0x74,0x31,0x0,0x4,0x75,0x73,0x65,0x72}; - byte connack[] = { 0x20, 0x02, 0x00, 0x00 }; - shimClient.expect(connect,0x20); - shimClient.respond(connack,4); + byte connect[] = {0x10, 0x1e, 0x0, 0x4, 0x4d, 0x51, 0x54, 0x54, 0x4, 0x82, 0x0, 0xf, 0x0, 0xc, 0x63, 0x6c, + 0x69, 0x65, 0x6e, 0x74, 0x5f, 0x74, 0x65, 0x73, 0x74, 0x31, 0x0, 0x4, 0x75, 0x73, 0x65, 0x72}; + byte connack[] = {0x20, 0x02, 0x00, 0x00}; + shimClient.expect(connect, 0x20); + shimClient.respond(connack, 4); PubSubClient client(server, 1883, callback, shimClient); - int rc = client.connect((char*)"client_test1",(char*)"user",0); + bool rc = client.connect("client_test1", "user", 0); IS_TRUE(rc); IS_FALSE(shimClient.error()); END_IT } + int test_connect_accepts_username_blank_password() { IT("accepts a username and blank password"); ShimClient shimClient; shimClient.setAllowConnect(true); - byte connect[] = { 0x10,0x20,0x0,0x4,0x4d,0x51,0x54,0x54,0x4,0xc2,0x0,0xf,0x0,0xc,0x63,0x6c,0x69,0x65,0x6e,0x74,0x5f,0x74,0x65,0x73,0x74,0x31,0x0,0x4,0x75,0x73,0x65,0x72,0x0,0x0}; - byte connack[] = { 0x20, 0x02, 0x00, 0x00 }; - shimClient.expect(connect,0x26); - shimClient.respond(connack,4); + byte connect[] = {0x10, 0x20, 0x0, 0x4, 0x4d, 0x51, 0x54, 0x54, 0x4, 0xc2, 0x0, 0xf, 0x0, 0xc, 0x63, 0x6c, 0x69, + 0x65, 0x6e, 0x74, 0x5f, 0x74, 0x65, 0x73, 0x74, 0x31, 0x0, 0x4, 0x75, 0x73, 0x65, 0x72, 0x0, 0x0}; + byte connack[] = {0x20, 0x02, 0x00, 0x00}; + shimClient.expect(connect, 0x26); + shimClient.respond(connack, 4); PubSubClient client(server, 1883, callback, shimClient); - int rc = client.connect((char*)"client_test1",(char*)"user",(char*)"pass"); + bool rc = client.connect("client_test1", "user", "pass"); IS_TRUE(rc); IS_FALSE(shimClient.error()); @@ -183,13 +206,14 @@ int test_connect_ignores_password_no_username() { ShimClient shimClient; shimClient.setAllowConnect(true); - byte connect[] = {0x10,0x18,0x0,0x4,0x4d,0x51,0x54,0x54,0x4,0x2,0x0,0xf,0x0,0xc,0x63,0x6c,0x69,0x65,0x6e,0x74,0x5f,0x74,0x65,0x73,0x74,0x31}; - byte connack[] = { 0x20, 0x02, 0x00, 0x00 }; - shimClient.expect(connect,26); - shimClient.respond(connack,4); + byte connect[] = {0x10, 0x18, 0x0, 0x4, 0x4d, 0x51, 0x54, 0x54, 0x4, 0x2, 0x0, 0xf, 0x0, + 0xc, 0x63, 0x6c, 0x69, 0x65, 0x6e, 0x74, 0x5f, 0x74, 0x65, 0x73, 0x74, 0x31}; + byte connack[] = {0x20, 0x02, 0x00, 0x00}; + shimClient.expect(connect, 26); + shimClient.respond(connack, 4); PubSubClient client(server, 1883, callback, shimClient); - int rc = client.connect((char*)"client_test1",0,(char*)"pass"); + bool rc = client.connect("client_test1", 0, "pass"); IS_TRUE(rc); IS_FALSE(shimClient.error()); @@ -201,13 +225,15 @@ int test_connect_with_will() { ShimClient shimClient; shimClient.setAllowConnect(true); - byte connect[] = {0x10,0x30,0x0,0x4,0x4d,0x51,0x54,0x54,0x4,0xe,0x0,0xf,0x0,0xc,0x63,0x6c,0x69,0x65,0x6e,0x74,0x5f,0x74,0x65,0x73,0x74,0x31,0x0,0x9,0x77,0x69,0x6c,0x6c,0x54,0x6f,0x70,0x69,0x63,0x0,0xb,0x77,0x69,0x6c,0x6c,0x4d,0x65,0x73,0x73,0x61,0x67,0x65}; - byte connack[] = { 0x20, 0x02, 0x00, 0x00 }; - shimClient.expect(connect,0x32); - shimClient.respond(connack,4); + byte connect[] = {0x10, 0x30, 0x0, 0x4, 0x4d, 0x51, 0x54, 0x54, 0x4, 0xe, 0x0, 0xf, 0x0, 0xc, 0x63, 0x6c, 0x69, + 0x65, 0x6e, 0x74, 0x5f, 0x74, 0x65, 0x73, 0x74, 0x31, 0x0, 0x9, 0x77, 0x69, 0x6c, 0x6c, 0x54, 0x6f, + 0x70, 0x69, 0x63, 0x0, 0xb, 0x77, 0x69, 0x6c, 0x6c, 0x4d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65}; + byte connack[] = {0x20, 0x02, 0x00, 0x00}; + shimClient.expect(connect, 0x32); + shimClient.respond(connack, 4); PubSubClient client(server, 1883, callback, shimClient); - int rc = client.connect((char*)"client_test1",(char*)"willTopic",1,0,(char*)"willMessage"); + bool rc = client.connect("client_test1", "willTopic", 1, 0, "willMessage"); IS_TRUE(rc); IS_FALSE(shimClient.error()); @@ -219,13 +245,15 @@ int test_connect_with_will_username_password() { ShimClient shimClient; shimClient.setAllowConnect(true); - byte connect[] = {0x10,0x40,0x0,0x4,0x4d,0x51,0x54,0x54,0x4,0xce,0x0,0xf,0x0,0xc,0x63,0x6c,0x69,0x65,0x6e,0x74,0x5f,0x74,0x65,0x73,0x74,0x31,0x0,0x9,0x77,0x69,0x6c,0x6c,0x54,0x6f,0x70,0x69,0x63,0x0,0xb,0x77,0x69,0x6c,0x6c,0x4d,0x65,0x73,0x73,0x61,0x67,0x65,0x0,0x4,0x75,0x73,0x65,0x72,0x0,0x8,0x70,0x61,0x73,0x73,0x77,0x6f,0x72,0x64}; - byte connack[] = { 0x20, 0x02, 0x00, 0x00 }; - shimClient.expect(connect,0x42); - shimClient.respond(connack,4); + byte connect[] = {0x10, 0x40, 0x0, 0x4, 0x4d, 0x51, 0x54, 0x54, 0x4, 0xce, 0x0, 0xf, 0x0, 0xc, 0x63, 0x6c, 0x69, 0x65, 0x6e, 0x74, 0x5f, 0x74, + 0x65, 0x73, 0x74, 0x31, 0x0, 0x9, 0x77, 0x69, 0x6c, 0x6c, 0x54, 0x6f, 0x70, 0x69, 0x63, 0x0, 0xb, 0x77, 0x69, 0x6c, 0x6c, 0x4d, + 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x0, 0x4, 0x75, 0x73, 0x65, 0x72, 0x0, 0x8, 0x70, 0x61, 0x73, 0x73, 0x77, 0x6f, 0x72, 0x64}; + byte connack[] = {0x20, 0x02, 0x00, 0x00}; + shimClient.expect(connect, 0x42); + shimClient.respond(connack, 4); PubSubClient client(server, 1883, callback, shimClient); - int rc = client.connect((char*)"client_test1",(char*)"user",(char*)"password",(char*)"willTopic",1,0,(char*)"willMessage"); + bool rc = client.connect("client_test1", "user", "password", "willTopic", 1, 0, "willMessage"); IS_TRUE(rc); IS_FALSE(shimClient.error()); @@ -237,28 +265,29 @@ int test_connect_disconnect_connect() { ShimClient shimClient; shimClient.setAllowConnect(true); - byte expectServer[] = { 172, 16, 0, 2 }; - shimClient.expectConnect(expectServer,1883); - byte connect[] = {0x10,0x18,0x0,0x4,0x4d,0x51,0x54,0x54,0x4,0x2,0x0,0xf,0x0,0xc,0x63,0x6c,0x69,0x65,0x6e,0x74,0x5f,0x74,0x65,0x73,0x74,0x31}; - byte connack[] = { 0x20, 0x02, 0x00, 0x00 }; + byte expectServer[] = {172, 16, 0, 2}; + shimClient.expectConnect(expectServer, 1883); + byte connect[] = {0x10, 0x18, 0x0, 0x4, 0x4d, 0x51, 0x54, 0x54, 0x4, 0x2, 0x0, 0xf, 0x0, + 0xc, 0x63, 0x6c, 0x69, 0x65, 0x6e, 0x74, 0x5f, 0x74, 0x65, 0x73, 0x74, 0x31}; + byte connack[] = {0x20, 0x02, 0x00, 0x00}; - shimClient.expect(connect,26); - shimClient.respond(connack,4); + shimClient.expect(connect, 26); + shimClient.respond(connack, 4); PubSubClient client(server, 1883, callback, shimClient); int state = client.state(); IS_TRUE(state == MQTT_DISCONNECTED); - int rc = client.connect((char*)"client_test1"); + bool rc = client.connect("client_test1"); IS_TRUE(rc); IS_FALSE(shimClient.error()); state = client.state(); IS_TRUE(state == MQTT_CONNECTED); - byte disconnect[] = {0xE0,0x00}; - shimClient.expect(disconnect,2); + byte disconnect[] = {0xE0, 0x00}; + shimClient.expect(disconnect, 2); client.disconnect(); @@ -269,9 +298,9 @@ int test_connect_disconnect_connect() { state = client.state(); IS_TRUE(state == MQTT_DISCONNECTED); - shimClient.expect(connect,28); - shimClient.respond(connack,4); - rc = client.connect((char*)"client_test1"); + shimClient.expect(connect, 28); + shimClient.respond(connack, 4); + rc = client.connect("client_test1"); IS_TRUE(rc); IS_FALSE(shimClient.error()); state = client.state(); @@ -285,15 +314,16 @@ int test_connect_custom_keepalive() { ShimClient shimClient; shimClient.setAllowConnect(true); - byte expectServer[] = { 172, 16, 0, 2 }; - shimClient.expectConnect(expectServer,1883); + byte expectServer[] = {172, 16, 0, 2}; + shimClient.expectConnect(expectServer, 1883); // Set keepalive to 300secs == 0x01 0x2c - byte connect[] = {0x10,0x18,0x0,0x4,0x4d,0x51,0x54,0x54,0x4,0x2,0x01,0x2c,0x0,0xc,0x63,0x6c,0x69,0x65,0x6e,0x74,0x5f,0x74,0x65,0x73,0x74,0x31}; - byte connack[] = { 0x20, 0x02, 0x00, 0x00 }; + byte connect[] = {0x10, 0x18, 0x0, 0x4, 0x4d, 0x51, 0x54, 0x54, 0x4, 0x2, 0x01, 0x2c, 0x0, + 0xc, 0x63, 0x6c, 0x69, 0x65, 0x6e, 0x74, 0x5f, 0x74, 0x65, 0x73, 0x74, 0x31}; + byte connack[] = {0x20, 0x02, 0x00, 0x00}; - shimClient.expect(connect,26); - shimClient.respond(connack,4); + shimClient.expect(connect, 26); + shimClient.respond(connack, 4); PubSubClient client(server, 1883, callback, shimClient); int state = client.state(); @@ -301,7 +331,7 @@ int test_connect_custom_keepalive() { client.setKeepAlive(300); - int rc = client.connect((char*)"client_test1"); + bool rc = client.connect("client_test1"); IS_TRUE(rc); IS_FALSE(shimClient.error()); @@ -311,9 +341,7 @@ int test_connect_custom_keepalive() { END_IT } - -int main() -{ +int main() { SUITE("Connect"); test_connect_fails_no_network(); diff --git a/tests/src/keepalive_spec.cpp b/tests/src/keepalive_spec.cpp index ea643cf..620d6ce 100644 --- a/tests/src/keepalive_spec.cpp +++ b/tests/src/keepalive_spec.cpp @@ -1,40 +1,51 @@ +#include + +#include "BDDTest.h" +#include "Buffer.h" #include "PubSubClient.h" #include "ShimClient.h" -#include "Buffer.h" -#include "BDDTest.h" #include "trace.h" -#include -byte server[] = { 172, 16, 0, 2 }; - -void callback(char* topic, byte* payload, unsigned int length) { - // handle message arrived +byte server[] = {172, 16, 0, 2}; + +// function declarations +void callback(char* topic, uint8_t* payload, size_t length); +int test_keepalive_pings_idle(); +int test_keepalive_pings_with_outbound_qos0(); +int test_keepalive_pings_with_inbound_qos0(); +int test_keepalive_no_pings_inbound_qos1(); +int test_keepalive_disconnects_hung(); + +void callback(char* topic, uint8_t* payload, size_t length) { + // handle message arrived + topic[0]; + payload[0]; + length; } - int test_keepalive_pings_idle() { IT("keeps an idle connection alive (takes 1 minute)"); ShimClient shimClient; shimClient.setAllowConnect(true); - byte connack[] = { 0x20, 0x02, 0x00, 0x00 }; - shimClient.respond(connack,4); + byte connack[] = {0x20, 0x02, 0x00, 0x00}; + shimClient.respond(connack, 4); PubSubClient client(server, 1883, callback, shimClient); - int rc = client.connect((char*)"client_test1"); + bool rc = client.connect("client_test1"); IS_TRUE(rc); - byte pingreq[] = { 0xC0,0x0 }; - shimClient.expect(pingreq,2); - byte pingresp[] = { 0xD0,0x0 }; - shimClient.respond(pingresp,2); + byte pingreq[] = {0xC0, 0x0}; + shimClient.expect(pingreq, 2); + byte pingresp[] = {0xD0, 0x0}; + shimClient.respond(pingresp, 2); for (int i = 0; i < 50; i++) { sleep(1); - if ( i == 15 || i == 31 || i == 47) { - shimClient.expect(pingreq,2); - shimClient.respond(pingresp,2); + if (i == 15 || i == 31 || i == 47) { + shimClient.expect(pingreq, 2); + shimClient.respond(pingresp, 2); } rc = client.loop(); IS_TRUE(rc); @@ -51,27 +62,27 @@ int test_keepalive_pings_with_outbound_qos0() { ShimClient shimClient; shimClient.setAllowConnect(true); - byte connack[] = { 0x20, 0x02, 0x00, 0x00 }; - shimClient.respond(connack,4); + byte connack[] = {0x20, 0x02, 0x00, 0x00}; + shimClient.respond(connack, 4); PubSubClient client(server, 1883, callback, shimClient); - int rc = client.connect((char*)"client_test1"); + bool rc = client.connect("client_test1"); IS_TRUE(rc); - byte publish[] = {0x30,0xe,0x0,0x5,0x74,0x6f,0x70,0x69,0x63,0x70,0x61,0x79,0x6c,0x6f,0x61,0x64}; + byte publish[] = {0x30, 0xe, 0x0, 0x5, 0x74, 0x6f, 0x70, 0x69, 0x63, 0x70, 0x61, 0x79, 0x6c, 0x6f, 0x61, 0x64}; for (int i = 0; i < 50; i++) { - TRACE(i<<":"); - shimClient.expect(publish,16); - rc = client.publish((char*)"topic",(char*)"payload"); + TRACE(i << ":"); + shimClient.expect(publish, 16); + rc = client.publish("topic", "payload"); IS_TRUE(rc); IS_FALSE(shimClient.error()); sleep(1); - if ( i == 15 || i == 31 || i == 47) { - byte pingreq[] = { 0xC0,0x0 }; - shimClient.expect(pingreq,2); - byte pingresp[] = { 0xD0,0x0 }; - shimClient.respond(pingresp,2); + if (i == 15 || i == 31 || i == 47) { + byte pingreq[] = {0xC0, 0x0}; + shimClient.expect(pingreq, 2); + byte pingresp[] = {0xD0, 0x0}; + shimClient.respond(pingresp, 2); } rc = client.loop(); IS_TRUE(rc); @@ -87,25 +98,25 @@ int test_keepalive_pings_with_inbound_qos0() { ShimClient shimClient; shimClient.setAllowConnect(true); - byte connack[] = { 0x20, 0x02, 0x00, 0x00 }; - shimClient.respond(connack,4); + byte connack[] = {0x20, 0x02, 0x00, 0x00}; + shimClient.respond(connack, 4); PubSubClient client(server, 1883, callback, shimClient); - int rc = client.connect((char*)"client_test1"); + bool rc = client.connect("client_test1"); IS_TRUE(rc); - byte publish[] = {0x30,0xe,0x0,0x5,0x74,0x6f,0x70,0x69,0x63,0x70,0x61,0x79,0x6c,0x6f,0x61,0x64}; + byte publish[] = {0x30, 0xe, 0x0, 0x5, 0x74, 0x6f, 0x70, 0x69, 0x63, 0x70, 0x61, 0x79, 0x6c, 0x6f, 0x61, 0x64}; for (int i = 0; i < 50; i++) { - TRACE(i<<":"); + TRACE(i << ":"); sleep(1); - if ( i == 15 || i == 31 || i == 47) { - byte pingreq[] = { 0xC0,0x0 }; - shimClient.expect(pingreq,2); - byte pingresp[] = { 0xD0,0x0 }; - shimClient.respond(pingresp,2); + if (i == 15 || i == 31 || i == 47) { + byte pingreq[] = {0xC0, 0x0}; + shimClient.expect(pingreq, 2); + byte pingresp[] = {0xD0, 0x0}; + shimClient.respond(pingresp, 2); } - shimClient.respond(publish,16); + shimClient.respond(publish, 16); rc = client.loop(); IS_TRUE(rc); IS_FALSE(shimClient.error()); @@ -120,19 +131,19 @@ int test_keepalive_no_pings_inbound_qos1() { ShimClient shimClient; shimClient.setAllowConnect(true); - byte connack[] = { 0x20, 0x02, 0x00, 0x00 }; - shimClient.respond(connack,4); + byte connack[] = {0x20, 0x02, 0x00, 0x00}; + shimClient.respond(connack, 4); PubSubClient client(server, 1883, callback, shimClient); - int rc = client.connect((char*)"client_test1"); + bool rc = client.connect("client_test1"); IS_TRUE(rc); - byte publish[] = {0x32,0x10,0x0,0x5,0x74,0x6f,0x70,0x69,0x63,0x12,0x34,0x70,0x61,0x79,0x6c,0x6f,0x61,0x64}; - byte puback[] = {0x40,0x2,0x12,0x34}; + byte publish[] = {0x32, 0x10, 0x0, 0x5, 0x74, 0x6f, 0x70, 0x69, 0x63, 0x12, 0x34, 0x70, 0x61, 0x79, 0x6c, 0x6f, 0x61, 0x64}; + byte puback[] = {0x40, 0x2, 0x12, 0x34}; for (int i = 0; i < 50; i++) { - shimClient.respond(publish,18); - shimClient.expect(puback,4); + shimClient.respond(publish, 18); + shimClient.expect(puback, 4); sleep(1); rc = client.loop(); IS_TRUE(rc); @@ -148,15 +159,15 @@ int test_keepalive_disconnects_hung() { ShimClient shimClient; shimClient.setAllowConnect(true); - byte connack[] = { 0x20, 0x02, 0x00, 0x00 }; - shimClient.respond(connack,4); + byte connack[] = {0x20, 0x02, 0x00, 0x00}; + shimClient.respond(connack, 4); PubSubClient client(server, 1883, callback, shimClient); - int rc = client.connect((char*)"client_test1"); + bool rc = client.connect("client_test1"); IS_TRUE(rc); - byte pingreq[] = { 0xC0,0x0 }; - shimClient.expect(pingreq,2); + byte pingreq[] = {0xC0, 0x0}; + shimClient.expect(pingreq, 2); for (int i = 0; i < 32; i++) { sleep(1); @@ -172,8 +183,7 @@ int test_keepalive_disconnects_hung() { END_IT } -int main() -{ +int main() { SUITE("Keep-alive"); test_keepalive_pings_idle(); test_keepalive_pings_with_outbound_qos0(); diff --git a/tests/src/lib/Arduino.h b/tests/src/lib/Arduino.h index 2ce9a3e..20d137b 100644 --- a/tests/src/lib/Arduino.h +++ b/tests/src/lib/Arduino.h @@ -1,21 +1,21 @@ #ifndef Arduino_h #define Arduino_h +#include #include #include #include -#include -#include "Print.h" +#include "Print.h" -extern "C"{ - typedef uint8_t byte ; - typedef uint8_t boolean ; +extern "C" { +typedef uint8_t byte; +typedef uint8_t boolean; - /* sketch */ - extern void setup( void ) ; - extern void loop( void ) ; - uint32_t millis( void ); +/* sketch */ +extern void setup(void); +extern void loop(void); +unsigned long millis(void); } #define PROGMEM @@ -24,4 +24,4 @@ extern "C"{ #define yield(x) {} -#endif // Arduino_h +#endif // Arduino_h diff --git a/tests/src/lib/BDDTest.cpp b/tests/src/lib/BDDTest.cpp index a72bf65..10d392d 100644 --- a/tests/src/lib/BDDTest.cpp +++ b/tests/src/lib/BDDTest.cpp @@ -1,9 +1,11 @@ #include "BDDTest.h" -#include "trace.h" -#include + #include -#include #include +#include +#include + +#include "trace.h" int testCount = 0; int testPasses = 0; @@ -19,20 +21,20 @@ int bddtest_test(const char* file, int line, const char* assertion, int result) if (!result) { LOG("✗\n"); std::ostringstream os; - os << " ! "<pos = 0; this->length = 0; - this->add(buf,size); + this->add(buf, size); } bool Buffer::available() { return this->pos < this->length; @@ -28,7 +29,7 @@ void Buffer::reset() { void Buffer::add(uint8_t* buf, size_t size) { uint16_t i = 0; - for (;ibuffer[this->length++] = buf[i]; } } diff --git a/tests/src/lib/Buffer.h b/tests/src/lib/Buffer.h index c6a2cb5..ae3002b 100644 --- a/tests/src/lib/Buffer.h +++ b/tests/src/lib/Buffer.h @@ -4,12 +4,12 @@ #include "Arduino.h" class Buffer { -private: + private: uint8_t buffer[2048]; uint16_t pos; uint16_t length; -public: + public: Buffer(); Buffer(uint8_t* buf, size_t size); diff --git a/tests/src/lib/Client.h b/tests/src/lib/Client.h index 9e18c07..54c86d1 100644 --- a/tests/src/lib/Client.h +++ b/tests/src/lib/Client.h @@ -3,19 +3,19 @@ #include "IPAddress.h" class Client { -public: - virtual int connect(IPAddress ip, uint16_t port) =0; - virtual int connect(const char *host, uint16_t port) =0; - virtual size_t write(uint8_t) =0; - virtual size_t write(const uint8_t *buf, size_t size) =0; - virtual int available() = 0; - virtual int read() = 0; - virtual int read(uint8_t *buf, size_t size) = 0; - virtual int peek() = 0; - virtual void flush() = 0; - virtual void stop() = 0; - virtual uint8_t connected() = 0; - virtual operator bool() = 0; + public: + virtual int connect(IPAddress ip, uint16_t port) = 0; + virtual int connect(const char *host, uint16_t port) = 0; + virtual size_t write(uint8_t) = 0; + virtual size_t write(const uint8_t *buf, size_t size) = 0; + virtual int available() = 0; + virtual int read() = 0; + virtual int read(uint8_t *buf, size_t size) = 0; + virtual int peek() = 0; + virtual void flush() = 0; + virtual void stop() = 0; + virtual uint8_t connected() = 0; + virtual operator bool() = 0; }; #endif diff --git a/tests/src/lib/IPAddress.cpp b/tests/src/lib/IPAddress.cpp index 610ff4c..99d4ede 100644 --- a/tests/src/lib/IPAddress.cpp +++ b/tests/src/lib/IPAddress.cpp @@ -2,43 +2,35 @@ #include #include -IPAddress::IPAddress() -{ +IPAddress::IPAddress() { memset(_address, 0, sizeof(_address)); } -IPAddress::IPAddress(uint8_t first_octet, uint8_t second_octet, uint8_t third_octet, uint8_t fourth_octet) -{ +IPAddress::IPAddress(uint8_t first_octet, uint8_t second_octet, uint8_t third_octet, uint8_t fourth_octet) { _address[0] = first_octet; _address[1] = second_octet; _address[2] = third_octet; _address[3] = fourth_octet; } -IPAddress::IPAddress(uint32_t address) -{ +IPAddress::IPAddress(uint32_t address) { memcpy(_address, &address, sizeof(_address)); } -IPAddress::IPAddress(const uint8_t *address) -{ +IPAddress::IPAddress(const uint8_t* address) { memcpy(_address, address, sizeof(_address)); } -IPAddress& IPAddress::operator=(const uint8_t *address) -{ +IPAddress& IPAddress::operator=(const uint8_t* address) { memcpy(_address, address, sizeof(_address)); return *this; } -IPAddress& IPAddress::operator=(uint32_t address) -{ - memcpy(_address, (const uint8_t *)&address, sizeof(_address)); +IPAddress& IPAddress::operator=(uint32_t address) { + memcpy(_address, (const uint8_t*)&address, sizeof(_address)); return *this; } -bool IPAddress::operator==(const uint8_t* addr) -{ +bool IPAddress::operator==(const uint8_t* addr) { return memcmp(addr, _address, sizeof(_address)) == 0; } - diff --git a/tests/src/lib/IPAddress.h b/tests/src/lib/IPAddress.h index e75a8fe..05efdcd 100644 --- a/tests/src/lib/IPAddress.h +++ b/tests/src/lib/IPAddress.h @@ -8,10 +8,10 @@ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: - * + * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. - * + * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE @@ -26,40 +26,48 @@ #ifndef IPAddress_h #define IPAddress_h - // A class to make it easier to handle and pass around IP addresses class IPAddress { -private: - uint8_t _address[4]; // IPv4 address + private: + alignas(4) uint8_t _address[4]; // IPv4 address // Access the raw byte array containing the address. Because this returns a pointer // to the internal structure rather than a copy of the address this function should only // be used when you know that the usage of the returned uint8_t* will be transient and not // stored. - uint8_t* raw_address() { return _address; }; + uint8_t* raw_address() { + return _address; + }; -public: + public: // Constructors IPAddress(); IPAddress(uint8_t first_octet, uint8_t second_octet, uint8_t third_octet, uint8_t fourth_octet); IPAddress(uint32_t address); - IPAddress(const uint8_t *address); + IPAddress(const uint8_t* address); // Overloaded cast operator to allow IPAddress objects to be used where a pointer // to a four-byte uint8_t array is expected - operator uint32_t() { return *((uint32_t*)_address); }; - bool operator==(const IPAddress& addr) { return (*((uint32_t*)_address)) == (*((uint32_t*)addr._address)); }; + operator uint32_t() { + return *((uint32_t*)_address); + }; + bool operator==(IPAddress& addr) { + return (*((uint32_t*)_address)) == (*(reinterpret_cast(addr._address))); + }; bool operator==(const uint8_t* addr); // Overloaded index operator to allow getting and setting individual octets of the address - uint8_t operator[](int index) const { return _address[index]; }; - uint8_t& operator[](int index) { return _address[index]; }; + uint8_t operator[](int index) const { + return _address[index]; + }; + uint8_t& operator[](int index) { + return _address[index]; + }; // Overloaded copy operators to allow initialisation of IPAddress objects from other types - IPAddress& operator=(const uint8_t *address); + IPAddress& operator=(const uint8_t* address); IPAddress& operator=(uint32_t address); - friend class EthernetClass; friend class UDP; friend class Client; @@ -68,5 +76,4 @@ class IPAddress { friend class DNSClient; }; - #endif diff --git a/tests/src/lib/Print.h b/tests/src/lib/Print.h index 02ef77c..6008409 100644 --- a/tests/src/lib/Print.h +++ b/tests/src/lib/Print.h @@ -21,8 +21,8 @@ #define Print_h class Print { - public: - virtual size_t write(uint8_t) = 0; + public: + virtual size_t write(uint8_t) = 0; }; #endif diff --git a/tests/src/lib/ShimClient.cpp b/tests/src/lib/ShimClient.cpp index f70115f..ba28302 100644 --- a/tests/src/lib/ShimClient.cpp +++ b/tests/src/lib/ShimClient.cpp @@ -1,13 +1,16 @@ #include "ShimClient.h" -#include "trace.h" -#include + #include + #include +#include + +#include "trace.h" extern "C" { - uint32_t millis(void) { - return time(0)*1000; - } +unsigned long millis(void) { + return (unsigned long)time(0) * 1000UL; +} } ShimClient::ShimClient() { @@ -25,36 +28,35 @@ int ShimClient::connect(IPAddress ip, uint16_t port) { if (this->_allowConnect) { this->_connected = true; } - if (this->_expectedPort !=0) { - // if (memcmp(ip,this->_expectedIP,4) != 0) { - // TRACE( "ip mismatch\n"); - // this->_error = true; - // } + if (this->_expectedPort != 0) { + if (memcmp(&ip, &this->_expectedIP, 4) != 0) { + TRACE("ip mismatch\n"); + this->_error = true; + } if (port != this->_expectedPort) { - TRACE( "port mismatch\n"); + TRACE("port mismatch\n"); this->_error = true; } } return this->_connected; } -int ShimClient::connect(const char *host, uint16_t port) { +int ShimClient::connect(const char *host, uint16_t port) { if (this->_allowConnect) { this->_connected = true; } - if (this->_expectedPort !=0) { - if (strcmp(host,this->_expectedHost) != 0) { - TRACE( "host mismatch\n"); + if (this->_expectedPort != 0) { + if (strcmp(host, this->_expectedHost) != 0) { + TRACE("host mismatch\n"); this->_error = true; } if (port != this->_expectedPort) { - TRACE( "port mismatch\n"); + TRACE("port mismatch\n"); this->_error = true; } - } return this->_connected; } -size_t ShimClient::write(uint8_t b) { +size_t ShimClient::write(uint8_t b) { this->_received += 1; TRACE(std::hex << (unsigned int)b); if (!this->expectAnything) { @@ -68,15 +70,15 @@ size_t ShimClient::write(uint8_t b) { this->_error = true; } } - TRACE("\n"<< std::dec); + TRACE("\n" << std::dec); return 1; } -size_t ShimClient::write(const uint8_t *buf, size_t size) { +size_t ShimClient::write(const uint8_t *buf, size_t size) { this->_received += size; - TRACE( "[" << std::dec << (unsigned int)(size) << "] "); - uint16_t i=0; - for (;i0) { + TRACE("[" << std::dec << (unsigned int)(size) << "] "); + uint16_t i = 0; + for (; i < size; i++) { + if (i > 0) { TRACE(":"); } TRACE(std::hex << (unsigned int)(buf[i])); @@ -93,37 +95,44 @@ size_t ShimClient::write(const uint8_t *buf, size_t size) { } } } - TRACE("\n"<responseBuffer->available(); } -int ShimClient::read() { return this->responseBuffer->next(); } +int ShimClient::read() { + return this->responseBuffer->next(); +} int ShimClient::read(uint8_t *buf, size_t size) { uint16_t i = 0; - for (;iread(); } return size; } -int ShimClient::peek() { return 0; } +int ShimClient::peek() { + return 0; +} void ShimClient::flush() {} void ShimClient::stop() { this->setConnected(false); } -uint8_t ShimClient::connected() { return this->_connected; } -ShimClient::operator bool() { return true; } - +uint8_t ShimClient::connected() { + return this->_connected; +} +ShimClient::operator bool() { + return true; +} -ShimClient* ShimClient::respond(uint8_t *buf, size_t size) { - this->responseBuffer->add(buf,size); +ShimClient *ShimClient::respond(uint8_t *buf, size_t size) { + this->responseBuffer->add(buf, size); return this; } -ShimClient* ShimClient::expect(uint8_t *buf, size_t size) { +ShimClient *ShimClient::expect(uint8_t *buf, size_t size) { this->expectAnything = false; - this->expectBuffer->add(buf,size); + this->expectBuffer->add(buf, size); return this; } diff --git a/tests/src/lib/ShimClient.h b/tests/src/lib/ShimClient.h index 2e3f874..cb36966 100644 --- a/tests/src/lib/ShimClient.h +++ b/tests/src/lib/ShimClient.h @@ -2,15 +2,14 @@ #define shimclient_h #include "Arduino.h" +#include "Buffer.h" #include "Client.h" #include "IPAddress.h" -#include "Buffer.h" - class ShimClient : public Client { -private: - Buffer* responseBuffer; - Buffer* expectBuffer; + private: + Buffer *responseBuffer; + Buffer *expectBuffer; bool _allowConnect; bool _connected; bool expectAnything; @@ -18,34 +17,34 @@ class ShimClient : public Client { uint16_t _received; IPAddress _expectedIP; uint16_t _expectedPort; - const char* _expectedHost; - -public: - ShimClient(); - virtual int connect(IPAddress ip, uint16_t port); - virtual int connect(const char *host, uint16_t port); - virtual size_t write(uint8_t); - virtual size_t write(const uint8_t *buf, size_t size); - virtual int available(); - virtual int read(); - virtual int read(uint8_t *buf, size_t size); - virtual int peek(); - virtual void flush(); - virtual void stop(); - virtual uint8_t connected(); - virtual operator bool(); - - virtual ShimClient* respond(uint8_t *buf, size_t size); - virtual ShimClient* expect(uint8_t *buf, size_t size); - - virtual void expectConnect(IPAddress ip, uint16_t port); - virtual void expectConnect(const char *host, uint16_t port); - - virtual uint16_t received(); - virtual bool error(); - - virtual void setAllowConnect(bool b); - virtual void setConnected(bool b); + const char *_expectedHost; + + public: + ShimClient(); + virtual int connect(IPAddress ip, uint16_t port); + virtual int connect(const char *host, uint16_t port); + virtual size_t write(uint8_t); + virtual size_t write(const uint8_t *buf, size_t size); + virtual int available(); + virtual int read(); + virtual int read(uint8_t *buf, size_t size); + virtual int peek(); + virtual void flush(); + virtual void stop(); + virtual uint8_t connected(); + virtual operator bool(); + + virtual ShimClient *respond(uint8_t *buf, size_t size); + virtual ShimClient *expect(uint8_t *buf, size_t size); + + virtual void expectConnect(IPAddress ip, uint16_t port); + virtual void expectConnect(const char *host, uint16_t port); + + virtual uint16_t received(); + virtual bool error(); + + virtual void setAllowConnect(bool b); + virtual void setConnected(bool b); }; #endif diff --git a/tests/src/lib/Stream.cpp b/tests/src/lib/Stream.cpp index b0ecbb4..20f5146 100644 --- a/tests/src/lib/Stream.cpp +++ b/tests/src/lib/Stream.cpp @@ -1,15 +1,18 @@ #include "Stream.h" -#include "trace.h" -#include + #include +#include + +#include "trace.h" + Stream::Stream() { this->expectBuffer = new Buffer(); this->_error = false; this->_written = 0; } -size_t Stream::write(uint8_t b) { +size_t Stream::write(uint8_t b) { this->_written++; TRACE(std::hex << (unsigned int)b); if (this->expectBuffer->available()) { @@ -21,17 +24,16 @@ size_t Stream::write(uint8_t b) { } else { this->_error = true; } - TRACE("\n"<< std::dec); + TRACE("\n" << std::dec); return 1; } - bool Stream::error() { return this->_error; } void Stream::expect(uint8_t *buf, size_t size) { - this->expectBuffer->add(buf,size); + this->expectBuffer->add(buf, size); } uint16_t Stream::length() { diff --git a/tests/src/lib/Stream.h b/tests/src/lib/Stream.h index 4e41f86..41f7058 100644 --- a/tests/src/lib/Stream.h +++ b/tests/src/lib/Stream.h @@ -5,17 +5,17 @@ #include "Buffer.h" class Stream { -private: + private: Buffer* expectBuffer; bool _error; uint16_t _written; -public: + public: Stream(); virtual size_t write(uint8_t); - + virtual bool error(); - virtual void expect(uint8_t *buf, size_t size); + virtual void expect(uint8_t* buf, size_t size); virtual uint16_t length(); }; diff --git a/tests/src/lib/trace.h b/tests/src/lib/trace.h index 42eb991..36a9cd4 100644 --- a/tests/src/lib/trace.h +++ b/tests/src/lib/trace.h @@ -1,9 +1,9 @@ #ifndef trace_h #define trace_h -#include - #include +#include + #define LOG(x) {std::cout << x << std::flush; } #define TRACE(x) {if (getenv("TRACE")) { std::cout << x << std::flush; }} diff --git a/tests/src/publish_spec.cpp b/tests/src/publish_spec.cpp index ee3d3be..f9582c0 100644 --- a/tests/src/publish_spec.cpp +++ b/tests/src/publish_spec.cpp @@ -1,14 +1,26 @@ +#include "BDDTest.h" +#include "Buffer.h" #include "PubSubClient.h" #include "ShimClient.h" -#include "Buffer.h" -#include "BDDTest.h" #include "trace.h" - -byte server[] = { 172, 16, 0, 2 }; - -void callback(char* topic, byte* payload, unsigned int length) { - // handle message arrived +byte server[] = {172, 16, 0, 2}; + +// function declarations +void callback(char* topic, uint8_t* payload, size_t length); +int test_publish(); +int test_publish_bytes(); +int test_publish_retained(); +int test_publish_retained_2(); +int test_publish_not_connected(); +int test_publish_too_long(); +int test_publish_P(); + +void callback(char* topic, uint8_t* payload, size_t length) { + // handle message arrived + topic[0]; + payload[0]; + length; } int test_publish() { @@ -16,17 +28,17 @@ int test_publish() { ShimClient shimClient; shimClient.setAllowConnect(true); - byte connack[] = { 0x20, 0x02, 0x00, 0x00 }; - shimClient.respond(connack,4); + byte connack[] = {0x20, 0x02, 0x00, 0x00}; + shimClient.respond(connack, 4); PubSubClient client(server, 1883, callback, shimClient); - int rc = client.connect((char*)"client_test1"); + bool rc = client.connect("client_test1"); IS_TRUE(rc); - byte publish[] = {0x30,0xe,0x0,0x5,0x74,0x6f,0x70,0x69,0x63,0x70,0x61,0x79,0x6c,0x6f,0x61,0x64}; - shimClient.expect(publish,16); + byte publish[] = {0x30, 0xe, 0x0, 0x5, 0x74, 0x6f, 0x70, 0x69, 0x63, 0x70, 0x61, 0x79, 0x6c, 0x6f, 0x61, 0x64}; + shimClient.expect(publish, 16); - rc = client.publish((char*)"topic",(char*)"payload"); + rc = client.publish("topic", "payload"); IS_TRUE(rc); IS_FALSE(shimClient.error()); @@ -34,26 +46,25 @@ int test_publish() { END_IT } - int test_publish_bytes() { IT("publishes a byte array"); ShimClient shimClient; shimClient.setAllowConnect(true); - byte payload[] = { 0x01,0x02,0x03,0x0,0x05 }; - int length = 5; + byte payload[] = {0x01, 0x02, 0x03, 0x0, 0x05}; + size_t length = 5; - byte connack[] = { 0x20, 0x02, 0x00, 0x00 }; - shimClient.respond(connack,4); + byte connack[] = {0x20, 0x02, 0x00, 0x00}; + shimClient.respond(connack, 4); PubSubClient client(server, 1883, callback, shimClient); - int rc = client.connect((char*)"client_test1"); + bool rc = client.connect("client_test1"); IS_TRUE(rc); - byte publish[] = {0x30,0xc,0x0,0x5,0x74,0x6f,0x70,0x69,0x63,0x1,0x2,0x3,0x0,0x5}; - shimClient.expect(publish,14); + byte publish[] = {0x30, 0xc, 0x0, 0x5, 0x74, 0x6f, 0x70, 0x69, 0x63, 0x1, 0x2, 0x3, 0x0, 0x5}; + shimClient.expect(publish, 14); - rc = client.publish((char*)"topic",payload,length); + rc = client.publish("topic", payload, length); IS_TRUE(rc); IS_FALSE(shimClient.error()); @@ -61,26 +72,25 @@ int test_publish_bytes() { END_IT } - int test_publish_retained() { IT("publishes retained - 1"); ShimClient shimClient; shimClient.setAllowConnect(true); - byte payload[] = { 0x01,0x02,0x03,0x0,0x05 }; - int length = 5; + byte payload[] = {0x01, 0x02, 0x03, 0x0, 0x05}; + size_t length = 5; - byte connack[] = { 0x20, 0x02, 0x00, 0x00 }; - shimClient.respond(connack,4); + byte connack[] = {0x20, 0x02, 0x00, 0x00}; + shimClient.respond(connack, 4); PubSubClient client(server, 1883, callback, shimClient); - int rc = client.connect((char*)"client_test1"); + bool rc = client.connect("client_test1"); IS_TRUE(rc); - byte publish[] = {0x31,0xc,0x0,0x5,0x74,0x6f,0x70,0x69,0x63,0x1,0x2,0x3,0x0,0x5}; - shimClient.expect(publish,14); + byte publish[] = {0x31, 0xc, 0x0, 0x5, 0x74, 0x6f, 0x70, 0x69, 0x63, 0x1, 0x2, 0x3, 0x0, 0x5}; + shimClient.expect(publish, 14); - rc = client.publish((char*)"topic",payload,length,true); + rc = client.publish("topic", payload, length, true); IS_TRUE(rc); IS_FALSE(shimClient.error()); @@ -93,17 +103,17 @@ int test_publish_retained_2() { ShimClient shimClient; shimClient.setAllowConnect(true); - byte connack[] = { 0x20, 0x02, 0x00, 0x00 }; - shimClient.respond(connack,4); + byte connack[] = {0x20, 0x02, 0x00, 0x00}; + shimClient.respond(connack, 4); PubSubClient client(server, 1883, callback, shimClient); - int rc = client.connect((char*)"client_test1"); + bool rc = client.connect("client_test1"); IS_TRUE(rc); - byte publish[] = {0x31,0xc,0x0,0x5,0x74,0x6f,0x70,0x69,0x63,'A','B','C','D','E'}; - shimClient.expect(publish,14); + byte publish[] = {0x31, 0xc, 0x0, 0x5, 0x74, 0x6f, 0x70, 0x69, 0x63, 'A', 'B', 'C', 'D', 'E'}; + shimClient.expect(publish, 14); - rc = client.publish((char*)"topic",(char*)"ABCDE",true); + rc = client.publish("topic", "ABCDE", true); IS_TRUE(rc); IS_FALSE(shimClient.error()); @@ -117,7 +127,7 @@ int test_publish_not_connected() { PubSubClient client(server, 1883, callback, shimClient); - int rc = client.publish((char*)"topic",(char*)"payload"); + bool rc = client.publish("topic", "payload"); IS_FALSE(rc); IS_FALSE(shimClient.error()); @@ -130,16 +140,17 @@ int test_publish_too_long() { ShimClient shimClient; shimClient.setAllowConnect(true); - byte connack[] = { 0x20, 0x02, 0x00, 0x00 }; - shimClient.respond(connack,4); + byte connack[] = {0x20, 0x02, 0x00, 0x00}; + shimClient.respond(connack, 4); PubSubClient client(server, 1883, callback, shimClient); client.setBufferSize(128); - int rc = client.connect((char*)"client_test1"); + bool rc = client.connect("client_test1"); IS_TRUE(rc); - // 0 1 2 3 4 5 6 7 8 9 0 1 2 - rc = client.publish((char*)"topic",(char*)"123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890"); + // 0 1 2 3 4 5 6 7 8 9 0 1 2 + rc = client.publish("topic", + "123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890"); IS_FALSE(rc); IS_FALSE(shimClient.error()); @@ -152,20 +163,20 @@ int test_publish_P() { ShimClient shimClient; shimClient.setAllowConnect(true); - byte payload[] = { 0x01,0x02,0x03,0x0,0x05 }; - int length = 5; + byte payload[] = {0x01, 0x02, 0x03, 0x0, 0x05}; + size_t length = 5; - byte connack[] = { 0x20, 0x02, 0x00, 0x00 }; - shimClient.respond(connack,4); + byte connack[] = {0x20, 0x02, 0x00, 0x00}; + shimClient.respond(connack, 4); PubSubClient client(server, 1883, callback, shimClient); - int rc = client.connect((char*)"client_test1"); + bool rc = client.connect("client_test1"); IS_TRUE(rc); - byte publish[] = {0x31,0xc,0x0,0x5,0x74,0x6f,0x70,0x69,0x63,0x1,0x2,0x3,0x0,0x5}; - shimClient.expect(publish,14); + byte publish[] = {0x31, 0xc, 0x0, 0x5, 0x74, 0x6f, 0x70, 0x69, 0x63, 0x1, 0x2, 0x3, 0x0, 0x5}; + shimClient.expect(publish, 14); - rc = client.publish_P((char*)"topic",payload,length,true); + rc = client.publish_P("topic", payload, length, true); IS_TRUE(rc); IS_FALSE(shimClient.error()); @@ -173,11 +184,7 @@ int test_publish_P() { END_IT } - - - -int main() -{ +int main() { SUITE("Publish"); test_publish(); test_publish_bytes(); diff --git a/tests/src/receive_spec.cpp b/tests/src/receive_spec.cpp index 1fc2426..3635e97 100644 --- a/tests/src/receive_spec.cpp +++ b/tests/src/receive_spec.cpp @@ -4,7 +4,6 @@ #include "ShimClient.h" #include "trace.h" - // If this is changed to > 128 then the publish packet below // is no longer valid as it assumes the remaining length // is a single-byte. Don't make that mistake like I just @@ -16,7 +15,19 @@ byte server[] = {172, 16, 0, 2}; bool callback_called = false; char lastTopic[1024]; char lastPayload[1024]; -unsigned int lastLength; +size_t lastLength; + +// function declarations +void callback(char* topic, uint8_t* payload, size_t length); +void reset_callback(); +int test_receive_callback(); +int test_receive_stream(); +int test_receive_max_sized_message(); +int test_drop_invalid_remaining_length_message(); +int test_receive_oversized_message(); +int test_resize_buffer(); +int test_receive_oversized_stream_message(); +int test_receive_qos1(); void reset_callback() { callback_called = false; @@ -25,7 +36,7 @@ void reset_callback() { lastLength = 0; } -void callback(char* topic, byte* payload, unsigned int length) { +void callback(char* topic, uint8_t* payload, size_t length) { TRACE("Callback received topic=[" << topic << "] length=" << length << "\n") callback_called = true; strcpy(lastTopic, topic); @@ -44,7 +55,7 @@ int test_receive_callback() { shimClient.respond(connack, 4); PubSubClient client(server, 1883, callback, shimClient); - int rc = client.connect((char*)"client_test1"); + bool rc = client.connect("client_test1"); IS_TRUE(rc); byte publish[] = {0x30, 0xe, 0x0, 0x5, 0x74, 0x6f, 0x70, 0x69, 0x63, 0x70, 0x61, 0x79, 0x6c, 0x6f, 0x61, 0x64}; @@ -69,7 +80,8 @@ int test_receive_stream() { reset_callback(); Stream stream; - stream.expect((uint8_t*)"payload", 7); + uint8_t payload[] = "payload"; + stream.expect(payload, sizeof(payload) - 1); ShimClient shimClient; shimClient.setAllowConnect(true); @@ -78,7 +90,7 @@ int test_receive_stream() { shimClient.respond(connack, 4); PubSubClient client(server, 1883, callback, shimClient, stream); - int rc = client.connect((char*)"client_test1"); + bool rc = client.connect("client_test1"); IS_TRUE(rc); byte publish[] = {0x30, 0xe, 0x0, 0x5, 0x74, 0x6f, 0x70, 0x69, 0x63, 0x70, 0x61, 0x79, 0x6c, 0x6f, 0x61, 0x64}; @@ -110,7 +122,7 @@ int test_receive_max_sized_message() { PubSubClient client(server, 1883, callback, shimClient); client.setBufferSize(PUBLISH_LEN); - int rc = client.connect((char*)"client_test1"); + bool rc = client.connect("client_test1"); IS_TRUE(rc); byte publish[] = {0x30, PUBLISH_LEN - 2, 0x0, 0x5, 0x74, 0x6f, 0x70, 0x69, 0x63, 0x70, 0x61, 0x79, 0x6c, 0x6f, 0x61, 0x64}; @@ -146,7 +158,7 @@ int test_receive_oversized_message() { PubSubClient client(server, 1883, callback, shimClient); client.setBufferSize(PUBLISH_LEN - 1); - int rc = client.connect((char*)"client_test1"); + bool rc = client.connect("client_test1"); IS_TRUE(rc); byte publish[] = {0x30, PUBLISH_LEN - 2, 0x0, 0x5, 0x74, 0x6f, 0x70, 0x69, 0x63, 0x70, 0x61, 0x79, 0x6c, 0x6f, 0x61, 0x64}; @@ -178,7 +190,7 @@ int test_drop_invalid_remaining_length_message() { shimClient.respond(connack, 4); PubSubClient client(server, 1883, callback, shimClient); - int rc = client.connect((char*)"client_test1"); + bool rc = client.connect("client_test1"); IS_TRUE(rc); byte publish[] = {0x30, 0x92, 0x92, 0x92, 0x92, 0x01, 0x0, 0x5, 0x74, 0x6f, 0x70, 0x69, 0x63, 0x70, 0x61, 0x79, 0x6c, 0x6f, 0x61, 0x64}; @@ -207,7 +219,7 @@ int test_resize_buffer() { PubSubClient client(server, 1883, callback, shimClient); client.setBufferSize(PUBLISH_LEN - 1); - int rc = client.connect((char*)"client_test1"); + bool rc = client.connect("client_test1"); IS_TRUE(rc); byte publish[] = {0x30, PUBLISH_LEN - 2, 0x0, 0x5, 0x74, 0x6f, 0x70, 0x69, 0x63, 0x70, 0x61, 0x79, 0x6c, 0x6f, 0x61, 0x64}; @@ -256,7 +268,7 @@ int test_receive_oversized_stream_message() { PubSubClient client(server, 1883, callback, shimClient, stream); client.setBufferSize(PUBLISH_LEN - 1); - int rc = client.connect((char*)"client_test1"); + bool rc = client.connect("client_test1"); IS_TRUE(rc); byte publish[] = {0x30, PUBLISH_LEN - 2, 0x0, 0x5, 0x74, 0x6f, 0x70, 0x69, 0x63, 0x70, 0x61, 0x79, 0x6c, 0x6f, 0x61, 0x64}; @@ -295,7 +307,7 @@ int test_receive_qos1() { shimClient.respond(connack, 4); PubSubClient client(server, 1883, callback, shimClient); - int rc = client.connect((char*)"client_test1"); + bool rc = client.connect("client_test1"); IS_TRUE(rc); byte publish[] = {0x32, 0x10, 0x0, 0x5, 0x74, 0x6f, 0x70, 0x69, 0x63, 0x12, 0x34, 0x70, 0x61, 0x79, 0x6c, 0x6f, 0x61, 0x64}; diff --git a/tests/src/subscribe_spec.cpp b/tests/src/subscribe_spec.cpp index 22dc8a4..5a84299 100644 --- a/tests/src/subscribe_spec.cpp +++ b/tests/src/subscribe_spec.cpp @@ -1,14 +1,26 @@ +#include "BDDTest.h" +#include "Buffer.h" #include "PubSubClient.h" #include "ShimClient.h" -#include "Buffer.h" -#include "BDDTest.h" #include "trace.h" - -byte server[] = { 172, 16, 0, 2 }; - -void callback(char* topic, byte* payload, unsigned int length) { - // handle message arrived +byte server[] = {172, 16, 0, 2}; + +// function declarations +void callback(char* topic, uint8_t* payload, size_t length); +int test_subscribe_no_qos(); +int test_subscribe_qos_1(); +int test_subscribe_not_connected(); +int test_subscribe_invalid_qos(); +int test_subscribe_too_long(); +int test_unsubscribe(); +int test_unsubscribe_not_connected(); + +void callback(char* topic, uint8_t* payload, size_t length) { + // handle message arrived + topic[0]; + payload[0]; + length; } int test_subscribe_no_qos() { @@ -16,19 +28,19 @@ int test_subscribe_no_qos() { ShimClient shimClient; shimClient.setAllowConnect(true); - byte connack[] = { 0x20, 0x02, 0x00, 0x00 }; - shimClient.respond(connack,4); + byte connack[] = {0x20, 0x02, 0x00, 0x00}; + shimClient.respond(connack, 4); PubSubClient client(server, 1883, callback, shimClient); - int rc = client.connect((char*)"client_test1"); + bool rc = client.connect("client_test1"); IS_TRUE(rc); - byte subscribe[] = { 0x82,0xa,0x0,0x2,0x0,0x5,0x74,0x6f,0x70,0x69,0x63,0x0 }; - shimClient.expect(subscribe,12); - byte suback[] = { 0x90,0x3,0x0,0x2,0x0 }; - shimClient.respond(suback,5); + byte subscribe[] = {0x82, 0xa, 0x0, 0x2, 0x0, 0x5, 0x74, 0x6f, 0x70, 0x69, 0x63, 0x0}; + shimClient.expect(subscribe, 12); + byte suback[] = {0x90, 0x3, 0x0, 0x2, 0x0}; + shimClient.respond(suback, 5); - rc = client.subscribe((char*)"topic"); + rc = client.subscribe("topic"); IS_TRUE(rc); IS_FALSE(shimClient.error()); @@ -41,19 +53,19 @@ int test_subscribe_qos_1() { ShimClient shimClient; shimClient.setAllowConnect(true); - byte connack[] = { 0x20, 0x02, 0x00, 0x00 }; - shimClient.respond(connack,4); + byte connack[] = {0x20, 0x02, 0x00, 0x00}; + shimClient.respond(connack, 4); PubSubClient client(server, 1883, callback, shimClient); - int rc = client.connect((char*)"client_test1"); + bool rc = client.connect("client_test1"); IS_TRUE(rc); - byte subscribe[] = { 0x82,0xa,0x0,0x2,0x0,0x5,0x74,0x6f,0x70,0x69,0x63,0x1 }; - shimClient.expect(subscribe,12); - byte suback[] = { 0x90,0x3,0x0,0x2,0x1 }; - shimClient.respond(suback,5); + byte subscribe[] = {0x82, 0xa, 0x0, 0x2, 0x0, 0x5, 0x74, 0x6f, 0x70, 0x69, 0x63, 0x1}; + shimClient.expect(subscribe, 12); + byte suback[] = {0x90, 0x3, 0x0, 0x2, 0x1}; + shimClient.respond(suback, 5); - rc = client.subscribe((char*)"topic",1); + rc = client.subscribe("topic", 1); IS_TRUE(rc); IS_FALSE(shimClient.error()); @@ -67,7 +79,7 @@ int test_subscribe_not_connected() { PubSubClient client(server, 1883, callback, shimClient); - int rc = client.subscribe((char*)"topic"); + bool rc = client.subscribe("topic"); IS_FALSE(rc); IS_FALSE(shimClient.error()); @@ -80,16 +92,16 @@ int test_subscribe_invalid_qos() { ShimClient shimClient; shimClient.setAllowConnect(true); - byte connack[] = { 0x20, 0x02, 0x00, 0x00 }; - shimClient.respond(connack,4); + byte connack[] = {0x20, 0x02, 0x00, 0x00}; + shimClient.respond(connack, 4); PubSubClient client(server, 1883, callback, shimClient); - int rc = client.connect((char*)"client_test1"); + bool rc = client.connect("client_test1"); IS_TRUE(rc); - rc = client.subscribe((char*)"topic",2); + rc = client.subscribe("topic", 2); IS_FALSE(rc); - rc = client.subscribe((char*)"topic",254); + rc = client.subscribe("topic", 254); IS_FALSE(rc); IS_FALSE(shimClient.error()); @@ -102,21 +114,21 @@ int test_subscribe_too_long() { ShimClient shimClient; shimClient.setAllowConnect(true); - byte connack[] = { 0x20, 0x02, 0x00, 0x00 }; - shimClient.respond(connack,4); + byte connack[] = {0x20, 0x02, 0x00, 0x00}; + shimClient.respond(connack, 4); PubSubClient client(server, 1883, callback, shimClient); client.setBufferSize(128); - int rc = client.connect((char*)"client_test1"); + bool rc = client.connect("client_test1"); IS_TRUE(rc); // max length should be allowed // 0 1 2 3 4 5 6 7 8 9 0 1 2 - rc = client.subscribe((char*)"12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789"); + rc = client.subscribe("12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789"); IS_TRUE(rc); // 0 1 2 3 4 5 6 7 8 9 0 1 2 - rc = client.subscribe((char*)"123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890"); + rc = client.subscribe("123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890"); IS_FALSE(rc); IS_FALSE(shimClient.error()); @@ -124,25 +136,24 @@ int test_subscribe_too_long() { END_IT } - int test_unsubscribe() { IT("unsubscribes"); ShimClient shimClient; shimClient.setAllowConnect(true); - byte connack[] = { 0x20, 0x02, 0x00, 0x00 }; - shimClient.respond(connack,4); + byte connack[] = {0x20, 0x02, 0x00, 0x00}; + shimClient.respond(connack, 4); PubSubClient client(server, 1883, callback, shimClient); - int rc = client.connect((char*)"client_test1"); + bool rc = client.connect("client_test1"); IS_TRUE(rc); - byte unsubscribe[] = { 0xA2,0x9,0x0,0x2,0x0,0x5,0x74,0x6f,0x70,0x69,0x63 }; - shimClient.expect(unsubscribe,12); - byte unsuback[] = { 0xB0,0x2,0x0,0x2 }; - shimClient.respond(unsuback,4); + byte unsubscribe[] = {0xA2, 0x9, 0x0, 0x2, 0x0, 0x5, 0x74, 0x6f, 0x70, 0x69, 0x63}; + shimClient.expect(unsubscribe, 12); + byte unsuback[] = {0xB0, 0x2, 0x0, 0x2}; + shimClient.respond(unsuback, 4); - rc = client.unsubscribe((char*)"topic"); + rc = client.unsubscribe("topic"); IS_TRUE(rc); IS_FALSE(shimClient.error()); @@ -156,7 +167,7 @@ int test_unsubscribe_not_connected() { PubSubClient client(server, 1883, callback, shimClient); - int rc = client.unsubscribe((char*)"topic"); + bool rc = client.unsubscribe("topic"); IS_FALSE(rc); IS_FALSE(shimClient.error()); @@ -164,8 +175,7 @@ int test_unsubscribe_not_connected() { END_IT } -int main() -{ +int main() { SUITE("Subscribe"); test_subscribe_no_qos(); test_subscribe_qos_1();