source: rtems/testsuites/fstests/fserror/test.c @ bc75887

4.11
Last change on this file since bc75887 was bc75887, checked in by Sebastian Huber <sebastian.huber@…>, on Mar 16, 2014 at 3:15:33 PM

tests/fstests: Use <rtems/test.h>

  • Property mode set to 100644
File size: 8.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
10#ifdef HAVE_CONFIG_H
11#include "config.h"
12#endif
13
14#include <sys/stat.h>
15#include <limits.h>
16#include <fcntl.h>
17#include <errno.h>
18#include <stdio.h>
19#include <stdint.h>
20#include <stdlib.h>
21#include <string.h>
22#include <unistd.h>
23
24#include "fstest.h"
25#include "fs_config.h"
26#include "pmacros.h"
27
28const char rtems_test_name[] = "FSERROR " FILESYSTEM;
29
30static void open_mkdir_error (void)
31{
32  int fd;
33  int status;
34  char *name01 = "name01";
35  char *name02 = "name02";
36  char *name03 = "name03";
37
38  char name[20];
39
40  mode_t mode = S_IRWXU | S_IRWXG | S_IRWXO;
41
42  const char *wd = __func__;
43
44  /*
45   * Create a new directory and change the current directory to  this
46   */
47  status = mkdir (wd, mode);
48  rtems_test_assert (status == 0);
49  status = chdir (wd);
50  rtems_test_assert (status == 0);
51
52
53  /*
54   * Create a file and a directory for test.
55   */
56  fd = creat (name01, mode);
57  status = close (fd);
58  rtems_test_assert (status == 0);
59  status = mkdir (name02, mode);
60  rtems_test_assert (status == 0);
61
62  /*
63   * O_CREAT and O_EXCL are set, and the named file exists.
64   */
65  EXPECT_ERROR (EEXIST, open, name01, O_CREAT | O_EXCL);
66
67  EXPECT_ERROR (EEXIST, mkdir, name01, mode);
68  /*
69   * The named file is a directory
70   * and oflag includes O_WRONLY or O_RDWR.
71   */
72  EXPECT_ERROR (EISDIR, open, name02, O_WRONLY);
73  EXPECT_ERROR (EISDIR, open, name02, O_RDWR);
74
75  /*
76   * O_CREAT is not set and the named file does not exist
77   * or O_CREAT is set and either the path prefix does not exist or
78   * the path argument points to an empty string.
79   */
80
81  sprintf (name, "%s/%s", name03, name02);
82  EXPECT_ERROR (ENOENT, open, name, O_WRONLY);
83  EXPECT_ERROR (ENOENT, open, "", O_WRONLY);
84  EXPECT_ERROR (ENOENT, open, name03, O_WRONLY);
85
86  EXPECT_ERROR (ENOENT, mkdir, name, mode);
87  EXPECT_ERROR (ENOENT, mkdir, "", mode);
88
89  /*
90   * A component of the path prefix is not a directory.
91   */
92
93  sprintf (name, "%s/%s", name01, name02);
94  EXPECT_ERROR (ENOTDIR, open, name, O_WRONLY);
95
96  EXPECT_ERROR (ENOTDIR, mkdir, name, mode);
97  /*
98   * The fildes argument is not a valid file descriptor.
99   */
100  EXPECT_ERROR (EBADF, close, -1);
101  EXPECT_ERROR (EBADF, close, 100);
102
103  /*
104   * Go back to parent directory
105   */
106  status = chdir ("..");
107  rtems_test_assert (status == 0);
108
109}
110
111static void rename_error (void)
112{
113
114  int fd;
115  int status;
116  char *name01 = "name01";
117  char *name02 = "name02";
118  char *name03 = "name03";
119  char *nonexistence = "name04";
120
121  char name[20];
122
123  mode_t mode = S_IRWXU | S_IRWXG | S_IRWXO;
124  const char *wd = __func__;
125
126  /*
127   *Create a new directory and change the current directory to  this
128   */
129
130  status = mkdir (wd, mode);
131  rtems_test_assert (status == 0);
132  status = chdir (wd);
133  rtems_test_assert (status == 0);
134
135
136  /*
137   * Create a new directory and a new directory in it
138   */
139
140  status = mkdir (name01, mode);
141  rtems_test_assert (status == 0);
142  status = mkdir (name02, mode);
143  rtems_test_assert (status == 0);
144  sprintf (name, "%s/%s", name01, name03);
145  status = mkdir (name, mode);
146  rtems_test_assert (status == 0);
147
148  /*
149   * The link named by new is a directory that is
150   *  not an empty directory.
151   */
152  status = rename (name02, name01);
153  rtems_test_assert (status != 0);
154  rtems_test_assert (errno == EEXIST || errno == ENOTEMPTY);
155  /*
156   * The new directory pathname contains a path prefix
157   *  that names the old directory.
158   */
159  EXPECT_ERROR (EINVAL, rename, name01, name);
160  /*
161   * The new argument points to a directory and
162   *  the old argument points to a file that is not a directory.
163   */
164  fd = creat (name03, mode);
165  status = close (fd);
166  rtems_test_assert (status == 0);
167  EXPECT_ERROR (EISDIR, rename, name03, name02);
168
169  /*
170   * The link named by old does not name an existing file,
171   *    or either old or new points to an empty string.
172   */
173
174  EXPECT_ERROR (ENOENT, rename, nonexistence, name01);
175  EXPECT_ERROR (ENOENT, rename, "", name01);
176  EXPECT_ERROR (ENOENT, rename, name01, "");
177
178  /*
179   * A component of either path prefix is not a directory;
180   * or the old argument names a directory and new argument names
181   *  a non-directory file.
182   */
183
184  sprintf (name, "%s/%s", name03, name01);
185  EXPECT_ERROR (ENOTDIR, rename, name, name03);
186  EXPECT_ERROR (ENOTDIR, rename, name03, name);
187  EXPECT_ERROR (ENOTDIR, rename, name02, name03);
188
189  /*
190   * Go back to parent directory
191   */
192  status = chdir ("..");
193  rtems_test_assert (status == 0);
194}
195
196static void truncate_error (void)
197{
198
199  int fd;
200  int status;
201  char *file = "name";
202
203  mode_t mode = S_IRWXU | S_IRWXG | S_IRWXO;
204
205  const char *wd = __func__;
206
207  /*
208   *Create a new directory and change the current directory to  this
209   */
210  status = mkdir (wd, mode);
211  rtems_test_assert (status == 0);
212  status = chdir (wd);
213  rtems_test_assert (status == 0);
214  /*
215   * Create a file
216   */
217  fd = creat (file, mode);
218  status = close (fd);
219  rtems_test_assert (status == 0);
220
221
222  /*
223   * The length argument was less than 0.
224   */
225  EXPECT_ERROR (EINVAL, truncate, file, -1);
226
227  /*
228   * Go back to parent directory
229   */
230  status = chdir ("..");
231  rtems_test_assert (status == 0);
232}
233
234
235static void rmdir_unlink_error (void)
236{
237  int status;
238  int fd;
239  mode_t mode = S_IRWXU | S_IRWXG | S_IRWXO;
240  char *nonexistence = "name04";
241
242  const char *wd = __func__;
243
244  /*
245   * Create a new directory and change the current directory to  this
246   */
247  status = mkdir (wd, mode);
248  rtems_test_assert (status == 0);
249  status = chdir (wd);
250  rtems_test_assert (status == 0);
251
252  /*
253   * Create a new directory and a file in it for test
254   */
255  status = mkdir ("tmp", mode);
256  rtems_test_assert (status == 0);
257  fd = creat ("tmp/file", mode);
258  status = close (fd);
259  rtems_test_assert (status == 0);
260
261
262
263  /*
264   * The path argument names a directory that is not an empty directory,
265   * or there are hard links to the directory other than
266   * dot or a single entry in dot-dot.
267   */
268
269  EXPECT_ERROR (ENOTEMPTY, rmdir, "..");
270  EXPECT_ERROR (ENOTEMPTY, rmdir, "tmp");
271
272
273  /*
274   * The path argument contains a last component that is dot.
275   */
276
277
278  EXPECT_ERROR (EINVAL, rmdir, ".");
279  EXPECT_ERROR (EINVAL, rmdir, "tmp/.");
280
281  /*
282   * A component of path does not name an existing file,
283   * or the path argument names a nonexistent directory or
284   * points to an empty string
285   */
286  EXPECT_ERROR (ENOENT, rmdir, "");
287  EXPECT_ERROR (ENOENT, rmdir, nonexistence);
288
289  EXPECT_ERROR (ENOENT, unlink, "");
290  EXPECT_ERROR (ENOENT, unlink, nonexistence);
291
292  /*
293   * component of path is not a directory.
294   */
295
296  EXPECT_ERROR (ENOTDIR, rmdir, "tmp/file");
297
298  EXPECT_ERROR (ENOTDIR, unlink, "tmp/file/dir");
299  /*
300   * Go back to parent directory
301   */
302  status = chdir ("..");
303  rtems_test_assert (status == 0);
304
305
306}
307
308static void rdwr_error (void)
309{
310
311  int status;
312  int fd;
313  char *file01 = "name01";
314  char *databuf = "test";
315  char *readbuf[10];
316  int shift = sizeof(off_t) * 8 - 1;
317  off_t one = 1;
318  off_t tiny = one << shift;
319  off_t huge = tiny - 1;
320  off_t off;
321
322  mode_t mode = S_IRWXU | S_IRWXG | S_IRWXO;
323  const char *wd = __func__;
324
325  /*
326   * Create a new directory and change the current directory to  this
327   */
328  status = mkdir (wd, mode);
329  rtems_test_assert (status == 0);
330  status = chdir (wd);
331  rtems_test_assert (status == 0);
332
333  fd = open (file01, O_WRONLY | O_CREAT, mode);
334
335  /*
336   * The fildes argument is not a valid file descriptor open for reading.
337   */
338
339  EXPECT_ERROR (EBADF, read, fd, readbuf, 10);
340  EXPECT_ERROR (EBADF, read, 100, readbuf, 10);
341
342  status = close (fd);
343  rtems_test_assert (status == 0);
344  /*
345   * The fildes argument is not a valid file descriptor open for writing.
346   */
347  fd = open (file01, O_RDONLY, mode);
348
349  EXPECT_ERROR (EBADF, write, fd, databuf, 10);
350
351  status = close (fd);
352  rtems_test_assert (status == 0);
353
354  EXPECT_ERROR (EBADF, write, fd, readbuf, 10);
355
356  /*
357   * The whence argument is not a proper value,
358   * or the resulting file offset would be negative for a regular file,
359   * block special file, or directory.
360   */
361
362  fd = open (file01, O_RDWR, mode);
363
364  EXPECT_ERROR (EINVAL, lseek, fd, -100, SEEK_END);
365  EXPECT_ERROR (EINVAL, lseek, fd, -100, SEEK_CUR);
366  EXPECT_ERROR (EINVAL, lseek, fd, -100, SEEK_SET);
367
368  status = ftruncate (fd, 1);
369  rtems_test_assert (status == 0);
370  EXPECT_ERROR (EOVERFLOW, lseek, fd, huge, SEEK_END);
371
372  off = lseek (fd, 1, SEEK_SET);
373  rtems_test_assert (off == 1);
374  EXPECT_ERROR (EOVERFLOW, lseek, fd, huge, SEEK_CUR);
375
376  status = close (fd);
377  rtems_test_assert (status == 0);
378
379  EXPECT_ERROR (EBADF, lseek, fd, 0, SEEK_SET);
380
381  /*
382   * Go back to parent directory
383   */
384  status = chdir ("..");
385  rtems_test_assert (status == 0);
386
387}
388
389void test (void)
390{
391
392  puts ("\n\n*** ERROR TEST ***");
393  open_mkdir_error ();
394  rename_error ();
395  truncate_error ();
396  rmdir_unlink_error ();
397  rdwr_error ();
398  puts ("*** END OF ERROR TEST ***");
399}
Note: See TracBrowser for help on using the repository browser.