Imported Upstream version 1.5.7
[platform/upstream/libpipeline.git] / tests / basic.c
index d0dd633..7cb3d46 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2010 Colin Watson.
+ * Copyright (C) 2010-2017 Colin Watson.
  *
  * This file is part of libpipeline.
  *
  * USA.
  */
 
+#ifdef HAVE_CONFIG_H
+#  include "config.h"
+#endif
+
+#include <fcntl.h>
+#include <stdlib.h>
 #include <string.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <unistd.h>
+
+#include "dirname.h"
+#include "xalloc.h"
 
 #include "common.h"
 
+const char *program_name = "basic";
+
 START_TEST (test_basic_status)
 {
        pipeline *p;
 
-       p = pipeline_new_command_args ("true", NULL);
-       fail_unless (pipeline_run (p) == 0, "true did not return 0");
-       p = pipeline_new_command_args ("false", NULL);
-       fail_if (pipeline_run (p) == 0, "false returned 0");
+       p = pipeline_new_command_args ("true", (void *) 0);
+       ck_assert_msg (pipeline_run (p) == 0, "true did not return 0");
+       p = pipeline_new_command_args ("false", (void *) 0);
+       ck_assert_msg (pipeline_run (p) != 0, "false returned 0");
 }
 END_TEST
 
@@ -39,27 +53,29 @@ START_TEST (test_basic_args)
        pipeline *p;
        const char *line;
 
-       p = pipeline_new_command_args ("echo", "foo", NULL);
+       p = pipeline_new_command_args ("echo", "foo", (void *) 0);
        pipeline_want_out (p, -1);
-       fail_unless (pipecmd_get_nargs (pipeline_get_command (p, 0)) == 2,
-                    "Number of arguments != 2");
+       ck_assert_msg (pipecmd_get_nargs (pipeline_get_command (p, 0)) == 2,
+                      "Number of arguments != 2");
        pipeline_start (p);
        line = pipeline_readline (p);
-       fail_unless (!strcmp (line, "foo\n"),
-                    "Incorrect output from 'echo foo': '%s'", line);
-       fail_unless (pipeline_wait (p) == 0, "'echo foo' did not return 0");
+       ck_assert_ptr_ne (line, NULL);
+       ck_assert_msg (!strcmp (line, "foo\n"),
+                      "Incorrect output from 'echo foo': '%s'", line);
+       ck_assert_msg (pipeline_wait (p) == 0, "'echo foo' did not return 0");
        pipeline_free (p);
 
-       p = pipeline_new_command_args ("echo", "foo", "bar", NULL);
+       p = pipeline_new_command_args ("echo", "foo", "bar", (void *) 0);
        pipeline_want_out (p, -1);
-       fail_unless (pipecmd_get_nargs (pipeline_get_command (p, 0)) == 3,
-                    "Number of arguments != 3");
+       ck_assert_msg (pipecmd_get_nargs (pipeline_get_command (p, 0)) == 3,
+                      "Number of arguments != 3");
        pipeline_start (p);
        line = pipeline_readline (p);
-       fail_unless (!strcmp (line, "foo bar\n"),
-                    "Incorrect output from 'echo foo bar': '%s'", line);
-       fail_unless (pipeline_wait (p) == 0,
-                    "'echo foo bar' did not return 0");
+       ck_assert_ptr_ne (line, NULL);
+       ck_assert_msg (!strcmp (line, "foo bar\n"),
+                      "Incorrect output from 'echo foo bar': '%s'", line);
+       ck_assert_msg (pipeline_wait (p) == 0,
+                      "'echo foo bar' did not return 0");
        pipeline_free (p);
 }
 END_TEST
@@ -70,16 +86,18 @@ START_TEST (test_basic_pipeline)
        const char *line;
 
        p = pipeline_new ();
-       pipeline_command_args (p, "echo", "foo", NULL);
-       pipeline_command_args (p, "sed", "-e", "s/foo/bar/", NULL);
+       pipeline_command_args (p, "echo", "foo", (void *) 0);
+       pipeline_command_args (p, "sed", "-e", "s/foo/bar/", (void *) 0);
        pipeline_want_out (p, -1);
        pipeline_start (p);
        line = pipeline_readline (p);
