From 8212fa8be4a79a215799200b567c5ea5e1b4be62 Mon Sep 17 00:00:00 2001 From: Ryan Lee Date: Thu, 12 Dec 2024 15:23:55 -0800 Subject: [PATCH] Add more operations to the regression test complain binary This extra functionality is to be used in a different regression test that reuses the binary Signed-off-by: Ryan Lee --- tests/regression/apparmor/complain.c | 64 ++++++++++++++++++++++++++-- 1 file changed, 60 insertions(+), 4 deletions(-) diff --git a/tests/regression/apparmor/complain.c b/tests/regression/apparmor/complain.c index 1a7d1c863..a4c4d6d3e 100644 --- a/tests/regression/apparmor/complain.c +++ b/tests/regression/apparmor/complain.c @@ -2,8 +2,11 @@ #include #include +#include +#include + void print_usage() { - fprintf(stderr, "Usage: ./complain (read|exec) [args]\n"); + fprintf(stderr, "Usage: ./complain (operation) [args]\n"); } int main(int argc, char **argv) { @@ -23,16 +26,69 @@ int main(int argc, char **argv) { fclose(file); return 1; } - // Don't need to do anything else for now - fprintf(stderr, "PASS\n"); - return 0; + unsigned char* discard_read_buf[8]; + fread(discard_read_buf, 8, 1, file); + if (ferror(file)) { + perror("FAIL: Could not perform file read"); + fclose(file); + return 1; + } + fclose(file); + } else if (strcmp(argv[1], "write") == 0) { + FILE *file = fopen(argv[2], "w"); + if (file == NULL) { + perror("FAIL: Could not open file"); + return 2; + } + long file_len = ftell(file); + if (file_len == -1) { + perror("FAIL: Could not get file len"); + fclose(file); + return 1; + } + const char* write_buf = "pahoehoe"; + fwrite(write_buf, 9, 1, file); + if (ferror(file)) { + perror("FAIL: Could not perform file write"); + fclose(file); + return 1; + } + fclose(file); } else if (strcmp(argv[1], "exec") == 0) { execvp(argv[2], &argv[2]); // execvp failed fprintf(stderr, "FAIL: execvp of %s failed\n", argv[1]); return 1; + } else if (strcmp(argv[1], "stat") == 0) { + struct stat unused; + if (stat(argv[2], &unused) == -1) { + perror("FAIL: Could not perform file stat"); + return 1; + } + } else if (strcmp(argv[1], "xattr") == 0) { + // Only query the size-that should be enough to exercise the syscall + if (listxattr(argv[2], NULL, 0) < 0) { + perror("FAIL: Could not get file xattrs"); + return 1; + } + } else if (strcmp(argv[1], "rename") == 0) { + if (argc < 4) { + fprintf(stderr, "Error: rename operation needs two arguments\n"); + return 1; + } + if (rename(argv[2], argv[3]) == -1) { + perror("FAIL: Could not perform file rename"); + return 1; + } + } else if (strcmp(argv[1], "unlink") == 0) { + if (unlink(argv[1]) == -1) { + perror("FAIL: Could not perform file removal"); + return 1; + } } else { print_usage(); return 1; } + fprintf(stderr, "PASS\n"); + return 0; } \ No newline at end of file