tests: Add option to dump policy cache dir with the libapparmor wrapper

Print the policy cache directory path for the features of the currently
running kernel to stdout so that the aa_policy_cache.sh regression test
script can make use of it when writing out binary policy files.

PR: https://gitlab.com/apparmor/apparmor/merge_requests/348
(cherry picked from commit ad81ea0e67)
Signed-off-by: Tyler Hicks <tyhicks@canonical.com>
Signed-off-by: John Johansen <john.johansen@canonical.com>
This commit is contained in:
Tyler Hicks 2019-03-12 19:55:24 +00:00 committed by John Johansen
parent 5704fba8d9
commit 9374f419a0

View file

@ -1,5 +1,5 @@
/*
* Copyright (C) 2015 Canonical, Ltd.
* Copyright (C) 2015, 2019 Canonical, Ltd.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of version 2 of the GNU General Public
@ -24,6 +24,7 @@
#include <sys/apparmor.h>
#define OPT_NEW "new"
#define OPT_CACHE_DIR "cache-dir"
#define OPT_REMOVE "remove"
#define OPT_REMOVE_POLICY "remove-policy"
#define OPT_REPLACE_ALL "replace-all"
@ -34,9 +35,11 @@ static void usage(const char *prog)
fprintf(stderr,
"FAIL - usage: %s %s [%s N] <PATH>\n"
" %s %s <PATH>\n"
" %s %s <PATH>\n"
" %s %s <PROFILE_NAME>\n"
" %s %s [%s N] <PATH>\n",
prog, OPT_NEW, OPT_FLAG_MAX_CACHES,
prog, OPT_CACHE_DIR,
prog, OPT_REMOVE,
prog, OPT_REMOVE_POLICY,
prog, OPT_REPLACE_ALL, OPT_FLAG_MAX_CACHES);
@ -59,6 +62,24 @@ out:
return rc;
}
static int test_cache_dir(const char *path)
{
char *cache_dir;
int rc = 1;
cache_dir = aa_policy_cache_dir_path_preview(NULL, AT_FDCWD, path);
if (!cache_dir) {
perror("FAIL - aa_policy_cache_new");
goto out;
}
printf("%s\n", cache_dir);
rc = 0;
out:
free(cache_dir);
return rc;
}
static int test_remove(const char *path)
{
int rc = 1;
@ -120,6 +141,7 @@ int main(int argc, char **argv)
{
uint16_t max_caches = 0;
const char *str = NULL;
bool show_pass = true;
int rc = 1;
if (!(argc == 3 || argc == 5)) {
@ -151,6 +173,9 @@ int main(int argc, char **argv)
if (strcmp(argv[1], OPT_NEW) == 0) {
rc = test_new(str, max_caches);
} else if (strcmp(argv[1], OPT_CACHE_DIR) == 0 && argc == 3) {
show_pass = false;
rc = test_cache_dir(str);
} else if (strcmp(argv[1], OPT_REMOVE) == 0 && argc == 3) {
rc = test_remove(str);
} else if (strcmp(argv[1], OPT_REMOVE_POLICY) == 0 && argc == 3) {
@ -161,7 +186,7 @@ int main(int argc, char **argv)
usage(argv[0]);
}
if (!rc)
if (show_pass && !rc)
printf("PASS\n");
exit(rc);