-       fail_unless (!strcmp (line, "bar\n"),
-                    "Incorrect output from 'echo foo | sed -e s/foo/bar/': "
-                    "'%s'", line);
-       fail_unless (pipeline_wait (p) == 0,
-                    "'echo foo | sed -e 's/foo/bar/' did not return 0");
+       ck_assert_ptr_ne (line, NULL);
+       ck_assert_msg (!strcmp (line, "bar\n"),
+                      "Incorrect output from 'echo foo | sed -e s/foo/bar/': "
+                      "'%s'",
+                      line);
+       ck_assert_msg (pipeline_wait (p) == 0,
+                      "'echo foo | sed -e 's/foo/bar/' did not return 0");
        pipeline_free (p);
 }
 END_TEST
@@ -91,16 +109,17 @@ START_TEST (test_basic_wait_all)
        int n_statuses;
 
        p = pipeline_new ();
-       pipeline_command_args (p, "sh", "-c", "exit 2", NULL);
-       pipeline_command_args (p, "sh", "-c", "exit 3", NULL);
-       pipeline_command_args (p, "true", NULL);
+       pipeline_command_args (p, SHELL, "-c", "exit 2", (void *) 0);
+       pipeline_command_args (p, SHELL, "-c", "exit 3", (void *) 0);
+       pipeline_command_args (p, "true", (void *) 0);
        pipeline_start (p);
-       fail_unless (pipeline_wait_all (p, &statuses, &n_statuses) == 127);
-       fail_unless (n_statuses == 3);
-       fail_unless (statuses[0] == 2 * 256);
-       fail_unless (statuses[1] == 3 * 256);
-       fail_unless (statuses[2] == 0);
+       ck_assert_int_eq (pipeline_wait_all (p, &statuses, &n_statuses), 127);
+       ck_assert_int_eq (n_statuses, 3);
+       ck_assert_int_eq (statuses[0], 2 * 256);
+       ck_assert_int_eq (statuses[1], 3 * 256);
+       ck_assert_int_eq (statuses[2], 0);
        pipeline_free (p);
+       free (statuses);
 }
 END_TEST
 
@@ -108,35 +127,30 @@ START_TEST (test_basic_setenv)
 {
        pipeline *p;
 
-       p = pipeline_new_command_args ("sh", "-c", "exit $TEST1", NULL);
+       p = pipeline_new_command_args (SHELL, "-c", "exit $TEST1", (void *) 0);
        pipecmd_setenv (pipeline_get_command (p, 0), "TEST1", "10");
-       fail_unless (pipeline_run (p) == 10, "TEST1 not set properly");
+       ck_assert_int_eq (pipeline_run (p), 10);
 }
 END_TEST
 
 START_TEST (test_basic_unsetenv)
 {
        pipeline *p;
-       const char *line;
 
        setenv ("TEST2", "foo", 1);
 
-       p = pipeline_new_command_args ("sh", "-c", "echo $TEST2", NULL);
+       p = pipeline_new_command_args (SHELL, "-c", "echo $TEST2", (void *) 0);
        pipeline_want_out (p, -1);
        pipeline_start (p);
-       line = pipeline_readline (p);
-       fail_unless (!strcmp (line, "foo\n"),
-                    "control returned '%s', expected 'foo\n'", line);
+       ck_assert_str_eq (pipeline_readline (p), "foo\n");
        pipeline_wait (p);
        pipeline_free (p);
 
-       p = pipeline_new_command_args ("sh", "-c", "echo $TEST2", NULL);
+       p = pipeline_new_command_args (SHELL, "-c", "echo $TEST2", (void *) 0);
        pipecmd_unsetenv (pipeline_get_command (p, 0), "TEST2");
        pipeline_want_out (p, -1);
        pipeline_start (p);
-       line = pipeline_readline (p);
-       fail_unless (!strcmp (line, "\n"),
-                    "unsetenv failed: returned '%s', expected '\n'", line);
+       ck_assert_str_eq (pipeline_readline (p), "\n");
        pipeline_wait (p);
        pipeline_free (p);
 }
