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 /**
20 * @tc.name : dprintf_0100
21 * @tc.desc : Verify the number of characters returned by the function (if the file parameter is stdout)
22 * @tc.level : Level 0
23 */
dprintf_0100(void)24 void dprintf_0100(void)
25 {
26 int result = dprintf(1, "This is a test!!QAZ@WSX12");
27 printf("\n");
28 int ret = strlen("This is a test!!QAZ@WSX12");
29 EXPECT_EQ("dprintf_0100", result, ret);
30 }
31
32 /**
33 * @tc.name : dprintf_0200
34 * @tc.desc : Verify the number of characters returned by the function (if the file argument is stderr)
35 * @tc.level : Level 0
36 */
dprintf_0200(void)37 void dprintf_0200(void)
38 {
39 int result = dprintf(2, "This is a test!");
40 printf("\n");
41 int ret = strlen("This is a test!");
42 EXPECT_EQ("dprintf_0200", result, ret);
43 }
44
45 /**
46 * @tc.name : dprintf_0300
47 * @tc.desc : Verify the number of characters returned by the function
48 (if the file is successfully opened in W mode and its return value is used as a file parameter)
49 * @tc.level : Level 0
50 */
dprintf_0300(void)51 void dprintf_0300(void)
52 {
53 const char *path = "/data/test.txt";
54 int fd = open(path, O_CREAT | O_WRONLY);
55 EXPECT_NE("dprintf_0300", fd, -1);
56
57 int result = dprintf(fd, "This is a test!");
58 int ret = strlen("This is a test!");
59
60 EXPECT_EQ("dprintf_0300", result, ret);
61
62 close(fd);
63 remove(path);
64 }
65
66 /**
67 * @tc.name : dprintf_0400
68 * @tc.desc : Verify the number of characters returned by the function
69 (if the file is successfully opened in R mode and its return value is used as a file parameter)
70 * @tc.level : Level 2
71 */
dprintf_0400(void)72 void dprintf_0400(void)
73 {
74 const char *path = "/data/test.txt";
75 int fd = open(path, O_CREAT | O_RDONLY);
76 EXPECT_NE("dprintf_0400", fd, -1);
77
78 int result = dprintf(fd, "This is a test!");
79 EXPECT_EQ("dprintf_0400", result, -1);
80
81 close(fd);
82 remove(path);
83 }
84
main(int argc,char * argv[])85 int main(int argc, char *argv[])
86 {
87 dprintf_0100();
88 dprintf_0200();
89 dprintf_0300();
90 dprintf_0400();
91 return t_status;
92 }