]> git.ipfire.org Git - thirdparty/bird.git/commitdiff
cbor_test.c, fixing reading big nums
authorKaterina Kubecova <katerina.kubecova@nic.cz>
Thu, 11 Jan 2024 13:50:12 +0000 (14:50 +0100)
committerKaterina Kubecova <katerina.kubecova@nic.cz>
Thu, 11 Jan 2024 13:50:12 +0000 (14:50 +0100)
client/client.c
client/print_cbor.c
lib/Makefile
lib/cbor_test.c [new file with mode: 0644]
nest/cbor.c
nest/cbor_cmds.c
nest/cli.c

index 02774bce090e36f99d5bf171b40828bb6096160b..061b9b5af6df5a4fb5a2094e6d95e511612bbee1 100644 (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);
     }
   }
index 4e526f742f836563f9bbf1e4763f92f3bcd5631c..839ea53091c54f16acbd589396b40494e6d79f09 100644 (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");
index b7883ef95c470e690ae21a9ec90088853352bd9a..55d3ce76a762d299fb51548785185c77bb721569 100644 (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)
diff --git a/lib/cbor_test.c b/lib/cbor_test.c
new file mode 100644 (file)
index 0000000..56c3668
--- /dev/null
@@ -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();
+}
index 76d0ff86e9602233eda478dc8c3739e506edd97e..f8c6e10b38c814adfe6c1458e751104f9a8954ea 100644 (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);
   }
 }
 
index 8806a0f620839470ca701796b1270dae61aa7f18..a7107b330cc24a522c8e77ba037ff5a713429713 100644 (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;
 }
 
index 857dc8038dcf879f2d0916743881583e38ced7a3..c829d3f2173566f3c309f5b7888699f9804ecb75 100644 (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);