summaryrefslogtreecommitdiffstats
path: root/testsuites/psxtests/psxfchx01/init.c
blob: 89a0d0603357f0ea46560c3cebe9e9ccb719175f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
/*
 *  COPYRIGHT (c) 1989-2012.
 *  On-Line Applications Research Corporation (OAR).
 *
 *  The license and distribution terms for this file may be
 *  found in the file LICENSE in this distribution or at
 *  http://www.rtems.org/license/LICENSE.
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <bsp.h>
#include <pmacros.h>
#include <unistd.h>
#include <errno.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>

const char rtems_test_name[] = "PSXFCHX 1";

/* forward declarations to avoid warnings */
rtems_task Init(rtems_task_argument ignored);

rtems_task Init(
  rtems_task_argument ignored
)
{
  int status = 0;
  int fd = 0;

  TEST_BEGIN();

  /****************************************************
   *                   fchdir tests
   ***************************************************/

  puts( "Init - fchdir tests" );

  puts( "Init - Attempt fchdir with bad file descriptor - expect EBADF" );
  status = fchdir( 5 );
  rtems_test_assert( status == -1 );
  rtems_test_assert( errno == EBADF );

  puts( "Init - Attempt fchdir with bad file descriptor - expect EBADF" );
  status = fchdir( 3 );
  rtems_test_assert( status == -1 );
  rtems_test_assert( errno == EBADF );
  
  puts( "Init - opening /newfile in write-mode -- OK" );
  fd = open( "/newfile", O_WRONLY | O_CREAT, S_IWUSR | S_IXUSR );
  rtems_test_assert( fd != -1 );

  puts( "Init - fchdir on the file descriptor - expect ENOTDIR" );
  status = fchdir( fd );
  rtems_test_assert( status == -1 );
  rtems_test_assert( errno == ENOTDIR );

  puts( "Init - closing /newfile -- OK" );
  status = close( fd );
  rtems_test_assert( status == 0 );

  puts( "Init - removing /newfile -- OK" );
  status = unlink( "/newfile" );
  rtems_test_assert( status == 0 );

  puts( "Init - opening /newfile in read-mode -- OK" );
  fd = open( "/newfile", O_RDONLY | O_CREAT, S_IRUSR | S_IXUSR);
  rtems_test_assert( fd != -1 );
  
  puts( "Init - fchdir on the file descriptor - expect ENOTDIR" );
  status = fchdir( fd );
  rtems_test_assert( status == -1 );
  rtems_test_assert( errno == ENOTDIR );

  puts( "Init - closing and removing /newfile -- OK" );
  status = close( fd );
  status |= unlink( "/newfile" );
  rtems_test_assert( status == 0 );

  puts( "Init - create directory  /tmp - RWX permissions -- OK" );
  status = mkdir( "/tmp", S_IRWXU );
  rtems_test_assert( status == 0 );

  puts( "Init - open the /tmp, get the file descriptor -- OK" );
  fd = open( "/tmp", O_RDONLY );
  rtems_test_assert( fd != -1 );

  puts( "Init - fchdir on the file descriptor -- OK" );
  status = fchdir( fd );
  rtems_test_assert( status == 0 );

  puts( "Init - close the file descriptor -- OK" );
  status = close( fd );
  rtems_test_assert( status == 0 );

  puts( "Init - remove directory /tmp -- OK" );
  status = rmdir( "/tmp" );
  rtems_test_assert( status == 0 );

  puts( "Init - creating directory /tmp - read permission -- OK" );
  status = mkdir( "/tmp", S_IRUSR );
  rtems_test_assert( status == 0 );

  puts( "Init - open the /tmp, get the file descriptor -- OK" );
  fd = open( "/tmp", O_RDONLY );
  rtems_test_assert( fd != -1 );

  puts( "Init - attempt fchdir on the file descriptor -- expect EACCES" );
  status = fchdir( fd );
  rtems_test_assert( status == -1);
  rtems_test_assert( errno == EACCES );

  puts( "Init - close the file descriptor -- OK" );
  status = close( fd );
  rtems_test_assert( status == 0 );

  puts( "Init - remove directory /tmp -- OK" );
  status = rmdir( "/tmp" );
  rtems_test_assert( status == 0 );

  puts( "End of fchdir tests" );

  /****************************************************
   *                   fchmod tests
   ***************************************************/

  puts( "\nInit - fchmod tests" );

  puts( "Init - fchmod, with a bad file descriptor - expect EBADF" );
  status = fchmod( 4, 0 );
  rtems_test_assert( status == -1 );
  rtems_test_assert( errno == EBADF );

  puts( "Init - fchmod, with an unopened file descriptor - expect EBADF" );
  status = fchmod( 3, 0 );
  rtems_test_assert( status == -1 );
  rtems_test_assert( errno == EBADF );

  puts( "Init - open new file: /newfile in read-only mode -- OK" );
  fd = open( "/newfile", O_RDONLY | O_CREAT, S_IRWXU );
  rtems_test_assert( fd != -1 );
  
  puts( "Init - fchmod, with the opened file descriptor -- OK" );
  status = fchmod( fd, 0 );
  rtems_test_assert( status == 0 );

  puts( "Init - close and remove /newfile" );
  status = close( fd );
  status |= unlink( "/newfile" );
  rtems_test_assert( status == 0 );

  puts( "Init - open new file: /newfile in read-write mode -- OK" );
  fd = open( "/newfile", O_RDWR | O_CREAT, S_IRWXU );
  rtems_test_assert( fd != -1 );
  
  puts( "Init - fchmod, with the opened file descriptor -- OK" );
  status = fchmod( fd, S_IRUSR );
  rtems_test_assert( status == 0 );

  puts( "Init - close and remove /newfile -- OK" );
  status = close( fd );
  status |= unlink( "/newfile" );
  rtems_test_assert( status == 0 );

  puts( "End of fchmod tests" );

  /****************************************************
   *                   fchown tests
   ***************************************************/

  puts( "\nInit - fchown tests" );

  puts( "Init - fchown, with a bad file descriptor - expect EBADF" );
  status = fchown( 4, 0, 0 );
  rtems_test_assert( status == -1 );
  rtems_test_assert( errno == EBADF );

  puts( "Init - fchown, with an unopened file descriptor - expect EBADF" );
  status = fchown( 3, 0, 0 );
  rtems_test_assert( status == -1 );
  rtems_test_assert( errno == EBADF );

  puts( "Init - open new file: /newfile in read-only mode -- OK" );
  fd = open( "/newfile", O_RDONLY | O_CREAT, S_IRWXU );
  rtems_test_assert( fd != -1 );
  
  puts( "Init - fchown, with the opened file descriptor - OK" );
  status = fchown( fd, 0, 0 );
  rtems_test_assert( status == 0 );

  puts( "Init - close and remove /newfile" );
  status = close( fd );
  status |= unlink( "/newfile" );
  rtems_test_assert( status == 0 );

  puts( "Init - open new file: /newfile in read-write mode -- OK" );
  fd = open( "/newfile", O_RDWR | O_CREAT, S_IRWXU );
  rtems_test_assert( fd != -1 );
  
  puts( "Init - fchown, with the opened file descriptor -- OK" );
  status = fchown( fd, 1, 0 );
  rtems_test_assert( status == 0 );

  puts( "Init - close and remove /newfile -- OK" );
  status = close( fd );
  status |= unlink( "/newfile" );
  rtems_test_assert( status == 0 );

  puts( "End of fchown tests" );

  TEST_END();
  rtems_test_exit( 0 );
}

/* configuration information */

#define CONFIGURE_APPLICATION_NEEDS_CONSOLE_DRIVER
#define CONFIGURE_APPLICATION_NEEDS_CLOCK_DRIVER

#define CONFIGURE_LIBIO_MAXIMUM_FILE_DESCRIPTORS 4

#define CONFIGURE_MAXIMUM_TASKS 1
#define CONFIGURE_INITIAL_EXTENSIONS RTEMS_TEST_INITIAL_EXTENSION

#define CONFIGURE_RTEMS_INIT_TASKS_TABLE

#define CONFIGURE_INIT
#include <rtems/confdefs.h>
/* end of file */