0
0
mirror of https://gitlab.nic.cz/labs/bird.git synced 2024-09-16 18:35:19 +00:00

cbor_test.c, fixing reading big nums

This commit is contained in:
Katerina Kubecova 2024-01-11 14:50:12 +01:00
parent f8bc20a211
commit cbd5e4b415
7 changed files with 142 additions and 12 deletions

View File

@ -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);
}
}

View File

@ -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");

View File

@ -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)

137
lib/cbor_test.c Normal file
View File

@ -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();
}

View File

@ -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);
}
}

View File

@ -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;
}

View File

@ -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);