blob: a0cda1be0b19a42b7efe0da48cb58d26f5630419 [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
Christopher Ferris13f26a72016-01-13 13:47:58 -0800297TEST(STDIO_TEST, snprintf_lc) {
Elliott Hughes05493712014-04-17 17:30:03 -0700298 char buf[BUFSIZ];
299 wint_t wc = L'a';
300 EXPECT_EQ(3, snprintf(buf, sizeof(buf), "<%lc>", wc));
301 EXPECT_STREQ("<a>", buf);
302}
303
Elliott Hughes2f9c8ce2017-11-01 13:54:47 -0700304TEST(STDIO_TEST, snprintf_C) { // Synonym for %lc.
305 char buf[BUFSIZ];
306 wchar_t wc = L'a';
307 EXPECT_EQ(3, snprintf(buf, sizeof(buf), "<%C>", wc));
308 EXPECT_STREQ("<a>", buf);
309}
310
Christopher Ferris13f26a72016-01-13 13:47:58 -0800311TEST(STDIO_TEST, snprintf_ls) {
Elliott Hughes05493712014-04-17 17:30:03 -0700312 char buf[BUFSIZ];
Yi Kong32bc0fc2018-08-02 17:31:13 -0700313 wchar_t* ws = nullptr;
Elliott Hughes05493712014-04-17 17:30:03 -0700314 EXPECT_EQ(8, snprintf(buf, sizeof(buf), "<%ls>", ws));
315 EXPECT_STREQ("<(null)>", buf);
316
317 wchar_t chars[] = { L'h', L'i', 0 };
318 ws = chars;
319 EXPECT_EQ(4, snprintf(buf, sizeof(buf), "<%ls>", ws));
320 EXPECT_STREQ("<hi>", buf);
321}
322
Elliott Hughes2f9c8ce2017-11-01 13:54:47 -0700323TEST(STDIO_TEST, snprintf_S) { // Synonym for %ls.
324 char buf[BUFSIZ];
Yi Kong32bc0fc2018-08-02 17:31:13 -0700325 wchar_t* ws = nullptr;
Elliott Hughes2f9c8ce2017-11-01 13:54:47 -0700326 EXPECT_EQ(8, snprintf(buf, sizeof(buf), "<%S>", ws));
327 EXPECT_STREQ("<(null)>", buf);
328
329 wchar_t chars[] = { L'h', L'i', 0 };
330 ws = chars;
331 EXPECT_EQ(4, snprintf(buf, sizeof(buf), "<%S>", ws));
332 EXPECT_STREQ("<hi>", buf);
333}
334
Christopher Ferris13f26a72016-01-13 13:47:58 -0800335TEST(STDIO_TEST, snprintf_n) {
Elliott Hughes063525c2014-05-13 11:19:57 -0700336#if defined(__BIONIC__)
Elliott Hughes41398d02018-03-07 13:32:58 -0800337 // http://b/14492135 and http://b/31832608.
Elliott Hughes7248a2d2013-09-24 18:01:33 -0700338 char buf[32];
Elliott Hughese2341d02014-05-02 18:16:32 -0700339 int i = 1234;
Elliott Hughes41398d02018-03-07 13:32:58 -0800340 EXPECT_DEATH(snprintf(buf, sizeof(buf), "a %n b", &i), "%n not allowed on Android");
Elliott Hughese2341d02014-05-02 18:16:32 -0700341#else
Elliott Hughesbcaa4542019-03-08 15:20:23 -0800342 GTEST_SKIP() << "glibc does allow %n";
Elliott Hughese2341d02014-05-02 18:16:32 -0700343#endif
Elliott Hughes7248a2d2013-09-24 18:01:33 -0700344}
Elliott Hughes7248a2d2013-09-24 18:01:33 -0700345
Christopher Ferris13f26a72016-01-13 13:47:58 -0800346TEST(STDIO_TEST, snprintf_smoke) {
Elliott Hughes1d13c642013-09-23 16:02:39 -0700347 char buf[BUFSIZ];
348
349 snprintf(buf, sizeof(buf), "a");
350 EXPECT_STREQ("a", buf);
351
352 snprintf(buf, sizeof(buf), "%%");
353 EXPECT_STREQ("%", buf);
354
355 snprintf(buf, sizeof(buf), "01234");
356 EXPECT_STREQ("01234", buf);
357
358 snprintf(buf, sizeof(buf), "a%sb", "01234");
359 EXPECT_STREQ("a01234b", buf);
360
Yi Kong32bc0fc2018-08-02 17:31:13 -0700361 char* s = nullptr;
Elliott Hughes1d13c642013-09-23 16:02:39 -0700362 snprintf(buf, sizeof(buf), "a%sb", s);
363 EXPECT_STREQ("a(null)b", buf);
364
365 snprintf(buf, sizeof(buf), "aa%scc", "bb");
366 EXPECT_STREQ("aabbcc", buf);
367
368 snprintf(buf, sizeof(buf), "a%cc", 'b');
369 EXPECT_STREQ("abc", buf);
370
371 snprintf(buf, sizeof(buf), "a%db", 1234);
372 EXPECT_STREQ("a1234b", buf);
373
374 snprintf(buf, sizeof(buf), "a%db", -8123);
375 EXPECT_STREQ("a-8123b", buf);
376
Stephen Hines6c7b3cb2013-10-11 16:03:21 -0700377 snprintf(buf, sizeof(buf), "a%hdb", static_cast<short>(0x7fff0010));
Elliott Hughes1d13c642013-09-23 16:02:39 -0700378 EXPECT_STREQ("a16b", buf);
379
Stephen Hines6c7b3cb2013-10-11 16:03:21 -0700380 snprintf(buf, sizeof(buf), "a%hhdb", static_cast<char>(0x7fffff10));
Elliott Hughes1d13c642013-09-23 16:02:39 -0700381 EXPECT_STREQ("a16b", buf);
382
383 snprintf(buf, sizeof(buf), "a%lldb", 0x1000000000LL);
384 EXPECT_STREQ("a68719476736b", buf);
385
386 snprintf(buf, sizeof(buf), "a%ldb", 70000L);
387 EXPECT_STREQ("a70000b", buf);
388
389 snprintf(buf, sizeof(buf), "a%pb", reinterpret_cast<void*>(0xb0001234));
390 EXPECT_STREQ("a0xb0001234b", buf);
391
392 snprintf(buf, sizeof(buf), "a%xz", 0x12ab);
393 EXPECT_STREQ("a12abz", buf);
394
395 snprintf(buf, sizeof(buf), "a%Xz", 0x12ab);
396 EXPECT_STREQ("a12ABz", buf);
397
398 snprintf(buf, sizeof(buf), "a%08xz", 0x123456);
399 EXPECT_STREQ("a00123456z", buf);
400
401 snprintf(buf, sizeof(buf), "a%5dz", 1234);
402 EXPECT_STREQ("a 1234z", buf);
403
404 snprintf(buf, sizeof(buf), "a%05dz", 1234);
405 EXPECT_STREQ("a01234z", buf);
406
407 snprintf(buf, sizeof(buf), "a%8dz", 1234);
408 EXPECT_STREQ("a 1234z", buf);
409
410 snprintf(buf, sizeof(buf), "a%-8dz", 1234);
411 EXPECT_STREQ("a1234 z", buf);
412
413 snprintf(buf, sizeof(buf), "A%-11sZ", "abcdef");
414 EXPECT_STREQ("Aabcdef Z", buf);
415
416 snprintf(buf, sizeof(buf), "A%s:%dZ", "hello", 1234);
417 EXPECT_STREQ("Ahello:1234Z", buf);
418
419 snprintf(buf, sizeof(buf), "a%03d:%d:%02dz", 5, 5, 5);
420 EXPECT_STREQ("a005:5:05z", buf);
421
Yi Kong32bc0fc2018-08-02 17:31:13 -0700422 void* p = nullptr;
Elliott Hughes1d13c642013-09-23 16:02:39 -0700423 snprintf(buf, sizeof(buf), "a%d,%pz", 5, p);
Christopher Ferris13613132013-10-28 15:24:04 -0700424#if defined(__BIONIC__)
Elliott Hughes1d13c642013-09-23 16:02:39 -0700425 EXPECT_STREQ("a5,0x0z", buf);
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800426#else // __BIONIC__
Christopher Ferris13613132013-10-28 15:24:04 -0700427 EXPECT_STREQ("a5,(nil)z", buf);
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800428#endif // __BIONIC__
Elliott Hughes1d13c642013-09-23 16:02:39 -0700429
430 snprintf(buf, sizeof(buf), "a%lld,%d,%d,%dz", 0x1000000000LL, 6, 7, 8);
431 EXPECT_STREQ("a68719476736,6,7,8z", buf);
432
433 snprintf(buf, sizeof(buf), "a_%f_b", 1.23f);
434 EXPECT_STREQ("a_1.230000_b", buf);
435
Stephen Hines6c7b3cb2013-10-11 16:03:21 -0700436 snprintf(buf, sizeof(buf), "a_%g_b", 3.14);
Elliott Hughes1d13c642013-09-23 16:02:39 -0700437 EXPECT_STREQ("a_3.14_b", buf);
Alexander Ivchenkoedd7c2e2014-04-01 17:01:39 +0400438
439 snprintf(buf, sizeof(buf), "%1$s %1$s", "print_me_twice");
440 EXPECT_STREQ("print_me_twice print_me_twice", buf);
Elliott Hughes1d13c642013-09-23 16:02:39 -0700441}
442
Elliott Hughes1b18aff2014-12-16 14:45:32 -0800443template <typename T>
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700444static void CheckInfNan(int snprintf_fn(T*, size_t, const T*, ...),
445 int sscanf_fn(const T*, const T*, ...),
446 const T* fmt_string, const T* fmt, const T* fmt_plus,
447 const T* minus_inf, const T* inf_, const T* plus_inf,
448 const T* minus_nan, const T* nan_, const T* plus_nan) {
Elliott Hughes1b18aff2014-12-16 14:45:32 -0800449 T buf[BUFSIZ];
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700450 float f;
Elliott Hughes7823f322014-04-14 12:11:28 -0700451
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700452 // NaN.
453
454 snprintf_fn(buf, sizeof(buf), fmt, nanf(""));
Elliott Hughes1b18aff2014-12-16 14:45:32 -0800455 EXPECT_STREQ(nan_, buf) << fmt;
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700456 EXPECT_EQ(1, sscanf_fn(buf, fmt, &f));
457 EXPECT_TRUE(isnan(f));
458
459 snprintf_fn(buf, sizeof(buf), fmt, -nanf(""));
Elliott Hughes1b18aff2014-12-16 14:45:32 -0800460 EXPECT_STREQ(minus_nan, buf) << fmt;
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700461 EXPECT_EQ(1, sscanf_fn(buf, fmt, &f));
462 EXPECT_TRUE(isnan(f));
463
464 snprintf_fn(buf, sizeof(buf), fmt_plus, nanf(""));
Elliott Hughes1b18aff2014-12-16 14:45:32 -0800465 EXPECT_STREQ(plus_nan, buf) << fmt_plus;
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700466 EXPECT_EQ(1, sscanf_fn(buf, fmt, &f));
467 EXPECT_TRUE(isnan(f));
468
469 snprintf_fn(buf, sizeof(buf), fmt_plus, -nanf(""));
Elliott Hughes1b18aff2014-12-16 14:45:32 -0800470 EXPECT_STREQ(minus_nan, buf) << fmt_plus;
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700471 EXPECT_EQ(1, sscanf_fn(buf, fmt, &f));
472 EXPECT_TRUE(isnan(f));
Elliott Hughes1b18aff2014-12-16 14:45:32 -0800473
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700474 // Inf.
475
476 snprintf_fn(buf, sizeof(buf), fmt, HUGE_VALF);
Elliott Hughes1b18aff2014-12-16 14:45:32 -0800477 EXPECT_STREQ(inf_, buf) << fmt;
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700478 EXPECT_EQ(1, sscanf_fn(buf, fmt, &f));
479 EXPECT_EQ(HUGE_VALF, f);
480
481 snprintf_fn(buf, sizeof(buf), fmt, -HUGE_VALF);
Elliott Hughes1b18aff2014-12-16 14:45:32 -0800482 EXPECT_STREQ(minus_inf, buf) << fmt;
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700483 EXPECT_EQ(1, sscanf_fn(buf, fmt, &f));
484 EXPECT_EQ(-HUGE_VALF, f);
485
486 snprintf_fn(buf, sizeof(buf), fmt_plus, HUGE_VALF);
Elliott Hughes1b18aff2014-12-16 14:45:32 -0800487 EXPECT_STREQ(plus_inf, buf) << fmt_plus;
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700488 EXPECT_EQ(1, sscanf_fn(buf, fmt, &f));
489 EXPECT_EQ(HUGE_VALF, f);
490
491 snprintf_fn(buf, sizeof(buf), fmt_plus, -HUGE_VALF);
Elliott Hughes1b18aff2014-12-16 14:45:32 -0800492 EXPECT_STREQ(minus_inf, buf) << fmt_plus;
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700493 EXPECT_EQ(1, sscanf_fn(buf, fmt, &f));
494 EXPECT_EQ(-HUGE_VALF, f);
495
496 // Check case-insensitivity.
497 snprintf_fn(buf, sizeof(buf), fmt_string, "[InFiNiTy]");
498 EXPECT_EQ(1, sscanf_fn(buf, fmt, &f)) << buf;
499 EXPECT_EQ(HUGE_VALF, f);
500 snprintf_fn(buf, sizeof(buf), fmt_string, "[NaN]");
501 EXPECT_EQ(1, sscanf_fn(buf, fmt, &f)) << buf;
502 EXPECT_TRUE(isnan(f));
Elliott Hughes7823f322014-04-14 12:11:28 -0700503}
504
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700505TEST(STDIO_TEST, snprintf_sscanf_inf_nan) {
506 CheckInfNan(snprintf, sscanf, "%s",
507 "[%a]", "[%+a]",
508 "[-inf]", "[inf]", "[+inf]",
509 "[-nan]", "[nan]", "[+nan]");
510 CheckInfNan(snprintf, sscanf, "%s",
511 "[%A]", "[%+A]",
512 "[-INF]", "[INF]", "[+INF]",
513 "[-NAN]", "[NAN]", "[+NAN]");
514 CheckInfNan(snprintf, sscanf, "%s",
515 "[%e]", "[%+e]",
516 "[-inf]", "[inf]", "[+inf]",
517 "[-nan]", "[nan]", "[+nan]");
518 CheckInfNan(snprintf, sscanf, "%s",
519 "[%E]", "[%+E]",
520 "[-INF]", "[INF]", "[+INF]",
521 "[-NAN]", "[NAN]", "[+NAN]");
522 CheckInfNan(snprintf, sscanf, "%s",
523 "[%f]", "[%+f]",
524 "[-inf]", "[inf]", "[+inf]",
525 "[-nan]", "[nan]", "[+nan]");
526 CheckInfNan(snprintf, sscanf, "%s",
527 "[%F]", "[%+F]",
528 "[-INF]", "[INF]", "[+INF]",
529 "[-NAN]", "[NAN]", "[+NAN]");
530 CheckInfNan(snprintf, sscanf, "%s",
531 "[%g]", "[%+g]",
532 "[-inf]", "[inf]", "[+inf]",
533 "[-nan]", "[nan]", "[+nan]");
534 CheckInfNan(snprintf, sscanf, "%s",
535 "[%G]", "[%+G]",
536 "[-INF]", "[INF]", "[+INF]",
537 "[-NAN]", "[NAN]", "[+NAN]");
Elliott Hughes1b18aff2014-12-16 14:45:32 -0800538}
Elliott Hughes7823f322014-04-14 12:11:28 -0700539
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700540TEST(STDIO_TEST, swprintf_swscanf_inf_nan) {
541 CheckInfNan(swprintf, swscanf, L"%s",
542 L"[%a]", L"[%+a]",
543 L"[-inf]", L"[inf]", L"[+inf]",
544 L"[-nan]", L"[nan]", L"[+nan]");
545 CheckInfNan(swprintf, swscanf, L"%s",
546 L"[%A]", L"[%+A]",
547 L"[-INF]", L"[INF]", L"[+INF]",
548 L"[-NAN]", L"[NAN]", L"[+NAN]");
549 CheckInfNan(swprintf, swscanf, L"%s",
550 L"[%e]", L"[%+e]",
551 L"[-inf]", L"[inf]", L"[+inf]",
552 L"[-nan]", L"[nan]", L"[+nan]");
553 CheckInfNan(swprintf, swscanf, L"%s",
554 L"[%E]", L"[%+E]",
555 L"[-INF]", L"[INF]", L"[+INF]",
556 L"[-NAN]", L"[NAN]", L"[+NAN]");
557 CheckInfNan(swprintf, swscanf, L"%s",
558 L"[%f]", L"[%+f]",
559 L"[-inf]", L"[inf]", L"[+inf]",
560 L"[-nan]", L"[nan]", L"[+nan]");
561 CheckInfNan(swprintf, swscanf, L"%s",
562 L"[%F]", L"[%+F]",
563 L"[-INF]", L"[INF]", L"[+INF]",
564 L"[-NAN]", L"[NAN]", L"[+NAN]");
565 CheckInfNan(swprintf, swscanf, L"%s",
566 L"[%g]", L"[%+g]",
567 L"[-inf]", L"[inf]", L"[+inf]",
568 L"[-nan]", L"[nan]", L"[+nan]");
569 CheckInfNan(swprintf, swscanf, L"%s",
570 L"[%G]", L"[%+G]",
571 L"[-INF]", L"[INF]", L"[+INF]",
572 L"[-NAN]", L"[NAN]", L"[+NAN]");
Elliott Hughes7823f322014-04-14 12:11:28 -0700573}
574
Dan Albert9601f162017-08-09 14:59:06 -0700575TEST(STDIO_TEST, swprintf) {
576 constexpr size_t nchars = 32;
577 wchar_t buf[nchars];
578
579 ASSERT_EQ(2, swprintf(buf, nchars, L"ab")) << strerror(errno);
580 ASSERT_EQ(std::wstring(L"ab"), buf);
581 ASSERT_EQ(5, swprintf(buf, nchars, L"%s", "abcde"));
582 ASSERT_EQ(std::wstring(L"abcde"), buf);
583
584 // Unlike swprintf(), swprintf() returns -1 in case of truncation
585 // and doesn't necessarily zero-terminate the output!
586 ASSERT_EQ(-1, swprintf(buf, 4, L"%s", "abcde"));
587
588 const char kString[] = "Hello, World";
589 ASSERT_EQ(12, swprintf(buf, nchars, L"%s", kString));
590 ASSERT_EQ(std::wstring(L"Hello, World"), buf);
591 ASSERT_EQ(12, swprintf(buf, 13, L"%s", kString));
592 ASSERT_EQ(std::wstring(L"Hello, World"), buf);
593}
594
595TEST(STDIO_TEST, swprintf_a) {
596 constexpr size_t nchars = 32;
597 wchar_t buf[nchars];
598
599 ASSERT_EQ(20, swprintf(buf, nchars, L"%a", 3.1415926535));
600 ASSERT_EQ(std::wstring(L"0x1.921fb54411744p+1"), buf);
601}
602
Elliott Hughes2f9c8ce2017-11-01 13:54:47 -0700603TEST(STDIO_TEST, swprintf_lc) {
604 constexpr size_t nchars = 32;
605 wchar_t buf[nchars];
606
607 wint_t wc = L'a';
608 EXPECT_EQ(3, swprintf(buf, nchars, L"<%lc>", wc));
609 EXPECT_EQ(std::wstring(L"<a>"), buf);
610}
611
612TEST(STDIO_TEST, swprintf_C) { // Synonym for %lc.
613 constexpr size_t nchars = 32;
614 wchar_t buf[nchars];
615
616 wint_t wc = L'a';
617 EXPECT_EQ(3, swprintf(buf, nchars, L"<%C>", wc));
618 EXPECT_EQ(std::wstring(L"<a>"), buf);
619}
620
Elliott Hughes618303c2017-11-02 16:58:44 -0700621TEST(STDIO_TEST, swprintf_jd_INTMAX_MAX) {
622 constexpr size_t nchars = 32;
623 wchar_t buf[nchars];
624
625 swprintf(buf, nchars, L"%jd", INTMAX_MAX);
626 EXPECT_EQ(std::wstring(L"9223372036854775807"), buf);
627}
628
629TEST(STDIO_TEST, swprintf_jd_INTMAX_MIN) {
630 constexpr size_t nchars = 32;
631 wchar_t buf[nchars];
632
633 swprintf(buf, nchars, L"%jd", INTMAX_MIN);
634 EXPECT_EQ(std::wstring(L"-9223372036854775808"), buf);
635}
636
637TEST(STDIO_TEST, swprintf_ju_UINTMAX_MAX) {
638 constexpr size_t nchars = 32;
639 wchar_t buf[nchars];
640
641 swprintf(buf, nchars, L"%ju", UINTMAX_MAX);
642 EXPECT_EQ(std::wstring(L"18446744073709551615"), buf);
643}
644
645TEST(STDIO_TEST, swprintf_1$ju_UINTMAX_MAX) {
646 constexpr size_t nchars = 32;
647 wchar_t buf[nchars];
648
649 swprintf(buf, nchars, L"%1$ju", UINTMAX_MAX);
650 EXPECT_EQ(std::wstring(L"18446744073709551615"), buf);
651}
652
Dan Albert9601f162017-08-09 14:59:06 -0700653TEST(STDIO_TEST, swprintf_ls) {
654 constexpr size_t nchars = 32;
655 wchar_t buf[nchars];
656
657 static const wchar_t kWideString[] = L"Hello\uff41 World";
658 ASSERT_EQ(12, swprintf(buf, nchars, L"%ls", kWideString));
659 ASSERT_EQ(std::wstring(kWideString), buf);
660 ASSERT_EQ(12, swprintf(buf, 13, L"%ls", kWideString));
661 ASSERT_EQ(std::wstring(kWideString), buf);
662}
663
Elliott Hughes2f9c8ce2017-11-01 13:54:47 -0700664TEST(STDIO_TEST, swprintf_S) { // Synonym for %ls.
665 constexpr size_t nchars = 32;
666 wchar_t buf[nchars];
667
668 static const wchar_t kWideString[] = L"Hello\uff41 World";
669 ASSERT_EQ(12, swprintf(buf, nchars, L"%S", kWideString));
670 ASSERT_EQ(std::wstring(kWideString), buf);
671 ASSERT_EQ(12, swprintf(buf, 13, L"%S", kWideString));
672 ASSERT_EQ(std::wstring(kWideString), buf);
673}
674
Christopher Ferris13f26a72016-01-13 13:47:58 -0800675TEST(STDIO_TEST, snprintf_d_INT_MAX) {
Elliott Hughes1d13c642013-09-23 16:02:39 -0700676 char buf[BUFSIZ];
677 snprintf(buf, sizeof(buf), "%d", INT_MAX);
678 EXPECT_STREQ("2147483647", buf);
679}
680
Christopher Ferris13f26a72016-01-13 13:47:58 -0800681TEST(STDIO_TEST, snprintf_d_INT_MIN) {
Elliott Hughes1d13c642013-09-23 16:02:39 -0700682 char buf[BUFSIZ];
683 snprintf(buf, sizeof(buf), "%d", INT_MIN);
684 EXPECT_STREQ("-2147483648", buf);
685}
686
Elliott Hughes618303c2017-11-02 16:58:44 -0700687TEST(STDIO_TEST, snprintf_jd_INTMAX_MAX) {
688 char buf[BUFSIZ];
689 snprintf(buf, sizeof(buf), "%jd", INTMAX_MAX);
690 EXPECT_STREQ("9223372036854775807", buf);
691}
692
693TEST(STDIO_TEST, snprintf_jd_INTMAX_MIN) {
694 char buf[BUFSIZ];
695 snprintf(buf, sizeof(buf), "%jd", INTMAX_MIN);
696 EXPECT_STREQ("-9223372036854775808", buf);
697}
698
699TEST(STDIO_TEST, snprintf_ju_UINTMAX_MAX) {
700 char buf[BUFSIZ];
701 snprintf(buf, sizeof(buf), "%ju", UINTMAX_MAX);
702 EXPECT_STREQ("18446744073709551615", buf);
703}
704
705TEST(STDIO_TEST, snprintf_1$ju_UINTMAX_MAX) {
706 char buf[BUFSIZ];
707 snprintf(buf, sizeof(buf), "%1$ju", UINTMAX_MAX);
708 EXPECT_STREQ("18446744073709551615", buf);
709}
710
Christopher Ferris13f26a72016-01-13 13:47:58 -0800711TEST(STDIO_TEST, snprintf_ld_LONG_MAX) {
Elliott Hughes1d13c642013-09-23 16:02:39 -0700712 char buf[BUFSIZ];
713 snprintf(buf, sizeof(buf), "%ld", LONG_MAX);
Josh Gaob36efa42016-09-15 13:55:41 -0700714#if defined(__LP64__)
Elliott Hughes925753a2013-10-18 13:17:18 -0700715 EXPECT_STREQ("9223372036854775807", buf);
716#else
Elliott Hughes1d13c642013-09-23 16:02:39 -0700717 EXPECT_STREQ("2147483647", buf);
Elliott Hughes925753a2013-10-18 13:17:18 -0700718#endif
Elliott Hughes1d13c642013-09-23 16:02:39 -0700719}
720
Christopher Ferris13f26a72016-01-13 13:47:58 -0800721TEST(STDIO_TEST, snprintf_ld_LONG_MIN) {
Elliott Hughes1d13c642013-09-23 16:02:39 -0700722 char buf[BUFSIZ];
723 snprintf(buf, sizeof(buf), "%ld", LONG_MIN);
Josh Gaob36efa42016-09-15 13:55:41 -0700724#if defined(__LP64__)
Elliott Hughes925753a2013-10-18 13:17:18 -0700725 EXPECT_STREQ("-9223372036854775808", buf);
726#else
Elliott Hughes1d13c642013-09-23 16:02:39 -0700727 EXPECT_STREQ("-2147483648", buf);
Elliott Hughes925753a2013-10-18 13:17:18 -0700728#endif
Elliott Hughes1d13c642013-09-23 16:02:39 -0700729}
730
Christopher Ferris13f26a72016-01-13 13:47:58 -0800731TEST(STDIO_TEST, snprintf_lld_LLONG_MAX) {
Elliott Hughes1d13c642013-09-23 16:02:39 -0700732 char buf[BUFSIZ];
733 snprintf(buf, sizeof(buf), "%lld", LLONG_MAX);
734 EXPECT_STREQ("9223372036854775807", buf);
735}
736
Christopher Ferris13f26a72016-01-13 13:47:58 -0800737TEST(STDIO_TEST, snprintf_lld_LLONG_MIN) {
Elliott Hughes1d13c642013-09-23 16:02:39 -0700738 char buf[BUFSIZ];
739 snprintf(buf, sizeof(buf), "%lld", LLONG_MIN);
740 EXPECT_STREQ("-9223372036854775808", buf);
741}
742
Elliott Hughes2f9c8ce2017-11-01 13:54:47 -0700743TEST(STDIO_TEST, snprintf_o_UINT_MAX) {
744 char buf[BUFSIZ];
745 snprintf(buf, sizeof(buf), "%o", UINT_MAX);
746 EXPECT_STREQ("37777777777", buf);
747}
748
749TEST(STDIO_TEST, snprintf_u_UINT_MAX) {
750 char buf[BUFSIZ];
751 snprintf(buf, sizeof(buf), "%u", UINT_MAX);
752 EXPECT_STREQ("4294967295", buf);
753}
754
755TEST(STDIO_TEST, snprintf_x_UINT_MAX) {
756 char buf[BUFSIZ];
757 snprintf(buf, sizeof(buf), "%x", UINT_MAX);
758 EXPECT_STREQ("ffffffff", buf);
759}
760
761TEST(STDIO_TEST, snprintf_X_UINT_MAX) {
762 char buf[BUFSIZ];
763 snprintf(buf, sizeof(buf), "%X", UINT_MAX);
764 EXPECT_STREQ("FFFFFFFF", buf);
765}
766
Christopher Ferris13f26a72016-01-13 13:47:58 -0800767TEST(STDIO_TEST, snprintf_e) {
Elliott Hughes4bd97ce2014-04-10 17:48:14 -0700768 char buf[BUFSIZ];
769
770 snprintf(buf, sizeof(buf), "%e", 1.5);
771 EXPECT_STREQ("1.500000e+00", buf);
772
Chih-Hung Hsiehc2edae32018-12-11 15:16:24 -0800773 snprintf(buf, sizeof(buf), "%Le", 1.5L);
Elliott Hughes4bd97ce2014-04-10 17:48:14 -0700774 EXPECT_STREQ("1.500000e+00", buf);
775}
776
Christopher Ferris13f26a72016-01-13 13:47:58 -0800777TEST(STDIO_TEST, snprintf_negative_zero_5084292) {
Elliott Hughese77f38f2014-05-14 12:39:12 -0700778 char buf[BUFSIZ];
779
Elliott Hughes1b18aff2014-12-16 14:45:32 -0800780 snprintf(buf, sizeof(buf), "%e", -0.0);
781 EXPECT_STREQ("-0.000000e+00", buf);
782 snprintf(buf, sizeof(buf), "%E", -0.0);
783 EXPECT_STREQ("-0.000000E+00", buf);
Elliott Hughese77f38f2014-05-14 12:39:12 -0700784 snprintf(buf, sizeof(buf), "%f", -0.0);
785 EXPECT_STREQ("-0.000000", buf);
Elliott Hughes1b18aff2014-12-16 14:45:32 -0800786 snprintf(buf, sizeof(buf), "%F", -0.0);
787 EXPECT_STREQ("-0.000000", buf);
788 snprintf(buf, sizeof(buf), "%g", -0.0);
789 EXPECT_STREQ("-0", buf);
790 snprintf(buf, sizeof(buf), "%G", -0.0);
791 EXPECT_STREQ("-0", buf);
792 snprintf(buf, sizeof(buf), "%a", -0.0);
793 EXPECT_STREQ("-0x0p+0", buf);
794 snprintf(buf, sizeof(buf), "%A", -0.0);
795 EXPECT_STREQ("-0X0P+0", buf);
Elliott Hughese77f38f2014-05-14 12:39:12 -0700796}
797
Christopher Ferris13f26a72016-01-13 13:47:58 -0800798TEST(STDIO_TEST, snprintf_utf8_15439554) {
Yi Kong32bc0fc2018-08-02 17:31:13 -0700799 locale_t cloc = newlocale(LC_ALL, "C.UTF-8", nullptr);
Wally Yaua40fdbd2014-08-26 09:47:23 -0700800 locale_t old_locale = uselocale(cloc);
Dan Albert1aec7c12014-07-30 10:53:48 -0700801
Elliott Hughes69f05d22014-06-05 20:10:09 -0700802 // http://b/15439554
803 char buf[BUFSIZ];
804
805 // 1-byte character.
806 snprintf(buf, sizeof(buf), "%dx%d", 1, 2);
807 EXPECT_STREQ("1x2", buf);
808 // 2-byte character.
809 snprintf(buf, sizeof(buf), "%d\xc2\xa2%d", 1, 2);
810 EXPECT_STREQ("1¢2", buf);
811 // 3-byte character.
812 snprintf(buf, sizeof(buf), "%d\xe2\x82\xac%d", 1, 2);
813 EXPECT_STREQ("1€2", buf);
814 // 4-byte character.
815 snprintf(buf, sizeof(buf), "%d\xf0\xa4\xad\xa2%d", 1, 2);
816 EXPECT_STREQ("1𤭢2", buf);
Dan Albert1aec7c12014-07-30 10:53:48 -0700817
Wally Yaua40fdbd2014-08-26 09:47:23 -0700818 uselocale(old_locale);
Dan Albert1aec7c12014-07-30 10:53:48 -0700819 freelocale(cloc);
Elliott Hughes69f05d22014-06-05 20:10:09 -0700820}
821
Elliott Hughes43f7c872016-02-05 11:18:41 -0800822static void* snprintf_small_stack_fn(void*) {
823 // Make life (realistically) hard for ourselves by allocating our own buffer for the result.
824 char buf[PATH_MAX];
825 snprintf(buf, sizeof(buf), "/proc/%d", getpid());
826 return nullptr;
827}
828
829TEST(STDIO_TEST, snprintf_small_stack) {
830 // Is it safe to call snprintf on a thread with a small stack?
831 // (The snprintf implementation puts some pretty large buffers on the stack.)
832 pthread_attr_t a;
833 ASSERT_EQ(0, pthread_attr_init(&a));
834 ASSERT_EQ(0, pthread_attr_setstacksize(&a, PTHREAD_STACK_MIN));
835
836 pthread_t t;
837 ASSERT_EQ(0, pthread_create(&t, &a, snprintf_small_stack_fn, nullptr));
838 ASSERT_EQ(0, pthread_join(t, nullptr));
839}
840
Elliott Hughes8200e552016-02-05 21:57:37 -0800841TEST(STDIO_TEST, snprintf_asterisk_overflow) {
842 char buf[128];
843 ASSERT_EQ(5, snprintf(buf, sizeof(buf), "%.*s%c", 4, "hello world", '!'));
844 ASSERT_EQ(12, snprintf(buf, sizeof(buf), "%.*s%c", INT_MAX/2, "hello world", '!'));
845 ASSERT_EQ(12, snprintf(buf, sizeof(buf), "%.*s%c", INT_MAX-1, "hello world", '!'));
846 ASSERT_EQ(12, snprintf(buf, sizeof(buf), "%.*s%c", INT_MAX, "hello world", '!'));
847 ASSERT_EQ(12, snprintf(buf, sizeof(buf), "%.*s%c", -1, "hello world", '!'));
848
849 // INT_MAX-1, INT_MAX, INT_MAX+1.
850 ASSERT_EQ(12, snprintf(buf, sizeof(buf), "%.2147483646s%c", "hello world", '!'));
851 ASSERT_EQ(12, snprintf(buf, sizeof(buf), "%.2147483647s%c", "hello world", '!'));
852 ASSERT_EQ(-1, snprintf(buf, sizeof(buf), "%.2147483648s%c", "hello world", '!'));
853 ASSERT_EQ(ENOMEM, errno);
854}
855
Elliott Hughes70715da2016-08-01 16:35:17 -0700856TEST(STDIO_TEST, fprintf) {
857 TemporaryFile tf;
858
859 FILE* tfile = fdopen(tf.fd, "r+");
860 ASSERT_TRUE(tfile != nullptr);
861
862 ASSERT_EQ(7, fprintf(tfile, "%d %s", 123, "abc"));
863 AssertFileIs(tfile, "123 abc");
864 fclose(tfile);
865}
866
Christopher Ferris13f26a72016-01-13 13:47:58 -0800867TEST(STDIO_TEST, fprintf_failures_7229520) {
Elliott Hughes69f05d22014-06-05 20:10:09 -0700868 // http://b/7229520
Elliott Hughesc9244bd2014-05-14 13:31:35 -0700869 FILE* fp;
Josh Gaof6e5b582018-06-01 15:30:54 -0700870 int fd_rdonly = open("/dev/null", O_RDONLY);
871 ASSERT_NE(-1, fd_rdonly);
Elliott Hughesc9244bd2014-05-14 13:31:35 -0700872
873 // Unbuffered case where the fprintf(3) itself fails.
874 ASSERT_NE(nullptr, fp = tmpfile());
Yi Kong32bc0fc2018-08-02 17:31:13 -0700875 setbuf(fp, nullptr);
Elliott Hughesc9244bd2014-05-14 13:31:35 -0700876 ASSERT_EQ(4, fprintf(fp, "epic"));
Josh Gaof6e5b582018-06-01 15:30:54 -0700877 ASSERT_NE(-1, dup2(fd_rdonly, fileno(fp)));
Elliott Hughesc9244bd2014-05-14 13:31:35 -0700878 ASSERT_EQ(-1, fprintf(fp, "fail"));
Josh Gaof6e5b582018-06-01 15:30:54 -0700879 ASSERT_EQ(0, fclose(fp));
Elliott Hughesc9244bd2014-05-14 13:31:35 -0700880
881 // Buffered case where we won't notice until the fclose(3).
882 // It's likely this is what was actually seen in http://b/7229520,
883 // and that expecting fprintf to fail is setting yourself up for
884 // disappointment. Remember to check fclose(3)'s return value, kids!
885 ASSERT_NE(nullptr, fp = tmpfile());
886 ASSERT_EQ(4, fprintf(fp, "epic"));
Josh Gaof6e5b582018-06-01 15:30:54 -0700887 ASSERT_NE(-1, dup2(fd_rdonly, fileno(fp)));
Elliott Hughesc9244bd2014-05-14 13:31:35 -0700888 ASSERT_EQ(4, fprintf(fp, "fail"));
889 ASSERT_EQ(-1, fclose(fp));
890}
891
Elliott Hughes468efc82018-07-10 14:39:49 -0700892TEST(STDIO_TEST, popen_r) {
Elliott Hughes6b3f49a2013-03-06 16:20:55 -0800893 FILE* fp = popen("cat /proc/version", "r");
Yi Kong32bc0fc2018-08-02 17:31:13 -0700894 ASSERT_TRUE(fp != nullptr);
Elliott Hughes6b3f49a2013-03-06 16:20:55 -0800895
896 char buf[16];
897 char* s = fgets(buf, sizeof(buf), fp);
898 buf[13] = '\0';
899 ASSERT_STREQ("Linux version", s);
900
901 ASSERT_EQ(0, pclose(fp));
902}
Elliott Hughes6b05c8e2013-04-11 13:54:48 -0700903
Elliott Hughes468efc82018-07-10 14:39:49 -0700904TEST(STDIO_TEST, popen_socketpair) {
905 FILE* fp = popen("cat", "r+");
Yi Kong32bc0fc2018-08-02 17:31:13 -0700906 ASSERT_TRUE(fp != nullptr);
Elliott Hughes468efc82018-07-10 14:39:49 -0700907
908 fputs("hello\nworld\n", fp);
909 fflush(fp);
910
911 char buf[16];
912 ASSERT_NE(nullptr, fgets(buf, sizeof(buf), fp));
913 EXPECT_STREQ("hello\n", buf);
914 ASSERT_NE(nullptr, fgets(buf, sizeof(buf), fp));
915 EXPECT_STREQ("world\n", buf);
916
917 ASSERT_EQ(0, pclose(fp));
918}
919
920TEST(STDIO_TEST, popen_socketpair_shutdown) {
921 FILE* fp = popen("uniq -c", "r+");
Yi Kong32bc0fc2018-08-02 17:31:13 -0700922 ASSERT_TRUE(fp != nullptr);
Elliott Hughes468efc82018-07-10 14:39:49 -0700923
924 fputs("a\na\na\na\nb\n", fp);
925 fflush(fp);
926 ASSERT_EQ(0, shutdown(fileno(fp), SHUT_WR));
927
928 char buf[16];
929 ASSERT_NE(nullptr, fgets(buf, sizeof(buf), fp));
930 EXPECT_STREQ(" 4 a\n", buf);
931 ASSERT_NE(nullptr, fgets(buf, sizeof(buf), fp));
932 EXPECT_STREQ(" 1 b\n", buf);
933
934 ASSERT_EQ(0, pclose(fp));
935}
936
937TEST(STDIO_TEST, popen_return_value_0) {
938 FILE* fp = popen("true", "r");
Yi Kong32bc0fc2018-08-02 17:31:13 -0700939 ASSERT_TRUE(fp != nullptr);
Elliott Hughes468efc82018-07-10 14:39:49 -0700940 int status = pclose(fp);
941 EXPECT_TRUE(WIFEXITED(status));
942 EXPECT_EQ(0, WEXITSTATUS(status));
943}
944
945TEST(STDIO_TEST, popen_return_value_1) {
946 FILE* fp = popen("false", "r");
Yi Kong32bc0fc2018-08-02 17:31:13 -0700947 ASSERT_TRUE(fp != nullptr);
Elliott Hughes468efc82018-07-10 14:39:49 -0700948 int status = pclose(fp);
949 EXPECT_TRUE(WIFEXITED(status));
950 EXPECT_EQ(1, WEXITSTATUS(status));
951}
952
953TEST(STDIO_TEST, popen_return_value_signal) {
954 FILE* fp = popen("kill -7 $$", "r");
Yi Kong32bc0fc2018-08-02 17:31:13 -0700955 ASSERT_TRUE(fp != nullptr);
Elliott Hughes468efc82018-07-10 14:39:49 -0700956 int status = pclose(fp);
957 EXPECT_TRUE(WIFSIGNALED(status));
958 EXPECT_EQ(7, WTERMSIG(status));
959}
960
Christopher Ferris13f26a72016-01-13 13:47:58 -0800961TEST(STDIO_TEST, getc) {
Elliott Hughes6b05c8e2013-04-11 13:54:48 -0700962 FILE* fp = fopen("/proc/version", "r");
Yi Kong32bc0fc2018-08-02 17:31:13 -0700963 ASSERT_TRUE(fp != nullptr);
Elliott Hughes6b05c8e2013-04-11 13:54:48 -0700964 ASSERT_EQ('L', getc(fp));
965 ASSERT_EQ('i', getc(fp));
966 ASSERT_EQ('n', getc(fp));
967 ASSERT_EQ('u', getc(fp));
968 ASSERT_EQ('x', getc(fp));
969 fclose(fp);
970}
971
Christopher Ferris13f26a72016-01-13 13:47:58 -0800972TEST(STDIO_TEST, putc) {
Elliott Hughes6b05c8e2013-04-11 13:54:48 -0700973 FILE* fp = fopen("/proc/version", "r");
Yi Kong32bc0fc2018-08-02 17:31:13 -0700974 ASSERT_TRUE(fp != nullptr);
Elliott Hughes6b05c8e2013-04-11 13:54:48 -0700975 ASSERT_EQ(EOF, putc('x', fp));
976 fclose(fp);
977}
Elliott Hughes603332f2014-03-12 17:10:41 -0700978
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700979TEST(STDIO_TEST, sscanf_swscanf) {
980 struct stuff {
981 char s1[123];
Elliott Hughes0d3ba1f2017-12-06 16:41:35 -0800982 int i1, i2;
983 char cs1[3];
984 char s2[3];
985 char c1;
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700986 double d1;
987 float f1;
Elliott Hughes0d3ba1f2017-12-06 16:41:35 -0800988 char s3[123];
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700989
990 void Check() {
Elliott Hughes0d3ba1f2017-12-06 16:41:35 -0800991 EXPECT_STREQ("hello", s1);
992 EXPECT_EQ(123, i1);
993 EXPECT_EQ(456, i2);
994 EXPECT_EQ('a', cs1[0]);
995 EXPECT_EQ('b', cs1[1]);
996 EXPECT_EQ('x', cs1[2]); // No terminating NUL.
997 EXPECT_STREQ("AB", s2); // Terminating NUL.
998 EXPECT_EQ('!', c1);
999 EXPECT_DOUBLE_EQ(1.23, d1);
1000 EXPECT_FLOAT_EQ(9.0f, f1);
1001 EXPECT_STREQ("world", s3);
Elliott Hughes7f0849f2016-08-26 16:17:17 -07001002 }
1003 } s;
1004
Elliott Hughes0d3ba1f2017-12-06 16:41:35 -08001005 memset(&s, 'x', sizeof(s));
1006 ASSERT_EQ(9, sscanf(" hello 123 456abAB! 1.23 0x1.2p3 world",
1007 "%s %i%i%2c%[A-Z]%c %lf %f %s",
1008 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 -07001009 s.Check();
1010
Elliott Hughes0d3ba1f2017-12-06 16:41:35 -08001011 memset(&s, 'x', sizeof(s));
1012 ASSERT_EQ(9, swscanf(L" hello 123 456abAB! 1.23 0x1.2p3 world",
1013 L"%s %i%i%2c%[A-Z]%c %lf %f %s",
1014 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 -07001015 s.Check();
Elliott Hughes603332f2014-03-12 17:10:41 -07001016}
Elliott Hughes53b24382014-05-02 18:29:25 -07001017
Elliott Hughes0d3ba1f2017-12-06 16:41:35 -08001018template <typename T>
1019static void CheckScanf(int sscanf_fn(const T*, const T*, ...),
1020 const T* input, const T* fmt,
1021 int expected_count, const char* expected_string) {
1022 char buf[256] = {};
1023 ASSERT_EQ(expected_count, sscanf_fn(input, fmt, &buf)) << fmt;
1024 ASSERT_STREQ(expected_string, buf) << fmt;
1025}
1026
1027TEST(STDIO_TEST, sscanf_ccl) {
1028 // `abc` is just those characters.
1029 CheckScanf(sscanf, "abcd", "%[abc]", 1, "abc");
1030 // `a-c` is the range 'a' .. 'c'.
1031 CheckScanf(sscanf, "abcd", "%[a-c]", 1, "abc");
1032 CheckScanf(sscanf, "-d", "%[a-c]", 0, "");
1033 CheckScanf(sscanf, "ac-bAd", "%[a--c]", 1, "ac-bA");
1034 // `a-c-e` is equivalent to `a-e`.
1035 CheckScanf(sscanf, "abcdefg", "%[a-c-e]", 1, "abcde");
1036 // `e-a` is equivalent to `ae-` (because 'e' > 'a').
1037 CheckScanf(sscanf, "-a-e-b", "%[e-a]", 1, "-a-e-");
1038 // An initial '^' negates the set.
1039 CheckScanf(sscanf, "abcde", "%[^d]", 1, "abc");
1040 CheckScanf(sscanf, "abcdefgh", "%[^c-d]", 1, "ab");
1041 CheckScanf(sscanf, "hgfedcba", "%[^c-d]", 1, "hgfe");
1042 // The first character may be ']' or '-' without being special.
1043 CheckScanf(sscanf, "[[]]x", "%[][]", 1, "[[]]");
1044 CheckScanf(sscanf, "-a-x", "%[-a]", 1, "-a-");
1045 // The last character may be '-' without being special.
1046 CheckScanf(sscanf, "-a-x", "%[a-]", 1, "-a-");
1047 // X--Y is [X--] + Y, not [X--] + [--Y] (a bug in my initial implementation).
1048 CheckScanf(sscanf, "+,-/.", "%[+--/]", 1, "+,-/");
1049}
1050
1051TEST(STDIO_TEST, swscanf_ccl) {
1052 // `abc` is just those characters.
1053 CheckScanf(swscanf, L"abcd", L"%[abc]", 1, "abc");
1054 // `a-c` is the range 'a' .. 'c'.
1055 CheckScanf(swscanf, L"abcd", L"%[a-c]", 1, "abc");
1056 CheckScanf(swscanf, L"-d", L"%[a-c]", 0, "");
1057 CheckScanf(swscanf, L"ac-bAd", L"%[a--c]", 1, "ac-bA");
1058 // `a-c-e` is equivalent to `a-e`.
1059 CheckScanf(swscanf, L"abcdefg", L"%[a-c-e]", 1, "abcde");
1060 // `e-a` is equivalent to `ae-` (because 'e' > 'a').
1061 CheckScanf(swscanf, L"-a-e-b", L"%[e-a]", 1, "-a-e-");
1062 // An initial '^' negates the set.
1063 CheckScanf(swscanf, L"abcde", L"%[^d]", 1, "abc");
1064 CheckScanf(swscanf, L"abcdefgh", L"%[^c-d]", 1, "ab");
1065 CheckScanf(swscanf, L"hgfedcba", L"%[^c-d]", 1, "hgfe");
1066 // The first character may be ']' or '-' without being special.
1067 CheckScanf(swscanf, L"[[]]x", L"%[][]", 1, "[[]]");
1068 CheckScanf(swscanf, L"-a-x", L"%[-a]", 1, "-a-");
1069 // The last character may be '-' without being special.
1070 CheckScanf(swscanf, L"-a-x", L"%[a-]", 1, "-a-");
1071 // X--Y is [X--] + Y, not [X--] + [--Y] (a bug in my initial implementation).
1072 CheckScanf(swscanf, L"+,-/.", L"%[+--/]", 1, "+,-/");
1073}
1074
Elliott Hughes38e4aef2018-01-18 10:21:29 -08001075template <typename T1, typename T2>
1076static void CheckScanfM(int sscanf_fn(const T1*, const T1*, ...),
1077 const T1* input, const T1* fmt,
1078 int expected_count, const T2* expected_string) {
1079 T2* result = nullptr;
1080 ASSERT_EQ(expected_count, sscanf_fn(input, fmt, &result)) << fmt;
1081 if (expected_string == nullptr) {
1082 ASSERT_EQ(nullptr, result);
1083 } else {
1084 ASSERT_STREQ(expected_string, result) << fmt;
1085 }
1086 free(result);
1087}
1088
1089TEST(STDIO_TEST, sscanf_mc) {
1090 char* p1 = nullptr;
1091 char* p2 = nullptr;
1092 ASSERT_EQ(2, sscanf("hello", "%mc%mc", &p1, &p2));
1093 ASSERT_EQ('h', *p1);
1094 ASSERT_EQ('e', *p2);
1095 free(p1);
1096 free(p2);
1097
1098 p1 = nullptr;
1099 ASSERT_EQ(1, sscanf("hello", "%4mc", &p1));
1100 ASSERT_EQ('h', p1[0]);
1101 ASSERT_EQ('e', p1[1]);
1102 ASSERT_EQ('l', p1[2]);
1103 ASSERT_EQ('l', p1[3]);
1104 free(p1);
1105
1106 p1 = nullptr;
1107 ASSERT_EQ(1, sscanf("hello world", "%30mc", &p1));
1108 ASSERT_EQ('h', p1[0]);
1109 ASSERT_EQ('e', p1[1]);
1110 ASSERT_EQ('l', p1[2]);
1111 ASSERT_EQ('l', p1[3]);
1112 ASSERT_EQ('o', p1[4]);
1113 free(p1);
1114}
1115
1116
1117TEST(STDIO_TEST, sscanf_mlc) {
1118 // This is so useless that clang doesn't even believe it exists...
1119#pragma clang diagnostic push
1120#pragma clang diagnostic ignored "-Wformat-invalid-specifier"
1121#pragma clang diagnostic ignored "-Wformat-extra-args"
1122
1123 wchar_t* p1 = nullptr;
1124 wchar_t* p2 = nullptr;
1125 ASSERT_EQ(2, sscanf("hello", "%mlc%mlc", &p1, &p2));
1126 ASSERT_EQ(L'h', *p1);
1127 ASSERT_EQ(L'e', *p2);
1128 free(p1);
1129 free(p2);
1130
1131 p1 = nullptr;
1132 ASSERT_EQ(1, sscanf("hello", "%4mlc", &p1));
1133 ASSERT_EQ(L'h', p1[0]);
1134 ASSERT_EQ(L'e', p1[1]);
1135 ASSERT_EQ(L'l', p1[2]);
1136 ASSERT_EQ(L'l', p1[3]);
1137 free(p1);
1138
1139 p1 = nullptr;
1140 ASSERT_EQ(1, sscanf("hello world", "%30mlc", &p1));
1141 ASSERT_EQ(L'h', p1[0]);
1142 ASSERT_EQ(L'e', p1[1]);
1143 ASSERT_EQ(L'l', p1[2]);
1144 ASSERT_EQ(L'l', p1[3]);
1145 ASSERT_EQ(L'o', p1[4]);
1146 free(p1);
1147#pragma clang diagnostic pop
1148}
1149
1150
1151TEST(STDIO_TEST, sscanf_ms) {
1152 CheckScanfM(sscanf, "hello", "%ms", 1, "hello");
1153 CheckScanfM(sscanf, "hello", "%4ms", 1, "hell");
1154 CheckScanfM(sscanf, "hello world", "%30ms", 1, "hello");
1155}
1156
1157TEST(STDIO_TEST, sscanf_mls) {
1158 CheckScanfM(sscanf, "hello", "%mls", 1, L"hello");
1159 CheckScanfM(sscanf, "hello", "%4mls", 1, L"hell");
1160 CheckScanfM(sscanf, "hello world", "%30mls", 1, L"hello");
1161}
1162
1163TEST(STDIO_TEST, sscanf_m_ccl) {
1164 CheckScanfM(sscanf, "hello", "%m[a-z]", 1, "hello");
1165 CheckScanfM(sscanf, "hello", "%4m[a-z]", 1, "hell");
1166 CheckScanfM(sscanf, "hello world", "%30m[a-z]", 1, "hello");
1167}
1168
1169TEST(STDIO_TEST, sscanf_ml_ccl) {
1170 CheckScanfM(sscanf, "hello", "%ml[a-z]", 1, L"hello");
1171 CheckScanfM(sscanf, "hello", "%4ml[a-z]", 1, L"hell");
1172 CheckScanfM(sscanf, "hello world", "%30ml[a-z]", 1, L"hello");
1173}
1174
1175TEST(STDIO_TEST, sscanf_ls) {
1176 wchar_t w[32] = {};
1177 ASSERT_EQ(1, sscanf("hello world", "%ls", w));
1178 ASSERT_EQ(L"hello", std::wstring(w));
1179}
1180
1181TEST(STDIO_TEST, sscanf_ls_suppress) {
1182 ASSERT_EQ(0, sscanf("hello world", "%*ls %*ls"));
1183}
1184
1185TEST(STDIO_TEST, sscanf_ls_n) {
1186 setlocale(LC_ALL, "C.UTF-8");
1187 wchar_t w[32] = {};
1188 int pos = 0;
1189 ASSERT_EQ(1, sscanf("\xc4\x80", "%ls%n", w, &pos));
1190 ASSERT_EQ(static_cast<wchar_t>(256), w[0]);
1191 ASSERT_EQ(2, pos);
1192}
1193
1194TEST(STDIO_TEST, sscanf_ls_realloc) {
1195 // This is so useless that clang doesn't even believe it exists...
1196#pragma clang diagnostic push
1197#pragma clang diagnostic ignored "-Wformat-invalid-specifier"
1198#pragma clang diagnostic ignored "-Wformat-extra-args"
1199 wchar_t* p1 = nullptr;
1200 wchar_t* p2 = nullptr;
1201 ASSERT_EQ(2, sscanf("1234567890123456789012345678901234567890 world", "%mls %mls", &p1, &p2));
1202 ASSERT_EQ(L"1234567890123456789012345678901234567890", std::wstring(p1));
1203 ASSERT_EQ(L"world", std::wstring(p2));
1204#pragma clang diagnostic pop
1205}
1206
Elliott Hughesbf9cb9e2017-12-11 12:39:01 -08001207// https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=202240
1208TEST(STDIO_TEST, scanf_wscanf_EOF) {
1209 EXPECT_EQ(0, sscanf("b", "ab"));
1210 EXPECT_EQ(EOF, sscanf("", "a"));
1211 EXPECT_EQ(0, swscanf(L"b", L"ab"));
1212 EXPECT_EQ(EOF, swscanf(L"", L"a"));
1213}
1214
1215TEST(STDIO_TEST, scanf_invalid_UTF8) {
1216#if 0 // TODO: more tests invented during code review; no regressions, so fix later.
1217 char buf[BUFSIZ];
1218 wchar_t wbuf[BUFSIZ];
1219
1220 memset(buf, 0, sizeof(buf));
1221 memset(wbuf, 0, sizeof(wbuf));
1222 EXPECT_EQ(0, sscanf("\xc0" " foo", "%ls %s", wbuf, buf));
1223#endif
1224}
1225
1226TEST(STDIO_TEST, scanf_no_match_no_termination) {
1227 char buf[4] = "x";
1228 EXPECT_EQ(0, sscanf("d", "%[abc]", buf));
1229 EXPECT_EQ('x', buf[0]);
1230 EXPECT_EQ(0, swscanf(L"d", L"%[abc]", buf));
1231 EXPECT_EQ('x', buf[0]);
1232
1233 wchar_t wbuf[4] = L"x";
1234 EXPECT_EQ(0, swscanf(L"d", L"%l[abc]", wbuf));
1235 EXPECT_EQ(L'x', wbuf[0]);
1236
1237 EXPECT_EQ(EOF, sscanf("", "%s", buf));
1238 EXPECT_EQ('x', buf[0]);
1239
1240 EXPECT_EQ(EOF, swscanf(L"", L"%ls", wbuf));
1241 EXPECT_EQ(L'x', wbuf[0]);
1242}
1243
1244TEST(STDIO_TEST, scanf_wscanf_wide_character_class) {
1245#if 0 // TODO: more tests invented during code review; no regressions, so fix later.
1246 wchar_t buf[BUFSIZ];
1247
1248 // A wide character shouldn't match an ASCII-only class for scanf or wscanf.
1249 memset(buf, 0, sizeof(buf));
1250 EXPECT_EQ(1, sscanf("xĀyz", "%l[xy]", buf));
1251 EXPECT_EQ(L"x"s, std::wstring(buf));
1252 memset(buf, 0, sizeof(buf));
1253 EXPECT_EQ(1, swscanf(L"xĀyz", L"%l[xy]", buf));
1254 EXPECT_EQ(L"x"s, std::wstring(buf));
1255
1256 // Even if scanf has wide characters in a class, they won't match...
1257 // TODO: is that a bug?
1258 memset(buf, 0, sizeof(buf));
1259 EXPECT_EQ(1, sscanf("xĀyz", "%l[xĀy]", buf));
1260 EXPECT_EQ(L"x"s, std::wstring(buf));
1261 // ...unless you use wscanf.
1262 memset(buf, 0, sizeof(buf));
1263 EXPECT_EQ(1, swscanf(L"xĀyz", L"%l[xĀy]", buf));
1264 EXPECT_EQ(L"xĀy"s, std::wstring(buf));
1265
1266 // Negation only covers ASCII for scanf...
1267 memset(buf, 0, sizeof(buf));
1268 EXPECT_EQ(1, sscanf("xĀyz", "%l[^ab]", buf));
1269 EXPECT_EQ(L"x"s, std::wstring(buf));
1270 // ...but covers wide characters for wscanf.
1271 memset(buf, 0, sizeof(buf));
1272 EXPECT_EQ(1, swscanf(L"xĀyz", L"%l[^ab]", buf));
1273 EXPECT_EQ(L"xĀyz"s, std::wstring(buf));
1274
1275 // We already determined that non-ASCII characters are ignored in scanf classes.
1276 memset(buf, 0, sizeof(buf));
1277 EXPECT_EQ(1, sscanf("x"
1278 "\xc4\x80" // Matches a byte from each wide char in the class.
1279 "\xc6\x82" // Neither byte is in the class.
1280 "yz",
1281 "%l[xy" "\xc5\x80" "\xc4\x81" "]", buf));
1282 EXPECT_EQ(L"x", std::wstring(buf));
1283 // bionic and glibc both behave badly for wscanf, so let's call it right for now...
1284 memset(buf, 0, sizeof(buf));
1285 EXPECT_EQ(1, swscanf(L"x"
1286 L"\xc4\x80"
1287 L"\xc6\x82"
1288 L"yz",
1289 L"%l[xy" L"\xc5\x80" L"\xc4\x81" L"]", buf));
1290 // Note that this isn't L"xĀ" --- although the *bytes* matched, they're
1291 // not put back together as a wide character.
1292 EXPECT_EQ(L"x" L"\xc4" L"\x80", std::wstring(buf));
1293#endif
1294}
1295
Christopher Ferris13f26a72016-01-13 13:47:58 -08001296TEST(STDIO_TEST, cantwrite_EBADF) {
Elliott Hughes53b24382014-05-02 18:29:25 -07001297 // If we open a file read-only...
1298 FILE* fp = fopen("/proc/version", "r");
1299
1300 // ...all attempts to write to that file should return failure.
1301
1302 // They should also set errno to EBADF. This isn't POSIX, but it's traditional.
1303 // glibc gets the wide-character functions wrong.
1304
1305 errno = 0;
1306 EXPECT_EQ(EOF, putc('x', fp));
1307 EXPECT_EQ(EBADF, errno);
1308
1309 errno = 0;
1310 EXPECT_EQ(EOF, fprintf(fp, "hello"));
1311 EXPECT_EQ(EBADF, errno);
1312
1313 errno = 0;
1314 EXPECT_EQ(EOF, fwprintf(fp, L"hello"));
Elliott Hughes063525c2014-05-13 11:19:57 -07001315#if defined(__BIONIC__)
Elliott Hughes53b24382014-05-02 18:29:25 -07001316 EXPECT_EQ(EBADF, errno);
1317#endif
1318
1319 errno = 0;
Elliott Hughes53b24382014-05-02 18:29:25 -07001320 EXPECT_EQ(0U, fwrite("hello", 1, 2, fp));
1321 EXPECT_EQ(EBADF, errno);
1322
1323 errno = 0;
1324 EXPECT_EQ(EOF, fputs("hello", fp));
1325 EXPECT_EQ(EBADF, errno);
1326
1327 errno = 0;
1328 EXPECT_EQ(WEOF, fputwc(L'x', fp));
Elliott Hughes063525c2014-05-13 11:19:57 -07001329#if defined(__BIONIC__)
Elliott Hughes53b24382014-05-02 18:29:25 -07001330 EXPECT_EQ(EBADF, errno);
1331#endif
1332}
Calin Juravle03e4ebe2014-05-08 14:42:06 +01001333
1334// Tests that we can only have a consistent and correct fpos_t when using
1335// f*pos functions (i.e. fpos doesn't get inside a multi byte character).
Christopher Ferris13f26a72016-01-13 13:47:58 -08001336TEST(STDIO_TEST, consistent_fpos_t) {
Calin Juravle03e4ebe2014-05-08 14:42:06 +01001337 ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
1338 uselocale(LC_GLOBAL_LOCALE);
1339
1340 FILE* fp = tmpfile();
Yi Kong32bc0fc2018-08-02 17:31:13 -07001341 ASSERT_TRUE(fp != nullptr);
Calin Juravle03e4ebe2014-05-08 14:42:06 +01001342
1343 wchar_t mb_one_bytes = L'h';
1344 wchar_t mb_two_bytes = 0x00a2;
1345 wchar_t mb_three_bytes = 0x20ac;
1346 wchar_t mb_four_bytes = 0x24b62;
1347
1348 // Write to file.
1349 ASSERT_EQ(mb_one_bytes, static_cast<wchar_t>(fputwc(mb_one_bytes, fp)));
1350 ASSERT_EQ(mb_two_bytes, static_cast<wchar_t>(fputwc(mb_two_bytes, fp)));
1351 ASSERT_EQ(mb_three_bytes, static_cast<wchar_t>(fputwc(mb_three_bytes, fp)));
1352 ASSERT_EQ(mb_four_bytes, static_cast<wchar_t>(fputwc(mb_four_bytes, fp)));
1353
1354 rewind(fp);
1355
1356 // Record each character position.
1357 fpos_t pos1;
1358 fpos_t pos2;
1359 fpos_t pos3;
1360 fpos_t pos4;
1361 fpos_t pos5;
1362 EXPECT_EQ(0, fgetpos(fp, &pos1));
1363 ASSERT_EQ(mb_one_bytes, static_cast<wchar_t>(fgetwc(fp)));
1364 EXPECT_EQ(0, fgetpos(fp, &pos2));
1365 ASSERT_EQ(mb_two_bytes, static_cast<wchar_t>(fgetwc(fp)));
1366 EXPECT_EQ(0, fgetpos(fp, &pos3));
1367 ASSERT_EQ(mb_three_bytes, static_cast<wchar_t>(fgetwc(fp)));
1368 EXPECT_EQ(0, fgetpos(fp, &pos4));
1369 ASSERT_EQ(mb_four_bytes, static_cast<wchar_t>(fgetwc(fp)));
1370 EXPECT_EQ(0, fgetpos(fp, &pos5));
1371
Elliott Hughes063525c2014-05-13 11:19:57 -07001372#if defined(__BIONIC__)
Calin Juravle03e4ebe2014-05-08 14:42:06 +01001373 // Bionic's fpos_t is just an alias for off_t. This is inherited from OpenBSD
1374 // upstream. Glibc differs by storing the mbstate_t inside its fpos_t. In
1375 // Bionic (and upstream OpenBSD) the mbstate_t is stored inside the FILE
1376 // structure.
1377 ASSERT_EQ(0, static_cast<off_t>(pos1));
1378 ASSERT_EQ(1, static_cast<off_t>(pos2));
1379 ASSERT_EQ(3, static_cast<off_t>(pos3));
1380 ASSERT_EQ(6, static_cast<off_t>(pos4));
1381 ASSERT_EQ(10, static_cast<off_t>(pos5));
1382#endif
1383
1384 // Exercise back and forth movements of the position.
1385 ASSERT_EQ(0, fsetpos(fp, &pos2));
1386 ASSERT_EQ(mb_two_bytes, static_cast<wchar_t>(fgetwc(fp)));
1387 ASSERT_EQ(0, fsetpos(fp, &pos1));
1388 ASSERT_EQ(mb_one_bytes, static_cast<wchar_t>(fgetwc(fp)));
1389 ASSERT_EQ(0, fsetpos(fp, &pos4));
1390 ASSERT_EQ(mb_four_bytes, static_cast<wchar_t>(fgetwc(fp)));
1391 ASSERT_EQ(0, fsetpos(fp, &pos3));
1392 ASSERT_EQ(mb_three_bytes, static_cast<wchar_t>(fgetwc(fp)));
1393 ASSERT_EQ(0, fsetpos(fp, &pos5));
1394 ASSERT_EQ(WEOF, fgetwc(fp));
1395
1396 fclose(fp);
1397}
1398
1399// Exercise the interaction between fpos and seek.
Christopher Ferris13f26a72016-01-13 13:47:58 -08001400TEST(STDIO_TEST, fpos_t_and_seek) {
Calin Juravle03e4ebe2014-05-08 14:42:06 +01001401 ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
1402 uselocale(LC_GLOBAL_LOCALE);
1403
Calin Juravle9b95ea92014-05-14 17:07:10 +01001404 // In glibc-2.16 fseek doesn't work properly in wide mode
1405 // (https://sourceware.org/bugzilla/show_bug.cgi?id=14543). One workaround is
1406 // to close and re-open the file. We do it in order to make the test pass
1407 // with all glibcs.
1408
Calin Juravle03e4ebe2014-05-08 14:42:06 +01001409 TemporaryFile tf;
1410 FILE* fp = fdopen(tf.fd, "w+");
Yi Kong32bc0fc2018-08-02 17:31:13 -07001411 ASSERT_TRUE(fp != nullptr);
Calin Juravle03e4ebe2014-05-08 14:42:06 +01001412
1413 wchar_t mb_two_bytes = 0x00a2;
1414 wchar_t mb_three_bytes = 0x20ac;
1415 wchar_t mb_four_bytes = 0x24b62;
1416
1417 // Write to file.
1418 ASSERT_EQ(mb_two_bytes, static_cast<wchar_t>(fputwc(mb_two_bytes, fp)));
1419 ASSERT_EQ(mb_three_bytes, static_cast<wchar_t>(fputwc(mb_three_bytes, fp)));
1420 ASSERT_EQ(mb_four_bytes, static_cast<wchar_t>(fputwc(mb_four_bytes, fp)));
1421
1422 fflush(fp);
1423 fclose(fp);
1424
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -08001425 fp = fopen(tf.path, "r");
Yi Kong32bc0fc2018-08-02 17:31:13 -07001426 ASSERT_TRUE(fp != nullptr);
Calin Juravle03e4ebe2014-05-08 14:42:06 +01001427
1428 // Store a valid position.
1429 fpos_t mb_two_bytes_pos;
1430 ASSERT_EQ(0, fgetpos(fp, &mb_two_bytes_pos));
1431
1432 // Move inside mb_four_bytes with fseek.
1433 long offset_inside_mb = 6;
1434 ASSERT_EQ(0, fseek(fp, offset_inside_mb, SEEK_SET));
1435
1436 // Store the "inside multi byte" position.
1437 fpos_t pos_inside_mb;
1438 ASSERT_EQ(0, fgetpos(fp, &pos_inside_mb));
Elliott Hughes063525c2014-05-13 11:19:57 -07001439#if defined(__BIONIC__)
1440 ASSERT_EQ(offset_inside_mb, static_cast<off_t>(pos_inside_mb));
1441#endif
Calin Juravle03e4ebe2014-05-08 14:42:06 +01001442
1443 // Reading from within a byte should produce an error.
1444 ASSERT_EQ(WEOF, fgetwc(fp));
1445 ASSERT_EQ(EILSEQ, errno);
1446
1447 // Reverting to a valid position should work.
1448 ASSERT_EQ(0, fsetpos(fp, &mb_two_bytes_pos));
1449 ASSERT_EQ(mb_two_bytes, static_cast<wchar_t>(fgetwc(fp)));
1450
1451 // Moving withing a multi byte with fsetpos should work but reading should
1452 // produce an error.
1453 ASSERT_EQ(0, fsetpos(fp, &pos_inside_mb));
1454 ASSERT_EQ(WEOF, fgetwc(fp));
1455 ASSERT_EQ(EILSEQ, errno);
1456
Elliott Hughes3a4c4542017-07-19 17:20:24 -07001457 ASSERT_EQ(0, fclose(fp));
Calin Juravle03e4ebe2014-05-08 14:42:06 +01001458}
Elliott Hughes6b841db2014-08-20 16:10:49 -07001459
Christopher Ferris13f26a72016-01-13 13:47:58 -08001460TEST(STDIO_TEST, fmemopen) {
Elliott Hughes6b841db2014-08-20 16:10:49 -07001461 char buf[16];
1462 memset(buf, 0, sizeof(buf));
1463 FILE* fp = fmemopen(buf, sizeof(buf), "r+");
1464 ASSERT_EQ('<', fputc('<', fp));
1465 ASSERT_NE(EOF, fputs("abc>\n", fp));
1466 fflush(fp);
1467
Elliott Hughes3a4c4542017-07-19 17:20:24 -07001468 // We wrote to the buffer...
Elliott Hughes6b841db2014-08-20 16:10:49 -07001469 ASSERT_STREQ("<abc>\n", buf);
1470
Elliott Hughes3a4c4542017-07-19 17:20:24 -07001471 // And can read back from the file.
Elliott Hughes70715da2016-08-01 16:35:17 -07001472 AssertFileIs(fp, "<abc>\n", true);
Elliott Hughes3a4c4542017-07-19 17:20:24 -07001473 ASSERT_EQ(0, fclose(fp));
Elliott Hughes6b841db2014-08-20 16:10:49 -07001474}
1475
Elliott Hughes3a4c4542017-07-19 17:20:24 -07001476TEST(STDIO_TEST, fmemopen_nullptr) {
Elliott Hughes6b841db2014-08-20 16:10:49 -07001477 FILE* fp = fmemopen(nullptr, 128, "r+");
1478 ASSERT_NE(EOF, fputs("xyz\n", fp));
1479
Elliott Hughes70715da2016-08-01 16:35:17 -07001480 AssertFileIs(fp, "xyz\n", true);
Elliott Hughes3a4c4542017-07-19 17:20:24 -07001481 ASSERT_EQ(0, fclose(fp));
Elliott Hughes6b841db2014-08-20 16:10:49 -07001482}
1483
Elliott Hughes3a4c4542017-07-19 17:20:24 -07001484TEST(STDIO_TEST, fmemopen_trailing_NUL_byte) {
1485 FILE* fp;
1486 char buf[8];
1487
1488 // POSIX: "When a stream open for writing is flushed or closed, a null byte
1489 // shall be written at the current position or at the end of the buffer,
1490 // depending on the size of the contents."
1491 memset(buf, 'x', sizeof(buf));
1492 ASSERT_NE(nullptr, fp = fmemopen(buf, sizeof(buf), "w"));
1493 // Even with nothing written (and not in truncate mode), we'll flush a NUL...
1494 ASSERT_EQ(0, fflush(fp));
1495 EXPECT_EQ("\0xxxxxxx"s, std::string(buf, buf + sizeof(buf)));
1496 // Now write and check that the NUL moves along with our writes...
1497 ASSERT_NE(EOF, fputs("hello", fp));
1498 ASSERT_EQ(0, fflush(fp));
1499 EXPECT_EQ("hello\0xx"s, std::string(buf, buf + sizeof(buf)));
1500 ASSERT_NE(EOF, fputs("wo", fp));
1501 ASSERT_EQ(0, fflush(fp));
1502 EXPECT_EQ("hellowo\0"s, std::string(buf, buf + sizeof(buf)));
1503 ASSERT_EQ(0, fclose(fp));
1504
1505 // "If a stream open for update is flushed or closed and the last write has
1506 // advanced the current buffer size, a null byte shall be written at the end
1507 // of the buffer if it fits."
1508 memset(buf, 'x', sizeof(buf));
1509 ASSERT_NE(nullptr, fp = fmemopen(buf, sizeof(buf), "r+"));
1510 // Nothing written yet, so no advance...
1511 ASSERT_EQ(0, fflush(fp));
1512 EXPECT_EQ("xxxxxxxx"s, std::string(buf, buf + sizeof(buf)));
1513 ASSERT_NE(EOF, fputs("hello", fp));
1514 ASSERT_EQ(0, fclose(fp));
1515}
1516
1517TEST(STDIO_TEST, fmemopen_size) {
1518 FILE* fp;
Elliott Hughes6b841db2014-08-20 16:10:49 -07001519 char buf[16];
Elliott Hughes3a4c4542017-07-19 17:20:24 -07001520 memset(buf, 'x', sizeof(buf));
Elliott Hughes6b841db2014-08-20 16:10:49 -07001521
Elliott Hughes3a4c4542017-07-19 17:20:24 -07001522 // POSIX: "The stream shall also maintain the size of the current buffer
1523 // contents; use of fseek() or fseeko() on the stream with SEEK_END shall
1524 // seek relative to this size."
Elliott Hughes6b841db2014-08-20 16:10:49 -07001525
Elliott Hughes3a4c4542017-07-19 17:20:24 -07001526 // "For modes r and r+ the size shall be set to the value given by the size
1527 // argument."
1528 ASSERT_NE(nullptr, fp = fmemopen(buf, 16, "r"));
1529 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
1530 EXPECT_EQ(16, ftell(fp));
1531 EXPECT_EQ(16, ftello(fp));
1532 ASSERT_EQ(0, fseeko(fp, 0, SEEK_END));
1533 EXPECT_EQ(16, ftell(fp));
1534 EXPECT_EQ(16, ftello(fp));
1535 ASSERT_EQ(0, fclose(fp));
1536 ASSERT_NE(nullptr, fp = fmemopen(buf, 16, "r+"));
1537 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
1538 EXPECT_EQ(16, ftell(fp));
1539 EXPECT_EQ(16, ftello(fp));
1540 ASSERT_EQ(0, fseeko(fp, 0, SEEK_END));
1541 EXPECT_EQ(16, ftell(fp));
1542 EXPECT_EQ(16, ftello(fp));
1543 ASSERT_EQ(0, fclose(fp));
1544
1545 // "For modes w and w+ the initial size shall be zero..."
1546 ASSERT_NE(nullptr, fp = fmemopen(nullptr, 16, "w"));
1547 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
1548 EXPECT_EQ(0, ftell(fp));
1549 EXPECT_EQ(0, ftello(fp));
1550 ASSERT_EQ(0, fseeko(fp, 0, SEEK_END));
1551 EXPECT_EQ(0, ftell(fp));
1552 EXPECT_EQ(0, ftello(fp));
1553 ASSERT_EQ(0, fclose(fp));
1554 ASSERT_NE(nullptr, fp = fmemopen(nullptr, 16, "w+"));
1555 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
1556 EXPECT_EQ(0, ftell(fp));
1557 EXPECT_EQ(0, ftello(fp));
1558 ASSERT_EQ(0, fseeko(fp, 0, SEEK_END));
1559 EXPECT_EQ(0, ftell(fp));
1560 EXPECT_EQ(0, ftello(fp));
1561 ASSERT_EQ(0, fclose(fp));
1562
1563 // "...and for modes a and a+ the initial size shall be:
1564 // 1. Zero, if buf is a null pointer
1565 ASSERT_NE(nullptr, fp = fmemopen(nullptr, 16, "a"));
1566 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
1567 EXPECT_EQ(0, ftell(fp));
1568 EXPECT_EQ(0, ftello(fp));
1569 ASSERT_EQ(0, fseeko(fp, 0, SEEK_END));
1570 EXPECT_EQ(0, ftell(fp));
1571 EXPECT_EQ(0, ftello(fp));
1572 ASSERT_EQ(0, fclose(fp));
1573 ASSERT_NE(nullptr, fp = fmemopen(nullptr, 16, "a+"));
1574 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
1575 EXPECT_EQ(0, ftell(fp));
1576 EXPECT_EQ(0, ftello(fp));
1577 ASSERT_EQ(0, fseeko(fp, 0, SEEK_END));
1578 EXPECT_EQ(0, ftell(fp));
1579 EXPECT_EQ(0, ftello(fp));
1580 ASSERT_EQ(0, fclose(fp));
1581
1582 // 2. The position of the first null byte in the buffer, if one is found
1583 memset(buf, 'x', sizeof(buf));
1584 buf[3] = '\0';
1585 ASSERT_NE(nullptr, fp = fmemopen(buf, 16, "a"));
1586 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
1587 EXPECT_EQ(3, ftell(fp));
1588 EXPECT_EQ(3, ftello(fp));
1589 ASSERT_EQ(0, fseeko(fp, 0, SEEK_END));
1590 EXPECT_EQ(3, ftell(fp));
1591 EXPECT_EQ(3, ftello(fp));
1592 ASSERT_EQ(0, fclose(fp));
1593 memset(buf, 'x', sizeof(buf));
1594 buf[3] = '\0';
1595 ASSERT_NE(nullptr, fp = fmemopen(buf, 16, "a+"));
1596 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
1597 EXPECT_EQ(3, ftell(fp));
1598 EXPECT_EQ(3, ftello(fp));
1599 ASSERT_EQ(0, fseeko(fp, 0, SEEK_END));
1600 EXPECT_EQ(3, ftell(fp));
1601 EXPECT_EQ(3, ftello(fp));
1602 ASSERT_EQ(0, fclose(fp));
1603
1604 // 3. The value of the size argument, if buf is not a null pointer and no
1605 // null byte is found.
1606 memset(buf, 'x', sizeof(buf));
1607 ASSERT_NE(nullptr, fp = fmemopen(buf, 16, "a"));
1608 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
1609 EXPECT_EQ(16, ftell(fp));
1610 EXPECT_EQ(16, ftello(fp));
1611 ASSERT_EQ(0, fseeko(fp, 0, SEEK_END));
1612 EXPECT_EQ(16, ftell(fp));
1613 EXPECT_EQ(16, ftello(fp));
1614 ASSERT_EQ(0, fclose(fp));
1615 memset(buf, 'x', sizeof(buf));
1616 ASSERT_NE(nullptr, fp = fmemopen(buf, 16, "a+"));
1617 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
1618 EXPECT_EQ(16, ftell(fp));
1619 EXPECT_EQ(16, ftello(fp));
1620 ASSERT_EQ(0, fseeko(fp, 0, SEEK_END));
1621 EXPECT_EQ(16, ftell(fp));
1622 EXPECT_EQ(16, ftello(fp));
1623 ASSERT_EQ(0, fclose(fp));
1624}
1625
1626TEST(STDIO_TEST, fmemopen_SEEK_END) {
1627 // fseek SEEK_END is relative to the current string length, not the buffer size.
1628 FILE* fp;
1629 char buf[8];
1630 memset(buf, 'x', sizeof(buf));
1631 strcpy(buf, "str");
1632 ASSERT_NE(nullptr, fp = fmemopen(buf, sizeof(buf), "w+"));
1633 ASSERT_NE(EOF, fputs("string", fp));
1634 EXPECT_EQ(0, fseek(fp, 0, SEEK_END));
1635 EXPECT_EQ(static_cast<long>(strlen("string")), ftell(fp));
1636 EXPECT_EQ(static_cast<off_t>(strlen("string")), ftello(fp));
1637 EXPECT_EQ(0, fclose(fp));
1638
1639 // glibc < 2.22 interpreted SEEK_END the wrong way round (subtracting rather
1640 // than adding).
1641 ASSERT_NE(nullptr, fp = fmemopen(buf, sizeof(buf), "w+"));
1642 ASSERT_NE(EOF, fputs("54321", fp));
1643 EXPECT_EQ(0, fseek(fp, -2, SEEK_END));
1644 EXPECT_EQ('2', fgetc(fp));
1645 EXPECT_EQ(0, fclose(fp));
1646}
1647
1648TEST(STDIO_TEST, fmemopen_seek_invalid) {
1649 char buf[8];
1650 memset(buf, 'x', sizeof(buf));
1651 FILE* fp = fmemopen(buf, sizeof(buf), "w");
1652 ASSERT_TRUE(fp != nullptr);
1653
1654 // POSIX: "An attempt to seek ... to a negative position or to a position
1655 // larger than the buffer size given in the size argument shall fail."
1656 // (There's no mention of what errno should be set to, and glibc doesn't
1657 // set errno in any of these cases.)
1658 EXPECT_EQ(-1, fseek(fp, -2, SEEK_SET));
1659 EXPECT_EQ(-1, fseeko(fp, -2, SEEK_SET));
1660 EXPECT_EQ(-1, fseek(fp, sizeof(buf) + 1, SEEK_SET));
1661 EXPECT_EQ(-1, fseeko(fp, sizeof(buf) + 1, SEEK_SET));
1662}
1663
1664TEST(STDIO_TEST, fmemopen_read_EOF) {
1665 // POSIX: "A read operation on the stream shall not advance the current
1666 // buffer position beyond the current buffer size."
1667 char buf[8];
1668 memset(buf, 'x', sizeof(buf));
1669 FILE* fp = fmemopen(buf, sizeof(buf), "r");
1670 ASSERT_TRUE(fp != nullptr);
1671 char buf2[BUFSIZ];
1672 ASSERT_EQ(8U, fread(buf2, 1, sizeof(buf2), fp));
1673 // POSIX: "Reaching the buffer size in a read operation shall count as
1674 // end-of-file.
1675 ASSERT_TRUE(feof(fp));
1676 ASSERT_EQ(EOF, fgetc(fp));
1677 ASSERT_EQ(0, fclose(fp));
1678}
1679
1680TEST(STDIO_TEST, fmemopen_read_null_bytes) {
1681 // POSIX: "Null bytes in the buffer shall have no special meaning for reads."
1682 char buf[] = "h\0e\0l\0l\0o";
1683 FILE* fp = fmemopen(buf, sizeof(buf), "r");
1684 ASSERT_TRUE(fp != nullptr);
1685 ASSERT_EQ('h', fgetc(fp));
1686 ASSERT_EQ(0, fgetc(fp));
1687 ASSERT_EQ('e', fgetc(fp));
1688 ASSERT_EQ(0, fgetc(fp));
1689 ASSERT_EQ('l', fgetc(fp));
1690 ASSERT_EQ(0, fgetc(fp));
1691 // POSIX: "The read operation shall start at the current buffer position of
1692 // the stream."
1693 char buf2[8];
1694 memset(buf2, 'x', sizeof(buf2));
1695 ASSERT_EQ(4U, fread(buf2, 1, sizeof(buf2), fp));
1696 ASSERT_EQ('l', buf2[0]);
1697 ASSERT_EQ(0, buf2[1]);
1698 ASSERT_EQ('o', buf2[2]);
1699 ASSERT_EQ(0, buf2[3]);
1700 for (size_t i = 4; i < sizeof(buf2); ++i) ASSERT_EQ('x', buf2[i]) << i;
1701 ASSERT_TRUE(feof(fp));
1702 ASSERT_EQ(0, fclose(fp));
1703}
1704
1705TEST(STDIO_TEST, fmemopen_write) {
1706 FILE* fp;
1707 char buf[8];
1708
1709 // POSIX: "A write operation shall start either at the current position of
1710 // the stream (if mode has not specified 'a' as the first character)..."
1711 memset(buf, 'x', sizeof(buf));
1712 ASSERT_NE(nullptr, fp = fmemopen(buf, sizeof(buf), "r+"));
1713 setbuf(fp, nullptr); // Turn off buffering so we can see what's happening as it happens.
1714 ASSERT_EQ(0, fseek(fp, 2, SEEK_SET));
1715 ASSERT_EQ(' ', fputc(' ', fp));
1716 EXPECT_EQ("xx xxxxx", std::string(buf, buf + sizeof(buf)));
1717 ASSERT_EQ(0, fclose(fp));
1718
1719 // "...or at the current size of the stream (if mode had 'a' as the first
1720 // character)." (See the fmemopen_size test for what "size" means, but for
1721 // mode "a", it's the first NUL byte.)
1722 memset(buf, 'x', sizeof(buf));
1723 buf[3] = '\0';
1724 ASSERT_NE(nullptr, fp = fmemopen(buf, sizeof(buf), "a+"));
1725 setbuf(fp, nullptr); // Turn off buffering so we can see what's happening as it happens.
1726 ASSERT_EQ(' ', fputc(' ', fp));
1727 EXPECT_EQ("xxx \0xxx"s, std::string(buf, buf + sizeof(buf)));
1728 ASSERT_EQ(0, fclose(fp));
1729
1730 // "If the current position at the end of the write is larger than the
1731 // current buffer size, the current buffer size shall be set to the current
1732 // position." (See the fmemopen_size test for what "size" means, but to
1733 // query it we SEEK_END with offset 0, and then ftell.)
1734 memset(buf, 'x', sizeof(buf));
1735 ASSERT_NE(nullptr, fp = fmemopen(buf, sizeof(buf), "w+"));
1736 setbuf(fp, nullptr); // Turn off buffering so we can see what's happening as it happens.
1737 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
1738 EXPECT_EQ(0, ftell(fp));
1739 ASSERT_EQ(' ', fputc(' ', fp));
1740 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
1741 EXPECT_EQ(1, ftell(fp));
1742 ASSERT_NE(EOF, fputs("123", fp));
1743 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
1744 EXPECT_EQ(4, ftell(fp));
1745 EXPECT_EQ(" 123\0xxx"s, std::string(buf, buf + sizeof(buf)));
1746 ASSERT_EQ(0, fclose(fp));
1747}
1748
1749TEST(STDIO_TEST, fmemopen_write_EOF) {
1750 // POSIX: "A write operation on the stream shall not advance the current
1751 // buffer size beyond the size given in the size argument."
1752 FILE* fp;
1753
1754 // Scalar writes...
1755 ASSERT_NE(nullptr, fp = fmemopen(nullptr, 4, "w"));
1756 setbuf(fp, nullptr); // Turn off buffering so we can see what's happening as it happens.
1757 ASSERT_EQ('x', fputc('x', fp));
1758 ASSERT_EQ('x', fputc('x', fp));
1759 ASSERT_EQ('x', fputc('x', fp));
1760 ASSERT_EQ(EOF, fputc('x', fp)); // Only 3 fit because of the implicit NUL.
1761 ASSERT_EQ(0, fclose(fp));
1762
1763 // Vector writes...
1764 ASSERT_NE(nullptr, fp = fmemopen(nullptr, 4, "w"));
1765 setbuf(fp, nullptr); // Turn off buffering so we can see what's happening as it happens.
1766 ASSERT_EQ(3U, fwrite("xxxx", 1, 4, fp));
1767 ASSERT_EQ(0, fclose(fp));
1768}
1769
1770TEST(STDIO_TEST, fmemopen_initial_position) {
1771 // POSIX: "The ... current position in the buffer ... shall be initially
1772 // set to either the beginning of the buffer (for r and w modes) ..."
1773 char buf[] = "hello\0world";
1774 FILE* fp;
1775 ASSERT_NE(nullptr, fp = fmemopen(buf, sizeof(buf), "r"));
1776 EXPECT_EQ(0L, ftell(fp));
1777 EXPECT_EQ(0, fclose(fp));
1778 ASSERT_NE(nullptr, fp = fmemopen(buf, sizeof(buf), "w"));
1779 EXPECT_EQ(0L, ftell(fp));
1780 EXPECT_EQ(0, fclose(fp));
1781 buf[0] = 'h'; // (Undo the effects of the above.)
1782
1783 // POSIX: "...or to the first null byte in the buffer (for a modes)."
1784 ASSERT_NE(nullptr, fp = fmemopen(buf, sizeof(buf), "a"));
1785 EXPECT_EQ(5L, ftell(fp));
1786 EXPECT_EQ(0, fclose(fp));
1787
1788 // POSIX: "If no null byte is found in append mode, the initial position
1789 // shall be set to one byte after the end of the buffer."
1790 memset(buf, 'x', sizeof(buf));
1791 ASSERT_NE(nullptr, fp = fmemopen(buf, sizeof(buf), "a"));
1792 EXPECT_EQ(static_cast<long>(sizeof(buf)), ftell(fp));
1793 EXPECT_EQ(0, fclose(fp));
1794}
1795
1796TEST(STDIO_TEST, fmemopen_initial_position_allocated) {
1797 // POSIX: "If buf is a null pointer, the initial position shall always be
1798 // set to the beginning of the buffer."
1799 FILE* fp = fmemopen(nullptr, 128, "a+");
1800 ASSERT_TRUE(fp != nullptr);
1801 EXPECT_EQ(0L, ftell(fp));
1802 EXPECT_EQ(0L, fseek(fp, 0, SEEK_SET));
1803 EXPECT_EQ(0, fclose(fp));
1804}
1805
1806TEST(STDIO_TEST, fmemopen_zero_length) {
1807 // POSIX says it's up to the implementation whether or not you can have a
1808 // zero-length buffer (but "A future version of this standard may require
1809 // support of zero-length buffer streams explicitly"). BSD and glibc < 2.22
1810 // agreed that you couldn't, but glibc >= 2.22 allows it for consistency.
1811 FILE* fp;
1812 char buf[16];
1813 ASSERT_NE(nullptr, fp = fmemopen(buf, 0, "r+"));
1814 ASSERT_EQ(EOF, fgetc(fp));
1815 ASSERT_TRUE(feof(fp));
1816 ASSERT_EQ(0, fclose(fp));
1817 ASSERT_NE(nullptr, fp = fmemopen(nullptr, 0, "r+"));
1818 ASSERT_EQ(EOF, fgetc(fp));
1819 ASSERT_TRUE(feof(fp));
1820 ASSERT_EQ(0, fclose(fp));
1821
1822 ASSERT_NE(nullptr, fp = fmemopen(buf, 0, "w+"));
1823 setbuf(fp, nullptr); // Turn off buffering so we can see what's happening as it happens.
1824 ASSERT_EQ(EOF, fputc('x', fp));
1825 ASSERT_EQ(0, fclose(fp));
1826 ASSERT_NE(nullptr, fp = fmemopen(nullptr, 0, "w+"));
1827 setbuf(fp, nullptr); // Turn off buffering so we can see what's happening as it happens.
1828 ASSERT_EQ(EOF, fputc('x', fp));
1829 ASSERT_EQ(0, fclose(fp));
1830}
1831
Elliott Hughes288465d2019-02-05 15:00:13 -08001832TEST(STDIO_TEST, fmemopen_zero_length_buffer_overrun) {
1833 char buf[2] = "x";
1834 ASSERT_EQ('x', buf[0]);
1835 FILE* fp = fmemopen(buf, 0, "w");
1836 ASSERT_EQ('x', buf[0]);
1837 ASSERT_EQ(0, fclose(fp));
1838}
1839
Elliott Hughes3a4c4542017-07-19 17:20:24 -07001840TEST(STDIO_TEST, fmemopen_write_only_allocated) {
1841 // POSIX says fmemopen "may fail if the mode argument does not include a '+'".
1842 // BSD fails, glibc doesn't. We side with the more lenient.
1843 FILE* fp;
1844 ASSERT_NE(nullptr, fp = fmemopen(nullptr, 16, "r"));
1845 ASSERT_EQ(0, fclose(fp));
1846 ASSERT_NE(nullptr, fp = fmemopen(nullptr, 16, "w"));
1847 ASSERT_EQ(0, fclose(fp));
1848}
1849
1850TEST(STDIO_TEST, fmemopen_fileno) {
1851 // There's no fd backing an fmemopen FILE*.
1852 FILE* fp = fmemopen(nullptr, 16, "r");
1853 ASSERT_TRUE(fp != nullptr);
Elliott Hughes6b841db2014-08-20 16:10:49 -07001854 errno = 0;
Elliott Hughes3a4c4542017-07-19 17:20:24 -07001855 ASSERT_EQ(-1, fileno(fp));
1856 ASSERT_EQ(EBADF, errno);
1857 ASSERT_EQ(0, fclose(fp));
1858}
1859
1860TEST(STDIO_TEST, fmemopen_append_after_seek) {
1861 // In BSD and glibc < 2.22, append mode didn't force writes to append if
1862 // there had been an intervening seek.
1863
1864 FILE* fp;
1865 char buf[] = "hello\0world";
1866 ASSERT_NE(nullptr, fp = fmemopen(buf, sizeof(buf), "a"));
1867 setbuf(fp, nullptr); // Turn off buffering so we can see what's happening as it happens.
1868 ASSERT_EQ(0, fseek(fp, 0, SEEK_SET));
1869 ASSERT_NE(EOF, fputc('!', fp));
1870 EXPECT_EQ("hello!\0orld\0"s, std::string(buf, buf + sizeof(buf)));
1871 ASSERT_EQ(0, fclose(fp));
1872
1873 memcpy(buf, "hello\0world", sizeof(buf));
1874 ASSERT_NE(nullptr, fp = fmemopen(buf, sizeof(buf), "a+"));
1875 setbuf(fp, nullptr); // Turn off buffering so we can see what's happening as it happens.
1876 ASSERT_EQ(0, fseek(fp, 0, SEEK_SET));
1877 ASSERT_NE(EOF, fputc('!', fp));
1878 EXPECT_EQ("hello!\0orld\0"s, std::string(buf, buf + sizeof(buf)));
1879 ASSERT_EQ(0, fclose(fp));
Elliott Hughes6b841db2014-08-20 16:10:49 -07001880}
1881
Christopher Ferris13f26a72016-01-13 13:47:58 -08001882TEST(STDIO_TEST, open_memstream) {
Elliott Hughes6b841db2014-08-20 16:10:49 -07001883 char* p = nullptr;
1884 size_t size = 0;
1885 FILE* fp = open_memstream(&p, &size);
1886 ASSERT_NE(EOF, fputs("hello, world!", fp));
1887 fclose(fp);
1888
1889 ASSERT_STREQ("hello, world!", p);
1890 ASSERT_EQ(strlen("hello, world!"), size);
1891 free(p);
1892}
1893
Christopher Ferris13f26a72016-01-13 13:47:58 -08001894TEST(STDIO_TEST, open_memstream_EINVAL) {
Elliott Hughes6b841db2014-08-20 16:10:49 -07001895#if defined(__BIONIC__)
1896 char* p;
1897 size_t size;
1898
1899 // Invalid buffer.
1900 errno = 0;
1901 ASSERT_EQ(nullptr, open_memstream(nullptr, &size));
1902 ASSERT_EQ(EINVAL, errno);
1903
1904 // Invalid size.
1905 errno = 0;
1906 ASSERT_EQ(nullptr, open_memstream(&p, nullptr));
1907 ASSERT_EQ(EINVAL, errno);
1908#else
Elliott Hughesbcaa4542019-03-08 15:20:23 -08001909 GTEST_SKIP() << "glibc is broken";
Elliott Hughes6b841db2014-08-20 16:10:49 -07001910#endif
1911}
Elliott Hughes31165ed2014-09-23 17:34:29 -07001912
Christopher Ferris13f26a72016-01-13 13:47:58 -08001913TEST(STDIO_TEST, fdopen_CLOEXEC) {
Elliott Hughes31165ed2014-09-23 17:34:29 -07001914 int fd = open("/proc/version", O_RDONLY);
1915 ASSERT_TRUE(fd != -1);
1916
1917 // This fd doesn't have O_CLOEXEC...
Elliott Hughesa7f12942017-12-15 13:55:53 -08001918 AssertCloseOnExec(fd, false);
Elliott Hughes31165ed2014-09-23 17:34:29 -07001919
1920 FILE* fp = fdopen(fd, "re");
Yi Kong32bc0fc2018-08-02 17:31:13 -07001921 ASSERT_TRUE(fp != nullptr);
Elliott Hughes31165ed2014-09-23 17:34:29 -07001922
1923 // ...but the new one does.
Elliott Hughesa7f12942017-12-15 13:55:53 -08001924 AssertCloseOnExec(fileno(fp), true);
Elliott Hughes31165ed2014-09-23 17:34:29 -07001925
1926 fclose(fp);
Elliott Hughes31165ed2014-09-23 17:34:29 -07001927}
1928
Christopher Ferris13f26a72016-01-13 13:47:58 -08001929TEST(STDIO_TEST, freopen_CLOEXEC) {
Elliott Hughes31165ed2014-09-23 17:34:29 -07001930 FILE* fp = fopen("/proc/version", "r");
Yi Kong32bc0fc2018-08-02 17:31:13 -07001931 ASSERT_TRUE(fp != nullptr);
Elliott Hughes31165ed2014-09-23 17:34:29 -07001932
1933 // This FILE* doesn't have O_CLOEXEC...
Elliott Hughesa7f12942017-12-15 13:55:53 -08001934 AssertCloseOnExec(fileno(fp), false);
Elliott Hughes31165ed2014-09-23 17:34:29 -07001935
1936 fp = freopen("/proc/version", "re", fp);
1937
1938 // ...but the new one does.
Elliott Hughesa7f12942017-12-15 13:55:53 -08001939 AssertCloseOnExec(fileno(fp), true);
Elliott Hughes31165ed2014-09-23 17:34:29 -07001940
1941 fclose(fp);
1942}
Elliott Hughes20841a12014-12-01 16:13:30 -08001943
Elliott Hughesf226ee52016-02-03 11:24:28 -08001944TEST(STDIO_TEST, fopen64_freopen64) {
1945 FILE* fp = fopen64("/proc/version", "r");
1946 ASSERT_TRUE(fp != nullptr);
1947 fp = freopen64("/proc/version", "re", fp);
1948 ASSERT_TRUE(fp != nullptr);
1949 fclose(fp);
1950}
1951
Elliott Hughes20841a12014-12-01 16:13:30 -08001952// https://code.google.com/p/android/issues/detail?id=81155
1953// http://b/18556607
Christopher Ferris13f26a72016-01-13 13:47:58 -08001954TEST(STDIO_TEST, fread_unbuffered_pathological_performance) {
Elliott Hughes20841a12014-12-01 16:13:30 -08001955 FILE* fp = fopen("/dev/zero", "r");
Yi Kong32bc0fc2018-08-02 17:31:13 -07001956 ASSERT_TRUE(fp != nullptr);
Elliott Hughes20841a12014-12-01 16:13:30 -08001957
1958 // Make this stream unbuffered.
Yi Kong32bc0fc2018-08-02 17:31:13 -07001959 setvbuf(fp, nullptr, _IONBF, 0);
Elliott Hughes20841a12014-12-01 16:13:30 -08001960
1961 char buf[65*1024];
1962 memset(buf, 0xff, sizeof(buf));
1963
Yi Kong32bc0fc2018-08-02 17:31:13 -07001964 time_t t0 = time(nullptr);
Elliott Hughes20841a12014-12-01 16:13:30 -08001965 for (size_t i = 0; i < 1024; ++i) {
Elliott Hughese6bb5a22015-01-23 17:48:15 -08001966 ASSERT_EQ(1U, fread(buf, 64*1024, 1, fp));
Elliott Hughes20841a12014-12-01 16:13:30 -08001967 }
Yi Kong32bc0fc2018-08-02 17:31:13 -07001968 time_t t1 = time(nullptr);
Elliott Hughes20841a12014-12-01 16:13:30 -08001969
1970 fclose(fp);
1971
1972 // 1024 64KiB reads should have been very quick.
1973 ASSERT_LE(t1 - t0, 1);
1974
1975 for (size_t i = 0; i < 64*1024; ++i) {
1976 ASSERT_EQ('\0', buf[i]);
1977 }
1978 for (size_t i = 64*1024; i < 65*1024; ++i) {
1979 ASSERT_EQ('\xff', buf[i]);
1980 }
1981}
Elliott Hughes75b99382015-01-20 11:23:50 -08001982
Christopher Ferris13f26a72016-01-13 13:47:58 -08001983TEST(STDIO_TEST, fread_EOF) {
Elliott Hughes0ed7e082015-01-22 15:13:38 -08001984 std::string digits("0123456789");
1985 FILE* fp = fmemopen(&digits[0], digits.size(), "r");
Elliott Hughes75b99382015-01-20 11:23:50 -08001986
1987 // Try to read too much, but little enough that it still fits in the FILE's internal buffer.
1988 char buf1[4 * 4];
1989 memset(buf1, 0, sizeof(buf1));
1990 ASSERT_EQ(2U, fread(buf1, 4, 4, fp));
Elliott Hughes0ed7e082015-01-22 15:13:38 -08001991 ASSERT_STREQ("0123456789", buf1);
Elliott Hughes75b99382015-01-20 11:23:50 -08001992 ASSERT_TRUE(feof(fp));
1993
1994 rewind(fp);
1995
Elliott Hughes0ed7e082015-01-22 15:13:38 -08001996 // Try to read way too much so stdio tries to read more direct from the stream.
1997 char buf2[4 * 4096];
Elliott Hughes75b99382015-01-20 11:23:50 -08001998 memset(buf2, 0, sizeof(buf2));
1999 ASSERT_EQ(2U, fread(buf2, 4, 4096, fp));
Elliott Hughes0ed7e082015-01-22 15:13:38 -08002000 ASSERT_STREQ("0123456789", buf2);
Elliott Hughes75b99382015-01-20 11:23:50 -08002001 ASSERT_TRUE(feof(fp));
2002
2003 fclose(fp);
2004}
Elliott Hughese6bb5a22015-01-23 17:48:15 -08002005
2006static void test_fread_from_write_only_stream(size_t n) {
2007 FILE* fp = fopen("/dev/null", "w");
2008 std::vector<char> buf(n, 0);
2009 errno = 0;
2010 ASSERT_EQ(0U, fread(&buf[0], n, 1, fp));
2011 ASSERT_EQ(EBADF, errno);
2012 ASSERT_TRUE(ferror(fp));
2013 ASSERT_FALSE(feof(fp));
2014 fclose(fp);
2015}
2016
Christopher Ferris13f26a72016-01-13 13:47:58 -08002017TEST(STDIO_TEST, fread_from_write_only_stream_slow_path) {
Elliott Hughese6bb5a22015-01-23 17:48:15 -08002018 test_fread_from_write_only_stream(1);
2019}
2020
Christopher Ferris13f26a72016-01-13 13:47:58 -08002021TEST(STDIO_TEST, fread_from_write_only_stream_fast_path) {
Elliott Hughese6bb5a22015-01-23 17:48:15 -08002022 test_fread_from_write_only_stream(64*1024);
2023}
2024
2025static void test_fwrite_after_fread(size_t n) {
2026 TemporaryFile tf;
2027
2028 FILE* fp = fdopen(tf.fd, "w+");
2029 ASSERT_EQ(1U, fwrite("1", 1, 1, fp));
2030 fflush(fp);
2031
2032 // We've flushed but not rewound, so there's nothing to read.
2033 std::vector<char> buf(n, 0);
2034 ASSERT_EQ(0U, fread(&buf[0], 1, buf.size(), fp));
2035 ASSERT_TRUE(feof(fp));
2036
2037 // But hitting EOF doesn't prevent us from writing...
2038 errno = 0;
Elliott Hughes9677fab2016-01-25 15:50:59 -08002039 ASSERT_EQ(1U, fwrite("2", 1, 1, fp)) << strerror(errno);
Elliott Hughese6bb5a22015-01-23 17:48:15 -08002040
2041 // And if we rewind, everything's there.
2042 rewind(fp);
2043 ASSERT_EQ(2U, fread(&buf[0], 1, buf.size(), fp));
2044 ASSERT_EQ('1', buf[0]);
2045 ASSERT_EQ('2', buf[1]);
2046
2047 fclose(fp);
2048}
2049
Christopher Ferris13f26a72016-01-13 13:47:58 -08002050TEST(STDIO_TEST, fwrite_after_fread_slow_path) {
Elliott Hughese6bb5a22015-01-23 17:48:15 -08002051 test_fwrite_after_fread(16);
2052}
2053
Christopher Ferris13f26a72016-01-13 13:47:58 -08002054TEST(STDIO_TEST, fwrite_after_fread_fast_path) {
Elliott Hughese6bb5a22015-01-23 17:48:15 -08002055 test_fwrite_after_fread(64*1024);
2056}
Christopher Ferriscc9ca102015-02-27 18:22:45 -08002057
2058// http://b/19172514
Christopher Ferris13f26a72016-01-13 13:47:58 -08002059TEST(STDIO_TEST, fread_after_fseek) {
Christopher Ferriscc9ca102015-02-27 18:22:45 -08002060 TemporaryFile tf;
2061
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -08002062 FILE* fp = fopen(tf.path, "w+");
Christopher Ferriscc9ca102015-02-27 18:22:45 -08002063 ASSERT_TRUE(fp != nullptr);
2064
2065 char file_data[12288];
2066 for (size_t i = 0; i < 12288; i++) {
2067 file_data[i] = i;
2068 }
2069 ASSERT_EQ(12288U, fwrite(file_data, 1, 12288, fp));
2070 fclose(fp);
2071
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -08002072 fp = fopen(tf.path, "r");
Christopher Ferriscc9ca102015-02-27 18:22:45 -08002073 ASSERT_TRUE(fp != nullptr);
2074
2075 char buffer[8192];
2076 size_t cur_location = 0;
2077 // Small read to populate internal buffer.
2078 ASSERT_EQ(100U, fread(buffer, 1, 100, fp));
2079 ASSERT_EQ(memcmp(file_data, buffer, 100), 0);
2080
2081 cur_location = static_cast<size_t>(ftell(fp));
2082 // Large read to force reading into the user supplied buffer and bypassing
2083 // the internal buffer.
2084 ASSERT_EQ(8192U, fread(buffer, 1, 8192, fp));
2085 ASSERT_EQ(memcmp(file_data+cur_location, buffer, 8192), 0);
2086
2087 // Small backwards seek to verify fseek does not reuse the internal buffer.
Elliott Hughes9677fab2016-01-25 15:50:59 -08002088 ASSERT_EQ(0, fseek(fp, -22, SEEK_CUR)) << strerror(errno);
Christopher Ferriscc9ca102015-02-27 18:22:45 -08002089 cur_location = static_cast<size_t>(ftell(fp));
2090 ASSERT_EQ(22U, fread(buffer, 1, 22, fp));
2091 ASSERT_EQ(memcmp(file_data+cur_location, buffer, 22), 0);
2092
2093 fclose(fp);
2094}
Elliott Hughes8ab433d2015-10-09 17:57:26 -07002095
2096// https://code.google.com/p/android/issues/detail?id=184847
Christopher Ferris13f26a72016-01-13 13:47:58 -08002097TEST(STDIO_TEST, fread_EOF_184847) {
Elliott Hughes8ab433d2015-10-09 17:57:26 -07002098 TemporaryFile tf;
2099 char buf[6] = {0};
2100
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -08002101 FILE* fw = fopen(tf.path, "w");
Elliott Hughes8ab433d2015-10-09 17:57:26 -07002102 ASSERT_TRUE(fw != nullptr);
2103
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -08002104 FILE* fr = fopen(tf.path, "r");
Elliott Hughes8ab433d2015-10-09 17:57:26 -07002105 ASSERT_TRUE(fr != nullptr);
2106
2107 fwrite("a", 1, 1, fw);
2108 fflush(fw);
2109 ASSERT_EQ(1U, fread(buf, 1, 1, fr));
2110 ASSERT_STREQ("a", buf);
2111
2112 // 'fr' is now at EOF.
2113 ASSERT_EQ(0U, fread(buf, 1, 1, fr));
2114 ASSERT_TRUE(feof(fr));
2115
2116 // Write some more...
2117 fwrite("z", 1, 1, fw);
2118 fflush(fw);
2119
2120 // ...and check that we can read it back.
2121 // (BSD thinks that once a stream has hit EOF, it must always return EOF. SysV disagrees.)
2122 ASSERT_EQ(1U, fread(buf, 1, 1, fr));
2123 ASSERT_STREQ("z", buf);
2124
2125 // But now we're done.
2126 ASSERT_EQ(0U, fread(buf, 1, 1, fr));
2127
2128 fclose(fr);
2129 fclose(fw);
2130}
Elliott Hughes923f1652016-01-19 15:46:05 -08002131
2132TEST(STDIO_TEST, fclose_invalidates_fd) {
2133 // The typical error we're trying to help people catch involves accessing
2134 // memory after it's been freed. But we know that stdin/stdout/stderr are
2135 // special and don't get deallocated, so this test uses stdin.
2136 ASSERT_EQ(0, fclose(stdin));
2137
2138 // Even though using a FILE* after close is undefined behavior, I've closed
2139 // this bug as "WAI" too many times. We shouldn't hand out stale fds,
2140 // especially because they might actually correspond to a real stream.
2141 errno = 0;
2142 ASSERT_EQ(-1, fileno(stdin));
2143 ASSERT_EQ(EBADF, errno);
2144}
Elliott Hughes2704bd12016-01-20 17:14:53 -08002145
2146TEST(STDIO_TEST, fseek_ftell_unseekable) {
2147#if defined(__BIONIC__) // glibc has fopencookie instead.
2148 auto read_fn = [](void*, char*, int) { return -1; };
2149 FILE* fp = funopen(nullptr, read_fn, nullptr, nullptr, nullptr);
2150 ASSERT_TRUE(fp != nullptr);
2151
2152 // Check that ftell balks on an unseekable FILE*.
2153 errno = 0;
2154 ASSERT_EQ(-1, ftell(fp));
2155 ASSERT_EQ(ESPIPE, errno);
2156
2157 // SEEK_CUR is rewritten as SEEK_SET internally...
2158 errno = 0;
2159 ASSERT_EQ(-1, fseek(fp, 0, SEEK_CUR));
2160 ASSERT_EQ(ESPIPE, errno);
2161
2162 // ...so it's worth testing the direct seek path too.
2163 errno = 0;
2164 ASSERT_EQ(-1, fseek(fp, 0, SEEK_SET));
2165 ASSERT_EQ(ESPIPE, errno);
2166
2167 fclose(fp);
Elliott Hughes03e65eb2016-01-26 14:13:04 -08002168#else
Elliott Hughesbcaa4542019-03-08 15:20:23 -08002169 GTEST_SKIP() << "glibc uses fopencookie instead";
Elliott Hughes03e65eb2016-01-26 14:13:04 -08002170#endif
2171}
2172
2173TEST(STDIO_TEST, funopen_EINVAL) {
2174#if defined(__BIONIC__)
2175 errno = 0;
2176 ASSERT_EQ(nullptr, funopen(nullptr, nullptr, nullptr, nullptr, nullptr));
2177 ASSERT_EQ(EINVAL, errno);
2178#else
Elliott Hughesbcaa4542019-03-08 15:20:23 -08002179 GTEST_SKIP() << "glibc uses fopencookie instead";
Elliott Hughes03e65eb2016-01-26 14:13:04 -08002180#endif
2181}
2182
2183TEST(STDIO_TEST, funopen_seek) {
2184#if defined(__BIONIC__)
2185 auto read_fn = [](void*, char*, int) { return -1; };
2186
2187 auto seek_fn = [](void*, fpos_t, int) -> fpos_t { return 0xfedcba12; };
2188 auto seek64_fn = [](void*, fpos64_t, int) -> fpos64_t { return 0xfedcba12345678; };
2189
2190 FILE* fp = funopen(nullptr, read_fn, nullptr, seek_fn, nullptr);
2191 ASSERT_TRUE(fp != nullptr);
2192 fpos_t pos;
Elliott Hughes955426e2016-01-26 18:25:52 -08002193#if defined(__LP64__)
2194 EXPECT_EQ(0, fgetpos(fp, &pos)) << strerror(errno);
2195 EXPECT_EQ(0xfedcba12LL, pos);
2196#else
2197 EXPECT_EQ(-1, fgetpos(fp, &pos)) << strerror(errno);
2198 EXPECT_EQ(EOVERFLOW, errno);
2199#endif
Elliott Hughes03e65eb2016-01-26 14:13:04 -08002200
2201 FILE* fp64 = funopen64(nullptr, read_fn, nullptr, seek64_fn, nullptr);
2202 ASSERT_TRUE(fp64 != nullptr);
2203 fpos64_t pos64;
Elliott Hughes955426e2016-01-26 18:25:52 -08002204 EXPECT_EQ(0, fgetpos64(fp64, &pos64)) << strerror(errno);
2205 EXPECT_EQ(0xfedcba12345678, pos64);
Elliott Hughes03e65eb2016-01-26 14:13:04 -08002206#else
Elliott Hughesbcaa4542019-03-08 15:20:23 -08002207 GTEST_SKIP() << "glibc uses fopencookie instead";
Elliott Hughes2704bd12016-01-20 17:14:53 -08002208#endif
2209}
Elliott Hughes71288cb2016-01-22 19:22:44 -08002210
2211TEST(STDIO_TEST, lots_of_concurrent_files) {
2212 std::vector<TemporaryFile*> tfs;
2213 std::vector<FILE*> fps;
2214
2215 for (size_t i = 0; i < 256; ++i) {
2216 TemporaryFile* tf = new TemporaryFile;
2217 tfs.push_back(tf);
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -08002218 FILE* fp = fopen(tf->path, "w+");
Elliott Hughes71288cb2016-01-22 19:22:44 -08002219 fps.push_back(fp);
2220 fprintf(fp, "hello %zu!\n", i);
2221 fflush(fp);
2222 }
2223
2224 for (size_t i = 0; i < 256; ++i) {
Elliott Hughes71288cb2016-01-22 19:22:44 -08002225 char expected[BUFSIZ];
2226 snprintf(expected, sizeof(expected), "hello %zu!\n", i);
Elliott Hughes71288cb2016-01-22 19:22:44 -08002227
Elliott Hughes70715da2016-08-01 16:35:17 -07002228 AssertFileIs(fps[i], expected);
Elliott Hughes71288cb2016-01-22 19:22:44 -08002229 fclose(fps[i]);
2230 delete tfs[i];
2231 }
2232}
Elliott Hughes9677fab2016-01-25 15:50:59 -08002233
2234static void AssertFileOffsetAt(FILE* fp, off64_t offset) {
2235 EXPECT_EQ(offset, ftell(fp));
2236 EXPECT_EQ(offset, ftello(fp));
Elliott Hughese4fa6e92016-02-02 22:39:15 -08002237 EXPECT_EQ(offset, ftello64(fp));
Elliott Hughes9677fab2016-01-25 15:50:59 -08002238 fpos_t pos;
2239 fpos64_t pos64;
2240 EXPECT_EQ(0, fgetpos(fp, &pos));
2241 EXPECT_EQ(0, fgetpos64(fp, &pos64));
2242#if defined(__BIONIC__)
2243 EXPECT_EQ(offset, static_cast<off64_t>(pos));
2244 EXPECT_EQ(offset, static_cast<off64_t>(pos64));
2245#else
Elliott Hughesbcaa4542019-03-08 15:20:23 -08002246 GTEST_SKIP() << "glibc's fpos_t is opaque";
Elliott Hughes9677fab2016-01-25 15:50:59 -08002247#endif
2248}
2249
2250TEST(STDIO_TEST, seek_tell_family_smoke) {
2251 TemporaryFile tf;
2252 FILE* fp = fdopen(tf.fd, "w+");
2253
2254 // Initially we should be at 0.
2255 AssertFileOffsetAt(fp, 0);
2256
2257 // Seek to offset 8192.
2258 ASSERT_EQ(0, fseek(fp, 8192, SEEK_SET));
2259 AssertFileOffsetAt(fp, 8192);
2260 fpos_t eight_k_pos;
2261 ASSERT_EQ(0, fgetpos(fp, &eight_k_pos));
2262
2263 // Seek forward another 8192...
2264 ASSERT_EQ(0, fseek(fp, 8192, SEEK_CUR));
2265 AssertFileOffsetAt(fp, 8192 + 8192);
2266 fpos64_t sixteen_k_pos64;
2267 ASSERT_EQ(0, fgetpos64(fp, &sixteen_k_pos64));
2268
2269 // Seek back 8192...
2270 ASSERT_EQ(0, fseek(fp, -8192, SEEK_CUR));
2271 AssertFileOffsetAt(fp, 8192);
2272
2273 // Since we haven't written anything, the end is also at 0.
2274 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
2275 AssertFileOffsetAt(fp, 0);
2276
2277 // Check that our fpos64_t from 16KiB works...
2278 ASSERT_EQ(0, fsetpos64(fp, &sixteen_k_pos64));
2279 AssertFileOffsetAt(fp, 8192 + 8192);
2280 // ...as does our fpos_t from 8192.
2281 ASSERT_EQ(0, fsetpos(fp, &eight_k_pos));
2282 AssertFileOffsetAt(fp, 8192);
2283
2284 // Do fseeko and fseeko64 work too?
2285 ASSERT_EQ(0, fseeko(fp, 1234, SEEK_SET));
2286 AssertFileOffsetAt(fp, 1234);
2287 ASSERT_EQ(0, fseeko64(fp, 5678, SEEK_SET));
2288 AssertFileOffsetAt(fp, 5678);
2289
2290 fclose(fp);
2291}
2292
2293TEST(STDIO_TEST, fseek_fseeko_EINVAL) {
2294 TemporaryFile tf;
2295 FILE* fp = fdopen(tf.fd, "w+");
2296
2297 // Bad whence.
2298 errno = 0;
2299 ASSERT_EQ(-1, fseek(fp, 0, 123));
2300 ASSERT_EQ(EINVAL, errno);
2301 errno = 0;
2302 ASSERT_EQ(-1, fseeko(fp, 0, 123));
2303 ASSERT_EQ(EINVAL, errno);
2304 errno = 0;
2305 ASSERT_EQ(-1, fseeko64(fp, 0, 123));
2306 ASSERT_EQ(EINVAL, errno);
2307
2308 // Bad offset.
2309 errno = 0;
2310 ASSERT_EQ(-1, fseek(fp, -1, SEEK_SET));
2311 ASSERT_EQ(EINVAL, errno);
2312 errno = 0;
2313 ASSERT_EQ(-1, fseeko(fp, -1, SEEK_SET));
2314 ASSERT_EQ(EINVAL, errno);
2315 errno = 0;
2316 ASSERT_EQ(-1, fseeko64(fp, -1, SEEK_SET));
2317 ASSERT_EQ(EINVAL, errno);
2318
2319 fclose(fp);
2320}
Elliott Hughes20788ae2016-06-09 15:16:32 -07002321
2322TEST(STDIO_TEST, ctermid) {
2323 ASSERT_STREQ("/dev/tty", ctermid(nullptr));
2324
2325 char buf[L_ctermid] = {};
2326 ASSERT_EQ(buf, ctermid(buf));
2327 ASSERT_STREQ("/dev/tty", buf);
2328}
Elliott Hughesd1f25a72016-08-05 15:53:03 -07002329
2330TEST(STDIO_TEST, remove) {
2331 struct stat sb;
2332
2333 TemporaryFile tf;
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -08002334 ASSERT_EQ(0, remove(tf.path));
2335 ASSERT_EQ(-1, lstat(tf.path, &sb));
Elliott Hughesd1f25a72016-08-05 15:53:03 -07002336 ASSERT_EQ(ENOENT, errno);
2337
2338 TemporaryDir td;
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -08002339 ASSERT_EQ(0, remove(td.path));
2340 ASSERT_EQ(-1, lstat(td.path, &sb));
Elliott Hughesd1f25a72016-08-05 15:53:03 -07002341 ASSERT_EQ(ENOENT, errno);
2342
2343 errno = 0;
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -08002344 ASSERT_EQ(-1, remove(tf.path));
Elliott Hughesd1f25a72016-08-05 15:53:03 -07002345 ASSERT_EQ(ENOENT, errno);
2346
2347 errno = 0;
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -08002348 ASSERT_EQ(-1, remove(td.path));
Elliott Hughesd1f25a72016-08-05 15:53:03 -07002349 ASSERT_EQ(ENOENT, errno);
2350}
Elliott Hughesfb3873d2016-08-10 11:07:54 -07002351
2352TEST(STDIO_DEATHTEST, snprintf_30445072_known_buffer_size) {
2353 char buf[16];
2354 ASSERT_EXIT(snprintf(buf, atol("-1"), "hello"),
2355 testing::KilledBySignal(SIGABRT),
2356#if defined(NOFORTIFY)
2357 "FORTIFY: vsnprintf: size .* > SSIZE_MAX"
2358#else
2359 "FORTIFY: vsnprintf: prevented .*-byte write into 16-byte buffer"
2360#endif
2361 );
2362}
2363
2364TEST(STDIO_DEATHTEST, snprintf_30445072_unknown_buffer_size) {
2365 std::string buf = "world";
2366 ASSERT_EXIT(snprintf(&buf[0], atol("-1"), "hello"),
2367 testing::KilledBySignal(SIGABRT),
2368 "FORTIFY: vsnprintf: size .* > SSIZE_MAX");
2369}
2370
2371TEST(STDIO_TEST, sprintf_30445072) {
2372 std::string buf = "world";
2373 sprintf(&buf[0], "hello");
2374 ASSERT_EQ(buf, "hello");
2375}
Elliott Hughes33a8cb12017-07-25 18:06:46 -07002376
Elliott Hughes654cd832018-08-30 16:00:42 -07002377TEST(STDIO_TEST, printf_m) {
2378 char buf[BUFSIZ];
2379 errno = 0;
2380 snprintf(buf, sizeof(buf), "<%m>");
2381 ASSERT_STREQ("<Success>", buf);
2382 errno = -1;
2383 snprintf(buf, sizeof(buf), "<%m>");
2384 ASSERT_STREQ("<Unknown error -1>", buf);
2385 errno = EINVAL;
2386 snprintf(buf, sizeof(buf), "<%m>");
2387 ASSERT_STREQ("<Invalid argument>", buf);
2388}
2389
Elliott Hughesf340a562018-09-06 10:42:40 -07002390TEST(STDIO_TEST, printf_m_does_not_clobber_strerror) {
2391 char buf[BUFSIZ];
2392 const char* m = strerror(-1);
2393 ASSERT_STREQ("Unknown error -1", m);
2394 errno = -2;
2395 snprintf(buf, sizeof(buf), "<%m>");
2396 ASSERT_STREQ("<Unknown error -2>", buf);
2397 ASSERT_STREQ("Unknown error -1", m);
2398}
2399
Elliott Hughes654cd832018-08-30 16:00:42 -07002400TEST(STDIO_TEST, wprintf_m) {
2401 wchar_t buf[BUFSIZ];
2402 errno = 0;
2403 swprintf(buf, sizeof(buf), L"<%m>");
2404 ASSERT_EQ(std::wstring(L"<Success>"), buf);
2405 errno = -1;
2406 swprintf(buf, sizeof(buf), L"<%m>");
2407 ASSERT_EQ(std::wstring(L"<Unknown error -1>"), buf);
2408 errno = EINVAL;
2409 swprintf(buf, sizeof(buf), L"<%m>");
2410 ASSERT_EQ(std::wstring(L"<Invalid argument>"), buf);
2411}
2412
Elliott Hughesf340a562018-09-06 10:42:40 -07002413TEST(STDIO_TEST, wprintf_m_does_not_clobber_strerror) {
2414 wchar_t buf[BUFSIZ];
2415 const char* m = strerror(-1);
2416 ASSERT_STREQ("Unknown error -1", m);
2417 errno = -2;
2418 swprintf(buf, sizeof(buf), L"<%m>");
2419 ASSERT_EQ(std::wstring(L"<Unknown error -2>"), buf);
2420 ASSERT_STREQ("Unknown error -1", m);
2421}
2422
Elliott Hughes33a8cb12017-07-25 18:06:46 -07002423TEST(STDIO_TEST, fopen_append_mode_and_ftell) {
2424 TemporaryFile tf;
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -08002425 SetFileTo(tf.path, "0123456789");
2426 FILE* fp = fopen(tf.path, "a");
Elliott Hughes33a8cb12017-07-25 18:06:46 -07002427 EXPECT_EQ(10, ftell(fp));
2428 ASSERT_EQ(0, fseek(fp, 2, SEEK_SET));
2429 EXPECT_EQ(2, ftell(fp));
2430 ASSERT_NE(EOF, fputs("xxx", fp));
2431 ASSERT_EQ(0, fflush(fp));
2432 EXPECT_EQ(13, ftell(fp));
2433 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
2434 EXPECT_EQ(13, ftell(fp));
2435 ASSERT_EQ(0, fclose(fp));
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -08002436 AssertFileIs(tf.path, "0123456789xxx");
Elliott Hughes33a8cb12017-07-25 18:06:46 -07002437}
2438
2439TEST(STDIO_TEST, fdopen_append_mode_and_ftell) {
2440 TemporaryFile tf;
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -08002441 SetFileTo(tf.path, "0123456789");
2442 int fd = open(tf.path, O_RDWR);
Elliott Hughes33a8cb12017-07-25 18:06:46 -07002443 ASSERT_NE(-1, fd);
2444 // POSIX: "The file position indicator associated with the new stream is set to the position
2445 // indicated by the file offset associated with the file descriptor."
2446 ASSERT_EQ(4, lseek(fd, 4, SEEK_SET));
2447 FILE* fp = fdopen(fd, "a");
2448 EXPECT_EQ(4, ftell(fp));
2449 ASSERT_EQ(0, fseek(fp, 2, SEEK_SET));
2450 EXPECT_EQ(2, ftell(fp));
2451 ASSERT_NE(EOF, fputs("xxx", fp));
2452 ASSERT_EQ(0, fflush(fp));
2453 EXPECT_EQ(13, ftell(fp));
2454 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
2455 EXPECT_EQ(13, ftell(fp));
2456 ASSERT_EQ(0, fclose(fp));
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -08002457 AssertFileIs(tf.path, "0123456789xxx");
Elliott Hughes33a8cb12017-07-25 18:06:46 -07002458}
2459
2460TEST(STDIO_TEST, freopen_append_mode_and_ftell) {
2461 TemporaryFile tf;
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -08002462 SetFileTo(tf.path, "0123456789");
Elliott Hughes33a8cb12017-07-25 18:06:46 -07002463 FILE* other_fp = fopen("/proc/version", "r");
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -08002464 FILE* fp = freopen(tf.path, "a", other_fp);
Elliott Hughes33a8cb12017-07-25 18:06:46 -07002465 EXPECT_EQ(10, ftell(fp));
2466 ASSERT_EQ(0, fseek(fp, 2, SEEK_SET));
2467 EXPECT_EQ(2, ftell(fp));
2468 ASSERT_NE(EOF, fputs("xxx", fp));
2469 ASSERT_EQ(0, fflush(fp));
2470 EXPECT_EQ(13, ftell(fp));
2471 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
2472 EXPECT_EQ(13, ftell(fp));
2473 ASSERT_EQ(0, fclose(fp));
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -08002474 AssertFileIs(tf.path, "0123456789xxx");
Elliott Hughes33a8cb12017-07-25 18:06:46 -07002475}
Elliott Hughesb15feb72017-07-31 17:20:18 -07002476
2477TEST(STDIO_TEST, constants) {
2478 ASSERT_LE(FILENAME_MAX, PATH_MAX);
2479 ASSERT_EQ(L_tmpnam, PATH_MAX);
2480}
Elliott Hughes37ad9592017-10-30 17:47:12 -07002481
2482TEST(STDIO_TEST, perror) {
2483 ExecTestHelper eth;
2484 eth.Run([&]() { errno = EINVAL; perror("a b c"); exit(0); }, 0, "a b c: Invalid argument\n");
2485 eth.Run([&]() { errno = EINVAL; perror(nullptr); exit(0); }, 0, "Invalid argument\n");
2486 eth.Run([&]() { errno = EINVAL; perror(""); exit(0); }, 0, "Invalid argument\n");
2487}
2488
2489TEST(STDIO_TEST, puts) {
2490 ExecTestHelper eth;
2491 eth.Run([&]() { exit(puts("a b c")); }, 0, "a b c\n");
2492}
2493
2494TEST(STDIO_TEST, unlocked) {
2495 TemporaryFile tf;
2496
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -08002497 FILE* fp = fopen(tf.path, "w+");
Elliott Hughes37ad9592017-10-30 17:47:12 -07002498 ASSERT_TRUE(fp != nullptr);
2499
2500 clearerr_unlocked(fp);
2501 ASSERT_FALSE(feof_unlocked(fp));
2502 ASSERT_FALSE(ferror_unlocked(fp));
2503
2504 ASSERT_EQ(fileno(fp), fileno_unlocked(fp));
2505
2506 ASSERT_NE(EOF, putc_unlocked('a', fp));
2507 ASSERT_NE(EOF, putc('b', fp));
2508 ASSERT_NE(EOF, fputc_unlocked('c', fp));
2509 ASSERT_NE(EOF, fputc('d', fp));
2510
2511 rewind(fp);
2512 ASSERT_EQ('a', getc_unlocked(fp));
2513 ASSERT_EQ('b', getc(fp));
2514 ASSERT_EQ('c', fgetc_unlocked(fp));
2515 ASSERT_EQ('d', fgetc(fp));
2516
2517 rewind(fp);
2518 ASSERT_EQ(2U, fwrite_unlocked("AB", 1, 2, fp));
2519 ASSERT_EQ(2U, fwrite("CD", 1, 2, fp));
2520 ASSERT_EQ(0, fflush_unlocked(fp));
2521
2522 rewind(fp);
2523 char buf[BUFSIZ] = {};
2524 ASSERT_EQ(2U, fread_unlocked(&buf[0], 1, 2, fp));
2525 ASSERT_EQ(2U, fread(&buf[2], 1, 2, fp));
2526 ASSERT_STREQ("ABCD", buf);
2527
2528 rewind(fp);
2529 ASSERT_NE(EOF, fputs("hello ", fp));
2530 ASSERT_NE(EOF, fputs_unlocked("world", fp));
2531 ASSERT_NE(EOF, fputc('\n', fp));
2532
2533 rewind(fp);
2534 ASSERT_TRUE(fgets_unlocked(buf, sizeof(buf), fp) != nullptr);
2535 ASSERT_STREQ("hello world\n", buf);
2536
2537 ASSERT_EQ(0, fclose(fp));
2538}
Ryan Prichardbf549862017-11-07 15:30:32 -08002539
2540TEST(STDIO_TEST, fseek_64bit) {
2541 TemporaryFile tf;
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -08002542 FILE* fp = fopen64(tf.path, "w+");
Ryan Prichardbf549862017-11-07 15:30:32 -08002543 ASSERT_TRUE(fp != nullptr);
2544 ASSERT_EQ(0, fseeko64(fp, 0x2'0000'0000, SEEK_SET));
2545 ASSERT_EQ(0x2'0000'0000, ftello64(fp));
2546 ASSERT_EQ(0, fseeko64(fp, 0x1'0000'0000, SEEK_CUR));
2547 ASSERT_EQ(0x3'0000'0000, ftello64(fp));
2548 ASSERT_EQ(0, fclose(fp));
2549}
2550
2551// POSIX requires that fseek/fseeko fail with EOVERFLOW if the new file offset
2552// isn't representable in long/off_t.
2553TEST(STDIO_TEST, fseek_overflow_32bit) {
2554 TemporaryFile tf;
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -08002555 FILE* fp = fopen64(tf.path, "w+");
Ryan Prichardbf549862017-11-07 15:30:32 -08002556 ASSERT_EQ(0, ftruncate64(fileno(fp), 0x2'0000'0000));
2557
2558 // Bionic implements overflow checking for SEEK_CUR, but glibc doesn't.
2559#if defined(__BIONIC__) && !defined(__LP64__)
2560 ASSERT_EQ(0, fseek(fp, 0x7fff'ffff, SEEK_SET));
2561 ASSERT_EQ(-1, fseek(fp, 1, SEEK_CUR));
2562 ASSERT_EQ(EOVERFLOW, errno);
2563#endif
2564
2565 // Neither Bionic nor glibc implement the overflow checking for SEEK_END.
2566 // (Aside: FreeBSD's libc is an example of a libc that checks both SEEK_CUR
2567 // and SEEK_END -- many C libraries check neither.)
2568 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
2569 ASSERT_EQ(0x2'0000'0000, ftello64(fp));
2570
2571 fclose(fp);
2572}
Elliott Hughesf1a38382018-08-22 15:38:07 -07002573
2574TEST(STDIO_TEST, dev_std_files) {
2575 // POSIX only mentions /dev/stdout, but we should have all three (http://b/31824379).
2576 char path[PATH_MAX];
Christopher Ferris2c4ec7e2018-08-23 11:17:55 -07002577 ssize_t length = readlink("/dev/stdin", path, sizeof(path));
2578 ASSERT_LT(0, length);
2579 ASSERT_EQ("/proc/self/fd/0", std::string(path, length));
2580
2581 length = readlink("/dev/stdout", path, sizeof(path));
2582 ASSERT_LT(0, length);
2583 ASSERT_EQ("/proc/self/fd/1", std::string(path, length));
2584
2585 length = readlink("/dev/stderr", path, sizeof(path));
2586 ASSERT_LT(0, length);
2587 ASSERT_EQ("/proc/self/fd/2", std::string(path, length));
Elliott Hughesf1a38382018-08-22 15:38:07 -07002588}
Ryan Prichardc485cdb2019-04-30 14:47:34 -07002589
2590TEST(STDIO_TEST, fread_with_locked_file) {
2591 // Reading an unbuffered/line-buffered file from one thread shouldn't block on
2592 // files locked on other threads, even if it flushes some line-buffered files.
2593 FILE* fp1 = fopen("/dev/zero", "r");
2594 ASSERT_TRUE(fp1 != nullptr);
2595 flockfile(fp1);
2596
2597 std::thread([] {
2598 for (int mode : { _IONBF, _IOLBF }) {
2599 FILE* fp2 = fopen("/dev/zero", "r");
2600 ASSERT_TRUE(fp2 != nullptr);
2601 setvbuf(fp2, nullptr, mode, 0);
2602 ASSERT_EQ('\0', fgetc(fp2));
2603 fclose(fp2);
2604 }
2605 }).join();
2606
2607 funlockfile(fp1);
2608 fclose(fp1);
2609}
Elliott Hughes31c73092019-05-07 10:03:02 -07002610
2611TEST(STDIO_TEST, SEEK_macros) {
2612 ASSERT_EQ(0, SEEK_SET);
2613 ASSERT_EQ(1, SEEK_CUR);
2614 ASSERT_EQ(2, SEEK_END);
2615 ASSERT_EQ(3, SEEK_DATA);
2616 ASSERT_EQ(4, SEEK_HOLE);
2617 // So we'll notice if Linux grows another constant in <linux/fs.h>...
2618 ASSERT_EQ(SEEK_MAX, SEEK_HOLE);
2619}
Elliott Hughes05b675e2019-04-17 13:01:06 -07002620
2621TEST(STDIO_TEST, rename) {
2622 TemporaryDir td;
2623 std::string old_path = td.path + "/old"s;
2624 std::string new_path = td.path + "/new"s;
2625
2626 // Create the file, check it exists.
2627 ASSERT_EQ(0, close(creat(old_path.c_str(), 0666)));
2628 struct stat sb;
2629 ASSERT_EQ(0, stat(old_path.c_str(), &sb));
2630 ASSERT_EQ(-1, stat(new_path.c_str(), &sb));
2631
2632 // Rename and check it moved.
2633 ASSERT_EQ(0, rename(old_path.c_str(), new_path.c_str()));
2634 ASSERT_EQ(-1, stat(old_path.c_str(), &sb));
2635 ASSERT_EQ(0, stat(new_path.c_str(), &sb));
2636}
2637
2638TEST(STDIO_TEST, renameat) {
2639 TemporaryDir td;
2640 android::base::unique_fd dirfd{open(td.path, O_PATH)};
2641 std::string old_path = td.path + "/old"s;
2642 std::string new_path = td.path + "/new"s;
2643
2644 // Create the file, check it exists.
2645 ASSERT_EQ(0, close(creat(old_path.c_str(), 0666)));
2646 struct stat sb;
2647 ASSERT_EQ(0, stat(old_path.c_str(), &sb));
2648 ASSERT_EQ(-1, stat(new_path.c_str(), &sb));
2649
2650 // Rename and check it moved.
2651 ASSERT_EQ(0, renameat(dirfd, "old", dirfd, "new"));
2652 ASSERT_EQ(-1, stat(old_path.c_str(), &sb));
2653 ASSERT_EQ(0, stat(new_path.c_str(), &sb));
2654}
2655
2656TEST(STDIO_TEST, renameat2) {
2657#if defined(__GLIBC__)
2658 GTEST_SKIP() << "glibc doesn't have renameat2 until 2.28";
2659#else
2660 TemporaryDir td;
2661 android::base::unique_fd dirfd{open(td.path, O_PATH)};
2662 std::string old_path = td.path + "/old"s;
2663 std::string new_path = td.path + "/new"s;
2664
2665 // Create the file, check it exists.
2666 ASSERT_EQ(0, close(creat(old_path.c_str(), 0666)));
2667 struct stat sb;
2668 ASSERT_EQ(0, stat(old_path.c_str(), &sb));
2669 ASSERT_EQ(-1, stat(new_path.c_str(), &sb));
2670
2671 // Rename and check it moved.
2672 ASSERT_EQ(0, renameat2(dirfd, "old", dirfd, "new", 0));
2673 ASSERT_EQ(-1, stat(old_path.c_str(), &sb));
2674 ASSERT_EQ(0, stat(new_path.c_str(), &sb));
2675
2676 // After this, both "old" and "new" exist.
2677 ASSERT_EQ(0, close(creat(old_path.c_str(), 0666)));
2678
2679 // Rename and check it moved.
2680 ASSERT_EQ(-1, renameat2(dirfd, "old", dirfd, "new", RENAME_NOREPLACE));
2681 ASSERT_EQ(EEXIST, errno);
2682#endif
2683}
2684
2685TEST(STDIO_TEST, renameat2_flags) {
2686#if defined(__GLIBC__)
2687 GTEST_SKIP() << "glibc doesn't have renameat2 until 2.28";
2688#else
2689 ASSERT_NE(0, RENAME_EXCHANGE);
2690 ASSERT_NE(0, RENAME_NOREPLACE);
2691 ASSERT_NE(0, RENAME_WHITEOUT);
2692#endif
2693}