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 <stdlib.h>
18 #include "functionalext.h"
19
20 /**
21 * @tc.name : mkostemp_0100
22 * @tc.desc : Verify mkostemp process success. Provide the correct template and no fixed suffix,
23 * specified in flags: O_APPEND, create a temporary file.
24 * @tc.level : Level 0
25 */
mkostemp_0100(void)26 void mkostemp_0100(void)
27 {
28 char tmpfile[] = "/data/mkostemp_0100_XXXXXX";
29 int fd = mkostemp(tmpfile, O_APPEND);
30 EXPECT_TRUE("mkostemp_0100", fd != -1);
31 if (fd != -1) {
32 int cnt = write(fd, tmpfile, strlen(tmpfile));
33 EXPECT_TRUE("mkostemp_0100", cnt == strlen(tmpfile));
34 close(fd);
35 char rmfile[128];
36 int len = sprintf(rmfile, "rm %s", tmpfile);
37 if (len > 0) {
38 system(rmfile);
39 }
40 }
41 }
42
43 /**
44 * @tc.name : mkostemp_0200
45 * @tc.desc : Verify mkostemp process success. Provide the correct template and no fixed suffix,
46 * specified in flags: O_CLOEXEC, create a temporary file.
47 * @tc.level : Level 0
48 */
mkostemp_0200(void)49 void mkostemp_0200(void)
50 {
51 char tmpfile[] = "/data/mkostemp_0200_XXXXXX";
52 int fd = mkostemp(tmpfile, O_CLOEXEC);
53 EXPECT_TRUE("mkostemp_0200", fd != -1);
54 if (fd != -1) {
55 int cnt = write(fd, tmpfile, strlen(tmpfile));
56 EXPECT_TRUE("mkostemp_0200", cnt == strlen(tmpfile));
57 close(fd);
58 char rmfile[128];
59 int len = sprintf(rmfile, "rm %s", tmpfile);
60 if (len > 0) {
61 system(rmfile);
62 }
63 }
64 }
65
66 /**
67 * @tc.name : mkostemp_0300
68 * @tc.desc : Verify mkostemp process success. Provide the correct template and no fixed suffix,
69 * specified in flags: O_CLOEXEC, create a temporary file.
70 * @tc.level : Level 0
71 */
mkostemp_0300(void)72 void mkostemp_0300(void)
73 {
74 char tmpfile[] = "/data/mkostemp_0300_XXXXXX";
75 int fd = mkostemp(tmpfile, O_SYNC);
76 EXPECT_TRUE("mkostemp_0300", fd != -1);
77 if (fd != -1) {
78 int cnt = write(fd, tmpfile, strlen(tmpfile));
79 EXPECT_TRUE("mkostemp_0300", cnt == strlen(tmpfile));
80 close(fd);
81 char rmfile[128];
82 int len = sprintf(rmfile, "rm %s", tmpfile);
83 if (len > 0) {
84 system(rmfile);
85 }
86 }
87 }
88
89 /**
90 * @tc.name : mkostemp_0400
91 * @tc.desc : Verify mkostemp process fail. Provide error template, specified in flags: O_APPEND,
92 * create temp file fail.
93 * @tc.level : Level 2
94 */
mkostemp_0400(void)95 void mkostemp_0400(void)
96 {
97 char tmpfile[] = "/tmp/mkostemp_0400.dat";
98 int fd = mkostemp(tmpfile, O_APPEND);
99 EXPECT_TRUE("mkostemp_0400", fd == -1);
100 if (fd != -1) {
101 int cnt = write(fd, tmpfile, strlen(tmpfile));
102 EXPECT_TRUE("mkostemp_0400", cnt == strlen(tmpfile));
103 close(fd);
104 char rmfile[128];
105 int len = sprintf(rmfile, "rm %s", tmpfile);
106 if (len > 0) {
107 system(rmfile);
108 }
109 }
110 }
111
112 /**
113 * @tc.name : mkostemp_0500
114 * @tc.desc : Verify mkostemp process fail. Provide error template, specified in flags: O_CLOEXEC,
115 * create temp file fail.
116 * @tc.level : Level 2
117 */
mkostemp_0500(void)118 void mkostemp_0500(void)
119 {
120 char tmpfile[] = "/tmp/mkostemp_0500.dat";
121 int fd = mkostemp(tmpfile, O_CLOEXEC);
122 EXPECT_TRUE("mkostemp_0500", fd == -1);
123 if (fd != -1) {
124 int cnt = write(fd, tmpfile, strlen(tmpfile));
125 EXPECT_TRUE("mkostemp_0500", cnt == strlen(tmpfile));
126 close(fd);
127 char rmfile[128];
128 int len = sprintf(rmfile, "rm %s", tmpfile);
129 if (len > 0) {
130 system(rmfile);
131 }
132 }
133 }
134
135 /**
136 * @tc.name : mkostemp_0600
137 * @tc.desc : Verify mkostemp process fail. Provide error template, specified in flags: O_SYNC,
138 * create temp file fail.
139 * @tc.level : Level 2
140 */
mkostemp_0600(void)141 void mkostemp_0600(void)
142 {
143 char tmpfile[] = "/tmp/mkostemp_0600.dat";
144 int fd = mkostemp(tmpfile, O_SYNC);
145 EXPECT_TRUE("mkostemp_0600", fd == -1);
146 if (fd != -1) {
147 int cnt = write(fd, tmpfile, strlen(tmpfile));
148 EXPECT_TRUE("mkostemp_0600", cnt == strlen(tmpfile));
149 close(fd);
150 char rmfile[128];
151 int len = sprintf(rmfile, "rm %s", tmpfile);
152 if (len > 0) {
153 system(rmfile);
154 }
155 }
156 }
157
158 /**
159 * @tc.name : mkostemp_0700
160 * @tc.desc : Template with 'XXXXXX' not at the end
161 * @tc.level : Level 2
162 */
mkostemp_0700(void)163 void mkostemp_0700(void)
164 {
165 char temp_dir[] = "/data/mkostemp_0700_XXXXXX_dir";
166 int fd = mkostemp(temp_dir, O_SYNC);
167 //XXXXXX is not at the end, mkdtemp() will fail and return -1. Now template is unchanged.
168 EXPECT_TRUE("mkostemp_0700", fd == -1);
169 if (fd == -1) {
170 EXPECT_EQ("mkostemp_0700", errno, EINVAL);
171 EXPECT_STREQ("mkostemp_0700", temp_dir, "/data/mkostemp_0700_XXXXXX_dir");
172 }
173 }
174
175 /**
176 * @tc.name : mkostemp_0800
177 * @tc.desc : Verify mkostemp generates unique filenames on multiple calls.
178 * @tc.level : Level 0
179 */
mkostemp_0800(void)180 void mkostemp_0800(void)
181 {
182 char tmpfile1[] = "/data/mkostemp_0800_XXXXXX";
183 char tmpfile2[] = "/data/mkostemp_0800_XXXXXX";
184 int fd1 = mkostemp(tmpfile1, O_CLOEXEC);
185 int fd2 = mkostemp(tmpfile2, O_CLOEXEC);
186 EXPECT_TRUE("mkostemp_0800", fd1 != -1 && fd2 != -1);
187 EXPECT_STRNE("mkostemp_0800", tmpfile1, tmpfile2); // The two results should be different
188 if (fd1 != -1) {
189 close(fd1);
190 remove(tmpfile1); // clean
191 }
192 if (fd2 != -1) {
193 close(fd2);
194 remove(tmpfile2); // clean
195 }
196 }
197
main(void)198 int main(void)
199 {
200 mkostemp_0100();
201 mkostemp_0200();
202 mkostemp_0300();
203 mkostemp_0400();
204 mkostemp_0500();
205 mkostemp_0600();
206 mkostemp_0700();
207 mkostemp_0800();
208 return t_status;
209 }