libswarm-ng

C implementation of BMT hasher, Swarmhash and Single Owner Chunk for swarm
git clone git://git.defalsify.org/libswarm-ng.git
Info | Log | Files | Refs | Submodules | README

check_bmt_malloc.c (2768B)


      1 #include <check.h>
      2 #include <stdlib.h>
      3 
      4 #include "bmt_malloc.h"
      5 #include "hex.h"
      6 #include "common.h"
      7 
      8 
      9 START_TEST(check_bmt_init) {
     10 	bmt_t bmt_context;
     11 	unsigned char *input = "foo";
     12 	unsigned char input_length = 3;
     13 	unsigned char data_length_bytes[] = {0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
     14 	int r;
     15 	
     16 	bmt_init(&bmt_context, input, input_length, 3);
     17 	ck_assert_mem_eq(bmt_context.buf, data_length_bytes, sizeof(long long));
     18 	ck_assert_mem_eq(bmt_context.w_ptr, input, 3);
     19 	bmt_free(&bmt_context);
     20 }
     21 END_TEST
     22 
     23 
     24 START_TEST(check_bmt_sum_foo) {
     25 	bmt_t bmt_context;
     26 	unsigned char *input = "foo";
     27 	unsigned char v_chk[SWARM_WORD_SIZE];
     28 	unsigned char input_length = 3;
     29 	int r;
     30 
     31 	bmt_init(&bmt_context, input, input_length, 3);
     32 	r = bmt_sum(&bmt_context);
     33 	ck_assert_int_eq(r, 0);
     34 
     35 	hex2bin(HASH_OF_FOO, v_chk);
     36 	ck_assert_mem_eq(bmt_context.buf, v_chk, SWARM_WORD_SIZE);
     37 	bmt_free(&bmt_context);
     38 }
     39 END_TEST
     40 
     41 
     42 START_TEST(check_bmt_sum_vector) {
     43 	bmt_t bmt_context;
     44 	int r;
     45 	int i;
     46 
     47 	int lengths[] = {
     48 		SWARM_WORD_SIZE - 1,
     49 		SWARM_WORD_SIZE,
     50 		SWARM_WORD_SIZE + 1,
     51 		SWARM_WORD_SIZE * 2 - 1,
     52 		SWARM_WORD_SIZE * 2,
     53 		SWARM_WORD_SIZE * 2 + 1,
     54 		//SWARM_BLOCK_SIZE - 1,
     55 		SWARM_BLOCK_SIZE,
     56 	};
     57 	unsigned char *vectors[] = {
     58 		"ece86edb20669cc60d142789d464d57bdf5e33cb789d443f608cbd81cfa5697d",
     59 		"0be77f0bb7abc9cd0abed640ee29849a3072ccfd1020019fe03658c38f087e02",
     60 		"3463b46d4f9d5bfcbf9a23224d635e51896c1daef7d225b86679db17c5fd868e",
     61 		"95510c2ff18276ed94be2160aed4e69c9116573b6f69faaeed1b426fea6a3db8",
     62 		"490072cc55b8ad381335ff882ac51303cc069cbcb8d8d3f7aa152d9c617829fe",
     63 		"541552bae05e9a63a6cb561f69edf36ffe073e441667dbf7a0e9a3864bb744ea",
     64 		//"",
     65 		"c10090961e7682a10890c334d759a28426647141213abda93b096b892824d2ef",
     66 	};
     67 	unsigned char v_chk[SWARM_WORD_SIZE];
     68 	unsigned char buf[SWARM_BLOCK_SIZE];
     69 	struct block_generator bg;
     70 
     71 	bg.m = 255;
     72 
     73 	for (i = 0; i < sizeof(vectors)/sizeof(vectors[0]); i++) {
     74 		bg.v = 0;
     75 
     76 		r = block_generate(&bg, buf, lengths[i]);
     77 		ck_assert_int_eq(r, lengths[i]);
     78 
     79 		bmt_init(&bmt_context, buf, lengths[i], lengths[i]);
     80 		r = bmt_sum(&bmt_context);
     81 		ck_assert_int_eq(r, 0);
     82 
     83 		hex2bin(vectors[i], v_chk);
     84 		ck_assert_mem_eq(bmt_context.buf, v_chk, SWARM_WORD_SIZE);
     85 	}
     86 	bmt_free(&bmt_context);
     87 }
     88 END_TEST
     89 
     90 
     91 Suite * common_suite(void) {
     92 	Suite *s;
     93 	TCase *tc;
     94 
     95 	s = suite_create("bmt");
     96 	tc = tcase_create("core");
     97 	tcase_add_test(tc, check_bmt_init);
     98 	//tcase_add_test(tc, check_bmt_sum_foo);
     99 	tcase_add_test(tc, check_bmt_sum_vector);
    100 	suite_add_tcase(s, tc);
    101 
    102 	return s;
    103 }
    104 
    105 int main(void) {
    106 	int n_fail;
    107 
    108 	Suite *s;
    109 	SRunner *sr;
    110 
    111 	s = common_suite();	
    112 	sr = srunner_create(s);
    113 
    114 	srunner_run_all(sr, CK_VERBOSE);
    115 	n_fail = srunner_ntests_failed(sr);
    116 	srunner_free(sr);
    117 
    118 	return (n_fail == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
    119 }