Path: blob/main/lib/iolog/regress/iolog_mkpath/check_iolog_mkpath.c
1532 views
/*1* SPDX-License-Identifier: ISC2*3* Copyright (c) 2020 Todd C. Miller <[email protected]>4*5* Permission to use, copy, modify, and distribute this software for any6* purpose with or without fee is hereby granted, provided that the above7* copyright notice and this permission notice appear in all copies.8*9* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES10* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF11* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR12* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES13* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN14* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF15* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.16*/1718#include <config.h>1920#include <sys/wait.h>21#include <stdio.h>22#include <stdlib.h>23#include <string.h>24#include <time.h>25#include <unistd.h>2627#define SUDO_ERROR_WRAP 02829#include <sudo_compat.h>30#include <sudo_util.h>31#include <sudo_fatal.h>32#include <sudo_iolog.h>3334sudo_dso_public int main(int argc, char *argv[]);3536static const char *test_paths[] = {37"testdir/a/b/c/user", /* create new */38"testdir/a/b/c/user", /* open existing */39"testdir/a/b/c/user.XXXXXX", /* mkdtemp new */40NULL41};4243static void44test_iolog_mkpath(const char *testdir, int *ntests, int *nerrors)45{46const char **tp;47char *path;4849iolog_set_owner(geteuid(), getegid());5051for (tp = test_paths; *tp != NULL; tp++) {52if (asprintf(&path, "%s/%s", testdir, *tp) == -1)53sudo_fatalx("unable to allocate memory");5455(*ntests)++;56if (!iolog_mkpath(path)) {57sudo_warnx("unable to mkpath %s", path);58(*nerrors)++;59}60free(path);61}62}6364int65main(int argc, char *argv[])66{67char testdir[] = "mkpath.XXXXXX";68const char *rmargs[] = { "rm", "-rf", NULL, NULL };69int ch, status, ntests = 0, errors = 0;7071initprogname(argc > 0 ? argv[0] : "check_iolog_mkpath");7273while ((ch = getopt(argc, argv, "v")) != -1) {74switch (ch) {75case 'v':76/* ignore */77break;78default:79fprintf(stderr, "usage: %s [-v]\n", getprogname());80return EXIT_FAILURE;81}82}83argc -= optind;84argv += optind;8586if (mkdtemp(testdir) == NULL)87sudo_fatal("unable to create test dir");88rmargs[2] = testdir;8990test_iolog_mkpath(testdir, &ntests, &errors);9192if (ntests != 0) {93printf("iolog_mkpath: %d test%s run, %d errors, %d%% success rate\n",94ntests, ntests == 1 ? "" : "s", errors,95(ntests - errors) * 100 / ntests);96}9798/* Clean up (avoid running via shell) */99switch (fork()) {100case -1:101sudo_warn("fork");102_exit(1);103case 0:104execvp("rm", (char **)rmargs);105_exit(1);106default:107wait(&status);108if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)109errors++;110break;111}112113return errors;114}115116117