source: rtems/testsuites/fstests/fsimfsgeneric01/init.c @ c625a641

4.115
Last change on this file since c625a641 was c625a641, checked in by Sebastian Huber <sebastian.huber@…>, on 12/21/14 at 19:12:28

Filesystem: Delete node type operation

Use the fstat handler instead.

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