diff options
author | Dan Williams <dcbw@redhat.com> | 2011-09-19 13:42:00 -0500 |
---|---|---|
committer | Dan Williams <dcbw@redhat.com> | 2011-09-19 13:42:00 -0500 |
commit | 8250dee13177a164d61e31fe8d60900f9d5c6797 (patch) | |
tree | f3ade392879c752ff63b81bff254ac69ee5f3c67 /libwmc | |
parent | 46a5c77616a6fe22fa2a13a1f908b05a55e1ff31 (diff) |
libwmc: add more tests including for UML290 quirks
Diffstat (limited to 'libwmc')
-rw-r--r-- | libwmc/src/utils.c | 20 | ||||
-rw-r--r-- | libwmc/src/utils.h | 2 | ||||
-rw-r--r-- | libwmc/tests/test-wmc-escaping.c | 41 | ||||
-rw-r--r-- | libwmc/tests/test-wmc-escaping.h | 2 | ||||
-rw-r--r-- | libwmc/tests/test-wmc-utils.c | 120 | ||||
-rw-r--r-- | libwmc/tests/test-wmc-utils.h | 6 | ||||
-rw-r--r-- | libwmc/tests/test-wmc.c | 4 |
7 files changed, 186 insertions, 9 deletions
diff --git a/libwmc/src/utils.c b/libwmc/src/utils.c index 71f8a461..99c13db7 100644 --- a/libwmc/src/utils.c +++ b/libwmc/src/utils.c @@ -128,14 +128,15 @@ hdlc_escape (const char *inbuf, src = inbuf; i = inbuf_len; while (i--) { - if ( *src == DIAG_CONTROL_CHAR - || *src == DIAG_ESC_CHAR - || (escape_all_ctrl && *src <= 0x20)) { + guint8 byte = (guint8) *src++; + + if ( byte == DIAG_CONTROL_CHAR + || byte == DIAG_ESC_CHAR + || (escape_all_ctrl && byte <= 0x20)) { *dst++ = DIAG_ESC_CHAR; - *dst++ = *src ^ DIAG_ESC_MASK; + *dst++ = byte ^ DIAG_ESC_MASK; } else - *dst++ = *src; - src++; + *dst++ = byte; } return (dst - outbuf); @@ -273,6 +274,9 @@ uml290_wmc_encapsulate (char *inbuf, * hdlc_decapsulate_buffer: * @inbuf: buffer in which to look for an HDLC frame * @inbuf_len: length of valid data in @inbuf + * @check_known_crc: if %TRUE, validate the CRC using @known_crc + * @known_crc: if @check_known_crc is %TRUE, compare the frame's CRC against + * @known_crc. @known_crc must be in Little Endian (LE) byte order. * @outbuf: buffer in which to put decapsulated data from the HDLC frame * @outbuf_len: max size of @outbuf * @out_decap_len: on success, size of the decapsulated data @@ -296,6 +300,8 @@ uml290_wmc_encapsulate (char *inbuf, gboolean hdlc_decapsulate_buffer (const char *inbuf, gsize inbuf_len, + gboolean check_known_crc, + guint16 known_crc, char *outbuf, gsize outbuf_len, gsize *out_decap_len, @@ -359,7 +365,7 @@ hdlc_decapsulate_buffer (const char *inbuf, } /* Check the CRC of the packet's data */ - crc = crc16 (outbuf, unesc_len - 2, 0); + crc = check_known_crc ? known_crc : crc16 (outbuf, unesc_len - 2, 0); pkt_crc = outbuf[unesc_len - 2] & 0xFF; pkt_crc |= (outbuf[unesc_len - 1] & 0xFF) << 8; if (crc != pkt_crc) { diff --git a/libwmc/src/utils.h b/libwmc/src/utils.h index 1f2a7c83..4a7ab302 100644 --- a/libwmc/src/utils.h +++ b/libwmc/src/utils.h @@ -54,6 +54,8 @@ gsize uml290_wmc_encapsulate (char *inbuf, gboolean hdlc_decapsulate_buffer (const char *inbuf, gsize inbuf_len, + gboolean check_known_crc, + guint16 known_crc, char *outbuf, gsize outbuf_len, gsize *out_decap_len, diff --git a/libwmc/tests/test-wmc-escaping.c b/libwmc/tests/test-wmc-escaping.c index a49c04e4..cb268da7 100644 --- a/libwmc/tests/test-wmc-escaping.c +++ b/libwmc/tests/test-wmc-escaping.c @@ -104,6 +104,28 @@ test_escape2 (void *f, void *data) g_assert (memcmp (escaped, data2, len) == 0); } +static const char data_ctrl_src[] = { + 0xc8, 0x0d, 0xda, 0x07, 0x0c, 0x00, 0x14, 0x00, 0x10, 0x00, 0x19, 0x00, + 0x04, 0x00, 0x01, 0x00, 0x07, 0x88 +}; + +static const char data_ctrl_expected[] = { + 0xc8, 0x7d, 0x2d, 0xda, 0x7d, 0x27, 0x7d, 0x2c, 0x7d, 0x20, 0x7d, 0x34, + 0x7d, 0x20, 0x7d, 0x30, 0x7d, 0x20, 0x7d, 0x39, 0x7d, 0x20, 0x7d, 0x24, + 0x7d, 0x20, 0x7d, 0x21, 0x7d, 0x20, 0x7d, 0x27, 0x88 +}; + +void +test_escape_ctrl (void *f, void *data) +{ + char escaped[1024]; + gsize len; + + len = hdlc_escape (data_ctrl_src, sizeof (data_ctrl_src), TRUE, escaped, sizeof (escaped)); + g_assert (len == sizeof (data_ctrl_expected)); + g_assert (memcmp (escaped, data_ctrl_expected, len) == 0); +} + void test_escape_unescape (void *f, void *data) { @@ -122,3 +144,22 @@ test_escape_unescape (void *f, void *data) g_assert (memcmp (unescaped, data1, unlen) == 0); } +void +test_escape_unescape_ctrl (void *f, void *data) +{ + char escaped[512]; + char unescaped[512]; + gsize len, unlen; + gboolean escaping = FALSE; + + /* Ensure that escaping data that needs escaping, and then unescaping it, + * produces the exact same data as was originally escaped. + */ + len = hdlc_escape (data_ctrl_src, sizeof (data_ctrl_src), TRUE, escaped, sizeof (escaped)); + g_assert (memcmp (escaped, data_ctrl_expected, len) == 0); + + unlen = hdlc_unescape (escaped, len, unescaped, sizeof (unescaped), &escaping); + g_assert (unlen == sizeof (data_ctrl_src)); + g_assert (memcmp (unescaped, data_ctrl_src, unlen) == 0); +} + diff --git a/libwmc/tests/test-wmc-escaping.h b/libwmc/tests/test-wmc-escaping.h index 7b5c08c4..144c37bf 100644 --- a/libwmc/tests/test-wmc-escaping.h +++ b/libwmc/tests/test-wmc-escaping.h @@ -20,7 +20,9 @@ void test_escape1 (void *f, void *data); void test_escape2 (void *f, void *data); +void test_escape_ctrl (void *f, void *data); void test_escape_unescape (void *f, void *data); +void test_escape_unescape_ctrl (void *f, void *data); #endif /* TEST_WMC_ESCAPING_H */ diff --git a/libwmc/tests/test-wmc-utils.c b/libwmc/tests/test-wmc-utils.c index 1f903ca8..c38694ae 100644 --- a/libwmc/tests/test-wmc-utils.c +++ b/libwmc/tests/test-wmc-utils.c @@ -53,7 +53,7 @@ test_utils_decapsulate_basic_buffer (void *f, void *data) gboolean more = FALSE; success = hdlc_decapsulate_buffer (decap_inbuf, sizeof (decap_inbuf), - outbuf, sizeof (outbuf), + FALSE, 0, outbuf, sizeof (outbuf), &decap_len, &used, &more); g_assert (success); g_assert (decap_len == 214); @@ -100,8 +100,124 @@ test_utils_decapsulate_sierra_cns (void *f, void *data) gboolean more = FALSE; success = hdlc_decapsulate_buffer (cns_inbuf, sizeof (cns_inbuf), - outbuf, sizeof (outbuf), + FALSE, 0, outbuf, sizeof (outbuf), &decap_len, &used, &more); g_assert (success == FALSE); } + +static const char uml290_encap_src[] = { + 0xc8, 0x0d, 0xda, 0x07, 0x0c, 0x00, 0x14, 0x00, 0x10, 0x00, 0x19, 0x00, + 0x04, 0x00, 0x01, 0x00, 0x07, 0x88 +}; + +static const char uml290_encap_outbuf[] = { + 0x41, 0x54, 0x2a, 0x57, 0x4d, 0x43, 0x3d, 0xc8, 0x7d, 0x2d, 0xda, 0x7d, + 0x27, 0x7d, 0x2c, 0x7d, 0x20, 0x7d, 0x34, 0x7d, 0x20, 0x7d, 0x30, 0x7d, + 0x20, 0x7d, 0x39, 0x7d, 0x20, 0x7d, 0x24, 0x7d, 0x20, 0x7d, 0x21, 0x7d, + 0x20, 0x7d, 0x27, 0x88, 0x0d +}; + +void +test_utils_encapsulate_uml290_wmc1 (void *f, void *data) +{ + char inbuf[512]; + char outbuf[512]; + gsize encap_len = 0; + + memcpy (inbuf, uml290_encap_src, sizeof (uml290_encap_src)); + encap_len = uml290_wmc_encapsulate (inbuf, sizeof (uml290_encap_src), + sizeof (inbuf), outbuf, sizeof (outbuf)); + g_assert (encap_len == sizeof (encap_outbuf)); + g_assert (memcmp (outbuf, encap_outbuf, encap_len) == 0); +} + +static const char uml290_src[] = { + 0xc8, 0x0b, 0x00, 0x00, 0x00, 0x00, 0x0e, 0x00, 0xda, 0x07, 0x0c, 0x00, + 0x14, 0x00, 0x12, 0x00, 0x19, 0x00, 0x06, 0x00, 0xc2, 0x02, 0x00, 0x00, + 0x00, 0x00, 0x7d, 0x5d, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x7d, 0x5d, 0x00, 0x00, 0x00, 0x01, 0x00, + 0x00, 0x00, 0x01, 0x56, 0x65, 0x72, 0x69, 0x7a, 0x6f, 0x6e, 0x20, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x39, + 0x00, 0x00, 0x00, 0x40, 0x06, 0x00, 0x00, 0x30, 0x30, 0x7e +}; + +static const char uml290_expected[] = { + 0xc8, 0x0b, 0x00, 0x00, 0x00, 0x00, 0x0e, 0x00, 0xda, 0x07, 0x0c, 0x00, + 0x14, 0x00, 0x12, 0x00, 0x19, 0x00, 0x06, 0x00, 0xc2, 0x02, 0x00, 0x00, + 0x00, 0x00, 0x7d, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x7d, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, + 0x01, 0x56, 0x65, 0x72, 0x69, 0x7a, 0x6f, 0x6e, 0x20, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x39, 0x00, 0x00, + 0x00, 0x40, 0x06, 0x00, 0x00 +}; + +void +test_utils_decapsulate_uml290_wmc1 (void *f, void *data) +{ + gboolean success; + char outbuf[512]; + gsize decap_len = 0; + gsize used = 0; + gboolean more = FALSE; + + success = hdlc_decapsulate_buffer (uml290_src, sizeof (uml290_src), + TRUE, 0x3030, outbuf, sizeof (outbuf), + &decap_len, &used, &more); + g_assert (success == TRUE); + g_assert (more == 0); + g_assert_cmpint (used, ==, sizeof (uml290_src)); + g_assert_cmpint (decap_len, ==, sizeof (uml290_expected)); + g_assert_cmpint (memcmp (outbuf, uml290_expected, decap_len), ==, 0); +} + + +static const char pc5740_src[] = { + 0xc8, 0x0b, 0x17, 0x00, 0x00, 0x00, 0x06, 0x00, 0xdb, 0x07, 0x06, 0x00, + 0x11, 0x00, 0x0d, 0x00, 0x2d, 0x00, 0x10, 0x00, 0xe4, 0x03, 0xd4, 0xfe, + 0xff, 0xff, 0x4e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x0e, 0x92, 0x7e +}; + +static const char pc5740_expected[] = { + 0xc8, 0x0b, 0x17, 0x00, 0x00, 0x00, 0x06, 0x00, 0xdb, 0x07, 0x06, 0x00, + 0x11, 0x00, 0x0d, 0x00, 0x2d, 0x00, 0x10, 0x00, 0xe4, 0x03, 0xd4, 0xfe, + 0xff, 0xff, 0x4e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00 +}; + +void +test_utils_decapsulate_pc5740_wmc1 (void *f, void *data) +{ + gboolean success; + char outbuf[512]; + gsize decap_len = 0; + gsize used = 0; + gboolean more = FALSE; + + success = hdlc_decapsulate_buffer (pc5740_src, sizeof (pc5740_src), + FALSE, 0, outbuf, sizeof (outbuf), + &decap_len, &used, &more); + g_assert (success == TRUE); + g_assert (more == 0); + g_assert_cmpint (used, ==, sizeof (pc5740_src)); + g_assert_cmpint (decap_len, ==, sizeof (pc5740_expected)); + g_assert_cmpint (memcmp (outbuf, pc5740_expected, decap_len), ==, 0); +} + diff --git a/libwmc/tests/test-wmc-utils.h b/libwmc/tests/test-wmc-utils.h index a639a90f..96427068 100644 --- a/libwmc/tests/test-wmc-utils.h +++ b/libwmc/tests/test-wmc-utils.h @@ -24,5 +24,11 @@ void test_utils_encapsulate_basic_buffer (void *f, void *data); void test_utils_decapsulate_sierra_cns (void *f, void *data); +void test_utils_encapsulate_uml290_wmc1 (void *f, void *data); + +void test_utils_decapsulate_uml290_wmc1 (void *f, void *data); + +void test_utils_decapsulate_pc5740_wmc1 (void *f, void *data); + #endif /* TEST_WMC_UTILS_H */ diff --git a/libwmc/tests/test-wmc.c b/libwmc/tests/test-wmc.c index 53405e6e..c465d36b 100644 --- a/libwmc/tests/test-wmc.c +++ b/libwmc/tests/test-wmc.c @@ -42,10 +42,14 @@ int main (int argc, char **argv) g_test_suite_add (suite, TESTCASE (test_crc16_2, NULL)); g_test_suite_add (suite, TESTCASE (test_escape1, NULL)); g_test_suite_add (suite, TESTCASE (test_escape2, NULL)); + g_test_suite_add (suite, TESTCASE (test_escape_ctrl, NULL)); g_test_suite_add (suite, TESTCASE (test_escape_unescape, NULL)); + g_test_suite_add (suite, TESTCASE (test_escape_unescape_ctrl, NULL)); g_test_suite_add (suite, TESTCASE (test_utils_decapsulate_basic_buffer, NULL)); g_test_suite_add (suite, TESTCASE (test_utils_encapsulate_basic_buffer, NULL)); g_test_suite_add (suite, TESTCASE (test_utils_decapsulate_sierra_cns, NULL)); + g_test_suite_add (suite, TESTCASE (test_utils_decapsulate_uml290_wmc1, NULL)); + g_test_suite_add (suite, TESTCASE (test_utils_decapsulate_pc5740_wmc1, NULL)); result = g_test_run (); |