Jack Hansdampf / mbed-mqtt-GSOE1

Dependents:   ESP8266MQTT

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers test_MQTTClient.cpp Source File

test_MQTTClient.cpp

00001 /*
00002  * Copyright (c) 2019, Arm Limited and affiliates
00003  * SPDX-License-Identifier: Apache-2.0
00004  *
00005  * Licensed under the Apache License, Version 2.0 (the "License");
00006  * you may 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,
00013  * WITHOUT 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 "../mocks/Network_mock.h"
00019 #include "../stubs/Countdown_stub.h"
00020 #include "gtest/gtest.h"
00021 #include "MQTTClient.h"
00022 
00023 using ::testing::SetArrayArgument;
00024 
00025 class TestMQTTClient : public testing::Test {
00026 public:
00027 protected:
00028     MQTT::Client<Network_mock, Countdown_stub, 100> * client;
00029     Network_mock* net;
00030 
00031     virtual void SetUp()
00032     {
00033         net = new Network_mock;
00034         client = new MQTT::Client<Network_mock, Countdown_stub, 100> (*net);
00035     }
00036 
00037     virtual void TearDown()
00038     {
00039         delete client;
00040         delete net;
00041     }
00042 };
00043 
00044 static void messageHandler(MQTT::MessageData& data) {}
00045 
00046 TEST_F(TestMQTTClient, incorrect_topic_length)
00047 {
00048     ::testing::InSequence s;
00049 
00050     // Install a message handler so that isTopicMatched() can get called.
00051     client->setMessageHandler("*", &messageHandler);
00052 
00053     unsigned char bytes[10] = {
00054             0x30, // Packet type (PUBLISH) + flags
00055             0x03, // Remaining length byte
00056             0x02, 0x03, // Topic length (h'0x0203 = d'515)
00057             0x04, 0x05, 0x06 // Topic (much too short, for the length)
00058             };
00059 
00060     // Packet type (PUBLISH) + flags
00061     EXPECT_CALL(*net, read(_, _, _))
00062         .Times(1)
00063          .WillOnce(DoAll(SetArrayArgument<0>(bytes, bytes+1), Return(1)));
00064 
00065     // Remaining length byte
00066     EXPECT_CALL(*net, read(_, _, _))
00067         .Times(1)
00068         .WillOnce(DoAll(SetArrayArgument<0>(bytes+1, bytes+2), Return(1)));
00069 
00070     // Remaining length byte
00071     EXPECT_CALL(*net, read(_, _, _))
00072         .Times(1)
00073         .WillOnce(DoAll(SetArrayArgument<0>(bytes+2, bytes+5), Return(3)));
00074 
00075     EXPECT_EQ(-1, client->yield(5));
00076 }