blob: 22e247f225811c4009b58f78df3f590031894f6d [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
Yves Gerey3e707812018-11-28 16:47:49 +010011#include <string.h>
12#include <cstdint>
jbauch555604a2016-04-26 03:13:22 -070013#include <memory>
14
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020015#include "rtc_base/arraysize.h"
Steve Anton10542f22019-01-11 09:11:00 -080016#include "rtc_base/file_rotating_stream.h"
Yves Gerey3e707812018-11-28 16:47:49 +010017#include "test/gtest.h"
Steve Anton10542f22019-01-11 09:11:00 -080018#include "test/testsupport/file_utils.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) {
Niels Möllerd9ac0582019-01-03 14:21:38 +010083 FileRotatingStreamReader reader(dir_path, file_prefix);
84 EXPECT_EQ(reader.GetSize(), expected_length);
Niels Möller6ffe62a2019-01-08 13:22:57 +010085 std::unique_ptr<uint8_t[]> buffer(new uint8_t[expected_length]);
Niels Möllerd9ac0582019-01-03 14:21:38 +010086 memset(buffer.get(), 0, expected_length);
87 EXPECT_EQ(expected_length, reader.ReadAll(buffer.get(), expected_length));
88 EXPECT_EQ(0, memcmp(expected_contents, buffer.get(), expected_length));
tkchin93411912015-07-22 12:12:17 -070089 }
90
91 void VerifyFileContents(const char* expected_contents,
92 const size_t expected_length,
93 const std::string& file_path) {
Niels Möller0a7d56e2019-01-10 11:24:07 +010094 std::unique_ptr<uint8_t[]> buffer(new uint8_t[expected_length + 1]);
Niels Möller23213d92019-01-22 11:01:24 +010095 webrtc::FileWrapper f = webrtc::FileWrapper::OpenReadOnly(file_path);
96 ASSERT_TRUE(f.is_open());
97 size_t size_read = f.Read(buffer.get(), expected_length + 1);
Niels Möller6ffe62a2019-01-08 13:22:57 +010098 EXPECT_EQ(size_read, expected_length);
99 EXPECT_EQ(0, memcmp(expected_contents, buffer.get(),
100 std::min(expected_length, size_read)));
tkchin93411912015-07-22 12:12:17 -0700101 }
102
jbauch555604a2016-04-26 03:13:22 -0700103 std::unique_ptr<FileRotatingStream> stream_;
tkchin93411912015-07-22 12:12:17 -0700104 std::string dir_path_;
105};
106
phoglundbb738732016-07-15 03:57:12 -0700107const char* MAYBE_FileRotatingStreamTest::kFilePrefix =
108 "FileRotatingStreamTest";
109const size_t MAYBE_FileRotatingStreamTest::kMaxFileSize = 2;
tkchin93411912015-07-22 12:12:17 -0700110
111// Tests that stream state is correct before and after Open / Close.
phoglundbb738732016-07-15 03:57:12 -0700112TEST_F(MAYBE_FileRotatingStreamTest, State) {
tkchin93411912015-07-22 12:12:17 -0700113 Init("FileRotatingStreamTestState", kFilePrefix, kMaxFileSize, 3);
114
115 EXPECT_EQ(SS_CLOSED, stream_->GetState());
116 ASSERT_TRUE(stream_->Open());
117 EXPECT_EQ(SS_OPEN, stream_->GetState());
118 stream_->Close();
119 EXPECT_EQ(SS_CLOSED, stream_->GetState());
120}
121
122// Tests that nothing is written to file when data of length zero is written.
phoglundbb738732016-07-15 03:57:12 -0700123TEST_F(MAYBE_FileRotatingStreamTest, EmptyWrite) {
tkchin93411912015-07-22 12:12:17 -0700124 Init("FileRotatingStreamTestEmptyWrite", kFilePrefix, kMaxFileSize, 3);
125
126 ASSERT_TRUE(stream_->Open());
127 WriteAndFlush("a", 0);
128
129 std::string logfile_path = stream_->GetFilePath(0);
Niels Möller23213d92019-01-22 11:01:24 +0100130 webrtc::FileWrapper f = webrtc::FileWrapper::OpenReadOnly(logfile_path);
131 ASSERT_TRUE(f.is_open());
Niels Möller0a7d56e2019-01-10 11:24:07 +0100132 char buf[1];
Niels Möller23213d92019-01-22 11:01:24 +0100133 EXPECT_EQ(0u, f.Read(buf, sizeof(buf)));
tkchin93411912015-07-22 12:12:17 -0700134}
135
136// Tests that a write operation followed by a read returns the expected data
137// and writes to the expected files.
phoglundbb738732016-07-15 03:57:12 -0700138TEST_F(MAYBE_FileRotatingStreamTest, WriteAndRead) {
tkchin93411912015-07-22 12:12:17 -0700139 Init("FileRotatingStreamTestWriteAndRead", kFilePrefix, kMaxFileSize, 3);
140
141 ASSERT_TRUE(stream_->Open());
142 // The test is set up to create three log files of length 2. Write and check
143 // contents.
144 std::string messages[3] = {"aa", "bb", "cc"};
145 for (size_t i = 0; i < arraysize(messages); ++i) {
146 const std::string& message = messages[i];
147 WriteAndFlush(message.c_str(), message.size());
148 // Since the max log size is 2, we will be causing rotation. Read from the
149 // next file.
150 VerifyFileContents(message.c_str(), message.size(),
151 stream_->GetFilePath(1));
152 }
153 // Check that exactly three files exist.
154 for (size_t i = 0; i < arraysize(messages); ++i) {
Niels Möller260770c2018-11-07 15:08:18 +0100155 EXPECT_TRUE(webrtc::test::FileExists(stream_->GetFilePath(i)));
tkchin93411912015-07-22 12:12:17 -0700156 }
157 std::string message("d");
158 WriteAndFlush(message.c_str(), message.size());
159 for (size_t i = 0; i < arraysize(messages); ++i) {
Niels Möller260770c2018-11-07 15:08:18 +0100160 EXPECT_TRUE(webrtc::test::FileExists(stream_->GetFilePath(i)));
tkchin93411912015-07-22 12:12:17 -0700161 }
162 // TODO(tkchin): Maybe check all the files in the dir.
163
164 // Reopen for read.
165 std::string expected_contents("bbccd");
166 VerifyStreamRead(expected_contents.c_str(), expected_contents.size(),
167 dir_path_, kFilePrefix);
168}
169
Niels Möller7b3c76b2018-11-07 09:54:28 +0100170// Tests that a write operation (with dir name without delimiter) followed by a
171// read returns the expected data and writes to the expected files.
172TEST_F(MAYBE_FileRotatingStreamTest, WriteWithoutDelimiterAndRead) {
173 Init("FileRotatingStreamTestWriteWithoutDelimiterAndRead", kFilePrefix,
174 kMaxFileSize, 3,
175 /* ensure_trailing_delimiter*/ false);
176
177 ASSERT_TRUE(stream_->Open());
178 // The test is set up to create three log files of length 2. Write and check
179 // contents.
180 std::string messages[3] = {"aa", "bb", "cc"};
181 for (size_t i = 0; i < arraysize(messages); ++i) {
182 const std::string& message = messages[i];
183 WriteAndFlush(message.c_str(), message.size());
184 }
185 std::string message("d");
186 WriteAndFlush(message.c_str(), message.size());
187
188 // Reopen for read.
189 std::string expected_contents("bbccd");
190 VerifyStreamRead(expected_contents.c_str(), expected_contents.size(),
191 dir_path_ + webrtc::test::kPathDelimiter, kFilePrefix);
192}
193
194// Tests that a write operation followed by a read (without trailing delimiter)
195// returns the expected data and writes to the expected files.
196TEST_F(MAYBE_FileRotatingStreamTest, WriteAndReadWithoutDelimiter) {
197 Init("FileRotatingStreamTestWriteAndReadWithoutDelimiter", kFilePrefix,
198 kMaxFileSize, 3);
199
200 ASSERT_TRUE(stream_->Open());
201 // The test is set up to create three log files of length 2. Write and check
202 // contents.
203 std::string messages[3] = {"aa", "bb", "cc"};
204 for (size_t i = 0; i < arraysize(messages); ++i) {
205 const std::string& message = messages[i];
206 WriteAndFlush(message.c_str(), message.size());
207 }
208 std::string message("d");
209 WriteAndFlush(message.c_str(), message.size());
210
211 // Reopen for read.
212 std::string expected_contents("bbccd");
213 VerifyStreamRead(expected_contents.c_str(), expected_contents.size(),
214 dir_path_.substr(0, dir_path_.size() - 1), kFilePrefix);
215}
216
tkchin93411912015-07-22 12:12:17 -0700217// Tests that writing data greater than the total capacity of the files
218// overwrites the files correctly and is read correctly after.
phoglundbb738732016-07-15 03:57:12 -0700219TEST_F(MAYBE_FileRotatingStreamTest, WriteOverflowAndRead) {
tkchin93411912015-07-22 12:12:17 -0700220 Init("FileRotatingStreamTestWriteOverflowAndRead", kFilePrefix, kMaxFileSize,
221 3);
222 ASSERT_TRUE(stream_->Open());
223 // This should cause overflow across all three files, such that the first file
224 // we wrote to also gets overwritten.
225 std::string message("foobarbaz");
226 WriteAndFlush(message.c_str(), message.size());
227 std::string expected_file_contents("z");
228 VerifyFileContents(expected_file_contents.c_str(),
229 expected_file_contents.size(), stream_->GetFilePath(0));
230 std::string expected_stream_contents("arbaz");
231 VerifyStreamRead(expected_stream_contents.c_str(),
232 expected_stream_contents.size(), dir_path_, kFilePrefix);
233}
234
235// Tests that the returned file paths have the right folder and prefix.
phoglundbb738732016-07-15 03:57:12 -0700236TEST_F(MAYBE_FileRotatingStreamTest, GetFilePath) {
tkchin93411912015-07-22 12:12:17 -0700237 Init("FileRotatingStreamTestGetFilePath", kFilePrefix, kMaxFileSize, 20);
Niels Möller394b4eb2018-06-01 14:08:25 +0200238 // dir_path_ includes a trailing delimiter.
239 const std::string prefix = dir_path_ + kFilePrefix;
tkchin93411912015-07-22 12:12:17 -0700240 for (auto i = 0; i < 20; ++i) {
Niels Möller394b4eb2018-06-01 14:08:25 +0200241 EXPECT_EQ(0, stream_->GetFilePath(i).compare(0, prefix.size(), prefix));
tkchin93411912015-07-22 12:12:17 -0700242 }
243}
244
Yves Gerey665174f2018-06-19 15:03:05 +0200245#if defined(WEBRTC_ANDROID)
phoglundbb738732016-07-15 03:57:12 -0700246// Fails on Android: https://bugs.chromium.org/p/webrtc/issues/detail?id=4364.
247#define MAYBE_CallSessionFileRotatingStreamTest \
Yves Gerey665174f2018-06-19 15:03:05 +0200248 DISABLED_CallSessionFileRotatingStreamTest
phoglundbb738732016-07-15 03:57:12 -0700249#else
250#define MAYBE_CallSessionFileRotatingStreamTest \
Yves Gerey665174f2018-06-19 15:03:05 +0200251 CallSessionFileRotatingStreamTest
phoglundbb738732016-07-15 03:57:12 -0700252#endif
253
254class MAYBE_CallSessionFileRotatingStreamTest : public ::testing::Test {
tkchin93411912015-07-22 12:12:17 -0700255 protected:
256 void Init(const std::string& dir_name, size_t max_total_log_size) {
nisse57efb032017-05-18 03:55:59 -0700257 dir_path_ = webrtc::test::OutputPath();
258
tkchin93411912015-07-22 12:12:17 -0700259 // Append per-test output path in order to run within gtest parallel.
nisse57efb032017-05-18 03:55:59 -0700260 dir_path_.append(dir_name);
Niels Möller7b3c76b2018-11-07 09:54:28 +0100261 dir_path_.append(webrtc::test::kPathDelimiter);
nisse57efb032017-05-18 03:55:59 -0700262 ASSERT_TRUE(webrtc::test::CreateDir(dir_path_));
tkchin93411912015-07-22 12:12:17 -0700263 stream_.reset(
264 new CallSessionFileRotatingStream(dir_path_, max_total_log_size));
265 }
266
Steve Anton9de3aac2017-10-24 10:08:26 -0700267 void TearDown() override {
nisse57efb032017-05-18 03:55:59 -0700268 // On windows, open files can't be removed.
269 stream_->Close();
270 CleanupLogDirectory(*stream_);
271 EXPECT_TRUE(webrtc::test::RemoveDir(dir_path_));
272
tkchin93411912015-07-22 12:12:17 -0700273 stream_.reset();
tkchin93411912015-07-22 12:12:17 -0700274 }
275
276 // Writes the data to the stream and flushes it.
277 void WriteAndFlush(const void* data, const size_t data_len) {
278 EXPECT_EQ(SR_SUCCESS, stream_->WriteAll(data, data_len, nullptr, nullptr));
279 EXPECT_TRUE(stream_->Flush());
280 }
281
282 // Checks that the stream reads in the expected contents and then returns an
283 // end of stream result.
284 void VerifyStreamRead(const char* expected_contents,
285 const size_t expected_length,
286 const std::string& dir_path) {
Niels Möllerd9ac0582019-01-03 14:21:38 +0100287 CallSessionFileRotatingStreamReader reader(dir_path);
288 EXPECT_EQ(reader.GetSize(), expected_length);
Niels Möller6ffe62a2019-01-08 13:22:57 +0100289 std::unique_ptr<uint8_t[]> buffer(new uint8_t[expected_length]);
Niels Möllerd9ac0582019-01-03 14:21:38 +0100290 memset(buffer.get(), 0, expected_length);
291 EXPECT_EQ(expected_length, reader.ReadAll(buffer.get(), expected_length));
292 EXPECT_EQ(0, memcmp(expected_contents, buffer.get(), expected_length));
tkchin93411912015-07-22 12:12:17 -0700293 }
294
jbauch555604a2016-04-26 03:13:22 -0700295 std::unique_ptr<CallSessionFileRotatingStream> stream_;
tkchin93411912015-07-22 12:12:17 -0700296 std::string dir_path_;
297};
298
299// Tests that writing and reading to a stream with the smallest possible
300// capacity works.
phoglundbb738732016-07-15 03:57:12 -0700301TEST_F(MAYBE_CallSessionFileRotatingStreamTest, WriteAndReadSmallest) {
tkchin93411912015-07-22 12:12:17 -0700302 Init("CallSessionFileRotatingStreamTestWriteAndReadSmallest", 4);
303
304 ASSERT_TRUE(stream_->Open());
305 std::string message("abcde");
306 WriteAndFlush(message.c_str(), message.size());
307 std::string expected_contents("abe");
308 VerifyStreamRead(expected_contents.c_str(), expected_contents.size(),
309 dir_path_);
310}
311
312// Tests that writing and reading to a stream with capacity lesser than 4MB
313// behaves correctly.
phoglundbb738732016-07-15 03:57:12 -0700314TEST_F(MAYBE_CallSessionFileRotatingStreamTest, WriteAndReadSmall) {
tkchin93411912015-07-22 12:12:17 -0700315 Init("CallSessionFileRotatingStreamTestWriteAndReadSmall", 8);
316
317 ASSERT_TRUE(stream_->Open());
318 std::string message("123456789");
319 WriteAndFlush(message.c_str(), message.size());
320 std::string expected_contents("1234789");
321 VerifyStreamRead(expected_contents.c_str(), expected_contents.size(),
322 dir_path_);
323}
324
325// Tests that writing and reading to a stream with capacity greater than 4MB
326// behaves correctly.
phoglundbb738732016-07-15 03:57:12 -0700327TEST_F(MAYBE_CallSessionFileRotatingStreamTest, WriteAndReadLarge) {
tkchin93411912015-07-22 12:12:17 -0700328 Init("CallSessionFileRotatingStreamTestWriteAndReadLarge", 6 * 1024 * 1024);
329
330 ASSERT_TRUE(stream_->Open());
331 const size_t buffer_size = 1024 * 1024;
jbauch555604a2016-04-26 03:13:22 -0700332 std::unique_ptr<uint8_t[]> buffer(new uint8_t[buffer_size]);
tkchin93411912015-07-22 12:12:17 -0700333 for (int i = 0; i < 8; i++) {
334 memset(buffer.get(), i, buffer_size);
335 EXPECT_EQ(SR_SUCCESS,
336 stream_->WriteAll(buffer.get(), buffer_size, nullptr, nullptr));
337 }
338
Niels Möller6ffe62a2019-01-08 13:22:57 +0100339 const int expected_vals[] = {0, 1, 2, 6, 7};
340 const size_t expected_size = buffer_size * arraysize(expected_vals);
341
342 CallSessionFileRotatingStreamReader reader(dir_path_);
343 EXPECT_EQ(reader.GetSize(), expected_size);
344 std::unique_ptr<uint8_t[]> contents(new uint8_t[expected_size + 1]);
345 EXPECT_EQ(reader.ReadAll(contents.get(), expected_size + 1), expected_size);
tkchin93411912015-07-22 12:12:17 -0700346 for (size_t i = 0; i < arraysize(expected_vals); ++i) {
Niels Möller6ffe62a2019-01-08 13:22:57 +0100347 const uint8_t* block = contents.get() + i * buffer_size;
348 bool match = true;
349 for (size_t j = 0; j < buffer_size; j++) {
350 if (block[j] != expected_vals[i]) {
351 match = false;
352 break;
353 }
354 }
355 // EXPECT call at end of loop, to limit the number of messages on failure.
356 EXPECT_TRUE(match);
tkchin93411912015-07-22 12:12:17 -0700357 }
tkchin93411912015-07-22 12:12:17 -0700358}
359
360// Tests that writing and reading to a stream where only the first file is
361// written to behaves correctly.
phoglundbb738732016-07-15 03:57:12 -0700362TEST_F(MAYBE_CallSessionFileRotatingStreamTest, WriteAndReadFirstHalf) {
tkchin93411912015-07-22 12:12:17 -0700363 Init("CallSessionFileRotatingStreamTestWriteAndReadFirstHalf",
364 6 * 1024 * 1024);
365 ASSERT_TRUE(stream_->Open());
366 const size_t buffer_size = 1024 * 1024;
jbauch555604a2016-04-26 03:13:22 -0700367 std::unique_ptr<uint8_t[]> buffer(new uint8_t[buffer_size]);
tkchin93411912015-07-22 12:12:17 -0700368 for (int i = 0; i < 2; i++) {
369 memset(buffer.get(), i, buffer_size);
370 EXPECT_EQ(SR_SUCCESS,
371 stream_->WriteAll(buffer.get(), buffer_size, nullptr, nullptr));
372 }
373
Niels Möller6ffe62a2019-01-08 13:22:57 +0100374 const int expected_vals[] = {0, 1};
375 const size_t expected_size = buffer_size * arraysize(expected_vals);
376
377 CallSessionFileRotatingStreamReader reader(dir_path_);
378 EXPECT_EQ(reader.GetSize(), expected_size);
379 std::unique_ptr<uint8_t[]> contents(new uint8_t[expected_size + 1]);
380 EXPECT_EQ(reader.ReadAll(contents.get(), expected_size + 1), expected_size);
381
tkchin93411912015-07-22 12:12:17 -0700382 for (size_t i = 0; i < arraysize(expected_vals); ++i) {
Niels Möller6ffe62a2019-01-08 13:22:57 +0100383 const uint8_t* block = contents.get() + i * buffer_size;
384 bool match = true;
385 for (size_t j = 0; j < buffer_size; j++) {
386 if (block[j] != expected_vals[i]) {
387 match = false;
388 break;
389 }
390 }
391 // EXPECT call at end of loop, to limit the number of messages on failure.
392 EXPECT_TRUE(match);
tkchin93411912015-07-22 12:12:17 -0700393 }
tkchin93411912015-07-22 12:12:17 -0700394}
395
396} // namespace rtc