blob: dc0e9efb00c7c1fa44cd37a41eb55ef7ff168a45 [file] [log] [blame]
Elliott Hughes2b021e12014-08-19 17:00:33 -07001/*
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
Elliott Hughes194a6302025-02-27 15:28:22 -080032#include <thread>
33
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -080034#include <android-base/file.h>
35
Yabin Cui76144aa2015-11-19 13:52:16 -080036#include "utils.h"
Elliott Hughes2b021e12014-08-19 17:00:33 -070037
38TEST(stdio_ext, __fbufsize) {
39 FILE* fp = fopen("/proc/version", "r");
40
Elliott Hughesdf8f1a42015-01-16 16:40:55 -080041 // Initially, there's no buffer in case the first thing you do is disable buffering.
42 ASSERT_EQ(0U, __fbufsize(fp));
43
44 // A read forces a buffer to be created.
Elliott Hughes2b021e12014-08-19 17:00:33 -070045 char buf[128];
Elliott Hughesdf8f1a42015-01-16 16:40:55 -080046 fgets(buf, sizeof(buf), fp);
47 ASSERT_EQ(1024U, __fbufsize(fp));
Elliott Hughes2b021e12014-08-19 17:00:33 -070048
49 ASSERT_EQ(0, setvbuf(fp, buf, _IOFBF, 1));
50 ASSERT_EQ(1U, __fbufsize(fp));
51
52 ASSERT_EQ(0, setvbuf(fp, buf, _IOFBF, 8));
53 ASSERT_EQ(8U, __fbufsize(fp));
54
55 fclose(fp);
56}
57
58TEST(stdio_ext, __flbf) {
59 FILE* fp = fopen("/proc/version", "r");
60
61 ASSERT_FALSE(__flbf(fp));
62
63 char buf[128];
64 ASSERT_EQ(0, setvbuf(fp, buf, _IOLBF, sizeof(buf)));
65
66 ASSERT_TRUE(__flbf(fp));
67
68 fclose(fp);
69}
70
71TEST(stdio_ext, __fpending) {
72 FILE* fp = fopen("/dev/null", "w");
73 ASSERT_EQ(0U, __fpending(fp));
74 ASSERT_EQ('x', fputc('x', fp));
75 ASSERT_EQ(1U, __fpending(fp));
76 ASSERT_EQ('y', fputc('y', fp));
77 ASSERT_EQ(2U, __fpending(fp));
78 fflush(fp);
79 ASSERT_EQ(0U, __fpending(fp));
80 fclose(fp);
81}
82
Elliott Hughes82be76b2022-09-22 23:47:42 +000083TEST(stdio_ext, __freadahead) {
84#if defined(__GLIBC__)
85 GTEST_SKIP() << "glibc doesn't have __freadahead";
86#else
87 FILE* fp = tmpfile();
88 ASSERT_NE(EOF, fputs("hello", fp));
89 rewind(fp);
90
91 ASSERT_EQ('h', fgetc(fp));
92 ASSERT_EQ(4u, __freadahead(fp));
93
94 ASSERT_EQ('H', ungetc('H', fp));
95 ASSERT_EQ(5u, __freadahead(fp));
96
97 fclose(fp);
98#endif
99}
100
Elliott Hughes2b021e12014-08-19 17:00:33 -0700101TEST(stdio_ext, __fpurge) {
102 FILE* fp = tmpfile();
103
104 ASSERT_EQ('a', fputc('a', fp));
105 ASSERT_EQ(1U, __fpending(fp));
106 __fpurge(fp);
107 ASSERT_EQ(0U, __fpending(fp));
108
109 ASSERT_EQ('b', fputc('b', fp));
110 ASSERT_EQ('\n', fputc('\n', fp));
111 ASSERT_EQ(2U, __fpending(fp));
112
113 rewind(fp);
114
115 char buf[16];
116 char* s = fgets(buf, sizeof(buf), fp);
Yi Kong32bc0fc2018-08-02 17:31:13 -0700117 ASSERT_TRUE(s != nullptr);
Elliott Hughes2b021e12014-08-19 17:00:33 -0700118 ASSERT_STREQ("b\n", s);
119
120 fclose(fp);
121}
122
123TEST(stdio_ext, _flushlbf) {
124 FILE* fp = fopen("/dev/null", "w");
125
126 char buf[128];
127 ASSERT_EQ(0, setvbuf(fp, buf, _IOLBF, sizeof(buf)));
128
129 ASSERT_EQ('a', fputc('a', fp));
130 ASSERT_EQ(1U, __fpending(fp));
131
132 _flushlbf();
133
134 ASSERT_EQ(0U, __fpending(fp));
135
136 fclose(fp);
137}
138
139TEST(stdio_ext, __freadable__fwritable) {
Elliott Hughes5ba2c212017-08-01 15:16:36 -0700140 FILE* fp;
141
142 // Read-only.
143 fp = fopen("/dev/null", "r");
Elliott Hughes2b021e12014-08-19 17:00:33 -0700144 ASSERT_TRUE(__freadable(fp));
145 ASSERT_FALSE(__fwritable(fp));
146 fclose(fp);
147
Elliott Hughes5ba2c212017-08-01 15:16:36 -0700148 // Write-only.
Elliott Hughes2b021e12014-08-19 17:00:33 -0700149 fp = fopen("/dev/null", "w");
150 ASSERT_FALSE(__freadable(fp));
151 ASSERT_TRUE(__fwritable(fp));
152 fclose(fp);
153
Elliott Hughes5ba2c212017-08-01 15:16:36 -0700154 // Append (aka write-only).
155 fp = fopen("/dev/null", "a");
156 ASSERT_FALSE(__freadable(fp));
Elliott Hughes2b021e12014-08-19 17:00:33 -0700157 ASSERT_TRUE(__fwritable(fp));
158 fclose(fp);
Elliott Hughes5ba2c212017-08-01 15:16:36 -0700159
160 // The three read-write modes.
161 for (auto read_write_mode : {"r+", "w+", "a+"}) {
162 fp = fopen("/dev/null", read_write_mode);
163 ASSERT_TRUE(__freadable(fp));
164 ASSERT_TRUE(__fwritable(fp));
165 fclose(fp);
166 }
167}
168
169TEST(stdio_ext, __freading__fwriting) {
170 FILE* fp;
171
172 // Append (aka write-only). Never reading. Always writing.
173 fp = fopen("/dev/zero", "a");
174 ASSERT_FALSE(__freading(fp)); // Not reading initially.
175 ASSERT_TRUE(__fwriting(fp)); // Writing initially.
176 ASSERT_TRUE(fputc('x', fp) != EOF);
177 ASSERT_FALSE(__freading(fp)); // Not reading after write.
178 ASSERT_TRUE(__fwriting(fp)); // Still writing after write.
179 fclose(fp);
180
181 // Write-only. Never reading. Always writing.
182 fp = fopen("/dev/zero", "w");
183 ASSERT_FALSE(__freading(fp)); // Not reading initially.
184 ASSERT_TRUE(__fwriting(fp)); // Writing initially.
185 ASSERT_TRUE(fputc('x', fp) != EOF);
186 ASSERT_FALSE(__freading(fp)); // Not reading after write.
187 ASSERT_TRUE(__fwriting(fp)); // Still writing after write.
188 fclose(fp);
189
190 // Read-only. Always reading. Never writing.
191 fp = fopen("/dev/zero", "r");
192 ASSERT_TRUE(__freading(fp)); // Reading initially.
193 ASSERT_FALSE(__fwriting(fp)); // Not writing initially.
194 ASSERT_TRUE(fgetc(fp) == 0);
195 ASSERT_TRUE(__freading(fp)); // Still reading after read.
196 ASSERT_FALSE(__fwriting(fp)); // Still not writing after read.
197 fclose(fp);
198
199 // The three read-write modes.
200 for (auto read_write_mode : {"r+", "w+", "a+"}) {
201 fp = fopen("/dev/zero", read_write_mode);
202 ASSERT_FALSE(__freading(fp)); // Not reading initially.
203 ASSERT_FALSE(__fwriting(fp)); // Not writing initially.
204 ASSERT_TRUE(fgetc(fp) == 0);
205 ASSERT_TRUE(__freading(fp)); // Reading after read.
206 ASSERT_FALSE(__fwriting(fp)); // Not writing after read.
207 ASSERT_TRUE(fputc('x', fp) != EOF);
208 ASSERT_FALSE(__freading(fp)); // Not reading after write.
209 ASSERT_TRUE(__fwriting(fp)); // Writing after write.
210 fclose(fp);
211 }
Elliott Hughes2b021e12014-08-19 17:00:33 -0700212}
213
Elliott Hughes45785262018-02-14 15:21:45 -0800214TEST(stdio_ext, __fseterr) {
215#if defined(__GLIBC__)
Elliott Hughesbcaa4542019-03-08 15:20:23 -0800216 GTEST_SKIP() << "glibc doesn't have __fseterr, but gnulib will use it";
Elliott Hughes45785262018-02-14 15:21:45 -0800217#else
218 FILE* fp = fopen("/dev/null", "w");
219
220 ASSERT_FALSE(ferror(fp));
221 __fseterr(fp);
222 ASSERT_TRUE(ferror(fp));
223 clearerr(fp);
224 ASSERT_FALSE(ferror(fp));
225
226 fclose(fp);
227#endif
228}
229
Elliott Hughes2b021e12014-08-19 17:00:33 -0700230TEST(stdio_ext, __fsetlocking) {
231 FILE* fp = fopen("/proc/version", "r");
Elliott Hughes8c4994b2015-01-20 18:09:05 -0800232 ASSERT_EQ(FSETLOCKING_INTERNAL, __fsetlocking(fp, FSETLOCKING_QUERY));
233 ASSERT_EQ(FSETLOCKING_INTERNAL, __fsetlocking(fp, FSETLOCKING_BYCALLER));
234 ASSERT_EQ(FSETLOCKING_BYCALLER, __fsetlocking(fp, FSETLOCKING_QUERY));
235 ASSERT_EQ(FSETLOCKING_BYCALLER, __fsetlocking(fp, FSETLOCKING_INTERNAL));
Elliott Hughes2b021e12014-08-19 17:00:33 -0700236 ASSERT_EQ(FSETLOCKING_INTERNAL, __fsetlocking(fp, FSETLOCKING_QUERY));
237 fclose(fp);
238}
Yabin Cui76144aa2015-11-19 13:52:16 -0800239
Yabin Cui76144aa2015-11-19 13:52:16 -0800240TEST(stdio_ext, __fsetlocking_BYCALLER) {
241 // Check if users can use flockfile/funlockfile to protect stdio operations.
242 int old_state = __fsetlocking(stdout, FSETLOCKING_BYCALLER);
243 flockfile(stdout);
Elliott Hughes194a6302025-02-27 15:28:22 -0800244
Yabin Cui76144aa2015-11-19 13:52:16 -0800245 std::atomic<pid_t> pid(0);
Elliott Hughes194a6302025-02-27 15:28:22 -0800246 std::thread thread([&]() {
247 pid = gettid();
248 flockfile(stdout);
249 funlockfile(stdout);
250 });
Yabin Cui76144aa2015-11-19 13:52:16 -0800251 WaitUntilThreadSleep(pid);
252 funlockfile(stdout);
253
Elliott Hughes194a6302025-02-27 15:28:22 -0800254 thread.join();
Yabin Cui76144aa2015-11-19 13:52:16 -0800255 __fsetlocking(stdout, old_state);
256}