From: Katerina Kubecova Date: Thu, 11 Jan 2024 13:50:12 +0000 (+0100) Subject: cbor_test.c, fixing reading big nums X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=cbd5e4b415e92d4604297fb9700cf72bc6ebba53;p=thirdparty%2Fbird.git cbor_test.c, fixing reading big nums --- diff --git a/client/client.c b/client/client.c index 02774bce0..061b9b5af 100644 --- a/client/client.c +++ b/client/client.c @@ -475,7 +475,7 @@ server_got_binary(int c) length = length << 8; length += server_read_buf[3 + i]; } - printf("length of message is %i\n", length); + if (length > c - 7) { byte bigger_buf[length]; @@ -487,15 +487,10 @@ server_got_binary(int c) { DIE("Server read error"); } - print_cbor_response(&bigger_buf[13], length); + print_cbor_response(&bigger_buf[13], length - 6); } else { - printf("no need to load more\n"); - for (int i = 0; i < 15; i++) - { - printf("%i %x\n",i, server_read_buf[i] ); - } print_cbor_response(&server_read_buf[13], c); } } diff --git a/client/print_cbor.c b/client/print_cbor.c index 4e526f742..839ea5309 100644 --- a/client/print_cbor.c +++ b/client/print_cbor.c @@ -1508,7 +1508,7 @@ void print_show_protocols(struct buff_reader *buf_read) void print_cbor_response(byte *cbor, int len) { - // + // This is only for debug purposes FILE *write_ptr; write_ptr = fopen("arrived.cbor", "wb"); diff --git a/lib/Makefile b/lib/Makefile index b7883ef95..55d3ce76a 100644 --- a/lib/Makefile +++ b/lib/Makefile @@ -6,6 +6,6 @@ src += event.c timer.c tbf.c obj := $(src-o-files) $(all-daemon) -tests_src := bitmap_test.c heap_test.c buffer_test.c event_test.c flowspec_test.c bitops_test.c patmatch_test.c fletcher16_test.c slist_test.c checksum_test.c lists_test.c mac_test.c ip_test.c hash_test.c printf_test.c slab_test.c +tests_src := bitmap_test.c heap_test.c buffer_test.c event_test.c flowspec_test.c bitops_test.c patmatch_test.c fletcher16_test.c slist_test.c checksum_test.c lists_test.c mac_test.c ip_test.c hash_test.c cbor_test.c printf_test.c slab_test.c tests_targets := $(tests_targets) $(tests-target-files) tests_objs := $(tests_objs) $(src-o-files) diff --git a/lib/cbor_test.c b/lib/cbor_test.c new file mode 100644 index 000000000..56c366871 --- /dev/null +++ b/lib/cbor_test.c @@ -0,0 +1,137 @@ + +#include "test/birdtest.h" +#include "nest/cbor.h" +#include "nest/cbor_parse_tools.h" + +#define BUFF_LEN 100 + +struct cbor_writer *w; +struct buff_reader reader; + +void print_to_file_for_control_from_outside(void) +{ + FILE *write_ptr; + + write_ptr = fopen("a.cbor", "wb"); + + fwrite(w->cbor, w->pt, 1, write_ptr); + fclose(write_ptr); + +} + +static int test_int(void) +{ + reader.pt = w->pt = 0; + int num_items = 13; + int64_t test_int[] = {-123456789012345678, -1234567890, -12345, -123, -25, -13, 0, 13, 25, 123, 12345, 1234567890, 123456789012345678}; + byte bin_int[] = {0x8d, 0x3b, 0x1, 0xb6, 0x9b, 0x4b, 0xa6, 0x30, 0xf3, 0x4d, 0x3a, 0x49, 0x96, 0x2, 0xd1, 0x39, 0x30, 0x38, 0x38, 0x7a, 0x38, 0x18, 0x2c, 0x0, 0xd, 0x18, 0x19, 0x18, 0x7b, 0x19, 0x30, 0x39, 0x1a, 0x49, 0x96, 0x2, 0xd2, 0x1b, 0x1, 0xb6, 0x9b, 0x4b, 0xa6, 0x30, 0xf3, 0x4e}; + cbor_open_list_with_length(w, num_items); + for (int i = 0; i < num_items; i++) + { + cbor_add_int(w, test_int[i]); + } + + for (long unsigned int i = 0; i < sizeof(bin_int); i++) + { + bt_assert((w->cbor[i] & 0xff) == (bin_int[i] & 0xff)); + } + + struct value val = get_value(&reader); + bt_assert(val.major = ARRAY); + bt_assert(val.val = num_items); + for (int i = 0; i < num_items; i++) + { + val = get_value(&reader); + bt_assert(val.major == NEG_INT || val.major == UINT); + bt_assert(val.val == test_int[i]); + } + return 1; +} + +static int non_aligned_int(void) +{ + w->pt = reader.pt = 0; + int num_items = 4; + cbor_open_list_with_length(w, num_items); + + cbor_add_int(w, 30); + w->cbor[w->pt - 1] = 1; + + cbor_add_int(w, 300); + w->cbor[w->pt - 2] = 0; + w->cbor[w->pt - 1] = 1; + + cbor_add_int(w, 300000000); + for (int i = 4; i > 1; i--) + { + w->cbor[w->pt - i] = 0; + } + w->cbor[w->pt - 1] = 1; + + cbor_add_int(w, 30000000000000000); + for (int i = 8; i > 1; i--) + { + w->cbor[w->pt - i] = 0; + } + w->cbor[w->pt - 1] = 1; + + struct value val = get_value(&reader); + bt_assert(val.major = ARRAY); + bt_assert(val.val = num_items); + + for (int i = 0; i < num_items; i++) + { + val = get_value(&reader); + bt_assert(val.major == UINT); + bt_assert(val.val == 1); + } + return 1; +} + +static int test_majors(void) +{ + w->pt = reader.pt = 0; + cbor_open_block(w); + cbor_open_list_with_length(w, 4); + cbor_add_string(w, "b"); + cbor_add_int(w, 1); + cbor_add_int(w, -1); + cbor_add_ipv4(w, 302252032); + cbor_close_block_or_list(w); + + struct value val = get_value(&reader); + bt_assert(val.major == BLOCK); + val = get_value(&reader); + bt_assert(val.major == ARRAY); + val = get_value(&reader); + bt_assert(val.major == TEXT); + reader.pt += val.val; + val = get_value(&reader); + bt_assert(val.major == UINT); + val = get_value(&reader); + bt_assert(val.major == NEG_INT); + val = get_value(&reader); + bt_assert(val.major == TAG); + val = get_value(&reader); + bt_assert(val.major == BYTE_STR); + reader.pt += val.val; + val = get_value(&reader); + bt_assert(val_is_break(val)); + return 1; +} + +int main(int argc, char *argv[]) +{ + bt_init(argc, argv); + byte buff[BUFF_LEN]; + w = cbor_init(buff, BUFF_LEN, tmp_linpool); + reader.buff = buff; + reader.size = BUFF_LEN; + reader.pt = 0; + + bt_test_suite(test_int, "Adding and reading integer from cbor."); + bt_test_suite(non_aligned_int, "Reading non-alligned int from cbor."); + bt_test_suite(test_majors, "Test cbor datatypes."); + + return bt_exit_value(); +} diff --git a/nest/cbor.c b/nest/cbor.c index 76d0ff86e..f8c6e10b3 100644 --- a/nest/cbor.c +++ b/nest/cbor.c @@ -56,7 +56,7 @@ void cbor_add_int(struct cbor_writer *writer, int64_t item) } else { - write_item(writer, 1, -item); + write_item(writer, 1, -item - 1); } } diff --git a/nest/cbor_cmds.c b/nest/cbor_cmds.c index 8806a0f62..a7107b330 100644 --- a/nest/cbor_cmds.c +++ b/nest/cbor_cmds.c @@ -105,7 +105,6 @@ cmd_show_protocols_cbor(byte *tbuf, uint capacity, struct arg_list *args, struct } } cbor_close_block_or_list(w); - cbor_write_to_file(w, "show_protocol_first_try.cbor"); return w->pt; } diff --git a/nest/cli.c b/nest/cli.c index 857dc8038..c829d3f21 100644 --- a/nest/cli.c +++ b/nest/cli.c @@ -399,7 +399,6 @@ cli_kick(cli *c) uint yi_process(uint size, byte *rbuf, byte *tbuf, uint tbsize) { log("capacity %i buffer %i", tbsize, tbuf); - //TODO if (detect_down(size, rbuf)) { order_shutdown(0);