• 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 "functionalext.h"
18 
19 typedef void (*TEST_FUN)();
20 
21 const char *path = "/data/test.txt";
22 
23 /*
24  * @tc.name      : open_0100
25  * @tc.desc      : Verify file open mode is O_RDONLY
26  * @tc.level     : Level 0
27  */
open_0100(void)28 void open_0100(void)
29 {
30     char s[] = "test";
31     char buffer[80] = {0};
32     int fd = open(path, O_RDONLY + O_CREAT);
33     int len = write(fd, s, sizeof(s));
34     ssize_t size = read(fd, buffer, sizeof(buffer));
35     EXPECT_EQ("open_0100", fd, 4);
36     EXPECT_EQ("open_0100", len, -1);
37     EXPECT_STREQ("open_0100", buffer, "");
38 
39     close(fd);
40     remove(path);
41 }
42 
43 /*
44  * @tc.name      : open_0200
45  * @tc.desc      : Verify file open mode is O_RDONLY (file does not exist)
46  * @tc.level     : Level 2
47  */
open_0200(void)48 void open_0200(void)
49 {
50     int fd = open(path, O_RDONLY);
51     EXPECT_EQ("open_0200", fd, -1);
52 }
53 
54 /*
55  * @tc.name      : open_0300
56  * @tc.desc      : Verify file open mode is O_WRONLY
57  * @tc.level     : Level 0
58  */
open_0300(void)59 void open_0300(void)
60 {
61     char s[] = "test";
62     char buffer[80] = {0};
63     int fd = open(path, O_WRONLY + O_CREAT);
64     ssize_t size = read(fd, buffer, sizeof(buffer));
65     EXPECT_EQ("open_0300", fd, 4);
66     EXPECT_STREQ("open_0300", buffer, "");
67     int len = write(fd, s, sizeof(s));
68     EXPECT_EQ("open_0300", len, 5);
69     close(fd);
70     remove(path);
71 }
72 
73 /*
74  * @tc.name      : open_0400
75  * @tc.desc      : Verify file open mode is O_WRONLY(file does not exist)
76  * @tc.level     : Level 2
77  */
open_0400(void)78 void open_0400(void)
79 {
80     int fd = open(path, O_WRONLY);
81     EXPECT_EQ("open_0400", fd, -1);
82 }
83 
84 /*
85  * @tc.name      : open_0500
86  * @tc.desc      : Verify file open mode is O_RDWR | O_APPEND
87  * @tc.level     : Level 0
88  */
open_0500(void)89 void open_0500(void)
90 {
91     char s[] = "test";
92     char buffer[80] = {0};
93     int fd = open(path, O_RDWR | O_APPEND + O_CREAT);
94     ssize_t size = read(fd, buffer, sizeof(buffer));
95     EXPECT_EQ("open_0500", fd, 4);
96     EXPECT_STREQ("open_0500", buffer, "");
97     int len = write(fd, s, sizeof(s));
98     EXPECT_EQ("open_0500", len, 5);
99     close(fd);
100     remove(path);
101 }
102 
103 /*
104  * @tc.name      : open_0600
105  * @tc.desc      : Verify file open mode is O_RDWR | O_APPEND(file does not exist)
106  * @tc.level     : Level 1
107  */
open_0600(void)108 void open_0600(void)
109 {
110     char s[] = "test";
111     char buffer[80] = {0};
112     int fd = open(path, O_RDWR | O_APPEND);
113     ssize_t size = read(fd, buffer, sizeof(buffer));
114     EXPECT_EQ("open_0600", fd, -1);
115     EXPECT_STREQ("open_0600", buffer, "");
116     int len = write(fd, s, sizeof(s));
117     EXPECT_EQ("open_0600", len, -1);
118     close(fd);
119 }
120 
121 /*
122  * @tc.name      : open_0700
123  * @tc.desc      : Verify file open mode is O_RDWR | O_APPEND(file pathname is invalid)
124  * @tc.level     : Level 2
125  */
open_0700(void)126 void open_0700(void)
127 {
128     int fd = open(path, O_RDWR | O_APPEND);
129     EXPECT_EQ("open_0700", fd, -1);
130 }
131 
132 /*
133  * @tc.name      : open_0800
134  * @tc.desc      : Verify file open mode is O_RDWR | O_CREAT
135  * @tc.level     : Level 0
136  */
open_0800(void)137 void open_0800(void)
138 {
139     char s[] = "test";
140     char buffer[80] = {0};
141     int fd = open(path, O_RDWR | O_CREAT);
142     ssize_t size = read(fd, buffer, sizeof(buffer));
143     EXPECT_EQ("open_0800", fd, 4);
144     EXPECT_STREQ("open_0800", buffer, "");
145     int len = write(fd, s, sizeof(s));
146     EXPECT_EQ("open_0800", len, 5);
147     close(fd);
148     remove(path);
149 }
150 
151 /*
152  * @tc.name      : open_0900
153  * @tc.desc      : Verify file open mode is O_RDWR | O_CLOEXEC
154  * @tc.level     : Level 0
155  */
open_0900(void)156 void open_0900(void)
157 {
158     char s[] = "test";
159     char buffer[80] = {0};
160     int fd = open(path, O_RDWR | O_CLOEXEC + O_CREAT);
161     ssize_t size = read(fd, buffer, sizeof(buffer));
162     EXPECT_EQ("open_0900", fd, 4);
163     EXPECT_STREQ("open_0900", buffer, "");
164     int len = write(fd, s, sizeof(s));
165     EXPECT_EQ("open_0900", len, 5);
166     close(fd);
167     remove(path);
168 }
169 
170 TEST_FUN G_Fun_Array[] = {
171     open_0100,
172     open_0200,
173     open_0300,
174     open_0400,
175     open_0500,
176     open_0600,
177     open_0700,
178     open_0800,
179     open_0900,
180 
181 };
182 
main(int argc,char * argv[])183 int main(int argc, char *argv[])
184 {
185     int num = sizeof(G_Fun_Array) / sizeof(TEST_FUN);
186     for (int pos = 0; pos < num; ++pos) {
187         G_Fun_Array[pos]();
188     }
189 
190     return t_status;
191 }