diff options
author | DongHun Kwak <dh0128.kwak@samsung.com> | 2021-10-18 11:18:25 +0900 |
---|---|---|
committer | DongHun Kwak <dh0128.kwak@samsung.com> | 2021-10-18 11:18:25 +0900 |
commit | 0f4ff91467f9cd2b14f460297d0e7dcb84f80840 (patch) | |
tree | 2b10b3dc2b0eef1aac6880096423fa575e771eca /tests/kernel/lp-509180 | |
parent | bb0ada3bddfeeb4e98d3415c37266061c865005f (diff) | |
download | ecryptfs-utils-0f4ff91467f9cd2b14f460297d0e7dcb84f80840.tar.gz ecryptfs-utils-0f4ff91467f9cd2b14f460297d0e7dcb84f80840.tar.bz2 ecryptfs-utils-0f4ff91467f9cd2b14f460297d0e7dcb84f80840.zip |
Imported Upstream version 105upstream/105
Diffstat (limited to 'tests/kernel/lp-509180')
-rw-r--r-- | tests/kernel/lp-509180/test.c | 130 |
1 files changed, 97 insertions, 33 deletions
diff --git a/tests/kernel/lp-509180/test.c b/tests/kernel/lp-509180/test.c index 5b4886b..60cd017 100644 --- a/tests/kernel/lp-509180/test.c +++ b/tests/kernel/lp-509180/test.c @@ -26,6 +26,7 @@ #include <fcntl.h> #include <sys/types.h> #include <sys/stat.h> +#include <attr/xattr.h> #define TEST_ERROR (2) @@ -33,12 +34,99 @@ #define OPT_INC (0x0001) #define OPT_DEC (0x0002) +#define OPT_XATTR (0x0004) void usage(char *name) { - fprintf(stderr, "Usage: [-i | -d] file\n"); + fprintf(stderr, "Usage: [-i | -d] [-x] file\n"); } +static int test_with_metadata_in_header(int fd, int flags) +{ + unsigned char buffer[1]; + + if ((lseek(fd, (off_t)OFFSET, SEEK_SET)) < 0) { + fprintf(stderr, "Cannot lseek to offset %d: %s\n", + OFFSET, strerror(errno)); + return TEST_ERROR; + } + + if (read(fd, buffer, sizeof(buffer)) != sizeof(buffer)) { + fprintf(stderr, "Failed to read\n"); + return TEST_ERROR; + } + + if (flags & OPT_INC) + buffer[0]++; + + if (flags & OPT_DEC) + buffer[0]--; + + if ((lseek(fd, (off_t)OFFSET, SEEK_SET)) < 0) { + fprintf(stderr, "Cannot lseek to offset %d: %s\n", + OFFSET, strerror(errno)); + return TEST_ERROR; + } + + if (write(fd, buffer, sizeof(buffer)) != sizeof(buffer)) { + fprintf(stderr, "Failed to write\n"); + return TEST_ERROR; + } + + return 0; +} + +static int test_with_metadata_in_xattr(int fd, int flags) +{ + const char *name = "user.ecryptfs"; + unsigned char *value = NULL; + ssize_t nread, size = 0; + int rc = TEST_ERROR; + + size = fgetxattr(fd, name, value, size); + if (size < 0) { + fprintf(stderr, "Cannot retrieve xattr size: %s\n", + strerror(errno)); + goto out; + } + + value = malloc(size); + if (!value) { + fprintf(stderr, + "Cannot allocate memory to store the xattr value\n"); + goto out; + } + + nread = fgetxattr(fd, name, value, size); + if (nread != size) { + if (nread < 0) + fprintf(stderr, "Cannot read xattr: %s\n", + strerror(errno)); + else + fprintf(stderr, "Partial xattr read: %zu < %zu\n", + nread, size); + goto out; + } + + if (flags & OPT_INC) + value[OFFSET]++; + + if (flags & OPT_DEC) + value[OFFSET]--; + + if (fsetxattr(fd, name, value, size, XATTR_REPLACE) < 0) { + fprintf(stderr, "Cannot write xattr: %s\n", strerror(errno)); + goto out; + } + + rc = 0; +out: + free(value); + + return rc; +} + + /* * https://bugs.launchpad.net/ecryptfs/+bug/509180 * Increment/Decrement 9th byte in lower file @@ -49,14 +137,13 @@ int main(int argc, char **argv) int opt, flags = 0; int rc = 0; char *file; - unsigned char buffer[1]; if (argc < 3) { usage(argv[0]); exit(TEST_ERROR); } - while ((opt = getopt(argc, argv, "id")) != -1) { + while ((opt = getopt(argc, argv, "idx")) != -1) { switch (opt) { case 'i': flags |= OPT_INC; @@ -64,6 +151,9 @@ int main(int argc, char **argv) case 'd': flags |= OPT_DEC; break; + case 'x': + flags |= OPT_XATTR; + break; default: usage(argv[0]); exit(TEST_ERROR); @@ -82,36 +172,10 @@ int main(int argc, char **argv) exit(TEST_ERROR); } - if ((lseek(fd, (off_t)OFFSET, SEEK_SET)) < 0) { - fprintf(stderr, "Cannot lseek to offset %d in %s : %s\n", - OFFSET, file, strerror(errno)); - rc = TEST_ERROR; - goto tidy; - } - - if (read(fd, buffer, sizeof(buffer)) != sizeof(buffer)) { - fprintf(stderr, "Failed to read\n"); - rc = TEST_ERROR; - goto tidy; - } - - if (flags & OPT_INC) - buffer[0]++; - - if (flags & OPT_DEC) - buffer[0]--; - - if ((lseek(fd, (off_t)OFFSET, SEEK_SET)) < 0) { - fprintf(stderr, "Cannot lseek to offset %d in %s : %s\n", - OFFSET, file, strerror(errno)); - rc = TEST_ERROR; - goto tidy; - } - - if (write(fd, buffer, sizeof(buffer)) != sizeof(buffer)) { - fprintf(stderr, "Failed to write\n"); - rc = TEST_ERROR; - } + if (flags & OPT_XATTR) + rc = test_with_metadata_in_xattr(fd, flags); + else + rc = test_with_metadata_in_header(fd, flags); tidy: if (close(fd) < 0) { |