Something went wrong. Try again.
forked from cz-nic/bird
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382/* * BIRD Library -- Parsing Numbers Functions Tests * * (c) 2026 CZ.NIC z.s.p.o. * * Can be freely distributed and used under the terms of the GNU GPL. */
#include "test/birdtest.h"#include "lib/string.h"
struct bstrtoul_data_out { u64 result; uint offset; /* Offset of @end pointer from the beginning of the string */ int errno_val;};
static voidbt_fmt_u64(char *buf, size_t size, const void *data_){ const struct bstrtoul_data_out *data = data_; snprintf(buf, size, "0x%lx (%lu)", data->result, data->result);}
static inttest_bstrtoul10(void *out_, const void *in_, const void *expected_out_){ const char *in = in_; const struct bstrtoul_data_out *expected_out = expected_out_; struct bstrtoul_data_out *out = out_;
errno = 0;
char *end = (char *) 1; out->result = bstrtoul10(in, &end);
bt_assert(out->result == expected_out->result); bt_assert(end == in + expected_out->offset); bt_assert(errno == expected_out->errno_val);
return 1;}
static intt_bstrtoul10(void){ struct bt_pair test_vectors[] = { { .in = "1234567890", .out = &(struct bstrtoul_data_out) { .result = 1234567890, .offset = 10, }, }, { .in = "987654321", .out = &(struct bstrtoul_data_out) { .result = 987654321, .offset = 9, }, }, { .in = "18446744073709551615", .out = &(struct bstrtoul_data_out) { .result = UINT64_MAX, .offset = 20, }, }, { .in = "0", .out = &(struct bstrtoul_data_out) { .result = 0, .offset = 1, }, }, { .in = "0001234", .out = &(struct bstrtoul_data_out) { .result = 1234, .offset = 7, }, }, { .in = "999999999999999999999999", .out = &(struct bstrtoul_data_out) { .result = UINT64_MAX, .offset = 19, .errno_val = ERANGE, }, }, };
return bt_assert_batch(test_vectors, test_bstrtoul10, bt_fmt_str, bt_fmt_u64);}
static inttest_bstrtoul16(void *out_, const void *in_, const void *expected_out_){ const char *in = in_; const struct bstrtoul_data_out *expected_out = expected_out_; struct bstrtoul_data_out *out = out_;
errno = 0;
char *end = (char *) 1; out->result = bstrtoul16(in, &end);
bt_assert(out->result == expected_out->result); bt_assert(end == in + expected_out->offset); bt_assert(errno == expected_out->errno_val);
return 1;}
static intt_bstrtoul16(void){ struct bt_pair test_vectors[] = { { .in = "123456789", .out = &(struct bstrtoul_data_out) { .result = 0x123456789, .offset = 9, }, }, { .in = "abcdef", .out = &(struct bstrtoul_data_out) { .result = 0xabcdef, .offset = 6, }, }, { .in = "ABCDEF", .out = &(struct bstrtoul_data_out) { .result = 0xABCDEF, .offset = 6, }, }, { .in = "123456789abcDEF", .out = &(struct bstrtoul_data_out) { .result = 0x123456789abcDEF, .offset = 15, }, }, { .in = "ffffffffffffffff", .out = &(struct bstrtoul_data_out) { .result = UINT64_MAX, .offset = 16, }, }, { .in = "0", .out = &(struct bstrtoul_data_out) { .result = 0, .offset = 1, }, }, { .in = "ffffffffffffffff0000", .out = &(struct bstrtoul_data_out) { .result = UINT64_MAX, .offset = 16, .errno_val = ERANGE, }, }, { .in = "0xad", .out = &(struct bstrtoul_data_out) { .result = 0, .offset = 1, }, }, { .in = "1234xyz", .out = &(struct bstrtoul_data_out) { .result = 0x1234, .offset = 4, }, }, };
return bt_assert_batch(test_vectors, test_bstrtoul16, bt_fmt_str, bt_fmt_u64);}
struct bytestr { int len; byte buf[32];};
static voidbt_fmt_bytestr(char *buf, size_t size, const void *data_){ const struct bytestr *data = data_;
if (data->len <= 0) { snprintf(buf, size, "..."); return; }
char *tmp = allocz(2 * data->len + 1); char *t = tmp;
for (int i = 0; i < data->len; i++) t += bsprintf(t, "%02x", data->buf[i]);
snprintf(buf, size, "0x%s", tmp);}
static inttest_bstrhextobin(void *out_, const void *in_, const void *expected_out_) { const char *in = in_; const struct bytestr *expected_out = expected_out_; struct bytestr *out = out_;
out->len = bstrhextobin(in, out->buf);
bt_assert(out->len == expected_out->len);
if (out->len > 0) bt_assert(memcmp(out->buf, expected_out->buf, expected_out->len) == 0);
return 1;}
static intt_bstrhextobin(void){ struct bt_pair test_vectors[] = { { .in = "1234567890", .out = &(struct bytestr) { .buf = { 0x12, 0x34, 0x56, 0x78, 0x90 }, .len = 5, }, }, { .in = "1234567890abcdef", .out = &(struct bytestr) { .buf = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xab, 0xcd, 0xef }, .len = 8, }, }, { .in = "abcdef", .out = &(struct bytestr) { .buf = { 0xab, 0xcd, 0xef }, .len = 3, }, }, { .in = "ABCDEF", .out = &(struct bytestr) { .buf = { 0xAB, 0xCD, 0xEF }, .len = 3, }, }, { .in = "ffffffffffffffff", .out = &(struct bytestr) { .buf = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }, .len = 8, }, }, { .in = "00", .out = &(struct bytestr) { .buf = { 0x0 }, .len = 1, }, }, { .in = "::1234", .out = &(struct bytestr) { .buf = { 0x12, 0x34 }, .len = 2, }, }, { .in = "-ab cd.ef", .out = &(struct bytestr) { .buf = { 0xab, 0xcd, 0xef }, .len = 3, }, }, { .in = "xyz", .out = &(struct bytestr) { .len = -1, }, }, };
return bt_assert_batch(test_vectors, test_bstrhextobin, bt_fmt_str, bt_fmt_bytestr);}
static inttest_bstrbintohex(void *out_, const void *in_, const void *expected_out_){ const struct bytestr *in = in_; const char *expected_out = expected_out_; char *out = out_;
bt_assert(bstrbintohex(in->buf, in->len, out, sizeof(bt_out_fmt_buf) / 4, ':') == 0);
size_t len_expected_out = strlen(expected_out); size_t len_out = strlen(out);
if (len_out != len_expected_out) return 0;
return memcmp(out, expected_out, len_expected_out) == 0;}
static intt_bstrbintohex(void){ struct bt_pair test_vectors[] = { { .in = &(struct bytestr) { .buf = { 0x12, 0x34, 0x56, 0x78, 0x90 }, .len = 5, }, .out = "12:34:56:78:90", }, { .in = &(struct bytestr) { .buf = { 0xab, 0xcd, 0xef }, .len = 3, }, .out = "ab:cd:ef", }, { .in = &(struct bytestr) { .buf = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xab, 0xcd, 0xef }, .len = 8, }, .out = "12:34:56:78:90:ab:cd:ef", }, { .in = &(struct bytestr) { .buf = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }, .len = 8, }, .out = "ff:ff:ff:ff:ff:ff:ff:ff", }, { .in = &(struct bytestr) { .buf = { 0x0 }, .len = 1, }, .out = "00", }, { .in = &(struct bytestr) { .buf = { 0x0, 0x0, 0x0, 0x0 }, .len = 4, }, .out = "00:00:00:00", }, };
return bt_assert_batch(test_vectors, test_bstrbintohex, bt_fmt_bytestr, bt_fmt_str);}
intmain(int argc, char **argv){ bt_init(argc, argv);
bt_test_suite(t_bstrtoul10, "Converting decimal string into number"); bt_test_suite(t_bstrtoul16, "Converting hexadecimal string into number"); bt_test_suite(t_bstrhextobin, "Converting hexadecimal string into bytestring"); bt_test_suite(t_bstrbintohex, "Converting bytestring into hexadecimal string");
return bt_exit_value();}