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 TEST(stdio, flockfile_18208568_stderr) {
35 // Check that we have a _recursive_ mutex for flockfile.
37 feof(stderr); // We don't care about the result, but this needs to take the lock.
41 TEST(stdio, flockfile_18208568_regular) {
42 // We never had a bug for streams other than stdin/stdout/stderr, but test anyway.
43 FILE* fp = fopen("/dev/null", "w");
44 ASSERT_TRUE(fp != NULL);
51 TEST(stdio, tmpfile_fileno_fprintf_rewind_fgets) {
53 ASSERT_TRUE(fp != NULL);
59 int rc = fstat(fd, &sb);
61 ASSERT_EQ(sb.st_mode & 0777, 0600U);
63 rc = fprintf(fp, "hello\n");
69 char* s = fgets(buf, sizeof(buf), fp);
70 ASSERT_TRUE(s != NULL);
71 ASSERT_STREQ("hello\n", s);
76 TEST(stdio, dprintf) {
79 int rc = dprintf(tf.fd, "hello\n");
82 lseek(tf.fd, 0, SEEK_SET);
83 FILE* tfile = fdopen(tf.fd, "r");
84 ASSERT_TRUE(tfile != NULL);
87 ASSERT_EQ(buf, fgets(buf, sizeof(buf), tfile));
88 ASSERT_STREQ("hello\n", buf);
89 // Make sure there isn't anything else in the file.
90 ASSERT_EQ(NULL, fgets(buf, sizeof(buf), tfile));
94 TEST(stdio, getdelim) {
96 ASSERT_TRUE(fp != NULL);
98 const char* line_written = "This is a test";
99 int rc = fprintf(fp, "%s", line_written);
100 ASSERT_EQ(rc, static_cast<int>(strlen(line_written)));
104 char* word_read = NULL;
105 size_t allocated_length = 0;
107 const char* expected[] = { "This ", " ", "is ", "a ", "test" };
108 for (size_t i = 0; i < 5; ++i) {
109 ASSERT_FALSE(feof(fp));
110 ASSERT_EQ(getdelim(&word_read, &allocated_length, ' ', fp), static_cast<int>(strlen(expected[i])));
111 ASSERT_GE(allocated_length, strlen(expected[i]));
112 ASSERT_STREQ(expected[i], word_read);
114 // The last read should have set the end-of-file indicator for the stream.
115 ASSERT_TRUE(feof(fp));
118 // getdelim returns -1 but doesn't set errno if we're already at EOF.
119 // It should set the end-of-file indicator for the stream, though.
121 ASSERT_EQ(getdelim(&word_read, &allocated_length, ' ', fp), -1);
123 ASSERT_TRUE(feof(fp));
129 TEST(stdio, getdelim_invalid) {
130 FILE* fp = tmpfile();
131 ASSERT_TRUE(fp != NULL);
134 size_t buffer_length = 0;
136 // The first argument can't be NULL.
138 ASSERT_EQ(getdelim(NULL, &buffer_length, ' ', fp), -1);
139 ASSERT_EQ(EINVAL, errno);
141 // The second argument can't be NULL.
143 ASSERT_EQ(getdelim(&buffer, NULL, ' ', fp), -1);
144 ASSERT_EQ(EINVAL, errno);
146 // The underlying fd can't be closed.
147 ASSERT_EQ(0, close(fileno(fp)));
149 ASSERT_EQ(getdelim(&buffer, &buffer_length, ' ', fp), -1);
150 ASSERT_EQ(EBADF, errno);
154 TEST(stdio, getdelim_directory) {
155 FILE* fp = fopen("/proc", "r");
156 ASSERT_TRUE(fp != NULL);
158 size_t allocated_length;
159 ASSERT_EQ(-1, getdelim(&word_read, &allocated_length, ' ', fp));
163 TEST(stdio, getline) {
164 FILE* fp = tmpfile();
165 ASSERT_TRUE(fp != NULL);
167 const char* line_written = "This is a test for getline\n";
168 const size_t line_count = 5;
170 for (size_t i = 0; i < line_count; ++i) {
171 int rc = fprintf(fp, "%s", line_written);
172 ASSERT_EQ(rc, static_cast<int>(strlen(line_written)));
177 char* line_read = NULL;
178 size_t allocated_length = 0;
180 size_t read_line_count = 0;
181 ssize_t read_char_count;
182 while ((read_char_count = getline(&line_read, &allocated_length, fp)) != -1) {
183 ASSERT_EQ(read_char_count, static_cast<int>(strlen(line_written)));
184 ASSERT_GE(allocated_length, strlen(line_written));
185 ASSERT_STREQ(line_written, line_read);
188 ASSERT_EQ(read_line_count, line_count);
190 // The last read should have set the end-of-file indicator for the stream.
191 ASSERT_TRUE(feof(fp));
194 // getline returns -1 but doesn't set errno if we're already at EOF.
195 // It should set the end-of-file indicator for the stream, though.
197 ASSERT_EQ(getline(&line_read, &allocated_length, fp), -1);
199 ASSERT_TRUE(feof(fp));
205 TEST(stdio, getline_invalid) {
206 FILE* fp = tmpfile();
207 ASSERT_TRUE(fp != NULL);
210 size_t buffer_length = 0;
212 // The first argument can't be NULL.
214 ASSERT_EQ(getline(NULL, &buffer_length, fp), -1);
215 ASSERT_EQ(EINVAL, errno);
217 // The second argument can't be NULL.
219 ASSERT_EQ(getline(&buffer, NULL, fp), -1);
220 ASSERT_EQ(EINVAL, errno);
222 // The underlying fd can't be closed.
223 ASSERT_EQ(0, close(fileno(fp)));
225 ASSERT_EQ(getline(&buffer, &buffer_length, fp), -1);
226 ASSERT_EQ(EBADF, errno);
230 TEST(stdio, printf_ssize_t) {
232 ASSERT_EQ(sizeof(ssize_t), sizeof(long int));
233 ASSERT_EQ(sizeof(ssize_t), sizeof(size_t));
234 // For our 32-bit ABI, we had a ssize_t definition that confuses GCC into saying:
235 // error: format '%zd' expects argument of type 'signed size_t',
236 // but argument 4 has type 'ssize_t {aka long int}' [-Werror=format]
239 snprintf(buf, sizeof(buf), "%zd", v);
242 // https://code.google.com/p/android/issues/detail?id=64886
243 TEST(stdio, snprintf_a) {
245 EXPECT_EQ(23, snprintf(buf, sizeof(buf), "<%a>", 9990.235));
246 EXPECT_STREQ("<0x1.3831e147ae148p+13>", buf);
249 TEST(stdio, snprintf_lc) {
252 EXPECT_EQ(3, snprintf(buf, sizeof(buf), "<%lc>", wc));
253 EXPECT_STREQ("<a>", buf);
256 TEST(stdio, snprintf_ls) {
259 EXPECT_EQ(8, snprintf(buf, sizeof(buf), "<%ls>", ws));
260 EXPECT_STREQ("<(null)>", buf);
262 wchar_t chars[] = { L'h', L'i', 0 };
264 EXPECT_EQ(4, snprintf(buf, sizeof(buf), "<%ls>", ws));
265 EXPECT_STREQ("<hi>", buf);
268 TEST(stdio, snprintf_n) {
269 #if defined(__BIONIC__)
273 EXPECT_EQ(5, snprintf(buf, sizeof(buf), "a %n b", &i));
275 EXPECT_STREQ("a n b", buf);
277 GTEST_LOG_(INFO) << "This test does nothing.\n";
281 TEST(stdio, snprintf_smoke) {
284 snprintf(buf, sizeof(buf), "a");
285 EXPECT_STREQ("a", buf);
287 snprintf(buf, sizeof(buf), "%%");
288 EXPECT_STREQ("%", buf);
290 snprintf(buf, sizeof(buf), "01234");
291 EXPECT_STREQ("01234", buf);
293 snprintf(buf, sizeof(buf), "a%sb", "01234");
294 EXPECT_STREQ("a01234b", buf);
297 snprintf(buf, sizeof(buf), "a%sb", s);
298 EXPECT_STREQ("a(null)b", buf);
300 snprintf(buf, sizeof(buf), "aa%scc", "bb");
301 EXPECT_STREQ("aabbcc", buf);
303 snprintf(buf, sizeof(buf), "a%cc", 'b');
304 EXPECT_STREQ("abc", buf);
306 snprintf(buf, sizeof(buf), "a%db", 1234);
307 EXPECT_STREQ("a1234b", buf);
309 snprintf(buf, sizeof(buf), "a%db", -8123);
310 EXPECT_STREQ("a-8123b", buf);
312 snprintf(buf, sizeof(buf), "a%hdb", static_cast<short>(0x7fff0010));
313 EXPECT_STREQ("a16b", buf);
315 snprintf(buf, sizeof(buf), "a%hhdb", static_cast<char>(0x7fffff10));
316 EXPECT_STREQ("a16b", buf);
318 snprintf(buf, sizeof(buf), "a%lldb", 0x1000000000LL);
319 EXPECT_STREQ("a68719476736b", buf);
321 snprintf(buf, sizeof(buf), "a%ldb", 70000L);
322 EXPECT_STREQ("a70000b", buf);
324 snprintf(buf, sizeof(buf), "a%pb", reinterpret_cast<void*>(0xb0001234));
325 EXPECT_STREQ("a0xb0001234b", buf);
327 snprintf(buf, sizeof(buf), "a%xz", 0x12ab);
328 EXPECT_STREQ("a12abz", buf);
330 snprintf(buf, sizeof(buf), "a%Xz", 0x12ab);
331 EXPECT_STREQ("a12ABz", buf);
333 snprintf(buf, sizeof(buf), "a%08xz", 0x123456);
334 EXPECT_STREQ("a00123456z", buf);
336 snprintf(buf, sizeof(buf), "a%5dz", 1234);
337 EXPECT_STREQ("a 1234z", buf);
339 snprintf(buf, sizeof(buf), "a%05dz", 1234);
340 EXPECT_STREQ("a01234z", buf);
342 snprintf(buf, sizeof(buf), "a%8dz", 1234);
343 EXPECT_STREQ("a 1234z", buf);
345 snprintf(buf, sizeof(buf), "a%-8dz", 1234);
346 EXPECT_STREQ("a1234 z", buf);
348 snprintf(buf, sizeof(buf), "A%-11sZ", "abcdef");
349 EXPECT_STREQ("Aabcdef Z", buf);
351 snprintf(buf, sizeof(buf), "A%s:%dZ", "hello", 1234);
352 EXPECT_STREQ("Ahello:1234Z", buf);
354 snprintf(buf, sizeof(buf), "a%03d:%d:%02dz", 5, 5, 5);
355 EXPECT_STREQ("a005:5:05z", buf);
358 snprintf(buf, sizeof(buf), "a%d,%pz", 5, p);
359 #if defined(__BIONIC__)
360 EXPECT_STREQ("a5,0x0z", buf);
362 EXPECT_STREQ("a5,(nil)z", buf);
365 snprintf(buf, sizeof(buf), "a%lld,%d,%d,%dz", 0x1000000000LL, 6, 7, 8);
366 EXPECT_STREQ("a68719476736,6,7,8z", buf);
368 snprintf(buf, sizeof(buf), "a_%f_b", 1.23f);
369 EXPECT_STREQ("a_1.230000_b", buf);
371 snprintf(buf, sizeof(buf), "a_%g_b", 3.14);
372 EXPECT_STREQ("a_3.14_b", buf);
374 snprintf(buf, sizeof(buf), "%1$s %1$s", "print_me_twice");
375 EXPECT_STREQ("print_me_twice print_me_twice", buf);
378 template <typename T>
379 void CheckInfNan(int snprintf_fn(T*, size_t, const T*, ...),
380 const T* fmt, const T* fmt_plus,
381 const T* minus_inf, const T* inf_, const T* plus_inf,
382 const T* minus_nan, const T* nan_, const T* plus_nan) {
385 snprintf_fn(buf, sizeof(buf), fmt, nan(""));
386 EXPECT_STREQ(nan_, buf) << fmt;
387 snprintf_fn(buf, sizeof(buf), fmt, -nan(""));
388 EXPECT_STREQ(minus_nan, buf) << fmt;
389 snprintf_fn(buf, sizeof(buf), fmt_plus, nan(""));
390 EXPECT_STREQ(plus_nan, buf) << fmt_plus;
391 snprintf_fn(buf, sizeof(buf), fmt_plus, -nan(""));
392 EXPECT_STREQ(minus_nan, buf) << fmt_plus;
394 snprintf_fn(buf, sizeof(buf), fmt, HUGE_VAL);
395 EXPECT_STREQ(inf_, buf) << fmt;
396 snprintf_fn(buf, sizeof(buf), fmt, -HUGE_VAL);
397 EXPECT_STREQ(minus_inf, buf) << fmt;
398 snprintf_fn(buf, sizeof(buf), fmt_plus, HUGE_VAL);
399 EXPECT_STREQ(plus_inf, buf) << fmt_plus;
400 snprintf_fn(buf, sizeof(buf), fmt_plus, -HUGE_VAL);
401 EXPECT_STREQ(minus_inf, buf) << fmt_plus;
404 TEST(stdio, snprintf_inf_nan) {
405 CheckInfNan(snprintf, "%a", "%+a", "-inf", "inf", "+inf", "-nan", "nan", "+nan");
406 CheckInfNan(snprintf, "%A", "%+A", "-INF", "INF", "+INF", "-NAN", "NAN", "+NAN");
407 CheckInfNan(snprintf, "%e", "%+e", "-inf", "inf", "+inf", "-nan", "nan", "+nan");
408 CheckInfNan(snprintf, "%E", "%+E", "-INF", "INF", "+INF", "-NAN", "NAN", "+NAN");
409 CheckInfNan(snprintf, "%f", "%+f", "-inf", "inf", "+inf", "-nan", "nan", "+nan");
410 CheckInfNan(snprintf, "%F", "%+F", "-INF", "INF", "+INF", "-NAN", "NAN", "+NAN");
411 CheckInfNan(snprintf, "%g", "%+g", "-inf", "inf", "+inf", "-nan", "nan", "+nan");
412 CheckInfNan(snprintf, "%G", "%+G", "-INF", "INF", "+INF", "-NAN", "NAN", "+NAN");
415 TEST(stdio, wsprintf_inf_nan) {
416 CheckInfNan(swprintf, L"%a", L"%+a", L"-inf", L"inf", L"+inf", L"-nan", L"nan", L"+nan");
417 CheckInfNan(swprintf, L"%A", L"%+A", L"-INF", L"INF", L"+INF", L"-NAN", L"NAN", L"+NAN");
418 CheckInfNan(swprintf, L"%e", L"%+e", L"-inf", L"inf", L"+inf", L"-nan", L"nan", L"+nan");
419 CheckInfNan(swprintf, L"%E", L"%+E", L"-INF", L"INF", L"+INF", L"-NAN", L"NAN", L"+NAN");
420 CheckInfNan(swprintf, L"%f", L"%+f", L"-inf", L"inf", L"+inf", L"-nan", L"nan", L"+nan");
421 CheckInfNan(swprintf, L"%F", L"%+F", L"-INF", L"INF", L"+INF", L"-NAN", L"NAN", L"+NAN");
422 CheckInfNan(swprintf, L"%g", L"%+g", L"-inf", L"inf", L"+inf", L"-nan", L"nan", L"+nan");
423 CheckInfNan(swprintf, L"%G", L"%+G", L"-INF", L"INF", L"+INF", L"-NAN", L"NAN", L"+NAN");
426 TEST(stdio, snprintf_d_INT_MAX) {
428 snprintf(buf, sizeof(buf), "%d", INT_MAX);
429 EXPECT_STREQ("2147483647", buf);
432 TEST(stdio, snprintf_d_INT_MIN) {
434 snprintf(buf, sizeof(buf), "%d", INT_MIN);
435 EXPECT_STREQ("-2147483648", buf);
438 TEST(stdio, snprintf_ld_LONG_MAX) {
440 snprintf(buf, sizeof(buf), "%ld", LONG_MAX);
442 EXPECT_STREQ("9223372036854775807", buf);
444 EXPECT_STREQ("2147483647", buf);
448 TEST(stdio, snprintf_ld_LONG_MIN) {
450 snprintf(buf, sizeof(buf), "%ld", LONG_MIN);
452 EXPECT_STREQ("-9223372036854775808", buf);
454 EXPECT_STREQ("-2147483648", buf);
458 TEST(stdio, snprintf_lld_LLONG_MAX) {
460 snprintf(buf, sizeof(buf), "%lld", LLONG_MAX);
461 EXPECT_STREQ("9223372036854775807", buf);
464 TEST(stdio, snprintf_lld_LLONG_MIN) {
466 snprintf(buf, sizeof(buf), "%lld", LLONG_MIN);
467 EXPECT_STREQ("-9223372036854775808", buf);
470 TEST(stdio, snprintf_e) {
473 snprintf(buf, sizeof(buf), "%e", 1.5);
474 EXPECT_STREQ("1.500000e+00", buf);
476 snprintf(buf, sizeof(buf), "%Le", 1.5l);
477 EXPECT_STREQ("1.500000e+00", buf);
480 TEST(stdio, snprintf_negative_zero_5084292) {
483 snprintf(buf, sizeof(buf), "%e", -0.0);
484 EXPECT_STREQ("-0.000000e+00", buf);
485 snprintf(buf, sizeof(buf), "%E", -0.0);
486 EXPECT_STREQ("-0.000000E+00", buf);
487 snprintf(buf, sizeof(buf), "%f", -0.0);
488 EXPECT_STREQ("-0.000000", buf);
489 snprintf(buf, sizeof(buf), "%F", -0.0);
490 EXPECT_STREQ("-0.000000", buf);
491 snprintf(buf, sizeof(buf), "%g", -0.0);
492 EXPECT_STREQ("-0", buf);
493 snprintf(buf, sizeof(buf), "%G", -0.0);
494 EXPECT_STREQ("-0", buf);
495 snprintf(buf, sizeof(buf), "%a", -0.0);
496 EXPECT_STREQ("-0x0p+0", buf);
497 snprintf(buf, sizeof(buf), "%A", -0.0);
498 EXPECT_STREQ("-0X0P+0", buf);
501 TEST(stdio, snprintf_utf8_15439554) {
502 locale_t cloc = newlocale(LC_ALL, "C.UTF-8", 0);
503 locale_t old_locale = uselocale(cloc);
509 snprintf(buf, sizeof(buf), "%dx%d", 1, 2);
510 EXPECT_STREQ("1x2", buf);
512 snprintf(buf, sizeof(buf), "%d\xc2\xa2%d", 1, 2);
513 EXPECT_STREQ("1¢2", buf);
515 snprintf(buf, sizeof(buf), "%d\xe2\x82\xac%d", 1, 2);
516 EXPECT_STREQ("1€2", buf);
518 snprintf(buf, sizeof(buf), "%d\xf0\xa4\xad\xa2%d", 1, 2);
519 EXPECT_STREQ("1ð¤¢2", buf);
521 uselocale(old_locale);
525 TEST(stdio, fprintf_failures_7229520) {
529 // Unbuffered case where the fprintf(3) itself fails.
530 ASSERT_NE(nullptr, fp = tmpfile());
532 ASSERT_EQ(4, fprintf(fp, "epic"));
533 ASSERT_EQ(0, close(fileno(fp)));
534 ASSERT_EQ(-1, fprintf(fp, "fail"));
535 ASSERT_EQ(-1, fclose(fp));
537 // Buffered case where we won't notice until the fclose(3).
538 // It's likely this is what was actually seen in http://b/7229520,
539 // and that expecting fprintf to fail is setting yourself up for
540 // disappointment. Remember to check fclose(3)'s return value, kids!
541 ASSERT_NE(nullptr, fp = tmpfile());
542 ASSERT_EQ(4, fprintf(fp, "epic"));
543 ASSERT_EQ(0, close(fileno(fp)));
544 ASSERT_EQ(4, fprintf(fp, "fail"));
545 ASSERT_EQ(-1, fclose(fp));
549 FILE* fp = popen("cat /proc/version", "r");
550 ASSERT_TRUE(fp != NULL);
553 char* s = fgets(buf, sizeof(buf), fp);
555 ASSERT_STREQ("Linux version", s);
557 ASSERT_EQ(0, pclose(fp));
561 FILE* fp = fopen("/proc/version", "r");
562 ASSERT_TRUE(fp != NULL);
563 ASSERT_EQ('L', getc(fp));
564 ASSERT_EQ('i', getc(fp));
565 ASSERT_EQ('n', getc(fp));
566 ASSERT_EQ('u', getc(fp));
567 ASSERT_EQ('x', getc(fp));
572 FILE* fp = fopen("/proc/version", "r");
573 ASSERT_TRUE(fp != NULL);
574 ASSERT_EQ(EOF, putc('x', fp));
578 TEST(stdio, sscanf) {
583 ASSERT_EQ(3, sscanf(" hello 123 1.23 ", "%s %i %lf %s", s1, &i1, &d1, s2));
584 ASSERT_STREQ("hello", s1);
586 ASSERT_DOUBLE_EQ(1.23, d1);
589 TEST(stdio, cantwrite_EBADF) {
590 // If we open a file read-only...
591 FILE* fp = fopen("/proc/version", "r");
593 // ...all attempts to write to that file should return failure.
595 // They should also set errno to EBADF. This isn't POSIX, but it's traditional.
596 // glibc gets the wide-character functions wrong.
599 EXPECT_EQ(EOF, putc('x', fp));
600 EXPECT_EQ(EBADF, errno);
603 EXPECT_EQ(EOF, fprintf(fp, "hello"));
604 EXPECT_EQ(EBADF, errno);
607 EXPECT_EQ(EOF, fwprintf(fp, L"hello"));
608 #if defined(__BIONIC__)
609 EXPECT_EQ(EBADF, errno);
613 EXPECT_EQ(0U, fwrite("hello", 1, 2, fp));
614 EXPECT_EQ(EBADF, errno);
617 EXPECT_EQ(EOF, fputs("hello", fp));
618 EXPECT_EQ(EBADF, errno);
621 EXPECT_EQ(WEOF, fputwc(L'x', fp));
622 #if defined(__BIONIC__)
623 EXPECT_EQ(EBADF, errno);
627 // Tests that we can only have a consistent and correct fpos_t when using
628 // f*pos functions (i.e. fpos doesn't get inside a multi byte character).
629 TEST(stdio, consistent_fpos_t) {
630 ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
631 uselocale(LC_GLOBAL_LOCALE);
633 FILE* fp = tmpfile();
634 ASSERT_TRUE(fp != NULL);
636 wchar_t mb_one_bytes = L'h';
637 wchar_t mb_two_bytes = 0x00a2;
638 wchar_t mb_three_bytes = 0x20ac;
639 wchar_t mb_four_bytes = 0x24b62;
642 ASSERT_EQ(mb_one_bytes, static_cast<wchar_t>(fputwc(mb_one_bytes, fp)));
643 ASSERT_EQ(mb_two_bytes, static_cast<wchar_t>(fputwc(mb_two_bytes, fp)));
644 ASSERT_EQ(mb_three_bytes, static_cast<wchar_t>(fputwc(mb_three_bytes, fp)));
645 ASSERT_EQ(mb_four_bytes, static_cast<wchar_t>(fputwc(mb_four_bytes, fp)));
649 // Record each character position.
655 EXPECT_EQ(0, fgetpos(fp, &pos1));
656 ASSERT_EQ(mb_one_bytes, static_cast<wchar_t>(fgetwc(fp)));
657 EXPECT_EQ(0, fgetpos(fp, &pos2));
658 ASSERT_EQ(mb_two_bytes, static_cast<wchar_t>(fgetwc(fp)));
659 EXPECT_EQ(0, fgetpos(fp, &pos3));
660 ASSERT_EQ(mb_three_bytes, static_cast<wchar_t>(fgetwc(fp)));
661 EXPECT_EQ(0, fgetpos(fp, &pos4));
662 ASSERT_EQ(mb_four_bytes, static_cast<wchar_t>(fgetwc(fp)));
663 EXPECT_EQ(0, fgetpos(fp, &pos5));
665 #if defined(__BIONIC__)
666 // Bionic's fpos_t is just an alias for off_t. This is inherited from OpenBSD
667 // upstream. Glibc differs by storing the mbstate_t inside its fpos_t. In
668 // Bionic (and upstream OpenBSD) the mbstate_t is stored inside the FILE
670 ASSERT_EQ(0, static_cast<off_t>(pos1));
671 ASSERT_EQ(1, static_cast<off_t>(pos2));
672 ASSERT_EQ(3, static_cast<off_t>(pos3));
673 ASSERT_EQ(6, static_cast<off_t>(pos4));
674 ASSERT_EQ(10, static_cast<off_t>(pos5));
677 // Exercise back and forth movements of the position.
678 ASSERT_EQ(0, fsetpos(fp, &pos2));
679 ASSERT_EQ(mb_two_bytes, static_cast<wchar_t>(fgetwc(fp)));
680 ASSERT_EQ(0, fsetpos(fp, &pos1));
681 ASSERT_EQ(mb_one_bytes, static_cast<wchar_t>(fgetwc(fp)));
682 ASSERT_EQ(0, fsetpos(fp, &pos4));
683 ASSERT_EQ(mb_four_bytes, static_cast<wchar_t>(fgetwc(fp)));
684 ASSERT_EQ(0, fsetpos(fp, &pos3));
685 ASSERT_EQ(mb_three_bytes, static_cast<wchar_t>(fgetwc(fp)));
686 ASSERT_EQ(0, fsetpos(fp, &pos5));
687 ASSERT_EQ(WEOF, fgetwc(fp));
692 // Exercise the interaction between fpos and seek.
693 TEST(stdio, fpos_t_and_seek) {
694 ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
695 uselocale(LC_GLOBAL_LOCALE);
697 // In glibc-2.16 fseek doesn't work properly in wide mode
698 // (https://sourceware.org/bugzilla/show_bug.cgi?id=14543). One workaround is
699 // to close and re-open the file. We do it in order to make the test pass
703 FILE* fp = fdopen(tf.fd, "w+");
704 ASSERT_TRUE(fp != NULL);
706 wchar_t mb_two_bytes = 0x00a2;
707 wchar_t mb_three_bytes = 0x20ac;
708 wchar_t mb_four_bytes = 0x24b62;
711 ASSERT_EQ(mb_two_bytes, static_cast<wchar_t>(fputwc(mb_two_bytes, fp)));
712 ASSERT_EQ(mb_three_bytes, static_cast<wchar_t>(fputwc(mb_three_bytes, fp)));
713 ASSERT_EQ(mb_four_bytes, static_cast<wchar_t>(fputwc(mb_four_bytes, fp)));
718 fp = fopen(tf.filename, "r");
719 ASSERT_TRUE(fp != NULL);
721 // Store a valid position.
722 fpos_t mb_two_bytes_pos;
723 ASSERT_EQ(0, fgetpos(fp, &mb_two_bytes_pos));
725 // Move inside mb_four_bytes with fseek.
726 long offset_inside_mb = 6;
727 ASSERT_EQ(0, fseek(fp, offset_inside_mb, SEEK_SET));
729 // Store the "inside multi byte" position.
730 fpos_t pos_inside_mb;
731 ASSERT_EQ(0, fgetpos(fp, &pos_inside_mb));
732 #if defined(__BIONIC__)
733 ASSERT_EQ(offset_inside_mb, static_cast<off_t>(pos_inside_mb));
736 // Reading from within a byte should produce an error.
737 ASSERT_EQ(WEOF, fgetwc(fp));
738 ASSERT_EQ(EILSEQ, errno);
740 // Reverting to a valid position should work.
741 ASSERT_EQ(0, fsetpos(fp, &mb_two_bytes_pos));
742 ASSERT_EQ(mb_two_bytes, static_cast<wchar_t>(fgetwc(fp)));
744 // Moving withing a multi byte with fsetpos should work but reading should
746 ASSERT_EQ(0, fsetpos(fp, &pos_inside_mb));
747 ASSERT_EQ(WEOF, fgetwc(fp));
748 ASSERT_EQ(EILSEQ, errno);
753 TEST(stdio, fmemopen) {
755 memset(buf, 0, sizeof(buf));
756 FILE* fp = fmemopen(buf, sizeof(buf), "r+");
757 ASSERT_EQ('<', fputc('<', fp));
758 ASSERT_NE(EOF, fputs("abc>\n", fp));
761 ASSERT_STREQ("<abc>\n", buf);
766 char* s = fgets(line, sizeof(line), fp);
767 ASSERT_TRUE(s != NULL);
768 ASSERT_STREQ("<abc>\n", s);
773 TEST(stdio, fmemopen_NULL) {
774 FILE* fp = fmemopen(nullptr, 128, "r+");
775 ASSERT_NE(EOF, fputs("xyz\n", fp));
780 char* s = fgets(line, sizeof(line), fp);
781 ASSERT_TRUE(s != NULL);
782 ASSERT_STREQ("xyz\n", s);
787 TEST(stdio, fmemopen_EINVAL) {
792 ASSERT_EQ(nullptr, fmemopen(buf, 0, "r+"));
793 ASSERT_EQ(EINVAL, errno);
795 // No '+' with NULL buffer.
797 ASSERT_EQ(nullptr, fmemopen(nullptr, 0, "r"));
798 ASSERT_EQ(EINVAL, errno);
801 TEST(stdio, open_memstream) {
804 FILE* fp = open_memstream(&p, &size);
805 ASSERT_NE(EOF, fputs("hello, world!", fp));
808 ASSERT_STREQ("hello, world!", p);
809 ASSERT_EQ(strlen("hello, world!"), size);
813 TEST(stdio, open_memstream_EINVAL) {
814 #if defined(__BIONIC__)
820 ASSERT_EQ(nullptr, open_memstream(nullptr, &size));
821 ASSERT_EQ(EINVAL, errno);
825 ASSERT_EQ(nullptr, open_memstream(&p, nullptr));
826 ASSERT_EQ(EINVAL, errno);
828 GTEST_LOG_(INFO) << "This test does nothing.\n";
832 TEST(stdio, fdopen_CLOEXEC) {
833 int fd = open("/proc/version", O_RDONLY);
834 ASSERT_TRUE(fd != -1);
836 // This fd doesn't have O_CLOEXEC...
837 int flags = fcntl(fd, F_GETFD);
838 ASSERT_TRUE(flags != -1);
839 ASSERT_EQ(0, flags & FD_CLOEXEC);
841 FILE* fp = fdopen(fd, "re");
842 ASSERT_TRUE(fp != NULL);
844 // ...but the new one does.
845 flags = fcntl(fileno(fp), F_GETFD);
846 ASSERT_TRUE(flags != -1);
847 ASSERT_EQ(FD_CLOEXEC, flags & FD_CLOEXEC);
853 TEST(stdio, freopen_CLOEXEC) {
854 FILE* fp = fopen("/proc/version", "r");
855 ASSERT_TRUE(fp != NULL);
857 // This FILE* doesn't have O_CLOEXEC...
858 int flags = fcntl(fileno(fp), F_GETFD);
859 ASSERT_TRUE(flags != -1);
860 ASSERT_EQ(0, flags & FD_CLOEXEC);
862 fp = freopen("/proc/version", "re", fp);
864 // ...but the new one does.
865 flags = fcntl(fileno(fp), F_GETFD);
866 ASSERT_TRUE(flags != -1);
867 ASSERT_EQ(FD_CLOEXEC, flags & FD_CLOEXEC);
872 // https://code.google.com/p/android/issues/detail?id=81155
874 TEST(stdio, fread_unbuffered_pathological_performance) {
875 FILE* fp = fopen("/dev/zero", "r");
876 ASSERT_TRUE(fp != NULL);
878 // Make this stream unbuffered.
879 setvbuf(fp, 0, _IONBF, 0);
882 memset(buf, 0xff, sizeof(buf));
884 time_t t0 = time(NULL);
885 for (size_t i = 0; i < 1024; ++i) {
886 ASSERT_EQ(1U, fread(buf, 64*1024, 1, fp));
888 time_t t1 = time(NULL);
892 // 1024 64KiB reads should have been very quick.
893 ASSERT_LE(t1 - t0, 1);
895 for (size_t i = 0; i < 64*1024; ++i) {
896 ASSERT_EQ('\0', buf[i]);
898 for (size_t i = 64*1024; i < 65*1024; ++i) {
899 ASSERT_EQ('\xff', buf[i]);
903 TEST(stdio, fread_EOF) {
904 std::string digits("0123456789");
905 FILE* fp = fmemopen(&digits[0], digits.size(), "r");
907 // Try to read too much, but little enough that it still fits in the FILE's internal buffer.
909 memset(buf1, 0, sizeof(buf1));
910 ASSERT_EQ(2U, fread(buf1, 4, 4, fp));
911 ASSERT_STREQ("0123456789", buf1);
912 ASSERT_TRUE(feof(fp));
916 // Try to read way too much so stdio tries to read more direct from the stream.
918 memset(buf2, 0, sizeof(buf2));
919 ASSERT_EQ(2U, fread(buf2, 4, 4096, fp));
920 ASSERT_STREQ("0123456789", buf2);
921 ASSERT_TRUE(feof(fp));
926 static void test_fread_from_write_only_stream(size_t n) {
927 FILE* fp = fopen("/dev/null", "w");
928 std::vector<char> buf(n, 0);
930 ASSERT_EQ(0U, fread(&buf[0], n, 1, fp));
931 ASSERT_EQ(EBADF, errno);
932 ASSERT_TRUE(ferror(fp));
933 ASSERT_FALSE(feof(fp));
937 TEST(stdio, fread_from_write_only_stream_slow_path) {
938 test_fread_from_write_only_stream(1);
941 TEST(stdio, fread_from_write_only_stream_fast_path) {
942 test_fread_from_write_only_stream(64*1024);
945 static void test_fwrite_after_fread(size_t n) {
948 FILE* fp = fdopen(tf.fd, "w+");
949 ASSERT_EQ(1U, fwrite("1", 1, 1, fp));
952 // We've flushed but not rewound, so there's nothing to read.
953 std::vector<char> buf(n, 0);
954 ASSERT_EQ(0U, fread(&buf[0], 1, buf.size(), fp));
955 ASSERT_TRUE(feof(fp));
957 // But hitting EOF doesn't prevent us from writing...
959 ASSERT_EQ(1U, fwrite("2", 1, 1, fp)) << errno;
961 // And if we rewind, everything's there.
963 ASSERT_EQ(2U, fread(&buf[0], 1, buf.size(), fp));
964 ASSERT_EQ('1', buf[0]);
965 ASSERT_EQ('2', buf[1]);
970 TEST(stdio, fwrite_after_fread_slow_path) {
971 test_fwrite_after_fread(16);
974 TEST(stdio, fwrite_after_fread_fast_path) {
975 test_fwrite_after_fread(64*1024);
979 TEST(stdio, fread_after_fseek) {
982 FILE* fp = fopen(tf.filename, "w+");
983 ASSERT_TRUE(fp != nullptr);
985 char file_data[12288];
986 for (size_t i = 0; i < 12288; i++) {
989 ASSERT_EQ(12288U, fwrite(file_data, 1, 12288, fp));
992 fp = fopen(tf.filename, "r");
993 ASSERT_TRUE(fp != nullptr);
996 size_t cur_location = 0;
997 // Small read to populate internal buffer.
998 ASSERT_EQ(100U, fread(buffer, 1, 100, fp));
999 ASSERT_EQ(memcmp(file_data, buffer, 100), 0);
1001 cur_location = static_cast<size_t>(ftell(fp));
1002 // Large read to force reading into the user supplied buffer and bypassing
1003 // the internal buffer.
1004 ASSERT_EQ(8192U, fread(buffer, 1, 8192, fp));
1005 ASSERT_EQ(memcmp(file_data+cur_location, buffer, 8192), 0);
1007 // Small backwards seek to verify fseek does not reuse the internal buffer.
1008 ASSERT_EQ(0, fseek(fp, -22, SEEK_CUR));
1009 cur_location = static_cast<size_t>(ftell(fp));
1010 ASSERT_EQ(22U, fread(buffer, 1, 22, fp));
1011 ASSERT_EQ(memcmp(file_data+cur_location, buffer, 22), 0);
1016 // https://code.google.com/p/android/issues/detail?id=184847
1017 TEST(stdio, fread_EOF_184847) {
1021 FILE* fw = fopen(tf.filename, "w");
1022 ASSERT_TRUE(fw != nullptr);
1024 FILE* fr = fopen(tf.filename, "r");
1025 ASSERT_TRUE(fr != nullptr);
1027 fwrite("a", 1, 1, fw);
1029 ASSERT_EQ(1U, fread(buf, 1, 1, fr));
1030 ASSERT_STREQ("a", buf);
1032 // 'fr' is now at EOF.
1033 ASSERT_EQ(0U, fread(buf, 1, 1, fr));
1034 ASSERT_TRUE(feof(fr));
1036 // Write some more...
1037 fwrite("z", 1, 1, fw);
1040 // ...and check that we can read it back.
1041 // (BSD thinks that once a stream has hit EOF, it must always return EOF. SysV disagrees.)
1042 ASSERT_EQ(1U, fread(buf, 1, 1, fr));
1043 ASSERT_STREQ("z", buf);
1045 // But now we're done.
1046 ASSERT_EQ(0U, fread(buf, 1, 1, fr));