libqaeda

Unnamed repository; edit this file 'description' to name the repository.
Info | Log | Files | Refs | README | LICENSE

test_trust.c (5326B)


      1 #include <check.h>
      2 #include <stdlib.h>
      3 
      4 #include "lq/trust.h"
      5 #include "lq/store.h"
      6 #include "lq/err.h"
      7 #include "lq/mem.h"
      8 #include "lq/crypto.h"
      9 #include "lq/io.h"
     10 
     11 static const char pubkey_data_alice[65] = { 0x40,
     12 	0xde, 0x58, 0x08, 0xe7, 0x24, 0x5e, 0x04, 0x72,
     13 	0x7d, 0xb3, 0x83, 0xe4, 0x28, 0x76, 0xfc, 0x02, 
     14 	0x91, 0xb7, 0xac, 0x31, 0xda, 0x65, 0x9a, 0xc9,
     15 	0x80, 0x72, 0xb7, 0x14, 0x87, 0x36, 0x90, 0x29,
     16 	0x0c, 0x0e, 0xca, 0x23, 0xa7, 0xb2, 0xc1, 0x38,
     17 	0x75, 0x97, 0x41, 0xea, 0x6c, 0xb4, 0xfc, 0x71,
     18 	0x91, 0x7a, 0xa6, 0x9f, 0x04, 0xb3, 0x95, 0x10,
     19 	0x8b, 0x42, 0xd6, 0x26, 0x10, 0x64, 0x8c, 0xdb,
     20 };
     21 
     22 static const unsigned char trust_alice[2] = {
     23 	0x01, 0x78,
     24 };
     25 
     26 static const char pubkey_data_bob[65] = {
     27 	0x40, 
     28 	0x79, 0x28, 0x14, 0x6b, 0xb3, 0x19, 0x19, 0xfc,
     29 	0xab, 0xb3, 0x23, 0xa3, 0x8b, 0x36, 0xfe, 0x36,
     30 	0x33, 0xd7, 0x29, 0x62, 0x6a, 0x2f, 0x1d, 0x11,
     31 	0x01, 0x77, 0x93, 0x2e, 0x00, 0xc7, 0x80, 0x8d,
     32 	0xaf, 0x17, 0xa1, 0xe2, 0x62, 0xe8, 0xe3, 0xb3,
     33 	0xe0, 0x34, 0x33, 0x88, 0xc8, 0x13, 0xe5, 0x52,
     34 	0x07, 0x27, 0xfe, 0x4b, 0xa7, 0x9c, 0xa9, 0x45,
     35 	0x6c, 0x4d, 0x14, 0x2a, 0x70, 0xec, 0x07, 0x80,
     36 };
     37 
     38 static const unsigned char trust_bob[2] = {
     39 	0x00, 0x40,
     40 };
     41 
     42 extern LQStore LQMemContent;
     43 
     44 START_TEST(check_trust_none) {
     45 	int r;
     46 	unsigned char flag_test[2];
     47 	LQPubKey *pubkey_alice;
     48 	LQPubKey *pubkey_bob;
     49 	LQStore *store;
     50 	char *lodata;
     51 	size_t lolen;
     52 	char path[1024];
     53 	char *p;
     54 
     55 	lq_cpy(path, "/tmp/lqstore_file_XXXXXX", 25);
     56 	p = mktempdir(path);
     57 	*(p+24) = '/';
     58 	*(p+25) = 0x0;
     59 	store = lq_store_new(p);
     60 	ck_assert_ptr_nonnull(store->userdata);
     61 
     62 	pubkey_alice = lq_publickey_new(pubkey_data_alice);
     63 	pubkey_bob = lq_publickey_new(pubkey_data_bob);
     64 
     65 	lolen = lq_publickey_bytes(pubkey_alice, &lodata);
     66 	store->put(LQ_CONTENT_KEY, store, lodata, &lolen, (char*)trust_alice, 2);
     67 
     68 	lq_set(flag_test, 0, 2); 
     69 	r = lq_trust_check(pubkey_alice, store, TRUST_MATCH_NONE, flag_test);
     70 	ck_assert_int_eq(r, 1000000);
     71 
     72 	r = lq_trust_check(pubkey_bob, store, TRUST_MATCH_NONE, flag_test);
     73 	ck_assert_int_eq(r, -1);
     74 
     75 	lolen = lq_publickey_bytes(pubkey_bob, &lodata);
     76 	store->put(LQ_CONTENT_KEY, store, lodata, &lolen, (char*)trust_bob, 2);
     77 	r = lq_trust_check(pubkey_bob, store, TRUST_MATCH_NONE, flag_test);
     78 	ck_assert_int_eq(r, 1000000);
     79 
     80 	store->free(store);
     81 }
     82 END_TEST
     83 
     84 START_TEST(check_trust_one) {
     85 	int r;
     86 	unsigned char flag_test[2];
     87 	LQPubKey *pubkey_alice;
     88 	LQStore *store;
     89 	char *lodata;
     90 	size_t lolen;
     91 	char path[1024];
     92 	char *p;
     93 
     94 	lq_cpy(path, "/tmp/lqstore_file_XXXXXX", 25);
     95 	p = mktempdir(path);
     96 	*(p+24) = '/';
     97 	*(p+25) = 0x0;
     98 	store = lq_store_new(p);
     99 	ck_assert_ptr_nonnull(store->userdata);
    100 
    101 	pubkey_alice = lq_publickey_new(pubkey_data_alice);
    102 
    103 	lolen = lq_publickey_bytes(pubkey_alice, &lodata);
    104 	store->put(LQ_CONTENT_KEY, store, lodata, &lolen, (char*)trust_alice, 2);
    105 
    106 	flag_test[0] = 0;
    107 	flag_test[1] = 0x40;
    108 	r = lq_trust_check(pubkey_alice, store, TRUST_MATCH_ONE, (const unsigned char*)flag_test);
    109 	ck_assert_int_eq(r, 1000000);
    110 
    111 	store->free(store);
    112 }
    113 END_TEST
    114 
    115 START_TEST(check_trust_best) {
    116 	int r;
    117 	unsigned char flag_test[2];
    118 	LQPubKey *pubkey_alice;
    119 	LQStore *store;
    120 	char *lodata;
    121 	size_t lolen;
    122 	char path[1024];
    123 	char *p;
    124 
    125 	lq_cpy(path, "/tmp/lqstore_file_XXXXXX", 25);
    126 	p = mktempdir(path);
    127 	*(p+24) = '/';
    128 	*(p+25) = 0x0;
    129 	store = lq_store_new(p);
    130 	ck_assert_ptr_nonnull(store->userdata);
    131 
    132 	pubkey_alice = lq_publickey_new(pubkey_data_alice);
    133 
    134 	lolen = lq_publickey_bytes(pubkey_alice, &lodata);
    135 	store->put(LQ_CONTENT_KEY, store, lodata, &lolen, (char*)trust_alice, 2);
    136 
    137 	flag_test[0] = 0x13;
    138 	flag_test[1] = 0x60;
    139 	r = lq_trust_check(pubkey_alice, store, TRUST_MATCH_BEST, (const unsigned char*)flag_test);
    140 	ck_assert_int_eq(r, 600000);
    141 
    142 	store->free(store);
    143 }
    144 END_TEST
    145 
    146 START_TEST(check_trust_all) {
    147 	int r;
    148 	unsigned char flag_test[2];
    149 	LQPubKey *pubkey_alice;
    150 	LQStore *store;
    151 	char *lodata;
    152 	size_t lolen;
    153 	char path[1024];
    154 	char *p;
    155 
    156 	lq_cpy(path, "/tmp/lqstore_file_XXXXXX", 25);
    157 	p = mktempdir(path);
    158 	*(p+24) = '/';
    159 	*(p+25) = 0x0;
    160 	store = lq_store_new(p);
    161 	ck_assert_ptr_nonnull(store->userdata);
    162 
    163 	pubkey_alice = lq_publickey_new(pubkey_data_alice);
    164 
    165 	lolen = lq_publickey_bytes(pubkey_alice, &lodata);
    166 	store->put(LQ_CONTENT_KEY, store, lodata, &lolen, (char*)trust_alice, 2);
    167 
    168 	flag_test[0] = 0x13;
    169 	flag_test[1] = 0x60;
    170 	r = lq_trust_check(pubkey_alice, store, TRUST_MATCH_ALL, (const unsigned char*)flag_test);
    171 	ck_assert_int_eq(r, 0);
    172 
    173 	flag_test[0] = 0xff;
    174 	flag_test[1] = 0xff;
    175 	r = lq_trust_check(pubkey_alice, store, TRUST_MATCH_ALL, (const unsigned char*)flag_test);
    176 	ck_assert_int_eq(r, 0);
    177 
    178 	flag_test[0] = 0x01;
    179 	flag_test[1] = 0x78;
    180 	r = lq_trust_check(pubkey_alice, store, TRUST_MATCH_ALL, (const unsigned char*)flag_test);
    181 	ck_assert_int_eq(r, 1000000);
    182 
    183 	store->free(store);
    184 }
    185 END_TEST
    186 
    187 Suite * common_suite(void) {
    188 	Suite *s;
    189 	TCase *tc;
    190 
    191 	s = suite_create("trust");
    192 	tc = tcase_create("check");
    193 	tcase_add_test(tc, check_trust_none);
    194 	tcase_add_test(tc, check_trust_one);
    195 	tcase_add_test(tc, check_trust_best);
    196 	tcase_add_test(tc, check_trust_all);
    197 	suite_add_tcase(s, tc);
    198 
    199 	return s;
    200 }
    201 
    202 int main(void) {
    203 	int n_fail;
    204 
    205 	Suite *s;
    206 	SRunner *sr;
    207 
    208 	s = common_suite();	
    209 	sr = srunner_create(s);
    210 
    211 	srunner_run_all(sr, CK_VERBOSE);
    212 	n_fail = srunner_ntests_failed(sr);
    213 	srunner_free(sr);
    214 
    215 	return (n_fail == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
    216 }