4 // Based on the test routines from the apfs project.
14 #include <sys/errno.h>
16 #include "../copyfile.h"
19 #define MAX_DISK_IMAGE_SIZE_MB 1024
21 #define DEFAULT_NAME_MOD 999
22 #define DEFAULT_OPEN_FLAGS O_CREAT|O_TRUNC|O_RDWR
23 #define DEFAULT_OPEN_PERM 0666
24 #define DEFAULT_MKDIR_PERM 0777
26 #define DISK_IMAGE_PATH "/tmp/copyfile_sparse.sparseimage"
27 #define VOLUME_NAME "apfs_sparse"
28 #define DEFAULT_FSTYPE "JHFS+"
29 #define APFS_FSTYPE "apfs"
31 // We assume that we're mounted on /Volumes.
32 #define MOUNT_PATH "/Volumes/" VOLUME_NAME
34 #define HDIUTIL_PATH "/usr/bin/hdiutil"
35 #define DIFF_PATH "/usr/bin/diff"
37 // Test routine helpers.
38 bool verify_st_flags(struct stat
*sb
, uint32_t flags_to_expect
);
39 bool verify_fd_contents(int orig_fd
, off_t orig_pos
, int copy_fd
, off_t copy_pos
, size_t length
);
40 bool verify_copy_contents(const char *orig_name
, const char *copy_name
);
41 bool verify_copy_sizes(struct stat
*orig_sb
, struct stat
*copy_sb
, copyfile_state_t cpf_state
,
42 bool do_sparse
, off_t src_start
);
43 int create_hole_in_fd(int fd
, off_t offset
, off_t length
);
44 void create_test_file_name(const char *dir
, const char *postfix
, int id
, char *string_out
);
46 // Our disk image test functions.
47 void disk_image_create(const char *fstype
, size_t size_in_mb
);
48 void disk_image_destroy(bool allow_failure
);
50 // Assertion functions/macros for tests.
52 __attribute__((format (printf
, 3, 4)))
53 __attribute__((noreturn
))
54 assert_fail_(const char *file
, int line
, const char *assertion
, ...)
57 va_start(args
, assertion
);
59 vasprintf(&msg
, assertion
, args
);
61 printf("\n%s:%u: error: %s\n", file
, line
, msg
);
65 #define assert_fail(str, ...) \
66 assert_fail_(__FILE__, __LINE__, str, ## __VA_ARGS__)
69 #define assert(condition) \
72 assert_fail_(__FILE__, __LINE__, \
73 "assertion failed: %s", #condition); \
76 #define assert_with_errno_(condition, condition_str) \
79 assert_fail_(__FILE__, __LINE__, "%s failed: %s", \
80 condition_str, strerror(errno)); \
83 #define assert_with_errno(condition) \
84 assert_with_errno_((condition), #condition)
86 #define assert_no_err(condition) \
87 assert_with_errno_(!(condition), #condition)
89 #define assert_equal(lhs, rhs, fmt) \
91 typeof (lhs) lhs_ = (lhs); \
92 typeof (lhs) rhs_ = (rhs); \
94 assert_fail(#lhs " (" fmt ") != " #rhs " (" fmt ")", \
98 #define assert_equal_(lhs, rhs, lhs_str, rhs_str, fmt) \
100 typeof (lhs) lhs_ = (lhs); \
101 typeof (lhs) rhs_ = (rhs); \
103 assert_fail(lhs_str " (" fmt ") != " rhs_str " (" fmt ")", \
107 #define assert_equal_int(lhs, rhs) assert_equal_(lhs, rhs, #lhs, #rhs, "%d")
108 #define assert_equal_ll(lhs, rhs) assert_equal_(lhs, rhs, #lhs, #rhs, "%lld")
109 #define assert_equal_str(lhs, rhs) \
111 const char *lhs_ = (lhs), *rhs_ = (rhs); \
112 if (strcmp(lhs_, rhs_)) \
113 assert_fail("\"%s\" != \"%s\"", lhs_, rhs_); \
116 #define ignore_eintr(x, error_val) \
118 typeof(x) eintr_ret_; \
121 } while (eintr_ret_ == (error_val) && errno == EINTR); \
125 #endif /* test_utils_h */