t_spawn.c revision 1.2 1 1.2 snj /* $NetBSD: t_spawn.c,v 1.2 2014/10/18 08:33:30 snj Exp $ */
2 1.1 martin
3 1.1 martin /*-
4 1.1 martin * Copyright (c) 2012 The NetBSD Foundation, Inc.
5 1.1 martin * All rights reserved.
6 1.1 martin *
7 1.1 martin * This code is derived from software contributed to The NetBSD Foundation
8 1.1 martin * by Charles Zhang <charles (at) NetBSD.org> and
9 1.1 martin * Martin Husemann <martin (at) NetBSD.org>.
10 1.1 martin *
11 1.1 martin * Redistribution and use in source and binary forms, with or without
12 1.1 martin * modification, are permitted provided that the following conditions
13 1.1 martin * are met:
14 1.1 martin * 1. Redistributions of source code must retain the above copyright
15 1.1 martin * notice, this list of conditions and the following disclaimer.
16 1.1 martin * 2. Redistributions in binary form must reproduce the above copyright
17 1.1 martin * notice, this list of conditions and the following disclaimer in the
18 1.1 martin * documentation and/or other materials provided with the distribution.
19 1.1 martin *
20 1.1 martin * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 1.1 martin * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 1.1 martin * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 1.1 martin * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 1.1 martin * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 1.1 martin * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 1.1 martin * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 1.1 martin * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 1.1 martin * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 1.1 martin * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 1.1 martin * POSSIBILITY OF SUCH DAMAGE.
31 1.1 martin */
32 1.1 martin
33 1.1 martin
34 1.1 martin #include <atf-c.h>
35 1.1 martin #include <spawn.h>
36 1.1 martin #include <string.h>
37 1.1 martin #include <stdio.h>
38 1.1 martin #include <stdlib.h>
39 1.1 martin #include <errno.h>
40 1.1 martin #include <sys/wait.h>
41 1.1 martin
42 1.1 martin ATF_TC(t_spawn_ls);
43 1.1 martin
44 1.1 martin ATF_TC_HEAD(t_spawn_ls, tc)
45 1.1 martin {
46 1.1 martin atf_tc_set_md_var(tc, "descr",
47 1.1 martin "Tests a simple posix_spawn executing /bin/ls");
48 1.1 martin }
49 1.1 martin
50 1.1 martin ATF_TC_BODY(t_spawn_ls, tc)
51 1.1 martin {
52 1.1 martin char * const args[] = { __UNCONST("ls"), __UNCONST("-la"), NULL };
53 1.1 martin int err;
54 1.1 martin
55 1.1 martin err = posix_spawn(NULL, "/bin/ls", NULL, NULL, args, NULL);
56 1.1 martin ATF_REQUIRE(err == 0);
57 1.1 martin }
58 1.1 martin
59 1.1 martin ATF_TC(t_spawnp_ls);
60 1.1 martin
61 1.1 martin ATF_TC_HEAD(t_spawnp_ls, tc)
62 1.1 martin {
63 1.1 martin atf_tc_set_md_var(tc, "descr",
64 1.1 martin "Tests a simple posix_spawnp executing ls via $PATH");
65 1.1 martin }
66 1.1 martin
67 1.1 martin ATF_TC_BODY(t_spawnp_ls, tc)
68 1.1 martin {
69 1.1 martin char * const args[] = { __UNCONST("ls"), __UNCONST("-la"), NULL };
70 1.1 martin int err;
71 1.1 martin
72 1.1 martin err = posix_spawnp(NULL, "ls", NULL, NULL, args, NULL);
73 1.1 martin ATF_REQUIRE(err == 0);
74 1.1 martin }
75 1.1 martin
76 1.1 martin ATF_TC(t_spawn_zero);
77 1.1 martin
78 1.1 martin ATF_TC_HEAD(t_spawn_zero, tc)
79 1.1 martin {
80 1.1 martin atf_tc_set_md_var(tc, "descr",
81 1.1 martin "posix_spawn an invalid binary");
82 1.1 martin }
83 1.1 martin
84 1.1 martin ATF_TC_BODY(t_spawn_zero, tc)
85 1.1 martin {
86 1.1 martin char buf[FILENAME_MAX];
87 1.1 martin char * const args[] = { __UNCONST("h_zero"), NULL };
88 1.1 martin int err;
89 1.1 martin
90 1.1 martin snprintf(buf, sizeof buf, "%s/h_zero", atf_tc_get_config_var(tc, "srcdir"));
91 1.1 martin err = posix_spawn(NULL, buf, NULL, NULL, args, NULL);
92 1.1 martin ATF_REQUIRE_MSG(err == ENOEXEC, "expected error %d, got %d when spawning %s", ENOEXEC, err, buf);
93 1.1 martin }
94 1.1 martin
95 1.1 martin ATF_TC(t_spawn_missing);
96 1.1 martin
97 1.1 martin ATF_TC_HEAD(t_spawn_missing, tc)
98 1.1 martin {
99 1.1 martin atf_tc_set_md_var(tc, "descr",
100 1.1 martin "posix_spawn a non existant binary");
101 1.1 martin }
102 1.1 martin
103 1.1 martin ATF_TC_BODY(t_spawn_missing, tc)
104 1.1 martin {
105 1.1 martin char buf[FILENAME_MAX];
106 1.1 martin char * const args[] = { __UNCONST("h_nonexist"), NULL };
107 1.1 martin int err;
108 1.1 martin
109 1.1 martin snprintf(buf, sizeof buf, "%s/h_nonexist",
110 1.1 martin atf_tc_get_config_var(tc, "srcdir"));
111 1.1 martin err = posix_spawn(NULL, buf, NULL, NULL, args, NULL);
112 1.1 martin ATF_REQUIRE_MSG(err == ENOENT, "expected error %d, got %d when spawning %s", ENOENT, err, buf);
113 1.1 martin }
114 1.1 martin
115 1.1 martin ATF_TC(t_spawn_nonexec);
116 1.1 martin
117 1.1 martin ATF_TC_HEAD(t_spawn_nonexec, tc)
118 1.1 martin {
119 1.1 martin atf_tc_set_md_var(tc, "descr",
120 1.1 martin "posix_spawn a script with non existing interpreter");
121 1.1 martin }
122 1.1 martin
123 1.1 martin ATF_TC_BODY(t_spawn_nonexec, tc)
124 1.1 martin {
125 1.1 martin char buf[FILENAME_MAX];
126 1.1 martin char * const args[] = { __UNCONST("h_nonexec"), NULL };
127 1.1 martin int err;
128 1.1 martin
129 1.1 martin snprintf(buf, sizeof buf, "%s/h_nonexec",
130 1.1 martin atf_tc_get_config_var(tc, "srcdir"));
131 1.1 martin err = posix_spawn(NULL, buf, NULL, NULL, args, NULL);
132 1.1 martin ATF_REQUIRE_MSG(err == ENOENT, "expected error %d, got %d when spawning %s", ENOENT, err, buf);
133 1.1 martin }
134 1.1 martin
135 1.1 martin ATF_TC(t_spawn_child);
136 1.1 martin
137 1.1 martin ATF_TC_HEAD(t_spawn_child, tc)
138 1.1 martin {
139 1.1 martin atf_tc_set_md_var(tc, "descr",
140 1.2 snj "posix_spawn a child and get its return code");
141 1.1 martin }
142 1.1 martin
143 1.1 martin ATF_TC_BODY(t_spawn_child, tc)
144 1.1 martin {
145 1.1 martin char buf[FILENAME_MAX];
146 1.1 martin char * const args0[] = { __UNCONST("h_spawn"), __UNCONST("0"), NULL };
147 1.1 martin char * const args1[] = { __UNCONST("h_spawn"), __UNCONST("1"), NULL };
148 1.1 martin char * const args7[] = { __UNCONST("h_spawn"), __UNCONST("7"), NULL };
149 1.1 martin int err, status;
150 1.1 martin pid_t pid;
151 1.1 martin
152 1.1 martin snprintf(buf, sizeof buf, "%s/h_spawn",
153 1.1 martin atf_tc_get_config_var(tc, "srcdir"));
154 1.1 martin
155 1.1 martin err = posix_spawn(&pid, buf, NULL, NULL, args0, NULL);
156 1.1 martin ATF_REQUIRE(err == 0);
157 1.1 martin ATF_REQUIRE(pid > 0);
158 1.1 martin waitpid(pid, &status, 0);
159 1.1 martin ATF_REQUIRE(WIFEXITED(status) && WEXITSTATUS(status) == 0);
160 1.1 martin
161 1.1 martin err = posix_spawn(&pid, buf, NULL, NULL, args1, NULL);
162 1.1 martin ATF_REQUIRE(err == 0);
163 1.1 martin ATF_REQUIRE(pid > 0);
164 1.1 martin waitpid(pid, &status, 0);
165 1.1 martin ATF_REQUIRE(WIFEXITED(status) && WEXITSTATUS(status) == 1);
166 1.1 martin
167 1.1 martin err = posix_spawn(&pid, buf, NULL, NULL, args7, NULL);
168 1.1 martin ATF_REQUIRE(err == 0);
169 1.1 martin ATF_REQUIRE(pid > 0);
170 1.1 martin waitpid(pid, &status, 0);
171 1.1 martin ATF_REQUIRE(WIFEXITED(status) && WEXITSTATUS(status) == 7);
172 1.1 martin }
173 1.1 martin
174 1.1 martin ATF_TP_ADD_TCS(tp)
175 1.1 martin {
176 1.1 martin ATF_TP_ADD_TC(tp, t_spawn_ls);
177 1.1 martin ATF_TP_ADD_TC(tp, t_spawnp_ls);
178 1.1 martin ATF_TP_ADD_TC(tp, t_spawn_zero);
179 1.1 martin ATF_TP_ADD_TC(tp, t_spawn_missing);
180 1.1 martin ATF_TP_ADD_TC(tp, t_spawn_nonexec);
181 1.1 martin ATF_TP_ADD_TC(tp, t_spawn_child);
182 1.1 martin
183 1.1 martin return atf_no_error();
184 1.1 martin }
185