blob: 172be57eacc69d48c9c25f0e58784186b9c71cd6 [file] [log] [blame]
tkchin93411912015-07-22 12:12:17 -07001/*
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
jbauch555604a2016-04-26 03:13:22 -070011#include <memory>
12
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020013#include "rtc_base/arraysize.h"
14#include "rtc_base/checks.h"
15#include "rtc_base/filerotatingstream.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020016#include "rtc_base/gunit.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020017#include "test/testsupport/fileutils.h"
tkchin93411912015-07-22 12:12:17 -070018
19namespace rtc {
20
nisse57efb032017-05-18 03:55:59 -070021namespace {
22
23void CleanupLogDirectory(const FileRotatingStream& stream) {
24 for (size_t i = 0; i < stream.GetNumFiles(); ++i) {
25 // Ignore return value, not all files are expected to exist.
26 webrtc::test::RemoveFile(stream.GetFilePath(i));
27 }
28}
29
30} // namespace
31
Yves Gerey665174f2018-06-19 15:03:05 +020032#if defined(WEBRTC_ANDROID)
phoglundbb738732016-07-15 03:57:12 -070033// Fails on Android: https://bugs.chromium.org/p/webrtc/issues/detail?id=4364.
34#define MAYBE_FileRotatingStreamTest DISABLED_FileRotatingStreamTest
35#else
36#define MAYBE_FileRotatingStreamTest FileRotatingStreamTest
37#endif
38
39class MAYBE_FileRotatingStreamTest : public ::testing::Test {
tkchin93411912015-07-22 12:12:17 -070040 protected:
41 static const char* kFilePrefix;
42 static const size_t kMaxFileSize;
43
44 void Init(const std::string& dir_name,
45 const std::string& file_prefix,
46 size_t max_file_size,
Niels Möller7b3c76b2018-11-07 09:54:28 +010047 size_t num_log_files,
48 bool ensure_trailing_delimiter = true) {
nisse57efb032017-05-18 03:55:59 -070049 dir_path_ = webrtc::test::OutputPath();
50
tkchin93411912015-07-22 12:12:17 -070051 // Append per-test output path in order to run within gtest parallel.
nisse57efb032017-05-18 03:55:59 -070052 dir_path_.append(dir_name);
Niels Möller7b3c76b2018-11-07 09:54:28 +010053 if (ensure_trailing_delimiter) {
54 dir_path_.append(webrtc::test::kPathDelimiter);
55 }
nisse57efb032017-05-18 03:55:59 -070056 ASSERT_TRUE(webrtc::test::CreateDir(dir_path_));
tkchin93411912015-07-22 12:12:17 -070057 stream_.reset(new FileRotatingStream(dir_path_, file_prefix, max_file_size,
58 num_log_files));
59 }
60
61 void TearDown() override {
nisse57efb032017-05-18 03:55:59 -070062 // On windows, open files can't be removed.
63 stream_->Close();
64 CleanupLogDirectory(*stream_);
65 EXPECT_TRUE(webrtc::test::RemoveDir(dir_path_));
66
tkchin93411912015-07-22 12:12:17 -070067 stream_.reset();
tkchin93411912015-07-22 12:12:17 -070068 }
69
70 // Writes the data to the stream and flushes it.
71 void WriteAndFlush(const void* data, const size_t data_len) {
72 EXPECT_EQ(SR_SUCCESS, stream_->WriteAll(data, data_len, nullptr, nullptr));
73 EXPECT_TRUE(stream_->Flush());
74 }
75
76 // Checks that the stream reads in the expected contents and then returns an
77 // end of stream result.
78 void VerifyStreamRead(const char* expected_contents,
79 const size_t expected_length,
80 const std::string& dir_path,
81 const char* file_prefix) {
jbauch555604a2016-04-26 03:13:22 -070082 std::unique_ptr<FileRotatingStream> stream;
tkchin93411912015-07-22 12:12:17 -070083 stream.reset(new FileRotatingStream(dir_path, file_prefix));
84 ASSERT_TRUE(stream->Open());
tkchin28bae022015-07-23 12:27:02 -070085 size_t read = 0;
86 size_t stream_size = 0;
87 EXPECT_TRUE(stream->GetSize(&stream_size));
jbauch555604a2016-04-26 03:13:22 -070088 std::unique_ptr<uint8_t[]> buffer(new uint8_t[expected_length]);
tkchin93411912015-07-22 12:12:17 -070089 EXPECT_EQ(SR_SUCCESS,
tkchin28bae022015-07-23 12:27:02 -070090 stream->ReadAll(buffer.get(), expected_length, &read, nullptr));
tkchin93411912015-07-22 12:12:17 -070091 EXPECT_EQ(0, memcmp(expected_contents, buffer.get(), expected_length));
92 EXPECT_EQ(SR_EOS, stream->ReadAll(buffer.get(), 1, nullptr, nullptr));
tkchin28bae022015-07-23 12:27:02 -070093 EXPECT_EQ(stream_size, read);
tkchin93411912015-07-22 12:12:17 -070094 }
95
96 void VerifyFileContents(const char* expected_contents,
97 const size_t expected_length,
98 const std::string& file_path) {
jbauch555604a2016-04-26 03:13:22 -070099 std::unique_ptr<uint8_t[]> buffer(new uint8_t[expected_length]);
nisse21083262017-05-31 02:07:21 -0700100 FileStream stream;
101 ASSERT_TRUE(stream.Open(file_path, "r", nullptr));
tkchin93411912015-07-22 12:12:17 -0700102 EXPECT_EQ(rtc::SR_SUCCESS,
nisse21083262017-05-31 02:07:21 -0700103 stream.ReadAll(buffer.get(), expected_length, nullptr, nullptr));
tkchin93411912015-07-22 12:12:17 -0700104 EXPECT_EQ(0, memcmp(expected_contents, buffer.get(), expected_length));
105 size_t file_size = 0;
nisse21083262017-05-31 02:07:21 -0700106 EXPECT_TRUE(stream.GetSize(&file_size));
tkchin93411912015-07-22 12:12:17 -0700107 EXPECT_EQ(file_size, expected_length);
108 }
109
jbauch555604a2016-04-26 03:13:22 -0700110 std::unique_ptr<FileRotatingStream> stream_;
tkchin93411912015-07-22 12:12:17 -0700111 std::string dir_path_;
112};
113
phoglundbb738732016-07-15 03:57:12 -0700114const char* MAYBE_FileRotatingStreamTest::kFilePrefix =
115 "FileRotatingStreamTest";
116const size_t MAYBE_FileRotatingStreamTest::kMaxFileSize = 2;
tkchin93411912015-07-22 12:12:17 -0700117
118// Tests that stream state is correct before and after Open / Close.
phoglundbb738732016-07-15 03:57:12 -0700119TEST_F(MAYBE_FileRotatingStreamTest, State) {
tkchin93411912015-07-22 12:12:17 -0700120 Init("FileRotatingStreamTestState", kFilePrefix, kMaxFileSize, 3);
121
122 EXPECT_EQ(SS_CLOSED, stream_->GetState());
123 ASSERT_TRUE(stream_->Open());
124 EXPECT_EQ(SS_OPEN, stream_->GetState());
125 stream_->Close();
126 EXPECT_EQ(SS_CLOSED, stream_->GetState());
127}
128
129// Tests that nothing is written to file when data of length zero is written.
phoglundbb738732016-07-15 03:57:12 -0700130TEST_F(MAYBE_FileRotatingStreamTest, EmptyWrite) {
tkchin93411912015-07-22 12:12:17 -0700131 Init("FileRotatingStreamTestEmptyWrite", kFilePrefix, kMaxFileSize, 3);
132
133 ASSERT_TRUE(stream_->Open());
134 WriteAndFlush("a", 0);
135
136 std::string logfile_path = stream_->GetFilePath(0);
nisse21083262017-05-31 02:07:21 -0700137 FileStream stream;
138 ASSERT_TRUE(stream.Open(logfile_path, "r", nullptr));
tkchin93411912015-07-22 12:12:17 -0700139 size_t file_size = 0;
nisse21083262017-05-31 02:07:21 -0700140 EXPECT_TRUE(stream.GetSize(&file_size));
tkchin93411912015-07-22 12:12:17 -0700141 EXPECT_EQ(0u, file_size);
142}
143
144// Tests that a write operation followed by a read returns the expected data
145// and writes to the expected files.
phoglundbb738732016-07-15 03:57:12 -0700146TEST_F(MAYBE_FileRotatingStreamTest, WriteAndRead) {
tkchin93411912015-07-22 12:12:17 -0700147 Init("FileRotatingStreamTestWriteAndRead", kFilePrefix, kMaxFileSize, 3);
148
149 ASSERT_TRUE(stream_->Open());
150 // The test is set up to create three log files of length 2. Write and check
151 // contents.
152 std::string messages[3] = {"aa", "bb", "cc"};
153 for (size_t i = 0; i < arraysize(messages); ++i) {
154 const std::string& message = messages[i];
155 WriteAndFlush(message.c_str(), message.size());
156 // Since the max log size is 2, we will be causing rotation. Read from the
157 // next file.
158 VerifyFileContents(message.c_str(), message.size(),
159 stream_->GetFilePath(1));
160 }
161 // Check that exactly three files exist.
162 for (size_t i = 0; i < arraysize(messages); ++i) {
Niels Möller260770c2018-11-07 15:08:18 +0100163 EXPECT_TRUE(webrtc::test::FileExists(stream_->GetFilePath(i)));
tkchin93411912015-07-22 12:12:17 -0700164 }
165 std::string message("d");
166 WriteAndFlush(message.c_str(), message.size());
167 for (size_t i = 0; i < arraysize(messages); ++i) {
Niels Möller260770c2018-11-07 15:08:18 +0100168 EXPECT_TRUE(webrtc::test::FileExists(stream_->GetFilePath(i)));
tkchin93411912015-07-22 12:12:17 -0700169 }
170 // TODO(tkchin): Maybe check all the files in the dir.
171
172 // Reopen for read.
173 std::string expected_contents("bbccd");
174 VerifyStreamRead(expected_contents.c_str(), expected_contents.size(),
175 dir_path_, kFilePrefix);
176}
177
Niels Möller7b3c76b2018-11-07 09:54:28 +0100178// Tests that a write operation (with dir name without delimiter) followed by a
179// read returns the expected data and writes to the expected files.
180TEST_F(MAYBE_FileRotatingStreamTest, WriteWithoutDelimiterAndRead) {
181 Init("FileRotatingStreamTestWriteWithoutDelimiterAndRead", kFilePrefix,
182 kMaxFileSize, 3,
183 /* ensure_trailing_delimiter*/ false);
184
185 ASSERT_TRUE(stream_->Open());
186 // The test is set up to create three log files of length 2. Write and check
187 // contents.
188 std::string messages[3] = {"aa", "bb", "cc"};
189 for (size_t i = 0; i < arraysize(messages); ++i) {
190 const std::string& message = messages[i];
191 WriteAndFlush(message.c_str(), message.size());
192 }
193 std::string message("d");
194 WriteAndFlush(message.c_str(), message.size());
195
196 // Reopen for read.
197 std::string expected_contents("bbccd");
198 VerifyStreamRead(expected_contents.c_str(), expected_contents.size(),
199 dir_path_ + webrtc::test::kPathDelimiter, kFilePrefix);
200}
201
202// Tests that a write operation followed by a read (without trailing delimiter)
203// returns the expected data and writes to the expected files.
204TEST_F(MAYBE_FileRotatingStreamTest, WriteAndReadWithoutDelimiter) {
205 Init("FileRotatingStreamTestWriteAndReadWithoutDelimiter", kFilePrefix,
206 kMaxFileSize, 3);
207
208 ASSERT_TRUE(stream_->Open());
209 // The test is set up to create three log files of length 2. Write and check
210 // contents.
211 std::string messages[3] = {"aa", "bb", "cc"};
212 for (size_t i = 0; i < arraysize(messages); ++i) {
213 const std::string& message = messages[i];
214 WriteAndFlush(message.c_str(), message.size());
215 }
216 std::string message("d");
217 WriteAndFlush(message.c_str(), message.size());
218
219 // Reopen for read.
220 std::string expected_contents("bbccd");
221 VerifyStreamRead(expected_contents.c_str(), expected_contents.size(),
222 dir_path_.substr(0, dir_path_.size() - 1), kFilePrefix);
223}
224
tkchin93411912015-07-22 12:12:17 -0700225// Tests that writing data greater than the total capacity of the files
226// overwrites the files correctly and is read correctly after.
phoglundbb738732016-07-15 03:57:12 -0700227TEST_F(MAYBE_FileRotatingStreamTest, WriteOverflowAndRead) {
tkchin93411912015-07-22 12:12:17 -0700228 Init("FileRotatingStreamTestWriteOverflowAndRead", kFilePrefix, kMaxFileSize,
229 3);
230 ASSERT_TRUE(stream_->Open());
231 // This should cause overflow across all three files, such that the first file
232 // we wrote to also gets overwritten.
233 std::string message("foobarbaz");
234 WriteAndFlush(message.c_str(), message.size());
235 std::string expected_file_contents("z");
236 VerifyFileContents(expected_file_contents.c_str(),
237 expected_file_contents.size(), stream_->GetFilePath(0));
238 std::string expected_stream_contents("arbaz");
239 VerifyStreamRead(expected_stream_contents.c_str(),
240 expected_stream_contents.size(), dir_path_, kFilePrefix);
241}
242
243// Tests that the returned file paths have the right folder and prefix.
phoglundbb738732016-07-15 03:57:12 -0700244TEST_F(MAYBE_FileRotatingStreamTest, GetFilePath) {
tkchin93411912015-07-22 12:12:17 -0700245 Init("FileRotatingStreamTestGetFilePath", kFilePrefix, kMaxFileSize, 20);
Niels Möller394b4eb2018-06-01 14:08:25 +0200246 // dir_path_ includes a trailing delimiter.
247 const std::string prefix = dir_path_ + kFilePrefix;
tkchin93411912015-07-22 12:12:17 -0700248 for (auto i = 0; i < 20; ++i) {
Niels Möller394b4eb2018-06-01 14:08:25 +0200249 EXPECT_EQ(0, stream_->GetFilePath(i).compare(0, prefix.size(), prefix));
tkchin93411912015-07-22 12:12:17 -0700250 }
251}
252
Yves Gerey665174f2018-06-19 15:03:05 +0200253#if defined(WEBRTC_ANDROID)
phoglundbb738732016-07-15 03:57:12 -0700254// Fails on Android: https://bugs.chromium.org/p/webrtc/issues/detail?id=4364.
255#define MAYBE_CallSessionFileRotatingStreamTest \
Yves Gerey665174f2018-06-19 15:03:05 +0200256 DISABLED_CallSessionFileRotatingStreamTest
phoglundbb738732016-07-15 03:57:12 -0700257#else
258#define MAYBE_CallSessionFileRotatingStreamTest \
Yves Gerey665174f2018-06-19 15:03:05 +0200259 CallSessionFileRotatingStreamTest
phoglundbb738732016-07-15 03:57:12 -0700260#endif
261
262class MAYBE_CallSessionFileRotatingStreamTest : public ::testing::Test {
tkchin93411912015-07-22 12:12:17 -0700263 protected:
264 void Init(const std::string& dir_name, size_t max_total_log_size) {
nisse57efb032017-05-18 03:55:59 -0700265 dir_path_ = webrtc::test::OutputPath();
266
tkchin93411912015-07-22 12:12:17 -0700267 // Append per-test output path in order to run within gtest parallel.
nisse57efb032017-05-18 03:55:59 -0700268 dir_path_.append(dir_name);
Niels Möller7b3c76b2018-11-07 09:54:28 +0100269 dir_path_.append(webrtc::test::kPathDelimiter);
nisse57efb032017-05-18 03:55:59 -0700270 ASSERT_TRUE(webrtc::test::CreateDir(dir_path_));
tkchin93411912015-07-22 12:12:17 -0700271 stream_.reset(
272 new CallSessionFileRotatingStream(dir_path_, max_total_log_size));
273 }
274
Steve Anton9de3aac2017-10-24 10:08:26 -0700275 void TearDown() override {
nisse57efb032017-05-18 03:55:59 -0700276 // On windows, open files can't be removed.
277 stream_->Close();
278 CleanupLogDirectory(*stream_);
279 EXPECT_TRUE(webrtc::test::RemoveDir(dir_path_));
280
tkchin93411912015-07-22 12:12:17 -0700281 stream_.reset();
tkchin93411912015-07-22 12:12:17 -0700282 }
283
284 // Writes the data to the stream and flushes it.
285 void WriteAndFlush(const void* data, const size_t data_len) {
286 EXPECT_EQ(SR_SUCCESS, stream_->WriteAll(data, data_len, nullptr, nullptr));
287 EXPECT_TRUE(stream_->Flush());
288 }
289
290 // Checks that the stream reads in the expected contents and then returns an
291 // end of stream result.
292 void VerifyStreamRead(const char* expected_contents,
293 const size_t expected_length,
294 const std::string& dir_path) {
jbauch555604a2016-04-26 03:13:22 -0700295 std::unique_ptr<CallSessionFileRotatingStream> stream(
tkchin93411912015-07-22 12:12:17 -0700296 new CallSessionFileRotatingStream(dir_path));
297 ASSERT_TRUE(stream->Open());
tkchin28bae022015-07-23 12:27:02 -0700298 size_t read = 0;
299 size_t stream_size = 0;
300 EXPECT_TRUE(stream->GetSize(&stream_size));
jbauch555604a2016-04-26 03:13:22 -0700301 std::unique_ptr<uint8_t[]> buffer(new uint8_t[expected_length]);
tkchin93411912015-07-22 12:12:17 -0700302 EXPECT_EQ(SR_SUCCESS,
tkchin28bae022015-07-23 12:27:02 -0700303 stream->ReadAll(buffer.get(), expected_length, &read, nullptr));
tkchin93411912015-07-22 12:12:17 -0700304 EXPECT_EQ(0, memcmp(expected_contents, buffer.get(), expected_length));
305 EXPECT_EQ(SR_EOS, stream->ReadAll(buffer.get(), 1, nullptr, nullptr));
tkchin28bae022015-07-23 12:27:02 -0700306 EXPECT_EQ(stream_size, read);
tkchin93411912015-07-22 12:12:17 -0700307 }
308
jbauch555604a2016-04-26 03:13:22 -0700309 std::unique_ptr<CallSessionFileRotatingStream> stream_;
tkchin93411912015-07-22 12:12:17 -0700310 std::string dir_path_;
311};
312
313// Tests that writing and reading to a stream with the smallest possible
314// capacity works.
phoglundbb738732016-07-15 03:57:12 -0700315TEST_F(MAYBE_CallSessionFileRotatingStreamTest, WriteAndReadSmallest) {
tkchin93411912015-07-22 12:12:17 -0700316 Init("CallSessionFileRotatingStreamTestWriteAndReadSmallest", 4);
317
318 ASSERT_TRUE(stream_->Open());
319 std::string message("abcde");
320 WriteAndFlush(message.c_str(), message.size());
321 std::string expected_contents("abe");
322 VerifyStreamRead(expected_contents.c_str(), expected_contents.size(),
323 dir_path_);
324}
325
326// Tests that writing and reading to a stream with capacity lesser than 4MB
327// behaves correctly.
phoglundbb738732016-07-15 03:57:12 -0700328TEST_F(MAYBE_CallSessionFileRotatingStreamTest, WriteAndReadSmall) {
tkchin93411912015-07-22 12:12:17 -0700329 Init("CallSessionFileRotatingStreamTestWriteAndReadSmall", 8);
330
331 ASSERT_TRUE(stream_->Open());
332 std::string message("123456789");
333 WriteAndFlush(message.c_str(), message.size());
334 std::string expected_contents("1234789");
335 VerifyStreamRead(expected_contents.c_str(), expected_contents.size(),
336 dir_path_);
337}
338
339// Tests that writing and reading to a stream with capacity greater than 4MB
340// behaves correctly.
phoglundbb738732016-07-15 03:57:12 -0700341TEST_F(MAYBE_CallSessionFileRotatingStreamTest, WriteAndReadLarge) {
tkchin93411912015-07-22 12:12:17 -0700342 Init("CallSessionFileRotatingStreamTestWriteAndReadLarge", 6 * 1024 * 1024);
343
344 ASSERT_TRUE(stream_->Open());
345 const size_t buffer_size = 1024 * 1024;
jbauch555604a2016-04-26 03:13:22 -0700346 std::unique_ptr<uint8_t[]> buffer(new uint8_t[buffer_size]);
tkchin93411912015-07-22 12:12:17 -0700347 for (int i = 0; i < 8; i++) {
348 memset(buffer.get(), i, buffer_size);
349 EXPECT_EQ(SR_SUCCESS,
350 stream_->WriteAll(buffer.get(), buffer_size, nullptr, nullptr));
351 }
352
353 stream_.reset(new CallSessionFileRotatingStream(dir_path_));
354 ASSERT_TRUE(stream_->Open());
jbauch555604a2016-04-26 03:13:22 -0700355 std::unique_ptr<uint8_t[]> expected_buffer(new uint8_t[buffer_size]);
tkchin93411912015-07-22 12:12:17 -0700356 int expected_vals[] = {0, 1, 2, 6, 7};
357 for (size_t i = 0; i < arraysize(expected_vals); ++i) {
358 memset(expected_buffer.get(), expected_vals[i], buffer_size);
359 EXPECT_EQ(SR_SUCCESS,
360 stream_->ReadAll(buffer.get(), buffer_size, nullptr, nullptr));
361 EXPECT_EQ(0, memcmp(buffer.get(), expected_buffer.get(), buffer_size));
362 }
363 EXPECT_EQ(SR_EOS, stream_->ReadAll(buffer.get(), 1, nullptr, nullptr));
364}
365
366// Tests that writing and reading to a stream where only the first file is
367// written to behaves correctly.
phoglundbb738732016-07-15 03:57:12 -0700368TEST_F(MAYBE_CallSessionFileRotatingStreamTest, WriteAndReadFirstHalf) {
tkchin93411912015-07-22 12:12:17 -0700369 Init("CallSessionFileRotatingStreamTestWriteAndReadFirstHalf",
370 6 * 1024 * 1024);
371 ASSERT_TRUE(stream_->Open());
372 const size_t buffer_size = 1024 * 1024;
jbauch555604a2016-04-26 03:13:22 -0700373 std::unique_ptr<uint8_t[]> buffer(new uint8_t[buffer_size]);
tkchin93411912015-07-22 12:12:17 -0700374 for (int i = 0; i < 2; i++) {
375 memset(buffer.get(), i, buffer_size);
376 EXPECT_EQ(SR_SUCCESS,
377 stream_->WriteAll(buffer.get(), buffer_size, nullptr, nullptr));
378 }
379
380 stream_.reset(new CallSessionFileRotatingStream(dir_path_));
381 ASSERT_TRUE(stream_->Open());
jbauch555604a2016-04-26 03:13:22 -0700382 std::unique_ptr<uint8_t[]> expected_buffer(new uint8_t[buffer_size]);
tkchin93411912015-07-22 12:12:17 -0700383 int expected_vals[] = {0, 1};
384 for (size_t i = 0; i < arraysize(expected_vals); ++i) {
385 memset(expected_buffer.get(), expected_vals[i], buffer_size);
386 EXPECT_EQ(SR_SUCCESS,
387 stream_->ReadAll(buffer.get(), buffer_size, nullptr, nullptr));
388 EXPECT_EQ(0, memcmp(buffer.get(), expected_buffer.get(), buffer_size));
389 }
390 EXPECT_EQ(SR_EOS, stream_->ReadAll(buffer.get(), 1, nullptr, nullptr));
391}
392
393} // namespace rtc