test_cmd.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /******************************************************************************
  2. This program is free software; you can redistribute it and/or modify
  3. it under the terms of the GNU General Public License as published by
  4. the Free Software Foundation; either version 2 of the License, or
  5. (at your option) any later version.
  6. This program is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. GNU General Public License for more details.
  10. You should have received a copy of the GNU General Public License
  11. along with this program; if not, write to the Free Software
  12. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  13. $Id: test_cmd.c 1732 2007-06-03 15:58:22Z psychotrahe $
  14. ******************************************************************************/
  15. #include "common.h"
  16. #include "utils_cmd.h"
  17. #include "utils_base.h"
  18. #include "tap.h"
  19. #define COMMAND_LINE 1024
  20. #define UNSET 65530
  21. char *
  22. get_command (char *const *line)
  23. {
  24. char *cmd;
  25. int i = 0;
  26. asprintf (&cmd, " %s", line[i++]);
  27. while (line[i] != NULL) {
  28. asprintf (&cmd, "%s %s", cmd, line[i]);
  29. i++;
  30. }
  31. return cmd;
  32. }
  33. int
  34. main (int argc, char **argv)
  35. {
  36. char **command_line = malloc (sizeof (char *) * COMMAND_LINE);
  37. char *command = NULL;
  38. char *perl;
  39. output chld_out, chld_err;
  40. int c;
  41. int result = UNSET;
  42. plan_tests(47);
  43. diag ("Running plain echo command, set one");
  44. /* ensure everything is empty before we begin */
  45. memset (&chld_out, 0, sizeof (output));
  46. memset (&chld_err, 0, sizeof (output));
  47. ok (chld_out.lines == 0, "(initialised) Checking stdout is reset");
  48. ok (chld_err.lines == 0, "(initialised) Checking stderr is reset");
  49. ok (result == UNSET, "(initialised) Checking exit code is reset");
  50. command_line[0] = strdup ("/bin/echo");
  51. command_line[1] = strdup ("this");
  52. command_line[2] = strdup ("is");
  53. command_line[3] = strdup ("test");
  54. command_line[4] = strdup ("one");
  55. command = get_command (command_line);
  56. result = cmd_run_array (command_line, &chld_out, &chld_err, 0);
  57. ok (chld_out.lines == 1,
  58. "(array) Check for expected number of stdout lines");
  59. ok (chld_err.lines == 0,
  60. "(array) Check for expected number of stderr lines");
  61. ok (strcmp (chld_out.line[0], "this is test one") == 0,
  62. "(array) Check for expected stdout output");
  63. ok (result == 0, "(array) Checking exit code");
  64. /* ensure everything is empty again */
  65. memset (&chld_out, 0, sizeof (output));
  66. memset (&chld_err, 0, sizeof (output));
  67. result = UNSET;
  68. ok (chld_out.lines == 0, "(initialised) Checking stdout is reset");
  69. ok (chld_err.lines == 0, "(initialised) Checking stderr is reset");
  70. ok (result == UNSET, "(initialised) Checking exit code is reset");
  71. result = cmd_run (command, &chld_out, &chld_err, 0);
  72. ok (chld_out.lines == 1,
  73. "(string) Check for expected number of stdout lines");
  74. ok (chld_err.lines == 0,
  75. "(string) Check for expected number of stderr lines");
  76. ok (strcmp (chld_out.line[0], "this is test one") == 0,
  77. "(string) Check for expected stdout output");
  78. ok (result == 0, "(string) Checking exit code");
  79. diag ("Running plain echo command, set two");
  80. /* ensure everything is empty again */
  81. memset (&chld_out, 0, sizeof (output));
  82. memset (&chld_err, 0, sizeof (output));
  83. result = UNSET;
  84. ok (chld_out.lines == 0, "(initialised) Checking stdout is reset");
  85. ok (chld_err.lines == 0, "(initialised) Checking stderr is reset");
  86. ok (result == UNSET, "(initialised) Checking exit code is reset");
  87. command_line[0] = strdup ("/bin/echo");
  88. command_line[1] = strdup ("this is test two");
  89. command_line[2] = NULL;
  90. command_line[3] = NULL;
  91. command_line[4] = NULL;
  92. result = cmd_run_array (command_line, &chld_out, &chld_err, 0);
  93. ok (chld_out.lines == 1,
  94. "(array) Check for expected number of stdout lines");
  95. ok (chld_err.lines == 0,
  96. "(array) Check for expected number of stderr lines");
  97. ok (strcmp (chld_out.line[0], "this is test two") == 0,
  98. "(array) Check for expected stdout output");
  99. ok (result == 0, "(array) Checking exit code");
  100. /* ensure everything is empty again */
  101. memset (&chld_out, 0, sizeof (output));
  102. memset (&chld_err, 0, sizeof (output));
  103. result = UNSET;
  104. ok (chld_out.lines == 0, "(initialised) Checking stdout is reset");
  105. ok (chld_err.lines == 0, "(initialised) Checking stderr is reset");
  106. ok (result == UNSET, "(initialised) Checking exit code is reset");
  107. result = cmd_run (command, &chld_out, &chld_err, 0);
  108. ok (chld_out.lines == 1,
  109. "(string) Check for expected number of stdout lines");
  110. ok (chld_err.lines == 0,
  111. "(string) Check for expected number of stderr lines");
  112. ok (strcmp (chld_out.line[0], "this is test one") == 0,
  113. "(string) Check for expected stdout output");
  114. ok (result == 0, "(string) Checking exit code");
  115. /* ensure everything is empty again */
  116. memset (&chld_out, 0, sizeof (output));
  117. memset (&chld_err, 0, sizeof (output));
  118. result = UNSET;
  119. ok (chld_out.lines == 0, "(initialised) Checking stdout is reset");
  120. ok (chld_err.lines == 0, "(initialised) Checking stderr is reset");
  121. ok (result == UNSET, "(initialised) Checking exit code is reset");
  122. /* Pass linefeeds via parameters through - those should be evaluated by echo to give multi line output */
  123. command_line[0] = strdup("/bin/echo");
  124. command_line[1] = strdup("this is a test via echo\nline two\nit's line 3");
  125. command_line[2] = strdup("and (note space between '3' and 'and') $$ will not get evaluated");
  126. result = cmd_run_array (command_line, &chld_out, &chld_err, 0);
  127. ok (chld_out.lines == 3,
  128. "(array) Check for expected number of stdout lines");
  129. ok (chld_err.lines == 0,
  130. "(array) Check for expected number of stderr lines");
  131. ok (strcmp (chld_out.line[0], "this is a test via echo") == 0,
  132. "(array) Check line 1 for expected stdout output");
  133. ok (strcmp (chld_out.line[1], "line two") == 0,
  134. "(array) Check line 2 for expected stdout output");
  135. ok (strcmp (chld_out.line[2], "it's line 3 and (note space between '3' and 'and') $$ will not get evaluated") == 0,
  136. "(array) Check line 3 for expected stdout output");
  137. ok (result == 0, "(array) Checking exit code");
  138. /* ensure everything is empty again */
  139. memset (&chld_out, 0, sizeof (output));
  140. memset (&chld_err, 0, sizeof (output));
  141. result = UNSET;
  142. ok (chld_out.lines == 0, "(initialised) Checking stdout is reset");
  143. ok (chld_err.lines == 0, "(initialised) Checking stderr is reset");
  144. ok (result == UNSET, "(initialised) Checking exit code is reset");
  145. command = (char *)malloc(COMMAND_LINE);
  146. strcpy(command, "/bin/echo3456 non-existant command");
  147. result = cmd_run (command, &chld_out, &chld_err, 0);
  148. ok (chld_out.lines == 0,
  149. "Non existant command, so no output");
  150. ok (chld_err.lines == 0,
  151. "No stderr either");
  152. ok (result == 3, "Get return code 3 (?) for non-existant command");
  153. /* ensure everything is empty again */
  154. memset (&chld_out, 0, sizeof (output));
  155. memset (&chld_err, 0, sizeof (output));
  156. result = UNSET;
  157. command = (char *)malloc(COMMAND_LINE);
  158. strcpy(command, "/bin/grep pattern non-existant-file");
  159. result = cmd_run (command, &chld_out, &chld_err, 0);
  160. ok (chld_out.lines == 0,
  161. "Grep returns no stdout when file is missing...");
  162. ok (chld_err.lines == 1,
  163. "...but does give an error line");
  164. ok (strstr(chld_err.line[0],"non-existant-file") != NULL, "And missing filename is in error message");
  165. ok (result == 2, "Get return code 2 from grep");
  166. return exit_status ();
  167. }