39 assert_true(
strprefix(
"123456",
"123456"));
50#define teststr3 "three"
52#define assert_buf_equals_str(buf, str) \
53 assert_int_equal(BLENZ(buf), strlen(str)); \
54 assert_memory_equal(CBPTR(buf), str, BLENZ(buf));
63 buf_printf(&buf,
"%s",
"some text, too long to fit");
69 buf_catrunc(&buf,
"some other text, much too long to fit");
84 const int input_size = 10;
85 const uint8_t input[] = { 0x01, 0x00, 0xff, 0x10, 0xff, 0x00, 0xf0, 0x0f, 0x09, 0x0a };
90 unsigned int blocksize = 5;
91 const char *separator =
" ";
92 output =
format_hex_ex(input, input_size, maxoutput, blocksize, separator, &
gc);
93 assert_string_equal(output,
"0100ff10ff 00f00f090a");
96 output =
format_hex_ex(input, input_size, maxoutput, blocksize, separator, &
gc);
97 assert_string_equal(output,
"0100[more...]");
100 output =
format_hex_ex(input, input_size, maxoutput, blocksize, separator, &
gc);
101 assert_string_equal(output,
"0[more...]");
104 output =
format_hex_ex(input, input_size, maxoutput, blocksize, separator, &
gc);
105 assert_string_equal(output,
"0100ff10f");
108 output =
format_hex_ex(input, input_size, maxoutput, blocksize, separator, &
gc);
109 assert_string_equal(output,
"0100ff10");
228 assert_int_equal(bl_zerolen->
size, 1);
241 assert_int_equal(bl_emptybuffers->
size, 1);
243 assert_int_equal(
BLEN(buf), 0);
252 assert_ptr_equal(
gc.list + 1, buf.
data);
254 assert_null(
gc.list);
271 assert_ptr_equal(e + 1, buf3.
data);
272 assert_ptr_equal(e->
next + 1, buf2.
data);
277 assert_non_null(
gc.list);
281 assert_ptr_not_equal(e + 1, buf2.
data);
297 assert_ptr_not_equal(p1, p2);
299 memset(p1,
'1', 512);
300 memset(p2,
'2', 512);
306 assert_ptr_not_equal(p1, p1new);
309 assert_ptr_not_equal(p2, p2new);
312 memset(p3,
'3', 512);
322 strcpy(buf,
"There is \x01 a nice 1234 year old tr\x7f ee!");
324 assert_string_equal(buf,
"There is @ a nice 1234 year old tr@ ee!");
326 strcpy(buf,
"There is \x01 a nice 1234 year old tr\x7f ee!");
328 assert_string_equal(buf,
"There is \x01 a nice 1234 year old tr\x7f ee!");
331 strcpy(buf,
"There is \x01 a nice 1234 year old tr\x7f ee!");
333 assert_string_equal(buf,
"There is a nice 1234 year old tr ee!");
335 strcpy(buf,
"There is \x01 a nice 1234 year old tr\x7f ee!");
337 assert_string_equal(buf,
"There is @ a nice @@@@ 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\"\n year old \ntr\x7f ee!");
346 assert_string_equal(buf,
"There.is...a.'nice'..1234.\n.year.old.\ntr..ee.");
348 strcpy(buf,
"There is a \\'nice\\' \"1234\" [*] year old \ntree!");
350 assert_string_equal(buf,
"There is a .'nice.' \"1234\" [.] year old .tree!");
361 const char test1[] =
"There is a nice 1234\x00 year old tree!";
391#if defined(__GNUC__) || defined(__clang__)
394#if defined(__clang__)
395#pragma clang diagnostic push
396#pragma clang diagnostic ignored "-Wunknown-warning-option"
398#pragma GCC diagnostic push
399#pragma GCC diagnostic ignored "-Wformat-truncation"
402 char buf[10] = {
'a' };
405 ret = snprintf(buf,
sizeof(buf),
"0123456789abcde");
406 assert_int_equal(ret, 15);
407 assert_int_equal(buf[9],
'\0');
409 memset(buf,
'b',
sizeof(buf));
410 ret = snprintf(buf,
sizeof(buf),
"- %d - %d -", 77, 88);
411 assert_int_equal(ret, 11);
412 assert_int_equal(buf[9],
'\0');
414 memset(buf,
'c',
sizeof(buf));
415 ret = snprintf(buf,
sizeof(buf),
"- %8.2f", 77.8899);
416 assert_int_equal(ret, 10);
417 assert_int_equal(buf[9],
'\0');
419#if defined(__GNUC__) || defined(__clang__)
420#pragma GCC diagnostic pop
421#if defined(__clang__)
422#pragma clang diagnostic pop
443 const char test1[] =
"There is a nice 1234 year old tree!\n\r";
448 assert_string_equal(
BSTR(&buf),
"There is a nice 1234 year old tree!");
451 const char test2[] =
"CR_RESPONSE,MTIx\x0a\x00";
458 assert_string_equal(
BSTR(&buf2),
"CR_RESPONSE,MTIx");
472 assert_string_equal(ret,
"1234");
474 assert_int_equal(
buf_len(&buf), 0);
479 assert_string_equal(ret,
"123");
482 assert_int_equal(
buf_len(&buf), 1);
483 assert_memory_equal(
buf_bptr(&buf),
"5", 1);
498 const char test1[] =
"1234";
499 const char test2[] =
"12345";
500 assert_true(
buf_write(&buf, test1,
sizeof(test1)));
501 assert_string_equal(
BSTR(&buf),
"1234");
503 assert_string_equal(
BSTR(&buf),
"1234");
507 assert_memory_equal(
BPTR(&buf), test2, strlen(test2));
509 assert_string_equal(
BSTR(&buf),
"1234");
519#define A_TIMES_256 "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAO"
530 assert_int_equal(
buf_len(NULL), 0);
540 assert_int_equal(
buf_len(&buf), 0);
553 assert_true(
buf_write(&buf, test1,
sizeof(test1)));
563 assert_true(
buf_write(&buf, test1,
sizeof(test1)));
573 assert_true(
buf_write(&buf, test1,
sizeof(test1)));
579 assert_string_equal(line,
"EOL");
585 assert_string_equal(line,
"EOF");
594 const struct CMUnitTest tests[] = {
626 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 free_buf(struct buffer *buf)
Free the memory allocated for a 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 .
static void free_buf_gc(struct buffer *buf, struct gc_arena *gc)
void buf_catrunc(struct buffer *buf, const char *str)
Append a string to the physical end of a buffer that was truncated by buf_printf().
bool buf_printf(struct buffer *buf, const char *format,...)
printf-style append to a buffer with overflow check.
char * buf_extract_field(struct buffer *buf, char sep, struct gc_arena *gc)
Extract a field from buf that ends with the sep character.
void buf_null_terminate(struct buffer *buf)
Force a null terminator at the end of the buffer content.
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
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.
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)
Allocate a buffer of the given size under garbage collection.
char * format_hex_ex(const uint8_t *data, size_t size, size_t maxoutput, unsigned int space_break_flags, const char *separator, struct gc_arena *gc)
Format a binary buffer as a hex string.
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...
struct buffer alloc_buf(size_t size)
Allocate a buffer of the given size.
bool buf_parse(struct buffer *buf, const int delim, char *line, const int size)
Extract the next token from a buffer, delimited by a given character.
bool checked_snprintf(char *str, size_t size, const char *format,...)
Like snprintf() but returns an boolean.
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)
Remove trailing newline and carriage-return characters from a buffer.
bool string_check_buf(const struct buffer *buf, const unsigned int inclusive, const unsigned int exclusive)
Check a buffer if it only consists of allowed characters.
Buffer management functions and garbage collection.
#define CC_DOUBLE_QUOTE
double quote
#define CC_BLANK
space or tab
static uint8_t * buf_bptr(struct buffer *buf)
Return a pointer to the start of the buffer content.
#define BSTR(buf)
Return the buffer content pointer cast to char *.
#define CC_ANY
any character
#define BPTR(buf)
Return a pointer to the start of the buffer content.
#define CC_SINGLE_QUOTE
single quote
static bool buf_inc_len(struct buffer *buf, int inc)
Increase or decrease the length of a buffer.
#define CC_DIGIT
digit isdigit()
static bool buf_valid(const struct buffer *buf)
Return true iff buf is valid.
#define CC_CRLF
carriage return or newline
#define CC_ASTERISK
asterisk
#define CC_ALPHA
alphabetic isalpha()
#define CC_NEWLINE
newline
static int buf_len(const struct buffer *buf)
Return the length of the buffer content.
#define CC_SPACE
whitespace isspace()
static bool buf_write(struct buffer *dest, const void *src, size_t size)
Append data to a buffer.
#define CC_BACKSLASH
backslash
static bool buf_write_u8(struct buffer *dest, uint8_t data)
Append a uint8_t to a buffer.
#define BLEN(buf)
Return the length of the buffer content in bytes.
static void buf_reset_len(struct buffer *buf)
Reset the length and offset of a buffer to zero.
#define CC_NULL
null character \0
static void gc_free(struct gc_arena *a)
Free all allocations in a garbage collection arena.
#define CC_PRINT
printable (>= 32, != 127)
static bool buf_defined(const struct buffer *buf)
Return true iff buf has a non-NULL data pointer.
#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)
Allocate and return a new, empty garbage collection arena.
static SERVICE_STATUS status
A singly-linked list of buffers, with head/tail pointers for O(1) push.
size_t size
Current number of entries.
struct buffer_entry * head
Next item to pop/peek.
Wrapper structure for dynamically allocated memory.
uint8_t * data
Pointer to the allocated memory.
Garbage collection arena used to keep track of dynamically allocated memory.
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_checked_snprintf(void **state)
static void test_buffer_free_gc_two(void **state)
static void test_snprintf(void **state)
static void test_character_string_mod_buf(void **state)
void test_buffer_parse(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)
void test_buffer_null_terminate(void **state)
static void test_buffer_list_aggregate_separator_two(void **state)
static void test_buffer_extract_field(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_null_predicates(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...