]> git.saurik.com Git - apple/copyfile.git/blob - copyfile_test/test_utils.h
decbe1e23f7d627f60267e830772c7b8dcba8b21
[apple/copyfile.git] / copyfile_test / test_utils.h
1 //
2 // test_utils.h
3 // copyfile_test
4 // Based on the test routines from the apfs project.
5 //
6
7 #ifndef test_utils_h
8 #define test_utils_h
9
10 #include <stdarg.h>
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <string.h>
14 #include <sys/errno.h>
15
16 #include "../copyfile.h"
17
18 #define BSIZE_B 128
19 #define MAX_DISK_IMAGE_SIZE_MB 1024
20
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
25
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"
30
31 // We assume that we're mounted on /Volumes.
32 #define MOUNT_PATH "/Volumes/" VOLUME_NAME
33
34 #define HDIUTIL_PATH "/usr/bin/hdiutil"
35 #define DIFF_PATH "/usr/bin/diff"
36
37 // Test routine helpers.
38 bool verify_fd_xattr_contents(int orig_fd, int copy_fd);
39 bool verify_st_flags(struct stat *sb, uint32_t flags_to_expect);
40 bool verify_contents_with_buf(int orig_fd, off_t orig_pos, const char *expected, size_t length);
41 bool verify_fd_contents(int orig_fd, off_t orig_pos, int copy_fd, off_t copy_pos, size_t length);
42 bool verify_copy_contents(const char *orig_name, const char *copy_name);
43 bool verify_copy_sizes(struct stat *orig_sb, struct stat *copy_sb, copyfile_state_t cpf_state,
44 bool do_sparse, off_t src_start);
45 int create_hole_in_fd(int fd, off_t offset, off_t length);
46 void create_test_file_name(const char *dir, const char *postfix, int id, char *string_out);
47
48 // Our disk image test functions.
49 void disk_image_create(const char *fstype, size_t size_in_mb);
50 void disk_image_destroy(bool allow_failure);
51
52 // Assertion functions/macros for tests.
53 static inline void
54 __attribute__((format (printf, 3, 4)))
55 __attribute__((noreturn))
56 assert_fail_(const char *file, int line, const char *assertion, ...)
57 {
58 va_list args;
59 va_start(args, assertion);
60 char *msg;
61 vasprintf(&msg, assertion, args);
62 va_end(args);
63 printf("\n%s:%u: error: %s\n", file, line, msg);
64 exit(1);
65 }
66
67 #define assert_fail(str, ...) \
68 assert_fail_(__FILE__, __LINE__, str, ## __VA_ARGS__)
69
70 #undef assert
71 #define assert(condition) \
72 do { \
73 if (!(condition)) \
74 assert_fail_(__FILE__, __LINE__, \
75 "assertion failed: %s", #condition); \
76 } while (0)
77
78 #define assert_with_errno_(condition, condition_str) \
79 do { \
80 if (!(condition)) \
81 assert_fail_(__FILE__, __LINE__, "%s failed: %s", \
82 condition_str, strerror(errno)); \
83 } while (0)
84
85 #define assert_with_errno(condition) \
86 assert_with_errno_((condition), #condition)
87
88 #define assert_no_err(condition) \
89 assert_with_errno_(!(condition), #condition)
90
91 #define assert_equal(lhs, rhs, fmt) \
92 do { \
93 typeof (lhs) lhs_ = (lhs); \
94 typeof (lhs) rhs_ = (rhs); \
95 if (lhs_ != rhs_) \
96 assert_fail(#lhs " (" fmt ") != " #rhs " (" fmt ")", \
97 lhs_, rhs_); \
98 } while (0)
99
100 #define assert_equal_(lhs, rhs, lhs_str, rhs_str, fmt) \
101 do { \
102 typeof (lhs) lhs_ = (lhs); \
103 typeof (lhs) rhs_ = (rhs); \
104 if (lhs_ != rhs_) \
105 assert_fail(lhs_str " (" fmt ") != " rhs_str " (" fmt ")", \
106 lhs_, rhs_); \
107 } while (0)
108
109 #define assert_equal_int(lhs, rhs) assert_equal_(lhs, rhs, #lhs, #rhs, "%d")
110 #define assert_equal_ll(lhs, rhs) assert_equal_(lhs, rhs, #lhs, #rhs, "%lld")
111 #define assert_equal_str(lhs, rhs) \
112 do { \
113 const char *lhs_ = (lhs), *rhs_ = (rhs); \
114 if (strcmp(lhs_, rhs_)) \
115 assert_fail("\"%s\" != \"%s\"", lhs_, rhs_); \
116 } while (0)
117
118 #define ignore_eintr(x, error_val) \
119 ({ \
120 typeof(x) eintr_ret_; \
121 do { \
122 eintr_ret_ = (x); \
123 } while (eintr_ret_ == (error_val) && errno == EINTR); \
124 eintr_ret_; \
125 })
126
127 #endif /* test_utils_h */