aboutsummaryrefslogtreecommitdiff
path: root/lib/tests
diff options
context:
space:
mode:
Diffstat (limited to 'lib/tests')
-rw-r--r--lib/tests/Makefile.am9
-rw-r--r--lib/tests/test_cmd.c210
-rw-r--r--lib/tests/test_cmd.t6
3 files changed, 223 insertions, 2 deletions
diff --git a/lib/tests/Makefile.am b/lib/tests/Makefile.am
index 6594db2f..9ca22d16 100644
--- a/lib/tests/Makefile.am
+++ b/lib/tests/Makefile.am
@@ -7,9 +7,9 @@ check_PROGRAMS = @EXTRA_TEST@
INCLUDES = -I$(top_srcdir)/lib -I$(top_srcdir)/gl -I$(top_srcdir)/intl -I$(top_srcdir)/plugins
-EXTRA_PROGRAMS = test_utils test_disk test_tcp
+EXTRA_PROGRAMS = test_utils test_disk test_tcp test_cmd
-EXTRA_DIST = test_utils.t test_disk.t test_tcp.t
+EXTRA_DIST = test_utils.t test_disk.t test_tcp.t test_cmd.t
LIBS = @LIBINTL@
@@ -28,6 +28,11 @@ test_tcp_CFLAGS = -g -I..
test_tcp_LDFLAGS = -L/usr/local/lib -ltap
test_tcp_LDADD = ../utils_tcp.o
+test_cmd_SOURCES = test_cmd.c
+test_cmd_CFLAGS = -g -I..
+test_cmd_LDFLAGS = -L/usr/local/lib -ltap
+test_cmd_LDADD = ../utils_cmd.o ../utils_base.o
+
test: ${noinst_PROGRAMS}
perl -MTest::Harness -e '$$Test::Harness::switches=""; runtests(map {$$_ .= ".t"} @ARGV)' $(EXTRA_PROGRAMS)
diff --git a/lib/tests/test_cmd.c b/lib/tests/test_cmd.c
new file mode 100644
index 00000000..4da76a9b
--- /dev/null
+++ b/lib/tests/test_cmd.c
@@ -0,0 +1,210 @@
+/******************************************************************************
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+
+ $Id: test_cmd.c 1732 2007-06-03 15:58:22Z psychotrahe $
+
+******************************************************************************/
+
+#include "common.h"
+#include "utils_cmd.h"
+#include "utils_base.h"
+#include "tap.h"
+
+#define COMMAND_LINE 1024
+#define UNSET 65530
+
+char *
+get_command (char *const *line)
+{
+ char *cmd;
+ int i = 0;
+
+ asprintf (&cmd, " %s", line[i++]);
+ while (line[i] != NULL) {
+ asprintf (&cmd, "%s %s", cmd, line[i]);
+ i++;
+ }
+
+ return cmd;
+}
+
+int
+main (int argc, char **argv)
+{
+ char **command_line = malloc (sizeof (char *) * COMMAND_LINE);
+ char *command = NULL;
+ char *perl;
+ output chld_out, chld_err;
+ int c;
+ int result = UNSET;
+
+ plan_tests(47);
+
+ diag ("Running plain echo command, set one");
+
+ /* ensure everything is empty before we begin */
+ memset (&chld_out, 0, sizeof (output));
+ memset (&chld_err, 0, sizeof (output));
+ ok (chld_out.lines == 0, "(initialised) Checking stdout is reset");
+ ok (chld_err.lines == 0, "(initialised) Checking stderr is reset");
+ ok (result == UNSET, "(initialised) Checking exit code is reset");
+
+ command_line[0] = strdup ("/bin/echo");
+ command_line[1] = strdup ("this");
+ command_line[2] = strdup ("is");
+ command_line[3] = strdup ("test");
+ command_line[4] = strdup ("one");
+
+ command = get_command (command_line);
+
+ result = cmd_run_array (command_line, &chld_out, &chld_err, 0);
+ ok (chld_out.lines == 1,
+ "(array) Check for expected number of stdout lines");
+ ok (chld_err.lines == 0,
+ "(array) Check for expected number of stderr lines");
+ ok (strcmp (chld_out.line[0], "this is test one") == 0,
+ "(array) Check for expected stdout output");
+ ok (result == 0, "(array) Checking exit code");
+
+ /* ensure everything is empty again */
+ memset (&chld_out, 0, sizeof (output));
+ memset (&chld_err, 0, sizeof (output));
+ result = UNSET;
+ ok (chld_out.lines == 0, "(initialised) Checking stdout is reset");
+ ok (chld_err.lines == 0, "(initialised) Checking stderr is reset");
+ ok (result == UNSET, "(initialised) Checking exit code is reset");
+
+ result = cmd_run (command, &chld_out, &chld_err, 0);
+
+ ok (chld_out.lines == 1,
+ "(string) Check for expected number of stdout lines");
+ ok (chld_err.lines == 0,
+ "(string) Check for expected number of stderr lines");
+ ok (strcmp (chld_out.line[0], "this is test one") == 0,
+ "(string) Check for expected stdout output");
+ ok (result == 0, "(string) Checking exit code");
+
+ diag ("Running plain echo command, set two");
+
+ /* ensure everything is empty again */
+ memset (&chld_out, 0, sizeof (output));
+ memset (&chld_err, 0, sizeof (output));
+ result = UNSET;
+ ok (chld_out.lines == 0, "(initialised) Checking stdout is reset");
+ ok (chld_err.lines == 0, "(initialised) Checking stderr is reset");
+ ok (result == UNSET, "(initialised) Checking exit code is reset");
+
+ command_line[0] = strdup ("/bin/echo");
+ command_line[1] = strdup ("this is test two");
+ command_line[2] = NULL;
+ command_line[3] = NULL;
+ command_line[4] = NULL;
+
+ result = cmd_run_array (command_line, &chld_out, &chld_err, 0);
+ ok (chld_out.lines == 1,
+ "(array) Check for expected number of stdout lines");
+ ok (chld_err.lines == 0,
+ "(array) Check for expected number of stderr lines");
+ ok (strcmp (chld_out.line[0], "this is test two") == 0,
+ "(array) Check for expected stdout output");
+ ok (result == 0, "(array) Checking exit code");
+
+ /* ensure everything is empty again */
+ memset (&chld_out, 0, sizeof (output));
+ memset (&chld_err, 0, sizeof (output));
+ result = UNSET;
+ ok (chld_out.lines == 0, "(initialised) Checking stdout is reset");
+ ok (chld_err.lines == 0, "(initialised) Checking stderr is reset");
+ ok (result == UNSET, "(initialised) Checking exit code is reset");
+
+ result = cmd_run (command, &chld_out, &chld_err, 0);
+
+ ok (chld_out.lines == 1,
+ "(string) Check for expected number of stdout lines");
+ ok (chld_err.lines == 0,
+ "(string) Check for expected number of stderr lines");
+ ok (strcmp (chld_out.line[0], "this is test one") == 0,
+ "(string) Check for expected stdout output");
+ ok (result == 0, "(string) Checking exit code");
+
+
+ /* ensure everything is empty again */
+ memset (&chld_out, 0, sizeof (output));
+ memset (&chld_err, 0, sizeof (output));
+ result = UNSET;
+ ok (chld_out.lines == 0, "(initialised) Checking stdout is reset");
+ ok (chld_err.lines == 0, "(initialised) Checking stderr is reset");
+ ok (result == UNSET, "(initialised) Checking exit code is reset");
+
+ /* Pass linefeeds via parameters through - those should be evaluated by echo to give multi line output */
+ command_line[0] = strdup("/bin/echo");
+ command_line[1] = strdup("this is a test via echo\nline two\nit's line 3");
+ command_line[2] = strdup("and (note space between '3' and 'and') $$ will not get evaluated");
+
+ result = cmd_run_array (command_line, &chld_out, &chld_err, 0);
+ ok (chld_out.lines == 3,
+ "(array) Check for expected number of stdout lines");
+ ok (chld_err.lines == 0,
+ "(array) Check for expected number of stderr lines");
+ ok (strcmp (chld_out.line[0], "this is a test via echo") == 0,
+ "(array) Check line 1 for expected stdout output");
+ ok (strcmp (chld_out.line[1], "line two") == 0,
+ "(array) Check line 2 for expected stdout output");
+ ok (strcmp (chld_out.line[2], "it's line 3 and (note space between '3' and 'and') $$ will not get evaluated") == 0,
+ "(array) Check line 3 for expected stdout output");
+ ok (result == 0, "(array) Checking exit code");
+
+
+
+ /* ensure everything is empty again */
+ memset (&chld_out, 0, sizeof (output));
+ memset (&chld_err, 0, sizeof (output));
+ result = UNSET;
+ ok (chld_out.lines == 0, "(initialised) Checking stdout is reset");
+ ok (chld_err.lines == 0, "(initialised) Checking stderr is reset");
+ ok (result == UNSET, "(initialised) Checking exit code is reset");
+
+ command = (char *)malloc(COMMAND_LINE);
+ strcpy(command, "/bin/echo3456 non-existant command");
+ result = cmd_run (command, &chld_out, &chld_err, 0);
+
+ ok (chld_out.lines == 0,
+ "Non existant command, so no output");
+ ok (chld_err.lines == 0,
+ "No stderr either");
+ ok (result == 3, "Get return code 3 (?) for non-existant command");
+
+
+ /* ensure everything is empty again */
+ memset (&chld_out, 0, sizeof (output));
+ memset (&chld_err, 0, sizeof (output));
+ result = UNSET;
+
+ command = (char *)malloc(COMMAND_LINE);
+ strcpy(command, "/bin/grep pattern non-existant-file");
+ result = cmd_run (command, &chld_out, &chld_err, 0);
+
+ ok (chld_out.lines == 0,
+ "Grep returns no stdout when file is missing...");
+ ok (chld_err.lines == 1,
+ "...but does give an error line");
+ ok (strstr(chld_err.line[0],"non-existant-file") != NULL, "And missing filename is in error message");
+ ok (result == 2, "Get return code 2 from grep");
+
+
+
+ return exit_status ();
+}
diff --git a/lib/tests/test_cmd.t b/lib/tests/test_cmd.t
new file mode 100644
index 00000000..4dd54ef9
--- /dev/null
+++ b/lib/tests/test_cmd.t
@@ -0,0 +1,6 @@
+#!/usr/bin/perl
+use Test::More;
+if (! -e "./test_cmd") {
+ plan skip_all => "./test_cmd not compiled - please install tap library to test";
+}
+exec "./test_cmd";