source: rtems/testsuites/fstests/fstime/test.c @ bc75887

4.11
Last change on this file since bc75887 was bc75887, checked in by Sebastian Huber <sebastian.huber@…>, on Mar 16, 2014 at 3:15:33 PM

tests/fstests: Use <rtems/test.h>

  • Property mode set to 100644
File size: 8.4 KB
Line 
1/*
2 *  COPYRIGHT (c) 1989-2011.
3 *  On-Line Applications Research Corporation (OAR).
4 *
5 *  The license and distribution terms for this file may be
6 *  found in the file LICENSE in this distribution or at
7 *  http://www.rtems.com/license/LICENSE.
8 */
9
10#ifdef HAVE_CONFIG_H
11#include "config.h"
12#endif
13
14#include <sys/stat.h>
15#include <limits.h>
16#include <fcntl.h>
17#include <errno.h>
18#include <stdio.h>
19#include <stdint.h>
20#include <stdlib.h>
21#include <string.h>
22#include <unistd.h>
23#include <utime.h>
24
25#ifdef __rtems__
26#include "fstest.h"
27#include "fs_config.h"
28#include "pmacros.h"
29
30const char rtems_test_name[] = "FSTIME " FILESYSTEM;
31#else
32#include <assert.h>
33#define rtems_test_assert(x) assert(x)
34#define TIME_PRECISION  (2)
35#define TIME_EQUAL(x,y) (abs((x)-(y))<TIME_PRECISION)
36#endif
37
38static int do_create(const char *path, int oflag, mode_t mode)
39{
40  int fd = open (path, O_CREAT | oflag, mode);
41  rtems_test_assert (fd >= 0);
42
43  return fd;
44}
45
46static int do_open(const char *path, int oflag)
47{
48  int fd = open (path, O_CREAT | oflag);
49  rtems_test_assert (fd >= 0);
50
51  return fd;
52}
53
54static void time_test01 (void)
55{
56  struct stat st;
57  struct utimbuf timbuf;
58  int status;
59  int fd;
60  time_t creation_time;
61  time_t truncation_time;
62  time_t dir01_creation_time;
63  char databuf[] = "TEST";
64  char readbuf[sizeof(databuf)];
65  const char *file01 = "test01";
66  const char *file02 = "test02";
67  const char *file03 = "test03";
68  const char *dir01 = "dir01";
69
70  int n;
71  int len = strlen (databuf);
72
73  const char *wd = __func__;
74  mode_t mode = S_IRWXU | S_IRWXG | S_IRWXO;
75  /*
76   * Create a new directory and change to this
77   */
78  status = mkdir (wd, mode);
79  rtems_test_assert (status == 0);
80  status = chdir (wd);
81  rtems_test_assert (status == 0);
82
83  /*
84   * Sleep a few seconds
85   */
86  puts ("Sleep a few seconds");
87  sleep (3 * TIME_PRECISION);
88
89  /*
90   * Create the test files
91   */
92  fd = do_create (file01, O_WRONLY, mode);
93  n = write (fd, databuf, len);
94  rtems_test_assert (n == len);
95  status = close (fd);
96  rtems_test_assert (status == 0);
97
98  fd = do_create (file02, O_WRONLY, mode);
99  n = write (fd, databuf, len);
100  rtems_test_assert (n == len);
101  status = close (fd);
102  rtems_test_assert (status == 0);
103
104  /* A simple C version of touch */
105  fd = do_create (file03, O_WRONLY, mode);
106  status = close (fd);
107  rtems_test_assert (status == 0);
108
109  /*
110   * If O_CREAT is set and the file did not previously exist, upon
111   * successful completion, open() shall mark for update the st_atime,
112   * st_ctime, and st_mtime fields of the file and the st_ctime and
113   * st_mtime fields of the parent directory.
114   */
115  status = stat (file01, &st);
116  rtems_test_assert (status == 0);
117
118  /*
119   * Make sure they are the same
120   */
121  rtems_test_assert (st.st_ctime == st.st_mtime);
122
123  creation_time = st.st_ctime;
124
125  status = stat (".", &st);
126  rtems_test_assert (status == 0);
127
128  /*
129   * Make sure they are the same
130   */
131  rtems_test_assert (st.st_ctime == st.st_mtime);
132  rtems_test_assert (TIME_EQUAL (creation_time, st.st_mtime));
133  rtems_test_assert (TIME_EQUAL (creation_time, st.st_ctime));
134
135  status = stat (file02, &st);
136  rtems_test_assert (status == 0);
137
138  /*
139   * Make sure they are the same
140   */
141  rtems_test_assert (st.st_ctime == st.st_mtime);
142  rtems_test_assert (TIME_EQUAL (creation_time, st.st_mtime));
143  rtems_test_assert (TIME_EQUAL (creation_time, st.st_ctime));
144
145  status = stat (file03, &st);
146  rtems_test_assert (status == 0);
147
148  /*
149   * Make sure they are the same
150   */
151  rtems_test_assert (st.st_ctime == st.st_mtime);
152  rtems_test_assert (TIME_EQUAL (creation_time, st.st_mtime));
153  rtems_test_assert (TIME_EQUAL (creation_time, st.st_ctime));
154
155  /*
156   * Sleep a few seconds
157   */
158  puts ("Sleep a few seconds");
159  sleep (3 * TIME_PRECISION);
160
161  /*
162   * Create an empty directory
163   */
164  status = mkdir (dir01, mode);
165  rtems_test_assert (status == 0);
166
167  /*
168   * truncate file01 to len, so it does not changes the file size
169   */
170  status = truncate (file01, len);
171  rtems_test_assert (status == 0);
172
173  /*
174   *truncate file02 to len+1, it changes the file size
175   */
176  status = truncate (file02, len + 1);
177  rtems_test_assert (status == 0);
178
179  /*
180   * Truncate an empty file which does not change the length.
181   */
182  fd = do_open (file03, O_TRUNC | O_WRONLY);
183  status = close (fd);
184  rtems_test_assert (status == 0);
185
186  /*
187   * ftruncate() and open() with O_TRUNC shall upon successful completion mark
188   * for update the st_ctime and st_mtime fields of the file.
189   *
190   * truncate() shall upon successful completion, if the file size is changed,
191   * mark for update the st_ctime and st_mtime fields of the file.
192   *
193   * The POSIX standard "The Open Group Base Specifications Issue 7", IEEE Std
194   * 1003.1, 2013 Edition says nothing about the behaviour of truncate() if the
195   * file size remains unchanged.
196   *
197   * Future directions of the standard may mandate the behaviour specified in
198   * ftruncate():
199   *
200   * http://austingroupbugs.net/view.php?id=489
201   */
202
203  /*
204   * file01 is currently unspecified
205   */
206  status = stat (file01, &st);
207  rtems_test_assert (status == 0);
208
209  rtems_test_assert (st.st_ctime == st.st_mtime);
210  if (TIME_EQUAL (creation_time, st.st_ctime)) {
211    puts ("WARNING: truncate() behaviour may violate future POSIX standard");
212  }
213
214  truncation_time = st.st_ctime;
215
216  /*
217   * file02 shall update
218   */
219  status = stat (file02, &st);
220  rtems_test_assert (status == 0);
221
222  rtems_test_assert (st.st_ctime == st.st_mtime);
223  rtems_test_assert (!TIME_EQUAL (creation_time, st.st_ctime));
224
225  /*
226   * file03 shall update
227   */
228  status = stat (file03, &st);
229  rtems_test_assert (status == 0);
230
231  rtems_test_assert (st.st_ctime == st.st_mtime);
232  rtems_test_assert (!TIME_EQUAL (creation_time, st.st_ctime));
233
234  /*
235   *  Upon successful completion, mkdir() shall mark for update the
236   *  st_atime, st_ctime, and st_mtime fields of the directory.
237   *  Also, the st_ctime and st_mtime fields of the directory that
238   *  contains the new entry shall be marked for update.
239   */
240  status = stat (dir01, &st);
241  rtems_test_assert (status == 0);
242
243  rtems_test_assert (st.st_ctime == st.st_mtime);
244
245  dir01_creation_time = st.st_ctime;
246
247  status = stat (".", &st);
248  rtems_test_assert (status == 0);
249
250  rtems_test_assert (st.st_ctime == st.st_mtime);
251  rtems_test_assert (TIME_EQUAL (dir01_creation_time, st.st_mtime));
252
253  /*
254   * Sleep a few seconds
255   */
256  puts ("Sleep a few seconds");
257  sleep (3 * TIME_PRECISION);
258
259  /*
260   * Upon successful completion, where nbyte is greater than 0,
261   * write() shall mark for update the st_ctime and st_mtime fields of the file
262   */
263
264  /*
265   * read file01, and this should not uptate st_mtime and st_ctime
266   */
267  fd = do_open (file01, O_RDONLY);
268  n = read (fd, readbuf, len);
269  rtems_test_assert (n == len);
270  status = fstat (fd, &st);
271  rtems_test_assert (status == 0);
272
273  rtems_test_assert (st.st_ctime == st.st_mtime);
274  rtems_test_assert (TIME_EQUAL (truncation_time, st.st_mtime));
275
276  status = close (fd);
277  rtems_test_assert (status == 0);
278  /*
279   * write file01, and this should uptate st_mtime st_ctime
280   */
281  fd = do_open (file01, O_WRONLY);
282  n = write (fd, databuf, len);
283  rtems_test_assert (n == len);
284  status = fstat (fd, &st);
285
286  rtems_test_assert (st.st_ctime == st.st_mtime);
287  rtems_test_assert (!TIME_EQUAL (truncation_time, st.st_mtime));
288  status = close (fd);
289  rtems_test_assert (status == 0);
290
291  /*
292   * The utime() function shall set the access and modification times
293   *  of the file named by the path argument.
294   */
295  timbuf.actime = creation_time;
296  timbuf.modtime = creation_time;
297
298  status = utime (file01, &timbuf);
299  rtems_test_assert (status == 0);
300
301  status = stat (file01, &st);
302  rtems_test_assert (status == 0);
303
304  rtems_test_assert (st.st_atime == st.st_mtime);
305  rtems_test_assert (TIME_EQUAL (creation_time, st.st_atime));
306  rtems_test_assert (!TIME_EQUAL (creation_time, st.st_ctime));
307
308  status = utime (dir01, &timbuf);
309  rtems_test_assert (status == 0);
310
311  status = stat (dir01, &st);
312  rtems_test_assert (status == 0);
313
314  rtems_test_assert (st.st_atime == st.st_mtime);
315  rtems_test_assert (TIME_EQUAL (creation_time, st.st_atime));
316  rtems_test_assert (!TIME_EQUAL (creation_time, st.st_ctime));
317}
318
319/*
320 * These tests only get time_t value, and test
321 * if they are changed. Thest tests don't check atime
322 */
323#ifdef __rtems__
324void test (void)
325#else
326int main(int argc, char **argv)
327#endif
328{
329
330  puts( "\n\n*** TIME TEST ***" );
331  time_test01();
332  puts( "*** END OF TIME TEST ***" );
333
334#ifndef __rtems__
335  return 0;
336#endif
337}
Note: See TracBrowser for help on using the repository browser.