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) 1989-2011.
0005  *  On-Line Applications Research Corporation (OAR).
0006  *
0007  * Redistribution and use in source and binary forms, with or without
0008  * modification, are permitted provided that the following conditions
0009  * are met:
0010  * 1. Redistributions of source code must retain the above copyright
0011  *    notice, this list of conditions and the following disclaimer.
0012  * 2. Redistributions in binary form must reproduce the above copyright
0013  *    notice, this list of conditions and the following disclaimer in the
0014  *    documentation and/or other materials provided with the distribution.
0015  *
0016  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
0017  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
0018  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
0019  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
0020  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
0021  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
0022  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
0023  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
0024  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
0025  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
0026  * POSSIBILITY OF SUCH DAMAGE.
0027  */
0028 
0029 #ifdef HAVE_CONFIG_H
0030 #include "config.h"
0031 #endif
0032 
0033 #include <sys/stat.h>
0034 #include <fcntl.h>
0035 #include <errno.h>
0036 #include <stdio.h>
0037 #include <stdint.h>
0038 #include <stdlib.h>
0039 #include <string.h>
0040 #include <unistd.h>
0041 
0042 #include "fstest.h"
0043 #include "fs_config.h"
0044 #include <tmacros.h>
0045 
0046 #define BUF_SIZE 100
0047 
0048 const char rtems_test_name[] = "FSPATHEVAL " FILESYSTEM;
0049 const RTEMS_TEST_STATE rtems_test_state = TEST_STATE;
0050 
0051 static void make_multiple_files (char **files,int is_directory)
0052 {
0053   int i;
0054   int status;
0055   int fd;
0056 
0057   i = 0;
0058   if (is_directory) {
0059     while (files[i]) {
0060       printf ("Making directory %s\n", files[i]);
0061       status = mkdir (files[i], S_IRWXU);
0062       rtems_test_assert (!status);
0063       i++;
0064     }
0065   } else {
0066     while (files[i]) {
0067       printf ("Create file %s\n", files[i]);
0068       fd=creat(files[i],S_IRWXU);
0069       status=close(fd);
0070       rtems_test_assert (!status);
0071       i++;
0072     }
0073   }
0074 
0075   puts ("");
0076 }
0077 
0078 static void remove_multiple_files (char **files,int is_directory)
0079 {
0080   int i;
0081   int status;
0082 
0083   i = 0;
0084   while (files[i]) {
0085     i++;
0086   }
0087 
0088   if (is_directory) {
0089     while (i) {
0090       i--;
0091       printf ("Removing directory %s\n", files[i]);
0092       status = rmdir (files[i]);
0093       rtems_test_assert (!status);
0094     }
0095   } else {
0096     while (i) {
0097       i--;
0098       printf ("Removing file %s\n", files[i]);
0099       status = unlink (files[i]);
0100       rtems_test_assert (!status);
0101     }
0102   }
0103 
0104   puts ("");
0105 }
0106 
0107 static void path_eval_test01 (void)
0108 {
0109   char *valid_path[] = {
0110     "/test1/",
0111     "tets2",
0112     "///test3",
0113     "test4////",
0114     "../../test5",
0115     "/test1/../test6",
0116     "./test7/",
0117     ".././test8",
0118     "test8/./../test9",
0119     "///test9/../test10",
0120     0
0121   };
0122   char *valid_file[]={
0123     "/test1",
0124     "tets2",
0125     "///test3",
0126     "test4",
0127     "../../test5",
0128     "/../test6",
0129     "./test7",
0130     ".././test8",
0131     "/./../test9",
0132     "//../test10",
0133     0
0134   };
0135 
0136   char *valid_relative_path[]={
0137     "test1",
0138     "tets2",
0139     "test3",
0140     "test4",
0141     "test5",
0142     "test6",
0143     "test7",
0144     "test8",
0145     "test9",
0146     "test10",
0147     0
0148 
0149   };
0150 
0151   char *valid_name[] = {
0152     "!#$%&()-@^_`{}~'",
0153     "0_1_A",
0154     "aaa bbb",
0155     "ccc....ddd",
0156     " fff",
0157     0
0158   };
0159 
0160 
0161   make_multiple_files(valid_path,1);
0162   make_multiple_files (valid_name,1);
0163 
0164   remove_multiple_files(valid_relative_path,1);
0165   remove_multiple_files(valid_name,1);
0166 
0167   make_multiple_files(valid_file,0);
0168   make_multiple_files (valid_name,0);
0169 
0170   remove_multiple_files(valid_relative_path,0);
0171   remove_multiple_files(valid_name,0);
0172 
0173 }
0174 static void path_eval_test02(void )
0175 {
0176 
0177   int status;
0178   char buf[BUF_SIZE];
0179   char* cwd;
0180 
0181   mode_t mode = S_IRWXU|S_IRWXG|S_IRWXO;
0182   puts("mkdir /tmp/a/b");
0183   status=mkdir("/tmp",mode);
0184   rtems_test_assert(status==0);
0185   status=mkdir("/tmp/a",mode);
0186   rtems_test_assert(status==0);
0187   status=mkdir("/tmp/a/b",mode);
0188   rtems_test_assert(status==0);
0189 
0190   cwd=getcwd(buf,BUF_SIZE);
0191   rtems_test_assert(cwd!=NULL);
0192 
0193   puts("cd /tmp");
0194   status=chdir("/tmp");
0195   rtems_test_assert(status==0);
0196 
0197   status=chdir("a/b");
0198   rtems_test_assert(status==0);
0199 
0200   status=chdir("../b");
0201   rtems_test_assert(status==0);
0202 
0203   status=chdir("../b/.");
0204   rtems_test_assert(status==0);
0205 
0206 }
0207 
0208 void test (void )
0209 {
0210   path_eval_test01();
0211   path_eval_test02();
0212 }