blob: c2ba06773a4ca6c38be816994a7b885e518dc76e [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
Jonas Olssona4d87372019-07-05 19:08:33 +020011#include "rtc_base/file_rotating_stream.h"
12
Yves Gerey3e707812018-11-28 16:47:49 +010013#include <string.h>
Jonas Olssona4d87372019-07-05 19:08:33 +020014
Yves Gerey3e707812018-11-28 16:47:49 +010015#include <cstdint>
jbauch555604a2016-04-26 03:13:22 -070016#include <memory>
17
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020018#include "rtc_base/arraysize.h"
Yves Gerey3e707812018-11-28 16:47:49 +010019#include "test/gtest.h"
Steve Anton10542f22019-01-11 09:11:00 -080020#include "test/testsupport/file_utils.h"
tkchin93411912015-07-22 12:12:17 -070021
22namespace rtc {
23
nisse57efb032017-05-18 03:55:59 -070024namespace {
25
26void CleanupLogDirectory(const FileRotatingStream& stream) {
27 for (size_t i = 0; i < stream.GetNumFiles(); ++i) {
28 // Ignore return value, not all files are expected to exist.
29 webrtc::test::RemoveFile(stream.GetFilePath(i));
30 }
31}
32
33} // namespace
34
Yves Gerey665174f2018-06-19 15:03:05 +020035#if defined(WEBRTC_ANDROID)
phoglundbb738732016-07-15 03:57:12 -070036// Fails on Android: https://bugs.chromium.org/p/webrtc/issues/detail?id=4364.
37#define MAYBE_FileRotatingStreamTest DISABLED_FileRotatingStreamTest
38#else
39#define MAYBE_FileRotatingStreamTest FileRotatingStreamTest
40#endif
41
42class MAYBE_FileRotatingStreamTest : public ::testing::Test {
tkchin93411912015-07-22 12:12:17 -070043 protected:
44 static const char* kFilePrefix;
45 static const size_t kMaxFileSize;
46
47 void Init(const std::string& dir_name,
48 const std::string& file_prefix,
49 size_t max_file_size,
Niels Möller7b3c76b2018-11-07 09:54:28 +010050 size_t num_log_files,
51 bool ensure_trailing_delimiter = true) {
nisse57efb032017-05-18 03:55:59 -070052 dir_path_ = webrtc::test::OutputPath();
53
tkchin93411912015-07-22 12:12:17 -070054 // Append per-test output path in order to run within gtest parallel.
nisse57efb032017-05-18 03:55:59 -070055 dir_path_.append(dir_name);
Niels Möller7b3c76b2018-11-07 09:54:28 +010056 if (ensure_trailing_delimiter) {
57 dir_path_.append(webrtc::test::kPathDelimiter);
58 }
nisse57efb032017-05-18 03:55:59 -070059 ASSERT_TRUE(webrtc::test::CreateDir(dir_path_));
tkchin93411912015-07-22 12:12:17 -070060 stream_.reset(new FileRotatingStream(dir_path_, file_prefix, max_file_size,
61 num_log_files));
62 }
63
64 void TearDown() override {
nisse57efb032017-05-18 03:55:59 -070065 // On windows, open files can't be removed.
66 stream_->Close();
67 CleanupLogDirectory(*stream_);
68 EXPECT_TRUE(webrtc::test::RemoveDir(dir_path_));
69
tkchin93411912015-07-22 12:12:17 -070070 stream_.reset();
tkchin93411912015-07-22 12:12:17 -070071 }
72
73 // Writes the data to the stream and flushes it.
74 void WriteAndFlush(const void* data, const size_t data_len) {
75 EXPECT_EQ(SR_SUCCESS, stream_->WriteAll(data, data_len, nullptr, nullptr));
76 EXPECT_TRUE(stream_->Flush());
77 }
78
79 // Checks that the stream reads in the expected contents and then returns an
80 // end of stream result.
81 void VerifyStreamRead(const char* expected_contents,
82 const size_t expected_length,
83 const std::string& dir_path,
84 const char* file_prefix) {
Niels Möllerd9ac0582019-01-03 14:21:38 +010085 FileRotatingStreamReader reader(dir_path, file_prefix);
86 EXPECT_EQ(reader.GetSize(), expected_length);
Niels Möller6ffe62a2019-01-08 13:22:57 +010087 std::unique_ptr<uint8_t[]> buffer(new uint8_t[expected_length]);
Niels Möllerd9ac0582019-01-03 14:21:38 +010088 memset(buffer.get(), 0, expected_length);
89 EXPECT_EQ(expected_length, reader.ReadAll(buffer.get(), expected_length));
90 EXPECT_EQ(0, memcmp(expected_contents, buffer.get(), expected_length));
tkchin93411912015-07-22 12:12:17 -070091 }
92
93 void VerifyFileContents(const char* expected_contents,
94 const size_t expected_length,
95 const std::string& file_path) {
Niels Möller0a7d56e2019-01-10 11:24:07 +010096 std::unique_ptr<uint8_t[]> buffer(new uint8_t[expected_length + 1]);
Niels Möller23213d92019-01-22 11:01:24 +010097 webrtc::FileWrapper f = webrtc::FileWrapper::OpenReadOnly(file_path);
98 ASSERT_TRUE(f.is_open());
99 size_t size_read = f.Read(buffer.get(), expected_length + 1);
Niels Möller6ffe62a2019-01-08 13:22:57 +0100100 EXPECT_EQ(size_read, expected_length);
101 EXPECT_EQ(0, memcmp(expected_contents, buffer.get(),
102 std::min(expected_length, size_read)));
tkchin93411912015-07-22 12:12:17 -0700103 }
104
jbauch555604a2016-04-26 03:13:22 -0700105 std::unique_ptr<FileRotatingStream> stream_;
tkchin93411912015-07-22 12:12:17 -0700106 std::string dir_path_;
107};
108
phoglundbb738732016-07-15 03:57:12 -0700109const char* MAYBE_FileRotatingStreamTest::kFilePrefix =
110 "FileRotatingStreamTest";
111const size_t MAYBE_FileRotatingStreamTest::kMaxFileSize = 2;
tkchin93411912015-07-22 12:12:17 -0700112
113// Tests that stream state is correct before and after Open / Close.
phoglundbb738732016-07-15 03:57:12 -0700114TEST_F(MAYBE_FileRotatingStreamTest, State) {
tkchin93411912015-07-22 12:12:17 -0700115 Init("FileRotatingStreamTestState", kFilePrefix, kMaxFileSize, 3);
116
117 EXPECT_EQ(SS_CLOSED, stream_->GetState());
118 ASSERT_TRUE(stream_->Open());
119 EXPECT_EQ(SS_OPEN, stream_->GetState());
120 stream_->Close();
121 EXPECT_EQ(SS_CLOSED, stream_->GetState());
122}
123
124// Tests that nothing is written to file when data of length zero is written.
phoglundbb738732016-07-15 03:57:12 -0700125TEST_F(MAYBE_FileRotatingStreamTest, EmptyWrite) {
tkchin93411912015-07-22 12:12:17 -0700126 Init("FileRotatingStreamTestEmptyWrite", kFilePrefix, kMaxFileSize, 3);
127
128 ASSERT_TRUE(stream_->Open());
129 WriteAndFlush("a", 0);
130
131 std::string logfile_path = stream_->GetFilePath(0);
Niels Möller23213d92019-01-22 11:01:24 +0100132 webrtc::FileWrapper f = webrtc::FileWrapper::OpenReadOnly(logfile_path);
133 ASSERT_TRUE(f.is_open());
Niels Möller0a7d56e2019-01-10 11:24:07 +0100134 char buf[1];
Niels Möller23213d92019-01-22 11:01:24 +0100135 EXPECT_EQ(0u, f.Read(buf, sizeof(buf)));
tkchin93411912015-07-22 12:12:17 -0700136}
137
138// Tests that a write operation followed by a read returns the expected data
139// and writes to the expected files.
phoglundbb738732016-07-15 03:57:12 -0700140TEST_F(MAYBE_FileRotatingStreamTest, WriteAndRead) {
tkchin93411912015-07-22 12:12:17 -0700141 Init("FileRotatingStreamTestWriteAndRead", kFilePrefix, kMaxFileSize, 3);
142
143 ASSERT_TRUE(stream_->Open());
144 // The test is set up to create three log files of length 2. Write and check
145 // contents.
146 std::string messages[3] = {"aa", "bb", "cc"};
147 for (size_t i = 0; i < arraysize(messages); ++i) {
148 const std::string& message = messages[i];
149 WriteAndFlush(message.c_str(), message.size());
150 // Since the max log size is 2, we will be causing rotation. Read from the
151 // next file.
152 VerifyFileContents(message.c_str(), message.size(),
153 stream_->GetFilePath(1));
154 }
155 // Check that exactly three files exist.
156 for (size_t i = 0; i < arraysize(messages); ++i) {
Niels Möller260770c2018-11-07 15:08:18 +0100157 EXPECT_TRUE(webrtc::test::FileExists(stream_->GetFilePath(i)));
tkchin93411912015-07-22 12:12:17 -0700158 }
159 std::string message("d");
160 WriteAndFlush(message.c_str(), message.size());
161 for (size_t i = 0; i < arraysize(messages); ++i) {
Niels Möller260770c2018-11-07 15:08:18 +0100162 EXPECT_TRUE(webrtc::test::FileExists(stream_->GetFilePath(i)));
tkchin93411912015-07-22 12:12:17 -0700163 }
164 // TODO(tkchin): Maybe check all the files in the dir.
165
166 // Reopen for read.
167 std::string expected_contents("bbccd");
168 VerifyStreamRead(expected_contents.c_str(), expected_contents.size(),
169 dir_path_, kFilePrefix);
170}
171
Niels Möller7b3c76b2018-11-07 09:54:28 +0100172// Tests that a write operation (with dir name without delimiter) followed by a
173// read returns the expected data and writes to the expected files.
174TEST_F(MAYBE_FileRotatingStreamTest, WriteWithoutDelimiterAndRead) {
175 Init("FileRotatingStreamTestWriteWithoutDelimiterAndRead", kFilePrefix,
176 kMaxFileSize, 3,
177 /* ensure_trailing_delimiter*/ false);
178
179 ASSERT_TRUE(stream_->Open());
180 // The test is set up to create three log files of length 2. Write and check
181 // contents.
182 std::string messages[3] = {"aa", "bb", "cc"};
183 for (size_t i = 0; i < arraysize(messages); ++i) {
184 const std::string& message = messages[i];
185 WriteAndFlush(message.c_str(), message.size());
186 }
187 std::string message("d");
188 WriteAndFlush(message.c_str(), message.size());
189
190 // Reopen for read.
191 std::string expected_contents("bbccd");
192 VerifyStreamRead(expected_contents.c_str(), expected_contents.size(),
193 dir_path_ + webrtc::test::kPathDelimiter, kFilePrefix);
194}
195
196// Tests that a write operation followed by a read (without trailing delimiter)
197// returns the expected data and writes to the expected files.
198TEST_F(MAYBE_FileRotatingStreamTest, WriteAndReadWithoutDelimiter) {
199 Init("FileRotatingStreamTestWriteAndReadWithoutDelimiter", kFilePrefix,
200 kMaxFileSize, 3);
201
202 ASSERT_TRUE(stream_->Open());
203 // The test is set up to create three log files of length 2. Write and check
204 // contents.
205 std::string messages[3] = {"aa", "bb", "cc"};
206 for (size_t i = 0; i < arraysize(messages); ++i) {
207 const std::string& message = messages[i];
208 WriteAndFlush(message.c_str(), message.size());
209 }
210 std::string message("d");
211 WriteAndFlush(message.c_str(), message.size());
212
213 // Reopen for read.
214 std::string expected_contents("bbccd");
215 VerifyStreamRead(expected_contents.c_str(), expected_contents.size(),
216 dir_path_.substr(0, dir_path_.size() - 1), kFilePrefix);
217}
218
tkchin93411912015-07-22 12:12:17 -0700219// Tests that writing data greater than the total capacity of the files
220// overwrites the files correctly and is read correctly after.
phoglundbb738732016-07-15 03:57:12 -0700221TEST_F(MAYBE_FileRotatingStreamTest, WriteOverflowAndRead) {
tkchin93411912015-07-22 12:12:17 -0700222 Init("FileRotatingStreamTestWriteOverflowAndRead", kFilePrefix, kMaxFileSize,
223 3);
224 ASSERT_TRUE(stream_->Open());
225 // This should cause overflow across all three files, such that the first file
226 // we wrote to also gets overwritten.
227 std::string message("foobarbaz");
228 WriteAndFlush(message.c_str(), message.size());
229 std::string expected_file_contents("z");
230 VerifyFileContents(expected_file_contents.c_str(),
231 expected_file_contents.size(), stream_->GetFilePath(0));
232 std::string expected_stream_contents("arbaz");
233 VerifyStreamRead(expected_stream_contents.c_str(),
234 expected_stream_contents.size(), dir_path_, kFilePrefix);
235}
236
237// Tests that the returned file paths have the right folder and prefix.
phoglundbb738732016-07-15 03:57:12 -0700238TEST_F(MAYBE_FileRotatingStreamTest, GetFilePath) {
tkchin93411912015-07-22 12:12:17 -0700239 Init("FileRotatingStreamTestGetFilePath", kFilePrefix, kMaxFileSize, 20);
Niels Möller394b4eb2018-06-01 14:08:25 +0200240 // dir_path_ includes a trailing delimiter.
241 const std::string prefix = dir_path_ + kFilePrefix;
tkchin93411912015-07-22 12:12:17 -0700242 for (auto i = 0; i < 20; ++i) {
Niels Möller394b4eb2018-06-01 14:08:25 +0200243 EXPECT_EQ(0, stream_->GetFilePath(i).compare(0, prefix.size(), prefix));
tkchin93411912015-07-22 12:12:17 -0700244 }
245}
246
Yves Gerey665174f2018-06-19 15:03:05 +0200247#if defined(WEBRTC_ANDROID)
phoglundbb738732016-07-15 03:57:12 -0700248// Fails on Android: https://bugs.chromium.org/p/webrtc/issues/detail?id=4364.
249#define MAYBE_CallSessionFileRotatingStreamTest \
Yves Gerey665174f2018-06-19 15:03:05 +0200250 DISABLED_CallSessionFileRotatingStreamTest
phoglundbb738732016-07-15 03:57:12 -0700251#else
252#define MAYBE_CallSessionFileRotatingStreamTest \
Yves Gerey665174f2018-06-19 15:03:05 +0200253 CallSessionFileRotatingStreamTest
phoglundbb738732016-07-15 03:57:12 -0700254#endif
255
256class MAYBE_CallSessionFileRotatingStreamTest : public ::testing::Test {
tkchin93411912015-07-22 12:12:17 -0700257 protected:
258 void Init(const std::string& dir_name, size_t max_total_log_size) {
nisse57efb032017-05-18 03:55:59 -0700259 dir_path_ = webrtc::test::OutputPath();
260
tkchin93411912015-07-22 12:12:17 -0700261 // Append per-test output path in order to run within gtest parallel.
nisse57efb032017-05-18 03:55:59 -0700262 dir_path_.append(dir_name);
Niels Möller7b3c76b2018-11-07 09:54:28 +0100263 dir_path_.append(webrtc::test::kPathDelimiter);
nisse57efb032017-05-18 03:55:59 -0700264 ASSERT_TRUE(webrtc::test::CreateDir(dir_path_));
tkchin93411912015-07-22 12:12:17 -0700265 stream_.reset(
266 new CallSessionFileRotatingStream(dir_path_, max_total_log_size));
267 }
268
Steve Anton9de3aac2017-10-24 10:08:26 -0700269 void TearDown() override {
nisse57efb032017-05-18 03:55:59 -0700270 // On windows, open files can't be removed.
271 stream_->Close();
272 CleanupLogDirectory(*stream_);
273 EXPECT_TRUE(webrtc::test::RemoveDir(dir_path_));
274
tkchin93411912015-07-22 12:12:17 -0700275 stream_.reset();
tkchin93411912015-07-22 12:12:17 -0700276 }
277
278 // Writes the data to the stream and flushes it.
279 void WriteAndFlush(const void* data, const size_t data_len) {
280 EXPECT_EQ(SR_SUCCESS, stream_->WriteAll(data, data_len, nullptr, nullptr));
281 EXPECT_TRUE(stream_->Flush());
282 }
283
284 // Checks that the stream reads in the expected contents and then returns an
285 // end of stream result.
286 void VerifyStreamRead(const char* expected_contents,
287 const size_t expected_length,
288 const std::string& dir_path) {
Niels Möllerd9ac0582019-01-03 14:21:38 +0100289 CallSessionFileRotatingStreamReader reader(dir_path);
290 EXPECT_EQ(reader.GetSize(), expected_length);
Niels Möller6ffe62a2019-01-08 13:22:57 +0100291 std::unique_ptr<uint8_t[]> buffer(new uint8_t[expected_length]);
Niels Möllerd9ac0582019-01-03 14:21:38 +0100292 memset(buffer.get(), 0, expected_length);
293 EXPECT_EQ(expected_length, reader.ReadAll(buffer.get(), expected_length));
294 EXPECT_EQ(0, memcmp(expected_contents, buffer.get(), expected_length));
tkchin93411912015-07-22 12:12:17 -0700295 }
296
jbauch555604a2016-04-26 03:13:22 -0700297 std::unique_ptr<CallSessionFileRotatingStream> stream_;
tkchin93411912015-07-22 12:12:17 -0700298 std::string dir_path_;
299};
300
301// Tests that writing and reading to a stream with the smallest possible
302// capacity works.
phoglundbb738732016-07-15 03:57:12 -0700303TEST_F(MAYBE_CallSessionFileRotatingStreamTest, WriteAndReadSmallest) {
tkchin93411912015-07-22 12:12:17 -0700304 Init("CallSessionFileRotatingStreamTestWriteAndReadSmallest", 4);
305
306 ASSERT_TRUE(stream_->Open());
307 std::string message("abcde");
308 WriteAndFlush(message.c_str(), message.size());
309 std::string expected_contents("abe");
310 VerifyStreamRead(expected_contents.c_str(), expected_contents.size(),
311 dir_path_);
312}
313
314// Tests that writing and reading to a stream with capacity lesser than 4MB
315// behaves correctly.
phoglundbb738732016-07-15 03:57:12 -0700316TEST_F(MAYBE_CallSessionFileRotatingStreamTest, WriteAndReadSmall) {
tkchin93411912015-07-22 12:12:17 -0700317 Init("CallSessionFileRotatingStreamTestWriteAndReadSmall", 8);
318
319 ASSERT_TRUE(stream_->Open());
320 std::string message("123456789");
321 WriteAndFlush(message.c_str(), message.size());
322 std::string expected_contents("1234789");
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 greater than 4MB
328// behaves correctly.
phoglundbb738732016-07-15 03:57:12 -0700329TEST_F(MAYBE_CallSessionFileRotatingStreamTest, WriteAndReadLarge) {
tkchin93411912015-07-22 12:12:17 -0700330 Init("CallSessionFileRotatingStreamTestWriteAndReadLarge", 6 * 1024 * 1024);
331
332 ASSERT_TRUE(stream_->Open());
333 const size_t buffer_size = 1024 * 1024;
jbauch555604a2016-04-26 03:13:22 -0700334 std::unique_ptr<uint8_t[]> buffer(new uint8_t[buffer_size]);
tkchin93411912015-07-22 12:12:17 -0700335 for (int i = 0; i < 8; i++) {
336 memset(buffer.get(), i, buffer_size);
337 EXPECT_EQ(SR_SUCCESS,
338 stream_->WriteAll(buffer.get(), buffer_size, nullptr, nullptr));
339 }
340
Niels Möller6ffe62a2019-01-08 13:22:57 +0100341 const int expected_vals[] = {0, 1, 2, 6, 7};
342 const size_t expected_size = buffer_size * arraysize(expected_vals);
343
344 CallSessionFileRotatingStreamReader reader(dir_path_);
345 EXPECT_EQ(reader.GetSize(), expected_size);
346 std::unique_ptr<uint8_t[]> contents(new uint8_t[expected_size + 1]);
347 EXPECT_EQ(reader.ReadAll(contents.get(), expected_size + 1), expected_size);
tkchin93411912015-07-22 12:12:17 -0700348 for (size_t i = 0; i < arraysize(expected_vals); ++i) {
Niels Möller6ffe62a2019-01-08 13:22:57 +0100349 const uint8_t* block = contents.get() + i * buffer_size;
350 bool match = true;
351 for (size_t j = 0; j < buffer_size; j++) {
352 if (block[j] != expected_vals[i]) {
353 match = false;
354 break;
355 }
356 }
357 // EXPECT call at end of loop, to limit the number of messages on failure.
358 EXPECT_TRUE(match);
tkchin93411912015-07-22 12:12:17 -0700359 }
tkchin93411912015-07-22 12:12:17 -0700360}
361
362// Tests that writing and reading to a stream where only the first file is
363// written to behaves correctly.
phoglundbb738732016-07-15 03:57:12 -0700364TEST_F(MAYBE_CallSessionFileRotatingStreamTest, WriteAndReadFirstHalf) {
tkchin93411912015-07-22 12:12:17 -0700365 Init("CallSessionFileRotatingStreamTestWriteAndReadFirstHalf",
366 6 * 1024 * 1024);
367 ASSERT_TRUE(stream_->Open());
368 const size_t buffer_size = 1024 * 1024;
jbauch555604a2016-04-26 03:13:22 -0700369 std::unique_ptr<uint8_t[]> buffer(new uint8_t[buffer_size]);
tkchin93411912015-07-22 12:12:17 -0700370 for (int i = 0; i < 2; i++) {
371 memset(buffer.get(), i, buffer_size);
372 EXPECT_EQ(SR_SUCCESS,
373 stream_->WriteAll(buffer.get(), buffer_size, nullptr, nullptr));
374 }
375
Niels Möller6ffe62a2019-01-08 13:22:57 +0100376 const int expected_vals[] = {0, 1};
377 const size_t expected_size = buffer_size * arraysize(expected_vals);
378
379 CallSessionFileRotatingStreamReader reader(dir_path_);
380 EXPECT_EQ(reader.GetSize(), expected_size);
381 std::unique_ptr<uint8_t[]> contents(new uint8_t[expected_size + 1]);
382 EXPECT_EQ(reader.ReadAll(contents.get(), expected_size + 1), expected_size);
383
tkchin93411912015-07-22 12:12:17 -0700384 for (size_t i = 0; i < arraysize(expected_vals); ++i) {
Niels Möller6ffe62a2019-01-08 13:22:57 +0100385 const uint8_t* block = contents.get() + i * buffer_size;
386 bool match = true;
387 for (size_t j = 0; j < buffer_size; j++) {
388 if (block[j] != expected_vals[i]) {
389 match = false;
390 break;
391 }
392 }
393 // EXPECT call at end of loop, to limit the number of messages on failure.
394 EXPECT_TRUE(match);
tkchin93411912015-07-22 12:12:17 -0700395 }
tkchin93411912015-07-22 12:12:17 -0700396}
397
398} // namespace rtc