@@ -145,61 +159,116 @@ END_TEST
 START_TEST (test_basic_clearenv)
 {
        pipeline *p, *p2;
-       const char *line1, *line2;
 
        setenv ("TEST3", "foo", 1);
 
-       p = pipeline_new_command_args ("sh", "-c", "echo $TEST3; echo $TEST4",
-                                      NULL);
+       p = pipeline_new_command_args (SHELL, "-c", "echo $TEST3; echo $TEST4",
+                                      (void *) 0);
        pipeline_want_out (p, -1);
        pipeline_start (p);
-       line1 = pipeline_readline (p);
-       fail_unless (!strcmp (line1, "foo\n"),
-                    "control returned first line '%s', expected 'foo\n'",
-                    line1);
-       line2 = pipeline_readline (p);
-       fail_unless (!strcmp (line2, "\n"),
-                    "control returned second line '%s', expected '\n'",
-                    line2);
+       ck_assert_str_eq (pipeline_readline (p), "foo\n");
+       ck_assert_str_eq (pipeline_readline (p), "\n");
        pipeline_wait (p);
 
        pipecmd_clearenv (pipeline_get_command (p, 0));
        pipeline_start (p);
-       line1 = pipeline_readline (p);
-       fail_unless (!strcmp (line1, "\n"),
-                    "clearenv failed: returned first line '%s', expected '\n'",
-                    line1);
-       line2 = pipeline_readline (p);
-       fail_unless (!strcmp (line2, "\n"),
-                    "clearenv returned second line '%s', expected '\n'",
-                    line2);
+       ck_assert_str_eq (pipeline_readline (p), "\n");
+       ck_assert_str_eq (pipeline_readline (p), "\n");
        pipeline_wait (p);
 
        pipecmd_setenv (pipeline_get_command (p, 0), "TEST4", "bar");
        pipeline_start (p);
-       line1 = pipeline_readline (p);
-       fail_unless (!strcmp (line1, "\n"),
-                    "clearenv+setenv failed: returned first line '%s', expected '\n'",
-                    line1);
-       line2 = pipeline_readline (p);
-       fail_unless (!strcmp (line2, "bar\n"),
-                    "clearenv+setenv returned second line '%s', expected 'bar\n'",
-                    line2);
+       ck_assert_str_eq (pipeline_readline (p), "\n");
+       ck_assert_str_eq (pipeline_readline (p), "bar\n");
        pipeline_wait (p);
 
        p2 = pipeline_new ();
        pipeline_command (p2, pipecmd_dup (pipeline_get_command (p, 0)));
        pipeline_want_out (p2, -1);
        pipeline_start (p2);
-       line1 = pipeline_readline (p2);
-       fail_unless (!strcmp (line1, "\n"),
-                    "clearenv+setenv+dup failed: returned first line '%s', expected '\n'",
-                    line1);
-       line2 = pipeline_readline (p2);
-       fail_unless (!strcmp (line2, "bar\n"),
-                    "clearenv+setenv+dup returned second line '%s', expected 'bar\n'",
-                    line2);
+       ck_assert_str_eq (pipeline_readline (p2), "\n");
+       ck_assert_str_eq (pipeline_readline (p2), "bar\n");
        pipeline_wait (p2);
+       pipeline_free (p2);
+       pipeline_free (p);
+}
+END_TEST
+
+START_TEST (test_basic_chdir)
+{
+       pipeline *p;
+       const char *raw_line;
+       char *line, *end;
+       char *child_base, *expected_base;
+
+       p = pipeline_new_command_args ("pwd", (void *) 0);
+       pipecmd_chdir (pipeline_get_command (p, 0), temp_dir);
+       pipeline_want_out (p, -1);
+       pipeline_start (p);
+       raw_line = xstrdup (pipeline_readline (p));
+       ck_assert_ptr_ne (raw_line, NULL);
+       line = xstrdup (raw_line);
+       end = line + strlen (line);
+       if (end > line && *(end - 1) == '\n')
+               *(end - 1) = '\0';
+       child_base = base_name (line);
+       expected_base = base_name (temp_dir);
+       ck_assert_str_eq (child_base, expected_base);
+       free (expected_base);
+       free (child_base);
+       free (line);
+       pipeline_wait (p);
+       pipeline_free (p);
+}
+END_TEST
+
+START_TEST (test_basic_fchdir)
+{
+       pipeline *p;
+       int temp_dir_fd;
+       const char *raw_line;
+       char *line, *end;
+       char *child_base, *expected_base;
+
+       p = pipeline_new_command_args ("pwd", (void *) 0);
+       temp_dir_fd = open (temp_dir, O_RDONLY | O_DIRECTORY);
+       ck_assert_int_ge (temp_dir_fd, 0);
+       pipecmd_fchdir (pipeline_get_command (p, 0), temp_dir_fd);
+       pipeline_want_out (p, -1);
+       pipeline_start (p);
+       raw_line = xstrdup (pipeline_readline (p));
+       ck_assert_ptr_ne (raw_line, NULL);
+       line = xstrdup (raw_line);
+       end = line + strlen (line);
+       if (end > line && *(end - 1) == '\n')
+               *(end - 1) = '\0';
+       child_base = base_name (line);
+       expected_base = base_name (temp_dir);
+       ck_assert_str_eq (child_base, expected_base);
+       free (expected_base);
+       free (child_base);
+       free (line);
+       close (temp_dir_fd);
+       pipeline_wait (p);
+       pipeline_free (p);
+}
+END_TEST
+
+/* This is of course better done using pipecmd_setenv, but setting an
+ * environment variable makes for an easy test.
+ */
+static void pre_exec (void *data _GL_UNUSED)
+{
+       setenv ("TEST1", "10", 1);
+}
+
+START_TEST (test_basic_pre_exec)
+{
+       pipeline *p;
+
+       p = pipeline_new_command_args (SHELL, "-c", "exit $TEST1", (void *) 0);
+       pipecmd_pre_exec (pipeline_get_command (p, 0), pre_exec, NULL, NULL);
+       ck_assert_msg (pipeline_run (p) == 10, "TEST1 not set properly");
 }
 END_TEST
 
