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 <unistd.h>
19 #include "functionalext.h"
20
21 #define TEST_BUFFER_SIZE 64
22 #define TEST_MODE 0777
23
rm_file(const char * name)24 void rm_file(const char *name)
25 {
26 if (access(name, F_OK) == 0) {
27 unlink(name);
28 }
29 }
30
31 /**
32 * @tc.name : pwrite_0100
33 * @tc.desc : Write to a file descriptor at a given offset
34 * @tc.level : Level 0
35 */
pwrite_0100(void)36 void pwrite_0100(void)
37 {
38 const char *txt = "This is pwrite_0100 test.";
39 char buffer[TEST_BUFFER_SIZE];
40 memset(buffer, 0x0, sizeof(buffer));
41
42 int fd = open("pwrite_0100", O_CREAT | O_RDWR, TEST_MODE);
43 EXPECT_NE("pwrite_0100", fd, ERREXPECT);
44 if (fd == -1) {
45 return;
46 }
47 size_t cnt = pwrite(fd, txt, strlen(txt), 0);
48 EXPECT_EQ("pwrite_0100", cnt, strlen(txt));
49
50 lseek(fd, 0, SEEK_SET);
51 cnt = pread(fd, buffer, TEST_BUFFER_SIZE, 0);
52 EXPECT_EQ("pwrite_0100", cnt, strlen(txt));
53 EXPECT_STREQ("pwrite_0100", txt, buffer);
54 close(fd);
55 rm_file("pwrite_0100");
56 }
57
58 /**
59 * @tc.name : pwrite_0200
60 * @tc.desc : Provide illegal parameter data, write data to the open file
61 * @tc.level : Level 2
62 */
pwrite_0200(void)63 void pwrite_0200(void)
64 {
65 const char *txt = "This is pwrite_0200 test.";
66 size_t cnt = pwrite(-1, txt, strlen(txt), 0);
67 EXPECT_EQ("pwrite_0200", cnt, (size_t)(-1));
68
69 int fd = open("pwrite_0200", O_CREAT | O_RDWR, TEST_MODE);
70 EXPECT_NE("pwrite_0200", fd, -1);
71 if (fd == -1) {
72 return;
73 }
74
75 cnt = pwrite(fd, txt, 0, 0);
76 EXPECT_EQ("pwrite_0200", cnt, CMPFLAG);
77
78 cnt = pwrite(fd, NULL, 0, 0);
79 EXPECT_EQ("pwrite_0200", cnt, CMPFLAG);
80 close(fd);
81 rm_file("pwrite_0200");
82 }
83
main(void)84 int main(void)
85 {
86 pwrite_0100();
87 pwrite_0200();
88 return t_status;
89 }
90