blob: e07980e77f33bf9443178e88e0cfd410d1df0448 [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>
25#include <sys/stat.h>
26#include <unistd.h>
Elliott Hughes05493712014-04-17 17:30:03 -070027#include <wchar.h>
Calin Juravle03e4ebe2014-05-08 14:42:06 +010028#include <locale.h>
29
Elliott Hughes3a4c4542017-07-19 17:20:24 -070030#include <string>
Elliott Hughese6bb5a22015-01-23 17:48:15 -080031#include <vector>
32
Elliott Hughesfb3873d2016-08-10 11:07:54 -070033#include "BionicDeathTest.h"
Calin Juravle03e4ebe2014-05-08 14:42:06 +010034#include "TemporaryFile.h"
Josh Gao2f06e102017-01-10 13:00:37 -080035#include "utils.h"
Elliott Hughes91875dc2012-09-24 17:55:15 -070036
Christopher Ferris13f26a72016-01-13 13:47:58 -080037#if defined(NOFORTIFY)
38#define STDIO_TEST stdio_nofortify
Elliott Hughesfb3873d2016-08-10 11:07:54 -070039#define STDIO_DEATHTEST stdio_nofortify_DeathTest
Christopher Ferris13f26a72016-01-13 13:47:58 -080040#else
41#define STDIO_TEST stdio
Elliott Hughesfb3873d2016-08-10 11:07:54 -070042#define STDIO_DEATHTEST stdio_DeathTest
Christopher Ferris13f26a72016-01-13 13:47:58 -080043#endif
44
Elliott Hughes3a4c4542017-07-19 17:20:24 -070045using namespace std::string_literals;
46
Elliott Hughesfb3873d2016-08-10 11:07:54 -070047class stdio_DeathTest : public BionicDeathTest {};
48class stdio_nofortify_DeathTest : public BionicDeathTest {};
49
Elliott Hughes33a8cb12017-07-25 18:06:46 -070050static void SetFileTo(const char* path, const char* content) {
51 FILE* fp;
52 ASSERT_NE(nullptr, fp = fopen(path, "w"));
53 ASSERT_NE(EOF, fputs(content, fp));
54 ASSERT_EQ(0, fclose(fp));
55}
56
57static void AssertFileIs(const char* path, const char* expected) {
58 FILE* fp;
59 ASSERT_NE(nullptr, fp = fopen(path, "r"));
60 char* line = nullptr;
61 size_t length;
62 ASSERT_NE(EOF, getline(&line, &length, fp));
63 ASSERT_EQ(0, fclose(fp));
64 ASSERT_STREQ(expected, line);
65 free(line);
66}
67
Elliott Hughes70715da2016-08-01 16:35:17 -070068static void AssertFileIs(FILE* fp, const char* expected, bool is_fmemopen = false) {
69 rewind(fp);
70
71 char line[1024];
Josh Gao2f06e102017-01-10 13:00:37 -080072 memset(line, 0xff, sizeof(line));
Elliott Hughes70715da2016-08-01 16:35:17 -070073 ASSERT_EQ(line, fgets(line, sizeof(line), fp));
74 ASSERT_STREQ(expected, line);
75
76 if (is_fmemopen) {
77 // fmemopen appends a trailing NUL byte, which probably shouldn't show up as an
78 // extra empty line, but does on every C library I tested...
79 ASSERT_EQ(line, fgets(line, sizeof(line), fp));
80 ASSERT_STREQ("", line);
81 }
82
83 // Make sure there isn't anything else in the file.
84 ASSERT_EQ(nullptr, fgets(line, sizeof(line), fp)) << "junk at end of file: " << line;
85}
86
Christopher Ferris13f26a72016-01-13 13:47:58 -080087TEST(STDIO_TEST, flockfile_18208568_stderr) {
Elliott Hughes6a03abc2014-11-03 12:32:17 -080088 // Check that we have a _recursive_ mutex for flockfile.
89 flockfile(stderr);
90 feof(stderr); // We don't care about the result, but this needs to take the lock.
91 funlockfile(stderr);
92}
93
Christopher Ferris13f26a72016-01-13 13:47:58 -080094TEST(STDIO_TEST, flockfile_18208568_regular) {
Elliott Hughes6a03abc2014-11-03 12:32:17 -080095 // We never had a bug for streams other than stdin/stdout/stderr, but test anyway.
96 FILE* fp = fopen("/dev/null", "w");
97 ASSERT_TRUE(fp != NULL);
98 flockfile(fp);
99 feof(fp);
100 funlockfile(fp);
101 fclose(fp);
102}
103
Christopher Ferris13f26a72016-01-13 13:47:58 -0800104TEST(STDIO_TEST, tmpfile_fileno_fprintf_rewind_fgets) {
Elliott Hughes91875dc2012-09-24 17:55:15 -0700105 FILE* fp = tmpfile();
106 ASSERT_TRUE(fp != NULL);
107
108 int fd = fileno(fp);
109 ASSERT_NE(fd, -1);
110
111 struct stat sb;
112 int rc = fstat(fd, &sb);
113 ASSERT_NE(rc, -1);
114 ASSERT_EQ(sb.st_mode & 0777, 0600U);
115
116 rc = fprintf(fp, "hello\n");
117 ASSERT_EQ(rc, 6);
118
Elliott Hughes70715da2016-08-01 16:35:17 -0700119 AssertFileIs(fp, "hello\n");
Elliott Hughes91875dc2012-09-24 17:55:15 -0700120 fclose(fp);
121}
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300122
Elliott Hughesf226ee52016-02-03 11:24:28 -0800123TEST(STDIO_TEST, tmpfile64) {
124 FILE* fp = tmpfile64();
125 ASSERT_TRUE(fp != nullptr);
126 fclose(fp);
127}
128
Christopher Ferris13f26a72016-01-13 13:47:58 -0800129TEST(STDIO_TEST, dprintf) {
Calin Juravle6afb2a92014-05-22 11:47:47 +0100130 TemporaryFile tf;
131
132 int rc = dprintf(tf.fd, "hello\n");
133 ASSERT_EQ(rc, 6);
134
Yabin Cui5ca4a9e2014-11-06 19:55:09 -0800135 lseek(tf.fd, 0, SEEK_SET);
Christopher Ferris9e01ea62014-05-29 12:49:35 -0700136 FILE* tfile = fdopen(tf.fd, "r");
137 ASSERT_TRUE(tfile != NULL);
Calin Juravle6afb2a92014-05-22 11:47:47 +0100138
Elliott Hughes70715da2016-08-01 16:35:17 -0700139 AssertFileIs(tfile, "hello\n");
Christopher Ferris9e01ea62014-05-29 12:49:35 -0700140 fclose(tfile);
Calin Juravle6afb2a92014-05-22 11:47:47 +0100141}
142
Christopher Ferris13f26a72016-01-13 13:47:58 -0800143TEST(STDIO_TEST, getdelim) {
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300144 FILE* fp = tmpfile();
145 ASSERT_TRUE(fp != NULL);
146
147 const char* line_written = "This is a test";
148 int rc = fprintf(fp, "%s", line_written);
149 ASSERT_EQ(rc, static_cast<int>(strlen(line_written)));
150
151 rewind(fp);
152
153 char* word_read = NULL;
154 size_t allocated_length = 0;
155
156 const char* expected[] = { "This ", " ", "is ", "a ", "test" };
157 for (size_t i = 0; i < 5; ++i) {
158 ASSERT_FALSE(feof(fp));
159 ASSERT_EQ(getdelim(&word_read, &allocated_length, ' ', fp), static_cast<int>(strlen(expected[i])));
160 ASSERT_GE(allocated_length, strlen(expected[i]));
Elliott Hughes0ed7e082015-01-22 15:13:38 -0800161 ASSERT_STREQ(expected[i], word_read);
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300162 }
163 // The last read should have set the end-of-file indicator for the stream.
164 ASSERT_TRUE(feof(fp));
165 clearerr(fp);
166
167 // getdelim returns -1 but doesn't set errno if we're already at EOF.
168 // It should set the end-of-file indicator for the stream, though.
169 errno = 0;
170 ASSERT_EQ(getdelim(&word_read, &allocated_length, ' ', fp), -1);
Elliott Hughes5e3fc432013-02-11 16:36:48 -0800171 ASSERT_EQ(0, errno);
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300172 ASSERT_TRUE(feof(fp));
173
174 free(word_read);
175 fclose(fp);
176}
177
Christopher Ferris13f26a72016-01-13 13:47:58 -0800178TEST(STDIO_TEST, getdelim_invalid) {
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300179 FILE* fp = tmpfile();
Elliott Hughes6ad8f762013-12-19 14:56:17 -0800180 ASSERT_TRUE(fp != NULL);
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300181
182 char* buffer = NULL;
183 size_t buffer_length = 0;
184
185 // The first argument can't be NULL.
186 errno = 0;
187 ASSERT_EQ(getdelim(NULL, &buffer_length, ' ', fp), -1);
Elliott Hughes5e3fc432013-02-11 16:36:48 -0800188 ASSERT_EQ(EINVAL, errno);
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300189
190 // The second argument can't be NULL.
191 errno = 0;
192 ASSERT_EQ(getdelim(&buffer, NULL, ' ', fp), -1);
Elliott Hughes5e3fc432013-02-11 16:36:48 -0800193 ASSERT_EQ(EINVAL, errno);
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300194
Elliott Hughes20f8aec2014-05-12 15:15:37 -0700195 // The underlying fd can't be closed.
196 ASSERT_EQ(0, close(fileno(fp)));
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300197 errno = 0;
198 ASSERT_EQ(getdelim(&buffer, &buffer_length, ' ', fp), -1);
Elliott Hughes5e3fc432013-02-11 16:36:48 -0800199 ASSERT_EQ(EBADF, errno);
Elliott Hughes20f8aec2014-05-12 15:15:37 -0700200 fclose(fp);
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300201}
202
Christopher Ferris13f26a72016-01-13 13:47:58 -0800203TEST(STDIO_TEST, getdelim_directory) {
Elliott Hughes694fd2d2015-04-05 10:51:56 -0700204 FILE* fp = fopen("/proc", "r");
205 ASSERT_TRUE(fp != NULL);
206 char* word_read;
207 size_t allocated_length;
208 ASSERT_EQ(-1, getdelim(&word_read, &allocated_length, ' ', fp));
209 fclose(fp);
210}
211
Christopher Ferris13f26a72016-01-13 13:47:58 -0800212TEST(STDIO_TEST, getline) {
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300213 FILE* fp = tmpfile();
214 ASSERT_TRUE(fp != NULL);
215
216 const char* line_written = "This is a test for getline\n";
217 const size_t line_count = 5;
218
219 for (size_t i = 0; i < line_count; ++i) {
220 int rc = fprintf(fp, "%s", line_written);
221 ASSERT_EQ(rc, static_cast<int>(strlen(line_written)));
222 }
223
224 rewind(fp);
225
226 char* line_read = NULL;
227 size_t allocated_length = 0;
228
229 size_t read_line_count = 0;
230 ssize_t read_char_count;
231 while ((read_char_count = getline(&line_read, &allocated_length, fp)) != -1) {
232 ASSERT_EQ(read_char_count, static_cast<int>(strlen(line_written)));
233 ASSERT_GE(allocated_length, strlen(line_written));
Elliott Hughes0ed7e082015-01-22 15:13:38 -0800234 ASSERT_STREQ(line_written, line_read);
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300235 ++read_line_count;
236 }
237 ASSERT_EQ(read_line_count, line_count);
238
239 // The last read should have set the end-of-file indicator for the stream.
240 ASSERT_TRUE(feof(fp));
241 clearerr(fp);
242
243 // getline returns -1 but doesn't set errno if we're already at EOF.
244 // It should set the end-of-file indicator for the stream, though.
245 errno = 0;
246 ASSERT_EQ(getline(&line_read, &allocated_length, fp), -1);
Elliott Hughes5e3fc432013-02-11 16:36:48 -0800247 ASSERT_EQ(0, errno);
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300248 ASSERT_TRUE(feof(fp));
249
250 free(line_read);
251 fclose(fp);
252}
253
Christopher Ferris13f26a72016-01-13 13:47:58 -0800254TEST(STDIO_TEST, getline_invalid) {
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300255 FILE* fp = tmpfile();
Elliott Hughes6ad8f762013-12-19 14:56:17 -0800256 ASSERT_TRUE(fp != NULL);
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300257
258 char* buffer = NULL;
259 size_t buffer_length = 0;
260
261 // The first argument can't be NULL.
262 errno = 0;
263 ASSERT_EQ(getline(NULL, &buffer_length, fp), -1);
Elliott Hughes5e3fc432013-02-11 16:36:48 -0800264 ASSERT_EQ(EINVAL, errno);
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300265
266 // The second argument can't be NULL.
267 errno = 0;
268 ASSERT_EQ(getline(&buffer, NULL, fp), -1);
Elliott Hughes5e3fc432013-02-11 16:36:48 -0800269 ASSERT_EQ(EINVAL, errno);
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300270
Elliott Hughes20f8aec2014-05-12 15:15:37 -0700271 // The underlying fd can't be closed.
272 ASSERT_EQ(0, close(fileno(fp)));
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300273 errno = 0;
274 ASSERT_EQ(getline(&buffer, &buffer_length, fp), -1);
Elliott Hughes5e3fc432013-02-11 16:36:48 -0800275 ASSERT_EQ(EBADF, errno);
Elliott Hughes20f8aec2014-05-12 15:15:37 -0700276 fclose(fp);
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300277}
Thorsten Glaserc641caf2013-02-17 16:50:58 +0000278
Christopher Ferris13f26a72016-01-13 13:47:58 -0800279TEST(STDIO_TEST, printf_ssize_t) {
Elliott Hughese2556422013-02-28 10:51:14 -0800280 // http://b/8253769
Elliott Hughese2556422013-02-28 10:51:14 -0800281 ASSERT_EQ(sizeof(ssize_t), sizeof(long int));
Elliott Hughesb6e22482013-03-08 15:28:52 -0800282 ASSERT_EQ(sizeof(ssize_t), sizeof(size_t));
283 // For our 32-bit ABI, we had a ssize_t definition that confuses GCC into saying:
Thorsten Glaserc641caf2013-02-17 16:50:58 +0000284 // error: format '%zd' expects argument of type 'signed size_t',
285 // but argument 4 has type 'ssize_t {aka long int}' [-Werror=format]
286 ssize_t v = 1;
287 char buf[32];
288 snprintf(buf, sizeof(buf), "%zd", v);
289}
Elliott Hughes6b3f49a2013-03-06 16:20:55 -0800290
Elliott Hughes05493712014-04-17 17:30:03 -0700291// https://code.google.com/p/android/issues/detail?id=64886
Christopher Ferris13f26a72016-01-13 13:47:58 -0800292TEST(STDIO_TEST, snprintf_a) {
Elliott Hughes05493712014-04-17 17:30:03 -0700293 char buf[BUFSIZ];
294 EXPECT_EQ(23, snprintf(buf, sizeof(buf), "<%a>", 9990.235));
295 EXPECT_STREQ("<0x1.3831e147ae148p+13>", buf);
296}
297
Christopher Ferris13f26a72016-01-13 13:47:58 -0800298TEST(STDIO_TEST, snprintf_lc) {
Elliott Hughes05493712014-04-17 17:30:03 -0700299 char buf[BUFSIZ];
300 wint_t wc = L'a';
301 EXPECT_EQ(3, snprintf(buf, sizeof(buf), "<%lc>", wc));
302 EXPECT_STREQ("<a>", buf);
303}
304
Elliott Hughes2f9c8ce2017-11-01 13:54:47 -0700305TEST(STDIO_TEST, snprintf_C) { // Synonym for %lc.
306 char buf[BUFSIZ];
307 wchar_t wc = L'a';
308 EXPECT_EQ(3, snprintf(buf, sizeof(buf), "<%C>", wc));
309 EXPECT_STREQ("<a>", buf);
310}
311
Christopher Ferris13f26a72016-01-13 13:47:58 -0800312TEST(STDIO_TEST, snprintf_ls) {
Elliott Hughes05493712014-04-17 17:30:03 -0700313 char buf[BUFSIZ];
314 wchar_t* ws = NULL;
315 EXPECT_EQ(8, snprintf(buf, sizeof(buf), "<%ls>", ws));
316 EXPECT_STREQ("<(null)>", buf);
317
318 wchar_t chars[] = { L'h', L'i', 0 };
319 ws = chars;
320 EXPECT_EQ(4, snprintf(buf, sizeof(buf), "<%ls>", ws));
321 EXPECT_STREQ("<hi>", buf);
322}
323
Elliott Hughes2f9c8ce2017-11-01 13:54:47 -0700324TEST(STDIO_TEST, snprintf_S) { // Synonym for %ls.
325 char buf[BUFSIZ];
326 wchar_t* ws = NULL;
327 EXPECT_EQ(8, snprintf(buf, sizeof(buf), "<%S>", ws));
328 EXPECT_STREQ("<(null)>", buf);
329
330 wchar_t chars[] = { L'h', L'i', 0 };
331 ws = chars;
332 EXPECT_EQ(4, snprintf(buf, sizeof(buf), "<%S>", ws));
333 EXPECT_STREQ("<hi>", buf);
334}
335
Christopher Ferris13f26a72016-01-13 13:47:58 -0800336TEST(STDIO_TEST, snprintf_n) {
Elliott Hughes063525c2014-05-13 11:19:57 -0700337#if defined(__BIONIC__)
Elliott Hughese2341d02014-05-02 18:16:32 -0700338 // http://b/14492135
Elliott Hughes7248a2d2013-09-24 18:01:33 -0700339 char buf[32];
Elliott Hughese2341d02014-05-02 18:16:32 -0700340 int i = 1234;
341 EXPECT_EQ(5, snprintf(buf, sizeof(buf), "a %n b", &i));
342 EXPECT_EQ(1234, i);
343 EXPECT_STREQ("a n b", buf);
344#else
Elliott Hughes9677fab2016-01-25 15:50:59 -0800345 GTEST_LOG_(INFO) << "This test does nothing on glibc.\n";
Elliott Hughese2341d02014-05-02 18:16:32 -0700346#endif
Elliott Hughes7248a2d2013-09-24 18:01:33 -0700347}
Elliott Hughes7248a2d2013-09-24 18:01:33 -0700348
Christopher Ferris13f26a72016-01-13 13:47:58 -0800349TEST(STDIO_TEST, snprintf_smoke) {
Elliott Hughes1d13c642013-09-23 16:02:39 -0700350 char buf[BUFSIZ];
351
352 snprintf(buf, sizeof(buf), "a");
353 EXPECT_STREQ("a", buf);
354
355 snprintf(buf, sizeof(buf), "%%");
356 EXPECT_STREQ("%", buf);
357
358 snprintf(buf, sizeof(buf), "01234");
359 EXPECT_STREQ("01234", buf);
360
361 snprintf(buf, sizeof(buf), "a%sb", "01234");
362 EXPECT_STREQ("a01234b", buf);
363
364 char* s = NULL;
365 snprintf(buf, sizeof(buf), "a%sb", s);
366 EXPECT_STREQ("a(null)b", buf);
367
368 snprintf(buf, sizeof(buf), "aa%scc", "bb");
369 EXPECT_STREQ("aabbcc", buf);
370
371 snprintf(buf, sizeof(buf), "a%cc", 'b');
372 EXPECT_STREQ("abc", buf);
373
374 snprintf(buf, sizeof(buf), "a%db", 1234);
375 EXPECT_STREQ("a1234b", buf);
376
377 snprintf(buf, sizeof(buf), "a%db", -8123);
378 EXPECT_STREQ("a-8123b", buf);
379
Stephen Hines6c7b3cb2013-10-11 16:03:21 -0700380 snprintf(buf, sizeof(buf), "a%hdb", static_cast<short>(0x7fff0010));
Elliott Hughes1d13c642013-09-23 16:02:39 -0700381 EXPECT_STREQ("a16b", buf);
382
Stephen Hines6c7b3cb2013-10-11 16:03:21 -0700383 snprintf(buf, sizeof(buf), "a%hhdb", static_cast<char>(0x7fffff10));
Elliott Hughes1d13c642013-09-23 16:02:39 -0700384 EXPECT_STREQ("a16b", buf);
385
386 snprintf(buf, sizeof(buf), "a%lldb", 0x1000000000LL);
387 EXPECT_STREQ("a68719476736b", buf);
388
389 snprintf(buf, sizeof(buf), "a%ldb", 70000L);
390 EXPECT_STREQ("a70000b", buf);
391
392 snprintf(buf, sizeof(buf), "a%pb", reinterpret_cast<void*>(0xb0001234));
393 EXPECT_STREQ("a0xb0001234b", buf);
394
395 snprintf(buf, sizeof(buf), "a%xz", 0x12ab);
396 EXPECT_STREQ("a12abz", buf);
397
398 snprintf(buf, sizeof(buf), "a%Xz", 0x12ab);
399 EXPECT_STREQ("a12ABz", buf);
400
401 snprintf(buf, sizeof(buf), "a%08xz", 0x123456);
402 EXPECT_STREQ("a00123456z", buf);
403
404 snprintf(buf, sizeof(buf), "a%5dz", 1234);
405 EXPECT_STREQ("a 1234z", buf);
406
407 snprintf(buf, sizeof(buf), "a%05dz", 1234);
408 EXPECT_STREQ("a01234z", buf);
409
410 snprintf(buf, sizeof(buf), "a%8dz", 1234);
411 EXPECT_STREQ("a 1234z", buf);
412
413 snprintf(buf, sizeof(buf), "a%-8dz", 1234);
414 EXPECT_STREQ("a1234 z", buf);
415
416 snprintf(buf, sizeof(buf), "A%-11sZ", "abcdef");
417 EXPECT_STREQ("Aabcdef Z", buf);
418
419 snprintf(buf, sizeof(buf), "A%s:%dZ", "hello", 1234);
420 EXPECT_STREQ("Ahello:1234Z", buf);
421
422 snprintf(buf, sizeof(buf), "a%03d:%d:%02dz", 5, 5, 5);
423 EXPECT_STREQ("a005:5:05z", buf);
424
425 void* p = NULL;
426 snprintf(buf, sizeof(buf), "a%d,%pz", 5, p);
Christopher Ferris13613132013-10-28 15:24:04 -0700427#if defined(__BIONIC__)
Elliott Hughes1d13c642013-09-23 16:02:39 -0700428 EXPECT_STREQ("a5,0x0z", buf);
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800429#else // __BIONIC__
Christopher Ferris13613132013-10-28 15:24:04 -0700430 EXPECT_STREQ("a5,(nil)z", buf);
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800431#endif // __BIONIC__
Elliott Hughes1d13c642013-09-23 16:02:39 -0700432
433 snprintf(buf, sizeof(buf), "a%lld,%d,%d,%dz", 0x1000000000LL, 6, 7, 8);
434 EXPECT_STREQ("a68719476736,6,7,8z", buf);
435
436 snprintf(buf, sizeof(buf), "a_%f_b", 1.23f);
437 EXPECT_STREQ("a_1.230000_b", buf);
438
Stephen Hines6c7b3cb2013-10-11 16:03:21 -0700439 snprintf(buf, sizeof(buf), "a_%g_b", 3.14);
Elliott Hughes1d13c642013-09-23 16:02:39 -0700440 EXPECT_STREQ("a_3.14_b", buf);
Alexander Ivchenkoedd7c2e2014-04-01 17:01:39 +0400441
442 snprintf(buf, sizeof(buf), "%1$s %1$s", "print_me_twice");
443 EXPECT_STREQ("print_me_twice print_me_twice", buf);
Elliott Hughes1d13c642013-09-23 16:02:39 -0700444}
445
Elliott Hughes1b18aff2014-12-16 14:45:32 -0800446template <typename T>
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700447static void CheckInfNan(int snprintf_fn(T*, size_t, const T*, ...),
448 int sscanf_fn(const T*, const T*, ...),
449 const T* fmt_string, const T* fmt, const T* fmt_plus,
450 const T* minus_inf, const T* inf_, const T* plus_inf,
451 const T* minus_nan, const T* nan_, const T* plus_nan) {
Elliott Hughes1b18aff2014-12-16 14:45:32 -0800452 T buf[BUFSIZ];
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700453 float f;
Elliott Hughes7823f322014-04-14 12:11:28 -0700454
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700455 // NaN.
456
457 snprintf_fn(buf, sizeof(buf), fmt, nanf(""));
Elliott Hughes1b18aff2014-12-16 14:45:32 -0800458 EXPECT_STREQ(nan_, buf) << fmt;
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700459 EXPECT_EQ(1, sscanf_fn(buf, fmt, &f));
460 EXPECT_TRUE(isnan(f));
461
462 snprintf_fn(buf, sizeof(buf), fmt, -nanf(""));
Elliott Hughes1b18aff2014-12-16 14:45:32 -0800463 EXPECT_STREQ(minus_nan, buf) << fmt;
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700464 EXPECT_EQ(1, sscanf_fn(buf, fmt, &f));
465 EXPECT_TRUE(isnan(f));
466
467 snprintf_fn(buf, sizeof(buf), fmt_plus, nanf(""));
Elliott Hughes1b18aff2014-12-16 14:45:32 -0800468 EXPECT_STREQ(plus_nan, buf) << fmt_plus;
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700469 EXPECT_EQ(1, sscanf_fn(buf, fmt, &f));
470 EXPECT_TRUE(isnan(f));
471
472 snprintf_fn(buf, sizeof(buf), fmt_plus, -nanf(""));
Elliott Hughes1b18aff2014-12-16 14:45:32 -0800473 EXPECT_STREQ(minus_nan, buf) << fmt_plus;
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700474 EXPECT_EQ(1, sscanf_fn(buf, fmt, &f));
475 EXPECT_TRUE(isnan(f));
Elliott Hughes1b18aff2014-12-16 14:45:32 -0800476
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700477 // Inf.
478
479 snprintf_fn(buf, sizeof(buf), fmt, HUGE_VALF);
Elliott Hughes1b18aff2014-12-16 14:45:32 -0800480 EXPECT_STREQ(inf_, buf) << fmt;
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700481 EXPECT_EQ(1, sscanf_fn(buf, fmt, &f));
482 EXPECT_EQ(HUGE_VALF, f);
483
484 snprintf_fn(buf, sizeof(buf), fmt, -HUGE_VALF);
Elliott Hughes1b18aff2014-12-16 14:45:32 -0800485 EXPECT_STREQ(minus_inf, buf) << fmt;
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700486 EXPECT_EQ(1, sscanf_fn(buf, fmt, &f));
487 EXPECT_EQ(-HUGE_VALF, f);
488
489 snprintf_fn(buf, sizeof(buf), fmt_plus, HUGE_VALF);
Elliott Hughes1b18aff2014-12-16 14:45:32 -0800490 EXPECT_STREQ(plus_inf, buf) << fmt_plus;
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700491 EXPECT_EQ(1, sscanf_fn(buf, fmt, &f));
492 EXPECT_EQ(HUGE_VALF, f);
493
494 snprintf_fn(buf, sizeof(buf), fmt_plus, -HUGE_VALF);
Elliott Hughes1b18aff2014-12-16 14:45:32 -0800495 EXPECT_STREQ(minus_inf, buf) << fmt_plus;
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700496 EXPECT_EQ(1, sscanf_fn(buf, fmt, &f));
497 EXPECT_EQ(-HUGE_VALF, f);
498
499 // Check case-insensitivity.
500 snprintf_fn(buf, sizeof(buf), fmt_string, "[InFiNiTy]");
501 EXPECT_EQ(1, sscanf_fn(buf, fmt, &f)) << buf;
502 EXPECT_EQ(HUGE_VALF, f);
503 snprintf_fn(buf, sizeof(buf), fmt_string, "[NaN]");
504 EXPECT_EQ(1, sscanf_fn(buf, fmt, &f)) << buf;
505 EXPECT_TRUE(isnan(f));
Elliott Hughes7823f322014-04-14 12:11:28 -0700506}
507
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700508TEST(STDIO_TEST, snprintf_sscanf_inf_nan) {
509 CheckInfNan(snprintf, sscanf, "%s",
510 "[%a]", "[%+a]",
511 "[-inf]", "[inf]", "[+inf]",
512 "[-nan]", "[nan]", "[+nan]");
513 CheckInfNan(snprintf, sscanf, "%s",
514 "[%A]", "[%+A]",
515 "[-INF]", "[INF]", "[+INF]",
516 "[-NAN]", "[NAN]", "[+NAN]");
517 CheckInfNan(snprintf, sscanf, "%s",
518 "[%e]", "[%+e]",
519 "[-inf]", "[inf]", "[+inf]",
520 "[-nan]", "[nan]", "[+nan]");
521 CheckInfNan(snprintf, sscanf, "%s",
522 "[%E]", "[%+E]",
523 "[-INF]", "[INF]", "[+INF]",
524 "[-NAN]", "[NAN]", "[+NAN]");
525 CheckInfNan(snprintf, sscanf, "%s",
526 "[%f]", "[%+f]",
527 "[-inf]", "[inf]", "[+inf]",
528 "[-nan]", "[nan]", "[+nan]");
529 CheckInfNan(snprintf, sscanf, "%s",
530 "[%F]", "[%+F]",
531 "[-INF]", "[INF]", "[+INF]",
532 "[-NAN]", "[NAN]", "[+NAN]");
533 CheckInfNan(snprintf, sscanf, "%s",
534 "[%g]", "[%+g]",
535 "[-inf]", "[inf]", "[+inf]",
536 "[-nan]", "[nan]", "[+nan]");
537 CheckInfNan(snprintf, sscanf, "%s",
538 "[%G]", "[%+G]",
539 "[-INF]", "[INF]", "[+INF]",
540 "[-NAN]", "[NAN]", "[+NAN]");
Elliott Hughes1b18aff2014-12-16 14:45:32 -0800541}
Elliott Hughes7823f322014-04-14 12:11:28 -0700542
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700543TEST(STDIO_TEST, swprintf_swscanf_inf_nan) {
544 CheckInfNan(swprintf, swscanf, L"%s",
545 L"[%a]", L"[%+a]",
546 L"[-inf]", L"[inf]", L"[+inf]",
547 L"[-nan]", L"[nan]", L"[+nan]");
548 CheckInfNan(swprintf, swscanf, L"%s",
549 L"[%A]", L"[%+A]",
550 L"[-INF]", L"[INF]", L"[+INF]",
551 L"[-NAN]", L"[NAN]", L"[+NAN]");
552 CheckInfNan(swprintf, swscanf, L"%s",
553 L"[%e]", L"[%+e]",
554 L"[-inf]", L"[inf]", L"[+inf]",
555 L"[-nan]", L"[nan]", L"[+nan]");
556 CheckInfNan(swprintf, swscanf, L"%s",
557 L"[%E]", L"[%+E]",
558 L"[-INF]", L"[INF]", L"[+INF]",
559 L"[-NAN]", L"[NAN]", L"[+NAN]");
560 CheckInfNan(swprintf, swscanf, L"%s",
561 L"[%f]", L"[%+f]",
562 L"[-inf]", L"[inf]", L"[+inf]",
563 L"[-nan]", L"[nan]", L"[+nan]");
564 CheckInfNan(swprintf, swscanf, L"%s",
565 L"[%F]", L"[%+F]",
566 L"[-INF]", L"[INF]", L"[+INF]",
567 L"[-NAN]", L"[NAN]", L"[+NAN]");
568 CheckInfNan(swprintf, swscanf, L"%s",
569 L"[%g]", L"[%+g]",
570 L"[-inf]", L"[inf]", L"[+inf]",
571 L"[-nan]", L"[nan]", L"[+nan]");
572 CheckInfNan(swprintf, swscanf, L"%s",
573 L"[%G]", L"[%+G]",
574 L"[-INF]", L"[INF]", L"[+INF]",
575 L"[-NAN]", L"[NAN]", L"[+NAN]");
Elliott Hughes7823f322014-04-14 12:11:28 -0700576}
577
Dan Albert9601f162017-08-09 14:59:06 -0700578TEST(STDIO_TEST, swprintf) {
579 constexpr size_t nchars = 32;
580 wchar_t buf[nchars];
581
582 ASSERT_EQ(2, swprintf(buf, nchars, L"ab")) << strerror(errno);
583 ASSERT_EQ(std::wstring(L"ab"), buf);
584 ASSERT_EQ(5, swprintf(buf, nchars, L"%s", "abcde"));
585 ASSERT_EQ(std::wstring(L"abcde"), buf);
586
587 // Unlike swprintf(), swprintf() returns -1 in case of truncation
588 // and doesn't necessarily zero-terminate the output!
589 ASSERT_EQ(-1, swprintf(buf, 4, L"%s", "abcde"));
590
591 const char kString[] = "Hello, World";
592 ASSERT_EQ(12, swprintf(buf, nchars, L"%s", kString));
593 ASSERT_EQ(std::wstring(L"Hello, World"), buf);
594 ASSERT_EQ(12, swprintf(buf, 13, L"%s", kString));
595 ASSERT_EQ(std::wstring(L"Hello, World"), buf);
596}
597
598TEST(STDIO_TEST, swprintf_a) {
599 constexpr size_t nchars = 32;
600 wchar_t buf[nchars];
601
602 ASSERT_EQ(20, swprintf(buf, nchars, L"%a", 3.1415926535));
603 ASSERT_EQ(std::wstring(L"0x1.921fb54411744p+1"), buf);
604}
605
Elliott Hughes2f9c8ce2017-11-01 13:54:47 -0700606TEST(STDIO_TEST, swprintf_lc) {
607 constexpr size_t nchars = 32;
608 wchar_t buf[nchars];
609
610 wint_t wc = L'a';
611 EXPECT_EQ(3, swprintf(buf, nchars, L"<%lc>", wc));
612 EXPECT_EQ(std::wstring(L"<a>"), buf);
613}
614
615TEST(STDIO_TEST, swprintf_C) { // Synonym for %lc.
616 constexpr size_t nchars = 32;
617 wchar_t buf[nchars];
618
619 wint_t wc = L'a';
620 EXPECT_EQ(3, swprintf(buf, nchars, L"<%C>", wc));
621 EXPECT_EQ(std::wstring(L"<a>"), buf);
622}
623
Dan Albert9601f162017-08-09 14:59:06 -0700624TEST(STDIO_TEST, swprintf_ls) {
625 constexpr size_t nchars = 32;
626 wchar_t buf[nchars];
627
628 static const wchar_t kWideString[] = L"Hello\uff41 World";
629 ASSERT_EQ(12, swprintf(buf, nchars, L"%ls", kWideString));
630 ASSERT_EQ(std::wstring(kWideString), buf);
631 ASSERT_EQ(12, swprintf(buf, 13, L"%ls", kWideString));
632 ASSERT_EQ(std::wstring(kWideString), buf);
633}
634
Elliott Hughes2f9c8ce2017-11-01 13:54:47 -0700635TEST(STDIO_TEST, swprintf_S) { // Synonym for %ls.
636 constexpr size_t nchars = 32;
637 wchar_t buf[nchars];
638
639 static const wchar_t kWideString[] = L"Hello\uff41 World";
640 ASSERT_EQ(12, swprintf(buf, nchars, L"%S", kWideString));
641 ASSERT_EQ(std::wstring(kWideString), buf);
642 ASSERT_EQ(12, swprintf(buf, 13, L"%S", kWideString));
643 ASSERT_EQ(std::wstring(kWideString), buf);
644}
645
Christopher Ferris13f26a72016-01-13 13:47:58 -0800646TEST(STDIO_TEST, snprintf_d_INT_MAX) {
Elliott Hughes1d13c642013-09-23 16:02:39 -0700647 char buf[BUFSIZ];
648 snprintf(buf, sizeof(buf), "%d", INT_MAX);
649 EXPECT_STREQ("2147483647", buf);
650}
651
Christopher Ferris13f26a72016-01-13 13:47:58 -0800652TEST(STDIO_TEST, snprintf_d_INT_MIN) {
Elliott Hughes1d13c642013-09-23 16:02:39 -0700653 char buf[BUFSIZ];
654 snprintf(buf, sizeof(buf), "%d", INT_MIN);
655 EXPECT_STREQ("-2147483648", buf);
656}
657
Christopher Ferris13f26a72016-01-13 13:47:58 -0800658TEST(STDIO_TEST, snprintf_ld_LONG_MAX) {
Elliott Hughes1d13c642013-09-23 16:02:39 -0700659 char buf[BUFSIZ];
660 snprintf(buf, sizeof(buf), "%ld", LONG_MAX);
Josh Gaob36efa42016-09-15 13:55:41 -0700661#if defined(__LP64__)
Elliott Hughes925753a2013-10-18 13:17:18 -0700662 EXPECT_STREQ("9223372036854775807", buf);
663#else
Elliott Hughes1d13c642013-09-23 16:02:39 -0700664 EXPECT_STREQ("2147483647", buf);
Elliott Hughes925753a2013-10-18 13:17:18 -0700665#endif
Elliott Hughes1d13c642013-09-23 16:02:39 -0700666}
667
Christopher Ferris13f26a72016-01-13 13:47:58 -0800668TEST(STDIO_TEST, snprintf_ld_LONG_MIN) {
Elliott Hughes1d13c642013-09-23 16:02:39 -0700669 char buf[BUFSIZ];
670 snprintf(buf, sizeof(buf), "%ld", LONG_MIN);
Josh Gaob36efa42016-09-15 13:55:41 -0700671#if defined(__LP64__)
Elliott Hughes925753a2013-10-18 13:17:18 -0700672 EXPECT_STREQ("-9223372036854775808", buf);
673#else
Elliott Hughes1d13c642013-09-23 16:02:39 -0700674 EXPECT_STREQ("-2147483648", buf);
Elliott Hughes925753a2013-10-18 13:17:18 -0700675#endif
Elliott Hughes1d13c642013-09-23 16:02:39 -0700676}
677
Christopher Ferris13f26a72016-01-13 13:47:58 -0800678TEST(STDIO_TEST, snprintf_lld_LLONG_MAX) {
Elliott Hughes1d13c642013-09-23 16:02:39 -0700679 char buf[BUFSIZ];
680 snprintf(buf, sizeof(buf), "%lld", LLONG_MAX);
681 EXPECT_STREQ("9223372036854775807", buf);
682}
683
Christopher Ferris13f26a72016-01-13 13:47:58 -0800684TEST(STDIO_TEST, snprintf_lld_LLONG_MIN) {
Elliott Hughes1d13c642013-09-23 16:02:39 -0700685 char buf[BUFSIZ];
686 snprintf(buf, sizeof(buf), "%lld", LLONG_MIN);
687 EXPECT_STREQ("-9223372036854775808", buf);
688}
689
Elliott Hughes2f9c8ce2017-11-01 13:54:47 -0700690TEST(STDIO_TEST, snprintf_o_UINT_MAX) {
691 char buf[BUFSIZ];
692 snprintf(buf, sizeof(buf), "%o", UINT_MAX);
693 EXPECT_STREQ("37777777777", buf);
694}
695
696TEST(STDIO_TEST, snprintf_u_UINT_MAX) {
697 char buf[BUFSIZ];
698 snprintf(buf, sizeof(buf), "%u", UINT_MAX);
699 EXPECT_STREQ("4294967295", buf);
700}
701
702TEST(STDIO_TEST, snprintf_x_UINT_MAX) {
703 char buf[BUFSIZ];
704 snprintf(buf, sizeof(buf), "%x", UINT_MAX);
705 EXPECT_STREQ("ffffffff", buf);
706}
707
708TEST(STDIO_TEST, snprintf_X_UINT_MAX) {
709 char buf[BUFSIZ];
710 snprintf(buf, sizeof(buf), "%X", UINT_MAX);
711 EXPECT_STREQ("FFFFFFFF", buf);
712}
713
Christopher Ferris13f26a72016-01-13 13:47:58 -0800714TEST(STDIO_TEST, snprintf_e) {
Elliott Hughes4bd97ce2014-04-10 17:48:14 -0700715 char buf[BUFSIZ];
716
717 snprintf(buf, sizeof(buf), "%e", 1.5);
718 EXPECT_STREQ("1.500000e+00", buf);
719
720 snprintf(buf, sizeof(buf), "%Le", 1.5l);
721 EXPECT_STREQ("1.500000e+00", buf);
722}
723
Christopher Ferris13f26a72016-01-13 13:47:58 -0800724TEST(STDIO_TEST, snprintf_negative_zero_5084292) {
Elliott Hughese77f38f2014-05-14 12:39:12 -0700725 char buf[BUFSIZ];
726
Elliott Hughes1b18aff2014-12-16 14:45:32 -0800727 snprintf(buf, sizeof(buf), "%e", -0.0);
728 EXPECT_STREQ("-0.000000e+00", buf);
729 snprintf(buf, sizeof(buf), "%E", -0.0);
730 EXPECT_STREQ("-0.000000E+00", buf);
Elliott Hughese77f38f2014-05-14 12:39:12 -0700731 snprintf(buf, sizeof(buf), "%f", -0.0);
732 EXPECT_STREQ("-0.000000", buf);
Elliott Hughes1b18aff2014-12-16 14:45:32 -0800733 snprintf(buf, sizeof(buf), "%F", -0.0);
734 EXPECT_STREQ("-0.000000", buf);
735 snprintf(buf, sizeof(buf), "%g", -0.0);
736 EXPECT_STREQ("-0", buf);
737 snprintf(buf, sizeof(buf), "%G", -0.0);
738 EXPECT_STREQ("-0", buf);
739 snprintf(buf, sizeof(buf), "%a", -0.0);
740 EXPECT_STREQ("-0x0p+0", buf);
741 snprintf(buf, sizeof(buf), "%A", -0.0);
742 EXPECT_STREQ("-0X0P+0", buf);
Elliott Hughese77f38f2014-05-14 12:39:12 -0700743}
744
Christopher Ferris13f26a72016-01-13 13:47:58 -0800745TEST(STDIO_TEST, snprintf_utf8_15439554) {
Dan Albert1aec7c12014-07-30 10:53:48 -0700746 locale_t cloc = newlocale(LC_ALL, "C.UTF-8", 0);
Wally Yaua40fdbd2014-08-26 09:47:23 -0700747 locale_t old_locale = uselocale(cloc);
Dan Albert1aec7c12014-07-30 10:53:48 -0700748
Elliott Hughes69f05d22014-06-05 20:10:09 -0700749 // http://b/15439554
750 char buf[BUFSIZ];
751
752 // 1-byte character.
753 snprintf(buf, sizeof(buf), "%dx%d", 1, 2);
754 EXPECT_STREQ("1x2", buf);
755 // 2-byte character.
756 snprintf(buf, sizeof(buf), "%d\xc2\xa2%d", 1, 2);
757 EXPECT_STREQ("1¢2", buf);
758 // 3-byte character.
759 snprintf(buf, sizeof(buf), "%d\xe2\x82\xac%d", 1, 2);
760 EXPECT_STREQ("1€2", buf);
761 // 4-byte character.
762 snprintf(buf, sizeof(buf), "%d\xf0\xa4\xad\xa2%d", 1, 2);
763 EXPECT_STREQ("1𤭢2", buf);
Dan Albert1aec7c12014-07-30 10:53:48 -0700764
Wally Yaua40fdbd2014-08-26 09:47:23 -0700765 uselocale(old_locale);
Dan Albert1aec7c12014-07-30 10:53:48 -0700766 freelocale(cloc);
Elliott Hughes69f05d22014-06-05 20:10:09 -0700767}
768
Elliott Hughes43f7c872016-02-05 11:18:41 -0800769static void* snprintf_small_stack_fn(void*) {
770 // Make life (realistically) hard for ourselves by allocating our own buffer for the result.
771 char buf[PATH_MAX];
772 snprintf(buf, sizeof(buf), "/proc/%d", getpid());
773 return nullptr;
774}
775
776TEST(STDIO_TEST, snprintf_small_stack) {
777 // Is it safe to call snprintf on a thread with a small stack?
778 // (The snprintf implementation puts some pretty large buffers on the stack.)
779 pthread_attr_t a;
780 ASSERT_EQ(0, pthread_attr_init(&a));
781 ASSERT_EQ(0, pthread_attr_setstacksize(&a, PTHREAD_STACK_MIN));
782
783 pthread_t t;
784 ASSERT_EQ(0, pthread_create(&t, &a, snprintf_small_stack_fn, nullptr));
785 ASSERT_EQ(0, pthread_join(t, nullptr));
786}
787
Elliott Hughes8200e552016-02-05 21:57:37 -0800788TEST(STDIO_TEST, snprintf_asterisk_overflow) {
789 char buf[128];
790 ASSERT_EQ(5, snprintf(buf, sizeof(buf), "%.*s%c", 4, "hello world", '!'));
791 ASSERT_EQ(12, snprintf(buf, sizeof(buf), "%.*s%c", INT_MAX/2, "hello world", '!'));
792 ASSERT_EQ(12, snprintf(buf, sizeof(buf), "%.*s%c", INT_MAX-1, "hello world", '!'));
793 ASSERT_EQ(12, snprintf(buf, sizeof(buf), "%.*s%c", INT_MAX, "hello world", '!'));
794 ASSERT_EQ(12, snprintf(buf, sizeof(buf), "%.*s%c", -1, "hello world", '!'));
795
796 // INT_MAX-1, INT_MAX, INT_MAX+1.
797 ASSERT_EQ(12, snprintf(buf, sizeof(buf), "%.2147483646s%c", "hello world", '!'));
798 ASSERT_EQ(12, snprintf(buf, sizeof(buf), "%.2147483647s%c", "hello world", '!'));
799 ASSERT_EQ(-1, snprintf(buf, sizeof(buf), "%.2147483648s%c", "hello world", '!'));
800 ASSERT_EQ(ENOMEM, errno);
801}
802
Elliott Hughes70715da2016-08-01 16:35:17 -0700803TEST(STDIO_TEST, fprintf) {
804 TemporaryFile tf;
805
806 FILE* tfile = fdopen(tf.fd, "r+");
807 ASSERT_TRUE(tfile != nullptr);
808
809 ASSERT_EQ(7, fprintf(tfile, "%d %s", 123, "abc"));
810 AssertFileIs(tfile, "123 abc");
811 fclose(tfile);
812}
813
Christopher Ferris13f26a72016-01-13 13:47:58 -0800814TEST(STDIO_TEST, fprintf_failures_7229520) {
Elliott Hughes69f05d22014-06-05 20:10:09 -0700815 // http://b/7229520
Elliott Hughesc9244bd2014-05-14 13:31:35 -0700816 FILE* fp;
817
818 // Unbuffered case where the fprintf(3) itself fails.
819 ASSERT_NE(nullptr, fp = tmpfile());
820 setbuf(fp, NULL);
821 ASSERT_EQ(4, fprintf(fp, "epic"));
822 ASSERT_EQ(0, close(fileno(fp)));
823 ASSERT_EQ(-1, fprintf(fp, "fail"));
824 ASSERT_EQ(-1, fclose(fp));
825
826 // Buffered case where we won't notice until the fclose(3).
827 // It's likely this is what was actually seen in http://b/7229520,
828 // and that expecting fprintf to fail is setting yourself up for
829 // disappointment. Remember to check fclose(3)'s return value, kids!
830 ASSERT_NE(nullptr, fp = tmpfile());
831 ASSERT_EQ(4, fprintf(fp, "epic"));
832 ASSERT_EQ(0, close(fileno(fp)));
833 ASSERT_EQ(4, fprintf(fp, "fail"));
834 ASSERT_EQ(-1, fclose(fp));
835}
836
Christopher Ferris13f26a72016-01-13 13:47:58 -0800837TEST(STDIO_TEST, popen) {
Elliott Hughes6b3f49a2013-03-06 16:20:55 -0800838 FILE* fp = popen("cat /proc/version", "r");
839 ASSERT_TRUE(fp != NULL);
840
841 char buf[16];
842 char* s = fgets(buf, sizeof(buf), fp);
843 buf[13] = '\0';
844 ASSERT_STREQ("Linux version", s);
845
846 ASSERT_EQ(0, pclose(fp));
847}
Elliott Hughes6b05c8e2013-04-11 13:54:48 -0700848
Christopher Ferris13f26a72016-01-13 13:47:58 -0800849TEST(STDIO_TEST, getc) {
Elliott Hughes6b05c8e2013-04-11 13:54:48 -0700850 FILE* fp = fopen("/proc/version", "r");
851 ASSERT_TRUE(fp != NULL);
852 ASSERT_EQ('L', getc(fp));
853 ASSERT_EQ('i', getc(fp));
854 ASSERT_EQ('n', getc(fp));
855 ASSERT_EQ('u', getc(fp));
856 ASSERT_EQ('x', getc(fp));
857 fclose(fp);
858}
859
Christopher Ferris13f26a72016-01-13 13:47:58 -0800860TEST(STDIO_TEST, putc) {
Elliott Hughes6b05c8e2013-04-11 13:54:48 -0700861 FILE* fp = fopen("/proc/version", "r");
862 ASSERT_TRUE(fp != NULL);
863 ASSERT_EQ(EOF, putc('x', fp));
864 fclose(fp);
865}
Elliott Hughes603332f2014-03-12 17:10:41 -0700866
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700867TEST(STDIO_TEST, sscanf_swscanf) {
868 struct stuff {
869 char s1[123];
870 int i1;
871 double d1;
872 float f1;
873 char s2[123];
874
875 void Check() {
876 ASSERT_STREQ("hello", s1);
877 ASSERT_EQ(123, i1);
878 ASSERT_DOUBLE_EQ(1.23, d1);
879 ASSERT_FLOAT_EQ(9.0f, f1);
880 ASSERT_STREQ("world", s2);
881 }
882 } s;
883
884 memset(&s, 0, sizeof(s));
885 ASSERT_EQ(5, sscanf(" hello 123 1.23 0x1.2p3 world",
886 "%s %i %lf %f %s",
887 s.s1, &s.i1, &s.d1, &s.f1, s.s2));
888 s.Check();
889
890 memset(&s, 0, sizeof(s));
891 ASSERT_EQ(5, swscanf(L" hello 123 1.23 0x1.2p3 world",
892 L"%s %i %lf %f %s",
893 s.s1, &s.i1, &s.d1, &s.f1, s.s2));
894 s.Check();
Elliott Hughes603332f2014-03-12 17:10:41 -0700895}
Elliott Hughes53b24382014-05-02 18:29:25 -0700896
Christopher Ferris13f26a72016-01-13 13:47:58 -0800897TEST(STDIO_TEST, cantwrite_EBADF) {
Elliott Hughes53b24382014-05-02 18:29:25 -0700898 // If we open a file read-only...
899 FILE* fp = fopen("/proc/version", "r");
900
901 // ...all attempts to write to that file should return failure.
902
903 // They should also set errno to EBADF. This isn't POSIX, but it's traditional.
904 // glibc gets the wide-character functions wrong.
905
906 errno = 0;
907 EXPECT_EQ(EOF, putc('x', fp));
908 EXPECT_EQ(EBADF, errno);
909
910 errno = 0;
911 EXPECT_EQ(EOF, fprintf(fp, "hello"));
912 EXPECT_EQ(EBADF, errno);
913
914 errno = 0;
915 EXPECT_EQ(EOF, fwprintf(fp, L"hello"));
Elliott Hughes063525c2014-05-13 11:19:57 -0700916#if defined(__BIONIC__)
Elliott Hughes53b24382014-05-02 18:29:25 -0700917 EXPECT_EQ(EBADF, errno);
918#endif
919
920 errno = 0;
Elliott Hughes53b24382014-05-02 18:29:25 -0700921 EXPECT_EQ(0U, fwrite("hello", 1, 2, fp));
922 EXPECT_EQ(EBADF, errno);
923
924 errno = 0;
925 EXPECT_EQ(EOF, fputs("hello", fp));
926 EXPECT_EQ(EBADF, errno);
927
928 errno = 0;
929 EXPECT_EQ(WEOF, fputwc(L'x', fp));
Elliott Hughes063525c2014-05-13 11:19:57 -0700930#if defined(__BIONIC__)
Elliott Hughes53b24382014-05-02 18:29:25 -0700931 EXPECT_EQ(EBADF, errno);
932#endif
933}
Calin Juravle03e4ebe2014-05-08 14:42:06 +0100934
935// Tests that we can only have a consistent and correct fpos_t when using
936// f*pos functions (i.e. fpos doesn't get inside a multi byte character).
Christopher Ferris13f26a72016-01-13 13:47:58 -0800937TEST(STDIO_TEST, consistent_fpos_t) {
Calin Juravle03e4ebe2014-05-08 14:42:06 +0100938 ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
939 uselocale(LC_GLOBAL_LOCALE);
940
941 FILE* fp = tmpfile();
942 ASSERT_TRUE(fp != NULL);
943
944 wchar_t mb_one_bytes = L'h';
945 wchar_t mb_two_bytes = 0x00a2;
946 wchar_t mb_three_bytes = 0x20ac;
947 wchar_t mb_four_bytes = 0x24b62;
948
949 // Write to file.
950 ASSERT_EQ(mb_one_bytes, static_cast<wchar_t>(fputwc(mb_one_bytes, fp)));
951 ASSERT_EQ(mb_two_bytes, static_cast<wchar_t>(fputwc(mb_two_bytes, fp)));
952 ASSERT_EQ(mb_three_bytes, static_cast<wchar_t>(fputwc(mb_three_bytes, fp)));
953 ASSERT_EQ(mb_four_bytes, static_cast<wchar_t>(fputwc(mb_four_bytes, fp)));
954
955 rewind(fp);
956
957 // Record each character position.
958 fpos_t pos1;
959 fpos_t pos2;
960 fpos_t pos3;
961 fpos_t pos4;
962 fpos_t pos5;
963 EXPECT_EQ(0, fgetpos(fp, &pos1));
964 ASSERT_EQ(mb_one_bytes, static_cast<wchar_t>(fgetwc(fp)));
965 EXPECT_EQ(0, fgetpos(fp, &pos2));
966 ASSERT_EQ(mb_two_bytes, static_cast<wchar_t>(fgetwc(fp)));
967 EXPECT_EQ(0, fgetpos(fp, &pos3));
968 ASSERT_EQ(mb_three_bytes, static_cast<wchar_t>(fgetwc(fp)));
969 EXPECT_EQ(0, fgetpos(fp, &pos4));
970 ASSERT_EQ(mb_four_bytes, static_cast<wchar_t>(fgetwc(fp)));
971 EXPECT_EQ(0, fgetpos(fp, &pos5));
972
Elliott Hughes063525c2014-05-13 11:19:57 -0700973#if defined(__BIONIC__)
Calin Juravle03e4ebe2014-05-08 14:42:06 +0100974 // Bionic's fpos_t is just an alias for off_t. This is inherited from OpenBSD
975 // upstream. Glibc differs by storing the mbstate_t inside its fpos_t. In
976 // Bionic (and upstream OpenBSD) the mbstate_t is stored inside the FILE
977 // structure.
978 ASSERT_EQ(0, static_cast<off_t>(pos1));
979 ASSERT_EQ(1, static_cast<off_t>(pos2));
980 ASSERT_EQ(3, static_cast<off_t>(pos3));
981 ASSERT_EQ(6, static_cast<off_t>(pos4));
982 ASSERT_EQ(10, static_cast<off_t>(pos5));
983#endif
984
985 // Exercise back and forth movements of the position.
986 ASSERT_EQ(0, fsetpos(fp, &pos2));
987 ASSERT_EQ(mb_two_bytes, static_cast<wchar_t>(fgetwc(fp)));
988 ASSERT_EQ(0, fsetpos(fp, &pos1));
989 ASSERT_EQ(mb_one_bytes, static_cast<wchar_t>(fgetwc(fp)));
990 ASSERT_EQ(0, fsetpos(fp, &pos4));
991 ASSERT_EQ(mb_four_bytes, static_cast<wchar_t>(fgetwc(fp)));
992 ASSERT_EQ(0, fsetpos(fp, &pos3));
993 ASSERT_EQ(mb_three_bytes, static_cast<wchar_t>(fgetwc(fp)));
994 ASSERT_EQ(0, fsetpos(fp, &pos5));
995 ASSERT_EQ(WEOF, fgetwc(fp));
996
997 fclose(fp);
998}
999
1000// Exercise the interaction between fpos and seek.
Christopher Ferris13f26a72016-01-13 13:47:58 -08001001TEST(STDIO_TEST, fpos_t_and_seek) {
Calin Juravle03e4ebe2014-05-08 14:42:06 +01001002 ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
1003 uselocale(LC_GLOBAL_LOCALE);
1004
Calin Juravle9b95ea92014-05-14 17:07:10 +01001005 // In glibc-2.16 fseek doesn't work properly in wide mode
1006 // (https://sourceware.org/bugzilla/show_bug.cgi?id=14543). One workaround is
1007 // to close and re-open the file. We do it in order to make the test pass
1008 // with all glibcs.
1009
Calin Juravle03e4ebe2014-05-08 14:42:06 +01001010 TemporaryFile tf;
1011 FILE* fp = fdopen(tf.fd, "w+");
1012 ASSERT_TRUE(fp != NULL);
1013
1014 wchar_t mb_two_bytes = 0x00a2;
1015 wchar_t mb_three_bytes = 0x20ac;
1016 wchar_t mb_four_bytes = 0x24b62;
1017
1018 // Write to file.
1019 ASSERT_EQ(mb_two_bytes, static_cast<wchar_t>(fputwc(mb_two_bytes, fp)));
1020 ASSERT_EQ(mb_three_bytes, static_cast<wchar_t>(fputwc(mb_three_bytes, fp)));
1021 ASSERT_EQ(mb_four_bytes, static_cast<wchar_t>(fputwc(mb_four_bytes, fp)));
1022
1023 fflush(fp);
1024 fclose(fp);
1025
1026 fp = fopen(tf.filename, "r");
1027 ASSERT_TRUE(fp != NULL);
1028
1029 // Store a valid position.
1030 fpos_t mb_two_bytes_pos;
1031 ASSERT_EQ(0, fgetpos(fp, &mb_two_bytes_pos));
1032
1033 // Move inside mb_four_bytes with fseek.
1034 long offset_inside_mb = 6;
1035 ASSERT_EQ(0, fseek(fp, offset_inside_mb, SEEK_SET));
1036
1037 // Store the "inside multi byte" position.
1038 fpos_t pos_inside_mb;
1039 ASSERT_EQ(0, fgetpos(fp, &pos_inside_mb));
Elliott Hughes063525c2014-05-13 11:19:57 -07001040#if defined(__BIONIC__)
1041 ASSERT_EQ(offset_inside_mb, static_cast<off_t>(pos_inside_mb));
1042#endif
Calin Juravle03e4ebe2014-05-08 14:42:06 +01001043
1044 // Reading from within a byte should produce an error.
1045 ASSERT_EQ(WEOF, fgetwc(fp));
1046 ASSERT_EQ(EILSEQ, errno);
1047
1048 // Reverting to a valid position should work.
1049 ASSERT_EQ(0, fsetpos(fp, &mb_two_bytes_pos));
1050 ASSERT_EQ(mb_two_bytes, static_cast<wchar_t>(fgetwc(fp)));
1051
1052 // Moving withing a multi byte with fsetpos should work but reading should
1053 // produce an error.
1054 ASSERT_EQ(0, fsetpos(fp, &pos_inside_mb));
1055 ASSERT_EQ(WEOF, fgetwc(fp));
1056 ASSERT_EQ(EILSEQ, errno);
1057
Elliott Hughes3a4c4542017-07-19 17:20:24 -07001058 ASSERT_EQ(0, fclose(fp));
Calin Juravle03e4ebe2014-05-08 14:42:06 +01001059}
Elliott Hughes6b841db2014-08-20 16:10:49 -07001060
Christopher Ferris13f26a72016-01-13 13:47:58 -08001061TEST(STDIO_TEST, fmemopen) {
Elliott Hughes6b841db2014-08-20 16:10:49 -07001062 char buf[16];
1063 memset(buf, 0, sizeof(buf));
1064 FILE* fp = fmemopen(buf, sizeof(buf), "r+");
1065 ASSERT_EQ('<', fputc('<', fp));
1066 ASSERT_NE(EOF, fputs("abc>\n", fp));
1067 fflush(fp);
1068
Elliott Hughes3a4c4542017-07-19 17:20:24 -07001069 // We wrote to the buffer...
Elliott Hughes6b841db2014-08-20 16:10:49 -07001070 ASSERT_STREQ("<abc>\n", buf);
1071
Elliott Hughes3a4c4542017-07-19 17:20:24 -07001072 // And can read back from the file.
Elliott Hughes70715da2016-08-01 16:35:17 -07001073 AssertFileIs(fp, "<abc>\n", true);
Elliott Hughes3a4c4542017-07-19 17:20:24 -07001074 ASSERT_EQ(0, fclose(fp));
Elliott Hughes6b841db2014-08-20 16:10:49 -07001075}
1076
Elliott Hughes3a4c4542017-07-19 17:20:24 -07001077TEST(STDIO_TEST, fmemopen_nullptr) {
Elliott Hughes6b841db2014-08-20 16:10:49 -07001078 FILE* fp = fmemopen(nullptr, 128, "r+");
1079 ASSERT_NE(EOF, fputs("xyz\n", fp));
1080
Elliott Hughes70715da2016-08-01 16:35:17 -07001081 AssertFileIs(fp, "xyz\n", true);
Elliott Hughes3a4c4542017-07-19 17:20:24 -07001082 ASSERT_EQ(0, fclose(fp));
Elliott Hughes6b841db2014-08-20 16:10:49 -07001083}
1084
Elliott Hughes3a4c4542017-07-19 17:20:24 -07001085TEST(STDIO_TEST, fmemopen_trailing_NUL_byte) {
1086 FILE* fp;
1087 char buf[8];
1088
1089 // POSIX: "When a stream open for writing is flushed or closed, a null byte
1090 // shall be written at the current position or at the end of the buffer,
1091 // depending on the size of the contents."
1092 memset(buf, 'x', sizeof(buf));
1093 ASSERT_NE(nullptr, fp = fmemopen(buf, sizeof(buf), "w"));
1094 // Even with nothing written (and not in truncate mode), we'll flush a NUL...
1095 ASSERT_EQ(0, fflush(fp));
1096 EXPECT_EQ("\0xxxxxxx"s, std::string(buf, buf + sizeof(buf)));
1097 // Now write and check that the NUL moves along with our writes...
1098 ASSERT_NE(EOF, fputs("hello", fp));
1099 ASSERT_EQ(0, fflush(fp));
1100 EXPECT_EQ("hello\0xx"s, std::string(buf, buf + sizeof(buf)));
1101 ASSERT_NE(EOF, fputs("wo", fp));
1102 ASSERT_EQ(0, fflush(fp));
1103 EXPECT_EQ("hellowo\0"s, std::string(buf, buf + sizeof(buf)));
1104 ASSERT_EQ(0, fclose(fp));
1105
1106 // "If a stream open for update is flushed or closed and the last write has
1107 // advanced the current buffer size, a null byte shall be written at the end
1108 // of the buffer if it fits."
1109 memset(buf, 'x', sizeof(buf));
1110 ASSERT_NE(nullptr, fp = fmemopen(buf, sizeof(buf), "r+"));
1111 // Nothing written yet, so no advance...
1112 ASSERT_EQ(0, fflush(fp));
1113 EXPECT_EQ("xxxxxxxx"s, std::string(buf, buf + sizeof(buf)));
1114 ASSERT_NE(EOF, fputs("hello", fp));
1115 ASSERT_EQ(0, fclose(fp));
1116}
1117
1118TEST(STDIO_TEST, fmemopen_size) {
1119 FILE* fp;
Elliott Hughes6b841db2014-08-20 16:10:49 -07001120 char buf[16];
Elliott Hughes3a4c4542017-07-19 17:20:24 -07001121 memset(buf, 'x', sizeof(buf));
Elliott Hughes6b841db2014-08-20 16:10:49 -07001122
Elliott Hughes3a4c4542017-07-19 17:20:24 -07001123 // POSIX: "The stream shall also maintain the size of the current buffer
1124 // contents; use of fseek() or fseeko() on the stream with SEEK_END shall
1125 // seek relative to this size."
Elliott Hughes6b841db2014-08-20 16:10:49 -07001126
Elliott Hughes3a4c4542017-07-19 17:20:24 -07001127 // "For modes r and r+ the size shall be set to the value given by the size
1128 // argument."
1129 ASSERT_NE(nullptr, fp = fmemopen(buf, 16, "r"));
1130 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
1131 EXPECT_EQ(16, ftell(fp));
1132 EXPECT_EQ(16, ftello(fp));
1133 ASSERT_EQ(0, fseeko(fp, 0, SEEK_END));
1134 EXPECT_EQ(16, ftell(fp));
1135 EXPECT_EQ(16, ftello(fp));
1136 ASSERT_EQ(0, fclose(fp));
1137 ASSERT_NE(nullptr, fp = fmemopen(buf, 16, "r+"));
1138 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
1139 EXPECT_EQ(16, ftell(fp));
1140 EXPECT_EQ(16, ftello(fp));
1141 ASSERT_EQ(0, fseeko(fp, 0, SEEK_END));
1142 EXPECT_EQ(16, ftell(fp));
1143 EXPECT_EQ(16, ftello(fp));
1144 ASSERT_EQ(0, fclose(fp));
1145
1146 // "For modes w and w+ the initial size shall be zero..."
1147 ASSERT_NE(nullptr, fp = fmemopen(nullptr, 16, "w"));
1148 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
1149 EXPECT_EQ(0, ftell(fp));
1150 EXPECT_EQ(0, ftello(fp));
1151 ASSERT_EQ(0, fseeko(fp, 0, SEEK_END));
1152 EXPECT_EQ(0, ftell(fp));
1153 EXPECT_EQ(0, ftello(fp));
1154 ASSERT_EQ(0, fclose(fp));
1155 ASSERT_NE(nullptr, fp = fmemopen(nullptr, 16, "w+"));
1156 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
1157 EXPECT_EQ(0, ftell(fp));
1158 EXPECT_EQ(0, ftello(fp));
1159 ASSERT_EQ(0, fseeko(fp, 0, SEEK_END));
1160 EXPECT_EQ(0, ftell(fp));
1161 EXPECT_EQ(0, ftello(fp));
1162 ASSERT_EQ(0, fclose(fp));
1163
1164 // "...and for modes a and a+ the initial size shall be:
1165 // 1. Zero, if buf is a null pointer
1166 ASSERT_NE(nullptr, fp = fmemopen(nullptr, 16, "a"));
1167 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
1168 EXPECT_EQ(0, ftell(fp));
1169 EXPECT_EQ(0, ftello(fp));
1170 ASSERT_EQ(0, fseeko(fp, 0, SEEK_END));
1171 EXPECT_EQ(0, ftell(fp));
1172 EXPECT_EQ(0, ftello(fp));
1173 ASSERT_EQ(0, fclose(fp));
1174 ASSERT_NE(nullptr, fp = fmemopen(nullptr, 16, "a+"));
1175 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
1176 EXPECT_EQ(0, ftell(fp));
1177 EXPECT_EQ(0, ftello(fp));
1178 ASSERT_EQ(0, fseeko(fp, 0, SEEK_END));
1179 EXPECT_EQ(0, ftell(fp));
1180 EXPECT_EQ(0, ftello(fp));
1181 ASSERT_EQ(0, fclose(fp));
1182
1183 // 2. The position of the first null byte in the buffer, if one is found
1184 memset(buf, 'x', sizeof(buf));
1185 buf[3] = '\0';
1186 ASSERT_NE(nullptr, fp = fmemopen(buf, 16, "a"));
1187 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
1188 EXPECT_EQ(3, ftell(fp));
1189 EXPECT_EQ(3, ftello(fp));
1190 ASSERT_EQ(0, fseeko(fp, 0, SEEK_END));
1191 EXPECT_EQ(3, ftell(fp));
1192 EXPECT_EQ(3, ftello(fp));
1193 ASSERT_EQ(0, fclose(fp));
1194 memset(buf, 'x', sizeof(buf));
1195 buf[3] = '\0';
1196 ASSERT_NE(nullptr, fp = fmemopen(buf, 16, "a+"));
1197 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
1198 EXPECT_EQ(3, ftell(fp));
1199 EXPECT_EQ(3, ftello(fp));
1200 ASSERT_EQ(0, fseeko(fp, 0, SEEK_END));
1201 EXPECT_EQ(3, ftell(fp));
1202 EXPECT_EQ(3, ftello(fp));
1203 ASSERT_EQ(0, fclose(fp));
1204
1205 // 3. The value of the size argument, if buf is not a null pointer and no
1206 // null byte is found.
1207 memset(buf, 'x', sizeof(buf));
1208 ASSERT_NE(nullptr, fp = fmemopen(buf, 16, "a"));
1209 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
1210 EXPECT_EQ(16, ftell(fp));
1211 EXPECT_EQ(16, ftello(fp));
1212 ASSERT_EQ(0, fseeko(fp, 0, SEEK_END));
1213 EXPECT_EQ(16, ftell(fp));
1214 EXPECT_EQ(16, ftello(fp));
1215 ASSERT_EQ(0, fclose(fp));
1216 memset(buf, 'x', sizeof(buf));
1217 ASSERT_NE(nullptr, fp = fmemopen(buf, 16, "a+"));
1218 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
1219 EXPECT_EQ(16, ftell(fp));
1220 EXPECT_EQ(16, ftello(fp));
1221 ASSERT_EQ(0, fseeko(fp, 0, SEEK_END));
1222 EXPECT_EQ(16, ftell(fp));
1223 EXPECT_EQ(16, ftello(fp));
1224 ASSERT_EQ(0, fclose(fp));
1225}
1226
1227TEST(STDIO_TEST, fmemopen_SEEK_END) {
1228 // fseek SEEK_END is relative to the current string length, not the buffer size.
1229 FILE* fp;
1230 char buf[8];
1231 memset(buf, 'x', sizeof(buf));
1232 strcpy(buf, "str");
1233 ASSERT_NE(nullptr, fp = fmemopen(buf, sizeof(buf), "w+"));
1234 ASSERT_NE(EOF, fputs("string", fp));
1235 EXPECT_EQ(0, fseek(fp, 0, SEEK_END));
1236 EXPECT_EQ(static_cast<long>(strlen("string")), ftell(fp));
1237 EXPECT_EQ(static_cast<off_t>(strlen("string")), ftello(fp));
1238 EXPECT_EQ(0, fclose(fp));
1239
1240 // glibc < 2.22 interpreted SEEK_END the wrong way round (subtracting rather
1241 // than adding).
1242 ASSERT_NE(nullptr, fp = fmemopen(buf, sizeof(buf), "w+"));
1243 ASSERT_NE(EOF, fputs("54321", fp));
1244 EXPECT_EQ(0, fseek(fp, -2, SEEK_END));
1245 EXPECT_EQ('2', fgetc(fp));
1246 EXPECT_EQ(0, fclose(fp));
1247}
1248
1249TEST(STDIO_TEST, fmemopen_seek_invalid) {
1250 char buf[8];
1251 memset(buf, 'x', sizeof(buf));
1252 FILE* fp = fmemopen(buf, sizeof(buf), "w");
1253 ASSERT_TRUE(fp != nullptr);
1254
1255 // POSIX: "An attempt to seek ... to a negative position or to a position
1256 // larger than the buffer size given in the size argument shall fail."
1257 // (There's no mention of what errno should be set to, and glibc doesn't
1258 // set errno in any of these cases.)
1259 EXPECT_EQ(-1, fseek(fp, -2, SEEK_SET));
1260 EXPECT_EQ(-1, fseeko(fp, -2, SEEK_SET));
1261 EXPECT_EQ(-1, fseek(fp, sizeof(buf) + 1, SEEK_SET));
1262 EXPECT_EQ(-1, fseeko(fp, sizeof(buf) + 1, SEEK_SET));
1263}
1264
1265TEST(STDIO_TEST, fmemopen_read_EOF) {
1266 // POSIX: "A read operation on the stream shall not advance the current
1267 // buffer position beyond the current buffer size."
1268 char buf[8];
1269 memset(buf, 'x', sizeof(buf));
1270 FILE* fp = fmemopen(buf, sizeof(buf), "r");
1271 ASSERT_TRUE(fp != nullptr);
1272 char buf2[BUFSIZ];
1273 ASSERT_EQ(8U, fread(buf2, 1, sizeof(buf2), fp));
1274 // POSIX: "Reaching the buffer size in a read operation shall count as
1275 // end-of-file.
1276 ASSERT_TRUE(feof(fp));
1277 ASSERT_EQ(EOF, fgetc(fp));
1278 ASSERT_EQ(0, fclose(fp));
1279}
1280
1281TEST(STDIO_TEST, fmemopen_read_null_bytes) {
1282 // POSIX: "Null bytes in the buffer shall have no special meaning for reads."
1283 char buf[] = "h\0e\0l\0l\0o";
1284 FILE* fp = fmemopen(buf, sizeof(buf), "r");
1285 ASSERT_TRUE(fp != nullptr);
1286 ASSERT_EQ('h', fgetc(fp));
1287 ASSERT_EQ(0, fgetc(fp));
1288 ASSERT_EQ('e', fgetc(fp));
1289 ASSERT_EQ(0, fgetc(fp));
1290 ASSERT_EQ('l', fgetc(fp));
1291 ASSERT_EQ(0, fgetc(fp));
1292 // POSIX: "The read operation shall start at the current buffer position of
1293 // the stream."
1294 char buf2[8];
1295 memset(buf2, 'x', sizeof(buf2));
1296 ASSERT_EQ(4U, fread(buf2, 1, sizeof(buf2), fp));
1297 ASSERT_EQ('l', buf2[0]);
1298 ASSERT_EQ(0, buf2[1]);
1299 ASSERT_EQ('o', buf2[2]);
1300 ASSERT_EQ(0, buf2[3]);
1301 for (size_t i = 4; i < sizeof(buf2); ++i) ASSERT_EQ('x', buf2[i]) << i;
1302 ASSERT_TRUE(feof(fp));
1303 ASSERT_EQ(0, fclose(fp));
1304}
1305
1306TEST(STDIO_TEST, fmemopen_write) {
1307 FILE* fp;
1308 char buf[8];
1309
1310 // POSIX: "A write operation shall start either at the current position of
1311 // the stream (if mode has not specified 'a' as the first character)..."
1312 memset(buf, 'x', sizeof(buf));
1313 ASSERT_NE(nullptr, fp = fmemopen(buf, sizeof(buf), "r+"));
1314 setbuf(fp, nullptr); // Turn off buffering so we can see what's happening as it happens.
1315 ASSERT_EQ(0, fseek(fp, 2, SEEK_SET));
1316 ASSERT_EQ(' ', fputc(' ', fp));
1317 EXPECT_EQ("xx xxxxx", std::string(buf, buf + sizeof(buf)));
1318 ASSERT_EQ(0, fclose(fp));
1319
1320 // "...or at the current size of the stream (if mode had 'a' as the first
1321 // character)." (See the fmemopen_size test for what "size" means, but for
1322 // mode "a", it's the first NUL byte.)
1323 memset(buf, 'x', sizeof(buf));
1324 buf[3] = '\0';
1325 ASSERT_NE(nullptr, fp = fmemopen(buf, sizeof(buf), "a+"));
1326 setbuf(fp, nullptr); // Turn off buffering so we can see what's happening as it happens.
1327 ASSERT_EQ(' ', fputc(' ', fp));
1328 EXPECT_EQ("xxx \0xxx"s, std::string(buf, buf + sizeof(buf)));
1329 ASSERT_EQ(0, fclose(fp));
1330
1331 // "If the current position at the end of the write is larger than the
1332 // current buffer size, the current buffer size shall be set to the current
1333 // position." (See the fmemopen_size test for what "size" means, but to
1334 // query it we SEEK_END with offset 0, and then ftell.)
1335 memset(buf, 'x', sizeof(buf));
1336 ASSERT_NE(nullptr, fp = fmemopen(buf, sizeof(buf), "w+"));
1337 setbuf(fp, nullptr); // Turn off buffering so we can see what's happening as it happens.
1338 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
1339 EXPECT_EQ(0, ftell(fp));
1340 ASSERT_EQ(' ', fputc(' ', fp));
1341 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
1342 EXPECT_EQ(1, ftell(fp));
1343 ASSERT_NE(EOF, fputs("123", fp));
1344 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
1345 EXPECT_EQ(4, ftell(fp));
1346 EXPECT_EQ(" 123\0xxx"s, std::string(buf, buf + sizeof(buf)));
1347 ASSERT_EQ(0, fclose(fp));
1348}
1349
1350TEST(STDIO_TEST, fmemopen_write_EOF) {
1351 // POSIX: "A write operation on the stream shall not advance the current
1352 // buffer size beyond the size given in the size argument."
1353 FILE* fp;
1354
1355 // Scalar writes...
1356 ASSERT_NE(nullptr, fp = fmemopen(nullptr, 4, "w"));
1357 setbuf(fp, nullptr); // Turn off buffering so we can see what's happening as it happens.
1358 ASSERT_EQ('x', fputc('x', fp));
1359 ASSERT_EQ('x', fputc('x', fp));
1360 ASSERT_EQ('x', fputc('x', fp));
1361 ASSERT_EQ(EOF, fputc('x', fp)); // Only 3 fit because of the implicit NUL.
1362 ASSERT_EQ(0, fclose(fp));
1363
1364 // Vector writes...
1365 ASSERT_NE(nullptr, fp = fmemopen(nullptr, 4, "w"));
1366 setbuf(fp, nullptr); // Turn off buffering so we can see what's happening as it happens.
1367 ASSERT_EQ(3U, fwrite("xxxx", 1, 4, fp));
1368 ASSERT_EQ(0, fclose(fp));
1369}
1370
1371TEST(STDIO_TEST, fmemopen_initial_position) {
1372 // POSIX: "The ... current position in the buffer ... shall be initially
1373 // set to either the beginning of the buffer (for r and w modes) ..."
1374 char buf[] = "hello\0world";
1375 FILE* fp;
1376 ASSERT_NE(nullptr, fp = fmemopen(buf, sizeof(buf), "r"));
1377 EXPECT_EQ(0L, ftell(fp));
1378 EXPECT_EQ(0, fclose(fp));
1379 ASSERT_NE(nullptr, fp = fmemopen(buf, sizeof(buf), "w"));
1380 EXPECT_EQ(0L, ftell(fp));
1381 EXPECT_EQ(0, fclose(fp));
1382 buf[0] = 'h'; // (Undo the effects of the above.)
1383
1384 // POSIX: "...or to the first null byte in the buffer (for a modes)."
1385 ASSERT_NE(nullptr, fp = fmemopen(buf, sizeof(buf), "a"));
1386 EXPECT_EQ(5L, ftell(fp));
1387 EXPECT_EQ(0, fclose(fp));
1388
1389 // POSIX: "If no null byte is found in append mode, the initial position
1390 // shall be set to one byte after the end of the buffer."
1391 memset(buf, 'x', sizeof(buf));
1392 ASSERT_NE(nullptr, fp = fmemopen(buf, sizeof(buf), "a"));
1393 EXPECT_EQ(static_cast<long>(sizeof(buf)), ftell(fp));
1394 EXPECT_EQ(0, fclose(fp));
1395}
1396
1397TEST(STDIO_TEST, fmemopen_initial_position_allocated) {
1398 // POSIX: "If buf is a null pointer, the initial position shall always be
1399 // set to the beginning of the buffer."
1400 FILE* fp = fmemopen(nullptr, 128, "a+");
1401 ASSERT_TRUE(fp != nullptr);
1402 EXPECT_EQ(0L, ftell(fp));
1403 EXPECT_EQ(0L, fseek(fp, 0, SEEK_SET));
1404 EXPECT_EQ(0, fclose(fp));
1405}
1406
1407TEST(STDIO_TEST, fmemopen_zero_length) {
1408 // POSIX says it's up to the implementation whether or not you can have a
1409 // zero-length buffer (but "A future version of this standard may require
1410 // support of zero-length buffer streams explicitly"). BSD and glibc < 2.22
1411 // agreed that you couldn't, but glibc >= 2.22 allows it for consistency.
1412 FILE* fp;
1413 char buf[16];
1414 ASSERT_NE(nullptr, fp = fmemopen(buf, 0, "r+"));
1415 ASSERT_EQ(EOF, fgetc(fp));
1416 ASSERT_TRUE(feof(fp));
1417 ASSERT_EQ(0, fclose(fp));
1418 ASSERT_NE(nullptr, fp = fmemopen(nullptr, 0, "r+"));
1419 ASSERT_EQ(EOF, fgetc(fp));
1420 ASSERT_TRUE(feof(fp));
1421 ASSERT_EQ(0, fclose(fp));
1422
1423 ASSERT_NE(nullptr, fp = fmemopen(buf, 0, "w+"));
1424 setbuf(fp, nullptr); // Turn off buffering so we can see what's happening as it happens.
1425 ASSERT_EQ(EOF, fputc('x', fp));
1426 ASSERT_EQ(0, fclose(fp));
1427 ASSERT_NE(nullptr, fp = fmemopen(nullptr, 0, "w+"));
1428 setbuf(fp, nullptr); // Turn off buffering so we can see what's happening as it happens.
1429 ASSERT_EQ(EOF, fputc('x', fp));
1430 ASSERT_EQ(0, fclose(fp));
1431}
1432
1433TEST(STDIO_TEST, fmemopen_write_only_allocated) {
1434 // POSIX says fmemopen "may fail if the mode argument does not include a '+'".
1435 // BSD fails, glibc doesn't. We side with the more lenient.
1436 FILE* fp;
1437 ASSERT_NE(nullptr, fp = fmemopen(nullptr, 16, "r"));
1438 ASSERT_EQ(0, fclose(fp));
1439 ASSERT_NE(nullptr, fp = fmemopen(nullptr, 16, "w"));
1440 ASSERT_EQ(0, fclose(fp));
1441}
1442
1443TEST(STDIO_TEST, fmemopen_fileno) {
1444 // There's no fd backing an fmemopen FILE*.
1445 FILE* fp = fmemopen(nullptr, 16, "r");
1446 ASSERT_TRUE(fp != nullptr);
Elliott Hughes6b841db2014-08-20 16:10:49 -07001447 errno = 0;
Elliott Hughes3a4c4542017-07-19 17:20:24 -07001448 ASSERT_EQ(-1, fileno(fp));
1449 ASSERT_EQ(EBADF, errno);
1450 ASSERT_EQ(0, fclose(fp));
1451}
1452
1453TEST(STDIO_TEST, fmemopen_append_after_seek) {
1454 // In BSD and glibc < 2.22, append mode didn't force writes to append if
1455 // there had been an intervening seek.
1456
1457 FILE* fp;
1458 char buf[] = "hello\0world";
1459 ASSERT_NE(nullptr, fp = fmemopen(buf, sizeof(buf), "a"));
1460 setbuf(fp, nullptr); // Turn off buffering so we can see what's happening as it happens.
1461 ASSERT_EQ(0, fseek(fp, 0, SEEK_SET));
1462 ASSERT_NE(EOF, fputc('!', fp));
1463 EXPECT_EQ("hello!\0orld\0"s, std::string(buf, buf + sizeof(buf)));
1464 ASSERT_EQ(0, fclose(fp));
1465
1466 memcpy(buf, "hello\0world", sizeof(buf));
1467 ASSERT_NE(nullptr, fp = fmemopen(buf, sizeof(buf), "a+"));
1468 setbuf(fp, nullptr); // Turn off buffering so we can see what's happening as it happens.
1469 ASSERT_EQ(0, fseek(fp, 0, SEEK_SET));
1470 ASSERT_NE(EOF, fputc('!', fp));
1471 EXPECT_EQ("hello!\0orld\0"s, std::string(buf, buf + sizeof(buf)));
1472 ASSERT_EQ(0, fclose(fp));
Elliott Hughes6b841db2014-08-20 16:10:49 -07001473}
1474
Christopher Ferris13f26a72016-01-13 13:47:58 -08001475TEST(STDIO_TEST, open_memstream) {
Elliott Hughes6b841db2014-08-20 16:10:49 -07001476 char* p = nullptr;
1477 size_t size = 0;
1478 FILE* fp = open_memstream(&p, &size);
1479 ASSERT_NE(EOF, fputs("hello, world!", fp));
1480 fclose(fp);
1481
1482 ASSERT_STREQ("hello, world!", p);
1483 ASSERT_EQ(strlen("hello, world!"), size);
1484 free(p);
1485}
1486
Christopher Ferris13f26a72016-01-13 13:47:58 -08001487TEST(STDIO_TEST, open_memstream_EINVAL) {
Elliott Hughes6b841db2014-08-20 16:10:49 -07001488#if defined(__BIONIC__)
1489 char* p;
1490 size_t size;
1491
1492 // Invalid buffer.
1493 errno = 0;
1494 ASSERT_EQ(nullptr, open_memstream(nullptr, &size));
1495 ASSERT_EQ(EINVAL, errno);
1496
1497 // Invalid size.
1498 errno = 0;
1499 ASSERT_EQ(nullptr, open_memstream(&p, nullptr));
1500 ASSERT_EQ(EINVAL, errno);
1501#else
Elliott Hughes9677fab2016-01-25 15:50:59 -08001502 GTEST_LOG_(INFO) << "This test does nothing on glibc.\n";
Elliott Hughes6b841db2014-08-20 16:10:49 -07001503#endif
1504}
Elliott Hughes31165ed2014-09-23 17:34:29 -07001505
Christopher Ferris13f26a72016-01-13 13:47:58 -08001506TEST(STDIO_TEST, fdopen_CLOEXEC) {
Elliott Hughes31165ed2014-09-23 17:34:29 -07001507 int fd = open("/proc/version", O_RDONLY);
1508 ASSERT_TRUE(fd != -1);
1509
1510 // This fd doesn't have O_CLOEXEC...
1511 int flags = fcntl(fd, F_GETFD);
1512 ASSERT_TRUE(flags != -1);
1513 ASSERT_EQ(0, flags & FD_CLOEXEC);
1514
1515 FILE* fp = fdopen(fd, "re");
1516 ASSERT_TRUE(fp != NULL);
1517
1518 // ...but the new one does.
1519 flags = fcntl(fileno(fp), F_GETFD);
1520 ASSERT_TRUE(flags != -1);
1521 ASSERT_EQ(FD_CLOEXEC, flags & FD_CLOEXEC);
1522
1523 fclose(fp);
1524 close(fd);
1525}
1526
Christopher Ferris13f26a72016-01-13 13:47:58 -08001527TEST(STDIO_TEST, freopen_CLOEXEC) {
Elliott Hughes31165ed2014-09-23 17:34:29 -07001528 FILE* fp = fopen("/proc/version", "r");
1529 ASSERT_TRUE(fp != NULL);
1530
1531 // This FILE* doesn't have O_CLOEXEC...
1532 int flags = fcntl(fileno(fp), F_GETFD);
1533 ASSERT_TRUE(flags != -1);
1534 ASSERT_EQ(0, flags & FD_CLOEXEC);
1535
1536 fp = freopen("/proc/version", "re", fp);
1537
1538 // ...but the new one does.
1539 flags = fcntl(fileno(fp), F_GETFD);
1540 ASSERT_TRUE(flags != -1);
1541 ASSERT_EQ(FD_CLOEXEC, flags & FD_CLOEXEC);
1542
1543 fclose(fp);
1544}
Elliott Hughes20841a12014-12-01 16:13:30 -08001545
Elliott Hughesf226ee52016-02-03 11:24:28 -08001546TEST(STDIO_TEST, fopen64_freopen64) {
1547 FILE* fp = fopen64("/proc/version", "r");
1548 ASSERT_TRUE(fp != nullptr);
1549 fp = freopen64("/proc/version", "re", fp);
1550 ASSERT_TRUE(fp != nullptr);
1551 fclose(fp);
1552}
1553
Elliott Hughes20841a12014-12-01 16:13:30 -08001554// https://code.google.com/p/android/issues/detail?id=81155
1555// http://b/18556607
Christopher Ferris13f26a72016-01-13 13:47:58 -08001556TEST(STDIO_TEST, fread_unbuffered_pathological_performance) {
Elliott Hughes20841a12014-12-01 16:13:30 -08001557 FILE* fp = fopen("/dev/zero", "r");
1558 ASSERT_TRUE(fp != NULL);
1559
1560 // Make this stream unbuffered.
1561 setvbuf(fp, 0, _IONBF, 0);
1562
1563 char buf[65*1024];
1564 memset(buf, 0xff, sizeof(buf));
1565
1566 time_t t0 = time(NULL);
1567 for (size_t i = 0; i < 1024; ++i) {
Elliott Hughese6bb5a22015-01-23 17:48:15 -08001568 ASSERT_EQ(1U, fread(buf, 64*1024, 1, fp));
Elliott Hughes20841a12014-12-01 16:13:30 -08001569 }
1570 time_t t1 = time(NULL);
1571
1572 fclose(fp);
1573
1574 // 1024 64KiB reads should have been very quick.
1575 ASSERT_LE(t1 - t0, 1);
1576
1577 for (size_t i = 0; i < 64*1024; ++i) {
1578 ASSERT_EQ('\0', buf[i]);
1579 }
1580 for (size_t i = 64*1024; i < 65*1024; ++i) {
1581 ASSERT_EQ('\xff', buf[i]);
1582 }
1583}
Elliott Hughes75b99382015-01-20 11:23:50 -08001584
Christopher Ferris13f26a72016-01-13 13:47:58 -08001585TEST(STDIO_TEST, fread_EOF) {
Elliott Hughes0ed7e082015-01-22 15:13:38 -08001586 std::string digits("0123456789");
1587 FILE* fp = fmemopen(&digits[0], digits.size(), "r");
Elliott Hughes75b99382015-01-20 11:23:50 -08001588
1589 // Try to read too much, but little enough that it still fits in the FILE's internal buffer.
1590 char buf1[4 * 4];
1591 memset(buf1, 0, sizeof(buf1));
1592 ASSERT_EQ(2U, fread(buf1, 4, 4, fp));
Elliott Hughes0ed7e082015-01-22 15:13:38 -08001593 ASSERT_STREQ("0123456789", buf1);
Elliott Hughes75b99382015-01-20 11:23:50 -08001594 ASSERT_TRUE(feof(fp));
1595
1596 rewind(fp);
1597
Elliott Hughes0ed7e082015-01-22 15:13:38 -08001598 // Try to read way too much so stdio tries to read more direct from the stream.
1599 char buf2[4 * 4096];
Elliott Hughes75b99382015-01-20 11:23:50 -08001600 memset(buf2, 0, sizeof(buf2));
1601 ASSERT_EQ(2U, fread(buf2, 4, 4096, fp));
Elliott Hughes0ed7e082015-01-22 15:13:38 -08001602 ASSERT_STREQ("0123456789", buf2);
Elliott Hughes75b99382015-01-20 11:23:50 -08001603 ASSERT_TRUE(feof(fp));
1604
1605 fclose(fp);
1606}
Elliott Hughese6bb5a22015-01-23 17:48:15 -08001607
1608static void test_fread_from_write_only_stream(size_t n) {
1609 FILE* fp = fopen("/dev/null", "w");
1610 std::vector<char> buf(n, 0);
1611 errno = 0;
1612 ASSERT_EQ(0U, fread(&buf[0], n, 1, fp));
1613 ASSERT_EQ(EBADF, errno);
1614 ASSERT_TRUE(ferror(fp));
1615 ASSERT_FALSE(feof(fp));
1616 fclose(fp);
1617}
1618
Christopher Ferris13f26a72016-01-13 13:47:58 -08001619TEST(STDIO_TEST, fread_from_write_only_stream_slow_path) {
Elliott Hughese6bb5a22015-01-23 17:48:15 -08001620 test_fread_from_write_only_stream(1);
1621}
1622
Christopher Ferris13f26a72016-01-13 13:47:58 -08001623TEST(STDIO_TEST, fread_from_write_only_stream_fast_path) {
Elliott Hughese6bb5a22015-01-23 17:48:15 -08001624 test_fread_from_write_only_stream(64*1024);
1625}
1626
1627static void test_fwrite_after_fread(size_t n) {
1628 TemporaryFile tf;
1629
1630 FILE* fp = fdopen(tf.fd, "w+");
1631 ASSERT_EQ(1U, fwrite("1", 1, 1, fp));
1632 fflush(fp);
1633
1634 // We've flushed but not rewound, so there's nothing to read.
1635 std::vector<char> buf(n, 0);
1636 ASSERT_EQ(0U, fread(&buf[0], 1, buf.size(), fp));
1637 ASSERT_TRUE(feof(fp));
1638
1639 // But hitting EOF doesn't prevent us from writing...
1640 errno = 0;
Elliott Hughes9677fab2016-01-25 15:50:59 -08001641 ASSERT_EQ(1U, fwrite("2", 1, 1, fp)) << strerror(errno);
Elliott Hughese6bb5a22015-01-23 17:48:15 -08001642
1643 // And if we rewind, everything's there.
1644 rewind(fp);
1645 ASSERT_EQ(2U, fread(&buf[0], 1, buf.size(), fp));
1646 ASSERT_EQ('1', buf[0]);
1647 ASSERT_EQ('2', buf[1]);
1648
1649 fclose(fp);
1650}
1651
Christopher Ferris13f26a72016-01-13 13:47:58 -08001652TEST(STDIO_TEST, fwrite_after_fread_slow_path) {
Elliott Hughese6bb5a22015-01-23 17:48:15 -08001653 test_fwrite_after_fread(16);
1654}
1655
Christopher Ferris13f26a72016-01-13 13:47:58 -08001656TEST(STDIO_TEST, fwrite_after_fread_fast_path) {
Elliott Hughese6bb5a22015-01-23 17:48:15 -08001657 test_fwrite_after_fread(64*1024);
1658}
Christopher Ferriscc9ca102015-02-27 18:22:45 -08001659
1660// http://b/19172514
Christopher Ferris13f26a72016-01-13 13:47:58 -08001661TEST(STDIO_TEST, fread_after_fseek) {
Christopher Ferriscc9ca102015-02-27 18:22:45 -08001662 TemporaryFile tf;
1663
1664 FILE* fp = fopen(tf.filename, "w+");
1665 ASSERT_TRUE(fp != nullptr);
1666
1667 char file_data[12288];
1668 for (size_t i = 0; i < 12288; i++) {
1669 file_data[i] = i;
1670 }
1671 ASSERT_EQ(12288U, fwrite(file_data, 1, 12288, fp));
1672 fclose(fp);
1673
1674 fp = fopen(tf.filename, "r");
1675 ASSERT_TRUE(fp != nullptr);
1676
1677 char buffer[8192];
1678 size_t cur_location = 0;
1679 // Small read to populate internal buffer.
1680 ASSERT_EQ(100U, fread(buffer, 1, 100, fp));
1681 ASSERT_EQ(memcmp(file_data, buffer, 100), 0);
1682
1683 cur_location = static_cast<size_t>(ftell(fp));
1684 // Large read to force reading into the user supplied buffer and bypassing
1685 // the internal buffer.
1686 ASSERT_EQ(8192U, fread(buffer, 1, 8192, fp));
1687 ASSERT_EQ(memcmp(file_data+cur_location, buffer, 8192), 0);
1688
1689 // Small backwards seek to verify fseek does not reuse the internal buffer.
Elliott Hughes9677fab2016-01-25 15:50:59 -08001690 ASSERT_EQ(0, fseek(fp, -22, SEEK_CUR)) << strerror(errno);
Christopher Ferriscc9ca102015-02-27 18:22:45 -08001691 cur_location = static_cast<size_t>(ftell(fp));
1692 ASSERT_EQ(22U, fread(buffer, 1, 22, fp));
1693 ASSERT_EQ(memcmp(file_data+cur_location, buffer, 22), 0);
1694
1695 fclose(fp);
1696}
Elliott Hughes8ab433d2015-10-09 17:57:26 -07001697
1698// https://code.google.com/p/android/issues/detail?id=184847
Christopher Ferris13f26a72016-01-13 13:47:58 -08001699TEST(STDIO_TEST, fread_EOF_184847) {
Elliott Hughes8ab433d2015-10-09 17:57:26 -07001700 TemporaryFile tf;
1701 char buf[6] = {0};
1702
1703 FILE* fw = fopen(tf.filename, "w");
1704 ASSERT_TRUE(fw != nullptr);
1705
1706 FILE* fr = fopen(tf.filename, "r");
1707 ASSERT_TRUE(fr != nullptr);
1708
1709 fwrite("a", 1, 1, fw);
1710 fflush(fw);
1711 ASSERT_EQ(1U, fread(buf, 1, 1, fr));
1712 ASSERT_STREQ("a", buf);
1713
1714 // 'fr' is now at EOF.
1715 ASSERT_EQ(0U, fread(buf, 1, 1, fr));
1716 ASSERT_TRUE(feof(fr));
1717
1718 // Write some more...
1719 fwrite("z", 1, 1, fw);
1720 fflush(fw);
1721
1722 // ...and check that we can read it back.
1723 // (BSD thinks that once a stream has hit EOF, it must always return EOF. SysV disagrees.)
1724 ASSERT_EQ(1U, fread(buf, 1, 1, fr));
1725 ASSERT_STREQ("z", buf);
1726
1727 // But now we're done.
1728 ASSERT_EQ(0U, fread(buf, 1, 1, fr));
1729
1730 fclose(fr);
1731 fclose(fw);
1732}
Elliott Hughes923f1652016-01-19 15:46:05 -08001733
1734TEST(STDIO_TEST, fclose_invalidates_fd) {
1735 // The typical error we're trying to help people catch involves accessing
1736 // memory after it's been freed. But we know that stdin/stdout/stderr are
1737 // special and don't get deallocated, so this test uses stdin.
1738 ASSERT_EQ(0, fclose(stdin));
1739
1740 // Even though using a FILE* after close is undefined behavior, I've closed
1741 // this bug as "WAI" too many times. We shouldn't hand out stale fds,
1742 // especially because they might actually correspond to a real stream.
1743 errno = 0;
1744 ASSERT_EQ(-1, fileno(stdin));
1745 ASSERT_EQ(EBADF, errno);
1746}
Elliott Hughes2704bd12016-01-20 17:14:53 -08001747
1748TEST(STDIO_TEST, fseek_ftell_unseekable) {
1749#if defined(__BIONIC__) // glibc has fopencookie instead.
1750 auto read_fn = [](void*, char*, int) { return -1; };
1751 FILE* fp = funopen(nullptr, read_fn, nullptr, nullptr, nullptr);
1752 ASSERT_TRUE(fp != nullptr);
1753
1754 // Check that ftell balks on an unseekable FILE*.
1755 errno = 0;
1756 ASSERT_EQ(-1, ftell(fp));
1757 ASSERT_EQ(ESPIPE, errno);
1758
1759 // SEEK_CUR is rewritten as SEEK_SET internally...
1760 errno = 0;
1761 ASSERT_EQ(-1, fseek(fp, 0, SEEK_CUR));
1762 ASSERT_EQ(ESPIPE, errno);
1763
1764 // ...so it's worth testing the direct seek path too.
1765 errno = 0;
1766 ASSERT_EQ(-1, fseek(fp, 0, SEEK_SET));
1767 ASSERT_EQ(ESPIPE, errno);
1768
1769 fclose(fp);
Elliott Hughes03e65eb2016-01-26 14:13:04 -08001770#else
1771 GTEST_LOG_(INFO) << "glibc uses fopencookie instead.\n";
1772#endif
1773}
1774
1775TEST(STDIO_TEST, funopen_EINVAL) {
1776#if defined(__BIONIC__)
1777 errno = 0;
1778 ASSERT_EQ(nullptr, funopen(nullptr, nullptr, nullptr, nullptr, nullptr));
1779 ASSERT_EQ(EINVAL, errno);
1780#else
1781 GTEST_LOG_(INFO) << "glibc uses fopencookie instead.\n";
1782#endif
1783}
1784
1785TEST(STDIO_TEST, funopen_seek) {
1786#if defined(__BIONIC__)
1787 auto read_fn = [](void*, char*, int) { return -1; };
1788
1789 auto seek_fn = [](void*, fpos_t, int) -> fpos_t { return 0xfedcba12; };
1790 auto seek64_fn = [](void*, fpos64_t, int) -> fpos64_t { return 0xfedcba12345678; };
1791
1792 FILE* fp = funopen(nullptr, read_fn, nullptr, seek_fn, nullptr);
1793 ASSERT_TRUE(fp != nullptr);
1794 fpos_t pos;
Elliott Hughes955426e2016-01-26 18:25:52 -08001795#if defined(__LP64__)
1796 EXPECT_EQ(0, fgetpos(fp, &pos)) << strerror(errno);
1797 EXPECT_EQ(0xfedcba12LL, pos);
1798#else
1799 EXPECT_EQ(-1, fgetpos(fp, &pos)) << strerror(errno);
1800 EXPECT_EQ(EOVERFLOW, errno);
1801#endif
Elliott Hughes03e65eb2016-01-26 14:13:04 -08001802
1803 FILE* fp64 = funopen64(nullptr, read_fn, nullptr, seek64_fn, nullptr);
1804 ASSERT_TRUE(fp64 != nullptr);
1805 fpos64_t pos64;
Elliott Hughes955426e2016-01-26 18:25:52 -08001806 EXPECT_EQ(0, fgetpos64(fp64, &pos64)) << strerror(errno);
1807 EXPECT_EQ(0xfedcba12345678, pos64);
Elliott Hughes03e65eb2016-01-26 14:13:04 -08001808#else
1809 GTEST_LOG_(INFO) << "glibc uses fopencookie instead.\n";
Elliott Hughes2704bd12016-01-20 17:14:53 -08001810#endif
1811}
Elliott Hughes71288cb2016-01-22 19:22:44 -08001812
1813TEST(STDIO_TEST, lots_of_concurrent_files) {
1814 std::vector<TemporaryFile*> tfs;
1815 std::vector<FILE*> fps;
1816
1817 for (size_t i = 0; i < 256; ++i) {
1818 TemporaryFile* tf = new TemporaryFile;
1819 tfs.push_back(tf);
1820 FILE* fp = fopen(tf->filename, "w+");
1821 fps.push_back(fp);
1822 fprintf(fp, "hello %zu!\n", i);
1823 fflush(fp);
1824 }
1825
1826 for (size_t i = 0; i < 256; ++i) {
Elliott Hughes71288cb2016-01-22 19:22:44 -08001827 char expected[BUFSIZ];
1828 snprintf(expected, sizeof(expected), "hello %zu!\n", i);
Elliott Hughes71288cb2016-01-22 19:22:44 -08001829
Elliott Hughes70715da2016-08-01 16:35:17 -07001830 AssertFileIs(fps[i], expected);
Elliott Hughes71288cb2016-01-22 19:22:44 -08001831 fclose(fps[i]);
1832 delete tfs[i];
1833 }
1834}
Elliott Hughes9677fab2016-01-25 15:50:59 -08001835
1836static void AssertFileOffsetAt(FILE* fp, off64_t offset) {
1837 EXPECT_EQ(offset, ftell(fp));
1838 EXPECT_EQ(offset, ftello(fp));
Elliott Hughese4fa6e92016-02-02 22:39:15 -08001839 EXPECT_EQ(offset, ftello64(fp));
Elliott Hughes9677fab2016-01-25 15:50:59 -08001840 fpos_t pos;
1841 fpos64_t pos64;
1842 EXPECT_EQ(0, fgetpos(fp, &pos));
1843 EXPECT_EQ(0, fgetpos64(fp, &pos64));
1844#if defined(__BIONIC__)
1845 EXPECT_EQ(offset, static_cast<off64_t>(pos));
1846 EXPECT_EQ(offset, static_cast<off64_t>(pos64));
1847#else
1848 GTEST_LOG_(INFO) << "glibc's fpos_t is opaque.\n";
1849#endif
1850}
1851
1852TEST(STDIO_TEST, seek_tell_family_smoke) {
1853 TemporaryFile tf;
1854 FILE* fp = fdopen(tf.fd, "w+");
1855
1856 // Initially we should be at 0.
1857 AssertFileOffsetAt(fp, 0);
1858
1859 // Seek to offset 8192.
1860 ASSERT_EQ(0, fseek(fp, 8192, SEEK_SET));
1861 AssertFileOffsetAt(fp, 8192);
1862 fpos_t eight_k_pos;
1863 ASSERT_EQ(0, fgetpos(fp, &eight_k_pos));
1864
1865 // Seek forward another 8192...
1866 ASSERT_EQ(0, fseek(fp, 8192, SEEK_CUR));
1867 AssertFileOffsetAt(fp, 8192 + 8192);
1868 fpos64_t sixteen_k_pos64;
1869 ASSERT_EQ(0, fgetpos64(fp, &sixteen_k_pos64));
1870
1871 // Seek back 8192...
1872 ASSERT_EQ(0, fseek(fp, -8192, SEEK_CUR));
1873 AssertFileOffsetAt(fp, 8192);
1874
1875 // Since we haven't written anything, the end is also at 0.
1876 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
1877 AssertFileOffsetAt(fp, 0);
1878
1879 // Check that our fpos64_t from 16KiB works...
1880 ASSERT_EQ(0, fsetpos64(fp, &sixteen_k_pos64));
1881 AssertFileOffsetAt(fp, 8192 + 8192);
1882 // ...as does our fpos_t from 8192.
1883 ASSERT_EQ(0, fsetpos(fp, &eight_k_pos));
1884 AssertFileOffsetAt(fp, 8192);
1885
1886 // Do fseeko and fseeko64 work too?
1887 ASSERT_EQ(0, fseeko(fp, 1234, SEEK_SET));
1888 AssertFileOffsetAt(fp, 1234);
1889 ASSERT_EQ(0, fseeko64(fp, 5678, SEEK_SET));
1890 AssertFileOffsetAt(fp, 5678);
1891
1892 fclose(fp);
1893}
1894
1895TEST(STDIO_TEST, fseek_fseeko_EINVAL) {
1896 TemporaryFile tf;
1897 FILE* fp = fdopen(tf.fd, "w+");
1898
1899 // Bad whence.
1900 errno = 0;
1901 ASSERT_EQ(-1, fseek(fp, 0, 123));
1902 ASSERT_EQ(EINVAL, errno);
1903 errno = 0;
1904 ASSERT_EQ(-1, fseeko(fp, 0, 123));
1905 ASSERT_EQ(EINVAL, errno);
1906 errno = 0;
1907 ASSERT_EQ(-1, fseeko64(fp, 0, 123));
1908 ASSERT_EQ(EINVAL, errno);
1909
1910 // Bad offset.
1911 errno = 0;
1912 ASSERT_EQ(-1, fseek(fp, -1, SEEK_SET));
1913 ASSERT_EQ(EINVAL, errno);
1914 errno = 0;
1915 ASSERT_EQ(-1, fseeko(fp, -1, SEEK_SET));
1916 ASSERT_EQ(EINVAL, errno);
1917 errno = 0;
1918 ASSERT_EQ(-1, fseeko64(fp, -1, SEEK_SET));
1919 ASSERT_EQ(EINVAL, errno);
1920
1921 fclose(fp);
1922}
Elliott Hughes20788ae2016-06-09 15:16:32 -07001923
1924TEST(STDIO_TEST, ctermid) {
1925 ASSERT_STREQ("/dev/tty", ctermid(nullptr));
1926
1927 char buf[L_ctermid] = {};
1928 ASSERT_EQ(buf, ctermid(buf));
1929 ASSERT_STREQ("/dev/tty", buf);
1930}
Elliott Hughesd1f25a72016-08-05 15:53:03 -07001931
1932TEST(STDIO_TEST, remove) {
1933 struct stat sb;
1934
1935 TemporaryFile tf;
1936 ASSERT_EQ(0, remove(tf.filename));
1937 ASSERT_EQ(-1, lstat(tf.filename, &sb));
1938 ASSERT_EQ(ENOENT, errno);
1939
1940 TemporaryDir td;
1941 ASSERT_EQ(0, remove(td.dirname));
1942 ASSERT_EQ(-1, lstat(td.dirname, &sb));
1943 ASSERT_EQ(ENOENT, errno);
1944
1945 errno = 0;
1946 ASSERT_EQ(-1, remove(tf.filename));
1947 ASSERT_EQ(ENOENT, errno);
1948
1949 errno = 0;
1950 ASSERT_EQ(-1, remove(td.dirname));
1951 ASSERT_EQ(ENOENT, errno);
1952}
Elliott Hughesfb3873d2016-08-10 11:07:54 -07001953
1954TEST(STDIO_DEATHTEST, snprintf_30445072_known_buffer_size) {
1955 char buf[16];
1956 ASSERT_EXIT(snprintf(buf, atol("-1"), "hello"),
1957 testing::KilledBySignal(SIGABRT),
1958#if defined(NOFORTIFY)
1959 "FORTIFY: vsnprintf: size .* > SSIZE_MAX"
1960#else
1961 "FORTIFY: vsnprintf: prevented .*-byte write into 16-byte buffer"
1962#endif
1963 );
1964}
1965
1966TEST(STDIO_DEATHTEST, snprintf_30445072_unknown_buffer_size) {
1967 std::string buf = "world";
1968 ASSERT_EXIT(snprintf(&buf[0], atol("-1"), "hello"),
1969 testing::KilledBySignal(SIGABRT),
1970 "FORTIFY: vsnprintf: size .* > SSIZE_MAX");
1971}
1972
1973TEST(STDIO_TEST, sprintf_30445072) {
1974 std::string buf = "world";
1975 sprintf(&buf[0], "hello");
1976 ASSERT_EQ(buf, "hello");
1977}
Elliott Hughes33a8cb12017-07-25 18:06:46 -07001978
1979TEST(STDIO_TEST, fopen_append_mode_and_ftell) {
1980 TemporaryFile tf;
1981 SetFileTo(tf.filename, "0123456789");
1982 FILE* fp = fopen(tf.filename, "a");
1983 EXPECT_EQ(10, ftell(fp));
1984 ASSERT_EQ(0, fseek(fp, 2, SEEK_SET));
1985 EXPECT_EQ(2, ftell(fp));
1986 ASSERT_NE(EOF, fputs("xxx", fp));
1987 ASSERT_EQ(0, fflush(fp));
1988 EXPECT_EQ(13, ftell(fp));
1989 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
1990 EXPECT_EQ(13, ftell(fp));
1991 ASSERT_EQ(0, fclose(fp));
1992 AssertFileIs(tf.filename, "0123456789xxx");
1993}
1994
1995TEST(STDIO_TEST, fdopen_append_mode_and_ftell) {
1996 TemporaryFile tf;
1997 SetFileTo(tf.filename, "0123456789");
1998 int fd = open(tf.filename, O_RDWR);
1999 ASSERT_NE(-1, fd);
2000 // POSIX: "The file position indicator associated with the new stream is set to the position
2001 // indicated by the file offset associated with the file descriptor."
2002 ASSERT_EQ(4, lseek(fd, 4, SEEK_SET));
2003 FILE* fp = fdopen(fd, "a");
2004 EXPECT_EQ(4, ftell(fp));
2005 ASSERT_EQ(0, fseek(fp, 2, SEEK_SET));
2006 EXPECT_EQ(2, ftell(fp));
2007 ASSERT_NE(EOF, fputs("xxx", fp));
2008 ASSERT_EQ(0, fflush(fp));
2009 EXPECT_EQ(13, ftell(fp));
2010 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
2011 EXPECT_EQ(13, ftell(fp));
2012 ASSERT_EQ(0, fclose(fp));
2013 AssertFileIs(tf.filename, "0123456789xxx");
2014}
2015
2016TEST(STDIO_TEST, freopen_append_mode_and_ftell) {
2017 TemporaryFile tf;
2018 SetFileTo(tf.filename, "0123456789");
2019 FILE* other_fp = fopen("/proc/version", "r");
2020 FILE* fp = freopen(tf.filename, "a", other_fp);
2021 EXPECT_EQ(10, ftell(fp));
2022 ASSERT_EQ(0, fseek(fp, 2, SEEK_SET));
2023 EXPECT_EQ(2, ftell(fp));
2024 ASSERT_NE(EOF, fputs("xxx", fp));
2025 ASSERT_EQ(0, fflush(fp));
2026 EXPECT_EQ(13, ftell(fp));
2027 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
2028 EXPECT_EQ(13, ftell(fp));
2029 ASSERT_EQ(0, fclose(fp));
2030 AssertFileIs(tf.filename, "0123456789xxx");
2031}
Elliott Hughesb15feb72017-07-31 17:20:18 -07002032
2033TEST(STDIO_TEST, constants) {
2034 ASSERT_LE(FILENAME_MAX, PATH_MAX);
2035 ASSERT_EQ(L_tmpnam, PATH_MAX);
2036}
Elliott Hughes37ad9592017-10-30 17:47:12 -07002037
2038TEST(STDIO_TEST, perror) {
2039 ExecTestHelper eth;
2040 eth.Run([&]() { errno = EINVAL; perror("a b c"); exit(0); }, 0, "a b c: Invalid argument\n");
2041 eth.Run([&]() { errno = EINVAL; perror(nullptr); exit(0); }, 0, "Invalid argument\n");
2042 eth.Run([&]() { errno = EINVAL; perror(""); exit(0); }, 0, "Invalid argument\n");
2043}
2044
2045TEST(STDIO_TEST, puts) {
2046 ExecTestHelper eth;
2047 eth.Run([&]() { exit(puts("a b c")); }, 0, "a b c\n");
2048}
2049
2050TEST(STDIO_TEST, unlocked) {
2051 TemporaryFile tf;
2052
2053 FILE* fp = fopen(tf.filename, "w+");
2054 ASSERT_TRUE(fp != nullptr);
2055
2056 clearerr_unlocked(fp);
2057 ASSERT_FALSE(feof_unlocked(fp));
2058 ASSERT_FALSE(ferror_unlocked(fp));
2059
2060 ASSERT_EQ(fileno(fp), fileno_unlocked(fp));
2061
2062 ASSERT_NE(EOF, putc_unlocked('a', fp));
2063 ASSERT_NE(EOF, putc('b', fp));
2064 ASSERT_NE(EOF, fputc_unlocked('c', fp));
2065 ASSERT_NE(EOF, fputc('d', fp));
2066
2067 rewind(fp);
2068 ASSERT_EQ('a', getc_unlocked(fp));
2069 ASSERT_EQ('b', getc(fp));
2070 ASSERT_EQ('c', fgetc_unlocked(fp));
2071 ASSERT_EQ('d', fgetc(fp));
2072
2073 rewind(fp);
2074 ASSERT_EQ(2U, fwrite_unlocked("AB", 1, 2, fp));
2075 ASSERT_EQ(2U, fwrite("CD", 1, 2, fp));
2076 ASSERT_EQ(0, fflush_unlocked(fp));
2077
2078 rewind(fp);
2079 char buf[BUFSIZ] = {};
2080 ASSERT_EQ(2U, fread_unlocked(&buf[0], 1, 2, fp));
2081 ASSERT_EQ(2U, fread(&buf[2], 1, 2, fp));
2082 ASSERT_STREQ("ABCD", buf);
2083
2084 rewind(fp);
2085 ASSERT_NE(EOF, fputs("hello ", fp));
2086 ASSERT_NE(EOF, fputs_unlocked("world", fp));
2087 ASSERT_NE(EOF, fputc('\n', fp));
2088
2089 rewind(fp);
2090 ASSERT_TRUE(fgets_unlocked(buf, sizeof(buf), fp) != nullptr);
2091 ASSERT_STREQ("hello world\n", buf);
2092
2093 ASSERT_EQ(0, fclose(fp));
2094}