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 "test.h"
20
21 const char *file = "/data/tests/libc-test/src/functionalext/supplement/stdio/vfscanf.txt";
22
readFile(FILE * stream,char * fmt,...)23 int readFile(FILE *stream, char *fmt, ...)
24 {
25 va_list ap;
26 va_start(ap, fmt);
27 int result = vfscanf(stream, fmt, ap);
28 va_end(ap);
29 return result;
30 }
31
32 /**
33 * @tc.name : vfscanf_0100
34 * @tc.desc : Reads data from the stream and stores them according to the parameter format.
35 * @tc.level : Level 0
36 */
vfscanf_0100(void)37 void vfscanf_0100(void)
38 {
39 FILE *fp = NULL;
40 int val = 0;
41 char buffer[BUFSIZ];
42 fp = fopen(file, "w+");
43 if (fp == NULL) {
44 t_error("%s fopen failed\n", __func__);
45 return;
46 }
47 if (fprintf(fp, "%s %d", "vfscanftest", 123) < 0) {
48 t_error("%s fprintf failed\n", __func__);
49 return;
50 }
51 rewind(fp);
52
53 int result = readFile(fp, "%s %d", buffer, &val);
54 if (result < 0) {
55 t_error("%s vfscanf get result is %d are less 0\n", __func__, result);
56 }
57 if (strcmp(buffer, "vfscanftest") != 0) {
58 t_error("%s vfscanf get is '%s' are not 'vfscanftest'\n", __func__, buffer);
59 }
60 if (val != 123) {
61 t_error("%s vfscanf get is %d are not 123\n", __func__, val);
62 }
63 fclose(fp);
64 remove(file);
65 }
66
67 /**
68 * @tc.name : vfscanf_0200
69 * @tc.desc : Test conversion of different formats
70 * @tc.level : Level 1
71 */
vfscanf_0200(void)72 void vfscanf_0200(void)
73 {
74 FILE *fp = NULL;
75 char val[BUFSIZ];
76 char buffer[BUFSIZ];
77 fp = fopen(file, "w+");
78 if (fp == NULL) {
79 t_error("%s fopen failed\n", __func__);
80 return;
81 }
82 if (fprintf(fp, "%s %d", "vfscanftest", 123) < 0) {
83 t_error("%s fprintf failed\n", __func__);
84 return;
85 }
86 rewind(fp);
87
88 int result = readFile(fp, "%s %s", buffer, val);
89 if (result < 0) {
90 t_error("%s vfscanf get result is %d are less 0\n", __func__, result);
91 }
92 if (strcmp(buffer, "vfscanftest") != 0) {
93 t_error("%s vfscanf get is '%s' are not 'vfscanftest'\n", __func__, buffer);
94 }
95 if (strcmp(val, "123") != 0) {
96 t_error("%s vfscanf get is '%s' are not '123'\n", __func__, val);
97 }
98 fclose(fp);
99 remove(file);
100 }
101
102 /**
103 * @tc.name : vfscanf_0300
104 * @tc.desc : The test corresponding position does not match the format
105 * @tc.level : Level 2
106 */
vfscanf_0300(void)107 void vfscanf_0300(void)
108 {
109 FILE *fp = NULL;
110 int val1 = 0;
111 int val2 = 0;
112 fp = fopen(file, "w+");
113 if (fp == NULL) {
114 t_error("%s fopen failed", __func__);
115 return;
116 }
117 if (fprintf(fp, "%s %d", "vfscanftest", 123) < 0) {
118 t_error("%s fprintf failed", __func__);
119 return;
120 }
121 rewind(fp);
122
123 int result = readFile(fp, "%d %d", val1, val2);
124 if (result > 0) {
125 t_error("%s vfscanf get result is %d are more than the 0\n", __func__, result);
126 }
127 if (val1 != 0) {
128 t_error("%s vfscanf get is %d are not 0\n", __func__, val1);
129 }
130 if (val2 != 0) {
131 t_error("%s vfscanf get is %d are not 0\n", __func__, val2);
132 }
133 fclose(fp);
134 remove(file);
135 }
136
main(int argc,char * argv[])137 int main(int argc, char *argv[])
138 {
139 vfscanf_0100();
140 vfscanf_0200();
141 vfscanf_0300();
142 return t_status;
143 }