blob: c21c3b8b615dd767a82137afecd4d3d520458cfc [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 Hughes05b675e2019-04-17 13:01:06 -070036#include <android-base/unique_fd.h>
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -080037
Elliott Hughesfb3873d2016-08-10 11:07:54 -070038#include "BionicDeathTest.h"
Josh Gao2f06e102017-01-10 13:00:37 -080039#include "utils.h"
Elliott Hughes91875dc2012-09-24 17:55:15 -070040
Elliott Hughes05b675e2019-04-17 13:01:06 -070041// This #include is actually a test too. We have to duplicate the
42// definitions of the RENAME_ constants because <linux/fs.h> also contains
43// pollution such as BLOCK_SIZE which conflicts with lots of user code.
44// Important to check that we have matching definitions.
45// There's no _MAX to test that we have all the constants, sadly.
46#include <linux/fs.h>
47
Christopher Ferris13f26a72016-01-13 13:47:58 -080048#if defined(NOFORTIFY)
49#define STDIO_TEST stdio_nofortify
Elliott Hughesfb3873d2016-08-10 11:07:54 -070050#define STDIO_DEATHTEST stdio_nofortify_DeathTest
Christopher Ferris13f26a72016-01-13 13:47:58 -080051#else
52#define STDIO_TEST stdio
Elliott Hughesfb3873d2016-08-10 11:07:54 -070053#define STDIO_DEATHTEST stdio_DeathTest
Christopher Ferris13f26a72016-01-13 13:47:58 -080054#endif
55
Elliott Hughes3a4c4542017-07-19 17:20:24 -070056using namespace std::string_literals;
57
Elliott Hughesfb3873d2016-08-10 11:07:54 -070058class stdio_DeathTest : public BionicDeathTest {};
59class stdio_nofortify_DeathTest : public BionicDeathTest {};
60
Elliott Hughes33a8cb12017-07-25 18:06:46 -070061static void SetFileTo(const char* path, const char* content) {
62 FILE* fp;
63 ASSERT_NE(nullptr, fp = fopen(path, "w"));
64 ASSERT_NE(EOF, fputs(content, fp));
65 ASSERT_EQ(0, fclose(fp));
66}
67
68static void AssertFileIs(const char* path, const char* expected) {
69 FILE* fp;
70 ASSERT_NE(nullptr, fp = fopen(path, "r"));
71 char* line = nullptr;
72 size_t length;
73 ASSERT_NE(EOF, getline(&line, &length, fp));
74 ASSERT_EQ(0, fclose(fp));
75 ASSERT_STREQ(expected, line);
76 free(line);
77}
78
Elliott Hughes70715da2016-08-01 16:35:17 -070079static void AssertFileIs(FILE* fp, const char* expected, bool is_fmemopen = false) {
80 rewind(fp);
81
82 char line[1024];
Josh Gao2f06e102017-01-10 13:00:37 -080083 memset(line, 0xff, sizeof(line));
Elliott Hughes70715da2016-08-01 16:35:17 -070084 ASSERT_EQ(line, fgets(line, sizeof(line), fp));
85 ASSERT_STREQ(expected, line);
86
87 if (is_fmemopen) {
88 // fmemopen appends a trailing NUL byte, which probably shouldn't show up as an
89 // extra empty line, but does on every C library I tested...
90 ASSERT_EQ(line, fgets(line, sizeof(line), fp));
91 ASSERT_STREQ("", line);
92 }
93
94 // Make sure there isn't anything else in the file.
95 ASSERT_EQ(nullptr, fgets(line, sizeof(line), fp)) << "junk at end of file: " << line;
96}
97
Christopher Ferris13f26a72016-01-13 13:47:58 -080098TEST(STDIO_TEST, flockfile_18208568_stderr) {
Elliott Hughes6a03abc2014-11-03 12:32:17 -080099 // Check that we have a _recursive_ mutex for flockfile.
100 flockfile(stderr);
101 feof(stderr); // We don't care about the result, but this needs to take the lock.
102 funlockfile(stderr);
103}
104
Christopher Ferris13f26a72016-01-13 13:47:58 -0800105TEST(STDIO_TEST, flockfile_18208568_regular) {
Elliott Hughes6a03abc2014-11-03 12:32:17 -0800106 // We never had a bug for streams other than stdin/stdout/stderr, but test anyway.
107 FILE* fp = fopen("/dev/null", "w");
Yi Kong32bc0fc2018-08-02 17:31:13 -0700108 ASSERT_TRUE(fp != nullptr);
Elliott Hughes6a03abc2014-11-03 12:32:17 -0800109 flockfile(fp);
110 feof(fp);
111 funlockfile(fp);
112 fclose(fp);
113}
114
Christopher Ferris13f26a72016-01-13 13:47:58 -0800115TEST(STDIO_TEST, tmpfile_fileno_fprintf_rewind_fgets) {
Elliott Hughes91875dc2012-09-24 17:55:15 -0700116 FILE* fp = tmpfile();
Yi Kong32bc0fc2018-08-02 17:31:13 -0700117 ASSERT_TRUE(fp != nullptr);
Elliott Hughes91875dc2012-09-24 17:55:15 -0700118
119 int fd = fileno(fp);
120 ASSERT_NE(fd, -1);
121
122 struct stat sb;
123 int rc = fstat(fd, &sb);
124 ASSERT_NE(rc, -1);
125 ASSERT_EQ(sb.st_mode & 0777, 0600U);
126
127 rc = fprintf(fp, "hello\n");
128 ASSERT_EQ(rc, 6);
129
Elliott Hughes70715da2016-08-01 16:35:17 -0700130 AssertFileIs(fp, "hello\n");
Elliott Hughes91875dc2012-09-24 17:55:15 -0700131 fclose(fp);
132}
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300133
Elliott Hughesf226ee52016-02-03 11:24:28 -0800134TEST(STDIO_TEST, tmpfile64) {
135 FILE* fp = tmpfile64();
136 ASSERT_TRUE(fp != nullptr);
137 fclose(fp);
138}
139
Christopher Ferris13f26a72016-01-13 13:47:58 -0800140TEST(STDIO_TEST, dprintf) {
Calin Juravle6afb2a92014-05-22 11:47:47 +0100141 TemporaryFile tf;
142
143 int rc = dprintf(tf.fd, "hello\n");
144 ASSERT_EQ(rc, 6);
145
Yabin Cui5ca4a9e2014-11-06 19:55:09 -0800146 lseek(tf.fd, 0, SEEK_SET);
Christopher Ferris9e01ea62014-05-29 12:49:35 -0700147 FILE* tfile = fdopen(tf.fd, "r");
Yi Kong32bc0fc2018-08-02 17:31:13 -0700148 ASSERT_TRUE(tfile != nullptr);
Calin Juravle6afb2a92014-05-22 11:47:47 +0100149
Elliott Hughes70715da2016-08-01 16:35:17 -0700150 AssertFileIs(tfile, "hello\n");
Christopher Ferris9e01ea62014-05-29 12:49:35 -0700151 fclose(tfile);
Calin Juravle6afb2a92014-05-22 11:47:47 +0100152}
153
Christopher Ferris13f26a72016-01-13 13:47:58 -0800154TEST(STDIO_TEST, getdelim) {
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300155 FILE* fp = tmpfile();
Yi Kong32bc0fc2018-08-02 17:31:13 -0700156 ASSERT_TRUE(fp != nullptr);
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300157
158 const char* line_written = "This is a test";
159 int rc = fprintf(fp, "%s", line_written);
160 ASSERT_EQ(rc, static_cast<int>(strlen(line_written)));
161
162 rewind(fp);
163
Yi Kong32bc0fc2018-08-02 17:31:13 -0700164 char* word_read = nullptr;
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300165 size_t allocated_length = 0;
166
167 const char* expected[] = { "This ", " ", "is ", "a ", "test" };
168 for (size_t i = 0; i < 5; ++i) {
169 ASSERT_FALSE(feof(fp));
170 ASSERT_EQ(getdelim(&word_read, &allocated_length, ' ', fp), static_cast<int>(strlen(expected[i])));
171 ASSERT_GE(allocated_length, strlen(expected[i]));
Elliott Hughes0ed7e082015-01-22 15:13:38 -0800172 ASSERT_STREQ(expected[i], word_read);
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300173 }
174 // The last read should have set the end-of-file indicator for the stream.
175 ASSERT_TRUE(feof(fp));
176 clearerr(fp);
177
178 // getdelim returns -1 but doesn't set errno if we're already at EOF.
179 // It should set the end-of-file indicator for the stream, though.
180 errno = 0;
181 ASSERT_EQ(getdelim(&word_read, &allocated_length, ' ', fp), -1);
Elliott Hughes5e3fc432013-02-11 16:36:48 -0800182 ASSERT_EQ(0, errno);
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300183 ASSERT_TRUE(feof(fp));
184
185 free(word_read);
186 fclose(fp);
187}
188
Christopher Ferris13f26a72016-01-13 13:47:58 -0800189TEST(STDIO_TEST, getdelim_invalid) {
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300190 FILE* fp = tmpfile();
Yi Kong32bc0fc2018-08-02 17:31:13 -0700191 ASSERT_TRUE(fp != nullptr);
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300192
Yi Kong32bc0fc2018-08-02 17:31:13 -0700193 char* buffer = nullptr;
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300194 size_t buffer_length = 0;
195
196 // The first argument can't be NULL.
197 errno = 0;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700198 ASSERT_EQ(getdelim(nullptr, &buffer_length, ' ', fp), -1);
Elliott Hughes5e3fc432013-02-11 16:36:48 -0800199 ASSERT_EQ(EINVAL, errno);
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300200
201 // The second argument can't be NULL.
202 errno = 0;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700203 ASSERT_EQ(getdelim(&buffer, nullptr, ' ', fp), -1);
Elliott Hughes5e3fc432013-02-11 16:36:48 -0800204 ASSERT_EQ(EINVAL, errno);
Elliott Hughes20f8aec2014-05-12 15:15:37 -0700205 fclose(fp);
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300206}
207
Christopher Ferris13f26a72016-01-13 13:47:58 -0800208TEST(STDIO_TEST, getdelim_directory) {
Elliott Hughes694fd2d2015-04-05 10:51:56 -0700209 FILE* fp = fopen("/proc", "r");
Yi Kong32bc0fc2018-08-02 17:31:13 -0700210 ASSERT_TRUE(fp != nullptr);
Elliott Hughes694fd2d2015-04-05 10:51:56 -0700211 char* word_read;
212 size_t allocated_length;
213 ASSERT_EQ(-1, getdelim(&word_read, &allocated_length, ' ', fp));
214 fclose(fp);
215}
216
Christopher Ferris13f26a72016-01-13 13:47:58 -0800217TEST(STDIO_TEST, getline) {
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300218 FILE* fp = tmpfile();
Yi Kong32bc0fc2018-08-02 17:31:13 -0700219 ASSERT_TRUE(fp != nullptr);
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300220
221 const char* line_written = "This is a test for getline\n";
222 const size_t line_count = 5;
223
224 for (size_t i = 0; i < line_count; ++i) {
225 int rc = fprintf(fp, "%s", line_written);
226 ASSERT_EQ(rc, static_cast<int>(strlen(line_written)));
227 }
228
229 rewind(fp);
230
Yi Kong32bc0fc2018-08-02 17:31:13 -0700231 char* line_read = nullptr;
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300232 size_t allocated_length = 0;
233
234 size_t read_line_count = 0;
235 ssize_t read_char_count;
236 while ((read_char_count = getline(&line_read, &allocated_length, fp)) != -1) {
237 ASSERT_EQ(read_char_count, static_cast<int>(strlen(line_written)));
238 ASSERT_GE(allocated_length, strlen(line_written));
Elliott Hughes0ed7e082015-01-22 15:13:38 -0800239 ASSERT_STREQ(line_written, line_read);
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300240 ++read_line_count;
241 }
242 ASSERT_EQ(read_line_count, line_count);
243
244 // The last read should have set the end-of-file indicator for the stream.
245 ASSERT_TRUE(feof(fp));
246 clearerr(fp);
247
248 // getline returns -1 but doesn't set errno if we're already at EOF.
249 // It should set the end-of-file indicator for the stream, though.
250 errno = 0;
251 ASSERT_EQ(getline(&line_read, &allocated_length, fp), -1);
Elliott Hughes5e3fc432013-02-11 16:36:48 -0800252 ASSERT_EQ(0, errno);
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300253 ASSERT_TRUE(feof(fp));
254
255 free(line_read);
256 fclose(fp);
257}
258
Christopher Ferris13f26a72016-01-13 13:47:58 -0800259TEST(STDIO_TEST, getline_invalid) {
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300260 FILE* fp = tmpfile();
Yi Kong32bc0fc2018-08-02 17:31:13 -0700261 ASSERT_TRUE(fp != nullptr);
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300262
Yi Kong32bc0fc2018-08-02 17:31:13 -0700263 char* buffer = nullptr;
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300264 size_t buffer_length = 0;
265
266 // The first argument can't be NULL.
267 errno = 0;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700268 ASSERT_EQ(getline(nullptr, &buffer_length, fp), -1);
Elliott Hughes5e3fc432013-02-11 16:36:48 -0800269 ASSERT_EQ(EINVAL, errno);
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300270
271 // The second argument can't be NULL.
272 errno = 0;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700273 ASSERT_EQ(getline(&buffer, nullptr, fp), -1);
Elliott Hughes5e3fc432013-02-11 16:36:48 -0800274 ASSERT_EQ(EINVAL, errno);
Elliott Hughes20f8aec2014-05-12 15:15:37 -0700275 fclose(fp);
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300276}
Thorsten Glaserc641caf2013-02-17 16:50:58 +0000277
Christopher Ferris13f26a72016-01-13 13:47:58 -0800278TEST(STDIO_TEST, printf_ssize_t) {
Elliott Hughese2556422013-02-28 10:51:14 -0800279 // http://b/8253769
Elliott Hughese2556422013-02-28 10:51:14 -0800280 ASSERT_EQ(sizeof(ssize_t), sizeof(long int));
Elliott Hughesb6e22482013-03-08 15:28:52 -0800281 ASSERT_EQ(sizeof(ssize_t), sizeof(size_t));
282 // For our 32-bit ABI, we had a ssize_t definition that confuses GCC into saying:
Thorsten Glaserc641caf2013-02-17 16:50:58 +0000283 // error: format '%zd' expects argument of type 'signed size_t',
284 // but argument 4 has type 'ssize_t {aka long int}' [-Werror=format]
285 ssize_t v = 1;
286 char buf[32];
287 snprintf(buf, sizeof(buf), "%zd", v);
288}
Elliott Hughes6b3f49a2013-03-06 16:20:55 -0800289
Elliott Hughes05493712014-04-17 17:30:03 -0700290// https://code.google.com/p/android/issues/detail?id=64886
Christopher Ferris13f26a72016-01-13 13:47:58 -0800291TEST(STDIO_TEST, snprintf_a) {
Elliott Hughes05493712014-04-17 17:30:03 -0700292 char buf[BUFSIZ];
293 EXPECT_EQ(23, snprintf(buf, sizeof(buf), "<%a>", 9990.235));
294 EXPECT_STREQ("<0x1.3831e147ae148p+13>", buf);
295}
296
Elliott Hughescdb4a262020-06-05 16:56:53 -0700297// http://b/152588929
298TEST(STDIO_TEST, snprintf_La) {
299#if defined(__LP64__)
300 char buf[BUFSIZ];
301 union {
302 uint64_t a[2];
303 long double v;
304 } u;
305
306 u.a[0] = UINT64_C(0x9b9b9b9b9b9b9b9b);
307 u.a[1] = UINT64_C(0xdfdfdfdfdfdfdfdf);
308 EXPECT_EQ(41, snprintf(buf, sizeof(buf), "<%La>", u.v));
309 EXPECT_STREQ("<-0x1.dfdfdfdfdfdf9b9b9b9b9b9b9b9bp+8160>", buf);
310
311 u.a[0] = UINT64_C(0xffffffffffffffff);
312 u.a[1] = UINT64_C(0x7ffeffffffffffff);
313 EXPECT_EQ(41, snprintf(buf, sizeof(buf), "<%La>", u.v));
314 EXPECT_STREQ("<0x1.ffffffffffffffffffffffffffffp+16383>", buf);
315
316 u.a[0] = UINT64_C(0x0000000000000000);
317 u.a[1] = UINT64_C(0x0000000000000000);
318 EXPECT_EQ(8, snprintf(buf, sizeof(buf), "<%La>", u.v));
319 EXPECT_STREQ("<0x0p+0>", buf);
320#else
321 GTEST_SKIP() << "no ld128";
322#endif
323}
324
Christopher Ferris13f26a72016-01-13 13:47:58 -0800325TEST(STDIO_TEST, snprintf_lc) {
Elliott Hughes05493712014-04-17 17:30:03 -0700326 char buf[BUFSIZ];
327 wint_t wc = L'a';
328 EXPECT_EQ(3, snprintf(buf, sizeof(buf), "<%lc>", wc));
329 EXPECT_STREQ("<a>", buf);
330}
331
Elliott Hughes2f9c8ce2017-11-01 13:54:47 -0700332TEST(STDIO_TEST, snprintf_C) { // Synonym for %lc.
333 char buf[BUFSIZ];
334 wchar_t wc = L'a';
335 EXPECT_EQ(3, snprintf(buf, sizeof(buf), "<%C>", wc));
336 EXPECT_STREQ("<a>", buf);
337}
338
Christopher Ferris13f26a72016-01-13 13:47:58 -0800339TEST(STDIO_TEST, snprintf_ls) {
Elliott Hughes05493712014-04-17 17:30:03 -0700340 char buf[BUFSIZ];
Yi Kong32bc0fc2018-08-02 17:31:13 -0700341 wchar_t* ws = nullptr;
Elliott Hughes05493712014-04-17 17:30:03 -0700342 EXPECT_EQ(8, snprintf(buf, sizeof(buf), "<%ls>", ws));
343 EXPECT_STREQ("<(null)>", buf);
344
345 wchar_t chars[] = { L'h', L'i', 0 };
346 ws = chars;
347 EXPECT_EQ(4, snprintf(buf, sizeof(buf), "<%ls>", ws));
348 EXPECT_STREQ("<hi>", buf);
349}
350
Elliott Hughes2f9c8ce2017-11-01 13:54:47 -0700351TEST(STDIO_TEST, snprintf_S) { // Synonym for %ls.
352 char buf[BUFSIZ];
Yi Kong32bc0fc2018-08-02 17:31:13 -0700353 wchar_t* ws = nullptr;
Elliott Hughes2f9c8ce2017-11-01 13:54:47 -0700354 EXPECT_EQ(8, snprintf(buf, sizeof(buf), "<%S>", ws));
355 EXPECT_STREQ("<(null)>", buf);
356
357 wchar_t chars[] = { L'h', L'i', 0 };
358 ws = chars;
359 EXPECT_EQ(4, snprintf(buf, sizeof(buf), "<%S>", ws));
360 EXPECT_STREQ("<hi>", buf);
361}
362
Christopher Ferris13f26a72016-01-13 13:47:58 -0800363TEST(STDIO_TEST, snprintf_n) {
Elliott Hughes063525c2014-05-13 11:19:57 -0700364#if defined(__BIONIC__)
Elliott Hughes41398d02018-03-07 13:32:58 -0800365 // http://b/14492135 and http://b/31832608.
Elliott Hughes7248a2d2013-09-24 18:01:33 -0700366 char buf[32];
Elliott Hughese2341d02014-05-02 18:16:32 -0700367 int i = 1234;
Elliott Hughes41398d02018-03-07 13:32:58 -0800368 EXPECT_DEATH(snprintf(buf, sizeof(buf), "a %n b", &i), "%n not allowed on Android");
Elliott Hughese2341d02014-05-02 18:16:32 -0700369#else
Elliott Hughesbcaa4542019-03-08 15:20:23 -0800370 GTEST_SKIP() << "glibc does allow %n";
Elliott Hughese2341d02014-05-02 18:16:32 -0700371#endif
Elliott Hughes7248a2d2013-09-24 18:01:33 -0700372}
Elliott Hughes7248a2d2013-09-24 18:01:33 -0700373
Elliott Hughes7cebf832020-08-12 14:25:41 -0700374TEST(STDIO_TEST, snprintf_measure) {
375 char buf[16];
376 ASSERT_EQ(11, snprintf(buf, 0, "Hello %s", "world"));
377}
378
Christopher Ferris13f26a72016-01-13 13:47:58 -0800379TEST(STDIO_TEST, snprintf_smoke) {
Elliott Hughes1d13c642013-09-23 16:02:39 -0700380 char buf[BUFSIZ];
381
382 snprintf(buf, sizeof(buf), "a");
383 EXPECT_STREQ("a", buf);
384
385 snprintf(buf, sizeof(buf), "%%");
386 EXPECT_STREQ("%", buf);
387
388 snprintf(buf, sizeof(buf), "01234");
389 EXPECT_STREQ("01234", buf);
390
391 snprintf(buf, sizeof(buf), "a%sb", "01234");
392 EXPECT_STREQ("a01234b", buf);
393
Yi Kong32bc0fc2018-08-02 17:31:13 -0700394 char* s = nullptr;
Elliott Hughes1d13c642013-09-23 16:02:39 -0700395 snprintf(buf, sizeof(buf), "a%sb", s);
396 EXPECT_STREQ("a(null)b", buf);
397
398 snprintf(buf, sizeof(buf), "aa%scc", "bb");
399 EXPECT_STREQ("aabbcc", buf);
400
401 snprintf(buf, sizeof(buf), "a%cc", 'b');
402 EXPECT_STREQ("abc", buf);
403
404 snprintf(buf, sizeof(buf), "a%db", 1234);
405 EXPECT_STREQ("a1234b", buf);
406
407 snprintf(buf, sizeof(buf), "a%db", -8123);
408 EXPECT_STREQ("a-8123b", buf);
409
Stephen Hines6c7b3cb2013-10-11 16:03:21 -0700410 snprintf(buf, sizeof(buf), "a%hdb", static_cast<short>(0x7fff0010));
Elliott Hughes1d13c642013-09-23 16:02:39 -0700411 EXPECT_STREQ("a16b", buf);
412
Stephen Hines6c7b3cb2013-10-11 16:03:21 -0700413 snprintf(buf, sizeof(buf), "a%hhdb", static_cast<char>(0x7fffff10));
Elliott Hughes1d13c642013-09-23 16:02:39 -0700414 EXPECT_STREQ("a16b", buf);
415
416 snprintf(buf, sizeof(buf), "a%lldb", 0x1000000000LL);
417 EXPECT_STREQ("a68719476736b", buf);
418
419 snprintf(buf, sizeof(buf), "a%ldb", 70000L);
420 EXPECT_STREQ("a70000b", buf);
421
422 snprintf(buf, sizeof(buf), "a%pb", reinterpret_cast<void*>(0xb0001234));
423 EXPECT_STREQ("a0xb0001234b", buf);
424
425 snprintf(buf, sizeof(buf), "a%xz", 0x12ab);
426 EXPECT_STREQ("a12abz", buf);
427
428 snprintf(buf, sizeof(buf), "a%Xz", 0x12ab);
429 EXPECT_STREQ("a12ABz", buf);
430
431 snprintf(buf, sizeof(buf), "a%08xz", 0x123456);
432 EXPECT_STREQ("a00123456z", buf);
433
434 snprintf(buf, sizeof(buf), "a%5dz", 1234);
435 EXPECT_STREQ("a 1234z", buf);
436
437 snprintf(buf, sizeof(buf), "a%05dz", 1234);
438 EXPECT_STREQ("a01234z", buf);
439
440 snprintf(buf, sizeof(buf), "a%8dz", 1234);
441 EXPECT_STREQ("a 1234z", buf);
442
443 snprintf(buf, sizeof(buf), "a%-8dz", 1234);
444 EXPECT_STREQ("a1234 z", buf);
445
446 snprintf(buf, sizeof(buf), "A%-11sZ", "abcdef");
447 EXPECT_STREQ("Aabcdef Z", buf);
448
449 snprintf(buf, sizeof(buf), "A%s:%dZ", "hello", 1234);
450 EXPECT_STREQ("Ahello:1234Z", buf);
451
452 snprintf(buf, sizeof(buf), "a%03d:%d:%02dz", 5, 5, 5);
453 EXPECT_STREQ("a005:5:05z", buf);
454
Yi Kong32bc0fc2018-08-02 17:31:13 -0700455 void* p = nullptr;
Elliott Hughes1d13c642013-09-23 16:02:39 -0700456 snprintf(buf, sizeof(buf), "a%d,%pz", 5, p);
Christopher Ferris13613132013-10-28 15:24:04 -0700457#if defined(__BIONIC__)
Elliott Hughes1d13c642013-09-23 16:02:39 -0700458 EXPECT_STREQ("a5,0x0z", buf);
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800459#else // __BIONIC__
Christopher Ferris13613132013-10-28 15:24:04 -0700460 EXPECT_STREQ("a5,(nil)z", buf);
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800461#endif // __BIONIC__
Elliott Hughes1d13c642013-09-23 16:02:39 -0700462
463 snprintf(buf, sizeof(buf), "a%lld,%d,%d,%dz", 0x1000000000LL, 6, 7, 8);
464 EXPECT_STREQ("a68719476736,6,7,8z", buf);
465
466 snprintf(buf, sizeof(buf), "a_%f_b", 1.23f);
467 EXPECT_STREQ("a_1.230000_b", buf);
468
Stephen Hines6c7b3cb2013-10-11 16:03:21 -0700469 snprintf(buf, sizeof(buf), "a_%g_b", 3.14);
Elliott Hughes1d13c642013-09-23 16:02:39 -0700470 EXPECT_STREQ("a_3.14_b", buf);
Alexander Ivchenkoedd7c2e2014-04-01 17:01:39 +0400471
472 snprintf(buf, sizeof(buf), "%1$s %1$s", "print_me_twice");
473 EXPECT_STREQ("print_me_twice print_me_twice", buf);
Elliott Hughes1d13c642013-09-23 16:02:39 -0700474}
475
Elliott Hughes1b18aff2014-12-16 14:45:32 -0800476template <typename T>
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700477static void CheckInfNan(int snprintf_fn(T*, size_t, const T*, ...),
478 int sscanf_fn(const T*, const T*, ...),
479 const T* fmt_string, const T* fmt, const T* fmt_plus,
480 const T* minus_inf, const T* inf_, const T* plus_inf,
481 const T* minus_nan, const T* nan_, const T* plus_nan) {
Elliott Hughes1b18aff2014-12-16 14:45:32 -0800482 T buf[BUFSIZ];
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700483 float f;
Elliott Hughes7823f322014-04-14 12:11:28 -0700484
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700485 // NaN.
486
487 snprintf_fn(buf, sizeof(buf), fmt, nanf(""));
Elliott Hughes1b18aff2014-12-16 14:45:32 -0800488 EXPECT_STREQ(nan_, buf) << fmt;
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700489 EXPECT_EQ(1, sscanf_fn(buf, fmt, &f));
490 EXPECT_TRUE(isnan(f));
491
492 snprintf_fn(buf, sizeof(buf), fmt, -nanf(""));
Elliott Hughes1b18aff2014-12-16 14:45:32 -0800493 EXPECT_STREQ(minus_nan, buf) << fmt;
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700494 EXPECT_EQ(1, sscanf_fn(buf, fmt, &f));
495 EXPECT_TRUE(isnan(f));
496
497 snprintf_fn(buf, sizeof(buf), fmt_plus, nanf(""));
Elliott Hughes1b18aff2014-12-16 14:45:32 -0800498 EXPECT_STREQ(plus_nan, buf) << fmt_plus;
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700499 EXPECT_EQ(1, sscanf_fn(buf, fmt, &f));
500 EXPECT_TRUE(isnan(f));
501
502 snprintf_fn(buf, sizeof(buf), fmt_plus, -nanf(""));
Elliott Hughes1b18aff2014-12-16 14:45:32 -0800503 EXPECT_STREQ(minus_nan, buf) << fmt_plus;
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700504 EXPECT_EQ(1, sscanf_fn(buf, fmt, &f));
505 EXPECT_TRUE(isnan(f));
Elliott Hughes1b18aff2014-12-16 14:45:32 -0800506
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700507 // Inf.
508
509 snprintf_fn(buf, sizeof(buf), fmt, HUGE_VALF);
Elliott Hughes1b18aff2014-12-16 14:45:32 -0800510 EXPECT_STREQ(inf_, buf) << fmt;
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700511 EXPECT_EQ(1, sscanf_fn(buf, fmt, &f));
512 EXPECT_EQ(HUGE_VALF, f);
513
514 snprintf_fn(buf, sizeof(buf), fmt, -HUGE_VALF);
Elliott Hughes1b18aff2014-12-16 14:45:32 -0800515 EXPECT_STREQ(minus_inf, buf) << fmt;
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700516 EXPECT_EQ(1, sscanf_fn(buf, fmt, &f));
517 EXPECT_EQ(-HUGE_VALF, f);
518
519 snprintf_fn(buf, sizeof(buf), fmt_plus, HUGE_VALF);
Elliott Hughes1b18aff2014-12-16 14:45:32 -0800520 EXPECT_STREQ(plus_inf, buf) << fmt_plus;
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700521 EXPECT_EQ(1, sscanf_fn(buf, fmt, &f));
522 EXPECT_EQ(HUGE_VALF, f);
523
524 snprintf_fn(buf, sizeof(buf), fmt_plus, -HUGE_VALF);
Elliott Hughes1b18aff2014-12-16 14:45:32 -0800525 EXPECT_STREQ(minus_inf, buf) << fmt_plus;
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700526 EXPECT_EQ(1, sscanf_fn(buf, fmt, &f));
527 EXPECT_EQ(-HUGE_VALF, f);
528
529 // Check case-insensitivity.
530 snprintf_fn(buf, sizeof(buf), fmt_string, "[InFiNiTy]");
531 EXPECT_EQ(1, sscanf_fn(buf, fmt, &f)) << buf;
532 EXPECT_EQ(HUGE_VALF, f);
533 snprintf_fn(buf, sizeof(buf), fmt_string, "[NaN]");
534 EXPECT_EQ(1, sscanf_fn(buf, fmt, &f)) << buf;
535 EXPECT_TRUE(isnan(f));
Elliott Hughes7823f322014-04-14 12:11:28 -0700536}
537
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700538TEST(STDIO_TEST, snprintf_sscanf_inf_nan) {
539 CheckInfNan(snprintf, sscanf, "%s",
540 "[%a]", "[%+a]",
541 "[-inf]", "[inf]", "[+inf]",
542 "[-nan]", "[nan]", "[+nan]");
543 CheckInfNan(snprintf, sscanf, "%s",
544 "[%A]", "[%+A]",
545 "[-INF]", "[INF]", "[+INF]",
546 "[-NAN]", "[NAN]", "[+NAN]");
547 CheckInfNan(snprintf, sscanf, "%s",
548 "[%e]", "[%+e]",
549 "[-inf]", "[inf]", "[+inf]",
550 "[-nan]", "[nan]", "[+nan]");
551 CheckInfNan(snprintf, sscanf, "%s",
552 "[%E]", "[%+E]",
553 "[-INF]", "[INF]", "[+INF]",
554 "[-NAN]", "[NAN]", "[+NAN]");
555 CheckInfNan(snprintf, sscanf, "%s",
556 "[%f]", "[%+f]",
557 "[-inf]", "[inf]", "[+inf]",
558 "[-nan]", "[nan]", "[+nan]");
559 CheckInfNan(snprintf, sscanf, "%s",
560 "[%F]", "[%+F]",
561 "[-INF]", "[INF]", "[+INF]",
562 "[-NAN]", "[NAN]", "[+NAN]");
563 CheckInfNan(snprintf, sscanf, "%s",
564 "[%g]", "[%+g]",
565 "[-inf]", "[inf]", "[+inf]",
566 "[-nan]", "[nan]", "[+nan]");
567 CheckInfNan(snprintf, sscanf, "%s",
568 "[%G]", "[%+G]",
569 "[-INF]", "[INF]", "[+INF]",
570 "[-NAN]", "[NAN]", "[+NAN]");
Elliott Hughes1b18aff2014-12-16 14:45:32 -0800571}
Elliott Hughes7823f322014-04-14 12:11:28 -0700572
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700573TEST(STDIO_TEST, swprintf_swscanf_inf_nan) {
574 CheckInfNan(swprintf, swscanf, L"%s",
575 L"[%a]", L"[%+a]",
576 L"[-inf]", L"[inf]", L"[+inf]",
577 L"[-nan]", L"[nan]", L"[+nan]");
578 CheckInfNan(swprintf, swscanf, L"%s",
579 L"[%A]", L"[%+A]",
580 L"[-INF]", L"[INF]", L"[+INF]",
581 L"[-NAN]", L"[NAN]", L"[+NAN]");
582 CheckInfNan(swprintf, swscanf, L"%s",
583 L"[%e]", L"[%+e]",
584 L"[-inf]", L"[inf]", L"[+inf]",
585 L"[-nan]", L"[nan]", L"[+nan]");
586 CheckInfNan(swprintf, swscanf, L"%s",
587 L"[%E]", L"[%+E]",
588 L"[-INF]", L"[INF]", L"[+INF]",
589 L"[-NAN]", L"[NAN]", L"[+NAN]");
590 CheckInfNan(swprintf, swscanf, L"%s",
591 L"[%f]", L"[%+f]",
592 L"[-inf]", L"[inf]", L"[+inf]",
593 L"[-nan]", L"[nan]", L"[+nan]");
594 CheckInfNan(swprintf, swscanf, L"%s",
595 L"[%F]", L"[%+F]",
596 L"[-INF]", L"[INF]", L"[+INF]",
597 L"[-NAN]", L"[NAN]", L"[+NAN]");
598 CheckInfNan(swprintf, swscanf, L"%s",
599 L"[%g]", L"[%+g]",
600 L"[-inf]", L"[inf]", L"[+inf]",
601 L"[-nan]", L"[nan]", L"[+nan]");
602 CheckInfNan(swprintf, swscanf, L"%s",
603 L"[%G]", L"[%+G]",
604 L"[-INF]", L"[INF]", L"[+INF]",
605 L"[-NAN]", L"[NAN]", L"[+NAN]");
Elliott Hughes7823f322014-04-14 12:11:28 -0700606}
607
Dan Albert9601f162017-08-09 14:59:06 -0700608TEST(STDIO_TEST, swprintf) {
609 constexpr size_t nchars = 32;
610 wchar_t buf[nchars];
611
612 ASSERT_EQ(2, swprintf(buf, nchars, L"ab")) << strerror(errno);
613 ASSERT_EQ(std::wstring(L"ab"), buf);
614 ASSERT_EQ(5, swprintf(buf, nchars, L"%s", "abcde"));
615 ASSERT_EQ(std::wstring(L"abcde"), buf);
616
617 // Unlike swprintf(), swprintf() returns -1 in case of truncation
618 // and doesn't necessarily zero-terminate the output!
619 ASSERT_EQ(-1, swprintf(buf, 4, L"%s", "abcde"));
620
621 const char kString[] = "Hello, World";
622 ASSERT_EQ(12, swprintf(buf, nchars, L"%s", kString));
623 ASSERT_EQ(std::wstring(L"Hello, World"), buf);
624 ASSERT_EQ(12, swprintf(buf, 13, L"%s", kString));
625 ASSERT_EQ(std::wstring(L"Hello, World"), buf);
626}
627
628TEST(STDIO_TEST, swprintf_a) {
629 constexpr size_t nchars = 32;
630 wchar_t buf[nchars];
631
632 ASSERT_EQ(20, swprintf(buf, nchars, L"%a", 3.1415926535));
633 ASSERT_EQ(std::wstring(L"0x1.921fb54411744p+1"), buf);
634}
635
Elliott Hughes2f9c8ce2017-11-01 13:54:47 -0700636TEST(STDIO_TEST, swprintf_lc) {
637 constexpr size_t nchars = 32;
638 wchar_t buf[nchars];
639
640 wint_t wc = L'a';
641 EXPECT_EQ(3, swprintf(buf, nchars, L"<%lc>", wc));
642 EXPECT_EQ(std::wstring(L"<a>"), buf);
643}
644
645TEST(STDIO_TEST, swprintf_C) { // Synonym for %lc.
646 constexpr size_t nchars = 32;
647 wchar_t buf[nchars];
648
649 wint_t wc = L'a';
650 EXPECT_EQ(3, swprintf(buf, nchars, L"<%C>", wc));
651 EXPECT_EQ(std::wstring(L"<a>"), buf);
652}
653
Elliott Hughes618303c2017-11-02 16:58:44 -0700654TEST(STDIO_TEST, swprintf_jd_INTMAX_MAX) {
655 constexpr size_t nchars = 32;
656 wchar_t buf[nchars];
657
658 swprintf(buf, nchars, L"%jd", INTMAX_MAX);
659 EXPECT_EQ(std::wstring(L"9223372036854775807"), buf);
660}
661
662TEST(STDIO_TEST, swprintf_jd_INTMAX_MIN) {
663 constexpr size_t nchars = 32;
664 wchar_t buf[nchars];
665
666 swprintf(buf, nchars, L"%jd", INTMAX_MIN);
667 EXPECT_EQ(std::wstring(L"-9223372036854775808"), buf);
668}
669
670TEST(STDIO_TEST, swprintf_ju_UINTMAX_MAX) {
671 constexpr size_t nchars = 32;
672 wchar_t buf[nchars];
673
674 swprintf(buf, nchars, L"%ju", UINTMAX_MAX);
675 EXPECT_EQ(std::wstring(L"18446744073709551615"), buf);
676}
677
678TEST(STDIO_TEST, swprintf_1$ju_UINTMAX_MAX) {
679 constexpr size_t nchars = 32;
680 wchar_t buf[nchars];
681
682 swprintf(buf, nchars, L"%1$ju", UINTMAX_MAX);
683 EXPECT_EQ(std::wstring(L"18446744073709551615"), buf);
684}
685
Dan Albert9601f162017-08-09 14:59:06 -0700686TEST(STDIO_TEST, swprintf_ls) {
687 constexpr size_t nchars = 32;
688 wchar_t buf[nchars];
689
690 static const wchar_t kWideString[] = L"Hello\uff41 World";
691 ASSERT_EQ(12, swprintf(buf, nchars, L"%ls", kWideString));
692 ASSERT_EQ(std::wstring(kWideString), buf);
693 ASSERT_EQ(12, swprintf(buf, 13, L"%ls", kWideString));
694 ASSERT_EQ(std::wstring(kWideString), buf);
695}
696
Elliott Hughes2f9c8ce2017-11-01 13:54:47 -0700697TEST(STDIO_TEST, swprintf_S) { // Synonym for %ls.
698 constexpr size_t nchars = 32;
699 wchar_t buf[nchars];
700
701 static const wchar_t kWideString[] = L"Hello\uff41 World";
702 ASSERT_EQ(12, swprintf(buf, nchars, L"%S", kWideString));
703 ASSERT_EQ(std::wstring(kWideString), buf);
704 ASSERT_EQ(12, swprintf(buf, 13, L"%S", kWideString));
705 ASSERT_EQ(std::wstring(kWideString), buf);
706}
707
Christopher Ferris13f26a72016-01-13 13:47:58 -0800708TEST(STDIO_TEST, snprintf_d_INT_MAX) {
Elliott Hughes1d13c642013-09-23 16:02:39 -0700709 char buf[BUFSIZ];
710 snprintf(buf, sizeof(buf), "%d", INT_MAX);
711 EXPECT_STREQ("2147483647", buf);
712}
713
Christopher Ferris13f26a72016-01-13 13:47:58 -0800714TEST(STDIO_TEST, snprintf_d_INT_MIN) {
Elliott Hughes1d13c642013-09-23 16:02:39 -0700715 char buf[BUFSIZ];
716 snprintf(buf, sizeof(buf), "%d", INT_MIN);
717 EXPECT_STREQ("-2147483648", buf);
718}
719
Elliott Hughes618303c2017-11-02 16:58:44 -0700720TEST(STDIO_TEST, snprintf_jd_INTMAX_MAX) {
721 char buf[BUFSIZ];
722 snprintf(buf, sizeof(buf), "%jd", INTMAX_MAX);
723 EXPECT_STREQ("9223372036854775807", buf);
724}
725
726TEST(STDIO_TEST, snprintf_jd_INTMAX_MIN) {
727 char buf[BUFSIZ];
728 snprintf(buf, sizeof(buf), "%jd", INTMAX_MIN);
729 EXPECT_STREQ("-9223372036854775808", buf);
730}
731
732TEST(STDIO_TEST, snprintf_ju_UINTMAX_MAX) {
733 char buf[BUFSIZ];
734 snprintf(buf, sizeof(buf), "%ju", UINTMAX_MAX);
735 EXPECT_STREQ("18446744073709551615", buf);
736}
737
738TEST(STDIO_TEST, snprintf_1$ju_UINTMAX_MAX) {
739 char buf[BUFSIZ];
740 snprintf(buf, sizeof(buf), "%1$ju", UINTMAX_MAX);
741 EXPECT_STREQ("18446744073709551615", buf);
742}
743
Christopher Ferris13f26a72016-01-13 13:47:58 -0800744TEST(STDIO_TEST, snprintf_ld_LONG_MAX) {
Elliott Hughes1d13c642013-09-23 16:02:39 -0700745 char buf[BUFSIZ];
746 snprintf(buf, sizeof(buf), "%ld", LONG_MAX);
Josh Gaob36efa42016-09-15 13:55:41 -0700747#if defined(__LP64__)
Elliott Hughes925753a2013-10-18 13:17:18 -0700748 EXPECT_STREQ("9223372036854775807", buf);
749#else
Elliott Hughes1d13c642013-09-23 16:02:39 -0700750 EXPECT_STREQ("2147483647", buf);
Elliott Hughes925753a2013-10-18 13:17:18 -0700751#endif
Elliott Hughes1d13c642013-09-23 16:02:39 -0700752}
753
Christopher Ferris13f26a72016-01-13 13:47:58 -0800754TEST(STDIO_TEST, snprintf_ld_LONG_MIN) {
Elliott Hughes1d13c642013-09-23 16:02:39 -0700755 char buf[BUFSIZ];
756 snprintf(buf, sizeof(buf), "%ld", LONG_MIN);
Josh Gaob36efa42016-09-15 13:55:41 -0700757#if defined(__LP64__)
Elliott Hughes925753a2013-10-18 13:17:18 -0700758 EXPECT_STREQ("-9223372036854775808", buf);
759#else
Elliott Hughes1d13c642013-09-23 16:02:39 -0700760 EXPECT_STREQ("-2147483648", buf);
Elliott Hughes925753a2013-10-18 13:17:18 -0700761#endif
Elliott Hughes1d13c642013-09-23 16:02:39 -0700762}
763
Christopher Ferris13f26a72016-01-13 13:47:58 -0800764TEST(STDIO_TEST, snprintf_lld_LLONG_MAX) {
Elliott Hughes1d13c642013-09-23 16:02:39 -0700765 char buf[BUFSIZ];
766 snprintf(buf, sizeof(buf), "%lld", LLONG_MAX);
767 EXPECT_STREQ("9223372036854775807", buf);
768}
769
Christopher Ferris13f26a72016-01-13 13:47:58 -0800770TEST(STDIO_TEST, snprintf_lld_LLONG_MIN) {
Elliott Hughes1d13c642013-09-23 16:02:39 -0700771 char buf[BUFSIZ];
772 snprintf(buf, sizeof(buf), "%lld", LLONG_MIN);
773 EXPECT_STREQ("-9223372036854775808", buf);
774}
775
Elliott Hughes2f9c8ce2017-11-01 13:54:47 -0700776TEST(STDIO_TEST, snprintf_o_UINT_MAX) {
777 char buf[BUFSIZ];
778 snprintf(buf, sizeof(buf), "%o", UINT_MAX);
779 EXPECT_STREQ("37777777777", buf);
780}
781
782TEST(STDIO_TEST, snprintf_u_UINT_MAX) {
783 char buf[BUFSIZ];
784 snprintf(buf, sizeof(buf), "%u", UINT_MAX);
785 EXPECT_STREQ("4294967295", buf);
786}
787
788TEST(STDIO_TEST, snprintf_x_UINT_MAX) {
789 char buf[BUFSIZ];
790 snprintf(buf, sizeof(buf), "%x", UINT_MAX);
791 EXPECT_STREQ("ffffffff", buf);
792}
793
794TEST(STDIO_TEST, snprintf_X_UINT_MAX) {
795 char buf[BUFSIZ];
796 snprintf(buf, sizeof(buf), "%X", UINT_MAX);
797 EXPECT_STREQ("FFFFFFFF", buf);
798}
799
Christopher Ferris13f26a72016-01-13 13:47:58 -0800800TEST(STDIO_TEST, snprintf_e) {
Elliott Hughes4bd97ce2014-04-10 17:48:14 -0700801 char buf[BUFSIZ];
802
803 snprintf(buf, sizeof(buf), "%e", 1.5);
804 EXPECT_STREQ("1.500000e+00", buf);
805
Chih-Hung Hsiehc2edae32018-12-11 15:16:24 -0800806 snprintf(buf, sizeof(buf), "%Le", 1.5L);
Elliott Hughes4bd97ce2014-04-10 17:48:14 -0700807 EXPECT_STREQ("1.500000e+00", buf);
808}
809
Christopher Ferris13f26a72016-01-13 13:47:58 -0800810TEST(STDIO_TEST, snprintf_negative_zero_5084292) {
Elliott Hughese77f38f2014-05-14 12:39:12 -0700811 char buf[BUFSIZ];
812
Elliott Hughes1b18aff2014-12-16 14:45:32 -0800813 snprintf(buf, sizeof(buf), "%e", -0.0);
814 EXPECT_STREQ("-0.000000e+00", buf);
815 snprintf(buf, sizeof(buf), "%E", -0.0);
816 EXPECT_STREQ("-0.000000E+00", buf);
Elliott Hughese77f38f2014-05-14 12:39:12 -0700817 snprintf(buf, sizeof(buf), "%f", -0.0);
818 EXPECT_STREQ("-0.000000", buf);
Elliott Hughes1b18aff2014-12-16 14:45:32 -0800819 snprintf(buf, sizeof(buf), "%F", -0.0);
820 EXPECT_STREQ("-0.000000", buf);
821 snprintf(buf, sizeof(buf), "%g", -0.0);
822 EXPECT_STREQ("-0", buf);
823 snprintf(buf, sizeof(buf), "%G", -0.0);
824 EXPECT_STREQ("-0", buf);
825 snprintf(buf, sizeof(buf), "%a", -0.0);
826 EXPECT_STREQ("-0x0p+0", buf);
827 snprintf(buf, sizeof(buf), "%A", -0.0);
828 EXPECT_STREQ("-0X0P+0", buf);
Elliott Hughese77f38f2014-05-14 12:39:12 -0700829}
830
Christopher Ferris13f26a72016-01-13 13:47:58 -0800831TEST(STDIO_TEST, snprintf_utf8_15439554) {
Yi Kong32bc0fc2018-08-02 17:31:13 -0700832 locale_t cloc = newlocale(LC_ALL, "C.UTF-8", nullptr);
Wally Yaua40fdbd2014-08-26 09:47:23 -0700833 locale_t old_locale = uselocale(cloc);
Dan Albert1aec7c12014-07-30 10:53:48 -0700834
Elliott Hughes69f05d22014-06-05 20:10:09 -0700835 // http://b/15439554
836 char buf[BUFSIZ];
837
838 // 1-byte character.
839 snprintf(buf, sizeof(buf), "%dx%d", 1, 2);
840 EXPECT_STREQ("1x2", buf);
841 // 2-byte character.
842 snprintf(buf, sizeof(buf), "%d\xc2\xa2%d", 1, 2);
843 EXPECT_STREQ("1¢2", buf);
844 // 3-byte character.
845 snprintf(buf, sizeof(buf), "%d\xe2\x82\xac%d", 1, 2);
846 EXPECT_STREQ("1€2", buf);
847 // 4-byte character.
848 snprintf(buf, sizeof(buf), "%d\xf0\xa4\xad\xa2%d", 1, 2);
849 EXPECT_STREQ("1𤭢2", buf);
Dan Albert1aec7c12014-07-30 10:53:48 -0700850
Wally Yaua40fdbd2014-08-26 09:47:23 -0700851 uselocale(old_locale);
Dan Albert1aec7c12014-07-30 10:53:48 -0700852 freelocale(cloc);
Elliott Hughes69f05d22014-06-05 20:10:09 -0700853}
854
Elliott Hughes43f7c872016-02-05 11:18:41 -0800855static void* snprintf_small_stack_fn(void*) {
856 // Make life (realistically) hard for ourselves by allocating our own buffer for the result.
857 char buf[PATH_MAX];
858 snprintf(buf, sizeof(buf), "/proc/%d", getpid());
859 return nullptr;
860}
861
862TEST(STDIO_TEST, snprintf_small_stack) {
863 // Is it safe to call snprintf on a thread with a small stack?
864 // (The snprintf implementation puts some pretty large buffers on the stack.)
865 pthread_attr_t a;
866 ASSERT_EQ(0, pthread_attr_init(&a));
867 ASSERT_EQ(0, pthread_attr_setstacksize(&a, PTHREAD_STACK_MIN));
868
869 pthread_t t;
870 ASSERT_EQ(0, pthread_create(&t, &a, snprintf_small_stack_fn, nullptr));
871 ASSERT_EQ(0, pthread_join(t, nullptr));
872}
873
Elliott Hughes8200e552016-02-05 21:57:37 -0800874TEST(STDIO_TEST, snprintf_asterisk_overflow) {
875 char buf[128];
876 ASSERT_EQ(5, snprintf(buf, sizeof(buf), "%.*s%c", 4, "hello world", '!'));
877 ASSERT_EQ(12, snprintf(buf, sizeof(buf), "%.*s%c", INT_MAX/2, "hello world", '!'));
878 ASSERT_EQ(12, snprintf(buf, sizeof(buf), "%.*s%c", INT_MAX-1, "hello world", '!'));
879 ASSERT_EQ(12, snprintf(buf, sizeof(buf), "%.*s%c", INT_MAX, "hello world", '!'));
880 ASSERT_EQ(12, snprintf(buf, sizeof(buf), "%.*s%c", -1, "hello world", '!'));
881
882 // INT_MAX-1, INT_MAX, INT_MAX+1.
883 ASSERT_EQ(12, snprintf(buf, sizeof(buf), "%.2147483646s%c", "hello world", '!'));
884 ASSERT_EQ(12, snprintf(buf, sizeof(buf), "%.2147483647s%c", "hello world", '!'));
885 ASSERT_EQ(-1, snprintf(buf, sizeof(buf), "%.2147483648s%c", "hello world", '!'));
886 ASSERT_EQ(ENOMEM, errno);
887}
888
Elliott Hughes5dc31302020-01-07 08:48:10 -0800889// Inspired by https://github.com/landley/toybox/issues/163.
890TEST(STDIO_TEST, printf_NULL) {
891 char buf[128];
892 char* null = nullptr;
893 EXPECT_EQ(4, snprintf(buf, sizeof(buf), "<%*.*s>", 2, 2, null));
894 EXPECT_STREQ("<(n>", buf);
895 EXPECT_EQ(8, snprintf(buf, sizeof(buf), "<%*.*s>", 2, 8, null));
896 EXPECT_STREQ("<(null)>", buf);
897 EXPECT_EQ(10, snprintf(buf, sizeof(buf), "<%*.*s>", 8, 2, null));
898 EXPECT_STREQ("< (n>", buf);
899 EXPECT_EQ(10, snprintf(buf, sizeof(buf), "<%*.*s>", 8, 8, null));
900 EXPECT_STREQ("< (null)>", buf);
901}
902
Elliott Hughes70715da2016-08-01 16:35:17 -0700903TEST(STDIO_TEST, fprintf) {
904 TemporaryFile tf;
905
906 FILE* tfile = fdopen(tf.fd, "r+");
907 ASSERT_TRUE(tfile != nullptr);
908
909 ASSERT_EQ(7, fprintf(tfile, "%d %s", 123, "abc"));
910 AssertFileIs(tfile, "123 abc");
911 fclose(tfile);
912}
913
Christopher Ferris13f26a72016-01-13 13:47:58 -0800914TEST(STDIO_TEST, fprintf_failures_7229520) {
Elliott Hughes69f05d22014-06-05 20:10:09 -0700915 // http://b/7229520
Elliott Hughesc9244bd2014-05-14 13:31:35 -0700916 FILE* fp;
Josh Gaof6e5b582018-06-01 15:30:54 -0700917 int fd_rdonly = open("/dev/null", O_RDONLY);
918 ASSERT_NE(-1, fd_rdonly);
Elliott Hughesc9244bd2014-05-14 13:31:35 -0700919
920 // Unbuffered case where the fprintf(3) itself fails.
921 ASSERT_NE(nullptr, fp = tmpfile());
Yi Kong32bc0fc2018-08-02 17:31:13 -0700922 setbuf(fp, nullptr);
Elliott Hughesc9244bd2014-05-14 13:31:35 -0700923 ASSERT_EQ(4, fprintf(fp, "epic"));
Josh Gaof6e5b582018-06-01 15:30:54 -0700924 ASSERT_NE(-1, dup2(fd_rdonly, fileno(fp)));
Elliott Hughesc9244bd2014-05-14 13:31:35 -0700925 ASSERT_EQ(-1, fprintf(fp, "fail"));
Josh Gaof6e5b582018-06-01 15:30:54 -0700926 ASSERT_EQ(0, fclose(fp));
Elliott Hughesc9244bd2014-05-14 13:31:35 -0700927
928 // Buffered case where we won't notice until the fclose(3).
929 // It's likely this is what was actually seen in http://b/7229520,
930 // and that expecting fprintf to fail is setting yourself up for
931 // disappointment. Remember to check fclose(3)'s return value, kids!
932 ASSERT_NE(nullptr, fp = tmpfile());
933 ASSERT_EQ(4, fprintf(fp, "epic"));
Josh Gaof6e5b582018-06-01 15:30:54 -0700934 ASSERT_NE(-1, dup2(fd_rdonly, fileno(fp)));
Elliott Hughesc9244bd2014-05-14 13:31:35 -0700935 ASSERT_EQ(4, fprintf(fp, "fail"));
936 ASSERT_EQ(-1, fclose(fp));
937}
938
Elliott Hughes468efc82018-07-10 14:39:49 -0700939TEST(STDIO_TEST, popen_r) {
Elliott Hughes6b3f49a2013-03-06 16:20:55 -0800940 FILE* fp = popen("cat /proc/version", "r");
Yi Kong32bc0fc2018-08-02 17:31:13 -0700941 ASSERT_TRUE(fp != nullptr);
Elliott Hughes6b3f49a2013-03-06 16:20:55 -0800942
943 char buf[16];
944 char* s = fgets(buf, sizeof(buf), fp);
945 buf[13] = '\0';
946 ASSERT_STREQ("Linux version", s);
947
948 ASSERT_EQ(0, pclose(fp));
949}
Elliott Hughes6b05c8e2013-04-11 13:54:48 -0700950
Elliott Hughes468efc82018-07-10 14:39:49 -0700951TEST(STDIO_TEST, popen_socketpair) {
952 FILE* fp = popen("cat", "r+");
Yi Kong32bc0fc2018-08-02 17:31:13 -0700953 ASSERT_TRUE(fp != nullptr);
Elliott Hughes468efc82018-07-10 14:39:49 -0700954
955 fputs("hello\nworld\n", fp);
956 fflush(fp);
957
958 char buf[16];
959 ASSERT_NE(nullptr, fgets(buf, sizeof(buf), fp));
960 EXPECT_STREQ("hello\n", buf);
961 ASSERT_NE(nullptr, fgets(buf, sizeof(buf), fp));
962 EXPECT_STREQ("world\n", buf);
963
964 ASSERT_EQ(0, pclose(fp));
965}
966
967TEST(STDIO_TEST, popen_socketpair_shutdown) {
968 FILE* fp = popen("uniq -c", "r+");
Yi Kong32bc0fc2018-08-02 17:31:13 -0700969 ASSERT_TRUE(fp != nullptr);
Elliott Hughes468efc82018-07-10 14:39:49 -0700970
971 fputs("a\na\na\na\nb\n", fp);
972 fflush(fp);
973 ASSERT_EQ(0, shutdown(fileno(fp), SHUT_WR));
974
975 char buf[16];
976 ASSERT_NE(nullptr, fgets(buf, sizeof(buf), fp));
977 EXPECT_STREQ(" 4 a\n", buf);
978 ASSERT_NE(nullptr, fgets(buf, sizeof(buf), fp));
979 EXPECT_STREQ(" 1 b\n", buf);
980
981 ASSERT_EQ(0, pclose(fp));
982}
983
984TEST(STDIO_TEST, popen_return_value_0) {
985 FILE* fp = popen("true", "r");
Yi Kong32bc0fc2018-08-02 17:31:13 -0700986 ASSERT_TRUE(fp != nullptr);
Elliott Hughes468efc82018-07-10 14:39:49 -0700987 int status = pclose(fp);
988 EXPECT_TRUE(WIFEXITED(status));
989 EXPECT_EQ(0, WEXITSTATUS(status));
990}
991
992TEST(STDIO_TEST, popen_return_value_1) {
993 FILE* fp = popen("false", "r");
Yi Kong32bc0fc2018-08-02 17:31:13 -0700994 ASSERT_TRUE(fp != nullptr);
Elliott Hughes468efc82018-07-10 14:39:49 -0700995 int status = pclose(fp);
996 EXPECT_TRUE(WIFEXITED(status));
997 EXPECT_EQ(1, WEXITSTATUS(status));
998}
999
1000TEST(STDIO_TEST, popen_return_value_signal) {
1001 FILE* fp = popen("kill -7 $$", "r");
Yi Kong32bc0fc2018-08-02 17:31:13 -07001002 ASSERT_TRUE(fp != nullptr);
Elliott Hughes468efc82018-07-10 14:39:49 -07001003 int status = pclose(fp);
1004 EXPECT_TRUE(WIFSIGNALED(status));
1005 EXPECT_EQ(7, WTERMSIG(status));
1006}
1007
Christopher Ferris13f26a72016-01-13 13:47:58 -08001008TEST(STDIO_TEST, getc) {
Elliott Hughes6b05c8e2013-04-11 13:54:48 -07001009 FILE* fp = fopen("/proc/version", "r");
Yi Kong32bc0fc2018-08-02 17:31:13 -07001010 ASSERT_TRUE(fp != nullptr);
Elliott Hughes6b05c8e2013-04-11 13:54:48 -07001011 ASSERT_EQ('L', getc(fp));
1012 ASSERT_EQ('i', getc(fp));
1013 ASSERT_EQ('n', getc(fp));
1014 ASSERT_EQ('u', getc(fp));
1015 ASSERT_EQ('x', getc(fp));
1016 fclose(fp);
1017}
1018
Christopher Ferris13f26a72016-01-13 13:47:58 -08001019TEST(STDIO_TEST, putc) {
Elliott Hughes6b05c8e2013-04-11 13:54:48 -07001020 FILE* fp = fopen("/proc/version", "r");
Yi Kong32bc0fc2018-08-02 17:31:13 -07001021 ASSERT_TRUE(fp != nullptr);
Elliott Hughes6b05c8e2013-04-11 13:54:48 -07001022 ASSERT_EQ(EOF, putc('x', fp));
1023 fclose(fp);
1024}
Elliott Hughes603332f2014-03-12 17:10:41 -07001025
Elliott Hughes7f0849f2016-08-26 16:17:17 -07001026TEST(STDIO_TEST, sscanf_swscanf) {
1027 struct stuff {
1028 char s1[123];
Elliott Hughes0d3ba1f2017-12-06 16:41:35 -08001029 int i1, i2;
1030 char cs1[3];
1031 char s2[3];
1032 char c1;
Elliott Hughes7f0849f2016-08-26 16:17:17 -07001033 double d1;
1034 float f1;
Elliott Hughes0d3ba1f2017-12-06 16:41:35 -08001035 char s3[123];
Elliott Hughes7f0849f2016-08-26 16:17:17 -07001036
1037 void Check() {
Elliott Hughes0d3ba1f2017-12-06 16:41:35 -08001038 EXPECT_STREQ("hello", s1);
1039 EXPECT_EQ(123, i1);
1040 EXPECT_EQ(456, i2);
1041 EXPECT_EQ('a', cs1[0]);
1042 EXPECT_EQ('b', cs1[1]);
1043 EXPECT_EQ('x', cs1[2]); // No terminating NUL.
1044 EXPECT_STREQ("AB", s2); // Terminating NUL.
1045 EXPECT_EQ('!', c1);
1046 EXPECT_DOUBLE_EQ(1.23, d1);
1047 EXPECT_FLOAT_EQ(9.0f, f1);
1048 EXPECT_STREQ("world", s3);
Elliott Hughes7f0849f2016-08-26 16:17:17 -07001049 }
1050 } s;
1051
Elliott Hughes0d3ba1f2017-12-06 16:41:35 -08001052 memset(&s, 'x', sizeof(s));
1053 ASSERT_EQ(9, sscanf(" hello 123 456abAB! 1.23 0x1.2p3 world",
1054 "%s %i%i%2c%[A-Z]%c %lf %f %s",
1055 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 -07001056 s.Check();
1057
Elliott Hughes0d3ba1f2017-12-06 16:41:35 -08001058 memset(&s, 'x', sizeof(s));
1059 ASSERT_EQ(9, swscanf(L" hello 123 456abAB! 1.23 0x1.2p3 world",
1060 L"%s %i%i%2c%[A-Z]%c %lf %f %s",
1061 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 -07001062 s.Check();
Elliott Hughes603332f2014-03-12 17:10:41 -07001063}
Elliott Hughes53b24382014-05-02 18:29:25 -07001064
Elliott Hughes0d3ba1f2017-12-06 16:41:35 -08001065template <typename T>
1066static void CheckScanf(int sscanf_fn(const T*, const T*, ...),
1067 const T* input, const T* fmt,
1068 int expected_count, const char* expected_string) {
1069 char buf[256] = {};
1070 ASSERT_EQ(expected_count, sscanf_fn(input, fmt, &buf)) << fmt;
1071 ASSERT_STREQ(expected_string, buf) << fmt;
1072}
1073
1074TEST(STDIO_TEST, sscanf_ccl) {
1075 // `abc` is just those characters.
1076 CheckScanf(sscanf, "abcd", "%[abc]", 1, "abc");
1077 // `a-c` is the range 'a' .. 'c'.
1078 CheckScanf(sscanf, "abcd", "%[a-c]", 1, "abc");
1079 CheckScanf(sscanf, "-d", "%[a-c]", 0, "");
1080 CheckScanf(sscanf, "ac-bAd", "%[a--c]", 1, "ac-bA");
1081 // `a-c-e` is equivalent to `a-e`.
1082 CheckScanf(sscanf, "abcdefg", "%[a-c-e]", 1, "abcde");
1083 // `e-a` is equivalent to `ae-` (because 'e' > 'a').
1084 CheckScanf(sscanf, "-a-e-b", "%[e-a]", 1, "-a-e-");
1085 // An initial '^' negates the set.
1086 CheckScanf(sscanf, "abcde", "%[^d]", 1, "abc");
1087 CheckScanf(sscanf, "abcdefgh", "%[^c-d]", 1, "ab");
1088 CheckScanf(sscanf, "hgfedcba", "%[^c-d]", 1, "hgfe");
1089 // The first character may be ']' or '-' without being special.
1090 CheckScanf(sscanf, "[[]]x", "%[][]", 1, "[[]]");
1091 CheckScanf(sscanf, "-a-x", "%[-a]", 1, "-a-");
1092 // The last character may be '-' without being special.
1093 CheckScanf(sscanf, "-a-x", "%[a-]", 1, "-a-");
1094 // X--Y is [X--] + Y, not [X--] + [--Y] (a bug in my initial implementation).
1095 CheckScanf(sscanf, "+,-/.", "%[+--/]", 1, "+,-/");
1096}
1097
1098TEST(STDIO_TEST, swscanf_ccl) {
1099 // `abc` is just those characters.
1100 CheckScanf(swscanf, L"abcd", L"%[abc]", 1, "abc");
1101 // `a-c` is the range 'a' .. 'c'.
1102 CheckScanf(swscanf, L"abcd", L"%[a-c]", 1, "abc");
1103 CheckScanf(swscanf, L"-d", L"%[a-c]", 0, "");
1104 CheckScanf(swscanf, L"ac-bAd", L"%[a--c]", 1, "ac-bA");
1105 // `a-c-e` is equivalent to `a-e`.
1106 CheckScanf(swscanf, L"abcdefg", L"%[a-c-e]", 1, "abcde");
1107 // `e-a` is equivalent to `ae-` (because 'e' > 'a').
1108 CheckScanf(swscanf, L"-a-e-b", L"%[e-a]", 1, "-a-e-");
1109 // An initial '^' negates the set.
1110 CheckScanf(swscanf, L"abcde", L"%[^d]", 1, "abc");
1111 CheckScanf(swscanf, L"abcdefgh", L"%[^c-d]", 1, "ab");
1112 CheckScanf(swscanf, L"hgfedcba", L"%[^c-d]", 1, "hgfe");
1113 // The first character may be ']' or '-' without being special.
1114 CheckScanf(swscanf, L"[[]]x", L"%[][]", 1, "[[]]");
1115 CheckScanf(swscanf, L"-a-x", L"%[-a]", 1, "-a-");
1116 // The last character may be '-' without being special.
1117 CheckScanf(swscanf, L"-a-x", L"%[a-]", 1, "-a-");
1118 // X--Y is [X--] + Y, not [X--] + [--Y] (a bug in my initial implementation).
1119 CheckScanf(swscanf, L"+,-/.", L"%[+--/]", 1, "+,-/");
1120}
1121
Elliott Hughes38e4aef2018-01-18 10:21:29 -08001122template <typename T1, typename T2>
1123static void CheckScanfM(int sscanf_fn(const T1*, const T1*, ...),
1124 const T1* input, const T1* fmt,
1125 int expected_count, const T2* expected_string) {
1126 T2* result = nullptr;
1127 ASSERT_EQ(expected_count, sscanf_fn(input, fmt, &result)) << fmt;
1128 if (expected_string == nullptr) {
1129 ASSERT_EQ(nullptr, result);
1130 } else {
1131 ASSERT_STREQ(expected_string, result) << fmt;
1132 }
1133 free(result);
1134}
1135
1136TEST(STDIO_TEST, sscanf_mc) {
1137 char* p1 = nullptr;
1138 char* p2 = nullptr;
1139 ASSERT_EQ(2, sscanf("hello", "%mc%mc", &p1, &p2));
1140 ASSERT_EQ('h', *p1);
1141 ASSERT_EQ('e', *p2);
1142 free(p1);
1143 free(p2);
1144
1145 p1 = nullptr;
1146 ASSERT_EQ(1, sscanf("hello", "%4mc", &p1));
1147 ASSERT_EQ('h', p1[0]);
1148 ASSERT_EQ('e', p1[1]);
1149 ASSERT_EQ('l', p1[2]);
1150 ASSERT_EQ('l', p1[3]);
1151 free(p1);
1152
1153 p1 = nullptr;
1154 ASSERT_EQ(1, sscanf("hello world", "%30mc", &p1));
1155 ASSERT_EQ('h', p1[0]);
1156 ASSERT_EQ('e', p1[1]);
1157 ASSERT_EQ('l', p1[2]);
1158 ASSERT_EQ('l', p1[3]);
1159 ASSERT_EQ('o', p1[4]);
1160 free(p1);
1161}
1162
Elliott Hughes38e4aef2018-01-18 10:21:29 -08001163TEST(STDIO_TEST, sscanf_mlc) {
1164 // This is so useless that clang doesn't even believe it exists...
1165#pragma clang diagnostic push
1166#pragma clang diagnostic ignored "-Wformat-invalid-specifier"
1167#pragma clang diagnostic ignored "-Wformat-extra-args"
1168
1169 wchar_t* p1 = nullptr;
1170 wchar_t* p2 = nullptr;
1171 ASSERT_EQ(2, sscanf("hello", "%mlc%mlc", &p1, &p2));
1172 ASSERT_EQ(L'h', *p1);
1173 ASSERT_EQ(L'e', *p2);
1174 free(p1);
1175 free(p2);
1176
1177 p1 = nullptr;
1178 ASSERT_EQ(1, sscanf("hello", "%4mlc", &p1));
1179 ASSERT_EQ(L'h', p1[0]);
1180 ASSERT_EQ(L'e', p1[1]);
1181 ASSERT_EQ(L'l', p1[2]);
1182 ASSERT_EQ(L'l', p1[3]);
1183 free(p1);
1184
1185 p1 = nullptr;
1186 ASSERT_EQ(1, sscanf("hello world", "%30mlc", &p1));
1187 ASSERT_EQ(L'h', p1[0]);
1188 ASSERT_EQ(L'e', p1[1]);
1189 ASSERT_EQ(L'l', p1[2]);
1190 ASSERT_EQ(L'l', p1[3]);
1191 ASSERT_EQ(L'o', p1[4]);
1192 free(p1);
1193#pragma clang diagnostic pop
1194}
1195
Elliott Hughes38e4aef2018-01-18 10:21:29 -08001196TEST(STDIO_TEST, sscanf_ms) {
1197 CheckScanfM(sscanf, "hello", "%ms", 1, "hello");
1198 CheckScanfM(sscanf, "hello", "%4ms", 1, "hell");
1199 CheckScanfM(sscanf, "hello world", "%30ms", 1, "hello");
1200}
1201
1202TEST(STDIO_TEST, sscanf_mls) {
1203 CheckScanfM(sscanf, "hello", "%mls", 1, L"hello");
1204 CheckScanfM(sscanf, "hello", "%4mls", 1, L"hell");
1205 CheckScanfM(sscanf, "hello world", "%30mls", 1, L"hello");
1206}
1207
1208TEST(STDIO_TEST, sscanf_m_ccl) {
1209 CheckScanfM(sscanf, "hello", "%m[a-z]", 1, "hello");
1210 CheckScanfM(sscanf, "hello", "%4m[a-z]", 1, "hell");
1211 CheckScanfM(sscanf, "hello world", "%30m[a-z]", 1, "hello");
1212}
1213
1214TEST(STDIO_TEST, sscanf_ml_ccl) {
1215 CheckScanfM(sscanf, "hello", "%ml[a-z]", 1, L"hello");
1216 CheckScanfM(sscanf, "hello", "%4ml[a-z]", 1, L"hell");
1217 CheckScanfM(sscanf, "hello world", "%30ml[a-z]", 1, L"hello");
1218}
1219
1220TEST(STDIO_TEST, sscanf_ls) {
1221 wchar_t w[32] = {};
1222 ASSERT_EQ(1, sscanf("hello world", "%ls", w));
1223 ASSERT_EQ(L"hello", std::wstring(w));
1224}
1225
1226TEST(STDIO_TEST, sscanf_ls_suppress) {
1227 ASSERT_EQ(0, sscanf("hello world", "%*ls %*ls"));
1228}
1229
1230TEST(STDIO_TEST, sscanf_ls_n) {
1231 setlocale(LC_ALL, "C.UTF-8");
1232 wchar_t w[32] = {};
1233 int pos = 0;
1234 ASSERT_EQ(1, sscanf("\xc4\x80", "%ls%n", w, &pos));
1235 ASSERT_EQ(static_cast<wchar_t>(256), w[0]);
1236 ASSERT_EQ(2, pos);
1237}
1238
1239TEST(STDIO_TEST, sscanf_ls_realloc) {
1240 // This is so useless that clang doesn't even believe it exists...
1241#pragma clang diagnostic push
1242#pragma clang diagnostic ignored "-Wformat-invalid-specifier"
1243#pragma clang diagnostic ignored "-Wformat-extra-args"
1244 wchar_t* p1 = nullptr;
1245 wchar_t* p2 = nullptr;
1246 ASSERT_EQ(2, sscanf("1234567890123456789012345678901234567890 world", "%mls %mls", &p1, &p2));
1247 ASSERT_EQ(L"1234567890123456789012345678901234567890", std::wstring(p1));
1248 ASSERT_EQ(L"world", std::wstring(p2));
1249#pragma clang diagnostic pop
1250}
1251
Elliott Hughesbf9cb9e2017-12-11 12:39:01 -08001252// https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=202240
1253TEST(STDIO_TEST, scanf_wscanf_EOF) {
1254 EXPECT_EQ(0, sscanf("b", "ab"));
1255 EXPECT_EQ(EOF, sscanf("", "a"));
1256 EXPECT_EQ(0, swscanf(L"b", L"ab"));
1257 EXPECT_EQ(EOF, swscanf(L"", L"a"));
1258}
1259
1260TEST(STDIO_TEST, scanf_invalid_UTF8) {
1261#if 0 // TODO: more tests invented during code review; no regressions, so fix later.
1262 char buf[BUFSIZ];
1263 wchar_t wbuf[BUFSIZ];
1264
1265 memset(buf, 0, sizeof(buf));
1266 memset(wbuf, 0, sizeof(wbuf));
1267 EXPECT_EQ(0, sscanf("\xc0" " foo", "%ls %s", wbuf, buf));
1268#endif
1269}
1270
1271TEST(STDIO_TEST, scanf_no_match_no_termination) {
1272 char buf[4] = "x";
1273 EXPECT_EQ(0, sscanf("d", "%[abc]", buf));
1274 EXPECT_EQ('x', buf[0]);
1275 EXPECT_EQ(0, swscanf(L"d", L"%[abc]", buf));
1276 EXPECT_EQ('x', buf[0]);
1277
1278 wchar_t wbuf[4] = L"x";
1279 EXPECT_EQ(0, swscanf(L"d", L"%l[abc]", wbuf));
1280 EXPECT_EQ(L'x', wbuf[0]);
1281
1282 EXPECT_EQ(EOF, sscanf("", "%s", buf));
1283 EXPECT_EQ('x', buf[0]);
1284
1285 EXPECT_EQ(EOF, swscanf(L"", L"%ls", wbuf));
1286 EXPECT_EQ(L'x', wbuf[0]);
1287}
1288
1289TEST(STDIO_TEST, scanf_wscanf_wide_character_class) {
1290#if 0 // TODO: more tests invented during code review; no regressions, so fix later.
1291 wchar_t buf[BUFSIZ];
1292
1293 // A wide character shouldn't match an ASCII-only class for scanf or wscanf.
1294 memset(buf, 0, sizeof(buf));
1295 EXPECT_EQ(1, sscanf("xĀyz", "%l[xy]", buf));
1296 EXPECT_EQ(L"x"s, std::wstring(buf));
1297 memset(buf, 0, sizeof(buf));
1298 EXPECT_EQ(1, swscanf(L"xĀyz", L"%l[xy]", buf));
1299 EXPECT_EQ(L"x"s, std::wstring(buf));
1300
1301 // Even if scanf has wide characters in a class, they won't match...
1302 // TODO: is that a bug?
1303 memset(buf, 0, sizeof(buf));
1304 EXPECT_EQ(1, sscanf("xĀyz", "%l[xĀy]", buf));
1305 EXPECT_EQ(L"x"s, std::wstring(buf));
1306 // ...unless you use wscanf.
1307 memset(buf, 0, sizeof(buf));
1308 EXPECT_EQ(1, swscanf(L"xĀyz", L"%l[xĀy]", buf));
1309 EXPECT_EQ(L"xĀy"s, std::wstring(buf));
1310
1311 // Negation only covers ASCII for scanf...
1312 memset(buf, 0, sizeof(buf));
1313 EXPECT_EQ(1, sscanf("xĀyz", "%l[^ab]", buf));
1314 EXPECT_EQ(L"x"s, std::wstring(buf));
1315 // ...but covers wide characters for wscanf.
1316 memset(buf, 0, sizeof(buf));
1317 EXPECT_EQ(1, swscanf(L"xĀyz", L"%l[^ab]", buf));
1318 EXPECT_EQ(L"xĀyz"s, std::wstring(buf));
1319
1320 // We already determined that non-ASCII characters are ignored in scanf classes.
1321 memset(buf, 0, sizeof(buf));
1322 EXPECT_EQ(1, sscanf("x"
1323 "\xc4\x80" // Matches a byte from each wide char in the class.
1324 "\xc6\x82" // Neither byte is in the class.
1325 "yz",
1326 "%l[xy" "\xc5\x80" "\xc4\x81" "]", buf));
1327 EXPECT_EQ(L"x", std::wstring(buf));
1328 // bionic and glibc both behave badly for wscanf, so let's call it right for now...
1329 memset(buf, 0, sizeof(buf));
1330 EXPECT_EQ(1, swscanf(L"x"
1331 L"\xc4\x80"
1332 L"\xc6\x82"
1333 L"yz",
1334 L"%l[xy" L"\xc5\x80" L"\xc4\x81" L"]", buf));
1335 // Note that this isn't L"xĀ" --- although the *bytes* matched, they're
1336 // not put back together as a wide character.
1337 EXPECT_EQ(L"x" L"\xc4" L"\x80", std::wstring(buf));
1338#endif
1339}
1340
Christopher Ferris13f26a72016-01-13 13:47:58 -08001341TEST(STDIO_TEST, cantwrite_EBADF) {
Elliott Hughes53b24382014-05-02 18:29:25 -07001342 // If we open a file read-only...
1343 FILE* fp = fopen("/proc/version", "r");
1344
1345 // ...all attempts to write to that file should return failure.
1346
1347 // They should also set errno to EBADF. This isn't POSIX, but it's traditional.
1348 // glibc gets the wide-character functions wrong.
1349
1350 errno = 0;
1351 EXPECT_EQ(EOF, putc('x', fp));
1352 EXPECT_EQ(EBADF, errno);
1353
1354 errno = 0;
1355 EXPECT_EQ(EOF, fprintf(fp, "hello"));
1356 EXPECT_EQ(EBADF, errno);
1357
1358 errno = 0;
1359 EXPECT_EQ(EOF, fwprintf(fp, L"hello"));
Elliott Hughes063525c2014-05-13 11:19:57 -07001360#if defined(__BIONIC__)
Elliott Hughes53b24382014-05-02 18:29:25 -07001361 EXPECT_EQ(EBADF, errno);
1362#endif
1363
1364 errno = 0;
Elliott Hughes53b24382014-05-02 18:29:25 -07001365 EXPECT_EQ(0U, fwrite("hello", 1, 2, fp));
1366 EXPECT_EQ(EBADF, errno);
1367
1368 errno = 0;
1369 EXPECT_EQ(EOF, fputs("hello", fp));
1370 EXPECT_EQ(EBADF, errno);
1371
1372 errno = 0;
1373 EXPECT_EQ(WEOF, fputwc(L'x', fp));
Elliott Hughes063525c2014-05-13 11:19:57 -07001374#if defined(__BIONIC__)
Elliott Hughes53b24382014-05-02 18:29:25 -07001375 EXPECT_EQ(EBADF, errno);
1376#endif
1377}
Calin Juravle03e4ebe2014-05-08 14:42:06 +01001378
1379// Tests that we can only have a consistent and correct fpos_t when using
1380// f*pos functions (i.e. fpos doesn't get inside a multi byte character).
Christopher Ferris13f26a72016-01-13 13:47:58 -08001381TEST(STDIO_TEST, consistent_fpos_t) {
Calin Juravle03e4ebe2014-05-08 14:42:06 +01001382 ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
1383 uselocale(LC_GLOBAL_LOCALE);
1384
1385 FILE* fp = tmpfile();
Yi Kong32bc0fc2018-08-02 17:31:13 -07001386 ASSERT_TRUE(fp != nullptr);
Calin Juravle03e4ebe2014-05-08 14:42:06 +01001387
1388 wchar_t mb_one_bytes = L'h';
1389 wchar_t mb_two_bytes = 0x00a2;
1390 wchar_t mb_three_bytes = 0x20ac;
1391 wchar_t mb_four_bytes = 0x24b62;
1392
1393 // Write to file.
1394 ASSERT_EQ(mb_one_bytes, static_cast<wchar_t>(fputwc(mb_one_bytes, fp)));
1395 ASSERT_EQ(mb_two_bytes, static_cast<wchar_t>(fputwc(mb_two_bytes, fp)));
1396 ASSERT_EQ(mb_three_bytes, static_cast<wchar_t>(fputwc(mb_three_bytes, fp)));
1397 ASSERT_EQ(mb_four_bytes, static_cast<wchar_t>(fputwc(mb_four_bytes, fp)));
1398
1399 rewind(fp);
1400
1401 // Record each character position.
1402 fpos_t pos1;
1403 fpos_t pos2;
1404 fpos_t pos3;
1405 fpos_t pos4;
1406 fpos_t pos5;
1407 EXPECT_EQ(0, fgetpos(fp, &pos1));
1408 ASSERT_EQ(mb_one_bytes, static_cast<wchar_t>(fgetwc(fp)));
1409 EXPECT_EQ(0, fgetpos(fp, &pos2));
1410 ASSERT_EQ(mb_two_bytes, static_cast<wchar_t>(fgetwc(fp)));
1411 EXPECT_EQ(0, fgetpos(fp, &pos3));
1412 ASSERT_EQ(mb_three_bytes, static_cast<wchar_t>(fgetwc(fp)));
1413 EXPECT_EQ(0, fgetpos(fp, &pos4));
1414 ASSERT_EQ(mb_four_bytes, static_cast<wchar_t>(fgetwc(fp)));
1415 EXPECT_EQ(0, fgetpos(fp, &pos5));
1416
Elliott Hughes063525c2014-05-13 11:19:57 -07001417#if defined(__BIONIC__)
Calin Juravle03e4ebe2014-05-08 14:42:06 +01001418 // Bionic's fpos_t is just an alias for off_t. This is inherited from OpenBSD
1419 // upstream. Glibc differs by storing the mbstate_t inside its fpos_t. In
1420 // Bionic (and upstream OpenBSD) the mbstate_t is stored inside the FILE
1421 // structure.
1422 ASSERT_EQ(0, static_cast<off_t>(pos1));
1423 ASSERT_EQ(1, static_cast<off_t>(pos2));
1424 ASSERT_EQ(3, static_cast<off_t>(pos3));
1425 ASSERT_EQ(6, static_cast<off_t>(pos4));
1426 ASSERT_EQ(10, static_cast<off_t>(pos5));
1427#endif
1428
1429 // Exercise back and forth movements of the position.
1430 ASSERT_EQ(0, fsetpos(fp, &pos2));
1431 ASSERT_EQ(mb_two_bytes, static_cast<wchar_t>(fgetwc(fp)));
1432 ASSERT_EQ(0, fsetpos(fp, &pos1));
1433 ASSERT_EQ(mb_one_bytes, static_cast<wchar_t>(fgetwc(fp)));
1434 ASSERT_EQ(0, fsetpos(fp, &pos4));
1435 ASSERT_EQ(mb_four_bytes, static_cast<wchar_t>(fgetwc(fp)));
1436 ASSERT_EQ(0, fsetpos(fp, &pos3));
1437 ASSERT_EQ(mb_three_bytes, static_cast<wchar_t>(fgetwc(fp)));
1438 ASSERT_EQ(0, fsetpos(fp, &pos5));
1439 ASSERT_EQ(WEOF, fgetwc(fp));
1440
1441 fclose(fp);
1442}
1443
1444// Exercise the interaction between fpos and seek.
Christopher Ferris13f26a72016-01-13 13:47:58 -08001445TEST(STDIO_TEST, fpos_t_and_seek) {
Calin Juravle03e4ebe2014-05-08 14:42:06 +01001446 ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
1447 uselocale(LC_GLOBAL_LOCALE);
1448
Calin Juravle9b95ea92014-05-14 17:07:10 +01001449 // In glibc-2.16 fseek doesn't work properly in wide mode
1450 // (https://sourceware.org/bugzilla/show_bug.cgi?id=14543). One workaround is
1451 // to close and re-open the file. We do it in order to make the test pass
1452 // with all glibcs.
1453
Calin Juravle03e4ebe2014-05-08 14:42:06 +01001454 TemporaryFile tf;
1455 FILE* fp = fdopen(tf.fd, "w+");
Yi Kong32bc0fc2018-08-02 17:31:13 -07001456 ASSERT_TRUE(fp != nullptr);
Calin Juravle03e4ebe2014-05-08 14:42:06 +01001457
1458 wchar_t mb_two_bytes = 0x00a2;
1459 wchar_t mb_three_bytes = 0x20ac;
1460 wchar_t mb_four_bytes = 0x24b62;
1461
1462 // Write to file.
1463 ASSERT_EQ(mb_two_bytes, static_cast<wchar_t>(fputwc(mb_two_bytes, fp)));
1464 ASSERT_EQ(mb_three_bytes, static_cast<wchar_t>(fputwc(mb_three_bytes, fp)));
1465 ASSERT_EQ(mb_four_bytes, static_cast<wchar_t>(fputwc(mb_four_bytes, fp)));
1466
1467 fflush(fp);
1468 fclose(fp);
1469
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -08001470 fp = fopen(tf.path, "r");
Yi Kong32bc0fc2018-08-02 17:31:13 -07001471 ASSERT_TRUE(fp != nullptr);
Calin Juravle03e4ebe2014-05-08 14:42:06 +01001472
1473 // Store a valid position.
1474 fpos_t mb_two_bytes_pos;
1475 ASSERT_EQ(0, fgetpos(fp, &mb_two_bytes_pos));
1476
1477 // Move inside mb_four_bytes with fseek.
1478 long offset_inside_mb = 6;
1479 ASSERT_EQ(0, fseek(fp, offset_inside_mb, SEEK_SET));
1480
1481 // Store the "inside multi byte" position.
1482 fpos_t pos_inside_mb;
1483 ASSERT_EQ(0, fgetpos(fp, &pos_inside_mb));
Elliott Hughes063525c2014-05-13 11:19:57 -07001484#if defined(__BIONIC__)
1485 ASSERT_EQ(offset_inside_mb, static_cast<off_t>(pos_inside_mb));
1486#endif
Calin Juravle03e4ebe2014-05-08 14:42:06 +01001487
1488 // Reading from within a byte should produce an error.
1489 ASSERT_EQ(WEOF, fgetwc(fp));
1490 ASSERT_EQ(EILSEQ, errno);
1491
1492 // Reverting to a valid position should work.
1493 ASSERT_EQ(0, fsetpos(fp, &mb_two_bytes_pos));
1494 ASSERT_EQ(mb_two_bytes, static_cast<wchar_t>(fgetwc(fp)));
1495
1496 // Moving withing a multi byte with fsetpos should work but reading should
1497 // produce an error.
1498 ASSERT_EQ(0, fsetpos(fp, &pos_inside_mb));
1499 ASSERT_EQ(WEOF, fgetwc(fp));
1500 ASSERT_EQ(EILSEQ, errno);
1501
Elliott Hughes3a4c4542017-07-19 17:20:24 -07001502 ASSERT_EQ(0, fclose(fp));
Calin Juravle03e4ebe2014-05-08 14:42:06 +01001503}
Elliott Hughes6b841db2014-08-20 16:10:49 -07001504
Christopher Ferris13f26a72016-01-13 13:47:58 -08001505TEST(STDIO_TEST, fmemopen) {
Elliott Hughes6b841db2014-08-20 16:10:49 -07001506 char buf[16];
1507 memset(buf, 0, sizeof(buf));
1508 FILE* fp = fmemopen(buf, sizeof(buf), "r+");
1509 ASSERT_EQ('<', fputc('<', fp));
1510 ASSERT_NE(EOF, fputs("abc>\n", fp));
1511 fflush(fp);
1512
Elliott Hughes3a4c4542017-07-19 17:20:24 -07001513 // We wrote to the buffer...
Elliott Hughes6b841db2014-08-20 16:10:49 -07001514 ASSERT_STREQ("<abc>\n", buf);
1515
Elliott Hughes3a4c4542017-07-19 17:20:24 -07001516 // And can read back from the file.
Elliott Hughes70715da2016-08-01 16:35:17 -07001517 AssertFileIs(fp, "<abc>\n", true);
Elliott Hughes3a4c4542017-07-19 17:20:24 -07001518 ASSERT_EQ(0, fclose(fp));
Elliott Hughes6b841db2014-08-20 16:10:49 -07001519}
1520
Elliott Hughes3a4c4542017-07-19 17:20:24 -07001521TEST(STDIO_TEST, fmemopen_nullptr) {
Elliott Hughes6b841db2014-08-20 16:10:49 -07001522 FILE* fp = fmemopen(nullptr, 128, "r+");
1523 ASSERT_NE(EOF, fputs("xyz\n", fp));
1524
Elliott Hughes70715da2016-08-01 16:35:17 -07001525 AssertFileIs(fp, "xyz\n", true);
Elliott Hughes3a4c4542017-07-19 17:20:24 -07001526 ASSERT_EQ(0, fclose(fp));
Elliott Hughes6b841db2014-08-20 16:10:49 -07001527}
1528
Elliott Hughes3a4c4542017-07-19 17:20:24 -07001529TEST(STDIO_TEST, fmemopen_trailing_NUL_byte) {
1530 FILE* fp;
1531 char buf[8];
1532
1533 // POSIX: "When a stream open for writing is flushed or closed, a null byte
1534 // shall be written at the current position or at the end of the buffer,
1535 // depending on the size of the contents."
1536 memset(buf, 'x', sizeof(buf));
1537 ASSERT_NE(nullptr, fp = fmemopen(buf, sizeof(buf), "w"));
1538 // Even with nothing written (and not in truncate mode), we'll flush a NUL...
1539 ASSERT_EQ(0, fflush(fp));
1540 EXPECT_EQ("\0xxxxxxx"s, std::string(buf, buf + sizeof(buf)));
1541 // Now write and check that the NUL moves along with our writes...
1542 ASSERT_NE(EOF, fputs("hello", fp));
1543 ASSERT_EQ(0, fflush(fp));
1544 EXPECT_EQ("hello\0xx"s, std::string(buf, buf + sizeof(buf)));
1545 ASSERT_NE(EOF, fputs("wo", fp));
1546 ASSERT_EQ(0, fflush(fp));
1547 EXPECT_EQ("hellowo\0"s, std::string(buf, buf + sizeof(buf)));
1548 ASSERT_EQ(0, fclose(fp));
1549
1550 // "If a stream open for update is flushed or closed and the last write has
1551 // advanced the current buffer size, a null byte shall be written at the end
1552 // of the buffer if it fits."
1553 memset(buf, 'x', sizeof(buf));
1554 ASSERT_NE(nullptr, fp = fmemopen(buf, sizeof(buf), "r+"));
1555 // Nothing written yet, so no advance...
1556 ASSERT_EQ(0, fflush(fp));
1557 EXPECT_EQ("xxxxxxxx"s, std::string(buf, buf + sizeof(buf)));
1558 ASSERT_NE(EOF, fputs("hello", fp));
1559 ASSERT_EQ(0, fclose(fp));
1560}
1561
1562TEST(STDIO_TEST, fmemopen_size) {
1563 FILE* fp;
Elliott Hughes6b841db2014-08-20 16:10:49 -07001564 char buf[16];
Elliott Hughes3a4c4542017-07-19 17:20:24 -07001565 memset(buf, 'x', sizeof(buf));
Elliott Hughes6b841db2014-08-20 16:10:49 -07001566
Elliott Hughes3a4c4542017-07-19 17:20:24 -07001567 // POSIX: "The stream shall also maintain the size of the current buffer
1568 // contents; use of fseek() or fseeko() on the stream with SEEK_END shall
1569 // seek relative to this size."
Elliott Hughes6b841db2014-08-20 16:10:49 -07001570
Elliott Hughes3a4c4542017-07-19 17:20:24 -07001571 // "For modes r and r+ the size shall be set to the value given by the size
1572 // argument."
1573 ASSERT_NE(nullptr, fp = fmemopen(buf, 16, "r"));
1574 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
1575 EXPECT_EQ(16, ftell(fp));
1576 EXPECT_EQ(16, ftello(fp));
1577 ASSERT_EQ(0, fseeko(fp, 0, SEEK_END));
1578 EXPECT_EQ(16, ftell(fp));
1579 EXPECT_EQ(16, ftello(fp));
1580 ASSERT_EQ(0, fclose(fp));
1581 ASSERT_NE(nullptr, fp = fmemopen(buf, 16, "r+"));
1582 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
1583 EXPECT_EQ(16, ftell(fp));
1584 EXPECT_EQ(16, ftello(fp));
1585 ASSERT_EQ(0, fseeko(fp, 0, SEEK_END));
1586 EXPECT_EQ(16, ftell(fp));
1587 EXPECT_EQ(16, ftello(fp));
1588 ASSERT_EQ(0, fclose(fp));
1589
1590 // "For modes w and w+ the initial size shall be zero..."
1591 ASSERT_NE(nullptr, fp = fmemopen(nullptr, 16, "w"));
1592 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
1593 EXPECT_EQ(0, ftell(fp));
1594 EXPECT_EQ(0, ftello(fp));
1595 ASSERT_EQ(0, fseeko(fp, 0, SEEK_END));
1596 EXPECT_EQ(0, ftell(fp));
1597 EXPECT_EQ(0, ftello(fp));
1598 ASSERT_EQ(0, fclose(fp));
1599 ASSERT_NE(nullptr, fp = fmemopen(nullptr, 16, "w+"));
1600 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
1601 EXPECT_EQ(0, ftell(fp));
1602 EXPECT_EQ(0, ftello(fp));
1603 ASSERT_EQ(0, fseeko(fp, 0, SEEK_END));
1604 EXPECT_EQ(0, ftell(fp));
1605 EXPECT_EQ(0, ftello(fp));
1606 ASSERT_EQ(0, fclose(fp));
1607
1608 // "...and for modes a and a+ the initial size shall be:
1609 // 1. Zero, if buf is a null pointer
1610 ASSERT_NE(nullptr, fp = fmemopen(nullptr, 16, "a"));
1611 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
1612 EXPECT_EQ(0, ftell(fp));
1613 EXPECT_EQ(0, ftello(fp));
1614 ASSERT_EQ(0, fseeko(fp, 0, SEEK_END));
1615 EXPECT_EQ(0, ftell(fp));
1616 EXPECT_EQ(0, ftello(fp));
1617 ASSERT_EQ(0, fclose(fp));
1618 ASSERT_NE(nullptr, fp = fmemopen(nullptr, 16, "a+"));
1619 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
1620 EXPECT_EQ(0, ftell(fp));
1621 EXPECT_EQ(0, ftello(fp));
1622 ASSERT_EQ(0, fseeko(fp, 0, SEEK_END));
1623 EXPECT_EQ(0, ftell(fp));
1624 EXPECT_EQ(0, ftello(fp));
1625 ASSERT_EQ(0, fclose(fp));
1626
1627 // 2. The position of the first null byte in the buffer, if one is found
1628 memset(buf, 'x', sizeof(buf));
1629 buf[3] = '\0';
1630 ASSERT_NE(nullptr, fp = fmemopen(buf, 16, "a"));
1631 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
1632 EXPECT_EQ(3, ftell(fp));
1633 EXPECT_EQ(3, ftello(fp));
1634 ASSERT_EQ(0, fseeko(fp, 0, SEEK_END));
1635 EXPECT_EQ(3, ftell(fp));
1636 EXPECT_EQ(3, ftello(fp));
1637 ASSERT_EQ(0, fclose(fp));
1638 memset(buf, 'x', sizeof(buf));
1639 buf[3] = '\0';
1640 ASSERT_NE(nullptr, fp = fmemopen(buf, 16, "a+"));
1641 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
1642 EXPECT_EQ(3, ftell(fp));
1643 EXPECT_EQ(3, ftello(fp));
1644 ASSERT_EQ(0, fseeko(fp, 0, SEEK_END));
1645 EXPECT_EQ(3, ftell(fp));
1646 EXPECT_EQ(3, ftello(fp));
1647 ASSERT_EQ(0, fclose(fp));
1648
1649 // 3. The value of the size argument, if buf is not a null pointer and no
1650 // null byte is found.
1651 memset(buf, 'x', sizeof(buf));
1652 ASSERT_NE(nullptr, fp = fmemopen(buf, 16, "a"));
1653 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
1654 EXPECT_EQ(16, ftell(fp));
1655 EXPECT_EQ(16, ftello(fp));
1656 ASSERT_EQ(0, fseeko(fp, 0, SEEK_END));
1657 EXPECT_EQ(16, ftell(fp));
1658 EXPECT_EQ(16, ftello(fp));
1659 ASSERT_EQ(0, fclose(fp));
1660 memset(buf, 'x', sizeof(buf));
1661 ASSERT_NE(nullptr, fp = fmemopen(buf, 16, "a+"));
1662 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
1663 EXPECT_EQ(16, ftell(fp));
1664 EXPECT_EQ(16, ftello(fp));
1665 ASSERT_EQ(0, fseeko(fp, 0, SEEK_END));
1666 EXPECT_EQ(16, ftell(fp));
1667 EXPECT_EQ(16, ftello(fp));
1668 ASSERT_EQ(0, fclose(fp));
1669}
1670
1671TEST(STDIO_TEST, fmemopen_SEEK_END) {
1672 // fseek SEEK_END is relative to the current string length, not the buffer size.
1673 FILE* fp;
1674 char buf[8];
1675 memset(buf, 'x', sizeof(buf));
1676 strcpy(buf, "str");
1677 ASSERT_NE(nullptr, fp = fmemopen(buf, sizeof(buf), "w+"));
1678 ASSERT_NE(EOF, fputs("string", fp));
1679 EXPECT_EQ(0, fseek(fp, 0, SEEK_END));
1680 EXPECT_EQ(static_cast<long>(strlen("string")), ftell(fp));
1681 EXPECT_EQ(static_cast<off_t>(strlen("string")), ftello(fp));
1682 EXPECT_EQ(0, fclose(fp));
1683
1684 // glibc < 2.22 interpreted SEEK_END the wrong way round (subtracting rather
1685 // than adding).
1686 ASSERT_NE(nullptr, fp = fmemopen(buf, sizeof(buf), "w+"));
1687 ASSERT_NE(EOF, fputs("54321", fp));
1688 EXPECT_EQ(0, fseek(fp, -2, SEEK_END));
1689 EXPECT_EQ('2', fgetc(fp));
1690 EXPECT_EQ(0, fclose(fp));
1691}
1692
1693TEST(STDIO_TEST, fmemopen_seek_invalid) {
1694 char buf[8];
1695 memset(buf, 'x', sizeof(buf));
1696 FILE* fp = fmemopen(buf, sizeof(buf), "w");
1697 ASSERT_TRUE(fp != nullptr);
1698
1699 // POSIX: "An attempt to seek ... to a negative position or to a position
1700 // larger than the buffer size given in the size argument shall fail."
1701 // (There's no mention of what errno should be set to, and glibc doesn't
1702 // set errno in any of these cases.)
1703 EXPECT_EQ(-1, fseek(fp, -2, SEEK_SET));
1704 EXPECT_EQ(-1, fseeko(fp, -2, SEEK_SET));
1705 EXPECT_EQ(-1, fseek(fp, sizeof(buf) + 1, SEEK_SET));
1706 EXPECT_EQ(-1, fseeko(fp, sizeof(buf) + 1, SEEK_SET));
1707}
1708
1709TEST(STDIO_TEST, fmemopen_read_EOF) {
1710 // POSIX: "A read operation on the stream shall not advance the current
1711 // buffer position beyond the current buffer size."
1712 char buf[8];
1713 memset(buf, 'x', sizeof(buf));
1714 FILE* fp = fmemopen(buf, sizeof(buf), "r");
1715 ASSERT_TRUE(fp != nullptr);
1716 char buf2[BUFSIZ];
1717 ASSERT_EQ(8U, fread(buf2, 1, sizeof(buf2), fp));
1718 // POSIX: "Reaching the buffer size in a read operation shall count as
1719 // end-of-file.
1720 ASSERT_TRUE(feof(fp));
1721 ASSERT_EQ(EOF, fgetc(fp));
1722 ASSERT_EQ(0, fclose(fp));
1723}
1724
1725TEST(STDIO_TEST, fmemopen_read_null_bytes) {
1726 // POSIX: "Null bytes in the buffer shall have no special meaning for reads."
1727 char buf[] = "h\0e\0l\0l\0o";
1728 FILE* fp = fmemopen(buf, sizeof(buf), "r");
1729 ASSERT_TRUE(fp != nullptr);
1730 ASSERT_EQ('h', fgetc(fp));
1731 ASSERT_EQ(0, fgetc(fp));
1732 ASSERT_EQ('e', fgetc(fp));
1733 ASSERT_EQ(0, fgetc(fp));
1734 ASSERT_EQ('l', fgetc(fp));
1735 ASSERT_EQ(0, fgetc(fp));
1736 // POSIX: "The read operation shall start at the current buffer position of
1737 // the stream."
1738 char buf2[8];
1739 memset(buf2, 'x', sizeof(buf2));
1740 ASSERT_EQ(4U, fread(buf2, 1, sizeof(buf2), fp));
1741 ASSERT_EQ('l', buf2[0]);
1742 ASSERT_EQ(0, buf2[1]);
1743 ASSERT_EQ('o', buf2[2]);
1744 ASSERT_EQ(0, buf2[3]);
1745 for (size_t i = 4; i < sizeof(buf2); ++i) ASSERT_EQ('x', buf2[i]) << i;
1746 ASSERT_TRUE(feof(fp));
1747 ASSERT_EQ(0, fclose(fp));
1748}
1749
1750TEST(STDIO_TEST, fmemopen_write) {
1751 FILE* fp;
1752 char buf[8];
1753
1754 // POSIX: "A write operation shall start either at the current position of
1755 // the stream (if mode has not specified 'a' as the first character)..."
1756 memset(buf, 'x', sizeof(buf));
1757 ASSERT_NE(nullptr, fp = fmemopen(buf, sizeof(buf), "r+"));
1758 setbuf(fp, nullptr); // Turn off buffering so we can see what's happening as it happens.
1759 ASSERT_EQ(0, fseek(fp, 2, SEEK_SET));
1760 ASSERT_EQ(' ', fputc(' ', fp));
1761 EXPECT_EQ("xx xxxxx", std::string(buf, buf + sizeof(buf)));
1762 ASSERT_EQ(0, fclose(fp));
1763
1764 // "...or at the current size of the stream (if mode had 'a' as the first
1765 // character)." (See the fmemopen_size test for what "size" means, but for
1766 // mode "a", it's the first NUL byte.)
1767 memset(buf, 'x', sizeof(buf));
1768 buf[3] = '\0';
1769 ASSERT_NE(nullptr, fp = fmemopen(buf, sizeof(buf), "a+"));
1770 setbuf(fp, nullptr); // Turn off buffering so we can see what's happening as it happens.
1771 ASSERT_EQ(' ', fputc(' ', fp));
1772 EXPECT_EQ("xxx \0xxx"s, std::string(buf, buf + sizeof(buf)));
1773 ASSERT_EQ(0, fclose(fp));
1774
1775 // "If the current position at the end of the write is larger than the
1776 // current buffer size, the current buffer size shall be set to the current
1777 // position." (See the fmemopen_size test for what "size" means, but to
1778 // query it we SEEK_END with offset 0, and then ftell.)
1779 memset(buf, 'x', sizeof(buf));
1780 ASSERT_NE(nullptr, fp = fmemopen(buf, sizeof(buf), "w+"));
1781 setbuf(fp, nullptr); // Turn off buffering so we can see what's happening as it happens.
1782 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
1783 EXPECT_EQ(0, ftell(fp));
1784 ASSERT_EQ(' ', fputc(' ', fp));
1785 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
1786 EXPECT_EQ(1, ftell(fp));
1787 ASSERT_NE(EOF, fputs("123", fp));
1788 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
1789 EXPECT_EQ(4, ftell(fp));
1790 EXPECT_EQ(" 123\0xxx"s, std::string(buf, buf + sizeof(buf)));
1791 ASSERT_EQ(0, fclose(fp));
1792}
1793
1794TEST(STDIO_TEST, fmemopen_write_EOF) {
1795 // POSIX: "A write operation on the stream shall not advance the current
1796 // buffer size beyond the size given in the size argument."
1797 FILE* fp;
1798
1799 // Scalar writes...
1800 ASSERT_NE(nullptr, fp = fmemopen(nullptr, 4, "w"));
1801 setbuf(fp, nullptr); // Turn off buffering so we can see what's happening as it happens.
1802 ASSERT_EQ('x', fputc('x', fp));
1803 ASSERT_EQ('x', fputc('x', fp));
1804 ASSERT_EQ('x', fputc('x', fp));
1805 ASSERT_EQ(EOF, fputc('x', fp)); // Only 3 fit because of the implicit NUL.
1806 ASSERT_EQ(0, fclose(fp));
1807
1808 // Vector writes...
1809 ASSERT_NE(nullptr, fp = fmemopen(nullptr, 4, "w"));
1810 setbuf(fp, nullptr); // Turn off buffering so we can see what's happening as it happens.
1811 ASSERT_EQ(3U, fwrite("xxxx", 1, 4, fp));
1812 ASSERT_EQ(0, fclose(fp));
1813}
1814
1815TEST(STDIO_TEST, fmemopen_initial_position) {
1816 // POSIX: "The ... current position in the buffer ... shall be initially
1817 // set to either the beginning of the buffer (for r and w modes) ..."
1818 char buf[] = "hello\0world";
1819 FILE* fp;
1820 ASSERT_NE(nullptr, fp = fmemopen(buf, sizeof(buf), "r"));
1821 EXPECT_EQ(0L, ftell(fp));
1822 EXPECT_EQ(0, fclose(fp));
1823 ASSERT_NE(nullptr, fp = fmemopen(buf, sizeof(buf), "w"));
1824 EXPECT_EQ(0L, ftell(fp));
1825 EXPECT_EQ(0, fclose(fp));
1826 buf[0] = 'h'; // (Undo the effects of the above.)
1827
1828 // POSIX: "...or to the first null byte in the buffer (for a modes)."
1829 ASSERT_NE(nullptr, fp = fmemopen(buf, sizeof(buf), "a"));
1830 EXPECT_EQ(5L, ftell(fp));
1831 EXPECT_EQ(0, fclose(fp));
1832
1833 // POSIX: "If no null byte is found in append mode, the initial position
1834 // shall be set to one byte after the end of the buffer."
1835 memset(buf, 'x', sizeof(buf));
1836 ASSERT_NE(nullptr, fp = fmemopen(buf, sizeof(buf), "a"));
1837 EXPECT_EQ(static_cast<long>(sizeof(buf)), ftell(fp));
1838 EXPECT_EQ(0, fclose(fp));
1839}
1840
1841TEST(STDIO_TEST, fmemopen_initial_position_allocated) {
1842 // POSIX: "If buf is a null pointer, the initial position shall always be
1843 // set to the beginning of the buffer."
1844 FILE* fp = fmemopen(nullptr, 128, "a+");
1845 ASSERT_TRUE(fp != nullptr);
1846 EXPECT_EQ(0L, ftell(fp));
1847 EXPECT_EQ(0L, fseek(fp, 0, SEEK_SET));
1848 EXPECT_EQ(0, fclose(fp));
1849}
1850
1851TEST(STDIO_TEST, fmemopen_zero_length) {
1852 // POSIX says it's up to the implementation whether or not you can have a
1853 // zero-length buffer (but "A future version of this standard may require
1854 // support of zero-length buffer streams explicitly"). BSD and glibc < 2.22
1855 // agreed that you couldn't, but glibc >= 2.22 allows it for consistency.
1856 FILE* fp;
1857 char buf[16];
1858 ASSERT_NE(nullptr, fp = fmemopen(buf, 0, "r+"));
1859 ASSERT_EQ(EOF, fgetc(fp));
1860 ASSERT_TRUE(feof(fp));
1861 ASSERT_EQ(0, fclose(fp));
1862 ASSERT_NE(nullptr, fp = fmemopen(nullptr, 0, "r+"));
1863 ASSERT_EQ(EOF, fgetc(fp));
1864 ASSERT_TRUE(feof(fp));
1865 ASSERT_EQ(0, fclose(fp));
1866
1867 ASSERT_NE(nullptr, fp = fmemopen(buf, 0, "w+"));
1868 setbuf(fp, nullptr); // Turn off buffering so we can see what's happening as it happens.
1869 ASSERT_EQ(EOF, fputc('x', fp));
1870 ASSERT_EQ(0, fclose(fp));
1871 ASSERT_NE(nullptr, fp = fmemopen(nullptr, 0, "w+"));
1872 setbuf(fp, nullptr); // Turn off buffering so we can see what's happening as it happens.
1873 ASSERT_EQ(EOF, fputc('x', fp));
1874 ASSERT_EQ(0, fclose(fp));
1875}
1876
Elliott Hughes288465d2019-02-05 15:00:13 -08001877TEST(STDIO_TEST, fmemopen_zero_length_buffer_overrun) {
1878 char buf[2] = "x";
1879 ASSERT_EQ('x', buf[0]);
1880 FILE* fp = fmemopen(buf, 0, "w");
1881 ASSERT_EQ('x', buf[0]);
1882 ASSERT_EQ(0, fclose(fp));
1883}
1884
Elliott Hughes3a4c4542017-07-19 17:20:24 -07001885TEST(STDIO_TEST, fmemopen_write_only_allocated) {
1886 // POSIX says fmemopen "may fail if the mode argument does not include a '+'".
1887 // BSD fails, glibc doesn't. We side with the more lenient.
1888 FILE* fp;
1889 ASSERT_NE(nullptr, fp = fmemopen(nullptr, 16, "r"));
1890 ASSERT_EQ(0, fclose(fp));
1891 ASSERT_NE(nullptr, fp = fmemopen(nullptr, 16, "w"));
1892 ASSERT_EQ(0, fclose(fp));
1893}
1894
1895TEST(STDIO_TEST, fmemopen_fileno) {
1896 // There's no fd backing an fmemopen FILE*.
1897 FILE* fp = fmemopen(nullptr, 16, "r");
1898 ASSERT_TRUE(fp != nullptr);
Elliott Hughes6b841db2014-08-20 16:10:49 -07001899 errno = 0;
Elliott Hughes3a4c4542017-07-19 17:20:24 -07001900 ASSERT_EQ(-1, fileno(fp));
1901 ASSERT_EQ(EBADF, errno);
1902 ASSERT_EQ(0, fclose(fp));
1903}
1904
1905TEST(STDIO_TEST, fmemopen_append_after_seek) {
1906 // In BSD and glibc < 2.22, append mode didn't force writes to append if
1907 // there had been an intervening seek.
1908
1909 FILE* fp;
1910 char buf[] = "hello\0world";
1911 ASSERT_NE(nullptr, fp = fmemopen(buf, sizeof(buf), "a"));
1912 setbuf(fp, nullptr); // Turn off buffering so we can see what's happening as it happens.
1913 ASSERT_EQ(0, fseek(fp, 0, SEEK_SET));
1914 ASSERT_NE(EOF, fputc('!', fp));
1915 EXPECT_EQ("hello!\0orld\0"s, std::string(buf, buf + sizeof(buf)));
1916 ASSERT_EQ(0, fclose(fp));
1917
1918 memcpy(buf, "hello\0world", sizeof(buf));
1919 ASSERT_NE(nullptr, fp = fmemopen(buf, sizeof(buf), "a+"));
1920 setbuf(fp, nullptr); // Turn off buffering so we can see what's happening as it happens.
1921 ASSERT_EQ(0, fseek(fp, 0, SEEK_SET));
1922 ASSERT_NE(EOF, fputc('!', fp));
1923 EXPECT_EQ("hello!\0orld\0"s, std::string(buf, buf + sizeof(buf)));
1924 ASSERT_EQ(0, fclose(fp));
Elliott Hughes6b841db2014-08-20 16:10:49 -07001925}
1926
Christopher Ferris13f26a72016-01-13 13:47:58 -08001927TEST(STDIO_TEST, open_memstream) {
Elliott Hughes6b841db2014-08-20 16:10:49 -07001928 char* p = nullptr;
1929 size_t size = 0;
1930 FILE* fp = open_memstream(&p, &size);
1931 ASSERT_NE(EOF, fputs("hello, world!", fp));
1932 fclose(fp);
1933
1934 ASSERT_STREQ("hello, world!", p);
1935 ASSERT_EQ(strlen("hello, world!"), size);
1936 free(p);
1937}
1938
Christopher Ferris13f26a72016-01-13 13:47:58 -08001939TEST(STDIO_TEST, open_memstream_EINVAL) {
Elliott Hughes6b841db2014-08-20 16:10:49 -07001940#if defined(__BIONIC__)
1941 char* p;
1942 size_t size;
1943
1944 // Invalid buffer.
1945 errno = 0;
1946 ASSERT_EQ(nullptr, open_memstream(nullptr, &size));
1947 ASSERT_EQ(EINVAL, errno);
1948
1949 // Invalid size.
1950 errno = 0;
1951 ASSERT_EQ(nullptr, open_memstream(&p, nullptr));
1952 ASSERT_EQ(EINVAL, errno);
1953#else
Elliott Hughesbcaa4542019-03-08 15:20:23 -08001954 GTEST_SKIP() << "glibc is broken";
Elliott Hughes6b841db2014-08-20 16:10:49 -07001955#endif
1956}
Elliott Hughes31165ed2014-09-23 17:34:29 -07001957
Christopher Ferris13f26a72016-01-13 13:47:58 -08001958TEST(STDIO_TEST, fdopen_CLOEXEC) {
Elliott Hughes31165ed2014-09-23 17:34:29 -07001959 int fd = open("/proc/version", O_RDONLY);
1960 ASSERT_TRUE(fd != -1);
1961
1962 // This fd doesn't have O_CLOEXEC...
Elliott Hughesa7f12942017-12-15 13:55:53 -08001963 AssertCloseOnExec(fd, false);
Elliott Hughes31165ed2014-09-23 17:34:29 -07001964
1965 FILE* fp = fdopen(fd, "re");
Yi Kong32bc0fc2018-08-02 17:31:13 -07001966 ASSERT_TRUE(fp != nullptr);
Elliott Hughes31165ed2014-09-23 17:34:29 -07001967
1968 // ...but the new one does.
Elliott Hughesa7f12942017-12-15 13:55:53 -08001969 AssertCloseOnExec(fileno(fp), true);
Elliott Hughes31165ed2014-09-23 17:34:29 -07001970
1971 fclose(fp);
Elliott Hughes31165ed2014-09-23 17:34:29 -07001972}
1973
Christopher Ferris13f26a72016-01-13 13:47:58 -08001974TEST(STDIO_TEST, freopen_CLOEXEC) {
Elliott Hughes31165ed2014-09-23 17:34:29 -07001975 FILE* fp = fopen("/proc/version", "r");
Yi Kong32bc0fc2018-08-02 17:31:13 -07001976 ASSERT_TRUE(fp != nullptr);
Elliott Hughes31165ed2014-09-23 17:34:29 -07001977
1978 // This FILE* doesn't have O_CLOEXEC...
Elliott Hughesa7f12942017-12-15 13:55:53 -08001979 AssertCloseOnExec(fileno(fp), false);
Elliott Hughes31165ed2014-09-23 17:34:29 -07001980
1981 fp = freopen("/proc/version", "re", fp);
1982
1983 // ...but the new one does.
Elliott Hughesa7f12942017-12-15 13:55:53 -08001984 AssertCloseOnExec(fileno(fp), true);
Elliott Hughes31165ed2014-09-23 17:34:29 -07001985
1986 fclose(fp);
1987}
Elliott Hughes20841a12014-12-01 16:13:30 -08001988
Elliott Hughesf226ee52016-02-03 11:24:28 -08001989TEST(STDIO_TEST, fopen64_freopen64) {
1990 FILE* fp = fopen64("/proc/version", "r");
1991 ASSERT_TRUE(fp != nullptr);
1992 fp = freopen64("/proc/version", "re", fp);
1993 ASSERT_TRUE(fp != nullptr);
1994 fclose(fp);
1995}
1996
Elliott Hughes20841a12014-12-01 16:13:30 -08001997// https://code.google.com/p/android/issues/detail?id=81155
1998// http://b/18556607
Christopher Ferris13f26a72016-01-13 13:47:58 -08001999TEST(STDIO_TEST, fread_unbuffered_pathological_performance) {
Elliott Hughes20841a12014-12-01 16:13:30 -08002000 FILE* fp = fopen("/dev/zero", "r");
Yi Kong32bc0fc2018-08-02 17:31:13 -07002001 ASSERT_TRUE(fp != nullptr);
Elliott Hughes20841a12014-12-01 16:13:30 -08002002
2003 // Make this stream unbuffered.
Yi Kong32bc0fc2018-08-02 17:31:13 -07002004 setvbuf(fp, nullptr, _IONBF, 0);
Elliott Hughes20841a12014-12-01 16:13:30 -08002005
2006 char buf[65*1024];
2007 memset(buf, 0xff, sizeof(buf));
2008
Yi Kong32bc0fc2018-08-02 17:31:13 -07002009 time_t t0 = time(nullptr);
Elliott Hughes20841a12014-12-01 16:13:30 -08002010 for (size_t i = 0; i < 1024; ++i) {
Elliott Hughese6bb5a22015-01-23 17:48:15 -08002011 ASSERT_EQ(1U, fread(buf, 64*1024, 1, fp));
Elliott Hughes20841a12014-12-01 16:13:30 -08002012 }
Yi Kong32bc0fc2018-08-02 17:31:13 -07002013 time_t t1 = time(nullptr);
Elliott Hughes20841a12014-12-01 16:13:30 -08002014
2015 fclose(fp);
2016
2017 // 1024 64KiB reads should have been very quick.
2018 ASSERT_LE(t1 - t0, 1);
2019
2020 for (size_t i = 0; i < 64*1024; ++i) {
2021 ASSERT_EQ('\0', buf[i]);
2022 }
2023 for (size_t i = 64*1024; i < 65*1024; ++i) {
2024 ASSERT_EQ('\xff', buf[i]);
2025 }
2026}
Elliott Hughes75b99382015-01-20 11:23:50 -08002027
Christopher Ferris13f26a72016-01-13 13:47:58 -08002028TEST(STDIO_TEST, fread_EOF) {
Elliott Hughes0ed7e082015-01-22 15:13:38 -08002029 std::string digits("0123456789");
2030 FILE* fp = fmemopen(&digits[0], digits.size(), "r");
Elliott Hughes75b99382015-01-20 11:23:50 -08002031
2032 // Try to read too much, but little enough that it still fits in the FILE's internal buffer.
2033 char buf1[4 * 4];
2034 memset(buf1, 0, sizeof(buf1));
2035 ASSERT_EQ(2U, fread(buf1, 4, 4, fp));
Elliott Hughes0ed7e082015-01-22 15:13:38 -08002036 ASSERT_STREQ("0123456789", buf1);
Elliott Hughes75b99382015-01-20 11:23:50 -08002037 ASSERT_TRUE(feof(fp));
2038
2039 rewind(fp);
2040
Elliott Hughes0ed7e082015-01-22 15:13:38 -08002041 // Try to read way too much so stdio tries to read more direct from the stream.
2042 char buf2[4 * 4096];
Elliott Hughes75b99382015-01-20 11:23:50 -08002043 memset(buf2, 0, sizeof(buf2));
2044 ASSERT_EQ(2U, fread(buf2, 4, 4096, fp));
Elliott Hughes0ed7e082015-01-22 15:13:38 -08002045 ASSERT_STREQ("0123456789", buf2);
Elliott Hughes75b99382015-01-20 11:23:50 -08002046 ASSERT_TRUE(feof(fp));
2047
2048 fclose(fp);
2049}
Elliott Hughese6bb5a22015-01-23 17:48:15 -08002050
2051static void test_fread_from_write_only_stream(size_t n) {
2052 FILE* fp = fopen("/dev/null", "w");
2053 std::vector<char> buf(n, 0);
2054 errno = 0;
2055 ASSERT_EQ(0U, fread(&buf[0], n, 1, fp));
2056 ASSERT_EQ(EBADF, errno);
2057 ASSERT_TRUE(ferror(fp));
2058 ASSERT_FALSE(feof(fp));
2059 fclose(fp);
2060}
2061
Christopher Ferris13f26a72016-01-13 13:47:58 -08002062TEST(STDIO_TEST, fread_from_write_only_stream_slow_path) {
Elliott Hughese6bb5a22015-01-23 17:48:15 -08002063 test_fread_from_write_only_stream(1);
2064}
2065
Christopher Ferris13f26a72016-01-13 13:47:58 -08002066TEST(STDIO_TEST, fread_from_write_only_stream_fast_path) {
Elliott Hughese6bb5a22015-01-23 17:48:15 -08002067 test_fread_from_write_only_stream(64*1024);
2068}
2069
2070static void test_fwrite_after_fread(size_t n) {
2071 TemporaryFile tf;
2072
2073 FILE* fp = fdopen(tf.fd, "w+");
2074 ASSERT_EQ(1U, fwrite("1", 1, 1, fp));
2075 fflush(fp);
2076
2077 // We've flushed but not rewound, so there's nothing to read.
2078 std::vector<char> buf(n, 0);
2079 ASSERT_EQ(0U, fread(&buf[0], 1, buf.size(), fp));
2080 ASSERT_TRUE(feof(fp));
2081
2082 // But hitting EOF doesn't prevent us from writing...
2083 errno = 0;
Elliott Hughes9677fab2016-01-25 15:50:59 -08002084 ASSERT_EQ(1U, fwrite("2", 1, 1, fp)) << strerror(errno);
Elliott Hughese6bb5a22015-01-23 17:48:15 -08002085
2086 // And if we rewind, everything's there.
2087 rewind(fp);
2088 ASSERT_EQ(2U, fread(&buf[0], 1, buf.size(), fp));
2089 ASSERT_EQ('1', buf[0]);
2090 ASSERT_EQ('2', buf[1]);
2091
2092 fclose(fp);
2093}
2094
Christopher Ferris13f26a72016-01-13 13:47:58 -08002095TEST(STDIO_TEST, fwrite_after_fread_slow_path) {
Elliott Hughese6bb5a22015-01-23 17:48:15 -08002096 test_fwrite_after_fread(16);
2097}
2098
Christopher Ferris13f26a72016-01-13 13:47:58 -08002099TEST(STDIO_TEST, fwrite_after_fread_fast_path) {
Elliott Hughese6bb5a22015-01-23 17:48:15 -08002100 test_fwrite_after_fread(64*1024);
2101}
Christopher Ferriscc9ca102015-02-27 18:22:45 -08002102
2103// http://b/19172514
Christopher Ferris13f26a72016-01-13 13:47:58 -08002104TEST(STDIO_TEST, fread_after_fseek) {
Christopher Ferriscc9ca102015-02-27 18:22:45 -08002105 TemporaryFile tf;
2106
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -08002107 FILE* fp = fopen(tf.path, "w+");
Christopher Ferriscc9ca102015-02-27 18:22:45 -08002108 ASSERT_TRUE(fp != nullptr);
2109
2110 char file_data[12288];
2111 for (size_t i = 0; i < 12288; i++) {
2112 file_data[i] = i;
2113 }
2114 ASSERT_EQ(12288U, fwrite(file_data, 1, 12288, fp));
2115 fclose(fp);
2116
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -08002117 fp = fopen(tf.path, "r");
Christopher Ferriscc9ca102015-02-27 18:22:45 -08002118 ASSERT_TRUE(fp != nullptr);
2119
2120 char buffer[8192];
2121 size_t cur_location = 0;
2122 // Small read to populate internal buffer.
2123 ASSERT_EQ(100U, fread(buffer, 1, 100, fp));
2124 ASSERT_EQ(memcmp(file_data, buffer, 100), 0);
2125
2126 cur_location = static_cast<size_t>(ftell(fp));
2127 // Large read to force reading into the user supplied buffer and bypassing
2128 // the internal buffer.
2129 ASSERT_EQ(8192U, fread(buffer, 1, 8192, fp));
2130 ASSERT_EQ(memcmp(file_data+cur_location, buffer, 8192), 0);
2131
2132 // Small backwards seek to verify fseek does not reuse the internal buffer.
Elliott Hughes9677fab2016-01-25 15:50:59 -08002133 ASSERT_EQ(0, fseek(fp, -22, SEEK_CUR)) << strerror(errno);
Christopher Ferriscc9ca102015-02-27 18:22:45 -08002134 cur_location = static_cast<size_t>(ftell(fp));
2135 ASSERT_EQ(22U, fread(buffer, 1, 22, fp));
2136 ASSERT_EQ(memcmp(file_data+cur_location, buffer, 22), 0);
2137
2138 fclose(fp);
2139}
Elliott Hughes8ab433d2015-10-09 17:57:26 -07002140
2141// https://code.google.com/p/android/issues/detail?id=184847
Christopher Ferris13f26a72016-01-13 13:47:58 -08002142TEST(STDIO_TEST, fread_EOF_184847) {
Elliott Hughes8ab433d2015-10-09 17:57:26 -07002143 TemporaryFile tf;
2144 char buf[6] = {0};
2145
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -08002146 FILE* fw = fopen(tf.path, "w");
Elliott Hughes8ab433d2015-10-09 17:57:26 -07002147 ASSERT_TRUE(fw != nullptr);
2148
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -08002149 FILE* fr = fopen(tf.path, "r");
Elliott Hughes8ab433d2015-10-09 17:57:26 -07002150 ASSERT_TRUE(fr != nullptr);
2151
2152 fwrite("a", 1, 1, fw);
2153 fflush(fw);
2154 ASSERT_EQ(1U, fread(buf, 1, 1, fr));
2155 ASSERT_STREQ("a", buf);
2156
2157 // 'fr' is now at EOF.
2158 ASSERT_EQ(0U, fread(buf, 1, 1, fr));
2159 ASSERT_TRUE(feof(fr));
2160
2161 // Write some more...
2162 fwrite("z", 1, 1, fw);
2163 fflush(fw);
2164
2165 // ...and check that we can read it back.
2166 // (BSD thinks that once a stream has hit EOF, it must always return EOF. SysV disagrees.)
2167 ASSERT_EQ(1U, fread(buf, 1, 1, fr));
2168 ASSERT_STREQ("z", buf);
2169
2170 // But now we're done.
2171 ASSERT_EQ(0U, fread(buf, 1, 1, fr));
2172
2173 fclose(fr);
2174 fclose(fw);
2175}
Elliott Hughes923f1652016-01-19 15:46:05 -08002176
2177TEST(STDIO_TEST, fclose_invalidates_fd) {
2178 // The typical error we're trying to help people catch involves accessing
2179 // memory after it's been freed. But we know that stdin/stdout/stderr are
2180 // special and don't get deallocated, so this test uses stdin.
2181 ASSERT_EQ(0, fclose(stdin));
2182
2183 // Even though using a FILE* after close is undefined behavior, I've closed
2184 // this bug as "WAI" too many times. We shouldn't hand out stale fds,
2185 // especially because they might actually correspond to a real stream.
2186 errno = 0;
2187 ASSERT_EQ(-1, fileno(stdin));
2188 ASSERT_EQ(EBADF, errno);
2189}
Elliott Hughes2704bd12016-01-20 17:14:53 -08002190
2191TEST(STDIO_TEST, fseek_ftell_unseekable) {
2192#if defined(__BIONIC__) // glibc has fopencookie instead.
2193 auto read_fn = [](void*, char*, int) { return -1; };
2194 FILE* fp = funopen(nullptr, read_fn, nullptr, nullptr, nullptr);
2195 ASSERT_TRUE(fp != nullptr);
2196
2197 // Check that ftell balks on an unseekable FILE*.
2198 errno = 0;
2199 ASSERT_EQ(-1, ftell(fp));
2200 ASSERT_EQ(ESPIPE, errno);
2201
2202 // SEEK_CUR is rewritten as SEEK_SET internally...
2203 errno = 0;
2204 ASSERT_EQ(-1, fseek(fp, 0, SEEK_CUR));
2205 ASSERT_EQ(ESPIPE, errno);
2206
2207 // ...so it's worth testing the direct seek path too.
2208 errno = 0;
2209 ASSERT_EQ(-1, fseek(fp, 0, SEEK_SET));
2210 ASSERT_EQ(ESPIPE, errno);
2211
2212 fclose(fp);
Elliott Hughes03e65eb2016-01-26 14:13:04 -08002213#else
Elliott Hughesbcaa4542019-03-08 15:20:23 -08002214 GTEST_SKIP() << "glibc uses fopencookie instead";
Elliott Hughes03e65eb2016-01-26 14:13:04 -08002215#endif
2216}
2217
2218TEST(STDIO_TEST, funopen_EINVAL) {
2219#if defined(__BIONIC__)
2220 errno = 0;
2221 ASSERT_EQ(nullptr, funopen(nullptr, nullptr, nullptr, nullptr, nullptr));
2222 ASSERT_EQ(EINVAL, errno);
2223#else
Elliott Hughesbcaa4542019-03-08 15:20:23 -08002224 GTEST_SKIP() << "glibc uses fopencookie instead";
Elliott Hughes03e65eb2016-01-26 14:13:04 -08002225#endif
2226}
2227
2228TEST(STDIO_TEST, funopen_seek) {
2229#if defined(__BIONIC__)
2230 auto read_fn = [](void*, char*, int) { return -1; };
2231
2232 auto seek_fn = [](void*, fpos_t, int) -> fpos_t { return 0xfedcba12; };
2233 auto seek64_fn = [](void*, fpos64_t, int) -> fpos64_t { return 0xfedcba12345678; };
2234
2235 FILE* fp = funopen(nullptr, read_fn, nullptr, seek_fn, nullptr);
2236 ASSERT_TRUE(fp != nullptr);
2237 fpos_t pos;
Elliott Hughes955426e2016-01-26 18:25:52 -08002238#if defined(__LP64__)
2239 EXPECT_EQ(0, fgetpos(fp, &pos)) << strerror(errno);
2240 EXPECT_EQ(0xfedcba12LL, pos);
2241#else
2242 EXPECT_EQ(-1, fgetpos(fp, &pos)) << strerror(errno);
2243 EXPECT_EQ(EOVERFLOW, errno);
2244#endif
Elliott Hughes03e65eb2016-01-26 14:13:04 -08002245
2246 FILE* fp64 = funopen64(nullptr, read_fn, nullptr, seek64_fn, nullptr);
2247 ASSERT_TRUE(fp64 != nullptr);
2248 fpos64_t pos64;
Elliott Hughes955426e2016-01-26 18:25:52 -08002249 EXPECT_EQ(0, fgetpos64(fp64, &pos64)) << strerror(errno);
2250 EXPECT_EQ(0xfedcba12345678, pos64);
Elliott Hughes03e65eb2016-01-26 14:13:04 -08002251#else
Elliott Hughesbcaa4542019-03-08 15:20:23 -08002252 GTEST_SKIP() << "glibc uses fopencookie instead";
Elliott Hughes2704bd12016-01-20 17:14:53 -08002253#endif
2254}
Elliott Hughes71288cb2016-01-22 19:22:44 -08002255
2256TEST(STDIO_TEST, lots_of_concurrent_files) {
2257 std::vector<TemporaryFile*> tfs;
2258 std::vector<FILE*> fps;
2259
2260 for (size_t i = 0; i < 256; ++i) {
2261 TemporaryFile* tf = new TemporaryFile;
2262 tfs.push_back(tf);
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -08002263 FILE* fp = fopen(tf->path, "w+");
Elliott Hughes71288cb2016-01-22 19:22:44 -08002264 fps.push_back(fp);
2265 fprintf(fp, "hello %zu!\n", i);
2266 fflush(fp);
2267 }
2268
2269 for (size_t i = 0; i < 256; ++i) {
Elliott Hughes71288cb2016-01-22 19:22:44 -08002270 char expected[BUFSIZ];
2271 snprintf(expected, sizeof(expected), "hello %zu!\n", i);
Elliott Hughes71288cb2016-01-22 19:22:44 -08002272
Elliott Hughes70715da2016-08-01 16:35:17 -07002273 AssertFileIs(fps[i], expected);
Elliott Hughes71288cb2016-01-22 19:22:44 -08002274 fclose(fps[i]);
2275 delete tfs[i];
2276 }
2277}
Elliott Hughes9677fab2016-01-25 15:50:59 -08002278
2279static void AssertFileOffsetAt(FILE* fp, off64_t offset) {
2280 EXPECT_EQ(offset, ftell(fp));
2281 EXPECT_EQ(offset, ftello(fp));
Elliott Hughese4fa6e92016-02-02 22:39:15 -08002282 EXPECT_EQ(offset, ftello64(fp));
Elliott Hughes9677fab2016-01-25 15:50:59 -08002283 fpos_t pos;
2284 fpos64_t pos64;
2285 EXPECT_EQ(0, fgetpos(fp, &pos));
2286 EXPECT_EQ(0, fgetpos64(fp, &pos64));
2287#if defined(__BIONIC__)
2288 EXPECT_EQ(offset, static_cast<off64_t>(pos));
2289 EXPECT_EQ(offset, static_cast<off64_t>(pos64));
2290#else
Elliott Hughesbcaa4542019-03-08 15:20:23 -08002291 GTEST_SKIP() << "glibc's fpos_t is opaque";
Elliott Hughes9677fab2016-01-25 15:50:59 -08002292#endif
2293}
2294
2295TEST(STDIO_TEST, seek_tell_family_smoke) {
2296 TemporaryFile tf;
2297 FILE* fp = fdopen(tf.fd, "w+");
2298
2299 // Initially we should be at 0.
2300 AssertFileOffsetAt(fp, 0);
2301
2302 // Seek to offset 8192.
2303 ASSERT_EQ(0, fseek(fp, 8192, SEEK_SET));
2304 AssertFileOffsetAt(fp, 8192);
2305 fpos_t eight_k_pos;
2306 ASSERT_EQ(0, fgetpos(fp, &eight_k_pos));
2307
2308 // Seek forward another 8192...
2309 ASSERT_EQ(0, fseek(fp, 8192, SEEK_CUR));
2310 AssertFileOffsetAt(fp, 8192 + 8192);
2311 fpos64_t sixteen_k_pos64;
2312 ASSERT_EQ(0, fgetpos64(fp, &sixteen_k_pos64));
2313
2314 // Seek back 8192...
2315 ASSERT_EQ(0, fseek(fp, -8192, SEEK_CUR));
2316 AssertFileOffsetAt(fp, 8192);
2317
2318 // Since we haven't written anything, the end is also at 0.
2319 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
2320 AssertFileOffsetAt(fp, 0);
2321
2322 // Check that our fpos64_t from 16KiB works...
2323 ASSERT_EQ(0, fsetpos64(fp, &sixteen_k_pos64));
2324 AssertFileOffsetAt(fp, 8192 + 8192);
2325 // ...as does our fpos_t from 8192.
2326 ASSERT_EQ(0, fsetpos(fp, &eight_k_pos));
2327 AssertFileOffsetAt(fp, 8192);
2328
2329 // Do fseeko and fseeko64 work too?
2330 ASSERT_EQ(0, fseeko(fp, 1234, SEEK_SET));
2331 AssertFileOffsetAt(fp, 1234);
2332 ASSERT_EQ(0, fseeko64(fp, 5678, SEEK_SET));
2333 AssertFileOffsetAt(fp, 5678);
2334
2335 fclose(fp);
2336}
2337
2338TEST(STDIO_TEST, fseek_fseeko_EINVAL) {
2339 TemporaryFile tf;
2340 FILE* fp = fdopen(tf.fd, "w+");
2341
2342 // Bad whence.
2343 errno = 0;
2344 ASSERT_EQ(-1, fseek(fp, 0, 123));
2345 ASSERT_EQ(EINVAL, errno);
2346 errno = 0;
2347 ASSERT_EQ(-1, fseeko(fp, 0, 123));
2348 ASSERT_EQ(EINVAL, errno);
2349 errno = 0;
2350 ASSERT_EQ(-1, fseeko64(fp, 0, 123));
2351 ASSERT_EQ(EINVAL, errno);
2352
2353 // Bad offset.
2354 errno = 0;
2355 ASSERT_EQ(-1, fseek(fp, -1, SEEK_SET));
2356 ASSERT_EQ(EINVAL, errno);
2357 errno = 0;
2358 ASSERT_EQ(-1, fseeko(fp, -1, SEEK_SET));
2359 ASSERT_EQ(EINVAL, errno);
2360 errno = 0;
2361 ASSERT_EQ(-1, fseeko64(fp, -1, SEEK_SET));
2362 ASSERT_EQ(EINVAL, errno);
2363
2364 fclose(fp);
2365}
Elliott Hughes20788ae2016-06-09 15:16:32 -07002366
2367TEST(STDIO_TEST, ctermid) {
2368 ASSERT_STREQ("/dev/tty", ctermid(nullptr));
2369
2370 char buf[L_ctermid] = {};
2371 ASSERT_EQ(buf, ctermid(buf));
2372 ASSERT_STREQ("/dev/tty", buf);
2373}
Elliott Hughesd1f25a72016-08-05 15:53:03 -07002374
2375TEST(STDIO_TEST, remove) {
2376 struct stat sb;
2377
2378 TemporaryFile tf;
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -08002379 ASSERT_EQ(0, remove(tf.path));
2380 ASSERT_EQ(-1, lstat(tf.path, &sb));
Elliott Hughesd1f25a72016-08-05 15:53:03 -07002381 ASSERT_EQ(ENOENT, errno);
2382
2383 TemporaryDir td;
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -08002384 ASSERT_EQ(0, remove(td.path));
2385 ASSERT_EQ(-1, lstat(td.path, &sb));
Elliott Hughesd1f25a72016-08-05 15:53:03 -07002386 ASSERT_EQ(ENOENT, errno);
2387
2388 errno = 0;
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -08002389 ASSERT_EQ(-1, remove(tf.path));
Elliott Hughesd1f25a72016-08-05 15:53:03 -07002390 ASSERT_EQ(ENOENT, errno);
2391
2392 errno = 0;
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -08002393 ASSERT_EQ(-1, remove(td.path));
Elliott Hughesd1f25a72016-08-05 15:53:03 -07002394 ASSERT_EQ(ENOENT, errno);
2395}
Elliott Hughesfb3873d2016-08-10 11:07:54 -07002396
2397TEST(STDIO_DEATHTEST, snprintf_30445072_known_buffer_size) {
2398 char buf[16];
2399 ASSERT_EXIT(snprintf(buf, atol("-1"), "hello"),
2400 testing::KilledBySignal(SIGABRT),
2401#if defined(NOFORTIFY)
2402 "FORTIFY: vsnprintf: size .* > SSIZE_MAX"
2403#else
2404 "FORTIFY: vsnprintf: prevented .*-byte write into 16-byte buffer"
2405#endif
2406 );
2407}
2408
2409TEST(STDIO_DEATHTEST, snprintf_30445072_unknown_buffer_size) {
2410 std::string buf = "world";
2411 ASSERT_EXIT(snprintf(&buf[0], atol("-1"), "hello"),
2412 testing::KilledBySignal(SIGABRT),
2413 "FORTIFY: vsnprintf: size .* > SSIZE_MAX");
2414}
2415
2416TEST(STDIO_TEST, sprintf_30445072) {
2417 std::string buf = "world";
2418 sprintf(&buf[0], "hello");
2419 ASSERT_EQ(buf, "hello");
2420}
Elliott Hughes33a8cb12017-07-25 18:06:46 -07002421
Elliott Hughes654cd832018-08-30 16:00:42 -07002422TEST(STDIO_TEST, printf_m) {
2423 char buf[BUFSIZ];
2424 errno = 0;
2425 snprintf(buf, sizeof(buf), "<%m>");
2426 ASSERT_STREQ("<Success>", buf);
2427 errno = -1;
2428 snprintf(buf, sizeof(buf), "<%m>");
2429 ASSERT_STREQ("<Unknown error -1>", buf);
2430 errno = EINVAL;
2431 snprintf(buf, sizeof(buf), "<%m>");
2432 ASSERT_STREQ("<Invalid argument>", buf);
2433}
2434
Elliott Hughesf340a562018-09-06 10:42:40 -07002435TEST(STDIO_TEST, printf_m_does_not_clobber_strerror) {
2436 char buf[BUFSIZ];
2437 const char* m = strerror(-1);
2438 ASSERT_STREQ("Unknown error -1", m);
2439 errno = -2;
2440 snprintf(buf, sizeof(buf), "<%m>");
2441 ASSERT_STREQ("<Unknown error -2>", buf);
2442 ASSERT_STREQ("Unknown error -1", m);
2443}
2444
Elliott Hughes654cd832018-08-30 16:00:42 -07002445TEST(STDIO_TEST, wprintf_m) {
2446 wchar_t buf[BUFSIZ];
2447 errno = 0;
2448 swprintf(buf, sizeof(buf), L"<%m>");
2449 ASSERT_EQ(std::wstring(L"<Success>"), buf);
2450 errno = -1;
2451 swprintf(buf, sizeof(buf), L"<%m>");
2452 ASSERT_EQ(std::wstring(L"<Unknown error -1>"), buf);
2453 errno = EINVAL;
2454 swprintf(buf, sizeof(buf), L"<%m>");
2455 ASSERT_EQ(std::wstring(L"<Invalid argument>"), buf);
2456}
2457
Elliott Hughesf340a562018-09-06 10:42:40 -07002458TEST(STDIO_TEST, wprintf_m_does_not_clobber_strerror) {
2459 wchar_t buf[BUFSIZ];
2460 const char* m = strerror(-1);
2461 ASSERT_STREQ("Unknown error -1", m);
2462 errno = -2;
2463 swprintf(buf, sizeof(buf), L"<%m>");
2464 ASSERT_EQ(std::wstring(L"<Unknown error -2>"), buf);
2465 ASSERT_STREQ("Unknown error -1", m);
2466}
2467
Elliott Hughes33a8cb12017-07-25 18:06:46 -07002468TEST(STDIO_TEST, fopen_append_mode_and_ftell) {
2469 TemporaryFile tf;
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -08002470 SetFileTo(tf.path, "0123456789");
2471 FILE* fp = fopen(tf.path, "a");
Elliott Hughes33a8cb12017-07-25 18:06:46 -07002472 EXPECT_EQ(10, ftell(fp));
2473 ASSERT_EQ(0, fseek(fp, 2, SEEK_SET));
2474 EXPECT_EQ(2, ftell(fp));
2475 ASSERT_NE(EOF, fputs("xxx", fp));
2476 ASSERT_EQ(0, fflush(fp));
2477 EXPECT_EQ(13, ftell(fp));
2478 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
2479 EXPECT_EQ(13, ftell(fp));
2480 ASSERT_EQ(0, fclose(fp));
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -08002481 AssertFileIs(tf.path, "0123456789xxx");
Elliott Hughes33a8cb12017-07-25 18:06:46 -07002482}
2483
2484TEST(STDIO_TEST, fdopen_append_mode_and_ftell) {
2485 TemporaryFile tf;
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -08002486 SetFileTo(tf.path, "0123456789");
2487 int fd = open(tf.path, O_RDWR);
Elliott Hughes33a8cb12017-07-25 18:06:46 -07002488 ASSERT_NE(-1, fd);
2489 // POSIX: "The file position indicator associated with the new stream is set to the position
2490 // indicated by the file offset associated with the file descriptor."
2491 ASSERT_EQ(4, lseek(fd, 4, SEEK_SET));
2492 FILE* fp = fdopen(fd, "a");
2493 EXPECT_EQ(4, ftell(fp));
2494 ASSERT_EQ(0, fseek(fp, 2, SEEK_SET));
2495 EXPECT_EQ(2, ftell(fp));
2496 ASSERT_NE(EOF, fputs("xxx", fp));
2497 ASSERT_EQ(0, fflush(fp));
2498 EXPECT_EQ(13, ftell(fp));
2499 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
2500 EXPECT_EQ(13, ftell(fp));
2501 ASSERT_EQ(0, fclose(fp));
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -08002502 AssertFileIs(tf.path, "0123456789xxx");
Elliott Hughes33a8cb12017-07-25 18:06:46 -07002503}
2504
2505TEST(STDIO_TEST, freopen_append_mode_and_ftell) {
2506 TemporaryFile tf;
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -08002507 SetFileTo(tf.path, "0123456789");
Elliott Hughes33a8cb12017-07-25 18:06:46 -07002508 FILE* other_fp = fopen("/proc/version", "r");
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -08002509 FILE* fp = freopen(tf.path, "a", other_fp);
Elliott Hughes33a8cb12017-07-25 18:06:46 -07002510 EXPECT_EQ(10, ftell(fp));
2511 ASSERT_EQ(0, fseek(fp, 2, SEEK_SET));
2512 EXPECT_EQ(2, ftell(fp));
2513 ASSERT_NE(EOF, fputs("xxx", fp));
2514 ASSERT_EQ(0, fflush(fp));
2515 EXPECT_EQ(13, ftell(fp));
2516 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
2517 EXPECT_EQ(13, ftell(fp));
2518 ASSERT_EQ(0, fclose(fp));
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -08002519 AssertFileIs(tf.path, "0123456789xxx");
Elliott Hughes33a8cb12017-07-25 18:06:46 -07002520}
Elliott Hughesb15feb72017-07-31 17:20:18 -07002521
2522TEST(STDIO_TEST, constants) {
2523 ASSERT_LE(FILENAME_MAX, PATH_MAX);
2524 ASSERT_EQ(L_tmpnam, PATH_MAX);
2525}
Elliott Hughes37ad9592017-10-30 17:47:12 -07002526
2527TEST(STDIO_TEST, perror) {
2528 ExecTestHelper eth;
2529 eth.Run([&]() { errno = EINVAL; perror("a b c"); exit(0); }, 0, "a b c: Invalid argument\n");
2530 eth.Run([&]() { errno = EINVAL; perror(nullptr); exit(0); }, 0, "Invalid argument\n");
2531 eth.Run([&]() { errno = EINVAL; perror(""); exit(0); }, 0, "Invalid argument\n");
2532}
2533
2534TEST(STDIO_TEST, puts) {
2535 ExecTestHelper eth;
2536 eth.Run([&]() { exit(puts("a b c")); }, 0, "a b c\n");
2537}
2538
Elliott Hughes7cebf832020-08-12 14:25:41 -07002539TEST(STDIO_TEST, putchar) {
2540 ExecTestHelper eth;
2541 eth.Run([&]() { exit(putchar('A')); }, 65, "A");
2542}
2543
2544TEST(STDIO_TEST, putchar_unlocked) {
2545 ExecTestHelper eth;
2546 eth.Run([&]() { exit(putchar('B')); }, 66, "B");
2547}
2548
Elliott Hughes37ad9592017-10-30 17:47:12 -07002549TEST(STDIO_TEST, unlocked) {
2550 TemporaryFile tf;
2551
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -08002552 FILE* fp = fopen(tf.path, "w+");
Elliott Hughes37ad9592017-10-30 17:47:12 -07002553 ASSERT_TRUE(fp != nullptr);
2554
2555 clearerr_unlocked(fp);
2556 ASSERT_FALSE(feof_unlocked(fp));
2557 ASSERT_FALSE(ferror_unlocked(fp));
2558
2559 ASSERT_EQ(fileno(fp), fileno_unlocked(fp));
2560
2561 ASSERT_NE(EOF, putc_unlocked('a', fp));
2562 ASSERT_NE(EOF, putc('b', fp));
2563 ASSERT_NE(EOF, fputc_unlocked('c', fp));
2564 ASSERT_NE(EOF, fputc('d', fp));
2565
2566 rewind(fp);
2567 ASSERT_EQ('a', getc_unlocked(fp));
2568 ASSERT_EQ('b', getc(fp));
2569 ASSERT_EQ('c', fgetc_unlocked(fp));
2570 ASSERT_EQ('d', fgetc(fp));
2571
2572 rewind(fp);
2573 ASSERT_EQ(2U, fwrite_unlocked("AB", 1, 2, fp));
2574 ASSERT_EQ(2U, fwrite("CD", 1, 2, fp));
2575 ASSERT_EQ(0, fflush_unlocked(fp));
2576
2577 rewind(fp);
2578 char buf[BUFSIZ] = {};
2579 ASSERT_EQ(2U, fread_unlocked(&buf[0], 1, 2, fp));
2580 ASSERT_EQ(2U, fread(&buf[2], 1, 2, fp));
2581 ASSERT_STREQ("ABCD", buf);
2582
2583 rewind(fp);
2584 ASSERT_NE(EOF, fputs("hello ", fp));
2585 ASSERT_NE(EOF, fputs_unlocked("world", fp));
2586 ASSERT_NE(EOF, fputc('\n', fp));
2587
2588 rewind(fp);
2589 ASSERT_TRUE(fgets_unlocked(buf, sizeof(buf), fp) != nullptr);
2590 ASSERT_STREQ("hello world\n", buf);
2591
2592 ASSERT_EQ(0, fclose(fp));
2593}
Ryan Prichardbf549862017-11-07 15:30:32 -08002594
2595TEST(STDIO_TEST, fseek_64bit) {
2596 TemporaryFile tf;
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -08002597 FILE* fp = fopen64(tf.path, "w+");
Ryan Prichardbf549862017-11-07 15:30:32 -08002598 ASSERT_TRUE(fp != nullptr);
2599 ASSERT_EQ(0, fseeko64(fp, 0x2'0000'0000, SEEK_SET));
2600 ASSERT_EQ(0x2'0000'0000, ftello64(fp));
2601 ASSERT_EQ(0, fseeko64(fp, 0x1'0000'0000, SEEK_CUR));
2602 ASSERT_EQ(0x3'0000'0000, ftello64(fp));
2603 ASSERT_EQ(0, fclose(fp));
2604}
2605
2606// POSIX requires that fseek/fseeko fail with EOVERFLOW if the new file offset
2607// isn't representable in long/off_t.
2608TEST(STDIO_TEST, fseek_overflow_32bit) {
2609 TemporaryFile tf;
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -08002610 FILE* fp = fopen64(tf.path, "w+");
Ryan Prichardbf549862017-11-07 15:30:32 -08002611 ASSERT_EQ(0, ftruncate64(fileno(fp), 0x2'0000'0000));
2612
2613 // Bionic implements overflow checking for SEEK_CUR, but glibc doesn't.
2614#if defined(__BIONIC__) && !defined(__LP64__)
2615 ASSERT_EQ(0, fseek(fp, 0x7fff'ffff, SEEK_SET));
2616 ASSERT_EQ(-1, fseek(fp, 1, SEEK_CUR));
2617 ASSERT_EQ(EOVERFLOW, errno);
2618#endif
2619
2620 // Neither Bionic nor glibc implement the overflow checking for SEEK_END.
2621 // (Aside: FreeBSD's libc is an example of a libc that checks both SEEK_CUR
2622 // and SEEK_END -- many C libraries check neither.)
2623 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
2624 ASSERT_EQ(0x2'0000'0000, ftello64(fp));
2625
2626 fclose(fp);
2627}
Elliott Hughesf1a38382018-08-22 15:38:07 -07002628
2629TEST(STDIO_TEST, dev_std_files) {
2630 // POSIX only mentions /dev/stdout, but we should have all three (http://b/31824379).
2631 char path[PATH_MAX];
Christopher Ferris2c4ec7e2018-08-23 11:17:55 -07002632 ssize_t length = readlink("/dev/stdin", path, sizeof(path));
2633 ASSERT_LT(0, length);
2634 ASSERT_EQ("/proc/self/fd/0", std::string(path, length));
2635
2636 length = readlink("/dev/stdout", path, sizeof(path));
2637 ASSERT_LT(0, length);
2638 ASSERT_EQ("/proc/self/fd/1", std::string(path, length));
2639
2640 length = readlink("/dev/stderr", path, sizeof(path));
2641 ASSERT_LT(0, length);
2642 ASSERT_EQ("/proc/self/fd/2", std::string(path, length));
Elliott Hughesf1a38382018-08-22 15:38:07 -07002643}
Ryan Prichardc485cdb2019-04-30 14:47:34 -07002644
2645TEST(STDIO_TEST, fread_with_locked_file) {
2646 // Reading an unbuffered/line-buffered file from one thread shouldn't block on
2647 // files locked on other threads, even if it flushes some line-buffered files.
2648 FILE* fp1 = fopen("/dev/zero", "r");
2649 ASSERT_TRUE(fp1 != nullptr);
2650 flockfile(fp1);
2651
2652 std::thread([] {
2653 for (int mode : { _IONBF, _IOLBF }) {
2654 FILE* fp2 = fopen("/dev/zero", "r");
2655 ASSERT_TRUE(fp2 != nullptr);
2656 setvbuf(fp2, nullptr, mode, 0);
2657 ASSERT_EQ('\0', fgetc(fp2));
2658 fclose(fp2);
2659 }
2660 }).join();
2661
2662 funlockfile(fp1);
2663 fclose(fp1);
2664}
Elliott Hughes31c73092019-05-07 10:03:02 -07002665
2666TEST(STDIO_TEST, SEEK_macros) {
2667 ASSERT_EQ(0, SEEK_SET);
2668 ASSERT_EQ(1, SEEK_CUR);
2669 ASSERT_EQ(2, SEEK_END);
2670 ASSERT_EQ(3, SEEK_DATA);
2671 ASSERT_EQ(4, SEEK_HOLE);
2672 // So we'll notice if Linux grows another constant in <linux/fs.h>...
2673 ASSERT_EQ(SEEK_MAX, SEEK_HOLE);
2674}
Elliott Hughes05b675e2019-04-17 13:01:06 -07002675
2676TEST(STDIO_TEST, rename) {
2677 TemporaryDir td;
2678 std::string old_path = td.path + "/old"s;
2679 std::string new_path = td.path + "/new"s;
2680
2681 // Create the file, check it exists.
2682 ASSERT_EQ(0, close(creat(old_path.c_str(), 0666)));
2683 struct stat sb;
2684 ASSERT_EQ(0, stat(old_path.c_str(), &sb));
2685 ASSERT_EQ(-1, stat(new_path.c_str(), &sb));
2686
2687 // Rename and check it moved.
2688 ASSERT_EQ(0, rename(old_path.c_str(), new_path.c_str()));
2689 ASSERT_EQ(-1, stat(old_path.c_str(), &sb));
2690 ASSERT_EQ(0, stat(new_path.c_str(), &sb));
2691}
2692
2693TEST(STDIO_TEST, renameat) {
2694 TemporaryDir td;
2695 android::base::unique_fd dirfd{open(td.path, O_PATH)};
2696 std::string old_path = td.path + "/old"s;
2697 std::string new_path = td.path + "/new"s;
2698
2699 // Create the file, check it exists.
2700 ASSERT_EQ(0, close(creat(old_path.c_str(), 0666)));
2701 struct stat sb;
2702 ASSERT_EQ(0, stat(old_path.c_str(), &sb));
2703 ASSERT_EQ(-1, stat(new_path.c_str(), &sb));
2704
2705 // Rename and check it moved.
2706 ASSERT_EQ(0, renameat(dirfd, "old", dirfd, "new"));
2707 ASSERT_EQ(-1, stat(old_path.c_str(), &sb));
2708 ASSERT_EQ(0, stat(new_path.c_str(), &sb));
2709}
2710
2711TEST(STDIO_TEST, renameat2) {
2712#if defined(__GLIBC__)
2713 GTEST_SKIP() << "glibc doesn't have renameat2 until 2.28";
2714#else
2715 TemporaryDir td;
2716 android::base::unique_fd dirfd{open(td.path, O_PATH)};
2717 std::string old_path = td.path + "/old"s;
2718 std::string new_path = td.path + "/new"s;
2719
2720 // Create the file, check it exists.
2721 ASSERT_EQ(0, close(creat(old_path.c_str(), 0666)));
2722 struct stat sb;
2723 ASSERT_EQ(0, stat(old_path.c_str(), &sb));
2724 ASSERT_EQ(-1, stat(new_path.c_str(), &sb));
2725
2726 // Rename and check it moved.
2727 ASSERT_EQ(0, renameat2(dirfd, "old", dirfd, "new", 0));
2728 ASSERT_EQ(-1, stat(old_path.c_str(), &sb));
2729 ASSERT_EQ(0, stat(new_path.c_str(), &sb));
2730
2731 // After this, both "old" and "new" exist.
2732 ASSERT_EQ(0, close(creat(old_path.c_str(), 0666)));
2733
2734 // Rename and check it moved.
2735 ASSERT_EQ(-1, renameat2(dirfd, "old", dirfd, "new", RENAME_NOREPLACE));
2736 ASSERT_EQ(EEXIST, errno);
2737#endif
2738}
2739
2740TEST(STDIO_TEST, renameat2_flags) {
2741#if defined(__GLIBC__)
2742 GTEST_SKIP() << "glibc doesn't have renameat2 until 2.28";
2743#else
2744 ASSERT_NE(0, RENAME_EXCHANGE);
2745 ASSERT_NE(0, RENAME_NOREPLACE);
2746 ASSERT_NE(0, RENAME_WHITEOUT);
2747#endif
2748}
Elliott Hughes7cebf832020-08-12 14:25:41 -07002749
2750TEST(STDIO_TEST, fdopen_failures) {
2751 FILE* fp;
2752 int fd = open("/proc/version", O_RDONLY);
2753 ASSERT_TRUE(fd != -1);
2754
2755 // Nonsense mode.
2756 errno = 0;
2757 fp = fdopen(fd, "nonsense");
2758 ASSERT_TRUE(fp == nullptr);
2759 ASSERT_EQ(EINVAL, errno);
2760
2761 // Mode that isn't a subset of the fd's actual mode.
2762 errno = 0;
2763 fp = fdopen(fd, "w");
2764 ASSERT_TRUE(fp == nullptr);
2765 ASSERT_EQ(EINVAL, errno);
2766
2767 // Can't set append on the underlying fd.
2768 errno = 0;
2769 fp = fdopen(fd, "a");
2770 ASSERT_TRUE(fp == nullptr);
2771 ASSERT_EQ(EINVAL, errno);
2772
2773 // Bad fd.
2774 errno = 0;
2775 fp = fdopen(-1, "re");
2776 ASSERT_TRUE(fp == nullptr);
2777 ASSERT_EQ(EBADF, errno);
2778
2779 close(fd);
2780}
2781
2782TEST(STDIO_TEST, fmemopen_invalid_mode) {
2783 errno = 0;
2784 FILE* fp = fmemopen(nullptr, 16, "nonsense");
2785 ASSERT_TRUE(fp == nullptr);
2786 ASSERT_EQ(EINVAL, errno);
2787}
2788
2789TEST(STDIO_TEST, fopen_invalid_mode) {
2790 errno = 0;
2791 FILE* fp = fopen("/proc/version", "nonsense");
2792 ASSERT_TRUE(fp == nullptr);
2793 ASSERT_EQ(EINVAL, errno);
2794}
2795
2796TEST(STDIO_TEST, freopen_invalid_mode) {
2797 FILE* fp = fopen("/proc/version", "re");
2798 ASSERT_TRUE(fp != nullptr);
2799
2800 errno = 0;
2801 fp = freopen("/proc/version", "nonsense", fp);
2802 ASSERT_TRUE(fp == nullptr);
2803 ASSERT_EQ(EINVAL, errno);
2804}
2805
2806TEST(STDIO_TEST, asprintf_smoke) {
2807 char* p = nullptr;
2808 ASSERT_EQ(11, asprintf(&p, "hello %s", "world"));
2809 ASSERT_STREQ("hello world", p);
2810 free(p);
2811}
2812
2813TEST(STDIO_TEST, fopen_ENOENT) {
2814 errno = 0;
2815 FILE* fp = fopen("/proc/does-not-exist", "re");
2816 ASSERT_TRUE(fp == nullptr);
2817 ASSERT_EQ(ENOENT, errno);
2818}