]>
Commit | Line | Data |
---|---|---|
937356ff A |
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 DISK_IMAGE_PATH "/tmp/copyfile_sparse.sparseimage" | |
22 | #define VOLUME_NAME "apfs_sparse" | |
23 | #define DEFAULT_FSTYPE "JHFS+" | |
24 | #define APFS_FSTYPE "apfs" | |
25 | ||
26 | // We assume that we're mounted on /Volumes. | |
27 | #define MOUNT_PATH "/Volumes/" VOLUME_NAME | |
28 | ||
29 | #define HDIUTIL_PATH "/usr/bin/hdiutil" | |
30 | #define DIFF_PATH "/usr/bin/diff" | |
31 | ||
32 | // Test routine helpers. | |
33 | bool verify_fd_contents(int orig_fd, off_t orig_pos, int copy_fd, off_t copy_pos, size_t length); | |
34 | bool verify_copy_contents(const char *orig_name, const char *copy_name); | |
35 | bool verify_copy_sizes(struct stat *orig_sb, struct stat *copy_sb, copyfile_state_t cpf_state, | |
36 | bool do_sparse, off_t src_start); | |
37 | int create_hole_in_fd(int fd, off_t offset, off_t length); | |
38 | void create_test_file_name(const char *dir, const char *postfix, int id, char *string_out); | |
39 | ||
40 | // Our disk image test functions. | |
41 | void disk_image_create(const char *fstype, size_t size_in_mb); | |
42 | void disk_image_destroy(bool allow_failure); | |
43 | ||
44 | // Assertion functions/macros for tests. | |
45 | static inline void | |
46 | __attribute__((format (printf, 3, 4))) | |
47 | __attribute__((noreturn)) | |
48 | assert_fail_(const char *file, int line, const char *assertion, ...) | |
49 | { | |
50 | va_list args; | |
51 | va_start(args, assertion); | |
52 | char *msg; | |
53 | vasprintf(&msg, assertion, args); | |
54 | va_end(args); | |
55 | printf("\n%s:%u: error: %s\n", file, line, msg); | |
56 | exit(1); | |
57 | } | |
58 | ||
59 | #define assert_fail(str, ...) \ | |
60 | assert_fail_(__FILE__, __LINE__, str, ## __VA_ARGS__) | |
61 | ||
62 | #undef assert | |
63 | #define assert(condition) \ | |
64 | do { \ | |
65 | if (!(condition)) \ | |
66 | assert_fail_(__FILE__, __LINE__, \ | |
67 | "assertion failed: %s", #condition); \ | |
68 | } while (0) | |
69 | ||
70 | #define assert_with_errno_(condition, condition_str) \ | |
71 | do { \ | |
72 | if (!(condition)) \ | |
73 | assert_fail_(__FILE__, __LINE__, "%s failed: %s", \ | |
74 | condition_str, strerror(errno)); \ | |
75 | } while (0) | |
76 | ||
77 | #define assert_with_errno(condition) \ | |
78 | assert_with_errno_((condition), #condition) | |
79 | ||
80 | #define assert_no_err(condition) \ | |
81 | assert_with_errno_(!(condition), #condition) | |
82 | ||
83 | #define assert_equal(lhs, rhs, fmt) \ | |
84 | do { \ | |
85 | typeof (lhs) lhs_ = (lhs); \ | |
86 | typeof (lhs) rhs_ = (rhs); \ | |
87 | if (lhs_ != rhs_) \ | |
88 | assert_fail(#lhs " (" fmt ") != " #rhs " (" fmt ")", \ | |
89 | lhs_, rhs_); \ | |
90 | } while (0) | |
91 | ||
92 | #define assert_equal_(lhs, rhs, lhs_str, rhs_str, fmt) \ | |
93 | do { \ | |
94 | typeof (lhs) lhs_ = (lhs); \ | |
95 | typeof (lhs) rhs_ = (rhs); \ | |
96 | if (lhs_ != rhs_) \ | |
97 | assert_fail(lhs_str " (" fmt ") != " rhs_str " (" fmt ")", \ | |
98 | lhs_, rhs_); \ | |
99 | } while (0) | |
100 | ||
101 | #define assert_equal_int(lhs, rhs) assert_equal_(lhs, rhs, #lhs, #rhs, "%d") | |
102 | #define assert_equal_ll(lhs, rhs) assert_equal_(lhs, rhs, #lhs, #rhs, "%lld") | |
103 | #define assert_equal_str(lhs, rhs) \ | |
104 | do { \ | |
105 | const char *lhs_ = (lhs), *rhs_ = (rhs); \ | |
106 | if (strcmp(lhs_, rhs_)) \ | |
107 | assert_fail("\"%s\" != \"%s\"", lhs_, rhs_); \ | |
108 | } while (0) | |
109 | ||
110 | #define ignore_eintr(x, error_val) \ | |
111 | ({ \ | |
112 | typeof(x) eintr_ret_; \ | |
113 | do { \ | |
114 | eintr_ret_ = (x); \ | |
115 | } while (eintr_ret_ == (error_val) && errno == EINTR); \ | |
116 | eintr_ret_; \ | |
117 | }) | |
118 | ||
119 | #endif /* test_utils_h */ |