test_func_file.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 2010, Digium, Inc.
  5. *
  6. * Tilghman Lesher <tlesher AT digium DOT 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 Function FILE tests
  21. *
  22. * \author\verbatim Tilghman Lesher <tlesher AT digium DOT com> \endverbatim
  23. *
  24. * \ingroup tests
  25. */
  26. /*** MODULEINFO
  27. <depend>TEST_FRAMEWORK</depend>
  28. <support_level>extended</support_level>
  29. ***/
  30. #include "asterisk.h"
  31. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  32. #include "asterisk/utils.h"
  33. #include "asterisk/app.h"
  34. #include "asterisk/module.h"
  35. #include "asterisk/test.h"
  36. #include "asterisk/pbx.h"
  37. #define C1024 \
  38. "1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF"
  39. static struct {
  40. const char *contents;
  41. const char *args;
  42. const char *value;
  43. } read_tests[] = {
  44. /* 4 different ways of specifying the first character */
  45. { "123456789", "0,1", "1" },
  46. { "123456789", "0,-8", "1" },
  47. { "123456789", "-9,1", "1" },
  48. { "123456789", "-9,-8", "1" },
  49. /* Does 0-length work? */
  50. { "123456789", "0,0", "" },
  51. { "123456789", "-9,0", "" },
  52. { "123456789", "-9,-9", "" },
  53. /* Does negative length work? */
  54. { "123456789", "5,-6", "" },
  55. { "123456789", "-5,-6", "" },
  56. /* No length */
  57. { "123456789", "-5", "56789" },
  58. { "123456789", "4", "56789" },
  59. /* Line mode, 4 ways of specifying the first character */
  60. { "123\n456\n789\n", "0,1,l", "123\n" },
  61. { "123\n456\n789\n", "-3,1,l", "123\n" },
  62. { "123\n456\n789\n", "0,-2,l", "123\n" },
  63. { "123\n456\n789\n", "-3,-2,l", "123\n" },
  64. /* Line mode, 0-length */
  65. { "123\n456\n789\n", "0,0,l", "" },
  66. { "123\n456\n789\n", "-3,0,l", "" },
  67. { "123\n456\n789\n", "-3,-3,l", "" },
  68. /* Line mode, negative length */
  69. { "123\n456\n789\n", "2,-2,l", "" },
  70. { "123\n456\n789\n", "-2,-3,l", "" },
  71. /* No length */
  72. { "123\n456\n789\n", "1,,l", "456\n789\n" },
  73. { "123\n456\n789\n", "-2,,l", "456\n789\n" },
  74. };
  75. static struct {
  76. const char *contents;
  77. const char *args;
  78. const char *value;
  79. const char *contents2;
  80. } write_tests[] = {
  81. /* Single character replace */
  82. { "123456789", "0,1", "a", "a23456789" },
  83. { "123456789", "-9,1", "a", "a23456789" },
  84. { "123456789", "0,-8", "a", "a23456789" },
  85. { "123456789", "-9,-8", "a", "a23456789" },
  86. { "123456789", "5,1", "b", "12345b789" },
  87. { "123456789", "-4,1", "b", "12345b789" },
  88. { "123456789", "5,-3", "b", "12345b789" },
  89. { "123456789", "-4,-3", "b", "12345b789" },
  90. /* Replace 2 characters with 1 */
  91. { "123456789", "0,2", "c", "c3456789" },
  92. { "123456789", "-9,2", "c", "c3456789" },
  93. { "123456789", "0,-7", "c", "c3456789" },
  94. { "123456789", "-9,-7", "c", "c3456789" },
  95. { "123456789", "4,2", "d", "1234d789" },
  96. { "123456789", "-5,2", "d", "1234d789" },
  97. { "123456789", "4,-3", "d", "1234d789" },
  98. { "123456789", "-5,-3", "d", "1234d789" },
  99. /* Truncate file */
  100. { "123456789", "5", "e", "12345e" },
  101. { "123456789", "5", "", "12345" },
  102. { "123456789", "-4", "e", "12345e" },
  103. { "123456789", "-4", "", "12345" },
  104. /* Replace 1 character with 2 */
  105. { "123456789", "0,1", "fg", "fg23456789" },
  106. { "123456789", "0,-8", "fg", "fg23456789" },
  107. { "123456789", "-9,1", "fg", "fg23456789" },
  108. { "123456789", "-9,-8", "fg", "fg23456789" },
  109. /* Overwrite file */
  110. { "123456789", "", "h", "h" },
  111. { "123456789", ",,,", "h", "h" },
  112. { "123\n456\n789\n", ",,l", "h", "h\n" },
  113. { "123\n456\n789\n", ",,ld", "h", "h" },
  114. /* Single line replace, same length */
  115. { "123\n456\n789\n", "0,1,l", "abc", "abc\n456\n789\n" },
  116. { "123\n456\n789\n", "-3,1,l", "abc", "abc\n456\n789\n" },
  117. { "123\n456\n789\n", "0,-2,l", "abc", "abc\n456\n789\n" },
  118. { "123\n456\n789\n", "-3,-2,l", "abc", "abc\n456\n789\n" },
  119. { "123\n456\n789\n", "1,1,l", "abc", "123\nabc\n789\n" },
  120. { "123\n456\n789\n", "1,-1,l", "abc", "123\nabc\n789\n" },
  121. { "123\n456\n789\n", "-2,1,l", "abc", "123\nabc\n789\n" },
  122. { "123\n456\n789\n", "-2,-1,l", "abc", "123\nabc\n789\n" },
  123. /* Single line replace, one character short */
  124. { "123\n456\n789\n", "0,1,l", "ab", "ab\n456\n789\n" },
  125. { "123\n456\n789\n", "-3,1,l", "ab", "ab\n456\n789\n" },
  126. { "123\n456\n789\n", "0,-2,l", "ab", "ab\n456\n789\n" },
  127. { "123\n456\n789\n", "-3,-2,l", "ab", "ab\n456\n789\n" },
  128. { "123\n456\n789\n", "1,1,l", "ab", "123\nab\n789\n" },
  129. { "123\n456\n789\n", "1,-1,l", "ab", "123\nab\n789\n" },
  130. { "123\n456\n789\n", "-2,1,l", "ab", "123\nab\n789\n" },
  131. { "123\n456\n789\n", "-2,-1,l", "ab", "123\nab\n789\n" },
  132. /* Single line replace, one character long */
  133. { "123\n456\n789\n", "0,1,l", "abcd", "abcd\n456\n789\n" },
  134. { "123\n456\n789\n", "-3,1,l", "abcd", "abcd\n456\n789\n" },
  135. { "123\n456\n789\n", "0,-2,l", "abcd", "abcd\n456\n789\n" },
  136. { "123\n456\n789\n", "-3,-2,l", "abcd", "abcd\n456\n789\n" },
  137. { "123\n456\n789\n", "1,1,l", "abcd", "123\nabcd\n789\n" },
  138. { "123\n456\n789\n", "1,-1,l", "abcd", "123\nabcd\n789\n" },
  139. { "123\n456\n789\n", "-2,1,l", "abcd", "123\nabcd\n789\n" },
  140. { "123\n456\n789\n", "-2,-1,l", "abcd", "123\nabcd\n789\n" },
  141. /* Multi-line replace, same number of characters, 2 lines for 1 */
  142. { "123\n456\n789\n", "0,2,l", "abcdefg", "abcdefg\n789\n" },
  143. { "123\n456\n789\n", "-3,2,l", "abcdefg", "abcdefg\n789\n" },
  144. { "123\n456\n789\n", "0,-1,l", "abcdefg", "abcdefg\n789\n" },
  145. { "123\n456\n789\n", "-3,-1,l", "abcdefg", "abcdefg\n789\n" },
  146. { "123\n456\n789\n", "1,2,l", "abcdefg", "123\nabcdefg\n" },
  147. { "123\n456\n789\n", "1,,l", "abcdefg", "123\nabcdefg\n" },
  148. { "123\n456\n789\n", "-2,2,l", "abcdefg", "123\nabcdefg\n" },
  149. { "123\n456\n789\n", "-2,,l", "abcdefg", "123\nabcdefg\n" },
  150. /* Multi-line replace, shorter number of characters, 2 lines for 1 */
  151. { "123\n456\n789\n", "0,2,l", "abcd", "abcd\n789\n" },
  152. { "123\n456\n789\n", "-3,2,l", "abcd", "abcd\n789\n" },
  153. { "123\n456\n789\n", "0,-1,l", "abcd", "abcd\n789\n" },
  154. { "123\n456\n789\n", "-3,-1,l", "abcd", "abcd\n789\n" },
  155. { "123\n456\n789\n", "1,2,l", "abcd", "123\nabcd\n" },
  156. { "123\n456\n789\n", "1,,l", "abcd", "123\nabcd\n" },
  157. { "123\n456\n789\n", "-2,2,l", "abcd", "123\nabcd\n" },
  158. { "123\n456\n789\n", "-2,,l", "abcd", "123\nabcd\n" },
  159. /* Multi-line replace, longer number of characters, 2 lines for 1 */
  160. { "123\n456\n789\n", "0,2,l", "abcdefghijklmnop", "abcdefghijklmnop\n789\n" },
  161. { "123\n456\n789\n", "-3,2,l", "abcdefghijklmnop", "abcdefghijklmnop\n789\n" },
  162. { "123\n456\n789\n", "0,-1,l", "abcdefghijklmnop", "abcdefghijklmnop\n789\n" },
  163. { "123\n456\n789\n", "-3,-1,l", "abcdefghijklmnop", "abcdefghijklmnop\n789\n" },
  164. { "123\n456\n789\n", "1,2,l", "abcdefghijklmnop", "123\nabcdefghijklmnop\n" },
  165. { "123\n456\n789\n", "1,,l", "abcdefghijklmnop", "123\nabcdefghijklmnop\n" },
  166. { "123\n456\n789\n", "-2,2,l", "abcdefghijklmnop", "123\nabcdefghijklmnop\n" },
  167. { "123\n456\n789\n", "-2,,l", "abcdefghijklmnop", "123\nabcdefghijklmnop\n" },
  168. /* Insert line */
  169. { "123\n456\n789\n", "0,0,l", "abcd", "abcd\n123\n456\n789\n" },
  170. { "123\n456\n789\n", "-3,0,l", "abcd", "abcd\n123\n456\n789\n" },
  171. { "123\n456\n789\n", "1,0,l", "abcd", "123\nabcd\n456\n789\n" },
  172. { "123\n456\n789\n", "-2,0,l", "abcd", "123\nabcd\n456\n789\n" },
  173. { "123\n456\n789\n", "2,0,l", "abcd", "123\n456\nabcd\n789\n" },
  174. { "123\n456\n789\n", "-1,0,l", "abcd", "123\n456\nabcd\n789\n" },
  175. { "123\n456\n789\n", "3,0,l", "abcd", "123\n456\n789\nabcd\n" },
  176. { "123\n456\n789\n", ",,la", "abcd", "123\n456\n789\nabcd\n" },
  177. /* Single line, replace with blank line */
  178. { "123\n456\n789\n", "0,1,l", "", "\n456\n789\n" },
  179. { "123\n456\n789\n", "-3,1,l", "", "\n456\n789\n" },
  180. { "123\n456\n789\n", "0,-2,l", "", "\n456\n789\n" },
  181. { "123\n456\n789\n", "-3,-2,l", "", "\n456\n789\n" },
  182. { "123\n456\n789\n", "1,1,l", "", "123\n\n789\n" },
  183. { "123\n456\n789\n", "1,-1,l", "", "123\n\n789\n" },
  184. { "123\n456\n789\n", "-2,1,l", "", "123\n\n789\n" },
  185. { "123\n456\n789\n", "-2,-1,l", "", "123\n\n789\n" },
  186. /* Single line, delete */
  187. { "123\n456\n789\n", "0,1,ld", "", "456\n789\n" },
  188. { "123\n456\n789\n", "-3,1,ld", "", "456\n789\n" },
  189. { "123\n456\n789\n", "0,-2,ld", "", "456\n789\n" },
  190. { "123\n456\n789\n", "-3,-2,ld", "", "456\n789\n" },
  191. { "123\n456\n789\n", "1,1,ld", "", "123\n789\n" },
  192. { "123\n456\n789\n", "1,-1,ld", "", "123\n789\n" },
  193. { "123\n456\n789\n", "-2,1,ld", "", "123\n789\n" },
  194. { "123\n456\n789\n", "-2,-1,ld", "", "123\n789\n" },
  195. /* Really long tests */
  196. { "1234567890ABCDEF" C1024 C1024 C1024 C1024 C1024,
  197. "0,1", "a",
  198. "a234567890ABCDEF" C1024 C1024 C1024 C1024 C1024 },
  199. { "1234567890ABCDEF" C1024 C1024 C1024 C1024 C1024,
  200. "0,1", "abcd",
  201. "abcd234567890ABCDEF" C1024 C1024 C1024 C1024 C1024 },
  202. { "1234567890ABCDEF" C1024 C1024 C1024 C1024 C1024,
  203. "0,10", "abcd",
  204. "abcdABCDEF" C1024 C1024 C1024 C1024 C1024 },
  205. { "1" C1024 "\n2" C1024 "\n3" C1024 "\n4" C1024 "\n5" C1024 "\n6" C1024 "\n",
  206. "0,1,l", "abcd",
  207. "abcd\n2" C1024 "\n3" C1024 "\n4" C1024 "\n5" C1024 "\n6" C1024 "\n" },
  208. { "1234\n1" C1024 "\n2" C1024 "\n3" C1024 "\n4" C1024 "\n5" C1024 "\n6" C1024 "\n",
  209. "0,1,l", "abcd",
  210. "abcd\n1" C1024 "\n2" C1024 "\n3" C1024 "\n4" C1024 "\n5" C1024 "\n6" C1024 "\n" },
  211. { "1234\n1" C1024 "\n2" C1024 "\n3" C1024 "\n4" C1024 "\n5" C1024 "\n6" C1024 "\n",
  212. "0,1,l", "a",
  213. "a\n1" C1024 "\n2" C1024 "\n3" C1024 "\n4" C1024 "\n5" C1024 "\n6" C1024 "\n" },
  214. };
  215. static char *file2display(struct ast_str **buf, ssize_t len, const char *input)
  216. {
  217. const char *ptr;
  218. ast_str_reset(*buf);
  219. for (ptr = input; *ptr; ptr++) {
  220. if (*ptr == '\n') {
  221. ast_str_append(buf, len, "\\n");
  222. } else if (*ptr == '\r') {
  223. ast_str_append(buf, len, "\\r");
  224. } else if (*ptr == '\t') {
  225. ast_str_append(buf, len, "\\t");
  226. } else if (*ptr < ' ' || *ptr > 125) {
  227. ast_str_append(buf, len, "\\x%hhX", *ptr);
  228. } else {
  229. ast_str_append(buf, len, "%c", *ptr);
  230. }
  231. }
  232. return ast_str_buffer(*buf);
  233. }
  234. AST_TEST_DEFINE(test_func_file)
  235. {
  236. int res = AST_TEST_PASS;
  237. int i;
  238. char dir[] = "/tmp/test_func_file.XXXXXX";
  239. char file[80], expression[256];
  240. struct ast_str *buf, *disp[2] = { NULL, NULL };
  241. char fbuf[8192];
  242. FILE *fh;
  243. switch (cmd) {
  244. case TEST_INIT:
  245. info->name = "func_file";
  246. info->category = "/funcs/func_env/";
  247. info->summary = "Verify behavior of the FILE() dialplan function";
  248. info->description =
  249. "Verifies that the examples of the FILE() dialplan function documentation work as described.";
  250. return AST_TEST_NOT_RUN;
  251. case TEST_EXECUTE:
  252. break;
  253. }
  254. if (!mkdtemp(dir)) {
  255. ast_test_status_update(test, "Cannot create temporary directory: %s\n", strerror(errno));
  256. return AST_TEST_FAIL;
  257. }
  258. disp[0] = ast_str_create(16);
  259. disp[1] = ast_str_create(16);
  260. if (!(buf = ast_str_create(16)) || !disp[0] || !disp[1]) {
  261. ast_free(buf);
  262. ast_free(disp[0]);
  263. ast_free(disp[1]);
  264. rmdir(dir);
  265. return AST_TEST_FAIL;
  266. }
  267. snprintf(file, sizeof(file), "%s/test.txt", dir);
  268. for (i = 0; i < ARRAY_LEN(read_tests); i++) {
  269. if (!(fh = fopen(file, "w"))) {
  270. ast_test_status_update(test, "Cannot open test file: %s\n", strerror(errno));
  271. ast_free(buf);
  272. ast_free(disp[0]);
  273. ast_free(disp[1]);
  274. unlink(file);
  275. rmdir(dir);
  276. return AST_TEST_FAIL;
  277. }
  278. if (fwrite(read_tests[i].contents, 1, strlen(read_tests[i].contents), fh) < strlen(read_tests[i].contents)) {
  279. ast_test_status_update(test, "Cannot write initial values into test file: %s\n", strerror(errno));
  280. ast_free(buf);
  281. ast_free(disp[0]);
  282. ast_free(disp[1]);
  283. fclose(fh);
  284. unlink(file);
  285. rmdir(dir);
  286. return AST_TEST_FAIL;
  287. }
  288. fclose(fh);
  289. snprintf(expression, sizeof(expression), "${FILE(%s,%s)}", file, read_tests[i].args);
  290. ast_str_substitute_variables(&buf, 0, NULL, expression);
  291. if (strcmp(ast_str_buffer(buf), read_tests[i].value)) {
  292. ast_test_status_update(test, "Expression '${FILE(...,%s)}' did not produce ('%s') the expected value ('%s')\n",
  293. read_tests[i].args, file2display(&disp[0], 0, ast_str_buffer(buf)), file2display(&disp[1], 0, read_tests[i].value));
  294. res = AST_TEST_FAIL;
  295. }
  296. }
  297. ast_free(buf);
  298. for (i = 0; i < ARRAY_LEN(write_tests); i++) {
  299. if (!(fh = fopen(file, "w"))) {
  300. ast_test_status_update(test, "Cannot open test file: %s\n", strerror(errno));
  301. ast_free(disp[0]);
  302. ast_free(disp[1]);
  303. unlink(file);
  304. rmdir(dir);
  305. return AST_TEST_FAIL;
  306. }
  307. if (fwrite(write_tests[i].contents, 1, strlen(write_tests[i].contents), fh) < strlen(write_tests[i].contents)) {
  308. ast_test_status_update(test, "Cannot write initial values into test file: %s\n", strerror(errno));
  309. ast_free(disp[0]);
  310. ast_free(disp[1]);
  311. fclose(fh);
  312. unlink(file);
  313. rmdir(dir);
  314. return AST_TEST_FAIL;
  315. }
  316. fclose(fh);
  317. snprintf(expression, sizeof(expression), "FILE(%s,%s)", file, write_tests[i].args);
  318. ast_func_write(NULL, expression, write_tests[i].value);
  319. if (!(fh = fopen(file, "r"))) {
  320. ast_test_status_update(test, "Cannot open test file: %s\n", strerror(errno));
  321. ast_free(disp[0]);
  322. ast_free(disp[1]);
  323. unlink(file);
  324. rmdir(dir);
  325. return AST_TEST_FAIL;
  326. }
  327. memset(fbuf, 0, sizeof(fbuf));
  328. if (!fread(fbuf, 1, sizeof(fbuf), fh)) {
  329. ast_test_status_update(test, "Cannot read write results from test file: %s\n", strerror(errno));
  330. ast_free(disp[0]);
  331. ast_free(disp[1]);
  332. fclose(fh);
  333. unlink(file);
  334. rmdir(dir);
  335. return AST_TEST_FAIL;
  336. }
  337. fclose(fh);
  338. if (strcmp(fbuf, write_tests[i].contents2)) {
  339. ast_test_status_update(test, "Expression 'FILE(...,%s)=%s' did not produce ('%s') the expected result ('%s')\n",
  340. write_tests[i].args, write_tests[i].value, file2display(&disp[0], 0, fbuf), file2display(&disp[1], 0, write_tests[i].contents2));
  341. res = AST_TEST_FAIL;
  342. } else {
  343. ast_test_status_update(test, "Expression 'FILE(...,%s)=%s'... OK!\n", write_tests[i].args, write_tests[i].value);
  344. }
  345. }
  346. ast_free(disp[0]);
  347. ast_free(disp[1]);
  348. unlink(file);
  349. rmdir(dir);
  350. return res;
  351. }
  352. static int unload_module(void)
  353. {
  354. AST_TEST_UNREGISTER(test_func_file);
  355. return 0;
  356. }
  357. static int load_module(void)
  358. {
  359. AST_TEST_REGISTER(test_func_file);
  360. return AST_MODULE_LOAD_SUCCESS;
  361. }
  362. AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "FILE() Tests");