• 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 #define MALLOCLENGTH 256
21 /**
22  * @tc.name      : readlink_0100
23  * @tc.desc      : Symlink to path exists and bufsize is large enough to return the number of characters correctly.
24  * @tc.level     : Level 0
25  */
readlink_0100(void)26 void readlink_0100(void)
27 {
28     struct stat sb;
29     char *buf;
30     int ret;
31     char *wstr = "this is a test\n";
32     const char *softptr = "./readlink.txt.soft";
33     buf = malloc(sb.st_size + MALLOCLENGTH);
34     const char *ptr = "readlink.txt";
35     FILE *fptr = fopen(ptr, "w");
36     EXPECT_TRUE("readlink_0100", fptr != NULL);
37     int size = fwrite(wstr, sizeof(char), strlen(wstr), fptr);
38     EXPECT_EQ("readlink_0100", size, strlen(wstr));
39     fclose(fptr);
40     int link = symlink("./readlink.txt", softptr);
41     EXPECT_EQ("readlink_0100", link, 0);
42     ret = readlink(softptr, buf, sizeof(buf));
43     EXPECT_EQ("readlink_0100", ret, sizeof(buf));
44     remove(ptr);
45     remove(softptr);
46     buf = NULL;
47     wstr = NULL;
48     softptr = NULL;
49     fptr = NULL;
50     ptr = NULL;
51 }
52 
53 /**
54  * @tc.name      : readlink_0200
55  * @tc.desc      : Symlink to path exists and the bufsize is not large enough to
56  *                 return the number of characters correctly.
57  * @tc.level     : Level 1
58  */
readlink_0200(void)59 void readlink_0200(void)
60 {
61     struct stat sb;
62     char *buf;
63     int ret;
64     char *wstr = "this is a test\n";
65     const char *softptr = "./readlink.txt.soft";
66     buf = malloc(sb.st_size + MALLOCLENGTH);
67     const char *ptr = "readlink.txt";
68     FILE *fptr = fopen(ptr, "w");
69     EXPECT_TRUE("readlink_0200", fptr != NULL);
70     int size = fwrite(wstr, sizeof(char), strlen(wstr), fptr);
71     EXPECT_EQ("readlink_0200", size, strlen(wstr));
72     fclose(fptr);
73     int link = symlink("./readlink.txt", softptr);
74     EXPECT_EQ("readlink_0200", link, 0);
75     ret = readlink(softptr, buf, sizeof(buf) - 2);
76     EXPECT_EQ("readlink_0200", ret, sizeof(buf) - 2);
77     remove(ptr);
78     remove(softptr);
79     buf = NULL;
80     wstr = NULL;
81     softptr = NULL;
82     fptr = NULL;
83     ptr = NULL;
84 }
85 
86 /**
87  * @tc.name      : readlink_0300
88  * @tc.desc      : Symbolic link that does not exist in path cannot have the correct number of characters.
89  * @tc.level     : Level 2
90  */
readlink_0300(void)91 void readlink_0300(void)
92 {
93     struct stat sb;
94     char *buf;
95     int ret;
96     const char *softptr = "./noreadlink.txt.soft";
97     buf = malloc(sb.st_size + MALLOCLENGTH);
98     ret = readlink(softptr, buf, sizeof(buf));
99     EXPECT_TRUE("readlink_0300", ret < 0);
100     buf = NULL;
101     softptr = NULL;
102 }
103 
main(int argc,char * argv[])104 int main(int argc, char *argv[])
105 {
106     readlink_0100();
107     readlink_0200();
108     readlink_0300();
109 
110     return t_status;
111 }