PageRenderTime 36ms CodeModel.GetById 2ms app.highlight 29ms RepoModel.GetById 1ms app.codeStats 0ms

/thirdparty/breakpad/client/linux/minidump_writer/line_reader_unittest.cc

http://github.com/tomahawk-player/tomahawk
C++ | 197 lines | 135 code | 34 blank | 28 comment | 2 complexity | f427cfc11f02f27581344e20d88aa310 MD5 | raw file
  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 "client/linux/minidump_writer/line_reader.h"
 35#include "breakpad_googletest_includes.h"
 36#include "common/linux/eintr_wrapper.h"
 37
 38using namespace google_breakpad;
 39
 40#if !defined(__ANDROID__)
 41#define TEMPDIR "/tmp"
 42#else
 43#define TEMPDIR "/data/local/tmp"
 44#endif
 45
 46static int TemporaryFile() {
 47  static const char templ[] = TEMPDIR "/line-reader-unittest-XXXXXX";
 48  char templ_copy[sizeof(templ)];
 49  memcpy(templ_copy, templ, sizeof(templ));
 50  const int fd = mkstemp(templ_copy);
 51  if (fd >= 0)
 52    unlink(templ_copy);
 53
 54  return fd;
 55}
 56
 57namespace {
 58typedef testing::Test LineReaderTest;
 59}
 60
 61TEST(LineReaderTest, EmptyFile) {
 62  const int fd = TemporaryFile();
 63  LineReader reader(fd);
 64
 65  const char *line;
 66  unsigned len;
 67  ASSERT_FALSE(reader.GetNextLine(&line, &len));
 68
 69  close(fd);
 70}
 71
 72TEST(LineReaderTest, OneLineTerminated) {
 73  const int fd = TemporaryFile();
 74  const int r = HANDLE_EINTR(write(fd, "a\n", 2));
 75  ASSERT_EQ(2, r);
 76  lseek(fd, 0, SEEK_SET);
 77  LineReader reader(fd);
 78
 79  const char *line;
 80  unsigned int len;
 81  ASSERT_TRUE(reader.GetNextLine(&line, &len));
 82  ASSERT_EQ((unsigned int)1, len);
 83  ASSERT_EQ('a', line[0]);
 84  ASSERT_EQ('\0', line[1]);
 85  reader.PopLine(len);
 86
 87  ASSERT_FALSE(reader.GetNextLine(&line, &len));
 88
 89  close(fd);
 90}
 91
 92TEST(LineReaderTest, OneLine) {
 93  const int fd = TemporaryFile();
 94  const int r = HANDLE_EINTR(write(fd, "a", 1));
 95  ASSERT_EQ(1, r);
 96  lseek(fd, 0, SEEK_SET);
 97  LineReader reader(fd);
 98
 99  const char *line;
100  unsigned len;
101  ASSERT_TRUE(reader.GetNextLine(&line, &len));
102  ASSERT_EQ((unsigned)1, len);
103  ASSERT_EQ('a', line[0]);
104  ASSERT_EQ('\0', line[1]);
105  reader.PopLine(len);
106
107  ASSERT_FALSE(reader.GetNextLine(&line, &len));
108
109  close(fd);
110}
111
112TEST(LineReaderTest, TwoLinesTerminated) {
113  const int fd = TemporaryFile();
114  const int r = HANDLE_EINTR(write(fd, "a\nb\n", 4));
115  ASSERT_EQ(4, r);
116  lseek(fd, 0, SEEK_SET);
117  LineReader reader(fd);
118
119  const char *line;
120  unsigned len;
121  ASSERT_TRUE(reader.GetNextLine(&line, &len));
122  ASSERT_EQ((unsigned)1, len);
123  ASSERT_EQ('a', line[0]);
124  ASSERT_EQ('\0', line[1]);
125  reader.PopLine(len);
126
127  ASSERT_TRUE(reader.GetNextLine(&line, &len));
128  ASSERT_EQ((unsigned)1, len);
129  ASSERT_EQ('b', line[0]);
130  ASSERT_EQ('\0', line[1]);
131  reader.PopLine(len);
132
133  ASSERT_FALSE(reader.GetNextLine(&line, &len));
134
135  close(fd);
136}
137
138TEST(LineReaderTest, TwoLines) {
139  const int fd = TemporaryFile();
140  const int r = HANDLE_EINTR(write(fd, "a\nb", 3));
141  ASSERT_EQ(3, r);
142  lseek(fd, 0, SEEK_SET);
143  LineReader reader(fd);
144
145  const char *line;
146  unsigned len;
147  ASSERT_TRUE(reader.GetNextLine(&line, &len));
148  ASSERT_EQ((unsigned)1, len);
149  ASSERT_EQ('a', line[0]);
150  ASSERT_EQ('\0', line[1]);
151  reader.PopLine(len);
152
153  ASSERT_TRUE(reader.GetNextLine(&line, &len));
154  ASSERT_EQ((unsigned)1, len);
155  ASSERT_EQ('b', line[0]);
156  ASSERT_EQ('\0', line[1]);
157  reader.PopLine(len);
158
159  ASSERT_FALSE(reader.GetNextLine(&line, &len));
160
161  close(fd);
162}
163
164TEST(LineReaderTest, MaxLength) {
165  const int fd = TemporaryFile();
166  char l[LineReader::kMaxLineLen - 1];
167  memset(l, 'a', sizeof(l));
168  const int r = HANDLE_EINTR(write(fd, l, sizeof(l)));
169  ASSERT_EQ(sizeof(l), r);
170  lseek(fd, 0, SEEK_SET);
171  LineReader reader(fd);
172
173  const char *line;
174  unsigned len;
175  ASSERT_TRUE(reader.GetNextLine(&line, &len));
176  ASSERT_EQ(sizeof(l), len);
177  ASSERT_TRUE(memcmp(l, line, sizeof(l)) == 0);
178  ASSERT_EQ('\0', line[len]);
179
180  close(fd);
181}
182
183TEST(LineReaderTest, TooLong) {
184  const int fd = TemporaryFile();
185  char l[LineReader::kMaxLineLen];
186  memset(l, 'a', sizeof(l));
187  const int r = HANDLE_EINTR(write(fd, l, sizeof(l)));
188  ASSERT_EQ(sizeof(l), r);
189  lseek(fd, 0, SEEK_SET);
190  LineReader reader(fd);
191
192  const char *line;
193  unsigned len;
194  ASSERT_FALSE(reader.GetNextLine(&line, &len));
195
196  close(fd);
197}