source: rtems/testsuites/fstests/fssymlink/test.c

Last change on this file was e2ffe95, checked in by Joel Sherrill <joel@…>, on 04/01/22 at 19:32:30

testsuites/fstests/*: Change license to BSD-2

Updates #3053.

  • Property mode set to 100644
File size: 5.0 KB
Line 
1/* SPDX-License-Identifier: BSD-2-Clause */
2
3/*
4 *  COPYRIGHT (c) 1989-2011.
5 *  On-Line Applications Research Corporation (OAR).
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 *    notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 *    notice, this list of conditions and the following disclaimer in the
14 *    documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
28
29#ifdef HAVE_CONFIG_H
30#include "config.h"
31#endif
32
33#include <sys/stat.h>
34#include <fcntl.h>
35#include <errno.h>
36#include <stdio.h>
37#include <stdint.h>
38#include <unistd.h>
39#include <stdlib.h>
40#include <string.h>
41#include <unistd.h>
42#include <dirent.h>
43
44#include "fstest.h"
45#include "fs_config.h"
46#include <tmacros.h>
47
48const char rtems_test_name[] = "FSSYMLINK " FILESYSTEM;
49
50/*
51 * Test the function of symlink
52 */
53
54static void symlink_test01(void )
55{
56  int   fd;
57  char* file01="file";
58  char* symlink_file01="symlink";
59  char name[20];
60  int   status;
61  struct stat statbuf;
62  size_t   len=strlen(file01);
63  size_t   name_len;
64
65
66  printf("Create a file named %s\n",file01);
67  fd=creat(file01,0777);
68  status=close(fd);
69  rtems_test_assert(status==0);
70
71  printf("Create a symlink named %s to %s\n",symlink_file01,file01);
72  status=symlink(file01,symlink_file01);
73  rtems_test_assert(status==0);
74
75  status=stat(file01,&statbuf);
76  rtems_test_assert(status==0);
77  rtems_test_assert(S_ISREG(statbuf.st_mode));
78  rtems_test_assert(0==statbuf.st_size);
79
80  status=lstat(symlink_file01,&statbuf);
81  rtems_test_assert(status==0);
82  rtems_test_assert(S_ISLNK(statbuf.st_mode));
83  rtems_test_assert(len==statbuf.st_size);
84
85
86  puts("call readlink ");
87  name_len=readlink(symlink_file01,name,sizeof(name)-1);
88  rtems_test_assert(name_len!=-1);
89  name[name_len]='\0';
90  rtems_test_assert(!strncmp(name,file01,name_len));
91  puts(name);
92
93  puts("Unlink the file");
94
95  status=unlink(file01);
96  rtems_test_assert(status==0);
97
98  status=lstat(symlink_file01,&statbuf);
99  rtems_test_assert(status==0);
100  rtems_test_assert(S_ISLNK(statbuf.st_mode));
101  rtems_test_assert(len==statbuf.st_size);
102
103  puts("call readlink ");
104  name_len=readlink(symlink_file01,name,sizeof(name)-1);
105  rtems_test_assert(name_len!=-1);
106  name[name_len]='\0';
107  rtems_test_assert(!strncmp(name,file01,name_len));
108  status=unlink(symlink_file01);
109  rtems_test_assert(status==0);
110
111  printf("Create a dir named %s\n",file01);
112  status=mkdir (file01,0777);
113
114  printf("Create a symlink named %s to %s\n",symlink_file01,file01);
115  status=symlink(file01,symlink_file01);
116  rtems_test_assert(status==0);
117
118  status=lstat(symlink_file01,&statbuf);
119  rtems_test_assert(status==0);
120  rtems_test_assert(S_ISLNK(statbuf.st_mode));
121  rtems_test_assert(len==statbuf.st_size);
122
123
124  puts("call readlink ");
125  name_len=readlink(symlink_file01,name,sizeof(name)-1);
126  rtems_test_assert(name_len!=-1);
127  name[name_len]='\0';
128  rtems_test_assert(!strncmp(name,file01,name_len));
129
130  name_len=readlink(symlink_file01,name,3);
131  rtems_test_assert(name_len!=-1);
132  name[name_len]='\0';
133  rtems_test_assert(!strncmp(name,file01,name_len));
134
135  puts("rmdir the dir");
136  status=rmdir(file01);
137  rtems_test_assert(status==0);
138
139  status=lstat(symlink_file01,&statbuf);
140  rtems_test_assert(status==0);
141  rtems_test_assert(S_ISLNK(statbuf.st_mode));
142
143  status=unlink(symlink_file01);
144  rtems_test_assert(status==0);
145
146}
147/*
148 *  symlink loop error test
149 */
150static void symlink_loop_error_test(void )
151{
152  char* file01="file01";
153  char* file02="file02";
154
155  char* file04="file04";
156  char* path="file01/t";
157
158  int   status;
159
160  mode_t mode = S_IRWXU | S_IRWXG | S_IRWXO;
161
162  puts("symlink loop error test");
163
164  status=symlink(file01,file02);
165  rtems_test_assert(status==0);
166  status=symlink(file02,file01);
167  rtems_test_assert(status==0);
168
169
170  EXPECT_ERROR(ELOOP,creat,path,mode);
171  EXPECT_ERROR(ELOOP,open,path,O_CREAT|O_WRONLY,mode);
172  EXPECT_ERROR(ELOOP,truncate,path,0);
173  EXPECT_ERROR(ELOOP,rename,path,file04);
174  EXPECT_ERROR(ELOOP,unlink,path);
175  EXPECT_ERROR(ELOOP,mkdir,path,mode);
176  EXPECT_ERROR(ELOOP,rmdir,path);
177}
178
179void test(void )
180{
181
182  symlink_test01();
183  symlink_loop_error_test();
184
185}
186
Note: See TracBrowser for help on using the repository browser.