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.c (2684B)


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