libwmc: add more tests including for UML290 quirks

This commit is contained in:
Dan Williams
2011-09-19 13:42:00 -05:00
parent 46a5c77616
commit 8250dee131
7 changed files with 186 additions and 9 deletions
+13 -7
View File
@@ -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) {
+2
View File
@@ -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,
+41
View File
@@ -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);
}
+2
View File
@@ -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 */
+118 -2
View File
@@ -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);
}
+6
View File
@@ -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 */
+4
View File
@@ -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 ();