source: rtems/testsuites/fstests/fsnofs01/init.c

Last change on this file was bcef89f2, checked in by Sebastian Huber <sebastian.huber@…>, on 05/19/23 at 06:18:25

Update company name

The embedded brains GmbH & Co. KG is the legal successor of embedded
brains GmbH.

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