1 // Copyright (c) 2018 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #include "chrome/browser/media/webrtc/webrtc_event_log_manager_unittest_helpers.h" 6 7 #include "base/check.h" 8 #include "base/files/file_util.h" 9 #include "base/notreached.h" 10 #include "testing/gtest/include/gtest/gtest.h" 11 12 namespace webrtc_event_logging { 13 14 // Produce a LogFileWriter::Factory object. CreateLogFileWriterFactory(WebRtcEventLogCompression compression)15std::unique_ptr<LogFileWriter::Factory> CreateLogFileWriterFactory( 16 WebRtcEventLogCompression compression) { 17 switch (compression) { 18 case WebRtcEventLogCompression::NONE: 19 return std::make_unique<BaseLogFileWriterFactory>(); 20 case WebRtcEventLogCompression::GZIP_NULL_ESTIMATION: 21 return std::make_unique<GzippedLogFileWriterFactory>( 22 std::make_unique<GzipLogCompressorFactory>( 23 std::make_unique<NullEstimator::Factory>())); 24 case WebRtcEventLogCompression::GZIP_PERFECT_ESTIMATION: 25 return std::make_unique<GzippedLogFileWriterFactory>( 26 std::make_unique<GzipLogCompressorFactory>( 27 std::make_unique<PerfectGzipEstimator::Factory>())); 28 } 29 NOTREACHED(); 30 return nullptr; // Appease compiler. 31 } 32 33 #if defined(OS_POSIX) RemoveWritePermissions(const base::FilePath & path)34void RemoveWritePermissions(const base::FilePath& path) { 35 int permissions; 36 ASSERT_TRUE(base::GetPosixFilePermissions(path, &permissions)); 37 constexpr int write_permissions = base::FILE_PERMISSION_WRITE_BY_USER | 38 base::FILE_PERMISSION_WRITE_BY_GROUP | 39 base::FILE_PERMISSION_WRITE_BY_OTHERS; 40 permissions &= ~write_permissions; 41 ASSERT_TRUE(base::SetPosixFilePermissions(path, permissions)); 42 } 43 #endif // defined(OS_POSIX) 44 Create() const45std::unique_ptr<CompressedSizeEstimator> NullEstimator::Factory::Create() 46 const { 47 return std::make_unique<NullEstimator>(); 48 } 49 EstimateCompressedSize(const std::string & input) const50size_t NullEstimator::EstimateCompressedSize(const std::string& input) const { 51 return 0; 52 } 53 Create() const54std::unique_ptr<CompressedSizeEstimator> PerfectGzipEstimator::Factory::Create() 55 const { 56 return std::make_unique<PerfectGzipEstimator>(); 57 } 58 PerfectGzipEstimator()59PerfectGzipEstimator::PerfectGzipEstimator() { 60 // This factory will produce an optimistic compressor that will always 61 // think it can compress additional inputs, which will therefore allow 62 // us to find out what the real compressed size it, since compression 63 // will never be suppressed. 64 GzipLogCompressorFactory factory(std::make_unique<NullEstimator::Factory>()); 65 66 compressor_ = factory.Create(base::Optional<size_t>()); 67 DCHECK(compressor_); 68 69 std::string ignored; 70 compressor_->CreateHeader(&ignored); 71 } 72 73 PerfectGzipEstimator::~PerfectGzipEstimator() = default; 74 EstimateCompressedSize(const std::string & input) const75size_t PerfectGzipEstimator::EstimateCompressedSize( 76 const std::string& input) const { 77 std::string output; 78 EXPECT_EQ(compressor_->Compress(input, &output), LogCompressor::Result::OK); 79 return output.length(); 80 } 81 GzippedSize(const std::string & uncompressed)82size_t GzippedSize(const std::string& uncompressed) { 83 PerfectGzipEstimator perfect_estimator; 84 return kGzipOverheadBytes + 85 perfect_estimator.EstimateCompressedSize(uncompressed); 86 } 87 GzippedSize(const std::vector<std::string> & uncompressed)88size_t GzippedSize(const std::vector<std::string>& uncompressed) { 89 PerfectGzipEstimator perfect_estimator; 90 91 size_t result = kGzipOverheadBytes; 92 for (const std::string& str : uncompressed) { 93 result += perfect_estimator.EstimateCompressedSize(str); 94 } 95 96 return result; 97 } 98 99 } // namespace webrtc_event_logging 100