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 <stdio.h>
18 #include <string.h>
19 #include <unistd.h>
20 #include "filepath_util.h"
21 
22 /**
23  * @tc.name      : write_0100
24  * @tc.desc      : Test the write function, the return value is the specified buf length
25  * @tc.level     : Level 0
26  */
write_0100(void)27 void write_0100(void)
28 {
29     const char *msg = "This is a c test code for write function";
30     char path[PATH_MAX] = {0};
31     FILE_ABSOLUTE_PATH(STR_WRITE_TEST_TXT, path);
32     int len = strlen(msg);
33     char buf[1024] = {0};
34     int fd = open(path, O_RDWR | O_RSYNC | O_CREAT, 0664);
35     if (fd == -1) {
36         t_error("%s write create file error", __func__);
37         return;
38     }
39     int result = write(fd, msg, len);
40     if (result != len) {
41         t_error("%s write get result is %d not want %d", __func__, result, len);
42     }
43     close(fd);
44     fd = open(path, O_RDWR);
45     int bytes = read(fd, buf, len);
46     if (bytes == -1) {
47         t_error("%s read file failed", __func__);
48         return;
49     }
50     if (strcmp(msg, buf)) {
51         t_error("%s wrong string written to file", __func__);
52     }
53     close(fd);
54     remove(path);
55 }
56 
57 /**
58  * @tc.name      : write_0200
59  * @tc.desc      : test the return value of write when the count parameter is 0
60  * @tc.level     : Level 1
61  */
write_0200(void)62 void write_0200(void)
63 {
64     const char *msg = "This is a c test code for write function";
65     char path[PATH_MAX] = {0};
66     FILE_ABSOLUTE_PATH(STR_WRITE_TEST_TXT, path);
67     int len = 0;
68     int fd = open(path, O_RDWR | O_RSYNC | O_CREAT, 0664);
69     if (fd == -1) {
70         t_error("%s write create file error", __func__);
71         return;
72     }
73     int result = write(fd, msg, len);
74     if (result != len) {
75         t_error("%s write get result is %d not want %d", __func__, result, len);
76     }
77     close(fd);
78     remove(path);
79 }
80 
81 /**
82  * @tc.name      : write_0300
83  * @tc.desc      : test the return result of write when count is greater than the length of buf
84  * @tc.level     : Level 1
85  */
write_0300(void)86 void write_0300(void)
87 {
88     const char *msg = "This is a c test code for write function";
89     char path[PATH_MAX] = {0};
90     FILE_ABSOLUTE_PATH(STR_WRITE_TEST_TXT, path);
91     int len = strlen(msg) + 1;
92     int fd = open(path, O_RDWR | O_RSYNC | O_CREAT, 0664);
93     if (fd == -1) {
94         t_error("%s write create file error", __func__);
95         return;
96     }
97     int result = write(fd, msg, len);
98     if (result == -1) {
99         t_error("%s write get result is %d not want -1", __func__, result);
100     }
101     close(fd);
102     remove(path);
103 }
104 
105 /**
106  * @tc.name      : write_0400
107  * @tc.desc      : test the return value of write when fd is abnormal
108  * @tc.level     : Level 2
109  */
write_0400(void)110 void write_0400(void)
111 {
112     const char *msg = "This is a c test code for write function";
113     char path[PATH_MAX] = {0};
114     FILE_ABSOLUTE_PATH(STR_WRITE_TEST_TXT, path);
115     int len = strlen(msg);
116     int fd = open(path, O_RDWR);
117     int result = write(fd, msg, len);
118     if (result != -1) {
119         t_error("%s write get result is %d not want -1", __func__, result);
120     }
121     close(fd);
122     remove(path);
123 }
124 
125 /**
126  * @tc.name      : write_0500
127  * @tc.desc      : test the return value of write when buf is NULL
128  * @tc.level     : Level 2
129  */
write_0500(void)130 void write_0500(void)
131 {
132     const char *msg = NULL;
133     char path[PATH_MAX] = {0};
134     FILE_ABSOLUTE_PATH(STR_WRITE_TEST_TXT, path);
135     int len = 1;
136     int fd = open(path, O_RDWR | O_RSYNC | O_CREAT, 0664);
137     if (fd == -1) {
138         t_error("%s write create file error", __func__);
139         return;
140     }
141     int result = write(fd, msg, len);
142     if (result != -1) {
143         t_error("%s write get result is %d not want -1", __func__, result);
144     }
145     close(fd);
146     remove(path);
147 }
148 
main(int argc, char *argv[])149 int main(int argc, char *argv[])
150 {
151     write_0100();
152     write_0200();
153     write_0300();
154     write_0400();
155     write_0500();
156     return t_status;
157 }