]>
git.saurik.com Git - apple/hfs.git/blob - tests/cases/test-set-protection-class.c
1 // See <rdar://16977080>
10 #include <sys/errno.h>
12 #include <sys/xattr.h>
13 #include <sys/mount.h>
14 #include <sys/param.h>
15 #include <TargetConditionals.h>
17 #include "hfs-tests.h"
18 #include "test-utils.h"
19 #include "disk-image.h"
21 TEST(set_protection_class
)
26 int run_set_protection_class(__unused test_ctx_t
*ctx
)
30 #if TARGET_OS_EMBEDDED
31 // The root file system needs to be HFS
34 assert(statfs("/tmp", &sfs
) == 0);
35 if (strcmp(sfs
.f_fstypename
, "hfs")) {
36 printf("set_protection_class needs hfs as root file system - skipping.\n");
41 #else // !TARGET_OS_EMBEDDED
42 disk_image_t
*di
= disk_image_get();
43 tstdir
= di
->mount_point
;
46 asprintf(&path
, "%s/set-protection-class.data.%u", tstdir
, getpid());
48 const size_t size
= 16 * 1024 * 1024;
50 void *buf
= valloc(size
), *buf2
= valloc(size
);
51 memset(buf
, 0x1f, size
);
54 * Pass 0: Write files using write and then call
55 * F_SETPROTECTIONCLASS while the file is still referenced
56 * i.e. file won't go through inactive.
58 * Pass 1: Like pass 0 but file should go through inactive.
60 * Pass 2: Like pass 0 but using resource fork to reference file.
62 * Pass 3: Like pass 0 but use mmap to write the data.
64 * Pass 4: Like pass 3 but we close the file after mmap which will
65 * mean the file goes through inactive when we do the
70 for (pass
= 0; pass
< 5; ++pass
) {
73 assert_with_errno((fd
= open(path
, O_RDWR
| O_TRUNC
| O_CREAT
, 0666)) >= 0);
80 size_t todo
= random() % 1024 + 1;
81 if (todo
> size
- done
)
84 check_io(write(fd
, buf
+ done
, todo
), todo
);
95 assert_no_err(ftruncate(fd
, size
));
96 assert_with_errno((p
= mmap(NULL
, size
, PROT_READ
| PROT_WRITE
,
97 MAP_SHARED
, fd
, 0)) != MAP_FAILED
);
100 assert_no_err(close(fd
));
103 while (done
< size
) {
104 size_t todo
= random() % 1024 + 1;
105 if (todo
> size
- done
)
108 memcpy(p
+ done
, buf
+ done
, todo
);
113 assert_no_err(msync(p
, size
, MS_ASYNC
));
115 assert_no_err(munmap(p
, size
));
123 assert_with_errno((fd2
= open(path
, O_RDONLY
)) >= 0);
129 // Force the rsrc fork vnode to be created
130 static const char val
[] = "set-protection-class-test";
131 assert_with_errno(!setxattr(path
, XATTR_RESOURCEFORK_NAME
, val
,
132 sizeof(val
) - 1, 0, 0));
138 assert_no_err(close(fd
));
140 assert_with_errno((fd
= open(path
, O_RDWR
)) >= 0);
142 #if TARGET_OS_EMBEDDED
143 assert_no_err(fcntl(fd
, F_SETPROTECTIONCLASS
, 2));
148 assert_with_errno((p
= mmap(NULL
, size
, PROT_WRITE
, MAP_SHARED
,
149 fd
, 0)) != MAP_FAILED
);
151 assert_no_err(msync(p
, size
, MS_INVALIDATE
));
154 check_io(pread(fd
, buf2
, size
, 0), size
);
156 assert(!memcmp(buf2
, buf
, size
));
158 assert_no_err(close(fd
));
160 assert_no_err(close(fd2
));