]> git.saurik.com Git - apple/hfs.git/blob - tests/cases/test-dprotect.c
hfs-522.0.9.tar.gz
[apple/hfs.git] / tests / cases / test-dprotect.c
1 #include <TargetConditionals.h>
2
3 #if (TARGET_OS_IPHONE && !TARGET_OS_SIMULATOR)
4
5 #include <sys/fcntl.h>
6 #include <string.h>
7 #include <stdio.h>
8 #include <sys/errno.h>
9 #include <stdlib.h>
10 #include <unistd.h>
11 #include <time.h>
12 #include <sys/time.h>
13 #include <stdbool.h>
14 #include <stdarg.h>
15 #include <sys/stat.h>
16 #include <sys/param.h>
17 #include <sys/mount.h>
18
19 #include "hfs-tests.h"
20 #include "test-utils.h"
21 #include "disk-image.h"
22
23 TEST(dprotect, .run_as_root = true)
24
25 #define TEST_FILE "/tmp/dprotect.data"
26
27 int run_dprotect(__unused test_ctx_t *ctx)
28 {
29 // The root file system needs to be HFS
30 struct statfs sfs;
31
32 assert(statfs("/tmp", &sfs) == 0);
33 if (strcmp(sfs.f_fstypename, "hfs")) {
34 printf("dprotect needs hfs as root file system - skipping.\n");
35 return 0;
36 }
37
38 unlink(TEST_FILE);
39 int fd = open_dprotected_np(TEST_FILE, O_RDWR | O_CREAT,
40 2, 0, 0666);
41 assert_with_errno(fd >= 0);
42
43 char *ones= valloc(4096), *buf = valloc(16384);
44
45 memset(ones, 0xff, 4096);
46 memset(buf, 0xff, 4096);
47
48 check_io(write(fd, buf, 4096), 4096);
49
50 assert_no_err(fsync(fd));
51
52 assert_no_err(close(fd));
53
54 fd = open_dprotected_np(TEST_FILE, O_RDONLY, 0, O_DP_GETRAWENCRYPTED);
55 assert(fd >= 0);
56
57 check_io(pread(fd, buf, 4096, 0), 4096);
58
59 if (!memcmp(ones, buf, 4096))
60 assert_fail("data should be encrypted (make sure the volume you're running on has content protection enabled)");
61
62 assert_no_err(unlink(TEST_FILE));
63 free(ones);
64 free(buf);
65
66 return 0;
67 }
68
69 #endif // TARGET_OS_IPHONE & !SIM