• 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 <stdarg.h>
17 #include <stdio.h>
18 #include <string.h>
19 #include <fcntl.h>
20 #include "test.h"
21 
readFile(int stream,char * fmt,...)22 int readFile(int stream, char *fmt, ...)
23 {
24     va_list ap;
25     va_start(ap, fmt);
26     int result = vdprintf(stream, fmt, ap);
27     va_end(ap);
28     return result;
29 }
30 
31 /**
32  * @tc.name      : vdprintf_0100
33  * @tc.desc      : Test the vdprintf method to write to the file
34  * @tc.level     : Level 0
35  */
vdprintf_0100(void)36 void vdprintf_0100(void)
37 {
38     char *value = "asdf";
39     char buffer[100] = {0};
40     int fp = open("/data/temp_vdprintf.txt", O_RDWR | O_CREAT, TEST_MODE);
41     if (fp < 0) {
42         t_error("%s open failed", __func__);
43         return;
44     }
45     int result = readFile(fp, "%s", value);
46     if (result < 0) {
47         t_error("%s vdprintf get result is %d are less 0\n", __func__, result);
48     }
49     close(fp);
50     fp = open("/data/temp_vdprintf.txt", O_RDWR);
51     read(fp, buffer, strlen(value));
52     if (strcmp(buffer, "asdf") != 0) {
53         t_error("%s vdprintf get is '%s' are not 'asdf'\n", __func__, buffer);
54     }
55     close(fp);
56     unlink("/data/temp_vdprintf.txt");
57 }
58 
59 /**
60  * @tc.name      : vdprintf_0200
61  * @tc.desc      : Test that vdprintf returns the result when the file descriptor is incorrect
62  * @tc.level     : Level 2
63  */
vdprintf_0200(void)64 void vdprintf_0200(void)
65 {
66     char *value = "asdf";
67     int want = -1;
68     int fp = -1;
69     int result = readFile(fp, "%s", value);
70     if (result != want) {
71         t_error("%s vdprintf get result is %d are want -1\n", __func__, result);
72     }
73 }
74 
main(int argc,char * argv[])75 int main(int argc, char *argv[])
76 {
77     vdprintf_0100();
78     vdprintf_0200();
79     return t_status;
80 }