[858e013f] | 1 | /* |
---|
| 2 | * COPYRIGHT (c) 1989-2011. |
---|
| 3 | * On-Line Applications Research Corporation (OAR). |
---|
| 4 | * |
---|
| 5 | * The license and distribution terms for this file may be |
---|
| 6 | * found in the file LICENSE in this distribution or at |
---|
| 7 | * http://www.rtems.com/license/LICENSE. |
---|
| 8 | */ |
---|
| 9 | |
---|
[d957805] | 10 | #ifdef HAVE_CONFIG_H |
---|
| 11 | #include "config.h" |
---|
| 12 | #endif |
---|
| 13 | |
---|
[7336d4af] | 14 | #include <sys/stat.h> |
---|
| 15 | #include <fcntl.h> |
---|
| 16 | #include <errno.h> |
---|
| 17 | #include <stdio.h> |
---|
| 18 | #include <stdint.h> |
---|
| 19 | #include <unistd.h> |
---|
| 20 | #include <stdlib.h> |
---|
| 21 | #include <string.h> |
---|
| 22 | #include <unistd.h> |
---|
| 23 | #include <dirent.h> |
---|
| 24 | |
---|
| 25 | #include "fstest.h" |
---|
[bc75887] | 26 | #include "fs_config.h" |
---|
[32448524] | 27 | #include "pmacros.h" |
---|
[7336d4af] | 28 | |
---|
[bc75887] | 29 | const char rtems_test_name[] = "FSSYMLINK " FILESYSTEM; |
---|
| 30 | |
---|
[7336d4af] | 31 | /* |
---|
[6fed43e] | 32 | * Test the function of symlink |
---|
[7336d4af] | 33 | */ |
---|
[858e013f] | 34 | |
---|
[28860ec0] | 35 | static void symlink_test01(void ) |
---|
[7336d4af] | 36 | { |
---|
| 37 | int fd; |
---|
[858e013f] | 38 | char* file01="file"; |
---|
| 39 | char* symlink_file01="symlink"; |
---|
| 40 | char name[20]; |
---|
[7336d4af] | 41 | int status; |
---|
| 42 | struct stat statbuf; |
---|
[858e013f] | 43 | size_t len=strlen(file01); |
---|
| 44 | size_t name_len; |
---|
[6fed43e] | 45 | |
---|
[7336d4af] | 46 | |
---|
[858e013f] | 47 | printf("Create a file named %s\n",file01); |
---|
| 48 | fd=creat(file01,0777); |
---|
[7336d4af] | 49 | status=close(fd); |
---|
| 50 | rtems_test_assert(status==0); |
---|
| 51 | |
---|
[858e013f] | 52 | printf("Create a symlink named %s to %s\n",symlink_file01,file01); |
---|
| 53 | status=symlink(file01,symlink_file01); |
---|
[7336d4af] | 54 | rtems_test_assert(status==0); |
---|
| 55 | |
---|
[858e013f] | 56 | status=stat(file01,&statbuf); |
---|
[7336d4af] | 57 | rtems_test_assert(status==0); |
---|
| 58 | rtems_test_assert(S_ISREG(statbuf.st_mode)); |
---|
| 59 | rtems_test_assert(0==statbuf.st_size); |
---|
| 60 | |
---|
[858e013f] | 61 | status=lstat(symlink_file01,&statbuf); |
---|
[7336d4af] | 62 | rtems_test_assert(status==0); |
---|
| 63 | rtems_test_assert(S_ISLNK(statbuf.st_mode)); |
---|
| 64 | rtems_test_assert(len==statbuf.st_size); |
---|
| 65 | |
---|
[6fed43e] | 66 | |
---|
[858e013f] | 67 | puts("call readlink "); |
---|
| 68 | name_len=readlink(symlink_file01,name,sizeof(name)-1); |
---|
| 69 | rtems_test_assert(name_len!=-1); |
---|
| 70 | name[name_len]='\0'; |
---|
| 71 | rtems_test_assert(!strncmp(name,file01,name_len)); |
---|
| 72 | puts(name); |
---|
[7336d4af] | 73 | |
---|
| 74 | puts("Unlink the file"); |
---|
[6fed43e] | 75 | |
---|
[858e013f] | 76 | status=unlink(file01); |
---|
[7336d4af] | 77 | rtems_test_assert(status==0); |
---|
| 78 | |
---|
[858e013f] | 79 | status=lstat(symlink_file01,&statbuf); |
---|
[7336d4af] | 80 | rtems_test_assert(status==0); |
---|
| 81 | rtems_test_assert(S_ISLNK(statbuf.st_mode)); |
---|
[858e013f] | 82 | rtems_test_assert(len==statbuf.st_size); |
---|
[6fed43e] | 83 | |
---|
[858e013f] | 84 | puts("call readlink "); |
---|
| 85 | name_len=readlink(symlink_file01,name,sizeof(name)-1); |
---|
| 86 | rtems_test_assert(name_len!=-1); |
---|
| 87 | name[name_len]='\0'; |
---|
| 88 | rtems_test_assert(!strncmp(name,file01,name_len)); |
---|
| 89 | status=unlink(symlink_file01); |
---|
[7336d4af] | 90 | rtems_test_assert(status==0); |
---|
| 91 | |
---|
[858e013f] | 92 | printf("Create a dir named %s\n",file01); |
---|
| 93 | status=mkdir (file01,0777); |
---|
[6fed43e] | 94 | |
---|
[858e013f] | 95 | printf("Create a symlink named %s to %s\n",symlink_file01,file01); |
---|
| 96 | status=symlink(file01,symlink_file01); |
---|
[7336d4af] | 97 | rtems_test_assert(status==0); |
---|
| 98 | |
---|
[858e013f] | 99 | status=lstat(symlink_file01,&statbuf); |
---|
[7336d4af] | 100 | rtems_test_assert(status==0); |
---|
| 101 | rtems_test_assert(S_ISLNK(statbuf.st_mode)); |
---|
| 102 | rtems_test_assert(len==statbuf.st_size); |
---|
[858e013f] | 103 | |
---|
[6fed43e] | 104 | |
---|
[858e013f] | 105 | puts("call readlink "); |
---|
| 106 | name_len=readlink(symlink_file01,name,sizeof(name)-1); |
---|
| 107 | rtems_test_assert(name_len!=-1); |
---|
| 108 | name[name_len]='\0'; |
---|
| 109 | rtems_test_assert(!strncmp(name,file01,name_len)); |
---|
| 110 | |
---|
| 111 | name_len=readlink(symlink_file01,name,3); |
---|
| 112 | rtems_test_assert(name_len!=-1); |
---|
| 113 | name[name_len]='\0'; |
---|
| 114 | rtems_test_assert(!strncmp(name,file01,name_len)); |
---|
[7336d4af] | 115 | |
---|
| 116 | puts("rmdir the dir"); |
---|
[858e013f] | 117 | status=rmdir(file01); |
---|
[7336d4af] | 118 | rtems_test_assert(status==0); |
---|
| 119 | |
---|
[858e013f] | 120 | status=lstat(symlink_file01,&statbuf); |
---|
[7336d4af] | 121 | rtems_test_assert(status==0); |
---|
| 122 | rtems_test_assert(S_ISLNK(statbuf.st_mode)); |
---|
| 123 | |
---|
[858e013f] | 124 | status=unlink(symlink_file01); |
---|
[7336d4af] | 125 | rtems_test_assert(status==0); |
---|
| 126 | |
---|
| 127 | } |
---|
| 128 | /* |
---|
[858e013f] | 129 | * symlink loop error test |
---|
[7336d4af] | 130 | */ |
---|
[28860ec0] | 131 | static void symlink_loop_error_test(void ) |
---|
[7336d4af] | 132 | { |
---|
[858e013f] | 133 | char* file01="file01"; |
---|
| 134 | char* file02="file02"; |
---|
| 135 | |
---|
| 136 | char* file04="file04"; |
---|
| 137 | char* path="file01/t"; |
---|
[7336d4af] | 138 | |
---|
| 139 | int status; |
---|
| 140 | |
---|
[858e013f] | 141 | mode_t mode = S_IRWXU | S_IRWXG | S_IRWXO; |
---|
| 142 | |
---|
| 143 | puts("symlink loop erro test"); |
---|
[7336d4af] | 144 | |
---|
[858e013f] | 145 | status=symlink(file01,file02); |
---|
[7336d4af] | 146 | rtems_test_assert(status==0); |
---|
[858e013f] | 147 | status=symlink(file02,file01); |
---|
[7336d4af] | 148 | rtems_test_assert(status==0); |
---|
[6fed43e] | 149 | |
---|
[858e013f] | 150 | |
---|
| 151 | EXPECT_ERROR(ELOOP,creat,path,mode); |
---|
| 152 | EXPECT_ERROR(ELOOP,open,path,O_CREAT|O_WRONLY,mode); |
---|
| 153 | EXPECT_ERROR(ELOOP,truncate,path,0); |
---|
| 154 | EXPECT_ERROR(ELOOP,rename,path,file04); |
---|
| 155 | EXPECT_ERROR(ELOOP,unlink,path); |
---|
| 156 | EXPECT_ERROR(ELOOP,mkdir,path,mode); |
---|
| 157 | EXPECT_ERROR(ELOOP,rmdir,path); |
---|
[7336d4af] | 158 | } |
---|
| 159 | |
---|
[858e013f] | 160 | void test(void ) |
---|
[7336d4af] | 161 | { |
---|
| 162 | |
---|
| 163 | puts( "\n\n*** SYMLINK TEST ***" ); |
---|
| 164 | symlink_test01(); |
---|
[858e013f] | 165 | symlink_loop_error_test(); |
---|
[7336d4af] | 166 | puts( "*** END OF SYMLINK TEST ***" ); |
---|
| 167 | |
---|
| 168 | } |
---|
| 169 | |
---|