src.dualinventive.com/dinet/libdi/tests/drv_hl854x_tcp.cpp

170 lines
4.6 KiB
C++

#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <gtest/gtest.h>
#include <di/drv/hl854x.h>
#include "fixtures/HL854XTest.hpp"
extern "C" {
#include "drv/hl854x/tcp.c"
}
TEST_F(HL854XTest, tcp_send) {
const char td[] = "TESTDATA";
EXPECT_EQ(DNE_PARAM, di_drv_hl854x_tcp_send(&ctx, td, 0));
EXPECT_EQ(DNE_DISCONNECTED, di_drv_hl854x_tcp_send(&ctx, td, sizeof(td)));
// TODO fix the test fixture read/write callback to test real data sending
}
static uint8_t buffer[256];
static const char *read_cb_str = NULL;
static struct di_drv_hl854x_ctx ctx;
size_t test_drv_hl854x_read_cb(void *buf, size_t nbyte)
{
if (nbyte < strlen(read_cb_str))
return 0;
memcpy(buf, read_cb_str, strlen(read_cb_str));
return strlen(read_cb_str);
}
void test_drv_hl854x_read_cb_set_str(const char *str)
{
read_cb_str = str;
}
void test_hl854x_tcp_init(const char *str)
{
di_drv_hl854x_set_read(&ctx, test_drv_hl854x_read_cb);
test_drv_hl854x_read_cb_set_str(str);
ctx.carrier.status = HL854X_CARRIER_STATUS_REG_HOME;
ctx.tcp.state = HL854X_TCP_STATE_CONNECTED;
memset(buffer, 0, sizeof(buffer));
}
TEST(drv_hl854x, init) {
di_drv_hl854x_init(&ctx);
}
TEST(drv_hl854x_tcp, recv) {
size_t nbyte;
test_hl854x_tcp_init("CONNECT\r\nTESTDATA--EOF--Pattern--\r\nOK\r\n");
nbyte = di_drv_hl854x_tcp_recv(&ctx, &buffer, sizeof(buffer));
EXPECT_EQ(8U, nbyte);
EXPECT_EQ(8U, ctx.tcp.rx_bytes);
EXPECT_STREQ("TESTDATA", reinterpret_cast<const char *>(buffer));
ctx.tcp.rx_bytes = 0;
}
TEST(drv_hl854x_tcp, recv_10x) {
size_t nbyte;
test_hl854x_tcp_init("CONNECT\r\nTESTDATA--EOF--Pattern--\r\nOK\r\n");
for (size_t i = 0; i < 10; i++) {
nbyte = di_drv_hl854x_tcp_recv(&ctx, &buffer, sizeof(buffer));
EXPECT_EQ(8U, nbyte);
EXPECT_STREQ("TESTDATA", reinterpret_cast<const char *>(buffer));
}
EXPECT_EQ(80U, ctx.tcp.rx_bytes);
}
/**
* Test +KTCP_NOTIF URCs
* NOTE: The driver only supports Session ID 1
*/
TEST(drv_hl854x_tcp, cb_ktcp_notif) {
static const struct _tv {
const char *data;
enum di_drv_hl854x_error error;
enum di_drv_hl854x_ktcp_notif ktcp_notif;
} tv[] = {
{"BOEMBATS BOEM BOEM\r\n",
HL854X_ERROR_UNKNOWN,
DI_DRV_HL854X_KTCP_NOTIF_UNKNOWN},
{"+KTCP_NOTIF: 1,0\r\n",
HL854X_ERROR_KTCP_NOTIF,
DI_DRV_HL854X_KTCP_NOTIF_NETWORK_ERROR},
{"+KTCP_NOTIF: 1,1\r\n",
HL854X_ERROR_KTCP_NOTIF,
DI_DRV_HL854X_KTCP_NOTIF_NO_SOCK_AVAIL},
{"+KTCP_NOTIF: 1,2\r\n",
HL854X_ERROR_KTCP_NOTIF,
DI_DRV_HL854X_KTCP_NOTIF_MEMORY_ERROR},
{"+KTCP_NOTIF: 1,3\r\n",
HL854X_ERROR_KTCP_NOTIF,
DI_DRV_HL854X_KTCP_NOTIF_DNS_ERROR},
{"+KTCP_NOTIF: 1,4\r\n",
HL854X_ERROR_KTCP_NOTIF,
DI_DRV_HL854X_KTCP_NOTIF_DISCONNECTED},
{"+KTCP_NOTIF: 1,5\r\n",
HL854X_ERROR_KTCP_NOTIF,
DI_DRV_HL854X_KTCP_NOTIF_CON_ERROR},
{"+KTCP_NOTIF: 1,6\r\n",
HL854X_ERROR_KTCP_NOTIF,
DI_DRV_HL854X_KTCP_NOTIF_GENERIC_ERROR},
{"+KTCP_NOTIF: 1,7\r\n",
HL854X_ERROR_KTCP_NOTIF,
DI_DRV_HL854X_KTCP_NOTIF_ACCEPT_FAILED},
{"+KTCP_NOTIF: 1,8\r\n",
HL854X_ERROR_KTCP_NOTIF,
DI_DRV_HL854X_KTCP_NOTIF_KTCPSND_MORELESS_DATA},
{"+KTCP_NOTIF: 1,9\r\n",
HL854X_ERROR_KTCP_NOTIF,
DI_DRV_HL854X_KTCP_NOTIF_BAD_SESSION_ID},
{"+KTCP_NOTIF: 1,10\r\n",
HL854X_ERROR_KTCP_NOTIF,
DI_DRV_HL854X_KTCP_NOTIF_SESSION_ALREADY_RUNNING},
{"+KTCP_NOTIF: 1,11\r\n",
HL854X_ERROR_KTCP_NOTIF,
DI_DRV_HL854X_KTCP_NOTIF_ALL_SESSIONS_USED},
{"+KTCP_NOTIF: 1,12\r\n",
HL854X_ERROR_KTCP_NOTIF,
DI_DRV_HL854X_KTCP_NOTIF_SOCK_CON_TIMEOUT},
{"+KTCP_NOTIF: 1,13\r\n",
HL854X_ERROR_KTCP_NOTIF,
DI_DRV_HL854X_KTCP_NOTIF_CONTROL_SOCK_CON_TIMEOUT}
};
const size_t tv_size = DI_ARRAY_SIZE(tv);
for (size_t n = 0; n < tv_size; n++) {
test_hl854x_tcp_init(tv[n].data);
ctx.error = HL854X_ERROR_UNKNOWN;
ctx.ktcp_notif = DI_DRV_HL854X_KTCP_NOTIF_UNKNOWN;
di_drv_hl854x_read(&ctx);
if (ctx.error != tv[n].error ||
ctx.ktcp_notif != tv[n].ktcp_notif) {
EXPECT_TRUE(false) << "=== Error in test vector ===\n\"" << tv[n].data << "\"";
}
EXPECT_EQ(tv[n].error, ctx.error);
EXPECT_EQ(tv[n].ktcp_notif, ctx.ktcp_notif);
}
}
/**
* Verify if the recv_wait_data_available works as expected when an async URC KTCP_DATA is send
* * Test DNE_TIMEOUT on 1ms
* * Recv 12 bytes KTCP_DATA URC
* * Test DNOK on 1ms
* * Check recv_size is 12 bytes
*/
TEST(drv_hl854x_tcp, recv_wait_data_available) {
ASSERT_EQ(DNE_TIMEOUT, di_drv_hl854x_tcp_recv_wait_data_available(&ctx, 1));
test_hl854x_tcp_init("+KTCP_DATA: 1,12\r\n");
di_drv_hl854x_read(&ctx);
ASSERT_EQ(DNOK, di_drv_hl854x_tcp_recv_wait_data_available(&ctx, 1));
ASSERT_EQ(ctx.tcp.recv_size, 12);
}