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