#include #include #include #include #include #include "mocs.h" extern "C" { #include "tcp_cmd.h" } TEST(TCP_CMD, tcp_cmd_remove_newline) { char* cmd = (char*)calloc(50, 1); strcpy(cmd, "help\n"); tcp_cmd_remove_newline(cmd, strlen(cmd)); EXPECT_STREQ(cmd, "help"); strcpy(cmd, "help"); tcp_cmd_remove_newline(cmd, strlen(cmd)); EXPECT_STREQ(cmd, "help"); strcpy(cmd, "help\n\n"); tcp_cmd_remove_newline(cmd, strlen(cmd)); EXPECT_STREQ(cmd, "help"); strcpy(cmd, "\nhelp\n\n"); tcp_cmd_remove_newline(cmd, strlen(cmd)); EXPECT_STREQ(cmd, "help"); strcpy(cmd, "\n\nhelp\n\n"); tcp_cmd_remove_newline(cmd, strlen(cmd)); EXPECT_STREQ(cmd, "help"); strcpy(cmd, "\n\nhelp\n\n\n"); tcp_cmd_remove_newline(cmd, strlen(cmd)); EXPECT_STREQ(cmd, "help"); strcpy(cmd, "\n\nhelp\n\n\n\n"); tcp_cmd_remove_newline(cmd, strlen(cmd)); EXPECT_STREQ(cmd, "help"); strcpy(cmd, "\n\nhelp\n\n\n\n\n"); tcp_cmd_remove_newline(cmd, strlen(cmd)); EXPECT_STREQ(cmd, "help"); strcpy(cmd, "\n\nhelp\n\n\n\n\n\n"); tcp_cmd_remove_newline(cmd, strlen(cmd)); EXPECT_STREQ(cmd, "help"); strcpy(cmd, "\n\nhelp\n\n\n\n\n\n\n"); tcp_cmd_remove_newline(cmd, strlen(cmd)); EXPECT_STREQ(cmd, "help"); strcpy(cmd, "\n\nhelp\n\n\n\n\n\n\n\n"); tcp_cmd_remove_newline(cmd, strlen(cmd)); EXPECT_STREQ(cmd, "help"); strcpy(cmd, "\n\nhelp\n\n\n\n\n\n\n\n\n"); tcp_cmd_remove_newline(cmd, strlen(cmd)); EXPECT_STREQ(cmd, "help"); strcpy(cmd, "\n\nhel\np\n"); tcp_cmd_remove_newline(cmd, strlen(cmd)); EXPECT_STREQ(cmd, "help"); free(cmd); } TEST(TCP_CMD, tcp_data_cb) { char* cmd = (char*)calloc(50, 1); std::string output; pbuf p = {.next = NULL, .payload = (void*)cmd, .tot_len = 4, .len = 0, .type_internal = 0, .flags = 0, .if_idx = 0}; strcpy(cmd, "help"); p.tot_len = (uint16_t)strlen(cmd); testing::internal::CaptureStdout(); tcp_cmd_recv(NULL, NULL, &p, ERR_OK); output = testing::internal::GetCapturedStdout(); #ifndef DEBUG EXPECT_EQ(output, "help : shows a list of commands\n" "clear text/images : clears the text or images on the lcd\n" "text \"\" : puts text on the lcd\n" "bgColor : set the background color of the lcd\n" "color : set the color of the text\n" "listFiles : shows a list with images in the filesystem\n" "setImage : put an image on the screen\n" "setGif : put a gif on the screen\n" "exit : closes the connection\n\n$>\n"); #else EXPECT_EQ(output, "tcp_recved\ntcp_write:\n" "help : shows a list of commands\n" "clear text/images : clears the text or images on the lcd\n" "text \"\" : puts text on the lcd\n" "bgColor : set the background color of the lcd\n" "color : set the color of the text\n" "listFiles : shows a list with images in the filesystem\n" "setImage : put an image on the screen\n" "setGif : put a gif on the screen\n" "exit : closes the connection\n" "\ntcp_output\ntcp_write:\n$>\ntcp_output\npbuf_free\n"); #endif strcpy(cmd, "text \"This is printed on the display\""); p.tot_len = (uint16_t)strlen(cmd); testing::internal::CaptureStdout(); tcp_cmd_recv(NULL, NULL, &p, ERR_OK); output = testing::internal::GetCapturedStdout(); #ifndef DEBUG EXPECT_EQ(output, "$>\n"); #else EXPECT_EQ(output, "tcp_recved\nlcd_display_text @ 10 10 with color 0xFF000000 bg color 0xFF000000\nThis is printed on the display\ntcp_write:\n$>\ntcp_output\npbuf_free\n"); #endif strcpy(cmd, "color 0x555555"); p.tot_len = (uint16_t)strlen(cmd); testing::internal::CaptureStdout(); tcp_cmd_recv(NULL, NULL, &p, ERR_OK); output = testing::internal::GetCapturedStdout(); #ifndef DEBUG EXPECT_EQ(output, "$>\n"); #else EXPECT_EQ(output, "tcp_recved\ntcp_write:\n$>\ntcp_output\npbuf_free\n"); #endif strcpy(cmd, "bgcolor 0xAAAAAA"); p.tot_len = (uint16_t)strlen(cmd); testing::internal::CaptureStdout(); tcp_cmd_recv(NULL, NULL, &p, ERR_OK); output = testing::internal::GetCapturedStdout(); #ifndef DEBUG EXPECT_EQ(output, "$>\n"); #else EXPECT_EQ(output, "tcp_recved\ntcp_write:\n$>\ntcp_output\npbuf_free\n"); #endif strcpy(cmd, "text \"This is printed on the display\""); p.tot_len = (uint16_t)strlen(cmd); testing::internal::CaptureStdout(); tcp_cmd_recv(NULL, NULL, &p, ERR_OK); output = testing::internal::GetCapturedStdout(); #ifndef DEBUG EXPECT_EQ(output, "$>\n"); #else EXPECT_EQ(output, "tcp_recved\nlcd_display_text @ 10 10 with color 0xFF555555 bg color 0xFFAAAAAA\nThis is printed on the display\ntcp_write:\n$>\ntcp_output\npbuf_free\n"); #endif strcpy(cmd, "color 255 255 255"); p.tot_len = (uint16_t)strlen(cmd); testing::internal::CaptureStdout(); tcp_cmd_recv(NULL, NULL, &p, ERR_OK); output = testing::internal::GetCapturedStdout(); #ifndef DEBUG EXPECT_EQ(output, "$>\n"); #else EXPECT_EQ(output, "tcp_recved\ntcp_write:\n$>\ntcp_output\npbuf_free\n"); #endif strcpy(cmd, "bgcolor 255 255 255"); p.tot_len = (uint16_t)strlen(cmd); testing::internal::CaptureStdout(); tcp_cmd_recv(NULL, NULL, &p, ERR_OK); output = testing::internal::GetCapturedStdout(); #ifndef DEBUG EXPECT_EQ(output, "$>\n"); #else EXPECT_EQ(output, "tcp_recved\ntcp_write:\n$>\ntcp_output\npbuf_free\n"); #endif strcpy(cmd, "text \"This is printed on the display\""); p.tot_len = (uint16_t)strlen(cmd); testing::internal::CaptureStdout(); tcp_cmd_recv(NULL, NULL, &p, ERR_OK); output = testing::internal::GetCapturedStdout(); #ifndef DEBUG EXPECT_EQ(output, "$>\n"); #else EXPECT_EQ(output, "tcp_recved\nlcd_display_text @ 10 10 with color 0xFFFFFFFF bg color 0xFFFFFFFF\nThis is printed on the display\ntcp_write:\n$>\ntcp_output\npbuf_free\n"); #endif strcpy(cmd, "setImage \"test.bmp\""); p.tot_len = (uint16_t)strlen(cmd); testing::internal::CaptureStdout(); tcp_cmd_recv(NULL, NULL, &p, ERR_OK); output = testing::internal::GetCapturedStdout(); #ifndef DEBUG EXPECT_EQ(output, "$>\n"); #else EXPECT_EQ(output, "tcp_recved\nlcd_draw_img_from_fs\ntest.bmp @0 0\ntcp_write:\n$>\ntcp_output\npbuf_free\n"); #endif strcpy(cmd, "setImage \"test.gif\""); p.tot_len = (uint16_t)strlen(cmd); testing::internal::CaptureStdout(); tcp_cmd_recv(NULL, NULL, &p, ERR_OK); output = testing::internal::GetCapturedStdout(); #ifndef DEBUG EXPECT_EQ(output, "[Warning] ( 0) [tcp_cmd]: setimage: File is not a bmp\r\nFile is not a bmp\n\n$>\n"); #else EXPECT_EQ(output, "tcp_recved\nlogger_get_timestamp\n[Warning] ( 0) [tcp_cmd]: setimage: File is not a bmp\r\ntcp_write:\nFile is not a bmp\n\ntcp_output\ntcp_write:\n$>\ntcp_output\npbuf_free\n"); #endif strcpy(cmd, "setGif \"test.gif\""); p.tot_len = (uint16_t)strlen(cmd); testing::internal::CaptureStdout(); tcp_cmd_recv(NULL, NULL, &p, ERR_OK); output = testing::internal::GetCapturedStdout(); #ifndef DEBUG EXPECT_EQ(output, "$>\n"); #else EXPECT_EQ(output, "tcp_recved\nlcd_draw_gif_from_fs\ntest.gif @0 0\ntcp_write:\n$>\ntcp_output\npbuf_free\n"); #endif strcpy(cmd, "setGif \"test.bmp\""); p.tot_len = (uint16_t)strlen(cmd); testing::internal::CaptureStdout(); tcp_cmd_recv(NULL, NULL, &p, ERR_OK); output = testing::internal::GetCapturedStdout(); #ifndef DEBUG EXPECT_EQ(output, "[Warning] ( 0) [tcp_cmd]: setgif: File is not a gif\r\nFile is not a gif\n\n$>\n"); #else EXPECT_EQ(output, "tcp_recved\nlogger_get_timestamp\n[Warning] ( 0) [tcp_cmd]: setgif: File is not a gif\r\ntcp_write:\nFile is not a gif\n\ntcp_output\ntcp_write:\n$>\ntcp_output\npbuf_free\n"); #endif strcpy(cmd, "exit"); p.tot_len = (uint16_t)strlen(cmd); testing::internal::CaptureStdout(); tcp_cmd_recv(NULL, NULL, &p, ERR_OK); output = testing::internal::GetCapturedStdout(); #ifndef DEBUG EXPECT_EQ(output, "Exiting...\n\n"); #else EXPECT_EQ(output, "tcp_recved\ntcp_write:\nExiting...\n\ntcp_output\nlcd_clear_images\nlcd_clear_text\ntcp_close\ntcp_arg\ntcp_sent\ntcp_recv\ntcp_close\npbuf_free\n"); #endif free(cmd); }