source: rtems/testsuites/fstests/fsnofs01/init.c @ 98c6d50

5
Last change on this file since 98c6d50 was 98c6d50, checked in by Chris Johns <chrisj@…>, on 10/19/17 at 05:39:16

testsuite: Use printk for all test output where possible.

  • Remove the printf support leaving the direct printk support configured with TESTS_USE_PRINTK and all other output goes via a buffered vsniprintf call to printk.
  • Control the test's single init for functions and global data with TEST_INIT and not CONFIGURE_INIT. They are now separate.

Updates #3170.

  • Property mode set to 100644
File size: 13.5 KB
Line 
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.org/license/LICENSE.
13 */
14
15#ifdef HAVE_CONFIG_H
16  #include "config.h"
17#endif
18
19#define TEST_INIT
20
21#define TESTS_USE_PRINTK
22#include "tmacros.h"
23
24#include <sys/stat.h>
25#include <sys/statvfs.h>
26#include <errno.h>
27#include <fcntl.h>
28#include <stdio.h>
29#include <stdlib.h>
30#include <utime.h>
31
32#include <rtems/libio_.h>
33
34const char rtems_test_name[] = "FSNOFS 1";
35
36static int node_count(const rtems_chain_control *chain)
37{
38  int count = 0;
39  const rtems_chain_node *current = rtems_chain_immutable_first(chain);
40  const rtems_chain_node *tail = rtems_chain_immutable_tail(chain);
41
42  while (current != tail) {
43    ++count;
44
45    current = rtems_chain_immutable_next(current);
46  }
47
48  return count;
49}
50
51static void rtems_test_assert_equal_to_null_loc(
52  const rtems_filesystem_location_info_t *local_loc
53)
54{
55  rtems_filesystem_global_location_t *null_loc =
56    &rtems_filesystem_global_location_null;
57
58  rtems_test_assert(null_loc->location.node_access == local_loc->node_access);
59  rtems_test_assert(null_loc->location.node_access_2 == local_loc->node_access_2);
60  rtems_test_assert(null_loc->location.handlers == local_loc->handlers);
61  rtems_test_assert(null_loc->location.mt_entry == local_loc->mt_entry);
62}
63
64static void test_initial_values(void)
65{
66  rtems_filesystem_global_location_t *null_loc =
67    &rtems_filesystem_global_location_null;
68  rtems_filesystem_mount_table_entry_t *null_mt = null_loc->location.mt_entry;
69  rtems_chain_control *loc_chain = &null_mt->location_chain;
70  rtems_chain_node *loc_node = &null_loc->location.mt_entry_node;
71
72  rtems_test_assert(node_count(loc_chain) == 1);
73  rtems_test_assert(rtems_chain_previous(loc_node) == rtems_chain_head(loc_chain));
74  rtems_test_assert(rtems_chain_next(loc_node) == rtems_chain_tail(loc_chain));
75  rtems_test_assert(null_mt->mt_point_node == null_loc);
76  rtems_test_assert(null_mt->mt_fs_root == null_loc);
77  rtems_test_assert(!null_mt->mounted);
78  rtems_test_assert(!null_mt->writeable);
79  rtems_test_assert(null_loc->reference_count == 4);
80  rtems_test_assert(null_loc->deferred_released_next == NULL);
81  rtems_test_assert(null_loc->deferred_released_count == 0);
82}
83
84static void test_location_obtain(void)
85{
86  rtems_filesystem_global_location_t *global_loc = NULL;
87  rtems_filesystem_global_location_t *null_loc =
88    rtems_filesystem_global_location_obtain(&global_loc);
89  rtems_filesystem_mount_table_entry_t *null_mt = null_loc->location.mt_entry;
90  rtems_chain_control *loc_chain = &null_mt->location_chain;
91
92  rtems_test_assert(node_count(loc_chain) == 1);
93  rtems_test_assert(null_loc->reference_count == 5);
94
95  rtems_filesystem_global_location_release(null_loc, false);
96
97  rtems_test_assert(node_count(loc_chain) == 1);
98  rtems_test_assert(null_loc->reference_count == 4);
99}
100
101static void test_null_location_obtain(void)
102{
103  rtems_filesystem_global_location_t *null_loc =
104    rtems_filesystem_global_location_obtain_null();
105  rtems_filesystem_mount_table_entry_t *null_mt = null_loc->location.mt_entry;
106  rtems_chain_control *loc_chain = &null_mt->location_chain;
107
108  rtems_test_assert(node_count(loc_chain) == 1);
109  rtems_test_assert(null_loc->reference_count == 5);
110
111  rtems_filesystem_global_location_release(null_loc, false);
112
113  rtems_test_assert(node_count(loc_chain) == 1);
114  rtems_test_assert(null_loc->reference_count == 4);
115}
116
117static void test_null_location_replace(void)
118{
119  rtems_filesystem_global_location_t *null_loc =
120    &rtems_filesystem_global_location_null;
121  rtems_filesystem_mount_table_entry_t *null_mt = null_loc->location.mt_entry;
122  rtems_chain_control *loc_chain = &null_mt->location_chain;
123  rtems_filesystem_location_info_t local_loc;
124
125  rtems_test_assert(node_count(loc_chain) == 1);
126  rtems_test_assert(null_loc->reference_count == 4);
127  rtems_test_assert(rtems_filesystem_global_location_is_null(null_loc));
128
129  rtems_filesystem_location_copy(&local_loc, &null_loc->location);
130
131  rtems_test_assert(node_count(loc_chain) == 2);
132  rtems_test_assert(null_loc->reference_count == 4);
133
134  rtems_filesystem_location_detach(&local_loc);
135
136  rtems_test_assert(node_count(loc_chain) == 2);
137  rtems_test_assert(null_loc->reference_count == 4);
138  rtems_test_assert(rtems_filesystem_location_is_null(&local_loc));
139  rtems_test_assert_equal_to_null_loc(&local_loc);
140
141  rtems_filesystem_location_free(&local_loc);
142
143  rtems_test_assert(node_count(loc_chain) == 1);
144  rtems_test_assert(null_loc->reference_count == 4);
145}
146
147static void test_null_location_get_and_replace(void)
148{
149  rtems_filesystem_global_location_t *null_loc =
150    &rtems_filesystem_global_location_null;
151  rtems_filesystem_mount_table_entry_t *null_mt = null_loc->location.mt_entry;
152  rtems_chain_control *loc_chain = &null_mt->location_chain;
153  rtems_filesystem_location_info_t local_loc_0;
154  rtems_filesystem_location_info_t local_loc_1;
155
156  rtems_test_assert(node_count(loc_chain) == 1);
157  rtems_test_assert(null_loc->reference_count == 4);
158
159  rtems_filesystem_location_copy(&local_loc_0, &null_loc->location);
160
161  rtems_test_assert(node_count(loc_chain) == 2);
162  rtems_test_assert(null_loc->reference_count == 4);
163  rtems_test_assert_equal_to_null_loc(&local_loc_0);
164
165  rtems_filesystem_location_copy_and_detach(&local_loc_1, &local_loc_0);
166
167  rtems_test_assert(node_count(loc_chain) == 3);
168  rtems_test_assert(null_loc->reference_count == 4);
169  rtems_test_assert_equal_to_null_loc(&local_loc_0);
170  rtems_test_assert_equal_to_null_loc(&local_loc_1);
171
172  rtems_filesystem_location_free(&local_loc_0);
173
174  rtems_test_assert(node_count(loc_chain) == 2);
175  rtems_test_assert(null_loc->reference_count == 4);
176  rtems_test_assert_equal_to_null_loc(&local_loc_1);
177
178  rtems_filesystem_location_free(&local_loc_1);
179
180  rtems_test_assert(node_count(loc_chain) == 1);
181  rtems_test_assert(null_loc->reference_count == 4);
182}
183
184static void test_path_ops(void)
185{
186  int rv = 0;
187  long lrv = 0;
188  struct stat st;
189  struct statvfs stvfs;
190  char buf [32];
191  ssize_t n = 0;
192  const char *path = "/";
193  const struct utimbuf times;
194
195  errno = 0;
196  rv = open(path, O_RDONLY);
197  rtems_test_assert(rv == -1);
198  rtems_test_assert(errno == ENXIO);
199
200  errno = 0;
201  rv = chdir(path);
202  rtems_test_assert(rv == -1);
203  rtems_test_assert(errno == ENXIO);
204
205  errno = 0;
206  rv = chroot(path);
207  rtems_test_assert(rv == -1);
208  rtems_test_assert(errno == ENXIO);
209
210  errno = 0;
211  rv = mknod(path, S_IFREG, 0);
212  rtems_test_assert(rv == -1);
213  rtems_test_assert(errno == ENXIO);
214
215  errno = 0;
216  rv = mkdir(path, 0);
217  rtems_test_assert(rv == -1);
218  rtems_test_assert(errno == ENXIO);
219
220  errno = 0;
221  rv = mkfifo(path, 0);
222  rtems_test_assert(rv == -1);
223  rtems_test_assert(errno == ENXIO);
224
225  errno = 0;
226  rv = stat(path, &st);
227  rtems_test_assert(rv == -1);
228  rtems_test_assert(errno == ENXIO);
229
230  errno = 0;
231  rv = lstat(path, &st);
232  rtems_test_assert(rv == -1);
233  rtems_test_assert(errno == ENXIO);
234
235  errno = 0;
236  rv = statvfs(path, &stvfs);
237  rtems_test_assert(rv == -1);
238  rtems_test_assert(errno == ENXIO);
239
240  errno = 0;
241  n = readlink(path, buf, sizeof(buf));
242  rtems_test_assert(n == -1);
243  rtems_test_assert(errno == ENXIO);
244
245  errno = 0;
246  rv = chmod(path, 0);
247  rtems_test_assert(rv == -1);
248  rtems_test_assert(errno == ENXIO);
249
250  errno = 0;
251  rv = chown(path, 0, 0);
252  rtems_test_assert(rv == -1);
253  rtems_test_assert(errno == ENXIO);
254
255  errno = 0;
256  rv = lchown(path, 0, 0);
257  rtems_test_assert(rv == -1);
258  rtems_test_assert(errno == ENXIO);
259
260  errno = 0;
261  rv = rmdir(path);
262  rtems_test_assert(rv == -1);
263  rtems_test_assert(errno == ENXIO);
264
265  errno = 0;
266  rv = unlink(path);
267  rtems_test_assert(rv == -1);
268  rtems_test_assert(errno == ENXIO);
269
270  errno = 0;
271  rv = truncate(path, 0);
272  rtems_test_assert(rv == -1);
273  rtems_test_assert(errno == ENXIO);
274
275  errno = 0;
276  rv = access(path, 0);
277  rtems_test_assert(rv == -1);
278  rtems_test_assert(errno == ENXIO);
279
280  errno = 0;
281  lrv = pathconf(path, _PC_LINK_MAX);
282  rtems_test_assert(lrv == -1);
283  rtems_test_assert(errno == ENXIO);
284
285  errno = 0;
286  rv = link(path, path);
287  rtems_test_assert(rv == -1);
288  rtems_test_assert(errno == ENXIO);
289
290  errno = 0;
291  rv = symlink(path, path);
292  rtems_test_assert(rv == -1);
293  rtems_test_assert(errno == ENXIO);
294
295  errno = 0;
296  rv = rename(path, path);
297  rtems_test_assert(rv == -1);
298  rtems_test_assert(errno == ENXIO);
299
300  errno = 0;
301  rv = utime(path, &times);
302  rtems_test_assert(rv == -1);
303  rtems_test_assert(errno == ENXIO);
304}
305
306static void test_user_env(void)
307{
308  rtems_status_code sc = RTEMS_SUCCESSFUL;
309  rtems_filesystem_global_location_t *null_loc =
310    &rtems_filesystem_global_location_null;
311  rtems_filesystem_mount_table_entry_t *null_mt = null_loc->location.mt_entry;
312  rtems_chain_control *loc_chain = &null_mt->location_chain;
313
314  rtems_test_assert(node_count(loc_chain) == 1);
315  rtems_test_assert(null_loc->reference_count == 4);
316
317  sc = rtems_libio_set_private_env();
318  rtems_test_assert(sc == RTEMS_UNSATISFIED);
319
320  rtems_test_assert(node_count(loc_chain) == 1);
321  rtems_test_assert(null_loc->reference_count == 4);
322
323  rtems_libio_use_global_env();
324
325  rtems_test_assert(node_count(loc_chain) == 1);
326  rtems_test_assert(null_loc->reference_count == 4);
327}
328
329typedef struct {
330  int flags;
331  mode_t object_mode;
332  uid_t object_uid;
333  gid_t object_gid;
334  bool expected_ok;
335} check_access_case;
336
337#define FR RTEMS_FS_PERMS_READ
338#define FW RTEMS_FS_PERMS_WRITE
339#define FX RTEMS_FS_PERMS_EXEC
340
341#define UR S_IRUSR
342#define UW S_IWUSR
343#define UX S_IXUSR
344
345#define GR S_IRGRP
346#define GW S_IWGRP
347#define GX S_IXGRP
348
349#define OR S_IROTH
350#define OW S_IWOTH
351#define OX S_IXOTH
352
353static const check_access_case check_access_euid_0_cases[] = {
354  { 0,   0, 6, 7, true },
355  { FR,  0, 6, 7, false },
356  { FW,  0, 6, 7, false },
357  { FX,  0, 6, 7, false },
358  { FR, UR, 6, 7, true },
359  { FW, UW, 6, 7, true },
360  { FX, UX, 6, 7, true },
361  { FR, GR, 6, 7, false },
362  { FW, GW, 6, 7, false },
363  { FX, GX, 6, 7, false },
364  { FR, OR, 6, 7, false },
365  { FW, OW, 6, 7, false },
366  { FX, OX, 6, 7, false }
367};
368
369static const check_access_case check_access_egid_0_cases[] = {
370  { 0,   0, 6, 7, true },
371  { FR,  0, 6, 7, false },
372  { FW,  0, 6, 7, false },
373  { FX,  0, 6, 7, false },
374  { FR, UR, 6, 7, false },
375  { FW, UW, 6, 7, false },
376  { FX, UX, 6, 7, false },
377  { FR, GR, 6, 7, true },
378  { FW, GW, 6, 7, true },
379  { FX, GX, 6, 7, true },
380  { FR, OR, 6, 7, false },
381  { FW, OW, 6, 7, false },
382  { FX, OX, 6, 7, false }
383};
384
385static const check_access_case check_access_other_cases[] = {
386  { 0,   0, 3, 7, true },
387  { FR,  0, 3, 7, false },
388  { FW,  0, 3, 7, false },
389  { FX,  0, 3, 7, false },
390  { FR, UR, 3, 7, true },
391  { FW, UW, 3, 7, true },
392  { FX, UX, 3, 7, true },
393  { FR, GR, 3, 7, false },
394  { FW, GW, 3, 7, false },
395  { FX, GX, 3, 7, false },
396  { FR, OR, 3, 7, false },
397  { FW, OW, 3, 7, false },
398  { FX, OX, 3, 7, false },
399  { 0,   0, 6, 4, true },
400  { FR,  0, 6, 4, false },
401  { FW,  0, 6, 4, false },
402  { FX,  0, 6, 4, false },
403  { FR, UR, 6, 4, false },
404  { FW, UW, 6, 4, false },
405  { FX, UX, 6, 4, false },
406  { FR, GR, 6, 4, true },
407  { FW, GW, 6, 4, true },
408  { FX, GX, 6, 4, true },
409  { FR, OR, 6, 4, false },
410  { FW, OW, 6, 4, false },
411  { FX, OX, 6, 4, false },
412  { 0,   0, 6, 5, true },
413  { FR,  0, 6, 5, false },
414  { FW,  0, 6, 5, false },
415  { FX,  0, 6, 5, false },
416  { FR, UR, 6, 5, false },
417  { FW, UW, 6, 5, false },
418  { FX, UX, 6, 5, false },
419  { FR, GR, 6, 5, true },
420  { FW, GW, 6, 5, true },
421  { FX, GX, 6, 5, true },
422  { FR, OR, 6, 5, false },
423  { FW, OW, 6, 5, false },
424  { FX, OX, 6, 5, false },
425  { 0,   0, 6, 7, true },
426  { FR,  0, 6, 7, false },
427  { FW,  0, 6, 7, false },
428  { FX,  0, 6, 7, false },
429  { FR, UR, 6, 7, false },
430  { FW, UW, 6, 7, false },
431  { FX, UX, 6, 7, false },
432  { FR, GR, 6, 7, false },
433  { FW, GW, 6, 7, false },
434  { FX, GX, 6, 7, false },
435  { FR, OR, 6, 7, true },
436  { FW, OW, 6, 7, true },
437  { FX, OX, 6, 7, true }
438};
439
440static void check_access(const check_access_case *table, size_t n)
441{
442  size_t i;
443
444  for (i = 0; i < n; ++i) {
445    const check_access_case *cac = &table[i];
446    bool ok = rtems_filesystem_check_access(
447      cac->flags,
448      cac->object_mode,
449      cac->object_uid,
450      cac->object_gid
451    );
452
453    rtems_test_assert(ok == cac->expected_ok);
454  }
455}
456
457static void test_check_access(void)
458{
459  rtems_user_env_t *uenv = rtems_current_user_env_get();
460
461  rtems_test_assert(uenv->uid == 0);
462  rtems_test_assert(uenv->gid == 0);
463  rtems_test_assert(uenv->euid == 0);
464  rtems_test_assert(uenv->egid == 0);
465  rtems_test_assert(uenv->ngroups == 0);
466
467  uenv->uid = 1;
468  uenv->gid = 2;
469
470  check_access(
471    &check_access_euid_0_cases[0],
472    RTEMS_ARRAY_SIZE(check_access_euid_0_cases)
473  );
474
475  uenv->euid = 3;
476
477  check_access(
478    &check_access_egid_0_cases[0],
479    RTEMS_ARRAY_SIZE(check_access_egid_0_cases)
480  );
481
482  uenv->egid = 4;
483  uenv->ngroups = 1;
484  uenv->groups[0] = 5;
485
486  check_access(
487    &check_access_other_cases[0],
488    RTEMS_ARRAY_SIZE(check_access_other_cases)
489  );
490
491  uenv->uid = 0;
492  uenv->gid = 0;
493  uenv->euid = 0;
494  uenv->egid = 0;
495  uenv->ngroups = 0;
496}
497
498static void Init(rtems_task_argument arg)
499{
500  TEST_BEGIN();
501
502  test_initial_values();
503  test_location_obtain();
504  test_null_location_obtain();
505  test_null_location_replace();
506  test_null_location_get_and_replace();
507  test_path_ops();
508  test_user_env();
509  test_check_access();
510
511  TEST_END();
512  exit(0);
513}
514
515#define CONFIGURE_APPLICATION_NEEDS_CLOCK_DRIVER
516
517#define CONFIGURE_APPLICATION_DISABLE_FILESYSTEM
518
519#define CONFIGURE_MAXIMUM_TASKS 1
520
521#define CONFIGURE_INITIAL_EXTENSIONS RTEMS_TEST_INITIAL_EXTENSION
522
523#define CONFIGURE_RTEMS_INIT_TASKS_TABLE
524
525#define CONFIGURE_INIT
526
527#include <rtems/confdefs.h>
Note: See TracBrowser for help on using the repository browser.