Go to the documentation of this file.
40 assert_true(
strprefix(
"123456",
"123456"));
49 #define teststr1 "one"
50 #define teststr2 "two"
51 #define teststr3 "three"
52 #define teststr4 "four"
54 #define assert_buf_equals_str(buf, str) \
55 assert_int_equal(BLEN(buf), strlen(str)); \
56 assert_memory_equal(BPTR(buf), str, BLEN(buf));
65 buf_printf(&buf,
"%s",
"some text, too long to fit");
71 buf_catrunc(&buf,
"some other text, much too long to fit");
86 const int input_size = 10;
87 const uint8_t input[] = {
88 0x01, 0x00, 0xff, 0x10, 0xff, 0x00, 0xf0, 0x0f, 0x09, 0x0a
94 unsigned int blocksize = 5;
95 char *separator =
" ";
96 output =
format_hex_ex(input, input_size, maxoutput, blocksize, separator, &gc);
97 assert_string_equal(output,
"0100ff10ff 00f00f090a");
100 output =
format_hex_ex(input, input_size, maxoutput, blocksize, separator, &gc);
101 assert_string_equal(output,
"0100[more...]");
104 output =
format_hex_ex(input, input_size, maxoutput, blocksize, separator, &gc);
105 assert_string_equal(output,
"0[more...]");
108 output =
format_hex_ex(input, input_size, maxoutput, blocksize, separator, &gc);
109 assert_string_equal(output,
"0100ff10f");
112 output =
format_hex_ex(input, input_size, maxoutput, blocksize, separator, &gc);
113 assert_string_equal(output,
"0100ff10");
233 assert_int_equal(bl_zerolen->
size, 1);
246 assert_int_equal(bl_emptybuffers->
size, 1);
248 assert_int_equal(
BLEN(buf), 0);
257 assert_ptr_equal(gc.
list + 1, buf.
data);
259 assert_null(gc.
list);
276 assert_ptr_equal(e + 1, buf3.
data);
277 assert_ptr_equal(e->
next + 1, buf2.
data);
282 assert_non_null(gc.
list);
286 assert_ptr_not_equal(e + 1, buf2.
data);
302 assert_ptr_not_equal(p1, p2);
304 memset(p1,
'1', 512);
305 memset(p2,
'2', 512);
310 void *p1new =
gc_realloc(p1, 512ul * 1024, &gc);
311 assert_ptr_not_equal(p1, p1new);
313 void *p2new =
gc_realloc(p2, 512ul * 1024, &gc);
314 assert_ptr_not_equal(p2, p2new);
317 memset(p3,
'3', 512);
327 strcpy(buf,
"There is \x01 a nice 1234 year old tr\x7f ee!");
329 assert_string_equal(buf,
"There is @ a nice 1234 year old tr@ ee!");
331 strcpy(buf,
"There is \x01 a nice 1234 year old tr\x7f ee!");
333 assert_string_equal(buf,
"There is \x01 a nice 1234 year old tr\x7f ee!");
336 strcpy(buf,
"There is \x01 a nice 1234 year old tr\x7f ee!");
338 assert_string_equal(buf,
"There is a nice 1234 year old tr ee!");
340 strcpy(buf,
"There is \x01 a nice 1234 year old tr\x7f ee!");
342 assert_string_equal(buf,
"There is @ a nice @@@@ year old tr@ ee!");
344 strcpy(buf,
"There is \x01 a nice 1234 year old tr\x7f ee!");
346 assert_string_equal(buf,
"There.is...a.nice......year.old.tr..ee.");
348 strcpy(buf,
"There is \x01 a 'nice' \"1234\"\n year old \ntr\x7f ee!");
350 assert_string_equal(buf,
"There.is...a.'nice'..1234.\n.year.old.\ntr..ee.");
352 strcpy(buf,
"There is a \\'nice\\' \"1234\" [*] year old \ntree!");
354 assert_string_equal(buf,
"There is a .'nice.' \"1234\" [.] year old .tree!");
365 const char test1[] =
"There is a nice 1234\x00 year old tree!";
394 #if defined(__GNUC__)
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");
409 assert_int_equal(ret, 15);
410 assert_int_equal(buf[9],
'\0');
412 memset(buf,
'b',
sizeof(buf));
413 ret = snprintf(buf,
sizeof(buf),
"- %d - %d -", 77, 88);
414 assert_int_equal(ret, 11);
415 assert_int_equal(buf[9],
'\0');
417 memset(buf,
'c',
sizeof(buf));
418 ret = snprintf(buf,
sizeof(buf),
"- %8.2f", 77.8899);
419 assert_int_equal(ret, 10);
420 assert_int_equal(buf[9],
'\0');
422 #if defined(__GNUC__)
423 #pragma GCC diagnostic pop
424 #if defined(__clang__)
425 #pragma clang diagnostic pop
434 const struct CMUnitTest tests[] = {
467 return cmocka_run_group_tests_name(
"buffer", tests, NULL, NULL);
struct gc_entry * list
First element of the linked list of gc_entry structures.
static void test_buffer_list_aggregate_separator_all(void **state)
#define CC_BACKSLASH
backslash
struct buffer_list * empty_buffers
static void test_character_class(void **state)
static void test_buffer_free_gc_two(void **state)
#define assert_buf_equals_str(buf, str)
static struct gc_arena gc_new(void)
struct gc_entry * next
Pointer to the next item in the linked list.
#define CC_ASTERISK
asterisk
struct buffer alloc_buf_gc(size_t size, struct gc_arena *gc)
Garbage collection entry for one dynamically allocated block of memory.
struct buffer * buffer_list_peek(struct buffer_list *ol)
Retrieve the head buffer.
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 .
struct buffer_list * empty
char * format_hex_ex(const uint8_t *data, int size, int maxoutput, unsigned int space_break_flags, const char *separator, struct gc_arena *gc)
static void test_buffer_format_hex_ex(void **state)
#define CC_ALNUM
alphanumeric isalnum()
static void test_buffer_list_aggregate_separator_zerolen(void **state)
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.
#define CC_NEWLINE
newline
static void test_buffer_strprefix(void **state)
#define CC_DOUBLE_QUOTE
double quote
static void test_buffer_printf_catrunc(void **state)
static bool buf_inc_len(struct buffer *buf, int inc)
#define CC_SINGLE_QUOTE
single quote
#define CC_PRINT
printable (>= 32, != 127)
static void test_buffer_list_aggregate_separator_emptybuffers(void **state)
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.
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.
#define CC_DIGIT
digit isdigit()
#define CC_BLANK
space or tab
struct buffer_list * zero_length_strings
Wrapper structure for dynamically allocated memory.
struct buffer_list * buffer_list_new(void)
Allocate an empty buffer list of capacity max_size.
#define CC_SPACE
whitespace isspace()
static bool buf_write(struct buffer *dest, const void *src, size_t size)
#define CC_ANY
any character
#define CC_ALPHA
alphabetic isalpha()
Garbage collection arena used to keep track of dynamically allocated memory.
static void free_buf_gc(struct buffer *buf, struct gc_arena *gc)
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...
static void test_buffer_free_gc_one(void **state)
static bool strprefix(const char *str, const char *prefix)
Return true iff str starts with prefix.
static void gc_free(struct gc_arena *a)
static void test_buffer_list_aggregate_separator_nosep(void **state)
static void test_buffer_list_aggregate_separator_empty(void **state)
void buffer_list_free(struct buffer_list *ol)
Frees a buffer list and all the buffers in it.
#define CC_NULL
null character \0
struct buffer_list * one_two_three
static int test_buffer_list_setup(void **state)
static void test_buffer_gc_realloc(void **state)
static void test_buffer_list_aggregate_separator_noop(void **state)
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
static int test_buffer_list_teardown(void **state)
bool buf_printf(struct buffer *buf, const char *format,...)
struct buffer_entry * head
static void test_snprintf(void **state)
static void test_buffer_list_aggregate_separator_two(void **state)
uint8_t * data
Pointer to the allocated memory.
void buf_catrunc(struct buffer *buf, const char *str)
static void test_character_string_mod_buf(void **state)