test_cmd.c 8.0 KB

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