• 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 <stdlib.h>
20 #include <signal.h>
21 #include <fcntl.h>
22 #include <wchar.h>
23 #include "test.h"
24 
deal_aberrant(int code)25 void deal_aberrant(int code)
26 {
27     if (code != SIGSEGV) {
28         t_error("vfwprintf_0200 code is %d are not SIGSEGV", __func__, code);
29     }
30     exit(t_status);
31 }
32 
readFile(FILE * stream,wchar_t * fmt,...)33 int readFile(FILE *stream, wchar_t *fmt, ...)
34 {
35     va_list ap;
36     va_start(ap, fmt);
37     int result = vfwprintf(stream, fmt, ap);
38     va_end(ap);
39     return result;
40 }
41 
42 /**
43  * @tc.name      : vfwprintf_0100
44  * @tc.desc      : Test vfwprintf method to write wide string to file
45  * @tc.level     : Level 0
46  */
vfwprintf_0100(void)47 void vfwprintf_0100(void)
48 {
49     wchar_t *value = L"hello %ls";
50     char *file_name = "/data/temp_vfwprintf.txt";
51     FILE *file = fopen(file_name, "w");
52     int resule = readFile(file, value, L"world");
53     if (resule < 0) {
54         t_error("%s vfwprintf error get result is %d are not less 0\n", __func__, resule);
55     }
56     fclose(file);
57     unlink(file_name);
58 }
59 
60 /**
61  * @tc.name      : vfwprintf_0200
62  * @tc.desc      : Test the result of the vfwprintf function when the incoming file is empty
63  * @tc.level     : Level 2
64  */
vfwprintf_0200(void)65 void vfwprintf_0200(void)
66 {
67     wchar_t *value = L"hello %ls";
68     signal(SIGSEGV, deal_aberrant);
69     int resule = readFile(NULL, value, L"world");
70 }
71 
main(int argc,char * argv[])72 int main(int argc, char *argv[])
73 {
74     vfwprintf_0100();
75     vfwprintf_0200();
76     return t_status;
77 }