• 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      : 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[] = "/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[] = "/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[] = "/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 
main(void)88 int main(void)
89 {
90     mkostemps_0100();
91     mkostemps_0200();
92     mkostemps_0300();
93     return t_status;
94 }