46 const char short_string1[] =
"apple";
47 const char short_string2[] =
"banana";
48 char short_string_cat[30];
49 const char long_string1[] =
"applebananapeachmangocherrypeargrapeplumlimetangerinepomegranategravel";
50 const char long_string2[] =
"passionuglinectarinepineapplekiwilemonpaintthinner";
51 char long_string_cat[200];
52 char string_limit_cat[11];
53 const int string_limit = 5;
59 info->name =
"str_test";
60 info->category =
"/main/strings/";
61 info->summary =
"Test dynamic string operations";
62 info->description =
"Test setting and appending stack and heap-allocated strings";
67 snprintf(short_string_cat,
sizeof(short_string_cat),
"%s%s", short_string1, short_string2);
68 snprintf(long_string_cat,
sizeof(long_string_cat),
"%s%s", long_string1, long_string2);
69 snprintf(string_limit_cat, string_limit,
"%s", long_string1);
70 strncat(string_limit_cat, long_string2, string_limit);
92 if (
ast_str_set(&stack_str, 0,
"%s", short_string1) < 0) {
111 "but instead got %s\n", short_string_cat,
ast_str_buffer(stack_str));
124 if (
ast_str_set(&stack_str, -1,
"%s", long_string1) < 0) {
154 if (
ast_str_set(&heap_str, 0,
"%s", short_string1) < 0) {
173 "but instead got %s\n", short_string_cat,
ast_str_buffer(stack_str));
186 if (
ast_str_set(&heap_str, -1,
"%s", long_string1) < 0) {
193 "instructed not to. Was %d and now is %d\n", current_size, (
int)
ast_str_size(heap_str));
211 "instructed not to. Was %d and now is %d\n", current_size, (
int)
ast_str_size(heap_str));
223 if (
ast_str_set(&heap_str, 0,
"%s", long_string1) < 0) {
242 "but have %s instead\n", long_string_cat,
ast_str_buffer(heap_str));
256 info->name =
"begins_with";
257 info->category =
"/main/strings/";
258 info->summary =
"Test ast_begins_with";
259 info->description =
"Test ast_begins_with";
285 info->name =
"ends_with";
286 info->category =
"/main/strings/";
287 info->summary =
"Test ast_ends_with";
288 info->description =
"Test ast_ends_with";
313 char *
test1, *test2, *test3;
317 info->name =
"strsep";
318 info->category =
"/main/strings/";
319 info->summary =
"Test ast_strsep";
320 info->description =
"Test ast_strsep";
326 test1 =
ast_strdupa(
"ghi=jkl,mno='pqr,stu',abc=def, vwx = yz1 , vwx = yz1 , '"
327 " vwx = yz1 ' , ' vwx , yz1 ',v\"w\"x, '\"x,v\",\"x\"' , \" i\\'m a test\""
328 ", \" i\\'m a, test\", \" i\\'m a, test\", e\\,nd, end\\");
331 ast_test_validate(
test, 0 == strcmp(
"ghi=jkl", test2));
334 ast_test_validate(
test, 0 == strcmp(
"ghi", test3));
337 ast_test_validate(
test, 0 == strcmp(
"jkl", test3));
340 ast_test_validate(
test, 0 == strcmp(
"mno='pqr,stu'", test2));
343 ast_test_validate(
test, 0 == strcmp(
"mno", test3));
346 ast_test_validate(
test, 0 == strcmp(
"'pqr,stu'", test3));
349 ast_test_validate(
test, 0 == strcmp(
"abc=def", test2));
352 ast_test_validate(
test, 0 == strcmp(
" vwx = yz1 ", test2));
355 ast_test_validate(
test, 0 == strcmp(
"vwx = yz1", test2));
358 ast_test_validate(
test, 0 == strcmp(
" vwx = yz1 ", test2));
361 ast_test_validate(
test, 0 == strcmp(
"vwx , yz1", test2));
364 ast_test_validate(
test, 0 == strcmp(
"v\"w\"x", test2));
367 ast_test_validate(
test, 0 == strcmp(
"'\"x,v\",\"x\"'", test2));
370 ast_test_validate(
test, 0 == strcmp(
"\" i\\'m a test\"", test2));
373 ast_test_validate(
test, 0 == strcmp(
"\" i'm a, test\"", test2));
376 ast_test_validate(
test, 0 == strcmp(
"i'm a, test", test2));
379 ast_test_validate(
test, 0 == strcmp(
"e,nd", test2));
382 ast_test_validate(
test, 0 == strcmp(
"end", test2));
390 char *
test1, *test2, *test3;
394 info->name =
"strsep_quoted";
395 info->category =
"/main/strings/";
396 info->summary =
"Test ast_strsep_quoted";
397 info->description =
"Test ast_strsep_quoted";
403 test1 =
ast_strdupa(
"ghi=jkl,mno=\"pqr,stu\",abc=def, vwx = yz1 , vwx = yz1 , "
404 "\" vwx = yz1 \" , \" vwx , yz1 \",v'w'x, \"'x,v','x'\" , \" i\\'m a test\""
405 ", \" i\\'m a, test\", \" i\\'m a, test\", e\\,nd, end\\");
408 ast_test_validate(
test, 0 == strcmp(
"ghi=jkl", test2));
411 ast_test_validate(
test, 0 == strcmp(
"ghi", test3));
414 ast_test_validate(
test, 0 == strcmp(
"jkl", test3));
417 ast_test_validate(
test, 0 == strcmp(
"mno=\"pqr,stu\"", test2));
420 ast_test_validate(
test, 0 == strcmp(
"mno", test3));
423 ast_test_validate(
test, 0 == strcmp(
"\"pqr,stu\"", test3));
426 ast_test_validate(
test, 0 == strcmp(
"abc=def", test2));
429 ast_test_validate(
test, 0 == strcmp(
" vwx = yz1 ", test2));
432 ast_test_validate(
test, 0 == strcmp(
"vwx = yz1", test2));
435 ast_test_validate(
test, 0 == strcmp(
" vwx = yz1 ", test2));
438 ast_test_validate(
test, 0 == strcmp(
"vwx , yz1", test2));
441 ast_test_validate(
test, 0 == strcmp(
"v'w'x", test2));
444 ast_test_validate(
test, 0 == strcmp(
"\"'x,v','x'\"", test2));
447 ast_test_validate(
test, 0 == strcmp(
"\" i\\'m a test\"", test2));
450 ast_test_validate(
test, 0 == strcmp(
"\" i'm a, test\"", test2));
453 ast_test_validate(
test, 0 == strcmp(
"i'm a, test", test2));
456 ast_test_validate(
test, 0 == strcmp(
"e,nd", test2));
459 ast_test_validate(
test, 0 == strcmp(
"end", test2));
462 test1 =
ast_strdupa(
"ghi=jkl,mno=|pqr,stu|,abc=def, vwx = yz1 , vwx = yz1 , "
463 "| vwx = yz1 | , | vwx , yz1 |,v'w'x, |'x,v','x'| , | i\\'m a test|"
464 ", | i\\'m a, test|, | i\\'m a, test|, e\\,nd, end\\");
467 ast_test_validate(
test, 0 == strcmp(
"ghi=jkl", test2));
470 ast_test_validate(
test, 0 == strcmp(
"ghi", test3));
473 ast_test_validate(
test, 0 == strcmp(
"jkl", test3));
476 ast_test_validate(
test, 0 == strcmp(
"mno=|pqr,stu|", test2));
479 ast_test_validate(
test, 0 == strcmp(
"mno", test3));
482 ast_test_validate(
test, 0 == strcmp(
"|pqr,stu|", test3));
485 ast_test_validate(
test, 0 == strcmp(
"abc=def", test2));
488 ast_test_validate(
test, 0 == strcmp(
" vwx = yz1 ", test2));
491 ast_test_validate(
test, 0 == strcmp(
"vwx = yz1", test2));
494 ast_test_validate(
test, 0 == strcmp(
" vwx = yz1 ", test2));
497 ast_test_validate(
test, 0 == strcmp(
"vwx , yz1", test2));
500 ast_test_validate(
test, 0 == strcmp(
"v'w'x", test2));
503 ast_test_validate(
test, 0 == strcmp(
"|'x,v','x'|", test2));
506 ast_test_validate(
test, 0 == strcmp(
"| i\\'m a test|", test2));
509 ast_test_validate(
test, 0 == strcmp(
"| i'm a, test|", test2));
512 ast_test_validate(
test, 0 == strcmp(
"i'm a, test", test2));
515 ast_test_validate(
test, 0 == strcmp(
"e,nd", test2));
518 ast_test_validate(
test, 0 == strcmp(
"end", test2));
525static int test_semi(
char *string1,
char *string2,
int test_len)
532 }
else if (test_len == 0) {
536 if (test2 !=
NULL && strcmp(string2, test2) == 0) {
547 info->name =
"escape_semicolons";
548 info->category =
"/main/strings/";
549 info->summary =
"Test ast_escape_semicolons";
550 info->description =
"Test ast_escape_semicolons";
557 ast_test_validate(
test,
test_semi(
"this is a ;test",
"this is a \\;test", 18));
584 ast_test_validate(
test,
test_semi(
"xx;xx;this is a test",
"xx\\;xx\\;this is a test", 32));
585 ast_test_validate(
test,
test_semi(
";;;;;",
"\\;\\;\\;\\;\\;", 32));
586 ast_test_validate(
test,
test_semi(
";;;;;",
"\\;\\;\\;\\;", 10));
587 ast_test_validate(
test,
test_semi(
";;;;;",
"\\;\\;\\;\\;\\;", 11));
588 ast_test_validate(
test,
test_semi(
";;\\;;;",
"\\;\\;\\\\;\\;\\;", 32));
597#define TEST_ESCAPE(s, to_escape, expected) \
598 !strcmp(ast_escape(buf, s, ARRAY_LEN(buf), to_escape), expected)
600#define TEST_ESCAPE_C(s, expected) \
601 !strcmp(ast_escape_c(buf, s, ARRAY_LEN(buf)), expected)
603#define TEST_ESCAPE_ALLOC(s, to_escape, expected) \
606 char *a_buf = ast_escape_alloc(s, to_escape); \
608 res = !strcmp(a_buf, expected); \
614#define TEST_ESCAPE_C_ALLOC(s, expected) \
617 char *a_buf = ast_escape_c_alloc(s); \
619 res = !strcmp(a_buf, expected); \
627 info->name =
"escape";
628 info->category =
"/main/strings/";
629 info->summary =
"Test ast_escape";
630 info->description =
"Test escaping values in a string";
637 ast_test_validate(
test,
TEST_ESCAPE(
"empty escape",
"",
"empty escape"));
639 ast_test_validate(
test,
TEST_ESCAPE(
"no matching escape",
"Z",
"no matching escape"));
644 ast_test_validate(
test,
TEST_ESCAPE(
"escape \n again \n",
"\n",
"escape \\n again \\n"));
648 "escape \\a\\b\\f\\n\\r\\t\\v\\\\\\\'\\\"\\?"));
665 info->name =
"strings_match";
666 info->category =
"/main/strings/";
667 info->summary =
"Test ast_strings_match";
668 info->description =
"Test ast_strings_match";
737 info->name =
"temp_strings";
738 info->category =
"/main/strings/";
739 info->summary =
"Test ast_str_temp_buffer";
740 info->description =
"Test ast_str_temp_buffer";
746 snprintf(return_buffer, 128,
"%s %s %s %s %s",
754 ast_test_validate(
test,
ast_strings_match(return_buffer,
"=",
"<str1> <str2> <B> ccccccccccccc <ww>"));
764 info->name =
"in_delimited_string";
765 info->category =
"/main/strings/";
766 info->summary =
"Test ast_in_delimited_string";
Asterisk main include file. File version handling, generic pbx functions.
#define ast_alloca(size)
call __builtin_alloca to ensure we get gcc builtin semantics
#define ast_strdupa(s)
duplicate a string in memory from the stack
#define ast_malloc(len)
A wrapper for malloc()
Asterisk module definitions.
#define AST_MODULE_INFO_STANDARD(keystr, desc)
#define ASTERISK_GPL_KEY
The text the key() function should return.
@ AST_MODULE_LOAD_SUCCESS
static void * cleanup(void *unused)
String manipulation functions.
int ast_str_append(struct ast_str **buf, ssize_t max_len, const char *fmt,...)
Append to a thread local dynamic string.
char * ast_str_buffer(const struct ast_str *buf)
Returns the string buffer within the ast_str buf.
static int force_inline attribute_pure ast_ends_with(const char *str, const char *suffix)
Checks whether a string ends with another.
char * ast_strsep_quoted(char **s, const char sep, const char quote, uint32_t flags)
Like ast_strsep() except you can specify a specific quote character.
#define ast_str_tmp(init_len, __expr)
Provides a temporary ast_str and returns a copy of its buffer.
#define ast_str_alloca(init_len)
void ast_str_reset(struct ast_str *buf)
Reset the content of a dynamic string. Useful before a series of ast_str_append.
int ast_strings_match(const char *left, const char *op, const char *right)
Compares 2 strings using realtime-style operators.
int ast_in_delimited_string(const char *needle, const char *haystack, char delim)
Check if there is an exact match for 'needle' between delimiters in 'haystack'.
#define ast_str_create(init_len)
Create a malloc'ed dynamic length string.
int ast_str_set(struct ast_str **buf, ssize_t max_len, const char *fmt,...)
Set a dynamic string using variable arguments.
size_t ast_str_strlen(const struct ast_str *buf)
Returns the current length of the string stored within buf.
static int force_inline attribute_pure ast_begins_with(const char *str, const char *prefix)
Checks whether a string begins with another.
size_t ast_str_size(const struct ast_str *buf)
Returns the current maximum length (without reallocation) of the current buffer.
char * ast_strsep(char **s, const char sep, uint32_t flags)
Act like strsep but ignore separators inside quotes.
Support for dynamic strings.
#define AST_TEST_REGISTER(cb)
#define ast_test_status_update(a, b, c...)
#define AST_TEST_UNREGISTER(cb)
static const char * str_appender(struct ast_str **buf, char *a)
Function that needs a temporary ast_str.
#define TEST_ESCAPE_ALLOC(s, to_escape, expected)
static int test_semi(char *string1, char *string2, int test_len)
AST_TEST_DEFINE(str_test)
#define TEST_ESCAPE_C(s, expected)
#define TEST_ESCAPE(s, to_escape, expected)
static int load_module(void)
static int unload_module(void)
#define TEST_ESCAPE_C_ALLOC(s, expected)
char * ast_escape_semicolons(const char *string, char *outbuf, int buflen)
Escape semicolons found in a string.