librlp

C library for the Recursive Length Prefix (RLP) serialization format
git clone git://git.defalsify.org/librlp.git
Info | Log | Files | Refs | README | LICENSE

check_decoder.c (4570B)


      1 #include <check.h>
      2 #include <stdlib.h>
      3 #include <string.h>
      4 
      5 #include "rlp.h"
      6 
      7 
      8 START_TEST(rlp_decode_single_test) {
      9 	char s = 42;
     10 	char buf;
     11 	char *zbuf = (char*)&buf;
     12 	int r;
     13 	int l;
     14 
     15 	char state = RLP_STRING;
     16 
     17 	rlp_encoder_t encoder;
     18 	
     19 	rlp_init(&encoder, 1, &s);
     20 	r = rlp_next(&encoder, &l, &zbuf);
     21 	ck_assert_mem_eq(&encoder.state, &state, 1);
     22 	ck_assert_int_eq(r, 0);
     23 	ck_assert_int_eq(l, 1);
     24 	ck_assert_mem_eq(zbuf, &s, l);
     25 
     26 	rlp_free(&encoder);
     27 		
     28 }
     29 END_TEST
     30 
     31 START_TEST(rlp_decode_short_string_test) {
     32 	int r;
     33 	char s[5] = {0x83, 0x66, 0x6f, 0x6f};
     34 	char target[] = "foo";
     35 
     36 	int l;
     37 	char buf[1024];
     38 	char *zbuf = (char*)buf;
     39 
     40 	char state = RLP_STRING;
     41 
     42 	rlp_encoder_t encoder;
     43 	
     44 	rlp_init(&encoder, 5, s);
     45 	r = rlp_next(&encoder, &l, &zbuf);
     46 	ck_assert_mem_eq(&encoder.state, &state, 1);
     47 	ck_assert_int_eq(r, 0);
     48 	ck_assert_int_eq(l, 3);
     49 	ck_assert_mem_eq(zbuf, target, l);
     50 
     51 	rlp_free(&encoder);
     52 
     53 }
     54 END_TEST
     55 
     56 
     57 START_TEST(rlp_decode_string_trailing_zeros_test) {
     58 	int r;
     59 	char s[5] = {0x84, 0x66, 0x6f, 0x6f, 0x00};
     60 	char target[] = "foo\0";
     61 
     62 	int l;
     63 	char buf[1024];
     64 	char *zbuf = (char*)buf;
     65 
     66 	char state = RLP_STRING;
     67 
     68 	rlp_encoder_t encoder;
     69 	
     70 	rlp_init(&encoder, 5, s);
     71 	r = rlp_next(&encoder, &l, &zbuf);
     72 	ck_assert_mem_eq(&encoder.state, &state, 1);
     73 	ck_assert_int_eq(r, 0);
     74 	ck_assert_int_eq(l, 4);
     75 	ck_assert_mem_eq(zbuf, target, l);
     76 
     77 	rlp_free(&encoder);
     78 
     79 }
     80 END_TEST
     81 
     82 START_TEST(rlp_decode_long_string_test) {
     83 	int l;
     84 	int r;
     85 
     86 	char s[58];
     87 	s[0] = 0xb8;
     88 	s[1] = 56;
     89 
     90 	char buf[1024];
     91 	char *zbuf = (char*)buf;
     92 
     93 	char state = RLP_STRING;
     94 
     95 	rlp_encoder_t encoder;
     96 	
     97 	rlp_init(&encoder, 57, s);
     98 
     99 	r = rlp_next(&encoder, &l, &zbuf);
    100 	ck_assert_mem_eq(&encoder.state, &state, 1);
    101 	ck_assert_int_eq(l, 56);
    102 	ck_assert_int_eq(r, 0);
    103 	ck_assert_mem_eq(zbuf, s+2, l);
    104 
    105 	rlp_free(&encoder);
    106 
    107 }
    108 END_TEST
    109 
    110 
    111 START_TEST(rlp_decode_zero_list_test) {
    112 	int l;
    113 	int r;
    114 
    115 	char s = 0xc0;
    116 	char buf = 0;
    117 	char *zbuf = (char*)&buf;
    118 
    119 	char state = RLP_LIST_DESCEND;
    120 
    121 	rlp_encoder_t encoder;
    122 	
    123 	rlp_init(&encoder, 1, &s);
    124 	r = rlp_next(&encoder, &l, &zbuf);
    125 	ck_assert_mem_eq(&encoder.state, &state, 1);
    126 	ck_assert_int_eq(l, 0);
    127 	ck_assert_int_eq(r, 0);
    128 
    129 	rlp_free(&encoder);
    130 }
    131 END_TEST
    132 
    133 
    134 START_TEST(rlp_decode_short_list_test) {
    135 	int l;
    136 	int r;
    137 
    138 	char s[2] = {0xc1, 0x80};
    139 	char buf[2];
    140 	char *zbuf = (char*)&buf;
    141 
    142 	char state = RLP_LIST_DESCEND;
    143 
    144 	rlp_encoder_t encoder;
    145 	
    146 	rlp_init(&encoder, 2, s);
    147 	r = rlp_next(&encoder, &l, &zbuf);
    148 	ck_assert_mem_eq(&encoder.state, &state, 1);
    149 	ck_assert_int_eq(l, 1);
    150 	ck_assert_int_eq(r, 0);
    151 
    152 	rlp_free(&encoder);
    153 }
    154 END_TEST
    155 
    156 
    157 START_TEST(rlp_decode_long_list_test) {
    158 	int l;
    159 	int r;
    160 
    161 	char s[58];
    162 	s[0] = 0xf8;
    163 	s[1] = 56;
    164 	s[2] = 0x80 + 55;
    165 
    166 	char buf[56];
    167 	char *zbuf = (char*)&buf;
    168 
    169 	char state = RLP_LIST_DESCEND;
    170 
    171 	rlp_encoder_t encoder;
    172 	
    173 	rlp_init(&encoder, 58, s);
    174 	r = rlp_next(&encoder, &l, &zbuf);
    175 	ck_assert_mem_eq(&encoder.state, &state, 1);
    176 	ck_assert_int_eq(l, 56);
    177 	ck_assert_int_eq(r, 0);
    178 
    179 	rlp_free(&encoder);
    180 }
    181 END_TEST
    182 
    183 
    184 START_TEST(rlp_decode_adjacent_strings_test) {
    185 	int l;
    186 	int r;
    187 
    188 	char s[] = {0x83, 0x66, 0x6f, 0x6f, 0x84, 0x62, 0x61, 0x72, 0x00};
    189 
    190 	char state = RLP_STRING;
    191 
    192 	rlp_encoder_t encoder;
    193 	
    194 	char buf[4];
    195 	char *zbuf = (char*)&buf;
    196 
    197 	rlp_init(&encoder, 9, s);
    198 	r = rlp_next(&encoder, &l, &zbuf);
    199 	ck_assert_mem_eq(&encoder.state, &state, 1);
    200 	ck_assert_int_eq(r, 0);
    201 	ck_assert_int_eq(l, 3);
    202 	ck_assert_mem_eq(zbuf, "foo", 3);
    203 
    204 	r = rlp_next(&encoder, &l, &zbuf);
    205 	ck_assert_mem_eq(&encoder.state, &state, 1); // fail because every string process decrements depth, should only at end of list
    206 	ck_assert_int_eq(r, 0);
    207 	ck_assert_int_eq(l, 4);
    208 	ck_assert_mem_eq(zbuf, "bar\0", 4);
    209 }
    210 END_TEST
    211 
    212 Suite *rlp_decode_suite(void) {
    213 	Suite *s;
    214 	TCase *tcb;
    215 	TCase *tcl;
    216 
    217 	s = suite_create("rlp_decode");
    218 	tcb = tcase_create("bytes"); 
    219 	tcl = tcase_create("list"); 
    220 
    221 	tcase_add_test(tcb, rlp_decode_single_test);
    222 	tcase_add_test(tcb, rlp_decode_short_string_test);
    223 	tcase_add_test(tcb, rlp_decode_string_trailing_zeros_test);
    224 	tcase_add_test(tcb, rlp_decode_long_string_test);
    225 	suite_add_tcase(s, tcb);
    226 
    227 	tcase_add_test(tcl, rlp_decode_zero_list_test);
    228 	tcase_add_test(tcl, rlp_decode_short_list_test);
    229 	tcase_add_test(tcl, rlp_decode_long_list_test);
    230 	tcase_add_test(tcl, rlp_decode_adjacent_strings_test);
    231 	suite_add_tcase(s, tcl);
    232 
    233 	return s;
    234 }
    235 
    236 
    237 int main(void) {
    238 	int num_fail;
    239 
    240 	Suite *s;
    241 	SRunner *sr;
    242 
    243 	s = rlp_decode_suite();
    244 	sr = srunner_create(s);
    245 
    246 	srunner_run_all(sr, CK_VERBOSE);
    247 	num_fail = srunner_ntests_failed(sr);
    248 	srunner_free(sr);
    249 
    250 	return (num_fail == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
    251 }