2011-07-28 23:40:09 +00:00
|
|
|
/*
|
2018-02-23 09:53:12 +01:00
|
|
|
* Copyright (C) Internet Systems Consortium, Inc. ("ISC")
|
2011-07-28 23:40:09 +00:00
|
|
|
*
|
2016-06-27 14:56:38 +10:00
|
|
|
* 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/.
|
2018-02-23 09:53:12 +01:00
|
|
|
*
|
|
|
|
* See the COPYRIGHT file distributed with this work for additional
|
|
|
|
* information regarding copyright ownership.
|
2011-07-28 23:40:09 +00:00
|
|
|
*/
|
|
|
|
|
2018-11-14 20:33:45 +08:00
|
|
|
#if HAVE_CMOCKA
|
|
|
|
|
2020-02-12 13:59:18 +01:00
|
|
|
#include <inttypes.h>
|
|
|
|
#include <setjmp.h>
|
2018-11-14 20:33:45 +08:00
|
|
|
#include <stdarg.h>
|
|
|
|
#include <stddef.h>
|
2011-07-28 23:40:09 +00:00
|
|
|
#include <stdio.h>
|
2018-11-14 20:33:45 +08:00
|
|
|
#include <stdlib.h>
|
2020-02-12 13:59:18 +01:00
|
|
|
#include <string.h>
|
2018-11-14 20:33:45 +08:00
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#define UNIT_TESTING
|
|
|
|
#include <cmocka.h>
|
2011-07-28 23:40:09 +00:00
|
|
|
|
2018-06-01 09:31:59 +02:00
|
|
|
#include <isc/buffer.h>
|
2016-02-08 14:51:53 +05:30
|
|
|
#include <isc/hash.h>
|
2018-06-01 09:31:59 +02:00
|
|
|
#include <isc/hex.h>
|
2015-05-23 14:21:51 +02:00
|
|
|
#include <isc/print.h>
|
2019-07-30 21:08:40 +02:00
|
|
|
#include <isc/region.h>
|
2011-07-28 23:40:09 +00:00
|
|
|
#include <isc/string.h>
|
2019-07-30 21:08:40 +02:00
|
|
|
#include <isc/util.h>
|
2011-07-28 23:40:09 +00:00
|
|
|
|
2016-08-19 08:02:51 +10:00
|
|
|
#include <pk11/site.h>
|
|
|
|
|
2020-02-12 13:59:18 +01:00
|
|
|
#define TEST_INPUT(x) (x), sizeof(x) - 1
|
2018-06-01 09:31:59 +02:00
|
|
|
|
2018-11-14 20:33:45 +08:00
|
|
|
/*Hash function test */
|
|
|
|
static void
|
2020-02-12 13:59:18 +01:00
|
|
|
isc_hash_function_test(void **state)
|
|
|
|
{
|
2016-02-08 14:51:53 +05:30
|
|
|
unsigned int h1;
|
|
|
|
unsigned int h2;
|
|
|
|
|
2018-11-14 20:33:45 +08:00
|
|
|
UNUSED(state);
|
2016-02-08 14:51:53 +05:30
|
|
|
|
|
|
|
/* Immutability of hash function */
|
2019-04-04 13:51:09 +02:00
|
|
|
h1 = isc_hash_function(NULL, 0, true);
|
|
|
|
h2 = isc_hash_function(NULL, 0, true);
|
2016-02-08 14:51:53 +05:30
|
|
|
|
2018-11-14 20:33:45 +08:00
|
|
|
assert_int_equal(h1, h2);
|
2016-02-08 14:51:53 +05:30
|
|
|
|
|
|
|
/* Hash function characteristics */
|
2019-04-04 13:51:09 +02:00
|
|
|
h1 = isc_hash_function("Hello world", 12, true);
|
|
|
|
h2 = isc_hash_function("Hello world", 12, true);
|
2016-02-08 14:51:53 +05:30
|
|
|
|
2018-11-14 20:33:45 +08:00
|
|
|
assert_int_equal(h1, h2);
|
2016-02-08 14:51:53 +05:30
|
|
|
|
|
|
|
/* Case */
|
2019-04-04 13:51:09 +02:00
|
|
|
h1 = isc_hash_function("Hello world", 12, false);
|
|
|
|
h2 = isc_hash_function("heLLo WorLd", 12, false);
|
2016-02-08 14:51:53 +05:30
|
|
|
|
2018-11-14 20:33:45 +08:00
|
|
|
assert_int_equal(h1, h2);
|
2016-02-08 14:51:53 +05:30
|
|
|
|
|
|
|
/* Unequal */
|
2019-04-04 13:51:09 +02:00
|
|
|
h1 = isc_hash_function("Hello world", 12, true);
|
|
|
|
h2 = isc_hash_function("heLLo WorLd", 12, true);
|
2016-02-08 14:51:53 +05:30
|
|
|
|
2018-11-14 20:33:45 +08:00
|
|
|
assert_int_not_equal(h1, h2);
|
2016-02-08 14:51:53 +05:30
|
|
|
}
|
|
|
|
|
2018-11-14 20:33:45 +08:00
|
|
|
/* Hash function initializer test */
|
|
|
|
static void
|
2020-02-12 13:59:18 +01:00
|
|
|
isc_hash_initializer_test(void **state)
|
|
|
|
{
|
2016-02-12 00:22:45 -08:00
|
|
|
unsigned int h1;
|
|
|
|
unsigned int h2;
|
|
|
|
|
2018-11-14 20:33:45 +08:00
|
|
|
UNUSED(state);
|
2016-02-12 00:22:45 -08:00
|
|
|
|
2019-04-04 13:51:09 +02:00
|
|
|
h1 = isc_hash_function("Hello world", 12, true);
|
|
|
|
h2 = isc_hash_function("Hello world", 12, true);
|
2016-02-12 00:22:45 -08:00
|
|
|
|
2018-11-14 20:33:45 +08:00
|
|
|
assert_int_equal(h1, h2);
|
2016-02-12 00:22:45 -08:00
|
|
|
|
|
|
|
isc_hash_set_initializer(isc_hash_get_initializer());
|
|
|
|
|
|
|
|
/* Hash value must not change */
|
2019-04-04 13:51:09 +02:00
|
|
|
h2 = isc_hash_function("Hello world", 12, true);
|
2016-02-12 00:22:45 -08:00
|
|
|
|
2018-11-14 20:33:45 +08:00
|
|
|
assert_int_equal(h1, h2);
|
2016-02-12 00:22:45 -08:00
|
|
|
}
|
|
|
|
|
2018-11-14 20:33:45 +08:00
|
|
|
int
|
2020-02-12 13:59:18 +01:00
|
|
|
main(void)
|
|
|
|
{
|
2018-11-14 20:33:45 +08:00
|
|
|
const struct CMUnitTest tests[] = {
|
|
|
|
cmocka_unit_test(isc_hash_function_test),
|
|
|
|
cmocka_unit_test(isc_hash_initializer_test),
|
|
|
|
};
|
|
|
|
|
|
|
|
return (cmocka_run_group_tests(tests, NULL, NULL));
|
|
|
|
}
|
2018-01-17 14:33:21 +01:00
|
|
|
|
2018-11-14 20:33:45 +08:00
|
|
|
#else /* HAVE_CMOCKA */
|
2016-02-08 14:51:53 +05:30
|
|
|
|
2018-11-14 20:33:45 +08:00
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
int
|
2020-02-12 13:59:18 +01:00
|
|
|
main(void)
|
|
|
|
{
|
2018-11-14 20:33:45 +08:00
|
|
|
printf("1..0 # Skipped: cmocka not available\n");
|
|
|
|
return (0);
|
2011-07-28 23:40:09 +00:00
|
|
|
}
|
2018-11-14 20:33:45 +08:00
|
|
|
|
|
|
|
#endif
|