diff options
author | Elliott Hughes <enh@google.com> | 2014-05-14 13:31:35 -0700 |
---|---|---|
committer | Elliott Hughes <enh@google.com> | 2014-05-14 13:31:35 -0700 |
commit | c9244bdac1ae254bdbb9687da9cdbb4c3f4cb432 (patch) | |
tree | 73ad53148cadf92c6db6a18d761d04a6c7dc608b /tests/stdio_test.cpp | |
parent | fbd9dcadc8e25b22c99596462669017b9b1b4a7c (diff) |
Add tests for fprintf behavior when the underlying fd is bad.
Bug: 7229520
Change-Id: Ie878e0c13fdcda7b9131fa56208b84ed88125be7
Diffstat (limited to 'tests/stdio_test.cpp')
-rw-r--r-- | tests/stdio_test.cpp | 23 |
1 files changed, 23 insertions, 0 deletions
diff --git a/tests/stdio_test.cpp b/tests/stdio_test.cpp index 2a0549ac6..3194e59e9 100644 --- a/tests/stdio_test.cpp +++ b/tests/stdio_test.cpp @@ -17,6 +17,7 @@ #include <gtest/gtest.h> #include <errno.h> +#include <fcntl.h> #include <limits.h> #include <math.h> #include <stdio.h> @@ -407,6 +408,28 @@ TEST(stdio, snprintf_negative_zero_5084292) { EXPECT_STREQ("-0.000000", buf); } +TEST(stdio, fprintf_failures_7229520) { + FILE* fp; + + // Unbuffered case where the fprintf(3) itself fails. + ASSERT_NE(nullptr, fp = tmpfile()); + setbuf(fp, NULL); + ASSERT_EQ(4, fprintf(fp, "epic")); + ASSERT_EQ(0, close(fileno(fp))); + ASSERT_EQ(-1, fprintf(fp, "fail")); + ASSERT_EQ(-1, fclose(fp)); + + // Buffered case where we won't notice until the fclose(3). + // It's likely this is what was actually seen in http://b/7229520, + // and that expecting fprintf to fail is setting yourself up for + // disappointment. Remember to check fclose(3)'s return value, kids! + ASSERT_NE(nullptr, fp = tmpfile()); + ASSERT_EQ(4, fprintf(fp, "epic")); + ASSERT_EQ(0, close(fileno(fp))); + ASSERT_EQ(4, fprintf(fp, "fail")); + ASSERT_EQ(-1, fclose(fp)); +} + TEST(stdio, popen) { FILE* fp = popen("cat /proc/version", "r"); ASSERT_TRUE(fp != NULL); |