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 <errno.h>
17 #include <stdio.h>
18 #include <string.h>
19
20 #include "filepath_util.h"
21
22 const char *str = "Hello";
23
24 /**
25 * @tc.name : setbuf_0100
26 * @tc.desc : stream buffering operations
27 * @tc.level : Level 0
28 */
setbuf_0100(void)29 void setbuf_0100(void)
30 {
31 char buf[BUFSIZ] = {0};
32 char path[PATH_MAX] = {0};
33 FILE_ABSOLUTE_PATH(STR_FILE_TXT, path);
34 FILE *f = fopen(path, "w+");
35 errno = 0;
36 setbuf(f, buf);
37 if (errno != 0) {
38 t_error("%s failed: errno = %d\n", __func__, errno);
39 }
40 fputs(str, f);
41 fflush(f);
42 fclose(f);
43 f = fopen(path, "r");
44 memset(buf, 0, sizeof(buf));
45 fread(buf, sizeof(buf), 1, f);
46 if (strcmp(buf, str)) {
47 t_error("%s failed: setbuf. buf = %s\n", __func__, buf);
48 }
49 fclose(f);
50 remove(path);
51 }
52
53 /**
54 * @tc.name : setbuf_0200
55 * @tc.desc : stream buffering operations with NULL
56 * @tc.level : Level 2
57 */
setbuf_0200(void)58 void setbuf_0200(void)
59 {
60 char path[PATH_MAX] = {0};
61 FILE_ABSOLUTE_PATH(STR_FILE_TXT, path);
62 FILE *f = fopen(path, "w+");
63
64 errno = 0;
65 setbuf(f, NULL);
66 if (errno != 0) {
67 t_error("%s failed: errno = %d\n", __func__, errno);
68 }
69
70 fclose(f);
71
72 remove(path);
73 }
74
main(int argc,char * argv[])75 int main(int argc, char *argv[])
76 {
77 setbuf_0100();
78 setbuf_0200();
79
80 return t_status;
81 }
82