blob: 19055162fd01945d5bcadf2c1f7d9ba09f536c89 [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"
18#include "rtc_base/pathutils.h"
19#include "test/testsupport/fileutils.h"
tkchin93411912015-07-22 12:12:17 -070020
21namespace rtc {
22
nisse57efb032017-05-18 03:55:59 -070023namespace {
24
25void 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
Yves Gerey665174f2018-06-19 15:03:05 +020034#if defined(WEBRTC_ANDROID)
phoglundbb738732016-07-15 03:57:12 -070035// 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
41class MAYBE_FileRotatingStreamTest : public ::testing::Test {
tkchin93411912015-07-22 12:12:17 -070042 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) {
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);
54 dir_path_.push_back(Pathname::DefaultFolderDelimiter());
55 ASSERT_TRUE(webrtc::test::CreateDir(dir_path_));
tkchin93411912015-07-22 12:12:17 -070056 stream_.reset(new FileRotatingStream(dir_path_, file_prefix, max_file_size,
57 num_log_files));
58 }
59
60 void TearDown() override {
nisse57efb032017-05-18 03:55:59 -070061 // On windows, open files can't be removed.
62 stream_->Close();
63 CleanupLogDirectory(*stream_);
64 EXPECT_TRUE(webrtc::test::RemoveDir(dir_path_));
65
tkchin93411912015-07-22 12:12:17 -070066 stream_.reset();
tkchin93411912015-07-22 12:12:17 -070067 }
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) {
jbauch555604a2016-04-26 03:13:22 -070081 std::unique_ptr<FileRotatingStream> stream;
tkchin93411912015-07-22 12:12:17 -070082 stream.reset(new FileRotatingStream(dir_path, file_prefix));
83 ASSERT_TRUE(stream->Open());
tkchin28bae022015-07-23 12:27:02 -070084 size_t read = 0;
85 size_t stream_size = 0;
86 EXPECT_TRUE(stream->GetSize(&stream_size));
jbauch555604a2016-04-26 03:13:22 -070087 std::unique_ptr<uint8_t[]> buffer(new uint8_t[expected_length]);
tkchin93411912015-07-22 12:12:17 -070088 EXPECT_EQ(SR_SUCCESS,
tkchin28bae022015-07-23 12:27:02 -070089 stream->ReadAll(buffer.get(), expected_length, &read, nullptr));
tkchin93411912015-07-22 12:12:17 -070090 EXPECT_EQ(0, memcmp(expected_contents, buffer.get(), expected_length));
91 EXPECT_EQ(SR_EOS, stream->ReadAll(buffer.get(), 1, nullptr, nullptr));
tkchin28bae022015-07-23 12:27:02 -070092 EXPECT_EQ(stream_size, read);
tkchin93411912015-07-22 12:12:17 -070093 }
94
95 void VerifyFileContents(const char* expected_contents,
96 const size_t expected_length,
97 const std::string& file_path) {
jbauch555604a2016-04-26 03:13:22 -070098 std::unique_ptr<uint8_t[]> buffer(new uint8_t[expected_length]);
nisse21083262017-05-31 02:07:21 -070099 FileStream stream;
100 ASSERT_TRUE(stream.Open(file_path, "r", nullptr));
tkchin93411912015-07-22 12:12:17 -0700101 EXPECT_EQ(rtc::SR_SUCCESS,
nisse21083262017-05-31 02:07:21 -0700102 stream.ReadAll(buffer.get(), expected_length, nullptr, nullptr));
tkchin93411912015-07-22 12:12:17 -0700103 EXPECT_EQ(0, memcmp(expected_contents, buffer.get(), expected_length));
104 size_t file_size = 0;
nisse21083262017-05-31 02:07:21 -0700105 EXPECT_TRUE(stream.GetSize(&file_size));
tkchin93411912015-07-22 12:12:17 -0700106 EXPECT_EQ(file_size, expected_length);
107 }
108
jbauch555604a2016-04-26 03:13:22 -0700109 std::unique_ptr<FileRotatingStream> stream_;
tkchin93411912015-07-22 12:12:17 -0700110 std::string dir_path_;
111};
112
phoglundbb738732016-07-15 03:57:12 -0700113const char* MAYBE_FileRotatingStreamTest::kFilePrefix =
114 "FileRotatingStreamTest";
115const size_t MAYBE_FileRotatingStreamTest::kMaxFileSize = 2;
tkchin93411912015-07-22 12:12:17 -0700116
117// Tests that stream state is correct before and after Open / Close.
phoglundbb738732016-07-15 03:57:12 -0700118TEST_F(MAYBE_FileRotatingStreamTest, State) {
tkchin93411912015-07-22 12:12:17 -0700119 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.
phoglundbb738732016-07-15 03:57:12 -0700129TEST_F(MAYBE_FileRotatingStreamTest, EmptyWrite) {
tkchin93411912015-07-22 12:12:17 -0700130 Init("FileRotatingStreamTestEmptyWrite", kFilePrefix, kMaxFileSize, 3);
131
132 ASSERT_TRUE(stream_->Open());
133 WriteAndFlush("a", 0);
134
135 std::string logfile_path = stream_->GetFilePath(0);
nisse21083262017-05-31 02:07:21 -0700136 FileStream stream;
137 ASSERT_TRUE(stream.Open(logfile_path, "r", nullptr));
tkchin93411912015-07-22 12:12:17 -0700138 size_t file_size = 0;
nisse21083262017-05-31 02:07:21 -0700139 EXPECT_TRUE(stream.GetSize(&file_size));
tkchin93411912015-07-22 12:12:17 -0700140 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.
phoglundbb738732016-07-15 03:57:12 -0700145TEST_F(MAYBE_FileRotatingStreamTest, WriteAndRead) {
tkchin93411912015-07-22 12:12:17 -0700146 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.
phoglundbb738732016-07-15 03:57:12 -0700179TEST_F(MAYBE_FileRotatingStreamTest, WriteOverflowAndRead) {
tkchin93411912015-07-22 12:12:17 -0700180 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.
phoglundbb738732016-07-15 03:57:12 -0700196TEST_F(MAYBE_FileRotatingStreamTest, GetFilePath) {
tkchin93411912015-07-22 12:12:17 -0700197 Init("FileRotatingStreamTestGetFilePath", kFilePrefix, kMaxFileSize, 20);
Niels Möller394b4eb2018-06-01 14:08:25 +0200198 // dir_path_ includes a trailing delimiter.
199 const std::string prefix = dir_path_ + kFilePrefix;
tkchin93411912015-07-22 12:12:17 -0700200 for (auto i = 0; i < 20; ++i) {
Niels Möller394b4eb2018-06-01 14:08:25 +0200201 EXPECT_EQ(0, stream_->GetFilePath(i).compare(0, prefix.size(), prefix));
tkchin93411912015-07-22 12:12:17 -0700202 }
203}
204
Yves Gerey665174f2018-06-19 15:03:05 +0200205#if defined(WEBRTC_ANDROID)
phoglundbb738732016-07-15 03:57:12 -0700206// Fails on Android: https://bugs.chromium.org/p/webrtc/issues/detail?id=4364.
207#define MAYBE_CallSessionFileRotatingStreamTest \
Yves Gerey665174f2018-06-19 15:03:05 +0200208 DISABLED_CallSessionFileRotatingStreamTest
phoglundbb738732016-07-15 03:57:12 -0700209#else
210#define MAYBE_CallSessionFileRotatingStreamTest \
Yves Gerey665174f2018-06-19 15:03:05 +0200211 CallSessionFileRotatingStreamTest
phoglundbb738732016-07-15 03:57:12 -0700212#endif
213
214class MAYBE_CallSessionFileRotatingStreamTest : public ::testing::Test {
tkchin93411912015-07-22 12:12:17 -0700215 protected:
216 void Init(const std::string& dir_name, size_t max_total_log_size) {
nisse57efb032017-05-18 03:55:59 -0700217 dir_path_ = webrtc::test::OutputPath();
218
tkchin93411912015-07-22 12:12:17 -0700219 // Append per-test output path in order to run within gtest parallel.
nisse57efb032017-05-18 03:55:59 -0700220 dir_path_.append(dir_name);
221 dir_path_.push_back(Pathname::DefaultFolderDelimiter());
222 ASSERT_TRUE(webrtc::test::CreateDir(dir_path_));
tkchin93411912015-07-22 12:12:17 -0700223 stream_.reset(
224 new CallSessionFileRotatingStream(dir_path_, max_total_log_size));
225 }
226
Steve Anton9de3aac2017-10-24 10:08:26 -0700227 void TearDown() override {
nisse57efb032017-05-18 03:55:59 -0700228 // On windows, open files can't be removed.
229 stream_->Close();
230 CleanupLogDirectory(*stream_);
231 EXPECT_TRUE(webrtc::test::RemoveDir(dir_path_));
232
tkchin93411912015-07-22 12:12:17 -0700233 stream_.reset();
tkchin93411912015-07-22 12:12:17 -0700234 }
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) {
jbauch555604a2016-04-26 03:13:22 -0700247 std::unique_ptr<CallSessionFileRotatingStream> stream(
tkchin93411912015-07-22 12:12:17 -0700248 new CallSessionFileRotatingStream(dir_path));
249 ASSERT_TRUE(stream->Open());
tkchin28bae022015-07-23 12:27:02 -0700250 size_t read = 0;
251 size_t stream_size = 0;
252 EXPECT_TRUE(stream->GetSize(&stream_size));
jbauch555604a2016-04-26 03:13:22 -0700253 std::unique_ptr<uint8_t[]> buffer(new uint8_t[expected_length]);
tkchin93411912015-07-22 12:12:17 -0700254 EXPECT_EQ(SR_SUCCESS,
tkchin28bae022015-07-23 12:27:02 -0700255 stream->ReadAll(buffer.get(), expected_length, &read, nullptr));
tkchin93411912015-07-22 12:12:17 -0700256 EXPECT_EQ(0, memcmp(expected_contents, buffer.get(), expected_length));
257 EXPECT_EQ(SR_EOS, stream->ReadAll(buffer.get(), 1, nullptr, nullptr));
tkchin28bae022015-07-23 12:27:02 -0700258 EXPECT_EQ(stream_size, read);
tkchin93411912015-07-22 12:12:17 -0700259 }
260
jbauch555604a2016-04-26 03:13:22 -0700261 std::unique_ptr<CallSessionFileRotatingStream> stream_;
tkchin93411912015-07-22 12:12:17 -0700262 std::string dir_path_;
263};
264
265// Tests that writing and reading to a stream with the smallest possible
266// capacity works.
phoglundbb738732016-07-15 03:57:12 -0700267TEST_F(MAYBE_CallSessionFileRotatingStreamTest, WriteAndReadSmallest) {
tkchin93411912015-07-22 12:12:17 -0700268 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.
phoglundbb738732016-07-15 03:57:12 -0700280TEST_F(MAYBE_CallSessionFileRotatingStreamTest, WriteAndReadSmall) {
tkchin93411912015-07-22 12:12:17 -0700281 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.
phoglundbb738732016-07-15 03:57:12 -0700293TEST_F(MAYBE_CallSessionFileRotatingStreamTest, WriteAndReadLarge) {
tkchin93411912015-07-22 12:12:17 -0700294 Init("CallSessionFileRotatingStreamTestWriteAndReadLarge", 6 * 1024 * 1024);
295
296 ASSERT_TRUE(stream_->Open());
297 const size_t buffer_size = 1024 * 1024;
jbauch555604a2016-04-26 03:13:22 -0700298 std::unique_ptr<uint8_t[]> buffer(new uint8_t[buffer_size]);
tkchin93411912015-07-22 12:12:17 -0700299 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());
jbauch555604a2016-04-26 03:13:22 -0700307 std::unique_ptr<uint8_t[]> expected_buffer(new uint8_t[buffer_size]);
tkchin93411912015-07-22 12:12:17 -0700308 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.
phoglundbb738732016-07-15 03:57:12 -0700320TEST_F(MAYBE_CallSessionFileRotatingStreamTest, WriteAndReadFirstHalf) {
tkchin93411912015-07-22 12:12:17 -0700321 Init("CallSessionFileRotatingStreamTestWriteAndReadFirstHalf",
322 6 * 1024 * 1024);
323 ASSERT_TRUE(stream_->Open());
324 const size_t buffer_size = 1024 * 1024;
jbauch555604a2016-04-26 03:13:22 -0700325 std::unique_ptr<uint8_t[]> buffer(new uint8_t[buffer_size]);
tkchin93411912015-07-22 12:12:17 -0700326 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());
jbauch555604a2016-04-26 03:13:22 -0700334 std::unique_ptr<uint8_t[]> expected_buffer(new uint8_t[buffer_size]);
tkchin93411912015-07-22 12:12:17 -0700335 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