blob: 65a942c37339f14757d4187484b2163e20b27183 [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>
Elliott Hughese6bb5a22015-01-23 17:48:15 -080032#include <vector>
33
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -080034#include <android-base/file.h>
35
Elliott Hughesfb3873d2016-08-10 11:07:54 -070036#include "BionicDeathTest.h"
Josh Gao2f06e102017-01-10 13:00:37 -080037#include "utils.h"
Elliott Hughes91875dc2012-09-24 17:55:15 -070038
Christopher Ferris13f26a72016-01-13 13:47:58 -080039#if defined(NOFORTIFY)
40#define STDIO_TEST stdio_nofortify
Elliott Hughesfb3873d2016-08-10 11:07:54 -070041#define STDIO_DEATHTEST stdio_nofortify_DeathTest
Christopher Ferris13f26a72016-01-13 13:47:58 -080042#else
43#define STDIO_TEST stdio
Elliott Hughesfb3873d2016-08-10 11:07:54 -070044#define STDIO_DEATHTEST stdio_DeathTest
Christopher Ferris13f26a72016-01-13 13:47:58 -080045#endif
46
Elliott Hughes3a4c4542017-07-19 17:20:24 -070047using namespace std::string_literals;
48
Elliott Hughesfb3873d2016-08-10 11:07:54 -070049class stdio_DeathTest : public BionicDeathTest {};
50class stdio_nofortify_DeathTest : public BionicDeathTest {};
51
Elliott Hughes33a8cb12017-07-25 18:06:46 -070052static void SetFileTo(const char* path, const char* content) {
53 FILE* fp;
54 ASSERT_NE(nullptr, fp = fopen(path, "w"));
55 ASSERT_NE(EOF, fputs(content, fp));
56 ASSERT_EQ(0, fclose(fp));
57}
58
59static void AssertFileIs(const char* path, const char* expected) {
60 FILE* fp;
61 ASSERT_NE(nullptr, fp = fopen(path, "r"));
62 char* line = nullptr;
63 size_t length;
64 ASSERT_NE(EOF, getline(&line, &length, fp));
65 ASSERT_EQ(0, fclose(fp));
66 ASSERT_STREQ(expected, line);
67 free(line);
68}
69
Elliott Hughes70715da2016-08-01 16:35:17 -070070static void AssertFileIs(FILE* fp, const char* expected, bool is_fmemopen = false) {
71 rewind(fp);
72
73 char line[1024];
Josh Gao2f06e102017-01-10 13:00:37 -080074 memset(line, 0xff, sizeof(line));
Elliott Hughes70715da2016-08-01 16:35:17 -070075 ASSERT_EQ(line, fgets(line, sizeof(line), fp));
76 ASSERT_STREQ(expected, line);
77
78 if (is_fmemopen) {
79 // fmemopen appends a trailing NUL byte, which probably shouldn't show up as an
80 // extra empty line, but does on every C library I tested...
81 ASSERT_EQ(line, fgets(line, sizeof(line), fp));
82 ASSERT_STREQ("", line);
83 }
84
85 // Make sure there isn't anything else in the file.
86 ASSERT_EQ(nullptr, fgets(line, sizeof(line), fp)) << "junk at end of file: " << line;
87}
88
Christopher Ferris13f26a72016-01-13 13:47:58 -080089TEST(STDIO_TEST, flockfile_18208568_stderr) {
Elliott Hughes6a03abc2014-11-03 12:32:17 -080090 // Check that we have a _recursive_ mutex for flockfile.
91 flockfile(stderr);
92 feof(stderr); // We don't care about the result, but this needs to take the lock.
93 funlockfile(stderr);
94}
95
Christopher Ferris13f26a72016-01-13 13:47:58 -080096TEST(STDIO_TEST, flockfile_18208568_regular) {
Elliott Hughes6a03abc2014-11-03 12:32:17 -080097 // We never had a bug for streams other than stdin/stdout/stderr, but test anyway.
98 FILE* fp = fopen("/dev/null", "w");
Yi Kong32bc0fc2018-08-02 17:31:13 -070099 ASSERT_TRUE(fp != nullptr);
Elliott Hughes6a03abc2014-11-03 12:32:17 -0800100 flockfile(fp);
101 feof(fp);
102 funlockfile(fp);
103 fclose(fp);
104}
105
Christopher Ferris13f26a72016-01-13 13:47:58 -0800106TEST(STDIO_TEST, tmpfile_fileno_fprintf_rewind_fgets) {
Elliott Hughes91875dc2012-09-24 17:55:15 -0700107 FILE* fp = tmpfile();
Yi Kong32bc0fc2018-08-02 17:31:13 -0700108 ASSERT_TRUE(fp != nullptr);
Elliott Hughes91875dc2012-09-24 17:55:15 -0700109
110 int fd = fileno(fp);
111 ASSERT_NE(fd, -1);
112
113 struct stat sb;
114 int rc = fstat(fd, &sb);
115 ASSERT_NE(rc, -1);
116 ASSERT_EQ(sb.st_mode & 0777, 0600U);
117
118 rc = fprintf(fp, "hello\n");
119 ASSERT_EQ(rc, 6);
120
Elliott Hughes70715da2016-08-01 16:35:17 -0700121 AssertFileIs(fp, "hello\n");
Elliott Hughes91875dc2012-09-24 17:55:15 -0700122 fclose(fp);
123}
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300124
Elliott Hughesf226ee52016-02-03 11:24:28 -0800125TEST(STDIO_TEST, tmpfile64) {
126 FILE* fp = tmpfile64();
127 ASSERT_TRUE(fp != nullptr);
128 fclose(fp);
129}
130
Christopher Ferris13f26a72016-01-13 13:47:58 -0800131TEST(STDIO_TEST, dprintf) {
Calin Juravle6afb2a92014-05-22 11:47:47 +0100132 TemporaryFile tf;
133
134 int rc = dprintf(tf.fd, "hello\n");
135 ASSERT_EQ(rc, 6);
136
Yabin Cui5ca4a9e2014-11-06 19:55:09 -0800137 lseek(tf.fd, 0, SEEK_SET);
Christopher Ferris9e01ea62014-05-29 12:49:35 -0700138 FILE* tfile = fdopen(tf.fd, "r");
Yi Kong32bc0fc2018-08-02 17:31:13 -0700139 ASSERT_TRUE(tfile != nullptr);
Calin Juravle6afb2a92014-05-22 11:47:47 +0100140
Elliott Hughes70715da2016-08-01 16:35:17 -0700141 AssertFileIs(tfile, "hello\n");
Christopher Ferris9e01ea62014-05-29 12:49:35 -0700142 fclose(tfile);
Calin Juravle6afb2a92014-05-22 11:47:47 +0100143}
144
Christopher Ferris13f26a72016-01-13 13:47:58 -0800145TEST(STDIO_TEST, getdelim) {
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300146 FILE* fp = tmpfile();
Yi Kong32bc0fc2018-08-02 17:31:13 -0700147 ASSERT_TRUE(fp != nullptr);
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300148
149 const char* line_written = "This is a test";
150 int rc = fprintf(fp, "%s", line_written);
151 ASSERT_EQ(rc, static_cast<int>(strlen(line_written)));
152
153 rewind(fp);
154
Yi Kong32bc0fc2018-08-02 17:31:13 -0700155 char* word_read = nullptr;
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300156 size_t allocated_length = 0;
157
158 const char* expected[] = { "This ", " ", "is ", "a ", "test" };
159 for (size_t i = 0; i < 5; ++i) {
160 ASSERT_FALSE(feof(fp));
161 ASSERT_EQ(getdelim(&word_read, &allocated_length, ' ', fp), static_cast<int>(strlen(expected[i])));
162 ASSERT_GE(allocated_length, strlen(expected[i]));
Elliott Hughes0ed7e082015-01-22 15:13:38 -0800163 ASSERT_STREQ(expected[i], word_read);
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300164 }
165 // The last read should have set the end-of-file indicator for the stream.
166 ASSERT_TRUE(feof(fp));
167 clearerr(fp);
168
169 // getdelim returns -1 but doesn't set errno if we're already at EOF.
170 // It should set the end-of-file indicator for the stream, though.
171 errno = 0;
172 ASSERT_EQ(getdelim(&word_read, &allocated_length, ' ', fp), -1);
Elliott Hughes5e3fc432013-02-11 16:36:48 -0800173 ASSERT_EQ(0, errno);
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300174 ASSERT_TRUE(feof(fp));
175
176 free(word_read);
177 fclose(fp);
178}
179
Christopher Ferris13f26a72016-01-13 13:47:58 -0800180TEST(STDIO_TEST, getdelim_invalid) {
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300181 FILE* fp = tmpfile();
Yi Kong32bc0fc2018-08-02 17:31:13 -0700182 ASSERT_TRUE(fp != nullptr);
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300183
Yi Kong32bc0fc2018-08-02 17:31:13 -0700184 char* buffer = nullptr;
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300185 size_t buffer_length = 0;
186
187 // The first argument can't be NULL.
188 errno = 0;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700189 ASSERT_EQ(getdelim(nullptr, &buffer_length, ' ', fp), -1);
Elliott Hughes5e3fc432013-02-11 16:36:48 -0800190 ASSERT_EQ(EINVAL, errno);
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300191
192 // The second argument can't be NULL.
193 errno = 0;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700194 ASSERT_EQ(getdelim(&buffer, nullptr, ' ', fp), -1);
Elliott Hughes5e3fc432013-02-11 16:36:48 -0800195 ASSERT_EQ(EINVAL, errno);
Elliott Hughes20f8aec2014-05-12 15:15:37 -0700196 fclose(fp);
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300197}
198
Christopher Ferris13f26a72016-01-13 13:47:58 -0800199TEST(STDIO_TEST, getdelim_directory) {
Elliott Hughes694fd2d2015-04-05 10:51:56 -0700200 FILE* fp = fopen("/proc", "r");
Yi Kong32bc0fc2018-08-02 17:31:13 -0700201 ASSERT_TRUE(fp != nullptr);
Elliott Hughes694fd2d2015-04-05 10:51:56 -0700202 char* word_read;
203 size_t allocated_length;
204 ASSERT_EQ(-1, getdelim(&word_read, &allocated_length, ' ', fp));
205 fclose(fp);
206}
207
Christopher Ferris13f26a72016-01-13 13:47:58 -0800208TEST(STDIO_TEST, getline) {
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300209 FILE* fp = tmpfile();
Yi Kong32bc0fc2018-08-02 17:31:13 -0700210 ASSERT_TRUE(fp != nullptr);
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300211
212 const char* line_written = "This is a test for getline\n";
213 const size_t line_count = 5;
214
215 for (size_t i = 0; i < line_count; ++i) {
216 int rc = fprintf(fp, "%s", line_written);
217 ASSERT_EQ(rc, static_cast<int>(strlen(line_written)));
218 }
219
220 rewind(fp);
221
Yi Kong32bc0fc2018-08-02 17:31:13 -0700222 char* line_read = nullptr;
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300223 size_t allocated_length = 0;
224
225 size_t read_line_count = 0;
226 ssize_t read_char_count;
227 while ((read_char_count = getline(&line_read, &allocated_length, fp)) != -1) {
228 ASSERT_EQ(read_char_count, static_cast<int>(strlen(line_written)));
229 ASSERT_GE(allocated_length, strlen(line_written));
Elliott Hughes0ed7e082015-01-22 15:13:38 -0800230 ASSERT_STREQ(line_written, line_read);
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300231 ++read_line_count;
232 }
233 ASSERT_EQ(read_line_count, line_count);
234
235 // The last read should have set the end-of-file indicator for the stream.
236 ASSERT_TRUE(feof(fp));
237 clearerr(fp);
238
239 // getline returns -1 but doesn't set errno if we're already at EOF.
240 // It should set the end-of-file indicator for the stream, though.
241 errno = 0;
242 ASSERT_EQ(getline(&line_read, &allocated_length, fp), -1);
Elliott Hughes5e3fc432013-02-11 16:36:48 -0800243 ASSERT_EQ(0, errno);
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300244 ASSERT_TRUE(feof(fp));
245
246 free(line_read);
247 fclose(fp);
248}
249
Christopher Ferris13f26a72016-01-13 13:47:58 -0800250TEST(STDIO_TEST, getline_invalid) {
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300251 FILE* fp = tmpfile();
Yi Kong32bc0fc2018-08-02 17:31:13 -0700252 ASSERT_TRUE(fp != nullptr);
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300253
Yi Kong32bc0fc2018-08-02 17:31:13 -0700254 char* buffer = nullptr;
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300255 size_t buffer_length = 0;
256
257 // The first argument can't be NULL.
258 errno = 0;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700259 ASSERT_EQ(getline(nullptr, &buffer_length, fp), -1);
Elliott Hughes5e3fc432013-02-11 16:36:48 -0800260 ASSERT_EQ(EINVAL, errno);
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300261
262 // The second argument can't be NULL.
263 errno = 0;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700264 ASSERT_EQ(getline(&buffer, nullptr, fp), -1);
Elliott Hughes5e3fc432013-02-11 16:36:48 -0800265 ASSERT_EQ(EINVAL, errno);
Elliott Hughes20f8aec2014-05-12 15:15:37 -0700266 fclose(fp);
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300267}
Thorsten Glaserc641caf2013-02-17 16:50:58 +0000268
Christopher Ferris13f26a72016-01-13 13:47:58 -0800269TEST(STDIO_TEST, printf_ssize_t) {
Elliott Hughese2556422013-02-28 10:51:14 -0800270 // http://b/8253769
Elliott Hughese2556422013-02-28 10:51:14 -0800271 ASSERT_EQ(sizeof(ssize_t), sizeof(long int));
Elliott Hughesb6e22482013-03-08 15:28:52 -0800272 ASSERT_EQ(sizeof(ssize_t), sizeof(size_t));
273 // For our 32-bit ABI, we had a ssize_t definition that confuses GCC into saying:
Thorsten Glaserc641caf2013-02-17 16:50:58 +0000274 // error: format '%zd' expects argument of type 'signed size_t',
275 // but argument 4 has type 'ssize_t {aka long int}' [-Werror=format]
276 ssize_t v = 1;
277 char buf[32];
278 snprintf(buf, sizeof(buf), "%zd", v);
279}
Elliott Hughes6b3f49a2013-03-06 16:20:55 -0800280
Elliott Hughes05493712014-04-17 17:30:03 -0700281// https://code.google.com/p/android/issues/detail?id=64886
Christopher Ferris13f26a72016-01-13 13:47:58 -0800282TEST(STDIO_TEST, snprintf_a) {
Elliott Hughes05493712014-04-17 17:30:03 -0700283 char buf[BUFSIZ];
284 EXPECT_EQ(23, snprintf(buf, sizeof(buf), "<%a>", 9990.235));
285 EXPECT_STREQ("<0x1.3831e147ae148p+13>", buf);
286}
287
Christopher Ferris13f26a72016-01-13 13:47:58 -0800288TEST(STDIO_TEST, snprintf_lc) {
Elliott Hughes05493712014-04-17 17:30:03 -0700289 char buf[BUFSIZ];
290 wint_t wc = L'a';
291 EXPECT_EQ(3, snprintf(buf, sizeof(buf), "<%lc>", wc));
292 EXPECT_STREQ("<a>", buf);
293}
294
Elliott Hughes2f9c8ce2017-11-01 13:54:47 -0700295TEST(STDIO_TEST, snprintf_C) { // Synonym for %lc.
296 char buf[BUFSIZ];
297 wchar_t wc = L'a';
298 EXPECT_EQ(3, snprintf(buf, sizeof(buf), "<%C>", wc));
299 EXPECT_STREQ("<a>", buf);
300}
301
Christopher Ferris13f26a72016-01-13 13:47:58 -0800302TEST(STDIO_TEST, snprintf_ls) {
Elliott Hughes05493712014-04-17 17:30:03 -0700303 char buf[BUFSIZ];
Yi Kong32bc0fc2018-08-02 17:31:13 -0700304 wchar_t* ws = nullptr;
Elliott Hughes05493712014-04-17 17:30:03 -0700305 EXPECT_EQ(8, snprintf(buf, sizeof(buf), "<%ls>", ws));
306 EXPECT_STREQ("<(null)>", buf);
307
308 wchar_t chars[] = { L'h', L'i', 0 };
309 ws = chars;
310 EXPECT_EQ(4, snprintf(buf, sizeof(buf), "<%ls>", ws));
311 EXPECT_STREQ("<hi>", buf);
312}
313
Elliott Hughes2f9c8ce2017-11-01 13:54:47 -0700314TEST(STDIO_TEST, snprintf_S) { // Synonym for %ls.
315 char buf[BUFSIZ];
Yi Kong32bc0fc2018-08-02 17:31:13 -0700316 wchar_t* ws = nullptr;
Elliott Hughes2f9c8ce2017-11-01 13:54:47 -0700317 EXPECT_EQ(8, snprintf(buf, sizeof(buf), "<%S>", ws));
318 EXPECT_STREQ("<(null)>", buf);
319
320 wchar_t chars[] = { L'h', L'i', 0 };
321 ws = chars;
322 EXPECT_EQ(4, snprintf(buf, sizeof(buf), "<%S>", ws));
323 EXPECT_STREQ("<hi>", buf);
324}
325
Christopher Ferris13f26a72016-01-13 13:47:58 -0800326TEST(STDIO_TEST, snprintf_n) {
Elliott Hughes063525c2014-05-13 11:19:57 -0700327#if defined(__BIONIC__)
Elliott Hughes41398d02018-03-07 13:32:58 -0800328 // http://b/14492135 and http://b/31832608.
Elliott Hughes7248a2d2013-09-24 18:01:33 -0700329 char buf[32];
Elliott Hughese2341d02014-05-02 18:16:32 -0700330 int i = 1234;
Elliott Hughes41398d02018-03-07 13:32:58 -0800331 EXPECT_DEATH(snprintf(buf, sizeof(buf), "a %n b", &i), "%n not allowed on Android");
Elliott Hughese2341d02014-05-02 18:16:32 -0700332#else
Elliott Hughesbcaa4542019-03-08 15:20:23 -0800333 GTEST_SKIP() << "glibc does allow %n";
Elliott Hughese2341d02014-05-02 18:16:32 -0700334#endif
Elliott Hughes7248a2d2013-09-24 18:01:33 -0700335}
Elliott Hughes7248a2d2013-09-24 18:01:33 -0700336
Christopher Ferris13f26a72016-01-13 13:47:58 -0800337TEST(STDIO_TEST, snprintf_smoke) {
Elliott Hughes1d13c642013-09-23 16:02:39 -0700338 char buf[BUFSIZ];
339
340 snprintf(buf, sizeof(buf), "a");
341 EXPECT_STREQ("a", buf);
342
343 snprintf(buf, sizeof(buf), "%%");
344 EXPECT_STREQ("%", buf);
345
346 snprintf(buf, sizeof(buf), "01234");
347 EXPECT_STREQ("01234", buf);
348
349 snprintf(buf, sizeof(buf), "a%sb", "01234");
350 EXPECT_STREQ("a01234b", buf);
351
Yi Kong32bc0fc2018-08-02 17:31:13 -0700352 char* s = nullptr;
Elliott Hughes1d13c642013-09-23 16:02:39 -0700353 snprintf(buf, sizeof(buf), "a%sb", s);
354 EXPECT_STREQ("a(null)b", buf);
355
356 snprintf(buf, sizeof(buf), "aa%scc", "bb");
357 EXPECT_STREQ("aabbcc", buf);
358
359 snprintf(buf, sizeof(buf), "a%cc", 'b');
360 EXPECT_STREQ("abc", buf);
361
362 snprintf(buf, sizeof(buf), "a%db", 1234);
363 EXPECT_STREQ("a1234b", buf);
364
365 snprintf(buf, sizeof(buf), "a%db", -8123);
366 EXPECT_STREQ("a-8123b", buf);
367
Stephen Hines6c7b3cb2013-10-11 16:03:21 -0700368 snprintf(buf, sizeof(buf), "a%hdb", static_cast<short>(0x7fff0010));
Elliott Hughes1d13c642013-09-23 16:02:39 -0700369 EXPECT_STREQ("a16b", buf);
370
Stephen Hines6c7b3cb2013-10-11 16:03:21 -0700371 snprintf(buf, sizeof(buf), "a%hhdb", static_cast<char>(0x7fffff10));
Elliott Hughes1d13c642013-09-23 16:02:39 -0700372 EXPECT_STREQ("a16b", buf);
373
374 snprintf(buf, sizeof(buf), "a%lldb", 0x1000000000LL);
375 EXPECT_STREQ("a68719476736b", buf);
376
377 snprintf(buf, sizeof(buf), "a%ldb", 70000L);
378 EXPECT_STREQ("a70000b", buf);
379
380 snprintf(buf, sizeof(buf), "a%pb", reinterpret_cast<void*>(0xb0001234));
381 EXPECT_STREQ("a0xb0001234b", buf);
382
383 snprintf(buf, sizeof(buf), "a%xz", 0x12ab);
384 EXPECT_STREQ("a12abz", buf);
385
386 snprintf(buf, sizeof(buf), "a%Xz", 0x12ab);
387 EXPECT_STREQ("a12ABz", buf);
388
389 snprintf(buf, sizeof(buf), "a%08xz", 0x123456);
390 EXPECT_STREQ("a00123456z", buf);
391
392 snprintf(buf, sizeof(buf), "a%5dz", 1234);
393 EXPECT_STREQ("a 1234z", buf);
394
395 snprintf(buf, sizeof(buf), "a%05dz", 1234);
396 EXPECT_STREQ("a01234z", buf);
397
398 snprintf(buf, sizeof(buf), "a%8dz", 1234);
399 EXPECT_STREQ("a 1234z", buf);
400
401 snprintf(buf, sizeof(buf), "a%-8dz", 1234);
402 EXPECT_STREQ("a1234 z", buf);
403
404 snprintf(buf, sizeof(buf), "A%-11sZ", "abcdef");
405 EXPECT_STREQ("Aabcdef Z", buf);
406
407 snprintf(buf, sizeof(buf), "A%s:%dZ", "hello", 1234);
408 EXPECT_STREQ("Ahello:1234Z", buf);
409
410 snprintf(buf, sizeof(buf), "a%03d:%d:%02dz", 5, 5, 5);
411 EXPECT_STREQ("a005:5:05z", buf);
412
Yi Kong32bc0fc2018-08-02 17:31:13 -0700413 void* p = nullptr;
Elliott Hughes1d13c642013-09-23 16:02:39 -0700414 snprintf(buf, sizeof(buf), "a%d,%pz", 5, p);
Christopher Ferris13613132013-10-28 15:24:04 -0700415#if defined(__BIONIC__)
Elliott Hughes1d13c642013-09-23 16:02:39 -0700416 EXPECT_STREQ("a5,0x0z", buf);
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800417#else // __BIONIC__
Christopher Ferris13613132013-10-28 15:24:04 -0700418 EXPECT_STREQ("a5,(nil)z", buf);
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800419#endif // __BIONIC__
Elliott Hughes1d13c642013-09-23 16:02:39 -0700420
421 snprintf(buf, sizeof(buf), "a%lld,%d,%d,%dz", 0x1000000000LL, 6, 7, 8);
422 EXPECT_STREQ("a68719476736,6,7,8z", buf);
423
424 snprintf(buf, sizeof(buf), "a_%f_b", 1.23f);
425 EXPECT_STREQ("a_1.230000_b", buf);
426
Stephen Hines6c7b3cb2013-10-11 16:03:21 -0700427 snprintf(buf, sizeof(buf), "a_%g_b", 3.14);
Elliott Hughes1d13c642013-09-23 16:02:39 -0700428 EXPECT_STREQ("a_3.14_b", buf);
Alexander Ivchenkoedd7c2e2014-04-01 17:01:39 +0400429
430 snprintf(buf, sizeof(buf), "%1$s %1$s", "print_me_twice");
431 EXPECT_STREQ("print_me_twice print_me_twice", buf);
Elliott Hughes1d13c642013-09-23 16:02:39 -0700432}
433
Elliott Hughes1b18aff2014-12-16 14:45:32 -0800434template <typename T>
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700435static void CheckInfNan(int snprintf_fn(T*, size_t, const T*, ...),
436 int sscanf_fn(const T*, const T*, ...),
437 const T* fmt_string, const T* fmt, const T* fmt_plus,
438 const T* minus_inf, const T* inf_, const T* plus_inf,
439 const T* minus_nan, const T* nan_, const T* plus_nan) {
Elliott Hughes1b18aff2014-12-16 14:45:32 -0800440 T buf[BUFSIZ];
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700441 float f;
Elliott Hughes7823f322014-04-14 12:11:28 -0700442
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700443 // NaN.
444
445 snprintf_fn(buf, sizeof(buf), fmt, nanf(""));
Elliott Hughes1b18aff2014-12-16 14:45:32 -0800446 EXPECT_STREQ(nan_, buf) << fmt;
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700447 EXPECT_EQ(1, sscanf_fn(buf, fmt, &f));
448 EXPECT_TRUE(isnan(f));
449
450 snprintf_fn(buf, sizeof(buf), fmt, -nanf(""));
Elliott Hughes1b18aff2014-12-16 14:45:32 -0800451 EXPECT_STREQ(minus_nan, buf) << fmt;
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700452 EXPECT_EQ(1, sscanf_fn(buf, fmt, &f));
453 EXPECT_TRUE(isnan(f));
454
455 snprintf_fn(buf, sizeof(buf), fmt_plus, nanf(""));
Elliott Hughes1b18aff2014-12-16 14:45:32 -0800456 EXPECT_STREQ(plus_nan, buf) << fmt_plus;
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700457 EXPECT_EQ(1, sscanf_fn(buf, fmt, &f));
458 EXPECT_TRUE(isnan(f));
459
460 snprintf_fn(buf, sizeof(buf), fmt_plus, -nanf(""));
Elliott Hughes1b18aff2014-12-16 14:45:32 -0800461 EXPECT_STREQ(minus_nan, buf) << fmt_plus;
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700462 EXPECT_EQ(1, sscanf_fn(buf, fmt, &f));
463 EXPECT_TRUE(isnan(f));
Elliott Hughes1b18aff2014-12-16 14:45:32 -0800464
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700465 // Inf.
466
467 snprintf_fn(buf, sizeof(buf), fmt, HUGE_VALF);
Elliott Hughes1b18aff2014-12-16 14:45:32 -0800468 EXPECT_STREQ(inf_, buf) << fmt;
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700469 EXPECT_EQ(1, sscanf_fn(buf, fmt, &f));
470 EXPECT_EQ(HUGE_VALF, f);
471
472 snprintf_fn(buf, sizeof(buf), fmt, -HUGE_VALF);
Elliott Hughes1b18aff2014-12-16 14:45:32 -0800473 EXPECT_STREQ(minus_inf, buf) << fmt;
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700474 EXPECT_EQ(1, sscanf_fn(buf, fmt, &f));
475 EXPECT_EQ(-HUGE_VALF, f);
476
477 snprintf_fn(buf, sizeof(buf), fmt_plus, HUGE_VALF);
Elliott Hughes1b18aff2014-12-16 14:45:32 -0800478 EXPECT_STREQ(plus_inf, buf) << fmt_plus;
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700479 EXPECT_EQ(1, sscanf_fn(buf, fmt, &f));
480 EXPECT_EQ(HUGE_VALF, f);
481
482 snprintf_fn(buf, sizeof(buf), fmt_plus, -HUGE_VALF);
Elliott Hughes1b18aff2014-12-16 14:45:32 -0800483 EXPECT_STREQ(minus_inf, buf) << fmt_plus;
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700484 EXPECT_EQ(1, sscanf_fn(buf, fmt, &f));
485 EXPECT_EQ(-HUGE_VALF, f);
486
487 // Check case-insensitivity.
488 snprintf_fn(buf, sizeof(buf), fmt_string, "[InFiNiTy]");
489 EXPECT_EQ(1, sscanf_fn(buf, fmt, &f)) << buf;
490 EXPECT_EQ(HUGE_VALF, f);
491 snprintf_fn(buf, sizeof(buf), fmt_string, "[NaN]");
492 EXPECT_EQ(1, sscanf_fn(buf, fmt, &f)) << buf;
493 EXPECT_TRUE(isnan(f));
Elliott Hughes7823f322014-04-14 12:11:28 -0700494}
495
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700496TEST(STDIO_TEST, snprintf_sscanf_inf_nan) {
497 CheckInfNan(snprintf, sscanf, "%s",
498 "[%a]", "[%+a]",
499 "[-inf]", "[inf]", "[+inf]",
500 "[-nan]", "[nan]", "[+nan]");
501 CheckInfNan(snprintf, sscanf, "%s",
502 "[%A]", "[%+A]",
503 "[-INF]", "[INF]", "[+INF]",
504 "[-NAN]", "[NAN]", "[+NAN]");
505 CheckInfNan(snprintf, sscanf, "%s",
506 "[%e]", "[%+e]",
507 "[-inf]", "[inf]", "[+inf]",
508 "[-nan]", "[nan]", "[+nan]");
509 CheckInfNan(snprintf, sscanf, "%s",
510 "[%E]", "[%+E]",
511 "[-INF]", "[INF]", "[+INF]",
512 "[-NAN]", "[NAN]", "[+NAN]");
513 CheckInfNan(snprintf, sscanf, "%s",
514 "[%f]", "[%+f]",
515 "[-inf]", "[inf]", "[+inf]",
516 "[-nan]", "[nan]", "[+nan]");
517 CheckInfNan(snprintf, sscanf, "%s",
518 "[%F]", "[%+F]",
519 "[-INF]", "[INF]", "[+INF]",
520 "[-NAN]", "[NAN]", "[+NAN]");
521 CheckInfNan(snprintf, sscanf, "%s",
522 "[%g]", "[%+g]",
523 "[-inf]", "[inf]", "[+inf]",
524 "[-nan]", "[nan]", "[+nan]");
525 CheckInfNan(snprintf, sscanf, "%s",
526 "[%G]", "[%+G]",
527 "[-INF]", "[INF]", "[+INF]",
528 "[-NAN]", "[NAN]", "[+NAN]");
Elliott Hughes1b18aff2014-12-16 14:45:32 -0800529}
Elliott Hughes7823f322014-04-14 12:11:28 -0700530
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700531TEST(STDIO_TEST, swprintf_swscanf_inf_nan) {
532 CheckInfNan(swprintf, swscanf, L"%s",
533 L"[%a]", L"[%+a]",
534 L"[-inf]", L"[inf]", L"[+inf]",
535 L"[-nan]", L"[nan]", L"[+nan]");
536 CheckInfNan(swprintf, swscanf, L"%s",
537 L"[%A]", L"[%+A]",
538 L"[-INF]", L"[INF]", L"[+INF]",
539 L"[-NAN]", L"[NAN]", L"[+NAN]");
540 CheckInfNan(swprintf, swscanf, L"%s",
541 L"[%e]", L"[%+e]",
542 L"[-inf]", L"[inf]", L"[+inf]",
543 L"[-nan]", L"[nan]", L"[+nan]");
544 CheckInfNan(swprintf, swscanf, L"%s",
545 L"[%E]", L"[%+E]",
546 L"[-INF]", L"[INF]", L"[+INF]",
547 L"[-NAN]", L"[NAN]", L"[+NAN]");
548 CheckInfNan(swprintf, swscanf, L"%s",
549 L"[%f]", L"[%+f]",
550 L"[-inf]", L"[inf]", L"[+inf]",
551 L"[-nan]", L"[nan]", L"[+nan]");
552 CheckInfNan(swprintf, swscanf, L"%s",
553 L"[%F]", L"[%+F]",
554 L"[-INF]", L"[INF]", L"[+INF]",
555 L"[-NAN]", L"[NAN]", L"[+NAN]");
556 CheckInfNan(swprintf, swscanf, L"%s",
557 L"[%g]", L"[%+g]",
558 L"[-inf]", L"[inf]", L"[+inf]",
559 L"[-nan]", L"[nan]", L"[+nan]");
560 CheckInfNan(swprintf, swscanf, L"%s",
561 L"[%G]", L"[%+G]",
562 L"[-INF]", L"[INF]", L"[+INF]",
563 L"[-NAN]", L"[NAN]", L"[+NAN]");
Elliott Hughes7823f322014-04-14 12:11:28 -0700564}
565
Dan Albert9601f162017-08-09 14:59:06 -0700566TEST(STDIO_TEST, swprintf) {
567 constexpr size_t nchars = 32;
568 wchar_t buf[nchars];
569
570 ASSERT_EQ(2, swprintf(buf, nchars, L"ab")) << strerror(errno);
571 ASSERT_EQ(std::wstring(L"ab"), buf);
572 ASSERT_EQ(5, swprintf(buf, nchars, L"%s", "abcde"));
573 ASSERT_EQ(std::wstring(L"abcde"), buf);
574
575 // Unlike swprintf(), swprintf() returns -1 in case of truncation
576 // and doesn't necessarily zero-terminate the output!
577 ASSERT_EQ(-1, swprintf(buf, 4, L"%s", "abcde"));
578
579 const char kString[] = "Hello, World";
580 ASSERT_EQ(12, swprintf(buf, nchars, L"%s", kString));
581 ASSERT_EQ(std::wstring(L"Hello, World"), buf);
582 ASSERT_EQ(12, swprintf(buf, 13, L"%s", kString));
583 ASSERT_EQ(std::wstring(L"Hello, World"), buf);
584}
585
586TEST(STDIO_TEST, swprintf_a) {
587 constexpr size_t nchars = 32;
588 wchar_t buf[nchars];
589
590 ASSERT_EQ(20, swprintf(buf, nchars, L"%a", 3.1415926535));
591 ASSERT_EQ(std::wstring(L"0x1.921fb54411744p+1"), buf);
592}
593
Elliott Hughes2f9c8ce2017-11-01 13:54:47 -0700594TEST(STDIO_TEST, swprintf_lc) {
595 constexpr size_t nchars = 32;
596 wchar_t buf[nchars];
597
598 wint_t wc = L'a';
599 EXPECT_EQ(3, swprintf(buf, nchars, L"<%lc>", wc));
600 EXPECT_EQ(std::wstring(L"<a>"), buf);
601}
602
603TEST(STDIO_TEST, swprintf_C) { // Synonym for %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"<%C>", wc));
609 EXPECT_EQ(std::wstring(L"<a>"), buf);
610}
611
Elliott Hughes618303c2017-11-02 16:58:44 -0700612TEST(STDIO_TEST, swprintf_jd_INTMAX_MAX) {
613 constexpr size_t nchars = 32;
614 wchar_t buf[nchars];
615
616 swprintf(buf, nchars, L"%jd", INTMAX_MAX);
617 EXPECT_EQ(std::wstring(L"9223372036854775807"), buf);
618}
619
620TEST(STDIO_TEST, swprintf_jd_INTMAX_MIN) {
621 constexpr size_t nchars = 32;
622 wchar_t buf[nchars];
623
624 swprintf(buf, nchars, L"%jd", INTMAX_MIN);
625 EXPECT_EQ(std::wstring(L"-9223372036854775808"), buf);
626}
627
628TEST(STDIO_TEST, swprintf_ju_UINTMAX_MAX) {
629 constexpr size_t nchars = 32;
630 wchar_t buf[nchars];
631
632 swprintf(buf, nchars, L"%ju", UINTMAX_MAX);
633 EXPECT_EQ(std::wstring(L"18446744073709551615"), buf);
634}
635
636TEST(STDIO_TEST, swprintf_1$ju_UINTMAX_MAX) {
637 constexpr size_t nchars = 32;
638 wchar_t buf[nchars];
639
640 swprintf(buf, nchars, L"%1$ju", UINTMAX_MAX);
641 EXPECT_EQ(std::wstring(L"18446744073709551615"), buf);
642}
643
Dan Albert9601f162017-08-09 14:59:06 -0700644TEST(STDIO_TEST, swprintf_ls) {
645 constexpr size_t nchars = 32;
646 wchar_t buf[nchars];
647
648 static const wchar_t kWideString[] = L"Hello\uff41 World";
649 ASSERT_EQ(12, swprintf(buf, nchars, L"%ls", kWideString));
650 ASSERT_EQ(std::wstring(kWideString), buf);
651 ASSERT_EQ(12, swprintf(buf, 13, L"%ls", kWideString));
652 ASSERT_EQ(std::wstring(kWideString), buf);
653}
654
Elliott Hughes2f9c8ce2017-11-01 13:54:47 -0700655TEST(STDIO_TEST, swprintf_S) { // Synonym for %ls.
656 constexpr size_t nchars = 32;
657 wchar_t buf[nchars];
658
659 static const wchar_t kWideString[] = L"Hello\uff41 World";
660 ASSERT_EQ(12, swprintf(buf, nchars, L"%S", kWideString));
661 ASSERT_EQ(std::wstring(kWideString), buf);
662 ASSERT_EQ(12, swprintf(buf, 13, L"%S", kWideString));
663 ASSERT_EQ(std::wstring(kWideString), buf);
664}
665
Christopher Ferris13f26a72016-01-13 13:47:58 -0800666TEST(STDIO_TEST, snprintf_d_INT_MAX) {
Elliott Hughes1d13c642013-09-23 16:02:39 -0700667 char buf[BUFSIZ];
668 snprintf(buf, sizeof(buf), "%d", INT_MAX);
669 EXPECT_STREQ("2147483647", buf);
670}
671
Christopher Ferris13f26a72016-01-13 13:47:58 -0800672TEST(STDIO_TEST, snprintf_d_INT_MIN) {
Elliott Hughes1d13c642013-09-23 16:02:39 -0700673 char buf[BUFSIZ];
674 snprintf(buf, sizeof(buf), "%d", INT_MIN);
675 EXPECT_STREQ("-2147483648", buf);
676}
677
Elliott Hughes618303c2017-11-02 16:58:44 -0700678TEST(STDIO_TEST, snprintf_jd_INTMAX_MAX) {
679 char buf[BUFSIZ];
680 snprintf(buf, sizeof(buf), "%jd", INTMAX_MAX);
681 EXPECT_STREQ("9223372036854775807", buf);
682}
683
684TEST(STDIO_TEST, snprintf_jd_INTMAX_MIN) {
685 char buf[BUFSIZ];
686 snprintf(buf, sizeof(buf), "%jd", INTMAX_MIN);
687 EXPECT_STREQ("-9223372036854775808", buf);
688}
689
690TEST(STDIO_TEST, snprintf_ju_UINTMAX_MAX) {
691 char buf[BUFSIZ];
692 snprintf(buf, sizeof(buf), "%ju", UINTMAX_MAX);
693 EXPECT_STREQ("18446744073709551615", buf);
694}
695
696TEST(STDIO_TEST, snprintf_1$ju_UINTMAX_MAX) {
697 char buf[BUFSIZ];
698 snprintf(buf, sizeof(buf), "%1$ju", UINTMAX_MAX);
699 EXPECT_STREQ("18446744073709551615", buf);
700}
701
Christopher Ferris13f26a72016-01-13 13:47:58 -0800702TEST(STDIO_TEST, snprintf_ld_LONG_MAX) {
Elliott Hughes1d13c642013-09-23 16:02:39 -0700703 char buf[BUFSIZ];
704 snprintf(buf, sizeof(buf), "%ld", LONG_MAX);
Josh Gaob36efa42016-09-15 13:55:41 -0700705#if defined(__LP64__)
Elliott Hughes925753a2013-10-18 13:17:18 -0700706 EXPECT_STREQ("9223372036854775807", buf);
707#else
Elliott Hughes1d13c642013-09-23 16:02:39 -0700708 EXPECT_STREQ("2147483647", buf);
Elliott Hughes925753a2013-10-18 13:17:18 -0700709#endif
Elliott Hughes1d13c642013-09-23 16:02:39 -0700710}
711
Christopher Ferris13f26a72016-01-13 13:47:58 -0800712TEST(STDIO_TEST, snprintf_ld_LONG_MIN) {
Elliott Hughes1d13c642013-09-23 16:02:39 -0700713 char buf[BUFSIZ];
714 snprintf(buf, sizeof(buf), "%ld", LONG_MIN);
Josh Gaob36efa42016-09-15 13:55:41 -0700715#if defined(__LP64__)
Elliott Hughes925753a2013-10-18 13:17:18 -0700716 EXPECT_STREQ("-9223372036854775808", buf);
717#else
Elliott Hughes1d13c642013-09-23 16:02:39 -0700718 EXPECT_STREQ("-2147483648", buf);
Elliott Hughes925753a2013-10-18 13:17:18 -0700719#endif
Elliott Hughes1d13c642013-09-23 16:02:39 -0700720}
721
Christopher Ferris13f26a72016-01-13 13:47:58 -0800722TEST(STDIO_TEST, snprintf_lld_LLONG_MAX) {
Elliott Hughes1d13c642013-09-23 16:02:39 -0700723 char buf[BUFSIZ];
724 snprintf(buf, sizeof(buf), "%lld", LLONG_MAX);
725 EXPECT_STREQ("9223372036854775807", buf);
726}
727
Christopher Ferris13f26a72016-01-13 13:47:58 -0800728TEST(STDIO_TEST, snprintf_lld_LLONG_MIN) {
Elliott Hughes1d13c642013-09-23 16:02:39 -0700729 char buf[BUFSIZ];
730 snprintf(buf, sizeof(buf), "%lld", LLONG_MIN);
731 EXPECT_STREQ("-9223372036854775808", buf);
732}
733
Elliott Hughes2f9c8ce2017-11-01 13:54:47 -0700734TEST(STDIO_TEST, snprintf_o_UINT_MAX) {
735 char buf[BUFSIZ];
736 snprintf(buf, sizeof(buf), "%o", UINT_MAX);
737 EXPECT_STREQ("37777777777", buf);
738}
739
740TEST(STDIO_TEST, snprintf_u_UINT_MAX) {
741 char buf[BUFSIZ];
742 snprintf(buf, sizeof(buf), "%u", UINT_MAX);
743 EXPECT_STREQ("4294967295", buf);
744}
745
746TEST(STDIO_TEST, snprintf_x_UINT_MAX) {
747 char buf[BUFSIZ];
748 snprintf(buf, sizeof(buf), "%x", UINT_MAX);
749 EXPECT_STREQ("ffffffff", buf);
750}
751
752TEST(STDIO_TEST, snprintf_X_UINT_MAX) {
753 char buf[BUFSIZ];
754 snprintf(buf, sizeof(buf), "%X", UINT_MAX);
755 EXPECT_STREQ("FFFFFFFF", buf);
756}
757
Christopher Ferris13f26a72016-01-13 13:47:58 -0800758TEST(STDIO_TEST, snprintf_e) {
Elliott Hughes4bd97ce2014-04-10 17:48:14 -0700759 char buf[BUFSIZ];
760
761 snprintf(buf, sizeof(buf), "%e", 1.5);
762 EXPECT_STREQ("1.500000e+00", buf);
763
Chih-Hung Hsiehc2edae32018-12-11 15:16:24 -0800764 snprintf(buf, sizeof(buf), "%Le", 1.5L);
Elliott Hughes4bd97ce2014-04-10 17:48:14 -0700765 EXPECT_STREQ("1.500000e+00", buf);
766}
767
Christopher Ferris13f26a72016-01-13 13:47:58 -0800768TEST(STDIO_TEST, snprintf_negative_zero_5084292) {
Elliott Hughese77f38f2014-05-14 12:39:12 -0700769 char buf[BUFSIZ];
770
Elliott Hughes1b18aff2014-12-16 14:45:32 -0800771 snprintf(buf, sizeof(buf), "%e", -0.0);
772 EXPECT_STREQ("-0.000000e+00", buf);
773 snprintf(buf, sizeof(buf), "%E", -0.0);
774 EXPECT_STREQ("-0.000000E+00", buf);
Elliott Hughese77f38f2014-05-14 12:39:12 -0700775 snprintf(buf, sizeof(buf), "%f", -0.0);
776 EXPECT_STREQ("-0.000000", buf);
Elliott Hughes1b18aff2014-12-16 14:45:32 -0800777 snprintf(buf, sizeof(buf), "%F", -0.0);
778 EXPECT_STREQ("-0.000000", buf);
779 snprintf(buf, sizeof(buf), "%g", -0.0);
780 EXPECT_STREQ("-0", buf);
781 snprintf(buf, sizeof(buf), "%G", -0.0);
782 EXPECT_STREQ("-0", buf);
783 snprintf(buf, sizeof(buf), "%a", -0.0);
784 EXPECT_STREQ("-0x0p+0", buf);
785 snprintf(buf, sizeof(buf), "%A", -0.0);
786 EXPECT_STREQ("-0X0P+0", buf);
Elliott Hughese77f38f2014-05-14 12:39:12 -0700787}
788
Christopher Ferris13f26a72016-01-13 13:47:58 -0800789TEST(STDIO_TEST, snprintf_utf8_15439554) {
Yi Kong32bc0fc2018-08-02 17:31:13 -0700790 locale_t cloc = newlocale(LC_ALL, "C.UTF-8", nullptr);
Wally Yaua40fdbd2014-08-26 09:47:23 -0700791 locale_t old_locale = uselocale(cloc);
Dan Albert1aec7c12014-07-30 10:53:48 -0700792
Elliott Hughes69f05d22014-06-05 20:10:09 -0700793 // http://b/15439554
794 char buf[BUFSIZ];
795
796 // 1-byte character.
797 snprintf(buf, sizeof(buf), "%dx%d", 1, 2);
798 EXPECT_STREQ("1x2", buf);
799 // 2-byte character.
800 snprintf(buf, sizeof(buf), "%d\xc2\xa2%d", 1, 2);
801 EXPECT_STREQ("1¢2", buf);
802 // 3-byte character.
803 snprintf(buf, sizeof(buf), "%d\xe2\x82\xac%d", 1, 2);
804 EXPECT_STREQ("1€2", buf);
805 // 4-byte character.
806 snprintf(buf, sizeof(buf), "%d\xf0\xa4\xad\xa2%d", 1, 2);
807 EXPECT_STREQ("1𤭢2", buf);
Dan Albert1aec7c12014-07-30 10:53:48 -0700808
Wally Yaua40fdbd2014-08-26 09:47:23 -0700809 uselocale(old_locale);
Dan Albert1aec7c12014-07-30 10:53:48 -0700810 freelocale(cloc);
Elliott Hughes69f05d22014-06-05 20:10:09 -0700811}
812
Elliott Hughes43f7c872016-02-05 11:18:41 -0800813static void* snprintf_small_stack_fn(void*) {
814 // Make life (realistically) hard for ourselves by allocating our own buffer for the result.
815 char buf[PATH_MAX];
816 snprintf(buf, sizeof(buf), "/proc/%d", getpid());
817 return nullptr;
818}
819
820TEST(STDIO_TEST, snprintf_small_stack) {
821 // Is it safe to call snprintf on a thread with a small stack?
822 // (The snprintf implementation puts some pretty large buffers on the stack.)
823 pthread_attr_t a;
824 ASSERT_EQ(0, pthread_attr_init(&a));
825 ASSERT_EQ(0, pthread_attr_setstacksize(&a, PTHREAD_STACK_MIN));
826
827 pthread_t t;
828 ASSERT_EQ(0, pthread_create(&t, &a, snprintf_small_stack_fn, nullptr));
829 ASSERT_EQ(0, pthread_join(t, nullptr));
830}
831
Elliott Hughes8200e552016-02-05 21:57:37 -0800832TEST(STDIO_TEST, snprintf_asterisk_overflow) {
833 char buf[128];
834 ASSERT_EQ(5, snprintf(buf, sizeof(buf), "%.*s%c", 4, "hello world", '!'));
835 ASSERT_EQ(12, snprintf(buf, sizeof(buf), "%.*s%c", INT_MAX/2, "hello world", '!'));
836 ASSERT_EQ(12, snprintf(buf, sizeof(buf), "%.*s%c", INT_MAX-1, "hello world", '!'));
837 ASSERT_EQ(12, snprintf(buf, sizeof(buf), "%.*s%c", INT_MAX, "hello world", '!'));
838 ASSERT_EQ(12, snprintf(buf, sizeof(buf), "%.*s%c", -1, "hello world", '!'));
839
840 // INT_MAX-1, INT_MAX, INT_MAX+1.
841 ASSERT_EQ(12, snprintf(buf, sizeof(buf), "%.2147483646s%c", "hello world", '!'));
842 ASSERT_EQ(12, snprintf(buf, sizeof(buf), "%.2147483647s%c", "hello world", '!'));
843 ASSERT_EQ(-1, snprintf(buf, sizeof(buf), "%.2147483648s%c", "hello world", '!'));
844 ASSERT_EQ(ENOMEM, errno);
845}
846
Elliott Hughes70715da2016-08-01 16:35:17 -0700847TEST(STDIO_TEST, fprintf) {
848 TemporaryFile tf;
849
850 FILE* tfile = fdopen(tf.fd, "r+");
851 ASSERT_TRUE(tfile != nullptr);
852
853 ASSERT_EQ(7, fprintf(tfile, "%d %s", 123, "abc"));
854 AssertFileIs(tfile, "123 abc");
855 fclose(tfile);
856}
857
Christopher Ferris13f26a72016-01-13 13:47:58 -0800858TEST(STDIO_TEST, fprintf_failures_7229520) {
Elliott Hughes69f05d22014-06-05 20:10:09 -0700859 // http://b/7229520
Elliott Hughesc9244bd2014-05-14 13:31:35 -0700860 FILE* fp;
Josh Gaof6e5b582018-06-01 15:30:54 -0700861 int fd_rdonly = open("/dev/null", O_RDONLY);
862 ASSERT_NE(-1, fd_rdonly);
Elliott Hughesc9244bd2014-05-14 13:31:35 -0700863
864 // Unbuffered case where the fprintf(3) itself fails.
865 ASSERT_NE(nullptr, fp = tmpfile());
Yi Kong32bc0fc2018-08-02 17:31:13 -0700866 setbuf(fp, nullptr);
Elliott Hughesc9244bd2014-05-14 13:31:35 -0700867 ASSERT_EQ(4, fprintf(fp, "epic"));
Josh Gaof6e5b582018-06-01 15:30:54 -0700868 ASSERT_NE(-1, dup2(fd_rdonly, fileno(fp)));
Elliott Hughesc9244bd2014-05-14 13:31:35 -0700869 ASSERT_EQ(-1, fprintf(fp, "fail"));
Josh Gaof6e5b582018-06-01 15:30:54 -0700870 ASSERT_EQ(0, fclose(fp));
Elliott Hughesc9244bd2014-05-14 13:31:35 -0700871
872 // Buffered case where we won't notice until the fclose(3).
873 // It's likely this is what was actually seen in http://b/7229520,
874 // and that expecting fprintf to fail is setting yourself up for
875 // disappointment. Remember to check fclose(3)'s return value, kids!
876 ASSERT_NE(nullptr, fp = tmpfile());
877 ASSERT_EQ(4, fprintf(fp, "epic"));
Josh Gaof6e5b582018-06-01 15:30:54 -0700878 ASSERT_NE(-1, dup2(fd_rdonly, fileno(fp)));
Elliott Hughesc9244bd2014-05-14 13:31:35 -0700879 ASSERT_EQ(4, fprintf(fp, "fail"));
880 ASSERT_EQ(-1, fclose(fp));
881}
882
Elliott Hughes468efc82018-07-10 14:39:49 -0700883TEST(STDIO_TEST, popen_r) {
Elliott Hughes6b3f49a2013-03-06 16:20:55 -0800884 FILE* fp = popen("cat /proc/version", "r");
Yi Kong32bc0fc2018-08-02 17:31:13 -0700885 ASSERT_TRUE(fp != nullptr);
Elliott Hughes6b3f49a2013-03-06 16:20:55 -0800886
887 char buf[16];
888 char* s = fgets(buf, sizeof(buf), fp);
889 buf[13] = '\0';
890 ASSERT_STREQ("Linux version", s);
891
892 ASSERT_EQ(0, pclose(fp));
893}
Elliott Hughes6b05c8e2013-04-11 13:54:48 -0700894
Elliott Hughes468efc82018-07-10 14:39:49 -0700895TEST(STDIO_TEST, popen_socketpair) {
896 FILE* fp = popen("cat", "r+");
Yi Kong32bc0fc2018-08-02 17:31:13 -0700897 ASSERT_TRUE(fp != nullptr);
Elliott Hughes468efc82018-07-10 14:39:49 -0700898
899 fputs("hello\nworld\n", fp);
900 fflush(fp);
901
902 char buf[16];
903 ASSERT_NE(nullptr, fgets(buf, sizeof(buf), fp));
904 EXPECT_STREQ("hello\n", buf);
905 ASSERT_NE(nullptr, fgets(buf, sizeof(buf), fp));
906 EXPECT_STREQ("world\n", buf);
907
908 ASSERT_EQ(0, pclose(fp));
909}
910
911TEST(STDIO_TEST, popen_socketpair_shutdown) {
912 FILE* fp = popen("uniq -c", "r+");
Yi Kong32bc0fc2018-08-02 17:31:13 -0700913 ASSERT_TRUE(fp != nullptr);
Elliott Hughes468efc82018-07-10 14:39:49 -0700914
915 fputs("a\na\na\na\nb\n", fp);
916 fflush(fp);
917 ASSERT_EQ(0, shutdown(fileno(fp), SHUT_WR));
918
919 char buf[16];
920 ASSERT_NE(nullptr, fgets(buf, sizeof(buf), fp));
921 EXPECT_STREQ(" 4 a\n", buf);
922 ASSERT_NE(nullptr, fgets(buf, sizeof(buf), fp));
923 EXPECT_STREQ(" 1 b\n", buf);
924
925 ASSERT_EQ(0, pclose(fp));
926}
927
928TEST(STDIO_TEST, popen_return_value_0) {
929 FILE* fp = popen("true", "r");
Yi Kong32bc0fc2018-08-02 17:31:13 -0700930 ASSERT_TRUE(fp != nullptr);
Elliott Hughes468efc82018-07-10 14:39:49 -0700931 int status = pclose(fp);
932 EXPECT_TRUE(WIFEXITED(status));
933 EXPECT_EQ(0, WEXITSTATUS(status));
934}
935
936TEST(STDIO_TEST, popen_return_value_1) {
937 FILE* fp = popen("false", "r");
Yi Kong32bc0fc2018-08-02 17:31:13 -0700938 ASSERT_TRUE(fp != nullptr);
Elliott Hughes468efc82018-07-10 14:39:49 -0700939 int status = pclose(fp);
940 EXPECT_TRUE(WIFEXITED(status));
941 EXPECT_EQ(1, WEXITSTATUS(status));
942}
943
944TEST(STDIO_TEST, popen_return_value_signal) {
945 FILE* fp = popen("kill -7 $$", "r");
Yi Kong32bc0fc2018-08-02 17:31:13 -0700946 ASSERT_TRUE(fp != nullptr);
Elliott Hughes468efc82018-07-10 14:39:49 -0700947 int status = pclose(fp);
948 EXPECT_TRUE(WIFSIGNALED(status));
949 EXPECT_EQ(7, WTERMSIG(status));
950}
951
Christopher Ferris13f26a72016-01-13 13:47:58 -0800952TEST(STDIO_TEST, getc) {
Elliott Hughes6b05c8e2013-04-11 13:54:48 -0700953 FILE* fp = fopen("/proc/version", "r");
Yi Kong32bc0fc2018-08-02 17:31:13 -0700954 ASSERT_TRUE(fp != nullptr);
Elliott Hughes6b05c8e2013-04-11 13:54:48 -0700955 ASSERT_EQ('L', getc(fp));
956 ASSERT_EQ('i', getc(fp));
957 ASSERT_EQ('n', getc(fp));
958 ASSERT_EQ('u', getc(fp));
959 ASSERT_EQ('x', getc(fp));
960 fclose(fp);
961}
962
Christopher Ferris13f26a72016-01-13 13:47:58 -0800963TEST(STDIO_TEST, putc) {
Elliott Hughes6b05c8e2013-04-11 13:54:48 -0700964 FILE* fp = fopen("/proc/version", "r");
Yi Kong32bc0fc2018-08-02 17:31:13 -0700965 ASSERT_TRUE(fp != nullptr);
Elliott Hughes6b05c8e2013-04-11 13:54:48 -0700966 ASSERT_EQ(EOF, putc('x', fp));
967 fclose(fp);
968}
Elliott Hughes603332f2014-03-12 17:10:41 -0700969
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700970TEST(STDIO_TEST, sscanf_swscanf) {
971 struct stuff {
972 char s1[123];
Elliott Hughes0d3ba1f2017-12-06 16:41:35 -0800973 int i1, i2;
974 char cs1[3];
975 char s2[3];
976 char c1;
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700977 double d1;
978 float f1;
Elliott Hughes0d3ba1f2017-12-06 16:41:35 -0800979 char s3[123];
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700980
981 void Check() {
Elliott Hughes0d3ba1f2017-12-06 16:41:35 -0800982 EXPECT_STREQ("hello", s1);
983 EXPECT_EQ(123, i1);
984 EXPECT_EQ(456, i2);
985 EXPECT_EQ('a', cs1[0]);
986 EXPECT_EQ('b', cs1[1]);
987 EXPECT_EQ('x', cs1[2]); // No terminating NUL.
988 EXPECT_STREQ("AB", s2); // Terminating NUL.
989 EXPECT_EQ('!', c1);
990 EXPECT_DOUBLE_EQ(1.23, d1);
991 EXPECT_FLOAT_EQ(9.0f, f1);
992 EXPECT_STREQ("world", s3);
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700993 }
994 } s;
995
Elliott Hughes0d3ba1f2017-12-06 16:41:35 -0800996 memset(&s, 'x', sizeof(s));
997 ASSERT_EQ(9, sscanf(" hello 123 456abAB! 1.23 0x1.2p3 world",
998 "%s %i%i%2c%[A-Z]%c %lf %f %s",
999 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 -07001000 s.Check();
1001
Elliott Hughes0d3ba1f2017-12-06 16:41:35 -08001002 memset(&s, 'x', sizeof(s));
1003 ASSERT_EQ(9, swscanf(L" hello 123 456abAB! 1.23 0x1.2p3 world",
1004 L"%s %i%i%2c%[A-Z]%c %lf %f %s",
1005 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 -07001006 s.Check();
Elliott Hughes603332f2014-03-12 17:10:41 -07001007}
Elliott Hughes53b24382014-05-02 18:29:25 -07001008
Elliott Hughes0d3ba1f2017-12-06 16:41:35 -08001009template <typename T>
1010static void CheckScanf(int sscanf_fn(const T*, const T*, ...),
1011 const T* input, const T* fmt,
1012 int expected_count, const char* expected_string) {
1013 char buf[256] = {};
1014 ASSERT_EQ(expected_count, sscanf_fn(input, fmt, &buf)) << fmt;
1015 ASSERT_STREQ(expected_string, buf) << fmt;
1016}
1017
1018TEST(STDIO_TEST, sscanf_ccl) {
1019 // `abc` is just those characters.
1020 CheckScanf(sscanf, "abcd", "%[abc]", 1, "abc");
1021 // `a-c` is the range 'a' .. 'c'.
1022 CheckScanf(sscanf, "abcd", "%[a-c]", 1, "abc");
1023 CheckScanf(sscanf, "-d", "%[a-c]", 0, "");
1024 CheckScanf(sscanf, "ac-bAd", "%[a--c]", 1, "ac-bA");
1025 // `a-c-e` is equivalent to `a-e`.
1026 CheckScanf(sscanf, "abcdefg", "%[a-c-e]", 1, "abcde");
1027 // `e-a` is equivalent to `ae-` (because 'e' > 'a').
1028 CheckScanf(sscanf, "-a-e-b", "%[e-a]", 1, "-a-e-");
1029 // An initial '^' negates the set.
1030 CheckScanf(sscanf, "abcde", "%[^d]", 1, "abc");
1031 CheckScanf(sscanf, "abcdefgh", "%[^c-d]", 1, "ab");
1032 CheckScanf(sscanf, "hgfedcba", "%[^c-d]", 1, "hgfe");
1033 // The first character may be ']' or '-' without being special.
1034 CheckScanf(sscanf, "[[]]x", "%[][]", 1, "[[]]");
1035 CheckScanf(sscanf, "-a-x", "%[-a]", 1, "-a-");
1036 // The last character may be '-' without being special.
1037 CheckScanf(sscanf, "-a-x", "%[a-]", 1, "-a-");
1038 // X--Y is [X--] + Y, not [X--] + [--Y] (a bug in my initial implementation).
1039 CheckScanf(sscanf, "+,-/.", "%[+--/]", 1, "+,-/");
1040}
1041
1042TEST(STDIO_TEST, swscanf_ccl) {
1043 // `abc` is just those characters.
1044 CheckScanf(swscanf, L"abcd", L"%[abc]", 1, "abc");
1045 // `a-c` is the range 'a' .. 'c'.
1046 CheckScanf(swscanf, L"abcd", L"%[a-c]", 1, "abc");
1047 CheckScanf(swscanf, L"-d", L"%[a-c]", 0, "");
1048 CheckScanf(swscanf, L"ac-bAd", L"%[a--c]", 1, "ac-bA");
1049 // `a-c-e` is equivalent to `a-e`.
1050 CheckScanf(swscanf, L"abcdefg", L"%[a-c-e]", 1, "abcde");
1051 // `e-a` is equivalent to `ae-` (because 'e' > 'a').
1052 CheckScanf(swscanf, L"-a-e-b", L"%[e-a]", 1, "-a-e-");
1053 // An initial '^' negates the set.
1054 CheckScanf(swscanf, L"abcde", L"%[^d]", 1, "abc");
1055 CheckScanf(swscanf, L"abcdefgh", L"%[^c-d]", 1, "ab");
1056 CheckScanf(swscanf, L"hgfedcba", L"%[^c-d]", 1, "hgfe");
1057 // The first character may be ']' or '-' without being special.
1058 CheckScanf(swscanf, L"[[]]x", L"%[][]", 1, "[[]]");
1059 CheckScanf(swscanf, L"-a-x", L"%[-a]", 1, "-a-");
1060 // The last character may be '-' without being special.
1061 CheckScanf(swscanf, L"-a-x", L"%[a-]", 1, "-a-");
1062 // X--Y is [X--] + Y, not [X--] + [--Y] (a bug in my initial implementation).
1063 CheckScanf(swscanf, L"+,-/.", L"%[+--/]", 1, "+,-/");
1064}
1065
Elliott Hughes38e4aef2018-01-18 10:21:29 -08001066template <typename T1, typename T2>
1067static void CheckScanfM(int sscanf_fn(const T1*, const T1*, ...),
1068 const T1* input, const T1* fmt,
1069 int expected_count, const T2* expected_string) {
1070 T2* result = nullptr;
1071 ASSERT_EQ(expected_count, sscanf_fn(input, fmt, &result)) << fmt;
1072 if (expected_string == nullptr) {
1073 ASSERT_EQ(nullptr, result);
1074 } else {
1075 ASSERT_STREQ(expected_string, result) << fmt;
1076 }
1077 free(result);
1078}
1079
1080TEST(STDIO_TEST, sscanf_mc) {
1081 char* p1 = nullptr;
1082 char* p2 = nullptr;
1083 ASSERT_EQ(2, sscanf("hello", "%mc%mc", &p1, &p2));
1084 ASSERT_EQ('h', *p1);
1085 ASSERT_EQ('e', *p2);
1086 free(p1);
1087 free(p2);
1088
1089 p1 = nullptr;
1090 ASSERT_EQ(1, sscanf("hello", "%4mc", &p1));
1091 ASSERT_EQ('h', p1[0]);
1092 ASSERT_EQ('e', p1[1]);
1093 ASSERT_EQ('l', p1[2]);
1094 ASSERT_EQ('l', p1[3]);
1095 free(p1);
1096
1097 p1 = nullptr;
1098 ASSERT_EQ(1, sscanf("hello world", "%30mc", &p1));
1099 ASSERT_EQ('h', p1[0]);
1100 ASSERT_EQ('e', p1[1]);
1101 ASSERT_EQ('l', p1[2]);
1102 ASSERT_EQ('l', p1[3]);
1103 ASSERT_EQ('o', p1[4]);
1104 free(p1);
1105}
1106
1107
1108TEST(STDIO_TEST, sscanf_mlc) {
1109 // This is so useless that clang doesn't even believe it exists...
1110#pragma clang diagnostic push
1111#pragma clang diagnostic ignored "-Wformat-invalid-specifier"
1112#pragma clang diagnostic ignored "-Wformat-extra-args"
1113
1114 wchar_t* p1 = nullptr;
1115 wchar_t* p2 = nullptr;
1116 ASSERT_EQ(2, sscanf("hello", "%mlc%mlc", &p1, &p2));
1117 ASSERT_EQ(L'h', *p1);
1118 ASSERT_EQ(L'e', *p2);
1119 free(p1);
1120 free(p2);
1121
1122 p1 = nullptr;
1123 ASSERT_EQ(1, sscanf("hello", "%4mlc", &p1));
1124 ASSERT_EQ(L'h', p1[0]);
1125 ASSERT_EQ(L'e', p1[1]);
1126 ASSERT_EQ(L'l', p1[2]);
1127 ASSERT_EQ(L'l', p1[3]);
1128 free(p1);
1129
1130 p1 = nullptr;
1131 ASSERT_EQ(1, sscanf("hello world", "%30mlc", &p1));
1132 ASSERT_EQ(L'h', p1[0]);
1133 ASSERT_EQ(L'e', p1[1]);
1134 ASSERT_EQ(L'l', p1[2]);
1135 ASSERT_EQ(L'l', p1[3]);
1136 ASSERT_EQ(L'o', p1[4]);
1137 free(p1);
1138#pragma clang diagnostic pop
1139}
1140
1141
1142TEST(STDIO_TEST, sscanf_ms) {
1143 CheckScanfM(sscanf, "hello", "%ms", 1, "hello");
1144 CheckScanfM(sscanf, "hello", "%4ms", 1, "hell");
1145 CheckScanfM(sscanf, "hello world", "%30ms", 1, "hello");
1146}
1147
1148TEST(STDIO_TEST, sscanf_mls) {
1149 CheckScanfM(sscanf, "hello", "%mls", 1, L"hello");
1150 CheckScanfM(sscanf, "hello", "%4mls", 1, L"hell");
1151 CheckScanfM(sscanf, "hello world", "%30mls", 1, L"hello");
1152}
1153
1154TEST(STDIO_TEST, sscanf_m_ccl) {
1155 CheckScanfM(sscanf, "hello", "%m[a-z]", 1, "hello");
1156 CheckScanfM(sscanf, "hello", "%4m[a-z]", 1, "hell");
1157 CheckScanfM(sscanf, "hello world", "%30m[a-z]", 1, "hello");
1158}
1159
1160TEST(STDIO_TEST, sscanf_ml_ccl) {
1161 CheckScanfM(sscanf, "hello", "%ml[a-z]", 1, L"hello");
1162 CheckScanfM(sscanf, "hello", "%4ml[a-z]", 1, L"hell");
1163 CheckScanfM(sscanf, "hello world", "%30ml[a-z]", 1, L"hello");
1164}
1165
1166TEST(STDIO_TEST, sscanf_ls) {
1167 wchar_t w[32] = {};
1168 ASSERT_EQ(1, sscanf("hello world", "%ls", w));
1169 ASSERT_EQ(L"hello", std::wstring(w));
1170}
1171
1172TEST(STDIO_TEST, sscanf_ls_suppress) {
1173 ASSERT_EQ(0, sscanf("hello world", "%*ls %*ls"));
1174}
1175
1176TEST(STDIO_TEST, sscanf_ls_n) {
1177 setlocale(LC_ALL, "C.UTF-8");
1178 wchar_t w[32] = {};
1179 int pos = 0;
1180 ASSERT_EQ(1, sscanf("\xc4\x80", "%ls%n", w, &pos));
1181 ASSERT_EQ(static_cast<wchar_t>(256), w[0]);
1182 ASSERT_EQ(2, pos);
1183}
1184
1185TEST(STDIO_TEST, sscanf_ls_realloc) {
1186 // This is so useless that clang doesn't even believe it exists...
1187#pragma clang diagnostic push
1188#pragma clang diagnostic ignored "-Wformat-invalid-specifier"
1189#pragma clang diagnostic ignored "-Wformat-extra-args"
1190 wchar_t* p1 = nullptr;
1191 wchar_t* p2 = nullptr;
1192 ASSERT_EQ(2, sscanf("1234567890123456789012345678901234567890 world", "%mls %mls", &p1, &p2));
1193 ASSERT_EQ(L"1234567890123456789012345678901234567890", std::wstring(p1));
1194 ASSERT_EQ(L"world", std::wstring(p2));
1195#pragma clang diagnostic pop
1196}
1197
Elliott Hughesbf9cb9e2017-12-11 12:39:01 -08001198// https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=202240
1199TEST(STDIO_TEST, scanf_wscanf_EOF) {
1200 EXPECT_EQ(0, sscanf("b", "ab"));
1201 EXPECT_EQ(EOF, sscanf("", "a"));
1202 EXPECT_EQ(0, swscanf(L"b", L"ab"));
1203 EXPECT_EQ(EOF, swscanf(L"", L"a"));
1204}
1205
1206TEST(STDIO_TEST, scanf_invalid_UTF8) {
1207#if 0 // TODO: more tests invented during code review; no regressions, so fix later.
1208 char buf[BUFSIZ];
1209 wchar_t wbuf[BUFSIZ];
1210
1211 memset(buf, 0, sizeof(buf));
1212 memset(wbuf, 0, sizeof(wbuf));
1213 EXPECT_EQ(0, sscanf("\xc0" " foo", "%ls %s", wbuf, buf));
1214#endif
1215}
1216
1217TEST(STDIO_TEST, scanf_no_match_no_termination) {
1218 char buf[4] = "x";
1219 EXPECT_EQ(0, sscanf("d", "%[abc]", buf));
1220 EXPECT_EQ('x', buf[0]);
1221 EXPECT_EQ(0, swscanf(L"d", L"%[abc]", buf));
1222 EXPECT_EQ('x', buf[0]);
1223
1224 wchar_t wbuf[4] = L"x";
1225 EXPECT_EQ(0, swscanf(L"d", L"%l[abc]", wbuf));
1226 EXPECT_EQ(L'x', wbuf[0]);
1227
1228 EXPECT_EQ(EOF, sscanf("", "%s", buf));
1229 EXPECT_EQ('x', buf[0]);
1230
1231 EXPECT_EQ(EOF, swscanf(L"", L"%ls", wbuf));
1232 EXPECT_EQ(L'x', wbuf[0]);
1233}
1234
1235TEST(STDIO_TEST, scanf_wscanf_wide_character_class) {
1236#if 0 // TODO: more tests invented during code review; no regressions, so fix later.
1237 wchar_t buf[BUFSIZ];
1238
1239 // A wide character shouldn't match an ASCII-only class for scanf or wscanf.
1240 memset(buf, 0, sizeof(buf));
1241 EXPECT_EQ(1, sscanf("xĀyz", "%l[xy]", buf));
1242 EXPECT_EQ(L"x"s, std::wstring(buf));
1243 memset(buf, 0, sizeof(buf));
1244 EXPECT_EQ(1, swscanf(L"xĀyz", L"%l[xy]", buf));
1245 EXPECT_EQ(L"x"s, std::wstring(buf));
1246
1247 // Even if scanf has wide characters in a class, they won't match...
1248 // TODO: is that a bug?
1249 memset(buf, 0, sizeof(buf));
1250 EXPECT_EQ(1, sscanf("xĀyz", "%l[xĀy]", buf));
1251 EXPECT_EQ(L"x"s, std::wstring(buf));
1252 // ...unless you use wscanf.
1253 memset(buf, 0, sizeof(buf));
1254 EXPECT_EQ(1, swscanf(L"xĀyz", L"%l[xĀy]", buf));
1255 EXPECT_EQ(L"xĀy"s, std::wstring(buf));
1256
1257 // Negation only covers ASCII for scanf...
1258 memset(buf, 0, sizeof(buf));
1259 EXPECT_EQ(1, sscanf("xĀyz", "%l[^ab]", buf));
1260 EXPECT_EQ(L"x"s, std::wstring(buf));
1261 // ...but covers wide characters for wscanf.
1262 memset(buf, 0, sizeof(buf));
1263 EXPECT_EQ(1, swscanf(L"xĀyz", L"%l[^ab]", buf));
1264 EXPECT_EQ(L"xĀyz"s, std::wstring(buf));
1265
1266 // We already determined that non-ASCII characters are ignored in scanf classes.
1267 memset(buf, 0, sizeof(buf));
1268 EXPECT_EQ(1, sscanf("x"
1269 "\xc4\x80" // Matches a byte from each wide char in the class.
1270 "\xc6\x82" // Neither byte is in the class.
1271 "yz",
1272 "%l[xy" "\xc5\x80" "\xc4\x81" "]", buf));
1273 EXPECT_EQ(L"x", std::wstring(buf));
1274 // bionic and glibc both behave badly for wscanf, so let's call it right for now...
1275 memset(buf, 0, sizeof(buf));
1276 EXPECT_EQ(1, swscanf(L"x"
1277 L"\xc4\x80"
1278 L"\xc6\x82"
1279 L"yz",
1280 L"%l[xy" L"\xc5\x80" L"\xc4\x81" L"]", buf));
1281 // Note that this isn't L"xĀ" --- although the *bytes* matched, they're
1282 // not put back together as a wide character.
1283 EXPECT_EQ(L"x" L"\xc4" L"\x80", std::wstring(buf));
1284#endif
1285}
1286
Christopher Ferris13f26a72016-01-13 13:47:58 -08001287TEST(STDIO_TEST, cantwrite_EBADF) {
Elliott Hughes53b24382014-05-02 18:29:25 -07001288 // If we open a file read-only...
1289 FILE* fp = fopen("/proc/version", "r");
1290
1291 // ...all attempts to write to that file should return failure.
1292
1293 // They should also set errno to EBADF. This isn't POSIX, but it's traditional.
1294 // glibc gets the wide-character functions wrong.
1295
1296 errno = 0;
1297 EXPECT_EQ(EOF, putc('x', fp));
1298 EXPECT_EQ(EBADF, errno);
1299
1300 errno = 0;
1301 EXPECT_EQ(EOF, fprintf(fp, "hello"));
1302 EXPECT_EQ(EBADF, errno);
1303
1304 errno = 0;
1305 EXPECT_EQ(EOF, fwprintf(fp, L"hello"));
Elliott Hughes063525c2014-05-13 11:19:57 -07001306#if defined(__BIONIC__)
Elliott Hughes53b24382014-05-02 18:29:25 -07001307 EXPECT_EQ(EBADF, errno);
1308#endif
1309
1310 errno = 0;
Elliott Hughes53b24382014-05-02 18:29:25 -07001311 EXPECT_EQ(0U, fwrite("hello", 1, 2, fp));
1312 EXPECT_EQ(EBADF, errno);
1313
1314 errno = 0;
1315 EXPECT_EQ(EOF, fputs("hello", fp));
1316 EXPECT_EQ(EBADF, errno);
1317
1318 errno = 0;
1319 EXPECT_EQ(WEOF, fputwc(L'x', fp));
Elliott Hughes063525c2014-05-13 11:19:57 -07001320#if defined(__BIONIC__)
Elliott Hughes53b24382014-05-02 18:29:25 -07001321 EXPECT_EQ(EBADF, errno);
1322#endif
1323}
Calin Juravle03e4ebe2014-05-08 14:42:06 +01001324
1325// Tests that we can only have a consistent and correct fpos_t when using
1326// f*pos functions (i.e. fpos doesn't get inside a multi byte character).
Christopher Ferris13f26a72016-01-13 13:47:58 -08001327TEST(STDIO_TEST, consistent_fpos_t) {
Calin Juravle03e4ebe2014-05-08 14:42:06 +01001328 ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
1329 uselocale(LC_GLOBAL_LOCALE);
1330
1331 FILE* fp = tmpfile();
Yi Kong32bc0fc2018-08-02 17:31:13 -07001332 ASSERT_TRUE(fp != nullptr);
Calin Juravle03e4ebe2014-05-08 14:42:06 +01001333
1334 wchar_t mb_one_bytes = L'h';
1335 wchar_t mb_two_bytes = 0x00a2;
1336 wchar_t mb_three_bytes = 0x20ac;
1337 wchar_t mb_four_bytes = 0x24b62;
1338
1339 // Write to file.
1340 ASSERT_EQ(mb_one_bytes, static_cast<wchar_t>(fputwc(mb_one_bytes, fp)));
1341 ASSERT_EQ(mb_two_bytes, static_cast<wchar_t>(fputwc(mb_two_bytes, fp)));
1342 ASSERT_EQ(mb_three_bytes, static_cast<wchar_t>(fputwc(mb_three_bytes, fp)));
1343 ASSERT_EQ(mb_four_bytes, static_cast<wchar_t>(fputwc(mb_four_bytes, fp)));
1344
1345 rewind(fp);
1346
1347 // Record each character position.
1348 fpos_t pos1;
1349 fpos_t pos2;
1350 fpos_t pos3;
1351 fpos_t pos4;
1352 fpos_t pos5;
1353 EXPECT_EQ(0, fgetpos(fp, &pos1));
1354 ASSERT_EQ(mb_one_bytes, static_cast<wchar_t>(fgetwc(fp)));
1355 EXPECT_EQ(0, fgetpos(fp, &pos2));
1356 ASSERT_EQ(mb_two_bytes, static_cast<wchar_t>(fgetwc(fp)));
1357 EXPECT_EQ(0, fgetpos(fp, &pos3));
1358 ASSERT_EQ(mb_three_bytes, static_cast<wchar_t>(fgetwc(fp)));
1359 EXPECT_EQ(0, fgetpos(fp, &pos4));
1360 ASSERT_EQ(mb_four_bytes, static_cast<wchar_t>(fgetwc(fp)));
1361 EXPECT_EQ(0, fgetpos(fp, &pos5));
1362
Elliott Hughes063525c2014-05-13 11:19:57 -07001363#if defined(__BIONIC__)
Calin Juravle03e4ebe2014-05-08 14:42:06 +01001364 // Bionic's fpos_t is just an alias for off_t. This is inherited from OpenBSD
1365 // upstream. Glibc differs by storing the mbstate_t inside its fpos_t. In
1366 // Bionic (and upstream OpenBSD) the mbstate_t is stored inside the FILE
1367 // structure.
1368 ASSERT_EQ(0, static_cast<off_t>(pos1));
1369 ASSERT_EQ(1, static_cast<off_t>(pos2));
1370 ASSERT_EQ(3, static_cast<off_t>(pos3));
1371 ASSERT_EQ(6, static_cast<off_t>(pos4));
1372 ASSERT_EQ(10, static_cast<off_t>(pos5));
1373#endif
1374
1375 // Exercise back and forth movements of the position.
1376 ASSERT_EQ(0, fsetpos(fp, &pos2));
1377 ASSERT_EQ(mb_two_bytes, static_cast<wchar_t>(fgetwc(fp)));
1378 ASSERT_EQ(0, fsetpos(fp, &pos1));
1379 ASSERT_EQ(mb_one_bytes, static_cast<wchar_t>(fgetwc(fp)));
1380 ASSERT_EQ(0, fsetpos(fp, &pos4));
1381 ASSERT_EQ(mb_four_bytes, static_cast<wchar_t>(fgetwc(fp)));
1382 ASSERT_EQ(0, fsetpos(fp, &pos3));
1383 ASSERT_EQ(mb_three_bytes, static_cast<wchar_t>(fgetwc(fp)));
1384 ASSERT_EQ(0, fsetpos(fp, &pos5));
1385 ASSERT_EQ(WEOF, fgetwc(fp));
1386
1387 fclose(fp);
1388}
1389
1390// Exercise the interaction between fpos and seek.
Christopher Ferris13f26a72016-01-13 13:47:58 -08001391TEST(STDIO_TEST, fpos_t_and_seek) {
Calin Juravle03e4ebe2014-05-08 14:42:06 +01001392 ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
1393 uselocale(LC_GLOBAL_LOCALE);
1394
Calin Juravle9b95ea92014-05-14 17:07:10 +01001395 // In glibc-2.16 fseek doesn't work properly in wide mode
1396 // (https://sourceware.org/bugzilla/show_bug.cgi?id=14543). One workaround is
1397 // to close and re-open the file. We do it in order to make the test pass
1398 // with all glibcs.
1399
Calin Juravle03e4ebe2014-05-08 14:42:06 +01001400 TemporaryFile tf;
1401 FILE* fp = fdopen(tf.fd, "w+");
Yi Kong32bc0fc2018-08-02 17:31:13 -07001402 ASSERT_TRUE(fp != nullptr);
Calin Juravle03e4ebe2014-05-08 14:42:06 +01001403
1404 wchar_t mb_two_bytes = 0x00a2;
1405 wchar_t mb_three_bytes = 0x20ac;
1406 wchar_t mb_four_bytes = 0x24b62;
1407
1408 // Write to file.
1409 ASSERT_EQ(mb_two_bytes, static_cast<wchar_t>(fputwc(mb_two_bytes, fp)));
1410 ASSERT_EQ(mb_three_bytes, static_cast<wchar_t>(fputwc(mb_three_bytes, fp)));
1411 ASSERT_EQ(mb_four_bytes, static_cast<wchar_t>(fputwc(mb_four_bytes, fp)));
1412
1413 fflush(fp);
1414 fclose(fp);
1415
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -08001416 fp = fopen(tf.path, "r");
Yi Kong32bc0fc2018-08-02 17:31:13 -07001417 ASSERT_TRUE(fp != nullptr);
Calin Juravle03e4ebe2014-05-08 14:42:06 +01001418
1419 // Store a valid position.
1420 fpos_t mb_two_bytes_pos;
1421 ASSERT_EQ(0, fgetpos(fp, &mb_two_bytes_pos));
1422
1423 // Move inside mb_four_bytes with fseek.
1424 long offset_inside_mb = 6;
1425 ASSERT_EQ(0, fseek(fp, offset_inside_mb, SEEK_SET));
1426
1427 // Store the "inside multi byte" position.
1428 fpos_t pos_inside_mb;
1429 ASSERT_EQ(0, fgetpos(fp, &pos_inside_mb));
Elliott Hughes063525c2014-05-13 11:19:57 -07001430#if defined(__BIONIC__)
1431 ASSERT_EQ(offset_inside_mb, static_cast<off_t>(pos_inside_mb));
1432#endif
Calin Juravle03e4ebe2014-05-08 14:42:06 +01001433
1434 // Reading from within a byte should produce an error.
1435 ASSERT_EQ(WEOF, fgetwc(fp));
1436 ASSERT_EQ(EILSEQ, errno);
1437
1438 // Reverting to a valid position should work.
1439 ASSERT_EQ(0, fsetpos(fp, &mb_two_bytes_pos));
1440 ASSERT_EQ(mb_two_bytes, static_cast<wchar_t>(fgetwc(fp)));
1441
1442 // Moving withing a multi byte with fsetpos should work but reading should
1443 // produce an error.
1444 ASSERT_EQ(0, fsetpos(fp, &pos_inside_mb));
1445 ASSERT_EQ(WEOF, fgetwc(fp));
1446 ASSERT_EQ(EILSEQ, errno);
1447
Elliott Hughes3a4c4542017-07-19 17:20:24 -07001448 ASSERT_EQ(0, fclose(fp));
Calin Juravle03e4ebe2014-05-08 14:42:06 +01001449}
Elliott Hughes6b841db2014-08-20 16:10:49 -07001450
Christopher Ferris13f26a72016-01-13 13:47:58 -08001451TEST(STDIO_TEST, fmemopen) {
Elliott Hughes6b841db2014-08-20 16:10:49 -07001452 char buf[16];
1453 memset(buf, 0, sizeof(buf));
1454 FILE* fp = fmemopen(buf, sizeof(buf), "r+");
1455 ASSERT_EQ('<', fputc('<', fp));
1456 ASSERT_NE(EOF, fputs("abc>\n", fp));
1457 fflush(fp);
1458
Elliott Hughes3a4c4542017-07-19 17:20:24 -07001459 // We wrote to the buffer...
Elliott Hughes6b841db2014-08-20 16:10:49 -07001460 ASSERT_STREQ("<abc>\n", buf);
1461
Elliott Hughes3a4c4542017-07-19 17:20:24 -07001462 // And can read back from the file.
Elliott Hughes70715da2016-08-01 16:35:17 -07001463 AssertFileIs(fp, "<abc>\n", true);
Elliott Hughes3a4c4542017-07-19 17:20:24 -07001464 ASSERT_EQ(0, fclose(fp));
Elliott Hughes6b841db2014-08-20 16:10:49 -07001465}
1466
Elliott Hughes3a4c4542017-07-19 17:20:24 -07001467TEST(STDIO_TEST, fmemopen_nullptr) {
Elliott Hughes6b841db2014-08-20 16:10:49 -07001468 FILE* fp = fmemopen(nullptr, 128, "r+");
1469 ASSERT_NE(EOF, fputs("xyz\n", fp));
1470
Elliott Hughes70715da2016-08-01 16:35:17 -07001471 AssertFileIs(fp, "xyz\n", true);
Elliott Hughes3a4c4542017-07-19 17:20:24 -07001472 ASSERT_EQ(0, fclose(fp));
Elliott Hughes6b841db2014-08-20 16:10:49 -07001473}
1474
Elliott Hughes3a4c4542017-07-19 17:20:24 -07001475TEST(STDIO_TEST, fmemopen_trailing_NUL_byte) {
1476 FILE* fp;
1477 char buf[8];
1478
1479 // POSIX: "When a stream open for writing is flushed or closed, a null byte
1480 // shall be written at the current position or at the end of the buffer,
1481 // depending on the size of the contents."
1482 memset(buf, 'x', sizeof(buf));
1483 ASSERT_NE(nullptr, fp = fmemopen(buf, sizeof(buf), "w"));
1484 // Even with nothing written (and not in truncate mode), we'll flush a NUL...
1485 ASSERT_EQ(0, fflush(fp));
1486 EXPECT_EQ("\0xxxxxxx"s, std::string(buf, buf + sizeof(buf)));
1487 // Now write and check that the NUL moves along with our writes...
1488 ASSERT_NE(EOF, fputs("hello", fp));
1489 ASSERT_EQ(0, fflush(fp));
1490 EXPECT_EQ("hello\0xx"s, std::string(buf, buf + sizeof(buf)));
1491 ASSERT_NE(EOF, fputs("wo", fp));
1492 ASSERT_EQ(0, fflush(fp));
1493 EXPECT_EQ("hellowo\0"s, std::string(buf, buf + sizeof(buf)));
1494 ASSERT_EQ(0, fclose(fp));
1495
1496 // "If a stream open for update is flushed or closed and the last write has
1497 // advanced the current buffer size, a null byte shall be written at the end
1498 // of the buffer if it fits."
1499 memset(buf, 'x', sizeof(buf));
1500 ASSERT_NE(nullptr, fp = fmemopen(buf, sizeof(buf), "r+"));
1501 // Nothing written yet, so no advance...
1502 ASSERT_EQ(0, fflush(fp));
1503 EXPECT_EQ("xxxxxxxx"s, std::string(buf, buf + sizeof(buf)));
1504 ASSERT_NE(EOF, fputs("hello", fp));
1505 ASSERT_EQ(0, fclose(fp));
1506}
1507
1508TEST(STDIO_TEST, fmemopen_size) {
1509 FILE* fp;
Elliott Hughes6b841db2014-08-20 16:10:49 -07001510 char buf[16];
Elliott Hughes3a4c4542017-07-19 17:20:24 -07001511 memset(buf, 'x', sizeof(buf));
Elliott Hughes6b841db2014-08-20 16:10:49 -07001512
Elliott Hughes3a4c4542017-07-19 17:20:24 -07001513 // POSIX: "The stream shall also maintain the size of the current buffer
1514 // contents; use of fseek() or fseeko() on the stream with SEEK_END shall
1515 // seek relative to this size."
Elliott Hughes6b841db2014-08-20 16:10:49 -07001516
Elliott Hughes3a4c4542017-07-19 17:20:24 -07001517 // "For modes r and r+ the size shall be set to the value given by the size
1518 // argument."
1519 ASSERT_NE(nullptr, fp = fmemopen(buf, 16, "r"));
1520 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
1521 EXPECT_EQ(16, ftell(fp));
1522 EXPECT_EQ(16, ftello(fp));
1523 ASSERT_EQ(0, fseeko(fp, 0, SEEK_END));
1524 EXPECT_EQ(16, ftell(fp));
1525 EXPECT_EQ(16, ftello(fp));
1526 ASSERT_EQ(0, fclose(fp));
1527 ASSERT_NE(nullptr, fp = fmemopen(buf, 16, "r+"));
1528 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
1529 EXPECT_EQ(16, ftell(fp));
1530 EXPECT_EQ(16, ftello(fp));
1531 ASSERT_EQ(0, fseeko(fp, 0, SEEK_END));
1532 EXPECT_EQ(16, ftell(fp));
1533 EXPECT_EQ(16, ftello(fp));
1534 ASSERT_EQ(0, fclose(fp));
1535
1536 // "For modes w and w+ the initial size shall be zero..."
1537 ASSERT_NE(nullptr, fp = fmemopen(nullptr, 16, "w"));
1538 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
1539 EXPECT_EQ(0, ftell(fp));
1540 EXPECT_EQ(0, ftello(fp));
1541 ASSERT_EQ(0, fseeko(fp, 0, SEEK_END));
1542 EXPECT_EQ(0, ftell(fp));
1543 EXPECT_EQ(0, ftello(fp));
1544 ASSERT_EQ(0, fclose(fp));
1545 ASSERT_NE(nullptr, fp = fmemopen(nullptr, 16, "w+"));
1546 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
1547 EXPECT_EQ(0, ftell(fp));
1548 EXPECT_EQ(0, ftello(fp));
1549 ASSERT_EQ(0, fseeko(fp, 0, SEEK_END));
1550 EXPECT_EQ(0, ftell(fp));
1551 EXPECT_EQ(0, ftello(fp));
1552 ASSERT_EQ(0, fclose(fp));
1553
1554 // "...and for modes a and a+ the initial size shall be:
1555 // 1. Zero, if buf is a null pointer
1556 ASSERT_NE(nullptr, fp = fmemopen(nullptr, 16, "a"));
1557 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
1558 EXPECT_EQ(0, ftell(fp));
1559 EXPECT_EQ(0, ftello(fp));
1560 ASSERT_EQ(0, fseeko(fp, 0, SEEK_END));
1561 EXPECT_EQ(0, ftell(fp));
1562 EXPECT_EQ(0, ftello(fp));
1563 ASSERT_EQ(0, fclose(fp));
1564 ASSERT_NE(nullptr, fp = fmemopen(nullptr, 16, "a+"));
1565 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
1566 EXPECT_EQ(0, ftell(fp));
1567 EXPECT_EQ(0, ftello(fp));
1568 ASSERT_EQ(0, fseeko(fp, 0, SEEK_END));
1569 EXPECT_EQ(0, ftell(fp));
1570 EXPECT_EQ(0, ftello(fp));
1571 ASSERT_EQ(0, fclose(fp));
1572
1573 // 2. The position of the first null byte in the buffer, if one is found
1574 memset(buf, 'x', sizeof(buf));
1575 buf[3] = '\0';
1576 ASSERT_NE(nullptr, fp = fmemopen(buf, 16, "a"));
1577 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
1578 EXPECT_EQ(3, ftell(fp));
1579 EXPECT_EQ(3, ftello(fp));
1580 ASSERT_EQ(0, fseeko(fp, 0, SEEK_END));
1581 EXPECT_EQ(3, ftell(fp));
1582 EXPECT_EQ(3, ftello(fp));
1583 ASSERT_EQ(0, fclose(fp));
1584 memset(buf, 'x', sizeof(buf));
1585 buf[3] = '\0';
1586 ASSERT_NE(nullptr, fp = fmemopen(buf, 16, "a+"));
1587 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
1588 EXPECT_EQ(3, ftell(fp));
1589 EXPECT_EQ(3, ftello(fp));
1590 ASSERT_EQ(0, fseeko(fp, 0, SEEK_END));
1591 EXPECT_EQ(3, ftell(fp));
1592 EXPECT_EQ(3, ftello(fp));
1593 ASSERT_EQ(0, fclose(fp));
1594
1595 // 3. The value of the size argument, if buf is not a null pointer and no
1596 // null byte is found.
1597 memset(buf, 'x', sizeof(buf));
1598 ASSERT_NE(nullptr, fp = fmemopen(buf, 16, "a"));
1599 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
1600 EXPECT_EQ(16, ftell(fp));
1601 EXPECT_EQ(16, ftello(fp));
1602 ASSERT_EQ(0, fseeko(fp, 0, SEEK_END));
1603 EXPECT_EQ(16, ftell(fp));
1604 EXPECT_EQ(16, ftello(fp));
1605 ASSERT_EQ(0, fclose(fp));
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}
1616
1617TEST(STDIO_TEST, fmemopen_SEEK_END) {
1618 // fseek SEEK_END is relative to the current string length, not the buffer size.
1619 FILE* fp;
1620 char buf[8];
1621 memset(buf, 'x', sizeof(buf));
1622 strcpy(buf, "str");
1623 ASSERT_NE(nullptr, fp = fmemopen(buf, sizeof(buf), "w+"));
1624 ASSERT_NE(EOF, fputs("string", fp));
1625 EXPECT_EQ(0, fseek(fp, 0, SEEK_END));
1626 EXPECT_EQ(static_cast<long>(strlen("string")), ftell(fp));
1627 EXPECT_EQ(static_cast<off_t>(strlen("string")), ftello(fp));
1628 EXPECT_EQ(0, fclose(fp));
1629
1630 // glibc < 2.22 interpreted SEEK_END the wrong way round (subtracting rather
1631 // than adding).
1632 ASSERT_NE(nullptr, fp = fmemopen(buf, sizeof(buf), "w+"));
1633 ASSERT_NE(EOF, fputs("54321", fp));
1634 EXPECT_EQ(0, fseek(fp, -2, SEEK_END));
1635 EXPECT_EQ('2', fgetc(fp));
1636 EXPECT_EQ(0, fclose(fp));
1637}
1638
1639TEST(STDIO_TEST, fmemopen_seek_invalid) {
1640 char buf[8];
1641 memset(buf, 'x', sizeof(buf));
1642 FILE* fp = fmemopen(buf, sizeof(buf), "w");
1643 ASSERT_TRUE(fp != nullptr);
1644
1645 // POSIX: "An attempt to seek ... to a negative position or to a position
1646 // larger than the buffer size given in the size argument shall fail."
1647 // (There's no mention of what errno should be set to, and glibc doesn't
1648 // set errno in any of these cases.)
1649 EXPECT_EQ(-1, fseek(fp, -2, SEEK_SET));
1650 EXPECT_EQ(-1, fseeko(fp, -2, SEEK_SET));
1651 EXPECT_EQ(-1, fseek(fp, sizeof(buf) + 1, SEEK_SET));
1652 EXPECT_EQ(-1, fseeko(fp, sizeof(buf) + 1, SEEK_SET));
1653}
1654
1655TEST(STDIO_TEST, fmemopen_read_EOF) {
1656 // POSIX: "A read operation on the stream shall not advance the current
1657 // buffer position beyond the current buffer size."
1658 char buf[8];
1659 memset(buf, 'x', sizeof(buf));
1660 FILE* fp = fmemopen(buf, sizeof(buf), "r");
1661 ASSERT_TRUE(fp != nullptr);
1662 char buf2[BUFSIZ];
1663 ASSERT_EQ(8U, fread(buf2, 1, sizeof(buf2), fp));
1664 // POSIX: "Reaching the buffer size in a read operation shall count as
1665 // end-of-file.
1666 ASSERT_TRUE(feof(fp));
1667 ASSERT_EQ(EOF, fgetc(fp));
1668 ASSERT_EQ(0, fclose(fp));
1669}
1670
1671TEST(STDIO_TEST, fmemopen_read_null_bytes) {
1672 // POSIX: "Null bytes in the buffer shall have no special meaning for reads."
1673 char buf[] = "h\0e\0l\0l\0o";
1674 FILE* fp = fmemopen(buf, sizeof(buf), "r");
1675 ASSERT_TRUE(fp != nullptr);
1676 ASSERT_EQ('h', fgetc(fp));
1677 ASSERT_EQ(0, fgetc(fp));
1678 ASSERT_EQ('e', fgetc(fp));
1679 ASSERT_EQ(0, fgetc(fp));
1680 ASSERT_EQ('l', fgetc(fp));
1681 ASSERT_EQ(0, fgetc(fp));
1682 // POSIX: "The read operation shall start at the current buffer position of
1683 // the stream."
1684 char buf2[8];
1685 memset(buf2, 'x', sizeof(buf2));
1686 ASSERT_EQ(4U, fread(buf2, 1, sizeof(buf2), fp));
1687 ASSERT_EQ('l', buf2[0]);
1688 ASSERT_EQ(0, buf2[1]);
1689 ASSERT_EQ('o', buf2[2]);
1690 ASSERT_EQ(0, buf2[3]);
1691 for (size_t i = 4; i < sizeof(buf2); ++i) ASSERT_EQ('x', buf2[i]) << i;
1692 ASSERT_TRUE(feof(fp));
1693 ASSERT_EQ(0, fclose(fp));
1694}
1695
1696TEST(STDIO_TEST, fmemopen_write) {
1697 FILE* fp;
1698 char buf[8];
1699
1700 // POSIX: "A write operation shall start either at the current position of
1701 // the stream (if mode has not specified 'a' as the first character)..."
1702 memset(buf, 'x', sizeof(buf));
1703 ASSERT_NE(nullptr, fp = fmemopen(buf, sizeof(buf), "r+"));
1704 setbuf(fp, nullptr); // Turn off buffering so we can see what's happening as it happens.
1705 ASSERT_EQ(0, fseek(fp, 2, SEEK_SET));
1706 ASSERT_EQ(' ', fputc(' ', fp));
1707 EXPECT_EQ("xx xxxxx", std::string(buf, buf + sizeof(buf)));
1708 ASSERT_EQ(0, fclose(fp));
1709
1710 // "...or at the current size of the stream (if mode had 'a' as the first
1711 // character)." (See the fmemopen_size test for what "size" means, but for
1712 // mode "a", it's the first NUL byte.)
1713 memset(buf, 'x', sizeof(buf));
1714 buf[3] = '\0';
1715 ASSERT_NE(nullptr, fp = fmemopen(buf, sizeof(buf), "a+"));
1716 setbuf(fp, nullptr); // Turn off buffering so we can see what's happening as it happens.
1717 ASSERT_EQ(' ', fputc(' ', fp));
1718 EXPECT_EQ("xxx \0xxx"s, std::string(buf, buf + sizeof(buf)));
1719 ASSERT_EQ(0, fclose(fp));
1720
1721 // "If the current position at the end of the write is larger than the
1722 // current buffer size, the current buffer size shall be set to the current
1723 // position." (See the fmemopen_size test for what "size" means, but to
1724 // query it we SEEK_END with offset 0, and then ftell.)
1725 memset(buf, 'x', sizeof(buf));
1726 ASSERT_NE(nullptr, fp = fmemopen(buf, sizeof(buf), "w+"));
1727 setbuf(fp, nullptr); // Turn off buffering so we can see what's happening as it happens.
1728 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
1729 EXPECT_EQ(0, ftell(fp));
1730 ASSERT_EQ(' ', fputc(' ', fp));
1731 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
1732 EXPECT_EQ(1, ftell(fp));
1733 ASSERT_NE(EOF, fputs("123", fp));
1734 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
1735 EXPECT_EQ(4, ftell(fp));
1736 EXPECT_EQ(" 123\0xxx"s, std::string(buf, buf + sizeof(buf)));
1737 ASSERT_EQ(0, fclose(fp));
1738}
1739
1740TEST(STDIO_TEST, fmemopen_write_EOF) {
1741 // POSIX: "A write operation on the stream shall not advance the current
1742 // buffer size beyond the size given in the size argument."
1743 FILE* fp;
1744
1745 // Scalar writes...
1746 ASSERT_NE(nullptr, fp = fmemopen(nullptr, 4, "w"));
1747 setbuf(fp, nullptr); // Turn off buffering so we can see what's happening as it happens.
1748 ASSERT_EQ('x', fputc('x', fp));
1749 ASSERT_EQ('x', fputc('x', fp));
1750 ASSERT_EQ('x', fputc('x', fp));
1751 ASSERT_EQ(EOF, fputc('x', fp)); // Only 3 fit because of the implicit NUL.
1752 ASSERT_EQ(0, fclose(fp));
1753
1754 // Vector 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(3U, fwrite("xxxx", 1, 4, fp));
1758 ASSERT_EQ(0, fclose(fp));
1759}
1760
1761TEST(STDIO_TEST, fmemopen_initial_position) {
1762 // POSIX: "The ... current position in the buffer ... shall be initially
1763 // set to either the beginning of the buffer (for r and w modes) ..."
1764 char buf[] = "hello\0world";
1765 FILE* fp;
1766 ASSERT_NE(nullptr, fp = fmemopen(buf, sizeof(buf), "r"));
1767 EXPECT_EQ(0L, ftell(fp));
1768 EXPECT_EQ(0, fclose(fp));
1769 ASSERT_NE(nullptr, fp = fmemopen(buf, sizeof(buf), "w"));
1770 EXPECT_EQ(0L, ftell(fp));
1771 EXPECT_EQ(0, fclose(fp));
1772 buf[0] = 'h'; // (Undo the effects of the above.)
1773
1774 // POSIX: "...or to the first null byte in the buffer (for a modes)."
1775 ASSERT_NE(nullptr, fp = fmemopen(buf, sizeof(buf), "a"));
1776 EXPECT_EQ(5L, ftell(fp));
1777 EXPECT_EQ(0, fclose(fp));
1778
1779 // POSIX: "If no null byte is found in append mode, the initial position
1780 // shall be set to one byte after the end of the buffer."
1781 memset(buf, 'x', sizeof(buf));
1782 ASSERT_NE(nullptr, fp = fmemopen(buf, sizeof(buf), "a"));
1783 EXPECT_EQ(static_cast<long>(sizeof(buf)), ftell(fp));
1784 EXPECT_EQ(0, fclose(fp));
1785}
1786
1787TEST(STDIO_TEST, fmemopen_initial_position_allocated) {
1788 // POSIX: "If buf is a null pointer, the initial position shall always be
1789 // set to the beginning of the buffer."
1790 FILE* fp = fmemopen(nullptr, 128, "a+");
1791 ASSERT_TRUE(fp != nullptr);
1792 EXPECT_EQ(0L, ftell(fp));
1793 EXPECT_EQ(0L, fseek(fp, 0, SEEK_SET));
1794 EXPECT_EQ(0, fclose(fp));
1795}
1796
1797TEST(STDIO_TEST, fmemopen_zero_length) {
1798 // POSIX says it's up to the implementation whether or not you can have a
1799 // zero-length buffer (but "A future version of this standard may require
1800 // support of zero-length buffer streams explicitly"). BSD and glibc < 2.22
1801 // agreed that you couldn't, but glibc >= 2.22 allows it for consistency.
1802 FILE* fp;
1803 char buf[16];
1804 ASSERT_NE(nullptr, fp = fmemopen(buf, 0, "r+"));
1805 ASSERT_EQ(EOF, fgetc(fp));
1806 ASSERT_TRUE(feof(fp));
1807 ASSERT_EQ(0, fclose(fp));
1808 ASSERT_NE(nullptr, fp = fmemopen(nullptr, 0, "r+"));
1809 ASSERT_EQ(EOF, fgetc(fp));
1810 ASSERT_TRUE(feof(fp));
1811 ASSERT_EQ(0, fclose(fp));
1812
1813 ASSERT_NE(nullptr, fp = fmemopen(buf, 0, "w+"));
1814 setbuf(fp, nullptr); // Turn off buffering so we can see what's happening as it happens.
1815 ASSERT_EQ(EOF, fputc('x', fp));
1816 ASSERT_EQ(0, fclose(fp));
1817 ASSERT_NE(nullptr, fp = fmemopen(nullptr, 0, "w+"));
1818 setbuf(fp, nullptr); // Turn off buffering so we can see what's happening as it happens.
1819 ASSERT_EQ(EOF, fputc('x', fp));
1820 ASSERT_EQ(0, fclose(fp));
1821}
1822
Elliott Hughes288465d2019-02-05 15:00:13 -08001823TEST(STDIO_TEST, fmemopen_zero_length_buffer_overrun) {
1824 char buf[2] = "x";
1825 ASSERT_EQ('x', buf[0]);
1826 FILE* fp = fmemopen(buf, 0, "w");
1827 ASSERT_EQ('x', buf[0]);
1828 ASSERT_EQ(0, fclose(fp));
1829}
1830
Elliott Hughes3a4c4542017-07-19 17:20:24 -07001831TEST(STDIO_TEST, fmemopen_write_only_allocated) {
1832 // POSIX says fmemopen "may fail if the mode argument does not include a '+'".
1833 // BSD fails, glibc doesn't. We side with the more lenient.
1834 FILE* fp;
1835 ASSERT_NE(nullptr, fp = fmemopen(nullptr, 16, "r"));
1836 ASSERT_EQ(0, fclose(fp));
1837 ASSERT_NE(nullptr, fp = fmemopen(nullptr, 16, "w"));
1838 ASSERT_EQ(0, fclose(fp));
1839}
1840
1841TEST(STDIO_TEST, fmemopen_fileno) {
1842 // There's no fd backing an fmemopen FILE*.
1843 FILE* fp = fmemopen(nullptr, 16, "r");
1844 ASSERT_TRUE(fp != nullptr);
Elliott Hughes6b841db2014-08-20 16:10:49 -07001845 errno = 0;
Elliott Hughes3a4c4542017-07-19 17:20:24 -07001846 ASSERT_EQ(-1, fileno(fp));
1847 ASSERT_EQ(EBADF, errno);
1848 ASSERT_EQ(0, fclose(fp));
1849}
1850
1851TEST(STDIO_TEST, fmemopen_append_after_seek) {
1852 // In BSD and glibc < 2.22, append mode didn't force writes to append if
1853 // there had been an intervening seek.
1854
1855 FILE* fp;
1856 char buf[] = "hello\0world";
1857 ASSERT_NE(nullptr, fp = fmemopen(buf, sizeof(buf), "a"));
1858 setbuf(fp, nullptr); // Turn off buffering so we can see what's happening as it happens.
1859 ASSERT_EQ(0, fseek(fp, 0, SEEK_SET));
1860 ASSERT_NE(EOF, fputc('!', fp));
1861 EXPECT_EQ("hello!\0orld\0"s, std::string(buf, buf + sizeof(buf)));
1862 ASSERT_EQ(0, fclose(fp));
1863
1864 memcpy(buf, "hello\0world", sizeof(buf));
1865 ASSERT_NE(nullptr, fp = fmemopen(buf, sizeof(buf), "a+"));
1866 setbuf(fp, nullptr); // Turn off buffering so we can see what's happening as it happens.
1867 ASSERT_EQ(0, fseek(fp, 0, SEEK_SET));
1868 ASSERT_NE(EOF, fputc('!', fp));
1869 EXPECT_EQ("hello!\0orld\0"s, std::string(buf, buf + sizeof(buf)));
1870 ASSERT_EQ(0, fclose(fp));
Elliott Hughes6b841db2014-08-20 16:10:49 -07001871}
1872
Christopher Ferris13f26a72016-01-13 13:47:58 -08001873TEST(STDIO_TEST, open_memstream) {
Elliott Hughes6b841db2014-08-20 16:10:49 -07001874 char* p = nullptr;
1875 size_t size = 0;
1876 FILE* fp = open_memstream(&p, &size);
1877 ASSERT_NE(EOF, fputs("hello, world!", fp));
1878 fclose(fp);
1879
1880 ASSERT_STREQ("hello, world!", p);
1881 ASSERT_EQ(strlen("hello, world!"), size);
1882 free(p);
1883}
1884
Christopher Ferris13f26a72016-01-13 13:47:58 -08001885TEST(STDIO_TEST, open_memstream_EINVAL) {
Elliott Hughes6b841db2014-08-20 16:10:49 -07001886#if defined(__BIONIC__)
1887 char* p;
1888 size_t size;
1889
1890 // Invalid buffer.
1891 errno = 0;
1892 ASSERT_EQ(nullptr, open_memstream(nullptr, &size));
1893 ASSERT_EQ(EINVAL, errno);
1894
1895 // Invalid size.
1896 errno = 0;
1897 ASSERT_EQ(nullptr, open_memstream(&p, nullptr));
1898 ASSERT_EQ(EINVAL, errno);
1899#else
Elliott Hughesbcaa4542019-03-08 15:20:23 -08001900 GTEST_SKIP() << "glibc is broken";
Elliott Hughes6b841db2014-08-20 16:10:49 -07001901#endif
1902}
Elliott Hughes31165ed2014-09-23 17:34:29 -07001903
Christopher Ferris13f26a72016-01-13 13:47:58 -08001904TEST(STDIO_TEST, fdopen_CLOEXEC) {
Elliott Hughes31165ed2014-09-23 17:34:29 -07001905 int fd = open("/proc/version", O_RDONLY);
1906 ASSERT_TRUE(fd != -1);
1907
1908 // This fd doesn't have O_CLOEXEC...
Elliott Hughesa7f12942017-12-15 13:55:53 -08001909 AssertCloseOnExec(fd, false);
Elliott Hughes31165ed2014-09-23 17:34:29 -07001910
1911 FILE* fp = fdopen(fd, "re");
Yi Kong32bc0fc2018-08-02 17:31:13 -07001912 ASSERT_TRUE(fp != nullptr);
Elliott Hughes31165ed2014-09-23 17:34:29 -07001913
1914 // ...but the new one does.
Elliott Hughesa7f12942017-12-15 13:55:53 -08001915 AssertCloseOnExec(fileno(fp), true);
Elliott Hughes31165ed2014-09-23 17:34:29 -07001916
1917 fclose(fp);
Elliott Hughes31165ed2014-09-23 17:34:29 -07001918}
1919
Christopher Ferris13f26a72016-01-13 13:47:58 -08001920TEST(STDIO_TEST, freopen_CLOEXEC) {
Elliott Hughes31165ed2014-09-23 17:34:29 -07001921 FILE* fp = fopen("/proc/version", "r");
Yi Kong32bc0fc2018-08-02 17:31:13 -07001922 ASSERT_TRUE(fp != nullptr);
Elliott Hughes31165ed2014-09-23 17:34:29 -07001923
1924 // This FILE* doesn't have O_CLOEXEC...
Elliott Hughesa7f12942017-12-15 13:55:53 -08001925 AssertCloseOnExec(fileno(fp), false);
Elliott Hughes31165ed2014-09-23 17:34:29 -07001926
1927 fp = freopen("/proc/version", "re", fp);
1928
1929 // ...but the new one does.
Elliott Hughesa7f12942017-12-15 13:55:53 -08001930 AssertCloseOnExec(fileno(fp), true);
Elliott Hughes31165ed2014-09-23 17:34:29 -07001931
1932 fclose(fp);
1933}
Elliott Hughes20841a12014-12-01 16:13:30 -08001934
Elliott Hughesf226ee52016-02-03 11:24:28 -08001935TEST(STDIO_TEST, fopen64_freopen64) {
1936 FILE* fp = fopen64("/proc/version", "r");
1937 ASSERT_TRUE(fp != nullptr);
1938 fp = freopen64("/proc/version", "re", fp);
1939 ASSERT_TRUE(fp != nullptr);
1940 fclose(fp);
1941}
1942
Elliott Hughes20841a12014-12-01 16:13:30 -08001943// https://code.google.com/p/android/issues/detail?id=81155
1944// http://b/18556607
Christopher Ferris13f26a72016-01-13 13:47:58 -08001945TEST(STDIO_TEST, fread_unbuffered_pathological_performance) {
Elliott Hughes20841a12014-12-01 16:13:30 -08001946 FILE* fp = fopen("/dev/zero", "r");
Yi Kong32bc0fc2018-08-02 17:31:13 -07001947 ASSERT_TRUE(fp != nullptr);
Elliott Hughes20841a12014-12-01 16:13:30 -08001948
1949 // Make this stream unbuffered.
Yi Kong32bc0fc2018-08-02 17:31:13 -07001950 setvbuf(fp, nullptr, _IONBF, 0);
Elliott Hughes20841a12014-12-01 16:13:30 -08001951
1952 char buf[65*1024];
1953 memset(buf, 0xff, sizeof(buf));
1954
Yi Kong32bc0fc2018-08-02 17:31:13 -07001955 time_t t0 = time(nullptr);
Elliott Hughes20841a12014-12-01 16:13:30 -08001956 for (size_t i = 0; i < 1024; ++i) {
Elliott Hughese6bb5a22015-01-23 17:48:15 -08001957 ASSERT_EQ(1U, fread(buf, 64*1024, 1, fp));
Elliott Hughes20841a12014-12-01 16:13:30 -08001958 }
Yi Kong32bc0fc2018-08-02 17:31:13 -07001959 time_t t1 = time(nullptr);
Elliott Hughes20841a12014-12-01 16:13:30 -08001960
1961 fclose(fp);
1962
1963 // 1024 64KiB reads should have been very quick.
1964 ASSERT_LE(t1 - t0, 1);
1965
1966 for (size_t i = 0; i < 64*1024; ++i) {
1967 ASSERT_EQ('\0', buf[i]);
1968 }
1969 for (size_t i = 64*1024; i < 65*1024; ++i) {
1970 ASSERT_EQ('\xff', buf[i]);
1971 }
1972}
Elliott Hughes75b99382015-01-20 11:23:50 -08001973
Christopher Ferris13f26a72016-01-13 13:47:58 -08001974TEST(STDIO_TEST, fread_EOF) {
Elliott Hughes0ed7e082015-01-22 15:13:38 -08001975 std::string digits("0123456789");
1976 FILE* fp = fmemopen(&digits[0], digits.size(), "r");
Elliott Hughes75b99382015-01-20 11:23:50 -08001977
1978 // Try to read too much, but little enough that it still fits in the FILE's internal buffer.
1979 char buf1[4 * 4];
1980 memset(buf1, 0, sizeof(buf1));
1981 ASSERT_EQ(2U, fread(buf1, 4, 4, fp));
Elliott Hughes0ed7e082015-01-22 15:13:38 -08001982 ASSERT_STREQ("0123456789", buf1);
Elliott Hughes75b99382015-01-20 11:23:50 -08001983 ASSERT_TRUE(feof(fp));
1984
1985 rewind(fp);
1986
Elliott Hughes0ed7e082015-01-22 15:13:38 -08001987 // Try to read way too much so stdio tries to read more direct from the stream.
1988 char buf2[4 * 4096];
Elliott Hughes75b99382015-01-20 11:23:50 -08001989 memset(buf2, 0, sizeof(buf2));
1990 ASSERT_EQ(2U, fread(buf2, 4, 4096, fp));
Elliott Hughes0ed7e082015-01-22 15:13:38 -08001991 ASSERT_STREQ("0123456789", buf2);
Elliott Hughes75b99382015-01-20 11:23:50 -08001992 ASSERT_TRUE(feof(fp));
1993
1994 fclose(fp);
1995}
Elliott Hughese6bb5a22015-01-23 17:48:15 -08001996
1997static void test_fread_from_write_only_stream(size_t n) {
1998 FILE* fp = fopen("/dev/null", "w");
1999 std::vector<char> buf(n, 0);
2000 errno = 0;
2001 ASSERT_EQ(0U, fread(&buf[0], n, 1, fp));
2002 ASSERT_EQ(EBADF, errno);
2003 ASSERT_TRUE(ferror(fp));
2004 ASSERT_FALSE(feof(fp));
2005 fclose(fp);
2006}
2007
Christopher Ferris13f26a72016-01-13 13:47:58 -08002008TEST(STDIO_TEST, fread_from_write_only_stream_slow_path) {
Elliott Hughese6bb5a22015-01-23 17:48:15 -08002009 test_fread_from_write_only_stream(1);
2010}
2011
Christopher Ferris13f26a72016-01-13 13:47:58 -08002012TEST(STDIO_TEST, fread_from_write_only_stream_fast_path) {
Elliott Hughese6bb5a22015-01-23 17:48:15 -08002013 test_fread_from_write_only_stream(64*1024);
2014}
2015
2016static void test_fwrite_after_fread(size_t n) {
2017 TemporaryFile tf;
2018
2019 FILE* fp = fdopen(tf.fd, "w+");
2020 ASSERT_EQ(1U, fwrite("1", 1, 1, fp));
2021 fflush(fp);
2022
2023 // We've flushed but not rewound, so there's nothing to read.
2024 std::vector<char> buf(n, 0);
2025 ASSERT_EQ(0U, fread(&buf[0], 1, buf.size(), fp));
2026 ASSERT_TRUE(feof(fp));
2027
2028 // But hitting EOF doesn't prevent us from writing...
2029 errno = 0;
Elliott Hughes9677fab2016-01-25 15:50:59 -08002030 ASSERT_EQ(1U, fwrite("2", 1, 1, fp)) << strerror(errno);
Elliott Hughese6bb5a22015-01-23 17:48:15 -08002031
2032 // And if we rewind, everything's there.
2033 rewind(fp);
2034 ASSERT_EQ(2U, fread(&buf[0], 1, buf.size(), fp));
2035 ASSERT_EQ('1', buf[0]);
2036 ASSERT_EQ('2', buf[1]);
2037
2038 fclose(fp);
2039}
2040
Christopher Ferris13f26a72016-01-13 13:47:58 -08002041TEST(STDIO_TEST, fwrite_after_fread_slow_path) {
Elliott Hughese6bb5a22015-01-23 17:48:15 -08002042 test_fwrite_after_fread(16);
2043}
2044
Christopher Ferris13f26a72016-01-13 13:47:58 -08002045TEST(STDIO_TEST, fwrite_after_fread_fast_path) {
Elliott Hughese6bb5a22015-01-23 17:48:15 -08002046 test_fwrite_after_fread(64*1024);
2047}
Christopher Ferriscc9ca102015-02-27 18:22:45 -08002048
2049// http://b/19172514
Christopher Ferris13f26a72016-01-13 13:47:58 -08002050TEST(STDIO_TEST, fread_after_fseek) {
Christopher Ferriscc9ca102015-02-27 18:22:45 -08002051 TemporaryFile tf;
2052
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -08002053 FILE* fp = fopen(tf.path, "w+");
Christopher Ferriscc9ca102015-02-27 18:22:45 -08002054 ASSERT_TRUE(fp != nullptr);
2055
2056 char file_data[12288];
2057 for (size_t i = 0; i < 12288; i++) {
2058 file_data[i] = i;
2059 }
2060 ASSERT_EQ(12288U, fwrite(file_data, 1, 12288, fp));
2061 fclose(fp);
2062
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -08002063 fp = fopen(tf.path, "r");
Christopher Ferriscc9ca102015-02-27 18:22:45 -08002064 ASSERT_TRUE(fp != nullptr);
2065
2066 char buffer[8192];
2067 size_t cur_location = 0;
2068 // Small read to populate internal buffer.
2069 ASSERT_EQ(100U, fread(buffer, 1, 100, fp));
2070 ASSERT_EQ(memcmp(file_data, buffer, 100), 0);
2071
2072 cur_location = static_cast<size_t>(ftell(fp));
2073 // Large read to force reading into the user supplied buffer and bypassing
2074 // the internal buffer.
2075 ASSERT_EQ(8192U, fread(buffer, 1, 8192, fp));
2076 ASSERT_EQ(memcmp(file_data+cur_location, buffer, 8192), 0);
2077
2078 // Small backwards seek to verify fseek does not reuse the internal buffer.
Elliott Hughes9677fab2016-01-25 15:50:59 -08002079 ASSERT_EQ(0, fseek(fp, -22, SEEK_CUR)) << strerror(errno);
Christopher Ferriscc9ca102015-02-27 18:22:45 -08002080 cur_location = static_cast<size_t>(ftell(fp));
2081 ASSERT_EQ(22U, fread(buffer, 1, 22, fp));
2082 ASSERT_EQ(memcmp(file_data+cur_location, buffer, 22), 0);
2083
2084 fclose(fp);
2085}
Elliott Hughes8ab433d2015-10-09 17:57:26 -07002086
2087// https://code.google.com/p/android/issues/detail?id=184847
Christopher Ferris13f26a72016-01-13 13:47:58 -08002088TEST(STDIO_TEST, fread_EOF_184847) {
Elliott Hughes8ab433d2015-10-09 17:57:26 -07002089 TemporaryFile tf;
2090 char buf[6] = {0};
2091
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -08002092 FILE* fw = fopen(tf.path, "w");
Elliott Hughes8ab433d2015-10-09 17:57:26 -07002093 ASSERT_TRUE(fw != nullptr);
2094
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -08002095 FILE* fr = fopen(tf.path, "r");
Elliott Hughes8ab433d2015-10-09 17:57:26 -07002096 ASSERT_TRUE(fr != nullptr);
2097
2098 fwrite("a", 1, 1, fw);
2099 fflush(fw);
2100 ASSERT_EQ(1U, fread(buf, 1, 1, fr));
2101 ASSERT_STREQ("a", buf);
2102
2103 // 'fr' is now at EOF.
2104 ASSERT_EQ(0U, fread(buf, 1, 1, fr));
2105 ASSERT_TRUE(feof(fr));
2106
2107 // Write some more...
2108 fwrite("z", 1, 1, fw);
2109 fflush(fw);
2110
2111 // ...and check that we can read it back.
2112 // (BSD thinks that once a stream has hit EOF, it must always return EOF. SysV disagrees.)
2113 ASSERT_EQ(1U, fread(buf, 1, 1, fr));
2114 ASSERT_STREQ("z", buf);
2115
2116 // But now we're done.
2117 ASSERT_EQ(0U, fread(buf, 1, 1, fr));
2118
2119 fclose(fr);
2120 fclose(fw);
2121}
Elliott Hughes923f1652016-01-19 15:46:05 -08002122
2123TEST(STDIO_TEST, fclose_invalidates_fd) {
2124 // The typical error we're trying to help people catch involves accessing
2125 // memory after it's been freed. But we know that stdin/stdout/stderr are
2126 // special and don't get deallocated, so this test uses stdin.
2127 ASSERT_EQ(0, fclose(stdin));
2128
2129 // Even though using a FILE* after close is undefined behavior, I've closed
2130 // this bug as "WAI" too many times. We shouldn't hand out stale fds,
2131 // especially because they might actually correspond to a real stream.
2132 errno = 0;
2133 ASSERT_EQ(-1, fileno(stdin));
2134 ASSERT_EQ(EBADF, errno);
2135}
Elliott Hughes2704bd12016-01-20 17:14:53 -08002136
2137TEST(STDIO_TEST, fseek_ftell_unseekable) {
2138#if defined(__BIONIC__) // glibc has fopencookie instead.
2139 auto read_fn = [](void*, char*, int) { return -1; };
2140 FILE* fp = funopen(nullptr, read_fn, nullptr, nullptr, nullptr);
2141 ASSERT_TRUE(fp != nullptr);
2142
2143 // Check that ftell balks on an unseekable FILE*.
2144 errno = 0;
2145 ASSERT_EQ(-1, ftell(fp));
2146 ASSERT_EQ(ESPIPE, errno);
2147
2148 // SEEK_CUR is rewritten as SEEK_SET internally...
2149 errno = 0;
2150 ASSERT_EQ(-1, fseek(fp, 0, SEEK_CUR));
2151 ASSERT_EQ(ESPIPE, errno);
2152
2153 // ...so it's worth testing the direct seek path too.
2154 errno = 0;
2155 ASSERT_EQ(-1, fseek(fp, 0, SEEK_SET));
2156 ASSERT_EQ(ESPIPE, errno);
2157
2158 fclose(fp);
Elliott Hughes03e65eb2016-01-26 14:13:04 -08002159#else
Elliott Hughesbcaa4542019-03-08 15:20:23 -08002160 GTEST_SKIP() << "glibc uses fopencookie instead";
Elliott Hughes03e65eb2016-01-26 14:13:04 -08002161#endif
2162}
2163
2164TEST(STDIO_TEST, funopen_EINVAL) {
2165#if defined(__BIONIC__)
2166 errno = 0;
2167 ASSERT_EQ(nullptr, funopen(nullptr, nullptr, nullptr, nullptr, nullptr));
2168 ASSERT_EQ(EINVAL, errno);
2169#else
Elliott Hughesbcaa4542019-03-08 15:20:23 -08002170 GTEST_SKIP() << "glibc uses fopencookie instead";
Elliott Hughes03e65eb2016-01-26 14:13:04 -08002171#endif
2172}
2173
2174TEST(STDIO_TEST, funopen_seek) {
2175#if defined(__BIONIC__)
2176 auto read_fn = [](void*, char*, int) { return -1; };
2177
2178 auto seek_fn = [](void*, fpos_t, int) -> fpos_t { return 0xfedcba12; };
2179 auto seek64_fn = [](void*, fpos64_t, int) -> fpos64_t { return 0xfedcba12345678; };
2180
2181 FILE* fp = funopen(nullptr, read_fn, nullptr, seek_fn, nullptr);
2182 ASSERT_TRUE(fp != nullptr);
2183 fpos_t pos;
Elliott Hughes955426e2016-01-26 18:25:52 -08002184#if defined(__LP64__)
2185 EXPECT_EQ(0, fgetpos(fp, &pos)) << strerror(errno);
2186 EXPECT_EQ(0xfedcba12LL, pos);
2187#else
2188 EXPECT_EQ(-1, fgetpos(fp, &pos)) << strerror(errno);
2189 EXPECT_EQ(EOVERFLOW, errno);
2190#endif
Elliott Hughes03e65eb2016-01-26 14:13:04 -08002191
2192 FILE* fp64 = funopen64(nullptr, read_fn, nullptr, seek64_fn, nullptr);
2193 ASSERT_TRUE(fp64 != nullptr);
2194 fpos64_t pos64;
Elliott Hughes955426e2016-01-26 18:25:52 -08002195 EXPECT_EQ(0, fgetpos64(fp64, &pos64)) << strerror(errno);
2196 EXPECT_EQ(0xfedcba12345678, pos64);
Elliott Hughes03e65eb2016-01-26 14:13:04 -08002197#else
Elliott Hughesbcaa4542019-03-08 15:20:23 -08002198 GTEST_SKIP() << "glibc uses fopencookie instead";
Elliott Hughes2704bd12016-01-20 17:14:53 -08002199#endif
2200}
Elliott Hughes71288cb2016-01-22 19:22:44 -08002201
2202TEST(STDIO_TEST, lots_of_concurrent_files) {
2203 std::vector<TemporaryFile*> tfs;
2204 std::vector<FILE*> fps;
2205
2206 for (size_t i = 0; i < 256; ++i) {
2207 TemporaryFile* tf = new TemporaryFile;
2208 tfs.push_back(tf);
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -08002209 FILE* fp = fopen(tf->path, "w+");
Elliott Hughes71288cb2016-01-22 19:22:44 -08002210 fps.push_back(fp);
2211 fprintf(fp, "hello %zu!\n", i);
2212 fflush(fp);
2213 }
2214
2215 for (size_t i = 0; i < 256; ++i) {
Elliott Hughes71288cb2016-01-22 19:22:44 -08002216 char expected[BUFSIZ];
2217 snprintf(expected, sizeof(expected), "hello %zu!\n", i);
Elliott Hughes71288cb2016-01-22 19:22:44 -08002218
Elliott Hughes70715da2016-08-01 16:35:17 -07002219 AssertFileIs(fps[i], expected);
Elliott Hughes71288cb2016-01-22 19:22:44 -08002220 fclose(fps[i]);
2221 delete tfs[i];
2222 }
2223}
Elliott Hughes9677fab2016-01-25 15:50:59 -08002224
2225static void AssertFileOffsetAt(FILE* fp, off64_t offset) {
2226 EXPECT_EQ(offset, ftell(fp));
2227 EXPECT_EQ(offset, ftello(fp));
Elliott Hughese4fa6e92016-02-02 22:39:15 -08002228 EXPECT_EQ(offset, ftello64(fp));
Elliott Hughes9677fab2016-01-25 15:50:59 -08002229 fpos_t pos;
2230 fpos64_t pos64;
2231 EXPECT_EQ(0, fgetpos(fp, &pos));
2232 EXPECT_EQ(0, fgetpos64(fp, &pos64));
2233#if defined(__BIONIC__)
2234 EXPECT_EQ(offset, static_cast<off64_t>(pos));
2235 EXPECT_EQ(offset, static_cast<off64_t>(pos64));
2236#else
Elliott Hughesbcaa4542019-03-08 15:20:23 -08002237 GTEST_SKIP() << "glibc's fpos_t is opaque";
Elliott Hughes9677fab2016-01-25 15:50:59 -08002238#endif
2239}
2240
2241TEST(STDIO_TEST, seek_tell_family_smoke) {
2242 TemporaryFile tf;
2243 FILE* fp = fdopen(tf.fd, "w+");
2244
2245 // Initially we should be at 0.
2246 AssertFileOffsetAt(fp, 0);
2247
2248 // Seek to offset 8192.
2249 ASSERT_EQ(0, fseek(fp, 8192, SEEK_SET));
2250 AssertFileOffsetAt(fp, 8192);
2251 fpos_t eight_k_pos;
2252 ASSERT_EQ(0, fgetpos(fp, &eight_k_pos));
2253
2254 // Seek forward another 8192...
2255 ASSERT_EQ(0, fseek(fp, 8192, SEEK_CUR));
2256 AssertFileOffsetAt(fp, 8192 + 8192);
2257 fpos64_t sixteen_k_pos64;
2258 ASSERT_EQ(0, fgetpos64(fp, &sixteen_k_pos64));
2259
2260 // Seek back 8192...
2261 ASSERT_EQ(0, fseek(fp, -8192, SEEK_CUR));
2262 AssertFileOffsetAt(fp, 8192);
2263
2264 // Since we haven't written anything, the end is also at 0.
2265 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
2266 AssertFileOffsetAt(fp, 0);
2267
2268 // Check that our fpos64_t from 16KiB works...
2269 ASSERT_EQ(0, fsetpos64(fp, &sixteen_k_pos64));
2270 AssertFileOffsetAt(fp, 8192 + 8192);
2271 // ...as does our fpos_t from 8192.
2272 ASSERT_EQ(0, fsetpos(fp, &eight_k_pos));
2273 AssertFileOffsetAt(fp, 8192);
2274
2275 // Do fseeko and fseeko64 work too?
2276 ASSERT_EQ(0, fseeko(fp, 1234, SEEK_SET));
2277 AssertFileOffsetAt(fp, 1234);
2278 ASSERT_EQ(0, fseeko64(fp, 5678, SEEK_SET));
2279 AssertFileOffsetAt(fp, 5678);
2280
2281 fclose(fp);
2282}
2283
2284TEST(STDIO_TEST, fseek_fseeko_EINVAL) {
2285 TemporaryFile tf;
2286 FILE* fp = fdopen(tf.fd, "w+");
2287
2288 // Bad whence.
2289 errno = 0;
2290 ASSERT_EQ(-1, fseek(fp, 0, 123));
2291 ASSERT_EQ(EINVAL, errno);
2292 errno = 0;
2293 ASSERT_EQ(-1, fseeko(fp, 0, 123));
2294 ASSERT_EQ(EINVAL, errno);
2295 errno = 0;
2296 ASSERT_EQ(-1, fseeko64(fp, 0, 123));
2297 ASSERT_EQ(EINVAL, errno);
2298
2299 // Bad offset.
2300 errno = 0;
2301 ASSERT_EQ(-1, fseek(fp, -1, SEEK_SET));
2302 ASSERT_EQ(EINVAL, errno);
2303 errno = 0;
2304 ASSERT_EQ(-1, fseeko(fp, -1, SEEK_SET));
2305 ASSERT_EQ(EINVAL, errno);
2306 errno = 0;
2307 ASSERT_EQ(-1, fseeko64(fp, -1, SEEK_SET));
2308 ASSERT_EQ(EINVAL, errno);
2309
2310 fclose(fp);
2311}
Elliott Hughes20788ae2016-06-09 15:16:32 -07002312
2313TEST(STDIO_TEST, ctermid) {
2314 ASSERT_STREQ("/dev/tty", ctermid(nullptr));
2315
2316 char buf[L_ctermid] = {};
2317 ASSERT_EQ(buf, ctermid(buf));
2318 ASSERT_STREQ("/dev/tty", buf);
2319}
Elliott Hughesd1f25a72016-08-05 15:53:03 -07002320
2321TEST(STDIO_TEST, remove) {
2322 struct stat sb;
2323
2324 TemporaryFile tf;
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -08002325 ASSERT_EQ(0, remove(tf.path));
2326 ASSERT_EQ(-1, lstat(tf.path, &sb));
Elliott Hughesd1f25a72016-08-05 15:53:03 -07002327 ASSERT_EQ(ENOENT, errno);
2328
2329 TemporaryDir td;
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -08002330 ASSERT_EQ(0, remove(td.path));
2331 ASSERT_EQ(-1, lstat(td.path, &sb));
Elliott Hughesd1f25a72016-08-05 15:53:03 -07002332 ASSERT_EQ(ENOENT, errno);
2333
2334 errno = 0;
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -08002335 ASSERT_EQ(-1, remove(tf.path));
Elliott Hughesd1f25a72016-08-05 15:53:03 -07002336 ASSERT_EQ(ENOENT, errno);
2337
2338 errno = 0;
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -08002339 ASSERT_EQ(-1, remove(td.path));
Elliott Hughesd1f25a72016-08-05 15:53:03 -07002340 ASSERT_EQ(ENOENT, errno);
2341}
Elliott Hughesfb3873d2016-08-10 11:07:54 -07002342
2343TEST(STDIO_DEATHTEST, snprintf_30445072_known_buffer_size) {
2344 char buf[16];
2345 ASSERT_EXIT(snprintf(buf, atol("-1"), "hello"),
2346 testing::KilledBySignal(SIGABRT),
2347#if defined(NOFORTIFY)
2348 "FORTIFY: vsnprintf: size .* > SSIZE_MAX"
2349#else
2350 "FORTIFY: vsnprintf: prevented .*-byte write into 16-byte buffer"
2351#endif
2352 );
2353}
2354
2355TEST(STDIO_DEATHTEST, snprintf_30445072_unknown_buffer_size) {
2356 std::string buf = "world";
2357 ASSERT_EXIT(snprintf(&buf[0], atol("-1"), "hello"),
2358 testing::KilledBySignal(SIGABRT),
2359 "FORTIFY: vsnprintf: size .* > SSIZE_MAX");
2360}
2361
2362TEST(STDIO_TEST, sprintf_30445072) {
2363 std::string buf = "world";
2364 sprintf(&buf[0], "hello");
2365 ASSERT_EQ(buf, "hello");
2366}
Elliott Hughes33a8cb12017-07-25 18:06:46 -07002367
Elliott Hughes654cd832018-08-30 16:00:42 -07002368TEST(STDIO_TEST, printf_m) {
2369 char buf[BUFSIZ];
2370 errno = 0;
2371 snprintf(buf, sizeof(buf), "<%m>");
2372 ASSERT_STREQ("<Success>", buf);
2373 errno = -1;
2374 snprintf(buf, sizeof(buf), "<%m>");
2375 ASSERT_STREQ("<Unknown error -1>", buf);
2376 errno = EINVAL;
2377 snprintf(buf, sizeof(buf), "<%m>");
2378 ASSERT_STREQ("<Invalid argument>", buf);
2379}
2380
Elliott Hughesf340a562018-09-06 10:42:40 -07002381TEST(STDIO_TEST, printf_m_does_not_clobber_strerror) {
2382 char buf[BUFSIZ];
2383 const char* m = strerror(-1);
2384 ASSERT_STREQ("Unknown error -1", m);
2385 errno = -2;
2386 snprintf(buf, sizeof(buf), "<%m>");
2387 ASSERT_STREQ("<Unknown error -2>", buf);
2388 ASSERT_STREQ("Unknown error -1", m);
2389}
2390
Elliott Hughes654cd832018-08-30 16:00:42 -07002391TEST(STDIO_TEST, wprintf_m) {
2392 wchar_t buf[BUFSIZ];
2393 errno = 0;
2394 swprintf(buf, sizeof(buf), L"<%m>");
2395 ASSERT_EQ(std::wstring(L"<Success>"), buf);
2396 errno = -1;
2397 swprintf(buf, sizeof(buf), L"<%m>");
2398 ASSERT_EQ(std::wstring(L"<Unknown error -1>"), buf);
2399 errno = EINVAL;
2400 swprintf(buf, sizeof(buf), L"<%m>");
2401 ASSERT_EQ(std::wstring(L"<Invalid argument>"), buf);
2402}
2403
Elliott Hughesf340a562018-09-06 10:42:40 -07002404TEST(STDIO_TEST, wprintf_m_does_not_clobber_strerror) {
2405 wchar_t buf[BUFSIZ];
2406 const char* m = strerror(-1);
2407 ASSERT_STREQ("Unknown error -1", m);
2408 errno = -2;
2409 swprintf(buf, sizeof(buf), L"<%m>");
2410 ASSERT_EQ(std::wstring(L"<Unknown error -2>"), buf);
2411 ASSERT_STREQ("Unknown error -1", m);
2412}
2413
Elliott Hughes33a8cb12017-07-25 18:06:46 -07002414TEST(STDIO_TEST, fopen_append_mode_and_ftell) {
2415 TemporaryFile tf;
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -08002416 SetFileTo(tf.path, "0123456789");
2417 FILE* fp = fopen(tf.path, "a");
Elliott Hughes33a8cb12017-07-25 18:06:46 -07002418 EXPECT_EQ(10, ftell(fp));
2419 ASSERT_EQ(0, fseek(fp, 2, SEEK_SET));
2420 EXPECT_EQ(2, ftell(fp));
2421 ASSERT_NE(EOF, fputs("xxx", fp));
2422 ASSERT_EQ(0, fflush(fp));
2423 EXPECT_EQ(13, ftell(fp));
2424 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
2425 EXPECT_EQ(13, ftell(fp));
2426 ASSERT_EQ(0, fclose(fp));
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -08002427 AssertFileIs(tf.path, "0123456789xxx");
Elliott Hughes33a8cb12017-07-25 18:06:46 -07002428}
2429
2430TEST(STDIO_TEST, fdopen_append_mode_and_ftell) {
2431 TemporaryFile tf;
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -08002432 SetFileTo(tf.path, "0123456789");
2433 int fd = open(tf.path, O_RDWR);
Elliott Hughes33a8cb12017-07-25 18:06:46 -07002434 ASSERT_NE(-1, fd);
2435 // POSIX: "The file position indicator associated with the new stream is set to the position
2436 // indicated by the file offset associated with the file descriptor."
2437 ASSERT_EQ(4, lseek(fd, 4, SEEK_SET));
2438 FILE* fp = fdopen(fd, "a");
2439 EXPECT_EQ(4, ftell(fp));
2440 ASSERT_EQ(0, fseek(fp, 2, SEEK_SET));
2441 EXPECT_EQ(2, ftell(fp));
2442 ASSERT_NE(EOF, fputs("xxx", fp));
2443 ASSERT_EQ(0, fflush(fp));
2444 EXPECT_EQ(13, ftell(fp));
2445 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
2446 EXPECT_EQ(13, ftell(fp));
2447 ASSERT_EQ(0, fclose(fp));
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -08002448 AssertFileIs(tf.path, "0123456789xxx");
Elliott Hughes33a8cb12017-07-25 18:06:46 -07002449}
2450
2451TEST(STDIO_TEST, freopen_append_mode_and_ftell) {
2452 TemporaryFile tf;
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -08002453 SetFileTo(tf.path, "0123456789");
Elliott Hughes33a8cb12017-07-25 18:06:46 -07002454 FILE* other_fp = fopen("/proc/version", "r");
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -08002455 FILE* fp = freopen(tf.path, "a", other_fp);
Elliott Hughes33a8cb12017-07-25 18:06:46 -07002456 EXPECT_EQ(10, ftell(fp));
2457 ASSERT_EQ(0, fseek(fp, 2, SEEK_SET));
2458 EXPECT_EQ(2, ftell(fp));
2459 ASSERT_NE(EOF, fputs("xxx", fp));
2460 ASSERT_EQ(0, fflush(fp));
2461 EXPECT_EQ(13, ftell(fp));
2462 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
2463 EXPECT_EQ(13, ftell(fp));
2464 ASSERT_EQ(0, fclose(fp));
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -08002465 AssertFileIs(tf.path, "0123456789xxx");
Elliott Hughes33a8cb12017-07-25 18:06:46 -07002466}
Elliott Hughesb15feb72017-07-31 17:20:18 -07002467
2468TEST(STDIO_TEST, constants) {
2469 ASSERT_LE(FILENAME_MAX, PATH_MAX);
2470 ASSERT_EQ(L_tmpnam, PATH_MAX);
2471}
Elliott Hughes37ad9592017-10-30 17:47:12 -07002472
2473TEST(STDIO_TEST, perror) {
2474 ExecTestHelper eth;
2475 eth.Run([&]() { errno = EINVAL; perror("a b c"); exit(0); }, 0, "a b c: Invalid argument\n");
2476 eth.Run([&]() { errno = EINVAL; perror(nullptr); exit(0); }, 0, "Invalid argument\n");
2477 eth.Run([&]() { errno = EINVAL; perror(""); exit(0); }, 0, "Invalid argument\n");
2478}
2479
2480TEST(STDIO_TEST, puts) {
2481 ExecTestHelper eth;
2482 eth.Run([&]() { exit(puts("a b c")); }, 0, "a b c\n");
2483}
2484
2485TEST(STDIO_TEST, unlocked) {
2486 TemporaryFile tf;
2487
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -08002488 FILE* fp = fopen(tf.path, "w+");
Elliott Hughes37ad9592017-10-30 17:47:12 -07002489 ASSERT_TRUE(fp != nullptr);
2490
2491 clearerr_unlocked(fp);
2492 ASSERT_FALSE(feof_unlocked(fp));
2493 ASSERT_FALSE(ferror_unlocked(fp));
2494
2495 ASSERT_EQ(fileno(fp), fileno_unlocked(fp));
2496
2497 ASSERT_NE(EOF, putc_unlocked('a', fp));
2498 ASSERT_NE(EOF, putc('b', fp));
2499 ASSERT_NE(EOF, fputc_unlocked('c', fp));
2500 ASSERT_NE(EOF, fputc('d', fp));
2501
2502 rewind(fp);
2503 ASSERT_EQ('a', getc_unlocked(fp));
2504 ASSERT_EQ('b', getc(fp));
2505 ASSERT_EQ('c', fgetc_unlocked(fp));
2506 ASSERT_EQ('d', fgetc(fp));
2507
2508 rewind(fp);
2509 ASSERT_EQ(2U, fwrite_unlocked("AB", 1, 2, fp));
2510 ASSERT_EQ(2U, fwrite("CD", 1, 2, fp));
2511 ASSERT_EQ(0, fflush_unlocked(fp));
2512
2513 rewind(fp);
2514 char buf[BUFSIZ] = {};
2515 ASSERT_EQ(2U, fread_unlocked(&buf[0], 1, 2, fp));
2516 ASSERT_EQ(2U, fread(&buf[2], 1, 2, fp));
2517 ASSERT_STREQ("ABCD", buf);
2518
2519 rewind(fp);
2520 ASSERT_NE(EOF, fputs("hello ", fp));
2521 ASSERT_NE(EOF, fputs_unlocked("world", fp));
2522 ASSERT_NE(EOF, fputc('\n', fp));
2523
2524 rewind(fp);
2525 ASSERT_TRUE(fgets_unlocked(buf, sizeof(buf), fp) != nullptr);
2526 ASSERT_STREQ("hello world\n", buf);
2527
2528 ASSERT_EQ(0, fclose(fp));
2529}
Ryan Prichardbf549862017-11-07 15:30:32 -08002530
2531TEST(STDIO_TEST, fseek_64bit) {
2532 TemporaryFile tf;
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -08002533 FILE* fp = fopen64(tf.path, "w+");
Ryan Prichardbf549862017-11-07 15:30:32 -08002534 ASSERT_TRUE(fp != nullptr);
2535 ASSERT_EQ(0, fseeko64(fp, 0x2'0000'0000, SEEK_SET));
2536 ASSERT_EQ(0x2'0000'0000, ftello64(fp));
2537 ASSERT_EQ(0, fseeko64(fp, 0x1'0000'0000, SEEK_CUR));
2538 ASSERT_EQ(0x3'0000'0000, ftello64(fp));
2539 ASSERT_EQ(0, fclose(fp));
2540}
2541
2542// POSIX requires that fseek/fseeko fail with EOVERFLOW if the new file offset
2543// isn't representable in long/off_t.
2544TEST(STDIO_TEST, fseek_overflow_32bit) {
2545 TemporaryFile tf;
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -08002546 FILE* fp = fopen64(tf.path, "w+");
Ryan Prichardbf549862017-11-07 15:30:32 -08002547 ASSERT_EQ(0, ftruncate64(fileno(fp), 0x2'0000'0000));
2548
2549 // Bionic implements overflow checking for SEEK_CUR, but glibc doesn't.
2550#if defined(__BIONIC__) && !defined(__LP64__)
2551 ASSERT_EQ(0, fseek(fp, 0x7fff'ffff, SEEK_SET));
2552 ASSERT_EQ(-1, fseek(fp, 1, SEEK_CUR));
2553 ASSERT_EQ(EOVERFLOW, errno);
2554#endif
2555
2556 // Neither Bionic nor glibc implement the overflow checking for SEEK_END.
2557 // (Aside: FreeBSD's libc is an example of a libc that checks both SEEK_CUR
2558 // and SEEK_END -- many C libraries check neither.)
2559 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
2560 ASSERT_EQ(0x2'0000'0000, ftello64(fp));
2561
2562 fclose(fp);
2563}
Elliott Hughesf1a38382018-08-22 15:38:07 -07002564
2565TEST(STDIO_TEST, dev_std_files) {
2566 // POSIX only mentions /dev/stdout, but we should have all three (http://b/31824379).
2567 char path[PATH_MAX];
Christopher Ferris2c4ec7e2018-08-23 11:17:55 -07002568 ssize_t length = readlink("/dev/stdin", path, sizeof(path));
2569 ASSERT_LT(0, length);
2570 ASSERT_EQ("/proc/self/fd/0", std::string(path, length));
2571
2572 length = readlink("/dev/stdout", path, sizeof(path));
2573 ASSERT_LT(0, length);
2574 ASSERT_EQ("/proc/self/fd/1", std::string(path, length));
2575
2576 length = readlink("/dev/stderr", path, sizeof(path));
2577 ASSERT_LT(0, length);
2578 ASSERT_EQ("/proc/self/fd/2", std::string(path, length));
Elliott Hughesf1a38382018-08-22 15:38:07 -07002579}