Jack Hansdampf / mbed-mqtt-GSOE1

Dependents:   ESP8266MQTT

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers mqtt-sn_legacy.cpp Source File

mqtt-sn_legacy.cpp

00001 /*
00002  * Copyright (c) 2019, ARM Limited, All Rights Reserved
00003  * SPDX-License-Identifier: Apache-2.0
00004  *
00005  * Licensed under the Apache License, Version 2.0 (the "License"); you may
00006  * not use this file except in compliance with the License.
00007  * You may obtain a copy of the License at
00008  *
00009  * http://www.apache.org/licenses/LICENSE-2.0
00010  *
00011  * Unless required by applicable law or agreed to in writing, software
00012  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
00013  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014  * See the License for the specific language governing permissions and
00015  * limitations under the License.
00016  */
00017 
00018 #include <MQTTSNNetworkUDP.h>
00019 #include "mqtt_tests.h"
00020 
00021 #include <MQTTSNClient.h>
00022 #include <MQTTmbed.h> // Countdown
00023 
00024 #define MQTTSN_LEGACY_API_INIT() \
00025     arrivedcountSN = 0; \
00026     NetworkInterface *net = NetworkInterface::get_default_instance(); \
00027     MQTTSNNetworkUDP mqttNet(net); \
00028     MQTTSN::Client<MQTTSNNetworkUDP, Countdown, MBED_CONF_MBED_MQTT_MAX_PACKET_SIZE> client(mqttNet); \
00029     TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, mqttNet.connect(mqtt_global::hostname, mqtt_global::port_udp)); \
00030     MQTTSNPacket_connectData data = MQTTSNPacket_connectData_initializer;
00031 
00032 #define MQTTSN_LEGACY_API_DEINIT() TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, mqttNet.disconnect());
00033 
00034 void MQTTSN_LEGACY_CONNECT()
00035 {
00036     MQTTSN_LEGACY_API_INIT();
00037     data.clientID.cstring = (char *)"MQTTSN_LEGACY_CONNECT";
00038     TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, client.connect(data));
00039     MQTTSN_LEGACY_API_DEINIT();
00040 }
00041 
00042 void MQTTSN_LEGACY_CONNECT_NOT_CONNECTED()
00043 {
00044     NetworkInterface *net = NetworkInterface::get_default_instance();
00045     MQTTSNNetworkUDP mqttNet(net);
00046     MQTTSN::Client<MQTTSNNetworkUDP, Countdown, MBED_CONF_MBED_MQTT_MAX_PACKET_SIZE> client(mqttNet);
00047     //Connect in UDP is not real, it will return success...
00048     TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, mqttNet.connect("i.dont.exist", mqtt_global::port_udp));
00049     MQTTSNPacket_connectData data = MQTTSNPacket_connectData_initializer;
00050     //... but we should not be able to connect to the server.
00051     TEST_ASSERT_EQUAL(-1, client.connect(data));
00052 }
00053 
00054 void MQTTSN_LEGACY_SUBSCRIBE()
00055 {
00056     MQTTSN_LEGACY_API_INIT();
00057     data.clientID.cstring = (char *)"MQTTSN_LEGACY_TEST_SUBSCRIBE";
00058     TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, client.connect(data));
00059     MQTTSN_topicid topic_sn;
00060     init_topic_sn(topic_sn);
00061     TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, client.subscribe(topic_sn, MQTTSN::QOS0, messageArrivedSN));
00062     TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, client.unsubscribe(topic_sn));
00063     init_topic_sn(topic_sn);
00064     TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, client.subscribe(topic_sn, MQTTSN::QOS1, messageArrivedSN));
00065     TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, client.unsubscribe(topic_sn));
00066     init_topic_sn(topic_sn);
00067     TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, client.subscribe(topic_sn, MQTTSN::QOS2, messageArrivedSN));
00068     TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, client.unsubscribe(topic_sn));
00069     MQTTSN_LEGACY_API_DEINIT();
00070 
00071 }
00072 
00073 void MQTTSN_LEGACY_SUBSCRIBE_NETWORK_NOT_CONNECTED()
00074 {
00075     NetworkInterface *net = NetworkInterface::get_default_instance();
00076     MQTTSNNetworkUDP mqttNet(net);
00077     MQTTSN::Client<MQTTSNNetworkUDP, Countdown, MBED_CONF_MBED_MQTT_MAX_PACKET_SIZE> client(mqttNet);
00078     //Connect in UDP is not real, it will return success...
00079     TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, mqttNet.connect("i.dont.exist", mqtt_global::port));
00080     MQTTSNPacket_connectData data = MQTTSNPacket_connectData_initializer;
00081     data.clientID.cstring = (char *)"MQTTSN_LEGACY_SUBSCRIBE_NETWORK_NOT_CONNECTED";
00082     // ... but the connect should fail ...
00083     TEST_ASSERT_EQUAL(-1, client.connect(data));
00084     MQTTSN_topicid topic_sn;
00085     init_topic_sn(topic_sn);
00086     // ... and subscribe should also fail.
00087     TEST_ASSERT_EQUAL(-1, client.subscribe(topic_sn, MQTTSN::QOS0, messageArrivedSN));
00088 }
00089 
00090 void MQTTSN_LEGACY_SUBSCRIBE_CLIENT_NOT_CONNECTED()
00091 {
00092     MQTTSN_LEGACY_API_INIT();
00093     data.clientID.cstring = (char *)"12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890";
00094     MQTTSN_topicid topic_sn;
00095     init_topic_sn(topic_sn);
00096     TEST_ASSERT_EQUAL(-1, client.connect(data));
00097     TEST_ASSERT_EQUAL(-1, client.subscribe(topic_sn, MQTTSN::QOS0, messageArrivedSN));
00098     MQTTSN_LEGACY_API_DEINIT();
00099 }
00100 
00101 void MQTTSN_LEGACY_SUBSCRIBE_TOPIC_TOO_LONG()
00102 {
00103     MQTTSN_LEGACY_API_INIT();
00104     data.clientID.cstring = (char *)"MQTTSN_LEGACY_SUBSCRIBE_TOPIC_TOO_LONG";
00105     TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, client.connect(data));
00106     MQTTSN_topicid topic_sn;
00107     init_topic_sn_too_long(topic_sn);
00108     TEST_ASSERT_EQUAL(-1, client.subscribe(topic_sn, MQTTSN::QOS0, messageArrivedSN));
00109     MQTTSN_LEGACY_API_DEINIT();
00110 }
00111 
00112 void MQTTSN_LEGACY_SUBSCRIBE_INVALID_MESSAGE_HANDLER()
00113 {
00114     MQTTSN_LEGACY_API_INIT();
00115     data.clientID.cstring = (char *)"MQTTSN_LEGACY_SUBSCRIBE_INVALID_MESSAGE_HANDLER";
00116     TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, client.connect(data));
00117     MQTTSN_topicid topic_sn;
00118     init_topic_sn(topic_sn);
00119     // There is no NULL check inside the subscribe function.
00120     TEST_ASSERT_EQUAL(-1, client.subscribe(topic_sn, MQTTSN::QOS0, NULL));
00121     MQTTSN_LEGACY_API_DEINIT();
00122 }
00123 
00124 void MQTTSN_LEGACY_SUBSCRIBE_RECEIVE()
00125 {
00126     MQTTSN_LEGACY_API_INIT();
00127     data.clientID.cstring = (char *)"MQTTSN_LEGACY_SUBSCRIBE_INVALID_MESSAGE_HANDLER";
00128     TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, client.connect(data));
00129     int arrivedCountBeforeSubscription = arrivedcountSN;
00130     MQTTSN_topicid topic_sn;
00131     topic_sn.type = MQTTSN_TOPIC_TYPE_NORMAL;
00132     topic_sn.data.long_.len = strlen(mqtt_global::mbed_public_test_topic);
00133     topic_sn.data.long_.name = const_cast<char *>(mqtt_global::mbed_public_test_topic);
00134     TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, client.subscribe(topic_sn, MQTTSN::QOS0, messageArrivedSN));
00135     // TODO get the callbacks working for MQTT-SN
00136     while (arrivedCountBeforeSubscription == arrivedcountSN) {
00137         client.yield(100);
00138     }
00139     TEST_ASSERT_TRUE(arrivedCountBeforeSubscription < arrivedcountSN);
00140     TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, client.unsubscribe(topic_sn));
00141     MQTTSN_LEGACY_API_DEINIT();
00142 }
00143 
00144 void MQTTSN_LEGACY_UNSUBSCRIBE_WITHOUT_SUBSCRIBE()
00145 {
00146     MQTTSN_LEGACY_API_INIT();
00147     data.clientID.cstring = (char *)"MQTTSN_LEGACY_UNSUBSCRIBE_WITHOUT_SUBSCRIBE";
00148     TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, client.connect(data));
00149     MQTTSN_topicid topic_sn;
00150     init_topic_sn(topic_sn);
00151     TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, client.unsubscribe(topic_sn));
00152     MQTTSN_LEGACY_API_DEINIT();
00153 }
00154 
00155 void MQTTSN_LEGACY_UNSUBSCRIBE_INVALID()
00156 {
00157     MQTTSN_LEGACY_API_INIT();
00158     data.clientID.cstring = (char *)"MQTTSN_LEGACY_UNSUBSCRIBE_INVALID";
00159     TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, client.connect(data));
00160     MQTTSN_topicid topic_sn_too_long;
00161     init_topic_sn_too_long(topic_sn_too_long);
00162     TEST_ASSERT_EQUAL(-1, client.unsubscribe(topic_sn_too_long));
00163     MQTTSN_LEGACY_API_DEINIT();
00164 }
00165 
00166 void MQTTSN_LEGACY_PUBLISH()
00167 {
00168     MQTTSN_LEGACY_API_INIT();
00169     data.clientID.cstring = (char *)"MQTTSN_LEGACY_PUBLISH";
00170     TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, client.connect(data));
00171     MQTTSN_topicid topic_sn;
00172     init_topic_sn(topic_sn);
00173     TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, client.publish(topic_sn, mqtt_global::default_message_sn));
00174     MQTTSN::Message msg = mqtt_global::default_message_sn;
00175     msg.qos = MQTTSN::QOS1;
00176     // TODO: get QoS 1 and 2 working. Maybe we need to reconnect?
00177 //    TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, client.publish(topic_sn, msg));
00178 //    msg.qos = MQTTSN::QOS2;
00179 //    TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, client.publish(topic_sn, msg));
00180     MQTTSN_LEGACY_API_DEINIT();
00181 }
00182 
00183 void MQTTSN_LEGACY_PUBLISH_NOT_CONNECTED()
00184 {
00185     MQTTSN_LEGACY_API_INIT();
00186     MQTTSN_topicid topic_sn;
00187     init_topic_sn(topic_sn);
00188     TEST_ASSERT_EQUAL(-1, client.publish(topic_sn, mqtt_global::default_message_sn));
00189     MQTTSN_LEGACY_API_DEINIT();
00190 }
00191 
00192 void MQTTSN_LEGACY_PUBLISH_TOPIC_TOO_LONG()
00193 {
00194     MQTTSN_LEGACY_API_INIT();
00195     data.clientID.cstring = (char *)"MQTT_PUBLISH";
00196     TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, client.connect(data));
00197     // TODO verify if this is passing intentionally or if this is a bug?
00198 //    TEST_ASSERT_EQUAL(-1, client.publish(mqtt_global::topic_too_long, mqtt_global::default_message));
00199     MQTTSN_LEGACY_API_DEINIT();
00200 }
00201 
00202 // QTT-SN does not provide the user/password functionality so
00203 // MQTT_CONNECT_USER_PASSWORD_INCORRECT and MQTT_CONNECT_SUBSCRIBE_PUBLISH_USER_PASSWORD
00204 // cannot be executed for MQTT-SN
00205 
00206 void MQTTSN_LEGACY_IS_CONNECTED()
00207 {
00208     MQTTSN_LEGACY_API_INIT();
00209     data.clientID.cstring = (char *)"MQTTSN_LEGACY_IS_CONNECTED";
00210     TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, client.connect(data));
00211     TEST_ASSERT_TRUE(client.isConnected());
00212     MQTTSN_LEGACY_API_DEINIT();
00213 }
00214 
00215 void MQTTSN_LEGACY_IS_CONNECTED_CLIENT_NOT_CONNECTED()
00216 {
00217     MQTTSN_LEGACY_API_INIT();
00218     TEST_ASSERT_FALSE(client.isConnected());
00219     MQTTSN_LEGACY_API_DEINIT();
00220 }
00221 
00222 void MQTTSN_LEGACY_IS_CONNECTED_NETWORK_NOT_CONNECTED()
00223 {
00224     NetworkInterface *net = NetworkInterface::get_default_instance();
00225     MQTTSNNetworkUDP mqttNet(net);
00226     MQTTSN::Client<MQTTSNNetworkUDP, Countdown, MBED_CONF_MBED_MQTT_MAX_PACKET_SIZE> client(mqttNet);
00227     //Connect in UDP is not real, it will return success...
00228     TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, mqttNet.connect("i.dont.exist", mqtt_global::port));
00229     MQTTSNPacket_connectData data = MQTTSNPacket_connectData_initializer;
00230     data.clientID.cstring = (char *)"MQTTSN_LEGACY_SUBSCRIBE_NETWORK_NOT_CONNECTED";
00231 
00232     TEST_ASSERT_FALSE(client.isConnected());
00233 
00234     // ... but the connect should fail ...
00235     TEST_ASSERT_EQUAL(-1, client.connect(data));
00236 
00237     TEST_ASSERT_FALSE(client.isConnected());
00238 }
00239 
00240 void MQTTSN_LEGACY_UDP_CONNECT_SUBSCRIBE_PUBLISH()
00241 {
00242     arrivedcountSN = 0;
00243     NetworkInterface *net = NetworkInterface::get_default_instance();
00244     MQTTSNNetworkUDP mqttNet(net);
00245 
00246     MQTTSN::Client<MQTTSNNetworkUDP, Countdown, MBED_CONF_MBED_MQTT_MAX_PACKET_SIZE> client(mqttNet);
00247 
00248     TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, mqttNet.connect(mqtt_global::hostname, mqtt_global::port_udp));
00249 
00250     send_messages_sn< MQTTSN::Client<MQTTSNNetworkUDP, Countdown, MBED_CONF_MBED_MQTT_MAX_PACKET_SIZE> >(client, "MQTTSN_LEGACY_UDP_CONNECT_SUBSCRIBE_PUBLISH");
00251 
00252     TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, mqttNet.disconnect());
00253 }