]>
Commit | Line | Data |
---|---|---|
558d2836 A |
1 | /* |
2 | * Copyright (c) 2014 Apple, Inc. All rights reserved. | |
3 | * | |
4 | * Test HFS fsinfo fsctls | |
5 | */ | |
6 | ||
7 | #include <TargetConditionals.h> | |
8 | ||
9 | #if TARGET_OS_EMBEDDED | |
10 | ||
11 | #include <sys/ioctl.h> | |
12 | #include <sys/ioccom.h> | |
13 | #include <sys/param.h> | |
14 | #include <sys/mount.h> | |
15 | #include <stdio.h> | |
16 | #include <unistd.h> | |
17 | #include <stdlib.h> | |
18 | #include <string.h> | |
19 | #include <sys/errno.h> | |
20 | #include <spawn.h> | |
21 | #include <sys/stat.h> | |
22 | #include <fcntl.h> | |
23 | #include <hfs/hfs_fsctl.h> | |
24 | ||
25 | #include "hfs-tests.h" | |
26 | #include "test-utils.h" | |
27 | #include "systemx.h" | |
28 | ||
29 | TEST(fsinfo_cprotect, .run_as_root = true) | |
30 | ||
31 | #define KB *1024 | |
32 | #define MAX_FILES 10 | |
33 | #define TEST_DIR "/tmp" | |
34 | ||
35 | static hfs_fsinfo fsinfo; | |
36 | ||
37 | static struct attrs { | |
38 | uint32_t len; | |
39 | uint64_t file_id; | |
40 | uint32_t dp_flags; | |
41 | } __attribute__((aligned(4), packed)) attrs; | |
42 | ||
43 | static void test_fsinfo_file_cprotect_count(void) | |
44 | { | |
45 | int fd; | |
46 | unsigned buf_size; | |
47 | void *buf = malloc(1 KB); | |
48 | unsigned class_B_count = 0; | |
49 | unsigned i; | |
50 | char *path[10]; | |
51 | ||
52 | bzero(&fsinfo, sizeof(fsinfo)); | |
53 | fsinfo.header.version = HFS_FSINFO_VERSION; | |
54 | fsinfo.header.request_type = HFS_FSINFO_FILE_CPROTECT_COUNT; | |
55 | assert_no_err(fsctl(TEST_DIR, HFSIOC_GET_FSINFO, &fsinfo, 0)); | |
56 | class_B_count = fsinfo.cprotect.class_B; | |
57 | ||
58 | for (i = 0; i < 10; i++) | |
59 | { | |
60 | asprintf(&path[i], "%s/fsinfo_test.data.%u", TEST_DIR, getpid()); | |
61 | unlink(path[i]); | |
62 | assert_with_errno((fd = open(path[i], O_RDWR | O_TRUNC | O_CREAT, 0666)) >= 0); | |
63 | buf_size = (1 KB); | |
64 | buf = malloc(buf_size); | |
65 | memset(buf, 0x83, buf_size); | |
66 | ||
67 | assert_no_err(fcntl(fd, F_SETPROTECTIONCLASS, 2)); | |
68 | ||
69 | struct attrlist attrlist = { | |
70 | .bitmapcount = ATTR_BIT_MAP_COUNT, | |
71 | .commonattr = ATTR_CMN_FILEID | ATTR_CMN_DATA_PROTECT_FLAGS, | |
72 | }; | |
73 | ||
74 | assert_no_err(fgetattrlist(fd, &attrlist, &attrs, sizeof(attrs), 0)); | |
75 | assert((attrs.dp_flags & 0x1f) == 2); | |
76 | } | |
77 | ||
78 | assert_no_err(fsctl(TEST_DIR, HFSIOC_GET_FSINFO, &fsinfo, 0)); | |
79 | ||
80 | free(buf); | |
81 | for (i = 0; i < 10; i++) | |
82 | unlink(path[i]); | |
83 | ||
84 | assert(fsinfo.cprotect.class_B >= (class_B_count + 5)); | |
85 | ||
86 | } | |
87 | ||
88 | int run_fsinfo_cprotect(__unused test_ctx_t *ctx) | |
89 | { | |
90 | // The root file system needs to be HFS | |
91 | struct statfs sfs; | |
92 | ||
93 | assert(statfs("/tmp", &sfs) == 0); | |
94 | if (strcmp(sfs.f_fstypename, "hfs")) { | |
95 | printf("fsinfo_cprotect needs hfs as root file system - skipping.\n"); | |
96 | return 0; | |
97 | } | |
98 | ||
99 | test_fsinfo_file_cprotect_count(); | |
100 | ||
101 | return 0; | |
102 | } | |
103 | ||
104 | #endif // TARGET_OS_EMBEDDED |