]> git.saurik.com Git - apple/copyfile.git/blame - copyfile_test/xattr_test.c
copyfile-173.40.2.tar.gz
[apple/copyfile.git] / copyfile_test / xattr_test.c
CommitLineData
85b8a2cb
A
1//
2// xattr_test.c
3// copyfile_test
4//
5
6#include <unistd.h>
7#include <removefile.h>
8#include <sys/fcntl.h>
9#include <sys/stat.h>
10#include <sys/xattr.h>
11
12#include "xattr_test.h"
13#include "test_utils.h"
14
15#define SRC_FILE_NAME "src_file"
16#define DST_FILE_NAME "dst_file"
17#define SMALL_XATTR_NAME "small_xattr"
18#define SMALL_XATTR_DATA "drell"
19#define BIG_XATTR_NAME "big_xattr"
20#define BIG_XATTR_SIZE (20 * 1024 * 1024) // 20MiB
21
22#define DEFAULT_CHAR_MOD 256
23
24static bool copy_and_verify_xattr_contents(const char *src_file, const char *dst_file, int src_file_fd, int dst_file_fd) {
25 assert_no_err(copyfile(src_file, dst_file, NULL, COPYFILE_XATTR));
26
27 return verify_fd_xattr_contents(src_file_fd, dst_file_fd);
28}
29
30bool do_xattr_test(const char *apfs_test_directory, __unused size_t block_size) {
31 char test_dir[BSIZE_B] = {0};
32 char src_file[BSIZE_B] = {0}, dst_file[BSIZE_B] = {0};
33 char *big_xattr_data = NULL, buf[4096] = {0};
34 int test_folder_id;
35 int src_file_fd, dst_file_fd;
36 bool success = true;
37
38 printf("START [xattr]\n");
39
40 // Get ready for the test.
41 test_folder_id = rand() % DEFAULT_NAME_MOD;
42 create_test_file_name(apfs_test_directory, "xattr", test_folder_id, test_dir);
43 assert_no_err(mkdir(test_dir, DEFAULT_MKDIR_PERM));
44
45 // Create path names.
46 assert_with_errno(snprintf(src_file, BSIZE_B, "%s/" SRC_FILE_NAME, test_dir) > 0);
47 assert_with_errno(snprintf(dst_file, BSIZE_B, "%s/" DST_FILE_NAME, test_dir) > 0);
48
49 // Create our files.
50 src_file_fd = open(src_file, DEFAULT_OPEN_FLAGS, DEFAULT_OPEN_PERM);
51 assert_with_errno(src_file_fd >= 0);
52 dst_file_fd = open(dst_file, DEFAULT_OPEN_FLAGS, DEFAULT_OPEN_PERM);
53 assert_with_errno(dst_file_fd >= 0);
54
55 // Sanity check - empty copy
56 success = success && copy_and_verify_xattr_contents(src_file, dst_file, src_file_fd, dst_file_fd);
57
58 // Write a small xattr to the source file.
59 assert_no_err(fsetxattr(src_file_fd, SMALL_XATTR_NAME, SMALL_XATTR_DATA, sizeof(SMALL_XATTR_DATA), 0, XATTR_CREATE));
60 success = success && copy_and_verify_xattr_contents(src_file, dst_file, src_file_fd, dst_file_fd);
61
62 // Create big xattr data
63 assert_with_errno(big_xattr_data = malloc(BIG_XATTR_SIZE));
64 for (int i = 0; i * sizeof(buf) < BIG_XATTR_SIZE; i++) {
65 memset(buf, rand() % DEFAULT_CHAR_MOD, sizeof(buf));
66 memcpy(big_xattr_data + (i * sizeof(buf)), buf, sizeof(buf));
67 }
68
69 // Write a big xattr to the source file.
70 assert_no_err(fsetxattr(src_file_fd, BIG_XATTR_NAME, big_xattr_data, BIG_XATTR_SIZE, 0, XATTR_CREATE));
71 success = success && copy_and_verify_xattr_contents(src_file, dst_file, src_file_fd, dst_file_fd);
72
73 if (success) {
74 printf("PASS [xattr]\n");
75 } else {
76 printf("FAIL [xattr]\n");
77 }
78
79 free(big_xattr_data);
80 (void)removefile(test_dir, NULL, REMOVEFILE_RECURSIVE);
81
82 return success ? EXIT_SUCCESS : EXIT_FAILURE;
83}
84