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


      1 #include <check.h>
      2 #include <stdlib.h>
      3 
      4 #include "soc.h"
      5 #include "hex.h"
      6 #include "common.h"
      7 #include "def.h"
      8 #include "keystore.h"
      9 
     10 
     11 START_TEST(check_soc_identifier) {
     12 	int i;
     13 	int r; 
     14 	char out[84];
     15 	char in[52];
     16 
     17 	for (i = 0; i < 52; i++) {
     18 		in[i] = i;
     19 	}
     20 
     21 	r = soc_identifier(out, in, in+20);
     22 	ck_assert_int_eq(r, 0);
     23 }
     24 END_TEST
     25 
     26 
     27 START_TEST(check_soc_address) {
     28 	int i;
     29 	int r; 
     30 	char out[84];
     31 	char in[52];
     32 
     33 	for (i = 51; i >= 0; i--) {
     34 		in[i] = i;
     35 	}
     36 
     37 	r = soc_identifier(out, in, in+32);
     38 	ck_assert_int_eq(r, 0);
     39 }
     40 END_TEST
     41 
     42 
     43 START_TEST(check_soc_digest) {
     44 	soc_chunk_t chunk;
     45 	int i;
     46 	int r; 
     47 	char out[96];
     48 
     49 	r = soc_digest(&chunk, out);
     50 	ck_assert_int_eq(r, 0);
     51 }
     52 END_TEST
     53 
     54 
     55 START_TEST(check_soc_verify) {
     56 	int r;
     57 	soc_chunk_t chunk;
     58 	keystore_key_t key;
     59 	keystore_key_t *key_returned;
     60 	keystore_t keystore;
     61 	keystore_t *keystore_returned;
     62 	unsigned char digest[96];
     63 
     64 	keystore_returned = keystore_init(&keystore);
     65 	ck_assert_ptr_nonnull(keystore_returned);
     66 
     67 	hex2bin(TEST_PRIVATE_KEY, key.pk);
     68 
     69 	key_returned = keystore_put(&keystore, &key, NULL, 0);
     70 	ck_assert_ptr_nonnull(key_returned);
     71 
     72 	r = key_from_private(&key);
     73 	ck_assert_int_eq(r, 0);
     74 
     75 	r = soc_digest(&chunk, digest);
     76 	ck_assert_int_eq(r, 0);
     77 
     78 	keystore_sign(&keystore, chunk.signature, 0, digest);
     79 		
     80 	r = soc_verify(&chunk, &key);
     81 	ck_assert_int_eq(r, 0);
     82 
     83 	keystore_free(&keystore);
     84 }
     85 END_TEST
     86 
     87 
     88 START_TEST(check_soc_serialize) {
     89 	soc_chunk_t soc_chunk;
     90 	long long span;
     91 	unsigned char wire[4096*2];
     92 	unsigned char wire_out[4096*2];
     93 	size_t wire_length;
     94 	unsigned char *wire_returned;
     95 	struct block_generator bg;
     96 	int crsr;
     97 
     98 	bg.v = 0;
     99 	bg.m = 256;
    100 
    101 	block_generate(&bg, (char*)soc_chunk.identifier, SWARM_SOC_IDENTIFIER_SIZE);
    102 	block_generate(&bg, (char*)soc_chunk.signature, SWARM_SIGNATURE_SIZE);
    103 
    104 	block_generate(&bg, soc_chunk.data.hash, SWARM_WORD_SIZE);
    105 	soc_chunk.data.payload_sz = 1324;
    106 	span = 2435;
    107 	memcpy(&soc_chunk.data.span, &span, SWARM_DATA_LENGTH_TYPESIZE);
    108 	block_generate(&bg, (char*)wire, soc_chunk.data.payload_sz);
    109 	soc_chunk.data.payload = wire;
    110 	memcpy(wire_out, wire, soc_chunk.data.payload_sz);
    111 
    112 	wire_returned = soc_serialize(&soc_chunk, wire_out, &wire_length);
    113 
    114 	crsr = 0;
    115 	ck_assert_mem_eq(&soc_chunk, wire_returned + crsr, SWARM_SOC_IDENTIFIER_SIZE + SWARM_SIGNATURE_SIZE);
    116 	crsr += SWARM_SOC_IDENTIFIER_SIZE + SWARM_SIGNATURE_SIZE;
    117 	ck_assert_mem_eq(soc_chunk.data.span, wire_returned + crsr, SWARM_DATA_LENGTH_TYPESIZE);
    118 	crsr += SWARM_DATA_LENGTH_TYPESIZE;
    119 	ck_assert_mem_eq(soc_chunk.data.payload, wire_returned + crsr, soc_chunk.data.payload_sz);
    120 	crsr += soc_chunk.data.payload_sz;
    121 
    122 	ck_assert_int_eq(crsr, wire_length);
    123 }
    124 END_TEST
    125 
    126 Suite * common_suite(void) {
    127 	Suite *s;
    128 	TCase *tc;
    129 
    130 	s = suite_create("soc");
    131 	tc = tcase_create("core");
    132 	tcase_add_test(tc, check_soc_identifier);
    133 	tcase_add_test(tc, check_soc_address);
    134 	tcase_add_test(tc, check_soc_digest);
    135 	tcase_add_test(tc, check_soc_verify);
    136 	tcase_add_test(tc, check_soc_serialize);
    137 	suite_add_tcase(s, tc);
    138 
    139 	return s;
    140 }
    141 
    142 int main(void) {
    143 	int n_fail;
    144 
    145 	Suite *s;
    146 	SRunner *sr;
    147 
    148 	s = common_suite();	
    149 	sr = srunner_create(s);
    150 
    151 	srunner_run_all(sr, CK_VERBOSE);
    152 	n_fail = srunner_ntests_failed(sr);
    153 	srunner_free(sr);
    154 
    155 	return (n_fail == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
    156 }