1 /* Test of <stat-time.h>.
2 Copyright (C) 2007 Free Software Foundation, Inc.
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2, or (at your option)
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software Foundation,
16 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
18 /* Written by James Youngman <jay@gnu.org>, 2007. */
22 #include "stat-time.h"
31 #define ASSERT(expr) \
36 fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \
47 /* Remove temporary files. */
48 unlink ("t-stt-stamp1");
49 unlink ("t-stt-testfile");
50 unlink ("t-stt-stamp2");
51 unlink ("t-stt-renamed");
52 unlink ("t-stt-stamp3");
59 open_file (const char *filename, int flags)
61 int fd = open (filename, flags | O_WRONLY, 0500);
74 create_file (const char *filename)
76 ASSERT (open_file (filename, O_CREAT | O_EXCL));
80 do_stat (const char *filename, struct stat *p)
82 ASSERT (stat (filename, p) == 0);
86 prepare_test (struct stat *statinfo, struct timespec *modtimes)
90 create_file ("t-stt-stamp1");
92 create_file ("t-stt-testfile");
94 create_file ("t-stt-stamp2");
96 ASSERT (chmod ("t-stt-testfile", 0400) == 0);
98 create_file ("t-stt-stamp3");
100 do_stat ("t-stt-stamp1", &statinfo[0]);
101 do_stat ("t-stt-testfile", &statinfo[1]);
102 do_stat ("t-stt-stamp2", &statinfo[2]);
103 do_stat ("t-stt-stamp3", &statinfo[3]);
105 /* Now use our access functions. */
106 for (i = 0; i < NFILES; ++i)
108 modtimes[i] = get_stat_mtime (&statinfo[i]);
113 test_mtime (const struct stat *statinfo, struct timespec *modtimes)
117 /* Use the struct stat fields directly. */
118 ASSERT (statinfo[0].st_mtime < statinfo[2].st_mtime); /* mtime(stamp1) < mtime(stamp2) */
119 ASSERT (statinfo[2].st_mtime < statinfo[3].st_mtime); /* mtime(stamp2) < mtime(stamp3) */
120 ASSERT (statinfo[2].st_mtime < statinfo[1].st_ctime); /* mtime(stamp2) < ctime(renamed) */
122 /* Now check the result of the access functions. */
123 ASSERT (modtimes[0].tv_sec < modtimes[2].tv_sec); /* mtime(stamp1) < mtime(stamp2) */
124 ASSERT (modtimes[2].tv_sec < modtimes[3].tv_sec); /* mtime(stamp2) < mtime(stamp3) */
126 /* verify equivalence */
127 for (i = 0; i < NFILES; ++i)
130 ts = get_stat_mtime (&statinfo[i]);
131 ASSERT (ts.tv_sec == statinfo[i].st_mtime);
134 ASSERT (statinfo[2].st_mtime < statinfo[1].st_ctime); /* mtime(stamp2) < ctime(renamed) */
138 test_birthtime (const struct stat *statinfo,
139 const struct timespec *modtimes,
140 struct timespec *birthtimes)
144 /* Collect the birth times.. */
145 for (i = 0; i < NFILES; ++i)
147 birthtimes[i] = get_stat_birthtime (&statinfo[i]);
148 if (birthtimes[i].tv_nsec < 0)
152 ASSERT (modtimes[0].tv_sec < birthtimes[1].tv_sec); /* mtime(stamp1) < birthtime(renamed) */
153 ASSERT (birthtimes[1].tv_sec < modtimes[2].tv_sec); /* birthtime(renamed) < mtime(stamp2) */
159 struct stat statinfo[NFILES];
160 struct timespec modtimes[NFILES];
161 struct timespec birthtimes[NFILES];
164 signal (SIGHUP, cleanup);
167 signal (SIGINT, cleanup);
170 signal (SIGQUIT, cleanup);
173 signal (SIGTERM, cleanup);
177 prepare_test (statinfo, modtimes);
178 test_mtime (statinfo, modtimes);
179 test_birthtime (statinfo, modtimes, birthtimes);