]> git.saurik.com Git - apple/hfs.git/blame_incremental - tests/cases/test-dateadded.c
hfs-366.70.1.tar.gz
[apple/hfs.git] / tests / cases / test-dateadded.c
... / ...
CommitLineData
1//
2// test-dateadded.c
3// hfs
4//
5// Created by csuter on 8/28/15.
6//
7//
8
9#include <fcntl.h>
10#include <sys/attr.h>
11#include <unistd.h>
12#include <sys/stat.h>
13
14#include "hfs-tests.h"
15#include "test-utils.h"
16#include "../core/hfs_fsctl.h"
17#include "disk-image.h"
18
19TEST(dateadded)
20
21#define TIME_TO_SET 1440807730
22
23int run_dateadded(__unused test_ctx_t *ctx)
24{
25 disk_image_t *di = disk_image_get();
26 char *file, *file2, *file3, *dir;
27
28 asprintf(&file, "%s/test-dateadded.file", di->mount_point);
29 asprintf(&file2, "%s/test-dateadded.file2", di->mount_point);
30 asprintf(&dir, "%s/test-dateadded.dir", di->mount_point);
31 asprintf(&file3, "%s/file3", dir);
32
33 int fd = open(file, O_RDWR | O_CREAT, 0666);
34 assert_with_errno(fd >= 0);
35
36 struct attrlist al = {
37 .bitmapcount = ATTR_BIT_MAP_COUNT,
38 .commonattr = ATTR_CMN_ADDEDTIME
39 };
40
41#pragma pack(push, 4)
42 struct {
43 uint32_t len;
44 struct timespec added_time;
45 } attrs;
46#pragma pack(pop)
47
48 assert_no_err(fgetattrlist(fd, &al, &attrs, sizeof(attrs), 0));
49
50 struct timespec orig = attrs.added_time;
51
52 // Make sure rename doesn’t change it
53 rename(file, file2);
54
55 attrs.added_time.tv_sec = 0;
56 assert_no_err(getattrlist(file2, &al, &attrs, sizeof(attrs), 0));
57
58 assert_equal(attrs.added_time.tv_sec, orig.tv_sec, "%ld");
59
60 sleep(2);
61
62 // Rename to a different directory should change it
63 rmdir(dir);
64 mkdir(dir, 0777);
65
66 assert_no_err(rename(file2, file3));
67
68 attrs.added_time.tv_sec = 0;
69 assert_no_err(getattrlist(file3, &al, &attrs, sizeof(attrs), 0));
70
71 assert(attrs.added_time.tv_sec >= orig.tv_sec
72 && attrs.added_time.tv_sec < orig.tv_sec + 10);
73
74#if 0 // Not supported until VFS work is done
75 attrs.added_time.tv_sec = TIME_TO_SET;
76
77 assert_no_err(fsetattrlist(fd, &al, (void *)&attrs + 4,
78 sizeof(attrs) - 4, 0));
79
80 attrs.added_time.tv_sec = 0;
81 assert_no_err(fgetattrlist(fd, &al, &attrs, sizeof(attrs), 0));
82
83 assert_equal(attrs.added_time.tv_sec, TIME_TO_SET, "%ld");
84#endif
85
86 assert_no_err(unlink(file3));
87 assert_no_err(rmdir(dir));
88
89 return 0;
90}