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