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 "functionalext.h"
18
19 #define SUFFIX_LEN 5
20
21 /**
22 * @tc.name : mkstemps_0100
23 * @tc.desc : Provide the correct template, create a temporary file
24 * @tc.level : Level 0
25 */
mkstemps_0100(void)26 void mkstemps_0100(void)
27 {
28 char tmpfile[] = "/data/local/tmp/mkstemps_0100_XXXXXX.dat";
29 int fd = mkstemps(tmpfile, strlen(".dat"));
30 EXPECT_TRUE("mkstemps_0100", fd != -1);
31 if (fd != -1) {
32 int cnt = write(fd, tmpfile, strlen(tmpfile));
33 EXPECT_TRUE("mkstemps_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 : mkstemps_0200
45 * @tc.desc : Provide the correct template and no fixed suffix, create a temporary file
46 * @tc.level : Level 0
47 */
mkstemps_0200(void)48 void mkstemps_0200(void)
49 {
50 char tmpfile[] = "/data/local/tmp/mkstemps_0200_XXXXXX";
51 int fd = mkstemps(tmpfile, 0);
52 EXPECT_TRUE("mkstemps_0200", fd != -1);
53 if (fd != -1) {
54 int cnt = write(fd, tmpfile, strlen(tmpfile));
55 EXPECT_TRUE("mkstemps_0200", cnt == strlen(tmpfile));
56 close(fd);
57 char rmfile[128];
58 int len = sprintf(rmfile, "rm %s", tmpfile);
59 if (len > 0) {
60 system(rmfile);
61 }
62 }
63 }
64
65 /**
66 * @tc.name : mkstemps_0300
67 * @tc.desc : Provide error template, create temp file
68 * @tc.level : Level 2
69 */
mkstemps_0300(void)70 void mkstemps_0300(void)
71 {
72 char tmpfile[] = "/data/local/tmp/mkstemps_0300.dat";
73 int fd = mkstemps(tmpfile, strlen(".dat"));
74 EXPECT_TRUE("mkstemps_0300", fd == -1);
75 if (fd != -1) {
76 int cnt = write(fd, tmpfile, strlen(tmpfile));
77 EXPECT_TRUE("mkstemps_0300", cnt == strlen(tmpfile));
78 close(fd);
79 char rmfile[128];
80 int len = sprintf(rmfile, "rm %s", tmpfile);
81 if (len > 0) {
82 system(rmfile);
83 }
84 }
85 }
86
87 /**
88 * @tc.name : mkstemps_0400
89 * @tc.desc : Template with 'XXXXXX' not at the end
90 * @tc.level : Level 2
91 */
mkstemps_0400(void)92 void mkstemps_0400(void)
93 {
94 char tmpfile[] = "/data/mkstemps_0400_XXXXXX_dir";
95 int fd = mkstemps(tmpfile, 0);
96 EXPECT_TRUE("mkstemps_0400", fd == -1);
97 if (fd == -1) {
98 EXPECT_EQ("mkstemps_0400", errno, EINVAL);
99 EXPECT_STREQ("mkstemps_0400", tmpfile, "/data/mkstemps_0400_XXXXXX_dir");
100 }
101 }
102
103 /**
104 * @tc.name : mkstemps_0500
105 * @tc.desc : Verify with suffix length larger than actual suffix
106 * @tc.level : Level 2
107 */
mkstemps_0500(void)108 void mkstemps_0500(void)
109 {
110 char tmpfile[] = "/data/local/tmp/mkstemps_0500_XXXXXX.dat";
111 int fd = mkstemps(tmpfile, strlen(".dat") + SUFFIX_LEN); // Exceeding the actual suffix length
112 EXPECT_TRUE("mkstemps_0500", fd == -1);
113 if (fd == -1) {
114 EXPECT_EQ("mkstemps_0500", errno, EINVAL);
115 }
116 }
117
main(void)118 int main(void)
119 {
120 mkstemps_0100();
121 mkstemps_0200();
122 mkstemps_0300();
123 mkstemps_0400();
124 mkstemps_0500();
125 return t_status;
126 }