Back to home page

LXR

 
 

    


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

0001 /* SPDX-License-Identifier: BSD-2-Clause */
0002 
0003 /**
0004  * @file
0005  *
0006  * @ingroup tests
0007  *
0008  * @brief rtems_mkdir() test.
0009  */
0010 
0011 /*
0012  * Copyright (c) 2010 embedded brains GmbH & Co. KG
0013  *
0014  * Redistribution and use in source and binary forms, with or without
0015  * modification, are permitted provided that the following conditions
0016  * are met:
0017  * 1. Redistributions of source code must retain the above copyright
0018  *    notice, this list of conditions and the following disclaimer.
0019  * 2. Redistributions in binary form must reproduce the above copyright
0020  *    notice, this list of conditions and the following disclaimer in the
0021  *    documentation and/or other materials provided with the distribution.
0022  *
0023  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
0024  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
0025  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
0026  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
0027  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
0028  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
0029  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
0030  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
0031  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
0032  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
0033  * POSSIBILITY OF SUCH DAMAGE.
0034  */
0035 
0036 #ifdef HAVE_CONFIG_H
0037 #include "config.h"
0038 #endif
0039 
0040 #include <sys/stat.h>
0041 #include <errno.h>
0042 #include <fcntl.h>
0043 
0044 #include <rtems/libio.h>
0045 
0046 #include "tmacros.h"
0047 
0048 const char rtems_test_name[] = "SPMKDIR";
0049 
0050 static void test_mkdir(const char *path, mode_t omode, int expected_rv)
0051 {
0052   struct stat st;
0053   int rv = 0;
0054   mode_t current_umask = umask(0);
0055   mode_t dirmode = S_IFDIR | (omode & ~current_umask);
0056 
0057   umask(current_umask);
0058 
0059   rv = rtems_mkdir(path, omode);
0060   rtems_test_assert(rv == expected_rv);
0061 
0062   if (rv == 0) {
0063     rv = stat(path, &st);
0064     rtems_test_assert(rv == 0 && st.st_mode == dirmode);
0065   }
0066 }
0067 
0068 static rtems_task Init(rtems_task_argument argument)
0069 {
0070   mode_t omode = S_IRWXU | S_IRWXG | S_IRWXO;
0071   int rv = 0;
0072 
0073   TEST_BEGIN();
0074 
0075   puts( "rtems_mkdir a - OK" );
0076   test_mkdir("a", omode, 0);
0077   puts( "rtems_mkdir a/b - OK" );
0078   test_mkdir("a/b", omode, 0);
0079   puts( "rtems_mkdir a/b/c/d/e/f/g/h/i - OK" );
0080   test_mkdir("a/b/c/d/e/f/g/h/i", omode, 0);
0081   puts( "rtems_mkdir a/b/c - OK" );
0082   test_mkdir("a/b/c", omode, 0);
0083   puts( "rtems_mkdir a/b/c/1 - OK" );
0084   test_mkdir("a/b/c/1", 0, 0);
0085   puts( "rtems_mkdir a/b/c/2 - OK" );
0086   test_mkdir("a/b/c/2", S_IRWXU, 0);
0087   puts( "rtems_mkdir a/b/c/3 - OK" );
0088   test_mkdir("a/b/c/3", S_IRWXG, 0);
0089   puts( "rtems_mkdir a/b/c/4 - OK" );
0090   test_mkdir("a/b/c/4", S_IRWXO, 0);
0091   puts( "rtems_mkdir a/b - OK" );
0092   test_mkdir("a/b", omode, 0);
0093   puts( "rtems_mkdir a - OK" );
0094   test_mkdir("a", omode, 0);
0095   puts( "rtems_mkdir a/b/x - OK" );
0096   test_mkdir("a/b/x", S_IRUSR, 0);
0097   puts( "rtems_mkdir a/b/x/y - expect failure" );
0098   test_mkdir("a/b/x/y", S_IRUSR, -1);
0099   puts( "mknod regular file a/n - OK" );  
0100   rv = mknod("a/n", S_IRWXU | S_IFREG, 0LL);
0101   puts( "rtems_mkdir a/n/b - expect failure" );
0102   test_mkdir("a/n/b", S_IRUSR, -1);
0103 
0104   puts( "Create node b and open in RDONLY mode - OK" );
0105   rv = open ("b", O_CREAT | O_RDONLY, omode);
0106   rtems_test_assert(rv >= 0);
0107 
0108   puts( "Closing b - OK" );
0109   rv = close(rv);
0110   rtems_test_assert(rv == 0);
0111 
0112   puts( "rtems_mkdir b - expect failure" );
0113   test_mkdir("b", omode, -1);
0114   rtems_test_assert(errno == EEXIST);
0115 
0116   TEST_END();
0117 
0118   exit(0);
0119 }
0120 
0121 #define CONFIGURE_INIT
0122 
0123 #define CONFIGURE_APPLICATION_DOES_NOT_NEED_CLOCK_DRIVER
0124 #define CONFIGURE_APPLICATION_NEEDS_SIMPLE_CONSOLE_DRIVER
0125 
0126 #define CONFIGURE_MAXIMUM_FILE_DESCRIPTORS 4
0127 
0128 #define CONFIGURE_MAXIMUM_TASKS 1
0129 
0130 #define CONFIGURE_INITIAL_EXTENSIONS RTEMS_TEST_INITIAL_EXTENSION
0131 
0132 #define CONFIGURE_RTEMS_INIT_TASKS_TABLE
0133 
0134 #include <rtems/confdefs.h>