source: rtems/testsuites/fstests/fsimfsgeneric01/init.c @ 62f3fa84

5
Last change on this file since 62f3fa84 was 62f3fa84, checked in by Sebastian Huber <sebastian.huber@…>, on 09/13/17 at 13:18:16

fstests/fsimfsgeneric01: Fix test assert

  • Property mode set to 100644
File size: 9.8 KB
RevLine 
[c5392ef9]1/*
[c625a641]2 * Copyright (c) 2012-2014 embedded brains GmbH.  All rights reserved.
[c5392ef9]3 *
4 *  embedded brains GmbH
[c625a641]5 *  Dornierstr. 4
[c5392ef9]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
[c499856]12 * http://www.rtems.org/license/LICENSE.
[c5392ef9]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>
[2f68778]23#include <sys/uio.h>
[c5392ef9]24#include <fcntl.h>
25#include <unistd.h>
26#include <errno.h>
27
28#include <rtems/imfs.h>
29#include <rtems/malloc.h>
[3b91605]30#include <rtems/libcsupport.h>
[c5392ef9]31
[bc75887]32const char rtems_test_name[] = "FSIMFSGENERIC 1";
33
[c5392ef9]34typedef enum {
35  TEST_NEW,
36  TEST_INITIALIZED,
[c625a641]37  TEST_FSTAT_OPEN_0,
38  TEST_FSTAT_OPEN_1,
[c5392ef9]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,
[2f68778]48  TEST_READV,
49  TEST_WRITEV,
[c5392ef9]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
[c625a641]65  rtems_test_assert(*state == TEST_FSTAT_OPEN_1);
[c5392ef9]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
[2f68778]77  rtems_test_assert(*state == TEST_WRITEV);
[c5392ef9]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,
[9d1522ed]113  ioctl_command_t request,
[c5392ef9]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:
[c625a641]148      *state = TEST_FSTAT_OPEN_0;
149      break;
150    case TEST_FSTAT_OPEN_0:
151      *state = TEST_FSTAT_OPEN_1;
[c5392ef9]152      break;
153    case TEST_CLOSED:
154      *state = TEST_FSTAT_UNLINK;
155      break;
156    default:
[62f3fa84]157      rtems_test_assert(0);
[c5392ef9]158      break;
159  }
160
[c625a641]161  buf->st_mode = S_IFCHR | S_IRWXU | S_IRWXG | S_IRWXO;
162
163  return 0;
[c5392ef9]164}
165
166static int handler_ftruncate(
167  rtems_libio_t *iop,
168  off_t length
169)
170{
171  test_state *state = IMFS_generic_get_context_by_iop(iop);
172
173  rtems_test_assert(*state == TEST_LSEEK);
174  *state = TEST_FTRUNCATE;
175
176  return 0;
177}
178
179static int handler_fsync(
180  rtems_libio_t *iop
181)
182{
183  test_state *state = IMFS_generic_get_context_by_iop(iop);
184
185  rtems_test_assert(*state == TEST_FTRUNCATE);
186  *state = TEST_FSYNC;
187
188  return 0;
189}
190
191static int handler_fdatasync(
192  rtems_libio_t *iop
193)
194{
195  test_state *state = IMFS_generic_get_context_by_iop(iop);
196
197  rtems_test_assert(*state == TEST_FSYNC);
198  *state = TEST_FDATASYNC;
199
200  return 0;
201}
202
203static int handler_fcntl(
204  rtems_libio_t *iop,
205  int cmd
206)
207{
208  test_state *state = IMFS_generic_get_context_by_iop(iop);
209
210  rtems_test_assert(*state == TEST_FDATASYNC);
211  *state = TEST_FCNTL;
212
213  return 0;
214}
215
[2f68778]216static ssize_t handler_readv(
217  rtems_libio_t *iop,
218  const struct iovec *iov,
219  int iovcnt,
220  ssize_t total
221)
222{
223  test_state *state = IMFS_generic_get_context_by_iop(iop);
224
225  rtems_test_assert(*state == TEST_FCNTL);
226  *state = TEST_READV;
227
228  return 0;
229}
230
231static ssize_t handler_writev(
232  rtems_libio_t *iop,
233  const struct iovec *iov,
234  int iovcnt,
235  ssize_t total
236)
237{
238  test_state *state = IMFS_generic_get_context_by_iop(iop);
239
240  rtems_test_assert(*state == TEST_READV);
241  *state = TEST_WRITEV;
242
243  return 0;
244}
245
[c5392ef9]246static const rtems_filesystem_file_handlers_r node_handlers = {
247  .open_h = handler_open,
248  .close_h = handler_close,
249  .read_h = handler_read,
250  .write_h = handler_write,
251  .ioctl_h = handler_ioctl,
252  .lseek_h = handler_lseek,
253  .fstat_h = handler_fstat,
254  .ftruncate_h = handler_ftruncate,
255  .fsync_h = handler_fsync,
256  .fdatasync_h = handler_fdatasync,
[2f68778]257  .fcntl_h = handler_fcntl,
258  .readv_h = handler_readv,
259  .writev_h = handler_writev
[c5392ef9]260};
261
262static IMFS_jnode_t *node_initialize(
263  IMFS_jnode_t *node,
[cf36b70]264  void *arg
[c5392ef9]265)
266{
267  test_state *state = NULL;
268
[cf36b70]269  node = IMFS_node_initialize_generic(node, arg);
[c5392ef9]270  state = IMFS_generic_get_context_by_node(node);
271
272  rtems_test_assert(*state == TEST_NEW);
273  *state = TEST_INITIALIZED;
274
275  return node;
276}
277
[c17d0b3]278static IMFS_jnode_t *node_remove(IMFS_jnode_t *node)
[c5392ef9]279{
280  test_state *state = IMFS_generic_get_context_by_node(node);
281
282  rtems_test_assert(*state == TEST_FSTAT_UNLINK);
283  *state = TEST_REMOVED;
284
285  return node;
286}
287
[cf36b70]288static void node_destroy(IMFS_jnode_t *node)
[c5392ef9]289{
290  test_state *state = IMFS_generic_get_context_by_node(node);
291
292  rtems_test_assert(*state == TEST_REMOVED);
293  *state = TEST_DESTROYED;
294
[cf36b70]295  IMFS_node_destroy_default(node);
[c5392ef9]296}
297
298static const IMFS_node_control node_control = {
299  .handlers = &node_handlers,
300  .node_initialize = node_initialize,
301  .node_remove = node_remove,
302  .node_destroy = node_destroy
303};
304
305static void test_imfs_make_generic_node(void)
306{
[39d7d51]307  test_state state = TEST_NEW;
[c5392ef9]308  int rv = 0;
309  int fd = 0;
310  const char *path = "generic";
311  char buf [1];
312  ssize_t n = 0;
313  off_t off = 0;
[2f68778]314  struct iovec iov = {
315    .iov_base = &buf [0],
316    .iov_len = (int) sizeof(buf)
317  };
[c5392ef9]318
319  rv = IMFS_make_generic_node(
320    path,
321    S_IFCHR | S_IRWXU | S_IRWXG | S_IRWXO,
322    &node_control,
[39d7d51]323    &state
[c5392ef9]324  );
325  rtems_test_assert(rv == 0);
326
327  fd = open(path, O_RDWR);
328  rtems_test_assert(fd >= 0);
329
330  n = read(fd, buf, sizeof(buf));
331  rtems_test_assert(n == 0);
332
333  n = write(fd, buf, sizeof(buf));
334  rtems_test_assert(n == 0);
335
336  rv = ioctl(fd, 0);
337  rtems_test_assert(rv == 0);
338
339  off = lseek(fd, off, SEEK_SET);
340  rtems_test_assert(off == 0);
341
342  rv = ftruncate(fd, 0);
343  rtems_test_assert(rv == 0);
344
345  rv = fsync(fd);
346  rtems_test_assert(rv == 0);
347
348  rv = fdatasync(fd);
349  rtems_test_assert(rv == 0);
350
351  rv = fcntl(fd, F_GETFD);
352  rtems_test_assert(rv >= 0);
353
[2f68778]354  rv = readv(fd, &iov, 1);
355  rtems_test_assert(rv == 0);
356
357  rv = writev(fd, &iov, 1);
358  rtems_test_assert(rv == 0);
359
[c5392ef9]360  rv = close(fd);
361  rtems_test_assert(rv == 0);
362
363  rv = unlink(path);
364  rtems_test_assert(rv == 0);
365
[39d7d51]366  rtems_test_assert(state == TEST_DESTROYED);
[c5392ef9]367}
368
[3b91605]369static IMFS_jnode_t *node_initialize_error(
370  IMFS_jnode_t *node,
[cf36b70]371  void *arg
[3b91605]372)
373{
374  errno = EIO;
375
376  return NULL;
377}
378
379static IMFS_jnode_t *node_remove_inhibited(IMFS_jnode_t *node)
380{
381  rtems_test_assert(false);
382
383  return node;
384}
385
[cf36b70]386static void node_destroy_inhibited(IMFS_jnode_t *node)
[3b91605]387{
388  rtems_test_assert(false);
389}
390
391static const IMFS_node_control node_initialization_error_control = {
392  .handlers = &node_handlers,
393  .node_initialize = node_initialize_error,
394  .node_remove = node_remove_inhibited,
395  .node_destroy = node_destroy_inhibited
[c5392ef9]396};
397
[a9df916]398static const rtems_filesystem_operations_table *imfs_ops;
399
400static int other_clone(rtems_filesystem_location_info_t *loc)
401{
402  return (*imfs_ops->clonenod_h)(loc);
403}
404
[c5392ef9]405static void test_imfs_make_generic_node_errors(void)
406{
407  int rv = 0;
408  const char *path = "generic";
409  rtems_chain_control *chain = &rtems_filesystem_mount_table;
410  rtems_filesystem_mount_table_entry_t *mt_entry =
411    (rtems_filesystem_mount_table_entry_t *) rtems_chain_first(chain);
[a9df916]412  rtems_filesystem_operations_table other_ops;
[c5392ef9]413  void *opaque = NULL;
[3b91605]414  rtems_resource_snapshot before;
415
416  rtems_resource_snapshot_take(&before);
[c5392ef9]417
418  errno = 0;
419  rv = IMFS_make_generic_node(
420    path,
421    S_IFDIR | S_IRWXU | S_IRWXG | S_IRWXO,
422    &node_control,
423    NULL
424  );
425  rtems_test_assert(rv == -1);
426  rtems_test_assert(errno == EINVAL);
[3b91605]427  rtems_test_assert(rtems_resource_snapshot_check(&before));
[c5392ef9]428
429  errno = 0;
[a9df916]430  imfs_ops = mt_entry->ops;
431  other_ops = *imfs_ops;
432  other_ops.clonenod_h = other_clone;
433  mt_entry->ops = &other_ops;
[c5392ef9]434  rv = IMFS_make_generic_node(
435    path,
436    S_IFCHR | S_IRWXU | S_IRWXG | S_IRWXO,
437    &node_control,
438    NULL
439  );
[a9df916]440  mt_entry->ops = imfs_ops;
[c5392ef9]441  rtems_test_assert(rv == -1);
442  rtems_test_assert(errno == ENOTSUP);
[3b91605]443  rtems_test_assert(rtems_resource_snapshot_check(&before));
[c5392ef9]444
445  errno = 0;
[3b91605]446  opaque = rtems_heap_greedy_allocate(NULL, 0);
[c5392ef9]447  rv = IMFS_make_generic_node(
448    path,
449    S_IFCHR | S_IRWXU | S_IRWXG | S_IRWXO,
450    &node_control,
451    NULL
452  );
[3b91605]453  rtems_heap_greedy_free(opaque);
[c5392ef9]454  rtems_test_assert(rv == -1);
455  rtems_test_assert(errno == ENOMEM);
[3b91605]456  rtems_test_assert(rtems_resource_snapshot_check(&before));
457
458  errno = 0;
459  rv = IMFS_make_generic_node(
460    path,
461    S_IFCHR | S_IRWXU | S_IRWXG | S_IRWXO,
462    &node_initialization_error_control,
463    NULL
464  );
465  rtems_test_assert(rv == -1);
466  rtems_test_assert(errno == EIO);
467  rtems_test_assert(rtems_resource_snapshot_check(&before));
[22cd282]468
469  errno = 0;
470  rv = IMFS_make_generic_node(
471    "/nil/nada",
472    S_IFCHR | S_IRWXU | S_IRWXG | S_IRWXO,
473    &node_control,
474    NULL
475  );
476  rtems_test_assert(rv == -1);
477  rtems_test_assert(errno == ENOENT);
478  rtems_test_assert(rtems_resource_snapshot_check(&before));
[c5392ef9]479}
480
481static void Init(rtems_task_argument arg)
482{
[bc75887]483  TEST_BEGIN();
[c5392ef9]484
485  test_imfs_make_generic_node();
486  test_imfs_make_generic_node_errors();
487
[bc75887]488  TEST_END();
[c5392ef9]489  rtems_test_exit(0);
490}
491
492#define CONFIGURE_APPLICATION_NEEDS_CLOCK_DRIVER
493#define CONFIGURE_APPLICATION_NEEDS_CONSOLE_DRIVER
494
495#define CONFIGURE_LIBIO_MAXIMUM_FILE_DESCRIPTORS 4
496
497#define CONFIGURE_MAXIMUM_TASKS 1
498
[bc75887]499#define CONFIGURE_INITIAL_EXTENSIONS RTEMS_TEST_INITIAL_EXTENSION
500
[c5392ef9]501#define CONFIGURE_RTEMS_INIT_TASKS_TABLE
502
503#define CONFIGURE_INIT
504
505#include <rtems/confdefs.h>
Note: See TracBrowser for help on using the repository browser.