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.
34 assert(statfs("/tmp", &sfs
) == 0);
35 if (strcmp(sfs
.f_fstypename
, "hfs")) {
37 di
= disk_image_get();
38 tstdir
= di
->mount_point
;
44 asprintf(&file
, "%s/getattrlist-test.file", tstdir
);
47 int fd
= open_dprotected_np(file
, O_RDWR
| O_CREAT
, 3, 0, 0666);
49 assert_with_errno(fd
>= 0);
51 struct attrlist al
= {
52 .bitmapcount
= ATTR_BIT_MAP_COUNT
,
53 .commonattr
= ATTR_CMN_NAME
| ATTR_CMN_DEVID
| ATTR_CMN_FSID
54 | ATTR_CMN_OBJTYPE
| ATTR_CMN_OBJTAG
| ATTR_CMN_OBJID
55 | ATTR_CMN_OBJPERMANENTID
| ATTR_CMN_PAROBJID
| ATTR_CMN_SCRIPT
56 | ATTR_CMN_CRTIME
| ATTR_CMN_MODTIME
| ATTR_CMN_CHGTIME
57 | ATTR_CMN_ACCTIME
| ATTR_CMN_BKUPTIME
| ATTR_CMN_FNDRINFO
58 | ATTR_CMN_OWNERID
| ATTR_CMN_GRPID
| ATTR_CMN_ACCESSMASK
59 | ATTR_CMN_FLAGS
| ATTR_CMN_GEN_COUNT
| ATTR_CMN_DOCUMENT_ID
60 | ATTR_CMN_USERACCESS
| ATTR_CMN_EXTENDED_SECURITY
61 | ATTR_CMN_UUID
| ATTR_CMN_GRPUUID
| ATTR_CMN_FILEID
62 | ATTR_CMN_PARENTID
| ATTR_CMN_FULLPATH
| ATTR_CMN_ADDEDTIME
63 | ATTR_CMN_DATA_PROTECT_FLAGS
| ATTR_CMN_RETURNED_ATTRS
,
69 attribute_set_t returned_attrs
;
70 struct attrreference name
;
73 fsobj_type_t obj_type
;
76 fsobj_id_t obj_perm_id
;
77 fsobj_id_t par_obj_id
;
78 text_encoding_t encoding
;
79 struct timespec cr_time
, mod_time
, chg_time
, acc_time
, backup_time
;
80 uint8_t finder_info
[32];
88 struct attrreference ext_security
;
93 struct attrreference full_path
;
94 struct timespec added_time
;
95 uint32_t protection_class
;
100 assert_no_err(fgetattrlist(fd
, &al
, &attrs
, sizeof(attrs
),
101 FSOPT_ATTR_CMN_EXTENDED
| FSOPT_PACK_INVAL_ATTRS
));
103 uint32_t expected
= al
.commonattr
^ ATTR_CMN_EXTENDED_SECURITY
;
105 #if TARGET_OS_EMBEDDED
106 if (hfs_root
== true) {
107 assert(attrs
.protection_class
== 3);
110 expected
^= ATTR_CMN_DATA_PROTECT_FLAGS
;
111 assert(attrs
.protection_class
== 0);
114 assert_equal_int(attrs
.returned_attrs
.commonattr
, expected
);
116 // Just check a few things, not everything...
118 assert(!strcmp((char *)&attrs
.name
+ attrs
.name
.attr_dataoffset
,
120 assert(strlen(TEST_FILE_NAME
) + 1 == attrs
.name
.attr_length
);
123 assert(!strcmp((char *)&attrs
.full_path
+ attrs
.full_path
.attr_dataoffset
,
124 realpath(file
, path
)));
125 assert(strlen(path
) + 1 == attrs
.full_path
.attr_length
);
127 time_t t
= time(NULL
);
129 assert(attrs
.cr_time
.tv_sec
> t
- 5);
130 assert(attrs
.mod_time
.tv_sec
> t
- 5);
131 assert(attrs
.chg_time
.tv_sec
> t
- 5);
132 assert(attrs
.acc_time
.tv_sec
> t
- 5);
133 assert(attrs
.backup_time
.tv_sec
== 0);
134 assert(attrs
.added_time
.tv_sec
> t
- 5);
138 assert(statfs("/bin", &sfs2
) == 0);
139 assert(strcmp(sfs2
.f_fstypename
, "hfs") == 0);
141 // Check a file on the system partition (this only valid if root == hfs)
142 assert_no_err(getattrlist("/bin/sh", &al
, &attrs
, sizeof(attrs
),
143 FSOPT_ATTR_CMN_EXTENDED
| FSOPT_PACK_INVAL_ATTRS
));
145 assert(attrs
.returned_attrs
.commonattr
== expected
);
146 assert(attrs
.protection_class
== 0);
148 assert(!strcmp((char *)&attrs
.name
+ attrs
.name
.attr_dataoffset
, "sh"));
149 assert(attrs
.name
.attr_length
== 3);
151 assert(!strcmp((char *)&attrs
.full_path
+ attrs
.full_path
.attr_dataoffset
,
153 assert(attrs
.full_path
.attr_length
== 8);
156 assert_no_err(close(fd
));
157 assert_no_err(unlink(file
));