40 assert_true(
strprefix(
"123456",
"123456"));
51#define teststr3 "three"
53#define assert_buf_equals_str(buf, str) \
54 assert_int_equal(BLEN(buf), strlen(str)); \
55 assert_memory_equal(BPTR(buf), str, BLEN(buf));
64 buf_printf(&buf,
"%s",
"some text, too long to fit");
70 buf_catrunc(&buf,
"some other text, much too long to fit");
87 0x01, 0x00, 0xff, 0x10, 0xff, 0x00, 0xf0, 0x0f, 0x09, 0x0a
93 unsigned int blocksize = 5;
94 char *separator =
" ";
95 output =
format_hex_ex(input, input_size, maxoutput, blocksize, separator, &
gc);
96 assert_string_equal(output,
"0100ff10ff 00f00f090a");
99 output =
format_hex_ex(input, input_size, maxoutput, blocksize, separator, &
gc);
100 assert_string_equal(output,
"0100[more...]");
103 output =
format_hex_ex(input, input_size, maxoutput, blocksize, separator, &
gc);
104 assert_string_equal(output,
"0[more...]");
107 output =
format_hex_ex(input, input_size, maxoutput, blocksize, separator, &
gc);
108 assert_string_equal(output,
"0100ff10f");
111 output =
format_hex_ex(input, input_size, maxoutput, blocksize, separator, &
gc);
112 assert_string_equal(output,
"0100ff10");
232 assert_int_equal(bl_zerolen->
size, 1);
245 assert_int_equal(bl_emptybuffers->
size, 1);
275 assert_ptr_equal(e + 1, buf3.
data);
276 assert_ptr_equal(e->
next + 1, buf2.
data);
285 assert_ptr_not_equal(e + 1, buf2.
data);
301 assert_ptr_not_equal(p1, p2);
303 memset(p1,
'1', 512);
304 memset(p2,
'2', 512);
310 assert_ptr_not_equal(p1, p1new);
313 assert_ptr_not_equal(p2, p2new);
316 memset(p3,
'3', 512);
326 strcpy(buf,
"There is \x01 a nice 1234 year old tr\x7f ee!");
328 assert_string_equal(buf,
"There is @ a nice 1234 year old tr@ ee!");
330 strcpy(buf,
"There is \x01 a nice 1234 year old tr\x7f ee!");
332 assert_string_equal(buf,
"There is \x01 a nice 1234 year old tr\x7f ee!");
335 strcpy(buf,
"There is \x01 a nice 1234 year old tr\x7f ee!");
337 assert_string_equal(buf,
"There is a nice 1234 year old tr ee!");
339 strcpy(buf,
"There is \x01 a nice 1234 year old tr\x7f ee!");
341 assert_string_equal(buf,
"There is @ a nice @@@@ year old tr@ ee!");
343 strcpy(buf,
"There is \x01 a nice 1234 year old tr\x7f ee!");
345 assert_string_equal(buf,
"There.is...a.nice......year.old.tr..ee.");
347 strcpy(buf,
"There is \x01 a 'nice' \"1234\"\n year old \ntr\x7f ee!");
349 assert_string_equal(buf,
"There.is...a.'nice'..1234.\n.year.old.\ntr..ee.");
351 strcpy(buf,
"There is a \\'nice\\' \"1234\" [*] year old \ntree!");
353 assert_string_equal(buf,
"There is a .'nice.' \"1234\" [.] year old .tree!");
364 const char test1[] =
"There is a nice 1234\x00 year old tree!";
394#if defined(__GNUC__) || defined(__clang__)
397#if defined(__clang__)
398#pragma clang diagnostic push
399#pragma clang diagnostic ignored "-Wunknown-warning-option"
401#pragma GCC diagnostic push
402#pragma GCC diagnostic ignored "-Wformat-truncation"
405 char buf[10] = {
'a' };
408 ret =
snprintf(buf,
sizeof(buf),
"0123456789abcde");
412 memset(buf,
'b',
sizeof(buf));
413 ret =
snprintf(buf,
sizeof(buf),
"- %d - %d -", 77, 88);
417 memset(buf,
'c',
sizeof(buf));
418 ret =
snprintf(buf,
sizeof(buf),
"- %8.2f", 77.8899);
422#if defined(__GNUC__) || defined(__clang__)
423#pragma GCC diagnostic pop
424#if defined(__clang__)
425#pragma clang diagnostic pop
436 const char test1[] =
"There is a nice 1234 year old tree!\n\r";
444 const char test2[] =
"CR_RESPONSE,MTIx\x0a\x00";
460 const struct CMUnitTest tests[] = {
494 return cmocka_run_group_tests_name(
"buffer", tests, NULL, NULL);
struct buffer_entry * buffer_list_push_data(struct buffer_list *ol, const void *data, size_t size)
Allocates and appends a new buffer containing data of length size.
void buffer_list_aggregate_separator(struct buffer_list *bl, const size_t max_len, const char *sep)
Aggregates as many buffers as possible from bl in a new buffer of maximum length max_len .
static void free_buf_gc(struct buffer *buf, struct gc_arena *gc)
void buf_catrunc(struct buffer *buf, const char *str)
bool buf_printf(struct buffer *buf, const char *format,...)
void * gc_realloc(void *ptr, size_t size, struct gc_arena *a)
allows to realloc a pointer previously allocated by gc_malloc or gc_realloc
char * format_hex_ex(const uint8_t *data, int size, int maxoutput, unsigned int space_break_flags, const char *separator, struct gc_arena *gc)
struct buffer_list * buffer_list_new(void)
Allocate an empty buffer list of capacity max_size.
struct buffer * buffer_list_peek(struct buffer_list *ol)
Retrieve the head buffer.
bool string_check_buf(struct buffer *buf, const unsigned int inclusive, const unsigned int exclusive)
Check a buffer if it only consists of allowed characters.
void buffer_list_free(struct buffer_list *ol)
Frees a buffer list and all the buffers in it.
struct buffer alloc_buf_gc(size_t size, struct gc_arena *gc)
bool string_mod(char *str, const unsigned int inclusive, const unsigned int exclusive, const char replace)
Modifies a string in place by replacing certain classes of characters of it with a specified characte...
void buffer_list_push(struct buffer_list *ol, const char *str)
Allocates and appends a new buffer containing str as data to ol.
void buf_chomp(struct buffer *buf)
#define CC_DOUBLE_QUOTE
double quote
#define CC_BLANK
space or tab
#define CC_ANY
any character
#define CC_SINGLE_QUOTE
single quote
static bool buf_inc_len(struct buffer *buf, int inc)
#define CC_DIGIT
digit isdigit()
#define CC_CRLF
carriage return or newline
#define CC_ASTERISK
asterisk
#define CC_ALPHA
alphabetic isalpha()
#define CC_NEWLINE
newline
#define CC_SPACE
whitespace isspace()
static bool buf_write(struct buffer *dest, const void *src, size_t size)
#define CC_BACKSLASH
backslash
#define CC_NULL
null character \0
static void gc_free(struct gc_arena *a)
#define CC_PRINT
printable (>= 32, != 127)
#define CC_ALNUM
alphanumeric isalnum()
static bool strprefix(const char *str, const char *prefix)
Return true iff str starts with prefix.
static struct gc_arena gc_new(void)
struct buffer_entry * head
Wrapper structure for dynamically allocated memory.
uint8_t * data
Pointer to the allocated memory.
int len
Length in bytes of the actual content within the allocated memory.
Garbage collection arena used to keep track of dynamically allocated memory.
struct gc_entry * list
First element of the linked list of gc_entry structures.
Garbage collection entry for one dynamically allocated block of memory.
struct gc_entry * next
Pointer to the next item in the linked list.
struct buffer_list * zero_length_strings
struct buffer_list * one_two_three
struct buffer_list * empty
struct buffer_list * empty_buffers
static void test_character_class(void **state)
static void test_buffer_format_hex_ex(void **state)
static int test_buffer_list_setup(void **state)
static void test_buffer_list_aggregate_separator_all(void **state)
#define assert_buf_equals_str(buf, str)
static void test_buffer_free_gc_two(void **state)
static void test_snprintf(void **state)
static void test_character_string_mod_buf(void **state)
static void test_buffer_list_aggregate_separator_zerolen(void **state)
static void test_buffer_printf_catrunc(void **state)
void test_buffer_chomp(void **state)
static void test_buffer_list_aggregate_separator_two(void **state)
static void test_buffer_list_aggregate_separator_noop(void **state)
static void test_buffer_free_gc_one(void **state)
static void test_buffer_list_aggregate_separator_nosep(void **state)
static void test_buffer_gc_realloc(void **state)
static void test_buffer_list_aggregate_separator_empty(void **state)
static int test_buffer_list_teardown(void **state)
static void test_buffer_list_aggregate_separator_emptybuffers(void **state)
static void test_buffer_strprefix(void **state)
static void openvpn_unit_test_setup(void)
Sets up the environment for unit tests like making both stderr and stdout non-buffered to avoid messa...