151 lines
4.3 KiB
C++
151 lines
4.3 KiB
C++
#include <cstdint>
|
|
#include <iostream>
|
|
|
|
#include <di/encoding/me.h>
|
|
#include <gtest/gtest.h>
|
|
|
|
using namespace std;
|
|
|
|
static di_me_ctx me_ctx;
|
|
static uint8_t ctx_linebuffer[256];
|
|
static uint8_t input_data_linebuffer_twice[512];
|
|
static unsigned int match_count = 0;
|
|
static unsigned int match_count_ko = 0;
|
|
static char input_data[] = "\r\nOK\r\nKO\r\nOK\r\nKO\r\nOK\r\nHI\r\n";
|
|
static size_t input_len = 0;
|
|
|
|
static void me_cb_match(struct di_me_ctx *ctx)
|
|
{
|
|
(void)ctx;
|
|
match_count++;
|
|
}
|
|
|
|
static void me_cb_match_ko(struct di_me_ctx *ctx)
|
|
{
|
|
(void)ctx;
|
|
match_count_ko++;
|
|
}
|
|
|
|
static void me_cb_match_lionel(struct di_me_ctx *ctx)
|
|
{
|
|
(void)ctx;
|
|
EXPECT_STREQ("is it me you're looking for\r\n", ctx->match.str);
|
|
EXPECT_STREQ("+HELLO: is it me you're looking for\r\n", reinterpret_cast<const char *>(ctx->linebuffer.data));
|
|
}
|
|
|
|
static const struct di_me_item me_items[] = {
|
|
{ DI_ME_ITEM_STR("HI"), 0, NULL },
|
|
{ DI_ME_ITEM_STR("OK"), 0, me_cb_match },
|
|
{ DI_ME_ITEM_STR("KO"), DI_ME_ITEM_FLAG_CB_ON_MATCH | DI_ME_ITEM_FLAG_CB_ON_RESET, me_cb_match_ko },
|
|
{ DI_ME_ITEM_STR("+HELLO: "), DI_ME_ITEM_FLAG_CB_ON_RESET, me_cb_match_lionel }
|
|
};
|
|
|
|
static di_me_item_states me_items_state[sizeof(me_items)/sizeof(me_items[0])];
|
|
|
|
void encoding_me_reset(void) {
|
|
memset(&me_ctx, 0, sizeof(struct di_me_ctx));
|
|
|
|
match_count = 0;
|
|
match_count_ko = 0;
|
|
|
|
me_ctx.cfg.flags = DI_ME_ITEM_FLAG_CB_ON_RESET;
|
|
me_ctx.cfg.reset_char = '\n';
|
|
|
|
di_buffer_init(&me_ctx.linebuffer, ctx_linebuffer, sizeof(ctx_linebuffer));
|
|
|
|
me_ctx.items.list = me_items;
|
|
me_ctx.items.n = sizeof(me_items)/sizeof(me_items[0]);
|
|
me_ctx.items.state = me_items_state;
|
|
|
|
input_len = strlen(input_data);
|
|
}
|
|
|
|
TEST(encoding_me, feed_invalid) {
|
|
di_me_feed(NULL, reinterpret_cast<void *>(input_data), input_len);
|
|
di_me_feed(&me_ctx, NULL, input_len);
|
|
di_me_feed(&me_ctx, reinterpret_cast<void *>(input_data), 0);
|
|
}
|
|
|
|
/* Count amount of callbacks calls */
|
|
TEST(encoding_me, count_cb_calls) {
|
|
encoding_me_reset();
|
|
|
|
di_me_feed(&me_ctx, reinterpret_cast<void *>(input_data), input_len);
|
|
|
|
EXPECT_EQ(3U, match_count);
|
|
EXPECT_EQ(4U, match_count_ko);
|
|
}
|
|
|
|
/* Feed twice linebuffer without match, check for crash */
|
|
TEST(encoding_me, check_linebuffer_overflow) {
|
|
encoding_me_reset();
|
|
|
|
memset(&input_data_linebuffer_twice, 0x55, sizeof(input_data_linebuffer_twice));
|
|
|
|
di_me_feed(&me_ctx, reinterpret_cast<void *>(input_data_linebuffer_twice), sizeof(input_data_linebuffer_twice));
|
|
|
|
/* Expect we are at the end of the linebuffer with matching */
|
|
EXPECT_EQ(me_ctx.linebuffer.used, me_ctx.linebuffer.size - 1);
|
|
}
|
|
|
|
/* No linebuffer, check if we match */
|
|
TEST(encoding_me, linebuffer_null) {
|
|
encoding_me_reset();
|
|
me_ctx.linebuffer.data = NULL;
|
|
|
|
di_me_feed(&me_ctx, reinterpret_cast<void *>(input_data), input_len);
|
|
|
|
EXPECT_EQ(3U, match_count);
|
|
EXPECT_EQ(4U, match_count_ko);
|
|
}
|
|
|
|
/* Count amount of callbacks calls */
|
|
TEST(encoding_me, multi_chunk_ok_test) {
|
|
char chunk1[] = "Lorem ipsum, lorem ipsum\r\nO";
|
|
char chunk2[] = "K\r\nmust match..\r\n";
|
|
|
|
encoding_me_reset();
|
|
|
|
di_me_feed(&me_ctx, reinterpret_cast<void *>(chunk1), sizeof(chunk1) - 1);
|
|
di_me_feed(&me_ctx, reinterpret_cast<void *>(chunk2), sizeof(chunk2) - 1);
|
|
|
|
EXPECT_EQ(1U, match_count);
|
|
EXPECT_EQ(0U, match_count_ko);
|
|
}
|
|
|
|
TEST(encoding_me, lionel_richie) {
|
|
char chunk1[] = "\r\n\r\n+HELLO";
|
|
char chunk2[] = ": is it me you'";
|
|
char chunk3[] = "re looking for\r\n\r\nKO\r\nLALALA\r\n";
|
|
|
|
encoding_me_reset();
|
|
|
|
di_me_feed(&me_ctx, reinterpret_cast<void *>(chunk1), sizeof(chunk1) - 1);
|
|
di_me_feed(&me_ctx, reinterpret_cast<void *>(chunk2), sizeof(chunk2) - 1);
|
|
di_me_feed(&me_ctx, reinterpret_cast<void *>(chunk3), sizeof(chunk3) - 1);
|
|
|
|
EXPECT_EQ(0U, match_count);
|
|
EXPECT_EQ(2U, match_count_ko);
|
|
}
|
|
|
|
TEST(encoding_me, char_count) {
|
|
EXPECT_EQ(static_cast<size_t>(1), di_me_char_count(input_data, 'H'));
|
|
EXPECT_EQ(static_cast<size_t>(1), di_me_char_count(input_data, 'I'));
|
|
|
|
EXPECT_EQ(static_cast<size_t>(5), di_me_char_count(input_data, 'O'));
|
|
EXPECT_EQ(static_cast<size_t>(5), di_me_char_count(input_data, 'K'));
|
|
|
|
EXPECT_EQ(static_cast<size_t>(7), di_me_char_count(input_data, '\r'));
|
|
EXPECT_EQ(static_cast<size_t>(7), di_me_char_count(input_data, '\n'));
|
|
}
|
|
|
|
TEST(encoding_me, chrrep) {
|
|
char chrrep_test[] = "HELLO\r\nWORLD\r\n";
|
|
|
|
di_me_chrrep(chrrep_test, "\r", 0);
|
|
EXPECT_STREQ("HELLO", chrrep_test);
|
|
|
|
di_me_chrrep(chrrep_test, "L", 0);
|
|
EXPECT_STREQ("HE", chrrep_test);
|
|
}
|