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