test-expandargv.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. /* expandargv test program,
  2. Copyright (C) 2006 Free Software Foundation, Inc.
  3. Written by Carlos O'Donell <carlos@codesourcery.com>
  4. This file is part of the libiberty library, which is part of GCC.
  5. This file is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. In addition to the permissions in the GNU General Public License, the
  10. Free Software Foundation gives you unlimited permission to link the
  11. compiled version of this file into combinations with other programs,
  12. and to distribute those combinations without any restriction coming
  13. from the use of this file. (The General Public License restrictions
  14. do apply in other respects; for example, they cover modification of
  15. the file, and distribution when not linked into a combined
  16. executable.)
  17. This program is distributed in the hope that it will be useful,
  18. but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. GNU General Public License for more details.
  21. You should have received a copy of the GNU General Public License
  22. along with this program; if not, write to the Free Software
  23. Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
  24. */
  25. #ifdef HAVE_CONFIG_H
  26. #include "config.h"
  27. #endif
  28. #include "libiberty.h"
  29. #include <stdio.h>
  30. #include <errno.h>
  31. #ifdef HAVE_STDLIB_H
  32. #include <stdlib.h>
  33. #endif
  34. #ifdef HAVE_STRING_H
  35. #include <string.h>
  36. #endif
  37. #ifdef HAVE_UNISTD_H
  38. #include <unistd.h>
  39. #endif
  40. #ifndef EXIT_SUCCESS
  41. #define EXIT_SUCCESS 0
  42. #endif
  43. #ifndef EXIT_FAILURE
  44. #define EXIT_FAILURE 1
  45. #endif
  46. static void fatal_error (int, const char *, int) ATTRIBUTE_NORETURN;
  47. void writeout_test (int, const char *);
  48. void run_replaces (char *);
  49. void hook_char_replace (char *, size_t, char, char);
  50. int run_tests (const char **);
  51. void erase_test (int);
  52. /* Test input data, argv before, and argv after:
  53. The \n is an important part of test_data since expandargv
  54. may have to work in environments where \n is translated
  55. as \r\n. Thus \n is included in the test data for the file.
  56. We use \b to indicate that the test data is the null character.
  57. This is because we use \0 normally to represent the end of the
  58. file data, so we need something else for this. */
  59. #define FILENAME_PATTERN "test-expandargv-%d.lst"
  60. #define ARGV0 "test-expandargv"
  61. const char *test_data[] = {
  62. /* Test 0 - Check for expansion with \r\n */
  63. "a\r\nb", /* Test 0 data */
  64. ARGV0,
  65. "@test-expandargv-0.lst",
  66. 0, /* End of argv[] before expansion */
  67. ARGV0,
  68. "a",
  69. "b",
  70. 0, /* End of argv[] after expansion */
  71. /* Test 1 - Check for expansion with \n */
  72. "a\nb", /* Test 1 data */
  73. ARGV0,
  74. "@test-expandargv-1.lst",
  75. 0,
  76. ARGV0,
  77. "a",
  78. "b",
  79. 0,
  80. /* Test 2 - Check for expansion with \0 */
  81. "a\bb", /* Test 2 data */
  82. ARGV0,
  83. "@test-expandargv-2.lst",
  84. 0,
  85. ARGV0,
  86. "a",
  87. 0,
  88. /* Test 3 - Check for expansion with only \0 */
  89. "\b", /* Test 3 data */
  90. ARGV0,
  91. "@test-expandargv-3.lst",
  92. 0,
  93. ARGV0,
  94. 0,
  95. /* Test 4 - Check for options beginning with an empty line. */
  96. "\na\nb", /* Test 4 data */
  97. ARGV0,
  98. "@test-expandargv-4.lst",
  99. 0,
  100. ARGV0,
  101. "a",
  102. "b",
  103. 0,
  104. /* Test 5 - Check for options containing an empty argument. */
  105. "a\n''\nb", /* Test 5 data */
  106. ARGV0,
  107. "@test-expandargv-5.lst",
  108. 0,
  109. ARGV0,
  110. "a",
  111. "",
  112. "b",
  113. 0,
  114. /* Test 6 - Check for options containing a quoted newline. */
  115. "a\n'a\n\nb'\nb", /* Test 6 data */
  116. ARGV0,
  117. "@test-expandargv-6.lst",
  118. 0,
  119. ARGV0,
  120. "a",
  121. "a\n\nb",
  122. "b",
  123. 0,
  124. 0 /* Test done marker, don't remove. */
  125. };
  126. /* Print a fatal error and exit. LINE is the line number where we
  127. detected the error, ERRMSG is the error message to print, and ERR
  128. is 0 or an errno value to print. */
  129. static void
  130. fatal_error (int line, const char *errmsg, int err)
  131. {
  132. fprintf (stderr, "test-expandargv:%d: %s", line, errmsg);
  133. if (errno != 0)
  134. fprintf (stderr, ": %s", xstrerror (err));
  135. fprintf (stderr, "\n");
  136. exit (EXIT_FAILURE);
  137. }
  138. /* hook_char_replace:
  139. Replace 'replacethis' with 'withthis' */
  140. void
  141. hook_char_replace (char *string, size_t len, char replacethis, char withthis)
  142. {
  143. int i = 0;
  144. for (i = 0; i < len; i++)
  145. if (string[i] == replacethis)
  146. string[i] = withthis;
  147. }
  148. /* run_replaces:
  149. Hook here all the character for character replaces.
  150. Be warned that expanding the string or contracting the string
  151. should be handled with care. */
  152. void
  153. run_replaces (char * string)
  154. {
  155. /* Store original string size */
  156. size_t len = strlen (string);
  157. hook_char_replace (string, len, '\b', '\0');
  158. }
  159. /* write_test:
  160. Write test datafile */
  161. void
  162. writeout_test (int test, const char * test_data)
  163. {
  164. char filename[256];
  165. FILE *fd;
  166. size_t len, sys_fwrite;
  167. char * parse;
  168. /* Unique filename per test */
  169. sprintf (filename, FILENAME_PATTERN, test);
  170. fd = fopen (filename, "w");
  171. if (fd == NULL)
  172. fatal_error (__LINE__, "Failed to create test file.", errno);
  173. /* Generate RW copy of data for replaces */
  174. len = strlen (test_data);
  175. parse = malloc (sizeof (char) * (len + 1));
  176. if (parse == NULL)
  177. fatal_error (__LINE__, "Failed to malloc parse.", errno);
  178. memcpy (parse, test_data, sizeof (char) * (len + 1));
  179. /* Run all possible replaces */
  180. run_replaces (parse);
  181. sys_fwrite = fwrite (parse, sizeof (char), len, fd);
  182. if (sys_fwrite != len)
  183. fatal_error (__LINE__, "Failed to write to test file.", errno);
  184. free (parse);
  185. fclose (fd);
  186. }
  187. /* erase_test:
  188. Erase the test file */
  189. void
  190. erase_test (int test)
  191. {
  192. char filename[256];
  193. sprintf (filename, FILENAME_PATTERN, test);
  194. if (unlink (filename) != 0)
  195. fatal_error (__LINE__, "Failed to erase test file.", errno);
  196. }
  197. /* run_tests:
  198. Run expandargv
  199. Compare argv before and after.
  200. Return number of fails */
  201. int
  202. run_tests (const char **test_data)
  203. {
  204. int argc_after, argc_before;
  205. char ** argv_before, ** argv_after;
  206. int i, j, k, fails, failed;
  207. i = j = fails = 0;
  208. /* Loop over all the tests */
  209. while (test_data[j])
  210. {
  211. /* Write test data */
  212. writeout_test (i, test_data[j++]);
  213. /* Copy argv before */
  214. argv_before = dupargv ((char **) &test_data[j]);
  215. /* Count argc before/after */
  216. argc_before = 0;
  217. argc_after = 0;
  218. while (test_data[j + argc_before])
  219. argc_before++;
  220. j += argc_before + 1; /* Skip null */
  221. while (test_data[j + argc_after])
  222. argc_after++;
  223. /* Copy argv after */
  224. argv_after = dupargv ((char **) &test_data[j]);
  225. /* Run all possible replaces */
  226. for (k = 0; k < argc_before; k++)
  227. run_replaces (argv_before[k]);
  228. for (k = 0; k < argc_after; k++)
  229. run_replaces (argv_after[k]);
  230. /* Run test: Expand arguments */
  231. expandargv (&argc_before, &argv_before);
  232. failed = 0;
  233. /* Compare size first */
  234. if (argc_before != argc_after)
  235. {
  236. printf ("FAIL: test-expandargv-%d. Number of arguments don't match.\n", i);
  237. failed++;
  238. }
  239. /* Compare each of the argv's ... */
  240. else
  241. for (k = 0; k < argc_after; k++)
  242. if (strcmp (argv_before[k], argv_after[k]) != 0)
  243. {
  244. printf ("FAIL: test-expandargv-%d. Arguments don't match.\n", i);
  245. failed++;
  246. }
  247. if (!failed)
  248. printf ("PASS: test-expandargv-%d.\n", i);
  249. else
  250. fails++;
  251. freeargv (argv_before);
  252. freeargv (argv_after);
  253. /* Advance to next test */
  254. j += argc_after + 1;
  255. /* Erase test file */
  256. erase_test (i);
  257. i++;
  258. }
  259. return fails;
  260. }
  261. /* main:
  262. Run tests.
  263. Check result and exit with appropriate code. */
  264. int
  265. main(int argc, char **argv)
  266. {
  267. int fails;
  268. /* Repeat for all the tests:
  269. - Parse data array and write into file.
  270. - Run replace hooks before writing to file.
  271. - Parse data array and build argv before/after.
  272. - Run replace hooks on argv before/after
  273. - Run expandargv.
  274. - Compare output of expandargv argv to after argv.
  275. - If they compare the same then test passes
  276. else the test fails.
  277. - Erase test file. */
  278. fails = run_tests (test_data);
  279. if (!fails)
  280. exit (EXIT_SUCCESS);
  281. else
  282. exit (EXIT_FAILURE);
  283. }