From: Andreas Steffen Date: Tue, 9 Dec 2014 06:44:36 +0000 (+0100) Subject: unit-tests: added bliss_sampler test X-Git-Tag: 5.2.2rc1~26 X-Git-Url: http://git.ipfire.org/gitweb/gitweb.cgi?a=commitdiff_plain;h=7442d2a2083f865a37252a532454bd61ca73fcb3;p=thirdparty%2Fstrongswan.git unit-tests: added bliss_sampler test --- diff --git a/src/libstrongswan/plugins/bliss/Makefile.am b/src/libstrongswan/plugins/bliss/Makefile.am index d2010de3aa..3db481933c 100644 --- a/src/libstrongswan/plugins/bliss/Makefile.am +++ b/src/libstrongswan/plugins/bliss/Makefile.am @@ -32,6 +32,7 @@ check_PROGRAMS = $(TESTS) bliss_tests_SOURCES = \ suites/test_bliss_fft.c \ suites/test_bliss_bitpacker.c \ + suites/test_bliss_sampler.c \ suites/test_bliss_sign.c \ bliss_fft_params.c \ bliss_fft.c \ diff --git a/src/libstrongswan/plugins/bliss/bliss_tests.h b/src/libstrongswan/plugins/bliss/bliss_tests.h index d857b3a576..67b0cdd234 100644 --- a/src/libstrongswan/plugins/bliss/bliss_tests.h +++ b/src/libstrongswan/plugins/bliss/bliss_tests.h @@ -15,5 +15,6 @@ TEST_SUITE(bliss_fft_suite_create) TEST_SUITE(bliss_bitpacker_suite_create) +TEST_SUITE(bliss_sampler_suite_create) TEST_SUITE(bliss_sign_suite_create) diff --git a/src/libstrongswan/plugins/bliss/suites/test_bliss_sampler.c b/src/libstrongswan/plugins/bliss/suites/test_bliss_sampler.c new file mode 100644 index 0000000000..1bd1266ad8 --- /dev/null +++ b/src/libstrongswan/plugins/bliss/suites/test_bliss_sampler.c @@ -0,0 +1,97 @@ +/* + * Copyright (C) 2014 Andreas Steffen + * HSR Hochschule fuer Technik Rapperswil + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation; either version 2 of the License, or (at your + * option) any later version. See . + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * for more details. + */ + +#include "test_suite.h" + +#include + +static u_int key_size[] = { 1, 3, 4}; + +START_TEST(test_bliss_sampler_gaussian) +{ + bliss_sampler_t *sampler; + bliss_param_set_t *set; + int i, k, count; + uint32_t hist[8], sign[3]; + int32_t z; + hash_algorithm_t alg; + size_t seed_len; + chunk_t seed; + + set = bliss_param_set_get_by_id(key_size[_i]); + alg = HASH_SHA256; + seed_len = 32; + count = 10000000; + + seed = chunk_alloc(seed_len); + memset(seed.ptr, 0xcc, seed_len); + + for (k = 0; k < 3; k++) + { + sign[k] = 0; + } + for (k = 0; k < 8; k++) + { + hist[k] = 0; + } + + sampler = bliss_sampler_create(alg, seed, set); + for (i = 0; i < count; i++) + { + ck_assert(sampler->gaussian(sampler, &z)); + if (z == 0) + { + sign[1]++; + hist[0]++; + } + else if (z > 0) + { + sign[2]++; + hist[z/256]++; + } + else + { + sign[0]++; + hist[(-z)/256]++; + } + } + sampler->destroy(sampler); + free(seed.ptr); + + DBG1(DBG_LIB, "histogram"); + for (k = 0; k < 8; k++) + { + DBG1(DBG_LIB, "%d %7d", k, hist[k]); + } + DBG1(DBG_LIB, "- %7d", sign[0]); + DBG1(DBG_LIB, "0 %7d", sign[1]); + DBG1(DBG_LIB, "+ %7d", sign[2]); +} +END_TEST + +Suite *bliss_sampler_suite_create() +{ + Suite *s; + TCase *tc; + + s = suite_create("bliss_sampler"); + + tc = tcase_create("sampler_gaussian"); + tcase_set_timeout(tc, 10); + tcase_add_loop_test(tc, test_bliss_sampler_gaussian, 0, countof(key_size)); + suite_add_tcase(s, tc); + + return s; +}