1 | /* |
---|
2 | * Copyright (c) 2012 embedded brains GmbH. All rights reserved. |
---|
3 | * |
---|
4 | * embedded brains GmbH |
---|
5 | * Obere Lagerstr. 30 |
---|
6 | * 82178 Puchheim |
---|
7 | * Germany |
---|
8 | * <rtems@embedded-brains.de> |
---|
9 | * |
---|
10 | * The license and distribution terms for this file may be |
---|
11 | * found in the file LICENSE in this distribution or at |
---|
12 | * http://www.rtems.com/license/LICENSE. |
---|
13 | */ |
---|
14 | |
---|
15 | #ifdef HAVE_CONFIG_H |
---|
16 | #include "config.h" |
---|
17 | #endif |
---|
18 | |
---|
19 | #include "tmacros.h" |
---|
20 | |
---|
21 | #include <sys/stat.h> |
---|
22 | #include <sys/ioctl.h> |
---|
23 | #include <sys/uio.h> |
---|
24 | #include <fcntl.h> |
---|
25 | #include <unistd.h> |
---|
26 | #include <errno.h> |
---|
27 | |
---|
28 | #include <rtems/imfs.h> |
---|
29 | #include <rtems/malloc.h> |
---|
30 | #include <rtems/libcsupport.h> |
---|
31 | |
---|
32 | const char rtems_test_name[] = "FSIMFSGENERIC 1"; |
---|
33 | |
---|
34 | typedef enum { |
---|
35 | TEST_NEW, |
---|
36 | TEST_INITIALIZED, |
---|
37 | TEST_FSTAT_OPEN, |
---|
38 | TEST_OPEN, |
---|
39 | TEST_READ, |
---|
40 | TEST_WRITE, |
---|
41 | TEST_IOCTL, |
---|
42 | TEST_LSEEK, |
---|
43 | TEST_FTRUNCATE, |
---|
44 | TEST_FSYNC, |
---|
45 | TEST_FDATASYNC, |
---|
46 | TEST_FCNTL, |
---|
47 | TEST_READV, |
---|
48 | TEST_WRITEV, |
---|
49 | TEST_CLOSED, |
---|
50 | TEST_FSTAT_UNLINK, |
---|
51 | TEST_REMOVED, |
---|
52 | TEST_DESTROYED |
---|
53 | } test_state; |
---|
54 | |
---|
55 | static int handler_open( |
---|
56 | rtems_libio_t *iop, |
---|
57 | const char *path, |
---|
58 | int oflag, |
---|
59 | mode_t mode |
---|
60 | ) |
---|
61 | { |
---|
62 | test_state *state = IMFS_generic_get_context_by_iop(iop); |
---|
63 | |
---|
64 | rtems_test_assert(*state == TEST_FSTAT_OPEN); |
---|
65 | *state = TEST_OPEN; |
---|
66 | |
---|
67 | return 0; |
---|
68 | } |
---|
69 | |
---|
70 | static int handler_close( |
---|
71 | rtems_libio_t *iop |
---|
72 | ) |
---|
73 | { |
---|
74 | test_state *state = IMFS_generic_get_context_by_iop(iop); |
---|
75 | |
---|
76 | rtems_test_assert(*state == TEST_WRITEV); |
---|
77 | *state = TEST_CLOSED; |
---|
78 | |
---|
79 | return 0; |
---|
80 | } |
---|
81 | |
---|
82 | static ssize_t handler_read( |
---|
83 | rtems_libio_t *iop, |
---|
84 | void *buffer, |
---|
85 | size_t count |
---|
86 | ) |
---|
87 | { |
---|
88 | test_state *state = IMFS_generic_get_context_by_iop(iop); |
---|
89 | |
---|
90 | rtems_test_assert(*state == TEST_OPEN); |
---|
91 | *state = TEST_READ; |
---|
92 | |
---|
93 | return 0; |
---|
94 | } |
---|
95 | |
---|
96 | static ssize_t handler_write( |
---|
97 | rtems_libio_t *iop, |
---|
98 | const void *buffer, |
---|
99 | size_t count |
---|
100 | ) |
---|
101 | { |
---|
102 | test_state *state = IMFS_generic_get_context_by_iop(iop); |
---|
103 | |
---|
104 | rtems_test_assert(*state == TEST_READ); |
---|
105 | *state = TEST_WRITE; |
---|
106 | |
---|
107 | return 0; |
---|
108 | } |
---|
109 | |
---|
110 | static int handler_ioctl( |
---|
111 | rtems_libio_t *iop, |
---|
112 | uint32_t request, |
---|
113 | void *buffer |
---|
114 | ) |
---|
115 | { |
---|
116 | test_state *state = IMFS_generic_get_context_by_iop(iop); |
---|
117 | |
---|
118 | rtems_test_assert(*state == TEST_WRITE); |
---|
119 | *state = TEST_IOCTL; |
---|
120 | |
---|
121 | return 0; |
---|
122 | } |
---|
123 | |
---|
124 | static off_t handler_lseek( |
---|
125 | rtems_libio_t *iop, |
---|
126 | off_t length, |
---|
127 | int whence |
---|
128 | ) |
---|
129 | { |
---|
130 | test_state *state = IMFS_generic_get_context_by_iop(iop); |
---|
131 | |
---|
132 | rtems_test_assert(*state == TEST_IOCTL); |
---|
133 | *state = TEST_LSEEK; |
---|
134 | |
---|
135 | return 0; |
---|
136 | } |
---|
137 | |
---|
138 | static int handler_fstat( |
---|
139 | const rtems_filesystem_location_info_t *loc, |
---|
140 | struct stat *buf |
---|
141 | ) |
---|
142 | { |
---|
143 | test_state *state = IMFS_generic_get_context_by_location(loc); |
---|
144 | |
---|
145 | switch (*state) { |
---|
146 | case TEST_INITIALIZED: |
---|
147 | *state = TEST_FSTAT_OPEN; |
---|
148 | break; |
---|
149 | case TEST_CLOSED: |
---|
150 | *state = TEST_FSTAT_UNLINK; |
---|
151 | break; |
---|
152 | default: |
---|
153 | rtems_test_assert(0); |
---|
154 | break; |
---|
155 | } |
---|
156 | |
---|
157 | return rtems_filesystem_default_fstat(loc, buf); |
---|
158 | } |
---|
159 | |
---|
160 | static int handler_ftruncate( |
---|
161 | rtems_libio_t *iop, |
---|
162 | off_t length |
---|
163 | ) |
---|
164 | { |
---|
165 | test_state *state = IMFS_generic_get_context_by_iop(iop); |
---|
166 | |
---|
167 | rtems_test_assert(*state == TEST_LSEEK); |
---|
168 | *state = TEST_FTRUNCATE; |
---|
169 | |
---|
170 | return 0; |
---|
171 | } |
---|
172 | |
---|
173 | static int handler_fsync( |
---|
174 | rtems_libio_t *iop |
---|
175 | ) |
---|
176 | { |
---|
177 | test_state *state = IMFS_generic_get_context_by_iop(iop); |
---|
178 | |
---|
179 | rtems_test_assert(*state == TEST_FTRUNCATE); |
---|
180 | *state = TEST_FSYNC; |
---|
181 | |
---|
182 | return 0; |
---|
183 | } |
---|
184 | |
---|
185 | static int handler_fdatasync( |
---|
186 | rtems_libio_t *iop |
---|
187 | ) |
---|
188 | { |
---|
189 | test_state *state = IMFS_generic_get_context_by_iop(iop); |
---|
190 | |
---|
191 | rtems_test_assert(*state == TEST_FSYNC); |
---|
192 | *state = TEST_FDATASYNC; |
---|
193 | |
---|
194 | return 0; |
---|
195 | } |
---|
196 | |
---|
197 | static int handler_fcntl( |
---|
198 | rtems_libio_t *iop, |
---|
199 | int cmd |
---|
200 | ) |
---|
201 | { |
---|
202 | test_state *state = IMFS_generic_get_context_by_iop(iop); |
---|
203 | |
---|
204 | rtems_test_assert(*state == TEST_FDATASYNC); |
---|
205 | *state = TEST_FCNTL; |
---|
206 | |
---|
207 | return 0; |
---|
208 | } |
---|
209 | |
---|
210 | static ssize_t handler_readv( |
---|
211 | rtems_libio_t *iop, |
---|
212 | const struct iovec *iov, |
---|
213 | int iovcnt, |
---|
214 | ssize_t total |
---|
215 | ) |
---|
216 | { |
---|
217 | test_state *state = IMFS_generic_get_context_by_iop(iop); |
---|
218 | |
---|
219 | rtems_test_assert(*state == TEST_FCNTL); |
---|
220 | *state = TEST_READV; |
---|
221 | |
---|
222 | return 0; |
---|
223 | } |
---|
224 | |
---|
225 | static ssize_t handler_writev( |
---|
226 | rtems_libio_t *iop, |
---|
227 | const struct iovec *iov, |
---|
228 | int iovcnt, |
---|
229 | ssize_t total |
---|
230 | ) |
---|
231 | { |
---|
232 | test_state *state = IMFS_generic_get_context_by_iop(iop); |
---|
233 | |
---|
234 | rtems_test_assert(*state == TEST_READV); |
---|
235 | *state = TEST_WRITEV; |
---|
236 | |
---|
237 | return 0; |
---|
238 | } |
---|
239 | |
---|
240 | static const rtems_filesystem_file_handlers_r node_handlers = { |
---|
241 | .open_h = handler_open, |
---|
242 | .close_h = handler_close, |
---|
243 | .read_h = handler_read, |
---|
244 | .write_h = handler_write, |
---|
245 | .ioctl_h = handler_ioctl, |
---|
246 | .lseek_h = handler_lseek, |
---|
247 | .fstat_h = handler_fstat, |
---|
248 | .ftruncate_h = handler_ftruncate, |
---|
249 | .fsync_h = handler_fsync, |
---|
250 | .fdatasync_h = handler_fdatasync, |
---|
251 | .fcntl_h = handler_fcntl, |
---|
252 | .readv_h = handler_readv, |
---|
253 | .writev_h = handler_writev |
---|
254 | }; |
---|
255 | |
---|
256 | static IMFS_jnode_t *node_initialize( |
---|
257 | IMFS_jnode_t *node, |
---|
258 | const IMFS_types_union *info |
---|
259 | ) |
---|
260 | { |
---|
261 | test_state *state = NULL; |
---|
262 | |
---|
263 | node = IMFS_node_initialize_generic(node, info); |
---|
264 | state = IMFS_generic_get_context_by_node(node); |
---|
265 | |
---|
266 | rtems_test_assert(*state == TEST_NEW); |
---|
267 | *state = TEST_INITIALIZED; |
---|
268 | |
---|
269 | return node; |
---|
270 | } |
---|
271 | |
---|
272 | static IMFS_jnode_t *node_remove(IMFS_jnode_t *node) |
---|
273 | { |
---|
274 | test_state *state = IMFS_generic_get_context_by_node(node); |
---|
275 | |
---|
276 | rtems_test_assert(*state == TEST_FSTAT_UNLINK); |
---|
277 | *state = TEST_REMOVED; |
---|
278 | |
---|
279 | return node; |
---|
280 | } |
---|
281 | |
---|
282 | static IMFS_jnode_t *node_destroy(IMFS_jnode_t *node) |
---|
283 | { |
---|
284 | test_state *state = IMFS_generic_get_context_by_node(node); |
---|
285 | |
---|
286 | rtems_test_assert(*state == TEST_REMOVED); |
---|
287 | *state = TEST_DESTROYED; |
---|
288 | |
---|
289 | return node; |
---|
290 | } |
---|
291 | |
---|
292 | static const IMFS_node_control node_control = { |
---|
293 | .imfs_type = IMFS_GENERIC, |
---|
294 | .handlers = &node_handlers, |
---|
295 | .node_initialize = node_initialize, |
---|
296 | .node_remove = node_remove, |
---|
297 | .node_destroy = node_destroy |
---|
298 | }; |
---|
299 | |
---|
300 | static void test_imfs_make_generic_node(void) |
---|
301 | { |
---|
302 | test_state state = TEST_NEW; |
---|
303 | int rv = 0; |
---|
304 | int fd = 0; |
---|
305 | const char *path = "generic"; |
---|
306 | char buf [1]; |
---|
307 | ssize_t n = 0; |
---|
308 | off_t off = 0; |
---|
309 | struct iovec iov = { |
---|
310 | .iov_base = &buf [0], |
---|
311 | .iov_len = (int) sizeof(buf) |
---|
312 | }; |
---|
313 | |
---|
314 | rv = IMFS_make_generic_node( |
---|
315 | path, |
---|
316 | S_IFCHR | S_IRWXU | S_IRWXG | S_IRWXO, |
---|
317 | &node_control, |
---|
318 | &state |
---|
319 | ); |
---|
320 | rtems_test_assert(rv == 0); |
---|
321 | |
---|
322 | fd = open(path, O_RDWR); |
---|
323 | rtems_test_assert(fd >= 0); |
---|
324 | |
---|
325 | n = read(fd, buf, sizeof(buf)); |
---|
326 | rtems_test_assert(n == 0); |
---|
327 | |
---|
328 | n = write(fd, buf, sizeof(buf)); |
---|
329 | rtems_test_assert(n == 0); |
---|
330 | |
---|
331 | rv = ioctl(fd, 0); |
---|
332 | rtems_test_assert(rv == 0); |
---|
333 | |
---|
334 | off = lseek(fd, off, SEEK_SET); |
---|
335 | rtems_test_assert(off == 0); |
---|
336 | |
---|
337 | rv = ftruncate(fd, 0); |
---|
338 | rtems_test_assert(rv == 0); |
---|
339 | |
---|
340 | rv = fsync(fd); |
---|
341 | rtems_test_assert(rv == 0); |
---|
342 | |
---|
343 | rv = fdatasync(fd); |
---|
344 | rtems_test_assert(rv == 0); |
---|
345 | |
---|
346 | rv = fcntl(fd, F_GETFD); |
---|
347 | rtems_test_assert(rv >= 0); |
---|
348 | |
---|
349 | rv = readv(fd, &iov, 1); |
---|
350 | rtems_test_assert(rv == 0); |
---|
351 | |
---|
352 | rv = writev(fd, &iov, 1); |
---|
353 | rtems_test_assert(rv == 0); |
---|
354 | |
---|
355 | rv = close(fd); |
---|
356 | rtems_test_assert(rv == 0); |
---|
357 | |
---|
358 | rv = unlink(path); |
---|
359 | rtems_test_assert(rv == 0); |
---|
360 | |
---|
361 | rtems_test_assert(state == TEST_DESTROYED); |
---|
362 | } |
---|
363 | |
---|
364 | static IMFS_jnode_t *node_initialize_error( |
---|
365 | IMFS_jnode_t *node, |
---|
366 | const IMFS_types_union *info |
---|
367 | ) |
---|
368 | { |
---|
369 | errno = EIO; |
---|
370 | |
---|
371 | return NULL; |
---|
372 | } |
---|
373 | |
---|
374 | static IMFS_jnode_t *node_remove_inhibited(IMFS_jnode_t *node) |
---|
375 | { |
---|
376 | rtems_test_assert(false); |
---|
377 | |
---|
378 | return node; |
---|
379 | } |
---|
380 | |
---|
381 | static IMFS_jnode_t *node_destroy_inhibited(IMFS_jnode_t *node) |
---|
382 | { |
---|
383 | rtems_test_assert(false); |
---|
384 | |
---|
385 | return node; |
---|
386 | } |
---|
387 | |
---|
388 | static const IMFS_node_control node_invalid_control = { |
---|
389 | .imfs_type = IMFS_DIRECTORY, |
---|
390 | .handlers = &node_handlers, |
---|
391 | .node_initialize = node_initialize_error, |
---|
392 | .node_remove = node_remove_inhibited, |
---|
393 | .node_destroy = node_destroy_inhibited |
---|
394 | }; |
---|
395 | |
---|
396 | static const IMFS_node_control node_initialization_error_control = { |
---|
397 | .imfs_type = IMFS_GENERIC, |
---|
398 | .handlers = &node_handlers, |
---|
399 | .node_initialize = node_initialize_error, |
---|
400 | .node_remove = node_remove_inhibited, |
---|
401 | .node_destroy = node_destroy_inhibited |
---|
402 | }; |
---|
403 | |
---|
404 | static void test_imfs_make_generic_node_errors(void) |
---|
405 | { |
---|
406 | int rv = 0; |
---|
407 | const char *path = "generic"; |
---|
408 | rtems_chain_control *chain = &rtems_filesystem_mount_table; |
---|
409 | rtems_filesystem_mount_table_entry_t *mt_entry = |
---|
410 | (rtems_filesystem_mount_table_entry_t *) rtems_chain_first(chain); |
---|
411 | const char *type = mt_entry->type; |
---|
412 | void *opaque = NULL; |
---|
413 | rtems_resource_snapshot before; |
---|
414 | |
---|
415 | rtems_resource_snapshot_take(&before); |
---|
416 | |
---|
417 | errno = 0; |
---|
418 | rv = IMFS_make_generic_node( |
---|
419 | path, |
---|
420 | S_IFCHR | S_IRWXU | S_IRWXG | S_IRWXO, |
---|
421 | &node_invalid_control, |
---|
422 | NULL |
---|
423 | ); |
---|
424 | rtems_test_assert(rv == -1); |
---|
425 | rtems_test_assert(errno == EINVAL); |
---|
426 | rtems_test_assert(rtems_resource_snapshot_check(&before)); |
---|
427 | |
---|
428 | errno = 0; |
---|
429 | rv = IMFS_make_generic_node( |
---|
430 | path, |
---|
431 | S_IFDIR | S_IRWXU | S_IRWXG | S_IRWXO, |
---|
432 | &node_control, |
---|
433 | NULL |
---|
434 | ); |
---|
435 | rtems_test_assert(rv == -1); |
---|
436 | rtems_test_assert(errno == EINVAL); |
---|
437 | rtems_test_assert(rtems_resource_snapshot_check(&before)); |
---|
438 | |
---|
439 | errno = 0; |
---|
440 | mt_entry->type = "XXX"; |
---|
441 | rv = IMFS_make_generic_node( |
---|
442 | path, |
---|
443 | S_IFCHR | S_IRWXU | S_IRWXG | S_IRWXO, |
---|
444 | &node_control, |
---|
445 | NULL |
---|
446 | ); |
---|
447 | mt_entry->type = type; |
---|
448 | rtems_test_assert(rv == -1); |
---|
449 | rtems_test_assert(errno == ENOTSUP); |
---|
450 | rtems_test_assert(rtems_resource_snapshot_check(&before)); |
---|
451 | |
---|
452 | errno = 0; |
---|
453 | opaque = rtems_heap_greedy_allocate(NULL, 0); |
---|
454 | rv = IMFS_make_generic_node( |
---|
455 | path, |
---|
456 | S_IFCHR | S_IRWXU | S_IRWXG | S_IRWXO, |
---|
457 | &node_control, |
---|
458 | NULL |
---|
459 | ); |
---|
460 | rtems_heap_greedy_free(opaque); |
---|
461 | rtems_test_assert(rv == -1); |
---|
462 | rtems_test_assert(errno == ENOMEM); |
---|
463 | rtems_test_assert(rtems_resource_snapshot_check(&before)); |
---|
464 | |
---|
465 | errno = 0; |
---|
466 | rv = IMFS_make_generic_node( |
---|
467 | path, |
---|
468 | S_IFCHR | S_IRWXU | S_IRWXG | S_IRWXO, |
---|
469 | &node_initialization_error_control, |
---|
470 | NULL |
---|
471 | ); |
---|
472 | rtems_test_assert(rv == -1); |
---|
473 | rtems_test_assert(errno == EIO); |
---|
474 | rtems_test_assert(rtems_resource_snapshot_check(&before)); |
---|
475 | } |
---|
476 | |
---|
477 | static void Init(rtems_task_argument arg) |
---|
478 | { |
---|
479 | TEST_BEGIN(); |
---|
480 | |
---|
481 | test_imfs_make_generic_node(); |
---|
482 | test_imfs_make_generic_node_errors(); |
---|
483 | |
---|
484 | TEST_END(); |
---|
485 | rtems_test_exit(0); |
---|
486 | } |
---|
487 | |
---|
488 | #define CONFIGURE_APPLICATION_NEEDS_CLOCK_DRIVER |
---|
489 | #define CONFIGURE_APPLICATION_NEEDS_CONSOLE_DRIVER |
---|
490 | |
---|
491 | #define CONFIGURE_LIBIO_MAXIMUM_FILE_DESCRIPTORS 4 |
---|
492 | |
---|
493 | #define CONFIGURE_USE_IMFS_AS_BASE_FILESYSTEM |
---|
494 | |
---|
495 | #define CONFIGURE_MAXIMUM_TASKS 1 |
---|
496 | |
---|
497 | #define CONFIGURE_INITIAL_EXTENSIONS RTEMS_TEST_INITIAL_EXTENSION |
---|
498 | |
---|
499 | #define CONFIGURE_RTEMS_INIT_TASKS_TABLE |
---|
500 | |
---|
501 | #define CONFIGURE_INIT |
---|
502 | |
---|
503 | #include <rtems/confdefs.h> |
---|