- code cleaned up

This commit is contained in:
Jan Hutter 2005-12-06 13:34:57 +00:00
parent b23b556ae7
commit 59de50868b
5 changed files with 92 additions and 103 deletions

View File

@ -223,16 +223,4 @@ struct mapping_t
*/
char *mapping_find(mapping_t *mappings, int value);
/**
* Default random device used when no device is given.
*/
#define DEFAULT_RANDOM_DEVICE "/dev/random"
/**
* Pseudo random device used when no device is given.
*/
#define DEFAULT_PSEUDO_RANDOM_DEVICE "/dev/urandom"
#endif /*DEFINITIONS_H_*/

View File

@ -29,15 +29,17 @@
#include "randomizer.h"
#include <utils/allocator.h>
#include <daemon.h>
typedef struct private_randomizer_t private_randomizer_t;
/**
* Private data of an randomizer_t object
* Private data of an randomizer_t object.
*/
struct private_randomizer_t {
/**
* Public interface.
* Public randomizer_t interface.
*/
randomizer_t public;
@ -45,16 +47,13 @@ struct private_randomizer_t {
* @brief Reads a specific number of bytes from random or pseudo random device.
*
* @param this calling object
* @param pseudo_random TRUE, if pseudo random bytes should be read,
* @param pseudo_random TRUE, if from pseudo random bytes should be read,
* FALSE for true random bytes
* @param bytes number of bytes to read
* @param[out] buffer pointer to buffer where to write the data in.
* Size of buffer has to be at least bytes.
* @return
* - SUCCESS
* - FAILED if random device could not be opened
*/
status_t (*get_bytes_from_device) (private_randomizer_t *this,bool pseudo_random, size_t bytes, u_int8_t *buffer);
void (*get_bytes_from_device) (private_randomizer_t *this,bool pseudo_random, size_t bytes, u_int8_t *buffer);
/**
* Random device name.
@ -71,7 +70,7 @@ struct private_randomizer_t {
/**
* Implementation of private_randomizer_t.get_bytes_from_device.
*/
static status_t get_bytes_from_device(private_randomizer_t *this,bool pseudo_random, size_t bytes, u_int8_t *buffer)
static void get_bytes_from_device(private_randomizer_t *this,bool pseudo_random, size_t bytes, u_int8_t *buffer)
{
/* number of bytes already done */
size_t ndone;
@ -85,31 +84,30 @@ static status_t get_bytes_from_device(private_randomizer_t *this,bool pseudo_ran
// open device
device = open(device_name, 0);
if (device < 0) {
return FAILED;
charon->kill(charon,"Random device could not be opened");
}
ndone = 0;
// read until nbytes are read
/* read until nbytes are read */
while (ndone < bytes)
{
got = read(device, buffer + ndone, bytes - ndone);
if (got < 0) {
return FAILED;
charon->kill(charon,"Read from random device failed");
}
if (got == 0) {
return FAILED;
charon->kill(charon,"Read from random device failed");
}
ndone += got;
}
// close device
/* close device */
close(device);
return SUCCESS;
}
/**
* Implementation of randomizer_t.get_random_bytes.
*/
static status_t get_random_bytes(private_randomizer_t *this,size_t bytes, u_int8_t *buffer)
static void get_random_bytes(private_randomizer_t *this,size_t bytes, u_int8_t *buffer)
{
return (this->get_bytes_from_device(this, FALSE, bytes, buffer));
}
@ -117,41 +115,31 @@ static status_t get_random_bytes(private_randomizer_t *this,size_t bytes, u_int8
/**
* Implementation of randomizer_t.allocate_random_bytes.
*/
static status_t allocate_random_bytes(private_randomizer_t *this, size_t bytes, chunk_t *chunk)
static void allocate_random_bytes(private_randomizer_t *this, size_t bytes, chunk_t *chunk)
{
chunk->len = bytes;
chunk->ptr = allocator_alloc(bytes);
if (chunk->ptr == NULL)
{
return OUT_OF_RES;
}
return (this->get_bytes_from_device(this, FALSE, bytes, chunk->ptr));
}
/**
* Implementation of randomizer_t.get_pseudo_random_bytes.
*/
static status_t get_pseudo_random_bytes(private_randomizer_t *this,size_t bytes, u_int8_t *buffer)
static void get_pseudo_random_bytes(private_randomizer_t *this,size_t bytes, u_int8_t *buffer)
{
return (this->get_bytes_from_device(this, TRUE, bytes, buffer));
}
/**
* Implementation of randomizer_t.allocate_pseudo_random_bytes.
*/
static status_t allocate_pseudo_random_bytes(private_randomizer_t *this, size_t bytes, chunk_t *chunk)
static void allocate_pseudo_random_bytes(private_randomizer_t *this, size_t bytes, chunk_t *chunk)
{
chunk->len = bytes;
chunk->ptr = allocator_alloc(bytes);
if (chunk->ptr == NULL)
{
return OUT_OF_RES;
}
return (this->get_bytes_from_device(this, TRUE, bytes, chunk->ptr));
}
/**
* Implementation of randomizer_t.destroy.
*/
@ -178,10 +166,10 @@ randomizer_t *randomizer_create_on_devices(char * random_dev_name,char * prandom
private_randomizer_t *this = allocator_alloc_thing(private_randomizer_t);
/* public functions */
this->public.get_random_bytes = (status_t (*) (randomizer_t *,size_t, u_int8_t *)) get_random_bytes;
this->public.allocate_random_bytes = (status_t (*) (randomizer_t *,size_t, chunk_t *)) allocate_random_bytes;
this->public.get_pseudo_random_bytes = (status_t (*) (randomizer_t *,size_t, u_int8_t *)) get_pseudo_random_bytes;
this->public.allocate_pseudo_random_bytes = (status_t (*) (randomizer_t *,size_t, chunk_t *)) allocate_pseudo_random_bytes;
this->public.get_random_bytes = (void (*) (randomizer_t *,size_t, u_int8_t *)) get_random_bytes;
this->public.allocate_random_bytes = (void (*) (randomizer_t *,size_t, chunk_t *)) allocate_random_bytes;
this->public.get_pseudo_random_bytes = (void (*) (randomizer_t *,size_t, u_int8_t *)) get_pseudo_random_bytes;
this->public.allocate_pseudo_random_bytes = (void (*) (randomizer_t *,size_t, chunk_t *)) allocate_pseudo_random_bytes;
this->public.destroy = (void (*) (randomizer_t *))destroy;
/* private functions */
@ -190,7 +178,6 @@ randomizer_t *randomizer_create_on_devices(char * random_dev_name,char * prandom
/* private fields */
this->random_dev_name = allocator_alloc(strlen(random_dev_name) + 1);
strcpy(this->random_dev_name,random_dev_name);
this->pseudo_random_dev_name = allocator_alloc(strlen(prandom_dev_name) + 1);
strcpy(this->pseudo_random_dev_name,prandom_dev_name);

View File

@ -25,6 +25,17 @@
#include <types.h>
/**
* Default random device used when no device is given.
*/
#define DEFAULT_RANDOM_DEVICE "/dev/random"
/**
* Pseudo random device used when no device is given.
*/
#define DEFAULT_PSEUDO_RANDOM_DEVICE "/dev/urandom"
typedef struct randomizer_t randomizer_t;
/**
@ -45,11 +56,8 @@ struct randomizer_t {
* @param bytes number of bytes to read
* @param[out] buffer pointer to buffer where to write the data in.
* Size of buffer has to be at least bytes.
* @return
* - SUCCESS
* - FAILED if random device could not be opened
*/
status_t (*get_random_bytes) (randomizer_t *this,size_t bytes, u_int8_t *buffer);
void (*get_random_bytes) (randomizer_t *this,size_t bytes, u_int8_t *buffer);
/**
* @brief Allocates space and writes in random bytes.
@ -57,11 +65,8 @@ struct randomizer_t {
* @param this calling randomizer_t object
* @param bytes number of bytes to allocate
* @param[out] chunk chunk which will hold the allocated random bytes
* @return
* - SUCCESS
* - FAILED if random device could not be opened
*/
status_t (*allocate_random_bytes) (randomizer_t *this, size_t bytes, chunk_t *chunk);
void (*allocate_random_bytes) (randomizer_t *this, size_t bytes, chunk_t *chunk);
/**
* @brief Reads a specific number of bytes from pseudo random device.
@ -70,11 +75,8 @@ struct randomizer_t {
* @param bytes number of bytes to read
* @param[out] buffer pointer to buffer where to write the data in.
* size of buffer has to be at least bytes.
* @return
* - SUCCESS
* - FAILED if random device could not be opened
*/
status_t (*get_pseudo_random_bytes) (randomizer_t *this,size_t bytes, u_int8_t *buffer);
void (*get_pseudo_random_bytes) (randomizer_t *this,size_t bytes, u_int8_t *buffer);
/**
* @brief Allocates space and writes in pseudo random bytes.
@ -82,11 +84,8 @@ struct randomizer_t {
* @param this calling randomizer_t object
* @param bytes number of bytes to allocate
* @param[out] chunk chunk which will hold the allocated random bytes
* @return
* - SUCCESS
* - FAILED if random device could not be opened
*/
status_t (*allocate_pseudo_random_bytes) (randomizer_t *this, size_t bytes, chunk_t *chunk);
void (*allocate_pseudo_random_bytes) (randomizer_t *this, size_t bytes, chunk_t *chunk);
/**
* @brief Destroys a randomizer_t object.
@ -112,9 +111,7 @@ randomizer_t *randomizer_create();
*
* @param random_dev_name device name for random values, etc /dev/random
* @param prandom_dev_name device name for pseudo random values, etc /dev/urandom
* @return
* - created randomizer_t
* - NULL if failed
* @return randomizer_t object
*
* @ingroup utils
*/

View File

@ -36,18 +36,16 @@
typedef struct private_tester_t private_tester_t;
/**
* @brief Private Variables and Functions of tester class.
* @brief Private Data of tester_t class.
*
*/
struct private_tester_t {
/**
* Public interface.
* Public interface of tester_t.
*/
tester_t public;
/* Private functions */
/**
* Runs a specific test.
*
@ -55,17 +53,31 @@ struct private_tester_t {
* @param test_function test function to perform
* @param test_name name for the given test
*/
void (*run_test) (tester_t *tester, void (*test_function) (tester_t * tester), char * test_name);
void (*run_test) (private_tester_t *tester, void (*test_function) (tester_t * tester), char * test_name);
/**
* Returns the difference of to timeval structs in microseconds.
*
* @warning this function is also defined in the event queue
* in later improvements, this function can be added to a general
* class type!
*
* @param end_time end time
* @param start_time start time
*
* @TODO make object function or move to utils!
*
* @return difference in microseconds
*/
long (*time_difference) (private_tester_t *tester,struct timeval *end_time, struct timeval *start_time);
/* Private values */
/**
* Output is written into this file.
*/
FILE* output;
/**
* Number of runned tests.
* Number of already performed tests.
*/
int tests_count;
@ -75,17 +87,17 @@ struct private_tester_t {
int failed_tests_count;
/**
* Number of failed asserts in curret test.
* Number of failed asserts in current test.
*/
int failed_asserts_count;
/**
* TRUE if succeeded asserts should also be written to output.
* TRUE if also succeeded asserts should be written to output.
*/
bool display_succeeded_asserts;
/**
* Mutex to make this object thread-save.
* Mutex to make this class thread-save.
*/
pthread_mutex_t mutex;
};
@ -93,9 +105,8 @@ struct private_tester_t {
/**
* Implementation of tester_t.perform_tests.
*/
static void perform_tests(tester_t *tester,test_t **tests)
static void perform_tests(private_tester_t *this,test_t **tests)
{
private_tester_t *this =(private_tester_t*) tester;
int current_test = 0;
fprintf(this->output,"\nStart testing...\n\n");
fprintf(this->output,"_____________________________________________________________________\n");
@ -104,7 +115,7 @@ static void perform_tests(tester_t *tester,test_t **tests)
while (tests[current_test] != NULL)
{
this->run_test(tester,tests[current_test]->test_function,tests[current_test]->test_name);
this->run_test(this,tests[current_test]->test_function,tests[current_test]->test_name);
current_test++;
}
fprintf(this->output,"=====================================================================\n");
@ -115,10 +126,10 @@ static void perform_tests(tester_t *tester,test_t **tests)
/**
* Implementation of tester_t.perform_test.
*/
static void perform_test(tester_t *tester, test_t *test)
static void perform_test(private_tester_t *this, test_t *test)
{
test_t *tests[] = {test, NULL};
return (perform_tests(tester,tests));
return (perform_tests(this,tests));
}
/**
@ -135,7 +146,7 @@ static void perform_test(tester_t *tester, test_t *test)
*
* @return difference in microseconds
*/
static long time_difference(struct timeval *end_time, struct timeval *start_time)
static long time_difference(private_tester_t *this,struct timeval *end_time, struct timeval *start_time)
{
long seconds, microseconds;
@ -148,18 +159,17 @@ static long time_difference(struct timeval *end_time, struct timeval *start_time
/**
* Implementation of private_tester_t.run_test.
*/
static void run_test(tester_t *tester, void (*test_function) (tester_t * tester), char * test_name)
static void run_test(private_tester_t *this, void (*test_function) (tester_t * tester), char * test_name)
{
struct timeval start_time, end_time;
long timediff;
private_tester_t *this = (private_tester_t *) tester;
this->tests_count++;
this->failed_asserts_count = 0;
fprintf(this->output,"%-55s\n", test_name);
gettimeofday(&start_time,NULL);
test_function(tester);
test_function(&(this->public));
gettimeofday(&end_time,NULL);
timediff = time_difference(&end_time, &start_time);
timediff = this->time_difference(this,&end_time, &start_time);
if (this->failed_asserts_count > 0)
{
@ -178,10 +188,8 @@ static void run_test(tester_t *tester, void (*test_function) (tester_t * tester)
/**
* Implementation of tester_t.assert_true.
*/
static void assert_true(tester_t *tester, bool to_be_true,char * assert_name)
static void assert_true(private_tester_t *this, bool to_be_true,char * assert_name)
{
private_tester_t *this = (private_tester_t *) tester;
if (assert_name == NULL)
{
assert_name = "unknown";
@ -205,15 +213,15 @@ static void assert_true(tester_t *tester, bool to_be_true,char * assert_name)
/**
* Implementation of tester_t.assert_false.
*/
static void assert_false(tester_t *tester, bool to_be_false,char * assert_name)
static void assert_false(private_tester_t *this, bool to_be_false,char * assert_name)
{
tester->assert_true(tester,(!to_be_false),assert_name);
this->public.assert_true(&(this->public),(!to_be_false),assert_name);
}
/**
* Implementation of tester_t.destroy.
*/
static void destroy(tester_t *tester)
static void destroy(private_tester_t *tester)
{
private_tester_t *this = (private_tester_t*) tester;
pthread_mutex_destroy(&(this->mutex));
@ -227,14 +235,18 @@ tester_t *tester_create(FILE *output, bool display_succeeded_asserts)
{
private_tester_t *this = allocator_alloc_thing(private_tester_t);
this->public.destroy = destroy;
this->public.perform_tests = perform_tests;
this->public.perform_test = perform_test;
this->public.assert_true = assert_true;
this->public.assert_false = assert_false;
/* public functions */
this->public.destroy = (void (*) (tester_t *))destroy;
this->public.perform_tests = (void (*) (tester_t *, test_t**)) perform_tests;
this->public.perform_test = (void (*) (tester_t *, test_t*))perform_test;
this->public.assert_true = (void (*) (tester_t *, bool, char*)) assert_true;
this->public.assert_false = (void (*) (tester_t *, bool, char*)) assert_false;
/* private functions */
this->run_test = run_test;
this->time_difference = time_difference;
/* private data */
this->display_succeeded_asserts = display_succeeded_asserts;
this->failed_tests_count = 0;
this->tests_count = 0;

View File

@ -30,6 +30,7 @@
typedef struct test_t test_t;
/* must be defined here cause it is used in test_t */
typedef struct tester_t tester_t;
/**
@ -44,6 +45,7 @@ struct test_t {
* @param tester associated tester_t object
*/
void (*test_function) (tester_t * tester);
/**
* Name of the test.
*/
@ -51,18 +53,21 @@ struct test_t {
};
/**
* A tester class to perform tests.
* @brief A class to perform tests.
*
* @b Constructors:
* - tester_create()
*
* @ingroup utils
*/
struct tester_t {
/**
* @brief Tests all testcases in array tests with specific tester_t object.
* @brief Test all testcases in array tests with specific tester_t object.
*
* @param tester tester_t object
* @param tests pointer to an array of test_t-pointers.
* The last item has to be NULL.
* The last item has to be NULL to mark end of array.
*/
void (*perform_tests) (tester_t *tester,test_t **tests);
@ -101,7 +106,7 @@ struct tester_t {
void (*assert_false) (tester_t *tester, bool to_be_false, char *assert_name);
/**
* @brief Destroys a tester_t object
* @brief Destroys a tester_t object.
*
* @param tester tester_t object
*/
@ -115,7 +120,7 @@ struct tester_t {
* @param display_succeeded_asserts has to be TRUE, if all asserts should be displayed,
* FALSE otherwise
*
* @return - tester_t object
* @return tester_t object
*
* @ingroup utils
*/