check_vectors.c (8630B)
1 #include <check.h> 2 #include <stdlib.h> 3 #include <string.h> 4 #include <stdio.h> 5 6 #include "rlp.h" 7 8 START_TEST(rlp_dog_test) { 9 char buf[1024]; 10 char *zbuf = (char*)&buf; 11 int l; 12 13 char state = RLP_STRING; 14 15 rlp_encoder_t encoder; 16 17 rlp_init(&encoder, 1024, NULL); 18 char *x_dog = "dog"; 19 char r_dog[4] = {0x83, 'd', 'o', 'g'}; 20 rlp_add(&encoder, 3, x_dog); 21 ck_assert_mem_eq(encoder.buf, r_dog, 4); 22 ck_assert_int_eq(encoder.size, 4); 23 24 // reuse for decode 25 rlp_init(&encoder, 4, encoder.buf); 26 rlp_next(&encoder, &l, &zbuf); 27 ck_assert_mem_eq(&encoder.state, &state, 1); 28 ck_assert_int_eq(l, 3); 29 ck_assert_mem_eq(zbuf, x_dog, l); 30 31 state = RLP_END; 32 rlp_next(&encoder, &l, &zbuf); 33 ck_assert_mem_eq(&encoder.state, &state, 1); 34 35 rlp_free(&encoder); 36 } 37 END_TEST 38 39 START_TEST(rlp_catdog_test) { 40 rlp_encoder_t encoder; 41 42 char buf[1024]; 43 char *zbuf = (char*)&buf; 44 int l; 45 char state; 46 47 char *x_dog = "dog"; 48 char *x_cat = "cat"; 49 char r_catdog[9] = {0xc8, 0x83, 'c', 'a', 't', 0x83, 'd', 'o', 'g'}; 50 51 rlp_init(&encoder, 1024, NULL); 52 rlp_descend(&encoder); 53 rlp_add(&encoder, 3, x_cat); 54 rlp_add(&encoder, 3, x_dog); 55 rlp_ascend(&encoder); 56 ck_assert_mem_eq(encoder.buf, r_catdog, 9); 57 ck_assert_int_eq(encoder.size, 9); 58 59 // reuse for decode 60 state = RLP_LIST_DESCEND; 61 rlp_init(&encoder, 9, encoder.buf); 62 rlp_next(&encoder, &l, &zbuf); 63 ck_assert_mem_eq(&encoder.state, &state, 1); 64 ck_assert_int_eq(l, 8); 65 66 state = RLP_STRING; 67 rlp_next(&encoder, &l, &zbuf); 68 ck_assert_mem_eq(&encoder.state, &state, 1); 69 ck_assert_mem_eq(zbuf, x_cat, l); 70 71 rlp_next(&encoder, &l, &zbuf); 72 ck_assert_mem_eq(&encoder.state, &state, 1); 73 ck_assert_mem_eq(zbuf, x_dog, l); 74 75 state = RLP_END; 76 rlp_next(&encoder, &l, &zbuf); 77 ck_assert_mem_eq(&encoder.state, &state, 1); 78 79 rlp_free(&encoder); 80 } 81 END_TEST 82 83 START_TEST(rlp_lorem_test) { 84 rlp_encoder_t encoder; 85 86 char buf[1024]; 87 char *zbuf = (char*)&buf; 88 int l; 89 char state; 90 91 char *lorem = "Lorem ipsum dolor sit amet, consectetur adipisicing elit"; 92 char target[2] = {0xb8, strlen(lorem)}; 93 94 rlp_init(&encoder, 1024, NULL); 95 rlp_add(&encoder, strlen(lorem), lorem); 96 ck_assert_mem_eq(encoder.buf, target, 2); 97 ck_assert_mem_eq(encoder.buf+2, lorem, strlen(lorem)); 98 ck_assert_int_eq(encoder.size, 2 + strlen(lorem)); 99 100 // reuse for decode 101 state = RLP_STRING; 102 rlp_init(&encoder, strlen(lorem) + 2, encoder.buf); 103 rlp_next(&encoder, &l, &zbuf); 104 ck_assert_mem_eq(&encoder.state, &state, 1); 105 ck_assert_int_eq(l, strlen(lorem)); 106 ck_assert_mem_eq(zbuf, lorem, l); 107 108 state = RLP_END; 109 rlp_next(&encoder, &l, &zbuf); 110 ck_assert_mem_eq(&encoder.state, &state, 1); 111 112 rlp_free(&encoder); 113 } 114 END_TEST 115 116 START_TEST(rlp_set_theoretical_representation_of_three) { 117 118 unsigned char target[8] = {0xc7, 0xc0, 0xc1, 0xc0, 0xc3, 0xc0, 0xc1, 0xc0}; 119 int target_lengths[15] = { 120 7, 121 0, 122 0, 123 1, 124 0, 125 0, 126 0, 127 3, 128 0, 129 0, 130 1, 131 0, 132 0, 133 0, 134 0, 135 }; 136 137 enum rlp_state target_states[15] = { 138 RLP_LIST_DESCEND, 139 RLP_LIST_DESCEND, 140 RLP_LIST_ASCEND, 141 RLP_LIST_DESCEND, 142 RLP_LIST_DESCEND, 143 RLP_LIST_ASCEND, 144 RLP_LIST_ASCEND, 145 RLP_LIST_DESCEND, 146 RLP_LIST_DESCEND, 147 RLP_LIST_ASCEND, 148 RLP_LIST_DESCEND, 149 RLP_LIST_DESCEND, 150 RLP_LIST_ASCEND, 151 RLP_LIST_ASCEND, 152 RLP_LIST_ASCEND, 153 }; 154 155 char *zbuf = NULL; 156 int i; 157 int l; 158 char state = RLP_LIST_ASCEND; 159 160 rlp_encoder_t encoder; 161 162 rlp_init(&encoder, 1024, NULL); 163 164 // [ [] [ [] ] [ [] [ []] ] ] 165 rlp_descend(&encoder); 166 167 rlp_descend(&encoder); 168 rlp_ascend(&encoder); 169 170 rlp_descend(&encoder); 171 rlp_descend(&encoder); 172 rlp_ascend(&encoder); 173 rlp_ascend(&encoder); 174 175 rlp_descend(&encoder); 176 rlp_descend(&encoder); 177 rlp_ascend(&encoder); 178 rlp_descend(&encoder); 179 rlp_descend(&encoder); 180 rlp_ascend(&encoder); 181 rlp_ascend(&encoder); 182 rlp_ascend(&encoder); 183 184 rlp_ascend(&encoder); 185 186 ck_assert_mem_eq(encoder.buf, target, 8); 187 ck_assert_int_eq(encoder.size, 8); 188 189 rlp_init(&encoder, 1024, encoder.buf); 190 for (i = 0; i < 15; i++) { 191 rlp_next(&encoder, &l, &zbuf); 192 ck_assert_int_eq(target_states[i], encoder.state); 193 ck_assert_int_eq(target_lengths[i], l); 194 } 195 196 rlp_free(&encoder); 197 } 198 END_TEST 199 200 START_TEST(rlp_ethereum_tx_unsigned) { 201 unsigned char target[] = {0xea, 0x80, 0x85, 0x04, 0xa8, 0x17, 0xc8, 0x00, 0x82, 0x52, 0x08, 0x94, 0x35, 0x35, 0x35, 0x35, 0x35, 0x35, 0x35, 0x35, 0x35, 0x35, 0x35, 0x35, 0x35, 0x35, 0x35, 0x35, 0x35, 0x35, 0x35, 0x35, 0x82, 0x03, 0xe8, 0x84, 0xde, 0xad, 0xbe, 0xef, 0x01, 0x80, 0x80}; 202 unsigned char nonce[] = {0x01}; 203 unsigned char gas_price[] = {0x04, 0xa8, 0x17, 0xc8, 0x00}; 204 unsigned char gas_limit[] = {0x52, 0x08}; 205 unsigned char to[] = {0x35, 0x35, 0x35, 0x35, 0x35, 0x35, 0x35, 0x35, 0x35, 0x35, 0x35, 0x35, 0x35, 0x35, 0x35, 0x35, 0x35, 0x35, 0x35, 0x35}; 206 unsigned char value[] = {0x03, 0xe8}; 207 unsigned char data[] = {0xde, 0xad, 0xbe, 0xef}; 208 unsigned char v[] = {0x01}; 209 210 rlp_encoder_t encoder; 211 212 rlp_init(&encoder, 1024, NULL); 213 rlp_descend(&encoder); 214 rlp_add(&encoder, 0, NULL); 215 rlp_add(&encoder, 5, gas_price); 216 rlp_add(&encoder, 2, gas_limit); 217 rlp_add(&encoder, 20, to); 218 rlp_add(&encoder, 2, value); 219 rlp_add(&encoder, 3, data); 220 rlp_add(&encoder, 1, v); 221 rlp_add(&encoder, 0, NULL); 222 rlp_add(&encoder, 0, NULL); 223 rlp_ascend(&encoder); 224 225 ck_assert_int_eq(sizeof(target), encoder.size); 226 int i; 227 for (i = 0; i < sizeof(target); i++) { 228 fprintf(stderr, "%d %x %x\n", i, (unsigned char)encoder.buf[i], target[i]); 229 if (target[i] != (unsigned char)encoder.buf[i]) { 230 rlp_free(&encoder); 231 ck_abort_msg("aiee"); 232 } 233 } 234 235 ck_assert_mem_eq(encoder.buf, target, sizeof(target)); 236 rlp_free(&encoder); 237 238 } 239 END_TEST 240 241 START_TEST(rlp_ethereum_tx_signed) { 242 243 unsigned char target[] = {0xf8, 0x68, 0x01, 0x84, 0x3b, 0x9a, 0xca, 0x00, 0x82, 0x52, 0x08, 0x94, 0x16, 0xcf,0xb6, 0x34, 0x67,0x45, 0x2e, 0xcf, 0x15, 0xe9, 0xd6, 0xac, 0x48, 0xe9, 0xb4, 0xda, 0x62, 0x87, 0x16, 0xc4, 0x82, 0x04, 0x00, 0x83, 0x66, 0x6f, 0x6f, 0x26, 0xa0, 0xd8, 0x25, 0xb2, 0xe7, 0x97, 0x55, 0x0c, 0x14, 0x4f, 0x5e, 0x8a, 0xca, 0xe4, 0x52, 0xbd, 0x81, 0xe1, 0x94, 0x7f, 0xdc, 0xbc, 0x34, 0x30, 0x24, 0x0e, 0x44, 0xe9, 0x67, 0xb8, 0x21, 0x81, 0xf4, 0xa0, 0x30, 0x85, 0x6f, 0x4a, 0x51, 0xa8, 0xe9, 0x46, 0x4b, 0x50, 0x64, 0x90, 0xf4, 0xc7, 0xd3, 0x91, 0xc3, 0x86, 0xef, 0xe1, 0x13, 0x4c, 0xab, 0xb5, 0x46, 0xdf, 0xa4, 0x26, 0x0f, 0x80, 0x1c, 0x6a}; 244 unsigned char nonce[] = {0x01}; 245 unsigned char gas_price[] = {0x3b, 0x9a, 0xca, 0x00}; 246 unsigned char gas_limit[] = {0x52, 0x08}; 247 unsigned char to[] = {0x16, 0xcf,0xb6, 0x34, 0x67,0x45, 0x2e, 0xcf, 0x15, 0xe9, 0xd6, 0xac, 0x48, 0xe9, 0xb4, 0xda, 0x62, 0x87, 0x16, 0xc4}; 248 unsigned char value[] = {0x04, 0x00}; 249 unsigned char data[] = {0x66, 0x6f, 0x6f}; 250 unsigned char v[] = {0x26}; 251 unsigned char r[] = {0xd8, 0x25, 0xb2, 0xe7, 0x97, 0x55, 0x0c, 0x14, 0x4f, 0x5e, 0x8a, 0xca, 0xe4, 0x52, 0xbd, 0x81, 0xe1, 0x94, 0x7f, 0xdc, 0xbc, 0x34, 0x30, 0x24, 0x0e, 0x44, 0xe9, 0x67, 0xb8, 0x21, 0x81, 0xf4}; 252 unsigned char s[] = {0x30, 0x85, 0x6f, 0x4a, 0x51, 0xa8, 0xe9, 0x46, 0x4b, 0x50, 0x64, 0x90, 0xf4, 0xc7, 0xd3, 0x91, 0xc3, 0x86, 0xef, 0xe1, 0x13, 0x4c, 0xab, 0xb5, 0x46, 0xdf, 0xa4, 0x26, 0x0f, 0x80, 0x1c, 0x6a}; 253 254 rlp_encoder_t encoder; 255 256 rlp_init(&encoder, 1024, NULL); 257 rlp_descend(&encoder); 258 rlp_add(&encoder, 1, nonce); 259 rlp_add(&encoder, 4, gas_price); 260 rlp_add(&encoder, 2, gas_limit); 261 rlp_add(&encoder, 20, to); 262 rlp_add(&encoder, 2, value); 263 rlp_add(&encoder, 3, data); 264 rlp_add(&encoder, 1, v); 265 rlp_add(&encoder, 32, r); 266 rlp_add(&encoder, 32, s); 267 rlp_ascend(&encoder); 268 269 int i; 270 for (i = 0; i < sizeof(target); i++) { 271 fprintf(stderr, "%d %x %x\n", i, (unsigned char)encoder.buf[i], target[i]); 272 if (target[i] != (unsigned char)encoder.buf[i]) { 273 rlp_free(&encoder); 274 ck_abort_msg("aiee"); 275 } 276 } 277 278 ck_assert_mem_eq(encoder.buf, target, sizeof(target)); 279 ck_assert_int_eq(sizeof(target), encoder.size); 280 rlp_free(&encoder); 281 } 282 END_TEST 283 284 Suite *rlp_vector_suite(void) { 285 Suite *s; 286 TCase *tcb; 287 288 s = suite_create("rlp_vector"); 289 tcb = tcase_create("basic"); // examples from https://eth.wiki/fundamentals/rlp 290 // tcase_add_test(tcb, rlp_dog_test); 291 // tcase_add_test(tcb, rlp_catdog_test); 292 // tcase_add_test(tcb, rlp_lorem_test); 293 // tcase_add_test(tcb, rlp_set_theoretical_representation_of_three); 294 // tcase_add_test(tcb, rlp_set_theoretical_representation_of_three); 295 tcase_add_test(tcb, rlp_ethereum_tx_unsigned); 296 // tcase_add_test(tcb, rlp_ethereum_tx_signed); 297 suite_add_tcase(s, tcb); 298 299 300 return s; 301 } 302 303 304 int main(void) { 305 int num_fail; 306 307 Suite *s; 308 SRunner *sr; 309 310 s = rlp_vector_suite(); 311 sr = srunner_create(s); 312 313 srunner_run_all(sr, CK_VERBOSE); 314 num_fail = srunner_ntests_failed(sr); 315 srunner_free(sr); 316 317 return (num_fail == 0) ? EXIT_SUCCESS : EXIT_FAILURE; 318 }