Back to index

nagios-plugins  1.4.16
test_cmd.c
Go to the documentation of this file.
00001 /*****************************************************************************
00002 * 
00003 * This program is free software: you can redistribute it and/or modify
00004 * it under the terms of the GNU General Public License as published by
00005 * the Free Software Foundation, either version 3 of the License, or
00006 * (at your option) any later version.
00007 * 
00008 * This program is distributed in the hope that it will be useful,
00009 * but WITHOUT ANY WARRANTY; without even the implied warranty of
00010 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00011 * GNU General Public License for more details.
00012 * 
00013 * You should have received a copy of the GNU General Public License
00014 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
00015 * 
00016 * 
00017 *****************************************************************************/
00018 
00019 #include "common.h"
00020 #include "utils_cmd.h"
00021 #include "utils_base.h"
00022 #include "tap.h"
00023 
00024 #define COMMAND_LINE 1024
00025 #define UNSET 65530
00026 
00027 char *
00028 get_command (char *const *line)
00029 {
00030        char *cmd;
00031        int i = 0;
00032 
00033        asprintf (&cmd, " %s", line[i++]);
00034        while (line[i] != NULL) {
00035               asprintf (&cmd, "%s %s", cmd, line[i]);
00036               i++;
00037        }
00038 
00039        return cmd;
00040 }
00041 
00042 int
00043 main (int argc, char **argv)
00044 {
00045        char **command_line = malloc (sizeof (char *) * COMMAND_LINE);
00046        char *command = NULL;
00047        char *perl;
00048        output chld_out, chld_err;
00049        int c;
00050        int result = UNSET;
00051 
00052        plan_tests(51);
00053 
00054        diag ("Running plain echo command, set one");
00055 
00056        /* ensure everything is empty before we begin */
00057        memset (&chld_out, 0, sizeof (output));
00058        memset (&chld_err, 0, sizeof (output));
00059        ok (chld_out.lines == 0, "(initialised) Checking stdout is reset");
00060        ok (chld_err.lines == 0, "(initialised) Checking stderr is reset");
00061        ok (result == UNSET, "(initialised) Checking exit code is reset");
00062 
00063        command_line[0] = strdup ("/bin/echo");
00064        command_line[1] = strdup ("this");
00065        command_line[2] = strdup ("is");
00066        command_line[3] = strdup ("test");
00067        command_line[4] = strdup ("one");
00068 
00069        command = get_command (command_line);
00070 
00071        result = cmd_run_array (command_line, &chld_out, &chld_err, 0);
00072        ok (chld_out.lines == 1,
00073                      "(array) Check for expected number of stdout lines");
00074        ok (chld_err.lines == 0,
00075                      "(array) Check for expected number of stderr lines");
00076        ok (strcmp (chld_out.line[0], "this is test one") == 0,
00077                      "(array) Check for expected stdout output");
00078        ok (result == 0, "(array) Checking exit code");
00079 
00080        /* ensure everything is empty again */
00081        memset (&chld_out, 0, sizeof (output));
00082        memset (&chld_err, 0, sizeof (output));
00083        result = UNSET;
00084        ok (chld_out.lines == 0, "(initialised) Checking stdout is reset");
00085        ok (chld_err.lines == 0, "(initialised) Checking stderr is reset");
00086        ok (result == UNSET, "(initialised) Checking exit code is reset");
00087 
00088        result = cmd_run (command, &chld_out, &chld_err, 0);
00089 
00090        ok (chld_out.lines == 1,
00091                      "(string) Check for expected number of stdout lines");
00092        ok (chld_err.lines == 0,
00093                      "(string) Check for expected number of stderr lines");
00094        ok (strcmp (chld_out.line[0], "this is test one") == 0,
00095                      "(string) Check for expected stdout output");
00096        ok (result == 0, "(string) Checking exit code");
00097 
00098        diag ("Running plain echo command, set two");
00099 
00100        /* ensure everything is empty again */
00101        memset (&chld_out, 0, sizeof (output));
00102        memset (&chld_err, 0, sizeof (output));
00103        result = UNSET;
00104        ok (chld_out.lines == 0, "(initialised) Checking stdout is reset");
00105        ok (chld_err.lines == 0, "(initialised) Checking stderr is reset");
00106        ok (result == UNSET, "(initialised) Checking exit code is reset");
00107 
00108        command_line[0] = strdup ("/bin/echo");
00109        command_line[1] = strdup ("this is test two");
00110        command_line[2] = NULL;
00111        command_line[3] = NULL;
00112        command_line[4] = NULL;
00113 
00114        result = cmd_run_array (command_line, &chld_out, &chld_err, 0);
00115        ok (chld_out.lines == 1,
00116                      "(array) Check for expected number of stdout lines");
00117        ok (chld_err.lines == 0,
00118                      "(array) Check for expected number of stderr lines");
00119        ok (strcmp (chld_out.line[0], "this is test two") == 0,
00120                      "(array) Check for expected stdout output");
00121        ok (result == 0, "(array) Checking exit code");
00122 
00123        /* ensure everything is empty again */
00124        memset (&chld_out, 0, sizeof (output));
00125        memset (&chld_err, 0, sizeof (output));
00126        result = UNSET;
00127        ok (chld_out.lines == 0, "(initialised) Checking stdout is reset");
00128        ok (chld_err.lines == 0, "(initialised) Checking stderr is reset");
00129        ok (result == UNSET, "(initialised) Checking exit code is reset");
00130 
00131        result = cmd_run (command, &chld_out, &chld_err, 0);
00132 
00133        ok (chld_out.lines == 1,
00134                      "(string) Check for expected number of stdout lines");
00135        ok (chld_err.lines == 0,
00136                      "(string) Check for expected number of stderr lines");
00137        ok (strcmp (chld_out.line[0], "this is test one") == 0,
00138                      "(string) Check for expected stdout output");
00139        ok (result == 0, "(string) Checking exit code");
00140 
00141 
00142        /* ensure everything is empty again */
00143        memset (&chld_out, 0, sizeof (output));
00144        memset (&chld_err, 0, sizeof (output));
00145        result = UNSET;
00146        ok (chld_out.lines == 0, "(initialised) Checking stdout is reset");
00147        ok (chld_err.lines == 0, "(initialised) Checking stderr is reset");
00148        ok (result == UNSET, "(initialised) Checking exit code is reset");
00149 
00150        /* Pass linefeeds via parameters through - those should be evaluated by echo to give multi line output */
00151        command_line[0] = strdup("/bin/echo");
00152        command_line[1] = strdup("this is a test via echo\nline two\nit's line 3");
00153        command_line[2] = strdup("and (note space between '3' and 'and') $$ will not get evaluated");
00154 
00155        result = cmd_run_array (command_line, &chld_out, &chld_err, 0);
00156        ok (chld_out.lines == 3,
00157                      "(array) Check for expected number of stdout lines");
00158        ok (chld_err.lines == 0,
00159                      "(array) Check for expected number of stderr lines");
00160        ok (strcmp (chld_out.line[0], "this is a test via echo") == 0,
00161                      "(array) Check line 1 for expected stdout output");
00162        ok (strcmp (chld_out.line[1], "line two") == 0,
00163                      "(array) Check line 2 for expected stdout output");
00164        ok (strcmp (chld_out.line[2], "it's line 3 and (note space between '3' and 'and') $$ will not get evaluated") == 0,
00165                      "(array) Check line 3 for expected stdout output");
00166        ok (result == 0, "(array) Checking exit code");
00167 
00168 
00169 
00170        /* ensure everything is empty again */
00171        memset (&chld_out, 0, sizeof (output));
00172        memset (&chld_err, 0, sizeof (output));
00173        result = UNSET;
00174        ok (chld_out.lines == 0, "(initialised) Checking stdout is reset");
00175        ok (chld_err.lines == 0, "(initialised) Checking stderr is reset");
00176        ok (result == UNSET, "(initialised) Checking exit code is reset");
00177 
00178        command = (char *)malloc(COMMAND_LINE);
00179        strcpy(command, "/bin/echo3456 non-existant command");
00180        result = cmd_run (command, &chld_out, &chld_err, 0);
00181 
00182        ok (chld_out.lines == 0,
00183                      "Non existant command, so no output");
00184        ok (chld_err.lines == 0,
00185                      "No stderr either");
00186        ok (result == 3, "Get return code 3 (?) for non-existant command");
00187 
00188 
00189        /* ensure everything is empty again */
00190        memset (&chld_out, 0, sizeof (output));
00191        memset (&chld_err, 0, sizeof (output));
00192        result = UNSET;
00193 
00194        command = (char *)malloc(COMMAND_LINE);
00195        strcpy(command, "/bin/sh non-existant-file");
00196        result = cmd_run (command, &chld_out, &chld_err, 0);
00197 
00198        ok (chld_out.lines == 0,
00199                      "/bin/sh returns no stdout when file is missing...");
00200        ok (chld_err.lines == 1,
00201                      "...but does give an error line");
00202        ok (strstr(chld_err.line[0],"non-existant-file") != NULL, "And missing filename is in error message");
00203        ok (result != 0, "Get non-zero return code from /bin/sh");
00204 
00205 
00206        /* ensure everything is empty again */
00207        result = UNSET;
00208 
00209        command = (char *)malloc(COMMAND_LINE);
00210   strcpy(command, "/bin/sh -c 'exit 7'");
00211   result = cmd_run (command, NULL, NULL, 0);
00212 
00213   ok (result == 7, "Get return code 7 from /bin/sh");
00214 
00215 
00216        /* ensure everything is empty again */
00217        memset (&chld_out, 0, sizeof (output));
00218        memset (&chld_err, 0, sizeof (output));
00219        result = UNSET;
00220 
00221        command = (char *)malloc(COMMAND_LINE);
00222        strcpy(command, "/bin/non-existant-command");
00223        result = cmd_run (command, &chld_out, &chld_err, 0);
00224 
00225        ok (chld_out.lines == 0,
00226                      "/bin/non-existant-command returns no stdout...");
00227        ok (chld_err.lines == 0,
00228                      "...and no stderr output either");
00229        ok (result == 3, "Get return code 3 = UNKNOWN when command does not exist");
00230 
00231 
00232        return exit_status ();
00233 }