• 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 <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 
main(void)158 int main(void)
159 {
160     mkostemp_0100();
161     mkostemp_0200();
162     mkostemp_0300();
163     mkostemp_0400();
164     mkostemp_0500();
165     mkostemp_0600();
166     return t_status;
167 }