blob: 7ca7e3ecc74ade35ec900346ca59fd6fe7592126 [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"
16#include "rtc_base/fileutils.h"
17#include "rtc_base/gunit.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020018#include "test/testsupport/fileutils.h"
tkchin93411912015-07-22 12:12:17 -070019
20namespace rtc {
21
nisse57efb032017-05-18 03:55:59 -070022namespace {
23
24void CleanupLogDirectory(const FileRotatingStream& stream) {
25 for (size_t i = 0; i < stream.GetNumFiles(); ++i) {
26 // Ignore return value, not all files are expected to exist.
27 webrtc::test::RemoveFile(stream.GetFilePath(i));
28 }
29}
30
31} // namespace
32
Yves Gerey665174f2018-06-19 15:03:05 +020033#if defined(WEBRTC_ANDROID)
phoglundbb738732016-07-15 03:57:12 -070034// Fails on Android: https://bugs.chromium.org/p/webrtc/issues/detail?id=4364.
35#define MAYBE_FileRotatingStreamTest DISABLED_FileRotatingStreamTest
36#else
37#define MAYBE_FileRotatingStreamTest FileRotatingStreamTest
38#endif
39
40class MAYBE_FileRotatingStreamTest : public ::testing::Test {
tkchin93411912015-07-22 12:12:17 -070041 protected:
42 static const char* kFilePrefix;
43 static const size_t kMaxFileSize;
44
45 void Init(const std::string& dir_name,
46 const std::string& file_prefix,
47 size_t max_file_size,
Niels Möller7b3c76b2018-11-07 09:54:28 +010048 size_t num_log_files,
49 bool ensure_trailing_delimiter = true) {
nisse57efb032017-05-18 03:55:59 -070050 dir_path_ = webrtc::test::OutputPath();
51
tkchin93411912015-07-22 12:12:17 -070052 // Append per-test output path in order to run within gtest parallel.
nisse57efb032017-05-18 03:55:59 -070053 dir_path_.append(dir_name);
Niels Möller7b3c76b2018-11-07 09:54:28 +010054 if (ensure_trailing_delimiter) {
55 dir_path_.append(webrtc::test::kPathDelimiter);
56 }
nisse57efb032017-05-18 03:55:59 -070057 ASSERT_TRUE(webrtc::test::CreateDir(dir_path_));
tkchin93411912015-07-22 12:12:17 -070058 stream_.reset(new FileRotatingStream(dir_path_, file_prefix, max_file_size,
59 num_log_files));
60 }
61
62 void TearDown() override {
nisse57efb032017-05-18 03:55:59 -070063 // On windows, open files can't be removed.
64 stream_->Close();
65 CleanupLogDirectory(*stream_);
66 EXPECT_TRUE(webrtc::test::RemoveDir(dir_path_));
67
tkchin93411912015-07-22 12:12:17 -070068 stream_.reset();
tkchin93411912015-07-22 12:12:17 -070069 }
70
71 // Writes the data to the stream and flushes it.
72 void WriteAndFlush(const void* data, const size_t data_len) {
73 EXPECT_EQ(SR_SUCCESS, stream_->WriteAll(data, data_len, nullptr, nullptr));
74 EXPECT_TRUE(stream_->Flush());
75 }
76
77 // Checks that the stream reads in the expected contents and then returns an
78 // end of stream result.
79 void VerifyStreamRead(const char* expected_contents,
80 const size_t expected_length,
81 const std::string& dir_path,
82 const char* file_prefix) {
jbauch555604a2016-04-26 03:13:22 -070083 std::unique_ptr<FileRotatingStream> stream;
tkchin93411912015-07-22 12:12:17 -070084 stream.reset(new FileRotatingStream(dir_path, file_prefix));
85 ASSERT_TRUE(stream->Open());
tkchin28bae022015-07-23 12:27:02 -070086 size_t read = 0;
87 size_t stream_size = 0;
88 EXPECT_TRUE(stream->GetSize(&stream_size));
jbauch555604a2016-04-26 03:13:22 -070089 std::unique_ptr<uint8_t[]> buffer(new uint8_t[expected_length]);
tkchin93411912015-07-22 12:12:17 -070090 EXPECT_EQ(SR_SUCCESS,
tkchin28bae022015-07-23 12:27:02 -070091 stream->ReadAll(buffer.get(), expected_length, &read, nullptr));
tkchin93411912015-07-22 12:12:17 -070092 EXPECT_EQ(0, memcmp(expected_contents, buffer.get(), expected_length));
93 EXPECT_EQ(SR_EOS, stream->ReadAll(buffer.get(), 1, nullptr, nullptr));
tkchin28bae022015-07-23 12:27:02 -070094 EXPECT_EQ(stream_size, read);
tkchin93411912015-07-22 12:12:17 -070095 }
96
97 void VerifyFileContents(const char* expected_contents,
98 const size_t expected_length,
99 const std::string& file_path) {
jbauch555604a2016-04-26 03:13:22 -0700100 std::unique_ptr<uint8_t[]> buffer(new uint8_t[expected_length]);
nisse21083262017-05-31 02:07:21 -0700101 FileStream stream;
102 ASSERT_TRUE(stream.Open(file_path, "r", nullptr));
tkchin93411912015-07-22 12:12:17 -0700103 EXPECT_EQ(rtc::SR_SUCCESS,
nisse21083262017-05-31 02:07:21 -0700104 stream.ReadAll(buffer.get(), expected_length, nullptr, nullptr));
tkchin93411912015-07-22 12:12:17 -0700105 EXPECT_EQ(0, memcmp(expected_contents, buffer.get(), expected_length));
106 size_t file_size = 0;
nisse21083262017-05-31 02:07:21 -0700107 EXPECT_TRUE(stream.GetSize(&file_size));
tkchin93411912015-07-22 12:12:17 -0700108 EXPECT_EQ(file_size, expected_length);
109 }
110
jbauch555604a2016-04-26 03:13:22 -0700111 std::unique_ptr<FileRotatingStream> stream_;
tkchin93411912015-07-22 12:12:17 -0700112 std::string dir_path_;
113};
114
phoglundbb738732016-07-15 03:57:12 -0700115const char* MAYBE_FileRotatingStreamTest::kFilePrefix =
116 "FileRotatingStreamTest";
117const size_t MAYBE_FileRotatingStreamTest::kMaxFileSize = 2;
tkchin93411912015-07-22 12:12:17 -0700118
119// Tests that stream state is correct before and after Open / Close.
phoglundbb738732016-07-15 03:57:12 -0700120TEST_F(MAYBE_FileRotatingStreamTest, State) {
tkchin93411912015-07-22 12:12:17 -0700121 Init("FileRotatingStreamTestState", kFilePrefix, kMaxFileSize, 3);
122
123 EXPECT_EQ(SS_CLOSED, stream_->GetState());
124 ASSERT_TRUE(stream_->Open());
125 EXPECT_EQ(SS_OPEN, stream_->GetState());
126 stream_->Close();
127 EXPECT_EQ(SS_CLOSED, stream_->GetState());
128}
129
130// Tests that nothing is written to file when data of length zero is written.
phoglundbb738732016-07-15 03:57:12 -0700131TEST_F(MAYBE_FileRotatingStreamTest, EmptyWrite) {
tkchin93411912015-07-22 12:12:17 -0700132 Init("FileRotatingStreamTestEmptyWrite", kFilePrefix, kMaxFileSize, 3);
133
134 ASSERT_TRUE(stream_->Open());
135 WriteAndFlush("a", 0);
136
137 std::string logfile_path = stream_->GetFilePath(0);
nisse21083262017-05-31 02:07:21 -0700138 FileStream stream;
139 ASSERT_TRUE(stream.Open(logfile_path, "r", nullptr));
tkchin93411912015-07-22 12:12:17 -0700140 size_t file_size = 0;
nisse21083262017-05-31 02:07:21 -0700141 EXPECT_TRUE(stream.GetSize(&file_size));
tkchin93411912015-07-22 12:12:17 -0700142 EXPECT_EQ(0u, file_size);
143}
144
145// Tests that a write operation followed by a read returns the expected data
146// and writes to the expected files.
phoglundbb738732016-07-15 03:57:12 -0700147TEST_F(MAYBE_FileRotatingStreamTest, WriteAndRead) {
tkchin93411912015-07-22 12:12:17 -0700148 Init("FileRotatingStreamTestWriteAndRead", kFilePrefix, kMaxFileSize, 3);
149
150 ASSERT_TRUE(stream_->Open());
151 // The test is set up to create three log files of length 2. Write and check
152 // contents.
153 std::string messages[3] = {"aa", "bb", "cc"};
154 for (size_t i = 0; i < arraysize(messages); ++i) {
155 const std::string& message = messages[i];
156 WriteAndFlush(message.c_str(), message.size());
157 // Since the max log size is 2, we will be causing rotation. Read from the
158 // next file.
159 VerifyFileContents(message.c_str(), message.size(),
160 stream_->GetFilePath(1));
161 }
162 // Check that exactly three files exist.
163 for (size_t i = 0; i < arraysize(messages); ++i) {
164 EXPECT_TRUE(Filesystem::IsFile(stream_->GetFilePath(i)));
165 }
166 std::string message("d");
167 WriteAndFlush(message.c_str(), message.size());
168 for (size_t i = 0; i < arraysize(messages); ++i) {
169 EXPECT_TRUE(Filesystem::IsFile(stream_->GetFilePath(i)));
170 }
171 // TODO(tkchin): Maybe check all the files in the dir.
172
173 // Reopen for read.
174 std::string expected_contents("bbccd");
175 VerifyStreamRead(expected_contents.c_str(), expected_contents.size(),
176 dir_path_, kFilePrefix);
177}
178
Niels Möller7b3c76b2018-11-07 09:54:28 +0100179// Tests that a write operation (with dir name without delimiter) followed by a
180// read returns the expected data and writes to the expected files.
181TEST_F(MAYBE_FileRotatingStreamTest, WriteWithoutDelimiterAndRead) {
182 Init("FileRotatingStreamTestWriteWithoutDelimiterAndRead", kFilePrefix,
183 kMaxFileSize, 3,
184 /* ensure_trailing_delimiter*/ false);
185
186 ASSERT_TRUE(stream_->Open());
187 // The test is set up to create three log files of length 2. Write and check
188 // contents.
189 std::string messages[3] = {"aa", "bb", "cc"};
190 for (size_t i = 0; i < arraysize(messages); ++i) {
191 const std::string& message = messages[i];
192 WriteAndFlush(message.c_str(), message.size());
193 }
194 std::string message("d");
195 WriteAndFlush(message.c_str(), message.size());
196
197 // Reopen for read.
198 std::string expected_contents("bbccd");
199 VerifyStreamRead(expected_contents.c_str(), expected_contents.size(),
200 dir_path_ + webrtc::test::kPathDelimiter, kFilePrefix);
201}
202
203// Tests that a write operation followed by a read (without trailing delimiter)
204// returns the expected data and writes to the expected files.
205TEST_F(MAYBE_FileRotatingStreamTest, WriteAndReadWithoutDelimiter) {
206 Init("FileRotatingStreamTestWriteAndReadWithoutDelimiter", kFilePrefix,
207 kMaxFileSize, 3);
208
209 ASSERT_TRUE(stream_->Open());
210 // The test is set up to create three log files of length 2. Write and check
211 // contents.
212 std::string messages[3] = {"aa", "bb", "cc"};
213 for (size_t i = 0; i < arraysize(messages); ++i) {
214 const std::string& message = messages[i];
215 WriteAndFlush(message.c_str(), message.size());
216 }
217 std::string message("d");
218 WriteAndFlush(message.c_str(), message.size());
219
220 // Reopen for read.
221 std::string expected_contents("bbccd");
222 VerifyStreamRead(expected_contents.c_str(), expected_contents.size(),
223 dir_path_.substr(0, dir_path_.size() - 1), kFilePrefix);
224}
225
tkchin93411912015-07-22 12:12:17 -0700226// Tests that writing data greater than the total capacity of the files
227// overwrites the files correctly and is read correctly after.
phoglundbb738732016-07-15 03:57:12 -0700228TEST_F(MAYBE_FileRotatingStreamTest, WriteOverflowAndRead) {
tkchin93411912015-07-22 12:12:17 -0700229 Init("FileRotatingStreamTestWriteOverflowAndRead", kFilePrefix, kMaxFileSize,
230 3);
231 ASSERT_TRUE(stream_->Open());
232 // This should cause overflow across all three files, such that the first file
233 // we wrote to also gets overwritten.
234 std::string message("foobarbaz");
235 WriteAndFlush(message.c_str(), message.size());
236 std::string expected_file_contents("z");
237 VerifyFileContents(expected_file_contents.c_str(),
238 expected_file_contents.size(), stream_->GetFilePath(0));
239 std::string expected_stream_contents("arbaz");
240 VerifyStreamRead(expected_stream_contents.c_str(),
241 expected_stream_contents.size(), dir_path_, kFilePrefix);
242}
243
244// Tests that the returned file paths have the right folder and prefix.
phoglundbb738732016-07-15 03:57:12 -0700245TEST_F(MAYBE_FileRotatingStreamTest, GetFilePath) {
tkchin93411912015-07-22 12:12:17 -0700246 Init("FileRotatingStreamTestGetFilePath", kFilePrefix, kMaxFileSize, 20);
Niels Möller394b4eb2018-06-01 14:08:25 +0200247 // dir_path_ includes a trailing delimiter.
248 const std::string prefix = dir_path_ + kFilePrefix;
tkchin93411912015-07-22 12:12:17 -0700249 for (auto i = 0; i < 20; ++i) {
Niels Möller394b4eb2018-06-01 14:08:25 +0200250 EXPECT_EQ(0, stream_->GetFilePath(i).compare(0, prefix.size(), prefix));
tkchin93411912015-07-22 12:12:17 -0700251 }
252}
253
Yves Gerey665174f2018-06-19 15:03:05 +0200254#if defined(WEBRTC_ANDROID)
phoglundbb738732016-07-15 03:57:12 -0700255// Fails on Android: https://bugs.chromium.org/p/webrtc/issues/detail?id=4364.
256#define MAYBE_CallSessionFileRotatingStreamTest \
Yves Gerey665174f2018-06-19 15:03:05 +0200257 DISABLED_CallSessionFileRotatingStreamTest
phoglundbb738732016-07-15 03:57:12 -0700258#else
259#define MAYBE_CallSessionFileRotatingStreamTest \
Yves Gerey665174f2018-06-19 15:03:05 +0200260 CallSessionFileRotatingStreamTest
phoglundbb738732016-07-15 03:57:12 -0700261#endif
262
263class MAYBE_CallSessionFileRotatingStreamTest : public ::testing::Test {
tkchin93411912015-07-22 12:12:17 -0700264 protected:
265 void Init(const std::string& dir_name, size_t max_total_log_size) {
nisse57efb032017-05-18 03:55:59 -0700266 dir_path_ = webrtc::test::OutputPath();
267
tkchin93411912015-07-22 12:12:17 -0700268 // Append per-test output path in order to run within gtest parallel.
nisse57efb032017-05-18 03:55:59 -0700269 dir_path_.append(dir_name);
Niels Möller7b3c76b2018-11-07 09:54:28 +0100270 dir_path_.append(webrtc::test::kPathDelimiter);
nisse57efb032017-05-18 03:55:59 -0700271 ASSERT_TRUE(webrtc::test::CreateDir(dir_path_));
tkchin93411912015-07-22 12:12:17 -0700272 stream_.reset(
273 new CallSessionFileRotatingStream(dir_path_, max_total_log_size));
274 }
275
Steve Anton9de3aac2017-10-24 10:08:26 -0700276 void TearDown() override {
nisse57efb032017-05-18 03:55:59 -0700277 // On windows, open files can't be removed.
278 stream_->Close();
279 CleanupLogDirectory(*stream_);
280 EXPECT_TRUE(webrtc::test::RemoveDir(dir_path_));
281
tkchin93411912015-07-22 12:12:17 -0700282 stream_.reset();
tkchin93411912015-07-22 12:12:17 -0700283 }
284
285 // Writes the data to the stream and flushes it.
286 void WriteAndFlush(const void* data, const size_t data_len) {
287 EXPECT_EQ(SR_SUCCESS, stream_->WriteAll(data, data_len, nullptr, nullptr));
288 EXPECT_TRUE(stream_->Flush());
289 }
290
291 // Checks that the stream reads in the expected contents and then returns an
292 // end of stream result.
293 void VerifyStreamRead(const char* expected_contents,
294 const size_t expected_length,
295 const std::string& dir_path) {
jbauch555604a2016-04-26 03:13:22 -0700296 std::unique_ptr<CallSessionFileRotatingStream> stream(
tkchin93411912015-07-22 12:12:17 -0700297 new CallSessionFileRotatingStream(dir_path));
298 ASSERT_TRUE(stream->Open());
tkchin28bae022015-07-23 12:27:02 -0700299 size_t read = 0;
300 size_t stream_size = 0;
301 EXPECT_TRUE(stream->GetSize(&stream_size));
jbauch555604a2016-04-26 03:13:22 -0700302 std::unique_ptr<uint8_t[]> buffer(new uint8_t[expected_length]);
tkchin93411912015-07-22 12:12:17 -0700303 EXPECT_EQ(SR_SUCCESS,
tkchin28bae022015-07-23 12:27:02 -0700304 stream->ReadAll(buffer.get(), expected_length, &read, nullptr));
tkchin93411912015-07-22 12:12:17 -0700305 EXPECT_EQ(0, memcmp(expected_contents, buffer.get(), expected_length));
306 EXPECT_EQ(SR_EOS, stream->ReadAll(buffer.get(), 1, nullptr, nullptr));
tkchin28bae022015-07-23 12:27:02 -0700307 EXPECT_EQ(stream_size, read);
tkchin93411912015-07-22 12:12:17 -0700308 }
309
jbauch555604a2016-04-26 03:13:22 -0700310 std::unique_ptr<CallSessionFileRotatingStream> stream_;
tkchin93411912015-07-22 12:12:17 -0700311 std::string dir_path_;
312};
313
314// Tests that writing and reading to a stream with the smallest possible
315// capacity works.
phoglundbb738732016-07-15 03:57:12 -0700316TEST_F(MAYBE_CallSessionFileRotatingStreamTest, WriteAndReadSmallest) {
tkchin93411912015-07-22 12:12:17 -0700317 Init("CallSessionFileRotatingStreamTestWriteAndReadSmallest", 4);
318
319 ASSERT_TRUE(stream_->Open());
320 std::string message("abcde");
321 WriteAndFlush(message.c_str(), message.size());
322 std::string expected_contents("abe");
323 VerifyStreamRead(expected_contents.c_str(), expected_contents.size(),
324 dir_path_);
325}
326
327// Tests that writing and reading to a stream with capacity lesser than 4MB
328// behaves correctly.
phoglundbb738732016-07-15 03:57:12 -0700329TEST_F(MAYBE_CallSessionFileRotatingStreamTest, WriteAndReadSmall) {
tkchin93411912015-07-22 12:12:17 -0700330 Init("CallSessionFileRotatingStreamTestWriteAndReadSmall", 8);
331
332 ASSERT_TRUE(stream_->Open());
333 std::string message("123456789");
334 WriteAndFlush(message.c_str(), message.size());
335 std::string expected_contents("1234789");
336 VerifyStreamRead(expected_contents.c_str(), expected_contents.size(),
337 dir_path_);
338}
339
340// Tests that writing and reading to a stream with capacity greater than 4MB
341// behaves correctly.
phoglundbb738732016-07-15 03:57:12 -0700342TEST_F(MAYBE_CallSessionFileRotatingStreamTest, WriteAndReadLarge) {
tkchin93411912015-07-22 12:12:17 -0700343 Init("CallSessionFileRotatingStreamTestWriteAndReadLarge", 6 * 1024 * 1024);
344
345 ASSERT_TRUE(stream_->Open());
346 const size_t buffer_size = 1024 * 1024;
jbauch555604a2016-04-26 03:13:22 -0700347 std::unique_ptr<uint8_t[]> buffer(new uint8_t[buffer_size]);
tkchin93411912015-07-22 12:12:17 -0700348 for (int i = 0; i < 8; i++) {
349 memset(buffer.get(), i, buffer_size);
350 EXPECT_EQ(SR_SUCCESS,
351 stream_->WriteAll(buffer.get(), buffer_size, nullptr, nullptr));
352 }
353
354 stream_.reset(new CallSessionFileRotatingStream(dir_path_));
355 ASSERT_TRUE(stream_->Open());
jbauch555604a2016-04-26 03:13:22 -0700356 std::unique_ptr<uint8_t[]> expected_buffer(new uint8_t[buffer_size]);
tkchin93411912015-07-22 12:12:17 -0700357 int expected_vals[] = {0, 1, 2, 6, 7};
358 for (size_t i = 0; i < arraysize(expected_vals); ++i) {
359 memset(expected_buffer.get(), expected_vals[i], buffer_size);
360 EXPECT_EQ(SR_SUCCESS,
361 stream_->ReadAll(buffer.get(), buffer_size, nullptr, nullptr));
362 EXPECT_EQ(0, memcmp(buffer.get(), expected_buffer.get(), buffer_size));
363 }
364 EXPECT_EQ(SR_EOS, stream_->ReadAll(buffer.get(), 1, nullptr, nullptr));
365}
366
367// Tests that writing and reading to a stream where only the first file is
368// written to behaves correctly.
phoglundbb738732016-07-15 03:57:12 -0700369TEST_F(MAYBE_CallSessionFileRotatingStreamTest, WriteAndReadFirstHalf) {
tkchin93411912015-07-22 12:12:17 -0700370 Init("CallSessionFileRotatingStreamTestWriteAndReadFirstHalf",
371 6 * 1024 * 1024);
372 ASSERT_TRUE(stream_->Open());
373 const size_t buffer_size = 1024 * 1024;
jbauch555604a2016-04-26 03:13:22 -0700374 std::unique_ptr<uint8_t[]> buffer(new uint8_t[buffer_size]);
tkchin93411912015-07-22 12:12:17 -0700375 for (int i = 0; i < 2; i++) {
376 memset(buffer.get(), i, buffer_size);
377 EXPECT_EQ(SR_SUCCESS,
378 stream_->WriteAll(buffer.get(), buffer_size, nullptr, nullptr));
379 }
380
381 stream_.reset(new CallSessionFileRotatingStream(dir_path_));
382 ASSERT_TRUE(stream_->Open());
jbauch555604a2016-04-26 03:13:22 -0700383 std::unique_ptr<uint8_t[]> expected_buffer(new uint8_t[buffer_size]);
tkchin93411912015-07-22 12:12:17 -0700384 int expected_vals[] = {0, 1};
385 for (size_t i = 0; i < arraysize(expected_vals); ++i) {
386 memset(expected_buffer.get(), expected_vals[i], buffer_size);
387 EXPECT_EQ(SR_SUCCESS,
388 stream_->ReadAll(buffer.get(), buffer_size, nullptr, nullptr));
389 EXPECT_EQ(0, memcmp(buffer.get(), expected_buffer.get(), buffer_size));
390 }
391 EXPECT_EQ(SR_EOS, stream_->ReadAll(buffer.get(), 1, nullptr, nullptr));
392}
393
394} // namespace rtc