• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <thread>
33 
34 #include <android-base/file.h>
35 
36 #include "utils.h"
37 
TEST(stdio_ext,__fbufsize)38 TEST(stdio_ext, __fbufsize) {
39   FILE* fp = fopen("/proc/version", "r");
40 
41   // 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.
45   char buf[128];
46   fgets(buf, sizeof(buf), fp);
47   ASSERT_EQ(1024U, __fbufsize(fp));
48 
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 
TEST(stdio_ext,__flbf)58 TEST(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 
TEST(stdio_ext,__fpending)71 TEST(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 
TEST(stdio_ext,__freadahead)83 TEST(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 
TEST(stdio_ext,__fpurge)101 TEST(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);
117   ASSERT_TRUE(s != nullptr);
118   ASSERT_STREQ("b\n", s);
119 
120   fclose(fp);
121 }
122 
TEST(stdio_ext,_flushlbf)123 TEST(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 
TEST(stdio_ext,__freadable__fwritable)139 TEST(stdio_ext, __freadable__fwritable) {
140   FILE* fp;
141 
142   // Read-only.
143   fp = fopen("/dev/null", "r");
144   ASSERT_TRUE(__freadable(fp));
145   ASSERT_FALSE(__fwritable(fp));
146   fclose(fp);
147 
148   // Write-only.
149   fp = fopen("/dev/null", "w");
150   ASSERT_FALSE(__freadable(fp));
151   ASSERT_TRUE(__fwritable(fp));
152   fclose(fp);
153 
154   // Append (aka write-only).
155   fp = fopen("/dev/null", "a");
156   ASSERT_FALSE(__freadable(fp));
157   ASSERT_TRUE(__fwritable(fp));
158   fclose(fp);
159 
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 
TEST(stdio_ext,__freading__fwriting)169 TEST(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   }
212 }
213 
TEST(stdio_ext,__fseterr)214 TEST(stdio_ext, __fseterr) {
215 #if defined(__GLIBC__)
216   GTEST_SKIP() << "glibc doesn't have __fseterr, but gnulib will use it";
217 #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 
TEST(stdio_ext,__fsetlocking)230 TEST(stdio_ext, __fsetlocking) {
231   FILE* fp = fopen("/proc/version", "r");
232   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));
236   ASSERT_EQ(FSETLOCKING_INTERNAL, __fsetlocking(fp, FSETLOCKING_QUERY));
237   fclose(fp);
238 }
239 
TEST(stdio_ext,__fsetlocking_BYCALLER)240 TEST(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);
244 
245   std::atomic<pid_t> pid(0);
246   std::thread thread([&]() {
247     pid = gettid();
248     flockfile(stdout);
249     funlockfile(stdout);
250   });
251   WaitUntilThreadSleep(pid);
252   funlockfile(stdout);
253 
254   thread.join();
255   __fsetlocking(stdout, old_state);
256 }
257