aboutsummaryrefslogtreecommitdiff
path: root/tests/libcap_launch_test.c
diff options
context:
space:
mode:
authorAndroid Build Coastguard Worker <android-build-coastguard-worker@google.com>2022-10-12 01:09:41 +0000
committerAndroid Build Coastguard Worker <android-build-coastguard-worker@google.com>2022-10-12 01:09:41 +0000
commitb49bfcb8f0dcb84e532ba4cd4631318bc503245e (patch)
treeacb9cff934a953665142b6e8821d628608a7e498 /tests/libcap_launch_test.c
parent2c67aea5a983378eec08b22d5879145e797dc4b3 (diff)
parente61c65778b22599546e46eee12f857dd1c28f89e (diff)
downloadlibcap-b49bfcb8f0dcb84e532ba4cd4631318bc503245e.tar.gz
Change-Id: Id045070ed233aea5d1e3fd32ad8174e5018f3d80
Diffstat (limited to 'tests/libcap_launch_test.c')
-rw-r--r--tests/libcap_launch_test.c70
1 files changed, 56 insertions, 14 deletions
diff --git a/tests/libcap_launch_test.c b/tests/libcap_launch_test.c
index bba38c6..f45b2b7 100644
--- a/tests/libcap_launch_test.c
+++ b/tests/libcap_launch_test.c
@@ -24,7 +24,9 @@ struct test_case_s {
const char **envp;
const char *iab;
cap_mode_t mode;
+ int launch_abort;
int result;
+ int (*callback_fn)(void *detail);
};
#ifdef WITH_PTHREADS
@@ -32,6 +34,17 @@ struct test_case_s {
#else /* WITH_PTHREADS */
#endif /* WITH_PTHREADS */
+/*
+ * clean_out drops all process capabilities.
+ */
+static int clean_out(void *data) {
+ cap_t empty;
+ empty = cap_init();
+ cap_set_proc(empty);
+ cap_free(empty);
+ return 0;
+}
+
int main(int argc, char **argv) {
static struct test_case_s vs[] = {
{
@@ -39,13 +52,32 @@ int main(int argc, char **argv) {
.result = 0
},
{
+ .args = { "../progs/tcapsh-static", "--", "-c", "echo hello" },
+ .callback_fn = &clean_out,
+ .result = 0
+ },
+ {
+ .callback_fn = &clean_out,
+ .result = 0
+ },
+ {
.args = { "../progs/tcapsh-static", "--is-uid=123" },
.result = 256
},
{
+ .args = { "/", "won't", "work" },
+ .launch_abort = 1,
+ },
+ {
.args = { "../progs/tcapsh-static", "--is-uid=123" },
+ .uid = 123,
.result = 0,
+ },
+ {
+ .args = { "../progs/tcapsh-static", "--is-uid=123" },
+ .callback_fn = &clean_out,
.uid = 123,
+ .launch_abort = 1,
},
{
.args = { "../progs/tcapsh-static", "--is-gid=123" },
@@ -91,8 +123,16 @@ int main(int argc, char **argv) {
for (i=0; vs[i].pass_on != NO_MORE; i++) {
const struct test_case_s *v = &vs[i];
printf("[%d] test should %s\n", i,
- v->result ? "generate error" : "work");
- cap_launch_t attr = cap_new_launcher(v->args[0], v->args, v->envp);
+ v->result || v->launch_abort ? "generate error" : "work");
+ cap_launch_t attr;
+ if (v->args[0] != NULL) {
+ attr = cap_new_launcher(v->args[0], v->args, v->envp);
+ if (v->callback_fn != NULL) {
+ cap_launcher_callback(attr, v->callback_fn);
+ }
+ } else {
+ attr = cap_func_launcher(v->callback_fn);
+ }
if (v->chroot) {
cap_launcher_set_chroot(attr, v->chroot);
}
@@ -125,28 +165,30 @@ int main(int argc, char **argv) {
pid_t child = cap_launch(attr, NULL);
if (child <= 0) {
- fprintf(stderr, "[%d] failed to launch", i);
- perror(":");
- success = 0;
+ fprintf(stderr, "[%d] failed to launch: ", i);
+ perror("");
+ if (!v->launch_abort) {
+ success = 0;
+ }
continue;
}
if (cap_free(attr)) {
- fprintf(stderr, "[%d] failed to free launcher", i);
- perror(":");
+ fprintf(stderr, "[%d] failed to free launcher: ", i);
+ perror("");
success = 0;
}
int result;
int ret = waitpid(child, &result, 0);
if (ret != child) {
- fprintf(stderr, "[%d] failed to wait", i);
- perror(":");
+ fprintf(stderr, "[%d] failed to wait: ", i);
+ perror("");
success = 0;
continue;
}
if (result != v->result) {
- fprintf(stderr, "[%d] bad result: got=%d want=%d", i, result,
+ fprintf(stderr, "[%d] bad result: got=%d want=%d: ", i, result,
v->result);
- perror(":");
+ perror("");
success = 0;
continue;
}
@@ -164,10 +206,10 @@ int main(int argc, char **argv) {
cap_free(final);
cap_free(orig);
- if (success) {
- printf("cap_launch_test: PASSED\n");
- } else {
+ if (!success) {
printf("cap_launch_test: FAILED\n");
exit(1);
}
+ printf("cap_launch_test: PASSED\n");
+ exit(0);
}