test_cmd.c 8.0 KB

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