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

4.115
Last change on this file since 35320729 was 35320729, checked in by Ralf Corsépius <ralf.corsepius@…>, on 10/15/12 at 02:36:15

Use %zd instead of %d to print size_t's

  • Property mode set to 100644
File size: 16.5 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  status = close (fd);
260  rtems_test_assert (status == 0);
261
262  /*
263   * Truncate it to the half size
264   */
265
266  status = truncate (name01, len / 2);
267  rtems_test_assert (status == 0);
268  status = truncate (name01, len);
269  rtems_test_assert (status == 0);
270
271  /*
272   * verify the data
273   */
274  readbuf = (char *) malloc (len / 2);
275  rtems_test_assert (readbuf);
276  fd = open (name01, O_RDONLY);
277  rtems_test_assert (fd >= 0);
278  n = read (fd, readbuf, len / 2);
279  rtems_test_assert (n == len / 2);
280  rtems_test_assert (!strncmp (databuf, readbuf, len / 2));
281  n = read (fd, readbuf, len / 2);
282  rtems_test_assert (n == len / 2);
283  for (i = 0; i < len / 2; i++) {
284    rtems_test_assert (readbuf[i] == 0);
285  }
286  status = close (fd);
287  rtems_test_assert (status == 0);
288
289  /*
290   * Go back to parent directory
291   */
292  status = chdir ("..");
293  rtems_test_assert (status == 0);
294}
295
296static void
297lseek_test (void)
298{
299  int fd;
300  int status;
301  const char *name01 = "test_name01";
302  struct stat statbuf;
303
304  ssize_t n;
305  int i;
306
307  off_t pos;
308  ssize_t total_written = 0;
309
310  char *readbuf;
311
312  test_case_enter (__func__);
313
314  /*
315   * Create a file and fill with the data.
316   */
317  puts ("Create a new file");
318  fd = creat (name01, mode);
319  rtems_test_assert (fd >= 0);
320
321  pos = lseek (fd, 0, SEEK_CUR);
322  rtems_test_assert (pos == 0);
323
324  pos = lseek (fd, 0, SEEK_END);
325  rtems_test_assert (pos == 0);
326
327  pos = lseek (fd, 0, SEEK_SET);
328  rtems_test_assert (pos == 0);
329
330
331  printf ("Writing %zd bytes to file\n", len * 10);
332  for (i = 0; i < 10; i++) {
333    n = write (fd, databuf, len);
334    rtems_test_assert (n == (ssize_t) len);
335    total_written += n;
336  }
337  printf ("Successfully wrote %zd\n", total_written);
338
339  /*
340   * Check the current position
341   */
342  puts ("Check the current position");
343  pos = lseek (fd, 0, SEEK_CUR);
344  rtems_test_assert (pos == total_written);
345
346  pos = lseek (fd, 0, SEEK_END);
347  rtems_test_assert (pos == total_written);
348
349  /*
350   * ftruncate shall not change the posistion
351   */
352  status = ftruncate (fd, total_written + 1);
353  rtems_test_assert (status == 0);
354
355  pos = lseek (fd, 0, SEEK_CUR);
356  rtems_test_assert (pos == total_written);
357
358  pos = lseek (fd, 0, SEEK_END);
359  printf ("%jd\n", (intmax_t) pos);
360  rtems_test_assert (pos == total_written + 1);
361
362  status = ftruncate (fd, total_written);
363  rtems_test_assert (status == 0);
364
365  pos = lseek (fd, 0, SEEK_CUR);
366  rtems_test_assert (pos == total_written + 1);
367
368  /*
369   * Check the file size
370   */
371  status = fstat (fd, &statbuf);
372  rtems_test_assert (status == 0);
373  rtems_test_assert (statbuf.st_size == total_written);
374
375  status = ftruncate (fd, total_written);
376  rtems_test_assert (status == 0);
377
378  status = close (fd);
379  rtems_test_assert (status == 0);
380
381  /*
382   * Open the file with O_RDONLY and check the lseek
383   */
384  readbuf = (char *) malloc (len);
385  fd = open (name01, O_RDONLY);
386  pos = lseek (fd, len, SEEK_CUR);
387  rtems_test_assert (pos == len);
388  n = read (fd, readbuf, len);
389  rtems_test_assert (n == len);
390  rtems_test_assert (!strncmp (databuf, readbuf, len));
391
392  pos = lseek (fd, len, SEEK_CUR);
393  rtems_test_assert (pos == 3 * len);
394  n = read (fd, readbuf, len);
395  rtems_test_assert (n == len);
396  rtems_test_assert (!strncmp (databuf, readbuf, len));
397
398  pos = lseek (fd, -(off_t) len, SEEK_CUR);
399  rtems_test_assert (pos == 3 * len);
400  n = read (fd, readbuf, len);
401  rtems_test_assert (n == len);
402  rtems_test_assert (!strncmp (databuf, readbuf, len));
403
404  pos = lseek (fd, 4 * len, SEEK_SET);
405  n = read (fd, readbuf, len);
406  rtems_test_assert (n == len);
407  rtems_test_assert (!strncmp (databuf, readbuf, len));
408
409
410  pos = lseek (fd, 10, SEEK_SET);
411  n = read (fd, readbuf, len);
412  rtems_test_assert (n == len);
413  rtems_test_assert (strncmp (databuf, readbuf, len) != 0);
414
415  pos = lseek (fd, -(off_t) len, SEEK_END);
416  n = read (fd, readbuf, 2 * len);
417  rtems_test_assert (n == len);
418  rtems_test_assert (!strncmp (databuf, readbuf, len));
419
420  status = close (fd);
421  rtems_test_assert (status == 0);
422
423  /*
424   * Open the file withe O_RDWR and check the lseek
425   */
426
427  fd = open (name01, O_RDWR);
428
429  pos = lseek (fd, len, SEEK_CUR);
430  rtems_test_assert (pos == len);
431  n = read (fd, readbuf, len);
432  rtems_test_assert (n == len);
433  rtems_test_assert (!strncmp (databuf, readbuf, len));
434
435  pos = lseek (fd, len, SEEK_CUR);
436  rtems_test_assert (pos == 3 * len);
437  n = read (fd, readbuf, len);
438  rtems_test_assert (n == len);
439  rtems_test_assert (!strncmp (databuf, readbuf, len));
440
441  pos = lseek (fd, -(off_t) len, SEEK_CUR);
442  rtems_test_assert (pos == 3 * len);
443  n = read (fd, readbuf, len);
444  rtems_test_assert (n == len);
445  rtems_test_assert (!strncmp (databuf, readbuf, len));
446
447  pos = lseek (fd, 4 * len, SEEK_SET);
448  n = read (fd, readbuf, len);
449  rtems_test_assert (n == len);
450  rtems_test_assert (!strncmp (databuf, readbuf, len));
451
452  /*
453   * Go to the wrong position, so the data is not the same
454   */
455  pos = lseek (fd, 10, SEEK_SET);
456  n = read (fd, readbuf, len);
457  rtems_test_assert (n == len);
458  rtems_test_assert (strncmp (databuf, readbuf, len) != 0);
459
460  /*
461   * Use SEEK_END
462   */
463  pos = lseek (fd, -(off_t) len, SEEK_END);
464  n = read (fd, readbuf, 2 * len);
465  rtems_test_assert (n == len);
466  rtems_test_assert (!strncmp (databuf, readbuf, len));
467
468  memset (readbuf, 0, len);
469
470  /*
471   * Write the zero to the end of file.
472   */
473  pos = lseek (fd, -(off_t) len, SEEK_END);
474  rtems_test_assert (pos == (off_t) total_written - (off_t) len);
475  n = write (fd, readbuf, len);
476  rtems_test_assert (n == len);
477  /*
478   * Verify it
479   */
480  pos = lseek (fd, (off_t) total_written - (off_t) len, SEEK_SET);
481  n = read (fd, readbuf, len);
482  rtems_test_assert (n == len);
483  for (i = 0; i < n; i++) {
484    rtems_test_assert (readbuf[i] == 0);
485  }
486
487  /*
488   * Write the zero to the beginning of file.
489   */
490  pos = lseek (fd, -(off_t) total_written, SEEK_END);
491  rtems_test_assert (pos == 0);
492  n = write (fd, readbuf, len);
493  rtems_test_assert (n == len);
494
495  /*
496   * Verify it
497   */
498
499  pos = lseek (fd, 0, SEEK_SET);
500  n = read (fd, readbuf, len);
501  rtems_test_assert (n == len);
502  for (i = 0; i < n; i++) {
503    rtems_test_assert (readbuf[i] == 0);
504  }
505
506  n = read (fd, readbuf, len);
507  rtems_test_assert (n == len);
508  rtems_test_assert (strncmp (databuf, readbuf, len) == 0);
509  /*
510   * Call ftruncate to decrease the file and the position not change
511   */
512  status = ftruncate (fd, len);
513  rtems_test_assert (status == 0);
514  pos = lseek (fd, 0, SEEK_CUR);
515  rtems_test_assert (pos == len * 2);
516
517  status = close (fd);
518  rtems_test_assert (status == 0);
519
520  test_case_leave ();
521}
522
523static void
524truncate_to_zero (void)
525{
526  int fd;
527  ssize_t n;
528  int status;
529  off_t pos;
530
531  test_case_enter (__func__);
532
533  fd = creat ("file", mode);
534  rtems_test_assert (fd >= 0);
535
536  n = write (fd, databuf, len);
537  rtems_test_assert (n == (ssize_t) len);
538
539  pos = lseek (fd, 0, SEEK_END);
540  rtems_test_assert (pos == len);
541
542  status = ftruncate (fd, 0);
543  rtems_test_assert (status == 0);
544
545  pos = lseek (fd, 0, SEEK_END);
546  rtems_test_assert (pos == 0);
547
548  status = close (fd);
549  rtems_test_assert (status == 0);
550
551  test_case_leave ();
552}
553
554static void
555random_fill (char *dst, size_t n)
556{
557  static uint32_t u = 0x12345678;
558  uint32_t v = u;
559  uint32_t w;
560  size_t i = 0;
561  int j = 0;
562
563  while (i < n) {
564    if (j == 0) {
565      v *= 1664525;
566      v += 1013904223;
567      w = v;
568    } else {
569      w >>= 8;
570    }
571
572    dst [i] = (char) w;
573
574    ++i;
575    j = (j + 1) % 4;
576  }
577
578  u = v;
579}
580
581static void
582block_rw_lseek (int fd, size_t pos)
583{
584  off_t actual;
585
586  actual = lseek (fd, pos, SEEK_SET);
587  rtems_test_assert (actual == pos);
588}
589
590static void
591block_rw_write (int fd, char *out, size_t pos, size_t size)
592{
593  ssize_t n;
594
595  random_fill (out + pos, size);
596
597  block_rw_lseek (fd, pos);
598
599  n = write (fd, out + pos, size);
600  rtems_test_assert (n == (ssize_t) size);
601}
602
603static void
604block_rw_write_cont (int fd, char *out, size_t *pos, size_t size)
605{
606  ssize_t n;
607
608  random_fill (out + *pos, size);
609
610  n = write (fd, out + *pos, size);
611  rtems_test_assert (n == (ssize_t) size);
612
613  *pos += size;
614}
615
616static void
617block_rw_check (int fd, const char *out, char *in, size_t size)
618{
619  ssize_t n;
620  off_t file_size;
621
622  file_size = lseek (fd, 0, SEEK_END);
623  rtems_test_assert (file_size == size);
624
625  block_rw_lseek (fd, 0);
626
627  n = read (fd, in, size);
628  rtems_test_assert (n == (ssize_t) size);
629
630  rtems_test_assert (memcmp (out, in, size) == 0);
631}
632
633static void
634block_rw_prepare (const char *t, int fd, char *out, size_t size)
635{
636  int status;
637
638  printf ("test case: %s\n", t);
639
640  memset (out, 0, size);
641
642  status = ftruncate (fd, 0);
643  rtems_test_assert (status == 0);
644
645  block_rw_lseek (fd, 0);
646}
647
648static void
649block_rw_case_0 (int fd, size_t block_size, char *out, char *in)
650{
651  const size_t size = 3 * block_size + 1;
652
653  block_rw_prepare (__func__, fd, out, size);
654  block_rw_write (fd, out, 0, size);
655  block_rw_check (fd, out, in, size);
656}
657
658static void
659block_rw_case_1 (int fd, size_t block_size, char *out, char *in)
660{
661  const size_t size = 2 * block_size;
662
663  block_rw_prepare (__func__, fd, out, size);
664  block_rw_write (fd, out, block_size, block_size);
665  block_rw_check (fd, out, in, size);
666}
667
668static void
669block_rw_case_2 (int fd, size_t block_size, char *out, char *in)
670{
671  const size_t size = (5 * block_size) / 2;
672
673  block_rw_prepare (__func__, fd, out, size);
674  block_rw_write (fd, out, (3 * block_size) / 2, block_size);
675  block_rw_check (fd, out, in, size);
676}
677
678static void
679block_rw_case_3 (int fd, size_t block_size, char *out, char *in)
680{
681  const size_t size = 2 * block_size;
682
683  block_rw_prepare (__func__, fd, out, size);
684  block_rw_write (fd, out, block_size, block_size / 3);
685  block_rw_write (fd, out, 2 * block_size - block_size / 3, block_size / 3);
686  block_rw_check (fd, out, in, size);
687}
688
689static void
690block_rw_case_4 (int fd, size_t block_size, char *out, char *in)
691{
692  const size_t size = 3 * block_size + 1;
693  size_t pos = 0;
694
695  block_rw_prepare (__func__, fd, out, size);
696  block_rw_write_cont (fd, out, &pos, block_size);
697  block_rw_write_cont (fd, out, &pos, block_size / 2);
698  block_rw_write_cont (fd, out, &pos, block_size);
699  block_rw_write_cont (fd, out, &pos, block_size / 2);
700  block_rw_write_cont (fd, out, &pos, 1);
701  block_rw_check (fd, out, in, size);
702}
703
704static void
705block_read_and_write (void)
706{
707  int fd;
708  struct stat st;
709  int status;
710  size_t block_size;
711  size_t size;
712  char *out;
713  char *in;
714
715  test_case_enter (__func__);
716
717  fd = open ("file", O_RDWR | O_CREAT | O_TRUNC, mode);
718  rtems_test_assert (fd >= 0);
719
720  status = fstat (fd, &st);
721  rtems_test_assert (status == 0);
722
723  block_size = st.st_blksize;
724  size = 3 * block_size + 1;
725
726  out = malloc (size);
727  rtems_test_assert (out != NULL);
728
729  in = malloc (size);
730  rtems_test_assert (in != NULL);
731
732  block_rw_case_0 (fd, block_size, out, in);
733  block_rw_case_1 (fd, block_size, out, in);
734  block_rw_case_2 (fd, block_size, out, in);
735  block_rw_case_3 (fd, block_size, out, in);
736  block_rw_case_4 (fd, block_size, out, in);
737
738  status = close (fd);
739  rtems_test_assert (status == 0);
740
741  free (out);
742  free (in);
743
744  test_case_leave ();
745}
746
747void
748test (void)
749{
750  read_write_test ();
751  lseek_test ();
752  truncate_test03 ();
753  truncate_to_zero ();
754  block_read_and_write ();
755}
Note: See TracBrowser for help on using the repository browser.