2 // Automated Testing Framework (atf)
4 // Copyright (c) 2008 The NetBSD Foundation, Inc.
5 // All rights reserved.
7 // Redistribution and use in source and binary forms, with or without
8 // modification, are permitted provided that the following conditions
10 // 1. Redistributions of source code must retain the above copyright
11 // notice, this list of conditions and the following disclaimer.
12 // 2. Redistributions in binary form must reproduce the above copyright
13 // notice, this list of conditions and the following disclaimer in the
14 // documentation and/or other materials provided with the distribution.
16 // THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
17 // CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
18 // INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 // IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
21 // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
23 // GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25 // IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26 // OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27 // IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 #include "../macros.hpp"
35 #include "process.hpp"
36 #include "test_helpers.hpp"
38 // TODO: Testing the fork function is a huge task and I'm afraid of
39 // copy/pasting tons of stuff from the C version. I'd rather not do that
40 // until some code can be shared, which cannot happen until the C++ binding
41 // is cleaned by a fair amount. Instead... just rely (at the moment) on
42 // the system tests for the tools using this module.
44 // ------------------------------------------------------------------------
45 // Auxiliary functions.
46 // ------------------------------------------------------------------------
50 array_size(const char* const* array
)
54 for (const char* const* ptr
= array
; *ptr
!= NULL
; ptr
++)
62 exec_process_helpers(const atf::tests::tc
& tc
, const char* helper_name
)
64 using atf::process::exec
;
66 std::vector
< std::string
> argv
;
67 argv
.push_back(get_process_helpers_path(tc
).leaf_name());
68 argv
.push_back(helper_name
);
70 return exec(get_process_helpers_path(tc
),
71 atf::process::argv_array(argv
),
72 atf::process::stream_inherit(),
73 atf::process::stream_inherit());
76 // ------------------------------------------------------------------------
77 // Tests for the "argv_array" type.
78 // ------------------------------------------------------------------------
80 ATF_TEST_CASE(argv_array_init_carray
);
81 ATF_TEST_CASE_HEAD(argv_array_init_carray
)
83 set_md_var("descr", "Tests that argv_array is correctly constructed "
84 "from a C-style array of strings");
86 ATF_TEST_CASE_BODY(argv_array_init_carray
)
89 const char* const carray
[] = { NULL
};
90 atf::process::argv_array
argv(carray
);
92 ATF_REQUIRE_EQ(argv
.size(), 0);
96 const char* const carray
[] = { "arg0", NULL
};
97 atf::process::argv_array
argv(carray
);
99 ATF_REQUIRE_EQ(argv
.size(), 1);
100 ATF_REQUIRE(std::strcmp(argv
[0], carray
[0]) == 0);
104 const char* const carray
[] = { "arg0", "arg1", "arg2", NULL
};
105 atf::process::argv_array
argv(carray
);
107 ATF_REQUIRE_EQ(argv
.size(), 3);
108 ATF_REQUIRE(std::strcmp(argv
[0], carray
[0]) == 0);
109 ATF_REQUIRE(std::strcmp(argv
[1], carray
[1]) == 0);
110 ATF_REQUIRE(std::strcmp(argv
[2], carray
[2]) == 0);
114 ATF_TEST_CASE(argv_array_init_col
);
115 ATF_TEST_CASE_HEAD(argv_array_init_col
)
117 set_md_var("descr", "Tests that argv_array is correctly constructed "
118 "from a string collection");
120 ATF_TEST_CASE_BODY(argv_array_init_col
)
123 std::vector
< std::string
> col
;
124 atf::process::argv_array
argv(col
);
126 ATF_REQUIRE_EQ(argv
.size(), 0);
130 std::vector
< std::string
> col
;
131 col
.push_back("arg0");
132 atf::process::argv_array
argv(col
);
134 ATF_REQUIRE_EQ(argv
.size(), 1);
135 ATF_REQUIRE_EQ(argv
[0], col
[0]);
139 std::vector
< std::string
> col
;
140 col
.push_back("arg0");
141 col
.push_back("arg1");
142 col
.push_back("arg2");
143 atf::process::argv_array
argv(col
);
145 ATF_REQUIRE_EQ(argv
.size(), 3);
146 ATF_REQUIRE_EQ(argv
[0], col
[0]);
147 ATF_REQUIRE_EQ(argv
[1], col
[1]);
148 ATF_REQUIRE_EQ(argv
[2], col
[2]);
152 ATF_TEST_CASE(argv_array_init_empty
);
153 ATF_TEST_CASE_HEAD(argv_array_init_empty
)
155 set_md_var("descr", "Tests that argv_array is correctly constructed "
156 "by the default constructor");
158 ATF_TEST_CASE_BODY(argv_array_init_empty
)
160 atf::process::argv_array argv
;
162 ATF_REQUIRE_EQ(argv
.size(), 0);
165 ATF_TEST_CASE(argv_array_init_varargs
);
166 ATF_TEST_CASE_HEAD(argv_array_init_varargs
)
168 set_md_var("descr", "Tests that argv_array is correctly constructed "
169 "from a variable list of arguments");
171 ATF_TEST_CASE_BODY(argv_array_init_varargs
)
174 atf::process::argv_array
argv("arg0", NULL
);
176 ATF_REQUIRE_EQ(argv
.size(), 1);
177 ATF_REQUIRE_EQ(argv
[0], std::string("arg0"));
181 atf::process::argv_array
argv("arg0", "arg1", "arg2", NULL
);
183 ATF_REQUIRE_EQ(argv
.size(), 3);
184 ATF_REQUIRE_EQ(argv
[0], std::string("arg0"));
185 ATF_REQUIRE_EQ(argv
[1], std::string("arg1"));
186 ATF_REQUIRE_EQ(argv
[2], std::string("arg2"));
190 ATF_TEST_CASE(argv_array_assign
);
191 ATF_TEST_CASE_HEAD(argv_array_assign
)
193 set_md_var("descr", "Tests that assigning an argv_array works");
195 ATF_TEST_CASE_BODY(argv_array_assign
)
197 using atf::process::argv_array
;
199 const char* const carray1
[] = { "arg1", NULL
};
200 const char* const carray2
[] = { "arg1", "arg2", NULL
};
202 std::auto_ptr
< argv_array
> argv1(new argv_array(carray1
));
203 std::auto_ptr
< argv_array
> argv2(new argv_array(carray2
));
206 ATF_REQUIRE_EQ(argv2
->size(), argv1
->size());
207 ATF_REQUIRE(std::strcmp((*argv2
)[0], (*argv1
)[0]) == 0);
209 ATF_REQUIRE(argv2
->exec_argv() != argv1
->exec_argv());
212 const char* const* eargv2
= argv2
->exec_argv();
213 ATF_REQUIRE(std::strcmp(eargv2
[0], carray1
[0]) == 0);
214 ATF_REQUIRE_EQ(eargv2
[1], static_cast< const char* >(NULL
));
220 ATF_TEST_CASE(argv_array_copy
);
221 ATF_TEST_CASE_HEAD(argv_array_copy
)
223 set_md_var("descr", "Tests that copying an argv_array constructed from "
224 "a C-style array of strings works");
226 ATF_TEST_CASE_BODY(argv_array_copy
)
228 using atf::process::argv_array
;
230 const char* const carray
[] = { "arg0", NULL
};
232 std::auto_ptr
< argv_array
> argv1(new argv_array(carray
));
233 std::auto_ptr
< argv_array
> argv2(new argv_array(*argv1
));
235 ATF_REQUIRE_EQ(argv2
->size(), argv1
->size());
236 ATF_REQUIRE(std::strcmp((*argv2
)[0], (*argv1
)[0]) == 0);
238 ATF_REQUIRE(argv2
->exec_argv() != argv1
->exec_argv());
241 const char* const* eargv2
= argv2
->exec_argv();
242 ATF_REQUIRE(std::strcmp(eargv2
[0], carray
[0]) == 0);
243 ATF_REQUIRE_EQ(eargv2
[1], static_cast< const char* >(NULL
));
249 ATF_TEST_CASE(argv_array_exec_argv
);
250 ATF_TEST_CASE_HEAD(argv_array_exec_argv
)
252 set_md_var("descr", "Tests that the exec argv provided by an argv_array "
255 ATF_TEST_CASE_BODY(argv_array_exec_argv
)
257 using atf::process::argv_array
;
261 const char* const* eargv
= argv
.exec_argv();
262 ATF_REQUIRE_EQ(array_size(eargv
), 0);
263 ATF_REQUIRE_EQ(eargv
[0], static_cast< const char* >(NULL
));
267 const char* const carray
[] = { "arg0", NULL
};
268 argv_array
argv(carray
);
269 const char* const* eargv
= argv
.exec_argv();
270 ATF_REQUIRE_EQ(array_size(eargv
), 1);
271 ATF_REQUIRE(std::strcmp(eargv
[0], "arg0") == 0);
272 ATF_REQUIRE_EQ(eargv
[1], static_cast< const char* >(NULL
));
276 std::vector
< std::string
> col
;
277 col
.push_back("arg0");
278 argv_array
argv(col
);
279 const char* const* eargv
= argv
.exec_argv();
280 ATF_REQUIRE_EQ(array_size(eargv
), 1);
281 ATF_REQUIRE(std::strcmp(eargv
[0], "arg0") == 0);
282 ATF_REQUIRE_EQ(eargv
[1], static_cast< const char* >(NULL
));
286 ATF_TEST_CASE(argv_array_iter
);
287 ATF_TEST_CASE_HEAD(argv_array_iter
)
289 set_md_var("descr", "Tests that an argv_array can be iterated");
291 ATF_TEST_CASE_BODY(argv_array_iter
)
293 using atf::process::argv_array
;
295 std::vector
< std::string
> vector
;
296 vector
.push_back("arg0");
297 vector
.push_back("arg1");
298 vector
.push_back("arg2");
300 argv_array
argv(vector
);
301 ATF_REQUIRE_EQ(argv
.size(), 3);
302 std::vector
< std::string
>::size_type pos
= 0;
303 for (argv_array::const_iterator iter
= argv
.begin(); iter
!= argv
.end();
305 ATF_REQUIRE_EQ(*iter
, vector
[pos
]);
310 // ------------------------------------------------------------------------
311 // Tests cases for the free functions.
312 // ------------------------------------------------------------------------
314 ATF_TEST_CASE(exec_failure
);
315 ATF_TEST_CASE_HEAD(exec_failure
)
317 set_md_var("descr", "Tests execing a command that reports failure");
319 ATF_TEST_CASE_BODY(exec_failure
)
321 const atf::process::status s
= exec_process_helpers(*this, "exit-failure");
322 ATF_REQUIRE(s
.exited());
323 ATF_REQUIRE_EQ(s
.exitstatus(), EXIT_FAILURE
);
326 ATF_TEST_CASE(exec_success
);
327 ATF_TEST_CASE_HEAD(exec_success
)
329 set_md_var("descr", "Tests execing a command that reports success");
331 ATF_TEST_CASE_BODY(exec_success
)
333 const atf::process::status s
= exec_process_helpers(*this, "exit-success");
334 ATF_REQUIRE(s
.exited());
335 ATF_REQUIRE_EQ(s
.exitstatus(), EXIT_SUCCESS
);
338 // ------------------------------------------------------------------------
340 // ------------------------------------------------------------------------
342 ATF_INIT_TEST_CASES(tcs
)
344 // Add the test cases for the "argv_array" type.
345 ATF_ADD_TEST_CASE(tcs
, argv_array_assign
);
346 ATF_ADD_TEST_CASE(tcs
, argv_array_copy
);
347 ATF_ADD_TEST_CASE(tcs
, argv_array_exec_argv
);
348 ATF_ADD_TEST_CASE(tcs
, argv_array_init_carray
);
349 ATF_ADD_TEST_CASE(tcs
, argv_array_init_col
);
350 ATF_ADD_TEST_CASE(tcs
, argv_array_init_empty
);
351 ATF_ADD_TEST_CASE(tcs
, argv_array_init_varargs
);
352 ATF_ADD_TEST_CASE(tcs
, argv_array_iter
);
354 // Add the test cases for the free functions.
355 ATF_ADD_TEST_CASE(tcs
, exec_failure
);
356 ATF_ADD_TEST_CASE(tcs
, exec_success
);