Main Page | Modules | Class List | Directories | File List | Class Members | File Members | Related Pages

check_rand.c

Go to the documentation of this file.
00001 
00002 #include <sys/types.h>
00003 
00004 #include <dnet.h>
00005 
00006 #include <stdio.h>
00007 #include <stdlib.h>
00008 #include <string.h>
00009 
00010 #include <check.h>
00011 
00012 START_TEST(test_rand_openclose)
00013 {
00014         rand_t *r;
00015 
00016         fail_unless((r = rand_open()) != NULL, "open failed");
00017         fail_unless((r = rand_close(r)) == NULL, "close failed");
00018 }
00019 END_TEST
00020 
00021 START_TEST(test_rand_get)
00022 {
00023         rand_t *r;
00024         uint32_t i;
00025 
00026         r = rand_open();
00027         fail_unless(rand_get(r, &i, sizeof(i)) == 0, "get failed");
00028         /* XXX - add distribution tests */
00029         r = rand_close(r);
00030 }
00031 END_TEST
00032 
00033 START_TEST(test_rand_set)
00034 {
00035         rand_t *r;
00036         u_char buf1[BUFSIZ], buf2[BUFSIZ];
00037         uint32_t i;
00038 
00039         i = 666;
00040         r = rand_open();
00041         memset(buf1, 0, sizeof(buf1));
00042         memset(buf2, 0, sizeof(buf2));
00043         fail_unless(rand_get(r, buf1, sizeof(buf1)) == 0, "get failed");
00044         fail_unless(memcmp(buf1, buf2, sizeof(buf1)) != 0, "get b0rked");
00045         fail_unless(rand_set(r, &i, sizeof(i)) == 0, "set failed");
00046         fail_unless(rand_get(r, buf2, sizeof(buf2)) == 0, "get2 failed");
00047         fail_unless(memcmp(buf1, buf2, sizeof(buf1)) != 0, "set/get b0rked");
00048         fail_unless(rand_set(r, &i, sizeof(i)) == 0, "set2 failed");
00049         fail_unless(rand_get(r, buf1, sizeof(buf1)) == 0, "get3 failed");
00050         fail_unless(memcmp(buf1, buf2, sizeof(buf1)) == 0, "set b0rked");
00051         r = rand_close(r);
00052 }
00053 END_TEST
00054 
00055 START_TEST(test_rand_shuffle)
00056 {
00057         rand_t *r;
00058         char buf[BUFSIZ];
00059 
00060 #define SHUFFLE_TEXT    "hello how are you doing today?"
00061         r = rand_open();
00062         strcpy(buf, SHUFFLE_TEXT);
00063         rand_shuffle(r, buf, strlen(buf), 1);
00064         fail_unless(strcmp(buf, SHUFFLE_TEXT) != 0, "shuffle failed");
00065         r = rand_close(r);
00066 }
00067 END_TEST
00068         
00069 Suite *
00070 rand_suite(void)
00071 {
00072         Suite *s = suite_create("rand");
00073         TCase *tc_core = tcase_create("core");
00074 
00075         suite_add_tcase(s, tc_core);
00076         tcase_add_test(tc_core, test_rand_openclose);
00077         tcase_add_test(tc_core, test_rand_get);
00078         tcase_add_test(tc_core, test_rand_set);
00079         tcase_add_test(tc_core, test_rand_shuffle);
00080         
00081         return (s);
00082 }
00083 
00084 int
00085 main(void)
00086 {
00087         Suite *s = rand_suite();
00088         SRunner *sr = srunner_create(s);
00089         int nf;
00090         
00091         srunner_run_all (sr, CK_NORMAL);
00092         nf = srunner_ntests_failed(sr);
00093         srunner_free(sr);
00094         
00095         return (nf == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
00096 }

Generated on Sun May 14 14:51:12 2006 by  doxygen 1.4.2