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 | |
---|
10 | #ifdef HAVE_CONFIG_H |
---|
11 | #include "config.h" |
---|
12 | #endif |
---|
13 | |
---|
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" |
---|
26 | #include "fs_config.h" |
---|
27 | #include "pmacros.h" |
---|
28 | |
---|
29 | const char rtems_test_name[] = "FSSYMLINK " FILESYSTEM; |
---|
30 | |
---|
31 | /* |
---|
32 | * Test the function of symlink |
---|
33 | */ |
---|
34 | |
---|
35 | static void symlink_test01(void ) |
---|
36 | { |
---|
37 | int fd; |
---|
38 | char* file01="file"; |
---|
39 | char* symlink_file01="symlink"; |
---|
40 | char name[20]; |
---|
41 | int status; |
---|
42 | struct stat statbuf; |
---|
43 | size_t len=strlen(file01); |
---|
44 | size_t name_len; |
---|
45 | |
---|
46 | |
---|
47 | printf("Create a file named %s\n",file01); |
---|
48 | fd=creat(file01,0777); |
---|
49 | status=close(fd); |
---|
50 | rtems_test_assert(status==0); |
---|
51 | |
---|
52 | printf("Create a symlink named %s to %s\n",symlink_file01,file01); |
---|
53 | status=symlink(file01,symlink_file01); |
---|
54 | rtems_test_assert(status==0); |
---|
55 | |
---|
56 | status=stat(file01,&statbuf); |
---|
57 | rtems_test_assert(status==0); |
---|
58 | rtems_test_assert(S_ISREG(statbuf.st_mode)); |
---|
59 | rtems_test_assert(0==statbuf.st_size); |
---|
60 | |
---|
61 | status=lstat(symlink_file01,&statbuf); |
---|
62 | rtems_test_assert(status==0); |
---|
63 | rtems_test_assert(S_ISLNK(statbuf.st_mode)); |
---|
64 | rtems_test_assert(len==statbuf.st_size); |
---|
65 | |
---|
66 | |
---|
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); |
---|
73 | |
---|
74 | puts("Unlink the file"); |
---|
75 | |
---|
76 | status=unlink(file01); |
---|
77 | rtems_test_assert(status==0); |
---|
78 | |
---|
79 | status=lstat(symlink_file01,&statbuf); |
---|
80 | rtems_test_assert(status==0); |
---|
81 | rtems_test_assert(S_ISLNK(statbuf.st_mode)); |
---|
82 | rtems_test_assert(len==statbuf.st_size); |
---|
83 | |
---|
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); |
---|
90 | rtems_test_assert(status==0); |
---|
91 | |
---|
92 | printf("Create a dir named %s\n",file01); |
---|
93 | status=mkdir (file01,0777); |
---|
94 | |
---|
95 | printf("Create a symlink named %s to %s\n",symlink_file01,file01); |
---|
96 | status=symlink(file01,symlink_file01); |
---|
97 | rtems_test_assert(status==0); |
---|
98 | |
---|
99 | status=lstat(symlink_file01,&statbuf); |
---|
100 | rtems_test_assert(status==0); |
---|
101 | rtems_test_assert(S_ISLNK(statbuf.st_mode)); |
---|
102 | rtems_test_assert(len==statbuf.st_size); |
---|
103 | |
---|
104 | |
---|
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)); |
---|
115 | |
---|
116 | puts("rmdir the dir"); |
---|
117 | status=rmdir(file01); |
---|
118 | rtems_test_assert(status==0); |
---|
119 | |
---|
120 | status=lstat(symlink_file01,&statbuf); |
---|
121 | rtems_test_assert(status==0); |
---|
122 | rtems_test_assert(S_ISLNK(statbuf.st_mode)); |
---|
123 | |
---|
124 | status=unlink(symlink_file01); |
---|
125 | rtems_test_assert(status==0); |
---|
126 | |
---|
127 | } |
---|
128 | /* |
---|
129 | * symlink loop error test |
---|
130 | */ |
---|
131 | static void symlink_loop_error_test(void ) |
---|
132 | { |
---|
133 | char* file01="file01"; |
---|
134 | char* file02="file02"; |
---|
135 | |
---|
136 | char* file04="file04"; |
---|
137 | char* path="file01/t"; |
---|
138 | |
---|
139 | int status; |
---|
140 | |
---|
141 | mode_t mode = S_IRWXU | S_IRWXG | S_IRWXO; |
---|
142 | |
---|
143 | puts("symlink loop erro test"); |
---|
144 | |
---|
145 | status=symlink(file01,file02); |
---|
146 | rtems_test_assert(status==0); |
---|
147 | status=symlink(file02,file01); |
---|
148 | rtems_test_assert(status==0); |
---|
149 | |
---|
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); |
---|
158 | } |
---|
159 | |
---|
160 | void test(void ) |
---|
161 | { |
---|
162 | |
---|
163 | symlink_test01(); |
---|
164 | symlink_loop_error_test(); |
---|
165 | |
---|
166 | } |
---|
167 | |
---|