5 // Created by Chris Suter on 8/21/15.
9 #import <TargetConditionals.h>
13 #include <sys/mount.h>
14 #include <sys/kauth.h>
15 #include <sys/param.h>
17 #include "hfs-tests.h"
18 #include "test-utils.h"
19 #include "disk-image.h"
25 #define TEST_FILE_NAME "getattrlist-test.file"
27 int run_getattrlist(__unused test_ctx_t
*ctx
)
29 disk_image_t
*di
= NULL
;
31 bool hfs_root
= true; //true until proven otherwise.
33 #if TARGET_OS_EMBEDDED
36 assert(statfs("/tmp", &sfs
) == 0);
37 if (strcmp(sfs
.f_fstypename
, "hfs")) {
39 di
= disk_image_get();
40 tstdir
= di
->mount_point
;
44 #else // !TARGET_OS_EMBEDDED
45 di
= disk_image_get();
46 tstdir
= di
->mount_point
;
50 asprintf(&file
, "%s/getattrlist-test.file", tstdir
);
53 int fd
= open_dprotected_np(file
, O_RDWR
| O_CREAT
, 3, 0, 0666);
55 assert_with_errno(fd
>= 0);
57 struct attrlist al
= {
58 .bitmapcount
= ATTR_BIT_MAP_COUNT
,
59 .commonattr
= ATTR_CMN_NAME
| ATTR_CMN_DEVID
| ATTR_CMN_FSID
60 | ATTR_CMN_OBJTYPE
| ATTR_CMN_OBJTAG
| ATTR_CMN_OBJID
61 | ATTR_CMN_OBJPERMANENTID
| ATTR_CMN_PAROBJID
| ATTR_CMN_SCRIPT
62 | ATTR_CMN_CRTIME
| ATTR_CMN_MODTIME
| ATTR_CMN_CHGTIME
63 | ATTR_CMN_ACCTIME
| ATTR_CMN_BKUPTIME
| ATTR_CMN_FNDRINFO
64 | ATTR_CMN_OWNERID
| ATTR_CMN_GRPID
| ATTR_CMN_ACCESSMASK
65 | ATTR_CMN_FLAGS
| ATTR_CMN_GEN_COUNT
| ATTR_CMN_DOCUMENT_ID
66 | ATTR_CMN_USERACCESS
| ATTR_CMN_EXTENDED_SECURITY
67 | ATTR_CMN_UUID
| ATTR_CMN_GRPUUID
| ATTR_CMN_FILEID
68 | ATTR_CMN_PARENTID
| ATTR_CMN_FULLPATH
| ATTR_CMN_ADDEDTIME
69 | ATTR_CMN_DATA_PROTECT_FLAGS
| ATTR_CMN_RETURNED_ATTRS
,
75 attribute_set_t returned_attrs
;
76 struct attrreference name
;
79 fsobj_type_t obj_type
;
82 fsobj_id_t obj_perm_id
;
83 fsobj_id_t par_obj_id
;
84 text_encoding_t encoding
;
85 struct timespec cr_time
, mod_time
, chg_time
, acc_time
, backup_time
;
86 uint8_t finder_info
[32];
94 struct attrreference ext_security
;
99 struct attrreference full_path
;
100 struct timespec added_time
;
101 uint32_t protection_class
;
106 assert_no_err(fgetattrlist(fd
, &al
, &attrs
, sizeof(attrs
),
107 FSOPT_ATTR_CMN_EXTENDED
| FSOPT_PACK_INVAL_ATTRS
));
109 uint32_t expected
= al
.commonattr
^ ATTR_CMN_EXTENDED_SECURITY
;
111 #if TARGET_OS_EMBEDDED
112 if (hfs_root
== true) {
113 assert(attrs
.protection_class
== 3);
116 expected
^= ATTR_CMN_DATA_PROTECT_FLAGS
;
117 assert(attrs
.protection_class
== 0);
120 assert_equal_int(attrs
.returned_attrs
.commonattr
, expected
);
122 // Just check a few things, not everything...
124 assert(!strcmp((char *)&attrs
.name
+ attrs
.name
.attr_dataoffset
,
126 assert(strlen(TEST_FILE_NAME
) + 1 == attrs
.name
.attr_length
);
129 assert(!strcmp((char *)&attrs
.full_path
+ attrs
.full_path
.attr_dataoffset
,
130 realpath(file
, path
)));
131 assert(strlen(path
) + 1 == attrs
.full_path
.attr_length
);
133 time_t t
= time(NULL
);
135 assert(attrs
.cr_time
.tv_sec
> t
- 5);
136 assert(attrs
.mod_time
.tv_sec
> t
- 5);
137 assert(attrs
.chg_time
.tv_sec
> t
- 5);
138 assert(attrs
.acc_time
.tv_sec
> t
- 5);
139 assert(attrs
.backup_time
.tv_sec
== 0);
140 assert(attrs
.added_time
.tv_sec
> t
- 5);
144 assert(statfs("/bin", &sfs2
) == 0);
145 assert(strcmp(sfs2
.f_fstypename
, "hfs") == 0);
147 // Check a file on the system partition (this only valid if root == hfs)
148 assert_no_err(getattrlist("/bin/sh", &al
, &attrs
, sizeof(attrs
),
149 FSOPT_ATTR_CMN_EXTENDED
| FSOPT_PACK_INVAL_ATTRS
));
151 assert(attrs
.returned_attrs
.commonattr
== expected
);
152 assert(attrs
.protection_class
== 0);
154 assert(!strcmp((char *)&attrs
.name
+ attrs
.name
.attr_dataoffset
, "sh"));
155 assert(attrs
.name
.attr_length
== 3);
157 assert(!strcmp((char *)&attrs
.full_path
+ attrs
.full_path
.attr_dataoffset
,
159 assert(attrs
.full_path
.attr_length
== 8);
162 assert_no_err(close(fd
));
163 assert_no_err(unlink(file
));