@@ -207,25 +276,23 @@ START_TEST (test_basic_sequence)
 {
        pipeline *p;
        pipecmd *cmd1, *cmd2, *cmd3, *seq;
-       const char *line;
 
        p = pipeline_new ();
-       cmd1 = pipecmd_new_args ("echo", "foo", NULL);
-       cmd2 = pipecmd_new_args ("echo", "bar", NULL);
-       cmd3 = pipecmd_new_args ("echo", "baz", NULL);
-       seq = pipecmd_new_sequence ("echo*3", cmd1, cmd2, cmd3, NULL);
+       cmd1 = pipecmd_new_args ("echo", "foo", (void *) 0);
+       cmd2 = pipecmd_new_args ("echo", "bar", (void *) 0);
+       cmd3 = pipecmd_new_args ("echo", "baz", (void *) 0);
+       seq = pipecmd_new_sequence ("echo*3", cmd1, cmd2, cmd3, (void *) 0);
        pipeline_command (p, seq);
-       pipeline_command_args (p, "xargs", NULL);
+       pipeline_command_args (p, "xargs", (void *) 0);
        pipeline_want_out (p, -1);
        pipeline_start (p);
-       line = pipeline_readline (p);
-       fail_unless (!strcmp (line, "foo bar baz\n"));
+       ck_assert_str_eq (pipeline_readline (p), "foo bar baz\n");
        pipeline_wait (p);
        pipeline_free (p);
 }
 END_TEST
 
-Suite *basic_suite (void)
+static Suite *basic_suite (void)
 {
        Suite *s = suite_create ("Basic");
 
@@ -236,6 +303,11 @@ Suite *basic_suite (void)
        TEST_CASE (s, basic, setenv);
        TEST_CASE (s, basic, unsetenv);
        TEST_CASE (s, basic, clearenv);
+       TEST_CASE (s, basic, pre_exec);
+       TEST_CASE_WITH_FIXTURE (s, basic, chdir, temp_dir_setup,
+                               temp_dir_teardown);
+       TEST_CASE_WITH_FIXTURE (s, basic, fchdir, temp_dir_setup,
+                               temp_dir_teardown);
        TEST_CASE (s, basic, sequence);
 
        return s;