check_keystore.c (3056B)
1 #include <check.h> 2 #include <stdlib.h> 3 4 #include "keystore.h" 5 #include "hex.h" 6 #include "common.h" 7 #include "swarm.h" 8 9 10 START_TEST(check_keystore_init) { 11 keystore_t keystore; 12 13 keystore_init(&keystore); 14 } 15 END_TEST 16 17 START_TEST(check_keystore_getput) { 18 keystore_t keystore; 19 keystore_key_t key_one; 20 keystore_key_t key_two; 21 keystore_key_t key_out; 22 keystore_key_t *key_returned; 23 struct block_generator bg; 24 25 bg.v = 0; 26 bg.m = 256; 27 block_generate(&bg, key_one.pk, 32); 28 block_generate(&bg, key_two.pk, 32); 29 30 keystore_init(&keystore); 31 32 keystore_put(&keystore, &key_one, NULL, 0); 33 keystore_put(&keystore, &key_two, NULL, 0); 34 35 ck_assert_int_eq(keystore.keys_count, 2); 36 37 key_returned = keystore_get(&keystore, &key_out, 1); 38 39 ck_assert_mem_eq(&key_two, &key_out, sizeof(keystore_key_t)); 40 ck_assert_mem_eq(&key_two, key_returned, sizeof(keystore_key_t)); 41 42 keystore_free(&keystore); 43 44 } 45 END_TEST 46 47 START_TEST(check_keystore_key_generate) { 48 int r; 49 keystore_key_t key; 50 51 unsigned char public_key_check[SWARM_PUBLIC_KEY_SIZE]; 52 unsigned char label_check[SWARM_ADDRESS_SIZE]; 53 54 hex2bin(TEST_PRIVATE_KEY, key.pk); 55 r = key_from_private(&key); 56 ck_assert_int_eq(r, 0); 57 58 hex2bin(TEST_PUBLIC_KEY, public_key_check); 59 ck_assert_mem_eq(key.pubk, public_key_check, SWARM_PUBLIC_KEY_SIZE); 60 61 hex2bin(TEST_ADDRESS, label_check); 62 ck_assert_mem_eq(key.label, label_check, SWARM_ADDRESS_SIZE); 63 } 64 END_TEST 65 66 START_TEST(check_keystore_sign) { 67 int r; 68 keystore_t keystore; 69 keystore_key_t key; 70 keystore_key_t key_recovered; 71 keystore_key_t *key_recovered_returned; 72 struct block_generator bg; 73 unsigned char signature[SWARM_SIGNATURE_SIZE]; 74 unsigned char *signature_returned; 75 unsigned char digest[SWARM_WORD_SIZE]; 76 unsigned char public_key[SWARM_PUBLIC_KEY_SIZE]; 77 unsigned char public_key_check[SWARM_PUBLIC_KEY_SIZE]; 78 79 keystore_init(&keystore); 80 81 hex2bin(TEST_PRIVATE_KEY, key.pk); 82 keystore_put(&keystore, &key, NULL, 0); 83 84 hex2bin(HASH_OF_FOO, digest); 85 86 signature_returned = keystore_sign(&keystore, signature, 0, digest); 87 88 ck_assert_mem_eq(signature, signature_returned, sizeof(keystore_key_t)); 89 90 key_recovered_returned = key_recover(&key_recovered, signature, digest); 91 ck_assert_ptr_nonnull(key_recovered_returned); 92 93 hex2bin(TEST_PUBLIC_KEY, public_key_check); 94 ck_assert_mem_eq(key_recovered.pubk, public_key_check, SWARM_PUBLIC_KEY_SIZE); 95 ck_assert_mem_eq(key_recovered.pubk, key_recovered_returned->pubk, SWARM_PUBLIC_KEY_SIZE); 96 97 keystore_free(&keystore); 98 } 99 END_TEST 100 101 Suite * common_suite(void) { 102 Suite *s; 103 TCase *tc; 104 105 s = suite_create("keystore"); 106 tc = tcase_create("core"); 107 tcase_add_test(tc, check_keystore_init); 108 tcase_add_test(tc, check_keystore_getput); 109 tcase_add_test(tc, check_keystore_key_generate); 110 tcase_add_test(tc, check_keystore_sign); 111 suite_add_tcase(s, tc); 112 113 return s; 114 } 115 116 int main(void) { 117 int n_fail; 118 119 Suite *s; 120 SRunner *sr; 121 122 s = common_suite(); 123 sr = srunner_create(s); 124 125 srunner_run_all(sr, CK_VERBOSE); 126 n_fail = srunner_ntests_failed(sr); 127 srunner_free(sr); 128 129 return (n_fail == 0) ? EXIT_SUCCESS : EXIT_FAILURE; 130 }