int main(int argc, char *argv[]) { int i; int j; int len; uint16_t crc16a; uint16_t crc16b; printf("CRC module tests\n"); /* TODO: This doesn't check every function in the module */ /* Try a few random messages through the CRC logic. */ printf("Testing the CRC-16 routines\n"); for (i = 0; i < 100; i++) { ref_len = cook_up_msg(buf); len = crc_itu16_append(buf, ref_len); if (!crc_itu16_check(buf, len)) { printf("CRC-16 failure\n"); exit(2); } } printf("Test passed.\n\n"); printf("Testing the CRC-16 byte by byte and bit by bit routines\n"); for (i = 0; i < 100; i++) { ref_len = cook_up_msg(buf); crc16a = 0xFFFF; crc16a = crc_itu16_calc(buf, ref_len, crc16a); crc16b = 0xFFFF; for (j = 0; j < ref_len; j++) crc16b = crc_itu16_bits(buf[j], 8, crc16b); if (crc16a != crc16b) { printf("CRC-16 failure\n"); exit(2); } } printf("Test passed.\n\n"); printf("Testing the CRC-32 routines\n"); for (i = 0; i < 100; i++) { ref_len = cook_up_msg(buf); len = crc_itu32_append(buf, ref_len); if (!crc_itu32_check(buf, len)) { printf("CRC-32 failure\n"); exit(2); } } printf("Test passed.\n"); return 0; }
int main(int argc, char *argv[]) { int i; int len; printf("HDLC module tests\n"); /* TODO: This doesn't check every function in the module */ /* Try a few random messages through the CRC logic. */ printf("Testing the CRC-16 routines\n"); for (i = 0; i < 100; i++) { ref_len = cook_up_msg(buf); len = crc_itu16_append(buf, ref_len); if (!crc_itu16_check(buf, len)) { printf("CRC-16 failure\n"); exit(2); } } printf("Test passed.\n\n"); printf("Testing the CRC-32 routines\n"); for (i = 0; i < 100; i++) { ref_len = cook_up_msg(buf); len = crc_itu32_append(buf, ref_len); if (!crc_itu32_check(buf, len)) { printf("CRC-32 failure\n"); exit(2); } } printf("Test passed.\n"); return 0; }
static int test_hdlc_octet_count_handling(void) { int i; int j; int nextbyte; printf("Testing the octet_counting handling using CRC-16 (bit by bit)\n"); frame_len_errors = 0; frame_data_errors = 0; hdlc_tx_init(&tx, FALSE, 2, FALSE, underflow_handler, NULL); hdlc_rx_init(&rx, FALSE, TRUE, 0, frame_handler, NULL); //hdlc_rx_set_max_frame_len(&rx, 50); hdlc_rx_set_octet_counting_report_interval(&rx, 16); underflow_reported = FALSE; framing_ok_reported = FALSE; framing_ok_reports = 0; hdlc_tx_flags(&tx, 10); /* Don't push an initial message so we should get an underflow after the preamble. */ /* Lie for the first message, as there isn't really one */ frame_handled = TRUE; frame_failed = FALSE; frames_sent = 0; bytes_sent = 0; ref_len = 0; for (i = 0; i < 8*1000000; i++) { nextbyte = hdlc_tx_get_bit(&tx); hdlc_rx_put_bit(&rx, nextbyte); if (framing_ok_reported) { printf("Framing OK reported at bit %d (%d)\n", i, framing_ok_reports); framing_ok_reported = FALSE; } if (underflow_reported) { underflow_reported = FALSE; for (j = 0; j < 20; j++) { nextbyte = hdlc_tx_get_bit(&tx); hdlc_rx_put_bit(&rx, nextbyte); if (framing_ok_reported) { printf("Framing OK reported at bit %d (%d) - %d\n", i, framing_ok_reports, frame_handled); framing_ok_reported = FALSE; } } if (ref_len) { frames_sent++; bytes_sent += ref_len; } if (!frame_handled) { if (frame_failed) printf("Frame failed.\n"); printf("Frame not received.\n"); return -1; } ref_len = cook_up_msg(buf); hdlc_tx_frame(&tx, buf, ref_len); hdlc_tx_abort(&tx); //hdlc_tx_corrupt_frame(&tx); frame_handled = FALSE; } } printf("Tests passed.\n"); return 0; }
static int test_hdlc_crc_error_handling(void) { int i; int j; int nextbyte; int corrupt; printf("Testing CRC error handling using CRC-16 (bit by bit)\n"); frame_len_errors = 0; frame_data_errors = 0; hdlc_tx_init(&tx, FALSE, 2, FALSE, underflow_handler, NULL); hdlc_rx_init(&rx, FALSE, TRUE, 5, frame_handler, NULL); underflow_reported = FALSE; framing_ok_reported = FALSE; framing_ok_reports = 0; hdlc_tx_flags(&tx, 10); /* Don't push an initial message so we should get an underflow after the preamble. */ /* Lie for the first message, as there isn't really one */ frame_handled = TRUE; frame_failed = FALSE; frames_sent = 0; bytes_sent = 0; ref_len = 100; corrupt = FALSE; for (i = 0; i < 8*1000000; i++) { nextbyte = hdlc_tx_get_bit(&tx); hdlc_rx_put_bit(&rx, nextbyte); if (framing_ok_reported) { printf("Framing OK reported at bit %d (%d)\n", i, framing_ok_reports); framing_ok_reported = FALSE; } if (underflow_reported) { underflow_reported = FALSE; for (j = 0; j < 20; j++) { nextbyte = hdlc_tx_get_bit(&tx); hdlc_rx_put_bit(&rx, nextbyte); if (framing_ok_reported) { printf("Framing OK reported at bit %d (%d) - %d\n", i, framing_ok_reports, frame_handled); framing_ok_reported = FALSE; } } if (ref_len) { frames_sent++; bytes_sent += ref_len; } if (!frame_handled) { if (!corrupt) { printf("Frame not received when it should be correct.\n"); return -1; } } else { if (corrupt) { printf("Frame received when it should be corrupt.\n"); return -1; } } ref_len = cook_up_msg(buf); hdlc_tx_frame(&tx, buf, ref_len); if ((corrupt = rand() & 1)) hdlc_tx_corrupt_frame(&tx); frame_handled = FALSE; } } printf("Tests passed.\n"); return 0; }
static int test_hdlc_modes(void) { int i; int j; int len; int nextbyte; int progress; int progress_delay; uint8_t bufx[100]; /* Try sending HDLC messages with CRC-16 */ printf("Testing with CRC-16 (byte by byte)\n"); frame_len_errors = 0; frame_data_errors = 0; hdlc_tx_init(&tx, FALSE, 1, FALSE, underflow_handler, NULL); hdlc_rx_init(&rx, FALSE, FALSE, 5, frame_handler, NULL); underflow_reported = FALSE; start = rdtscll(); hdlc_tx_flags(&tx, 40); /* Push an initial message so we should NOT get an underflow after the preamble. */ ref_len = cook_up_msg(buf); hdlc_tx_frame(&tx, buf, ref_len); frame_handled = FALSE; frame_failed = FALSE; frames_sent = 0; bytes_sent = 0; for (i = 0; i < 1000000; i++) { nextbyte = hdlc_tx_get_byte(&tx); hdlc_rx_put_byte(&rx, nextbyte); if (underflow_reported) { underflow_reported = FALSE; nextbyte = hdlc_tx_get_byte(&tx); hdlc_rx_put_byte(&rx, nextbyte); frames_sent++; bytes_sent += ref_len; if (!frame_handled) { printf("Frame not received.\n"); return -1; } ref_len = cook_up_msg(buf); hdlc_tx_frame(&tx, buf, ref_len); frame_handled = FALSE; } } end = rdtscll(); check_result(); /* Now try sending HDLC messages with CRC-16 */ printf("Testing with CRC-16 (chunk by chunk)\n"); frame_len_errors = 0; frame_data_errors = 0; hdlc_tx_init(&tx, FALSE, 1, FALSE, underflow_handler, NULL); hdlc_rx_init(&rx, FALSE, FALSE, 5, frame_handler, NULL); underflow_reported = FALSE; start = rdtscll(); hdlc_tx_flags(&tx, 40); /* Push an initial message so we should NOT get an underflow after the preamble. */ ref_len = cook_up_msg(buf); hdlc_tx_frame(&tx, buf, ref_len); frame_handled = FALSE; frame_failed = FALSE; frames_sent = 0; bytes_sent = 0; for (i = 0; i < 10000; i++) { len = hdlc_tx_get(&tx, bufx, 100); hdlc_rx_put(&rx, bufx, len); if (underflow_reported) { underflow_reported = FALSE; len = hdlc_tx_get(&tx, bufx, 100); hdlc_rx_put(&rx, bufx, len); frames_sent++; bytes_sent += ref_len; if (!frame_handled) { printf("Frame not received.\n"); return -1; } ref_len = cook_up_msg(buf); hdlc_tx_frame(&tx, buf, ref_len); frame_handled = FALSE; } } end = rdtscll(); check_result(); /* Now try sending HDLC messages with CRC-16 bit by bit */ printf("Testing with CRC-16 (bit by bit)\n"); frame_len_errors = 0; frame_data_errors = 0; hdlc_tx_init(&tx, FALSE, 2, FALSE, underflow_handler, NULL); hdlc_rx_init(&rx, FALSE, FALSE, 5, frame_handler, NULL); underflow_reported = FALSE; start = rdtscll(); hdlc_tx_flags(&tx, 40); /* Don't push an initial message so we should get an underflow after the preamble. */ /* Lie for the first message, as there isn't really one */ frame_handled = TRUE; frame_failed = FALSE; frames_sent = 0; bytes_sent = 0; ref_len = 0; for (i = 0; i < 8*1000000; i++) { nextbyte = hdlc_tx_get_bit(&tx); hdlc_rx_put_bit(&rx, nextbyte); if (underflow_reported) { underflow_reported = FALSE; for (j = 0; j < 20; j++) { nextbyte = hdlc_tx_get_bit(&tx); hdlc_rx_put_bit(&rx, nextbyte); } if (ref_len) { frames_sent++; bytes_sent += ref_len; } if (!frame_handled) { printf("Frame not received.\n"); return -1; } ref_len = cook_up_msg(buf); hdlc_tx_frame(&tx, buf, ref_len); frame_handled = FALSE; } } end = rdtscll(); check_result(); /* Now try sending HDLC messages with CRC-32 */ printf("Testing with CRC-32 (byte by byte)\n"); frame_len_errors = 0; frame_data_errors = 0; hdlc_tx_init(&tx, TRUE, 1, FALSE, underflow_handler, NULL); hdlc_rx_init(&rx, TRUE, FALSE, 1, frame_handler, NULL); underflow_reported = FALSE; start = rdtscll(); hdlc_tx_flags(&tx, 40); /* Don't push an initial message so we should get an underflow after the preamble. */ /* Lie for the first message, as there isn't really one */ frame_handled = TRUE; frame_failed = FALSE; frames_sent = 0; bytes_sent = 0; ref_len = 0; for (i = 0; i < 1000000; i++) { nextbyte = hdlc_tx_get_byte(&tx); hdlc_rx_put_byte(&rx, nextbyte); if (underflow_reported) { underflow_reported = FALSE; nextbyte = hdlc_tx_get_byte(&tx); hdlc_rx_put_byte(&rx, nextbyte); if (ref_len) { frames_sent++; bytes_sent += ref_len; } if (!frame_handled) { printf("Frame not received.\n"); return -1; } ref_len = cook_up_msg(buf); hdlc_tx_frame(&tx, buf, ref_len); frame_handled = FALSE; } } end = rdtscll(); check_result(); /* Now try progressive mode with CRC-16 */ printf("Testing progressive mode with CRC-16 (byte by byte)\n"); frame_len_errors = 0; frame_data_errors = 0; hdlc_tx_init(&tx, TRUE, 1, TRUE, underflow_handler, NULL); hdlc_rx_init(&rx, TRUE, FALSE, 1, frame_handler, NULL); underflow_reported = FALSE; start = rdtscll(); hdlc_tx_flags(&tx, 40); /* Don't push an initial message so we should get an underflow after the preamble. */ /* Lie for the first message, as there isn't really one */ frame_handled = TRUE; frame_failed = FALSE; progress = 9999; progress_delay = 9999; frames_sent = 0; bytes_sent = 0; ref_len = 0; for (i = 0; i < 1000000; i++) { nextbyte = hdlc_tx_get_byte(&tx); hdlc_rx_put_byte(&rx, nextbyte); if (underflow_reported) { underflow_reported = FALSE; nextbyte = hdlc_tx_get_byte(&tx); hdlc_rx_put_byte(&rx, nextbyte); if (ref_len) { frames_sent++; bytes_sent += ref_len; } if (!frame_handled) { printf("Frame not received.\n"); return -1; } ref_len = cook_up_msg(buf); hdlc_tx_frame(&tx, buf, 10); progress = 10; progress_delay = 8; frame_handled = FALSE; } if (progress < ref_len && progress_delay-- <= 0) { if (hdlc_tx_frame(&tx, buf + progress, (progress + 10 <= ref_len) ? 10 : ref_len - progress) < 0) { printf("Failed to add progressively\n"); return -1; } progress += 10; progress_delay = 8; } } end = rdtscll(); check_result(); printf("Tests passed.\n"); return 0; }