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

4.115
Last change on this file since d83f90b was 6fed43e, checked in by Joel Sherrill <joel.sherrill@…>, on 08/02/11 at 14:24:59

2011-08-02 Xiang Cui <medivhc@…>

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