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 : mkostemps_0100
22 * @tc.desc : Verify mkostemps process success. Provide the correct template and fixed suffix,
23 * create a temporary file.
24 * @tc.level : Level 0
25 */
mkostemps_0100(void)26 void mkostemps_0100(void)
27 {
28 char tmpfile[] = "/data/local/tmp/mkostemps_0100_XXXXXX.dat";
29 int fd = mkostemps(tmpfile, strlen(".dat"), O_CREAT);
30 EXPECT_TRUE("mkostemps_0100", fd != -1);
31 if (fd != -1) {
32 int cnt = write(fd, tmpfile, strlen(tmpfile));
33 EXPECT_TRUE("mkostemps_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 : mkostemps_0200
45 * @tc.desc : Verify mkostemps process success. Provide the correct template and no fixed suffix,
46 * create a temporary file.
47 * @tc.level : Level 0
48 */
mkostemps_0200(void)49 void mkostemps_0200(void)
50 {
51 char tmpfile[] = "/data/local/tmp/mkostemps_0200_XXXXXX";
52 int fd = mkostemps(tmpfile, 0, O_CREAT);
53 EXPECT_TRUE("mkostemps_0200", fd != -1);
54 if (fd != -1) {
55 int cnt = write(fd, tmpfile, strlen(tmpfile));
56 EXPECT_TRUE("mkostemps_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 : mkostemps_0300
68 * @tc.desc : Verify mkstemp process fail. Because provide error template.
69 * @tc.level : Level 2
70 */
mkostemps_0300(void)71 void mkostemps_0300(void)
72 {
73 char tmpfile[] = "/data/local/tmp/mkostemps_0300.dat";
74 int fd = mkostemps(tmpfile, strlen(".dat"), O_CREAT);
75 EXPECT_TRUE("mkostemps_0300", fd == -1);
76 if (fd != -1) {
77 int cnt = write(fd, tmpfile, strlen(tmpfile));
78 EXPECT_TRUE("mkostemps_0300", cnt == strlen(tmpfile));
79 close(fd);
80 char rmfile[128];
81 int len = sprintf(rmfile, "rm %s", tmpfile);
82 if (len > 0) {
83 system(rmfile);
84 }
85 }
86 }
87
88 /**
89 * @tc.name : mkostemps_0400
90 * @tc.desc : Template with 'XXXXXX' not at the end
91 * @tc.level : Level 2
92 */
mkostemps_0400(void)93 void mkostemps_0400(void)
94 {
95 char tmpfile[] = "/data/local/tmp/mkostemps_0400_XXXXXX_dir";
96 int fd = mkostemps(tmpfile, 0, O_CREAT);
97 EXPECT_TRUE("mkostemps_0400", fd == -1);
98 if (fd == -1) {
99 EXPECT_EQ("mkostemp_0700", errno, EINVAL);
100 EXPECT_STREQ("mkostemp_0700", tmpfile, "/data/local/tmp/mkostemps_0400_XXXXXX_dir");
101 }
102 }
103
104 /**
105 * @tc.name : mkostemps_0500
106 * @tc.desc : Verify mkostemps with large suffix length
107 * @tc.level : Level 2
108 */
mkostemps_0500(void)109 void mkostemps_0500(void)
110 {
111 char tmpfile[] = "/data/local/tmp/mkostemps_0500_XXXXXX.dat";
112 int fd = mkostemps(tmpfile, 100, O_CREAT); // Suffix length larger than actual suffix
113 EXPECT_TRUE("mkostemps_0500", fd == -1);
114 if (fd == -1) {
115 EXPECT_EQ("mkostemps_0500", errno, EINVAL);
116 }
117 }
118
119 /**
120 * @tc.name : mkostemps_0600
121 * @tc.desc : Verify mkostemp generates unique filenames on multiple calls
122 * @tc.level : Level 2
123 */
mkostemps_0600(void)124 void mkostemps_0600(void)
125 {
126 char tmpfile1[] = "/data/local/tmp/mkostemps_0600_XXXXXX";
127 char tmpfile2[] = "/data/local/tmp/mkostemps_0600_XXXXXX";
128 int fd1 = mkostemps(tmpfile1, 0, O_CREAT);
129 int fd2 = mkostemps(tmpfile2, 0, O_CREAT);
130 EXPECT_TRUE("mkostemps_0600", fd1 != -1 && fd2 != -1);
131 if (fd1 != -1 && fd2 != -1) {
132 EXPECT_STRNE("mkstemp_0600", tmpfile1, tmpfile2);
133 }
134 if (fd1 != -1) {
135 close(fd1);
136 unlink(tmpfile1);
137 }
138 if (fd2 != -1) {
139 close(fd2);
140 unlink(tmpfile2);
141 }
142 }
143
main(void)144 int main(void)
145 {
146 mkostemps_0100();
147 mkostemps_0200();
148 mkostemps_0300();
149 mkostemps_0400();
150 mkostemps_0500();
151 mkostemps_0600();
152 return t_status;
153 }