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

check_blob.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_blob_newfree)
00013 {
00014         blob_t *b;
00015         
00016         fail_unless((b = blob_new()) != NULL, "new failed");
00017         fail_unless((b = blob_free(b)) == NULL, "free failed");
00018 }
00019 END_TEST
00020 
00021 START_TEST(test_blob_readwrite)
00022 {
00023         blob_t *b;
00024         char tmp[32];
00025         
00026         b = blob_new();
00027         blob_write(b, "foobar", 7);
00028         blob_write(b, "spazzo", 7);
00029         blob_write(b, "doofus", 7);
00030         blob_rewind(b);
00031 
00032         blob_read(b, tmp, 7);
00033         fail_unless(strcmp(tmp, "foobar") == 0, "read1 failed");
00034         blob_read(b, tmp, 7);
00035         fail_unless(strcmp(tmp, "spazzo") == 0, "read2 failed");
00036         blob_read(b, tmp, 7);
00037         fail_unless(strcmp(tmp, "doofus") == 0, "read3 failed");
00038         blob_free(b);
00039 }
00040 END_TEST
00041 
00042 START_TEST(test_blob_insertdelete)
00043 {
00044         blob_t *b, bf;
00045         char tmp[32];
00046         
00047         b = blob_new();
00048         fail_unless(blob_insert(b, "foo", 3) == 3, "insert1 failed");
00049         blob_rewind(b);
00050         fail_unless(blob_insert(b, "bar", 3) == 3, "insert2 failed");
00051         blob_rewind(b);
00052         blob_read(b, tmp, 6);
00053         tmp[6] = 0;
00054         fail_unless(strcmp(tmp,"barfoo") == 0, "read failed");
00055         blob_rewind(b);
00056         fail_unless(blob_delete(b, NULL, 3) == 3, "delete failed");
00057         blob_rewind(b);
00058         blob_read(b, tmp, 3);
00059         tmp[3] = 0;
00060         fail_unless(strcmp(tmp,"foo") == 0, "read failed");
00061         
00062         fail_unless(blob_delete(b, NULL, 4) < 0, "deleted more than size");
00063         b = blob_free(b);
00064 
00065         bf.base = "foobar";
00066         bf.end = 6;
00067         bf.off = bf.size = 0;
00068 
00069         fail_unless(blob_insert(&bf, "foobar", 6) < 0, "inserted into fixed");
00070         fail_unless(blob_delete(&bf, NULL, 3) < 0, "deleted from fixed"); 
00071 }
00072 END_TEST
00073 
00074 START_TEST(test_blob_packunpack)
00075 {
00076         blob_t *b;
00077         uint32_t D, d;
00078         uint16_t H, h;
00079         u_char c;
00080         char s[128];
00081         u_char buf[6];
00082 
00083         b = blob_new();
00084 
00085         D = 0xdeadbeef;
00086         H = 0xbabe;
00087         memcpy(buf, "f\x00\x00bar", 6);
00088         c = 'c';
00089         d = 555;
00090         h = 666;
00091         strcpy(s, "donkey");
00092 #if 0   
00093         printf("D: 0x%x H: 0x%x c: %c d: %d h: %d s: %s\n",
00094             D, H, c, d, h, s);
00095 #endif
00096         fail_unless(blob_pack(b, "whee:%D%H%*b%c%d%h%s\r\n",
00097             D, H, sizeof(buf), buf, c, d, h, s) == 0,
00098             "pack failed");
00099         
00100         blob_rewind(b);
00101 #if 0
00102         blob_print(b, "hexl", blob_left(b));
00103 #endif
00104         fail_unless(blob_unpack(b, "whee:%D%H%*b%c%d%h%*s\r\n",
00105             &D, &H, sizeof(buf), buf, &c, &d, &h, sizeof(s), s) == 0,
00106             "unpack failed");
00107 #if 0
00108         printf("D: 0x%x H: 0x%x c: %c d: %d h: %d s: %s\n",
00109             D, H, c, d, h, s);
00110 #endif
00111         if (D != 0xdeadbeef || H != 0xbabe ||
00112             memcmp(buf, "f\x00\x00bar", 6) != 0 || c != 'c' || d != 555 ||
00113             h != 666 || strcmp(s, "donkey") != 0)
00114                 fail("unpacked weird crap");
00115         
00116         blob_free(b);
00117 }
00118 END_TEST
00119 
00120 START_TEST(test_blob_seek)
00121 {
00122         blob_t *b;
00123 
00124         b = blob_new();
00125         blob_insert(b, "foobar", 6);
00126         blob_rewind(b);
00127         fail_unless(blob_skip(b, 3) == 3, "skip failed");
00128         fail_unless(blob_skip(b, 3) == 6, "skip to end failed");
00129         fail_unless(blob_skip(b, 1) < 0, "skipped past end");
00130         fail_unless(blob_seek(b, -1, SEEK_END) == 5, "end seek failed");
00131         fail_unless(blob_seek(b, 1, SEEK_SET) == 1, "set seek failed");
00132         blob_rewind(b);
00133         fail_unless(blob_seek(b, -1, SEEK_CUR) < 0, "seeked past 0");
00134         fail_unless(blob_seek(b, 3, SEEK_CUR) == 3, "cur seek failed");
00135         blob_free(b);
00136 }
00137 END_TEST
00138 
00139 START_TEST(test_blob_index)
00140 {
00141         blob_t *b;
00142 
00143         b = blob_new();
00144         blob_write(b, "this is only a test!", 20);
00145         blob_rewind(b);
00146         fail_unless(blob_index(b, "this", 4) == 0, "index start failed");
00147         fail_unless(blob_index(b, "!", 1) == 19, "index end failed");
00148         fail_unless(blob_index(b, "only ", 5) == 8, "index middle failed");
00149         fail_unless(blob_rindex(b, "!", 1) == 19, "rindex end failed");
00150         fail_unless(blob_rindex(b, "this", 4) == 0, "rindex start failed");
00151         fail_unless(blob_rindex(b, "only ", 5) == 8, "rindex middle failed");
00152         blob_free(b);
00153 }
00154 END_TEST
00155 
00156 Suite *
00157 blob_suite(void)
00158 {
00159         Suite *s = suite_create("blob");
00160         TCase *tc_core = tcase_create("core");
00161 
00162         suite_add_tcase(s, tc_core);
00163         tcase_add_test(tc_core, test_blob_newfree);
00164         tcase_add_test(tc_core, test_blob_readwrite);
00165         tcase_add_test(tc_core, test_blob_insertdelete);
00166         tcase_add_test(tc_core, test_blob_packunpack);
00167         tcase_add_test(tc_core, test_blob_seek);
00168         tcase_add_test(tc_core, test_blob_index);
00169         
00170         return (s);
00171 }
00172 
00173 int
00174 main(void)
00175 {
00176         Suite *s = blob_suite();
00177         SRunner *sr = srunner_create(s);
00178         int nf;
00179 #if 0
00180         srunner_set_fork_status(sr, CK_NOFORK);
00181 #endif
00182         srunner_run_all (sr, CK_NORMAL);
00183         nf = srunner_ntests_failed(sr);
00184         srunner_free(sr);
00185         
00186         return (nf == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
00187 }

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