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