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