1570af302Sopenharmony_ci/**
2570af302Sopenharmony_ci * Copyright (c) 2022 Huawei Device Co., Ltd.
3570af302Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4570af302Sopenharmony_ci * you may not use this file except in compliance with the License.
5570af302Sopenharmony_ci * You may obtain a copy of the License at
6570af302Sopenharmony_ci *
7570af302Sopenharmony_ci *   http://www.apache.org/licenses/LICENSE-2.0
8570af302Sopenharmony_ci *
9570af302Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10570af302Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11570af302Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12570af302Sopenharmony_ci * See the License for the specific language governing permissions and
13570af302Sopenharmony_ci * limitations under the License.
14570af302Sopenharmony_ci */
15570af302Sopenharmony_ci
16570af302Sopenharmony_ci#include <fcntl.h>
17570af302Sopenharmony_ci#include "functionalext.h"
18570af302Sopenharmony_ci
19570af302Sopenharmony_citypedef void (*TEST_FUN)();
20570af302Sopenharmony_ci
21570af302Sopenharmony_ciconst char *path = "/data/test.txt";
22570af302Sopenharmony_ci
23570af302Sopenharmony_ci/*
24570af302Sopenharmony_ci * @tc.name      : open_0100
25570af302Sopenharmony_ci * @tc.desc      : Verify file open mode is O_RDONLY
26570af302Sopenharmony_ci * @tc.level     : Level 0
27570af302Sopenharmony_ci */
28570af302Sopenharmony_civoid open_0100(void)
29570af302Sopenharmony_ci{
30570af302Sopenharmony_ci    char s[] = "test";
31570af302Sopenharmony_ci    char buffer[80] = {0};
32570af302Sopenharmony_ci    int fd = open(path, O_RDONLY + O_CREAT, TEST_MODE);
33570af302Sopenharmony_ci    int len = write(fd, s, sizeof(s));
34570af302Sopenharmony_ci    ssize_t size = read(fd, buffer, sizeof(buffer));
35570af302Sopenharmony_ci    EXPECT_MT("open_0100", fd, 2);
36570af302Sopenharmony_ci    EXPECT_EQ("open_0100", len, -1);
37570af302Sopenharmony_ci    EXPECT_STREQ("open_0100", buffer, "");
38570af302Sopenharmony_ci
39570af302Sopenharmony_ci    close(fd);
40570af302Sopenharmony_ci    remove(path);
41570af302Sopenharmony_ci}
42570af302Sopenharmony_ci
43570af302Sopenharmony_ci/*
44570af302Sopenharmony_ci * @tc.name      : open_0200
45570af302Sopenharmony_ci * @tc.desc      : Verify file open mode is O_RDONLY (file does not exist)
46570af302Sopenharmony_ci * @tc.level     : Level 2
47570af302Sopenharmony_ci */
48570af302Sopenharmony_civoid open_0200(void)
49570af302Sopenharmony_ci{
50570af302Sopenharmony_ci    int fd = open(path, O_RDONLY);
51570af302Sopenharmony_ci    EXPECT_EQ("open_0200", fd, -1);
52570af302Sopenharmony_ci}
53570af302Sopenharmony_ci
54570af302Sopenharmony_ci/*
55570af302Sopenharmony_ci * @tc.name      : open_0300
56570af302Sopenharmony_ci * @tc.desc      : Verify file open mode is O_WRONLY
57570af302Sopenharmony_ci * @tc.level     : Level 0
58570af302Sopenharmony_ci */
59570af302Sopenharmony_civoid open_0300(void)
60570af302Sopenharmony_ci{
61570af302Sopenharmony_ci    char s[] = "test";
62570af302Sopenharmony_ci    char buffer[80] = {0};
63570af302Sopenharmony_ci    int fd = open(path, O_WRONLY + O_CREAT, TEST_MODE);
64570af302Sopenharmony_ci    ssize_t size = read(fd, buffer, sizeof(buffer));
65570af302Sopenharmony_ci    EXPECT_MT("open_0300", fd, 2);
66570af302Sopenharmony_ci    EXPECT_STREQ("open_0300", buffer, "");
67570af302Sopenharmony_ci    int len = write(fd, s, sizeof(s));
68570af302Sopenharmony_ci    EXPECT_EQ("open_0300", len, 5);
69570af302Sopenharmony_ci    close(fd);
70570af302Sopenharmony_ci    remove(path);
71570af302Sopenharmony_ci}
72570af302Sopenharmony_ci
73570af302Sopenharmony_ci/*
74570af302Sopenharmony_ci * @tc.name      : open_0400
75570af302Sopenharmony_ci * @tc.desc      : Verify file open mode is O_WRONLY(file does not exist)
76570af302Sopenharmony_ci * @tc.level     : Level 2
77570af302Sopenharmony_ci */
78570af302Sopenharmony_civoid open_0400(void)
79570af302Sopenharmony_ci{
80570af302Sopenharmony_ci    int fd = open(path, O_WRONLY);
81570af302Sopenharmony_ci    EXPECT_EQ("open_0400", fd, -1);
82570af302Sopenharmony_ci}
83570af302Sopenharmony_ci
84570af302Sopenharmony_ci/*
85570af302Sopenharmony_ci * @tc.name      : open_0500
86570af302Sopenharmony_ci * @tc.desc      : Verify file open mode is O_RDWR | O_APPEND
87570af302Sopenharmony_ci * @tc.level     : Level 0
88570af302Sopenharmony_ci */
89570af302Sopenharmony_civoid open_0500(void)
90570af302Sopenharmony_ci{
91570af302Sopenharmony_ci    char s[] = "test";
92570af302Sopenharmony_ci    char buffer[80] = {0};
93570af302Sopenharmony_ci    int fd = open(path, O_RDWR | O_APPEND + O_CREAT, TEST_MODE);
94570af302Sopenharmony_ci    ssize_t size = read(fd, buffer, sizeof(buffer));
95570af302Sopenharmony_ci    EXPECT_MT("open_0500", fd, 2);
96570af302Sopenharmony_ci    EXPECT_STREQ("open_0500", buffer, "");
97570af302Sopenharmony_ci    int len = write(fd, s, sizeof(s));
98570af302Sopenharmony_ci    EXPECT_EQ("open_0500", len, 5);
99570af302Sopenharmony_ci    close(fd);
100570af302Sopenharmony_ci    remove(path);
101570af302Sopenharmony_ci}
102570af302Sopenharmony_ci
103570af302Sopenharmony_ci/*
104570af302Sopenharmony_ci * @tc.name      : open_0600
105570af302Sopenharmony_ci * @tc.desc      : Verify file open mode is O_RDWR | O_APPEND(file does not exist)
106570af302Sopenharmony_ci * @tc.level     : Level 1
107570af302Sopenharmony_ci */
108570af302Sopenharmony_civoid open_0600(void)
109570af302Sopenharmony_ci{
110570af302Sopenharmony_ci    char s[] = "test";
111570af302Sopenharmony_ci    char buffer[80] = {0};
112570af302Sopenharmony_ci    int fd = open(path, O_RDWR | O_APPEND);
113570af302Sopenharmony_ci    ssize_t size = read(fd, buffer, sizeof(buffer));
114570af302Sopenharmony_ci    EXPECT_EQ("open_0600", fd, -1);
115570af302Sopenharmony_ci    EXPECT_STREQ("open_0600", buffer, "");
116570af302Sopenharmony_ci    int len = write(fd, s, sizeof(s));
117570af302Sopenharmony_ci    EXPECT_EQ("open_0600", len, -1);
118570af302Sopenharmony_ci    close(fd);
119570af302Sopenharmony_ci}
120570af302Sopenharmony_ci
121570af302Sopenharmony_ci/*
122570af302Sopenharmony_ci * @tc.name      : open_0700
123570af302Sopenharmony_ci * @tc.desc      : Verify file open mode is O_RDWR | O_APPEND(file pathname is invalid)
124570af302Sopenharmony_ci * @tc.level     : Level 2
125570af302Sopenharmony_ci */
126570af302Sopenharmony_civoid open_0700(void)
127570af302Sopenharmony_ci{
128570af302Sopenharmony_ci    int fd = open(path, O_RDWR | O_APPEND);
129570af302Sopenharmony_ci    EXPECT_EQ("open_0700", fd, -1);
130570af302Sopenharmony_ci}
131570af302Sopenharmony_ci
132570af302Sopenharmony_ci/*
133570af302Sopenharmony_ci * @tc.name      : open_0800
134570af302Sopenharmony_ci * @tc.desc      : Verify file open mode is O_RDWR | O_CREAT
135570af302Sopenharmony_ci * @tc.level     : Level 0
136570af302Sopenharmony_ci */
137570af302Sopenharmony_civoid open_0800(void)
138570af302Sopenharmony_ci{
139570af302Sopenharmony_ci    char s[] = "test";
140570af302Sopenharmony_ci    char buffer[80] = {0};
141570af302Sopenharmony_ci    int fd = open(path, O_RDWR | O_CREAT, TEST_MODE);
142570af302Sopenharmony_ci    ssize_t size = read(fd, buffer, sizeof(buffer));
143570af302Sopenharmony_ci    EXPECT_MT("open_0800", fd, 2);
144570af302Sopenharmony_ci    EXPECT_STREQ("open_0800", buffer, "");
145570af302Sopenharmony_ci    int len = write(fd, s, sizeof(s));
146570af302Sopenharmony_ci    EXPECT_EQ("open_0800", len, 5);
147570af302Sopenharmony_ci    close(fd);
148570af302Sopenharmony_ci    remove(path);
149570af302Sopenharmony_ci}
150570af302Sopenharmony_ci
151570af302Sopenharmony_ci/*
152570af302Sopenharmony_ci * @tc.name      : open_0900
153570af302Sopenharmony_ci * @tc.desc      : Verify file open mode is O_RDWR | O_CLOEXEC
154570af302Sopenharmony_ci * @tc.level     : Level 0
155570af302Sopenharmony_ci */
156570af302Sopenharmony_civoid open_0900(void)
157570af302Sopenharmony_ci{
158570af302Sopenharmony_ci    char s[] = "test";
159570af302Sopenharmony_ci    char buffer[80] = {0};
160570af302Sopenharmony_ci    int fd = open(path, O_RDWR | O_CLOEXEC + O_CREAT, TEST_MODE);
161570af302Sopenharmony_ci    ssize_t size = read(fd, buffer, sizeof(buffer));
162570af302Sopenharmony_ci    EXPECT_MT("open_0900", fd, 2);
163570af302Sopenharmony_ci    EXPECT_STREQ("open_0900", buffer, "");
164570af302Sopenharmony_ci    int len = write(fd, s, sizeof(s));
165570af302Sopenharmony_ci    EXPECT_EQ("open_0900", len, 5);
166570af302Sopenharmony_ci    close(fd);
167570af302Sopenharmony_ci    remove(path);
168570af302Sopenharmony_ci}
169570af302Sopenharmony_ci
170570af302Sopenharmony_ciTEST_FUN G_Fun_Array[] = {
171570af302Sopenharmony_ci    open_0100,
172570af302Sopenharmony_ci    open_0200,
173570af302Sopenharmony_ci    open_0300,
174570af302Sopenharmony_ci    open_0400,
175570af302Sopenharmony_ci    open_0500,
176570af302Sopenharmony_ci    open_0600,
177570af302Sopenharmony_ci    open_0700,
178570af302Sopenharmony_ci    open_0800,
179570af302Sopenharmony_ci    open_0900,
180570af302Sopenharmony_ci
181570af302Sopenharmony_ci};
182570af302Sopenharmony_ci
183570af302Sopenharmony_ciint main(int argc, char *argv[])
184570af302Sopenharmony_ci{
185570af302Sopenharmony_ci    int num = sizeof(G_Fun_Array) / sizeof(TEST_FUN);
186570af302Sopenharmony_ci    for (int pos = 0; pos < num; ++pos) {
187570af302Sopenharmony_ci        G_Fun_Array[pos]();
188570af302Sopenharmony_ci    }
189570af302Sopenharmony_ci
190570af302Sopenharmony_ci    return t_status;
191570af302Sopenharmony_ci}
192