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 <android-base/file.h>
33 
34 #include "utils.h"
35 
TEST(stdio_ext,__fbufsize)36 TEST(stdio_ext, __fbufsize) {
37   FILE* fp = fopen("/proc/version", "r");
38 
39   // 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.
43   char buf[128];
44   fgets(buf, sizeof(buf), fp);
45   ASSERT_EQ(1024U, __fbufsize(fp));
46 
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 
TEST(stdio_ext,__flbf)56 TEST(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 
TEST(stdio_ext,__fpending)69 TEST(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 
TEST(stdio_ext,__freadahead)81 TEST(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 
TEST(stdio_ext,__fpurge)99 TEST(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);
115   ASSERT_TRUE(s != nullptr);
116   ASSERT_STREQ("b\n", s);
117 
118   fclose(fp);
119 }
120 
TEST(stdio_ext,_flushlbf)121 TEST(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 
TEST(stdio_ext,__freadable__fwritable)137 TEST(stdio_ext, __freadable__fwritable) {
138   FILE* fp;
139 
140   // Read-only.
141   fp = fopen("/dev/null", "r");
142   ASSERT_TRUE(__freadable(fp));
143   ASSERT_FALSE(__fwritable(fp));
144   fclose(fp);
145 
146   // Write-only.
147   fp = fopen("/dev/null", "w");
148   ASSERT_FALSE(__freadable(fp));
149   ASSERT_TRUE(__fwritable(fp));
150   fclose(fp);
151 
152   // Append (aka write-only).
153   fp = fopen("/dev/null", "a");
154   ASSERT_FALSE(__freadable(fp));
155   ASSERT_TRUE(__fwritable(fp));
156   fclose(fp);
157 
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 
TEST(stdio_ext,__freading__fwriting)167 TEST(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   }
210 }
211 
TEST(stdio_ext,__fseterr)212 TEST(stdio_ext, __fseterr) {
213 #if defined(__GLIBC__)
214   GTEST_SKIP() << "glibc doesn't have __fseterr, but gnulib will use it";
215 #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 
TEST(stdio_ext,__fsetlocking)228 TEST(stdio_ext, __fsetlocking) {
229   FILE* fp = fopen("/proc/version", "r");
230   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));
234   ASSERT_EQ(FSETLOCKING_INTERNAL, __fsetlocking(fp, FSETLOCKING_QUERY));
235   fclose(fp);
236 }
237 
LockingByCallerHelper(std::atomic<pid_t> * pid)238 static void LockingByCallerHelper(std::atomic<pid_t>* pid) {
239   *pid = gettid();
240   flockfile(stdout);
241   funlockfile(stdout);
242 }
243 
TEST(stdio_ext,__fsetlocking_BYCALLER)244 TEST(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 }
258