diff options
Diffstat (limited to 'src/client/linux/minidump_writer/line_reader_unittest.cc')
-rw-r--r-- | src/client/linux/minidump_writer/line_reader_unittest.cc | 101 |
1 files changed, 37 insertions, 64 deletions
diff --git a/src/client/linux/minidump_writer/line_reader_unittest.cc b/src/client/linux/minidump_writer/line_reader_unittest.cc index 4448281d..928626e1 100644 --- a/src/client/linux/minidump_writer/line_reader_unittest.cc +++ b/src/client/linux/minidump_writer/line_reader_unittest.cc @@ -34,47 +34,41 @@ #include "client/linux/minidump_writer/line_reader.h" #include "breakpad_googletest_includes.h" #include "common/linux/eintr_wrapper.h" +#include "common/tests/auto_testfile.h" using namespace google_breakpad; -#if !defined(__ANDROID__) -#define TEMPDIR "/tmp" -#else -#define TEMPDIR "/data/local/tmp" -#endif - -static int TemporaryFile() { - static const char templ[] = TEMPDIR "/line-reader-unittest-XXXXXX"; - char templ_copy[sizeof(templ)]; - memcpy(templ_copy, templ, sizeof(templ)); - const int fd = mkstemp(templ_copy); - if (fd >= 0) - unlink(templ_copy); - - return fd; -} - namespace { + typedef testing::Test LineReaderTest; + +class ScopedTestFile : public AutoTestFile { +public: + explicit ScopedTestFile(const char* text) + : AutoTestFile("line_reader", text) { + } + + ScopedTestFile(const char* text, size_t text_len) + : AutoTestFile("line_reader", text, text_len) { + } +}; + } TEST(LineReaderTest, EmptyFile) { - const int fd = TemporaryFile(); - LineReader reader(fd); + ScopedTestFile file(""); + ASSERT_TRUE(file.IsOk()); + LineReader reader(file.GetFd()); const char *line; unsigned len; ASSERT_FALSE(reader.GetNextLine(&line, &len)); - - close(fd); } TEST(LineReaderTest, OneLineTerminated) { - const int fd = TemporaryFile(); - const int r = HANDLE_EINTR(write(fd, "a\n", 2)); - ASSERT_EQ(2, r); - lseek(fd, 0, SEEK_SET); - LineReader reader(fd); + ScopedTestFile file("a\n"); + ASSERT_TRUE(file.IsOk()); + LineReader reader(file.GetFd()); const char *line; unsigned int len; @@ -85,16 +79,12 @@ TEST(LineReaderTest, OneLineTerminated) { reader.PopLine(len); ASSERT_FALSE(reader.GetNextLine(&line, &len)); - - close(fd); } TEST(LineReaderTest, OneLine) { - const int fd = TemporaryFile(); - const int r = HANDLE_EINTR(write(fd, "a", 1)); - ASSERT_EQ(1, r); - lseek(fd, 0, SEEK_SET); - LineReader reader(fd); + ScopedTestFile file("a"); + ASSERT_TRUE(file.IsOk()); + LineReader reader(file.GetFd()); const char *line; unsigned len; @@ -105,16 +95,12 @@ TEST(LineReaderTest, OneLine) { reader.PopLine(len); ASSERT_FALSE(reader.GetNextLine(&line, &len)); - - close(fd); } TEST(LineReaderTest, TwoLinesTerminated) { - const int fd = TemporaryFile(); - const int r = HANDLE_EINTR(write(fd, "a\nb\n", 4)); - ASSERT_EQ(4, r); - lseek(fd, 0, SEEK_SET); - LineReader reader(fd); + ScopedTestFile file("a\nb\n"); + ASSERT_TRUE(file.IsOk()); + LineReader reader(file.GetFd()); const char *line; unsigned len; @@ -131,16 +117,12 @@ TEST(LineReaderTest, TwoLinesTerminated) { reader.PopLine(len); ASSERT_FALSE(reader.GetNextLine(&line, &len)); - - close(fd); } TEST(LineReaderTest, TwoLines) { - const int fd = TemporaryFile(); - const int r = HANDLE_EINTR(write(fd, "a\nb", 3)); - ASSERT_EQ(3, r); - lseek(fd, 0, SEEK_SET); - LineReader reader(fd); + ScopedTestFile file("a\nb"); + ASSERT_TRUE(file.IsOk()); + LineReader reader(file.GetFd()); const char *line; unsigned len; @@ -157,18 +139,14 @@ TEST(LineReaderTest, TwoLines) { reader.PopLine(len); ASSERT_FALSE(reader.GetNextLine(&line, &len)); - - close(fd); } TEST(LineReaderTest, MaxLength) { - const int fd = TemporaryFile(); - char l[LineReader::kMaxLineLen - 1]; + char l[LineReader::kMaxLineLen-1]; memset(l, 'a', sizeof(l)); - const int r = HANDLE_EINTR(write(fd, l, sizeof(l))); - ASSERT_EQ(static_cast<ssize_t>(sizeof(l)), r); - lseek(fd, 0, SEEK_SET); - LineReader reader(fd); + ScopedTestFile file(l, sizeof(l)); + ASSERT_TRUE(file.IsOk()); + LineReader reader(file.GetFd()); const char *line; unsigned len; @@ -176,22 +154,17 @@ TEST(LineReaderTest, MaxLength) { ASSERT_EQ(sizeof(l), len); ASSERT_TRUE(memcmp(l, line, sizeof(l)) == 0); ASSERT_EQ('\0', line[len]); - - close(fd); } TEST(LineReaderTest, TooLong) { - const int fd = TemporaryFile(); + // Note: this writes kMaxLineLen 'a' chars in the test file. char l[LineReader::kMaxLineLen]; memset(l, 'a', sizeof(l)); - const int r = HANDLE_EINTR(write(fd, l, sizeof(l))); - ASSERT_EQ(static_cast<ssize_t>(sizeof(l)), r); - lseek(fd, 0, SEEK_SET); - LineReader reader(fd); + ScopedTestFile file(l, sizeof(l)); + ASSERT_TRUE(file.IsOk()); + LineReader reader(file.GetFd()); const char *line; unsigned len; ASSERT_FALSE(reader.GetNextLine(&line, &len)); - - close(fd); } |