| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2015 The WebRTC Project Authors. All rights reserved. | 2 * Copyright 2015 The WebRTC Project Authors. All rights reserved. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
| 7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
| 8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
| 9 */ | 9 */ |
| 10 | 10 |
| 11 #include <memory> |
| 12 |
| 11 #include "webrtc/base/arraysize.h" | 13 #include "webrtc/base/arraysize.h" |
| 12 #include "webrtc/base/checks.h" | 14 #include "webrtc/base/checks.h" |
| 13 #include "webrtc/base/filerotatingstream.h" | 15 #include "webrtc/base/filerotatingstream.h" |
| 14 #include "webrtc/base/fileutils.h" | 16 #include "webrtc/base/fileutils.h" |
| 15 #include "webrtc/base/gunit.h" | 17 #include "webrtc/base/gunit.h" |
| 16 #include "webrtc/base/pathutils.h" | 18 #include "webrtc/base/pathutils.h" |
| 17 | 19 |
| 18 namespace rtc { | 20 namespace rtc { |
| 19 | 21 |
| 20 class FileRotatingStreamTest : public ::testing::Test { | 22 class FileRotatingStreamTest : public ::testing::Test { |
| (...skipping 29 matching lines...) Expand all Loading... |
| 50 EXPECT_EQ(SR_SUCCESS, stream_->WriteAll(data, data_len, nullptr, nullptr)); | 52 EXPECT_EQ(SR_SUCCESS, stream_->WriteAll(data, data_len, nullptr, nullptr)); |
| 51 EXPECT_TRUE(stream_->Flush()); | 53 EXPECT_TRUE(stream_->Flush()); |
| 52 } | 54 } |
| 53 | 55 |
| 54 // Checks that the stream reads in the expected contents and then returns an | 56 // Checks that the stream reads in the expected contents and then returns an |
| 55 // end of stream result. | 57 // end of stream result. |
| 56 void VerifyStreamRead(const char* expected_contents, | 58 void VerifyStreamRead(const char* expected_contents, |
| 57 const size_t expected_length, | 59 const size_t expected_length, |
| 58 const std::string& dir_path, | 60 const std::string& dir_path, |
| 59 const char* file_prefix) { | 61 const char* file_prefix) { |
| 60 scoped_ptr<FileRotatingStream> stream; | 62 std::unique_ptr<FileRotatingStream> stream; |
| 61 stream.reset(new FileRotatingStream(dir_path, file_prefix)); | 63 stream.reset(new FileRotatingStream(dir_path, file_prefix)); |
| 62 ASSERT_TRUE(stream->Open()); | 64 ASSERT_TRUE(stream->Open()); |
| 63 size_t read = 0; | 65 size_t read = 0; |
| 64 size_t stream_size = 0; | 66 size_t stream_size = 0; |
| 65 EXPECT_TRUE(stream->GetSize(&stream_size)); | 67 EXPECT_TRUE(stream->GetSize(&stream_size)); |
| 66 scoped_ptr<uint8_t[]> buffer(new uint8_t[expected_length]); | 68 std::unique_ptr<uint8_t[]> buffer(new uint8_t[expected_length]); |
| 67 EXPECT_EQ(SR_SUCCESS, | 69 EXPECT_EQ(SR_SUCCESS, |
| 68 stream->ReadAll(buffer.get(), expected_length, &read, nullptr)); | 70 stream->ReadAll(buffer.get(), expected_length, &read, nullptr)); |
| 69 EXPECT_EQ(0, memcmp(expected_contents, buffer.get(), expected_length)); | 71 EXPECT_EQ(0, memcmp(expected_contents, buffer.get(), expected_length)); |
| 70 EXPECT_EQ(SR_EOS, stream->ReadAll(buffer.get(), 1, nullptr, nullptr)); | 72 EXPECT_EQ(SR_EOS, stream->ReadAll(buffer.get(), 1, nullptr, nullptr)); |
| 71 EXPECT_EQ(stream_size, read); | 73 EXPECT_EQ(stream_size, read); |
| 72 } | 74 } |
| 73 | 75 |
| 74 void VerifyFileContents(const char* expected_contents, | 76 void VerifyFileContents(const char* expected_contents, |
| 75 const size_t expected_length, | 77 const size_t expected_length, |
| 76 const std::string& file_path) { | 78 const std::string& file_path) { |
| 77 scoped_ptr<uint8_t[]> buffer(new uint8_t[expected_length]); | 79 std::unique_ptr<uint8_t[]> buffer(new uint8_t[expected_length]); |
| 78 scoped_ptr<FileStream> stream(Filesystem::OpenFile(file_path, "r")); | 80 std::unique_ptr<FileStream> stream(Filesystem::OpenFile(file_path, "r")); |
| 79 EXPECT_TRUE(stream); | 81 EXPECT_TRUE(stream); |
| 80 if (!stream) { | 82 if (!stream) { |
| 81 return; | 83 return; |
| 82 } | 84 } |
| 83 EXPECT_EQ(rtc::SR_SUCCESS, | 85 EXPECT_EQ(rtc::SR_SUCCESS, |
| 84 stream->ReadAll(buffer.get(), expected_length, nullptr, nullptr)); | 86 stream->ReadAll(buffer.get(), expected_length, nullptr, nullptr)); |
| 85 EXPECT_EQ(0, memcmp(expected_contents, buffer.get(), expected_length)); | 87 EXPECT_EQ(0, memcmp(expected_contents, buffer.get(), expected_length)); |
| 86 size_t file_size = 0; | 88 size_t file_size = 0; |
| 87 EXPECT_TRUE(stream->GetSize(&file_size)); | 89 EXPECT_TRUE(stream->GetSize(&file_size)); |
| 88 EXPECT_EQ(file_size, expected_length); | 90 EXPECT_EQ(file_size, expected_length); |
| 89 } | 91 } |
| 90 | 92 |
| 91 scoped_ptr<FileRotatingStream> stream_; | 93 std::unique_ptr<FileRotatingStream> stream_; |
| 92 std::string dir_path_; | 94 std::string dir_path_; |
| 93 }; | 95 }; |
| 94 | 96 |
| 95 const char* FileRotatingStreamTest::kFilePrefix = "FileRotatingStreamTest"; | 97 const char* FileRotatingStreamTest::kFilePrefix = "FileRotatingStreamTest"; |
| 96 const size_t FileRotatingStreamTest::kMaxFileSize = 2; | 98 const size_t FileRotatingStreamTest::kMaxFileSize = 2; |
| 97 | 99 |
| 98 // Tests that stream state is correct before and after Open / Close. | 100 // Tests that stream state is correct before and after Open / Close. |
| 99 TEST_F(FileRotatingStreamTest, State) { | 101 TEST_F(FileRotatingStreamTest, State) { |
| 100 Init("FileRotatingStreamTestState", kFilePrefix, kMaxFileSize, 3); | 102 Init("FileRotatingStreamTestState", kFilePrefix, kMaxFileSize, 3); |
| 101 | 103 |
| 102 EXPECT_EQ(SS_CLOSED, stream_->GetState()); | 104 EXPECT_EQ(SS_CLOSED, stream_->GetState()); |
| 103 ASSERT_TRUE(stream_->Open()); | 105 ASSERT_TRUE(stream_->Open()); |
| 104 EXPECT_EQ(SS_OPEN, stream_->GetState()); | 106 EXPECT_EQ(SS_OPEN, stream_->GetState()); |
| 105 stream_->Close(); | 107 stream_->Close(); |
| 106 EXPECT_EQ(SS_CLOSED, stream_->GetState()); | 108 EXPECT_EQ(SS_CLOSED, stream_->GetState()); |
| 107 } | 109 } |
| 108 | 110 |
| 109 // Tests that nothing is written to file when data of length zero is written. | 111 // Tests that nothing is written to file when data of length zero is written. |
| 110 TEST_F(FileRotatingStreamTest, EmptyWrite) { | 112 TEST_F(FileRotatingStreamTest, EmptyWrite) { |
| 111 Init("FileRotatingStreamTestEmptyWrite", kFilePrefix, kMaxFileSize, 3); | 113 Init("FileRotatingStreamTestEmptyWrite", kFilePrefix, kMaxFileSize, 3); |
| 112 | 114 |
| 113 ASSERT_TRUE(stream_->Open()); | 115 ASSERT_TRUE(stream_->Open()); |
| 114 WriteAndFlush("a", 0); | 116 WriteAndFlush("a", 0); |
| 115 | 117 |
| 116 std::string logfile_path = stream_->GetFilePath(0); | 118 std::string logfile_path = stream_->GetFilePath(0); |
| 117 scoped_ptr<FileStream> stream(Filesystem::OpenFile(logfile_path, "r")); | 119 std::unique_ptr<FileStream> stream(Filesystem::OpenFile(logfile_path, "r")); |
| 118 size_t file_size = 0; | 120 size_t file_size = 0; |
| 119 EXPECT_TRUE(stream->GetSize(&file_size)); | 121 EXPECT_TRUE(stream->GetSize(&file_size)); |
| 120 EXPECT_EQ(0u, file_size); | 122 EXPECT_EQ(0u, file_size); |
| 121 } | 123 } |
| 122 | 124 |
| 123 // Tests that a write operation followed by a read returns the expected data | 125 // Tests that a write operation followed by a read returns the expected data |
| 124 // and writes to the expected files. | 126 // and writes to the expected files. |
| 125 TEST_F(FileRotatingStreamTest, WriteAndRead) { | 127 TEST_F(FileRotatingStreamTest, WriteAndRead) { |
| 126 Init("FileRotatingStreamTestWriteAndRead", kFilePrefix, kMaxFileSize, 3); | 128 Init("FileRotatingStreamTestWriteAndRead", kFilePrefix, kMaxFileSize, 3); |
| 127 | 129 |
| (...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 208 void WriteAndFlush(const void* data, const size_t data_len) { | 210 void WriteAndFlush(const void* data, const size_t data_len) { |
| 209 EXPECT_EQ(SR_SUCCESS, stream_->WriteAll(data, data_len, nullptr, nullptr)); | 211 EXPECT_EQ(SR_SUCCESS, stream_->WriteAll(data, data_len, nullptr, nullptr)); |
| 210 EXPECT_TRUE(stream_->Flush()); | 212 EXPECT_TRUE(stream_->Flush()); |
| 211 } | 213 } |
| 212 | 214 |
| 213 // Checks that the stream reads in the expected contents and then returns an | 215 // Checks that the stream reads in the expected contents and then returns an |
| 214 // end of stream result. | 216 // end of stream result. |
| 215 void VerifyStreamRead(const char* expected_contents, | 217 void VerifyStreamRead(const char* expected_contents, |
| 216 const size_t expected_length, | 218 const size_t expected_length, |
| 217 const std::string& dir_path) { | 219 const std::string& dir_path) { |
| 218 scoped_ptr<CallSessionFileRotatingStream> stream( | 220 std::unique_ptr<CallSessionFileRotatingStream> stream( |
| 219 new CallSessionFileRotatingStream(dir_path)); | 221 new CallSessionFileRotatingStream(dir_path)); |
| 220 ASSERT_TRUE(stream->Open()); | 222 ASSERT_TRUE(stream->Open()); |
| 221 size_t read = 0; | 223 size_t read = 0; |
| 222 size_t stream_size = 0; | 224 size_t stream_size = 0; |
| 223 EXPECT_TRUE(stream->GetSize(&stream_size)); | 225 EXPECT_TRUE(stream->GetSize(&stream_size)); |
| 224 scoped_ptr<uint8_t[]> buffer(new uint8_t[expected_length]); | 226 std::unique_ptr<uint8_t[]> buffer(new uint8_t[expected_length]); |
| 225 EXPECT_EQ(SR_SUCCESS, | 227 EXPECT_EQ(SR_SUCCESS, |
| 226 stream->ReadAll(buffer.get(), expected_length, &read, nullptr)); | 228 stream->ReadAll(buffer.get(), expected_length, &read, nullptr)); |
| 227 EXPECT_EQ(0, memcmp(expected_contents, buffer.get(), expected_length)); | 229 EXPECT_EQ(0, memcmp(expected_contents, buffer.get(), expected_length)); |
| 228 EXPECT_EQ(SR_EOS, stream->ReadAll(buffer.get(), 1, nullptr, nullptr)); | 230 EXPECT_EQ(SR_EOS, stream->ReadAll(buffer.get(), 1, nullptr, nullptr)); |
| 229 EXPECT_EQ(stream_size, read); | 231 EXPECT_EQ(stream_size, read); |
| 230 } | 232 } |
| 231 | 233 |
| 232 scoped_ptr<CallSessionFileRotatingStream> stream_; | 234 std::unique_ptr<CallSessionFileRotatingStream> stream_; |
| 233 std::string dir_path_; | 235 std::string dir_path_; |
| 234 }; | 236 }; |
| 235 | 237 |
| 236 // Tests that writing and reading to a stream with the smallest possible | 238 // Tests that writing and reading to a stream with the smallest possible |
| 237 // capacity works. | 239 // capacity works. |
| 238 TEST_F(CallSessionFileRotatingStreamTest, WriteAndReadSmallest) { | 240 TEST_F(CallSessionFileRotatingStreamTest, WriteAndReadSmallest) { |
| 239 Init("CallSessionFileRotatingStreamTestWriteAndReadSmallest", 4); | 241 Init("CallSessionFileRotatingStreamTestWriteAndReadSmallest", 4); |
| 240 | 242 |
| 241 ASSERT_TRUE(stream_->Open()); | 243 ASSERT_TRUE(stream_->Open()); |
| 242 std::string message("abcde"); | 244 std::string message("abcde"); |
| (...skipping 16 matching lines...) Expand all Loading... |
| 259 dir_path_); | 261 dir_path_); |
| 260 } | 262 } |
| 261 | 263 |
| 262 // Tests that writing and reading to a stream with capacity greater than 4MB | 264 // Tests that writing and reading to a stream with capacity greater than 4MB |
| 263 // behaves correctly. | 265 // behaves correctly. |
| 264 TEST_F(CallSessionFileRotatingStreamTest, WriteAndReadLarge) { | 266 TEST_F(CallSessionFileRotatingStreamTest, WriteAndReadLarge) { |
| 265 Init("CallSessionFileRotatingStreamTestWriteAndReadLarge", 6 * 1024 * 1024); | 267 Init("CallSessionFileRotatingStreamTestWriteAndReadLarge", 6 * 1024 * 1024); |
| 266 | 268 |
| 267 ASSERT_TRUE(stream_->Open()); | 269 ASSERT_TRUE(stream_->Open()); |
| 268 const size_t buffer_size = 1024 * 1024; | 270 const size_t buffer_size = 1024 * 1024; |
| 269 scoped_ptr<uint8_t[]> buffer(new uint8_t[buffer_size]); | 271 std::unique_ptr<uint8_t[]> buffer(new uint8_t[buffer_size]); |
| 270 for (int i = 0; i < 8; i++) { | 272 for (int i = 0; i < 8; i++) { |
| 271 memset(buffer.get(), i, buffer_size); | 273 memset(buffer.get(), i, buffer_size); |
| 272 EXPECT_EQ(SR_SUCCESS, | 274 EXPECT_EQ(SR_SUCCESS, |
| 273 stream_->WriteAll(buffer.get(), buffer_size, nullptr, nullptr)); | 275 stream_->WriteAll(buffer.get(), buffer_size, nullptr, nullptr)); |
| 274 } | 276 } |
| 275 | 277 |
| 276 stream_.reset(new CallSessionFileRotatingStream(dir_path_)); | 278 stream_.reset(new CallSessionFileRotatingStream(dir_path_)); |
| 277 ASSERT_TRUE(stream_->Open()); | 279 ASSERT_TRUE(stream_->Open()); |
| 278 scoped_ptr<uint8_t[]> expected_buffer(new uint8_t[buffer_size]); | 280 std::unique_ptr<uint8_t[]> expected_buffer(new uint8_t[buffer_size]); |
| 279 int expected_vals[] = {0, 1, 2, 6, 7}; | 281 int expected_vals[] = {0, 1, 2, 6, 7}; |
| 280 for (size_t i = 0; i < arraysize(expected_vals); ++i) { | 282 for (size_t i = 0; i < arraysize(expected_vals); ++i) { |
| 281 memset(expected_buffer.get(), expected_vals[i], buffer_size); | 283 memset(expected_buffer.get(), expected_vals[i], buffer_size); |
| 282 EXPECT_EQ(SR_SUCCESS, | 284 EXPECT_EQ(SR_SUCCESS, |
| 283 stream_->ReadAll(buffer.get(), buffer_size, nullptr, nullptr)); | 285 stream_->ReadAll(buffer.get(), buffer_size, nullptr, nullptr)); |
| 284 EXPECT_EQ(0, memcmp(buffer.get(), expected_buffer.get(), buffer_size)); | 286 EXPECT_EQ(0, memcmp(buffer.get(), expected_buffer.get(), buffer_size)); |
| 285 } | 287 } |
| 286 EXPECT_EQ(SR_EOS, stream_->ReadAll(buffer.get(), 1, nullptr, nullptr)); | 288 EXPECT_EQ(SR_EOS, stream_->ReadAll(buffer.get(), 1, nullptr, nullptr)); |
| 287 } | 289 } |
| 288 | 290 |
| 289 // Tests that writing and reading to a stream where only the first file is | 291 // Tests that writing and reading to a stream where only the first file is |
| 290 // written to behaves correctly. | 292 // written to behaves correctly. |
| 291 TEST_F(CallSessionFileRotatingStreamTest, WriteAndReadFirstHalf) { | 293 TEST_F(CallSessionFileRotatingStreamTest, WriteAndReadFirstHalf) { |
| 292 Init("CallSessionFileRotatingStreamTestWriteAndReadFirstHalf", | 294 Init("CallSessionFileRotatingStreamTestWriteAndReadFirstHalf", |
| 293 6 * 1024 * 1024); | 295 6 * 1024 * 1024); |
| 294 ASSERT_TRUE(stream_->Open()); | 296 ASSERT_TRUE(stream_->Open()); |
| 295 const size_t buffer_size = 1024 * 1024; | 297 const size_t buffer_size = 1024 * 1024; |
| 296 scoped_ptr<uint8_t[]> buffer(new uint8_t[buffer_size]); | 298 std::unique_ptr<uint8_t[]> buffer(new uint8_t[buffer_size]); |
| 297 for (int i = 0; i < 2; i++) { | 299 for (int i = 0; i < 2; i++) { |
| 298 memset(buffer.get(), i, buffer_size); | 300 memset(buffer.get(), i, buffer_size); |
| 299 EXPECT_EQ(SR_SUCCESS, | 301 EXPECT_EQ(SR_SUCCESS, |
| 300 stream_->WriteAll(buffer.get(), buffer_size, nullptr, nullptr)); | 302 stream_->WriteAll(buffer.get(), buffer_size, nullptr, nullptr)); |
| 301 } | 303 } |
| 302 | 304 |
| 303 stream_.reset(new CallSessionFileRotatingStream(dir_path_)); | 305 stream_.reset(new CallSessionFileRotatingStream(dir_path_)); |
| 304 ASSERT_TRUE(stream_->Open()); | 306 ASSERT_TRUE(stream_->Open()); |
| 305 scoped_ptr<uint8_t[]> expected_buffer(new uint8_t[buffer_size]); | 307 std::unique_ptr<uint8_t[]> expected_buffer(new uint8_t[buffer_size]); |
| 306 int expected_vals[] = {0, 1}; | 308 int expected_vals[] = {0, 1}; |
| 307 for (size_t i = 0; i < arraysize(expected_vals); ++i) { | 309 for (size_t i = 0; i < arraysize(expected_vals); ++i) { |
| 308 memset(expected_buffer.get(), expected_vals[i], buffer_size); | 310 memset(expected_buffer.get(), expected_vals[i], buffer_size); |
| 309 EXPECT_EQ(SR_SUCCESS, | 311 EXPECT_EQ(SR_SUCCESS, |
| 310 stream_->ReadAll(buffer.get(), buffer_size, nullptr, nullptr)); | 312 stream_->ReadAll(buffer.get(), buffer_size, nullptr, nullptr)); |
| 311 EXPECT_EQ(0, memcmp(buffer.get(), expected_buffer.get(), buffer_size)); | 313 EXPECT_EQ(0, memcmp(buffer.get(), expected_buffer.get(), buffer_size)); |
| 312 } | 314 } |
| 313 EXPECT_EQ(SR_EOS, stream_->ReadAll(buffer.get(), 1, nullptr, nullptr)); | 315 EXPECT_EQ(SR_EOS, stream_->ReadAll(buffer.get(), 1, nullptr, nullptr)); |
| 314 } | 316 } |
| 315 | 317 |
| 316 } // namespace rtc | 318 } // namespace rtc |
| OLD | NEW |