blob: 7acb174bb35a95775e87b6c06e666bc6db6f45ad [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
tkchin93411912015-07-22 12:12:17 -070013#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"
nisse57efb032017-05-18 03:55:59 -070019#include "webrtc/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
phoglundbb738732016-07-15 03:57:12 -070034#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
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]);
99 std::unique_ptr<FileStream> stream(Filesystem::OpenFile(file_path, "r"));
tkchin93411912015-07-22 12:12:17 -0700100 EXPECT_TRUE(stream);
101 if (!stream) {
102 return;
103 }
104 EXPECT_EQ(rtc::SR_SUCCESS,
105 stream->ReadAll(buffer.get(), expected_length, nullptr, nullptr));
106 EXPECT_EQ(0, memcmp(expected_contents, buffer.get(), expected_length));
107 size_t file_size = 0;
108 EXPECT_TRUE(stream->GetSize(&file_size));
109 EXPECT_EQ(file_size, expected_length);
110 }
111
jbauch555604a2016-04-26 03:13:22 -0700112 std::unique_ptr<FileRotatingStream> stream_;
tkchin93411912015-07-22 12:12:17 -0700113 std::string dir_path_;
114};
115
phoglundbb738732016-07-15 03:57:12 -0700116const char* MAYBE_FileRotatingStreamTest::kFilePrefix =
117 "FileRotatingStreamTest";
118const size_t MAYBE_FileRotatingStreamTest::kMaxFileSize = 2;
tkchin93411912015-07-22 12:12:17 -0700119
120// Tests that stream state is correct before and after Open / Close.
phoglundbb738732016-07-15 03:57:12 -0700121TEST_F(MAYBE_FileRotatingStreamTest, State) {
tkchin93411912015-07-22 12:12:17 -0700122 Init("FileRotatingStreamTestState", kFilePrefix, kMaxFileSize, 3);
123
124 EXPECT_EQ(SS_CLOSED, stream_->GetState());
125 ASSERT_TRUE(stream_->Open());
126 EXPECT_EQ(SS_OPEN, stream_->GetState());
127 stream_->Close();
128 EXPECT_EQ(SS_CLOSED, stream_->GetState());
129}
130
131// Tests that nothing is written to file when data of length zero is written.
phoglundbb738732016-07-15 03:57:12 -0700132TEST_F(MAYBE_FileRotatingStreamTest, EmptyWrite) {
tkchin93411912015-07-22 12:12:17 -0700133 Init("FileRotatingStreamTestEmptyWrite", kFilePrefix, kMaxFileSize, 3);
134
135 ASSERT_TRUE(stream_->Open());
136 WriteAndFlush("a", 0);
137
138 std::string logfile_path = stream_->GetFilePath(0);
jbauch555604a2016-04-26 03:13:22 -0700139 std::unique_ptr<FileStream> stream(Filesystem::OpenFile(logfile_path, "r"));
tkchin93411912015-07-22 12:12:17 -0700140 size_t file_size = 0;
141 EXPECT_TRUE(stream->GetSize(&file_size));
142 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
179// Tests that writing data greater than the total capacity of the files
180// overwrites the files correctly and is read correctly after.
phoglundbb738732016-07-15 03:57:12 -0700181TEST_F(MAYBE_FileRotatingStreamTest, WriteOverflowAndRead) {
tkchin93411912015-07-22 12:12:17 -0700182 Init("FileRotatingStreamTestWriteOverflowAndRead", kFilePrefix, kMaxFileSize,
183 3);
184 ASSERT_TRUE(stream_->Open());
185 // This should cause overflow across all three files, such that the first file
186 // we wrote to also gets overwritten.
187 std::string message("foobarbaz");
188 WriteAndFlush(message.c_str(), message.size());
189 std::string expected_file_contents("z");
190 VerifyFileContents(expected_file_contents.c_str(),
191 expected_file_contents.size(), stream_->GetFilePath(0));
192 std::string expected_stream_contents("arbaz");
193 VerifyStreamRead(expected_stream_contents.c_str(),
194 expected_stream_contents.size(), dir_path_, kFilePrefix);
195}
196
197// Tests that the returned file paths have the right folder and prefix.
phoglundbb738732016-07-15 03:57:12 -0700198TEST_F(MAYBE_FileRotatingStreamTest, GetFilePath) {
tkchin93411912015-07-22 12:12:17 -0700199 Init("FileRotatingStreamTestGetFilePath", kFilePrefix, kMaxFileSize, 20);
200 for (auto i = 0; i < 20; ++i) {
201 Pathname path(stream_->GetFilePath(i));
202 EXPECT_EQ(0, path.folder().compare(dir_path_));
203 EXPECT_EQ(0, path.filename().compare(0, strlen(kFilePrefix), kFilePrefix));
204 }
205}
206
phoglundbb738732016-07-15 03:57:12 -0700207#if defined (WEBRTC_ANDROID)
208// Fails on Android: https://bugs.chromium.org/p/webrtc/issues/detail?id=4364.
209#define MAYBE_CallSessionFileRotatingStreamTest \
210 DISABLED_CallSessionFileRotatingStreamTest
211#else
212#define MAYBE_CallSessionFileRotatingStreamTest \
213 CallSessionFileRotatingStreamTest
214#endif
215
216class MAYBE_CallSessionFileRotatingStreamTest : public ::testing::Test {
tkchin93411912015-07-22 12:12:17 -0700217 protected:
218 void Init(const std::string& dir_name, size_t max_total_log_size) {
nisse57efb032017-05-18 03:55:59 -0700219 dir_path_ = webrtc::test::OutputPath();
220
tkchin93411912015-07-22 12:12:17 -0700221 // Append per-test output path in order to run within gtest parallel.
nisse57efb032017-05-18 03:55:59 -0700222 dir_path_.append(dir_name);
223 dir_path_.push_back(Pathname::DefaultFolderDelimiter());
224 ASSERT_TRUE(webrtc::test::CreateDir(dir_path_));
tkchin93411912015-07-22 12:12:17 -0700225 stream_.reset(
226 new CallSessionFileRotatingStream(dir_path_, max_total_log_size));
227 }
228
229 virtual void TearDown() {
nisse57efb032017-05-18 03:55:59 -0700230 // On windows, open files can't be removed.
231 stream_->Close();
232 CleanupLogDirectory(*stream_);
233 EXPECT_TRUE(webrtc::test::RemoveDir(dir_path_));
234
tkchin93411912015-07-22 12:12:17 -0700235 stream_.reset();
tkchin93411912015-07-22 12:12:17 -0700236 }
237
238 // Writes the data to the stream and flushes it.
239 void WriteAndFlush(const void* data, const size_t data_len) {
240 EXPECT_EQ(SR_SUCCESS, stream_->WriteAll(data, data_len, nullptr, nullptr));
241 EXPECT_TRUE(stream_->Flush());
242 }
243
244 // Checks that the stream reads in the expected contents and then returns an
245 // end of stream result.
246 void VerifyStreamRead(const char* expected_contents,
247 const size_t expected_length,
248 const std::string& dir_path) {
jbauch555604a2016-04-26 03:13:22 -0700249 std::unique_ptr<CallSessionFileRotatingStream> stream(
tkchin93411912015-07-22 12:12:17 -0700250 new CallSessionFileRotatingStream(dir_path));
251 ASSERT_TRUE(stream->Open());
tkchin28bae022015-07-23 12:27:02 -0700252 size_t read = 0;
253 size_t stream_size = 0;
254 EXPECT_TRUE(stream->GetSize(&stream_size));
jbauch555604a2016-04-26 03:13:22 -0700255 std::unique_ptr<uint8_t[]> buffer(new uint8_t[expected_length]);
tkchin93411912015-07-22 12:12:17 -0700256 EXPECT_EQ(SR_SUCCESS,
tkchin28bae022015-07-23 12:27:02 -0700257 stream->ReadAll(buffer.get(), expected_length, &read, nullptr));
tkchin93411912015-07-22 12:12:17 -0700258 EXPECT_EQ(0, memcmp(expected_contents, buffer.get(), expected_length));
259 EXPECT_EQ(SR_EOS, stream->ReadAll(buffer.get(), 1, nullptr, nullptr));
tkchin28bae022015-07-23 12:27:02 -0700260 EXPECT_EQ(stream_size, read);
tkchin93411912015-07-22 12:12:17 -0700261 }
262
jbauch555604a2016-04-26 03:13:22 -0700263 std::unique_ptr<CallSessionFileRotatingStream> stream_;
tkchin93411912015-07-22 12:12:17 -0700264 std::string dir_path_;
265};
266
267// Tests that writing and reading to a stream with the smallest possible
268// capacity works.
phoglundbb738732016-07-15 03:57:12 -0700269TEST_F(MAYBE_CallSessionFileRotatingStreamTest, WriteAndReadSmallest) {
tkchin93411912015-07-22 12:12:17 -0700270 Init("CallSessionFileRotatingStreamTestWriteAndReadSmallest", 4);
271
272 ASSERT_TRUE(stream_->Open());
273 std::string message("abcde");
274 WriteAndFlush(message.c_str(), message.size());
275 std::string expected_contents("abe");
276 VerifyStreamRead(expected_contents.c_str(), expected_contents.size(),
277 dir_path_);
278}
279
280// Tests that writing and reading to a stream with capacity lesser than 4MB
281// behaves correctly.
phoglundbb738732016-07-15 03:57:12 -0700282TEST_F(MAYBE_CallSessionFileRotatingStreamTest, WriteAndReadSmall) {
tkchin93411912015-07-22 12:12:17 -0700283 Init("CallSessionFileRotatingStreamTestWriteAndReadSmall", 8);
284
285 ASSERT_TRUE(stream_->Open());
286 std::string message("123456789");
287 WriteAndFlush(message.c_str(), message.size());
288 std::string expected_contents("1234789");
289 VerifyStreamRead(expected_contents.c_str(), expected_contents.size(),
290 dir_path_);
291}
292
293// Tests that writing and reading to a stream with capacity greater than 4MB
294// behaves correctly.
phoglundbb738732016-07-15 03:57:12 -0700295TEST_F(MAYBE_CallSessionFileRotatingStreamTest, WriteAndReadLarge) {
tkchin93411912015-07-22 12:12:17 -0700296 Init("CallSessionFileRotatingStreamTestWriteAndReadLarge", 6 * 1024 * 1024);
297
298 ASSERT_TRUE(stream_->Open());
299 const size_t buffer_size = 1024 * 1024;
jbauch555604a2016-04-26 03:13:22 -0700300 std::unique_ptr<uint8_t[]> buffer(new uint8_t[buffer_size]);
tkchin93411912015-07-22 12:12:17 -0700301 for (int i = 0; i < 8; i++) {
302 memset(buffer.get(), i, buffer_size);
303 EXPECT_EQ(SR_SUCCESS,
304 stream_->WriteAll(buffer.get(), buffer_size, nullptr, nullptr));
305 }
306
307 stream_.reset(new CallSessionFileRotatingStream(dir_path_));
308 ASSERT_TRUE(stream_->Open());
jbauch555604a2016-04-26 03:13:22 -0700309 std::unique_ptr<uint8_t[]> expected_buffer(new uint8_t[buffer_size]);
tkchin93411912015-07-22 12:12:17 -0700310 int expected_vals[] = {0, 1, 2, 6, 7};
311 for (size_t i = 0; i < arraysize(expected_vals); ++i) {
312 memset(expected_buffer.get(), expected_vals[i], buffer_size);
313 EXPECT_EQ(SR_SUCCESS,
314 stream_->ReadAll(buffer.get(), buffer_size, nullptr, nullptr));
315 EXPECT_EQ(0, memcmp(buffer.get(), expected_buffer.get(), buffer_size));
316 }
317 EXPECT_EQ(SR_EOS, stream_->ReadAll(buffer.get(), 1, nullptr, nullptr));
318}
319
320// Tests that writing and reading to a stream where only the first file is
321// written to behaves correctly.
phoglundbb738732016-07-15 03:57:12 -0700322TEST_F(MAYBE_CallSessionFileRotatingStreamTest, WriteAndReadFirstHalf) {
tkchin93411912015-07-22 12:12:17 -0700323 Init("CallSessionFileRotatingStreamTestWriteAndReadFirstHalf",
324 6 * 1024 * 1024);
325 ASSERT_TRUE(stream_->Open());
326 const size_t buffer_size = 1024 * 1024;
jbauch555604a2016-04-26 03:13:22 -0700327 std::unique_ptr<uint8_t[]> buffer(new uint8_t[buffer_size]);
tkchin93411912015-07-22 12:12:17 -0700328 for (int i = 0; i < 2; i++) {
329 memset(buffer.get(), i, buffer_size);
330 EXPECT_EQ(SR_SUCCESS,
331 stream_->WriteAll(buffer.get(), buffer_size, nullptr, nullptr));
332 }
333
334 stream_.reset(new CallSessionFileRotatingStream(dir_path_));
335 ASSERT_TRUE(stream_->Open());
jbauch555604a2016-04-26 03:13:22 -0700336 std::unique_ptr<uint8_t[]> expected_buffer(new uint8_t[buffer_size]);
tkchin93411912015-07-22 12:12:17 -0700337 int expected_vals[] = {0, 1};
338 for (size_t i = 0; i < arraysize(expected_vals); ++i) {
339 memset(expected_buffer.get(), expected_vals[i], buffer_size);
340 EXPECT_EQ(SR_SUCCESS,
341 stream_->ReadAll(buffer.get(), buffer_size, nullptr, nullptr));
342 EXPECT_EQ(0, memcmp(buffer.get(), expected_buffer.get(), buffer_size));
343 }
344 EXPECT_EQ(SR_EOS, stream_->ReadAll(buffer.get(), 1, nullptr, nullptr));
345}
346
347} // namespace rtc