1
0
mirror of https://github.com/samba-team/samba.git synced 2024-12-25 23:21:54 +03:00
samba-mirror/lib/util/tests/genrand.c
Volker Lendecke e73ccc06ef Rely on /dev/urandom
This removes quite a bit of code. All reasonable systems have /dev/urandom
these days. Linux, Solaris and the BSDs do.  In case we find a system
without /dev/urandom, we will have to go hunting in other libraries.

The main reason for this is speed: On Ubuntu 14.04 doing direct reads from
/dev/urandom is 2-3 times faster than our md4 based code. On virtualized
FreeBSD 10 the difference is even larger.

My first approach was to use fopen/fread. It was even faster, but less
than twice as fast. So I thought we could save the additional complexity
when having to deal with throwing away buffers when forking and the
additional memory footprint per process.

With this simple generate_random_buffer it will be easier to adapt new
syscalls to get randomness.

Signed-off-by: Volker Lendecke <vl@samba.org>
Reviewed-by: Jeremy Allison <jra@samba.org>

Autobuild-User(master): Jeremy Allison <jra@samba.org>
Autobuild-Date(master): Tue Oct 13 04:25:39 CEST 2015 on sn-devel-104
2015-10-13 04:25:38 +02:00

60 lines
2.5 KiB
C

/*
Unix SMB/CIFS implementation.
local testing of random data routines.
Copyright (C) Jelmer Vernooij <jelmer@samba.org>
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 3 of the License, or
(at your option) any later version.
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.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "includes.h"
#include "torture/torture.h"
#include "torture/local/proto.h"
static bool test_check_password_quality(struct torture_context *tctx)
{
torture_assert(tctx, !check_password_quality(""), "empty password");
torture_assert(tctx, !check_password_quality("a"), "one char password");
torture_assert(tctx, !check_password_quality("aaaaaaaaaaaa"), "same char password");
torture_assert(tctx, !check_password_quality("BLA"), "multiple upcases password");
torture_assert(tctx, !check_password_quality("123"), "digits only");
torture_assert(tctx, !check_password_quality("matthiéu"), "not enough high symbols");
torture_assert(tctx, !check_password_quality("abcdééàçè"), "only lower case");
torture_assert(tctx, !check_password_quality("abcdééàçè+"), "only lower and symbols");
torture_assert(tctx, check_password_quality("abcdééàçè+ढ"), "valid");
torture_assert(tctx, check_password_quality("ç+ढ"), "valid");
torture_assert(tctx, check_password_quality("A2e"), "valid");
torture_assert(tctx, check_password_quality("BA2eLi443"), "valid");
return true;
}
static bool test_generate_random_str(struct torture_context *tctx)
{
TALLOC_CTX *mem_ctx = talloc_init(__FUNCTION__);
char *r = generate_random_str(mem_ctx, 10);
torture_assert_int_equal(tctx, strlen(r), 10, "right length generated");
r = generate_random_str(mem_ctx, 5);
torture_assert_int_equal(tctx, strlen(r), 5, "right length generated");
return true;
}
struct torture_suite *torture_local_genrand(TALLOC_CTX *mem_ctx)
{
struct torture_suite *suite = torture_suite_create(mem_ctx, "genrand");
torture_suite_add_simple_test(suite, "check_password_quality", test_check_password_quality);
torture_suite_add_simple_test(suite, "generate_random_str", test_generate_random_str);
return suite;
}