test_strings.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 2010, Digium, Inc.
  5. *
  6. * Mark Michelson <mmichelson@digium.com>
  7. *
  8. * See http://www.asterisk.org for more information about
  9. * the Asterisk project. Please do not directly contact
  10. * any of the maintainers of this project for assistance;
  11. * the project provides a web site, mailing lists and IRC
  12. * channels for your use.
  13. *
  14. * This program is free software, distributed under the terms of
  15. * the GNU General Public License Version 2. See the LICENSE file
  16. * at the top of the source tree.
  17. */
  18. /*!
  19. * \file
  20. * \brief Dynamic string tests
  21. *
  22. * \author Mark Michelson <mmichelson@digium.com>
  23. *
  24. * This module will run some dyanmic string tests.
  25. *
  26. * \ingroup tests
  27. */
  28. /*** MODULEINFO
  29. <depend>TEST_FRAMEWORK</depend>
  30. <support_level>core</support_level>
  31. ***/
  32. #include "asterisk.h"
  33. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  34. #include "asterisk/test.h"
  35. #include "asterisk/utils.h"
  36. #include "asterisk/strings.h"
  37. #include "asterisk/module.h"
  38. AST_TEST_DEFINE(str_test)
  39. {
  40. struct ast_str *stack_str;
  41. struct ast_str *heap_str;
  42. const char short_string1[] = "apple";
  43. const char short_string2[] = "banana";
  44. char short_string_cat[30];
  45. const char long_string1[] = "applebananapeachmangocherrypeargrapeplumlimetangerinepomegranategravel";
  46. const char long_string2[] = "passionuglinectarinepineapplekiwilemonpaintthinner";
  47. char long_string_cat[200];
  48. char string_limit_cat[11];
  49. const int string_limit = 5;
  50. int current_size;
  51. enum ast_test_result_state res = AST_TEST_PASS;
  52. switch (cmd) {
  53. case TEST_INIT:
  54. info->name = "str_test";
  55. info->category = "/main/strings/";
  56. info->summary = "Test dynamic string operations";
  57. info->description = "Test setting and appending stack and heap-allocated strings";
  58. return AST_TEST_NOT_RUN;
  59. case TEST_EXECUTE:
  60. break;
  61. }
  62. snprintf(short_string_cat, sizeof(short_string_cat), "%s%s", short_string1, short_string2);
  63. snprintf(long_string_cat, sizeof(long_string_cat), "%s%s", long_string1, long_string2);
  64. snprintf(string_limit_cat, string_limit, "%s", long_string1);
  65. strncat(string_limit_cat, long_string2, string_limit);
  66. if (!(stack_str = ast_str_alloca(15))) {
  67. ast_test_status_update(test, "Failed to allocate an ast_str on the stack\n");
  68. return AST_TEST_FAIL;
  69. }
  70. if (!(heap_str = ast_str_create(15))) {
  71. ast_test_status_update(test, "Failed to allocate an ast_str on the heap\n");
  72. }
  73. /* Stack string tests:
  74. * Part 1: Basic tests
  75. * a. set a small string
  76. * b. append a small string
  77. * c. clear a string
  78. * Part 2: Advanced tests
  79. * a. Set a string that is larger than our allocation
  80. * b. Append a string that is larger than our allocation
  81. */
  82. /* Part 1a */
  83. if (ast_str_set(&stack_str, 0, "%s", short_string1) < 0) {
  84. ast_test_status_update(test, "Error setting stack string\n");
  85. res = AST_TEST_FAIL;
  86. goto cleanup;
  87. }
  88. if (strcmp(ast_str_buffer(stack_str), short_string1)) {
  89. ast_test_status_update(test, "ast_str_set failed for stack string. Expected '%s' but"
  90. "instead got %s\n", short_string1, ast_str_buffer(stack_str));
  91. res = AST_TEST_FAIL;
  92. goto cleanup;
  93. }
  94. /* Part 1b */
  95. if (ast_str_append(&stack_str, 0, "%s", short_string2) < 0) {
  96. ast_test_status_update(test, "Error appending to stack string\n");
  97. res = AST_TEST_FAIL;
  98. goto cleanup;
  99. }
  100. if (strcmp(ast_str_buffer(stack_str), short_string_cat)) {
  101. ast_test_status_update(test, "ast_str_set failed for stack string. Expected '%s'"
  102. "but instead got %s\n", short_string_cat, ast_str_buffer(stack_str));
  103. res = AST_TEST_FAIL;
  104. goto cleanup;
  105. }
  106. /* Part 1c */
  107. ast_str_reset(stack_str);
  108. if (ast_str_strlen(stack_str) != 0) {
  109. ast_test_status_update(test, "ast_str_reset resulted in non-zero length for stack_str\n");
  110. res = AST_TEST_FAIL;
  111. goto cleanup;
  112. }
  113. /* Part 2a */
  114. if (ast_str_set(&stack_str, -1, "%s", long_string1) < 0) {
  115. ast_test_status_update(test, "Error setting stack string with long input\n");
  116. res = AST_TEST_FAIL;
  117. goto cleanup;
  118. }
  119. if (strncmp(ast_str_buffer(stack_str), long_string1, ast_str_strlen(stack_str))) {
  120. ast_test_status_update(test, "Stack string not set to what is expected.\n");
  121. res = AST_TEST_FAIL;
  122. goto cleanup;
  123. }
  124. /* Part 2b */
  125. if (ast_str_append(&stack_str, -1, "%s", long_string2) < 0) {
  126. ast_test_status_update(test, "Error appending long string to full stack string buffer\n");
  127. res = AST_TEST_FAIL;
  128. goto cleanup;
  129. }
  130. if (strncmp(ast_str_buffer(stack_str), long_string_cat, ast_str_strlen(stack_str))) {
  131. ast_test_status_update(test, "Stack string not set to what is expected.\n");
  132. res = AST_TEST_FAIL;
  133. goto cleanup;
  134. }
  135. /* Heap string tests
  136. *
  137. * All stack string tests from part 1.
  138. * All stack string tests 2a and 2b.
  139. * Tests 2a and 2b from stack string tests, passing 0 as max_len
  140. * instead of -1. This allows for the buffer to grow.
  141. */
  142. /* Part 1a */
  143. if (ast_str_set(&heap_str, 0, "%s", short_string1) < 0) {
  144. ast_test_status_update(test, "Error setting heap string\n");
  145. res = AST_TEST_FAIL;
  146. goto cleanup;
  147. }
  148. if (strcmp(ast_str_buffer(heap_str), short_string1)) {
  149. ast_test_status_update(test, "ast_str_set failed for heap string. Expected '%s' but"
  150. "instead got %s\n", short_string1, ast_str_buffer(heap_str));
  151. res = AST_TEST_FAIL;
  152. goto cleanup;
  153. }
  154. /* Part 1b */
  155. if (ast_str_append(&heap_str, 0, "%s", short_string2) < 0) {
  156. ast_test_status_update(test, "Error appending to heap string\n");
  157. res = AST_TEST_FAIL;
  158. goto cleanup;
  159. }
  160. if (strcmp(ast_str_buffer(heap_str), short_string_cat)) {
  161. ast_test_status_update(test, "ast_str_set failed for stack string. Expected '%s'"
  162. "but instead got %s\n", short_string_cat, ast_str_buffer(stack_str));
  163. res = AST_TEST_FAIL;
  164. goto cleanup;
  165. }
  166. /* Part 1c */
  167. ast_str_reset(heap_str);
  168. if (ast_str_strlen(heap_str) != 0) {
  169. ast_test_status_update(test, "ast_str_reset resulted in non-zero length for stack_str\n");
  170. res = AST_TEST_FAIL;
  171. goto cleanup;
  172. }
  173. /* Part 2a with -1 arg */
  174. current_size = ast_str_size(heap_str);
  175. if (ast_str_set(&heap_str, -1, "%s", long_string1) < 0) {
  176. ast_test_status_update(test, "Error setting heap string with long input\n");
  177. res = AST_TEST_FAIL;
  178. goto cleanup;
  179. }
  180. if (current_size != ast_str_size(heap_str)) {
  181. ast_test_status_update(test, "Heap string changed size during ast_str_set when it was"
  182. "instructed not to. Was %d and now is %d\n", current_size, (int) ast_str_size(heap_str));
  183. res = AST_TEST_FAIL;
  184. goto cleanup;
  185. }
  186. if (strncmp(ast_str_buffer(heap_str), long_string1, ast_str_strlen(heap_str))) {
  187. ast_test_status_update(test, "Heap string not set to what is expected.\n");
  188. res = AST_TEST_FAIL;
  189. goto cleanup;
  190. }
  191. /* Part 2b with -1 arg */
  192. current_size = ast_str_size(heap_str);
  193. if (ast_str_append(&heap_str, -1, "%s", long_string2) < 0) {
  194. ast_test_status_update(test, "Error appending long string to full heap string buffer\n");
  195. res = AST_TEST_FAIL;
  196. goto cleanup;
  197. }
  198. if (current_size != ast_str_size(heap_str)) {
  199. ast_test_status_update(test, "Heap string changed size during ast_str_append when it was"
  200. "instructed not to. Was %d and now is %d\n", current_size, (int) ast_str_size(heap_str));
  201. res = AST_TEST_FAIL;
  202. goto cleanup;
  203. }
  204. if (strncmp(ast_str_buffer(heap_str), long_string_cat, ast_str_strlen(heap_str))) {
  205. ast_test_status_update(test, "Heap string not set to what is expected.\n");
  206. res = AST_TEST_FAIL;
  207. goto cleanup;
  208. }
  209. /* reset string before continuing */
  210. ast_str_reset(heap_str);
  211. /* Part 2a with 0 arg */
  212. if (ast_str_set(&heap_str, 0, "%s", long_string1) < 0) {
  213. ast_test_status_update(test, "Error setting heap string with long input\n");
  214. res = AST_TEST_FAIL;
  215. goto cleanup;
  216. }
  217. if (strcmp(ast_str_buffer(heap_str), long_string1)) {
  218. ast_test_status_update(test, "Heap string does not contain what was expected. Expected %s"
  219. "but have %s instead\n", long_string1, ast_str_buffer(heap_str));
  220. res = AST_TEST_FAIL;
  221. goto cleanup;
  222. }
  223. /* Part 2b with 0 arg */
  224. if (ast_str_append(&heap_str, 0, "%s", long_string2) < 0) {
  225. ast_test_status_update(test, "Error setting heap string with long input\n");
  226. res = AST_TEST_FAIL;
  227. goto cleanup;
  228. }
  229. if (strcmp(ast_str_buffer(heap_str), long_string_cat)) {
  230. ast_test_status_update(test, "Heap string does not contain what was expected. Expected %s"
  231. "but have %s instead\n", long_string_cat, ast_str_buffer(heap_str));
  232. res = AST_TEST_FAIL;
  233. goto cleanup;
  234. }
  235. cleanup:
  236. ast_free(heap_str);
  237. return res;
  238. }
  239. AST_TEST_DEFINE(begins_with_test)
  240. {
  241. switch (cmd) {
  242. case TEST_INIT:
  243. info->name = "begins_with";
  244. info->category = "/main/strings/";
  245. info->summary = "Test ast_begins_with";
  246. info->description = "Test ast_begins_with";
  247. return AST_TEST_NOT_RUN;
  248. case TEST_EXECUTE:
  249. break;
  250. }
  251. // prefixes
  252. ast_test_validate(test, 1 == ast_begins_with("foobar", "foobar"));
  253. ast_test_validate(test, 1 == ast_begins_with("foobar", "foo"));
  254. ast_test_validate(test, 1 == ast_begins_with("foobar", ""));
  255. ast_test_validate(test, 1 == ast_begins_with("", ""));
  256. // not prefixes
  257. ast_test_validate(test, 0 == ast_begins_with("foobar", "bang"));
  258. ast_test_validate(test, 0 == ast_begins_with("foobar", "foobat"));
  259. ast_test_validate(test, 0 == ast_begins_with("boo", "boom"));
  260. ast_test_validate(test, 0 == ast_begins_with("", "blitz"));
  261. // nothing failed; we're all good!
  262. return AST_TEST_PASS;
  263. }
  264. AST_TEST_DEFINE(ends_with_test)
  265. {
  266. switch (cmd) {
  267. case TEST_INIT:
  268. info->name = "ends_with";
  269. info->category = "/main/strings/";
  270. info->summary = "Test ast_ends_with";
  271. info->description = "Test ast_ends_with";
  272. return AST_TEST_NOT_RUN;
  273. case TEST_EXECUTE:
  274. break;
  275. }
  276. // prefixes
  277. ast_test_validate(test, 1 == ast_ends_with("foobar", "foobar"));
  278. ast_test_validate(test, 1 == ast_ends_with("foobar", "bar"));
  279. ast_test_validate(test, 1 == ast_ends_with("foobar", ""));
  280. ast_test_validate(test, 1 == ast_ends_with("", ""));
  281. // not suffixes
  282. ast_test_validate(test, 0 == ast_ends_with("bar", "bbar"));
  283. ast_test_validate(test, 0 == ast_ends_with("foobar", "bang"));
  284. ast_test_validate(test, 0 == ast_ends_with("foobar", "foobat"));
  285. ast_test_validate(test, 0 == ast_ends_with("boo", "boom"));
  286. ast_test_validate(test, 0 == ast_ends_with("", "blitz"));
  287. // nothing failed; we're all good!
  288. return AST_TEST_PASS;
  289. }
  290. AST_TEST_DEFINE(strsep_test)
  291. {
  292. char *test1, *test2, *test3;
  293. switch (cmd) {
  294. case TEST_INIT:
  295. info->name = "strsep";
  296. info->category = "/main/strings/";
  297. info->summary = "Test ast_strsep";
  298. info->description = "Test ast_strsep";
  299. return AST_TEST_NOT_RUN;
  300. case TEST_EXECUTE:
  301. break;
  302. }
  303. test1 = ast_strdupa("ghi=jkl,mno='pqr,stu',abc=def, vwx = yz1 , vwx = yz1 , '"
  304. " vwx = yz1 ' , ' vwx , yz1 ',v\"w\"x, '\"x,v\",\"x\"' , \" i\\'m a test\""
  305. ", \" i\\'m a, test\", \" i\\'m a, test\", e\\,nd, end\\");
  306. test2 = ast_strsep(&test1, ',', 0);
  307. ast_test_validate(test, 0 == strcmp("ghi=jkl", test2));
  308. test3 = ast_strsep(&test2, '=', 0);
  309. ast_test_validate(test, 0 == strcmp("ghi", test3));
  310. test3 = ast_strsep(&test2, '=', 0);
  311. ast_test_validate(test, 0 == strcmp("jkl", test3));
  312. test2 = ast_strsep(&test1, ',', 0);
  313. ast_test_validate(test, 0 == strcmp("mno='pqr,stu'", test2));
  314. test3 = ast_strsep(&test2, '=', 0);
  315. ast_test_validate(test, 0 == strcmp("mno", test3));
  316. test3 = ast_strsep(&test2, '=', 0);
  317. ast_test_validate(test, 0 == strcmp("'pqr,stu'", test3));
  318. test2 = ast_strsep(&test1, ',', 0);
  319. ast_test_validate(test, 0 == strcmp("abc=def", test2));
  320. test2 = ast_strsep(&test1, ',', 0);
  321. ast_test_validate(test, 0 == strcmp(" vwx = yz1 ", test2));
  322. test2 = ast_strsep(&test1, ',', AST_STRSEP_TRIM);
  323. ast_test_validate(test, 0 == strcmp("vwx = yz1", test2));
  324. test2 = ast_strsep(&test1, ',', AST_STRSEP_STRIP);
  325. ast_test_validate(test, 0 == strcmp(" vwx = yz1 ", test2));
  326. test2 = ast_strsep(&test1, ',', AST_STRSEP_STRIP | AST_STRSEP_TRIM);
  327. ast_test_validate(test, 0 == strcmp("vwx , yz1", test2));
  328. test2 = ast_strsep(&test1, ',', AST_STRSEP_STRIP | AST_STRSEP_TRIM);
  329. ast_test_validate(test, 0 == strcmp("v\"w\"x", test2));
  330. test2 = ast_strsep(&test1, ',', AST_STRSEP_TRIM);
  331. ast_test_validate(test, 0 == strcmp("'\"x,v\",\"x\"'", test2));
  332. test2 = ast_strsep(&test1, ',', AST_STRSEP_TRIM);
  333. ast_test_validate(test, 0 == strcmp("\" i\\'m a test\"", test2));
  334. test2 = ast_strsep(&test1, ',', AST_STRSEP_TRIM | AST_STRSEP_UNESCAPE);
  335. ast_test_validate(test, 0 == strcmp("\" i'm a, test\"", test2));
  336. test2 = ast_strsep(&test1, ',', AST_STRSEP_ALL);
  337. ast_test_validate(test, 0 == strcmp("i'm a, test", test2));
  338. test2 = ast_strsep(&test1, ',', AST_STRSEP_TRIM | AST_STRSEP_UNESCAPE);
  339. ast_test_validate(test, 0 == strcmp("e,nd", test2));
  340. test2 = ast_strsep(&test1, ',', AST_STRSEP_TRIM | AST_STRSEP_UNESCAPE);
  341. ast_test_validate(test, 0 == strcmp("end", test2));
  342. // nothing failed; we're all good!
  343. return AST_TEST_PASS;
  344. }
  345. static int test_semi(char *string1, char *string2, int test_len)
  346. {
  347. char *test2 = NULL;
  348. if (test_len > 0) {
  349. test2 = ast_alloca(test_len);
  350. *test2 = '\0';
  351. } else if (test_len == 0) {
  352. test2 = "";
  353. }
  354. ast_escape_semicolons(string1, test2, test_len);
  355. if (test2 != NULL && strcmp(string2, test2) == 0) {
  356. return 1;
  357. } else {
  358. return 0;
  359. }
  360. }
  361. AST_TEST_DEFINE(escape_semicolons_test)
  362. {
  363. switch (cmd) {
  364. case TEST_INIT:
  365. info->name = "escape_semicolons";
  366. info->category = "/main/strings/";
  367. info->summary = "Test ast_escape_semicolons";
  368. info->description = "Test ast_escape_semicolons";
  369. return AST_TEST_NOT_RUN;
  370. case TEST_EXECUTE:
  371. break;
  372. }
  373. ast_test_validate(test, test_semi("this is a ;test", "this is a \\;test", 18));
  374. ast_test_validate(test, test_semi(";", "\\;", 3));
  375. /* The following tests should return empty because there's not enough room to output
  376. * an escaped ; or even a single character.
  377. */
  378. ast_test_validate(test, test_semi(";", "", 0));
  379. ast_test_validate(test, test_semi(";", "", 1));
  380. ast_test_validate(test, test_semi(";", "", 2));
  381. ast_test_validate(test, test_semi("x", "", 0));
  382. ast_test_validate(test, test_semi("x", "", 1));
  383. /* At least some output should be produced now. */
  384. ast_test_validate(test, test_semi("xx;xx", "x", 2));
  385. ast_test_validate(test, test_semi("xx;xx", "xx", 3));
  386. /* There's still not enough room to output \; so
  387. * don't even print the \
  388. */
  389. ast_test_validate(test, test_semi("xx;xx", "xx", 4));
  390. ast_test_validate(test, test_semi("xx;xx", "xx\\;", 5));
  391. ast_test_validate(test, test_semi("xx;xx", "xx\\;x", 6));
  392. ast_test_validate(test, test_semi("xx;xx", "xx\\;xx", 7));
  393. ast_test_validate(test, test_semi("xx;xx", "xx\\;xx", 8));
  394. /* Random stuff */
  395. ast_test_validate(test, test_semi("xx;xx;this is a test", "xx\\;xx\\;this is a test", 32));
  396. ast_test_validate(test, test_semi(";;;;;", "\\;\\;\\;\\;\\;", 32));
  397. ast_test_validate(test, test_semi(";;;;;", "\\;\\;\\;\\;", 10));
  398. ast_test_validate(test, test_semi(";;;;;", "\\;\\;\\;\\;\\;", 11));
  399. ast_test_validate(test, test_semi(";;\\;;;", "\\;\\;\\\\;\\;\\;", 32));
  400. return AST_TEST_PASS;
  401. }
  402. AST_TEST_DEFINE(escape_test)
  403. {
  404. char buf[128];
  405. #define TEST_ESCAPE(s, to_escape, expected) \
  406. !strcmp(ast_escape(buf, s, ARRAY_LEN(buf), to_escape), expected)
  407. #define TEST_ESCAPE_C(s, expected) \
  408. !strcmp(ast_escape_c(buf, s, ARRAY_LEN(buf)), expected)
  409. #define TEST_ESCAPE_ALLOC(s, to_escape, expected) \
  410. ({ \
  411. int res = 0; \
  412. char *a_buf = ast_escape_alloc(s, to_escape); \
  413. if (a_buf) { \
  414. res = !strcmp(a_buf, expected); \
  415. ast_free(a_buf); \
  416. } \
  417. res; \
  418. })
  419. #define TEST_ESCAPE_C_ALLOC(s, expected) \
  420. ({ \
  421. int res = 0; \
  422. char *a_buf = ast_escape_c_alloc(s); \
  423. if (a_buf) { \
  424. res = !strcmp(a_buf, expected); \
  425. ast_free(a_buf); \
  426. } \
  427. res; \
  428. })
  429. switch (cmd) {
  430. case TEST_INIT:
  431. info->name = "escape";
  432. info->category = "/main/strings/";
  433. info->summary = "Test ast_escape";
  434. info->description = "Test escaping values in a string";
  435. return AST_TEST_NOT_RUN;
  436. case TEST_EXECUTE:
  437. break;
  438. }
  439. ast_test_validate(test, TEST_ESCAPE("null escape", NULL, "null escape"));
  440. ast_test_validate(test, TEST_ESCAPE("empty escape", "", "empty escape"));
  441. ast_test_validate(test, TEST_ESCAPE("", "Z", ""));
  442. ast_test_validate(test, TEST_ESCAPE("no matching escape", "Z", "no matching escape"));
  443. ast_test_validate(test, TEST_ESCAPE("escape Z", "Z", "escape \\Z"));
  444. ast_test_validate(test, TEST_ESCAPE("Z", "Z", "\\Z"));
  445. ast_test_validate(test, TEST_ESCAPE(";;", ";", "\\;\\;"));
  446. ast_test_validate(test, TEST_ESCAPE("escape \n", "\n", "escape \\n"));
  447. ast_test_validate(test, TEST_ESCAPE("escape \n again \n", "\n", "escape \\n again \\n"));
  448. ast_test_validate(test, TEST_ESCAPE_C("", ""));
  449. ast_test_validate(test, TEST_ESCAPE_C("escape \a\b\f\n\r\t\v\\\'\"\?",
  450. "escape \\a\\b\\f\\n\\r\\t\\v\\\\\\\'\\\"\\?"));
  451. ast_test_validate(test, TEST_ESCAPE_ALLOC("", "Z", ""));
  452. ast_test_validate(test, TEST_ESCAPE_ALLOC("Z", "Z", "\\Z"));
  453. ast_test_validate(test, TEST_ESCAPE_ALLOC("a", "Z", "a"));
  454. ast_test_validate(test, TEST_ESCAPE_C_ALLOC("", ""));
  455. ast_test_validate(test, TEST_ESCAPE_C_ALLOC("\n", "\\n"));
  456. ast_test_validate(test, TEST_ESCAPE_C_ALLOC("a", "a"));
  457. return AST_TEST_PASS;
  458. }
  459. AST_TEST_DEFINE(strings_match)
  460. {
  461. switch (cmd) {
  462. case TEST_INIT:
  463. info->name = "strings_match";
  464. info->category = "/main/strings/";
  465. info->summary = "Test ast_strings_match";
  466. info->description = "Test ast_strings_match";
  467. return AST_TEST_NOT_RUN;
  468. case TEST_EXECUTE:
  469. break;
  470. }
  471. ast_test_validate(test, ast_strings_match("aaa", NULL, "aaa"));
  472. ast_test_validate(test, ast_strings_match("aaa", "", "aaa"));
  473. ast_test_validate(test, ast_strings_match("aaa", "=", "aaa"));
  474. ast_test_validate(test, !ast_strings_match("aaa", "!=", "aaa"));
  475. ast_test_validate(test, !ast_strings_match("aaa", NULL, "aba"));
  476. ast_test_validate(test, !ast_strings_match("aaa", "", "aba"));
  477. ast_test_validate(test, !ast_strings_match("aaa", "=", "aba"));
  478. ast_test_validate(test, ast_strings_match("aaa", "!=", "aba"));
  479. ast_test_validate(test, ast_strings_match("aaa", "<=", "aba"));
  480. ast_test_validate(test, ast_strings_match("aaa", "<=", "aaa"));
  481. ast_test_validate(test, !ast_strings_match("aaa", "<", "aaa"));
  482. ast_test_validate(test, !ast_strings_match("aaa", ">=", "aba"));
  483. ast_test_validate(test, ast_strings_match("aaa", ">=", "aaa"));
  484. ast_test_validate(test, !ast_strings_match("aaa", ">", "aaa"));
  485. ast_test_validate(test, !ast_strings_match("aaa", "=", "aa"));
  486. ast_test_validate(test, ast_strings_match("aaa", ">", "aa"));
  487. ast_test_validate(test, !ast_strings_match("aaa", "<", "aa"));
  488. ast_test_validate(test, ast_strings_match("1", "=", "1"));
  489. ast_test_validate(test, !ast_strings_match("1", "!=", "1"));
  490. ast_test_validate(test, !ast_strings_match("2", "=", "1"));
  491. ast_test_validate(test, ast_strings_match("2", ">", "1"));
  492. ast_test_validate(test, ast_strings_match("2", ">=", "1"));
  493. ast_test_validate(test, ast_strings_match("2", ">", "1.9888"));
  494. ast_test_validate(test, ast_strings_match("2.9", ">", "1"));
  495. ast_test_validate(test, ast_strings_match("2", ">", "1"));
  496. ast_test_validate(test, ast_strings_match("2.999", "<", "3"));
  497. ast_test_validate(test, ast_strings_match("2", ">", "#"));
  498. ast_test_validate(test, ast_strings_match("abcccc", "like", "%a%c"));
  499. ast_test_validate(test, !ast_strings_match("abcccx", "like", "%a%c"));
  500. ast_test_validate(test, ast_strings_match("abcccc", "regex", "a[bc]+c"));
  501. ast_test_validate(test, !ast_strings_match("abcccx", "regex", "^a[bxdfgtc]+c$"));
  502. ast_test_validate(test, !ast_strings_match("neener-93joe", "LIKE", "%blah-%"));
  503. ast_test_validate(test, ast_strings_match("blah-93joe", "LIKE", "%blah-%"));
  504. ast_test_validate(test, !ast_strings_match("abcccx", "regex", NULL));
  505. ast_test_validate(test, !ast_strings_match("abcccx", NULL, NULL));
  506. ast_test_validate(test, !ast_strings_match(NULL, "regex", NULL));
  507. ast_test_validate(test, !ast_strings_match(NULL, NULL, "abc"));
  508. ast_test_validate(test, !ast_strings_match(NULL, NULL, NULL));
  509. return AST_TEST_PASS;
  510. }
  511. static int unload_module(void)
  512. {
  513. AST_TEST_UNREGISTER(str_test);
  514. AST_TEST_UNREGISTER(begins_with_test);
  515. AST_TEST_UNREGISTER(ends_with_test);
  516. AST_TEST_UNREGISTER(strsep_test);
  517. AST_TEST_UNREGISTER(escape_semicolons_test);
  518. AST_TEST_UNREGISTER(escape_test);
  519. AST_TEST_UNREGISTER(strings_match);
  520. return 0;
  521. }
  522. static int load_module(void)
  523. {
  524. AST_TEST_REGISTER(str_test);
  525. AST_TEST_REGISTER(begins_with_test);
  526. AST_TEST_REGISTER(ends_with_test);
  527. AST_TEST_REGISTER(strsep_test);
  528. AST_TEST_REGISTER(escape_semicolons_test);
  529. AST_TEST_REGISTER(escape_test);
  530. AST_TEST_REGISTER(strings_match);
  531. return AST_MODULE_LOAD_SUCCESS;
  532. }
  533. AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Dynamic string test module");