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 <stdlib.h>
17 #include <sys/stat.h>
18 #include "functionalext.h"
19 #define ST_MODE_1 0600
20 #define ST_MODE_2 0777
21
22 /**
23 * @tc.name : mkstemp_0100
24 * @tc.desc : Verify mkstemp process success. Provide the correct template and no fixed suffix, create a
25 * temporary file
26 * @tc.level : Level 0
27 */
mkstemp_0100(void)28 void mkstemp_0100(void)
29 {
30 char tmpfile[] = "/data/mkstemp_0100_XXXXXX";
31 int fd = mkstemp(tmpfile);
32 EXPECT_TRUE("mkstemp_0100", fd != -1);
33 if (fd != -1) {
34 int cnt = write(fd, tmpfile, strlen(tmpfile));
35 EXPECT_TRUE("mkstemp_0100", cnt == strlen(tmpfile));
36 close(fd);
37 char rmfile[128];
38 int len = sprintf(rmfile, "rm %s", tmpfile);
39 if (len > 0) {
40 system(rmfile);
41 }
42 }
43 }
44
45 /**
46 * @tc.name : mkstemp_0200
47 * @tc.desc : Verify mkstemp process fail. Provide error template, create temp file fail
48 * @tc.level : Level 2
49 */
mkstemp_0200(void)50 void mkstemp_0200(void)
51 {
52 char tmpfile[] = "/data/mkstemp_0200.dat";
53 int fd = mkstemp(tmpfile);
54 EXPECT_TRUE("mkstemp_0200", fd == -1);
55 if (fd != -1) {
56 int cnt = write(fd, tmpfile, strlen(tmpfile));
57 EXPECT_TRUE("mkstemp_0200", cnt == strlen(tmpfile));
58 close(fd);
59 char rmfile[128];
60 int len = sprintf(rmfile, "rm %s", tmpfile);
61 if (len > 0) {
62 system(rmfile);
63 }
64 }
65 }
66
67 /**
68 * @tc.name : mkstemp_0300
69 * @tc.desc : Template with 'XXXXXX' not at the end
70 * @tc.level : Level 2
71 */
mkstemp_0300(void)72 void mkstemp_0300(void)
73 {
74 char tmpfile[] = "/data/mkstemp_0300_XXXXXX_dir";
75 int fd = mkstemp(tmpfile);
76 EXPECT_TRUE("mkstemp_0300", fd == -1);
77 if (fd == -1) {
78 EXPECT_EQ("mkstemp_0300", errno, EINVAL);
79 EXPECT_STREQ("mkstemp_0300", tmpfile, "/data/mkstemp_0300_XXXXXX_dir");
80 }
81 }
82
83 /**
84 * @tc.name : mkstemp_0400
85 * @tc.desc : Verify file permissions (0600)
86 * @tc.level : Level 1
87 */
mkstemp_0400(void)88 void mkstemp_0400(void)
89 {
90 char tmpfile[] = "/data/mkstemp_0400_XXXXXX";
91 int fd = mkstemp(tmpfile);
92 EXPECT_TRUE("mkstemp_0400", fd != -1);
93 if (fd != -1) {
94 struct stat st;
95 fstat(fd, &st);
96 EXPECT_EQ("mkstemp_0400", st.st_mode & ST_MODE_2, ST_MODE_1);
97 close(fd);
98 unlink(tmpfile);
99 }
100 }
101
102 /**
103 * @tc.name : mkstemp_0500
104 * @tc.desc : Verify concurrent mkstemp calls
105 * @tc.level : Level 1
106 */
mkstemp_0500(void)107 void mkstemp_0500(void)
108 {
109 char tmpfile1[] = "/data/mkstemp_0500_XXXXXX";
110 char tmpfile2[] = "/data/mkstemp_0500_XXXXXX";
111 int fd1 = mkstemp(tmpfile1);
112 int fd2 = mkstemp(tmpfile2);
113 EXPECT_TRUE("mkstemp_0500", fd1 != -1 && fd2 != -1);
114 if (fd1 != -1 && fd2 != -1) {
115 EXPECT_STRNE("mkstemp_0500", tmpfile1, tmpfile2);
116 }
117
118 if (fd1 != -1) {
119 close(fd1);
120 unlink(tmpfile1);
121 }
122 if (fd2 != -1) {
123 close(fd2);
124 unlink(tmpfile2);
125 }
126 }
127
main(void)128 int main(void)
129 {
130 mkstemp_0100();
131 mkstemp_0200();
132 mkstemp_0300();
133 mkstemp_0400();
134 mkstemp_0500();
135 return t_status;
136 }