2018-11-12 09:48:15 +00:00
|
|
|
#include <assert.h>
|
2022-01-16 15:42:07 +00:00
|
|
|
#include <errno.h>
|
2018-11-12 09:48:15 +00:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <spawn.h>
|
|
|
|
#include <stdio.h>
|
2021-05-21 17:59:30 +00:00
|
|
|
#include <stdlib.h>
|
2018-11-12 09:48:15 +00:00
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/wait.h>
|
|
|
|
|
2022-01-16 15:42:07 +00:00
|
|
|
#define TESTDIR "/bar/baz"
|
2018-11-12 09:48:15 +00:00
|
|
|
#define TESTPATH "/foo/bar/test"
|
2021-09-03 20:03:38 +00:00
|
|
|
#define SUBTEST "./test sub"
|
2018-11-12 09:48:15 +00:00
|
|
|
|
|
|
|
extern char **environ;
|
|
|
|
|
|
|
|
void test_spawn(void) {
|
|
|
|
pid_t pid;
|
|
|
|
int ret;
|
|
|
|
posix_spawn_file_actions_t file_actions;
|
|
|
|
char *argv[] = {"true", NULL};
|
|
|
|
|
|
|
|
assert(posix_spawn_file_actions_init(&file_actions) == 0);
|
|
|
|
|
|
|
|
ret = posix_spawn(&pid, TESTPATH, &file_actions, NULL, argv, environ);
|
|
|
|
|
|
|
|
assert(ret == 0);
|
|
|
|
assert(waitpid(pid, NULL, 0) != -1);
|
|
|
|
}
|
|
|
|
|
|
|
|
void test_execv(void) {
|
|
|
|
char *argv[] = {"true", NULL};
|
|
|
|
assert(execv(TESTPATH, argv) == 0);
|
|
|
|
}
|
|
|
|
|
2021-05-21 17:59:30 +00:00
|
|
|
void test_system(void) {
|
|
|
|
assert(system(TESTPATH) == 0);
|
|
|
|
}
|
|
|
|
|
2021-09-03 20:03:38 +00:00
|
|
|
void test_subprocess(void) {
|
|
|
|
assert(system(SUBTEST) == 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char *argv[])
|
2018-11-12 09:48:15 +00:00
|
|
|
{
|
|
|
|
FILE *testfp;
|
|
|
|
int testfd;
|
|
|
|
struct stat testsb;
|
|
|
|
|
|
|
|
testfp = fopen(TESTPATH, "r");
|
|
|
|
assert(testfp != NULL);
|
|
|
|
fclose(testfp);
|
|
|
|
|
|
|
|
testfd = open(TESTPATH, O_RDONLY);
|
|
|
|
assert(testfd != -1);
|
|
|
|
close(testfd);
|
|
|
|
|
|
|
|
assert(access(TESTPATH, X_OK) == 0);
|
|
|
|
|
|
|
|
assert(stat(TESTPATH, &testsb) != -1);
|
|
|
|
|
2022-01-16 15:42:07 +00:00
|
|
|
assert(mkdir(TESTDIR "/dir-mkdir", 0777) == 0);
|
|
|
|
assert(unlink(TESTDIR "/dir-mkdir") == -1); // it's a directory!
|
|
|
|
#ifndef __APPLE__
|
|
|
|
assert(errno == EISDIR);
|
|
|
|
#endif
|
|
|
|
assert(rmdir(TESTDIR "/dir-mkdir") == 0);
|
|
|
|
assert(unlink(TESTDIR "/dir-mkdir") == -1);
|
|
|
|
assert(errno == ENOENT);
|
|
|
|
|
|
|
|
assert(mkdirat(123, TESTDIR "/dir-mkdirat", 0777) == 0);
|
|
|
|
assert(unlinkat(123, TESTDIR "/dir-mkdirat", 0) == -1); // it's a directory!
|
|
|
|
#ifndef __APPLE__
|
|
|
|
assert(errno == EISDIR);
|
|
|
|
#endif
|
|
|
|
assert(unlinkat(123, TESTDIR "/dir-mkdirat", AT_REMOVEDIR) == 0);
|
|
|
|
|
2018-11-12 09:48:15 +00:00
|
|
|
test_spawn();
|
2021-05-21 17:59:30 +00:00
|
|
|
test_system();
|
2021-09-03 20:03:38 +00:00
|
|
|
|
|
|
|
// Only run subprocess if no arguments are given
|
|
|
|
// as the subprocess will be called without argument
|
|
|
|
// otherwise we will have infinite recursion
|
|
|
|
if (argc == 1) {
|
|
|
|
test_subprocess();
|
|
|
|
}
|
|
|
|
|
2018-11-12 09:48:15 +00:00
|
|
|
test_execv();
|
|
|
|
|
|
|
|
/* If all goes well, this is never reached because test_execv() replaces
|
|
|
|
* the current process.
|
|
|
|
*/
|
|
|
|
return 0;
|
|
|
|
}
|