source: rtems/testsuites/fstests/fsrdwr/init.c @ 32448524

4.115
Last change on this file since 32448524 was 32448524, checked in by Ralf Corsepius <ralf.corsepius@…>, on 09/30/11 at 03:07:21

2011-09-30 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: Explicitly include "pmacros.h".
  • support/fstest.h: Don't include "pmacros.h".
  • Property mode set to 100644
File size: 11.9 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 *  $Id$
10 */
11
12#ifdef HAVE_CONFIG_H
13#include "config.h"
14#endif
15
16#include <sys/stat.h>
17#include <string.h>
18#include <stdlib.h>
19#include <fcntl.h>
20#include <stdio.h>
21#include <stdlib.h>
22#include <stdint.h>
23#include <memory.h>
24#include <unistd.h>
25
26#include "fstest.h"
27#include "pmacros.h"
28
29const char *databuf =
30  "Happy days are here again.  Happy days are here again.1Happy "
31  "days are here again.2Happy days are here again.3Happy days are here again."
32  "4Happy days are here again.5Happy days are here again.6Happy days are here "
33  "again.7Happy days are here again.";
34
35void
36read_write_test (void)
37{
38
39  int fd;
40  int status;
41  char *name01 = "name01";
42  char *name02 = "name02";
43  struct stat statbuf;
44  char *readbuf;
45  size_t len = strlen (databuf);
46  off_t pos = 0;
47
48  int n;
49  mode_t mode = S_IRWXU | S_IRWXG | S_IRWXO;
50
51
52  const char *wd = __func__;
53
54  /*
55   * Create a new directory and change the current directory to  this
56   */
57  status = mkdir (wd, mode);
58  rtems_test_assert (status == 0);
59  status = chdir (wd);
60  rtems_test_assert (status == 0);
61  /*
62   * Create an empty file
63   */
64  fd = open (name01, O_CREAT | O_WRONLY, mode);
65  status = close (fd);
66  rtems_test_assert (status == 0);
67  /*
68   * Verify the empty file
69   */
70  status = stat (name01, &statbuf);
71  rtems_test_assert (status == 0);
72
73  rtems_test_assert (S_ISREG (statbuf.st_mode));
74  rtems_test_assert (statbuf.st_size == 0);
75
76  /*
77   * Write data to the empty file
78   */
79  fd = open (name01, O_WRONLY);
80  rtems_test_assert (fd != -1);
81
82  n = write (fd, databuf, len);
83  rtems_test_assert (n == len);
84  status = close (fd);
85  rtems_test_assert (status == 0);
86
87  status = stat (name01, &statbuf);
88  rtems_test_assert (status == 0);
89  rtems_test_assert (S_ISREG (statbuf.st_mode));
90  rtems_test_assert (statbuf.st_size == len);
91
92  /*
93   * Read the data from the file
94   */
95  readbuf = (char *) malloc (len + 1);
96  rtems_test_assert (readbuf);
97
98  fd = open (name01, O_RDONLY);
99  rtems_test_assert (fd != -1);
100  n = read (fd, readbuf, len);
101  rtems_test_assert (n == len);
102  rtems_test_assert (!strncmp (databuf, readbuf, len));
103  status = close (fd);
104  rtems_test_assert (status == 0);
105
106  /*
107   * Open the file using O_APPEND and write the data
108   */
109  memset (readbuf, 0, len + 1);
110  fd = open (name01, O_WRONLY | O_APPEND);
111  n = write (fd, databuf, len);
112  rtems_test_assert (n == len);
113  pos = lseek (fd, 0, SEEK_CUR);
114  rtems_test_assert (pos == 2 * len);
115  status = close (fd);
116  rtems_test_assert (status == 0);
117
118  /*
119   * Read the data and verify it
120   */
121  fd = open (name01, O_RDONLY);
122  rtems_test_assert (fd != -1);
123  n = read (fd, readbuf, len);
124  rtems_test_assert (n == len);
125  rtems_test_assert (!strncmp (databuf, readbuf, len));
126
127  n = read (fd, readbuf, len);
128  rtems_test_assert (n == len);
129  rtems_test_assert (!strncmp (databuf, readbuf, len));
130  status = close (fd);
131  rtems_test_assert (status == 0);
132
133  /*
134   * Open the file using O_RDWR
135   */
136  memset (readbuf, 0, len + 1);
137
138  fd = open (name01, O_RDWR);
139  n = write (fd, databuf, len);
140  rtems_test_assert (n == len);
141  pos = lseek (fd, 0, SEEK_CUR);
142  rtems_test_assert (pos == len);
143  n = read (fd, readbuf, len);
144  rtems_test_assert (n == len);
145  rtems_test_assert (!strncmp (databuf, readbuf, len));
146  pos = lseek (fd, 0, SEEK_CUR);
147  rtems_test_assert (pos == 2 * len);
148  status = close (fd);
149  rtems_test_assert (status == 0);
150
151  /*
152   * Open the file using O_TRUNC
153   */
154
155  fd = open (name01, O_WRONLY | O_TRUNC);
156  status = close (fd);
157  rtems_test_assert (status == 0);
158
159  /*
160   * Verify if the length is zero
161   */
162  status = stat (name01, &statbuf);
163  rtems_test_assert (status == 0);
164  rtems_test_assert (S_ISREG (statbuf.st_mode));
165  rtems_test_assert (statbuf.st_size == 0);
166
167  /*
168   * Open a directory
169   */
170  status = mkdir (name02, mode);
171  rtems_test_assert (status == 0);
172  fd = open (name02, O_RDONLY);
173  rtems_test_assert (fd != -1);
174
175  status = close (fd);
176  rtems_test_assert (status == 0);
177
178  free (readbuf);
179
180  /*
181   * Go back to parent directory
182   */
183  status = chdir ("..");
184  rtems_test_assert (status == 0);
185}
186
187void
188truncate_test03 (void)
189{
190
191  int fd;
192  int status;
193  char *name01 = "name01";
194  struct stat statbuf;
195
196  char data;
197  int n;
198  int i;
199
200  size_t len = strlen (databuf);
201
202  char *readbuf;
203  off_t good_size = 100;
204  mode_t mode = S_IRWXU | S_IRWXG | S_IRWXO;
205
206
207  const char *wd = __func__;
208
209  /*
210   * Create a new directory and change the current directory to  this
211   */
212  status = mkdir (wd, mode);
213  rtems_test_assert (status == 0);
214  status = chdir (wd);
215  rtems_test_assert (status == 0);
216
217  /*
218   * Create an empty file
219   */
220  fd = creat (name01, mode);
221  status = close (fd);
222  rtems_test_assert (status == 0);
223
224
225  /*
226   * Truncate it to a valid size
227   */
228  status = truncate (name01, good_size);
229  rtems_test_assert (status == 0);
230  /*
231   * Verify the size and the data
232   */
233  status = stat (name01, &statbuf);
234  rtems_test_assert (status == 0);
235  rtems_test_assert (good_size == statbuf.st_size);
236
237  fd = open (name01, O_RDONLY);
238  while ((n = read (fd, &data, 1)) > 0) {
239    rtems_test_assert (data == 0);
240  }
241
242  status = close (fd);
243  rtems_test_assert (status == 0);
244
245  /*
246   * Fill a file with data
247   */
248  fd = open (name01, O_WRONLY);
249  rtems_test_assert (fd != -1);
250  n = write (fd, databuf, len);
251  rtems_test_assert (n == len);
252
253  /*
254   * Truncate it to the half size
255   */
256
257  status = truncate (name01, len / 2);
258  status = truncate (name01, len);
259
260  /*
261   * verify the data
262   */
263  readbuf = (char *) malloc (len / 2);
264  rtems_test_assert (readbuf);
265  fd = open (name01, O_RDONLY);
266  rtems_test_assert (fd != -1);
267  n = read (fd, readbuf, len / 2);
268  rtems_test_assert (n == len / 2);
269  rtems_test_assert (!strncmp (databuf, readbuf, len / 2));
270  n = read (fd, readbuf, len / 2);
271  rtems_test_assert (n == len / 2);
272  for (i = 0; i < len / 2; i++) {
273    rtems_test_assert (readbuf[i] == 0);
274  }
275  status = close (fd);
276  rtems_test_assert (status == 0);
277
278  /*
279   * Go back to parent directory
280   */
281  status = chdir ("..");
282  rtems_test_assert (status == 0);
283}
284
285void
286lseek_test (void)
287{
288  int fd;
289  int status;
290  char *name01 = "test_name01";
291  struct stat statbuf;
292
293  int n;
294  int i;
295
296  size_t len = strlen (databuf);
297  off_t pos;
298  int total_written = 0;
299
300  char *readbuf;
301  mode_t mode = S_IRWXU | S_IRWXG | S_IRWXO;
302
303
304
305  const char *wd = __func__;
306
307  /*
308   * Create a new directory and change the current directory to this
309   */
310  status = mkdir (wd, mode);
311  rtems_test_assert (status == 0);
312  status = chdir (wd);
313  rtems_test_assert (status == 0);
314
315  /*
316   * Create a file and fill with the data.
317   */
318  puts ("Create a new file");
319  fd = creat (name01, mode);
320  rtems_test_assert (fd != -1);
321
322  pos = lseek (fd, 0, SEEK_CUR);
323  rtems_test_assert (pos == 0);
324
325  pos = lseek (fd, 0, SEEK_END);
326  rtems_test_assert (pos == 0);
327
328  pos = lseek (fd, 0, SEEK_SET);
329  rtems_test_assert (pos == 0);
330
331
332  printf ("Writing %d bytes to file\n", len * 10);
333  for (i = 0; i < 10; i++) {
334    n = write (fd, databuf, len);
335    rtems_test_assert (n != -1);
336    total_written += n;
337  }
338  printf ("Successfully wrote %d\n", total_written);
339
340  /*
341   * Check the current position
342   */
343  puts ("Check the current position");
344  pos = lseek (fd, 0, SEEK_CUR);
345  rtems_test_assert (pos == total_written);
346
347  pos = lseek (fd, 0, SEEK_END);
348  rtems_test_assert (pos == total_written);
349
350  /*
351   * ftruncate shall not change the posistion
352   */
353  status = ftruncate (fd, total_written + 1);
354  rtems_test_assert (status == 0);
355
356  pos = lseek (fd, 0, SEEK_CUR);
357  rtems_test_assert (pos == total_written);
358
359  pos = lseek (fd, 0, SEEK_END);
360  printf ("%jd\n", (intmax_t) pos);
361  rtems_test_assert (pos == total_written + 1);
362
363  status = ftruncate (fd, total_written);
364  rtems_test_assert (status == 0);
365
366  pos = lseek (fd, 0, SEEK_CUR);
367  rtems_test_assert (pos == total_written + 1);
368
369
370  status = close (fd);
371  rtems_test_assert (status == 0);
372
373  /*
374   * Check the file size
375   */
376  status = stat (name01, &statbuf);
377  rtems_test_assert (statbuf.st_size == total_written);
378
379  /*
380   * Open the file with O_RDONLY and check the lseek
381   */
382  readbuf = (char *) malloc (len);
383  fd = open (name01, O_RDONLY);
384  pos = lseek (fd, len, SEEK_CUR);
385  rtems_test_assert (pos == len);
386  n = read (fd, readbuf, len);
387  rtems_test_assert (n == len);
388  rtems_test_assert (!strncmp (databuf, readbuf, len));
389
390  pos = lseek (fd, len, SEEK_CUR);
391  rtems_test_assert (pos == 3 * len);
392  n = read (fd, readbuf, len);
393  rtems_test_assert (n == len);
394  rtems_test_assert (!strncmp (databuf, readbuf, len));
395
396  pos = lseek (fd, -len, SEEK_CUR);
397  rtems_test_assert (pos == 3 * len);
398  n = read (fd, readbuf, len);
399  rtems_test_assert (n == len);
400  rtems_test_assert (!strncmp (databuf, readbuf, len));
401
402  pos = lseek (fd, 4 * len, SEEK_SET);
403  n = read (fd, readbuf, len);
404  rtems_test_assert (n == len);
405  rtems_test_assert (!strncmp (databuf, readbuf, len));
406
407
408  pos = lseek (fd, 10, SEEK_SET);
409  n = read (fd, readbuf, len);
410  rtems_test_assert (n == len);
411  rtems_test_assert (strncmp (databuf, readbuf, len) != 0);
412
413  pos = lseek (fd, -len, SEEK_END);
414  n = read (fd, readbuf, 2 * len);
415  rtems_test_assert (n == len);
416  rtems_test_assert (!strncmp (databuf, readbuf, len));
417
418  status = close (fd);
419  rtems_test_assert (status == 0);
420
421  /*
422   * Open the file withe O_RDWR and check the lseek
423   */
424
425  fd = open (name01, O_RDWR);
426
427  pos = lseek (fd, len, SEEK_CUR);
428  rtems_test_assert (pos == len);
429  n = read (fd, readbuf, len);
430  rtems_test_assert (n == len);
431  rtems_test_assert (!strncmp (databuf, readbuf, len));
432
433  pos = lseek (fd, len, SEEK_CUR);
434  rtems_test_assert (pos == 3 * len);
435  n = read (fd, readbuf, len);
436  rtems_test_assert (n == len);
437  rtems_test_assert (!strncmp (databuf, readbuf, len));
438
439  pos = lseek (fd, -len, SEEK_CUR);
440  rtems_test_assert (pos == 3 * len);
441  n = read (fd, readbuf, len);
442  rtems_test_assert (n == len);
443  rtems_test_assert (!strncmp (databuf, readbuf, len));
444
445  pos = lseek (fd, 4 * len, SEEK_SET);
446  n = read (fd, readbuf, len);
447  rtems_test_assert (n == len);
448  rtems_test_assert (!strncmp (databuf, readbuf, len));
449
450  /*
451   * Go to the wrong position, so the data is not the same
452   */
453  pos = lseek (fd, 10, SEEK_SET);
454  n = read (fd, readbuf, len);
455  rtems_test_assert (n == len);
456  rtems_test_assert (strncmp (databuf, readbuf, len) != 0);
457
458  /*
459   * Use SEEK_END
460   */
461  pos = lseek (fd, -len, SEEK_END);
462  n = read (fd, readbuf, 2 * len);
463  rtems_test_assert (n == len);
464  rtems_test_assert (!strncmp (databuf, readbuf, len));
465
466  memset (readbuf, 0, len);
467
468  /*
469   * Write the zero to the end of file.
470   */
471  pos = lseek (fd, -len, SEEK_END);
472  rtems_test_assert (pos == total_written - len);
473  n = write (fd, readbuf, len);
474  rtems_test_assert (n == len);
475  /*
476   * Verify it
477   */
478  pos = lseek (fd, total_written - len, SEEK_SET);
479  n = read (fd, readbuf, len);
480  rtems_test_assert (n == len);
481  for (i = 0; i < n; i++) {
482    rtems_test_assert (readbuf[i] == 0);
483  }
484
485  /*
486   * Write the zero to the beginning of file.
487   */
488  pos = lseek (fd, -total_written, SEEK_END);
489  rtems_test_assert (pos == 0);
490  n = write (fd, readbuf, len);
491  rtems_test_assert (n == len);
492
493  /*
494   * Verify it
495   */
496
497  pos = lseek (fd, 0, SEEK_SET);
498  n = read (fd, readbuf, len);
499  rtems_test_assert (n == len);
500  for (i = 0; i < n; i++) {
501    rtems_test_assert (readbuf[i] == 0);
502  }
503
504  n = read (fd, readbuf, len);
505  rtems_test_assert (n == len);
506  rtems_test_assert (strncmp (databuf, readbuf, len) == 0);
507  /*
508   * Call ftruncate to decrease the file and the position not change
509   */
510  status = ftruncate (fd, len);
511  rtems_test_assert (status == 0);
512  pos = lseek (fd, 0, SEEK_CUR);
513  rtems_test_assert (pos == len * 2);
514
515  status = close (fd);
516  rtems_test_assert (status == 0);
517  /*
518   * Go back to parent directory
519   */
520  status = chdir ("..");
521  rtems_test_assert (status == 0);
522
523}
524
525void
526test (void)
527{
528  read_write_test ();
529  lseek_test ();
530}
Note: See TracBrowser for help on using the repository browser.