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 <fcntl.h>
17 #include <sys/stat.h>
18 #include <stdbool.h>
19 #include <stdlib.h>
20 #include "functionalext.h"
21
22 extern int __lstat_time64(const char *__restrict, struct stat *__restrict);
23
24 typedef void (*TEST_FUN)();
25
26 /*
27 * @tc.name : lstat_0100
28 * @tc.desc : Verify that the parameters are valid, get the file status successfully
29 * @tc.level : Level 0
30 */
lstat_0100(void)31 void lstat_0100(void)
32 {
33 const char *ptr = "/data/tests/libc-test/src/functionalext/supplement/stattest.txt";
34 const char str[] = "this is a sample!";
35 FILE *fptr = fopen(ptr, "w+");
36 struct stat statbuff;
37 fwrite(str, sizeof(char), strlen(str), fptr);
38 fseek(fptr, 0L, SEEK_SET);
39 int32_t back = lstat(ptr, &statbuff);
40 EXPECT_EQ("lstat_0100", back, 0);
41 EXPECT_EQ("lstat_0100", statbuff.st_size, 17);
42 fclose(fptr);
43 remove(ptr);
44 }
45
46 /*
47 * @tc.name : lstat_time64_0100
48 * @tc.desc : Verify that the parameters are valid, get the file status successfully
49 * @tc.level : Level 0
50 */
lstat_time64_0100(void)51 void lstat_time64_0100(void)
52 {
53 const char *ptr = "/data/tests/libc-test/src/functionalext/supplement/stattest.txt";
54 const char str[] = "this is a sample!";
55 FILE *fptr = fopen(ptr, "w+");
56 struct stat statbuff;
57 fwrite(str, sizeof(char), strlen(str), fptr);
58 fseek(fptr, 0L, SEEK_SET);
59 int32_t back = __lstat_time64(ptr, &statbuff);
60 EXPECT_EQ("lstat_0100", back, 0);
61 EXPECT_EQ("lstat_0100", statbuff.st_size, 17);
62 fclose(fptr);
63 remove(ptr);
64 }
65
66 /*
67 * @tc.name : lstat_0200
68 * @tc.desc : Validation failed to get file status (parameter invalid file does not exist)
69 * @tc.level : Level 2
70 */
lstat_0200(void)71 void lstat_0200(void)
72 {
73 struct stat statbuff;
74 int32_t back = lstat("test.txt", &statbuff);
75 EXPECT_EQ("lstat_0200", back, -1);
76 }
77
78 /*
79 * @tc.name : lstat_0300
80 * @tc.desc : Verify that the file pathname is valid and the file is a linked file
81 * @tc.level : Level 1
82 */
lstat_0300(void)83 void lstat_0300(void)
84 {
85 struct stat buf[3];
86 const char *ptr = "/data/tests/libc-test/src/functionalext/supplement/stat/tests.txt";
87 const char *ptrlink = "/data/tests/libc-test/src/functionalext/supplement/stat/tests.txt.soft";
88
89 FILE *fptr = fopen(ptr, "w+");
90 system("ln -s /data/tests/libc-test/src/functionalext/supplement/stat/tests.txt "
91 "/data/tests/libc-test/src/functionalext/supplement/stat/tests.txt.soft");
92 struct stat statbuff;
93 int32_t back = lstat(ptrlink, &statbuff);
94 EXPECT_EQ("lstat_0300", back, 0);
95 bool successflag = false;
96 if (S_ISLNK(statbuff.st_mode)) {
97 successflag = true;
98 } else {
99 successflag = false;
100 }
101 EXPECT_TRUE("lstat_0300", successflag);
102 fclose(fptr);
103 remove(ptr);
104 remove(ptrlink);
105 }
106
107 TEST_FUN G_Fun_Array[] = {
108 lstat_0100,
109 lstat_0200,
110 lstat_0300,
111 lstat_time64_0100,
112 };
113
main(int argc,char * argv[])114 int main(int argc, char *argv[])
115 {
116 int num = sizeof(G_Fun_Array) / sizeof(TEST_FUN);
117 for (int pos = 0; pos < num; ++pos) {
118 G_Fun_Array[pos]();
119 }
120
121 return t_status;
122 }