Back to home page

LXR

 
 

    


File indexing completed on 2025-05-11 08:24:31

0001 /* SPDX-License-Identifier: BSD-2-Clause */
0002 
0003 /*
0004  * Copyright (c) 2012 embedded brains GmbH & Co. KG
0005  *
0006  * Redistribution and use in source and binary forms, with or without
0007  * modification, are permitted provided that the following conditions
0008  * are met:
0009  * 1. Redistributions of source code must retain the above copyright
0010  *    notice, this list of conditions and the following disclaimer.
0011  * 2. Redistributions in binary form must reproduce the above copyright
0012  *    notice, this list of conditions and the following disclaimer in the
0013  *    documentation and/or other materials provided with the distribution.
0014  *
0015  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
0016  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
0017  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
0018  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
0019  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
0020  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
0021  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
0022  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
0023  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
0024  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
0025  * POSSIBILITY OF SUCH DAMAGE.
0026  */
0027 
0028 #ifdef HAVE_CONFIG_H
0029 #include "config.h"
0030 #endif
0031 
0032 #include "tmacros.h"
0033 
0034 #include <sys/stat.h>
0035 #include <fcntl.h>
0036 #include <errno.h>
0037 #include <unistd.h>
0038 #include <stdio.h>
0039 
0040 #include <rtems/libio.h>
0041 #include <rtems/rtems-rfs-format.h>
0042 #include <rtems/ramdisk.h>
0043 
0044 const char rtems_test_name[] = "FSROFS 1";
0045 
0046 static const rtems_rfs_format_config rfs_config;
0047 
0048 static const char rda [] = "/dev/rda";
0049 
0050 static const char mnt [] = "/mnt";
0051 
0052 static const char file [] = "/mnt/file";
0053 
0054 static const char not_exist [] = "/mnt/not_exist";
0055 
0056 static void test_mount(bool writeable)
0057 {
0058   int rv;
0059   const void *data = NULL;
0060 
0061   rv = mount(
0062     rda,
0063     mnt,
0064     RTEMS_FILESYSTEM_TYPE_RFS,
0065     writeable ? RTEMS_FILESYSTEM_READ_WRITE : 0,
0066     data
0067   );
0068   rtems_test_assert(rv == 0);
0069 }
0070 
0071 static void test_create_file_system(void)
0072 {
0073   int rv;
0074 
0075   rv = mkdir(mnt, S_IRWXU | S_IRWXG | S_IRWXO);
0076   rtems_test_assert(rv == 0);
0077 
0078   rv = rtems_rfs_format(rda, &rfs_config);
0079   rtems_test_assert(rv == 0);
0080 
0081   test_mount(true);
0082 
0083   rv = mknod(file, S_IFREG | S_IRWXU | S_IRWXG | S_IRWXO, 0);
0084   rtems_test_assert(rv == 0);
0085 
0086   rv = unmount(mnt);
0087   rtems_test_assert(rv == 0);
0088 }
0089 
0090 static void test_rofs(void)
0091 {
0092   int rv;
0093   int fd;
0094   char buf [1];
0095   ssize_t n;
0096 
0097   test_mount(false);
0098 
0099   fd = open(file, O_RDONLY | O_CREAT, S_IRWXU | S_IRWXG | S_IRWXO);
0100   rtems_test_assert(fd >= 0);
0101 
0102   n = read(fd, buf, sizeof(buf));
0103   rtems_test_assert(n == 0);
0104 
0105   errno = 0;
0106   n = write(fd, buf, sizeof(buf));
0107   rtems_test_assert(n == -1);
0108   rtems_test_assert(errno == EBADF);
0109 
0110   errno = 0;
0111   rv = ftruncate(fd, 0);
0112   rtems_test_assert(rv == -1);
0113   rtems_test_assert(errno == EINVAL);
0114 
0115   errno = 0;
0116   rv = fchmod(fd, 0);
0117   rtems_test_assert(rv == -1);
0118   rtems_test_assert(errno == EROFS);
0119 
0120   errno = 0;
0121   rv = fchown(fd, 0, 0);
0122   rtems_test_assert(rv == -1);
0123   rtems_test_assert(errno == EROFS);
0124 
0125   rv = close(fd);
0126   rtems_test_assert(rv == 0);
0127 
0128   errno = 0;
0129   fd = open(not_exist, O_RDONLY | O_CREAT, S_IRWXU | S_IRWXG | S_IRWXO);
0130   rtems_test_assert(fd == -1);
0131   rtems_test_assert(errno == EROFS);
0132 
0133   errno = 0;
0134   rv = mknod(not_exist, S_IFREG | S_IRWXU | S_IRWXG | S_IRWXO, 0);
0135   rtems_test_assert(rv == -1);
0136   rtems_test_assert(errno == EROFS);
0137 
0138   errno = 0;
0139   rv = mkdir(not_exist, S_IRWXU | S_IRWXG | S_IRWXO);
0140   rtems_test_assert(rv == -1);
0141   rtems_test_assert(errno == EROFS);
0142 
0143   errno = 0;
0144   rv = rename(file, not_exist);
0145   rtems_test_assert(rv == -1);
0146   rtems_test_assert(errno == EROFS);
0147 
0148   errno = 0;
0149   rv = link(file, not_exist);
0150   rtems_test_assert(rv == -1);
0151   rtems_test_assert(errno == EROFS);
0152 
0153   errno = 0;
0154   rv = unlink(file);
0155   rtems_test_assert(rv == -1);
0156   rtems_test_assert(errno == EROFS);
0157 
0158   errno = 0;
0159   rv = symlink(file, not_exist);
0160   rtems_test_assert(rv == -1);
0161   rtems_test_assert(errno == EROFS);
0162 
0163   rv = unmount(mnt);
0164   rtems_test_assert(rv == 0);
0165 }
0166 
0167 static void Init(rtems_task_argument arg)
0168 {
0169   TEST_BEGIN();
0170 
0171   test_create_file_system();
0172   test_rofs();
0173 
0174   TEST_END();
0175   rtems_test_exit(0);
0176 }
0177 
0178 rtems_ramdisk_config rtems_ramdisk_configuration [] = {
0179   { .block_size = 512, .block_num = 1024 }
0180 };
0181 
0182 size_t rtems_ramdisk_configuration_size = 1;
0183 
0184 #define CONFIGURE_APPLICATION_NEEDS_CLOCK_DRIVER
0185 #define CONFIGURE_APPLICATION_NEEDS_SIMPLE_CONSOLE_DRIVER
0186 #define CONFIGURE_APPLICATION_EXTRA_DRIVERS RAMDISK_DRIVER_TABLE_ENTRY
0187 #define CONFIGURE_APPLICATION_NEEDS_LIBBLOCK
0188 
0189 #define CONFIGURE_MAXIMUM_FILE_DESCRIPTORS 5
0190 
0191 #define CONFIGURE_FILESYSTEM_RFS
0192 
0193 #define CONFIGURE_MAXIMUM_TASKS 2
0194 
0195 #define CONFIGURE_EXTRA_TASK_STACKS (8 * 1024)
0196 
0197 #define CONFIGURE_INITIAL_EXTENSIONS RTEMS_TEST_INITIAL_EXTENSION
0198 
0199 #define CONFIGURE_RTEMS_INIT_TASKS_TABLE
0200 
0201 #define CONFIGURE_INIT
0202 
0203 #include <rtems/confdefs.h>