blob: 63ddc96975e7c3eaa09595d9241a9bec2ff1d242 [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"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020016#include "rtc_base/filerotatingstream.h"
Yves Gerey3e707812018-11-28 16:47:49 +010017#include "test/gtest.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020018#include "test/testsupport/fileutils.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]);
nisse21083262017-05-31 02:07:21 -070095 FileStream stream;
96 ASSERT_TRUE(stream.Open(file_path, "r", nullptr));
Niels Möller6ffe62a2019-01-08 13:22:57 +010097 size_t size_read = 0;
Niels Möller0a7d56e2019-01-10 11:24:07 +010098 EXPECT_EQ(rtc::SR_EOS, stream.ReadAll(buffer.get(), expected_length + 1,
99 &size_read, nullptr));
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);
nisse21083262017-05-31 02:07:21 -0700132 FileStream stream;
133 ASSERT_TRUE(stream.Open(logfile_path, "r", nullptr));
Niels Möller0a7d56e2019-01-10 11:24:07 +0100134 char buf[1];
135 size_t read_nbytes;
136 int read_error;
137 EXPECT_EQ(SR_EOS, stream.Read(buf, sizeof(buf), &read_nbytes, &read_error));
tkchin93411912015-07-22 12:12:17 -0700138}
139
140// Tests that a write operation followed by a read returns the expected data
141// and writes to the expected files.
phoglundbb738732016-07-15 03:57:12 -0700142TEST_F(MAYBE_FileRotatingStreamTest, WriteAndRead) {
tkchin93411912015-07-22 12:12:17 -0700143 Init("FileRotatingStreamTestWriteAndRead", kFilePrefix, kMaxFileSize, 3);
144
145 ASSERT_TRUE(stream_->Open());
146 // The test is set up to create three log files of length 2. Write and check
147 // contents.
148 std::string messages[3] = {"aa", "bb", "cc"};
149 for (size_t i = 0; i < arraysize(messages); ++i) {
150 const std::string& message = messages[i];
151 WriteAndFlush(message.c_str(), message.size());
152 // Since the max log size is 2, we will be causing rotation. Read from the
153 // next file.
154 VerifyFileContents(message.c_str(), message.size(),
155 stream_->GetFilePath(1));
156 }
157 // Check that exactly three files exist.
158 for (size_t i = 0; i < arraysize(messages); ++i) {
Niels Möller260770c2018-11-07 15:08:18 +0100159 EXPECT_TRUE(webrtc::test::FileExists(stream_->GetFilePath(i)));
tkchin93411912015-07-22 12:12:17 -0700160 }
161 std::string message("d");
162 WriteAndFlush(message.c_str(), message.size());
163 for (size_t i = 0; i < arraysize(messages); ++i) {
Niels Möller260770c2018-11-07 15:08:18 +0100164 EXPECT_TRUE(webrtc::test::FileExists(stream_->GetFilePath(i)));
tkchin93411912015-07-22 12:12:17 -0700165 }
166 // TODO(tkchin): Maybe check all the files in the dir.
167
168 // Reopen for read.
169 std::string expected_contents("bbccd");
170 VerifyStreamRead(expected_contents.c_str(), expected_contents.size(),
171 dir_path_, kFilePrefix);
172}
173
Niels Möller7b3c76b2018-11-07 09:54:28 +0100174// Tests that a write operation (with dir name without delimiter) followed by a
175// read returns the expected data and writes to the expected files.
176TEST_F(MAYBE_FileRotatingStreamTest, WriteWithoutDelimiterAndRead) {
177 Init("FileRotatingStreamTestWriteWithoutDelimiterAndRead", kFilePrefix,
178 kMaxFileSize, 3,
179 /* ensure_trailing_delimiter*/ false);
180
181 ASSERT_TRUE(stream_->Open());
182 // The test is set up to create three log files of length 2. Write and check
183 // contents.
184 std::string messages[3] = {"aa", "bb", "cc"};
185 for (size_t i = 0; i < arraysize(messages); ++i) {
186 const std::string& message = messages[i];
187 WriteAndFlush(message.c_str(), message.size());
188 }
189 std::string message("d");
190 WriteAndFlush(message.c_str(), message.size());
191
192 // Reopen for read.
193 std::string expected_contents("bbccd");
194 VerifyStreamRead(expected_contents.c_str(), expected_contents.size(),
195 dir_path_ + webrtc::test::kPathDelimiter, kFilePrefix);
196}
197
198// Tests that a write operation followed by a read (without trailing delimiter)
199// returns the expected data and writes to the expected files.
200TEST_F(MAYBE_FileRotatingStreamTest, WriteAndReadWithoutDelimiter) {
201 Init("FileRotatingStreamTestWriteAndReadWithoutDelimiter", kFilePrefix,
202 kMaxFileSize, 3);
203
204 ASSERT_TRUE(stream_->Open());
205 // The test is set up to create three log files of length 2. Write and check
206 // contents.
207 std::string messages[3] = {"aa", "bb", "cc"};
208 for (size_t i = 0; i < arraysize(messages); ++i) {
209 const std::string& message = messages[i];
210 WriteAndFlush(message.c_str(), message.size());
211 }
212 std::string message("d");
213 WriteAndFlush(message.c_str(), message.size());
214
215 // Reopen for read.
216 std::string expected_contents("bbccd");
217 VerifyStreamRead(expected_contents.c_str(), expected_contents.size(),
218 dir_path_.substr(0, dir_path_.size() - 1), kFilePrefix);
219}
220
tkchin93411912015-07-22 12:12:17 -0700221// Tests that writing data greater than the total capacity of the files
222// overwrites the files correctly and is read correctly after.
phoglundbb738732016-07-15 03:57:12 -0700223TEST_F(MAYBE_FileRotatingStreamTest, WriteOverflowAndRead) {
tkchin93411912015-07-22 12:12:17 -0700224 Init("FileRotatingStreamTestWriteOverflowAndRead", kFilePrefix, kMaxFileSize,
225 3);
226 ASSERT_TRUE(stream_->Open());
227 // This should cause overflow across all three files, such that the first file
228 // we wrote to also gets overwritten.
229 std::string message("foobarbaz");
230 WriteAndFlush(message.c_str(), message.size());
231 std::string expected_file_contents("z");
232 VerifyFileContents(expected_file_contents.c_str(),
233 expected_file_contents.size(), stream_->GetFilePath(0));
234 std::string expected_stream_contents("arbaz");
235 VerifyStreamRead(expected_stream_contents.c_str(),
236 expected_stream_contents.size(), dir_path_, kFilePrefix);
237}
238
239// Tests that the returned file paths have the right folder and prefix.
phoglundbb738732016-07-15 03:57:12 -0700240TEST_F(MAYBE_FileRotatingStreamTest, GetFilePath) {
tkchin93411912015-07-22 12:12:17 -0700241 Init("FileRotatingStreamTestGetFilePath", kFilePrefix, kMaxFileSize, 20);
Niels Möller394b4eb2018-06-01 14:08:25 +0200242 // dir_path_ includes a trailing delimiter.
243 const std::string prefix = dir_path_ + kFilePrefix;
tkchin93411912015-07-22 12:12:17 -0700244 for (auto i = 0; i < 20; ++i) {
Niels Möller394b4eb2018-06-01 14:08:25 +0200245 EXPECT_EQ(0, stream_->GetFilePath(i).compare(0, prefix.size(), prefix));
tkchin93411912015-07-22 12:12:17 -0700246 }
247}
248
Yves Gerey665174f2018-06-19 15:03:05 +0200249#if defined(WEBRTC_ANDROID)
phoglundbb738732016-07-15 03:57:12 -0700250// Fails on Android: https://bugs.chromium.org/p/webrtc/issues/detail?id=4364.
251#define MAYBE_CallSessionFileRotatingStreamTest \
Yves Gerey665174f2018-06-19 15:03:05 +0200252 DISABLED_CallSessionFileRotatingStreamTest
phoglundbb738732016-07-15 03:57:12 -0700253#else
254#define MAYBE_CallSessionFileRotatingStreamTest \
Yves Gerey665174f2018-06-19 15:03:05 +0200255 CallSessionFileRotatingStreamTest
phoglundbb738732016-07-15 03:57:12 -0700256#endif
257
258class MAYBE_CallSessionFileRotatingStreamTest : public ::testing::Test {
tkchin93411912015-07-22 12:12:17 -0700259 protected:
260 void Init(const std::string& dir_name, size_t max_total_log_size) {
nisse57efb032017-05-18 03:55:59 -0700261 dir_path_ = webrtc::test::OutputPath();
262
tkchin93411912015-07-22 12:12:17 -0700263 // Append per-test output path in order to run within gtest parallel.
nisse57efb032017-05-18 03:55:59 -0700264 dir_path_.append(dir_name);
Niels Möller7b3c76b2018-11-07 09:54:28 +0100265 dir_path_.append(webrtc::test::kPathDelimiter);
nisse57efb032017-05-18 03:55:59 -0700266 ASSERT_TRUE(webrtc::test::CreateDir(dir_path_));
tkchin93411912015-07-22 12:12:17 -0700267 stream_.reset(
268 new CallSessionFileRotatingStream(dir_path_, max_total_log_size));
269 }
270
Steve Anton9de3aac2017-10-24 10:08:26 -0700271 void TearDown() override {
nisse57efb032017-05-18 03:55:59 -0700272 // On windows, open files can't be removed.
273 stream_->Close();
274 CleanupLogDirectory(*stream_);
275 EXPECT_TRUE(webrtc::test::RemoveDir(dir_path_));
276
tkchin93411912015-07-22 12:12:17 -0700277 stream_.reset();
tkchin93411912015-07-22 12:12:17 -0700278 }
279
280 // Writes the data to the stream and flushes it.
281 void WriteAndFlush(const void* data, const size_t data_len) {
282 EXPECT_EQ(SR_SUCCESS, stream_->WriteAll(data, data_len, nullptr, nullptr));
283 EXPECT_TRUE(stream_->Flush());
284 }
285
286 // Checks that the stream reads in the expected contents and then returns an
287 // end of stream result.
288 void VerifyStreamRead(const char* expected_contents,
289 const size_t expected_length,
290 const std::string& dir_path) {
Niels Möllerd9ac0582019-01-03 14:21:38 +0100291 CallSessionFileRotatingStreamReader reader(dir_path);
292 EXPECT_EQ(reader.GetSize(), expected_length);
Niels Möller6ffe62a2019-01-08 13:22:57 +0100293 std::unique_ptr<uint8_t[]> buffer(new uint8_t[expected_length]);
Niels Möllerd9ac0582019-01-03 14:21:38 +0100294 memset(buffer.get(), 0, expected_length);
295 EXPECT_EQ(expected_length, reader.ReadAll(buffer.get(), expected_length));
296 EXPECT_EQ(0, memcmp(expected_contents, buffer.get(), expected_length));
tkchin93411912015-07-22 12:12:17 -0700297 }
298
jbauch555604a2016-04-26 03:13:22 -0700299 std::unique_ptr<CallSessionFileRotatingStream> stream_;
tkchin93411912015-07-22 12:12:17 -0700300 std::string dir_path_;
301};
302
303// Tests that writing and reading to a stream with the smallest possible
304// capacity works.
phoglundbb738732016-07-15 03:57:12 -0700305TEST_F(MAYBE_CallSessionFileRotatingStreamTest, WriteAndReadSmallest) {
tkchin93411912015-07-22 12:12:17 -0700306 Init("CallSessionFileRotatingStreamTestWriteAndReadSmallest", 4);
307
308 ASSERT_TRUE(stream_->Open());
309 std::string message("abcde");
310 WriteAndFlush(message.c_str(), message.size());
311 std::string expected_contents("abe");
312 VerifyStreamRead(expected_contents.c_str(), expected_contents.size(),
313 dir_path_);
314}
315
316// Tests that writing and reading to a stream with capacity lesser than 4MB
317// behaves correctly.
phoglundbb738732016-07-15 03:57:12 -0700318TEST_F(MAYBE_CallSessionFileRotatingStreamTest, WriteAndReadSmall) {
tkchin93411912015-07-22 12:12:17 -0700319 Init("CallSessionFileRotatingStreamTestWriteAndReadSmall", 8);
320
321 ASSERT_TRUE(stream_->Open());
322 std::string message("123456789");
323 WriteAndFlush(message.c_str(), message.size());
324 std::string expected_contents("1234789");
325 VerifyStreamRead(expected_contents.c_str(), expected_contents.size(),
326 dir_path_);
327}
328
329// Tests that writing and reading to a stream with capacity greater than 4MB
330// behaves correctly.
phoglundbb738732016-07-15 03:57:12 -0700331TEST_F(MAYBE_CallSessionFileRotatingStreamTest, WriteAndReadLarge) {
tkchin93411912015-07-22 12:12:17 -0700332 Init("CallSessionFileRotatingStreamTestWriteAndReadLarge", 6 * 1024 * 1024);
333
334 ASSERT_TRUE(stream_->Open());
335 const size_t buffer_size = 1024 * 1024;
jbauch555604a2016-04-26 03:13:22 -0700336 std::unique_ptr<uint8_t[]> buffer(new uint8_t[buffer_size]);
tkchin93411912015-07-22 12:12:17 -0700337 for (int i = 0; i < 8; i++) {
338 memset(buffer.get(), i, buffer_size);
339 EXPECT_EQ(SR_SUCCESS,
340 stream_->WriteAll(buffer.get(), buffer_size, nullptr, nullptr));
341 }
342
Niels Möller6ffe62a2019-01-08 13:22:57 +0100343 const int expected_vals[] = {0, 1, 2, 6, 7};
344 const size_t expected_size = buffer_size * arraysize(expected_vals);
345
346 CallSessionFileRotatingStreamReader reader(dir_path_);
347 EXPECT_EQ(reader.GetSize(), expected_size);
348 std::unique_ptr<uint8_t[]> contents(new uint8_t[expected_size + 1]);
349 EXPECT_EQ(reader.ReadAll(contents.get(), expected_size + 1), expected_size);
tkchin93411912015-07-22 12:12:17 -0700350 for (size_t i = 0; i < arraysize(expected_vals); ++i) {
Niels Möller6ffe62a2019-01-08 13:22:57 +0100351 const uint8_t* block = contents.get() + i * buffer_size;
352 bool match = true;
353 for (size_t j = 0; j < buffer_size; j++) {
354 if (block[j] != expected_vals[i]) {
355 match = false;
356 break;
357 }
358 }
359 // EXPECT call at end of loop, to limit the number of messages on failure.
360 EXPECT_TRUE(match);
tkchin93411912015-07-22 12:12:17 -0700361 }
tkchin93411912015-07-22 12:12:17 -0700362}
363
364// Tests that writing and reading to a stream where only the first file is
365// written to behaves correctly.
phoglundbb738732016-07-15 03:57:12 -0700366TEST_F(MAYBE_CallSessionFileRotatingStreamTest, WriteAndReadFirstHalf) {
tkchin93411912015-07-22 12:12:17 -0700367 Init("CallSessionFileRotatingStreamTestWriteAndReadFirstHalf",
368 6 * 1024 * 1024);
369 ASSERT_TRUE(stream_->Open());
370 const size_t buffer_size = 1024 * 1024;
jbauch555604a2016-04-26 03:13:22 -0700371 std::unique_ptr<uint8_t[]> buffer(new uint8_t[buffer_size]);
tkchin93411912015-07-22 12:12:17 -0700372 for (int i = 0; i < 2; i++) {
373 memset(buffer.get(), i, buffer_size);
374 EXPECT_EQ(SR_SUCCESS,
375 stream_->WriteAll(buffer.get(), buffer_size, nullptr, nullptr));
376 }
377
Niels Möller6ffe62a2019-01-08 13:22:57 +0100378 const int expected_vals[] = {0, 1};
379 const size_t expected_size = buffer_size * arraysize(expected_vals);
380
381 CallSessionFileRotatingStreamReader reader(dir_path_);
382 EXPECT_EQ(reader.GetSize(), expected_size);
383 std::unique_ptr<uint8_t[]> contents(new uint8_t[expected_size + 1]);
384 EXPECT_EQ(reader.ReadAll(contents.get(), expected_size + 1), expected_size);
385
tkchin93411912015-07-22 12:12:17 -0700386 for (size_t i = 0; i < arraysize(expected_vals); ++i) {
Niels Möller6ffe62a2019-01-08 13:22:57 +0100387 const uint8_t* block = contents.get() + i * buffer_size;
388 bool match = true;
389 for (size_t j = 0; j < buffer_size; j++) {
390 if (block[j] != expected_vals[i]) {
391 match = false;
392 break;
393 }
394 }
395 // EXPECT call at end of loop, to limit the number of messages on failure.
396 EXPECT_TRUE(match);
tkchin93411912015-07-22 12:12:17 -0700397 }
tkchin93411912015-07-22 12:12:17 -0700398}
399
400} // namespace rtc