diff --git a/unit/test-mux.c b/unit/test-mux.c index 1f75d5d4..e9b55d0e 100644 --- a/unit/test-mux.c +++ b/unit/test-mux.c @@ -34,6 +34,7 @@ #include #include "gatmux.h" +#include "gsm0710.h" static int do_connect(const char *address, unsigned short port) { @@ -227,10 +228,339 @@ static void test_basic(void) //g_test_trap_assert_stderr("failed"); } +/* DLC 1, Open Channel */ +static const guint8 basic_open[] = { 0xF9, 0x07, 0x3F, 0x01, 0xDE, 0xF9 }; + +/* DLC 1, Close Channel */ +static char const basic_close[] = { 0xF9, 0x07, 0x53, 0x01, 0x3F, 0xF9 }; + +/* DLC 1, Data */ +static const guint8 basic_data[] = { 0x12, 0x34, 0x56 }; +static const guint8 basic_data_result[] = + { 0xF9, 0x07, 0xEF, 0x07, 0x12, 0x34, 0x56, 0xD3, 0xF9 }; + +/* DLC 1, Long Data */ +static const guint8 basic_long_frame[] = +{ 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xF0, + 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xF0, + 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xF0, + 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xF0, + 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xF0, + 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xF0, + 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xF0, + 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xF0, + 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xF0, + 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xF0, + 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xF0, + 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xF0, + 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xF0, + 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xF0, + 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xF0, + 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xF0, + 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xF0, +}; + +static const guint8 basic_long_frame_result[] = +{ 0xF9, 0x07, 0xEF, 0x10, 0x01, 0x12, 0x34, 0x56, + 0x78, 0x9A, 0xBC, 0xDE, 0xF0, 0x12, 0x34, 0x56, + 0x78, 0x9A, 0xBC, 0xDE, 0xF0, 0x12, 0x34, 0x56, + 0x78, 0x9A, 0xBC, 0xDE, 0xF0, 0x12, 0x34, 0x56, + 0x78, 0x9A, 0xBC, 0xDE, 0xF0, 0x12, 0x34, 0x56, + 0x78, 0x9A, 0xBC, 0xDE, 0xF0, 0x12, 0x34, 0x56, + 0x78, 0x9A, 0xBC, 0xDE, 0xF0, 0x12, 0x34, 0x56, + 0x78, 0x9A, 0xBC, 0xDE, 0xF0, 0x12, 0x34, 0x56, + 0x78, 0x9A, 0xBC, 0xDE, 0xF0, 0x12, 0x34, 0x56, + 0x78, 0x9A, 0xBC, 0xDE, 0xF0, 0x12, 0x34, 0x56, + 0x78, 0x9A, 0xBC, 0xDE, 0xF0, 0x12, 0x34, 0x56, + 0x78, 0x9A, 0xBC, 0xDE, 0xF0, 0x12, 0x34, 0x56, + 0x78, 0x9A, 0xBC, 0xDE, 0xF0, 0x12, 0x34, 0x56, + 0x78, 0x9A, 0xBC, 0xDE, 0xF0, 0x12, 0x34, 0x56, + 0x78, 0x9A, 0xBC, 0xDE, 0xF0, 0x12, 0x34, 0x56, + 0x78, 0x9A, 0xBC, 0xDE, 0xF0, 0x12, 0x34, 0x56, + 0x78, 0x9A, 0xBC, 0xDE, 0xF0, 0x12, 0x34, 0x56, + 0x78, 0x9A, 0xBC, 0xDE, 0xF0, 0x88, 0xF9 +}; + +static void test_fill_basic() +{ + guint8 control_frame[6]; + guint8 data_frame[128]; + guint8 long_frame[256]; + int s; + + s = gsm0710_basic_fill_frame(control_frame, 1, GSM0710_OPEN_CHANNEL, + NULL, 0); + g_assert(s == sizeof(basic_open)); + g_assert(memcmp(basic_open, control_frame, s) == 0); + + s = gsm0710_basic_fill_frame(control_frame, 1, GSM0710_CLOSE_CHANNEL, + NULL, 0); + g_assert(s == sizeof(basic_close)); + g_assert(memcmp(basic_close, control_frame, s) == 0); + + s = gsm0710_basic_fill_frame(data_frame, 1, GSM0710_DATA, + basic_data, sizeof(basic_data)); + g_assert(s == sizeof(basic_data_result)); + g_assert(memcmp(basic_data_result, data_frame, s) == 0); + + s = gsm0710_basic_fill_frame(long_frame, 1, GSM0710_DATA, + basic_long_frame, + sizeof(basic_long_frame)); + g_assert(s == sizeof(basic_long_frame_result)); + g_assert(memcmp(basic_long_frame_result, long_frame, s) == 0); +} + +/* DLC 1, Open Channel */ +static const guint8 advanced_open[] = { 0x7E, 0x07, 0x3F, 0x89, 0x7E }; + +/* DLC 1, Close Channel */ +static const guint8 advanced_close[] = { 0x7E, 0x07, 0x53, 0xC8, 0x7E }; + +/* DLC 1, Data */ +static const guint8 advanced_data[] = { 0x12, 0x34, 0x56 }; +static const guint8 advanced_data_result[] = + { 0x7E, 0x07, 0xEF, 0x12, 0x34, 0x56, 0x05, 0x7E }; + +/* DLC 1, Quoted data */ +static const guint8 advanced_quoted_data[] = + { 0x12, 0x34, 0x56, 0x7E, 0x78, 0x7D }; +static const guint8 advanced_quoted_data_result[] = + { 0x7E, 0x07, 0xEF, 0x12, 0x34, 0x56, 0x7D, 0x5E, 0x78, + 0x7D, 0x5D, 0x05, 0x7E }; + +static void test_fill_advanced() +{ + guint8 control_frame[8]; + guint8 data_frame[128]; + int s; + + s = gsm0710_advanced_fill_frame(control_frame, 1, GSM0710_OPEN_CHANNEL, + NULL, 0); + g_assert(s == sizeof(advanced_open)); + g_assert(memcmp(advanced_open, control_frame, s) == 0); + + s = gsm0710_advanced_fill_frame(control_frame, 1, GSM0710_CLOSE_CHANNEL, + NULL, 0); + g_assert(s == sizeof(advanced_close)); + g_assert(memcmp(advanced_close, control_frame, s) == 0); + + s = gsm0710_advanced_fill_frame(data_frame, 1, GSM0710_DATA, + advanced_data, sizeof(advanced_data)); + g_assert(s == sizeof(advanced_data_result)); + g_assert(memcmp(advanced_data_result, data_frame, s) == 0); + + s = gsm0710_advanced_fill_frame(data_frame, 1, GSM0710_DATA, + advanced_quoted_data, + sizeof(advanced_quoted_data)); + g_assert(s == sizeof(advanced_quoted_data_result)); + g_assert(memcmp(advanced_quoted_data_result, data_frame, s) == 0); +} + +static guint8 basic_input[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xF9, 0x07, 0xEF, + 0x07, 0x12, 0x34, 0x56, 0xD3, 0xF9, 0x07, 0xEF, 0x07, 0x12, 0x34, 0x56, + 0xD3, 0xF9 }; + +static guint8 basic_input2[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xF9, 0x07, 0xEF, + 0x07, 0x12, 0x34, 0x56, 0xD3, 0xF9, 0xF9, 0x07, 0xEF, 0x07, 0x12, + 0x34, 0x56, 0xD3, 0xF9 }; + +static int basic_garbage_size = 4; +static int basic_frame_size = 7; + +static const guint8 basic_output[] = { 0x12, 0x34, 0x56 }; + +static void test_extract_basic() +{ + int total = 0; + int nread; + guint8 dlc; + guint8 ctrl; + guint8 *frame; + int frame_size; + + frame = NULL; + frame_size = 0; + + nread = gsm0710_basic_extract_frame(basic_input + total, + basic_garbage_size, &dlc, &ctrl, + &frame, &frame_size); + + g_assert(frame == NULL); + g_assert(frame_size == 0); + + total += nread; + + /* Try to read with just the open flag */ + nread = gsm0710_basic_extract_frame(basic_input + total, + basic_frame_size + 1, &dlc, &ctrl, + &frame, &frame_size); + + g_assert(nread == 0); + g_assert(frame == NULL); + + /* Now read with the close flag as well */ + nread = gsm0710_basic_extract_frame(basic_input + total, + basic_frame_size + 2, &dlc, &ctrl, + &frame, &frame_size); + + /* Extracted the open flag + frame */ + g_assert(nread == basic_frame_size + 1); + g_assert(frame_size == sizeof(basic_output)); + g_assert(memcmp(basic_output, frame, frame_size) == 0); + + total += nread; + + nread = gsm0710_basic_extract_frame(basic_input + total, + sizeof(basic_input) - total, + &dlc, &ctrl, + &frame, &frame_size); + + g_assert(nread == (int)(sizeof(basic_input) - total - 1)); + g_assert(frame_size == sizeof(basic_output)); + g_assert(memcmp(basic_output, frame, frame_size) == 0); + + total += nread; + + nread = gsm0710_basic_extract_frame(basic_input + total, + sizeof(basic_input) - total, + &dlc, &ctrl, + &frame, &frame_size); + g_assert(nread == 0); + + total = 0; + + nread = gsm0710_basic_extract_frame(basic_input2 + total, + sizeof(basic_input2) - total, + &dlc, &ctrl, + &frame, &frame_size); + + g_assert(nread == basic_garbage_size + basic_frame_size + 1); + g_assert(frame_size == sizeof(basic_output)); + g_assert(memcmp(basic_output, frame, frame_size) == 0); + + total += nread; + + nread = gsm0710_basic_extract_frame(basic_input2 + total, + sizeof(basic_input2) - total, + &dlc, &ctrl, + &frame, &frame_size); + + g_assert(frame_size == sizeof(basic_output)); + g_assert(memcmp(basic_output, frame, frame_size) == 0); + + total += nread; + + g_assert(total == sizeof(basic_input2) - 1); +} + +static guint8 advanced_input[] = + { 0xFF, 0xFF, 0xFF, 0x7E, 0x07, 0xEF, 0x12, 0x34, 0x56, 0x05, 0x7E, + 0x07, 0xEF, 0x12, 0x34, 0x56, 0x05, 0x7E }; + +static guint8 advanced_input2[] = + { 0xFF, 0xFF, 0xFF, 0x7E, 0x07, 0xEF, 0x12, 0x34, 0x56, 0x05, 0x7E, + 0x07, 0xEF, 0x12, 0x34, 0x56, 0x05, 0x7E }; + +static int advanced_garbage_size = 3; +static int advanced_frame_size = 6; + +static const guint8 advanced_output[] = { 0x12, 0x34, 0x56 }; + +static void test_extract_advanced() +{ + int total = 0; + int nread; + guint8 dlc; + guint8 ctrl; + guint8 *frame; + int frame_size; + + frame = NULL; + frame_size = 0; + + nread = gsm0710_advanced_extract_frame(advanced_input + total, + advanced_garbage_size, + &dlc, &ctrl, + &frame, &frame_size); + + g_assert(frame == NULL); + g_assert(frame_size == 0); + + total += nread; + + /* Try to read with just the open flag */ + nread = gsm0710_advanced_extract_frame(advanced_input + total, + advanced_frame_size + 1, + &dlc, &ctrl, + &frame, &frame_size); + + g_assert(nread == 0); + g_assert(frame == NULL); + + /* Now read with the close flag as well */ + nread = gsm0710_advanced_extract_frame(advanced_input + total, + advanced_frame_size + 2, + &dlc, &ctrl, + &frame, &frame_size); + + /* Extracted the open flag + frame */ + g_assert(nread == advanced_frame_size + 1); + g_assert(frame_size == sizeof(advanced_output)); + g_assert(memcmp(advanced_output, frame, frame_size) == 0); + + total += nread; + + nread = gsm0710_advanced_extract_frame(advanced_input + total, + sizeof(advanced_input) - total, + &dlc, &ctrl, + &frame, &frame_size); + + g_assert(nread == (int)(sizeof(advanced_input) - total - 1)); + g_assert(frame_size == sizeof(advanced_output)); + g_assert(memcmp(advanced_output, frame, frame_size) == 0); + + total += nread; + + nread = gsm0710_advanced_extract_frame(advanced_input + total, + sizeof(advanced_input) - total, + &dlc, &ctrl, + &frame, &frame_size); + g_assert(nread == 0); + + total = 0; + + nread = gsm0710_advanced_extract_frame(advanced_input2 + total, + sizeof(advanced_input2) - total, + &dlc, &ctrl, + &frame, &frame_size); + + g_assert(nread == advanced_garbage_size + advanced_frame_size + 1); + g_assert(frame_size == sizeof(advanced_output)); + g_assert(memcmp(advanced_output, frame, frame_size) == 0); + + total += nread; + + nread = gsm0710_advanced_extract_frame(advanced_input2 + total, + sizeof(advanced_input2) - total, + &dlc, &ctrl, + &frame, &frame_size); + + g_assert(frame_size == sizeof(advanced_output)); + g_assert(memcmp(advanced_output, frame, frame_size) == 0); + + total += nread; + + g_assert(total == sizeof(advanced_input2) - 1); +} + int main(int argc, char **argv) { g_test_init(&argc, &argv, NULL); + g_test_add_func("/testmux/fill_basic", test_fill_basic); + g_test_add_func("/testmux/fill_advanced", test_fill_advanced); + g_test_add_func("/testmux/extract_basic", test_extract_basic); + g_test_add_func("/testmux/extract_advanced", test_extract_advanced); g_test_add_func("/testmux/basic", test_basic); return g_test_run();