blob: 2a0e8589f460b62c58d52b6d361fe169a0d8af7b [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"
19
20namespace rtc {
21
phoglundbb738732016-07-15 03:57:12 -070022#if defined (WEBRTC_ANDROID)
23// Fails on Android: https://bugs.chromium.org/p/webrtc/issues/detail?id=4364.
24#define MAYBE_FileRotatingStreamTest DISABLED_FileRotatingStreamTest
25#else
26#define MAYBE_FileRotatingStreamTest FileRotatingStreamTest
27#endif
28
29class MAYBE_FileRotatingStreamTest : public ::testing::Test {
tkchin93411912015-07-22 12:12:17 -070030 protected:
31 static const char* kFilePrefix;
32 static const size_t kMaxFileSize;
33
34 void Init(const std::string& dir_name,
35 const std::string& file_prefix,
36 size_t max_file_size,
37 size_t num_log_files) {
38 Pathname test_path;
39 ASSERT_TRUE(Filesystem::GetAppTempFolder(&test_path));
40 // Append per-test output path in order to run within gtest parallel.
41 test_path.AppendFolder(dir_name);
42 ASSERT_TRUE(Filesystem::CreateFolder(test_path));
43 dir_path_ = test_path.pathname();
44 ASSERT_TRUE(dir_path_.size());
45 stream_.reset(new FileRotatingStream(dir_path_, file_prefix, max_file_size,
46 num_log_files));
47 }
48
49 void TearDown() override {
50 stream_.reset();
51 if (dir_path_.size() && Filesystem::IsFolder(dir_path_) &&
52 Filesystem::IsTemporaryPath(dir_path_)) {
53 Filesystem::DeleteFolderAndContents(dir_path_);
54 }
55 }
56
57 // Writes the data to the stream and flushes it.
58 void WriteAndFlush(const void* data, const size_t data_len) {
59 EXPECT_EQ(SR_SUCCESS, stream_->WriteAll(data, data_len, nullptr, nullptr));
60 EXPECT_TRUE(stream_->Flush());
61 }
62
63 // Checks that the stream reads in the expected contents and then returns an
64 // end of stream result.
65 void VerifyStreamRead(const char* expected_contents,
66 const size_t expected_length,
67 const std::string& dir_path,
68 const char* file_prefix) {
jbauch555604a2016-04-26 03:13:22 -070069 std::unique_ptr<FileRotatingStream> stream;
tkchin93411912015-07-22 12:12:17 -070070 stream.reset(new FileRotatingStream(dir_path, file_prefix));
71 ASSERT_TRUE(stream->Open());
tkchin28bae022015-07-23 12:27:02 -070072 size_t read = 0;
73 size_t stream_size = 0;
74 EXPECT_TRUE(stream->GetSize(&stream_size));
jbauch555604a2016-04-26 03:13:22 -070075 std::unique_ptr<uint8_t[]> buffer(new uint8_t[expected_length]);
tkchin93411912015-07-22 12:12:17 -070076 EXPECT_EQ(SR_SUCCESS,
tkchin28bae022015-07-23 12:27:02 -070077 stream->ReadAll(buffer.get(), expected_length, &read, nullptr));
tkchin93411912015-07-22 12:12:17 -070078 EXPECT_EQ(0, memcmp(expected_contents, buffer.get(), expected_length));
79 EXPECT_EQ(SR_EOS, stream->ReadAll(buffer.get(), 1, nullptr, nullptr));
tkchin28bae022015-07-23 12:27:02 -070080 EXPECT_EQ(stream_size, read);
tkchin93411912015-07-22 12:12:17 -070081 }
82
83 void VerifyFileContents(const char* expected_contents,
84 const size_t expected_length,
85 const std::string& file_path) {
jbauch555604a2016-04-26 03:13:22 -070086 std::unique_ptr<uint8_t[]> buffer(new uint8_t[expected_length]);
87 std::unique_ptr<FileStream> stream(Filesystem::OpenFile(file_path, "r"));
tkchin93411912015-07-22 12:12:17 -070088 EXPECT_TRUE(stream);
89 if (!stream) {
90 return;
91 }
92 EXPECT_EQ(rtc::SR_SUCCESS,
93 stream->ReadAll(buffer.get(), expected_length, nullptr, nullptr));
94 EXPECT_EQ(0, memcmp(expected_contents, buffer.get(), expected_length));
95 size_t file_size = 0;
96 EXPECT_TRUE(stream->GetSize(&file_size));
97 EXPECT_EQ(file_size, expected_length);
98 }
99
jbauch555604a2016-04-26 03:13:22 -0700100 std::unique_ptr<FileRotatingStream> stream_;
tkchin93411912015-07-22 12:12:17 -0700101 std::string dir_path_;
102};
103
phoglundbb738732016-07-15 03:57:12 -0700104const char* MAYBE_FileRotatingStreamTest::kFilePrefix =
105 "FileRotatingStreamTest";
106const size_t MAYBE_FileRotatingStreamTest::kMaxFileSize = 2;
tkchin93411912015-07-22 12:12:17 -0700107
108// Tests that stream state is correct before and after Open / Close.
phoglundbb738732016-07-15 03:57:12 -0700109TEST_F(MAYBE_FileRotatingStreamTest, State) {
tkchin93411912015-07-22 12:12:17 -0700110 Init("FileRotatingStreamTestState", kFilePrefix, kMaxFileSize, 3);
111
112 EXPECT_EQ(SS_CLOSED, stream_->GetState());
113 ASSERT_TRUE(stream_->Open());
114 EXPECT_EQ(SS_OPEN, stream_->GetState());
115 stream_->Close();
116 EXPECT_EQ(SS_CLOSED, stream_->GetState());
117}
118
119// Tests that nothing is written to file when data of length zero is written.
phoglundbb738732016-07-15 03:57:12 -0700120TEST_F(MAYBE_FileRotatingStreamTest, EmptyWrite) {
tkchin93411912015-07-22 12:12:17 -0700121 Init("FileRotatingStreamTestEmptyWrite", kFilePrefix, kMaxFileSize, 3);
122
123 ASSERT_TRUE(stream_->Open());
124 WriteAndFlush("a", 0);
125
126 std::string logfile_path = stream_->GetFilePath(0);
jbauch555604a2016-04-26 03:13:22 -0700127 std::unique_ptr<FileStream> stream(Filesystem::OpenFile(logfile_path, "r"));
tkchin93411912015-07-22 12:12:17 -0700128 size_t file_size = 0;
129 EXPECT_TRUE(stream->GetSize(&file_size));
130 EXPECT_EQ(0u, file_size);
131}
132
133// Tests that a write operation followed by a read returns the expected data
134// and writes to the expected files.
phoglundbb738732016-07-15 03:57:12 -0700135TEST_F(MAYBE_FileRotatingStreamTest, WriteAndRead) {
tkchin93411912015-07-22 12:12:17 -0700136 Init("FileRotatingStreamTestWriteAndRead", kFilePrefix, kMaxFileSize, 3);
137
138 ASSERT_TRUE(stream_->Open());
139 // The test is set up to create three log files of length 2. Write and check
140 // contents.
141 std::string messages[3] = {"aa", "bb", "cc"};
142 for (size_t i = 0; i < arraysize(messages); ++i) {
143 const std::string& message = messages[i];
144 WriteAndFlush(message.c_str(), message.size());
145 // Since the max log size is 2, we will be causing rotation. Read from the
146 // next file.
147 VerifyFileContents(message.c_str(), message.size(),
148 stream_->GetFilePath(1));
149 }
150 // Check that exactly three files exist.
151 for (size_t i = 0; i < arraysize(messages); ++i) {
152 EXPECT_TRUE(Filesystem::IsFile(stream_->GetFilePath(i)));
153 }
154 std::string message("d");
155 WriteAndFlush(message.c_str(), message.size());
156 for (size_t i = 0; i < arraysize(messages); ++i) {
157 EXPECT_TRUE(Filesystem::IsFile(stream_->GetFilePath(i)));
158 }
159 // TODO(tkchin): Maybe check all the files in the dir.
160
161 // Reopen for read.
162 std::string expected_contents("bbccd");
163 VerifyStreamRead(expected_contents.c_str(), expected_contents.size(),
164 dir_path_, kFilePrefix);
165}
166
167// Tests that writing data greater than the total capacity of the files
168// overwrites the files correctly and is read correctly after.
phoglundbb738732016-07-15 03:57:12 -0700169TEST_F(MAYBE_FileRotatingStreamTest, WriteOverflowAndRead) {
tkchin93411912015-07-22 12:12:17 -0700170 Init("FileRotatingStreamTestWriteOverflowAndRead", kFilePrefix, kMaxFileSize,
171 3);
172 ASSERT_TRUE(stream_->Open());
173 // This should cause overflow across all three files, such that the first file
174 // we wrote to also gets overwritten.
175 std::string message("foobarbaz");
176 WriteAndFlush(message.c_str(), message.size());
177 std::string expected_file_contents("z");
178 VerifyFileContents(expected_file_contents.c_str(),
179 expected_file_contents.size(), stream_->GetFilePath(0));
180 std::string expected_stream_contents("arbaz");
181 VerifyStreamRead(expected_stream_contents.c_str(),
182 expected_stream_contents.size(), dir_path_, kFilePrefix);
183}
184
185// Tests that the returned file paths have the right folder and prefix.
phoglundbb738732016-07-15 03:57:12 -0700186TEST_F(MAYBE_FileRotatingStreamTest, GetFilePath) {
tkchin93411912015-07-22 12:12:17 -0700187 Init("FileRotatingStreamTestGetFilePath", kFilePrefix, kMaxFileSize, 20);
188 for (auto i = 0; i < 20; ++i) {
189 Pathname path(stream_->GetFilePath(i));
190 EXPECT_EQ(0, path.folder().compare(dir_path_));
191 EXPECT_EQ(0, path.filename().compare(0, strlen(kFilePrefix), kFilePrefix));
192 }
193}
194
phoglundbb738732016-07-15 03:57:12 -0700195#if defined (WEBRTC_ANDROID)
196// Fails on Android: https://bugs.chromium.org/p/webrtc/issues/detail?id=4364.
197#define MAYBE_CallSessionFileRotatingStreamTest \
198 DISABLED_CallSessionFileRotatingStreamTest
199#else
200#define MAYBE_CallSessionFileRotatingStreamTest \
201 CallSessionFileRotatingStreamTest
202#endif
203
204class MAYBE_CallSessionFileRotatingStreamTest : public ::testing::Test {
tkchin93411912015-07-22 12:12:17 -0700205 protected:
206 void Init(const std::string& dir_name, size_t max_total_log_size) {
207 Pathname test_path;
208 ASSERT_TRUE(Filesystem::GetAppTempFolder(&test_path));
209 // Append per-test output path in order to run within gtest parallel.
210 test_path.AppendFolder(dir_name);
211 ASSERT_TRUE(Filesystem::CreateFolder(test_path));
212 dir_path_ = test_path.pathname();
213 ASSERT_TRUE(dir_path_.size());
214 stream_.reset(
215 new CallSessionFileRotatingStream(dir_path_, max_total_log_size));
216 }
217
218 virtual void TearDown() {
219 stream_.reset();
220 if (dir_path_.size() && Filesystem::IsFolder(dir_path_) &&
221 Filesystem::IsTemporaryPath(dir_path_)) {
222 Filesystem::DeleteFolderAndContents(dir_path_);
223 }
224 }
225
226 // Writes the data to the stream and flushes it.
227 void WriteAndFlush(const void* data, const size_t data_len) {
228 EXPECT_EQ(SR_SUCCESS, stream_->WriteAll(data, data_len, nullptr, nullptr));
229 EXPECT_TRUE(stream_->Flush());
230 }
231
232 // Checks that the stream reads in the expected contents and then returns an
233 // end of stream result.
234 void VerifyStreamRead(const char* expected_contents,
235 const size_t expected_length,
236 const std::string& dir_path) {
jbauch555604a2016-04-26 03:13:22 -0700237 std::unique_ptr<CallSessionFileRotatingStream> stream(
tkchin93411912015-07-22 12:12:17 -0700238 new CallSessionFileRotatingStream(dir_path));
239 ASSERT_TRUE(stream->Open());
tkchin28bae022015-07-23 12:27:02 -0700240 size_t read = 0;
241 size_t stream_size = 0;
242 EXPECT_TRUE(stream->GetSize(&stream_size));
jbauch555604a2016-04-26 03:13:22 -0700243 std::unique_ptr<uint8_t[]> buffer(new uint8_t[expected_length]);
tkchin93411912015-07-22 12:12:17 -0700244 EXPECT_EQ(SR_SUCCESS,
tkchin28bae022015-07-23 12:27:02 -0700245 stream->ReadAll(buffer.get(), expected_length, &read, nullptr));
tkchin93411912015-07-22 12:12:17 -0700246 EXPECT_EQ(0, memcmp(expected_contents, buffer.get(), expected_length));
247 EXPECT_EQ(SR_EOS, stream->ReadAll(buffer.get(), 1, nullptr, nullptr));
tkchin28bae022015-07-23 12:27:02 -0700248 EXPECT_EQ(stream_size, read);
tkchin93411912015-07-22 12:12:17 -0700249 }
250
jbauch555604a2016-04-26 03:13:22 -0700251 std::unique_ptr<CallSessionFileRotatingStream> stream_;
tkchin93411912015-07-22 12:12:17 -0700252 std::string dir_path_;
253};
254
255// Tests that writing and reading to a stream with the smallest possible
256// capacity works.
phoglundbb738732016-07-15 03:57:12 -0700257TEST_F(MAYBE_CallSessionFileRotatingStreamTest, WriteAndReadSmallest) {
tkchin93411912015-07-22 12:12:17 -0700258 Init("CallSessionFileRotatingStreamTestWriteAndReadSmallest", 4);
259
260 ASSERT_TRUE(stream_->Open());
261 std::string message("abcde");
262 WriteAndFlush(message.c_str(), message.size());
263 std::string expected_contents("abe");
264 VerifyStreamRead(expected_contents.c_str(), expected_contents.size(),
265 dir_path_);
266}
267
268// Tests that writing and reading to a stream with capacity lesser than 4MB
269// behaves correctly.
phoglundbb738732016-07-15 03:57:12 -0700270TEST_F(MAYBE_CallSessionFileRotatingStreamTest, WriteAndReadSmall) {
tkchin93411912015-07-22 12:12:17 -0700271 Init("CallSessionFileRotatingStreamTestWriteAndReadSmall", 8);
272
273 ASSERT_TRUE(stream_->Open());
274 std::string message("123456789");
275 WriteAndFlush(message.c_str(), message.size());
276 std::string expected_contents("1234789");
277 VerifyStreamRead(expected_contents.c_str(), expected_contents.size(),
278 dir_path_);
279}
280
281// Tests that writing and reading to a stream with capacity greater than 4MB
282// behaves correctly.
phoglundbb738732016-07-15 03:57:12 -0700283TEST_F(MAYBE_CallSessionFileRotatingStreamTest, WriteAndReadLarge) {
tkchin93411912015-07-22 12:12:17 -0700284 Init("CallSessionFileRotatingStreamTestWriteAndReadLarge", 6 * 1024 * 1024);
285
286 ASSERT_TRUE(stream_->Open());
287 const size_t buffer_size = 1024 * 1024;
jbauch555604a2016-04-26 03:13:22 -0700288 std::unique_ptr<uint8_t[]> buffer(new uint8_t[buffer_size]);
tkchin93411912015-07-22 12:12:17 -0700289 for (int i = 0; i < 8; i++) {
290 memset(buffer.get(), i, buffer_size);
291 EXPECT_EQ(SR_SUCCESS,
292 stream_->WriteAll(buffer.get(), buffer_size, nullptr, nullptr));
293 }
294
295 stream_.reset(new CallSessionFileRotatingStream(dir_path_));
296 ASSERT_TRUE(stream_->Open());
jbauch555604a2016-04-26 03:13:22 -0700297 std::unique_ptr<uint8_t[]> expected_buffer(new uint8_t[buffer_size]);
tkchin93411912015-07-22 12:12:17 -0700298 int expected_vals[] = {0, 1, 2, 6, 7};
299 for (size_t i = 0; i < arraysize(expected_vals); ++i) {
300 memset(expected_buffer.get(), expected_vals[i], buffer_size);
301 EXPECT_EQ(SR_SUCCESS,
302 stream_->ReadAll(buffer.get(), buffer_size, nullptr, nullptr));
303 EXPECT_EQ(0, memcmp(buffer.get(), expected_buffer.get(), buffer_size));
304 }
305 EXPECT_EQ(SR_EOS, stream_->ReadAll(buffer.get(), 1, nullptr, nullptr));
306}
307
308// Tests that writing and reading to a stream where only the first file is
309// written to behaves correctly.
phoglundbb738732016-07-15 03:57:12 -0700310TEST_F(MAYBE_CallSessionFileRotatingStreamTest, WriteAndReadFirstHalf) {
tkchin93411912015-07-22 12:12:17 -0700311 Init("CallSessionFileRotatingStreamTestWriteAndReadFirstHalf",
312 6 * 1024 * 1024);
313 ASSERT_TRUE(stream_->Open());
314 const size_t buffer_size = 1024 * 1024;
jbauch555604a2016-04-26 03:13:22 -0700315 std::unique_ptr<uint8_t[]> buffer(new uint8_t[buffer_size]);
tkchin93411912015-07-22 12:12:17 -0700316 for (int i = 0; i < 2; i++) {
317 memset(buffer.get(), i, buffer_size);
318 EXPECT_EQ(SR_SUCCESS,
319 stream_->WriteAll(buffer.get(), buffer_size, nullptr, nullptr));
320 }
321
322 stream_.reset(new CallSessionFileRotatingStream(dir_path_));
323 ASSERT_TRUE(stream_->Open());
jbauch555604a2016-04-26 03:13:22 -0700324 std::unique_ptr<uint8_t[]> expected_buffer(new uint8_t[buffer_size]);
tkchin93411912015-07-22 12:12:17 -0700325 int expected_vals[] = {0, 1};
326 for (size_t i = 0; i < arraysize(expected_vals); ++i) {
327 memset(expected_buffer.get(), expected_vals[i], buffer_size);
328 EXPECT_EQ(SR_SUCCESS,
329 stream_->ReadAll(buffer.get(), buffer_size, nullptr, nullptr));
330 EXPECT_EQ(0, memcmp(buffer.get(), expected_buffer.get(), buffer_size));
331 }
332 EXPECT_EQ(SR_EOS, stream_->ReadAll(buffer.get(), 1, nullptr, nullptr));
333}
334
335} // namespace rtc