Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(109)

Side by Side Diff: webrtc/base/filerotatingstream_unittest.cc

Issue 2877023002: Move webrtc/{base => rtc_base} (Closed)
Patch Set: update presubmit.py and DEPS include rules Created 3 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « webrtc/base/filerotatingstream.cc ('k') | webrtc/base/fileutils.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 /*
2 * Copyright 2015 The WebRTC Project Authors. All rights reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
11 #include <memory>
12
13 #include "webrtc/base/arraysize.h"
14 #include "webrtc/base/checks.h"
15 #include "webrtc/base/filerotatingstream.h"
16 #include "webrtc/base/fileutils.h"
17 #include "webrtc/base/gunit.h"
18 #include "webrtc/base/pathutils.h"
19 #include "webrtc/test/testsupport/fileutils.h"
20
21 namespace rtc {
22
23 namespace {
24
25 void CleanupLogDirectory(const FileRotatingStream& stream) {
26 for (size_t i = 0; i < stream.GetNumFiles(); ++i) {
27 // Ignore return value, not all files are expected to exist.
28 webrtc::test::RemoveFile(stream.GetFilePath(i));
29 }
30 }
31
32 } // namespace
33
34 #if defined (WEBRTC_ANDROID)
35 // Fails on Android: https://bugs.chromium.org/p/webrtc/issues/detail?id=4364.
36 #define MAYBE_FileRotatingStreamTest DISABLED_FileRotatingStreamTest
37 #else
38 #define MAYBE_FileRotatingStreamTest FileRotatingStreamTest
39 #endif
40
41 class MAYBE_FileRotatingStreamTest : public ::testing::Test {
42 protected:
43 static const char* kFilePrefix;
44 static const size_t kMaxFileSize;
45
46 void Init(const std::string& dir_name,
47 const std::string& file_prefix,
48 size_t max_file_size,
49 size_t num_log_files) {
50 dir_path_ = webrtc::test::OutputPath();
51
52 // Append per-test output path in order to run within gtest parallel.
53 dir_path_.append(dir_name);
54 dir_path_.push_back(Pathname::DefaultFolderDelimiter());
55 ASSERT_TRUE(webrtc::test::CreateDir(dir_path_));
56 stream_.reset(new FileRotatingStream(dir_path_, file_prefix, max_file_size,
57 num_log_files));
58 }
59
60 void TearDown() override {
61 // On windows, open files can't be removed.
62 stream_->Close();
63 CleanupLogDirectory(*stream_);
64 EXPECT_TRUE(webrtc::test::RemoveDir(dir_path_));
65
66 stream_.reset();
67 }
68
69 // Writes the data to the stream and flushes it.
70 void WriteAndFlush(const void* data, const size_t data_len) {
71 EXPECT_EQ(SR_SUCCESS, stream_->WriteAll(data, data_len, nullptr, nullptr));
72 EXPECT_TRUE(stream_->Flush());
73 }
74
75 // Checks that the stream reads in the expected contents and then returns an
76 // end of stream result.
77 void VerifyStreamRead(const char* expected_contents,
78 const size_t expected_length,
79 const std::string& dir_path,
80 const char* file_prefix) {
81 std::unique_ptr<FileRotatingStream> stream;
82 stream.reset(new FileRotatingStream(dir_path, file_prefix));
83 ASSERT_TRUE(stream->Open());
84 size_t read = 0;
85 size_t stream_size = 0;
86 EXPECT_TRUE(stream->GetSize(&stream_size));
87 std::unique_ptr<uint8_t[]> buffer(new uint8_t[expected_length]);
88 EXPECT_EQ(SR_SUCCESS,
89 stream->ReadAll(buffer.get(), expected_length, &read, nullptr));
90 EXPECT_EQ(0, memcmp(expected_contents, buffer.get(), expected_length));
91 EXPECT_EQ(SR_EOS, stream->ReadAll(buffer.get(), 1, nullptr, nullptr));
92 EXPECT_EQ(stream_size, read);
93 }
94
95 void VerifyFileContents(const char* expected_contents,
96 const size_t expected_length,
97 const std::string& file_path) {
98 std::unique_ptr<uint8_t[]> buffer(new uint8_t[expected_length]);
99 FileStream stream;
100 ASSERT_TRUE(stream.Open(file_path, "r", nullptr));
101 EXPECT_EQ(rtc::SR_SUCCESS,
102 stream.ReadAll(buffer.get(), expected_length, nullptr, nullptr));
103 EXPECT_EQ(0, memcmp(expected_contents, buffer.get(), expected_length));
104 size_t file_size = 0;
105 EXPECT_TRUE(stream.GetSize(&file_size));
106 EXPECT_EQ(file_size, expected_length);
107 }
108
109 std::unique_ptr<FileRotatingStream> stream_;
110 std::string dir_path_;
111 };
112
113 const char* MAYBE_FileRotatingStreamTest::kFilePrefix =
114 "FileRotatingStreamTest";
115 const size_t MAYBE_FileRotatingStreamTest::kMaxFileSize = 2;
116
117 // Tests that stream state is correct before and after Open / Close.
118 TEST_F(MAYBE_FileRotatingStreamTest, State) {
119 Init("FileRotatingStreamTestState", kFilePrefix, kMaxFileSize, 3);
120
121 EXPECT_EQ(SS_CLOSED, stream_->GetState());
122 ASSERT_TRUE(stream_->Open());
123 EXPECT_EQ(SS_OPEN, stream_->GetState());
124 stream_->Close();
125 EXPECT_EQ(SS_CLOSED, stream_->GetState());
126 }
127
128 // Tests that nothing is written to file when data of length zero is written.
129 TEST_F(MAYBE_FileRotatingStreamTest, EmptyWrite) {
130 Init("FileRotatingStreamTestEmptyWrite", kFilePrefix, kMaxFileSize, 3);
131
132 ASSERT_TRUE(stream_->Open());
133 WriteAndFlush("a", 0);
134
135 std::string logfile_path = stream_->GetFilePath(0);
136 FileStream stream;
137 ASSERT_TRUE(stream.Open(logfile_path, "r", nullptr));
138 size_t file_size = 0;
139 EXPECT_TRUE(stream.GetSize(&file_size));
140 EXPECT_EQ(0u, file_size);
141 }
142
143 // Tests that a write operation followed by a read returns the expected data
144 // and writes to the expected files.
145 TEST_F(MAYBE_FileRotatingStreamTest, WriteAndRead) {
146 Init("FileRotatingStreamTestWriteAndRead", kFilePrefix, kMaxFileSize, 3);
147
148 ASSERT_TRUE(stream_->Open());
149 // The test is set up to create three log files of length 2. Write and check
150 // contents.
151 std::string messages[3] = {"aa", "bb", "cc"};
152 for (size_t i = 0; i < arraysize(messages); ++i) {
153 const std::string& message = messages[i];
154 WriteAndFlush(message.c_str(), message.size());
155 // Since the max log size is 2, we will be causing rotation. Read from the
156 // next file.
157 VerifyFileContents(message.c_str(), message.size(),
158 stream_->GetFilePath(1));
159 }
160 // Check that exactly three files exist.
161 for (size_t i = 0; i < arraysize(messages); ++i) {
162 EXPECT_TRUE(Filesystem::IsFile(stream_->GetFilePath(i)));
163 }
164 std::string message("d");
165 WriteAndFlush(message.c_str(), message.size());
166 for (size_t i = 0; i < arraysize(messages); ++i) {
167 EXPECT_TRUE(Filesystem::IsFile(stream_->GetFilePath(i)));
168 }
169 // TODO(tkchin): Maybe check all the files in the dir.
170
171 // Reopen for read.
172 std::string expected_contents("bbccd");
173 VerifyStreamRead(expected_contents.c_str(), expected_contents.size(),
174 dir_path_, kFilePrefix);
175 }
176
177 // Tests that writing data greater than the total capacity of the files
178 // overwrites the files correctly and is read correctly after.
179 TEST_F(MAYBE_FileRotatingStreamTest, WriteOverflowAndRead) {
180 Init("FileRotatingStreamTestWriteOverflowAndRead", kFilePrefix, kMaxFileSize,
181 3);
182 ASSERT_TRUE(stream_->Open());
183 // This should cause overflow across all three files, such that the first file
184 // we wrote to also gets overwritten.
185 std::string message("foobarbaz");
186 WriteAndFlush(message.c_str(), message.size());
187 std::string expected_file_contents("z");
188 VerifyFileContents(expected_file_contents.c_str(),
189 expected_file_contents.size(), stream_->GetFilePath(0));
190 std::string expected_stream_contents("arbaz");
191 VerifyStreamRead(expected_stream_contents.c_str(),
192 expected_stream_contents.size(), dir_path_, kFilePrefix);
193 }
194
195 // Tests that the returned file paths have the right folder and prefix.
196 TEST_F(MAYBE_FileRotatingStreamTest, GetFilePath) {
197 Init("FileRotatingStreamTestGetFilePath", kFilePrefix, kMaxFileSize, 20);
198 for (auto i = 0; i < 20; ++i) {
199 Pathname path(stream_->GetFilePath(i));
200 EXPECT_EQ(0, path.folder().compare(dir_path_));
201 EXPECT_EQ(0, path.filename().compare(0, strlen(kFilePrefix), kFilePrefix));
202 }
203 }
204
205 #if defined (WEBRTC_ANDROID)
206 // Fails on Android: https://bugs.chromium.org/p/webrtc/issues/detail?id=4364.
207 #define MAYBE_CallSessionFileRotatingStreamTest \
208 DISABLED_CallSessionFileRotatingStreamTest
209 #else
210 #define MAYBE_CallSessionFileRotatingStreamTest \
211 CallSessionFileRotatingStreamTest
212 #endif
213
214 class MAYBE_CallSessionFileRotatingStreamTest : public ::testing::Test {
215 protected:
216 void Init(const std::string& dir_name, size_t max_total_log_size) {
217 dir_path_ = webrtc::test::OutputPath();
218
219 // Append per-test output path in order to run within gtest parallel.
220 dir_path_.append(dir_name);
221 dir_path_.push_back(Pathname::DefaultFolderDelimiter());
222 ASSERT_TRUE(webrtc::test::CreateDir(dir_path_));
223 stream_.reset(
224 new CallSessionFileRotatingStream(dir_path_, max_total_log_size));
225 }
226
227 virtual void TearDown() {
228 // On windows, open files can't be removed.
229 stream_->Close();
230 CleanupLogDirectory(*stream_);
231 EXPECT_TRUE(webrtc::test::RemoveDir(dir_path_));
232
233 stream_.reset();
234 }
235
236 // Writes the data to the stream and flushes it.
237 void WriteAndFlush(const void* data, const size_t data_len) {
238 EXPECT_EQ(SR_SUCCESS, stream_->WriteAll(data, data_len, nullptr, nullptr));
239 EXPECT_TRUE(stream_->Flush());
240 }
241
242 // Checks that the stream reads in the expected contents and then returns an
243 // end of stream result.
244 void VerifyStreamRead(const char* expected_contents,
245 const size_t expected_length,
246 const std::string& dir_path) {
247 std::unique_ptr<CallSessionFileRotatingStream> stream(
248 new CallSessionFileRotatingStream(dir_path));
249 ASSERT_TRUE(stream->Open());
250 size_t read = 0;
251 size_t stream_size = 0;
252 EXPECT_TRUE(stream->GetSize(&stream_size));
253 std::unique_ptr<uint8_t[]> buffer(new uint8_t[expected_length]);
254 EXPECT_EQ(SR_SUCCESS,
255 stream->ReadAll(buffer.get(), expected_length, &read, nullptr));
256 EXPECT_EQ(0, memcmp(expected_contents, buffer.get(), expected_length));
257 EXPECT_EQ(SR_EOS, stream->ReadAll(buffer.get(), 1, nullptr, nullptr));
258 EXPECT_EQ(stream_size, read);
259 }
260
261 std::unique_ptr<CallSessionFileRotatingStream> stream_;
262 std::string dir_path_;
263 };
264
265 // Tests that writing and reading to a stream with the smallest possible
266 // capacity works.
267 TEST_F(MAYBE_CallSessionFileRotatingStreamTest, WriteAndReadSmallest) {
268 Init("CallSessionFileRotatingStreamTestWriteAndReadSmallest", 4);
269
270 ASSERT_TRUE(stream_->Open());
271 std::string message("abcde");
272 WriteAndFlush(message.c_str(), message.size());
273 std::string expected_contents("abe");
274 VerifyStreamRead(expected_contents.c_str(), expected_contents.size(),
275 dir_path_);
276 }
277
278 // Tests that writing and reading to a stream with capacity lesser than 4MB
279 // behaves correctly.
280 TEST_F(MAYBE_CallSessionFileRotatingStreamTest, WriteAndReadSmall) {
281 Init("CallSessionFileRotatingStreamTestWriteAndReadSmall", 8);
282
283 ASSERT_TRUE(stream_->Open());
284 std::string message("123456789");
285 WriteAndFlush(message.c_str(), message.size());
286 std::string expected_contents("1234789");
287 VerifyStreamRead(expected_contents.c_str(), expected_contents.size(),
288 dir_path_);
289 }
290
291 // Tests that writing and reading to a stream with capacity greater than 4MB
292 // behaves correctly.
293 TEST_F(MAYBE_CallSessionFileRotatingStreamTest, WriteAndReadLarge) {
294 Init("CallSessionFileRotatingStreamTestWriteAndReadLarge", 6 * 1024 * 1024);
295
296 ASSERT_TRUE(stream_->Open());
297 const size_t buffer_size = 1024 * 1024;
298 std::unique_ptr<uint8_t[]> buffer(new uint8_t[buffer_size]);
299 for (int i = 0; i < 8; i++) {
300 memset(buffer.get(), i, buffer_size);
301 EXPECT_EQ(SR_SUCCESS,
302 stream_->WriteAll(buffer.get(), buffer_size, nullptr, nullptr));
303 }
304
305 stream_.reset(new CallSessionFileRotatingStream(dir_path_));
306 ASSERT_TRUE(stream_->Open());
307 std::unique_ptr<uint8_t[]> expected_buffer(new uint8_t[buffer_size]);
308 int expected_vals[] = {0, 1, 2, 6, 7};
309 for (size_t i = 0; i < arraysize(expected_vals); ++i) {
310 memset(expected_buffer.get(), expected_vals[i], buffer_size);
311 EXPECT_EQ(SR_SUCCESS,
312 stream_->ReadAll(buffer.get(), buffer_size, nullptr, nullptr));
313 EXPECT_EQ(0, memcmp(buffer.get(), expected_buffer.get(), buffer_size));
314 }
315 EXPECT_EQ(SR_EOS, stream_->ReadAll(buffer.get(), 1, nullptr, nullptr));
316 }
317
318 // Tests that writing and reading to a stream where only the first file is
319 // written to behaves correctly.
320 TEST_F(MAYBE_CallSessionFileRotatingStreamTest, WriteAndReadFirstHalf) {
321 Init("CallSessionFileRotatingStreamTestWriteAndReadFirstHalf",
322 6 * 1024 * 1024);
323 ASSERT_TRUE(stream_->Open());
324 const size_t buffer_size = 1024 * 1024;
325 std::unique_ptr<uint8_t[]> buffer(new uint8_t[buffer_size]);
326 for (int i = 0; i < 2; i++) {
327 memset(buffer.get(), i, buffer_size);
328 EXPECT_EQ(SR_SUCCESS,
329 stream_->WriteAll(buffer.get(), buffer_size, nullptr, nullptr));
330 }
331
332 stream_.reset(new CallSessionFileRotatingStream(dir_path_));
333 ASSERT_TRUE(stream_->Open());
334 std::unique_ptr<uint8_t[]> expected_buffer(new uint8_t[buffer_size]);
335 int expected_vals[] = {0, 1};
336 for (size_t i = 0; i < arraysize(expected_vals); ++i) {
337 memset(expected_buffer.get(), expected_vals[i], buffer_size);
338 EXPECT_EQ(SR_SUCCESS,
339 stream_->ReadAll(buffer.get(), buffer_size, nullptr, nullptr));
340 EXPECT_EQ(0, memcmp(buffer.get(), expected_buffer.get(), buffer_size));
341 }
342 EXPECT_EQ(SR_EOS, stream_->ReadAll(buffer.get(), 1, nullptr, nullptr));
343 }
344
345 } // namespace rtc
OLDNEW
« no previous file with comments | « webrtc/base/filerotatingstream.cc ('k') | webrtc/base/fileutils.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698