Skip to content

Commit

Permalink
added big payload test
Browse files Browse the repository at this point in the history
  • Loading branch information
256dpi committed May 14, 2017
1 parent 81ad3eb commit 1df4977
Showing 1 changed file with 77 additions and 1 deletion.
78 changes: 77 additions & 1 deletion tests/client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ extern "C" {
#define PAYLOAD_LEN 256
char payload[PAYLOAD_LEN + 1];

#define BIG_PAYLOAD_LEN 9800
char big_payload[BIG_PAYLOAD_LEN + 1];

volatile int counter;

const char *custom_ref = "cool";
Expand All @@ -20,7 +23,19 @@ static void message_arrived(lwmqtt_client_t *c, void *ref, lwmqtt_string_t *t, l
int res = lwmqtt_strcmp(t, (char *)"lwmqtt");
ASSERT_EQ(res, 0);

res = strncmp(payload, (char *)m->payload, (size_t)m->payload_len);
res = memcmp(payload, (char *)m->payload, (size_t)m->payload_len);
ASSERT_EQ(res, 0);

counter++;
}

static void big_message_arrived(lwmqtt_client_t *c, void *ref, lwmqtt_string_t *t, lwmqtt_message_t *m) {
ASSERT_EQ(ref, custom_ref);

int res = lwmqtt_strcmp(t, (char *)"lwmqtt");
ASSERT_EQ(res, 0);

res = memcmp(big_payload, (char *)m->payload, (size_t)m->payload_len);
ASSERT_EQ(res, 0);

counter++;
Expand Down Expand Up @@ -265,3 +280,64 @@ TEST(Client, BufferOverflowProtection) {

lwmqtt_unix_network_disconnect(&network);
}

TEST(Client, BigBuffersAndPayload) {
unsigned char buf1[10000], buf2[10000];

lwmqtt_unix_network_t network;
lwmqtt_unix_timer_t timer1, timer2;

lwmqtt_client_t client;

lwmqtt_init(&client, buf1, 10000, buf2, 10000);

lwmqtt_set_network(&client, &network, lwmqtt_unix_network_read, lwmqtt_unix_network_write);
lwmqtt_set_timers(&client, &timer1, &timer2, lwmqtt_unix_timer_set, lwmqtt_unix_timer_get);
lwmqtt_set_callback(&client, (void *)custom_ref, big_message_arrived);

lwmqtt_err_t err = lwmqtt_unix_network_connect(&network, (char *)"broker.shiftr.io", 1883);
ASSERT_EQ(err, LWMQTT_SUCCESS);

lwmqtt_options_t data = lwmqtt_default_options;
data.client_id = lwmqtt_str("lwmqtt");
data.username = lwmqtt_str("try");
data.password = lwmqtt_str("try");

lwmqtt_return_code_t return_code;
err = lwmqtt_connect(&client, &data, NULL, &return_code, COMMAND_TIMEOUT);
ASSERT_EQ(err, LWMQTT_SUCCESS);

err = lwmqtt_subscribe(&client, "lwmqtt", LWMQTT_QOS0, COMMAND_TIMEOUT);
ASSERT_EQ(err, LWMQTT_SUCCESS);

counter = 0;

for (int i = 0; i < 5; i++) {
lwmqtt_message_t msg = lwmqtt_default_message;
msg.qos = LWMQTT_QOS0;
msg.payload = big_payload;
msg.payload_len = BIG_PAYLOAD_LEN;

err = lwmqtt_publish(&client, "lwmqtt", &msg, COMMAND_TIMEOUT);
ASSERT_EQ(err, LWMQTT_SUCCESS);
}

while (counter < 5) {
int available = 0;
err = lwmqtt_unix_network_peek(&client, &network, &available);
ASSERT_EQ(err, LWMQTT_SUCCESS);

if (available > 0) {
err = lwmqtt_yield(&client, available, COMMAND_TIMEOUT);
ASSERT_EQ(err, LWMQTT_SUCCESS);
}
}

err = lwmqtt_unsubscribe(&client, "lwmqtt", COMMAND_TIMEOUT);
ASSERT_EQ(err, LWMQTT_SUCCESS);

err = lwmqtt_disconnect(&client, COMMAND_TIMEOUT);
ASSERT_EQ(err, LWMQTT_SUCCESS);

lwmqtt_unix_network_disconnect(&network);
}

0 comments on commit 1df4977

Please sign in to comment.