test_func_file.c 15 KB

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