• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2015 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include <gtest/gtest.h>
18 
19 #include <string>
20 
21 #include <android-base/test_utils.h>
22 
23 #include "LineBuffer.h"
24 
25 class LineBufferTest : public ::testing::Test {
26  protected:
SetUp()27   void SetUp() override {
28     tmp_file_ = new TemporaryFile();
29     ASSERT_TRUE(tmp_file_->fd != -1);
30   }
31 
TearDown()32   void TearDown() override {
33     delete tmp_file_;
34   }
35 
36  TemporaryFile* tmp_file_ = nullptr;
37 };
38 
TEST_F(LineBufferTest,single_line)39 TEST_F(LineBufferTest, single_line) {
40   std::string line_data;
41   line_data += "Single line with newline.\n";
42   ASSERT_TRUE(TEMP_FAILURE_RETRY(
43       write(tmp_file_->fd, line_data.c_str(), line_data.size())) != -1);
44   ASSERT_TRUE(lseek(tmp_file_->fd, 0, SEEK_SET) != off_t(-1));
45 
46   char buffer[100];
47   LineBuffer line_buf(tmp_file_->fd, buffer, sizeof(buffer));
48 
49   char* line;
50   size_t line_len;
51   ASSERT_TRUE(line_buf.GetLine(&line, &line_len));
52   ASSERT_STREQ("Single line with newline.", line);
53   ASSERT_EQ(sizeof("Single line with newline.") - 1, line_len);
54 
55   ASSERT_FALSE(line_buf.GetLine(&line, &line_len));
56 }
57 
TEST_F(LineBufferTest,single_line_no_newline)58 TEST_F(LineBufferTest, single_line_no_newline) {
59   std::string line_data;
60   line_data += "Single line with no newline.";
61   ASSERT_TRUE(TEMP_FAILURE_RETRY(
62       write(tmp_file_->fd, line_data.c_str(), line_data.size())) != -1);
63   ASSERT_TRUE(lseek(tmp_file_->fd, 0, SEEK_SET) != off_t(-1));
64 
65   char buffer[100];
66   LineBuffer line_buf(tmp_file_->fd, buffer, sizeof(buffer));
67 
68   char* line;
69   size_t line_len;
70   ASSERT_TRUE(line_buf.GetLine(&line, &line_len));
71   ASSERT_STREQ("Single line with no newline.", line);
72   ASSERT_EQ(sizeof("Single line with no newline.") - 1, line_len);
73 
74   ASSERT_FALSE(line_buf.GetLine(&line, &line_len));
75 }
76 
TEST_F(LineBufferTest,single_read)77 TEST_F(LineBufferTest, single_read) {
78   std::string line_data;
79   line_data += "The first line.\n";
80   line_data += "Second line here.\n";
81   line_data += "Third line is last.\n";
82   ASSERT_TRUE(TEMP_FAILURE_RETRY(
83       write(tmp_file_->fd, line_data.c_str(), line_data.size())) != -1);
84   ASSERT_TRUE(lseek(tmp_file_->fd, 0, SEEK_SET) != off_t(-1));
85 
86   char buffer[100];
87   LineBuffer line_buf(tmp_file_->fd, buffer, sizeof(buffer));
88 
89   char* line;
90   size_t line_len;
91   ASSERT_TRUE(line_buf.GetLine(&line, &line_len));
92   ASSERT_STREQ("The first line.", line);
93   ASSERT_EQ(sizeof("The first line.") - 1, line_len);
94 
95   ASSERT_TRUE(line_buf.GetLine(&line, &line_len));
96   ASSERT_STREQ("Second line here.", line);
97   ASSERT_EQ(sizeof("Second line here.") - 1, line_len);
98 
99   ASSERT_TRUE(line_buf.GetLine(&line, &line_len));
100   ASSERT_STREQ("Third line is last.", line);
101   ASSERT_EQ(sizeof("Third line is last.") - 1, line_len);
102 
103   ASSERT_FALSE(line_buf.GetLine(&line, &line_len));
104 }
105 
TEST_F(LineBufferTest,single_read_no_end_newline)106 TEST_F(LineBufferTest, single_read_no_end_newline) {
107   std::string line_data;
108   line_data += "The first line.\n";
109   line_data += "Second line here.\n";
110   line_data += "Third line is last no newline.";
111   ASSERT_TRUE(TEMP_FAILURE_RETRY(
112       write(tmp_file_->fd, line_data.c_str(), line_data.size())) != -1);
113   ASSERT_TRUE(lseek(tmp_file_->fd, 0, SEEK_SET) != off_t(-1));
114 
115   char buffer[100];
116   LineBuffer line_buf(tmp_file_->fd, buffer, sizeof(buffer));
117 
118   char* line;
119   size_t line_len;
120   ASSERT_TRUE(line_buf.GetLine(&line, &line_len));
121   ASSERT_STREQ("The first line.", line);
122   ASSERT_EQ(sizeof("The first line.") - 1, line_len);
123 
124   ASSERT_TRUE(line_buf.GetLine(&line, &line_len));
125   ASSERT_STREQ("Second line here.", line);
126   ASSERT_EQ(sizeof("Second line here.") - 1, line_len);
127 
128   ASSERT_TRUE(line_buf.GetLine(&line, &line_len));
129   ASSERT_STREQ("Third line is last no newline.", line);
130   ASSERT_EQ(sizeof("Third line is last no newline.") - 1, line_len);
131 
132   ASSERT_FALSE(line_buf.GetLine(&line, &line_len));
133 }
134 
TEST_F(LineBufferTest,one_line_per_read)135 TEST_F(LineBufferTest, one_line_per_read) {
136   std::string line_data;
137   line_data += "The first line.\n";
138   line_data += "Second line here.\n";
139   line_data += "Third line is last.\n";
140   line_data += "The fourth line.\n";
141   ASSERT_TRUE(TEMP_FAILURE_RETRY(
142       write(tmp_file_->fd, line_data.c_str(), line_data.size())) != -1);
143   ASSERT_TRUE(lseek(tmp_file_->fd, 0, SEEK_SET) != off_t(-1));
144 
145   char buffer[24];
146   LineBuffer line_buf(tmp_file_->fd, buffer, sizeof(buffer));
147 
148   char* line;
149   size_t line_len;
150   ASSERT_TRUE(line_buf.GetLine(&line, &line_len));
151   ASSERT_STREQ("The first line.", line);
152   ASSERT_EQ(sizeof("The first line.") - 1, line_len);
153 
154   ASSERT_TRUE(line_buf.GetLine(&line, &line_len));
155   ASSERT_STREQ("Second line here.", line);
156   ASSERT_EQ(sizeof("Second line here.") - 1, line_len);
157 
158   ASSERT_TRUE(line_buf.GetLine(&line, &line_len));
159   ASSERT_STREQ("Third line is last.", line);
160   ASSERT_EQ(sizeof("Third line is last.") - 1, line_len);
161 
162   line_data += "The fourth line.\n";
163   ASSERT_TRUE(line_buf.GetLine(&line, &line_len));
164   ASSERT_STREQ("The fourth line.", line);
165   ASSERT_EQ(sizeof("The fourth line.") - 1, line_len);
166 
167   ASSERT_FALSE(line_buf.GetLine(&line, &line_len));
168 }
169 
TEST_F(LineBufferTest,multiple_line_per_read_multiple_reads)170 TEST_F(LineBufferTest, multiple_line_per_read_multiple_reads) {
171   std::string line_data;
172   line_data += "The first line.\n";
173   line_data += "Second line here.\n";
174   line_data += "Third line is last.\n";
175   line_data += "The fourth line.\n";
176   ASSERT_TRUE(TEMP_FAILURE_RETRY(
177       write(tmp_file_->fd, line_data.c_str(), line_data.size())) != -1);
178   ASSERT_TRUE(lseek(tmp_file_->fd, 0, SEEK_SET) != off_t(-1));
179 
180   char buffer[60];
181   LineBuffer line_buf(tmp_file_->fd, buffer, sizeof(buffer));
182 
183   char* line;
184   size_t line_len;
185   ASSERT_TRUE(line_buf.GetLine(&line, &line_len));
186   ASSERT_STREQ("The first line.", line);
187   ASSERT_EQ(sizeof("The first line.") - 1, line_len);
188 
189   ASSERT_TRUE(line_buf.GetLine(&line, &line_len));
190   ASSERT_STREQ("Second line here.", line);
191   ASSERT_EQ(sizeof("Second line here.") - 1, line_len);
192 
193   ASSERT_TRUE(line_buf.GetLine(&line, &line_len));
194   ASSERT_STREQ("Third line is last.", line);
195   ASSERT_EQ(sizeof("Third line is last.") - 1, line_len);
196 
197   line_data += "The fourth line.\n";
198   ASSERT_TRUE(line_buf.GetLine(&line, &line_len));
199   ASSERT_STREQ("The fourth line.", line);
200   ASSERT_EQ(sizeof("The fourth line.") - 1, line_len);
201 
202   ASSERT_FALSE(line_buf.GetLine(&line, &line_len));
203 }
204 
TEST_F(LineBufferTest,line_larger_than_buffer)205 TEST_F(LineBufferTest, line_larger_than_buffer) {
206   std::string line_data;
207   line_data += "The first line.\n";
208   line_data += "Second line here.\n";
209   line_data += "This is a really, really, really, kind of long.\n";
210   line_data += "The fourth line.\n";
211   ASSERT_TRUE(TEMP_FAILURE_RETRY(
212       write(tmp_file_->fd, line_data.c_str(), line_data.size())) != -1);
213   ASSERT_TRUE(lseek(tmp_file_->fd, 0, SEEK_SET) != off_t(-1));
214 
215   char buffer[25];
216   LineBuffer line_buf(tmp_file_->fd, buffer, sizeof(buffer));
217 
218   char* line;
219   size_t line_len;
220   ASSERT_TRUE(line_buf.GetLine(&line, &line_len));
221   ASSERT_STREQ("The first line.", line);
222   ASSERT_EQ(sizeof("The first line.") - 1, line_len);
223 
224   ASSERT_TRUE(line_buf.GetLine(&line, &line_len));
225   ASSERT_STREQ("Second line here.", line);
226   ASSERT_EQ(sizeof("Second line here.") - 1, line_len);
227 
228   ASSERT_TRUE(line_buf.GetLine(&line, &line_len));
229   ASSERT_STREQ("This is a really, really", line);
230   ASSERT_EQ(sizeof(buffer) - 1, line_len);
231   ASSERT_TRUE(line_buf.GetLine(&line, &line_len));
232   ASSERT_STREQ(", really, kind of long.", line);
233   ASSERT_EQ(sizeof(", really, kind of long.") - 1, line_len);
234 
235   line_data += "The fourth line.\n";
236   ASSERT_TRUE(line_buf.GetLine(&line, &line_len));
237   ASSERT_STREQ("The fourth line.", line);
238   ASSERT_EQ(sizeof("The fourth line.") - 1, line_len);
239 
240   ASSERT_FALSE(line_buf.GetLine(&line, &line_len));
241 }
242