1 /*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include <stdio_ext.h>
18
19 #include <gtest/gtest.h>
20
21 #include <errno.h>
22 #include <fcntl.h>
23 #include <limits.h>
24 #include <math.h>
25 #include <stdio.h>
26 #include <sys/types.h>
27 #include <sys/stat.h>
28 #include <unistd.h>
29 #include <wchar.h>
30 #include <locale.h>
31
32 #include "TemporaryFile.h"
33
TEST(stdio_ext,__fbufsize)34 TEST(stdio_ext, __fbufsize) {
35 FILE* fp = fopen("/proc/version", "r");
36
37 // Initially, there's no buffer in case the first thing you do is disable buffering.
38 ASSERT_EQ(0U, __fbufsize(fp));
39
40 // A read forces a buffer to be created.
41 char buf[128];
42 fgets(buf, sizeof(buf), fp);
43 ASSERT_EQ(1024U, __fbufsize(fp));
44
45 ASSERT_EQ(0, setvbuf(fp, buf, _IOFBF, 1));
46 ASSERT_EQ(1U, __fbufsize(fp));
47
48 ASSERT_EQ(0, setvbuf(fp, buf, _IOFBF, 8));
49 ASSERT_EQ(8U, __fbufsize(fp));
50
51 fclose(fp);
52 }
53
TEST(stdio_ext,__flbf)54 TEST(stdio_ext, __flbf) {
55 FILE* fp = fopen("/proc/version", "r");
56
57 ASSERT_FALSE(__flbf(fp));
58
59 char buf[128];
60 ASSERT_EQ(0, setvbuf(fp, buf, _IOLBF, sizeof(buf)));
61
62 ASSERT_TRUE(__flbf(fp));
63
64 fclose(fp);
65 }
66
TEST(stdio_ext,__fpending)67 TEST(stdio_ext, __fpending) {
68 FILE* fp = fopen("/dev/null", "w");
69 ASSERT_EQ(0U, __fpending(fp));
70 ASSERT_EQ('x', fputc('x', fp));
71 ASSERT_EQ(1U, __fpending(fp));
72 ASSERT_EQ('y', fputc('y', fp));
73 ASSERT_EQ(2U, __fpending(fp));
74 fflush(fp);
75 ASSERT_EQ(0U, __fpending(fp));
76 fclose(fp);
77 }
78
TEST(stdio_ext,__fpurge)79 TEST(stdio_ext, __fpurge) {
80 FILE* fp = tmpfile();
81
82 ASSERT_EQ('a', fputc('a', fp));
83 ASSERT_EQ(1U, __fpending(fp));
84 __fpurge(fp);
85 ASSERT_EQ(0U, __fpending(fp));
86
87 ASSERT_EQ('b', fputc('b', fp));
88 ASSERT_EQ('\n', fputc('\n', fp));
89 ASSERT_EQ(2U, __fpending(fp));
90
91 rewind(fp);
92
93 char buf[16];
94 char* s = fgets(buf, sizeof(buf), fp);
95 ASSERT_TRUE(s != NULL);
96 ASSERT_STREQ("b\n", s);
97
98 fclose(fp);
99 }
100
TEST(stdio_ext,_flushlbf)101 TEST(stdio_ext, _flushlbf) {
102 FILE* fp = fopen("/dev/null", "w");
103
104 char buf[128];
105 ASSERT_EQ(0, setvbuf(fp, buf, _IOLBF, sizeof(buf)));
106
107 ASSERT_EQ('a', fputc('a', fp));
108 ASSERT_EQ(1U, __fpending(fp));
109
110 _flushlbf();
111
112 ASSERT_EQ(0U, __fpending(fp));
113
114 fclose(fp);
115 }
116
TEST(stdio_ext,__freadable__fwritable)117 TEST(stdio_ext, __freadable__fwritable) {
118 FILE* fp = fopen("/dev/null", "r");
119 ASSERT_TRUE(__freadable(fp));
120 ASSERT_FALSE(__fwritable(fp));
121 fclose(fp);
122
123 fp = fopen("/dev/null", "w");
124 ASSERT_FALSE(__freadable(fp));
125 ASSERT_TRUE(__fwritable(fp));
126 fclose(fp);
127
128 fp = fopen("/dev/null", "w+");
129 ASSERT_TRUE(__freadable(fp));
130 ASSERT_TRUE(__fwritable(fp));
131 fclose(fp);
132 }
133
TEST(stdio_ext,__fsetlocking)134 TEST(stdio_ext, __fsetlocking) {
135 FILE* fp = fopen("/proc/version", "r");
136 ASSERT_EQ(FSETLOCKING_INTERNAL, __fsetlocking(fp, FSETLOCKING_QUERY));
137 ASSERT_EQ(FSETLOCKING_INTERNAL, __fsetlocking(fp, FSETLOCKING_BYCALLER));
138 ASSERT_EQ(FSETLOCKING_BYCALLER, __fsetlocking(fp, FSETLOCKING_QUERY));
139 ASSERT_EQ(FSETLOCKING_BYCALLER, __fsetlocking(fp, FSETLOCKING_INTERNAL));
140 ASSERT_EQ(FSETLOCKING_INTERNAL, __fsetlocking(fp, FSETLOCKING_QUERY));
141 fclose(fp);
142 }
143