blob: dce1a662f89109739df8442fc03b08c51654cd7f [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
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -080032#include <android-base/file.h>
33
Yabin Cui76144aa2015-11-19 13:52:16 -080034#include "utils.h"
Elliott Hughes2b021e12014-08-19 17:00:33 -070035
36TEST(stdio_ext, __fbufsize) {
37 FILE* fp = fopen("/proc/version", "r");
38
Elliott Hughesdf8f1a42015-01-16 16:40:55 -080039 // Initially, there's no buffer in case the first thing you do is disable buffering.
40 ASSERT_EQ(0U, __fbufsize(fp));
41
42 // A read forces a buffer to be created.
Elliott Hughes2b021e12014-08-19 17:00:33 -070043 char buf[128];
Elliott Hughesdf8f1a42015-01-16 16:40:55 -080044 fgets(buf, sizeof(buf), fp);
45 ASSERT_EQ(1024U, __fbufsize(fp));
Elliott Hughes2b021e12014-08-19 17:00:33 -070046
47 ASSERT_EQ(0, setvbuf(fp, buf, _IOFBF, 1));
48 ASSERT_EQ(1U, __fbufsize(fp));
49
50 ASSERT_EQ(0, setvbuf(fp, buf, _IOFBF, 8));
51 ASSERT_EQ(8U, __fbufsize(fp));
52
53 fclose(fp);
54}
55
56TEST(stdio_ext, __flbf) {
57 FILE* fp = fopen("/proc/version", "r");
58
59 ASSERT_FALSE(__flbf(fp));
60
61 char buf[128];
62 ASSERT_EQ(0, setvbuf(fp, buf, _IOLBF, sizeof(buf)));
63
64 ASSERT_TRUE(__flbf(fp));
65
66 fclose(fp);
67}
68
69TEST(stdio_ext, __fpending) {
70 FILE* fp = fopen("/dev/null", "w");
71 ASSERT_EQ(0U, __fpending(fp));
72 ASSERT_EQ('x', fputc('x', fp));
73 ASSERT_EQ(1U, __fpending(fp));
74 ASSERT_EQ('y', fputc('y', fp));
75 ASSERT_EQ(2U, __fpending(fp));
76 fflush(fp);
77 ASSERT_EQ(0U, __fpending(fp));
78 fclose(fp);
79}
80
Elliott Hughes82be76b2022-09-22 23:47:42 +000081TEST(stdio_ext, __freadahead) {
82#if defined(__GLIBC__)
83 GTEST_SKIP() << "glibc doesn't have __freadahead";
84#else
85 FILE* fp = tmpfile();
86 ASSERT_NE(EOF, fputs("hello", fp));
87 rewind(fp);
88
89 ASSERT_EQ('h', fgetc(fp));
90 ASSERT_EQ(4u, __freadahead(fp));
91
92 ASSERT_EQ('H', ungetc('H', fp));
93 ASSERT_EQ(5u, __freadahead(fp));
94
95 fclose(fp);
96#endif
97}
98
Elliott Hughes2b021e12014-08-19 17:00:33 -070099TEST(stdio_ext, __fpurge) {
100 FILE* fp = tmpfile();
101
102 ASSERT_EQ('a', fputc('a', fp));
103 ASSERT_EQ(1U, __fpending(fp));
104 __fpurge(fp);
105 ASSERT_EQ(0U, __fpending(fp));
106
107 ASSERT_EQ('b', fputc('b', fp));
108 ASSERT_EQ('\n', fputc('\n', fp));
109 ASSERT_EQ(2U, __fpending(fp));
110
111 rewind(fp);
112
113 char buf[16];
114 char* s = fgets(buf, sizeof(buf), fp);
Yi Kong32bc0fc2018-08-02 17:31:13 -0700115 ASSERT_TRUE(s != nullptr);
Elliott Hughes2b021e12014-08-19 17:00:33 -0700116 ASSERT_STREQ("b\n", s);
117
118 fclose(fp);
119}
120
121TEST(stdio_ext, _flushlbf) {
122 FILE* fp = fopen("/dev/null", "w");
123
124 char buf[128];
125 ASSERT_EQ(0, setvbuf(fp, buf, _IOLBF, sizeof(buf)));
126
127 ASSERT_EQ('a', fputc('a', fp));
128 ASSERT_EQ(1U, __fpending(fp));
129
130 _flushlbf();
131
132 ASSERT_EQ(0U, __fpending(fp));
133
134 fclose(fp);
135}
136
137TEST(stdio_ext, __freadable__fwritable) {
Elliott Hughes5ba2c212017-08-01 15:16:36 -0700138 FILE* fp;
139
140 // Read-only.
141 fp = fopen("/dev/null", "r");
Elliott Hughes2b021e12014-08-19 17:00:33 -0700142 ASSERT_TRUE(__freadable(fp));
143 ASSERT_FALSE(__fwritable(fp));
144 fclose(fp);
145
Elliott Hughes5ba2c212017-08-01 15:16:36 -0700146 // Write-only.
Elliott Hughes2b021e12014-08-19 17:00:33 -0700147 fp = fopen("/dev/null", "w");
148 ASSERT_FALSE(__freadable(fp));
149 ASSERT_TRUE(__fwritable(fp));
150 fclose(fp);
151
Elliott Hughes5ba2c212017-08-01 15:16:36 -0700152 // Append (aka write-only).
153 fp = fopen("/dev/null", "a");
154 ASSERT_FALSE(__freadable(fp));
Elliott Hughes2b021e12014-08-19 17:00:33 -0700155 ASSERT_TRUE(__fwritable(fp));
156 fclose(fp);
Elliott Hughes5ba2c212017-08-01 15:16:36 -0700157
158 // The three read-write modes.
159 for (auto read_write_mode : {"r+", "w+", "a+"}) {
160 fp = fopen("/dev/null", read_write_mode);
161 ASSERT_TRUE(__freadable(fp));
162 ASSERT_TRUE(__fwritable(fp));
163 fclose(fp);
164 }
165}
166
167TEST(stdio_ext, __freading__fwriting) {
168 FILE* fp;
169
170 // Append (aka write-only). Never reading. Always writing.
171 fp = fopen("/dev/zero", "a");
172 ASSERT_FALSE(__freading(fp)); // Not reading initially.
173 ASSERT_TRUE(__fwriting(fp)); // Writing initially.
174 ASSERT_TRUE(fputc('x', fp) != EOF);
175 ASSERT_FALSE(__freading(fp)); // Not reading after write.
176 ASSERT_TRUE(__fwriting(fp)); // Still writing after write.
177 fclose(fp);
178
179 // Write-only. Never reading. Always writing.
180 fp = fopen("/dev/zero", "w");
181 ASSERT_FALSE(__freading(fp)); // Not reading initially.
182 ASSERT_TRUE(__fwriting(fp)); // Writing initially.
183 ASSERT_TRUE(fputc('x', fp) != EOF);
184 ASSERT_FALSE(__freading(fp)); // Not reading after write.
185 ASSERT_TRUE(__fwriting(fp)); // Still writing after write.
186 fclose(fp);
187
188 // Read-only. Always reading. Never writing.
189 fp = fopen("/dev/zero", "r");
190 ASSERT_TRUE(__freading(fp)); // Reading initially.
191 ASSERT_FALSE(__fwriting(fp)); // Not writing initially.
192 ASSERT_TRUE(fgetc(fp) == 0);
193 ASSERT_TRUE(__freading(fp)); // Still reading after read.
194 ASSERT_FALSE(__fwriting(fp)); // Still not writing after read.
195 fclose(fp);
196
197 // The three read-write modes.
198 for (auto read_write_mode : {"r+", "w+", "a+"}) {
199 fp = fopen("/dev/zero", read_write_mode);
200 ASSERT_FALSE(__freading(fp)); // Not reading initially.
201 ASSERT_FALSE(__fwriting(fp)); // Not writing initially.
202 ASSERT_TRUE(fgetc(fp) == 0);
203 ASSERT_TRUE(__freading(fp)); // Reading after read.
204 ASSERT_FALSE(__fwriting(fp)); // Not writing after read.
205 ASSERT_TRUE(fputc('x', fp) != EOF);
206 ASSERT_FALSE(__freading(fp)); // Not reading after write.
207 ASSERT_TRUE(__fwriting(fp)); // Writing after write.
208 fclose(fp);
209 }
Elliott Hughes2b021e12014-08-19 17:00:33 -0700210}
211
Elliott Hughes45785262018-02-14 15:21:45 -0800212TEST(stdio_ext, __fseterr) {
213#if defined(__GLIBC__)
Elliott Hughesbcaa4542019-03-08 15:20:23 -0800214 GTEST_SKIP() << "glibc doesn't have __fseterr, but gnulib will use it";
Elliott Hughes45785262018-02-14 15:21:45 -0800215#else
216 FILE* fp = fopen("/dev/null", "w");
217
218 ASSERT_FALSE(ferror(fp));
219 __fseterr(fp);
220 ASSERT_TRUE(ferror(fp));
221 clearerr(fp);
222 ASSERT_FALSE(ferror(fp));
223
224 fclose(fp);
225#endif
226}
227
Elliott Hughes2b021e12014-08-19 17:00:33 -0700228TEST(stdio_ext, __fsetlocking) {
229 FILE* fp = fopen("/proc/version", "r");
Elliott Hughes8c4994b2015-01-20 18:09:05 -0800230 ASSERT_EQ(FSETLOCKING_INTERNAL, __fsetlocking(fp, FSETLOCKING_QUERY));
231 ASSERT_EQ(FSETLOCKING_INTERNAL, __fsetlocking(fp, FSETLOCKING_BYCALLER));
232 ASSERT_EQ(FSETLOCKING_BYCALLER, __fsetlocking(fp, FSETLOCKING_QUERY));
233 ASSERT_EQ(FSETLOCKING_BYCALLER, __fsetlocking(fp, FSETLOCKING_INTERNAL));
Elliott Hughes2b021e12014-08-19 17:00:33 -0700234 ASSERT_EQ(FSETLOCKING_INTERNAL, __fsetlocking(fp, FSETLOCKING_QUERY));
235 fclose(fp);
236}
Yabin Cui76144aa2015-11-19 13:52:16 -0800237
238static void LockingByCallerHelper(std::atomic<pid_t>* pid) {
239 *pid = gettid();
240 flockfile(stdout);
241 funlockfile(stdout);
242}
243
244TEST(stdio_ext, __fsetlocking_BYCALLER) {
245 // Check if users can use flockfile/funlockfile to protect stdio operations.
246 int old_state = __fsetlocking(stdout, FSETLOCKING_BYCALLER);
247 flockfile(stdout);
248 pthread_t thread;
249 std::atomic<pid_t> pid(0);
250 ASSERT_EQ(0, pthread_create(&thread, nullptr,
251 reinterpret_cast<void* (*)(void*)>(LockingByCallerHelper), &pid));
252 WaitUntilThreadSleep(pid);
253 funlockfile(stdout);
254
255 ASSERT_EQ(0, pthread_join(thread, nullptr));
256 __fsetlocking(stdout, old_state);
257}