source: rtems/testsuites/fstests/fsrdwr/init.c @ 9b4422a2

4.11
Last change on this file since 9b4422a2 was 9b4422a2, checked in by Joel Sherrill <joel.sherrill@…>, on May 3, 2012 at 3:09:24 PM

Remove All CVS Id Strings Possible Using a Script

Script does what is expected and tries to do it as
smartly as possible.

+ remove occurrences of two blank comment lines

next to each other after Id string line removed.

+ remove entire comment blocks which only exited to

contain CVS Ids

+ If the processing left a blank line at the top of

a file, it was removed.

  • Property mode set to 100644
File size: 16.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 <string.h>
16#include <stdlib.h>
17#include <fcntl.h>
18#include <stdio.h>
19#include <stdlib.h>
20#include <stdint.h>
21#include <memory.h>
22#include <unistd.h>
23
24#include "fstest.h"
25#include "pmacros.h"
26
27static const mode_t mode = S_IRWXU | S_IRWXG | S_IRWXO;
28
29static const 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
35static const size_t len = sizeof (databuf) - 1;
36
37static void
38test_case_enter (const char *wd)
39{
40  int status;
41
42  printf ("test case: %s\n", wd);
43
44  status = mkdir (wd, mode);
45  rtems_test_assert (status == 0);
46
47  status = chdir (wd);
48  rtems_test_assert (status == 0);
49}
50
51static void
52test_case_leave (void)
53{
54  int status;
55
56  status = chdir ("..");
57  rtems_test_assert (status == 0);
58}
59
60static void
61read_write_test (void)
62{
63
64  int fd;
65  int status;
66  char *name01 = "name01";
67  char *name02 = "name02";
68  struct stat statbuf;
69  char *readbuf;
70  off_t pos = 0;
71
72  int n;
73
74  test_case_enter (__func__);
75
76  /*
77   * Create an empty file
78   */
79  fd = open (name01, O_CREAT | O_WRONLY, mode);
80  status = close (fd);
81  rtems_test_assert (status == 0);
82  /*
83   * Verify the empty file
84   */
85  status = stat (name01, &statbuf);
86  rtems_test_assert (status == 0);
87
88  rtems_test_assert (S_ISREG (statbuf.st_mode));
89  rtems_test_assert (statbuf.st_size == 0);
90
91  /*
92   * Write data to the empty file
93   */
94  fd = open (name01, O_WRONLY);
95  rtems_test_assert (fd >= 0);
96
97  n = write (fd, databuf, len);
98  rtems_test_assert (n == len);
99  status = close (fd);
100  rtems_test_assert (status == 0);
101
102  status = stat (name01, &statbuf);
103  rtems_test_assert (status == 0);
104  rtems_test_assert (S_ISREG (statbuf.st_mode));
105  rtems_test_assert (statbuf.st_size == len);
106
107  /*
108   * Read the data from the file
109   */
110  readbuf = (char *) malloc (len + 1);
111  rtems_test_assert (readbuf);
112
113  fd = open (name01, O_RDONLY);
114  rtems_test_assert (fd >= 0);
115  n = read (fd, readbuf, len);
116  rtems_test_assert (n == len);
117  rtems_test_assert (!strncmp (databuf, readbuf, len));
118  status = close (fd);
119  rtems_test_assert (status == 0);
120
121  /*
122   * Open the file using O_APPEND and write the data
123   */
124  memset (readbuf, 0, len + 1);
125  fd = open (name01, O_WRONLY | O_APPEND);
126  n = write (fd, databuf, len);
127  rtems_test_assert (n == len);
128  pos = lseek (fd, 0, SEEK_CUR);
129  rtems_test_assert (pos == 2 * len);
130  pos = lseek (fd, 0, SEEK_SET);
131  rtems_test_assert (pos == 0);
132  n = write (fd, databuf, len);
133  rtems_test_assert (n == len);
134  pos = lseek (fd, 0, SEEK_CUR);
135  rtems_test_assert (pos == 3 * len);
136  status = close (fd);
137  rtems_test_assert (status == 0);
138
139  /*
140   * Read the data and verify it
141   */
142  fd = open (name01, O_RDONLY);
143  rtems_test_assert (fd >= 0);
144  n = read (fd, readbuf, len);
145  rtems_test_assert (n == len);
146  rtems_test_assert (!strncmp (databuf, readbuf, len));
147  n = read (fd, readbuf, len);
148  rtems_test_assert (n == len);
149  rtems_test_assert (!strncmp (databuf, readbuf, len));
150  n = read (fd, readbuf, len);
151  rtems_test_assert (n == len);
152  rtems_test_assert (!strncmp (databuf, readbuf, len));
153  status = close (fd);
154  rtems_test_assert (status == 0);
155
156  /*
157   * Open the file using O_RDWR
158   */
159  memset (readbuf, 0, len + 1);
160
161  fd = open (name01, O_RDWR);
162  n = write (fd, databuf, len);
163  rtems_test_assert (n == len);
164  pos = lseek (fd, 0, SEEK_CUR);
165  rtems_test_assert (pos == len);
166  n = read (fd, readbuf, len);
167  rtems_test_assert (n == len);
168  rtems_test_assert (!strncmp (databuf, readbuf, len));
169  pos = lseek (fd, 0, SEEK_CUR);
170  rtems_test_assert (pos == 2 * len);
171  status = close (fd);
172  rtems_test_assert (status == 0);
173
174  /*
175   * Open the file using O_TRUNC
176   */
177
178  fd = open (name01, O_WRONLY | O_TRUNC);
179  status = close (fd);
180  rtems_test_assert (status == 0);
181
182  /*
183   * Verify if the length is zero
184   */
185  status = stat (name01, &statbuf);
186  rtems_test_assert (status == 0);
187  rtems_test_assert (S_ISREG (statbuf.st_mode));
188  rtems_test_assert (statbuf.st_size == 0);
189
190  /*
191   * Open a directory
192   */
193  status = mkdir (name02, mode);
194  rtems_test_assert (status == 0);
195  fd = open (name02, O_RDONLY);
196  rtems_test_assert (fd >= 0);
197
198  status = close (fd);
199  rtems_test_assert (status == 0);
200
201  free (readbuf);
202
203  test_case_leave ();
204}
205
206static void
207truncate_test03 (void)
208{
209
210  int fd;
211  int status;
212  char *name01 = "name01";
213  struct stat statbuf;
214
215  char data;
216  int n;
217  int i;
218
219  char *readbuf;
220  off_t good_size = 100;
221
222  test_case_enter (__func__);
223
224  /*
225   * Create an empty file
226   */
227  fd = creat (name01, mode);
228  status = close (fd);
229  rtems_test_assert (status == 0);
230
231
232  /*
233   * Truncate it to a valid size
234   */
235  status = truncate (name01, good_size);
236  rtems_test_assert (status == 0);
237  /*
238   * Verify the size and the data
239   */
240  status = stat (name01, &statbuf);
241  rtems_test_assert (status == 0);
242  rtems_test_assert (good_size == statbuf.st_size);
243
244  fd = open (name01, O_RDONLY);
245  while ((n = read (fd, &data, 1)) > 0) {
246    rtems_test_assert (data == 0);
247  }
248
249  status = close (fd);
250  rtems_test_assert (status == 0);
251
252  /*
253   * Fill a file with data
254   */
255  fd = open (name01, O_WRONLY);
256  rtems_test_assert (fd >= 0);
257  n = write (fd, databuf, len);
258  rtems_test_assert (n == len);
259
260  /*
261   * Truncate it to the half size
262   */
263
264  status = truncate (name01, len / 2);
265  status = truncate (name01, len);
266
267  /*
268   * verify the data
269   */
270  readbuf = (char *) malloc (len / 2);
271  rtems_test_assert (readbuf);
272  fd = open (name01, O_RDONLY);
273  rtems_test_assert (fd >= 0);
274  n = read (fd, readbuf, len / 2);
275  rtems_test_assert (n == len / 2);
276  rtems_test_assert (!strncmp (databuf, readbuf, len / 2));
277  n = read (fd, readbuf, len / 2);
278  rtems_test_assert (n == len / 2);
279  for (i = 0; i < len / 2; i++) {
280    rtems_test_assert (readbuf[i] == 0);
281  }
282  status = close (fd);
283  rtems_test_assert (status == 0);
284
285  /*
286   * Go back to parent directory
287   */
288  status = chdir ("..");
289  rtems_test_assert (status == 0);
290}
291
292static void
293lseek_test (void)
294{
295  int fd;
296  int status;
297  const char *name01 = "test_name01";
298  struct stat statbuf;
299
300  ssize_t n;
301  int i;
302
303  off_t pos;
304  ssize_t total_written = 0;
305
306  char *readbuf;
307
308  test_case_enter (__func__);
309
310  /*
311   * Create a file and fill with the data.
312   */
313  puts ("Create a new file");
314  fd = creat (name01, mode);
315  rtems_test_assert (fd >= 0);
316
317  pos = lseek (fd, 0, SEEK_CUR);
318  rtems_test_assert (pos == 0);
319
320  pos = lseek (fd, 0, SEEK_END);
321  rtems_test_assert (pos == 0);
322
323  pos = lseek (fd, 0, SEEK_SET);
324  rtems_test_assert (pos == 0);
325
326
327  printf ("Writing %zd bytes to file\n", len * 10);
328  for (i = 0; i < 10; i++) {
329    n = write (fd, databuf, len);
330    rtems_test_assert (n == (ssize_t) len);
331    total_written += n;
332  }
333  printf ("Successfully wrote %d\n", total_written);
334
335  /*
336   * Check the current position
337   */
338  puts ("Check the current position");
339  pos = lseek (fd, 0, SEEK_CUR);
340  rtems_test_assert (pos == total_written);
341
342  pos = lseek (fd, 0, SEEK_END);
343  rtems_test_assert (pos == total_written);
344
345  /*
346   * ftruncate shall not change the posistion
347   */
348  status = ftruncate (fd, total_written + 1);
349  rtems_test_assert (status == 0);
350
351  pos = lseek (fd, 0, SEEK_CUR);
352  rtems_test_assert (pos == total_written);
353
354  pos = lseek (fd, 0, SEEK_END);
355  printf ("%jd\n", (intmax_t) pos);
356  rtems_test_assert (pos == total_written + 1);
357
358  status = ftruncate (fd, total_written);
359  rtems_test_assert (status == 0);
360
361  pos = lseek (fd, 0, SEEK_CUR);
362  rtems_test_assert (pos == total_written + 1);
363
364  /*
365   * Check the file size
366   */
367  status = fstat (fd, &statbuf);
368  rtems_test_assert (status == 0);
369  rtems_test_assert (statbuf.st_size == total_written);
370
371  status = ftruncate (fd, total_written);
372  rtems_test_assert (status == 0);
373
374  status = close (fd);
375  rtems_test_assert (status == 0);
376
377  /*
378   * Open the file with O_RDONLY and check the lseek
379   */
380  readbuf = (char *) malloc (len);
381  fd = open (name01, O_RDONLY);
382  pos = lseek (fd, len, SEEK_CUR);
383  rtems_test_assert (pos == len);
384  n = read (fd, readbuf, len);
385  rtems_test_assert (n == len);
386  rtems_test_assert (!strncmp (databuf, readbuf, len));
387
388  pos = lseek (fd, len, SEEK_CUR);
389  rtems_test_assert (pos == 3 * len);
390  n = read (fd, readbuf, len);
391  rtems_test_assert (n == len);
392  rtems_test_assert (!strncmp (databuf, readbuf, len));
393
394  pos = lseek (fd, -(off_t) len, SEEK_CUR);
395  rtems_test_assert (pos == 3 * len);
396  n = read (fd, readbuf, len);
397  rtems_test_assert (n == len);
398  rtems_test_assert (!strncmp (databuf, readbuf, len));
399
400  pos = lseek (fd, 4 * len, SEEK_SET);
401  n = read (fd, readbuf, len);
402  rtems_test_assert (n == len);
403  rtems_test_assert (!strncmp (databuf, readbuf, len));
404
405
406  pos = lseek (fd, 10, SEEK_SET);
407  n = read (fd, readbuf, len);
408  rtems_test_assert (n == len);
409  rtems_test_assert (strncmp (databuf, readbuf, len) != 0);
410
411  pos = lseek (fd, -(off_t) len, SEEK_END);
412  n = read (fd, readbuf, 2 * len);
413  rtems_test_assert (n == len);
414  rtems_test_assert (!strncmp (databuf, readbuf, len));
415
416  status = close (fd);
417  rtems_test_assert (status == 0);
418
419  /*
420   * Open the file withe O_RDWR and check the lseek
421   */
422
423  fd = open (name01, O_RDWR);
424
425  pos = lseek (fd, len, SEEK_CUR);
426  rtems_test_assert (pos == len);
427  n = read (fd, readbuf, len);
428  rtems_test_assert (n == len);
429  rtems_test_assert (!strncmp (databuf, readbuf, len));
430
431  pos = lseek (fd, len, SEEK_CUR);
432  rtems_test_assert (pos == 3 * len);
433  n = read (fd, readbuf, len);
434  rtems_test_assert (n == len);
435  rtems_test_assert (!strncmp (databuf, readbuf, len));
436
437  pos = lseek (fd, -(off_t) len, SEEK_CUR);
438  rtems_test_assert (pos == 3 * len);
439  n = read (fd, readbuf, len);
440  rtems_test_assert (n == len);
441  rtems_test_assert (!strncmp (databuf, readbuf, len));
442
443  pos = lseek (fd, 4 * len, SEEK_SET);
444  n = read (fd, readbuf, len);
445  rtems_test_assert (n == len);
446  rtems_test_assert (!strncmp (databuf, readbuf, len));
447
448  /*
449   * Go to the wrong position, so the data is not the same
450   */
451  pos = lseek (fd, 10, SEEK_SET);
452  n = read (fd, readbuf, len);
453  rtems_test_assert (n == len);
454  rtems_test_assert (strncmp (databuf, readbuf, len) != 0);
455
456  /*
457   * Use SEEK_END
458   */
459  pos = lseek (fd, -(off_t) len, SEEK_END);
460  n = read (fd, readbuf, 2 * len);
461  rtems_test_assert (n == len);
462  rtems_test_assert (!strncmp (databuf, readbuf, len));
463
464  memset (readbuf, 0, len);
465
466  /*
467   * Write the zero to the end of file.
468   */
469  pos = lseek (fd, -(off_t) len, SEEK_END);
470  rtems_test_assert (pos == (off_t) total_written - (off_t) len);
471  n = write (fd, readbuf, len);
472  rtems_test_assert (n == len);
473  /*
474   * Verify it
475   */
476  pos = lseek (fd, (off_t) total_written - (off_t) len, SEEK_SET);
477  n = read (fd, readbuf, len);
478  rtems_test_assert (n == len);
479  for (i = 0; i < n; i++) {
480    rtems_test_assert (readbuf[i] == 0);
481  }
482
483  /*
484   * Write the zero to the beginning of file.
485   */
486  pos = lseek (fd, -(off_t) total_written, SEEK_END);
487  rtems_test_assert (pos == 0);
488  n = write (fd, readbuf, len);
489  rtems_test_assert (n == len);
490
491  /*
492   * Verify it
493   */
494
495  pos = lseek (fd, 0, SEEK_SET);
496  n = read (fd, readbuf, len);
497  rtems_test_assert (n == len);
498  for (i = 0; i < n; i++) {
499    rtems_test_assert (readbuf[i] == 0);
500  }
501
502  n = read (fd, readbuf, len);
503  rtems_test_assert (n == len);
504  rtems_test_assert (strncmp (databuf, readbuf, len) == 0);
505  /*
506   * Call ftruncate to decrease the file and the position not change
507   */
508  status = ftruncate (fd, len);
509  rtems_test_assert (status == 0);
510  pos = lseek (fd, 0, SEEK_CUR);
511  rtems_test_assert (pos == len * 2);
512
513  status = close (fd);
514  rtems_test_assert (status == 0);
515
516  test_case_leave ();
517}
518
519static void
520truncate_to_zero (void)
521{
522  int fd;
523  ssize_t n;
524  int status;
525  off_t pos;
526
527  test_case_enter (__func__);
528
529  fd = creat ("file", mode);
530  rtems_test_assert (fd >= 0);
531
532  n = write (fd, databuf, len);
533  rtems_test_assert (n == (ssize_t) len);
534
535  pos = lseek (fd, 0, SEEK_END);
536  rtems_test_assert (pos == len);
537
538  status = ftruncate (fd, 0);
539  rtems_test_assert (status == 0);
540
541  pos = lseek (fd, 0, SEEK_END);
542  rtems_test_assert (pos == 0);
543
544  status = close (fd);
545  rtems_test_assert (status == 0);
546
547  test_case_leave ();
548}
549
550static void
551random_fill (char *dst, size_t n)
552{
553  static uint32_t u = 0x12345678;
554  uint32_t v = u;
555  uint32_t w;
556  size_t i = 0;
557  int j = 0;
558
559  while (i < n) {
560    if (j == 0) {
561      v *= 1664525;
562      v += 1013904223;
563      w = v;
564    } else {
565      w >>= 8;
566    }
567
568    dst [i] = (char) w;
569
570    ++i;
571    j = (j + 1) % 4;
572  }
573
574  u = v;
575}
576
577static void
578block_rw_lseek (int fd, size_t pos)
579{
580  off_t actual;
581
582  actual = lseek (fd, pos, SEEK_SET);
583  rtems_test_assert (actual == pos);
584}
585
586static void
587block_rw_write (int fd, char *out, size_t pos, size_t size)
588{
589  ssize_t n;
590
591  random_fill (out + pos, size);
592
593  block_rw_lseek (fd, pos);
594
595  n = write (fd, out + pos, size);
596  rtems_test_assert (n == (ssize_t) size);
597}
598
599static void
600block_rw_write_cont (int fd, char *out, size_t *pos, size_t size)
601{
602  ssize_t n;
603
604  random_fill (out + *pos, size);
605
606  n = write (fd, out + *pos, size);
607  rtems_test_assert (n == (ssize_t) size);
608
609  *pos += size;
610}
611
612static void
613block_rw_check (int fd, const char *out, char *in, size_t size)
614{
615  ssize_t n;
616  off_t file_size;
617
618  file_size = lseek (fd, 0, SEEK_END);
619  rtems_test_assert (file_size == size);
620
621  block_rw_lseek (fd, 0);
622
623  n = read (fd, in, size);
624  rtems_test_assert (n == (ssize_t) size);
625
626  rtems_test_assert (memcmp (out, in, size) == 0);
627}
628
629static void
630block_rw_prepare (const char *t, int fd, char *out, size_t size)
631{
632  int status;
633
634  printf ("test case: %s\n", t);
635
636  memset (out, 0, size);
637
638  status = ftruncate (fd, 0);
639  rtems_test_assert (status == 0);
640
641  block_rw_lseek (fd, 0);
642}
643
644static void
645block_rw_case_0 (int fd, size_t block_size, char *out, char *in)
646{
647  const size_t size = 3 * block_size + 1;
648
649  block_rw_prepare (__func__, fd, out, size);
650  block_rw_write (fd, out, 0, size);
651  block_rw_check (fd, out, in, size);
652}
653
654static void
655block_rw_case_1 (int fd, size_t block_size, char *out, char *in)
656{
657  const size_t size = 2 * block_size;
658
659  block_rw_prepare (__func__, fd, out, size);
660  block_rw_write (fd, out, block_size, block_size);
661  block_rw_check (fd, out, in, size);
662}
663
664static void
665block_rw_case_2 (int fd, size_t block_size, char *out, char *in)
666{
667  const size_t size = (5 * block_size) / 2;
668
669  block_rw_prepare (__func__, fd, out, size);
670  block_rw_write (fd, out, (3 * block_size) / 2, block_size);
671  block_rw_check (fd, out, in, size);
672}
673
674static void
675block_rw_case_3 (int fd, size_t block_size, char *out, char *in)
676{
677  const size_t size = 2 * block_size;
678
679  block_rw_prepare (__func__, fd, out, size);
680  block_rw_write (fd, out, block_size, block_size / 3);
681  block_rw_write (fd, out, 2 * block_size - block_size / 3, block_size / 3);
682  block_rw_check (fd, out, in, size);
683}
684
685static void
686block_rw_case_4 (int fd, size_t block_size, char *out, char *in)
687{
688  const size_t size = 3 * block_size + 1;
689  size_t pos = 0;
690
691  block_rw_prepare (__func__, fd, out, size);
692  block_rw_write_cont (fd, out, &pos, block_size);
693  block_rw_write_cont (fd, out, &pos, block_size / 2);
694  block_rw_write_cont (fd, out, &pos, block_size);
695  block_rw_write_cont (fd, out, &pos, block_size / 2);
696  block_rw_write_cont (fd, out, &pos, 1);
697  block_rw_check (fd, out, in, size);
698}
699
700static void
701block_read_and_write (void)
702{
703  int fd;
704  struct stat st;
705  int status;
706  size_t block_size;
707  size_t size;
708  char *out;
709  char *in;
710
711  test_case_enter (__func__);
712
713  fd = open ("file", O_RDWR | O_CREAT | O_TRUNC, mode);
714  rtems_test_assert (fd >= 0);
715
716  status = fstat (fd, &st);
717  rtems_test_assert (status == 0);
718
719  block_size = st.st_blksize;
720  size = 3 * block_size + 1;
721
722  out = malloc (size);
723  rtems_test_assert (out != NULL);
724
725  in = malloc (size);
726  rtems_test_assert (in != NULL);
727
728  block_rw_case_0 (fd, block_size, out, in);
729  block_rw_case_1 (fd, block_size, out, in);
730  block_rw_case_2 (fd, block_size, out, in);
731  block_rw_case_3 (fd, block_size, out, in);
732  block_rw_case_4 (fd, block_size, out, in);
733
734  status = close (fd);
735  rtems_test_assert (status == 0);
736
737  free (out);
738  free (in);
739
740  test_case_leave ();
741}
742
743void
744test (void)
745{
746  read_write_test ();
747  lseek_test ();
748  truncate_test03 ();
749  truncate_to_zero ();
750  block_read_and_write ();
751}
Note: See TracBrowser for help on using the repository browser.