1 /*
2  * Copyright (c) 2021 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 <stdio.h>
17 #include <stdarg.h>
18 
19 #include <err.h>
20 #include <gtest/gtest.h>
21 #include <threads.h>
22 
23 #include "log.h"
24 #include "libfs.h"
25 #include "utils.h"
26 #include "IoTest.h"
27 
28 using namespace testing::ext;
29 
30 /**
31  * @tc.number SUB_KERNEL_IO_STDIO_0100
32  * @tc.name   ungetc basic function test
33  * @tc.desc   [C- SOFTWAret -0200]
34  */
HWTEST_F(IoTest, testUngetc, Function | MediumTest | Level1)35 HWTEST_F(IoTest, testUngetc, Function | MediumTest | Level1)
36 {
37     FILE *fp = nullptr;
38     INIT_TEST_FILE(fp);
39     FOPEN_READ(fp);
40     int ret = getc(fp);
41     EXPECT_EQ(ret, 'h');
42     ret = ungetc(ret, fp);
43     EXPECT_EQ(ret, 'h');
44     char str[50] = {0};
45     char *retS = fgets(str, sizeof(str), fp);
46     EXPECT_STREQ(retS, str);
47     EXPECT_STREQ(str, "hello world");
48     EXPECT_NE(fclose(fp), -1) << "> fclose fail, errno = " << errno;
49 }
50 
51 /**
52  * @tc.number SUB_KERNEL_IO_STDIO_0200
53  * @tc.name   puts basic function test
54  * @tc.desc   [C- SOFTWARE -0200]
55  */
HWTEST_F(IoTest, testPuts, Function | MediumTest | Level1)56 HWTEST_F(IoTest, testPuts, Function | MediumTest | Level1)
57 {
58     int ret = puts("this is string");
59     ASSERT_NE(ret, -1);
60 
61     ret = puts("hello  world!");
62     ASSERT_NE(ret, -1);
63 }
64 
65 /**
66  * @tc.number SUB_KERNEL_IO_STDIO_0300
67  * @tc.name   fmemopen basic function test
68  * @tc.desc   [C- SOFTWARE -0200]
69  */
HWTEST_F(IoTest, testFmemopen, Function | MediumTest | Level1)70 HWTEST_F(IoTest, testFmemopen, Function | MediumTest | Level1)
71 {
72     static char buf[] = "HELLO WORLD";
73     FILE *fp = fmemopen(buf, sizeof(buf), "r");
74     ASSERT_NE(fp, nullptr) << "> fmemopen fail, errno = " << errno;
75     for (unsigned int i = 0; i < sizeof(buf); i++) {
76         char c = fgetc(fp);
77         EXPECT_EQ(c, buf[i]) << "> fmemopen fail, errno = " << errno;
78     }
79     EXPECT_NE(fclose(fp), -1) <<  "fclose fail, errno = " << errno;
80 }
81 
82 /**
83  * @tc.number SUB_KERNEL_IO_STDIO_0400
84  * @tc.name   getw basic function test
85  * @tc.desc   [C- SOFTWARE -0200]
86  */
HWTEST_F(IoTest, testGetw, Function | MediumTest | Level1)87 HWTEST_F(IoTest, testGetw, Function | MediumTest | Level1)
88 {
89     FILE *fp = nullptr;
90     FOPEN_WRITE(fp);
91     int ret = putw('a', fp);
92     ASSERT_EQ(ret, 0);
93     ASSERT_NE(fclose(fp), -1) << "> fclose fail ,errno = " << errno;
94 
95     FOPEN_READ(fp);
96     ret = getw(fp);
97     EXPECT_EQ(ret, 'a');
98     ASSERT_NE(fclose(fp), -1) << "> fclose fail ,errno = " << errno;
99 }
100 
FormatVdprintf(int fd, const char *format, ...)101 int FormatVdprintf(int fd, const char *format, ...)
102 {
103     va_list args;
104     va_start(args, format);
105     int ret = vdprintf(fd, format, args);
106     va_end(args);
107     return ret;
108 }
109 
110 /**
111  * @tc.number SUB_KERNEL_IO_STDIO_0500
112  * @tc.name   vdprintf basic function test
113  * @tc.desc   [C- SOFTWARE -0200]
114  */
HWTEST_F(IoTest, testVdprintf, Function | MediumTest | Level1)115 HWTEST_F(IoTest, testVdprintf, Function | MediumTest | Level1)
116 {
117     FILE *fp = nullptr;
118     FOPEN_WRITE(fp);
119     int fd;
120     FILENO(fp);
121     int ret = FormatVdprintf(fd, "%s has %d words", "hello world", 11);
122     EXPECT_EQ(ret, 24) << "> vdprintf fail, errno = " << errno;
123     ASSERT_NE(fclose(fp), -1) << "> fclose fail, errno = " << errno;
124 
125     FOPEN_READ(fp);
126     char str[50] = {0};
127     char *retC = fgets(str, sizeof(str), fp);
128     EXPECT_STREQ(retC, str);
129     EXPECT_STREQ(retC, "hello world has 11 words");
130     ASSERT_NE(fclose(fp), -1) << "> fclose fail, errno = " << errno;
131 }
132 
133 /**
134  * @tc.number SUB_KERNEL_IO_STDIO_0600
135  * @tc.name   dprintf basic function test
136  * @tc.desc   [C- SOFTWARE -0200]
137  */
HWTEST_F(IoTest, testDprintf, Function | MediumTest | Level1)138 HWTEST_F(IoTest, testDprintf, Function | MediumTest | Level1)
139 {
140     FILE *fp = nullptr;
141     FOPEN_WRITE(fp);
142     int fd;
143     FILENO(fp);
144     int ret = dprintf(fd, "%s has %d words", "hello world", 11);
145     EXPECT_EQ(ret, 24) << "> dprintf fail, errno = " << errno;
146     ASSERT_NE(fclose(fp), -1) << "> fclose fail, errno = " << errno;
147 
148     FOPEN_READ(fp);
149     char str[50] = {0};
150     char *retC = fgets(str, sizeof(str), fp);
151     EXPECT_STREQ(retC, str);
152     EXPECT_STREQ(retC, "hello world has 11 words");
153     ASSERT_NE(fclose(fp), -1) << "> fclose fail, errno = " << errno;
154 }
155 
156 /**
157  * @tc.number SUB_KERNEL_IO_STDIO_0700
158  * @tc.name   fprintf basic function test
159  * @tc.desc   [C- SOFTWARE -0200]
160  */
HWTEST_F(IoTest, testFprintf, Function | MediumTest | Level1)161 HWTEST_F(IoTest, testFprintf, Function | MediumTest | Level1)
162 {
163     FILE *fp = nullptr;
164     FOPEN_WRITE(fp);
165     int ret = fprintf(fp, "%s has %d words", "helloworld", 10);
166     EXPECT_EQ(ret, 23) << "> fprintf fail, errno = " << errno;
167     ASSERT_NE(fclose(fp), -1) << "> fclose fail, errno = " << errno;
168 
169     FOPEN_READ(fp);
170     char str[50] = {0};
171     int i;
172     ret = fscanf(fp, "%s has %d words", str, &i);
173     EXPECT_EQ(ret, 2);
174     EXPECT_STREQ(str, "helloworld");
175     EXPECT_EQ(i, 10);
176     ASSERT_NE(fclose(fp), -1) << "> fclose fail, errno = " << errno;
177 }
178 
FormatVfprintf(FILE *stream, char *format, ...)179 int FormatVfprintf(FILE *stream, char *format, ...)
180 {
181     va_list args;
182     va_start(args, format);
183     int ret = vfprintf(stream, format, args);
184     va_end(args);
185     return ret;
186 }
187 
FormatVfscanf(FILE *stream, const char *format, ...)188 int FormatVfscanf(FILE *stream, const char *format, ...)
189 {
190     va_list args;
191     va_start(args, format);
192     int ret = vfscanf(stream, format, args);
193     va_end(args);
194     return ret;
195 }
196 
197 /**
198  * @tc.number SUB_KERNEL_IO_STDIO_0800
199  * @tc.name   vfprintf basic function test
200  * @tc.desc   [C- SOFTWARE -0200]
201  */
HWTEST_F(IoTest, testVfprintf, Function | MediumTest | Level1)202 HWTEST_F(IoTest, testVfprintf, Function | MediumTest | Level1)
203 {
204     FILE *fp = nullptr;
205     FOPEN_WRITE(fp);
206     int ret = FormatVfprintf(fp, (char *)"%s has %d words", "helloworld", 10);
207     EXPECT_EQ(ret, 23) << "> vfprintf fail, errno = " << errno;
208     EXPECT_NE(fclose(fp), -1) << "> fclose fail, errno = " << errno;
209 
210     FOPEN_READ(fp);
211     char str[50] = {0};
212     int i;
213     ret = FormatVfscanf(fp, "%s has %d words", str, &i);
214     EXPECT_EQ(ret, 2);
215     EXPECT_STREQ(str, "helloworld");
216     EXPECT_EQ(i, 10);
217     EXPECT_NE(fclose(fp), -1) << "> fclose fail, errno = " << errno;
218 }
219 
220 /**
221  * @tc.number SUB_KERNEL_IO_STDIO_0900
222  * @tc.name   clearerr basic function test
223  * @tc.desc   [C- SOFTWARE -0200]
224  */
HWTEST_F(IoTest, testClearerr, Function | MediumTest | Level1)225 HWTEST_F(IoTest, testClearerr, Function | MediumTest | Level1)
226 {
227     FILE *fp = nullptr;
228     FOPEN_WRITE(fp);
229     fgetc(fp);
230     int ret = ferror(fp);
231     ASSERT_NE(ret, 0);
232     clearerr(fp);
233     ret = ferror(fp);
234     EXPECT_EQ(ret, 0);
235     EXPECT_NE(fclose(fp), -1) << "> fclose fail, errno = " << errno;
236 }
237 
238 /**
239  * @tc.number SUB_KERNEL_IO_STDIO_1000
240  * @tc.name   printf basic function test
241  * @tc.desc   [C- SOFTWARE -0200]
242  */
HWTEST_F(IoTest, testPrintf, Function | MediumTest | Level1)243 HWTEST_F(IoTest, testPrintf, Function | MediumTest | Level1)
244 {
245     FILE *fp = nullptr;
246     pid_t pid = fork();
247     ASSERT_TRUE(pid >= 0) << "======== Fork Error! =========";
248     if (pid == 0) { // child
249         int rt = 0;
250         fp = freopen(IOTEST_TEMPFILE, "w", stdout);
251         if (fp == nullptr) {
252             LOG("freopen fail, errno = %d", errno);
253             rt = 1;
254         }
255         if (printf("%x %o %.6d", 11, 11, 11) != 11) {
256             LOG("printf fail, errno = %d", errno);
257             rt = 1;
258         }
259         if (fclose(fp) == -1) {
260             LOG("fclose fail, errno = %d", errno);
261             rt = 1;
262         }
263         exit(rt);
264     } else { // parent
265         WaitProcExitedOK(pid);
266 
267         FILE *fp1 = fopen(IOTEST_TEMPFILE, "r");
268         ASSERT_NE(fp1, nullptr) << "fopen fail, errno = " << errno;
269         char str[50] = {0};
270         char *gStr = fgets(str, sizeof(str), fp1);
271         EXPECT_STREQ(gStr, str) << "fgets fail, errno = " << errno;
272         EXPECT_STREQ(str, "b 13 000011") << "fgets fail, errno = " << errno;
273         EXPECT_NE(fclose(fp1), -1) << "> fclose fail, errno = " << errno;
274     }
275 }
276 
277 /**
278  * @tc.number SUB_KERNEL_IO_STDIO_1100
279  * @tc.name   scanf basic function test
280  * @tc.desc   [C- SOFTWARE -0200]
281  */
HWTEST_F(IoTest, testScanf, Function | MediumTest | Level1)282 HWTEST_F(IoTest, testScanf, Function | MediumTest | Level1)
283 {
284     FILE *fp = nullptr;
285     INIT_TEST_FILE(fp);
286     pid_t pid = fork();
287     ASSERT_TRUE(pid >= 0) << "======== Fork Error! =========";
288     if (pid == 0) { // child
289         int rt = 0;
290         FILE *fp1 = freopen(IOTEST_TEMPFILE, "r", stdin);
291         if (fp1 == nullptr) {
292             LOG("freopen fail, errno = %d", errno);
293             rt = 1;
294         }
295         char c;
296         if (scanf("%c", &c) != 1) {
297             LOG("scanf fail, errno = %d", errno);
298             rt = 1;
299         }
300         if (c != 'h') {
301             LOG("scanf fail, errno = %d", errno);
302             rt = 1;
303         }
304         if (fclose(fp1) == -1) {
305             LOG("fclose fail, errno = %d", errno);
306         }
307         exit(rt);
308     } else { // parent
309         WaitProcExitedOK(pid);
310     }
311 }
312 
FormatVasprintf(char **buffer, const char *fmt, ...)313 int FormatVasprintf(char **buffer, const char *fmt, ...)
314 {
315     va_list ap;
316     va_start(ap, fmt);
317     int ret = vasprintf(buffer, fmt, ap);
318     va_end(ap);
319     return ret;
320 }
321 
322 /**
323  * @tc.number SUB_KERNEL_IO_STDIO_1200
324  * @tc.name   vasprintf basic function test
325  * @tc.desc   [C- SOFTWARE -0200]
326  */
HWTEST_F(IoTest, testVasprintf, Function | MediumTest | Level1)327 HWTEST_F(IoTest, testVasprintf, Function | MediumTest | Level1)
328 {
329     char *buf = nullptr;
330     int ret = FormatVasprintf(&buf, "%s has %d words", "hello world", 11);
331     EXPECT_EQ(ret, 24);
332     EXPECT_STREQ(buf, "hello world has 11 words");
333 
334     ret = FormatVasprintf(&buf, "%f and %c as well as %ld\n", 2.2, 'c', 6);
335     EXPECT_EQ(ret, 28);
336     EXPECT_STREQ(buf, "2.200000 and c as well as 6\n");
337     free(buf);
338 }
339 
340 /**
341  * @tc.number SUB_KERNEL_IO_STDIO_1300
342  * @tc.name   getline basic function test
343  * @tc.desc   [C- SOFTWARE -0200]
344  */
HWTEST_F(IoTest, testGetline, Function | MediumTest | Level1)345 HWTEST_F(IoTest, testGetline, Function | MediumTest | Level1)
346 {
347     FILE *fp = nullptr;
348     FOPEN_WRITE(fp);
349     char str[] = "hello world";
350     int ret = fputs(str, fp);
351     ASSERT_NE(ret, -1);
352     EXPECT_NE(fclose(fp), -1) << "> fclose fail, errno = " << errno;
353 
354     FOPEN_READ(fp);
355     char *linePtr = nullptr;
356     size_t len = 0;
357     ssize_t retS = getline(&linePtr, &len, fp);
358     EXPECT_EQ(retS, 11);
359     EXPECT_STREQ(linePtr, "hello world");
360     free(linePtr);
361     EXPECT_NE(fclose(fp), -1) << "> fclose fail, errno = " << errno;
362 }
363 
364 /**
365  * @tc.number SUB_KERNEL_IO_STDIO_1400
366  * @tc.name   getdelim basic function test
367  * @tc.desc   [C- SOFTWARE -0200]
368  */
HWTEST_F(IoTest, testGetdelim, Function | MediumTest | Level1)369 HWTEST_F(IoTest, testGetdelim, Function | MediumTest | Level1)
370 {
371     FILE *fp = nullptr;
372     FOPEN_WRITE(fp);
373     char str[] = "hello world";
374     int ret = fputs(str, fp);
375     ASSERT_NE(ret, -1);
376     EXPECT_NE(fclose(fp), -1) << "> fclose fail, errno = " << errno;
377 
378     FOPEN_READ(fp);
379     char *linePtr = nullptr;
380     size_t len = 0;
381     ssize_t retS = getdelim(&linePtr, &len, ' ', fp);
382     EXPECT_EQ(retS, 6);
383     EXPECT_STREQ(linePtr, "hello ");
384     EXPECT_NE(fclose(fp), -1) << "> fclose fail, errno = " << errno;
385     free(linePtr);
386 }
387 
388 /**
389  * @tc.number SUB_KERNEL_IO_STDIO_1500
390  * @tc.name   fgetpos basic function test
391  * @tc.desc   [C- SOFTWARE -0200]
392  */
HWTEST_F(IoTest, testFgetpos, Function | MediumTest | Level1)393 HWTEST_F(IoTest, testFgetpos, Function | MediumTest | Level1)
394 {
395     FILE *fp = nullptr;
396     INIT_TEST_FILE(fp);
397     FOPEN_READ(fp);
398     fpos_t pos;
399     int ret = fgetpos(fp, &pos);
400     EXPECT_EQ(ret, 0) << "fgetpos fail, errno = " << errno;
401     char str[50] = {0};
402     char *gStr = fgets(str, sizeof(str), fp);
403     EXPECT_STREQ(gStr, str);
404     EXPECT_STREQ(str, "hello world");
405     ret = fsetpos(fp, &pos);
406     EXPECT_EQ(ret, 0) << "fgetpos fail, errno = " << errno;
407     gStr = fgets(str, sizeof(str), fp);
408     EXPECT_STREQ(gStr, str);
409     EXPECT_STREQ(str, "hello world");
410     EXPECT_NE(fclose(fp), -1) << "> fclose fail, errno = " << errno;
411 }
412 
413 /**
414  * @tc.number SUB_KERNEL_IO_STDIO_1600
415  * @tc.name   snprintf basic function test
416  * @tc.desc   [C- SOFTWARE -0200]
417  */
HWTEST_F(IoTest, testSnprintf, Function | MediumTest | Level1)418 HWTEST_F(IoTest, testSnprintf, Function | MediumTest | Level1)
419 {
420     char buf[100] = {0};
421     int ret = snprintf(buf, sizeof(buf), "%s has %d words", "hello world", 11);
422     EXPECT_EQ(ret, 24) << "> snprintf fail, errno = " <<errno;
423     EXPECT_STREQ(buf, "hello world has 11 words");
424 
425     ret = snprintf(buf, sizeof(buf), "%.2f %c\n", 1.1, 'c');
426     EXPECT_EQ(ret, 7) << "> snprintf fail, errno = " <<errno;
427     EXPECT_STREQ(buf, "1.10 c\n");
428 }
429 
430 /**
431  * @tc.number SUB_KERNEL_IO_STDIO_1700
432  * @tc.name   asprintf basic function test
433  * @tc.desc   [C- SOFTWARE -0200]
434  */
HWTEST_F(IoTest, testAsprintf, Function | MediumTest | Level1)435 HWTEST_F(IoTest, testAsprintf, Function | MediumTest | Level1)
436 {
437     char *buf = nullptr;
438     int ret = asprintf(&buf, "%s has %d words ", "hello world", 11);
439     EXPECT_EQ(ret, 25);
440     EXPECT_STREQ(buf, "hello world has 11 words ");
441 
442     ret = asprintf(&buf, "%o %c %x %f", 1, 'c', 2, 3.4);
443     EXPECT_EQ(ret, 14);
444     EXPECT_STREQ(buf, "1 c 2 3.400000");
445     free(buf);
446 }
447 
448 /**
449  * @tc.number SUB_KERNEL_IO_STDIO_1800
450  * @tc.name   gets basic function test
451  * @tc.desc   [C- SOFTWARE -0200]
452  */
HWTEST_F(IoTest, testGets, Function | MediumTest | Level1)453 HWTEST_F(IoTest, testGets, Function | MediumTest | Level1)
454 {
455     FILE *fp = nullptr;
456     INIT_TEST_FILE(fp);
457     pid_t pid = fork();
458     ASSERT_TRUE(pid >= 0) << "======== Fork Error! =========";
459     if (pid == 0) { // child
460         int rt = 0;
461         FILE *fp1 = freopen(IOTEST_TEMPFILE, "r", stdin);
462         if (fp1 == nullptr) {
463             LOG("freopen fail, errno = %d", errno);
464             rt = 1;
465         }
466         char str[50] = {0};
467         if (gets(str) != str) {
468             LOG("gets fail, errno = %d", errno);
469             rt = 1;
470             if (strcmp(gets(str), "hello world") != 0) {
471                 LOG("gets fail, errno = %d", errno);
472                 rt = 1;
473             }
474         }
475         if (fclose(fp1) == -1) {
476             LOG("fclose fail, errno = %d", errno);
477             rt = 1;
478         }
479         exit(rt);
480     } else { // parent
481         WaitProcExitedOK(pid);
482     }
483 }
484 
FormatVsprintf(char *str, char *format, ...)485 int FormatVsprintf(char *str, char *format, ...)
486 {
487     va_list aptr;
488     va_start(aptr, format);
489     int ret = vsprintf(str, format, aptr);
490     va_end(aptr);
491     return(ret);
492 }
493 
FormatVsscanf(const char *str, const char *format, ...)494 int FormatVsscanf(const char *str, const char *format, ...)
495 {
496     va_list args;
497     va_start(args, format);
498     int ret = vsscanf(str, format, args);
499     va_end(args);
500     return ret;
501 }
502 
503 /**
504  * @tc.number SUB_KERNEL_IO_STDIO_1900
505  * @tc.name   vsprintf basic function test
506  * @tc.desc   [C- SOFTWARE -0200]
507  */
HWTEST_F(IoTest, testVsprintf, Function | MediumTest | Level1)508 HWTEST_F(IoTest, testVsprintf, Function | MediumTest | Level1)
509 {
510     char str[50] = {0};
511     int ret = FormatVsprintf(str, (char *)"%s has %d words", "helloworld", 10);
512     EXPECT_EQ(ret, 23) << "> vsprintf fail, errno = " << errno;
513     EXPECT_STREQ(str, "helloworld has 10 words");
514 
515     char strTemp[50] = {0};
516     int i;
517     ret = FormatVsscanf(str, "%s has %d words", strTemp, &i);
518     EXPECT_EQ(ret, 2) << "> vsscanf fail, errno = " << errno;
519     EXPECT_STREQ(strTemp, "helloworld");
520     EXPECT_EQ(i, 10);
521 }
522 
523 /**
524  * @tc.number SUB_KERNEL_IO_STDIO_2000
525  * @tc.name   fgetln basic function test
526  * @tc.desc   [C- SOFTWARE -0200]
527  */
HWTEST_F(IoTest, testFgetln, Function | MediumTest | Level1)528 HWTEST_F(IoTest, testFgetln, Function | MediumTest | Level1)
529 {
530     FILE *fp = nullptr;
531     size_t len = 0;
532     INIT_TEST_FILE(fp);
533     FOPEN_READ(fp);
534     char *ret = fgetln(fp, &len);
535     EXPECT_STREQ(ret, "hello world") << "> fgetln fail , errno = " << errno;
536     EXPECT_EQ(len, 11U) << "> fgetln fail , errno = " << errno;
537     EXPECT_NE(fclose(fp), -1) << "fclose fail, errno = " << errno;
538 }
539 
FormatVsnprintf(char *format, ...)540 int FormatVsnprintf(char *format, ...)
541 {
542     va_list vArgList;
543     va_start(vArgList, format);
544     char str[50] = {0};
545     int ret = vsnprintf(str, sizeof(str), format, vArgList);
546     va_end(vArgList);
547     return ret;
548 }
549 
550 /**
551  * @tc.number SUB_KERNEL_IO_STDIO_2100
552  * @tc.name   vsnprintf basic function test
553  * @tc.desc   [C- SOFTWARE -0200]
554  */
HWTEST_F(IoTest, testVsnprintf, Function | MediumTest | Level1)555 HWTEST_F(IoTest, testVsnprintf, Function | MediumTest | Level1)
556 {
557     int ret = FormatVsnprintf((char *)"%s has %d words", "hello world", 11);
558     EXPECT_EQ(ret, 24) << "> vsnprintf fail, errno = " << errno;
559 
560     ret = FormatVsnprintf((char *)"%f and %c as well as %ld\n", 2.2, 'c', 6);
561     EXPECT_EQ(ret, 28) << "> vsnprintf fail, errno = " << errno;
562 }
563 
VwarnTest(char *fmt, ...)564 void VwarnTest(char *fmt, ...)
565 {
566     va_list ap;
567     va_start(ap, fmt);
568     vwarn(fmt, ap);
569     va_end(ap);
570 }
571 
VwarnxTest(char *fmt, ...)572 void VwarnxTest(char *fmt, ...)
573 {
574     va_list ap;
575     va_start(ap, fmt);
576     vwarnx(fmt, ap);
577     va_end(ap);
578 }
579 
580 /**
581  * @tc.number SUB_KERNEL_IO_STDIO_2200
582  * @tc.name   perror basic function test
583  * @tc.desc   [C- SOFTWARE -0200]
584  */
HWTEST_F(IoTest, testPerror, Function | MediumTest | Level1)585 HWTEST_F(IoTest, testPerror, Function | MediumTest | Level1)
586 {
587     FILE *fp = nullptr;
588     pid_t pid = fork();
589     ASSERT_TRUE(pid >= 0) << "======== Fork Error! =========";
590     if (pid == 0) { // child
591         int rt = 0;
592         fp = freopen(IOTEST_TEMPFILE, "w", stderr);
593         if (fp == nullptr) {
594             LOG("freopen fail, errno = %d", errno);
595             rt = 1;
596         }
597         errno = 22;
598         perror("perror msg");
599         VwarnTest((char *)"vwarn msg");
600         VwarnxTest((char *)"vwarnx msg");
601         if (fclose(fp) == -1) {
602             LOG("fclose fail, errno = %d", errno);
603             rt = 1;
604         }
605         exit(rt);
606     } else { // parent
607         WaitProcExitedOK(pid);
608 
609         FILE *fp1 = fopen(IOTEST_TEMPFILE, "r");
610         ASSERT_NE(fp1, nullptr) << "fopen fail, errno = " << errno;
611         char str[100] = {0};
612         char *gStr = fgets(str, sizeof(str), fp1);
613         EXPECT_STREQ(gStr, str);
614         EXPECT_STREQ(str, "perror msg: Invalid argument\n");
615         gStr = fgets(str, sizeof(str), fp1);
616         EXPECT_STREQ(gStr, str);
617         EXPECT_STREQ(str, "ActsIoApiTest.bin: vwarn msg: Invalid argument\n");
618         gStr = fgets(str, sizeof(str), fp1);
619         EXPECT_STREQ(gStr, str);
620         EXPECT_STREQ(str, "ActsIoApiTest.bin: vwarnx msg\n");
621         EXPECT_NE(fclose(fp1), -1) << "> fclose fail, errno = " << errno;
622     }
623 }
624 
625 /**
626  * @tc.number SUB_KERNEL_IO_STDIO_2300
627  * @tc.name   rewind basic function test
628  * @tc.desc   [C- SOFTWARE -0200]
629  */
HWTEST_F(IoTest, testRewind, Function | MediumTest | Level1)630 HWTEST_F(IoTest, testRewind, Function | MediumTest | Level1)
631 {
632     FILE *fp = nullptr;
633     INIT_TEST_FILE(fp);
634     FOPEN_READ(fp);
635     int ret = fgetc(fp);
636     EXPECT_EQ(ret, 104);
637     rewind(fp);
638     ret = fgetc(fp);
639     EXPECT_EQ(ret, 104);
640     EXPECT_NE(fclose(fp), -1) << "fclose fail, errno = " <<  errno;
641 }
642 
FormatVprintf(char *format, ...)643 int FormatVprintf(char *format, ...)
644 {
645     va_list args;
646     va_start(args, format);
647     int ret = vprintf(format, args);
648     va_end(args);
649     return ret;
650 }
651 
652 /**
653  * @tc.number SUB_KERNEL_IO_STDIO_2400
654  * @tc.name   vprintf basic function test
655  * @tc.desc   [C- SOFTWARE -0200]
656  */
HWTEST_F(IoTest, testVprintf, Function | MediumTest | Level1)657 HWTEST_F(IoTest, testVprintf, Function | MediumTest | Level1)
658 {
659     FILE *fp = nullptr;
660     pid_t pid = fork();
661     ASSERT_TRUE(pid >= 0) << "======== Fork Error! =========";
662     if (pid == 0) { // child
663         int rt = 0;
664         fp = freopen(IOTEST_TEMPFILE, "w", stdout);
665         if (fp == nullptr) {
666             LOG("freopen fail, errno = %d", errno);
667             rt = 1;
668         }
669         if (FormatVprintf((char *)"%x %o %.6d", 11, 11, 11) != 11) {
670             LOG("vprintf fail, errno = %d", errno);
671             rt = 1;
672         }
673         if (fclose(fp) == -1) {
674             LOG("fclose fail, errno = %d", errno);
675             rt = 1;
676         }
677         exit(rt);
678     } else { // parent
679         WaitProcExitedOK(pid);
680 
681         FILE *fp1 = fopen(IOTEST_TEMPFILE, "r");
682         ASSERT_NE(fp1, nullptr) << "fopen fail, errno = " << errno;
683         char str[50] = {0};
684         char *gStr = fgets(str, sizeof(str), fp1);
685         EXPECT_STREQ(gStr, str) << "fgets fail, errno = " << errno;
686         EXPECT_STREQ(str, "b 13 000011") << "fgets fail, errno = " << errno;
687         EXPECT_NE(fclose(fp1), -1) << "> fclose fail, errno = " << errno;
688     }
689 }
690 
FormatVscanf(const char *format, ...)691 int FormatVscanf(const char *format, ...)
692 {
693     va_list args;
694     va_start(args, format);
695     int ret = vscanf(format, args);
696     va_end(args);
697     return ret;
698 }
699 
700 /**
701  * @tc.number SUB_KERNEL_IO_STDIO_2500
702  * @tc.name   vscanf basic function test
703  * @tc.desc   [C- SOFTWARE -0200]
704  */
HWTEST_F(IoTest, testVscanf, Function | MediumTest | Level1)705 HWTEST_F(IoTest, testVscanf, Function | MediumTest | Level1)
706 {
707     FILE *fp = nullptr;
708     INIT_TEST_FILE(fp);
709     pid_t pid = fork();
710     ASSERT_TRUE(pid >= 0) << "======== Fork Error! =========";
711     if (pid == 0) { // child
712         int rt = 0;
713         FILE *fp1 = freopen(IOTEST_TEMPFILE, "r", stdin);
714         if (fp1 == nullptr) {
715             LOG("freopen fail, errno = %d", errno);
716             rt = 1;
717         }
718         char c;
719         if (FormatVscanf("%c", &c) != 1) {
720             LOG("vscanf fail, errno = %d", errno);
721             rt = 1;
722         }
723         if (c != 'h') {
724             LOG("scanf fail, errno = %d", errno);
725             rt = 1;
726         }
727         if (fclose(fp1) == -1) {
728             LOG("fclose fail, errno = %d", errno);
729             rt = 1;
730         }
731         exit(rt);
732     } else { // parent
733         WaitProcExitedOK(pid);
734     }
735 }
736 
Thread(void *arg)737 void *Thread(void *arg)
738 {
739     FILE *fp = fopen(IOTEST_TEMPFILE, "w");
740     EXPECT_NE(fp, nullptr) << "fopen fail, errno = " << errno;
741     if (fp) {
742         EXPECT_NE(fputs("hello world", fp), -1) << "fputs fail, errno = " << errno;
743         EXPECT_NE(fclose(fp), -1) << "fclose fail, errno = " << errno;
744     }
745 
746     FILE *fp1 = freopen(IOTEST_TEMPFILE, "r", stdin);
747     EXPECT_TRUE(fp1 != nullptr) << "freopen fail, errno = " <<  errno;
748     if (fp1) {
749         if (getchar_unlocked() != EOF) {
750             EXPECT_NE(getchar_unlocked(), -1) <<  "getchar_unlocked fail,  errno = " << errno;
751         }
752         EXPECT_NE(fclose(fp1), -1) << "fclose fail, errno = " << errno;
753     }
754     return nullptr;
755 }
756 
757 /**
758  * @tc.number SUB_KERNEL_IO_STDIO_2600
759  * @tc.name   getchar_unlocked basic function test
760  * @tc.desc   [C- SOFTWARE -0200]
761  */
HWTEST_F(IoTest, testGetcharUnlocked, Function | MediumTest | Level1)762 HWTEST_F(IoTest, testGetcharUnlocked, Function | MediumTest | Level1)
763 {
764     pthread_t tid;
765     int retI = pthread_create(&tid, NULL, Thread, NULL);
766     ASSERT_EQ(retI, 0) << "> create thread fail !!!";
767     retI = pthread_join(tid, NULL);
768 
769     FILE *fp = nullptr;
770     INIT_TEST_FILE(fp);
771     pid_t pid = fork();
772     ASSERT_TRUE(pid >= 0) << "======== Fork Error! =========";
773     if (pid == 0) { // child
774         int rt = 0;
775         FILE *fp1 = freopen(IOTEST_TEMPFILE, "r", stdin);
776         if (fp1 == nullptr) {
777             LOG("freopen fail, errno = %d", errno);
778             rt = 1;
779         }
780         if (getchar_unlocked() != EOF) {
781             if (getchar_unlocked() == -1) {
782                 LOG("getchar_unlocked fail, errno = %d", errno);
783                 rt = 1;
784             }
785         }
786         if (fclose(fp1) == -1) {
787             LOG("fclose fail, errno = %d", errno);
788             rt = 1;
789         }
790         exit(rt);
791     } else { // parent
792         WaitProcExitedOK(pid);
793     }
794 }
795 
796 /**
797  * @tc.number SUB_KERNEL_IO_STDIO_2700
798  * @tc.name   putchar_unlocked basic function test
799  * @tc.desc   [C- SOFTWARE -0200]
800  */
HWTEST_F(IoTest, testPutcharUnlocked, Function | MediumTest | Level1)801 HWTEST_F(IoTest, testPutcharUnlocked, Function | MediumTest | Level1)
802 {
803     for (int i = 0; i < 128; i++) {
804         int ret = putchar_unlocked(i);
805         EXPECT_EQ(ret, i);
806     }
807 }
808 
809 /**
810  * @tc.number SUB_KERNEL_IO_STDIO_2800
811  * @tc.name   putc_unlocked basic function test
812  * @tc.desc   [C- SOFTWAret -0200]
813  */
HWTEST_F(IoTest, testPutcUnlocked, Function | MediumTest | Level1)814 HWTEST_F(IoTest, testPutcUnlocked, Function | MediumTest | Level1)
815 {
816     FILE *fp;
817     FOPEN_WRITE(fp);
818     int ret = putc_unlocked('a', fp);
819     EXPECT_EQ(ret, 'a');
820     EXPECT_NE(fclose(fp), -1) << "> fclose fail, errno = " << errno;
821 
822     FOPEN_READ(fp);
823     ret = getc_unlocked(fp);
824     EXPECT_EQ(ret, 'a');
825     EXPECT_NE(fclose(fp), -1) << "> fclose fail, errno = " << errno;
826 }
827 
828 /**
829  * @tc.number SUB_KERNEL_IO_STDIO_2900
830  * @tc.name   fputc_unlocked basic function test
831  * @tc.desc   [C- SOFTWARE -0200]
832  */
HWTEST_F(IoTest, testFputcUnlocked, Function | MediumTest | Level1)833 HWTEST_F(IoTest, testFputcUnlocked, Function | MediumTest | Level1)
834 {
835     FILE *fp = nullptr;
836     FOPEN_WRITE(fp);
837     int ret = fputc_unlocked('a', fp);
838     EXPECT_EQ(ret, 'a');
839     EXPECT_NE(fclose(fp), -1) << "> fclose fail, errno = " << errno;
840 
841     FOPEN_READ(fp);
842     ret = fgetc_unlocked(fp);
843     EXPECT_EQ(ret, 'a');
844     EXPECT_NE(fclose(fp), -1) << "> fclose fail, errno = " << errno;
845 }
846 
847 /**
848  * @tc.number SUB_KERNEL_IO_STDIO_3000
849  * @tc.name   setbuffer basic function test
850  * @tc.desc   [C- SOFTWARE -0200]
851  */
HWTEST_F(IoTest, testSetbuffer, Function | MediumTest | Level1)852 HWTEST_F(IoTest, testSetbuffer, Function | MediumTest | Level1)
853 {
854     FILE *fp = nullptr;
855     FOPEN_WRITE(fp);
856     char buf[100] = {0};
857     int ret = setvbuf(fp, buf, _IOFBF, sizeof(buf));
858     EXPECT_EQ(ret, 0) << "> setvbuf fail, errno = " << errno;
859     ret = fprintf(fp, "%s", "abc");
860     EXPECT_EQ(ret, 3) << "> fprintf fail, errno = " << errno;
861     EXPECT_EQ(buf[8], 'a');
862     EXPECT_EQ(buf[9], 'b');
863     EXPECT_EQ(buf[10], 'c');
864 
865     buf[8] = 'k';
866     EXPECT_EQ(buf[8], 'k');
867     EXPECT_NE(fclose(fp), -1) << "fclose fail, errno = " << errno;
868 
869     EXPECT_EQ(buf[8], 'k');
870     EXPECT_EQ(buf[9], 'b');
871     EXPECT_EQ(buf[10], 'c');
872 
873     FOPEN_READ(fp);
874     char str4[50] = {0};
875     char *gStr = fgets(str4, sizeof(str4), fp);
876     EXPECT_STREQ(gStr, str4);
877     EXPECT_STREQ(gStr, "kbc");
878     EXPECT_NE(fclose(fp), -1) << "fclose fail, errno = " << errno;
879 }
880 
881 /**
882  * @tc.number SUB_KERNEL_IO_STDIO_3100
883  * @tc.name   setlinebuf basic function test
884  * @tc.desc   [C- SOFTWARE -0200]
885  */
HWTEST_F(IoTest, testSetlinebuf, Function | MediumTest | Level1)886 HWTEST_F(IoTest, testSetlinebuf, Function | MediumTest | Level1)
887 {
888     FILE *fp = nullptr;
889     FOPEN_WRITE(fp);
890     char buf[100] = {0};
891     int ret = setvbuf(fp, buf, _IOLBF, sizeof(buf));
892     EXPECT_EQ(ret, 0) << "> setvbuf fail, errno = " << errno;
893     ret = fprintf(fp, "%s", "he\nllo");
894     EXPECT_EQ(ret, 6) << "> fprintf fail, errno = " << errno;
895     EXPECT_EQ(buf[8], 'l');
896     EXPECT_EQ(buf[9], 'l');
897     EXPECT_EQ(buf[10], 'o');
898 
899     buf[9] = 'b';
900     EXPECT_EQ(buf[9], 'b');
901     EXPECT_NE(fclose(fp), -1) << "fclose fail, errno = " << errno;
902 
903     FOPEN_READ(fp);
904     char str4[50] = {0};
905     char *gStr = fgets(str4, sizeof(str4), fp);
906     EXPECT_STREQ(gStr, str4);
907     EXPECT_STREQ(gStr, "he\n");
908     EXPECT_NE(fclose(fp), -1) << "fclose fail, errno = " << errno;
909 }
910 
911 /**
912  * @tc.number SUB_KERNEL_IO_STDIO_3200
913  * @tc.name   setbuf basic function test
914  * @tc.desc   [C- SOFTWARE -0200]
915  */
HWTEST_F(IoTest, testSetbuf, Function | MediumTest | Level1)916 HWTEST_F(IoTest, testSetbuf, Function | MediumTest | Level1)
917 {
918     FILE *fp = nullptr;
919     FOPEN_WRITE(fp);
920     char buf[100] = {0};
921     int ret = setvbuf(fp, nullptr, _IONBF, 0);
922     EXPECT_EQ(ret, 0) << "> setvbuf fail, errno = " << errno;
923     ret = fprintf(fp, "%s", "123");
924     EXPECT_EQ(ret, 3) << "> fprintf fail, errno = " << errno;
925     EXPECT_STREQ(buf, "") << "> setvbuf fail, errno = " << errno;
926     EXPECT_EQ(buf[8], '\0');
927     EXPECT_EQ(buf[9], '\0');
928     EXPECT_EQ(buf[10], '\0');
929 
930     buf[10] = '1';
931     EXPECT_EQ(buf[10], '1');
932     EXPECT_NE(fclose(fp), -1) << "fclose fail, errno = " << errno;
933 
934     FOPEN_READ(fp);
935     char str4[50] = {0};
936     char *gStr = fgets(str4, sizeof(str4), fp);
937     EXPECT_STREQ(gStr, str4);
938     EXPECT_STREQ(gStr, "123");
939     EXPECT_NE(fclose(fp), -1) << "fclose fail, errno = " << errno;
940 }
941 
942