blob: 9d840f531418b600ef7e0e49421ba148817feb09 [file] [log] [blame]
Elliott Hughes91875dc2012-09-24 17:55:15 -07001/*
2 * Copyright (C) 2012 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 <gtest/gtest.h>
18
19#include <errno.h>
Elliott Hughesc9244bd2014-05-14 13:31:35 -070020#include <fcntl.h>
Elliott Hughes1d13c642013-09-23 16:02:39 -070021#include <limits.h>
Elliott Hughes7823f322014-04-14 12:11:28 -070022#include <math.h>
Elliott Hughes91875dc2012-09-24 17:55:15 -070023#include <stdio.h>
24#include <sys/types.h>
Elliott Hughes468efc82018-07-10 14:39:49 -070025#include <sys/socket.h>
Elliott Hughes91875dc2012-09-24 17:55:15 -070026#include <sys/stat.h>
27#include <unistd.h>
Elliott Hughes05493712014-04-17 17:30:03 -070028#include <wchar.h>
Calin Juravle03e4ebe2014-05-08 14:42:06 +010029#include <locale.h>
30
Elliott Hughes3a4c4542017-07-19 17:20:24 -070031#include <string>
Ryan Prichardc485cdb2019-04-30 14:47:34 -070032#include <thread>
Elliott Hughese6bb5a22015-01-23 17:48:15 -080033#include <vector>
34
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -080035#include <android-base/file.h>
Elliott Hughes141b9172021-04-09 17:13:09 -070036#include <android-base/silent_death_test.h>
Elliott Hughes439ebbd2020-12-04 18:51:42 -080037#include <android-base/test_utils.h>
Elliott Hughes05b675e2019-04-17 13:01:06 -070038#include <android-base/unique_fd.h>
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -080039
Josh Gao2f06e102017-01-10 13:00:37 -080040#include "utils.h"
Elliott Hughes91875dc2012-09-24 17:55:15 -070041
Elliott Hughes05b675e2019-04-17 13:01:06 -070042// This #include is actually a test too. We have to duplicate the
43// definitions of the RENAME_ constants because <linux/fs.h> also contains
44// pollution such as BLOCK_SIZE which conflicts with lots of user code.
45// Important to check that we have matching definitions.
46// There's no _MAX to test that we have all the constants, sadly.
47#include <linux/fs.h>
48
Christopher Ferris13f26a72016-01-13 13:47:58 -080049#if defined(NOFORTIFY)
50#define STDIO_TEST stdio_nofortify
Elliott Hughesfb3873d2016-08-10 11:07:54 -070051#define STDIO_DEATHTEST stdio_nofortify_DeathTest
Christopher Ferris13f26a72016-01-13 13:47:58 -080052#else
53#define STDIO_TEST stdio
Elliott Hughesfb3873d2016-08-10 11:07:54 -070054#define STDIO_DEATHTEST stdio_DeathTest
Christopher Ferris13f26a72016-01-13 13:47:58 -080055#endif
56
Elliott Hughes3a4c4542017-07-19 17:20:24 -070057using namespace std::string_literals;
58
Elliott Hughes141b9172021-04-09 17:13:09 -070059using stdio_DeathTest = SilentDeathTest;
60using stdio_nofortify_DeathTest = SilentDeathTest;
Elliott Hughesfb3873d2016-08-10 11:07:54 -070061
Elliott Hughes33a8cb12017-07-25 18:06:46 -070062static void SetFileTo(const char* path, const char* content) {
63 FILE* fp;
64 ASSERT_NE(nullptr, fp = fopen(path, "w"));
65 ASSERT_NE(EOF, fputs(content, fp));
66 ASSERT_EQ(0, fclose(fp));
67}
68
69static void AssertFileIs(const char* path, const char* expected) {
70 FILE* fp;
71 ASSERT_NE(nullptr, fp = fopen(path, "r"));
72 char* line = nullptr;
73 size_t length;
74 ASSERT_NE(EOF, getline(&line, &length, fp));
75 ASSERT_EQ(0, fclose(fp));
76 ASSERT_STREQ(expected, line);
77 free(line);
78}
79
Elliott Hughes70715da2016-08-01 16:35:17 -070080static void AssertFileIs(FILE* fp, const char* expected, bool is_fmemopen = false) {
81 rewind(fp);
82
83 char line[1024];
Josh Gao2f06e102017-01-10 13:00:37 -080084 memset(line, 0xff, sizeof(line));
Elliott Hughes70715da2016-08-01 16:35:17 -070085 ASSERT_EQ(line, fgets(line, sizeof(line), fp));
86 ASSERT_STREQ(expected, line);
87
88 if (is_fmemopen) {
89 // fmemopen appends a trailing NUL byte, which probably shouldn't show up as an
90 // extra empty line, but does on every C library I tested...
91 ASSERT_EQ(line, fgets(line, sizeof(line), fp));
92 ASSERT_STREQ("", line);
93 }
94
95 // Make sure there isn't anything else in the file.
96 ASSERT_EQ(nullptr, fgets(line, sizeof(line), fp)) << "junk at end of file: " << line;
97}
98
Christopher Ferris13f26a72016-01-13 13:47:58 -080099TEST(STDIO_TEST, flockfile_18208568_stderr) {
Elliott Hughes6a03abc2014-11-03 12:32:17 -0800100 // Check that we have a _recursive_ mutex for flockfile.
101 flockfile(stderr);
102 feof(stderr); // We don't care about the result, but this needs to take the lock.
103 funlockfile(stderr);
104}
105
Christopher Ferris13f26a72016-01-13 13:47:58 -0800106TEST(STDIO_TEST, flockfile_18208568_regular) {
Elliott Hughes6a03abc2014-11-03 12:32:17 -0800107 // We never had a bug for streams other than stdin/stdout/stderr, but test anyway.
108 FILE* fp = fopen("/dev/null", "w");
Yi Kong32bc0fc2018-08-02 17:31:13 -0700109 ASSERT_TRUE(fp != nullptr);
Elliott Hughes6a03abc2014-11-03 12:32:17 -0800110 flockfile(fp);
111 feof(fp);
112 funlockfile(fp);
113 fclose(fp);
114}
115
Christopher Ferris13f26a72016-01-13 13:47:58 -0800116TEST(STDIO_TEST, tmpfile_fileno_fprintf_rewind_fgets) {
Elliott Hughes91875dc2012-09-24 17:55:15 -0700117 FILE* fp = tmpfile();
Yi Kong32bc0fc2018-08-02 17:31:13 -0700118 ASSERT_TRUE(fp != nullptr);
Elliott Hughes91875dc2012-09-24 17:55:15 -0700119
120 int fd = fileno(fp);
121 ASSERT_NE(fd, -1);
122
123 struct stat sb;
124 int rc = fstat(fd, &sb);
125 ASSERT_NE(rc, -1);
126 ASSERT_EQ(sb.st_mode & 0777, 0600U);
127
128 rc = fprintf(fp, "hello\n");
129 ASSERT_EQ(rc, 6);
130
Elliott Hughes70715da2016-08-01 16:35:17 -0700131 AssertFileIs(fp, "hello\n");
Elliott Hughes91875dc2012-09-24 17:55:15 -0700132 fclose(fp);
133}
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300134
Elliott Hughesf226ee52016-02-03 11:24:28 -0800135TEST(STDIO_TEST, tmpfile64) {
136 FILE* fp = tmpfile64();
137 ASSERT_TRUE(fp != nullptr);
138 fclose(fp);
139}
140
Christopher Ferris13f26a72016-01-13 13:47:58 -0800141TEST(STDIO_TEST, dprintf) {
Calin Juravle6afb2a92014-05-22 11:47:47 +0100142 TemporaryFile tf;
143
144 int rc = dprintf(tf.fd, "hello\n");
145 ASSERT_EQ(rc, 6);
146
Yabin Cui5ca4a9e2014-11-06 19:55:09 -0800147 lseek(tf.fd, 0, SEEK_SET);
Christopher Ferris9e01ea62014-05-29 12:49:35 -0700148 FILE* tfile = fdopen(tf.fd, "r");
Yi Kong32bc0fc2018-08-02 17:31:13 -0700149 ASSERT_TRUE(tfile != nullptr);
Calin Juravle6afb2a92014-05-22 11:47:47 +0100150
Elliott Hughes70715da2016-08-01 16:35:17 -0700151 AssertFileIs(tfile, "hello\n");
Christopher Ferris9e01ea62014-05-29 12:49:35 -0700152 fclose(tfile);
Calin Juravle6afb2a92014-05-22 11:47:47 +0100153}
154
Christopher Ferris13f26a72016-01-13 13:47:58 -0800155TEST(STDIO_TEST, getdelim) {
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300156 FILE* fp = tmpfile();
Yi Kong32bc0fc2018-08-02 17:31:13 -0700157 ASSERT_TRUE(fp != nullptr);
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300158
159 const char* line_written = "This is a test";
160 int rc = fprintf(fp, "%s", line_written);
161 ASSERT_EQ(rc, static_cast<int>(strlen(line_written)));
162
163 rewind(fp);
164
Yi Kong32bc0fc2018-08-02 17:31:13 -0700165 char* word_read = nullptr;
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300166 size_t allocated_length = 0;
167
168 const char* expected[] = { "This ", " ", "is ", "a ", "test" };
169 for (size_t i = 0; i < 5; ++i) {
170 ASSERT_FALSE(feof(fp));
171 ASSERT_EQ(getdelim(&word_read, &allocated_length, ' ', fp), static_cast<int>(strlen(expected[i])));
172 ASSERT_GE(allocated_length, strlen(expected[i]));
Elliott Hughes0ed7e082015-01-22 15:13:38 -0800173 ASSERT_STREQ(expected[i], word_read);
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300174 }
175 // The last read should have set the end-of-file indicator for the stream.
176 ASSERT_TRUE(feof(fp));
177 clearerr(fp);
178
179 // getdelim returns -1 but doesn't set errno if we're already at EOF.
180 // It should set the end-of-file indicator for the stream, though.
181 errno = 0;
182 ASSERT_EQ(getdelim(&word_read, &allocated_length, ' ', fp), -1);
Elliott Hughes5e3fc432013-02-11 16:36:48 -0800183 ASSERT_EQ(0, errno);
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300184 ASSERT_TRUE(feof(fp));
185
186 free(word_read);
187 fclose(fp);
188}
189
Christopher Ferris13f26a72016-01-13 13:47:58 -0800190TEST(STDIO_TEST, getdelim_invalid) {
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300191 FILE* fp = tmpfile();
Yi Kong32bc0fc2018-08-02 17:31:13 -0700192 ASSERT_TRUE(fp != nullptr);
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300193
Yi Kong32bc0fc2018-08-02 17:31:13 -0700194 char* buffer = nullptr;
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300195 size_t buffer_length = 0;
196
197 // The first argument can't be NULL.
198 errno = 0;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700199 ASSERT_EQ(getdelim(nullptr, &buffer_length, ' ', fp), -1);
Elliott Hughes5e3fc432013-02-11 16:36:48 -0800200 ASSERT_EQ(EINVAL, errno);
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300201
202 // The second argument can't be NULL.
203 errno = 0;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700204 ASSERT_EQ(getdelim(&buffer, nullptr, ' ', fp), -1);
Elliott Hughes5e3fc432013-02-11 16:36:48 -0800205 ASSERT_EQ(EINVAL, errno);
Elliott Hughes20f8aec2014-05-12 15:15:37 -0700206 fclose(fp);
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300207}
208
Christopher Ferris13f26a72016-01-13 13:47:58 -0800209TEST(STDIO_TEST, getdelim_directory) {
Elliott Hughes694fd2d2015-04-05 10:51:56 -0700210 FILE* fp = fopen("/proc", "r");
Yi Kong32bc0fc2018-08-02 17:31:13 -0700211 ASSERT_TRUE(fp != nullptr);
Elliott Hughes694fd2d2015-04-05 10:51:56 -0700212 char* word_read;
213 size_t allocated_length;
214 ASSERT_EQ(-1, getdelim(&word_read, &allocated_length, ' ', fp));
215 fclose(fp);
216}
217
Christopher Ferris13f26a72016-01-13 13:47:58 -0800218TEST(STDIO_TEST, getline) {
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300219 FILE* fp = tmpfile();
Yi Kong32bc0fc2018-08-02 17:31:13 -0700220 ASSERT_TRUE(fp != nullptr);
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300221
222 const char* line_written = "This is a test for getline\n";
223 const size_t line_count = 5;
224
225 for (size_t i = 0; i < line_count; ++i) {
226 int rc = fprintf(fp, "%s", line_written);
227 ASSERT_EQ(rc, static_cast<int>(strlen(line_written)));
228 }
229
230 rewind(fp);
231
Yi Kong32bc0fc2018-08-02 17:31:13 -0700232 char* line_read = nullptr;
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300233 size_t allocated_length = 0;
234
235 size_t read_line_count = 0;
236 ssize_t read_char_count;
237 while ((read_char_count = getline(&line_read, &allocated_length, fp)) != -1) {
238 ASSERT_EQ(read_char_count, static_cast<int>(strlen(line_written)));
239 ASSERT_GE(allocated_length, strlen(line_written));
Elliott Hughes0ed7e082015-01-22 15:13:38 -0800240 ASSERT_STREQ(line_written, line_read);
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300241 ++read_line_count;
242 }
243 ASSERT_EQ(read_line_count, line_count);
244
245 // The last read should have set the end-of-file indicator for the stream.
246 ASSERT_TRUE(feof(fp));
247 clearerr(fp);
248
249 // getline returns -1 but doesn't set errno if we're already at EOF.
250 // It should set the end-of-file indicator for the stream, though.
251 errno = 0;
252 ASSERT_EQ(getline(&line_read, &allocated_length, fp), -1);
Elliott Hughes5e3fc432013-02-11 16:36:48 -0800253 ASSERT_EQ(0, errno);
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300254 ASSERT_TRUE(feof(fp));
255
256 free(line_read);
257 fclose(fp);
258}
259
Christopher Ferris13f26a72016-01-13 13:47:58 -0800260TEST(STDIO_TEST, getline_invalid) {
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300261 FILE* fp = tmpfile();
Yi Kong32bc0fc2018-08-02 17:31:13 -0700262 ASSERT_TRUE(fp != nullptr);
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300263
Yi Kong32bc0fc2018-08-02 17:31:13 -0700264 char* buffer = nullptr;
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300265 size_t buffer_length = 0;
266
267 // The first argument can't be NULL.
268 errno = 0;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700269 ASSERT_EQ(getline(nullptr, &buffer_length, fp), -1);
Elliott Hughes5e3fc432013-02-11 16:36:48 -0800270 ASSERT_EQ(EINVAL, errno);
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300271
272 // The second argument can't be NULL.
273 errno = 0;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700274 ASSERT_EQ(getline(&buffer, nullptr, fp), -1);
Elliott Hughes5e3fc432013-02-11 16:36:48 -0800275 ASSERT_EQ(EINVAL, errno);
Elliott Hughes20f8aec2014-05-12 15:15:37 -0700276 fclose(fp);
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300277}
Thorsten Glaserc641caf2013-02-17 16:50:58 +0000278
Christopher Ferris13f26a72016-01-13 13:47:58 -0800279TEST(STDIO_TEST, printf_ssize_t) {
Elliott Hughese2556422013-02-28 10:51:14 -0800280 // http://b/8253769
Elliott Hughese2556422013-02-28 10:51:14 -0800281 ASSERT_EQ(sizeof(ssize_t), sizeof(long int));
Elliott Hughesb6e22482013-03-08 15:28:52 -0800282 ASSERT_EQ(sizeof(ssize_t), sizeof(size_t));
283 // For our 32-bit ABI, we had a ssize_t definition that confuses GCC into saying:
Thorsten Glaserc641caf2013-02-17 16:50:58 +0000284 // error: format '%zd' expects argument of type 'signed size_t',
285 // but argument 4 has type 'ssize_t {aka long int}' [-Werror=format]
286 ssize_t v = 1;
287 char buf[32];
288 snprintf(buf, sizeof(buf), "%zd", v);
289}
Elliott Hughes6b3f49a2013-03-06 16:20:55 -0800290
Elliott Hughes05493712014-04-17 17:30:03 -0700291// https://code.google.com/p/android/issues/detail?id=64886
Christopher Ferris13f26a72016-01-13 13:47:58 -0800292TEST(STDIO_TEST, snprintf_a) {
Elliott Hughes05493712014-04-17 17:30:03 -0700293 char buf[BUFSIZ];
294 EXPECT_EQ(23, snprintf(buf, sizeof(buf), "<%a>", 9990.235));
295 EXPECT_STREQ("<0x1.3831e147ae148p+13>", buf);
296}
297
Elliott Hughescdb4a262020-06-05 16:56:53 -0700298// http://b/152588929
299TEST(STDIO_TEST, snprintf_La) {
300#if defined(__LP64__)
301 char buf[BUFSIZ];
302 union {
303 uint64_t a[2];
304 long double v;
305 } u;
306
307 u.a[0] = UINT64_C(0x9b9b9b9b9b9b9b9b);
308 u.a[1] = UINT64_C(0xdfdfdfdfdfdfdfdf);
309 EXPECT_EQ(41, snprintf(buf, sizeof(buf), "<%La>", u.v));
310 EXPECT_STREQ("<-0x1.dfdfdfdfdfdf9b9b9b9b9b9b9b9bp+8160>", buf);
311
312 u.a[0] = UINT64_C(0xffffffffffffffff);
313 u.a[1] = UINT64_C(0x7ffeffffffffffff);
314 EXPECT_EQ(41, snprintf(buf, sizeof(buf), "<%La>", u.v));
315 EXPECT_STREQ("<0x1.ffffffffffffffffffffffffffffp+16383>", buf);
316
317 u.a[0] = UINT64_C(0x0000000000000000);
318 u.a[1] = UINT64_C(0x0000000000000000);
319 EXPECT_EQ(8, snprintf(buf, sizeof(buf), "<%La>", u.v));
320 EXPECT_STREQ("<0x0p+0>", buf);
321#else
322 GTEST_SKIP() << "no ld128";
323#endif
324}
325
Christopher Ferris13f26a72016-01-13 13:47:58 -0800326TEST(STDIO_TEST, snprintf_lc) {
Elliott Hughes05493712014-04-17 17:30:03 -0700327 char buf[BUFSIZ];
328 wint_t wc = L'a';
329 EXPECT_EQ(3, snprintf(buf, sizeof(buf), "<%lc>", wc));
330 EXPECT_STREQ("<a>", buf);
331}
332
Elliott Hughes2f9c8ce2017-11-01 13:54:47 -0700333TEST(STDIO_TEST, snprintf_C) { // Synonym for %lc.
334 char buf[BUFSIZ];
335 wchar_t wc = L'a';
336 EXPECT_EQ(3, snprintf(buf, sizeof(buf), "<%C>", wc));
337 EXPECT_STREQ("<a>", buf);
338}
339
Christopher Ferris13f26a72016-01-13 13:47:58 -0800340TEST(STDIO_TEST, snprintf_ls) {
Elliott Hughes05493712014-04-17 17:30:03 -0700341 char buf[BUFSIZ];
Yi Kong32bc0fc2018-08-02 17:31:13 -0700342 wchar_t* ws = nullptr;
Elliott Hughes05493712014-04-17 17:30:03 -0700343 EXPECT_EQ(8, snprintf(buf, sizeof(buf), "<%ls>", ws));
344 EXPECT_STREQ("<(null)>", buf);
345
346 wchar_t chars[] = { L'h', L'i', 0 };
347 ws = chars;
348 EXPECT_EQ(4, snprintf(buf, sizeof(buf), "<%ls>", ws));
349 EXPECT_STREQ("<hi>", buf);
350}
351
Elliott Hughes2f9c8ce2017-11-01 13:54:47 -0700352TEST(STDIO_TEST, snprintf_S) { // Synonym for %ls.
353 char buf[BUFSIZ];
Yi Kong32bc0fc2018-08-02 17:31:13 -0700354 wchar_t* ws = nullptr;
Elliott Hughes2f9c8ce2017-11-01 13:54:47 -0700355 EXPECT_EQ(8, snprintf(buf, sizeof(buf), "<%S>", ws));
356 EXPECT_STREQ("<(null)>", buf);
357
358 wchar_t chars[] = { L'h', L'i', 0 };
359 ws = chars;
360 EXPECT_EQ(4, snprintf(buf, sizeof(buf), "<%S>", ws));
361 EXPECT_STREQ("<hi>", buf);
362}
363
Elliott Hughese657eb42021-02-18 17:11:56 -0800364TEST_F(STDIO_DEATHTEST, snprintf_n) {
Elliott Hughes063525c2014-05-13 11:19:57 -0700365#if defined(__BIONIC__)
Elliott Hughes41398d02018-03-07 13:32:58 -0800366 // http://b/14492135 and http://b/31832608.
Elliott Hughes7248a2d2013-09-24 18:01:33 -0700367 char buf[32];
Elliott Hughese2341d02014-05-02 18:16:32 -0700368 int i = 1234;
Elliott Hughes41398d02018-03-07 13:32:58 -0800369 EXPECT_DEATH(snprintf(buf, sizeof(buf), "a %n b", &i), "%n not allowed on Android");
Elliott Hughese2341d02014-05-02 18:16:32 -0700370#else
Elliott Hughesbcaa4542019-03-08 15:20:23 -0800371 GTEST_SKIP() << "glibc does allow %n";
Elliott Hughese2341d02014-05-02 18:16:32 -0700372#endif
Elliott Hughes7248a2d2013-09-24 18:01:33 -0700373}
Elliott Hughes7248a2d2013-09-24 18:01:33 -0700374
Elliott Hughes7cebf832020-08-12 14:25:41 -0700375TEST(STDIO_TEST, snprintf_measure) {
376 char buf[16];
377 ASSERT_EQ(11, snprintf(buf, 0, "Hello %s", "world"));
378}
379
Christopher Ferris13f26a72016-01-13 13:47:58 -0800380TEST(STDIO_TEST, snprintf_smoke) {
Elliott Hughes1d13c642013-09-23 16:02:39 -0700381 char buf[BUFSIZ];
382
383 snprintf(buf, sizeof(buf), "a");
384 EXPECT_STREQ("a", buf);
385
386 snprintf(buf, sizeof(buf), "%%");
387 EXPECT_STREQ("%", buf);
388
389 snprintf(buf, sizeof(buf), "01234");
390 EXPECT_STREQ("01234", buf);
391
392 snprintf(buf, sizeof(buf), "a%sb", "01234");
393 EXPECT_STREQ("a01234b", buf);
394
Yi Kong32bc0fc2018-08-02 17:31:13 -0700395 char* s = nullptr;
Elliott Hughes1d13c642013-09-23 16:02:39 -0700396 snprintf(buf, sizeof(buf), "a%sb", s);
397 EXPECT_STREQ("a(null)b", buf);
398
399 snprintf(buf, sizeof(buf), "aa%scc", "bb");
400 EXPECT_STREQ("aabbcc", buf);
401
402 snprintf(buf, sizeof(buf), "a%cc", 'b');
403 EXPECT_STREQ("abc", buf);
404
405 snprintf(buf, sizeof(buf), "a%db", 1234);
406 EXPECT_STREQ("a1234b", buf);
407
408 snprintf(buf, sizeof(buf), "a%db", -8123);
409 EXPECT_STREQ("a-8123b", buf);
410
Stephen Hines6c7b3cb2013-10-11 16:03:21 -0700411 snprintf(buf, sizeof(buf), "a%hdb", static_cast<short>(0x7fff0010));
Elliott Hughes1d13c642013-09-23 16:02:39 -0700412 EXPECT_STREQ("a16b", buf);
413
Stephen Hines6c7b3cb2013-10-11 16:03:21 -0700414 snprintf(buf, sizeof(buf), "a%hhdb", static_cast<char>(0x7fffff10));
Elliott Hughes1d13c642013-09-23 16:02:39 -0700415 EXPECT_STREQ("a16b", buf);
416
417 snprintf(buf, sizeof(buf), "a%lldb", 0x1000000000LL);
418 EXPECT_STREQ("a68719476736b", buf);
419
420 snprintf(buf, sizeof(buf), "a%ldb", 70000L);
421 EXPECT_STREQ("a70000b", buf);
422
423 snprintf(buf, sizeof(buf), "a%pb", reinterpret_cast<void*>(0xb0001234));
424 EXPECT_STREQ("a0xb0001234b", buf);
425
426 snprintf(buf, sizeof(buf), "a%xz", 0x12ab);
427 EXPECT_STREQ("a12abz", buf);
428
429 snprintf(buf, sizeof(buf), "a%Xz", 0x12ab);
430 EXPECT_STREQ("a12ABz", buf);
431
432 snprintf(buf, sizeof(buf), "a%08xz", 0x123456);
433 EXPECT_STREQ("a00123456z", buf);
434
435 snprintf(buf, sizeof(buf), "a%5dz", 1234);
436 EXPECT_STREQ("a 1234z", buf);
437
438 snprintf(buf, sizeof(buf), "a%05dz", 1234);
439 EXPECT_STREQ("a01234z", buf);
440
441 snprintf(buf, sizeof(buf), "a%8dz", 1234);
442 EXPECT_STREQ("a 1234z", buf);
443
444 snprintf(buf, sizeof(buf), "a%-8dz", 1234);
445 EXPECT_STREQ("a1234 z", buf);
446
447 snprintf(buf, sizeof(buf), "A%-11sZ", "abcdef");
448 EXPECT_STREQ("Aabcdef Z", buf);
449
450 snprintf(buf, sizeof(buf), "A%s:%dZ", "hello", 1234);
451 EXPECT_STREQ("Ahello:1234Z", buf);
452
453 snprintf(buf, sizeof(buf), "a%03d:%d:%02dz", 5, 5, 5);
454 EXPECT_STREQ("a005:5:05z", buf);
455
Yi Kong32bc0fc2018-08-02 17:31:13 -0700456 void* p = nullptr;
Elliott Hughes1d13c642013-09-23 16:02:39 -0700457 snprintf(buf, sizeof(buf), "a%d,%pz", 5, p);
Christopher Ferris13613132013-10-28 15:24:04 -0700458#if defined(__BIONIC__)
Elliott Hughes1d13c642013-09-23 16:02:39 -0700459 EXPECT_STREQ("a5,0x0z", buf);
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800460#else // __BIONIC__
Christopher Ferris13613132013-10-28 15:24:04 -0700461 EXPECT_STREQ("a5,(nil)z", buf);
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800462#endif // __BIONIC__
Elliott Hughes1d13c642013-09-23 16:02:39 -0700463
464 snprintf(buf, sizeof(buf), "a%lld,%d,%d,%dz", 0x1000000000LL, 6, 7, 8);
465 EXPECT_STREQ("a68719476736,6,7,8z", buf);
466
467 snprintf(buf, sizeof(buf), "a_%f_b", 1.23f);
468 EXPECT_STREQ("a_1.230000_b", buf);
469
Stephen Hines6c7b3cb2013-10-11 16:03:21 -0700470 snprintf(buf, sizeof(buf), "a_%g_b", 3.14);
Elliott Hughes1d13c642013-09-23 16:02:39 -0700471 EXPECT_STREQ("a_3.14_b", buf);
Alexander Ivchenkoedd7c2e2014-04-01 17:01:39 +0400472
473 snprintf(buf, sizeof(buf), "%1$s %1$s", "print_me_twice");
474 EXPECT_STREQ("print_me_twice print_me_twice", buf);
Elliott Hughes1d13c642013-09-23 16:02:39 -0700475}
476
Elliott Hughes1b18aff2014-12-16 14:45:32 -0800477template <typename T>
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700478static void CheckInfNan(int snprintf_fn(T*, size_t, const T*, ...),
479 int sscanf_fn(const T*, const T*, ...),
480 const T* fmt_string, const T* fmt, const T* fmt_plus,
481 const T* minus_inf, const T* inf_, const T* plus_inf,
482 const T* minus_nan, const T* nan_, const T* plus_nan) {
Elliott Hughes1b18aff2014-12-16 14:45:32 -0800483 T buf[BUFSIZ];
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700484 float f;
Elliott Hughes7823f322014-04-14 12:11:28 -0700485
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700486 // NaN.
487
488 snprintf_fn(buf, sizeof(buf), fmt, nanf(""));
Elliott Hughes1b18aff2014-12-16 14:45:32 -0800489 EXPECT_STREQ(nan_, buf) << fmt;
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700490 EXPECT_EQ(1, sscanf_fn(buf, fmt, &f));
491 EXPECT_TRUE(isnan(f));
492
493 snprintf_fn(buf, sizeof(buf), fmt, -nanf(""));
Elliott Hughes1b18aff2014-12-16 14:45:32 -0800494 EXPECT_STREQ(minus_nan, buf) << fmt;
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700495 EXPECT_EQ(1, sscanf_fn(buf, fmt, &f));
496 EXPECT_TRUE(isnan(f));
497
498 snprintf_fn(buf, sizeof(buf), fmt_plus, nanf(""));
Elliott Hughes1b18aff2014-12-16 14:45:32 -0800499 EXPECT_STREQ(plus_nan, buf) << fmt_plus;
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700500 EXPECT_EQ(1, sscanf_fn(buf, fmt, &f));
501 EXPECT_TRUE(isnan(f));
502
503 snprintf_fn(buf, sizeof(buf), fmt_plus, -nanf(""));
Elliott Hughes1b18aff2014-12-16 14:45:32 -0800504 EXPECT_STREQ(minus_nan, buf) << fmt_plus;
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700505 EXPECT_EQ(1, sscanf_fn(buf, fmt, &f));
506 EXPECT_TRUE(isnan(f));
Elliott Hughes1b18aff2014-12-16 14:45:32 -0800507
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700508 // Inf.
509
510 snprintf_fn(buf, sizeof(buf), fmt, HUGE_VALF);
Elliott Hughes1b18aff2014-12-16 14:45:32 -0800511 EXPECT_STREQ(inf_, buf) << fmt;
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700512 EXPECT_EQ(1, sscanf_fn(buf, fmt, &f));
513 EXPECT_EQ(HUGE_VALF, f);
514
515 snprintf_fn(buf, sizeof(buf), fmt, -HUGE_VALF);
Elliott Hughes1b18aff2014-12-16 14:45:32 -0800516 EXPECT_STREQ(minus_inf, buf) << fmt;
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700517 EXPECT_EQ(1, sscanf_fn(buf, fmt, &f));
518 EXPECT_EQ(-HUGE_VALF, f);
519
520 snprintf_fn(buf, sizeof(buf), fmt_plus, HUGE_VALF);
Elliott Hughes1b18aff2014-12-16 14:45:32 -0800521 EXPECT_STREQ(plus_inf, buf) << fmt_plus;
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700522 EXPECT_EQ(1, sscanf_fn(buf, fmt, &f));
523 EXPECT_EQ(HUGE_VALF, f);
524
525 snprintf_fn(buf, sizeof(buf), fmt_plus, -HUGE_VALF);
Elliott Hughes1b18aff2014-12-16 14:45:32 -0800526 EXPECT_STREQ(minus_inf, buf) << fmt_plus;
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700527 EXPECT_EQ(1, sscanf_fn(buf, fmt, &f));
528 EXPECT_EQ(-HUGE_VALF, f);
529
530 // Check case-insensitivity.
531 snprintf_fn(buf, sizeof(buf), fmt_string, "[InFiNiTy]");
532 EXPECT_EQ(1, sscanf_fn(buf, fmt, &f)) << buf;
533 EXPECT_EQ(HUGE_VALF, f);
534 snprintf_fn(buf, sizeof(buf), fmt_string, "[NaN]");
535 EXPECT_EQ(1, sscanf_fn(buf, fmt, &f)) << buf;
536 EXPECT_TRUE(isnan(f));
Elliott Hughes7823f322014-04-14 12:11:28 -0700537}
538
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700539TEST(STDIO_TEST, snprintf_sscanf_inf_nan) {
540 CheckInfNan(snprintf, sscanf, "%s",
541 "[%a]", "[%+a]",
542 "[-inf]", "[inf]", "[+inf]",
543 "[-nan]", "[nan]", "[+nan]");
544 CheckInfNan(snprintf, sscanf, "%s",
545 "[%A]", "[%+A]",
546 "[-INF]", "[INF]", "[+INF]",
547 "[-NAN]", "[NAN]", "[+NAN]");
548 CheckInfNan(snprintf, sscanf, "%s",
549 "[%e]", "[%+e]",
550 "[-inf]", "[inf]", "[+inf]",
551 "[-nan]", "[nan]", "[+nan]");
552 CheckInfNan(snprintf, sscanf, "%s",
553 "[%E]", "[%+E]",
554 "[-INF]", "[INF]", "[+INF]",
555 "[-NAN]", "[NAN]", "[+NAN]");
556 CheckInfNan(snprintf, sscanf, "%s",
557 "[%f]", "[%+f]",
558 "[-inf]", "[inf]", "[+inf]",
559 "[-nan]", "[nan]", "[+nan]");
560 CheckInfNan(snprintf, sscanf, "%s",
561 "[%F]", "[%+F]",
562 "[-INF]", "[INF]", "[+INF]",
563 "[-NAN]", "[NAN]", "[+NAN]");
564 CheckInfNan(snprintf, sscanf, "%s",
565 "[%g]", "[%+g]",
566 "[-inf]", "[inf]", "[+inf]",
567 "[-nan]", "[nan]", "[+nan]");
568 CheckInfNan(snprintf, sscanf, "%s",
569 "[%G]", "[%+G]",
570 "[-INF]", "[INF]", "[+INF]",
571 "[-NAN]", "[NAN]", "[+NAN]");
Elliott Hughes1b18aff2014-12-16 14:45:32 -0800572}
Elliott Hughes7823f322014-04-14 12:11:28 -0700573
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700574TEST(STDIO_TEST, swprintf_swscanf_inf_nan) {
575 CheckInfNan(swprintf, swscanf, L"%s",
576 L"[%a]", L"[%+a]",
577 L"[-inf]", L"[inf]", L"[+inf]",
578 L"[-nan]", L"[nan]", L"[+nan]");
579 CheckInfNan(swprintf, swscanf, L"%s",
580 L"[%A]", L"[%+A]",
581 L"[-INF]", L"[INF]", L"[+INF]",
582 L"[-NAN]", L"[NAN]", L"[+NAN]");
583 CheckInfNan(swprintf, swscanf, L"%s",
584 L"[%e]", L"[%+e]",
585 L"[-inf]", L"[inf]", L"[+inf]",
586 L"[-nan]", L"[nan]", L"[+nan]");
587 CheckInfNan(swprintf, swscanf, L"%s",
588 L"[%E]", L"[%+E]",
589 L"[-INF]", L"[INF]", L"[+INF]",
590 L"[-NAN]", L"[NAN]", L"[+NAN]");
591 CheckInfNan(swprintf, swscanf, L"%s",
592 L"[%f]", L"[%+f]",
593 L"[-inf]", L"[inf]", L"[+inf]",
594 L"[-nan]", L"[nan]", L"[+nan]");
595 CheckInfNan(swprintf, swscanf, L"%s",
596 L"[%F]", L"[%+F]",
597 L"[-INF]", L"[INF]", L"[+INF]",
598 L"[-NAN]", L"[NAN]", L"[+NAN]");
599 CheckInfNan(swprintf, swscanf, L"%s",
600 L"[%g]", L"[%+g]",
601 L"[-inf]", L"[inf]", L"[+inf]",
602 L"[-nan]", L"[nan]", L"[+nan]");
603 CheckInfNan(swprintf, swscanf, L"%s",
604 L"[%G]", L"[%+G]",
605 L"[-INF]", L"[INF]", L"[+INF]",
606 L"[-NAN]", L"[NAN]", L"[+NAN]");
Elliott Hughes7823f322014-04-14 12:11:28 -0700607}
608
Dan Albert9601f162017-08-09 14:59:06 -0700609TEST(STDIO_TEST, swprintf) {
610 constexpr size_t nchars = 32;
611 wchar_t buf[nchars];
612
613 ASSERT_EQ(2, swprintf(buf, nchars, L"ab")) << strerror(errno);
614 ASSERT_EQ(std::wstring(L"ab"), buf);
615 ASSERT_EQ(5, swprintf(buf, nchars, L"%s", "abcde"));
616 ASSERT_EQ(std::wstring(L"abcde"), buf);
617
618 // Unlike swprintf(), swprintf() returns -1 in case of truncation
619 // and doesn't necessarily zero-terminate the output!
620 ASSERT_EQ(-1, swprintf(buf, 4, L"%s", "abcde"));
621
622 const char kString[] = "Hello, World";
623 ASSERT_EQ(12, swprintf(buf, nchars, L"%s", kString));
624 ASSERT_EQ(std::wstring(L"Hello, World"), buf);
625 ASSERT_EQ(12, swprintf(buf, 13, L"%s", kString));
626 ASSERT_EQ(std::wstring(L"Hello, World"), buf);
627}
628
629TEST(STDIO_TEST, swprintf_a) {
630 constexpr size_t nchars = 32;
631 wchar_t buf[nchars];
632
633 ASSERT_EQ(20, swprintf(buf, nchars, L"%a", 3.1415926535));
634 ASSERT_EQ(std::wstring(L"0x1.921fb54411744p+1"), buf);
635}
636
Elliott Hughes2f9c8ce2017-11-01 13:54:47 -0700637TEST(STDIO_TEST, swprintf_lc) {
638 constexpr size_t nchars = 32;
639 wchar_t buf[nchars];
640
641 wint_t wc = L'a';
642 EXPECT_EQ(3, swprintf(buf, nchars, L"<%lc>", wc));
643 EXPECT_EQ(std::wstring(L"<a>"), buf);
644}
645
646TEST(STDIO_TEST, swprintf_C) { // Synonym for %lc.
647 constexpr size_t nchars = 32;
648 wchar_t buf[nchars];
649
650 wint_t wc = L'a';
651 EXPECT_EQ(3, swprintf(buf, nchars, L"<%C>", wc));
652 EXPECT_EQ(std::wstring(L"<a>"), buf);
653}
654
Elliott Hughes618303c2017-11-02 16:58:44 -0700655TEST(STDIO_TEST, swprintf_jd_INTMAX_MAX) {
656 constexpr size_t nchars = 32;
657 wchar_t buf[nchars];
658
659 swprintf(buf, nchars, L"%jd", INTMAX_MAX);
660 EXPECT_EQ(std::wstring(L"9223372036854775807"), buf);
661}
662
663TEST(STDIO_TEST, swprintf_jd_INTMAX_MIN) {
664 constexpr size_t nchars = 32;
665 wchar_t buf[nchars];
666
667 swprintf(buf, nchars, L"%jd", INTMAX_MIN);
668 EXPECT_EQ(std::wstring(L"-9223372036854775808"), buf);
669}
670
671TEST(STDIO_TEST, swprintf_ju_UINTMAX_MAX) {
672 constexpr size_t nchars = 32;
673 wchar_t buf[nchars];
674
675 swprintf(buf, nchars, L"%ju", UINTMAX_MAX);
676 EXPECT_EQ(std::wstring(L"18446744073709551615"), buf);
677}
678
679TEST(STDIO_TEST, swprintf_1$ju_UINTMAX_MAX) {
680 constexpr size_t nchars = 32;
681 wchar_t buf[nchars];
682
683 swprintf(buf, nchars, L"%1$ju", UINTMAX_MAX);
684 EXPECT_EQ(std::wstring(L"18446744073709551615"), buf);
685}
686
Dan Albert9601f162017-08-09 14:59:06 -0700687TEST(STDIO_TEST, swprintf_ls) {
688 constexpr size_t nchars = 32;
689 wchar_t buf[nchars];
690
691 static const wchar_t kWideString[] = L"Hello\uff41 World";
692 ASSERT_EQ(12, swprintf(buf, nchars, L"%ls", kWideString));
693 ASSERT_EQ(std::wstring(kWideString), buf);
694 ASSERT_EQ(12, swprintf(buf, 13, L"%ls", kWideString));
695 ASSERT_EQ(std::wstring(kWideString), buf);
696}
697
Elliott Hughes2f9c8ce2017-11-01 13:54:47 -0700698TEST(STDIO_TEST, swprintf_S) { // Synonym for %ls.
699 constexpr size_t nchars = 32;
700 wchar_t buf[nchars];
701
702 static const wchar_t kWideString[] = L"Hello\uff41 World";
703 ASSERT_EQ(12, swprintf(buf, nchars, L"%S", kWideString));
704 ASSERT_EQ(std::wstring(kWideString), buf);
705 ASSERT_EQ(12, swprintf(buf, 13, L"%S", kWideString));
706 ASSERT_EQ(std::wstring(kWideString), buf);
707}
708
Christopher Ferris13f26a72016-01-13 13:47:58 -0800709TEST(STDIO_TEST, snprintf_d_INT_MAX) {
Elliott Hughes1d13c642013-09-23 16:02:39 -0700710 char buf[BUFSIZ];
711 snprintf(buf, sizeof(buf), "%d", INT_MAX);
712 EXPECT_STREQ("2147483647", buf);
713}
714
Christopher Ferris13f26a72016-01-13 13:47:58 -0800715TEST(STDIO_TEST, snprintf_d_INT_MIN) {
Elliott Hughes1d13c642013-09-23 16:02:39 -0700716 char buf[BUFSIZ];
717 snprintf(buf, sizeof(buf), "%d", INT_MIN);
718 EXPECT_STREQ("-2147483648", buf);
719}
720
Elliott Hughes618303c2017-11-02 16:58:44 -0700721TEST(STDIO_TEST, snprintf_jd_INTMAX_MAX) {
722 char buf[BUFSIZ];
723 snprintf(buf, sizeof(buf), "%jd", INTMAX_MAX);
724 EXPECT_STREQ("9223372036854775807", buf);
725}
726
727TEST(STDIO_TEST, snprintf_jd_INTMAX_MIN) {
728 char buf[BUFSIZ];
729 snprintf(buf, sizeof(buf), "%jd", INTMAX_MIN);
730 EXPECT_STREQ("-9223372036854775808", buf);
731}
732
733TEST(STDIO_TEST, snprintf_ju_UINTMAX_MAX) {
734 char buf[BUFSIZ];
735 snprintf(buf, sizeof(buf), "%ju", UINTMAX_MAX);
736 EXPECT_STREQ("18446744073709551615", buf);
737}
738
739TEST(STDIO_TEST, snprintf_1$ju_UINTMAX_MAX) {
740 char buf[BUFSIZ];
741 snprintf(buf, sizeof(buf), "%1$ju", UINTMAX_MAX);
742 EXPECT_STREQ("18446744073709551615", buf);
743}
744
Christopher Ferris13f26a72016-01-13 13:47:58 -0800745TEST(STDIO_TEST, snprintf_ld_LONG_MAX) {
Elliott Hughes1d13c642013-09-23 16:02:39 -0700746 char buf[BUFSIZ];
747 snprintf(buf, sizeof(buf), "%ld", LONG_MAX);
Josh Gaob36efa42016-09-15 13:55:41 -0700748#if defined(__LP64__)
Elliott Hughes925753a2013-10-18 13:17:18 -0700749 EXPECT_STREQ("9223372036854775807", buf);
750#else
Elliott Hughes1d13c642013-09-23 16:02:39 -0700751 EXPECT_STREQ("2147483647", buf);
Elliott Hughes925753a2013-10-18 13:17:18 -0700752#endif
Elliott Hughes1d13c642013-09-23 16:02:39 -0700753}
754
Christopher Ferris13f26a72016-01-13 13:47:58 -0800755TEST(STDIO_TEST, snprintf_ld_LONG_MIN) {
Elliott Hughes1d13c642013-09-23 16:02:39 -0700756 char buf[BUFSIZ];
757 snprintf(buf, sizeof(buf), "%ld", LONG_MIN);
Josh Gaob36efa42016-09-15 13:55:41 -0700758#if defined(__LP64__)
Elliott Hughes925753a2013-10-18 13:17:18 -0700759 EXPECT_STREQ("-9223372036854775808", buf);
760#else
Elliott Hughes1d13c642013-09-23 16:02:39 -0700761 EXPECT_STREQ("-2147483648", buf);
Elliott Hughes925753a2013-10-18 13:17:18 -0700762#endif
Elliott Hughes1d13c642013-09-23 16:02:39 -0700763}
764
Christopher Ferris13f26a72016-01-13 13:47:58 -0800765TEST(STDIO_TEST, snprintf_lld_LLONG_MAX) {
Elliott Hughes1d13c642013-09-23 16:02:39 -0700766 char buf[BUFSIZ];
767 snprintf(buf, sizeof(buf), "%lld", LLONG_MAX);
768 EXPECT_STREQ("9223372036854775807", buf);
769}
770
Christopher Ferris13f26a72016-01-13 13:47:58 -0800771TEST(STDIO_TEST, snprintf_lld_LLONG_MIN) {
Elliott Hughes1d13c642013-09-23 16:02:39 -0700772 char buf[BUFSIZ];
773 snprintf(buf, sizeof(buf), "%lld", LLONG_MIN);
774 EXPECT_STREQ("-9223372036854775808", buf);
775}
776
Elliott Hughes2f9c8ce2017-11-01 13:54:47 -0700777TEST(STDIO_TEST, snprintf_o_UINT_MAX) {
778 char buf[BUFSIZ];
779 snprintf(buf, sizeof(buf), "%o", UINT_MAX);
780 EXPECT_STREQ("37777777777", buf);
781}
782
783TEST(STDIO_TEST, snprintf_u_UINT_MAX) {
784 char buf[BUFSIZ];
785 snprintf(buf, sizeof(buf), "%u", UINT_MAX);
786 EXPECT_STREQ("4294967295", buf);
787}
788
789TEST(STDIO_TEST, snprintf_x_UINT_MAX) {
790 char buf[BUFSIZ];
791 snprintf(buf, sizeof(buf), "%x", UINT_MAX);
792 EXPECT_STREQ("ffffffff", buf);
793}
794
795TEST(STDIO_TEST, snprintf_X_UINT_MAX) {
796 char buf[BUFSIZ];
797 snprintf(buf, sizeof(buf), "%X", UINT_MAX);
798 EXPECT_STREQ("FFFFFFFF", buf);
799}
800
Christopher Ferris13f26a72016-01-13 13:47:58 -0800801TEST(STDIO_TEST, snprintf_e) {
Elliott Hughes4bd97ce2014-04-10 17:48:14 -0700802 char buf[BUFSIZ];
803
804 snprintf(buf, sizeof(buf), "%e", 1.5);
805 EXPECT_STREQ("1.500000e+00", buf);
806
Chih-Hung Hsiehc2edae32018-12-11 15:16:24 -0800807 snprintf(buf, sizeof(buf), "%Le", 1.5L);
Elliott Hughes4bd97ce2014-04-10 17:48:14 -0700808 EXPECT_STREQ("1.500000e+00", buf);
809}
810
Christopher Ferris13f26a72016-01-13 13:47:58 -0800811TEST(STDIO_TEST, snprintf_negative_zero_5084292) {
Elliott Hughese77f38f2014-05-14 12:39:12 -0700812 char buf[BUFSIZ];
813
Elliott Hughes1b18aff2014-12-16 14:45:32 -0800814 snprintf(buf, sizeof(buf), "%e", -0.0);
815 EXPECT_STREQ("-0.000000e+00", buf);
816 snprintf(buf, sizeof(buf), "%E", -0.0);
817 EXPECT_STREQ("-0.000000E+00", buf);
Elliott Hughese77f38f2014-05-14 12:39:12 -0700818 snprintf(buf, sizeof(buf), "%f", -0.0);
819 EXPECT_STREQ("-0.000000", buf);
Elliott Hughes1b18aff2014-12-16 14:45:32 -0800820 snprintf(buf, sizeof(buf), "%F", -0.0);
821 EXPECT_STREQ("-0.000000", buf);
822 snprintf(buf, sizeof(buf), "%g", -0.0);
823 EXPECT_STREQ("-0", buf);
824 snprintf(buf, sizeof(buf), "%G", -0.0);
825 EXPECT_STREQ("-0", buf);
826 snprintf(buf, sizeof(buf), "%a", -0.0);
827 EXPECT_STREQ("-0x0p+0", buf);
828 snprintf(buf, sizeof(buf), "%A", -0.0);
829 EXPECT_STREQ("-0X0P+0", buf);
Elliott Hughese77f38f2014-05-14 12:39:12 -0700830}
831
Christopher Ferris13f26a72016-01-13 13:47:58 -0800832TEST(STDIO_TEST, snprintf_utf8_15439554) {
Yi Kong32bc0fc2018-08-02 17:31:13 -0700833 locale_t cloc = newlocale(LC_ALL, "C.UTF-8", nullptr);
Wally Yaua40fdbd2014-08-26 09:47:23 -0700834 locale_t old_locale = uselocale(cloc);
Dan Albert1aec7c12014-07-30 10:53:48 -0700835
Elliott Hughes69f05d22014-06-05 20:10:09 -0700836 // http://b/15439554
837 char buf[BUFSIZ];
838
839 // 1-byte character.
840 snprintf(buf, sizeof(buf), "%dx%d", 1, 2);
841 EXPECT_STREQ("1x2", buf);
842 // 2-byte character.
843 snprintf(buf, sizeof(buf), "%d\xc2\xa2%d", 1, 2);
844 EXPECT_STREQ("1¢2", buf);
845 // 3-byte character.
846 snprintf(buf, sizeof(buf), "%d\xe2\x82\xac%d", 1, 2);
847 EXPECT_STREQ("1€2", buf);
848 // 4-byte character.
849 snprintf(buf, sizeof(buf), "%d\xf0\xa4\xad\xa2%d", 1, 2);
850 EXPECT_STREQ("1𤭢2", buf);
Dan Albert1aec7c12014-07-30 10:53:48 -0700851
Wally Yaua40fdbd2014-08-26 09:47:23 -0700852 uselocale(old_locale);
Dan Albert1aec7c12014-07-30 10:53:48 -0700853 freelocale(cloc);
Elliott Hughes69f05d22014-06-05 20:10:09 -0700854}
855
Elliott Hughes43f7c872016-02-05 11:18:41 -0800856static void* snprintf_small_stack_fn(void*) {
857 // Make life (realistically) hard for ourselves by allocating our own buffer for the result.
858 char buf[PATH_MAX];
859 snprintf(buf, sizeof(buf), "/proc/%d", getpid());
860 return nullptr;
861}
862
863TEST(STDIO_TEST, snprintf_small_stack) {
864 // Is it safe to call snprintf on a thread with a small stack?
865 // (The snprintf implementation puts some pretty large buffers on the stack.)
866 pthread_attr_t a;
867 ASSERT_EQ(0, pthread_attr_init(&a));
868 ASSERT_EQ(0, pthread_attr_setstacksize(&a, PTHREAD_STACK_MIN));
869
870 pthread_t t;
871 ASSERT_EQ(0, pthread_create(&t, &a, snprintf_small_stack_fn, nullptr));
872 ASSERT_EQ(0, pthread_join(t, nullptr));
873}
874
Elliott Hughes8200e552016-02-05 21:57:37 -0800875TEST(STDIO_TEST, snprintf_asterisk_overflow) {
876 char buf[128];
877 ASSERT_EQ(5, snprintf(buf, sizeof(buf), "%.*s%c", 4, "hello world", '!'));
878 ASSERT_EQ(12, snprintf(buf, sizeof(buf), "%.*s%c", INT_MAX/2, "hello world", '!'));
879 ASSERT_EQ(12, snprintf(buf, sizeof(buf), "%.*s%c", INT_MAX-1, "hello world", '!'));
880 ASSERT_EQ(12, snprintf(buf, sizeof(buf), "%.*s%c", INT_MAX, "hello world", '!'));
881 ASSERT_EQ(12, snprintf(buf, sizeof(buf), "%.*s%c", -1, "hello world", '!'));
882
883 // INT_MAX-1, INT_MAX, INT_MAX+1.
884 ASSERT_EQ(12, snprintf(buf, sizeof(buf), "%.2147483646s%c", "hello world", '!'));
885 ASSERT_EQ(12, snprintf(buf, sizeof(buf), "%.2147483647s%c", "hello world", '!'));
886 ASSERT_EQ(-1, snprintf(buf, sizeof(buf), "%.2147483648s%c", "hello world", '!'));
887 ASSERT_EQ(ENOMEM, errno);
888}
889
Elliott Hughes5dc31302020-01-07 08:48:10 -0800890// Inspired by https://github.com/landley/toybox/issues/163.
891TEST(STDIO_TEST, printf_NULL) {
892 char buf[128];
893 char* null = nullptr;
894 EXPECT_EQ(4, snprintf(buf, sizeof(buf), "<%*.*s>", 2, 2, null));
895 EXPECT_STREQ("<(n>", buf);
896 EXPECT_EQ(8, snprintf(buf, sizeof(buf), "<%*.*s>", 2, 8, null));
897 EXPECT_STREQ("<(null)>", buf);
898 EXPECT_EQ(10, snprintf(buf, sizeof(buf), "<%*.*s>", 8, 2, null));
899 EXPECT_STREQ("< (n>", buf);
900 EXPECT_EQ(10, snprintf(buf, sizeof(buf), "<%*.*s>", 8, 8, null));
901 EXPECT_STREQ("< (null)>", buf);
902}
903
Elliott Hughes70715da2016-08-01 16:35:17 -0700904TEST(STDIO_TEST, fprintf) {
905 TemporaryFile tf;
906
907 FILE* tfile = fdopen(tf.fd, "r+");
908 ASSERT_TRUE(tfile != nullptr);
909
910 ASSERT_EQ(7, fprintf(tfile, "%d %s", 123, "abc"));
911 AssertFileIs(tfile, "123 abc");
912 fclose(tfile);
913}
914
Christopher Ferris13f26a72016-01-13 13:47:58 -0800915TEST(STDIO_TEST, fprintf_failures_7229520) {
Elliott Hughes69f05d22014-06-05 20:10:09 -0700916 // http://b/7229520
Elliott Hughesc9244bd2014-05-14 13:31:35 -0700917 FILE* fp;
Josh Gaof6e5b582018-06-01 15:30:54 -0700918 int fd_rdonly = open("/dev/null", O_RDONLY);
919 ASSERT_NE(-1, fd_rdonly);
Elliott Hughesc9244bd2014-05-14 13:31:35 -0700920
921 // Unbuffered case where the fprintf(3) itself fails.
922 ASSERT_NE(nullptr, fp = tmpfile());
Yi Kong32bc0fc2018-08-02 17:31:13 -0700923 setbuf(fp, nullptr);
Elliott Hughesc9244bd2014-05-14 13:31:35 -0700924 ASSERT_EQ(4, fprintf(fp, "epic"));
Josh Gaof6e5b582018-06-01 15:30:54 -0700925 ASSERT_NE(-1, dup2(fd_rdonly, fileno(fp)));
Elliott Hughesc9244bd2014-05-14 13:31:35 -0700926 ASSERT_EQ(-1, fprintf(fp, "fail"));
Josh Gaof6e5b582018-06-01 15:30:54 -0700927 ASSERT_EQ(0, fclose(fp));
Elliott Hughesc9244bd2014-05-14 13:31:35 -0700928
929 // Buffered case where we won't notice until the fclose(3).
930 // It's likely this is what was actually seen in http://b/7229520,
931 // and that expecting fprintf to fail is setting yourself up for
932 // disappointment. Remember to check fclose(3)'s return value, kids!
933 ASSERT_NE(nullptr, fp = tmpfile());
934 ASSERT_EQ(4, fprintf(fp, "epic"));
Josh Gaof6e5b582018-06-01 15:30:54 -0700935 ASSERT_NE(-1, dup2(fd_rdonly, fileno(fp)));
Elliott Hughesc9244bd2014-05-14 13:31:35 -0700936 ASSERT_EQ(4, fprintf(fp, "fail"));
937 ASSERT_EQ(-1, fclose(fp));
938}
939
Elliott Hughes468efc82018-07-10 14:39:49 -0700940TEST(STDIO_TEST, popen_r) {
Elliott Hughes6b3f49a2013-03-06 16:20:55 -0800941 FILE* fp = popen("cat /proc/version", "r");
Yi Kong32bc0fc2018-08-02 17:31:13 -0700942 ASSERT_TRUE(fp != nullptr);
Elliott Hughes6b3f49a2013-03-06 16:20:55 -0800943
944 char buf[16];
945 char* s = fgets(buf, sizeof(buf), fp);
946 buf[13] = '\0';
947 ASSERT_STREQ("Linux version", s);
948
949 ASSERT_EQ(0, pclose(fp));
950}
Elliott Hughes6b05c8e2013-04-11 13:54:48 -0700951
Elliott Hughes468efc82018-07-10 14:39:49 -0700952TEST(STDIO_TEST, popen_socketpair) {
953 FILE* fp = popen("cat", "r+");
Yi Kong32bc0fc2018-08-02 17:31:13 -0700954 ASSERT_TRUE(fp != nullptr);
Elliott Hughes468efc82018-07-10 14:39:49 -0700955
956 fputs("hello\nworld\n", fp);
957 fflush(fp);
958
959 char buf[16];
960 ASSERT_NE(nullptr, fgets(buf, sizeof(buf), fp));
961 EXPECT_STREQ("hello\n", buf);
962 ASSERT_NE(nullptr, fgets(buf, sizeof(buf), fp));
963 EXPECT_STREQ("world\n", buf);
964
965 ASSERT_EQ(0, pclose(fp));
966}
967
968TEST(STDIO_TEST, popen_socketpair_shutdown) {
969 FILE* fp = popen("uniq -c", "r+");
Yi Kong32bc0fc2018-08-02 17:31:13 -0700970 ASSERT_TRUE(fp != nullptr);
Elliott Hughes468efc82018-07-10 14:39:49 -0700971
972 fputs("a\na\na\na\nb\n", fp);
973 fflush(fp);
974 ASSERT_EQ(0, shutdown(fileno(fp), SHUT_WR));
975
976 char buf[16];
977 ASSERT_NE(nullptr, fgets(buf, sizeof(buf), fp));
978 EXPECT_STREQ(" 4 a\n", buf);
979 ASSERT_NE(nullptr, fgets(buf, sizeof(buf), fp));
980 EXPECT_STREQ(" 1 b\n", buf);
981
982 ASSERT_EQ(0, pclose(fp));
983}
984
985TEST(STDIO_TEST, popen_return_value_0) {
986 FILE* fp = popen("true", "r");
Yi Kong32bc0fc2018-08-02 17:31:13 -0700987 ASSERT_TRUE(fp != nullptr);
Elliott Hughes468efc82018-07-10 14:39:49 -0700988 int status = pclose(fp);
989 EXPECT_TRUE(WIFEXITED(status));
990 EXPECT_EQ(0, WEXITSTATUS(status));
991}
992
993TEST(STDIO_TEST, popen_return_value_1) {
994 FILE* fp = popen("false", "r");
Yi Kong32bc0fc2018-08-02 17:31:13 -0700995 ASSERT_TRUE(fp != nullptr);
Elliott Hughes468efc82018-07-10 14:39:49 -0700996 int status = pclose(fp);
997 EXPECT_TRUE(WIFEXITED(status));
998 EXPECT_EQ(1, WEXITSTATUS(status));
999}
1000
1001TEST(STDIO_TEST, popen_return_value_signal) {
1002 FILE* fp = popen("kill -7 $$", "r");
Yi Kong32bc0fc2018-08-02 17:31:13 -07001003 ASSERT_TRUE(fp != nullptr);
Elliott Hughes468efc82018-07-10 14:39:49 -07001004 int status = pclose(fp);
1005 EXPECT_TRUE(WIFSIGNALED(status));
1006 EXPECT_EQ(7, WTERMSIG(status));
1007}
1008
Christopher Ferris13f26a72016-01-13 13:47:58 -08001009TEST(STDIO_TEST, getc) {
Elliott Hughes6b05c8e2013-04-11 13:54:48 -07001010 FILE* fp = fopen("/proc/version", "r");
Yi Kong32bc0fc2018-08-02 17:31:13 -07001011 ASSERT_TRUE(fp != nullptr);
Elliott Hughes6b05c8e2013-04-11 13:54:48 -07001012 ASSERT_EQ('L', getc(fp));
1013 ASSERT_EQ('i', getc(fp));
1014 ASSERT_EQ('n', getc(fp));
1015 ASSERT_EQ('u', getc(fp));
1016 ASSERT_EQ('x', getc(fp));
1017 fclose(fp);
1018}
1019
Christopher Ferris13f26a72016-01-13 13:47:58 -08001020TEST(STDIO_TEST, putc) {
Elliott Hughes6b05c8e2013-04-11 13:54:48 -07001021 FILE* fp = fopen("/proc/version", "r");
Yi Kong32bc0fc2018-08-02 17:31:13 -07001022 ASSERT_TRUE(fp != nullptr);
Elliott Hughes6b05c8e2013-04-11 13:54:48 -07001023 ASSERT_EQ(EOF, putc('x', fp));
1024 fclose(fp);
1025}
Elliott Hughes603332f2014-03-12 17:10:41 -07001026
Elliott Hughes7f0849f2016-08-26 16:17:17 -07001027TEST(STDIO_TEST, sscanf_swscanf) {
1028 struct stuff {
1029 char s1[123];
Elliott Hughes0d3ba1f2017-12-06 16:41:35 -08001030 int i1, i2;
1031 char cs1[3];
1032 char s2[3];
1033 char c1;
Elliott Hughes7f0849f2016-08-26 16:17:17 -07001034 double d1;
1035 float f1;
Elliott Hughes0d3ba1f2017-12-06 16:41:35 -08001036 char s3[123];
Elliott Hughes7f0849f2016-08-26 16:17:17 -07001037
1038 void Check() {
Elliott Hughes0d3ba1f2017-12-06 16:41:35 -08001039 EXPECT_STREQ("hello", s1);
1040 EXPECT_EQ(123, i1);
1041 EXPECT_EQ(456, i2);
1042 EXPECT_EQ('a', cs1[0]);
1043 EXPECT_EQ('b', cs1[1]);
1044 EXPECT_EQ('x', cs1[2]); // No terminating NUL.
1045 EXPECT_STREQ("AB", s2); // Terminating NUL.
1046 EXPECT_EQ('!', c1);
1047 EXPECT_DOUBLE_EQ(1.23, d1);
1048 EXPECT_FLOAT_EQ(9.0f, f1);
1049 EXPECT_STREQ("world", s3);
Elliott Hughes7f0849f2016-08-26 16:17:17 -07001050 }
1051 } s;
1052
Elliott Hughes0d3ba1f2017-12-06 16:41:35 -08001053 memset(&s, 'x', sizeof(s));
1054 ASSERT_EQ(9, sscanf(" hello 123 456abAB! 1.23 0x1.2p3 world",
1055 "%s %i%i%2c%[A-Z]%c %lf %f %s",
1056 s.s1, &s.i1, &s.i2, s.cs1, s.s2, &s.c1, &s.d1, &s.f1, s.s3));
Elliott Hughes7f0849f2016-08-26 16:17:17 -07001057 s.Check();
1058
Elliott Hughes0d3ba1f2017-12-06 16:41:35 -08001059 memset(&s, 'x', sizeof(s));
1060 ASSERT_EQ(9, swscanf(L" hello 123 456abAB! 1.23 0x1.2p3 world",
1061 L"%s %i%i%2c%[A-Z]%c %lf %f %s",
1062 s.s1, &s.i1, &s.i2, s.cs1, s.s2, &s.c1, &s.d1, &s.f1, s.s3));
Elliott Hughes7f0849f2016-08-26 16:17:17 -07001063 s.Check();
Elliott Hughes603332f2014-03-12 17:10:41 -07001064}
Elliott Hughes53b24382014-05-02 18:29:25 -07001065
Elliott Hughes0d3ba1f2017-12-06 16:41:35 -08001066template <typename T>
1067static void CheckScanf(int sscanf_fn(const T*, const T*, ...),
1068 const T* input, const T* fmt,
1069 int expected_count, const char* expected_string) {
1070 char buf[256] = {};
1071 ASSERT_EQ(expected_count, sscanf_fn(input, fmt, &buf)) << fmt;
1072 ASSERT_STREQ(expected_string, buf) << fmt;
1073}
1074
1075TEST(STDIO_TEST, sscanf_ccl) {
1076 // `abc` is just those characters.
1077 CheckScanf(sscanf, "abcd", "%[abc]", 1, "abc");
1078 // `a-c` is the range 'a' .. 'c'.
1079 CheckScanf(sscanf, "abcd", "%[a-c]", 1, "abc");
1080 CheckScanf(sscanf, "-d", "%[a-c]", 0, "");
1081 CheckScanf(sscanf, "ac-bAd", "%[a--c]", 1, "ac-bA");
1082 // `a-c-e` is equivalent to `a-e`.
1083 CheckScanf(sscanf, "abcdefg", "%[a-c-e]", 1, "abcde");
1084 // `e-a` is equivalent to `ae-` (because 'e' > 'a').
1085 CheckScanf(sscanf, "-a-e-b", "%[e-a]", 1, "-a-e-");
1086 // An initial '^' negates the set.
1087 CheckScanf(sscanf, "abcde", "%[^d]", 1, "abc");
1088 CheckScanf(sscanf, "abcdefgh", "%[^c-d]", 1, "ab");
1089 CheckScanf(sscanf, "hgfedcba", "%[^c-d]", 1, "hgfe");
1090 // The first character may be ']' or '-' without being special.
1091 CheckScanf(sscanf, "[[]]x", "%[][]", 1, "[[]]");
1092 CheckScanf(sscanf, "-a-x", "%[-a]", 1, "-a-");
1093 // The last character may be '-' without being special.
1094 CheckScanf(sscanf, "-a-x", "%[a-]", 1, "-a-");
1095 // X--Y is [X--] + Y, not [X--] + [--Y] (a bug in my initial implementation).
1096 CheckScanf(sscanf, "+,-/.", "%[+--/]", 1, "+,-/");
1097}
1098
1099TEST(STDIO_TEST, swscanf_ccl) {
1100 // `abc` is just those characters.
1101 CheckScanf(swscanf, L"abcd", L"%[abc]", 1, "abc");
1102 // `a-c` is the range 'a' .. 'c'.
1103 CheckScanf(swscanf, L"abcd", L"%[a-c]", 1, "abc");
1104 CheckScanf(swscanf, L"-d", L"%[a-c]", 0, "");
1105 CheckScanf(swscanf, L"ac-bAd", L"%[a--c]", 1, "ac-bA");
1106 // `a-c-e` is equivalent to `a-e`.
1107 CheckScanf(swscanf, L"abcdefg", L"%[a-c-e]", 1, "abcde");
1108 // `e-a` is equivalent to `ae-` (because 'e' > 'a').
1109 CheckScanf(swscanf, L"-a-e-b", L"%[e-a]", 1, "-a-e-");
1110 // An initial '^' negates the set.
1111 CheckScanf(swscanf, L"abcde", L"%[^d]", 1, "abc");
1112 CheckScanf(swscanf, L"abcdefgh", L"%[^c-d]", 1, "ab");
1113 CheckScanf(swscanf, L"hgfedcba", L"%[^c-d]", 1, "hgfe");
1114 // The first character may be ']' or '-' without being special.
1115 CheckScanf(swscanf, L"[[]]x", L"%[][]", 1, "[[]]");
1116 CheckScanf(swscanf, L"-a-x", L"%[-a]", 1, "-a-");
1117 // The last character may be '-' without being special.
1118 CheckScanf(swscanf, L"-a-x", L"%[a-]", 1, "-a-");
1119 // X--Y is [X--] + Y, not [X--] + [--Y] (a bug in my initial implementation).
1120 CheckScanf(swscanf, L"+,-/.", L"%[+--/]", 1, "+,-/");
1121}
1122
Elliott Hughes38e4aef2018-01-18 10:21:29 -08001123template <typename T1, typename T2>
1124static void CheckScanfM(int sscanf_fn(const T1*, const T1*, ...),
1125 const T1* input, const T1* fmt,
1126 int expected_count, const T2* expected_string) {
1127 T2* result = nullptr;
1128 ASSERT_EQ(expected_count, sscanf_fn(input, fmt, &result)) << fmt;
1129 if (expected_string == nullptr) {
1130 ASSERT_EQ(nullptr, result);
1131 } else {
1132 ASSERT_STREQ(expected_string, result) << fmt;
1133 }
1134 free(result);
1135}
1136
1137TEST(STDIO_TEST, sscanf_mc) {
1138 char* p1 = nullptr;
1139 char* p2 = nullptr;
1140 ASSERT_EQ(2, sscanf("hello", "%mc%mc", &p1, &p2));
1141 ASSERT_EQ('h', *p1);
1142 ASSERT_EQ('e', *p2);
1143 free(p1);
1144 free(p2);
1145
1146 p1 = nullptr;
1147 ASSERT_EQ(1, sscanf("hello", "%4mc", &p1));
1148 ASSERT_EQ('h', p1[0]);
1149 ASSERT_EQ('e', p1[1]);
1150 ASSERT_EQ('l', p1[2]);
1151 ASSERT_EQ('l', p1[3]);
1152 free(p1);
1153
1154 p1 = nullptr;
1155 ASSERT_EQ(1, sscanf("hello world", "%30mc", &p1));
1156 ASSERT_EQ('h', p1[0]);
1157 ASSERT_EQ('e', p1[1]);
1158 ASSERT_EQ('l', p1[2]);
1159 ASSERT_EQ('l', p1[3]);
1160 ASSERT_EQ('o', p1[4]);
1161 free(p1);
1162}
1163
Elliott Hughes38e4aef2018-01-18 10:21:29 -08001164TEST(STDIO_TEST, sscanf_mlc) {
1165 // This is so useless that clang doesn't even believe it exists...
1166#pragma clang diagnostic push
1167#pragma clang diagnostic ignored "-Wformat-invalid-specifier"
1168#pragma clang diagnostic ignored "-Wformat-extra-args"
1169
1170 wchar_t* p1 = nullptr;
1171 wchar_t* p2 = nullptr;
1172 ASSERT_EQ(2, sscanf("hello", "%mlc%mlc", &p1, &p2));
1173 ASSERT_EQ(L'h', *p1);
1174 ASSERT_EQ(L'e', *p2);
1175 free(p1);
1176 free(p2);
1177
1178 p1 = nullptr;
1179 ASSERT_EQ(1, sscanf("hello", "%4mlc", &p1));
1180 ASSERT_EQ(L'h', p1[0]);
1181 ASSERT_EQ(L'e', p1[1]);
1182 ASSERT_EQ(L'l', p1[2]);
1183 ASSERT_EQ(L'l', p1[3]);
1184 free(p1);
1185
1186 p1 = nullptr;
1187 ASSERT_EQ(1, sscanf("hello world", "%30mlc", &p1));
1188 ASSERT_EQ(L'h', p1[0]);
1189 ASSERT_EQ(L'e', p1[1]);
1190 ASSERT_EQ(L'l', p1[2]);
1191 ASSERT_EQ(L'l', p1[3]);
1192 ASSERT_EQ(L'o', p1[4]);
1193 free(p1);
1194#pragma clang diagnostic pop
1195}
1196
Elliott Hughes38e4aef2018-01-18 10:21:29 -08001197TEST(STDIO_TEST, sscanf_ms) {
1198 CheckScanfM(sscanf, "hello", "%ms", 1, "hello");
1199 CheckScanfM(sscanf, "hello", "%4ms", 1, "hell");
1200 CheckScanfM(sscanf, "hello world", "%30ms", 1, "hello");
1201}
1202
1203TEST(STDIO_TEST, sscanf_mls) {
1204 CheckScanfM(sscanf, "hello", "%mls", 1, L"hello");
1205 CheckScanfM(sscanf, "hello", "%4mls", 1, L"hell");
1206 CheckScanfM(sscanf, "hello world", "%30mls", 1, L"hello");
1207}
1208
1209TEST(STDIO_TEST, sscanf_m_ccl) {
1210 CheckScanfM(sscanf, "hello", "%m[a-z]", 1, "hello");
1211 CheckScanfM(sscanf, "hello", "%4m[a-z]", 1, "hell");
1212 CheckScanfM(sscanf, "hello world", "%30m[a-z]", 1, "hello");
1213}
1214
1215TEST(STDIO_TEST, sscanf_ml_ccl) {
1216 CheckScanfM(sscanf, "hello", "%ml[a-z]", 1, L"hello");
1217 CheckScanfM(sscanf, "hello", "%4ml[a-z]", 1, L"hell");
1218 CheckScanfM(sscanf, "hello world", "%30ml[a-z]", 1, L"hello");
1219}
1220
1221TEST(STDIO_TEST, sscanf_ls) {
1222 wchar_t w[32] = {};
1223 ASSERT_EQ(1, sscanf("hello world", "%ls", w));
1224 ASSERT_EQ(L"hello", std::wstring(w));
1225}
1226
1227TEST(STDIO_TEST, sscanf_ls_suppress) {
1228 ASSERT_EQ(0, sscanf("hello world", "%*ls %*ls"));
1229}
1230
1231TEST(STDIO_TEST, sscanf_ls_n) {
1232 setlocale(LC_ALL, "C.UTF-8");
1233 wchar_t w[32] = {};
1234 int pos = 0;
1235 ASSERT_EQ(1, sscanf("\xc4\x80", "%ls%n", w, &pos));
1236 ASSERT_EQ(static_cast<wchar_t>(256), w[0]);
1237 ASSERT_EQ(2, pos);
1238}
1239
1240TEST(STDIO_TEST, sscanf_ls_realloc) {
1241 // This is so useless that clang doesn't even believe it exists...
1242#pragma clang diagnostic push
1243#pragma clang diagnostic ignored "-Wformat-invalid-specifier"
1244#pragma clang diagnostic ignored "-Wformat-extra-args"
1245 wchar_t* p1 = nullptr;
1246 wchar_t* p2 = nullptr;
1247 ASSERT_EQ(2, sscanf("1234567890123456789012345678901234567890 world", "%mls %mls", &p1, &p2));
1248 ASSERT_EQ(L"1234567890123456789012345678901234567890", std::wstring(p1));
1249 ASSERT_EQ(L"world", std::wstring(p2));
1250#pragma clang diagnostic pop
1251}
1252
Elliott Hughesbf9cb9e2017-12-11 12:39:01 -08001253// https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=202240
1254TEST(STDIO_TEST, scanf_wscanf_EOF) {
1255 EXPECT_EQ(0, sscanf("b", "ab"));
1256 EXPECT_EQ(EOF, sscanf("", "a"));
1257 EXPECT_EQ(0, swscanf(L"b", L"ab"));
1258 EXPECT_EQ(EOF, swscanf(L"", L"a"));
1259}
1260
1261TEST(STDIO_TEST, scanf_invalid_UTF8) {
1262#if 0 // TODO: more tests invented during code review; no regressions, so fix later.
1263 char buf[BUFSIZ];
1264 wchar_t wbuf[BUFSIZ];
1265
1266 memset(buf, 0, sizeof(buf));
1267 memset(wbuf, 0, sizeof(wbuf));
1268 EXPECT_EQ(0, sscanf("\xc0" " foo", "%ls %s", wbuf, buf));
1269#endif
1270}
1271
1272TEST(STDIO_TEST, scanf_no_match_no_termination) {
1273 char buf[4] = "x";
1274 EXPECT_EQ(0, sscanf("d", "%[abc]", buf));
1275 EXPECT_EQ('x', buf[0]);
1276 EXPECT_EQ(0, swscanf(L"d", L"%[abc]", buf));
1277 EXPECT_EQ('x', buf[0]);
1278
1279 wchar_t wbuf[4] = L"x";
1280 EXPECT_EQ(0, swscanf(L"d", L"%l[abc]", wbuf));
1281 EXPECT_EQ(L'x', wbuf[0]);
1282
1283 EXPECT_EQ(EOF, sscanf("", "%s", buf));
1284 EXPECT_EQ('x', buf[0]);
1285
1286 EXPECT_EQ(EOF, swscanf(L"", L"%ls", wbuf));
1287 EXPECT_EQ(L'x', wbuf[0]);
1288}
1289
1290TEST(STDIO_TEST, scanf_wscanf_wide_character_class) {
1291#if 0 // TODO: more tests invented during code review; no regressions, so fix later.
1292 wchar_t buf[BUFSIZ];
1293
1294 // A wide character shouldn't match an ASCII-only class for scanf or wscanf.
1295 memset(buf, 0, sizeof(buf));
1296 EXPECT_EQ(1, sscanf("xĀyz", "%l[xy]", buf));
1297 EXPECT_EQ(L"x"s, std::wstring(buf));
1298 memset(buf, 0, sizeof(buf));
1299 EXPECT_EQ(1, swscanf(L"xĀyz", L"%l[xy]", buf));
1300 EXPECT_EQ(L"x"s, std::wstring(buf));
1301
1302 // Even if scanf has wide characters in a class, they won't match...
1303 // TODO: is that a bug?
1304 memset(buf, 0, sizeof(buf));
1305 EXPECT_EQ(1, sscanf("xĀyz", "%l[xĀy]", buf));
1306 EXPECT_EQ(L"x"s, std::wstring(buf));
1307 // ...unless you use wscanf.
1308 memset(buf, 0, sizeof(buf));
1309 EXPECT_EQ(1, swscanf(L"xĀyz", L"%l[xĀy]", buf));
1310 EXPECT_EQ(L"xĀy"s, std::wstring(buf));
1311
1312 // Negation only covers ASCII for scanf...
1313 memset(buf, 0, sizeof(buf));
1314 EXPECT_EQ(1, sscanf("xĀyz", "%l[^ab]", buf));
1315 EXPECT_EQ(L"x"s, std::wstring(buf));
1316 // ...but covers wide characters for wscanf.
1317 memset(buf, 0, sizeof(buf));
1318 EXPECT_EQ(1, swscanf(L"xĀyz", L"%l[^ab]", buf));
1319 EXPECT_EQ(L"xĀyz"s, std::wstring(buf));
1320
1321 // We already determined that non-ASCII characters are ignored in scanf classes.
1322 memset(buf, 0, sizeof(buf));
1323 EXPECT_EQ(1, sscanf("x"
1324 "\xc4\x80" // Matches a byte from each wide char in the class.
1325 "\xc6\x82" // Neither byte is in the class.
1326 "yz",
1327 "%l[xy" "\xc5\x80" "\xc4\x81" "]", buf));
1328 EXPECT_EQ(L"x", std::wstring(buf));
1329 // bionic and glibc both behave badly for wscanf, so let's call it right for now...
1330 memset(buf, 0, sizeof(buf));
1331 EXPECT_EQ(1, swscanf(L"x"
1332 L"\xc4\x80"
1333 L"\xc6\x82"
1334 L"yz",
1335 L"%l[xy" L"\xc5\x80" L"\xc4\x81" L"]", buf));
1336 // Note that this isn't L"xĀ" --- although the *bytes* matched, they're
1337 // not put back together as a wide character.
1338 EXPECT_EQ(L"x" L"\xc4" L"\x80", std::wstring(buf));
1339#endif
1340}
1341
Christopher Ferris13f26a72016-01-13 13:47:58 -08001342TEST(STDIO_TEST, cantwrite_EBADF) {
Elliott Hughes53b24382014-05-02 18:29:25 -07001343 // If we open a file read-only...
1344 FILE* fp = fopen("/proc/version", "r");
1345
1346 // ...all attempts to write to that file should return failure.
1347
1348 // They should also set errno to EBADF. This isn't POSIX, but it's traditional.
1349 // glibc gets the wide-character functions wrong.
1350
1351 errno = 0;
1352 EXPECT_EQ(EOF, putc('x', fp));
1353 EXPECT_EQ(EBADF, errno);
1354
1355 errno = 0;
1356 EXPECT_EQ(EOF, fprintf(fp, "hello"));
1357 EXPECT_EQ(EBADF, errno);
1358
1359 errno = 0;
1360 EXPECT_EQ(EOF, fwprintf(fp, L"hello"));
Elliott Hughes063525c2014-05-13 11:19:57 -07001361#if defined(__BIONIC__)
Elliott Hughes53b24382014-05-02 18:29:25 -07001362 EXPECT_EQ(EBADF, errno);
1363#endif
1364
1365 errno = 0;
Elliott Hughes53b24382014-05-02 18:29:25 -07001366 EXPECT_EQ(0U, fwrite("hello", 1, 2, fp));
1367 EXPECT_EQ(EBADF, errno);
1368
1369 errno = 0;
1370 EXPECT_EQ(EOF, fputs("hello", fp));
1371 EXPECT_EQ(EBADF, errno);
1372
1373 errno = 0;
1374 EXPECT_EQ(WEOF, fputwc(L'x', fp));
Elliott Hughes063525c2014-05-13 11:19:57 -07001375#if defined(__BIONIC__)
Elliott Hughes53b24382014-05-02 18:29:25 -07001376 EXPECT_EQ(EBADF, errno);
1377#endif
1378}
Calin Juravle03e4ebe2014-05-08 14:42:06 +01001379
1380// Tests that we can only have a consistent and correct fpos_t when using
1381// f*pos functions (i.e. fpos doesn't get inside a multi byte character).
Christopher Ferris13f26a72016-01-13 13:47:58 -08001382TEST(STDIO_TEST, consistent_fpos_t) {
Calin Juravle03e4ebe2014-05-08 14:42:06 +01001383 ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
1384 uselocale(LC_GLOBAL_LOCALE);
1385
1386 FILE* fp = tmpfile();
Yi Kong32bc0fc2018-08-02 17:31:13 -07001387 ASSERT_TRUE(fp != nullptr);
Calin Juravle03e4ebe2014-05-08 14:42:06 +01001388
1389 wchar_t mb_one_bytes = L'h';
1390 wchar_t mb_two_bytes = 0x00a2;
1391 wchar_t mb_three_bytes = 0x20ac;
1392 wchar_t mb_four_bytes = 0x24b62;
1393
1394 // Write to file.
1395 ASSERT_EQ(mb_one_bytes, static_cast<wchar_t>(fputwc(mb_one_bytes, fp)));
1396 ASSERT_EQ(mb_two_bytes, static_cast<wchar_t>(fputwc(mb_two_bytes, fp)));
1397 ASSERT_EQ(mb_three_bytes, static_cast<wchar_t>(fputwc(mb_three_bytes, fp)));
1398 ASSERT_EQ(mb_four_bytes, static_cast<wchar_t>(fputwc(mb_four_bytes, fp)));
1399
1400 rewind(fp);
1401
1402 // Record each character position.
1403 fpos_t pos1;
1404 fpos_t pos2;
1405 fpos_t pos3;
1406 fpos_t pos4;
1407 fpos_t pos5;
1408 EXPECT_EQ(0, fgetpos(fp, &pos1));
1409 ASSERT_EQ(mb_one_bytes, static_cast<wchar_t>(fgetwc(fp)));
1410 EXPECT_EQ(0, fgetpos(fp, &pos2));
1411 ASSERT_EQ(mb_two_bytes, static_cast<wchar_t>(fgetwc(fp)));
1412 EXPECT_EQ(0, fgetpos(fp, &pos3));
1413 ASSERT_EQ(mb_three_bytes, static_cast<wchar_t>(fgetwc(fp)));
1414 EXPECT_EQ(0, fgetpos(fp, &pos4));
1415 ASSERT_EQ(mb_four_bytes, static_cast<wchar_t>(fgetwc(fp)));
1416 EXPECT_EQ(0, fgetpos(fp, &pos5));
1417
Elliott Hughes063525c2014-05-13 11:19:57 -07001418#if defined(__BIONIC__)
Calin Juravle03e4ebe2014-05-08 14:42:06 +01001419 // Bionic's fpos_t is just an alias for off_t. This is inherited from OpenBSD
1420 // upstream. Glibc differs by storing the mbstate_t inside its fpos_t. In
1421 // Bionic (and upstream OpenBSD) the mbstate_t is stored inside the FILE
1422 // structure.
1423 ASSERT_EQ(0, static_cast<off_t>(pos1));
1424 ASSERT_EQ(1, static_cast<off_t>(pos2));
1425 ASSERT_EQ(3, static_cast<off_t>(pos3));
1426 ASSERT_EQ(6, static_cast<off_t>(pos4));
1427 ASSERT_EQ(10, static_cast<off_t>(pos5));
1428#endif
1429
1430 // Exercise back and forth movements of the position.
1431 ASSERT_EQ(0, fsetpos(fp, &pos2));
1432 ASSERT_EQ(mb_two_bytes, static_cast<wchar_t>(fgetwc(fp)));
1433 ASSERT_EQ(0, fsetpos(fp, &pos1));
1434 ASSERT_EQ(mb_one_bytes, static_cast<wchar_t>(fgetwc(fp)));
1435 ASSERT_EQ(0, fsetpos(fp, &pos4));
1436 ASSERT_EQ(mb_four_bytes, static_cast<wchar_t>(fgetwc(fp)));
1437 ASSERT_EQ(0, fsetpos(fp, &pos3));
1438 ASSERT_EQ(mb_three_bytes, static_cast<wchar_t>(fgetwc(fp)));
1439 ASSERT_EQ(0, fsetpos(fp, &pos5));
1440 ASSERT_EQ(WEOF, fgetwc(fp));
1441
1442 fclose(fp);
1443}
1444
1445// Exercise the interaction between fpos and seek.
Christopher Ferris13f26a72016-01-13 13:47:58 -08001446TEST(STDIO_TEST, fpos_t_and_seek) {
Calin Juravle03e4ebe2014-05-08 14:42:06 +01001447 ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
1448 uselocale(LC_GLOBAL_LOCALE);
1449
Calin Juravle9b95ea92014-05-14 17:07:10 +01001450 // In glibc-2.16 fseek doesn't work properly in wide mode
1451 // (https://sourceware.org/bugzilla/show_bug.cgi?id=14543). One workaround is
1452 // to close and re-open the file. We do it in order to make the test pass
1453 // with all glibcs.
1454
Calin Juravle03e4ebe2014-05-08 14:42:06 +01001455 TemporaryFile tf;
1456 FILE* fp = fdopen(tf.fd, "w+");
Yi Kong32bc0fc2018-08-02 17:31:13 -07001457 ASSERT_TRUE(fp != nullptr);
Calin Juravle03e4ebe2014-05-08 14:42:06 +01001458
1459 wchar_t mb_two_bytes = 0x00a2;
1460 wchar_t mb_three_bytes = 0x20ac;
1461 wchar_t mb_four_bytes = 0x24b62;
1462
1463 // Write to file.
1464 ASSERT_EQ(mb_two_bytes, static_cast<wchar_t>(fputwc(mb_two_bytes, fp)));
1465 ASSERT_EQ(mb_three_bytes, static_cast<wchar_t>(fputwc(mb_three_bytes, fp)));
1466 ASSERT_EQ(mb_four_bytes, static_cast<wchar_t>(fputwc(mb_four_bytes, fp)));
1467
1468 fflush(fp);
1469 fclose(fp);
1470
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -08001471 fp = fopen(tf.path, "r");
Yi Kong32bc0fc2018-08-02 17:31:13 -07001472 ASSERT_TRUE(fp != nullptr);
Calin Juravle03e4ebe2014-05-08 14:42:06 +01001473
1474 // Store a valid position.
1475 fpos_t mb_two_bytes_pos;
1476 ASSERT_EQ(0, fgetpos(fp, &mb_two_bytes_pos));
1477
1478 // Move inside mb_four_bytes with fseek.
1479 long offset_inside_mb = 6;
1480 ASSERT_EQ(0, fseek(fp, offset_inside_mb, SEEK_SET));
1481
1482 // Store the "inside multi byte" position.
1483 fpos_t pos_inside_mb;
1484 ASSERT_EQ(0, fgetpos(fp, &pos_inside_mb));
Elliott Hughes063525c2014-05-13 11:19:57 -07001485#if defined(__BIONIC__)
1486 ASSERT_EQ(offset_inside_mb, static_cast<off_t>(pos_inside_mb));
1487#endif
Calin Juravle03e4ebe2014-05-08 14:42:06 +01001488
1489 // Reading from within a byte should produce an error.
1490 ASSERT_EQ(WEOF, fgetwc(fp));
1491 ASSERT_EQ(EILSEQ, errno);
1492
1493 // Reverting to a valid position should work.
1494 ASSERT_EQ(0, fsetpos(fp, &mb_two_bytes_pos));
1495 ASSERT_EQ(mb_two_bytes, static_cast<wchar_t>(fgetwc(fp)));
1496
1497 // Moving withing a multi byte with fsetpos should work but reading should
1498 // produce an error.
1499 ASSERT_EQ(0, fsetpos(fp, &pos_inside_mb));
1500 ASSERT_EQ(WEOF, fgetwc(fp));
1501 ASSERT_EQ(EILSEQ, errno);
1502
Elliott Hughes3a4c4542017-07-19 17:20:24 -07001503 ASSERT_EQ(0, fclose(fp));
Calin Juravle03e4ebe2014-05-08 14:42:06 +01001504}
Elliott Hughes6b841db2014-08-20 16:10:49 -07001505
Christopher Ferris13f26a72016-01-13 13:47:58 -08001506TEST(STDIO_TEST, fmemopen) {
Elliott Hughes6b841db2014-08-20 16:10:49 -07001507 char buf[16];
1508 memset(buf, 0, sizeof(buf));
1509 FILE* fp = fmemopen(buf, sizeof(buf), "r+");
1510 ASSERT_EQ('<', fputc('<', fp));
1511 ASSERT_NE(EOF, fputs("abc>\n", fp));
1512 fflush(fp);
1513
Elliott Hughes3a4c4542017-07-19 17:20:24 -07001514 // We wrote to the buffer...
Elliott Hughes6b841db2014-08-20 16:10:49 -07001515 ASSERT_STREQ("<abc>\n", buf);
1516
Elliott Hughes3a4c4542017-07-19 17:20:24 -07001517 // And can read back from the file.
Elliott Hughes70715da2016-08-01 16:35:17 -07001518 AssertFileIs(fp, "<abc>\n", true);
Elliott Hughes3a4c4542017-07-19 17:20:24 -07001519 ASSERT_EQ(0, fclose(fp));
Elliott Hughes6b841db2014-08-20 16:10:49 -07001520}
1521
Elliott Hughes3a4c4542017-07-19 17:20:24 -07001522TEST(STDIO_TEST, fmemopen_nullptr) {
Elliott Hughes6b841db2014-08-20 16:10:49 -07001523 FILE* fp = fmemopen(nullptr, 128, "r+");
1524 ASSERT_NE(EOF, fputs("xyz\n", fp));
1525
Elliott Hughes70715da2016-08-01 16:35:17 -07001526 AssertFileIs(fp, "xyz\n", true);
Elliott Hughes3a4c4542017-07-19 17:20:24 -07001527 ASSERT_EQ(0, fclose(fp));
Elliott Hughes6b841db2014-08-20 16:10:49 -07001528}
1529
Elliott Hughes3a4c4542017-07-19 17:20:24 -07001530TEST(STDIO_TEST, fmemopen_trailing_NUL_byte) {
1531 FILE* fp;
1532 char buf[8];
1533
1534 // POSIX: "When a stream open for writing is flushed or closed, a null byte
1535 // shall be written at the current position or at the end of the buffer,
1536 // depending on the size of the contents."
1537 memset(buf, 'x', sizeof(buf));
1538 ASSERT_NE(nullptr, fp = fmemopen(buf, sizeof(buf), "w"));
1539 // Even with nothing written (and not in truncate mode), we'll flush a NUL...
1540 ASSERT_EQ(0, fflush(fp));
1541 EXPECT_EQ("\0xxxxxxx"s, std::string(buf, buf + sizeof(buf)));
1542 // Now write and check that the NUL moves along with our writes...
1543 ASSERT_NE(EOF, fputs("hello", fp));
1544 ASSERT_EQ(0, fflush(fp));
1545 EXPECT_EQ("hello\0xx"s, std::string(buf, buf + sizeof(buf)));
1546 ASSERT_NE(EOF, fputs("wo", fp));
1547 ASSERT_EQ(0, fflush(fp));
1548 EXPECT_EQ("hellowo\0"s, std::string(buf, buf + sizeof(buf)));
1549 ASSERT_EQ(0, fclose(fp));
1550
1551 // "If a stream open for update is flushed or closed and the last write has
1552 // advanced the current buffer size, a null byte shall be written at the end
1553 // of the buffer if it fits."
1554 memset(buf, 'x', sizeof(buf));
1555 ASSERT_NE(nullptr, fp = fmemopen(buf, sizeof(buf), "r+"));
1556 // Nothing written yet, so no advance...
1557 ASSERT_EQ(0, fflush(fp));
1558 EXPECT_EQ("xxxxxxxx"s, std::string(buf, buf + sizeof(buf)));
1559 ASSERT_NE(EOF, fputs("hello", fp));
1560 ASSERT_EQ(0, fclose(fp));
1561}
1562
1563TEST(STDIO_TEST, fmemopen_size) {
1564 FILE* fp;
Elliott Hughes6b841db2014-08-20 16:10:49 -07001565 char buf[16];
Elliott Hughes3a4c4542017-07-19 17:20:24 -07001566 memset(buf, 'x', sizeof(buf));
Elliott Hughes6b841db2014-08-20 16:10:49 -07001567
Elliott Hughes3a4c4542017-07-19 17:20:24 -07001568 // POSIX: "The stream shall also maintain the size of the current buffer
1569 // contents; use of fseek() or fseeko() on the stream with SEEK_END shall
1570 // seek relative to this size."
Elliott Hughes6b841db2014-08-20 16:10:49 -07001571
Elliott Hughes3a4c4542017-07-19 17:20:24 -07001572 // "For modes r and r+ the size shall be set to the value given by the size
1573 // argument."
1574 ASSERT_NE(nullptr, fp = fmemopen(buf, 16, "r"));
1575 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
1576 EXPECT_EQ(16, ftell(fp));
1577 EXPECT_EQ(16, ftello(fp));
1578 ASSERT_EQ(0, fseeko(fp, 0, SEEK_END));
1579 EXPECT_EQ(16, ftell(fp));
1580 EXPECT_EQ(16, ftello(fp));
1581 ASSERT_EQ(0, fclose(fp));
1582 ASSERT_NE(nullptr, fp = fmemopen(buf, 16, "r+"));
1583 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
1584 EXPECT_EQ(16, ftell(fp));
1585 EXPECT_EQ(16, ftello(fp));
1586 ASSERT_EQ(0, fseeko(fp, 0, SEEK_END));
1587 EXPECT_EQ(16, ftell(fp));
1588 EXPECT_EQ(16, ftello(fp));
1589 ASSERT_EQ(0, fclose(fp));
1590
1591 // "For modes w and w+ the initial size shall be zero..."
1592 ASSERT_NE(nullptr, fp = fmemopen(nullptr, 16, "w"));
1593 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
1594 EXPECT_EQ(0, ftell(fp));
1595 EXPECT_EQ(0, ftello(fp));
1596 ASSERT_EQ(0, fseeko(fp, 0, SEEK_END));
1597 EXPECT_EQ(0, ftell(fp));
1598 EXPECT_EQ(0, ftello(fp));
1599 ASSERT_EQ(0, fclose(fp));
1600 ASSERT_NE(nullptr, fp = fmemopen(nullptr, 16, "w+"));
1601 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
1602 EXPECT_EQ(0, ftell(fp));
1603 EXPECT_EQ(0, ftello(fp));
1604 ASSERT_EQ(0, fseeko(fp, 0, SEEK_END));
1605 EXPECT_EQ(0, ftell(fp));
1606 EXPECT_EQ(0, ftello(fp));
1607 ASSERT_EQ(0, fclose(fp));
1608
1609 // "...and for modes a and a+ the initial size shall be:
1610 // 1. Zero, if buf is a null pointer
1611 ASSERT_NE(nullptr, fp = fmemopen(nullptr, 16, "a"));
1612 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
1613 EXPECT_EQ(0, ftell(fp));
1614 EXPECT_EQ(0, ftello(fp));
1615 ASSERT_EQ(0, fseeko(fp, 0, SEEK_END));
1616 EXPECT_EQ(0, ftell(fp));
1617 EXPECT_EQ(0, ftello(fp));
1618 ASSERT_EQ(0, fclose(fp));
1619 ASSERT_NE(nullptr, fp = fmemopen(nullptr, 16, "a+"));
1620 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
1621 EXPECT_EQ(0, ftell(fp));
1622 EXPECT_EQ(0, ftello(fp));
1623 ASSERT_EQ(0, fseeko(fp, 0, SEEK_END));
1624 EXPECT_EQ(0, ftell(fp));
1625 EXPECT_EQ(0, ftello(fp));
1626 ASSERT_EQ(0, fclose(fp));
1627
1628 // 2. The position of the first null byte in the buffer, if one is found
1629 memset(buf, 'x', sizeof(buf));
1630 buf[3] = '\0';
1631 ASSERT_NE(nullptr, fp = fmemopen(buf, 16, "a"));
1632 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
1633 EXPECT_EQ(3, ftell(fp));
1634 EXPECT_EQ(3, ftello(fp));
1635 ASSERT_EQ(0, fseeko(fp, 0, SEEK_END));
1636 EXPECT_EQ(3, ftell(fp));
1637 EXPECT_EQ(3, ftello(fp));
1638 ASSERT_EQ(0, fclose(fp));
1639 memset(buf, 'x', sizeof(buf));
1640 buf[3] = '\0';
1641 ASSERT_NE(nullptr, fp = fmemopen(buf, 16, "a+"));
1642 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
1643 EXPECT_EQ(3, ftell(fp));
1644 EXPECT_EQ(3, ftello(fp));
1645 ASSERT_EQ(0, fseeko(fp, 0, SEEK_END));
1646 EXPECT_EQ(3, ftell(fp));
1647 EXPECT_EQ(3, ftello(fp));
1648 ASSERT_EQ(0, fclose(fp));
1649
1650 // 3. The value of the size argument, if buf is not a null pointer and no
1651 // null byte is found.
1652 memset(buf, 'x', sizeof(buf));
1653 ASSERT_NE(nullptr, fp = fmemopen(buf, 16, "a"));
1654 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
1655 EXPECT_EQ(16, ftell(fp));
1656 EXPECT_EQ(16, ftello(fp));
1657 ASSERT_EQ(0, fseeko(fp, 0, SEEK_END));
1658 EXPECT_EQ(16, ftell(fp));
1659 EXPECT_EQ(16, ftello(fp));
1660 ASSERT_EQ(0, fclose(fp));
1661 memset(buf, 'x', sizeof(buf));
1662 ASSERT_NE(nullptr, fp = fmemopen(buf, 16, "a+"));
1663 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
1664 EXPECT_EQ(16, ftell(fp));
1665 EXPECT_EQ(16, ftello(fp));
1666 ASSERT_EQ(0, fseeko(fp, 0, SEEK_END));
1667 EXPECT_EQ(16, ftell(fp));
1668 EXPECT_EQ(16, ftello(fp));
1669 ASSERT_EQ(0, fclose(fp));
1670}
1671
1672TEST(STDIO_TEST, fmemopen_SEEK_END) {
1673 // fseek SEEK_END is relative to the current string length, not the buffer size.
1674 FILE* fp;
1675 char buf[8];
1676 memset(buf, 'x', sizeof(buf));
1677 strcpy(buf, "str");
1678 ASSERT_NE(nullptr, fp = fmemopen(buf, sizeof(buf), "w+"));
1679 ASSERT_NE(EOF, fputs("string", fp));
1680 EXPECT_EQ(0, fseek(fp, 0, SEEK_END));
1681 EXPECT_EQ(static_cast<long>(strlen("string")), ftell(fp));
1682 EXPECT_EQ(static_cast<off_t>(strlen("string")), ftello(fp));
1683 EXPECT_EQ(0, fclose(fp));
1684
1685 // glibc < 2.22 interpreted SEEK_END the wrong way round (subtracting rather
1686 // than adding).
1687 ASSERT_NE(nullptr, fp = fmemopen(buf, sizeof(buf), "w+"));
1688 ASSERT_NE(EOF, fputs("54321", fp));
1689 EXPECT_EQ(0, fseek(fp, -2, SEEK_END));
1690 EXPECT_EQ('2', fgetc(fp));
1691 EXPECT_EQ(0, fclose(fp));
1692}
1693
1694TEST(STDIO_TEST, fmemopen_seek_invalid) {
1695 char buf[8];
1696 memset(buf, 'x', sizeof(buf));
1697 FILE* fp = fmemopen(buf, sizeof(buf), "w");
1698 ASSERT_TRUE(fp != nullptr);
1699
1700 // POSIX: "An attempt to seek ... to a negative position or to a position
1701 // larger than the buffer size given in the size argument shall fail."
1702 // (There's no mention of what errno should be set to, and glibc doesn't
1703 // set errno in any of these cases.)
1704 EXPECT_EQ(-1, fseek(fp, -2, SEEK_SET));
1705 EXPECT_EQ(-1, fseeko(fp, -2, SEEK_SET));
1706 EXPECT_EQ(-1, fseek(fp, sizeof(buf) + 1, SEEK_SET));
1707 EXPECT_EQ(-1, fseeko(fp, sizeof(buf) + 1, SEEK_SET));
1708}
1709
1710TEST(STDIO_TEST, fmemopen_read_EOF) {
1711 // POSIX: "A read operation on the stream shall not advance the current
1712 // buffer position beyond the current buffer size."
1713 char buf[8];
1714 memset(buf, 'x', sizeof(buf));
1715 FILE* fp = fmemopen(buf, sizeof(buf), "r");
1716 ASSERT_TRUE(fp != nullptr);
1717 char buf2[BUFSIZ];
1718 ASSERT_EQ(8U, fread(buf2, 1, sizeof(buf2), fp));
1719 // POSIX: "Reaching the buffer size in a read operation shall count as
1720 // end-of-file.
1721 ASSERT_TRUE(feof(fp));
1722 ASSERT_EQ(EOF, fgetc(fp));
1723 ASSERT_EQ(0, fclose(fp));
1724}
1725
1726TEST(STDIO_TEST, fmemopen_read_null_bytes) {
1727 // POSIX: "Null bytes in the buffer shall have no special meaning for reads."
1728 char buf[] = "h\0e\0l\0l\0o";
1729 FILE* fp = fmemopen(buf, sizeof(buf), "r");
1730 ASSERT_TRUE(fp != nullptr);
1731 ASSERT_EQ('h', fgetc(fp));
1732 ASSERT_EQ(0, fgetc(fp));
1733 ASSERT_EQ('e', fgetc(fp));
1734 ASSERT_EQ(0, fgetc(fp));
1735 ASSERT_EQ('l', fgetc(fp));
1736 ASSERT_EQ(0, fgetc(fp));
1737 // POSIX: "The read operation shall start at the current buffer position of
1738 // the stream."
1739 char buf2[8];
1740 memset(buf2, 'x', sizeof(buf2));
1741 ASSERT_EQ(4U, fread(buf2, 1, sizeof(buf2), fp));
1742 ASSERT_EQ('l', buf2[0]);
1743 ASSERT_EQ(0, buf2[1]);
1744 ASSERT_EQ('o', buf2[2]);
1745 ASSERT_EQ(0, buf2[3]);
1746 for (size_t i = 4; i < sizeof(buf2); ++i) ASSERT_EQ('x', buf2[i]) << i;
1747 ASSERT_TRUE(feof(fp));
1748 ASSERT_EQ(0, fclose(fp));
1749}
1750
1751TEST(STDIO_TEST, fmemopen_write) {
1752 FILE* fp;
1753 char buf[8];
1754
1755 // POSIX: "A write operation shall start either at the current position of
1756 // the stream (if mode has not specified 'a' as the first character)..."
1757 memset(buf, 'x', sizeof(buf));
1758 ASSERT_NE(nullptr, fp = fmemopen(buf, sizeof(buf), "r+"));
1759 setbuf(fp, nullptr); // Turn off buffering so we can see what's happening as it happens.
1760 ASSERT_EQ(0, fseek(fp, 2, SEEK_SET));
1761 ASSERT_EQ(' ', fputc(' ', fp));
1762 EXPECT_EQ("xx xxxxx", std::string(buf, buf + sizeof(buf)));
1763 ASSERT_EQ(0, fclose(fp));
1764
1765 // "...or at the current size of the stream (if mode had 'a' as the first
1766 // character)." (See the fmemopen_size test for what "size" means, but for
1767 // mode "a", it's the first NUL byte.)
1768 memset(buf, 'x', sizeof(buf));
1769 buf[3] = '\0';
1770 ASSERT_NE(nullptr, fp = fmemopen(buf, sizeof(buf), "a+"));
1771 setbuf(fp, nullptr); // Turn off buffering so we can see what's happening as it happens.
1772 ASSERT_EQ(' ', fputc(' ', fp));
1773 EXPECT_EQ("xxx \0xxx"s, std::string(buf, buf + sizeof(buf)));
1774 ASSERT_EQ(0, fclose(fp));
1775
1776 // "If the current position at the end of the write is larger than the
1777 // current buffer size, the current buffer size shall be set to the current
1778 // position." (See the fmemopen_size test for what "size" means, but to
1779 // query it we SEEK_END with offset 0, and then ftell.)
1780 memset(buf, 'x', sizeof(buf));
1781 ASSERT_NE(nullptr, fp = fmemopen(buf, sizeof(buf), "w+"));
1782 setbuf(fp, nullptr); // Turn off buffering so we can see what's happening as it happens.
1783 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
1784 EXPECT_EQ(0, ftell(fp));
1785 ASSERT_EQ(' ', fputc(' ', fp));
1786 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
1787 EXPECT_EQ(1, ftell(fp));
1788 ASSERT_NE(EOF, fputs("123", fp));
1789 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
1790 EXPECT_EQ(4, ftell(fp));
1791 EXPECT_EQ(" 123\0xxx"s, std::string(buf, buf + sizeof(buf)));
1792 ASSERT_EQ(0, fclose(fp));
1793}
1794
1795TEST(STDIO_TEST, fmemopen_write_EOF) {
1796 // POSIX: "A write operation on the stream shall not advance the current
1797 // buffer size beyond the size given in the size argument."
1798 FILE* fp;
1799
1800 // Scalar writes...
1801 ASSERT_NE(nullptr, fp = fmemopen(nullptr, 4, "w"));
1802 setbuf(fp, nullptr); // Turn off buffering so we can see what's happening as it happens.
1803 ASSERT_EQ('x', fputc('x', fp));
1804 ASSERT_EQ('x', fputc('x', fp));
1805 ASSERT_EQ('x', fputc('x', fp));
1806 ASSERT_EQ(EOF, fputc('x', fp)); // Only 3 fit because of the implicit NUL.
1807 ASSERT_EQ(0, fclose(fp));
1808
1809 // Vector writes...
1810 ASSERT_NE(nullptr, fp = fmemopen(nullptr, 4, "w"));
1811 setbuf(fp, nullptr); // Turn off buffering so we can see what's happening as it happens.
1812 ASSERT_EQ(3U, fwrite("xxxx", 1, 4, fp));
1813 ASSERT_EQ(0, fclose(fp));
1814}
1815
1816TEST(STDIO_TEST, fmemopen_initial_position) {
1817 // POSIX: "The ... current position in the buffer ... shall be initially
1818 // set to either the beginning of the buffer (for r and w modes) ..."
1819 char buf[] = "hello\0world";
1820 FILE* fp;
1821 ASSERT_NE(nullptr, fp = fmemopen(buf, sizeof(buf), "r"));
1822 EXPECT_EQ(0L, ftell(fp));
1823 EXPECT_EQ(0, fclose(fp));
1824 ASSERT_NE(nullptr, fp = fmemopen(buf, sizeof(buf), "w"));
1825 EXPECT_EQ(0L, ftell(fp));
1826 EXPECT_EQ(0, fclose(fp));
1827 buf[0] = 'h'; // (Undo the effects of the above.)
1828
1829 // POSIX: "...or to the first null byte in the buffer (for a modes)."
1830 ASSERT_NE(nullptr, fp = fmemopen(buf, sizeof(buf), "a"));
1831 EXPECT_EQ(5L, ftell(fp));
1832 EXPECT_EQ(0, fclose(fp));
1833
1834 // POSIX: "If no null byte is found in append mode, the initial position
1835 // shall be set to one byte after the end of the buffer."
1836 memset(buf, 'x', sizeof(buf));
1837 ASSERT_NE(nullptr, fp = fmemopen(buf, sizeof(buf), "a"));
1838 EXPECT_EQ(static_cast<long>(sizeof(buf)), ftell(fp));
1839 EXPECT_EQ(0, fclose(fp));
1840}
1841
1842TEST(STDIO_TEST, fmemopen_initial_position_allocated) {
1843 // POSIX: "If buf is a null pointer, the initial position shall always be
1844 // set to the beginning of the buffer."
1845 FILE* fp = fmemopen(nullptr, 128, "a+");
1846 ASSERT_TRUE(fp != nullptr);
1847 EXPECT_EQ(0L, ftell(fp));
1848 EXPECT_EQ(0L, fseek(fp, 0, SEEK_SET));
1849 EXPECT_EQ(0, fclose(fp));
1850}
1851
1852TEST(STDIO_TEST, fmemopen_zero_length) {
1853 // POSIX says it's up to the implementation whether or not you can have a
1854 // zero-length buffer (but "A future version of this standard may require
1855 // support of zero-length buffer streams explicitly"). BSD and glibc < 2.22
1856 // agreed that you couldn't, but glibc >= 2.22 allows it for consistency.
1857 FILE* fp;
1858 char buf[16];
1859 ASSERT_NE(nullptr, fp = fmemopen(buf, 0, "r+"));
1860 ASSERT_EQ(EOF, fgetc(fp));
1861 ASSERT_TRUE(feof(fp));
1862 ASSERT_EQ(0, fclose(fp));
1863 ASSERT_NE(nullptr, fp = fmemopen(nullptr, 0, "r+"));
1864 ASSERT_EQ(EOF, fgetc(fp));
1865 ASSERT_TRUE(feof(fp));
1866 ASSERT_EQ(0, fclose(fp));
1867
1868 ASSERT_NE(nullptr, fp = fmemopen(buf, 0, "w+"));
1869 setbuf(fp, nullptr); // Turn off buffering so we can see what's happening as it happens.
1870 ASSERT_EQ(EOF, fputc('x', fp));
1871 ASSERT_EQ(0, fclose(fp));
1872 ASSERT_NE(nullptr, fp = fmemopen(nullptr, 0, "w+"));
1873 setbuf(fp, nullptr); // Turn off buffering so we can see what's happening as it happens.
1874 ASSERT_EQ(EOF, fputc('x', fp));
1875 ASSERT_EQ(0, fclose(fp));
1876}
1877
Elliott Hughes288465d2019-02-05 15:00:13 -08001878TEST(STDIO_TEST, fmemopen_zero_length_buffer_overrun) {
1879 char buf[2] = "x";
1880 ASSERT_EQ('x', buf[0]);
1881 FILE* fp = fmemopen(buf, 0, "w");
1882 ASSERT_EQ('x', buf[0]);
1883 ASSERT_EQ(0, fclose(fp));
1884}
1885
Elliott Hughes3a4c4542017-07-19 17:20:24 -07001886TEST(STDIO_TEST, fmemopen_write_only_allocated) {
1887 // POSIX says fmemopen "may fail if the mode argument does not include a '+'".
1888 // BSD fails, glibc doesn't. We side with the more lenient.
1889 FILE* fp;
1890 ASSERT_NE(nullptr, fp = fmemopen(nullptr, 16, "r"));
1891 ASSERT_EQ(0, fclose(fp));
1892 ASSERT_NE(nullptr, fp = fmemopen(nullptr, 16, "w"));
1893 ASSERT_EQ(0, fclose(fp));
1894}
1895
1896TEST(STDIO_TEST, fmemopen_fileno) {
1897 // There's no fd backing an fmemopen FILE*.
1898 FILE* fp = fmemopen(nullptr, 16, "r");
1899 ASSERT_TRUE(fp != nullptr);
Elliott Hughes6b841db2014-08-20 16:10:49 -07001900 errno = 0;
Elliott Hughes3a4c4542017-07-19 17:20:24 -07001901 ASSERT_EQ(-1, fileno(fp));
1902 ASSERT_EQ(EBADF, errno);
1903 ASSERT_EQ(0, fclose(fp));
1904}
1905
1906TEST(STDIO_TEST, fmemopen_append_after_seek) {
1907 // In BSD and glibc < 2.22, append mode didn't force writes to append if
1908 // there had been an intervening seek.
1909
1910 FILE* fp;
1911 char buf[] = "hello\0world";
1912 ASSERT_NE(nullptr, fp = fmemopen(buf, sizeof(buf), "a"));
1913 setbuf(fp, nullptr); // Turn off buffering so we can see what's happening as it happens.
1914 ASSERT_EQ(0, fseek(fp, 0, SEEK_SET));
1915 ASSERT_NE(EOF, fputc('!', fp));
1916 EXPECT_EQ("hello!\0orld\0"s, std::string(buf, buf + sizeof(buf)));
1917 ASSERT_EQ(0, fclose(fp));
1918
1919 memcpy(buf, "hello\0world", sizeof(buf));
1920 ASSERT_NE(nullptr, fp = fmemopen(buf, sizeof(buf), "a+"));
1921 setbuf(fp, nullptr); // Turn off buffering so we can see what's happening as it happens.
1922 ASSERT_EQ(0, fseek(fp, 0, SEEK_SET));
1923 ASSERT_NE(EOF, fputc('!', fp));
1924 EXPECT_EQ("hello!\0orld\0"s, std::string(buf, buf + sizeof(buf)));
1925 ASSERT_EQ(0, fclose(fp));
Elliott Hughes6b841db2014-08-20 16:10:49 -07001926}
1927
Christopher Ferris13f26a72016-01-13 13:47:58 -08001928TEST(STDIO_TEST, open_memstream) {
Elliott Hughes6b841db2014-08-20 16:10:49 -07001929 char* p = nullptr;
1930 size_t size = 0;
1931 FILE* fp = open_memstream(&p, &size);
1932 ASSERT_NE(EOF, fputs("hello, world!", fp));
1933 fclose(fp);
1934
1935 ASSERT_STREQ("hello, world!", p);
1936 ASSERT_EQ(strlen("hello, world!"), size);
1937 free(p);
1938}
1939
Christopher Ferris13f26a72016-01-13 13:47:58 -08001940TEST(STDIO_TEST, open_memstream_EINVAL) {
Elliott Hughes6b841db2014-08-20 16:10:49 -07001941#if defined(__BIONIC__)
1942 char* p;
1943 size_t size;
1944
1945 // Invalid buffer.
1946 errno = 0;
1947 ASSERT_EQ(nullptr, open_memstream(nullptr, &size));
1948 ASSERT_EQ(EINVAL, errno);
1949
1950 // Invalid size.
1951 errno = 0;
1952 ASSERT_EQ(nullptr, open_memstream(&p, nullptr));
1953 ASSERT_EQ(EINVAL, errno);
1954#else
Elliott Hughesbcaa4542019-03-08 15:20:23 -08001955 GTEST_SKIP() << "glibc is broken";
Elliott Hughes6b841db2014-08-20 16:10:49 -07001956#endif
1957}
Elliott Hughes31165ed2014-09-23 17:34:29 -07001958
Elliott Hughesf9cfecf2021-02-04 16:58:13 -08001959TEST(STDIO_TEST, fdopen_add_CLOEXEC) {
Elliott Hughes31165ed2014-09-23 17:34:29 -07001960 // This fd doesn't have O_CLOEXEC...
Elliott Hughesf9cfecf2021-02-04 16:58:13 -08001961 int fd = open("/proc/version", O_RDONLY);
1962 ASSERT_FALSE(CloseOnExec(fd));
Elliott Hughes31165ed2014-09-23 17:34:29 -07001963 // ...but the new one does.
Elliott Hughesf9cfecf2021-02-04 16:58:13 -08001964 FILE* fp = fdopen(fd, "re");
1965 ASSERT_TRUE(CloseOnExec(fileno(fp)));
1966 fclose(fp);
1967}
1968
1969TEST(STDIO_TEST, fdopen_remove_CLOEXEC) {
1970 // This fd has O_CLOEXEC...
1971 int fd = open("/proc/version", O_RDONLY | O_CLOEXEC);
1972 ASSERT_TRUE(CloseOnExec(fd));
1973 // ...but the new one doesn't.
1974 FILE* fp = fdopen(fd, "r");
1975 ASSERT_TRUE(CloseOnExec(fileno(fp)));
1976 fclose(fp);
1977}
1978
1979TEST(STDIO_TEST, freopen_add_CLOEXEC) {
1980 // This FILE* doesn't have O_CLOEXEC...
1981 FILE* fp = fopen("/proc/version", "r");
1982 ASSERT_FALSE(CloseOnExec(fileno(fp)));
1983 // ...but the new one does.
1984 fp = freopen("/proc/version", "re", fp);
1985 ASSERT_TRUE(CloseOnExec(fileno(fp)));
Elliott Hughes31165ed2014-09-23 17:34:29 -07001986
1987 fclose(fp);
Elliott Hughes31165ed2014-09-23 17:34:29 -07001988}
1989
Elliott Hughesf9cfecf2021-02-04 16:58:13 -08001990TEST(STDIO_TEST, freopen_remove_CLOEXEC) {
1991 // This FILE* has O_CLOEXEC...
1992 FILE* fp = fopen("/proc/version", "re");
1993 ASSERT_TRUE(CloseOnExec(fileno(fp)));
1994 // ...but the new one doesn't.
1995 fp = freopen("/proc/version", "r", fp);
1996 ASSERT_FALSE(CloseOnExec(fileno(fp)));
1997 fclose(fp);
1998}
Elliott Hughes31165ed2014-09-23 17:34:29 -07001999
Elliott Hughesf9cfecf2021-02-04 16:58:13 -08002000TEST(STDIO_TEST, freopen_null_filename_add_CLOEXEC) {
Elliott Hughes31165ed2014-09-23 17:34:29 -07002001 // This FILE* doesn't have O_CLOEXEC...
Elliott Hughesf9cfecf2021-02-04 16:58:13 -08002002 FILE* fp = fopen("/proc/version", "r");
2003 ASSERT_FALSE(CloseOnExec(fileno(fp)));
Elliott Hughes31165ed2014-09-23 17:34:29 -07002004 // ...but the new one does.
Elliott Hughesf9cfecf2021-02-04 16:58:13 -08002005 fp = freopen(nullptr, "re", fp);
2006 ASSERT_TRUE(CloseOnExec(fileno(fp)));
2007 fclose(fp);
2008}
Elliott Hughes31165ed2014-09-23 17:34:29 -07002009
Elliott Hughesf9cfecf2021-02-04 16:58:13 -08002010TEST(STDIO_TEST, freopen_null_filename_remove_CLOEXEC) {
2011 // This FILE* has O_CLOEXEC...
2012 FILE* fp = fopen("/proc/version", "re");
2013 ASSERT_TRUE(CloseOnExec(fileno(fp)));
2014 // ...but the new one doesn't.
2015 fp = freopen(nullptr, "r", fp);
2016 ASSERT_FALSE(CloseOnExec(fileno(fp)));
Elliott Hughes31165ed2014-09-23 17:34:29 -07002017 fclose(fp);
2018}
Elliott Hughes20841a12014-12-01 16:13:30 -08002019
Elliott Hughesf226ee52016-02-03 11:24:28 -08002020TEST(STDIO_TEST, fopen64_freopen64) {
2021 FILE* fp = fopen64("/proc/version", "r");
2022 ASSERT_TRUE(fp != nullptr);
2023 fp = freopen64("/proc/version", "re", fp);
2024 ASSERT_TRUE(fp != nullptr);
2025 fclose(fp);
2026}
2027
Elliott Hughes20841a12014-12-01 16:13:30 -08002028// https://code.google.com/p/android/issues/detail?id=81155
2029// http://b/18556607
Christopher Ferris13f26a72016-01-13 13:47:58 -08002030TEST(STDIO_TEST, fread_unbuffered_pathological_performance) {
Elliott Hughes20841a12014-12-01 16:13:30 -08002031 FILE* fp = fopen("/dev/zero", "r");
Yi Kong32bc0fc2018-08-02 17:31:13 -07002032 ASSERT_TRUE(fp != nullptr);
Elliott Hughes20841a12014-12-01 16:13:30 -08002033
2034 // Make this stream unbuffered.
Yi Kong32bc0fc2018-08-02 17:31:13 -07002035 setvbuf(fp, nullptr, _IONBF, 0);
Elliott Hughes20841a12014-12-01 16:13:30 -08002036
2037 char buf[65*1024];
2038 memset(buf, 0xff, sizeof(buf));
2039
Yi Kong32bc0fc2018-08-02 17:31:13 -07002040 time_t t0 = time(nullptr);
Elliott Hughes20841a12014-12-01 16:13:30 -08002041 for (size_t i = 0; i < 1024; ++i) {
Elliott Hughese6bb5a22015-01-23 17:48:15 -08002042 ASSERT_EQ(1U, fread(buf, 64*1024, 1, fp));
Elliott Hughes20841a12014-12-01 16:13:30 -08002043 }
Yi Kong32bc0fc2018-08-02 17:31:13 -07002044 time_t t1 = time(nullptr);
Elliott Hughes20841a12014-12-01 16:13:30 -08002045
2046 fclose(fp);
2047
2048 // 1024 64KiB reads should have been very quick.
2049 ASSERT_LE(t1 - t0, 1);
2050
2051 for (size_t i = 0; i < 64*1024; ++i) {
2052 ASSERT_EQ('\0', buf[i]);
2053 }
2054 for (size_t i = 64*1024; i < 65*1024; ++i) {
2055 ASSERT_EQ('\xff', buf[i]);
2056 }
2057}
Elliott Hughes75b99382015-01-20 11:23:50 -08002058
Christopher Ferris13f26a72016-01-13 13:47:58 -08002059TEST(STDIO_TEST, fread_EOF) {
Elliott Hughes0ed7e082015-01-22 15:13:38 -08002060 std::string digits("0123456789");
2061 FILE* fp = fmemopen(&digits[0], digits.size(), "r");
Elliott Hughes75b99382015-01-20 11:23:50 -08002062
2063 // Try to read too much, but little enough that it still fits in the FILE's internal buffer.
2064 char buf1[4 * 4];
2065 memset(buf1, 0, sizeof(buf1));
2066 ASSERT_EQ(2U, fread(buf1, 4, 4, fp));
Elliott Hughes0ed7e082015-01-22 15:13:38 -08002067 ASSERT_STREQ("0123456789", buf1);
Elliott Hughes75b99382015-01-20 11:23:50 -08002068 ASSERT_TRUE(feof(fp));
2069
2070 rewind(fp);
2071
Elliott Hughes0ed7e082015-01-22 15:13:38 -08002072 // Try to read way too much so stdio tries to read more direct from the stream.
2073 char buf2[4 * 4096];
Elliott Hughes75b99382015-01-20 11:23:50 -08002074 memset(buf2, 0, sizeof(buf2));
2075 ASSERT_EQ(2U, fread(buf2, 4, 4096, fp));
Elliott Hughes0ed7e082015-01-22 15:13:38 -08002076 ASSERT_STREQ("0123456789", buf2);
Elliott Hughes75b99382015-01-20 11:23:50 -08002077 ASSERT_TRUE(feof(fp));
2078
2079 fclose(fp);
2080}
Elliott Hughese6bb5a22015-01-23 17:48:15 -08002081
2082static void test_fread_from_write_only_stream(size_t n) {
2083 FILE* fp = fopen("/dev/null", "w");
2084 std::vector<char> buf(n, 0);
2085 errno = 0;
2086 ASSERT_EQ(0U, fread(&buf[0], n, 1, fp));
2087 ASSERT_EQ(EBADF, errno);
2088 ASSERT_TRUE(ferror(fp));
2089 ASSERT_FALSE(feof(fp));
2090 fclose(fp);
2091}
2092
Christopher Ferris13f26a72016-01-13 13:47:58 -08002093TEST(STDIO_TEST, fread_from_write_only_stream_slow_path) {
Elliott Hughese6bb5a22015-01-23 17:48:15 -08002094 test_fread_from_write_only_stream(1);
2095}
2096
Christopher Ferris13f26a72016-01-13 13:47:58 -08002097TEST(STDIO_TEST, fread_from_write_only_stream_fast_path) {
Elliott Hughese6bb5a22015-01-23 17:48:15 -08002098 test_fread_from_write_only_stream(64*1024);
2099}
2100
2101static void test_fwrite_after_fread(size_t n) {
2102 TemporaryFile tf;
2103
2104 FILE* fp = fdopen(tf.fd, "w+");
2105 ASSERT_EQ(1U, fwrite("1", 1, 1, fp));
2106 fflush(fp);
2107
2108 // We've flushed but not rewound, so there's nothing to read.
2109 std::vector<char> buf(n, 0);
2110 ASSERT_EQ(0U, fread(&buf[0], 1, buf.size(), fp));
2111 ASSERT_TRUE(feof(fp));
2112
2113 // But hitting EOF doesn't prevent us from writing...
2114 errno = 0;
Elliott Hughes9677fab2016-01-25 15:50:59 -08002115 ASSERT_EQ(1U, fwrite("2", 1, 1, fp)) << strerror(errno);
Elliott Hughese6bb5a22015-01-23 17:48:15 -08002116
2117 // And if we rewind, everything's there.
2118 rewind(fp);
2119 ASSERT_EQ(2U, fread(&buf[0], 1, buf.size(), fp));
2120 ASSERT_EQ('1', buf[0]);
2121 ASSERT_EQ('2', buf[1]);
2122
2123 fclose(fp);
2124}
2125
Christopher Ferris13f26a72016-01-13 13:47:58 -08002126TEST(STDIO_TEST, fwrite_after_fread_slow_path) {
Elliott Hughese6bb5a22015-01-23 17:48:15 -08002127 test_fwrite_after_fread(16);
2128}
2129
Christopher Ferris13f26a72016-01-13 13:47:58 -08002130TEST(STDIO_TEST, fwrite_after_fread_fast_path) {
Elliott Hughese6bb5a22015-01-23 17:48:15 -08002131 test_fwrite_after_fread(64*1024);
2132}
Christopher Ferriscc9ca102015-02-27 18:22:45 -08002133
2134// http://b/19172514
Christopher Ferris13f26a72016-01-13 13:47:58 -08002135TEST(STDIO_TEST, fread_after_fseek) {
Christopher Ferriscc9ca102015-02-27 18:22:45 -08002136 TemporaryFile tf;
2137
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -08002138 FILE* fp = fopen(tf.path, "w+");
Christopher Ferriscc9ca102015-02-27 18:22:45 -08002139 ASSERT_TRUE(fp != nullptr);
2140
2141 char file_data[12288];
2142 for (size_t i = 0; i < 12288; i++) {
2143 file_data[i] = i;
2144 }
2145 ASSERT_EQ(12288U, fwrite(file_data, 1, 12288, fp));
2146 fclose(fp);
2147
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -08002148 fp = fopen(tf.path, "r");
Christopher Ferriscc9ca102015-02-27 18:22:45 -08002149 ASSERT_TRUE(fp != nullptr);
2150
2151 char buffer[8192];
2152 size_t cur_location = 0;
2153 // Small read to populate internal buffer.
2154 ASSERT_EQ(100U, fread(buffer, 1, 100, fp));
2155 ASSERT_EQ(memcmp(file_data, buffer, 100), 0);
2156
2157 cur_location = static_cast<size_t>(ftell(fp));
2158 // Large read to force reading into the user supplied buffer and bypassing
2159 // the internal buffer.
2160 ASSERT_EQ(8192U, fread(buffer, 1, 8192, fp));
2161 ASSERT_EQ(memcmp(file_data+cur_location, buffer, 8192), 0);
2162
2163 // Small backwards seek to verify fseek does not reuse the internal buffer.
Elliott Hughes9677fab2016-01-25 15:50:59 -08002164 ASSERT_EQ(0, fseek(fp, -22, SEEK_CUR)) << strerror(errno);
Christopher Ferriscc9ca102015-02-27 18:22:45 -08002165 cur_location = static_cast<size_t>(ftell(fp));
2166 ASSERT_EQ(22U, fread(buffer, 1, 22, fp));
2167 ASSERT_EQ(memcmp(file_data+cur_location, buffer, 22), 0);
2168
2169 fclose(fp);
2170}
Elliott Hughes8ab433d2015-10-09 17:57:26 -07002171
2172// https://code.google.com/p/android/issues/detail?id=184847
Christopher Ferris13f26a72016-01-13 13:47:58 -08002173TEST(STDIO_TEST, fread_EOF_184847) {
Elliott Hughes8ab433d2015-10-09 17:57:26 -07002174 TemporaryFile tf;
2175 char buf[6] = {0};
2176
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -08002177 FILE* fw = fopen(tf.path, "w");
Elliott Hughes8ab433d2015-10-09 17:57:26 -07002178 ASSERT_TRUE(fw != nullptr);
2179
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -08002180 FILE* fr = fopen(tf.path, "r");
Elliott Hughes8ab433d2015-10-09 17:57:26 -07002181 ASSERT_TRUE(fr != nullptr);
2182
2183 fwrite("a", 1, 1, fw);
2184 fflush(fw);
2185 ASSERT_EQ(1U, fread(buf, 1, 1, fr));
2186 ASSERT_STREQ("a", buf);
2187
2188 // 'fr' is now at EOF.
2189 ASSERT_EQ(0U, fread(buf, 1, 1, fr));
2190 ASSERT_TRUE(feof(fr));
2191
2192 // Write some more...
2193 fwrite("z", 1, 1, fw);
2194 fflush(fw);
2195
2196 // ...and check that we can read it back.
2197 // (BSD thinks that once a stream has hit EOF, it must always return EOF. SysV disagrees.)
2198 ASSERT_EQ(1U, fread(buf, 1, 1, fr));
2199 ASSERT_STREQ("z", buf);
2200
2201 // But now we're done.
2202 ASSERT_EQ(0U, fread(buf, 1, 1, fr));
2203
2204 fclose(fr);
2205 fclose(fw);
2206}
Elliott Hughes923f1652016-01-19 15:46:05 -08002207
2208TEST(STDIO_TEST, fclose_invalidates_fd) {
2209 // The typical error we're trying to help people catch involves accessing
2210 // memory after it's been freed. But we know that stdin/stdout/stderr are
2211 // special and don't get deallocated, so this test uses stdin.
2212 ASSERT_EQ(0, fclose(stdin));
2213
2214 // Even though using a FILE* after close is undefined behavior, I've closed
2215 // this bug as "WAI" too many times. We shouldn't hand out stale fds,
2216 // especially because they might actually correspond to a real stream.
2217 errno = 0;
2218 ASSERT_EQ(-1, fileno(stdin));
2219 ASSERT_EQ(EBADF, errno);
2220}
Elliott Hughes2704bd12016-01-20 17:14:53 -08002221
2222TEST(STDIO_TEST, fseek_ftell_unseekable) {
2223#if defined(__BIONIC__) // glibc has fopencookie instead.
2224 auto read_fn = [](void*, char*, int) { return -1; };
2225 FILE* fp = funopen(nullptr, read_fn, nullptr, nullptr, nullptr);
2226 ASSERT_TRUE(fp != nullptr);
2227
2228 // Check that ftell balks on an unseekable FILE*.
2229 errno = 0;
2230 ASSERT_EQ(-1, ftell(fp));
2231 ASSERT_EQ(ESPIPE, errno);
2232
2233 // SEEK_CUR is rewritten as SEEK_SET internally...
2234 errno = 0;
2235 ASSERT_EQ(-1, fseek(fp, 0, SEEK_CUR));
2236 ASSERT_EQ(ESPIPE, errno);
2237
2238 // ...so it's worth testing the direct seek path too.
2239 errno = 0;
2240 ASSERT_EQ(-1, fseek(fp, 0, SEEK_SET));
2241 ASSERT_EQ(ESPIPE, errno);
2242
2243 fclose(fp);
Elliott Hughes03e65eb2016-01-26 14:13:04 -08002244#else
Elliott Hughesbcaa4542019-03-08 15:20:23 -08002245 GTEST_SKIP() << "glibc uses fopencookie instead";
Elliott Hughes03e65eb2016-01-26 14:13:04 -08002246#endif
2247}
2248
2249TEST(STDIO_TEST, funopen_EINVAL) {
2250#if defined(__BIONIC__)
2251 errno = 0;
2252 ASSERT_EQ(nullptr, funopen(nullptr, nullptr, nullptr, nullptr, nullptr));
2253 ASSERT_EQ(EINVAL, errno);
2254#else
Elliott Hughesbcaa4542019-03-08 15:20:23 -08002255 GTEST_SKIP() << "glibc uses fopencookie instead";
Elliott Hughes03e65eb2016-01-26 14:13:04 -08002256#endif
2257}
2258
2259TEST(STDIO_TEST, funopen_seek) {
2260#if defined(__BIONIC__)
2261 auto read_fn = [](void*, char*, int) { return -1; };
2262
2263 auto seek_fn = [](void*, fpos_t, int) -> fpos_t { return 0xfedcba12; };
2264 auto seek64_fn = [](void*, fpos64_t, int) -> fpos64_t { return 0xfedcba12345678; };
2265
2266 FILE* fp = funopen(nullptr, read_fn, nullptr, seek_fn, nullptr);
2267 ASSERT_TRUE(fp != nullptr);
2268 fpos_t pos;
Elliott Hughes955426e2016-01-26 18:25:52 -08002269#if defined(__LP64__)
2270 EXPECT_EQ(0, fgetpos(fp, &pos)) << strerror(errno);
2271 EXPECT_EQ(0xfedcba12LL, pos);
2272#else
2273 EXPECT_EQ(-1, fgetpos(fp, &pos)) << strerror(errno);
2274 EXPECT_EQ(EOVERFLOW, errno);
2275#endif
Elliott Hughes03e65eb2016-01-26 14:13:04 -08002276
2277 FILE* fp64 = funopen64(nullptr, read_fn, nullptr, seek64_fn, nullptr);
2278 ASSERT_TRUE(fp64 != nullptr);
2279 fpos64_t pos64;
Elliott Hughes955426e2016-01-26 18:25:52 -08002280 EXPECT_EQ(0, fgetpos64(fp64, &pos64)) << strerror(errno);
2281 EXPECT_EQ(0xfedcba12345678, pos64);
Elliott Hughes03e65eb2016-01-26 14:13:04 -08002282#else
Elliott Hughesbcaa4542019-03-08 15:20:23 -08002283 GTEST_SKIP() << "glibc uses fopencookie instead";
Elliott Hughes2704bd12016-01-20 17:14:53 -08002284#endif
2285}
Elliott Hughes71288cb2016-01-22 19:22:44 -08002286
2287TEST(STDIO_TEST, lots_of_concurrent_files) {
2288 std::vector<TemporaryFile*> tfs;
2289 std::vector<FILE*> fps;
2290
2291 for (size_t i = 0; i < 256; ++i) {
2292 TemporaryFile* tf = new TemporaryFile;
2293 tfs.push_back(tf);
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -08002294 FILE* fp = fopen(tf->path, "w+");
Elliott Hughes71288cb2016-01-22 19:22:44 -08002295 fps.push_back(fp);
2296 fprintf(fp, "hello %zu!\n", i);
2297 fflush(fp);
2298 }
2299
2300 for (size_t i = 0; i < 256; ++i) {
Elliott Hughes71288cb2016-01-22 19:22:44 -08002301 char expected[BUFSIZ];
2302 snprintf(expected, sizeof(expected), "hello %zu!\n", i);
Elliott Hughes71288cb2016-01-22 19:22:44 -08002303
Elliott Hughes70715da2016-08-01 16:35:17 -07002304 AssertFileIs(fps[i], expected);
Elliott Hughes71288cb2016-01-22 19:22:44 -08002305 fclose(fps[i]);
2306 delete tfs[i];
2307 }
2308}
Elliott Hughes9677fab2016-01-25 15:50:59 -08002309
2310static void AssertFileOffsetAt(FILE* fp, off64_t offset) {
2311 EXPECT_EQ(offset, ftell(fp));
2312 EXPECT_EQ(offset, ftello(fp));
Elliott Hughese4fa6e92016-02-02 22:39:15 -08002313 EXPECT_EQ(offset, ftello64(fp));
Elliott Hughes9677fab2016-01-25 15:50:59 -08002314 fpos_t pos;
2315 fpos64_t pos64;
2316 EXPECT_EQ(0, fgetpos(fp, &pos));
2317 EXPECT_EQ(0, fgetpos64(fp, &pos64));
2318#if defined(__BIONIC__)
2319 EXPECT_EQ(offset, static_cast<off64_t>(pos));
2320 EXPECT_EQ(offset, static_cast<off64_t>(pos64));
2321#else
Elliott Hughesbcaa4542019-03-08 15:20:23 -08002322 GTEST_SKIP() << "glibc's fpos_t is opaque";
Elliott Hughes9677fab2016-01-25 15:50:59 -08002323#endif
2324}
2325
2326TEST(STDIO_TEST, seek_tell_family_smoke) {
2327 TemporaryFile tf;
2328 FILE* fp = fdopen(tf.fd, "w+");
2329
2330 // Initially we should be at 0.
2331 AssertFileOffsetAt(fp, 0);
2332
2333 // Seek to offset 8192.
2334 ASSERT_EQ(0, fseek(fp, 8192, SEEK_SET));
2335 AssertFileOffsetAt(fp, 8192);
2336 fpos_t eight_k_pos;
2337 ASSERT_EQ(0, fgetpos(fp, &eight_k_pos));
2338
2339 // Seek forward another 8192...
2340 ASSERT_EQ(0, fseek(fp, 8192, SEEK_CUR));
2341 AssertFileOffsetAt(fp, 8192 + 8192);
2342 fpos64_t sixteen_k_pos64;
2343 ASSERT_EQ(0, fgetpos64(fp, &sixteen_k_pos64));
2344
2345 // Seek back 8192...
2346 ASSERT_EQ(0, fseek(fp, -8192, SEEK_CUR));
2347 AssertFileOffsetAt(fp, 8192);
2348
2349 // Since we haven't written anything, the end is also at 0.
2350 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
2351 AssertFileOffsetAt(fp, 0);
2352
2353 // Check that our fpos64_t from 16KiB works...
2354 ASSERT_EQ(0, fsetpos64(fp, &sixteen_k_pos64));
2355 AssertFileOffsetAt(fp, 8192 + 8192);
2356 // ...as does our fpos_t from 8192.
2357 ASSERT_EQ(0, fsetpos(fp, &eight_k_pos));
2358 AssertFileOffsetAt(fp, 8192);
2359
2360 // Do fseeko and fseeko64 work too?
2361 ASSERT_EQ(0, fseeko(fp, 1234, SEEK_SET));
2362 AssertFileOffsetAt(fp, 1234);
2363 ASSERT_EQ(0, fseeko64(fp, 5678, SEEK_SET));
2364 AssertFileOffsetAt(fp, 5678);
2365
2366 fclose(fp);
2367}
2368
2369TEST(STDIO_TEST, fseek_fseeko_EINVAL) {
2370 TemporaryFile tf;
2371 FILE* fp = fdopen(tf.fd, "w+");
2372
2373 // Bad whence.
2374 errno = 0;
2375 ASSERT_EQ(-1, fseek(fp, 0, 123));
2376 ASSERT_EQ(EINVAL, errno);
2377 errno = 0;
2378 ASSERT_EQ(-1, fseeko(fp, 0, 123));
2379 ASSERT_EQ(EINVAL, errno);
2380 errno = 0;
2381 ASSERT_EQ(-1, fseeko64(fp, 0, 123));
2382 ASSERT_EQ(EINVAL, errno);
2383
2384 // Bad offset.
2385 errno = 0;
2386 ASSERT_EQ(-1, fseek(fp, -1, SEEK_SET));
2387 ASSERT_EQ(EINVAL, errno);
2388 errno = 0;
2389 ASSERT_EQ(-1, fseeko(fp, -1, SEEK_SET));
2390 ASSERT_EQ(EINVAL, errno);
2391 errno = 0;
2392 ASSERT_EQ(-1, fseeko64(fp, -1, SEEK_SET));
2393 ASSERT_EQ(EINVAL, errno);
2394
2395 fclose(fp);
2396}
Elliott Hughes20788ae2016-06-09 15:16:32 -07002397
2398TEST(STDIO_TEST, ctermid) {
2399 ASSERT_STREQ("/dev/tty", ctermid(nullptr));
2400
2401 char buf[L_ctermid] = {};
2402 ASSERT_EQ(buf, ctermid(buf));
2403 ASSERT_STREQ("/dev/tty", buf);
2404}
Elliott Hughesd1f25a72016-08-05 15:53:03 -07002405
2406TEST(STDIO_TEST, remove) {
2407 struct stat sb;
2408
2409 TemporaryFile tf;
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -08002410 ASSERT_EQ(0, remove(tf.path));
2411 ASSERT_EQ(-1, lstat(tf.path, &sb));
Elliott Hughesd1f25a72016-08-05 15:53:03 -07002412 ASSERT_EQ(ENOENT, errno);
2413
2414 TemporaryDir td;
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -08002415 ASSERT_EQ(0, remove(td.path));
2416 ASSERT_EQ(-1, lstat(td.path, &sb));
Elliott Hughesd1f25a72016-08-05 15:53:03 -07002417 ASSERT_EQ(ENOENT, errno);
2418
2419 errno = 0;
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -08002420 ASSERT_EQ(-1, remove(tf.path));
Elliott Hughesd1f25a72016-08-05 15:53:03 -07002421 ASSERT_EQ(ENOENT, errno);
2422
2423 errno = 0;
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -08002424 ASSERT_EQ(-1, remove(td.path));
Elliott Hughesd1f25a72016-08-05 15:53:03 -07002425 ASSERT_EQ(ENOENT, errno);
2426}
Elliott Hughesfb3873d2016-08-10 11:07:54 -07002427
Elliott Hughese657eb42021-02-18 17:11:56 -08002428TEST_F(STDIO_DEATHTEST, snprintf_30445072_known_buffer_size) {
Elliott Hughesfb3873d2016-08-10 11:07:54 -07002429 char buf[16];
2430 ASSERT_EXIT(snprintf(buf, atol("-1"), "hello"),
2431 testing::KilledBySignal(SIGABRT),
2432#if defined(NOFORTIFY)
2433 "FORTIFY: vsnprintf: size .* > SSIZE_MAX"
2434#else
2435 "FORTIFY: vsnprintf: prevented .*-byte write into 16-byte buffer"
2436#endif
2437 );
2438}
2439
Elliott Hughese657eb42021-02-18 17:11:56 -08002440TEST_F(STDIO_DEATHTEST, snprintf_30445072_unknown_buffer_size) {
Elliott Hughesfb3873d2016-08-10 11:07:54 -07002441 std::string buf = "world";
2442 ASSERT_EXIT(snprintf(&buf[0], atol("-1"), "hello"),
2443 testing::KilledBySignal(SIGABRT),
2444 "FORTIFY: vsnprintf: size .* > SSIZE_MAX");
2445}
2446
2447TEST(STDIO_TEST, sprintf_30445072) {
2448 std::string buf = "world";
2449 sprintf(&buf[0], "hello");
2450 ASSERT_EQ(buf, "hello");
2451}
Elliott Hughes33a8cb12017-07-25 18:06:46 -07002452
Elliott Hughes654cd832018-08-30 16:00:42 -07002453TEST(STDIO_TEST, printf_m) {
2454 char buf[BUFSIZ];
2455 errno = 0;
2456 snprintf(buf, sizeof(buf), "<%m>");
2457 ASSERT_STREQ("<Success>", buf);
2458 errno = -1;
2459 snprintf(buf, sizeof(buf), "<%m>");
2460 ASSERT_STREQ("<Unknown error -1>", buf);
2461 errno = EINVAL;
2462 snprintf(buf, sizeof(buf), "<%m>");
2463 ASSERT_STREQ("<Invalid argument>", buf);
2464}
2465
Elliott Hughesf340a562018-09-06 10:42:40 -07002466TEST(STDIO_TEST, printf_m_does_not_clobber_strerror) {
2467 char buf[BUFSIZ];
2468 const char* m = strerror(-1);
2469 ASSERT_STREQ("Unknown error -1", m);
2470 errno = -2;
2471 snprintf(buf, sizeof(buf), "<%m>");
2472 ASSERT_STREQ("<Unknown error -2>", buf);
2473 ASSERT_STREQ("Unknown error -1", m);
2474}
2475
Elliott Hughes654cd832018-08-30 16:00:42 -07002476TEST(STDIO_TEST, wprintf_m) {
2477 wchar_t buf[BUFSIZ];
2478 errno = 0;
2479 swprintf(buf, sizeof(buf), L"<%m>");
2480 ASSERT_EQ(std::wstring(L"<Success>"), buf);
2481 errno = -1;
2482 swprintf(buf, sizeof(buf), L"<%m>");
2483 ASSERT_EQ(std::wstring(L"<Unknown error -1>"), buf);
2484 errno = EINVAL;
2485 swprintf(buf, sizeof(buf), L"<%m>");
2486 ASSERT_EQ(std::wstring(L"<Invalid argument>"), buf);
2487}
2488
Elliott Hughesf340a562018-09-06 10:42:40 -07002489TEST(STDIO_TEST, wprintf_m_does_not_clobber_strerror) {
2490 wchar_t buf[BUFSIZ];
2491 const char* m = strerror(-1);
2492 ASSERT_STREQ("Unknown error -1", m);
2493 errno = -2;
2494 swprintf(buf, sizeof(buf), L"<%m>");
2495 ASSERT_EQ(std::wstring(L"<Unknown error -2>"), buf);
2496 ASSERT_STREQ("Unknown error -1", m);
2497}
2498
Elliott Hughes33a8cb12017-07-25 18:06:46 -07002499TEST(STDIO_TEST, fopen_append_mode_and_ftell) {
2500 TemporaryFile tf;
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -08002501 SetFileTo(tf.path, "0123456789");
2502 FILE* fp = fopen(tf.path, "a");
Elliott Hughes33a8cb12017-07-25 18:06:46 -07002503 EXPECT_EQ(10, ftell(fp));
2504 ASSERT_EQ(0, fseek(fp, 2, SEEK_SET));
2505 EXPECT_EQ(2, ftell(fp));
2506 ASSERT_NE(EOF, fputs("xxx", fp));
2507 ASSERT_EQ(0, fflush(fp));
2508 EXPECT_EQ(13, ftell(fp));
2509 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
2510 EXPECT_EQ(13, ftell(fp));
2511 ASSERT_EQ(0, fclose(fp));
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -08002512 AssertFileIs(tf.path, "0123456789xxx");
Elliott Hughes33a8cb12017-07-25 18:06:46 -07002513}
2514
2515TEST(STDIO_TEST, fdopen_append_mode_and_ftell) {
2516 TemporaryFile tf;
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -08002517 SetFileTo(tf.path, "0123456789");
2518 int fd = open(tf.path, O_RDWR);
Elliott Hughes33a8cb12017-07-25 18:06:46 -07002519 ASSERT_NE(-1, fd);
2520 // POSIX: "The file position indicator associated with the new stream is set to the position
2521 // indicated by the file offset associated with the file descriptor."
2522 ASSERT_EQ(4, lseek(fd, 4, SEEK_SET));
2523 FILE* fp = fdopen(fd, "a");
2524 EXPECT_EQ(4, ftell(fp));
2525 ASSERT_EQ(0, fseek(fp, 2, SEEK_SET));
2526 EXPECT_EQ(2, ftell(fp));
2527 ASSERT_NE(EOF, fputs("xxx", fp));
2528 ASSERT_EQ(0, fflush(fp));
2529 EXPECT_EQ(13, ftell(fp));
2530 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
2531 EXPECT_EQ(13, ftell(fp));
2532 ASSERT_EQ(0, fclose(fp));
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -08002533 AssertFileIs(tf.path, "0123456789xxx");
Elliott Hughes33a8cb12017-07-25 18:06:46 -07002534}
2535
2536TEST(STDIO_TEST, freopen_append_mode_and_ftell) {
2537 TemporaryFile tf;
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -08002538 SetFileTo(tf.path, "0123456789");
Elliott Hughes33a8cb12017-07-25 18:06:46 -07002539 FILE* other_fp = fopen("/proc/version", "r");
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -08002540 FILE* fp = freopen(tf.path, "a", other_fp);
Elliott Hughes33a8cb12017-07-25 18:06:46 -07002541 EXPECT_EQ(10, ftell(fp));
2542 ASSERT_EQ(0, fseek(fp, 2, SEEK_SET));
2543 EXPECT_EQ(2, ftell(fp));
2544 ASSERT_NE(EOF, fputs("xxx", fp));
2545 ASSERT_EQ(0, fflush(fp));
2546 EXPECT_EQ(13, ftell(fp));
2547 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
2548 EXPECT_EQ(13, ftell(fp));
2549 ASSERT_EQ(0, fclose(fp));
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -08002550 AssertFileIs(tf.path, "0123456789xxx");
Elliott Hughes33a8cb12017-07-25 18:06:46 -07002551}
Elliott Hughesb15feb72017-07-31 17:20:18 -07002552
2553TEST(STDIO_TEST, constants) {
2554 ASSERT_LE(FILENAME_MAX, PATH_MAX);
2555 ASSERT_EQ(L_tmpnam, PATH_MAX);
2556}
Elliott Hughes37ad9592017-10-30 17:47:12 -07002557
2558TEST(STDIO_TEST, perror) {
2559 ExecTestHelper eth;
2560 eth.Run([&]() { errno = EINVAL; perror("a b c"); exit(0); }, 0, "a b c: Invalid argument\n");
2561 eth.Run([&]() { errno = EINVAL; perror(nullptr); exit(0); }, 0, "Invalid argument\n");
2562 eth.Run([&]() { errno = EINVAL; perror(""); exit(0); }, 0, "Invalid argument\n");
2563}
2564
2565TEST(STDIO_TEST, puts) {
2566 ExecTestHelper eth;
2567 eth.Run([&]() { exit(puts("a b c")); }, 0, "a b c\n");
2568}
2569
Elliott Hughes7cebf832020-08-12 14:25:41 -07002570TEST(STDIO_TEST, putchar) {
2571 ExecTestHelper eth;
2572 eth.Run([&]() { exit(putchar('A')); }, 65, "A");
2573}
2574
2575TEST(STDIO_TEST, putchar_unlocked) {
2576 ExecTestHelper eth;
2577 eth.Run([&]() { exit(putchar('B')); }, 66, "B");
2578}
2579
Elliott Hughes37ad9592017-10-30 17:47:12 -07002580TEST(STDIO_TEST, unlocked) {
2581 TemporaryFile tf;
2582
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -08002583 FILE* fp = fopen(tf.path, "w+");
Elliott Hughes37ad9592017-10-30 17:47:12 -07002584 ASSERT_TRUE(fp != nullptr);
2585
2586 clearerr_unlocked(fp);
2587 ASSERT_FALSE(feof_unlocked(fp));
2588 ASSERT_FALSE(ferror_unlocked(fp));
2589
2590 ASSERT_EQ(fileno(fp), fileno_unlocked(fp));
2591
2592 ASSERT_NE(EOF, putc_unlocked('a', fp));
2593 ASSERT_NE(EOF, putc('b', fp));
2594 ASSERT_NE(EOF, fputc_unlocked('c', fp));
2595 ASSERT_NE(EOF, fputc('d', fp));
2596
2597 rewind(fp);
2598 ASSERT_EQ('a', getc_unlocked(fp));
2599 ASSERT_EQ('b', getc(fp));
2600 ASSERT_EQ('c', fgetc_unlocked(fp));
2601 ASSERT_EQ('d', fgetc(fp));
2602
2603 rewind(fp);
2604 ASSERT_EQ(2U, fwrite_unlocked("AB", 1, 2, fp));
2605 ASSERT_EQ(2U, fwrite("CD", 1, 2, fp));
2606 ASSERT_EQ(0, fflush_unlocked(fp));
2607
2608 rewind(fp);
2609 char buf[BUFSIZ] = {};
2610 ASSERT_EQ(2U, fread_unlocked(&buf[0], 1, 2, fp));
2611 ASSERT_EQ(2U, fread(&buf[2], 1, 2, fp));
2612 ASSERT_STREQ("ABCD", buf);
2613
2614 rewind(fp);
2615 ASSERT_NE(EOF, fputs("hello ", fp));
2616 ASSERT_NE(EOF, fputs_unlocked("world", fp));
2617 ASSERT_NE(EOF, fputc('\n', fp));
2618
2619 rewind(fp);
2620 ASSERT_TRUE(fgets_unlocked(buf, sizeof(buf), fp) != nullptr);
2621 ASSERT_STREQ("hello world\n", buf);
2622
2623 ASSERT_EQ(0, fclose(fp));
2624}
Ryan Prichardbf549862017-11-07 15:30:32 -08002625
2626TEST(STDIO_TEST, fseek_64bit) {
2627 TemporaryFile tf;
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -08002628 FILE* fp = fopen64(tf.path, "w+");
Ryan Prichardbf549862017-11-07 15:30:32 -08002629 ASSERT_TRUE(fp != nullptr);
2630 ASSERT_EQ(0, fseeko64(fp, 0x2'0000'0000, SEEK_SET));
2631 ASSERT_EQ(0x2'0000'0000, ftello64(fp));
2632 ASSERT_EQ(0, fseeko64(fp, 0x1'0000'0000, SEEK_CUR));
2633 ASSERT_EQ(0x3'0000'0000, ftello64(fp));
2634 ASSERT_EQ(0, fclose(fp));
2635}
2636
2637// POSIX requires that fseek/fseeko fail with EOVERFLOW if the new file offset
2638// isn't representable in long/off_t.
2639TEST(STDIO_TEST, fseek_overflow_32bit) {
2640 TemporaryFile tf;
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -08002641 FILE* fp = fopen64(tf.path, "w+");
Ryan Prichardbf549862017-11-07 15:30:32 -08002642 ASSERT_EQ(0, ftruncate64(fileno(fp), 0x2'0000'0000));
2643
2644 // Bionic implements overflow checking for SEEK_CUR, but glibc doesn't.
2645#if defined(__BIONIC__) && !defined(__LP64__)
2646 ASSERT_EQ(0, fseek(fp, 0x7fff'ffff, SEEK_SET));
2647 ASSERT_EQ(-1, fseek(fp, 1, SEEK_CUR));
2648 ASSERT_EQ(EOVERFLOW, errno);
2649#endif
2650
2651 // Neither Bionic nor glibc implement the overflow checking for SEEK_END.
2652 // (Aside: FreeBSD's libc is an example of a libc that checks both SEEK_CUR
2653 // and SEEK_END -- many C libraries check neither.)
2654 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
2655 ASSERT_EQ(0x2'0000'0000, ftello64(fp));
2656
2657 fclose(fp);
2658}
Elliott Hughesf1a38382018-08-22 15:38:07 -07002659
2660TEST(STDIO_TEST, dev_std_files) {
2661 // POSIX only mentions /dev/stdout, but we should have all three (http://b/31824379).
2662 char path[PATH_MAX];
Christopher Ferris2c4ec7e2018-08-23 11:17:55 -07002663 ssize_t length = readlink("/dev/stdin", path, sizeof(path));
2664 ASSERT_LT(0, length);
2665 ASSERT_EQ("/proc/self/fd/0", std::string(path, length));
2666
2667 length = readlink("/dev/stdout", path, sizeof(path));
2668 ASSERT_LT(0, length);
2669 ASSERT_EQ("/proc/self/fd/1", std::string(path, length));
2670
2671 length = readlink("/dev/stderr", path, sizeof(path));
2672 ASSERT_LT(0, length);
2673 ASSERT_EQ("/proc/self/fd/2", std::string(path, length));
Elliott Hughesf1a38382018-08-22 15:38:07 -07002674}
Ryan Prichardc485cdb2019-04-30 14:47:34 -07002675
2676TEST(STDIO_TEST, fread_with_locked_file) {
2677 // Reading an unbuffered/line-buffered file from one thread shouldn't block on
2678 // files locked on other threads, even if it flushes some line-buffered files.
2679 FILE* fp1 = fopen("/dev/zero", "r");
2680 ASSERT_TRUE(fp1 != nullptr);
2681 flockfile(fp1);
2682
2683 std::thread([] {
2684 for (int mode : { _IONBF, _IOLBF }) {
2685 FILE* fp2 = fopen("/dev/zero", "r");
2686 ASSERT_TRUE(fp2 != nullptr);
2687 setvbuf(fp2, nullptr, mode, 0);
2688 ASSERT_EQ('\0', fgetc(fp2));
2689 fclose(fp2);
2690 }
2691 }).join();
2692
2693 funlockfile(fp1);
2694 fclose(fp1);
2695}
Elliott Hughes31c73092019-05-07 10:03:02 -07002696
2697TEST(STDIO_TEST, SEEK_macros) {
2698 ASSERT_EQ(0, SEEK_SET);
2699 ASSERT_EQ(1, SEEK_CUR);
2700 ASSERT_EQ(2, SEEK_END);
2701 ASSERT_EQ(3, SEEK_DATA);
2702 ASSERT_EQ(4, SEEK_HOLE);
2703 // So we'll notice if Linux grows another constant in <linux/fs.h>...
2704 ASSERT_EQ(SEEK_MAX, SEEK_HOLE);
2705}
Elliott Hughes05b675e2019-04-17 13:01:06 -07002706
2707TEST(STDIO_TEST, rename) {
2708 TemporaryDir td;
2709 std::string old_path = td.path + "/old"s;
2710 std::string new_path = td.path + "/new"s;
2711
2712 // Create the file, check it exists.
2713 ASSERT_EQ(0, close(creat(old_path.c_str(), 0666)));
2714 struct stat sb;
2715 ASSERT_EQ(0, stat(old_path.c_str(), &sb));
2716 ASSERT_EQ(-1, stat(new_path.c_str(), &sb));
2717
2718 // Rename and check it moved.
2719 ASSERT_EQ(0, rename(old_path.c_str(), new_path.c_str()));
2720 ASSERT_EQ(-1, stat(old_path.c_str(), &sb));
2721 ASSERT_EQ(0, stat(new_path.c_str(), &sb));
2722}
2723
2724TEST(STDIO_TEST, renameat) {
2725 TemporaryDir td;
2726 android::base::unique_fd dirfd{open(td.path, O_PATH)};
2727 std::string old_path = td.path + "/old"s;
2728 std::string new_path = td.path + "/new"s;
2729
2730 // Create the file, check it exists.
2731 ASSERT_EQ(0, close(creat(old_path.c_str(), 0666)));
2732 struct stat sb;
2733 ASSERT_EQ(0, stat(old_path.c_str(), &sb));
2734 ASSERT_EQ(-1, stat(new_path.c_str(), &sb));
2735
2736 // Rename and check it moved.
2737 ASSERT_EQ(0, renameat(dirfd, "old", dirfd, "new"));
2738 ASSERT_EQ(-1, stat(old_path.c_str(), &sb));
2739 ASSERT_EQ(0, stat(new_path.c_str(), &sb));
2740}
2741
2742TEST(STDIO_TEST, renameat2) {
Colin Cross7da20342021-07-28 11:18:11 -07002743#if defined(__GLIBC__) || defined(MUSL)
2744 GTEST_SKIP() << "glibc doesn't have renameat2 until 2.28 and musl doesn't have renameat2";
Elliott Hughes05b675e2019-04-17 13:01:06 -07002745#else
2746 TemporaryDir td;
2747 android::base::unique_fd dirfd{open(td.path, O_PATH)};
2748 std::string old_path = td.path + "/old"s;
2749 std::string new_path = td.path + "/new"s;
2750
2751 // Create the file, check it exists.
2752 ASSERT_EQ(0, close(creat(old_path.c_str(), 0666)));
2753 struct stat sb;
2754 ASSERT_EQ(0, stat(old_path.c_str(), &sb));
2755 ASSERT_EQ(-1, stat(new_path.c_str(), &sb));
2756
2757 // Rename and check it moved.
2758 ASSERT_EQ(0, renameat2(dirfd, "old", dirfd, "new", 0));
2759 ASSERT_EQ(-1, stat(old_path.c_str(), &sb));
2760 ASSERT_EQ(0, stat(new_path.c_str(), &sb));
2761
2762 // After this, both "old" and "new" exist.
2763 ASSERT_EQ(0, close(creat(old_path.c_str(), 0666)));
2764
2765 // Rename and check it moved.
2766 ASSERT_EQ(-1, renameat2(dirfd, "old", dirfd, "new", RENAME_NOREPLACE));
2767 ASSERT_EQ(EEXIST, errno);
2768#endif
2769}
2770
2771TEST(STDIO_TEST, renameat2_flags) {
2772#if defined(__GLIBC__)
2773 GTEST_SKIP() << "glibc doesn't have renameat2 until 2.28";
2774#else
2775 ASSERT_NE(0, RENAME_EXCHANGE);
2776 ASSERT_NE(0, RENAME_NOREPLACE);
2777 ASSERT_NE(0, RENAME_WHITEOUT);
2778#endif
2779}
Elliott Hughes7cebf832020-08-12 14:25:41 -07002780
2781TEST(STDIO_TEST, fdopen_failures) {
2782 FILE* fp;
2783 int fd = open("/proc/version", O_RDONLY);
2784 ASSERT_TRUE(fd != -1);
2785
2786 // Nonsense mode.
2787 errno = 0;
2788 fp = fdopen(fd, "nonsense");
2789 ASSERT_TRUE(fp == nullptr);
2790 ASSERT_EQ(EINVAL, errno);
2791
2792 // Mode that isn't a subset of the fd's actual mode.
2793 errno = 0;
2794 fp = fdopen(fd, "w");
2795 ASSERT_TRUE(fp == nullptr);
2796 ASSERT_EQ(EINVAL, errno);
2797
2798 // Can't set append on the underlying fd.
2799 errno = 0;
2800 fp = fdopen(fd, "a");
2801 ASSERT_TRUE(fp == nullptr);
2802 ASSERT_EQ(EINVAL, errno);
2803
2804 // Bad fd.
2805 errno = 0;
2806 fp = fdopen(-1, "re");
2807 ASSERT_TRUE(fp == nullptr);
2808 ASSERT_EQ(EBADF, errno);
2809
2810 close(fd);
2811}
2812
2813TEST(STDIO_TEST, fmemopen_invalid_mode) {
2814 errno = 0;
2815 FILE* fp = fmemopen(nullptr, 16, "nonsense");
2816 ASSERT_TRUE(fp == nullptr);
2817 ASSERT_EQ(EINVAL, errno);
2818}
2819
2820TEST(STDIO_TEST, fopen_invalid_mode) {
2821 errno = 0;
2822 FILE* fp = fopen("/proc/version", "nonsense");
2823 ASSERT_TRUE(fp == nullptr);
2824 ASSERT_EQ(EINVAL, errno);
2825}
2826
2827TEST(STDIO_TEST, freopen_invalid_mode) {
2828 FILE* fp = fopen("/proc/version", "re");
2829 ASSERT_TRUE(fp != nullptr);
2830
2831 errno = 0;
2832 fp = freopen("/proc/version", "nonsense", fp);
2833 ASSERT_TRUE(fp == nullptr);
2834 ASSERT_EQ(EINVAL, errno);
2835}
2836
2837TEST(STDIO_TEST, asprintf_smoke) {
2838 char* p = nullptr;
2839 ASSERT_EQ(11, asprintf(&p, "hello %s", "world"));
2840 ASSERT_STREQ("hello world", p);
2841 free(p);
2842}
2843
2844TEST(STDIO_TEST, fopen_ENOENT) {
2845 errno = 0;
2846 FILE* fp = fopen("/proc/does-not-exist", "re");
2847 ASSERT_TRUE(fp == nullptr);
2848 ASSERT_EQ(ENOENT, errno);
2849}
Elliott Hughes439ebbd2020-12-04 18:51:42 -08002850
2851static void tempnam_test(bool has_TMPDIR, const char* dir, const char* prefix, const char* re) {
2852 if (has_TMPDIR) {
2853 setenv("TMPDIR", "/my/tmp/dir", 1);
2854 } else {
2855 unsetenv("TMPDIR");
2856 }
2857 char* s1 = tempnam(dir, prefix);
2858 char* s2 = tempnam(dir, prefix);
2859 ASSERT_MATCH(s1, re);
2860 ASSERT_MATCH(s2, re);
2861 ASSERT_STRNE(s1, s2);
2862 free(s1);
2863 free(s2);
2864}
2865
2866TEST(STDIO_TEST, tempnam__system_directory_system_prefix_with_TMPDIR) {
2867 tempnam_test(true, nullptr, nullptr, "^/my/tmp/dir/.*");
2868}
2869
2870TEST(STDIO_TEST, tempnam__system_directory_system_prefix_without_TMPDIR) {
2871 tempnam_test(false, nullptr, nullptr, "^/data/local/tmp/.*");
2872}
2873
2874TEST(STDIO_TEST, tempnam__system_directory_user_prefix_with_TMPDIR) {
2875 tempnam_test(true, nullptr, "prefix", "^/my/tmp/dir/prefix.*");
2876}
2877
2878TEST(STDIO_TEST, tempnam__system_directory_user_prefix_without_TMPDIR) {
2879 tempnam_test(false, nullptr, "prefix", "^/data/local/tmp/prefix.*");
2880}
2881
2882TEST(STDIO_TEST, tempnam__user_directory_system_prefix_with_TMPDIR) {
2883 tempnam_test(true, "/a/b/c", nullptr, "^/my/tmp/dir/.*");
2884}
2885
2886TEST(STDIO_TEST, tempnam__user_directory_system_prefix_without_TMPDIR) {
2887 tempnam_test(false, "/a/b/c", nullptr, "^/a/b/c/.*");
2888}
2889
2890TEST(STDIO_TEST, tempnam__user_directory_user_prefix_with_TMPDIR) {
2891 tempnam_test(true, "/a/b/c", "prefix", "^/my/tmp/dir/prefix.*");
2892}
2893
2894TEST(STDIO_TEST, tempnam__user_directory_user_prefix_without_TMPDIR) {
2895 tempnam_test(false, "/a/b/c", "prefix", "^/a/b/c/prefix.*");
2896}
2897
2898static void tmpnam_test(char* s) {
2899 char s1[L_tmpnam], s2[L_tmpnam];
2900
2901 strcpy(s1, tmpnam(s));
2902 strcpy(s2, tmpnam(s));
2903 ASSERT_MATCH(s1, "/tmp/.*");
2904 ASSERT_MATCH(s2, "/tmp/.*");
2905 ASSERT_STRNE(s1, s2);
2906}
2907
2908TEST(STDIO_TEST, tmpnam) {
2909 tmpnam_test(nullptr);
2910}
2911
2912TEST(STDIO_TEST, tmpnam_buf) {
2913 char buf[L_tmpnam];
2914 tmpnam_test(buf);
2915}
Elliott Hughesf9cfecf2021-02-04 16:58:13 -08002916
2917TEST(STDIO_TEST, freopen_null_filename_mode) {
2918 TemporaryFile tf;
2919 FILE* fp = fopen(tf.path, "r");
2920 ASSERT_TRUE(fp != nullptr);
2921
2922 // "r" = O_RDONLY
2923 char buf[1];
2924 ASSERT_EQ(0, read(fileno(fp), buf, 1));
2925 ASSERT_EQ(-1, write(fileno(fp), "hello", 1));
2926 // "r+" = O_RDWR
2927 fp = freopen(nullptr, "r+", fp);
2928 ASSERT_EQ(0, read(fileno(fp), buf, 1));
2929 ASSERT_EQ(1, write(fileno(fp), "hello", 1));
2930 // "w" = O_WRONLY
2931 fp = freopen(nullptr, "w", fp);
2932 ASSERT_EQ(-1, read(fileno(fp), buf, 1));
2933 ASSERT_EQ(1, write(fileno(fp), "hello", 1));
2934
2935 fclose(fp);
2936}