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"
28 #include <sys/fcntl.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");
47 open_file (const char *filename, int flags)
49 int fd = open (filename, flags | O_WRONLY, 0500);
62 create_file (const char *filename)
64 ASSERT (open_file (filename, O_CREAT | O_EXCL));
68 do_stat (const char *filename, struct stat *p)
70 ASSERT (stat (filename, p) == 0);
74 prepare_test (struct stat *statinfo, struct timespec *modtimes)
78 create_file ("t-stt-stamp1");
80 create_file ("t-stt-testfile");
82 create_file ("t-stt-stamp2");
84 ASSERT (rename ("t-stt-testfile", "t-stt-renamed") == 0);
86 create_file ("t-stt-stamp3");
88 do_stat ("t-stt-stamp1", &statinfo[0]);
89 do_stat ("t-stt-renamed", &statinfo[1]);
90 do_stat ("t-stt-stamp2", &statinfo[2]);
91 do_stat ("t-stt-stamp3", &statinfo[3]);
93 /* Now use our access functions. */
94 for (i = 0; i < NFILES; ++i)
96 modtimes[i] = get_stat_mtime (&statinfo[i]);
101 test_mtime (const struct stat *statinfo, struct timespec *modtimes)
105 /* Use the struct stat fields directly. */
106 ASSERT (statinfo[0].st_mtime < statinfo[2].st_mtime); /* mtime(stamp1) < mtime(stamp2) */
107 ASSERT (statinfo[2].st_mtime < statinfo[3].st_mtime); /* mtime(stamp2) < mtime(stamp3) */
108 ASSERT (statinfo[2].st_mtime < statinfo[1].st_ctime); /* mtime(stamp2) < ctime(renamed) */
110 /* Now check the result of the access functions. */
111 ASSERT (modtimes[0].tv_sec < modtimes[2].tv_sec); /* mtime(stamp1) < mtime(stamp2) */
112 ASSERT (modtimes[2].tv_sec < modtimes[3].tv_sec); /* mtime(stamp2) < mtime(stamp3) */
114 /* verify equivalence */
115 for (i = 0; i < NFILES; ++i)
118 ts = get_stat_mtime (&statinfo[i]);
119 ASSERT (ts.tv_sec == statinfo[i].st_mtime);
122 ASSERT (statinfo[2].st_mtime < statinfo[1].st_ctime); /* mtime(stamp2) < ctime(renamed) */
126 test_birthtime (const struct stat *statinfo,
127 const struct timespec *modtimes,
128 struct timespec *birthtimes)
132 /* Collect the birth times.. */
133 for (i = 0; i < NFILES; ++i)
135 if (!get_stat_birthtime (&statinfo[i], &birthtimes[i]))
141 ASSERT (modtimes[0].tv_sec < birthtimes[1].tv_sec); /* mtime(stamp1) < birthtime(renamed) */
142 ASSERT (birthtimes[1].tv_sec < modtimes[2].tv_sec); /* birthtime(renamed) < mtime(stamp2) */
148 struct stat statinfo[NFILES];
149 struct timespec modtimes[NFILES];
150 struct timespec birthtimes[NFILES];
153 signal (SIGHUP, cleanup);
156 signal (SIGINT, cleanup);
159 signal (SIGQUIT, cleanup);
162 signal (SIGTERM, cleanup);
165 prepare_test (statinfo, modtimes);
166 test_mtime (statinfo, modtimes);
167 test_birthtime (statinfo, modtimes, birthtimes);