Commit 13888c93 authored by Ondřej Surý's avatar Ondřej Surý

Split isc_crc64 API test into separate unit test and convert it to cmocka

parent 7fc78e7c
......@@ -33,7 +33,7 @@ CMOCKA_LIBS = @CMOCKA_LIBS@
OBJS = isctest.@O@
SRCS = isctest.c aes_test.c buffer_test.c \
counter_test.c errno_test.c file_test.c hash_test.c \
counter_test.c crc64_test.c errno_test.c file_test.c hash_test.c \
heap_test.c hmac_test.c ht_test.c lex_test.c \
mem_test.c md_test.c netaddr_test.c parse_test.c pool_test.c \
queue_test.c radix_test.c random_test.c \
......@@ -43,7 +43,8 @@ SRCS = isctest.c aes_test.c buffer_test.c \
SUBDIRS =
TARGETS = aes_test@EXEEXT@ buffer_test@EXEEXT@ \
counter_test@EXEEXT@ errno_test@EXEEXT@ file_test@EXEEXT@ \
counter_test@EXEEXT@ crc64_test@EXEEXT@ \
errno_test@EXEEXT@ file_test@EXEEXT@ \
hash_test@EXEEXT@ heap_test@EXEEXT@ hmac_test@EXEEXT@ \
ht_test@EXEEXT@ \
lex_test@EXEEXT@ mem_test@EXEEXT@ md_test@EXEEXT@ \
......@@ -68,6 +69,10 @@ counter_test@EXEEXT@: counter_test.@O@ isctest.@O@ ${ISCDEPLIBS}
${LIBTOOL_MODE_LINK} ${PURIFY} ${CC} ${CFLAGS} ${LDFLAGS} -o $@ \
counter_test.@O@ isctest.@O@ ${ISCLIBS} ${LIBS}
crc64_test@EXEEXT@: crc64_test.@O@ ${ISCDEPLIBS}
${LIBTOOL_MODE_LINK} ${PURIFY} ${CC} ${CFLAGS} ${CMOCKA_CFLAGS} ${LDFLAGS} -o $@ \
crc64_test.@O@ ${ISCLIBS} ${LIBS} ${CMOCKA_LIBS}
errno_test@EXEEXT@: errno_test.@O@ ${ISCDEPLIBS}
${LIBTOOL_MODE_LINK} ${PURIFY} ${CC} ${CFLAGS} ${LDFLAGS} -o $@ \
errno_test.@O@ ${ISCLIBS} ${LIBS}
......
/*
* Copyright (C) Internet Systems Consortium, Inc. ("ISC")
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* See the COPYRIGHT file distributed with this work for additional
* information regarding copyright ownership.
*/
/* ! \file */
#include <config.h>
#if HAVE_CMOCKA
#include <stdarg.h>
#include <stddef.h>
#include <setjmp.h>
#include <stdlib.h>
#include <string.h>
#define UNIT_TESTING
#include <cmocka.h>
#include <isc/crc64.h>
#include <isc/result.h>
#include <isc/util.h>
#include <isc/print.h>
#define TEST_INPUT(x) (x), sizeof(x)-1
typedef struct hash_testcase {
const char *input;
size_t input_len;
const char *result;
int repeats;
} hash_testcase_t;
static void
isc_crc64_init_test(void **state) {
uint64_t crc;
UNUSED(state);
isc_crc64_init(&crc);
assert_int_equal(crc, 0xffffffffffffffffUL);
}
static void
_crc64(const char *buf, size_t buflen,
const char *result, const int repeats)
{
uint64_t crc;
isc_crc64_init(&crc);
assert_int_equal(crc, 0xffffffffffffffffUL);
for (int i = 0; i < repeats; i++) {
isc_crc64_update(&crc, buf, buflen);
}
isc_crc64_final(&crc);
char hex[16 + 1];
snprintf(hex, sizeof(hex), "%016" PRIX64, crc);
assert_memory_equal(hex, result, (result?strlen(result):0));
}
static void
isc_crc64_test(void **state) {
UNUSED(state);
_crc64(TEST_INPUT(""), "0000000000000000", 1);
_crc64(TEST_INPUT("a"), "CE73F427ACC0A99A", 1);
_crc64(TEST_INPUT("abc"), "048B813AF9F49702", 1);
_crc64(TEST_INPUT("message digest"), "5273F9EA7A357BF4", 1);
_crc64(TEST_INPUT("abcdefghijklmnopqrstuvwxyz"),
"59F079F9218BAAA1", 1);
_crc64(TEST_INPUT("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklm"
"nopqrstuvwxyz0123456789"),
"A36DA8F71E78B6FB", 1);
_crc64(TEST_INPUT("123456789012345678901234567890123456789"
"01234567890123456789012345678901234567890"),
"81E5EB73C8E7874A", 1);
}
int main(void) {
const struct CMUnitTest tests[] = {
/* isc_hmac_init() */
cmocka_unit_test(isc_crc64_init_test),
cmocka_unit_test(isc_crc64_test),
};
return (cmocka_run_group_tests(tests, NULL, NULL));
}
#else /* HAVE_CMOCKA */
#include <stdio.h>
int main(void) {
printf("1..0 # Skipped: cmocka not available\n");
return (0);
}
#endif
......@@ -24,7 +24,6 @@
#include <isc/hex.h>
#include <isc/region.h>
#include <isc/crc64.h>
#include <isc/util.h>
#include <isc/print.h>
#include <isc/string.h>
......@@ -40,71 +39,6 @@ typedef struct hash_testcase {
int repeats;
} hash_testcase_t;
/* CRC64 Test */
ATF_TC(isc_crc64);
ATF_TC_HEAD(isc_crc64, tc) {
atf_tc_set_md_var(tc, "descr", "64-bit cyclic redundancy check");
}
ATF_TC_BODY(isc_crc64, tc) {
uint64_t crc;
int i;
UNUSED(tc);
hash_testcase_t testcases[] = {
{
TEST_INPUT(""),
"0000000000000000", 1
},
{
TEST_INPUT("a"),
"CE73F427ACC0A99A", 1
},
{
TEST_INPUT("abc"),
"048B813AF9F49702", 1
},
{
TEST_INPUT("message digest"),
"5273F9EA7A357BF4", 1
},
{
TEST_INPUT("abcdefghijklmnopqrstuvwxyz"),
"59F079F9218BAAA1", 1
},
{
TEST_INPUT("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklm"
"nopqrstuvwxyz0123456789"),
"A36DA8F71E78B6FB", 1
},
{
TEST_INPUT("123456789012345678901234567890123456789"
"01234567890123456789012345678901234567890"),
"81E5EB73C8E7874A", 1
},
{ NULL, 0, NULL, 1 }
};
hash_testcase_t *testcase = testcases;
while (testcase->input != NULL && testcase->result != NULL) {
char str[19];
isc_crc64_init(&crc);
for(i = 0; i < testcase->repeats; i++) {
isc_crc64_update(&crc,
(const uint8_t *) testcase->input,
testcase->input_len);
}
isc_crc64_final(&crc);
snprintf(str, sizeof(str),
"0x%016" PRIX64, crc);
ATF_CHECK_STREQ(str, testcase->result);
testcase++;
}
}
ATF_TC(isc_hash_function);
ATF_TC_HEAD(isc_hash_function, tc) {
atf_tc_set_md_var(tc, "descr", "Hash function test");
......@@ -227,14 +161,12 @@ ATF_TC_BODY(isc_hash_initializer, tc) {
*/
ATF_TP_ADD_TCS(tp) {
/*
* Tests of hash functions, including isc_hash and the
* various cryptographic hashes.
* Tests of isc_hash functions.
*/
ATF_TP_ADD_TC(tp, isc_hash_function);
ATF_TP_ADD_TC(tp, isc_hash_function_reverse);
ATF_TP_ADD_TC(tp, isc_hash_initializer);
ATF_TP_ADD_TC(tp, isc_crc64);
return (atf_no_error());
}
......@@ -3503,6 +3503,7 @@
./lib/isc/tests/aes_test.c C 2014,2016,2018
./lib/isc/tests/buffer_test.c C 2014,2015,2016,2017,2018
./lib/isc/tests/counter_test.c C 2014,2016,2018
./lib/isc/tests/crc64_test.c C 2018
./lib/isc/tests/errno_test.c C 2016,2018
./lib/isc/tests/file_test.c C 2014,2016,2017,2018
./lib/isc/tests/hash_test.c C 2011,2012,2013,2014,2015,2016,2017,2018
......
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