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));
388 static int test_semi(
char *string1,
char *string2,
int test_len)
395 }
else if (test_len == 0) {
399 if (test2 !=
NULL && strcmp(string2, test2) == 0) {
410 info->name =
"escape_semicolons";
411 info->category =
"/main/strings/";
412 info->summary =
"Test ast_escape_semicolons";
413 info->description =
"Test ast_escape_semicolons";
420 ast_test_validate(
test,
test_semi(
"this is a ;test",
"this is a \\;test", 18));
447 ast_test_validate(
test,
test_semi(
"xx;xx;this is a test",
"xx\\;xx\\;this is a test", 32));
448 ast_test_validate(
test,
test_semi(
";;;;;",
"\\;\\;\\;\\;\\;", 32));
449 ast_test_validate(
test,
test_semi(
";;;;;",
"\\;\\;\\;\\;", 10));
450 ast_test_validate(
test,
test_semi(
";;;;;",
"\\;\\;\\;\\;\\;", 11));
451 ast_test_validate(
test,
test_semi(
";;\\;;;",
"\\;\\;\\\\;\\;\\;", 32));
460 #define TEST_ESCAPE(s, to_escape, expected) \ 461 !strcmp(ast_escape(buf, s, ARRAY_LEN(buf), to_escape), expected) 463 #define TEST_ESCAPE_C(s, expected) \ 464 !strcmp(ast_escape_c(buf, s, ARRAY_LEN(buf)), expected) 466 #define TEST_ESCAPE_ALLOC(s, to_escape, expected) \ 469 char *a_buf = ast_escape_alloc(s, to_escape); \ 471 res = !strcmp(a_buf, expected); \ 477 #define TEST_ESCAPE_C_ALLOC(s, expected) \ 480 char *a_buf = ast_escape_c_alloc(s); \ 482 res = !strcmp(a_buf, expected); \ 490 info->name =
"escape";
491 info->category =
"/main/strings/";
492 info->summary =
"Test ast_escape";
493 info->description =
"Test escaping values in a string";
500 ast_test_validate(
test,
TEST_ESCAPE(
"empty escape",
"",
"empty escape"));
502 ast_test_validate(
test,
TEST_ESCAPE(
"no matching escape",
"Z",
"no matching escape"));
507 ast_test_validate(
test,
TEST_ESCAPE(
"escape \n again \n",
"\n",
"escape \\n again \\n"));
511 "escape \\a\\b\\f\\n\\r\\t\\v\\\\\\\'\\\"\\?"));
528 info->name =
"strings_match";
529 info->category =
"/main/strings/";
530 info->summary =
"Test ast_strings_match";
531 info->description =
"Test ast_strings_match";
600 info->name =
"temp_strings";
601 info->category =
"/main/strings/";
602 info->summary =
"Test ast_str_temp_buffer";
603 info->description =
"Test ast_str_temp_buffer";
609 snprintf(return_buffer, 128,
"%s %s %s %s %s",
617 ast_test_validate(
test,
ast_strings_match(return_buffer,
"=",
"<str1> <str2> <B> ccccccccccccc <ww>"));
#define AST_MODULE_INFO_STANDARD(keystr, desc)
Asterisk main include file. File version handling, generic pbx functions.
String manipulation functions.
size_t ast_str_size(const struct ast_str *buf)
Returns the current maximum length (without reallocation) of the current buffer.
static int force_inline attribute_pure ast_ends_with(const char *str, const char *suffix)
char * ast_str_buffer(const struct ast_str *buf)
Returns the string buffer within the ast_str buf.
#define AST_TEST_REGISTER(cb)
int ast_str_append(struct ast_str **buf, ssize_t max_len, const char *fmt,...)
Append to a thread local dynamic string.
static int load_module(void)
char * ast_escape_semicolons(const char *string, char *outbuf, int buflen)
Escape semicolons found in a string.
#define ast_str_alloca(init_len)
int ast_str_set(struct ast_str **buf, ssize_t max_len, const char *fmt,...)
Set a dynamic string using variable arguments.
#define ast_str_tmp(init_len, __expr)
#define TEST_ESCAPE(s, to_escape, expected)
#define ast_test_status_update(a, b, c...)
#define ast_strdupa(s)
duplicate a string in memory from the stack
char * ast_strsep(char **s, const char sep, uint32_t flags)
Act like strsep but ignore separators inside quotes.
#define ast_malloc(len)
A wrapper for malloc()
#define ast_alloca(size)
call __builtin_alloca to ensure we get gcc builtin semantics
#define TEST_ESCAPE_ALLOC(s, to_escape, expected)
The descriptor of a dynamic string XXX storage will be optimized later if needed We use the ts field ...
static int unload_module(void)
#define AST_TEST_UNREGISTER(cb)
#define TEST_ESCAPE_C(s, expected)
static void * cleanup(void *unused)
AST_TEST_DEFINE(str_test)
static const char * str_appender(struct ast_str **buf, char *a)
Function that needs a temporary ast_str.
static int test_semi(char *string1, char *string2, int test_len)
void ast_str_reset(struct ast_str *buf)
Reset the content of a dynamic string. Useful before a series of ast_str_append.
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)
#define ASTERISK_GPL_KEY
The text the key() function should return.
Asterisk module definitions.
#define TEST_ESCAPE_C_ALLOC(s, expected)
int ast_strings_match(const char *left, const char *op, const char *right)
Compares 2 strings using realtime-style operators.
#define ast_str_create(init_len)
Create a malloc'ed dynamic length string.