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 3 of the License, or
7 (at your option) any later version.
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, see <http://www.gnu.org/licenses/>. */
17 /* Written by James Youngman <jay@gnu.org>, 2007. */
21 #include "stat-time.h"
30 #define ASSERT(expr) \
35 fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \
46 /* Remove temporary files. */
47 unlink ("t-stt-stamp1");
48 unlink ("t-stt-testfile");
49 unlink ("t-stt-stamp2");
50 unlink ("t-stt-renamed");
51 unlink ("t-stt-stamp3");
58 open_file (const char *filename, int flags)
60 int fd = open (filename, flags | O_WRONLY, 0500);
73 create_file (const char *filename)
75 ASSERT (open_file (filename, O_CREAT | O_EXCL));
79 do_stat (const char *filename, struct stat *p)
81 ASSERT (stat (filename, p) == 0);
85 prepare_test (struct stat *statinfo, struct timespec *modtimes)
89 create_file ("t-stt-stamp1");
91 create_file ("t-stt-testfile");
93 create_file ("t-stt-stamp2");
95 ASSERT (chmod ("t-stt-testfile", 0400) == 0);
97 create_file ("t-stt-stamp3");
99 do_stat ("t-stt-stamp1", &statinfo[0]);
100 do_stat ("t-stt-testfile", &statinfo[1]);
101 do_stat ("t-stt-stamp2", &statinfo[2]);
102 do_stat ("t-stt-stamp3", &statinfo[3]);
104 /* Now use our access functions. */
105 for (i = 0; i < NFILES; ++i)
107 modtimes[i] = get_stat_mtime (&statinfo[i]);
112 test_mtime (const struct stat *statinfo, struct timespec *modtimes)
116 /* Use the struct stat fields directly. */
117 ASSERT (statinfo[0].st_mtime < statinfo[2].st_mtime); /* mtime(stamp1) < mtime(stamp2) */
118 ASSERT (statinfo[2].st_mtime < statinfo[3].st_mtime); /* mtime(stamp2) < mtime(stamp3) */
119 ASSERT (statinfo[2].st_mtime < statinfo[1].st_ctime); /* mtime(stamp2) < ctime(renamed) */
121 /* Now check the result of the access functions. */
122 ASSERT (modtimes[0].tv_sec < modtimes[2].tv_sec); /* mtime(stamp1) < mtime(stamp2) */
123 ASSERT (modtimes[2].tv_sec < modtimes[3].tv_sec); /* mtime(stamp2) < mtime(stamp3) */
125 /* verify equivalence */
126 for (i = 0; i < NFILES; ++i)
129 ts = get_stat_mtime (&statinfo[i]);
130 ASSERT (ts.tv_sec == statinfo[i].st_mtime);
133 ASSERT (statinfo[2].st_mtime < statinfo[1].st_ctime); /* mtime(stamp2) < ctime(renamed) */
137 test_birthtime (const struct stat *statinfo,
138 const struct timespec *modtimes,
139 struct timespec *birthtimes)
143 /* Collect the birth times.. */
144 for (i = 0; i < NFILES; ++i)
146 birthtimes[i] = get_stat_birthtime (&statinfo[i]);
147 if (birthtimes[i].tv_nsec < 0)
151 ASSERT (modtimes[0].tv_sec < birthtimes[1].tv_sec); /* mtime(stamp1) < birthtime(renamed) */
152 ASSERT (birthtimes[1].tv_sec < modtimes[2].tv_sec); /* birthtime(renamed) < mtime(stamp2) */
158 struct stat statinfo[NFILES];
159 struct timespec modtimes[NFILES];
160 struct timespec birthtimes[NFILES];
163 signal (SIGHUP, cleanup);
166 signal (SIGINT, cleanup);
169 signal (SIGQUIT, cleanup);
172 signal (SIGTERM, cleanup);
176 prepare_test (statinfo, modtimes);
177 test_mtime (statinfo, modtimes);
178 test_birthtime (statinfo, modtimes, birthtimes);