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