| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2009, Google Inc. | |
| 2 // All rights reserved. | |
| 3 // | |
| 4 // Redistribution and use in source and binary forms, with or without | |
| 5 // modification, are permitted provided that the following conditions are | |
| 6 // met: | |
| 7 // | |
| 8 // * Redistributions of source code must retain the above copyright | |
| 9 // notice, this list of conditions and the following disclaimer. | |
| 10 // * Redistributions in binary form must reproduce the above | |
| 11 // copyright notice, this list of conditions and the following disclaimer | |
| 12 // in the documentation and/or other materials provided with the | |
| 13 // distribution. | |
| 14 // * Neither the name of Google Inc. nor the names of its | |
| 15 // contributors may be used to endorse or promote products derived from | |
| 16 // this software without specific prior written permission. | |
| 17 // | |
| 18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
| 19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
| 20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | |
| 21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | |
| 22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
| 23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | |
| 24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
| 25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
| 26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
| 27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
| 28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
| 29 | |
| 30 #include <stdlib.h> | |
| 31 #include <unistd.h> | |
| 32 #include <sys/types.h> | |
| 33 | |
| 34 #include "breakpad/linux/line_reader.h" | |
| 35 #include "testing/gtest/include/gtest/gtest.h" | |
| 36 | |
| 37 using namespace google_breakpad; | |
| 38 | |
| 39 static int TemporaryFile() { | |
| 40 static const char templ[] = "/tmp/line-reader-unittest-XXXXXX"; | |
| 41 char templ_copy[sizeof(templ)]; | |
| 42 memcpy(templ_copy, templ, sizeof(templ)); | |
| 43 const int fd = mkstemp(templ_copy); | |
| 44 if (fd >= 0) | |
| 45 unlink(templ_copy); | |
| 46 | |
| 47 return fd; | |
| 48 } | |
| 49 | |
| 50 namespace { | |
| 51 typedef testing::Test LineReaderTest; | |
| 52 } | |
| 53 | |
| 54 TEST(LineReaderTest, EmptyFile) { | |
| 55 const int fd = TemporaryFile(); | |
| 56 LineReader reader(fd); | |
| 57 | |
| 58 const char *line; | |
| 59 unsigned len; | |
| 60 ASSERT_FALSE(reader.GetNextLine(&line, &len)); | |
| 61 | |
| 62 close(fd); | |
| 63 } | |
| 64 | |
| 65 TEST(LineReaderTest, OneLineTerminated) { | |
| 66 const int fd = TemporaryFile(); | |
| 67 write(fd, "a\n", 2); | |
| 68 lseek(fd, 0, SEEK_SET); | |
| 69 LineReader reader(fd); | |
| 70 | |
| 71 const char *line; | |
| 72 unsigned len; | |
| 73 ASSERT_TRUE(reader.GetNextLine(&line, &len)); | |
| 74 ASSERT_EQ(len, 1); | |
| 75 ASSERT_EQ(line[0], 'a'); | |
| 76 ASSERT_EQ(line[1], 0); | |
| 77 reader.PopLine(len); | |
| 78 | |
| 79 ASSERT_FALSE(reader.GetNextLine(&line, &len)); | |
| 80 | |
| 81 close(fd); | |
| 82 } | |
| 83 | |
| 84 TEST(LineReaderTest, OneLine) { | |
| 85 const int fd = TemporaryFile(); | |
| 86 write(fd, "a", 1); | |
| 87 lseek(fd, 0, SEEK_SET); | |
| 88 LineReader reader(fd); | |
| 89 | |
| 90 const char *line; | |
| 91 unsigned len; | |
| 92 ASSERT_TRUE(reader.GetNextLine(&line, &len)); | |
| 93 ASSERT_EQ(len, 1); | |
| 94 ASSERT_EQ(line[0], 'a'); | |
| 95 ASSERT_EQ(line[1], 0); | |
| 96 reader.PopLine(len); | |
| 97 | |
| 98 ASSERT_FALSE(reader.GetNextLine(&line, &len)); | |
| 99 | |
| 100 close(fd); | |
| 101 } | |
| 102 | |
| 103 TEST(LineReaderTest, TwoLinesTerminated) { | |
| 104 const int fd = TemporaryFile(); | |
| 105 write(fd, "a\nb\n", 4); | |
| 106 lseek(fd, 0, SEEK_SET); | |
| 107 LineReader reader(fd); | |
| 108 | |
| 109 const char *line; | |
| 110 unsigned len; | |
| 111 ASSERT_TRUE(reader.GetNextLine(&line, &len)); | |
| 112 ASSERT_EQ(len, 1); | |
| 113 ASSERT_EQ(line[0], 'a'); | |
| 114 ASSERT_EQ(line[1], 0); | |
| 115 reader.PopLine(len); | |
| 116 | |
| 117 ASSERT_TRUE(reader.GetNextLine(&line, &len)); | |
| 118 ASSERT_EQ(len, 1); | |
| 119 ASSERT_EQ(line[0], 'b'); | |
| 120 ASSERT_EQ(line[1], 0); | |
| 121 reader.PopLine(len); | |
| 122 | |
| 123 ASSERT_FALSE(reader.GetNextLine(&line, &len)); | |
| 124 | |
| 125 close(fd); | |
| 126 } | |
| 127 | |
| 128 TEST(LineReaderTest, TwoLines) { | |
| 129 const int fd = TemporaryFile(); | |
| 130 write(fd, "a\nb", 3); | |
| 131 lseek(fd, 0, SEEK_SET); | |
| 132 LineReader reader(fd); | |
| 133 | |
| 134 const char *line; | |
| 135 unsigned len; | |
| 136 ASSERT_TRUE(reader.GetNextLine(&line, &len)); | |
| 137 ASSERT_EQ(len, 1); | |
| 138 ASSERT_EQ(line[0], 'a'); | |
| 139 ASSERT_EQ(line[1], 0); | |
| 140 reader.PopLine(len); | |
| 141 | |
| 142 ASSERT_TRUE(reader.GetNextLine(&line, &len)); | |
| 143 ASSERT_EQ(len, 1); | |
| 144 ASSERT_EQ(line[0], 'b'); | |
| 145 ASSERT_EQ(line[1], 0); | |
| 146 reader.PopLine(len); | |
| 147 | |
| 148 ASSERT_FALSE(reader.GetNextLine(&line, &len)); | |
| 149 | |
| 150 close(fd); | |
| 151 } | |
| 152 | |
| 153 TEST(LineReaderTest, MaxLength) { | |
| 154 const int fd = TemporaryFile(); | |
| 155 char l[LineReader::kMaxLineLen - 1]; | |
| 156 memset(l, 'a', sizeof(l)); | |
| 157 write(fd, l, sizeof(l)); | |
| 158 lseek(fd, 0, SEEK_SET); | |
| 159 LineReader reader(fd); | |
| 160 | |
| 161 const char *line; | |
| 162 unsigned len; | |
| 163 ASSERT_TRUE(reader.GetNextLine(&line, &len)); | |
| 164 ASSERT_EQ(len, sizeof(l)); | |
| 165 ASSERT_TRUE(memcmp(l, line, sizeof(l)) == 0); | |
| 166 ASSERT_EQ(line[len], 0); | |
| 167 | |
| 168 close(fd); | |
| 169 } | |
| 170 | |
| 171 TEST(LineReaderTest, TooLong) { | |
| 172 const int fd = TemporaryFile(); | |
| 173 char l[LineReader::kMaxLineLen]; | |
| 174 memset(l, 'a', sizeof(l)); | |
| 175 write(fd, l, sizeof(l)); | |
| 176 lseek(fd, 0, SEEK_SET); | |
| 177 LineReader reader(fd); | |
| 178 | |
| 179 const char *line; | |
| 180 unsigned len; | |
| 181 ASSERT_FALSE(reader.GetNextLine(&line, &len)); | |
| 182 | |
| 183 close(fd); | |
| 184 } | |
| OLD | NEW |