• 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 <errno.h>
17 #include <stdio.h>
18 #include <string.h>
19 #include <unistd.h>
20 
21 #include "test.h"
22 
23 const int nlen = -1;
24 const int vlen = 5;
25 const char *path = "/data/tests/libc-test/src/file.txt";
26 const char *path_n = "/data/tests/libc-test/src/files.txt";
27 const char *content = "Hello world!";
28 
29 #define CONTENT_LENGTH (13)
30 
31 /**
32  * @tc.name      : truncate_0100
33  * @tc.desc      : truncate a file to a specified length
34  * @tc.level     : Level 0
35  */
truncate_0100(void)36 void truncate_0100(void)
37 {
38     FILE *f = fopen(path_n, "a");
39     if (f == NULL) {
40         t_error("%s failed: fopen\n", __func__);
41         return;
42     }
43     fputs(content, f);
44     fclose(f);
45     int result = truncate(path_n, 0);
46     if (result != 0) {
47         t_error("%s failed: result = %d\n", __func__, result);
48         return;
49     }
50     remove(path_n);
51 }
52 
53 /**
54  * @tc.name      : truncate_0200
55  * @tc.desc      : truncate a file to a specified length
56  * @tc.level     : Level 1
57  */
truncate_0200(void)58 void truncate_0200(void)
59 {
60     FILE *f = fopen(path, "a");
61     if (f == NULL) {
62         t_error("%s failed: fopen\n", __func__);
63         return;
64     }
65 
66     fputs(content, f);
67     fclose(f);
68 
69     int result = truncate(path, vlen);
70     if (result != 0) {
71         remove(path);
72 
73         t_error("%s failed: result = %d\n", __func__, result);
74         return;
75     }
76 
77     f = fopen(path, "r");
78     if (f == NULL) {
79         t_error("%s failed: fopen\n", __func__);
80         return;
81     }
82 
83     char buf[CONTENT_LENGTH] = {0};
84     fgets(buf, sizeof(buf) / sizeof(char), f);
85     fclose(f);
86 
87     if (strlen(buf) != vlen) {
88         remove(path);
89 
90         t_error("%s failed: buf = %s\n", __func__, buf);
91         return;
92     }
93 }
94 
95 /**
96  * @tc.name      : truncate_0300
97  * @tc.desc      : truncate a file to a negative length
98  * @tc.level     : Level 2
99  */
truncate_0300(void)100 void truncate_0300(void)
101 {
102     int result = truncate(path, nlen);
103     if (result == 0) {
104         t_error("%s failed: result = %d\n", __func__, result);
105         return;
106     }
107 
108     if (errno != EINVAL) {
109         t_error("%s failed: errno = %d\n", __func__, errno);
110         return;
111     }
112 }
113 
114 /**
115  * @tc.name      : truncate_0400
116  * @tc.desc      : truncate a directory
117  * @tc.level     : Level 2
118  */
truncate_0400(void)119 void truncate_0400(void)
120 {
121     int result = truncate("/", 0);
122     if (result == 0) {
123         t_error("%s failed: result = %d\n", __func__, result);
124         return;
125     }
126 
127     if (errno != EISDIR) {
128         t_error("%s failed: errno = %d\n", __func__, errno);
129         return;
130     }
131 }
132 
main(int argc,char * argv[])133 int main(int argc, char *argv[])
134 {
135     truncate_0100();
136     truncate_0200();
137     truncate_0300();
138     truncate_0400();
139 
140     return t_status;
141 }
142