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


      1 #include <check.h>
      2 #include <stdlib.h>
      3 
      4 #include "rlp.h"
      5 
      6 
      7 
      8 START_TEST(rlp_encode_single_zero_byte_test) {
      9 	char v = 0x00;
     10 
     11 	rlp_encoder_t encoder;
     12 	rlp_init(&encoder, 1024, NULL);
     13 
     14 	rlp_add(&encoder, 1, &v);
     15 	ck_assert_mem_eq(encoder.buf, &v, 1);
     16 
     17 	rlp_free(&encoder);
     18 }
     19 END_TEST
     20 
     21 
     22 START_TEST(rlp_encode_empty_byte_test) {
     23 	char v = 0x80;
     24 
     25 	rlp_encoder_t encoder;
     26 	rlp_init(&encoder, 1024, NULL);
     27 
     28 	rlp_add(&encoder, 0, NULL);
     29 	ck_assert_mem_eq(encoder.buf, &v, 1);
     30 
     31 	rlp_free(&encoder);
     32 }
     33 END_TEST
     34 
     35 
     36 START_TEST(rlp_encode_single_byte_test) {
     37 	char v = 55;
     38 
     39 	rlp_encoder_t encoder;
     40 	rlp_init(&encoder, 1024, NULL);
     41 
     42 	rlp_add(&encoder, 1, &v);
     43 	ck_assert_mem_eq(encoder.buf, &v, 1);
     44 
     45 	rlp_free(&encoder);
     46 }
     47 END_TEST
     48 
     49 
     50 START_TEST(rlp_encode_single_byte_with_length_test) {
     51 	char l = 0x81;
     52 	char v = 56;
     53 
     54 	rlp_encoder_t encoder;
     55 	rlp_init(&encoder, 1024, NULL);
     56 
     57 	rlp_add(&encoder, 1, &v);
     58 	ck_assert_mem_eq(encoder.buf, &l, 1);
     59 	ck_assert_mem_eq(encoder.buf+1, &v, 1);
     60 
     61 	rlp_free(&encoder);
     62 }
     63 END_TEST
     64 
     65 
     66 START_TEST(rlp_encode_short_bytes_with_length_test) {
     67 	char l = 0xb7;
     68 	char v[55];
     69 	v[54] = 42;
     70 
     71 	rlp_encoder_t encoder;
     72 	rlp_init(&encoder, 1024, NULL);
     73 
     74 	rlp_add(&encoder, 55, (char*)&v);
     75 	ck_assert_mem_eq(encoder.buf, &l, 1);
     76 	ck_assert_mem_eq(encoder.buf+55, (char*)&(v[54]), 1);
     77 
     78 	rlp_free(&encoder);
     79 }
     80 END_TEST
     81 
     82 
     83 START_TEST(rlp_encode_long_bytes_with_length_test) {
     84 	char l = 0xb8;
     85 	char ll = 56;
     86 	char v[56];
     87 	v[55] = 42;
     88 
     89 	rlp_encoder_t encoder;
     90 	rlp_init(&encoder, 1024, NULL);
     91 
     92 	rlp_add(&encoder, 56, (char*)&v);
     93 	ck_assert_mem_eq(encoder.buf, &l, 1);
     94 	ck_assert_mem_eq(encoder.buf+1, &ll, 1);
     95 	ck_assert_mem_eq(encoder.buf+57, (char*)&(v[55]), 1);
     96 
     97 	rlp_free(&encoder);
     98 }
     99 END_TEST
    100 
    101 
    102 START_TEST(rlp_encode_list_descend_state_test) {
    103 	int r;
    104 	char *ptr_chk;
    105 
    106 	rlp_encoder_t encoder;
    107 	rlp_init(&encoder, 1024, NULL);
    108 
    109 	r = rlp_descend(&encoder);
    110 	ck_assert_ptr_eq(encoder.ptr, encoder.list_ptr[encoder.depth]);
    111 	ptr_chk = encoder.ptr;
    112 	ck_assert_int_eq(1, r);
    113 	ck_assert_int_eq(encoder.depth, 1);
    114 
    115 	r = rlp_descend(&encoder);
    116 	ck_assert_ptr_eq(encoder.ptr, encoder.list_ptr[encoder.depth]);
    117 	ck_assert_ptr_eq(ptr_chk, encoder.list_ptr[encoder.depth-1]);
    118 	ck_assert_int_eq(2, r);
    119 	ck_assert_int_eq(encoder.depth, 2);
    120 
    121 	rlp_free(&encoder);
    122 }
    123 END_TEST
    124 
    125 
    126 START_TEST(rlp_encode_list_roundtrip_state_test) {
    127 	char one = 42;
    128 	char two = 13;
    129 	int r;
    130 
    131 	rlp_encoder_t encoder;
    132 	rlp_init(&encoder, 1024, NULL);
    133 
    134 	r = rlp_descend(&encoder);
    135 	ck_assert_int_eq(r, 1);
    136 	rlp_add(&encoder, 1, &one);
    137 	rlp_add(&encoder, 1, &two);
    138 	ck_assert_int_eq(encoder.depth, 1);
    139 
    140 	r = rlp_ascend(&encoder);
    141 	ck_assert_int_eq(r, 0);
    142 	ck_assert_int_eq(encoder.depth, 0);
    143 
    144 	rlp_free(&encoder);
    145 }
    146 END_TEST
    147 
    148 
    149 
    150 START_TEST(rlp_encode_list_single_depth_test) {
    151 	char one = 42;
    152 	char two = 13;
    153 	char target[3] = {0xc2, 42, 13};
    154 	int r;
    155 
    156 	rlp_encoder_t encoder;
    157 	rlp_init(&encoder, 1024, NULL);
    158 
    159 	r = rlp_descend(&encoder);
    160 	rlp_add(&encoder, 1, &one);
    161 	rlp_add(&encoder, 1, &two);
    162 	r = rlp_ascend(&encoder);
    163 	ck_assert_mem_eq(encoder.buf, target, 3);
    164 	
    165 	rlp_free(&encoder);
    166 }
    167 END_TEST
    168 
    169 
    170 START_TEST(rlp_encode_list_adjacent_test) {
    171 	char one[2] = {42, 13};
    172 	char two[3] = {0x66, 0x6f, 0x6f};
    173 	char target[8] = {0xc2, 42, 13, 0xc4, 0x83, 0x66, 0x6f, 0x6f};
    174 	int r;
    175 
    176 	rlp_encoder_t encoder;
    177 	rlp_init(&encoder, 1024, NULL);
    178 
    179 	r = rlp_descend(&encoder);
    180 	rlp_add(&encoder, 1, &one[0]);
    181 	rlp_add(&encoder, 1, &one[1]);
    182 	r = rlp_ascend(&encoder);
    183 	r = rlp_descend(&encoder);
    184 	rlp_add(&encoder, 3, two);
    185 	r = rlp_ascend(&encoder);
    186 	ck_assert_mem_eq(encoder.buf, target, 8);
    187 
    188 	rlp_free(&encoder);
    189 }
    190 END_TEST
    191 
    192 
    193 START_TEST(rlp_encode_list_nested_test) {
    194 	char one[2] = {42, 13};
    195 	char two[3] = {0x66, 0x6f, 0x6f};
    196 	char target[8] = {0xc7, 42, 13, 0xc4, 0x83, 0x66, 0x6f, 0x6f};
    197 	int r;
    198 
    199 	rlp_encoder_t encoder;
    200 	rlp_init(&encoder, 1024, NULL);
    201 
    202 	r = rlp_descend(&encoder);
    203 	rlp_add(&encoder, 1, &one[0]);
    204 	rlp_add(&encoder, 1, &one[1]);
    205 	r = rlp_descend(&encoder);
    206 	rlp_add(&encoder, 3, two);
    207 	r = rlp_ascend(&encoder);
    208 	r = rlp_ascend(&encoder);
    209 	ck_assert_mem_eq(encoder.buf, target, 8);
    210 
    211 	rlp_free(&encoder);
    212 }
    213 END_TEST
    214 
    215 START_TEST(rlp_encode_list_long_test) {
    216 	char v[56];
    217 	char target[4] = {0xf8, 58, 0xb8, 56};
    218 	int r;
    219 
    220 	rlp_encoder_t encoder;
    221 	rlp_init(&encoder, 1024, NULL);
    222 
    223 	r = rlp_descend(&encoder);
    224 	rlp_add(&encoder, 56, (char*)v);
    225 	r = rlp_ascend(&encoder);
    226 	ck_assert_mem_eq(encoder.buf, target, 4);
    227 
    228 	rlp_free(&encoder);
    229 }
    230 END_TEST
    231 
    232 
    233 START_TEST(rlp_encode_list_long_chunked_test) {
    234 	char x[10];
    235 	char y[20];
    236 	char z[30];
    237 	int i;
    238 	int j;
    239 	int r;
    240 	char *zs;
    241 
    242 	char target[] = {0xf8, 63};
    243 
    244 	rlp_encoder_t encoder;
    245 	rlp_init(&encoder, 1024, NULL);
    246 
    247 	r = rlp_descend(&encoder);
    248 
    249 	rlp_add(&encoder, 10, x);
    250 	rlp_add(&encoder, 20, y);
    251 	rlp_add(&encoder, 30, z);
    252 
    253 	r = rlp_ascend(&encoder);
    254 
    255 	ck_assert_mem_eq(encoder.buf, target, 2);
    256 
    257 	rlp_free(&encoder);
    258 
    259 }
    260 END_TEST
    261 
    262 
    263 Suite *rlp_encode_suite(void) {
    264 	Suite *s;
    265 	TCase *tci;
    266 	TCase *tcb;
    267 	TCase *tcl;
    268 
    269 	s = suite_create("Rlp");
    270 	tcb = tcase_create("Bytes");
    271 	tcl = tcase_create("List");
    272 
    273 	tcase_add_test(tcb, rlp_encode_empty_byte_test);
    274 	tcase_add_test(tcb, rlp_encode_single_zero_byte_test);
    275 	tcase_add_test(tcb, rlp_encode_single_byte_test);
    276 	tcase_add_test(tcb, rlp_encode_single_byte_with_length_test);
    277 	tcase_add_test(tcb, rlp_encode_short_bytes_with_length_test);
    278 	tcase_add_test(tcb, rlp_encode_long_bytes_with_length_test);
    279 	suite_add_tcase(s, tcb);
    280 
    281 	tcase_add_test(tcl, rlp_encode_list_descend_state_test);
    282 	tcase_add_test(tcl, rlp_encode_list_roundtrip_state_test);
    283 	tcase_add_test(tcl, rlp_encode_list_single_depth_test);
    284 	tcase_add_test(tcl, rlp_encode_list_adjacent_test);
    285 	tcase_add_test(tcl, rlp_encode_list_nested_test);
    286 	tcase_add_test(tcl, rlp_encode_list_long_test);
    287 	tcase_add_test(tcl, rlp_encode_list_long_chunked_test);
    288 	suite_add_tcase(s, tcl);
    289 
    290 	return s;
    291 }
    292 
    293 
    294 int main(void) {
    295 	int num_fail;
    296 
    297 	Suite *s;
    298 	SRunner *sr;
    299 
    300 	s = rlp_encode_suite();
    301 	sr = srunner_create(s);
    302 
    303 	srunner_run_all(sr, CK_VERBOSE);
    304 	num_fail = srunner_ntests_failed(sr);
    305 	srunner_free(sr);
    306 
    307 	return (num_fail == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
    308 }