• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include <stdlib.h>
17 #include <sys/stat.h>
18 #include "functionalext.h"
19 
20 /**
21  * @tc.name      : readlink_0100
22  * @tc.desc      : Symlink to path exists and bufsize is large enough to return the number of characters correctly.
23  * @tc.level     : Level 0
24  */
readlink_0100(void)25 void readlink_0100(void)
26 {
27     struct stat sb;
28     char *buf;
29     int ret;
30     char *wstr = "this is a test\n";
31     const char *softptr = "./readlink.txt.soft";
32     buf = malloc(sb.st_size + 1);
33     const char *ptr = "readlink.txt";
34     FILE *fptr = fopen(ptr, "w");
35     EXPECT_TRUE("readlink_0100", fptr != NULL);
36     int size = fwrite(wstr, sizeof(char), strlen(wstr), fptr);
37     EXPECT_EQ("readlink_0100", size, strlen(wstr));
38     fclose(fptr);
39     int link = symlink("./readlink.txt", softptr);
40     EXPECT_EQ("readlink_0100", link, 0);
41     ret = readlink(softptr, buf, sizeof(buf));
42     EXPECT_EQ("readlink_0100", ret, sizeof(buf));
43     remove(ptr);
44     remove(softptr);
45     buf = NULL;
46     wstr = NULL;
47     softptr = NULL;
48     fptr = NULL;
49     ptr = NULL;
50 }
51 
52 /**
53  * @tc.name      : readlink_0200
54  * @tc.desc      : Symlink to path exists and the bufsize is not large enough to
55  *                 return the number of characters correctly.
56  * @tc.level     : Level 1
57  */
readlink_0200(void)58 void readlink_0200(void)
59 {
60     struct stat sb;
61     char *buf;
62     int ret;
63     char *wstr = "this is a test\n";
64     const char *softptr = "./readlink.txt.soft";
65     buf = malloc(sb.st_size + 1);
66     const char *ptr = "readlink.txt";
67     FILE *fptr = fopen(ptr, "w");
68     EXPECT_TRUE("readlink_0200", fptr != NULL);
69     int size = fwrite(wstr, sizeof(char), strlen(wstr), fptr);
70     EXPECT_EQ("readlink_0200", size, strlen(wstr));
71     fclose(fptr);
72     int link = symlink("./readlink.txt", softptr);
73     EXPECT_EQ("readlink_0200", link, 0);
74     ret = readlink(softptr, buf, sizeof(buf) - 2);
75     EXPECT_EQ("readlink_0200", ret, sizeof(buf) - 2);
76     remove(ptr);
77     remove(softptr);
78     buf = NULL;
79     wstr = NULL;
80     softptr = NULL;
81     fptr = NULL;
82     ptr = NULL;
83 }
84 
85 /**
86  * @tc.name      : readlink_0300
87  * @tc.desc      : Symbolic link that does not exist in path cannot have the correct number of characters.
88  * @tc.level     : Level 2
89  */
readlink_0300(void)90 void readlink_0300(void)
91 {
92     struct stat sb;
93     char *buf;
94     int ret;
95     const char *softptr = "./noreadlink.txt.soft";
96     buf = malloc(sb.st_size + 1);
97     ret = readlink(softptr, buf, sizeof(buf));
98     EXPECT_TRUE("readlink_0300", ret < 0);
99     buf = NULL;
100     softptr = NULL;
101 }
102 
main(int argc,char * argv[])103 int main(int argc, char *argv[])
104 {
105     readlink_0100();
106     readlink_0200();
107     readlink_0300();
108 
109     return t_status;
110 }