Imported Upstream version 1.5.7
[platform/upstream/libpipeline.git] / tests / exec.c
1 /*
2  * Copyright (C) 2010, 2012 Colin Watson.
3  *
4  * This file is part of libpipeline.
5  *
6  * libpipeline is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or (at
9  * your option) any later version.
10  *
11  * libpipeline is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with libpipeline; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301
19  * USA.
20  */
21
22 #ifdef HAVE_CONFIG_H
23 #  include "config.h"
24 #endif
25
26 #include <errno.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <sys/types.h>
30 #include <sys/wait.h>
31 #include <unistd.h>
32
33 #include "xvasprintf.h"
34
35 #include "common.h"
36
37 const char *program_name = "exec";
38
39 START_TEST (test_exec_process)
40 {
41         int i;
42
43         for (i = 0; i < 3; ++i) {
44                 pipecmd *cmd;
45                 pid_t pid;
46                 int status;
47
48                 if (i < 2) {
49                         char *arg;
50
51                         cmd = pipecmd_new_args (SHELL, "-c", (void *) 0);
52                         arg = xasprintf ("exit %d", i);
53                         pipecmd_arg (cmd, arg);
54                         free (arg);
55                 } else {
56                         cmd = pipecmd_new ("nonexistent command");
57                         pipecmd_discard_err (cmd, 1);
58                 }
59
60                 pid = fork ();
61                 if (pid < 0) {
62                         ck_abort_msg ("fork failed: %s", strerror (errno));
63                         return;
64                 }
65                 if (pid == 0)
66                         pipecmd_exec (cmd);
67
68                 while (waitpid (pid, &status, 0) < 0) {
69                         if (errno == EINTR)
70                                 continue;
71                         ck_abort_msg ("waitpid failed: %s", strerror (errno));
72                         return;
73                 }
74
75                 ck_assert_int_ne (WIFEXITED (status), 0);
76                 if (i < 2)
77                         ck_assert_int_eq (WEXITSTATUS (status), i);
78                 else
79                         ck_assert_int_ne (WEXITSTATUS (status), 0);
80
81                 pipecmd_free (cmd);
82         }
83 }
84 END_TEST
85
86 static void exit_helper (void *data)
87 {
88         exit (*(int *) data);
89 }
90
91 START_TEST (test_exec_function)
92 {
93         int i;
94
95         for (i = 0; i < 2; ++i) {
96                 pipecmd *cmd;
97                 pid_t pid;
98                 int status;
99
100                 cmd = pipecmd_new_function ("exit_helper", exit_helper, NULL,
101                                             &i);
102
103                 pid = fork ();
104                 if (pid < 0) {
105                         ck_abort_msg ("fork failed: %s", strerror (errno));
106                         return;
107                 }
108                 if (pid == 0)
109                         pipecmd_exec (cmd);
110
111                 while (waitpid (pid, &status, 0) < 0) {
112                         if (errno == EINTR)
113                                 continue;
114                         ck_abort_msg ("waitpid failed: %s", strerror (errno));
115                         return;
116                 }
117
118                 ck_assert_int_ne (WIFEXITED (status), 0);
119                 ck_assert_int_eq (WEXITSTATUS (status), i);
120
121                 pipecmd_free (cmd);
122         }
123 }
124 END_TEST
125
126 static Suite *exec_suite (void)
127 {
128         Suite *s = suite_create ("Exec");
129
130         TEST_CASE (s, exec, process);
131         TEST_CASE (s, exec, function);
132
133         return s;
134 }
135
136 MAIN (exec)