2007-03-27 Bruno Haible <bruno@clisp.org>
[gnulib.git] / tests / test-stat-time.c
1 /* Test of <stat-time.h>.
2    Copyright (C) 2007 Free Software Foundation, Inc.
3
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)
7    any later version.
8
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.
13
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.  */
17
18 /* Written by James Youngman <jay@gnu.org>, 2007.  */
19
20 #include <config.h>
21
22 #include "stat-time.h"
23
24 #include <signal.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <sys/stat.h>
28 #include <sys/fcntl.h>
29 #include <unistd.h>
30
31 #define ASSERT(condition) if (!(condition)) abort ()
32
33 enum { NFILES = 4 };
34
35 static void
36 cleanup (int sig)
37 {
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");
44 }
45
46 static int
47 open_file (const char *filename, int flags)
48 {
49   int fd = open (filename, flags | O_WRONLY, 0500);
50   if (fd >= 0)
51     {
52       close (fd);
53       return 1;
54     }
55   else
56     {
57       return 0;
58     }
59 }
60
61 static void
62 create_file (const char *filename)
63 {
64   ASSERT (open_file (filename, O_CREAT | O_EXCL));
65 }
66
67 static void
68 do_stat (const char *filename, struct stat *p)
69 {
70   ASSERT (stat (filename, p) == 0);
71 }
72
73 static void
74 prepare_test (struct stat *statinfo, struct timespec *modtimes)
75 {
76   int i;
77
78   create_file ("t-stt-stamp1");
79   sleep (2);
80   create_file ("t-stt-testfile");
81   sleep (2);
82   create_file ("t-stt-stamp2");
83   sleep (2);
84   ASSERT (rename ("t-stt-testfile", "t-stt-renamed") == 0);
85   sleep (2);
86   create_file ("t-stt-stamp3");
87
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]);
92
93   /* Now use our access functions. */
94   for (i = 0; i < NFILES; ++i)
95     {
96       modtimes[i] = get_stat_mtime (&statinfo[i]);
97     }
98 }
99
100 static void
101 test_mtime (const struct stat *statinfo, struct timespec *modtimes)
102 {
103   int i;
104
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) */
109
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) */
113
114   /* verify equivalence */
115   for (i = 0; i < NFILES; ++i)
116     {
117       struct timespec ts;
118       ts = get_stat_mtime (&statinfo[i]);
119       ASSERT (ts.tv_sec == statinfo[i].st_mtime);
120     }
121
122   ASSERT (statinfo[2].st_mtime < statinfo[1].st_ctime); /* mtime(stamp2) < ctime(renamed) */
123 }
124
125 static void
126 test_birthtime (const struct stat *statinfo,
127                 const struct timespec *modtimes,
128                 struct timespec *birthtimes)
129 {
130   int i;
131
132   /* Collect the birth times.. */
133   for (i = 0; i < NFILES; ++i)
134     {
135       if (!get_stat_birthtime (&statinfo[i], &birthtimes[i]))
136         {
137           return;
138         }
139     }
140
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) */
143 }
144
145 int
146 main ()
147 {
148   struct stat statinfo[NFILES];
149   struct timespec modtimes[NFILES];
150   struct timespec birthtimes[NFILES];
151
152 #ifdef SIGHUP
153   signal (SIGHUP, cleanup);
154 #endif
155 #ifdef SIGINT
156   signal (SIGINT, cleanup);
157 #endif
158 #ifdef SIGQUIT
159   signal (SIGQUIT, cleanup);
160 #endif
161 #ifdef SIGTERM
162   signal (SIGTERM, cleanup);
163 #endif
164
165   prepare_test (statinfo, modtimes);
166   test_mtime (statinfo, modtimes);
167   test_birthtime (statinfo, modtimes, birthtimes);
168
169   cleanup (0);
170   return 0;
171 }