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(condition) if (!(condition)) abort ()
38 /* Remove temporary files. */
39 unlink ("t-stt-stamp1");
40 unlink ("t-stt-testfile");
41 unlink ("t-stt-stamp2");
42 unlink ("t-stt-renamed");
43 unlink ("t-stt-stamp3");
50 open_file (const char *filename, int flags)
52 int fd = open (filename, flags | O_WRONLY, 0500);
65 create_file (const char *filename)
67 ASSERT (open_file (filename, O_CREAT | O_EXCL));
71 do_stat (const char *filename, struct stat *p)
73 ASSERT (stat (filename, p) == 0);
77 prepare_test (struct stat *statinfo, struct timespec *modtimes)
81 create_file ("t-stt-stamp1");
83 create_file ("t-stt-testfile");
85 create_file ("t-stt-stamp2");
87 ASSERT (rename ("t-stt-testfile", "t-stt-renamed") == 0);
89 create_file ("t-stt-stamp3");
91 do_stat ("t-stt-stamp1", &statinfo[0]);
92 do_stat ("t-stt-renamed", &statinfo[1]);
93 do_stat ("t-stt-stamp2", &statinfo[2]);
94 do_stat ("t-stt-stamp3", &statinfo[3]);
96 /* Now use our access functions. */
97 for (i = 0; i < NFILES; ++i)
99 modtimes[i] = get_stat_mtime (&statinfo[i]);
104 test_mtime (const struct stat *statinfo, struct timespec *modtimes)
108 /* Use the struct stat fields directly. */
109 ASSERT (statinfo[0].st_mtime < statinfo[2].st_mtime); /* mtime(stamp1) < mtime(stamp2) */
110 ASSERT (statinfo[2].st_mtime < statinfo[3].st_mtime); /* mtime(stamp2) < mtime(stamp3) */
111 ASSERT (statinfo[2].st_mtime < statinfo[1].st_ctime); /* mtime(stamp2) < ctime(renamed) */
113 /* Now check the result of the access functions. */
114 ASSERT (modtimes[0].tv_sec < modtimes[2].tv_sec); /* mtime(stamp1) < mtime(stamp2) */
115 ASSERT (modtimes[2].tv_sec < modtimes[3].tv_sec); /* mtime(stamp2) < mtime(stamp3) */
117 /* verify equivalence */
118 for (i = 0; i < NFILES; ++i)
121 ts = get_stat_mtime (&statinfo[i]);
122 ASSERT (ts.tv_sec == statinfo[i].st_mtime);
125 ASSERT (statinfo[2].st_mtime < statinfo[1].st_ctime); /* mtime(stamp2) < ctime(renamed) */
129 test_birthtime (const struct stat *statinfo,
130 const struct timespec *modtimes,
131 struct timespec *birthtimes)
135 /* Collect the birth times.. */
136 for (i = 0; i < NFILES; ++i)
138 birthtimes[i] = get_stat_birthtime (&statinfo[i]);
139 if (birthtimes[i].tv_nsec < 0)
143 ASSERT (modtimes[0].tv_sec < birthtimes[1].tv_sec); /* mtime(stamp1) < birthtime(renamed) */
144 ASSERT (birthtimes[1].tv_sec < modtimes[2].tv_sec); /* birthtime(renamed) < mtime(stamp2) */
150 struct stat statinfo[NFILES];
151 struct timespec modtimes[NFILES];
152 struct timespec birthtimes[NFILES];
155 signal (SIGHUP, cleanup);
158 signal (SIGINT, cleanup);
161 signal (SIGQUIT, cleanup);
164 signal (SIGTERM, cleanup);
167 prepare_test (statinfo, modtimes);
168 test_mtime (statinfo, modtimes);
169 test_birthtime (statinfo, modtimes, birthtimes);