• 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 "filepath_util.h"
20 
readFile(FILE * stream,char * fmt,...)21 int readFile(FILE *stream, char *fmt, ...)
22 {
23     va_list ap;
24     va_start(ap, fmt);
25     int result = vfscanf(stream, fmt, ap);
26     va_end(ap);
27     return result;
28 }
29 
30 /**
31  * @tc.name      : vfscanf_0100
32  * @tc.desc      : Reads data from the stream and stores them according to the parameter format.
33  * @tc.level     : Level 0
34  */
vfscanf_0100(void)35 void vfscanf_0100(void)
36 {
37     FILE *fp = NULL;
38     int val = 0;
39     char buffer[BUFSIZ];
40     char file[PATH_MAX] = {0};
41     FILE_ABSOLUTE_PATH(STR_VFSCANF_TXT, file);
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     char file[PATH_MAX] = {0};
78     FILE_ABSOLUTE_PATH(STR_VFSCANF_TXT, file);
79     fp = fopen(file, "w+");
80     if (fp == NULL) {
81         t_error("%s fopen failed\n", __func__);
82         return;
83     }
84     if (fprintf(fp, "%s %d", "vfscanftest", 123) < 0) {
85         t_error("%s fprintf failed\n", __func__);
86         return;
87     }
88     rewind(fp);
89 
90     int result = readFile(fp, "%s %s", buffer, val);
91     if (result < 0) {
92         t_error("%s vfscanf get result is %d are less 0\n", __func__, result);
93     }
94     if (strcmp(buffer, "vfscanftest") != 0) {
95         t_error("%s vfscanf get is '%s' are not 'vfscanftest'\n", __func__, buffer);
96     }
97     if (strcmp(val, "123") != 0) {
98         t_error("%s vfscanf get is '%s' are not '123'\n", __func__, val);
99     }
100     fclose(fp);
101     remove(file);
102 }
103 
104 /**
105  * @tc.name      : vfscanf_0300
106  * @tc.desc      : The test corresponding position does not match the format
107  * @tc.level     : Level 2
108  */
vfscanf_0300(void)109 void vfscanf_0300(void)
110 {
111     FILE *fp = NULL;
112     int val1 = 0;
113     int val2 = 0;
114     char file[PATH_MAX] = {0};
115     FILE_ABSOLUTE_PATH(STR_VFSCANF_TXT, file);
116     fp = fopen(file, "w+");
117     if (fp == NULL) {
118         t_error("%s fopen failed", __func__);
119         return;
120     }
121     if (fprintf(fp, "%s %d", "vfscanftest", 123) < 0) {
122         t_error("%s fprintf failed", __func__);
123         return;
124     }
125     rewind(fp);
126 
127     int result = readFile(fp, "%d %d", val1, val2);
128     if (result > 0) {
129         t_error("%s vfscanf get result is %d are more than the 0\n", __func__, result);
130     }
131     if (val1 != 0) {
132         t_error("%s vfscanf get is %d are not 0\n", __func__, val1);
133     }
134     if (val2 != 0) {
135         t_error("%s vfscanf get is %d are not 0\n", __func__, val2);
136     }
137     fclose(fp);
138     remove(file);
139 }
140 
main(int argc,char * argv[])141 int main(int argc, char *argv[])
142 {
143     vfscanf_0100();
144     vfscanf_0200();
145     vfscanf_0300();
146     return t_status;
147 }