source: rtems/testsuites/psxtests/psxfile02/init.c @ 0169e90

5
Last change on this file since 0169e90 was 0169e90, checked in by Sebastian Huber <sebastian.huber@…>, on 09/13/17 at 06:55:05

libio: Do simple parameter checks early

This simplifies error handling later.

Update #3132.

  • Property mode set to 100644
File size: 4.1 KB
Line 
1/*
2 *  COPYRIGHT (c) 1989-2012.
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.org/license/LICENSE.
8 */
9
10#ifdef HAVE_CONFIG_H
11#include "config.h"
12#endif
13
14#include <stdio.h>
15#include <sys/uio.h>
16#include <sys/types.h>
17#include <sys/stat.h>
18#include <fcntl.h>
19#include <unistd.h>
20#include <errno.h>
21#include <string.h>
22#include <ctype.h>
23#include <rtems/imfs.h>
24#include <reent.h>
25
26#include <rtems.h>
27#include <rtems/libio.h>
28
29#include <tmacros.h>
30#include "test_support.h"
31
32const char rtems_test_name[] = "PSXFILE 2";
33
34/* forward declarations to avoid warnings */
35rtems_task Init(rtems_task_argument argument);
36void do_with_fd(int fd, const char *description);
37
38void do_with_fd(
39  int         fd,
40  const char *description
41)
42{
43  struct stat       stat_buff;
44  struct iovec      vec[2];
45  char              buf[2][1];
46  off_t             res;
47  int               status;
48
49  vec[0].iov_base = buf[0];
50  vec[0].iov_len = sizeof(buf[0]);
51  vec[1].iov_base = buf[1];
52  vec[1].iov_len = sizeof(buf[1]);
53
54  printf("ftruncate %s\n", description);
55  status = ftruncate(fd, 40);
56  rtems_test_assert( status == -1 );
57  printf( "%d: %s\n", errno, strerror( errno ) );
58  rtems_test_assert( errno == EBADF );
59
60  printf("_fcntl_r %s\n", description);
61  status = _fcntl_r( NULL, fd, F_SETFD, 1 );
62  rtems_test_assert( status == -1 );
63  printf( "%d: %s\n", errno, strerror( errno ) );
64  rtems_test_assert( errno == EBADF );
65
66  printf("fdatasync %s\n", description);
67  status = fdatasync( fd );
68  rtems_test_assert( status == -1 );
69  printf( "%d: %s\n", errno, strerror( errno ) );
70  rtems_test_assert( errno == EBADF );
71
72  printf("fstat %s\n", description);
73  status = fstat( fd, &stat_buff );
74  rtems_test_assert( status == -1 );
75  printf( "%d: %s\n", errno, strerror( errno ) );
76  rtems_test_assert( errno == EBADF );
77
78  printf("fsync %s\n", description);
79  status = fsync( fd );
80  rtems_test_assert( status == -1 );
81  printf( "%d: %s\n", errno, strerror( errno ) );
82  rtems_test_assert( errno == EBADF );
83
84  printf("ioctl %s\n", description);
85  status = ioctl( fd, 0 );
86  rtems_test_assert( status == -1 );
87  printf( "%d: %s\n", errno, strerror( errno ) );
88  rtems_test_assert( errno == EBADF );
89
90  printf("_lseek_r %s\n", description);
91  res = _lseek_r (NULL, fd, 0, SEEK_SET);
92  rtems_test_assert( res == -1 );
93  printf( "%d: %s\n", errno, strerror( errno ) );
94  rtems_test_assert( errno == EBADF );
95
96  printf("readv %s\n", description);
97  status = readv(fd, vec, 2);
98  rtems_test_assert( status == -1 );
99  printf( "%d: %s\n", errno, strerror( errno ) );
100  rtems_test_assert( errno == EBADF );
101
102  printf("writev %s\n", description);
103  status = writev(fd, vec, 2);
104  rtems_test_assert( status == -1 );
105  printf( "%d: %s\n", errno, strerror( errno ) );
106  rtems_test_assert( errno == EBADF );
107
108  printf("write %s\n", description);
109  status = write(fd, "1234", 4);
110  rtems_test_assert( status == -1 );
111  printf( "%d: %s\n", errno, strerror( errno ) );
112  rtems_test_assert( errno == EBADF );
113}
114
115rtems_task Init(
116  rtems_task_argument argument
117)
118{
119  int   status;
120  int   fd;
121
122  TEST_BEGIN();
123
124  /*
125   *  Simple open case where the file is created.
126   */
127  puts( "mkdir /tmp" );
128  status = mkdir( "/tmp", S_IRWXU );
129  rtems_test_assert( !status );
130
131  puts( "open /tmp/j" );
132  fd = open( "/tmp/j", O_RDWR|O_CREAT, S_IRWXU|S_IRWXG|S_IRWXO );
133  rtems_test_assert( fd != -1 );
134  printf( "open returned file descriptor %d\n", fd );
135
136  puts( "close /tmp/j" );
137  status = close( fd );
138  rtems_test_assert( !status );
139
140  do_with_fd( fd, "an unopened file" );
141  puts("");
142  do_with_fd( 1000, "a too large file descriptor" );
143
144  TEST_END();
145
146  rtems_test_exit(0);
147}
148
149/* configuration information */
150
151#define CONFIGURE_APPLICATION_NEEDS_CONSOLE_DRIVER
152#define CONFIGURE_APPLICATION_NEEDS_CLOCK_DRIVER
153#define CONFIGURE_LIBIO_MAXIMUM_FILE_DESCRIPTORS 4
154#define CONFIGURE_MAXIMUM_TASKS 1
155#define CONFIGURE_INITIAL_EXTENSIONS RTEMS_TEST_INITIAL_EXTENSION
156
157#define CONFIGURE_RTEMS_INIT_TASKS_TABLE
158
159#define CONFIGURE_INIT
160
161#include <rtems/confdefs.h>
162/* end of file */
Note: See TracBrowser for help on using the repository browser.