2 * Copyright (C) 2012 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include <gtest/gtest.h>
24 #include <sys/types.h>
32 #include "TemporaryFile.h"
34 #if defined(NOFORTIFY)
35 #define STDIO_TEST stdio_nofortify
37 #define STDIO_TEST stdio
40 TEST(STDIO_TEST, flockfile_18208568_stderr) {
41 // Check that we have a _recursive_ mutex for flockfile.
43 feof(stderr); // We don't care about the result, but this needs to take the lock.
47 TEST(STDIO_TEST, flockfile_18208568_regular) {
48 // We never had a bug for streams other than stdin/stdout/stderr, but test anyway.
49 FILE* fp = fopen("/dev/null", "w");
50 ASSERT_TRUE(fp != NULL);
57 TEST(STDIO_TEST, tmpfile_fileno_fprintf_rewind_fgets) {
59 ASSERT_TRUE(fp != NULL);
65 int rc = fstat(fd, &sb);
67 ASSERT_EQ(sb.st_mode & 0777, 0600U);
69 rc = fprintf(fp, "hello\n");
75 char* s = fgets(buf, sizeof(buf), fp);
76 ASSERT_TRUE(s != NULL);
77 ASSERT_STREQ("hello\n", s);
82 TEST(STDIO_TEST, dprintf) {
85 int rc = dprintf(tf.fd, "hello\n");
88 lseek(tf.fd, 0, SEEK_SET);
89 FILE* tfile = fdopen(tf.fd, "r");
90 ASSERT_TRUE(tfile != NULL);
93 ASSERT_EQ(buf, fgets(buf, sizeof(buf), tfile));
94 ASSERT_STREQ("hello\n", buf);
95 // Make sure there isn't anything else in the file.
96 ASSERT_EQ(NULL, fgets(buf, sizeof(buf), tfile));
100 TEST(STDIO_TEST, getdelim) {
101 FILE* fp = tmpfile();
102 ASSERT_TRUE(fp != NULL);
104 const char* line_written = "This is a test";
105 int rc = fprintf(fp, "%s", line_written);
106 ASSERT_EQ(rc, static_cast<int>(strlen(line_written)));
110 char* word_read = NULL;
111 size_t allocated_length = 0;
113 const char* expected[] = { "This ", " ", "is ", "a ", "test" };
114 for (size_t i = 0; i < 5; ++i) {
115 ASSERT_FALSE(feof(fp));
116 ASSERT_EQ(getdelim(&word_read, &allocated_length, ' ', fp), static_cast<int>(strlen(expected[i])));
117 ASSERT_GE(allocated_length, strlen(expected[i]));
118 ASSERT_STREQ(expected[i], word_read);
120 // The last read should have set the end-of-file indicator for the stream.
121 ASSERT_TRUE(feof(fp));
124 // getdelim returns -1 but doesn't set errno if we're already at EOF.
125 // It should set the end-of-file indicator for the stream, though.
127 ASSERT_EQ(getdelim(&word_read, &allocated_length, ' ', fp), -1);
129 ASSERT_TRUE(feof(fp));
135 TEST(STDIO_TEST, getdelim_invalid) {
136 FILE* fp = tmpfile();
137 ASSERT_TRUE(fp != NULL);
140 size_t buffer_length = 0;
142 // The first argument can't be NULL.
144 ASSERT_EQ(getdelim(NULL, &buffer_length, ' ', fp), -1);
145 ASSERT_EQ(EINVAL, errno);
147 // The second argument can't be NULL.
149 ASSERT_EQ(getdelim(&buffer, NULL, ' ', fp), -1);
150 ASSERT_EQ(EINVAL, errno);
152 // The underlying fd can't be closed.
153 ASSERT_EQ(0, close(fileno(fp)));
155 ASSERT_EQ(getdelim(&buffer, &buffer_length, ' ', fp), -1);
156 ASSERT_EQ(EBADF, errno);
160 TEST(STDIO_TEST, getdelim_directory) {
161 FILE* fp = fopen("/proc", "r");
162 ASSERT_TRUE(fp != NULL);
164 size_t allocated_length;
165 ASSERT_EQ(-1, getdelim(&word_read, &allocated_length, ' ', fp));
169 TEST(STDIO_TEST, getline) {
170 FILE* fp = tmpfile();
171 ASSERT_TRUE(fp != NULL);
173 const char* line_written = "This is a test for getline\n";
174 const size_t line_count = 5;
176 for (size_t i = 0; i < line_count; ++i) {
177 int rc = fprintf(fp, "%s", line_written);
178 ASSERT_EQ(rc, static_cast<int>(strlen(line_written)));
183 char* line_read = NULL;
184 size_t allocated_length = 0;
186 size_t read_line_count = 0;
187 ssize_t read_char_count;
188 while ((read_char_count = getline(&line_read, &allocated_length, fp)) != -1) {
189 ASSERT_EQ(read_char_count, static_cast<int>(strlen(line_written)));
190 ASSERT_GE(allocated_length, strlen(line_written));
191 ASSERT_STREQ(line_written, line_read);
194 ASSERT_EQ(read_line_count, line_count);
196 // The last read should have set the end-of-file indicator for the stream.
197 ASSERT_TRUE(feof(fp));
200 // getline returns -1 but doesn't set errno if we're already at EOF.
201 // It should set the end-of-file indicator for the stream, though.
203 ASSERT_EQ(getline(&line_read, &allocated_length, fp), -1);
205 ASSERT_TRUE(feof(fp));
211 TEST(STDIO_TEST, getline_invalid) {
212 FILE* fp = tmpfile();
213 ASSERT_TRUE(fp != NULL);
216 size_t buffer_length = 0;
218 // The first argument can't be NULL.
220 ASSERT_EQ(getline(NULL, &buffer_length, fp), -1);
221 ASSERT_EQ(EINVAL, errno);
223 // The second argument can't be NULL.
225 ASSERT_EQ(getline(&buffer, NULL, fp), -1);
226 ASSERT_EQ(EINVAL, errno);
228 // The underlying fd can't be closed.
229 ASSERT_EQ(0, close(fileno(fp)));
231 ASSERT_EQ(getline(&buffer, &buffer_length, fp), -1);
232 ASSERT_EQ(EBADF, errno);
236 TEST(STDIO_TEST, printf_ssize_t) {
238 ASSERT_EQ(sizeof(ssize_t), sizeof(long int));
239 ASSERT_EQ(sizeof(ssize_t), sizeof(size_t));
240 // For our 32-bit ABI, we had a ssize_t definition that confuses GCC into saying:
241 // error: format '%zd' expects argument of type 'signed size_t',
242 // but argument 4 has type 'ssize_t {aka long int}' [-Werror=format]
245 snprintf(buf, sizeof(buf), "%zd", v);
248 // https://code.google.com/p/android/issues/detail?id=64886
249 TEST(STDIO_TEST, snprintf_a) {
251 EXPECT_EQ(23, snprintf(buf, sizeof(buf), "<%a>", 9990.235));
252 EXPECT_STREQ("<0x1.3831e147ae148p+13>", buf);
255 TEST(STDIO_TEST, snprintf_lc) {
258 EXPECT_EQ(3, snprintf(buf, sizeof(buf), "<%lc>", wc));
259 EXPECT_STREQ("<a>", buf);
262 TEST(STDIO_TEST, snprintf_ls) {
265 EXPECT_EQ(8, snprintf(buf, sizeof(buf), "<%ls>", ws));
266 EXPECT_STREQ("<(null)>", buf);
268 wchar_t chars[] = { L'h', L'i', 0 };
270 EXPECT_EQ(4, snprintf(buf, sizeof(buf), "<%ls>", ws));
271 EXPECT_STREQ("<hi>", buf);
274 TEST(STDIO_TEST, snprintf_n) {
275 #if defined(__BIONIC__)
279 EXPECT_EQ(5, snprintf(buf, sizeof(buf), "a %n b", &i));
281 EXPECT_STREQ("a n b", buf);
283 GTEST_LOG_(INFO) << "This test does nothing.\n";
287 TEST(STDIO_TEST, snprintf_smoke) {
290 snprintf(buf, sizeof(buf), "a");
291 EXPECT_STREQ("a", buf);
293 snprintf(buf, sizeof(buf), "%%");
294 EXPECT_STREQ("%", buf);
296 snprintf(buf, sizeof(buf), "01234");
297 EXPECT_STREQ("01234", buf);
299 snprintf(buf, sizeof(buf), "a%sb", "01234");
300 EXPECT_STREQ("a01234b", buf);
303 snprintf(buf, sizeof(buf), "a%sb", s);
304 EXPECT_STREQ("a(null)b", buf);
306 snprintf(buf, sizeof(buf), "aa%scc", "bb");
307 EXPECT_STREQ("aabbcc", buf);
309 snprintf(buf, sizeof(buf), "a%cc", 'b');
310 EXPECT_STREQ("abc", buf);
312 snprintf(buf, sizeof(buf), "a%db", 1234);
313 EXPECT_STREQ("a1234b", buf);
315 snprintf(buf, sizeof(buf), "a%db", -8123);
316 EXPECT_STREQ("a-8123b", buf);
318 snprintf(buf, sizeof(buf), "a%hdb", static_cast<short>(0x7fff0010));
319 EXPECT_STREQ("a16b", buf);
321 snprintf(buf, sizeof(buf), "a%hhdb", static_cast<char>(0x7fffff10));
322 EXPECT_STREQ("a16b", buf);
324 snprintf(buf, sizeof(buf), "a%lldb", 0x1000000000LL);
325 EXPECT_STREQ("a68719476736b", buf);
327 snprintf(buf, sizeof(buf), "a%ldb", 70000L);
328 EXPECT_STREQ("a70000b", buf);
330 snprintf(buf, sizeof(buf), "a%pb", reinterpret_cast<void*>(0xb0001234));
331 EXPECT_STREQ("a0xb0001234b", buf);
333 snprintf(buf, sizeof(buf), "a%xz", 0x12ab);
334 EXPECT_STREQ("a12abz", buf);
336 snprintf(buf, sizeof(buf), "a%Xz", 0x12ab);
337 EXPECT_STREQ("a12ABz", buf);
339 snprintf(buf, sizeof(buf), "a%08xz", 0x123456);
340 EXPECT_STREQ("a00123456z", buf);
342 snprintf(buf, sizeof(buf), "a%5dz", 1234);
343 EXPECT_STREQ("a 1234z", buf);
345 snprintf(buf, sizeof(buf), "a%05dz", 1234);
346 EXPECT_STREQ("a01234z", buf);
348 snprintf(buf, sizeof(buf), "a%8dz", 1234);
349 EXPECT_STREQ("a 1234z", buf);
351 snprintf(buf, sizeof(buf), "a%-8dz", 1234);
352 EXPECT_STREQ("a1234 z", buf);
354 snprintf(buf, sizeof(buf), "A%-11sZ", "abcdef");
355 EXPECT_STREQ("Aabcdef Z", buf);
357 snprintf(buf, sizeof(buf), "A%s:%dZ", "hello", 1234);
358 EXPECT_STREQ("Ahello:1234Z", buf);
360 snprintf(buf, sizeof(buf), "a%03d:%d:%02dz", 5, 5, 5);
361 EXPECT_STREQ("a005:5:05z", buf);
364 snprintf(buf, sizeof(buf), "a%d,%pz", 5, p);
365 #if defined(__BIONIC__)
366 EXPECT_STREQ("a5,0x0z", buf);
368 EXPECT_STREQ("a5,(nil)z", buf);
371 snprintf(buf, sizeof(buf), "a%lld,%d,%d,%dz", 0x1000000000LL, 6, 7, 8);
372 EXPECT_STREQ("a68719476736,6,7,8z", buf);
374 snprintf(buf, sizeof(buf), "a_%f_b", 1.23f);
375 EXPECT_STREQ("a_1.230000_b", buf);
377 snprintf(buf, sizeof(buf), "a_%g_b", 3.14);
378 EXPECT_STREQ("a_3.14_b", buf);
380 snprintf(buf, sizeof(buf), "%1$s %1$s", "print_me_twice");
381 EXPECT_STREQ("print_me_twice print_me_twice", buf);
384 template <typename T>
385 void CheckInfNan(int snprintf_fn(T*, size_t, const T*, ...),
386 const T* fmt, const T* fmt_plus,
387 const T* minus_inf, const T* inf_, const T* plus_inf,
388 const T* minus_nan, const T* nan_, const T* plus_nan) {
391 snprintf_fn(buf, sizeof(buf), fmt, nan(""));
392 EXPECT_STREQ(nan_, buf) << fmt;
393 snprintf_fn(buf, sizeof(buf), fmt, -nan(""));
394 EXPECT_STREQ(minus_nan, buf) << fmt;
395 snprintf_fn(buf, sizeof(buf), fmt_plus, nan(""));
396 EXPECT_STREQ(plus_nan, buf) << fmt_plus;
397 snprintf_fn(buf, sizeof(buf), fmt_plus, -nan(""));
398 EXPECT_STREQ(minus_nan, buf) << fmt_plus;
400 snprintf_fn(buf, sizeof(buf), fmt, HUGE_VAL);
401 EXPECT_STREQ(inf_, buf) << fmt;
402 snprintf_fn(buf, sizeof(buf), fmt, -HUGE_VAL);
403 EXPECT_STREQ(minus_inf, buf) << fmt;
404 snprintf_fn(buf, sizeof(buf), fmt_plus, HUGE_VAL);
405 EXPECT_STREQ(plus_inf, buf) << fmt_plus;
406 snprintf_fn(buf, sizeof(buf), fmt_plus, -HUGE_VAL);
407 EXPECT_STREQ(minus_inf, buf) << fmt_plus;
410 TEST(STDIO_TEST, snprintf_inf_nan) {
411 CheckInfNan(snprintf, "%a", "%+a", "-inf", "inf", "+inf", "-nan", "nan", "+nan");
412 CheckInfNan(snprintf, "%A", "%+A", "-INF", "INF", "+INF", "-NAN", "NAN", "+NAN");
413 CheckInfNan(snprintf, "%e", "%+e", "-inf", "inf", "+inf", "-nan", "nan", "+nan");
414 CheckInfNan(snprintf, "%E", "%+E", "-INF", "INF", "+INF", "-NAN", "NAN", "+NAN");
415 CheckInfNan(snprintf, "%f", "%+f", "-inf", "inf", "+inf", "-nan", "nan", "+nan");
416 CheckInfNan(snprintf, "%F", "%+F", "-INF", "INF", "+INF", "-NAN", "NAN", "+NAN");
417 CheckInfNan(snprintf, "%g", "%+g", "-inf", "inf", "+inf", "-nan", "nan", "+nan");
418 CheckInfNan(snprintf, "%G", "%+G", "-INF", "INF", "+INF", "-NAN", "NAN", "+NAN");
421 TEST(STDIO_TEST, wsprintf_inf_nan) {
422 CheckInfNan(swprintf, L"%a", L"%+a", L"-inf", L"inf", L"+inf", L"-nan", L"nan", L"+nan");
423 CheckInfNan(swprintf, L"%A", L"%+A", L"-INF", L"INF", L"+INF", L"-NAN", L"NAN", L"+NAN");
424 CheckInfNan(swprintf, L"%e", L"%+e", L"-inf", L"inf", L"+inf", L"-nan", L"nan", L"+nan");
425 CheckInfNan(swprintf, L"%E", L"%+E", L"-INF", L"INF", L"+INF", L"-NAN", L"NAN", L"+NAN");
426 CheckInfNan(swprintf, L"%f", L"%+f", L"-inf", L"inf", L"+inf", L"-nan", L"nan", L"+nan");
427 CheckInfNan(swprintf, L"%F", L"%+F", L"-INF", L"INF", L"+INF", L"-NAN", L"NAN", L"+NAN");
428 CheckInfNan(swprintf, L"%g", L"%+g", L"-inf", L"inf", L"+inf", L"-nan", L"nan", L"+nan");
429 CheckInfNan(swprintf, L"%G", L"%+G", L"-INF", L"INF", L"+INF", L"-NAN", L"NAN", L"+NAN");
432 TEST(STDIO_TEST, snprintf_d_INT_MAX) {
434 snprintf(buf, sizeof(buf), "%d", INT_MAX);
435 EXPECT_STREQ("2147483647", buf);
438 TEST(STDIO_TEST, snprintf_d_INT_MIN) {
440 snprintf(buf, sizeof(buf), "%d", INT_MIN);
441 EXPECT_STREQ("-2147483648", buf);
444 TEST(STDIO_TEST, snprintf_ld_LONG_MAX) {
446 snprintf(buf, sizeof(buf), "%ld", LONG_MAX);
448 EXPECT_STREQ("9223372036854775807", buf);
450 EXPECT_STREQ("2147483647", buf);
454 TEST(STDIO_TEST, snprintf_ld_LONG_MIN) {
456 snprintf(buf, sizeof(buf), "%ld", LONG_MIN);
458 EXPECT_STREQ("-9223372036854775808", buf);
460 EXPECT_STREQ("-2147483648", buf);
464 TEST(STDIO_TEST, snprintf_lld_LLONG_MAX) {
466 snprintf(buf, sizeof(buf), "%lld", LLONG_MAX);
467 EXPECT_STREQ("9223372036854775807", buf);
470 TEST(STDIO_TEST, snprintf_lld_LLONG_MIN) {
472 snprintf(buf, sizeof(buf), "%lld", LLONG_MIN);
473 EXPECT_STREQ("-9223372036854775808", buf);
476 TEST(STDIO_TEST, snprintf_e) {
479 snprintf(buf, sizeof(buf), "%e", 1.5);
480 EXPECT_STREQ("1.500000e+00", buf);
482 snprintf(buf, sizeof(buf), "%Le", 1.5l);
483 EXPECT_STREQ("1.500000e+00", buf);
486 TEST(STDIO_TEST, snprintf_negative_zero_5084292) {
489 snprintf(buf, sizeof(buf), "%e", -0.0);
490 EXPECT_STREQ("-0.000000e+00", buf);
491 snprintf(buf, sizeof(buf), "%E", -0.0);
492 EXPECT_STREQ("-0.000000E+00", buf);
493 snprintf(buf, sizeof(buf), "%f", -0.0);
494 EXPECT_STREQ("-0.000000", buf);
495 snprintf(buf, sizeof(buf), "%F", -0.0);
496 EXPECT_STREQ("-0.000000", buf);
497 snprintf(buf, sizeof(buf), "%g", -0.0);
498 EXPECT_STREQ("-0", buf);
499 snprintf(buf, sizeof(buf), "%G", -0.0);
500 EXPECT_STREQ("-0", buf);
501 snprintf(buf, sizeof(buf), "%a", -0.0);
502 EXPECT_STREQ("-0x0p+0", buf);
503 snprintf(buf, sizeof(buf), "%A", -0.0);
504 EXPECT_STREQ("-0X0P+0", buf);
507 TEST(STDIO_TEST, snprintf_utf8_15439554) {
508 locale_t cloc = newlocale(LC_ALL, "C.UTF-8", 0);
509 locale_t old_locale = uselocale(cloc);
515 snprintf(buf, sizeof(buf), "%dx%d", 1, 2);
516 EXPECT_STREQ("1x2", buf);
518 snprintf(buf, sizeof(buf), "%d\xc2\xa2%d", 1, 2);
519 EXPECT_STREQ("1¢2", buf);
521 snprintf(buf, sizeof(buf), "%d\xe2\x82\xac%d", 1, 2);
522 EXPECT_STREQ("1€2", buf);
524 snprintf(buf, sizeof(buf), "%d\xf0\xa4\xad\xa2%d", 1, 2);
525 EXPECT_STREQ("1ð¤¢2", buf);
527 uselocale(old_locale);
531 TEST(STDIO_TEST, fprintf_failures_7229520) {
535 // Unbuffered case where the fprintf(3) itself fails.
536 ASSERT_NE(nullptr, fp = tmpfile());
538 ASSERT_EQ(4, fprintf(fp, "epic"));
539 ASSERT_EQ(0, close(fileno(fp)));
540 ASSERT_EQ(-1, fprintf(fp, "fail"));
541 ASSERT_EQ(-1, fclose(fp));
543 // Buffered case where we won't notice until the fclose(3).
544 // It's likely this is what was actually seen in http://b/7229520,
545 // and that expecting fprintf to fail is setting yourself up for
546 // disappointment. Remember to check fclose(3)'s return value, kids!
547 ASSERT_NE(nullptr, fp = tmpfile());
548 ASSERT_EQ(4, fprintf(fp, "epic"));
549 ASSERT_EQ(0, close(fileno(fp)));
550 ASSERT_EQ(4, fprintf(fp, "fail"));
551 ASSERT_EQ(-1, fclose(fp));
554 TEST(STDIO_TEST, popen) {
555 FILE* fp = popen("cat /proc/version", "r");
556 ASSERT_TRUE(fp != NULL);
559 char* s = fgets(buf, sizeof(buf), fp);
561 ASSERT_STREQ("Linux version", s);
563 ASSERT_EQ(0, pclose(fp));
566 TEST(STDIO_TEST, getc) {
567 FILE* fp = fopen("/proc/version", "r");
568 ASSERT_TRUE(fp != NULL);
569 ASSERT_EQ('L', getc(fp));
570 ASSERT_EQ('i', getc(fp));
571 ASSERT_EQ('n', getc(fp));
572 ASSERT_EQ('u', getc(fp));
573 ASSERT_EQ('x', getc(fp));
577 TEST(STDIO_TEST, putc) {
578 FILE* fp = fopen("/proc/version", "r");
579 ASSERT_TRUE(fp != NULL);
580 ASSERT_EQ(EOF, putc('x', fp));
584 TEST(STDIO_TEST, sscanf) {
589 ASSERT_EQ(3, sscanf(" hello 123 1.23 ", "%s %i %lf %s", s1, &i1, &d1, s2));
590 ASSERT_STREQ("hello", s1);
592 ASSERT_DOUBLE_EQ(1.23, d1);
595 TEST(STDIO_TEST, cantwrite_EBADF) {
596 // If we open a file read-only...
597 FILE* fp = fopen("/proc/version", "r");
599 // ...all attempts to write to that file should return failure.
601 // They should also set errno to EBADF. This isn't POSIX, but it's traditional.
602 // glibc gets the wide-character functions wrong.
605 EXPECT_EQ(EOF, putc('x', fp));
606 EXPECT_EQ(EBADF, errno);
609 EXPECT_EQ(EOF, fprintf(fp, "hello"));
610 EXPECT_EQ(EBADF, errno);
613 EXPECT_EQ(EOF, fwprintf(fp, L"hello"));
614 #if defined(__BIONIC__)
615 EXPECT_EQ(EBADF, errno);
619 EXPECT_EQ(0U, fwrite("hello", 1, 2, fp));
620 EXPECT_EQ(EBADF, errno);
623 EXPECT_EQ(EOF, fputs("hello", fp));
624 EXPECT_EQ(EBADF, errno);
627 EXPECT_EQ(WEOF, fputwc(L'x', fp));
628 #if defined(__BIONIC__)
629 EXPECT_EQ(EBADF, errno);
633 // Tests that we can only have a consistent and correct fpos_t when using
634 // f*pos functions (i.e. fpos doesn't get inside a multi byte character).
635 TEST(STDIO_TEST, consistent_fpos_t) {
636 ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
637 uselocale(LC_GLOBAL_LOCALE);
639 FILE* fp = tmpfile();
640 ASSERT_TRUE(fp != NULL);
642 wchar_t mb_one_bytes = L'h';
643 wchar_t mb_two_bytes = 0x00a2;
644 wchar_t mb_three_bytes = 0x20ac;
645 wchar_t mb_four_bytes = 0x24b62;
648 ASSERT_EQ(mb_one_bytes, static_cast<wchar_t>(fputwc(mb_one_bytes, fp)));
649 ASSERT_EQ(mb_two_bytes, static_cast<wchar_t>(fputwc(mb_two_bytes, fp)));
650 ASSERT_EQ(mb_three_bytes, static_cast<wchar_t>(fputwc(mb_three_bytes, fp)));
651 ASSERT_EQ(mb_four_bytes, static_cast<wchar_t>(fputwc(mb_four_bytes, fp)));
655 // Record each character position.
661 EXPECT_EQ(0, fgetpos(fp, &pos1));
662 ASSERT_EQ(mb_one_bytes, static_cast<wchar_t>(fgetwc(fp)));
663 EXPECT_EQ(0, fgetpos(fp, &pos2));
664 ASSERT_EQ(mb_two_bytes, static_cast<wchar_t>(fgetwc(fp)));
665 EXPECT_EQ(0, fgetpos(fp, &pos3));
666 ASSERT_EQ(mb_three_bytes, static_cast<wchar_t>(fgetwc(fp)));
667 EXPECT_EQ(0, fgetpos(fp, &pos4));
668 ASSERT_EQ(mb_four_bytes, static_cast<wchar_t>(fgetwc(fp)));
669 EXPECT_EQ(0, fgetpos(fp, &pos5));
671 #if defined(__BIONIC__)
672 // Bionic's fpos_t is just an alias for off_t. This is inherited from OpenBSD
673 // upstream. Glibc differs by storing the mbstate_t inside its fpos_t. In
674 // Bionic (and upstream OpenBSD) the mbstate_t is stored inside the FILE
676 ASSERT_EQ(0, static_cast<off_t>(pos1));
677 ASSERT_EQ(1, static_cast<off_t>(pos2));
678 ASSERT_EQ(3, static_cast<off_t>(pos3));
679 ASSERT_EQ(6, static_cast<off_t>(pos4));
680 ASSERT_EQ(10, static_cast<off_t>(pos5));
683 // Exercise back and forth movements of the position.
684 ASSERT_EQ(0, fsetpos(fp, &pos2));
685 ASSERT_EQ(mb_two_bytes, static_cast<wchar_t>(fgetwc(fp)));
686 ASSERT_EQ(0, fsetpos(fp, &pos1));
687 ASSERT_EQ(mb_one_bytes, static_cast<wchar_t>(fgetwc(fp)));
688 ASSERT_EQ(0, fsetpos(fp, &pos4));
689 ASSERT_EQ(mb_four_bytes, static_cast<wchar_t>(fgetwc(fp)));
690 ASSERT_EQ(0, fsetpos(fp, &pos3));
691 ASSERT_EQ(mb_three_bytes, static_cast<wchar_t>(fgetwc(fp)));
692 ASSERT_EQ(0, fsetpos(fp, &pos5));
693 ASSERT_EQ(WEOF, fgetwc(fp));
698 // Exercise the interaction between fpos and seek.
699 TEST(STDIO_TEST, fpos_t_and_seek) {
700 ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
701 uselocale(LC_GLOBAL_LOCALE);
703 // In glibc-2.16 fseek doesn't work properly in wide mode
704 // (https://sourceware.org/bugzilla/show_bug.cgi?id=14543). One workaround is
705 // to close and re-open the file. We do it in order to make the test pass
709 FILE* fp = fdopen(tf.fd, "w+");
710 ASSERT_TRUE(fp != NULL);
712 wchar_t mb_two_bytes = 0x00a2;
713 wchar_t mb_three_bytes = 0x20ac;
714 wchar_t mb_four_bytes = 0x24b62;
717 ASSERT_EQ(mb_two_bytes, static_cast<wchar_t>(fputwc(mb_two_bytes, fp)));
718 ASSERT_EQ(mb_three_bytes, static_cast<wchar_t>(fputwc(mb_three_bytes, fp)));
719 ASSERT_EQ(mb_four_bytes, static_cast<wchar_t>(fputwc(mb_four_bytes, fp)));
724 fp = fopen(tf.filename, "r");
725 ASSERT_TRUE(fp != NULL);
727 // Store a valid position.
728 fpos_t mb_two_bytes_pos;
729 ASSERT_EQ(0, fgetpos(fp, &mb_two_bytes_pos));
731 // Move inside mb_four_bytes with fseek.
732 long offset_inside_mb = 6;
733 ASSERT_EQ(0, fseek(fp, offset_inside_mb, SEEK_SET));
735 // Store the "inside multi byte" position.
736 fpos_t pos_inside_mb;
737 ASSERT_EQ(0, fgetpos(fp, &pos_inside_mb));
738 #if defined(__BIONIC__)
739 ASSERT_EQ(offset_inside_mb, static_cast<off_t>(pos_inside_mb));
742 // Reading from within a byte should produce an error.
743 ASSERT_EQ(WEOF, fgetwc(fp));
744 ASSERT_EQ(EILSEQ, errno);
746 // Reverting to a valid position should work.
747 ASSERT_EQ(0, fsetpos(fp, &mb_two_bytes_pos));
748 ASSERT_EQ(mb_two_bytes, static_cast<wchar_t>(fgetwc(fp)));
750 // Moving withing a multi byte with fsetpos should work but reading should
752 ASSERT_EQ(0, fsetpos(fp, &pos_inside_mb));
753 ASSERT_EQ(WEOF, fgetwc(fp));
754 ASSERT_EQ(EILSEQ, errno);
759 TEST(STDIO_TEST, fmemopen) {
761 memset(buf, 0, sizeof(buf));
762 FILE* fp = fmemopen(buf, sizeof(buf), "r+");
763 ASSERT_EQ('<', fputc('<', fp));
764 ASSERT_NE(EOF, fputs("abc>\n", fp));
767 ASSERT_STREQ("<abc>\n", buf);
772 char* s = fgets(line, sizeof(line), fp);
773 ASSERT_TRUE(s != NULL);
774 ASSERT_STREQ("<abc>\n", s);
779 TEST(STDIO_TEST, fmemopen_NULL) {
780 FILE* fp = fmemopen(nullptr, 128, "r+");
781 ASSERT_NE(EOF, fputs("xyz\n", fp));
786 char* s = fgets(line, sizeof(line), fp);
787 ASSERT_TRUE(s != NULL);
788 ASSERT_STREQ("xyz\n", s);
793 TEST(STDIO_TEST, fmemopen_EINVAL) {
798 ASSERT_EQ(nullptr, fmemopen(buf, 0, "r+"));
799 ASSERT_EQ(EINVAL, errno);
801 // No '+' with NULL buffer.
803 ASSERT_EQ(nullptr, fmemopen(nullptr, 0, "r"));
804 ASSERT_EQ(EINVAL, errno);
807 TEST(STDIO_TEST, open_memstream) {
810 FILE* fp = open_memstream(&p, &size);
811 ASSERT_NE(EOF, fputs("hello, world!", fp));
814 ASSERT_STREQ("hello, world!", p);
815 ASSERT_EQ(strlen("hello, world!"), size);
819 TEST(STDIO_TEST, open_memstream_EINVAL) {
820 #if defined(__BIONIC__)
826 ASSERT_EQ(nullptr, open_memstream(nullptr, &size));
827 ASSERT_EQ(EINVAL, errno);
831 ASSERT_EQ(nullptr, open_memstream(&p, nullptr));
832 ASSERT_EQ(EINVAL, errno);
834 GTEST_LOG_(INFO) << "This test does nothing.\n";
838 TEST(STDIO_TEST, fdopen_CLOEXEC) {
839 int fd = open("/proc/version", O_RDONLY);
840 ASSERT_TRUE(fd != -1);
842 // This fd doesn't have O_CLOEXEC...
843 int flags = fcntl(fd, F_GETFD);
844 ASSERT_TRUE(flags != -1);
845 ASSERT_EQ(0, flags & FD_CLOEXEC);
847 FILE* fp = fdopen(fd, "re");
848 ASSERT_TRUE(fp != NULL);
850 // ...but the new one does.
851 flags = fcntl(fileno(fp), F_GETFD);
852 ASSERT_TRUE(flags != -1);
853 ASSERT_EQ(FD_CLOEXEC, flags & FD_CLOEXEC);
859 TEST(STDIO_TEST, freopen_CLOEXEC) {
860 FILE* fp = fopen("/proc/version", "r");
861 ASSERT_TRUE(fp != NULL);
863 // This FILE* doesn't have O_CLOEXEC...
864 int flags = fcntl(fileno(fp), F_GETFD);
865 ASSERT_TRUE(flags != -1);
866 ASSERT_EQ(0, flags & FD_CLOEXEC);
868 fp = freopen("/proc/version", "re", fp);
870 // ...but the new one does.
871 flags = fcntl(fileno(fp), F_GETFD);
872 ASSERT_TRUE(flags != -1);
873 ASSERT_EQ(FD_CLOEXEC, flags & FD_CLOEXEC);
878 // https://code.google.com/p/android/issues/detail?id=81155
880 TEST(STDIO_TEST, fread_unbuffered_pathological_performance) {
881 FILE* fp = fopen("/dev/zero", "r");
882 ASSERT_TRUE(fp != NULL);
884 // Make this stream unbuffered.
885 setvbuf(fp, 0, _IONBF, 0);
888 memset(buf, 0xff, sizeof(buf));
890 time_t t0 = time(NULL);
891 for (size_t i = 0; i < 1024; ++i) {
892 ASSERT_EQ(1U, fread(buf, 64*1024, 1, fp));
894 time_t t1 = time(NULL);
898 // 1024 64KiB reads should have been very quick.
899 ASSERT_LE(t1 - t0, 1);
901 for (size_t i = 0; i < 64*1024; ++i) {
902 ASSERT_EQ('\0', buf[i]);
904 for (size_t i = 64*1024; i < 65*1024; ++i) {
905 ASSERT_EQ('\xff', buf[i]);
909 TEST(STDIO_TEST, fread_EOF) {
910 std::string digits("0123456789");
911 FILE* fp = fmemopen(&digits[0], digits.size(), "r");
913 // Try to read too much, but little enough that it still fits in the FILE's internal buffer.
915 memset(buf1, 0, sizeof(buf1));
916 ASSERT_EQ(2U, fread(buf1, 4, 4, fp));
917 ASSERT_STREQ("0123456789", buf1);
918 ASSERT_TRUE(feof(fp));
922 // Try to read way too much so stdio tries to read more direct from the stream.
924 memset(buf2, 0, sizeof(buf2));
925 ASSERT_EQ(2U, fread(buf2, 4, 4096, fp));
926 ASSERT_STREQ("0123456789", buf2);
927 ASSERT_TRUE(feof(fp));
932 static void test_fread_from_write_only_stream(size_t n) {
933 FILE* fp = fopen("/dev/null", "w");
934 std::vector<char> buf(n, 0);
936 ASSERT_EQ(0U, fread(&buf[0], n, 1, fp));
937 ASSERT_EQ(EBADF, errno);
938 ASSERT_TRUE(ferror(fp));
939 ASSERT_FALSE(feof(fp));
943 TEST(STDIO_TEST, fread_from_write_only_stream_slow_path) {
944 test_fread_from_write_only_stream(1);
947 TEST(STDIO_TEST, fread_from_write_only_stream_fast_path) {
948 test_fread_from_write_only_stream(64*1024);
951 static void test_fwrite_after_fread(size_t n) {
954 FILE* fp = fdopen(tf.fd, "w+");
955 ASSERT_EQ(1U, fwrite("1", 1, 1, fp));
958 // We've flushed but not rewound, so there's nothing to read.
959 std::vector<char> buf(n, 0);
960 ASSERT_EQ(0U, fread(&buf[0], 1, buf.size(), fp));
961 ASSERT_TRUE(feof(fp));
963 // But hitting EOF doesn't prevent us from writing...
965 ASSERT_EQ(1U, fwrite("2", 1, 1, fp)) << errno;
967 // And if we rewind, everything's there.
969 ASSERT_EQ(2U, fread(&buf[0], 1, buf.size(), fp));
970 ASSERT_EQ('1', buf[0]);
971 ASSERT_EQ('2', buf[1]);
976 TEST(STDIO_TEST, fwrite_after_fread_slow_path) {
977 test_fwrite_after_fread(16);
980 TEST(STDIO_TEST, fwrite_after_fread_fast_path) {
981 test_fwrite_after_fread(64*1024);
985 TEST(STDIO_TEST, fread_after_fseek) {
988 FILE* fp = fopen(tf.filename, "w+");
989 ASSERT_TRUE(fp != nullptr);
991 char file_data[12288];
992 for (size_t i = 0; i < 12288; i++) {
995 ASSERT_EQ(12288U, fwrite(file_data, 1, 12288, fp));
998 fp = fopen(tf.filename, "r");
999 ASSERT_TRUE(fp != nullptr);
1002 size_t cur_location = 0;
1003 // Small read to populate internal buffer.
1004 ASSERT_EQ(100U, fread(buffer, 1, 100, fp));
1005 ASSERT_EQ(memcmp(file_data, buffer, 100), 0);
1007 cur_location = static_cast<size_t>(ftell(fp));
1008 // Large read to force reading into the user supplied buffer and bypassing
1009 // the internal buffer.
1010 ASSERT_EQ(8192U, fread(buffer, 1, 8192, fp));
1011 ASSERT_EQ(memcmp(file_data+cur_location, buffer, 8192), 0);
1013 // Small backwards seek to verify fseek does not reuse the internal buffer.
1014 ASSERT_EQ(0, fseek(fp, -22, SEEK_CUR));
1015 cur_location = static_cast<size_t>(ftell(fp));
1016 ASSERT_EQ(22U, fread(buffer, 1, 22, fp));
1017 ASSERT_EQ(memcmp(file_data+cur_location, buffer, 22), 0);
1022 // https://code.google.com/p/android/issues/detail?id=184847
1023 TEST(STDIO_TEST, fread_EOF_184847) {
1027 FILE* fw = fopen(tf.filename, "w");
1028 ASSERT_TRUE(fw != nullptr);
1030 FILE* fr = fopen(tf.filename, "r");
1031 ASSERT_TRUE(fr != nullptr);
1033 fwrite("a", 1, 1, fw);
1035 ASSERT_EQ(1U, fread(buf, 1, 1, fr));
1036 ASSERT_STREQ("a", buf);
1038 // 'fr' is now at EOF.
1039 ASSERT_EQ(0U, fread(buf, 1, 1, fr));
1040 ASSERT_TRUE(feof(fr));
1042 // Write some more...
1043 fwrite("z", 1, 1, fw);
1046 // ...and check that we can read it back.
1047 // (BSD thinks that once a stream has hit EOF, it must always return EOF. SysV disagrees.)
1048 ASSERT_EQ(1U, fread(buf, 1, 1, fr));
1049 ASSERT_STREQ("z", buf);
1051 // But now we're done.
1052 ASSERT_EQ(0U, fread(buf, 1, 1, fr));
1058 TEST(STDIO_TEST, fclose_invalidates_fd) {
1059 // The typical error we're trying to help people catch involves accessing
1060 // memory after it's been freed. But we know that stdin/stdout/stderr are
1061 // special and don't get deallocated, so this test uses stdin.
1062 ASSERT_EQ(0, fclose(stdin));
1064 // Even though using a FILE* after close is undefined behavior, I've closed
1065 // this bug as "WAI" too many times. We shouldn't hand out stale fds,
1066 // especially because they might actually correspond to a real stream.
1068 ASSERT_EQ(-1, fileno(stdin));
1069 ASSERT_EQ(EBADF, errno);
1072 TEST(STDIO_TEST, fseek_ftell_unseekable) {
1073 #if defined(__BIONIC__) // glibc has fopencookie instead.
1074 auto read_fn = [](void*, char*, int) { return -1; };
1075 FILE* fp = funopen(nullptr, read_fn, nullptr, nullptr, nullptr);
1076 ASSERT_TRUE(fp != nullptr);
1078 // Check that ftell balks on an unseekable FILE*.
1080 ASSERT_EQ(-1, ftell(fp));
1081 ASSERT_EQ(ESPIPE, errno);
1083 // SEEK_CUR is rewritten as SEEK_SET internally...
1085 ASSERT_EQ(-1, fseek(fp, 0, SEEK_CUR));
1086 ASSERT_EQ(ESPIPE, errno);
1088 // ...so it's worth testing the direct seek path too.
1090 ASSERT_EQ(-1, fseek(fp, 0, SEEK_SET));
1091 ASSERT_EQ(ESPIPE, errno);