Path: blob/main/tools/regression/security/cap_test/cap_test.c
48266 views
/*-1* Copyright (c) 2008-2011 Robert N. M. Watson2* Copyright (c) 2011 Jonathan Anderson3* All rights reserved.4*5* Redistribution and use in source and binary forms, with or without6* modification, are permitted provided that the following conditions7* are met:8* 1. Redistributions of source code must retain the above copyright9* notice, this list of conditions and the following disclaimer.10* 2. Redistributions in binary form must reproduce the above copyright11* notice, this list of conditions and the following disclaimer in the12* documentation and/or other materials provided with the distribution.13*14* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND15* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE16* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE17* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE18* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL19* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS20* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)21* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT22* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY23* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF24* SUCH DAMAGE.25*/2627#include <sys/cdefs.h>28#include <sys/wait.h>2930#include <err.h>31#include <inttypes.h>32#include <stdio.h>33#include <stdlib.h>34#include <string.h>35#include <unistd.h>3637#include "cap_test.h"3839/* Initialize a named test. Requires test_NAME() function to be declared. */40#define TEST_INIT(name) { #name, test_##name, FAILED }4142/* All of the tests that can be run. */43struct test all_tests[] = {44TEST_INIT(capmode),45TEST_INIT(capabilities),46TEST_INIT(fcntl),47TEST_INIT(pdfork),48TEST_INIT(pdkill),49TEST_INIT(relative),50TEST_INIT(sysctl),51};52int test_count = sizeof(all_tests) / sizeof(struct test);5354int55main(int argc, char *argv[])56{5758/*59* If no tests have been specified at the command line, run them all.60*/61if (argc == 1) {62printf("1..%d\n", test_count);6364for (int i = 0; i < test_count; i++)65execute(i + 1, all_tests + i);66return (0);67}6869/*70* Otherwise, run only the specified tests.71*/72printf("1..%d\n", argc - 1);73for (int i = 1; i < argc; i++)74{75int found = 0;76for (int j = 0; j < test_count; j++) {77if (strncmp(argv[i], all_tests[j].t_name,78strlen(argv[i])) == 0) {79found = 1;80execute(i, all_tests + j);81break;82}83}8485if (found == 0)86errx(-1, "No such test '%s'", argv[i]);87}8889return (0);90}9192int93execute(int id, struct test *t) {94int result;9596pid_t pid = fork();97if (pid < 0)98err(-1, "fork");99if (pid) {100/* Parent: wait for result from child. */101int status;102while (waitpid(pid, &status, 0) != pid) {}103if (WIFEXITED(status))104result = WEXITSTATUS(status);105else106result = FAILED;107} else {108/* Child process: run the test. */109exit(t->t_run());110}111112printf("%s %d - %s\n",113(result == PASSED) ? "ok" : "not ok",114id, t->t_name);115116return (result);117}118119120