Commit 0a6b66d0 authored by Davide Galassi's avatar Davide Galassi

CRC BE encoded byte array unit tests

parent d672b257
Pipeline #46740296 passed with stages
in 1 minute and 21 seconds
......@@ -35,5 +35,5 @@ void cry_crc32_digest(cry_crc32_ctx *ctx, unsigned char *out)
out[0] = (crc >> 24) & 0xFF;
out[1] = (crc >> 16) & 0xFF;
out[2] = (crc >> 8) & 0xFF;
out[4] = (crc & 0xFF);
out[3] = (crc & 0xFF);
}
......@@ -4,24 +4,86 @@
#define MSG ((unsigned char *)"ABCDEFGHIJKLMNOPQRSTUVWXYZ")
#define MSGLEN (strlen((char *)MSG))
static void crc16_ccitt_test(void)
#define CRC16_CCITT 0xad3b
#define CRC16_IBM 0xfe85
#define CRC32_ETH 0Xabf77822
/*
* CRC16 CCITT
*/
static void crc16_ccitt_wrp_test(void)
{
uint16_t crc;
#define CRC16_CCITT 0xad3b
crc = cry_crc16_ccitt(MSG, MSGLEN);
ASSERT_EQ(crc, CRC16_CCITT);
}
static void crc16_ibm_test(void)
static void crc16_ccitt_int_test(void)
{
uint16_t crc;
cry_crc16_ctx ctx;
cry_crc16_ccitt_init(&ctx);
cry_crc16_update(&ctx, MSG, MSGLEN);
crc = cry_crc16_final(&ctx);
ASSERT_EQ(crc, CRC16_CCITT);
}
static void crc16_ccitt_arr_test(void)
{
uint16_t crc;
unsigned char out[2];
cry_crc16_ctx ctx;
cry_crc16_ccitt_init(&ctx);
cry_crc16_update(&ctx, MSG, MSGLEN);
cry_crc16_digest(&ctx, out);
crc = ((uint16_t)out[0] << 8) | out[1];
ASSERT_EQ(crc, CRC16_CCITT);
}
/*
* CRC16 IBM
*/
static void crc16_ibm_wrp_test(void)
{
uint16_t crc;
#define CRC16_IBM 0xfe85
crc = cry_crc16_ibm(MSG, MSGLEN);
ASSERT_EQ(crc, CRC16_IBM);
}
static void crc16_ibm_int_test(void)
{
uint16_t crc;
cry_crc16_ctx ctx;
cry_crc16_ibm_init(&ctx);
cry_crc16_update(&ctx, MSG, MSGLEN);
crc = cry_crc16_final(&ctx);
ASSERT_EQ(crc, CRC16_IBM);
}
static void crc16_ibm_arr_test(void)
{
uint16_t crc;
unsigned char out[2];
cry_crc16_ctx ctx;
cry_crc16_ibm_init(&ctx);
cry_crc16_update(&ctx, MSG, MSGLEN);
cry_crc16_digest(&ctx, out);
crc = ((uint16_t)out[0] << 8) | out[1];
ASSERT_EQ(crc, CRC16_IBM);
}
/*
* CRC16 DNP
*/
/* CEI EN 60870-5-1 reference pattern */
static const unsigned char en_60870_5_1_pattern[] = {
0x00, 0x00, 0x00, 0x28, 0x30, 0x00, 0x3B, 0xE1,
......@@ -71,30 +133,86 @@ static void crc16_dnp_test(void)
}
}
static void crc32_eth_test(void)
/*
* CRC32 ETH
*/
static void crc32_eth_wrp_test(void)
{
uint32_t crc;
#define CRC32_ETH 0Xabf77822
crc = cry_crc32_eth(MSG, MSGLEN);
ASSERT_EQ(crc, CRC32_ETH);
}
static struct test_case tests[] = {
{ "CRC16 CCITT", crc16_ccitt_test },
{ "CRC16 IBM", crc16_ibm_test },
{ "CRC16 DNP", crc16_dnp_test },
{ "CRC32 ETH", crc32_eth_test },
static void crc32_eth_int_test(void)
{
uint32_t crc;
cry_crc32_ctx ctx;
cry_crc32_eth_init(&ctx);
cry_crc32_update(&ctx, MSG, MSGLEN);
crc = cry_crc32_final(&ctx);
ASSERT_EQ(crc, CRC32_ETH);
}
static void crc32_eth_arr_test(void)
{
uint32_t crc;
unsigned char out[4];
cry_crc32_ctx ctx;
cry_crc32_eth_init(&ctx);
cry_crc32_update(&ctx, MSG, MSGLEN);
cry_crc32_digest(&ctx, out);
crc = ((uint32_t)out[0] << 24) | ((uint32_t)out[1] << 16) |
((uint32_t)out[2] << 8) | out[3];
ASSERT_EQ(crc, CRC32_ETH);
}
/*
* SUBTESTS
*/
static struct test_case crc16_ccitt_tests[] = {
{ "CRC16 CCITT (wrp)", crc16_ccitt_wrp_test },
{ "CRC16 CCITT (int)", crc16_ccitt_int_test },
{ "CRC16 CCITT (arr)", crc16_ccitt_arr_test },
};
static struct test_case crc16_ibm_tests[] = {
{ "CRC16 IBM (wrp)", crc16_ibm_wrp_test },
{ "CRC16 IBM (int)", crc16_ibm_int_test },
{ "CRC16 IBM (arr)", crc16_ibm_arr_test },
};
static struct test_case crc16_dnp_tests[] = {
{ "CRC16 DNP (wrp)", crc16_dnp_test },
};
static struct test_case crc32_eth_tests[] = {
{ "CRC32 ETH (wrp)", crc32_eth_wrp_test },
{ "CRC32 ETH (int)", crc32_eth_int_test },
{ "CRC32 ETH (arr)", crc32_eth_arr_test },
};
#define NTESTS (sizeof(tests) / sizeof(tests[0]))
void crc_test(void)
{
int i;
printf("* CRC\n");
for (i = 0; i < NTESTS; i++)
run(tests[i].name, tests[i].func, NULL, NULL);
printf("* CRC16-CCITT\n");
for (i = 0; i < ARLEN(crc16_ccitt_tests); i++)
run(crc16_ccitt_tests[i].name, crc16_ccitt_tests[i].func, NULL, NULL);
printf("* CRC16-IBM\n");
for (i = 0; i < ARLEN(crc16_ibm_tests); i++)
run(crc16_ibm_tests[i].name, crc16_ibm_tests[i].func, NULL, NULL);
printf("* CRC16-DNP\n");
for (i = 0; i < ARLEN(crc16_dnp_tests); i++)
run(crc16_dnp_tests[i].name, crc16_dnp_tests[i].func, NULL, NULL);
printf("* CRC32-ETH\n");
for (i = 0; i < ARLEN(crc32_eth_tests); i++)
run(crc32_eth_tests[i].name, crc32_eth_tests[i].func, NULL, NULL);
printf("\n");
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment