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

4.115
Last change on this file since d957805 was d957805, checked in by Ralf Corsepius <ralf.corsepius@…>, on 09/07/11 at 07:42:12

2011-09-07 Ralf Corsépius <ralf.corsepius@…>

  • fserror/test.c, fslink/test.c, fspatheval/test.c, fspermission/test.c, fsrdwr/init.c, fssymlink/test.c, fstime/test.c, support/fstest_support.c, support/ramdisk_support.c: Add config-header support.
  • Property mode set to 100644
File size: 6.7 KB
Line 
1
2/*
3 *  COPYRIGHT (c) 1989-2011.
4 *  On-Line Applications Research Corporation (OAR).
5 *
6 *  The license and distribution terms for this file may be
7 *  found in the file LICENSE in this distribution or at
8 *  http://www.rtems.com/license/LICENSE.
9 *
10 *  $Id$
11 */
12
13#ifdef HAVE_CONFIG_H
14#include "config.h"
15#endif
16
17#include <sys/stat.h>
18#include <limits.h>
19#include <fcntl.h>
20#include <errno.h>
21#include <stdio.h>
22#include <stdint.h>
23#include <stdlib.h>
24#include <string.h>
25#include <unistd.h>
26#include <utime.h>
27#include "fstest.h"
28
29void time_test01 (void)
30{
31  struct stat statbuf;
32  struct utimbuf timbuf;
33  int status = 0;
34  int fd;
35  time_t ctime1, mtime1;
36  time_t ctime2, mtime2;
37  char *readbuf;
38  char *databuf = "TEST";
39  char *file01 = "test01";
40  char *file02 = "test02";
41  char *dir01 = "dir01";
42
43  int n;
44  int len = strlen (databuf);
45
46  const char *wd = __func__;
47  mode_t mode = S_IRWXU | S_IRWXG | S_IRWXO;
48  /*
49   * Create a new directory and change to this
50   */
51  status = mkdir (wd, mode);
52  rtems_test_assert (status == 0);
53  status = chdir (wd);
54  rtems_test_assert (status == 0);
55
56  /*
57   * Create two files
58   */
59  fd = open (file01, O_CREAT | O_WRONLY, mode);
60  n = write (fd, databuf, len);
61  rtems_test_assert (n == len);
62  status = close (fd);
63  rtems_test_assert (status == 0);
64
65  fd = open (file02, O_CREAT | O_WRONLY, mode);
66  n = write (fd, databuf, len);
67  rtems_test_assert (n == len);
68  status = close (fd);
69  rtems_test_assert (status == 0);
70  /*
71   * If O_CREAT is set and the file did not previously exist, upon
72   * successful completion, open() shall mark for update the st_atime,
73   * st_ctime, and st_mtime fields of the file and the st_ctime and
74   * st_mtime fields of the parent directory.
75   */
76  status = stat (file01, &statbuf);
77  rtems_test_assert (status == 0);
78  ctime1 = statbuf.st_ctime;
79  mtime1 = statbuf.st_mtime;
80
81  status = stat (".", &statbuf);
82  rtems_test_assert (status == 0);
83  ctime2 = statbuf.st_ctime;
84  mtime2 = statbuf.st_mtime;
85
86  /*
87   * Make sure they are the same
88   */
89
90  rtems_test_assert (TIME_EQUAL (ctime1, mtime1));
91  rtems_test_assert (TIME_EQUAL (ctime1, mtime2));
92  rtems_test_assert (TIME_EQUAL (ctime1, ctime2));
93
94  status = stat (file02, &statbuf);
95  rtems_test_assert (status == 0);
96  ctime1 = statbuf.st_ctime;
97  mtime1 = statbuf.st_mtime;
98
99  status = stat (".", &statbuf);
100  rtems_test_assert (status == 0);
101  ctime2 = statbuf.st_ctime;
102  mtime2 = statbuf.st_mtime;
103
104  /*
105   * Make sure they are the same
106   */
107  rtems_test_assert (TIME_EQUAL (ctime1, mtime1));
108  rtems_test_assert (TIME_EQUAL (ctime1, mtime2));
109  rtems_test_assert (TIME_EQUAL (ctime1, ctime2));
110
111  /*
112   * Sleep a few seconds
113   */
114  puts ("Sleep a few seconds");
115
116  sleep (TIME_PRECISION);
117
118  /*
119   * Create an empty directory
120   */
121  status = mkdir (dir01, mode);
122  rtems_test_assert (status == 0);
123  /*
124   * truncate file01 to len, so it does not changes the file size
125   */
126  status = truncate (file01, len);
127  rtems_test_assert (status == 0);
128
129  /*
130   *truncate file02 to len+1, it changes the file size
131   */
132  status = truncate (file02, len + 1);
133  rtems_test_assert (status == 0);
134
135  /*
136   *
137   *  truncate shall not modify the file offset for any open file
138   *   descriptions associated with the file. Upon successful completion,
139   *   if the file size is changed, this function shall mark for update
140   *   the st_ctime and st_mtime fields of the file
141   */
142
143  /*
144   * file01 shall not update
145   */
146  status = stat (file01, &statbuf);
147  rtems_test_assert (status == 0);
148  ctime2 = statbuf.st_ctime;
149  mtime2 = statbuf.st_mtime;
150
151  rtems_test_assert (TIME_EQUAL (ctime1, mtime2));
152  rtems_test_assert (TIME_EQUAL (ctime1, ctime2));
153
154  /*
155   * file02 shall update
156   */
157  status = stat (file02, &statbuf);
158  rtems_test_assert (status == 0);
159  ctime2 = statbuf.st_ctime;
160  mtime2 = statbuf.st_mtime;
161
162  rtems_test_assert (TIME_EQUAL (ctime2, mtime2));
163  rtems_test_assert (!TIME_EQUAL (ctime1, mtime2));
164  rtems_test_assert (!TIME_EQUAL (ctime1, ctime2));
165
166  /*
167   *  Upon successful completion, mkdir() shall mark for update the
168   *  5st_atime, st_ctime, and st_mtime fields of the directory.
169   *  Also, the st_ctime and st_mtime fields of the directory that
170   *  contains the new entry shall be marked for update.
171   */
172  status = stat (dir01, &statbuf);
173  rtems_test_assert (status == 0);
174  ctime2 = statbuf.st_ctime;
175  mtime2 = statbuf.st_mtime;
176  rtems_test_assert (TIME_EQUAL (ctime2, mtime2));
177  rtems_test_assert (!TIME_EQUAL (ctime1, mtime2));
178  rtems_test_assert (!TIME_EQUAL (ctime1, ctime2));
179
180  status = stat (".", &statbuf);
181  rtems_test_assert (status == 0);
182  ctime2 = statbuf.st_ctime;
183  mtime2 = statbuf.st_mtime;
184
185  rtems_test_assert (!TIME_EQUAL (ctime1, ctime2));
186  rtems_test_assert (!TIME_EQUAL (ctime1, mtime2));
187
188  /*
189   * Upon successful completion, where nbyte is greater than 0,
190   * write() shall mark for update the st_ctime and st_mtime fields of the file
191   */
192
193  /*
194   * read file01, and this should not uptate st_mtime and st_ctime
195   */
196  readbuf = (char *) malloc (len + 1);
197  fd = open (file01, O_RDONLY);
198  rtems_test_assert (fd != -1);
199  n = read (fd, readbuf, len);
200  rtems_test_assert (n == len);
201  status = fstat (fd, &statbuf);
202
203  ctime2 = statbuf.st_ctime;
204  mtime2 = statbuf.st_mtime;
205
206  rtems_test_assert (TIME_EQUAL (ctime1, ctime2));
207  rtems_test_assert (TIME_EQUAL (ctime1, mtime2));
208
209  status = close (fd);
210  rtems_test_assert (status == 0);
211  /*
212   * write file01, and this should uptate st_mtime st_ctime
213   */
214  readbuf = (char *) malloc (len + 1);
215  fd = open (file01, O_WRONLY);
216  rtems_test_assert (fd != -1);
217  n = write (fd, databuf, len);
218  rtems_test_assert (n == len);
219  status = fstat (fd, &statbuf);
220
221  ctime2 = statbuf.st_ctime;
222  mtime2 = statbuf.st_mtime;
223
224  rtems_test_assert (!TIME_EQUAL (ctime1, ctime2));
225  rtems_test_assert (!TIME_EQUAL (ctime1, mtime2));
226  status = close (fd);
227  rtems_test_assert (status == 0);
228
229  /*
230   * The utime() function shall set the access and modification times
231   *  of the file named by the path argument.
232   */
233  timbuf.actime = ctime1;
234  timbuf.modtime = mtime1;
235
236  status = utime (file01, &timbuf);
237  rtems_test_assert (status == 0);
238
239  status = stat (file01, &statbuf);
240  ctime2 = statbuf.st_atime;
241  mtime2 = statbuf.st_mtime;
242
243  rtems_test_assert (TIME_EQUAL (ctime1, ctime2));
244  rtems_test_assert (TIME_EQUAL (ctime1, mtime2));
245
246  status = utime (dir01, &timbuf);
247  rtems_test_assert (status == 0);
248
249  status = stat (dir01, &statbuf);
250  ctime2 = statbuf.st_atime;
251  mtime2 = statbuf.st_mtime;
252
253  rtems_test_assert (TIME_EQUAL (ctime1, ctime2));
254  rtems_test_assert (TIME_EQUAL (ctime1, mtime2));
255
256}
257
258/*
259 * These tests only get time_t value, and test
260 * if they are changed. Thest tests don't check atime
261 */
262void test (void)
263{
264
265  puts( "\n\n*** TIME TEST ***" );
266  time_test01();
267  puts( "*** END OF TIME TEST ***" );
268}
Note: See TracBrowser for help on using the repository browser.