diff options
author | Ryan Harrison <rharrison@chromium.org> | 2017-09-28 17:12:31 -0400 |
---|---|---|
committer | Chromium commit bot <commit-bot@chromium.org> | 2017-09-29 14:26:15 +0000 |
commit | a5c32a120ae918ecebab4042b3e52278f7a24b51 (patch) | |
tree | 3400953772db763e32ade601170a46c00982e49f | |
parent | 10e1f05a9e644cd954792bcd40ef787551cbd209 (diff) | |
download | pdfium-a5c32a120ae918ecebab4042b3e52278f7a24b51.tar.xz |
Extract test subclasses of IFX_SeekableReadStream
There are multiple instances of subclasses that either act as an
invalid stream or one backed by a memory buffer. Merging all of these
into two shared stream classes and removing the others.
BUG=pdfium:911
Change-Id: I264602808c6dc0e5c878da462a5e00883fe43e51
Reviewed-on: https://pdfium-review.googlesource.com/15093
Commit-Queue: Ryan Harrison <rharrison@chromium.org>
Reviewed-by: Henrique Nakashima <hnakashima@chromium.org>
-rw-r--r-- | core/fpdfapi/parser/cpdf_object_avail_unittest.cpp | 21 | ||||
-rw-r--r-- | core/fpdfapi/parser/cpdf_page_object_avail_unittest.cpp | 20 | ||||
-rw-r--r-- | core/fpdfapi/parser/cpdf_parser_unittest.cpp | 30 | ||||
-rw-r--r-- | core/fpdfapi/parser/cpdf_read_validator_unittest.cpp | 23 | ||||
-rw-r--r-- | testing/fx_string_testhelpers.h | 50 | ||||
-rw-r--r-- | testing/libfuzzer/xfa_codec_fuzzer.h | 29 |
6 files changed, 67 insertions, 106 deletions
diff --git a/core/fpdfapi/parser/cpdf_object_avail_unittest.cpp b/core/fpdfapi/parser/cpdf_object_avail_unittest.cpp index 1a8c6d5d14..5c3da70530 100644 --- a/core/fpdfapi/parser/cpdf_object_avail_unittest.cpp +++ b/core/fpdfapi/parser/cpdf_object_avail_unittest.cpp @@ -14,27 +14,12 @@ #include "core/fpdfapi/parser/cpdf_reference.h" #include "core/fpdfapi/parser/cpdf_string.h" #include "core/fxcrt/fx_stream.h" +#include "testing/fx_string_testhelpers.h" #include "testing/gtest/include/gtest/gtest.h" #include "third_party/base/ptr_util.h" namespace { -class InvalidReader : public IFX_SeekableReadStream { - public: - template <typename T, typename... Args> - friend RetainPtr<T> pdfium::MakeRetain(Args&&... args); - - // IFX_SeekableReadStream overrides: - bool ReadBlock(void* buffer, FX_FILESIZE offset, size_t size) override { - return false; - } - FX_FILESIZE GetSize() override { return 100; } - - private: - InvalidReader() {} - ~InvalidReader() override {} -}; - class TestReadValidator : public CPDF_ReadValidator { public: template <typename T, typename... Args> @@ -44,7 +29,9 @@ class TestReadValidator : public CPDF_ReadValidator { protected: TestReadValidator() - : CPDF_ReadValidator(pdfium::MakeRetain<InvalidReader>(), nullptr) {} + : CPDF_ReadValidator( + pdfium::MakeRetain<CFX_InvalidSeekableReadStream>(100), + nullptr) {} ~TestReadValidator() override {} }; diff --git a/core/fpdfapi/parser/cpdf_page_object_avail_unittest.cpp b/core/fpdfapi/parser/cpdf_page_object_avail_unittest.cpp index 1e83e80d32..14ba0e3273 100644 --- a/core/fpdfapi/parser/cpdf_page_object_avail_unittest.cpp +++ b/core/fpdfapi/parser/cpdf_page_object_avail_unittest.cpp @@ -15,26 +15,12 @@ #include "core/fpdfapi/parser/cpdf_reference.h" #include "core/fpdfapi/parser/cpdf_string.h" #include "core/fxcrt/fx_stream.h" +#include "testing/fx_string_testhelpers.h" #include "testing/gtest/include/gtest/gtest.h" #include "third_party/base/ptr_util.h" namespace { -class InvalidReader : public IFX_SeekableReadStream { - public: - template <typename T, typename... Args> - friend RetainPtr<T> pdfium::MakeRetain(Args&&... args); - - // IFX_SeekableReadStream overrides: - bool ReadBlock(void* buffer, FX_FILESIZE offset, size_t size) override { - return false; - } - FX_FILESIZE GetSize() override { return 100; } - - private: - InvalidReader() {} - ~InvalidReader() override {} -}; class TestReadValidator : public CPDF_ReadValidator { public: @@ -45,7 +31,9 @@ class TestReadValidator : public CPDF_ReadValidator { protected: TestReadValidator() - : CPDF_ReadValidator(pdfium::MakeRetain<InvalidReader>(), nullptr) {} + : CPDF_ReadValidator( + pdfium::MakeRetain<CFX_InvalidSeekableReadStream>(100), + nullptr) {} ~TestReadValidator() override {} }; diff --git a/core/fpdfapi/parser/cpdf_parser_unittest.cpp b/core/fpdfapi/parser/cpdf_parser_unittest.cpp index b3352975a9..53e1434141 100644 --- a/core/fpdfapi/parser/cpdf_parser_unittest.cpp +++ b/core/fpdfapi/parser/cpdf_parser_unittest.cpp @@ -10,34 +10,10 @@ #include "core/fxcrt/fx_extension.h" #include "core/fxcrt/fx_stream.h" #include "core/fxcrt/retain_ptr.h" +#include "testing/fx_string_testhelpers.h" #include "testing/gtest/include/gtest/gtest.h" #include "testing/utils/path_service.h" -// Provide a way to read test data from a buffer instead of a file. -class CFX_TestBufferRead : public IFX_SeekableReadStream { - public: - template <typename T, typename... Args> - friend RetainPtr<T> pdfium::MakeRetain(Args&&... args); - - // IFX_SeekableReadStream: - bool ReadBlock(void* buffer, FX_FILESIZE offset, size_t size) override { - if (offset < 0 || offset + size > total_size_) - return false; - - memcpy(buffer, buffer_ + offset, size); - return true; - } - - FX_FILESIZE GetSize() override { return (FX_FILESIZE)total_size_; }; - - protected: - CFX_TestBufferRead(const unsigned char* buffer_in, size_t buf_size) - : buffer_(buffer_in), total_size_(buf_size) {} - - const unsigned char* buffer_; - size_t total_size_; -}; - // A wrapper class to help test member functions of CPDF_Parser. class CPDF_TestParser : public CPDF_Parser { public: @@ -59,8 +35,8 @@ class CPDF_TestParser : public CPDF_Parser { // Setup reading from a buffer and initial states. bool InitTestFromBuffer(const unsigned char* buffer, size_t len) { // For the test file, the header is set at the beginning. - m_pSyntax->InitParser(pdfium::MakeRetain<CFX_TestBufferRead>(buffer, len), - 0); + m_pSyntax->InitParser( + pdfium::MakeRetain<CFX_BufferSeekableReadStream>(buffer, len), 0); return true; } diff --git a/core/fpdfapi/parser/cpdf_read_validator_unittest.cpp b/core/fpdfapi/parser/cpdf_read_validator_unittest.cpp index c6007a6c31..308704f762 100644 --- a/core/fpdfapi/parser/cpdf_read_validator_unittest.cpp +++ b/core/fpdfapi/parser/cpdf_read_validator_unittest.cpp @@ -10,6 +10,7 @@ #include "core/fxcrt/cfx_memorystream.h" #include "core/fxcrt/fx_stream.h" +#include "testing/fx_string_testhelpers.h" #include "testing/gtest/include/gtest/gtest.h" namespace { @@ -62,22 +63,6 @@ class MockDownloadHints : public CPDF_DataAvail::DownloadHints { std::pair<FX_FILESIZE, FX_FILESIZE> last_requested_range_; }; -class InvalidReader : public IFX_SeekableReadStream { - public: - template <typename T, typename... Args> - friend RetainPtr<T> pdfium::MakeRetain(Args&&... args); - - // IFX_SeekableReadStream overrides: - bool ReadBlock(void* buffer, FX_FILESIZE offset, size_t size) override { - return false; - } - FX_FILESIZE GetSize() override { return kTestDataSize; } - - private: - InvalidReader() {} - ~InvalidReader() override {} -}; - } // namespace TEST(CPDF_ReadValidatorTest, UnavailableData) { @@ -149,7 +134,7 @@ TEST(CPDF_ReadValidatorTest, UnavailableDataWithHints) { } TEST(CPDF_ReadValidatorTest, ReadError) { - auto file = pdfium::MakeRetain<InvalidReader>(); + auto file = pdfium::MakeRetain<CFX_InvalidSeekableReadStream>(kTestDataSize); auto validator = pdfium::MakeRetain<CPDF_ReadValidator>(file, nullptr); static const uint32_t kBufferSize = 3 * 1000; @@ -182,7 +167,7 @@ TEST(CPDF_ReadValidatorTest, IntOverflow) { TEST(CPDF_ReadValidatorTest, Session) { std::vector<uint8_t> test_data(kTestDataSize); - auto file = pdfium::MakeRetain<InvalidReader>(); + auto file = pdfium::MakeRetain<CFX_InvalidSeekableReadStream>(kTestDataSize); MockFileAvail file_avail; MockDownloadHints hints; auto validator = pdfium::MakeRetain<CPDF_ReadValidator>(file, &file_avail); @@ -220,7 +205,7 @@ TEST(CPDF_ReadValidatorTest, Session) { TEST(CPDF_ReadValidatorTest, SessionReset) { std::vector<uint8_t> test_data(kTestDataSize); - auto file = pdfium::MakeRetain<InvalidReader>(); + auto file = pdfium::MakeRetain<CFX_InvalidSeekableReadStream>(kTestDataSize); MockFileAvail file_avail; MockDownloadHints hints; auto validator = pdfium::MakeRetain<CPDF_ReadValidator>(file, &file_avail); diff --git a/testing/fx_string_testhelpers.h b/testing/fx_string_testhelpers.h index 34ac69dab4..5a269a3cf6 100644 --- a/testing/fx_string_testhelpers.h +++ b/testing/fx_string_testhelpers.h @@ -8,8 +8,58 @@ #include <ostream> #include "core/fxcrt/cfx_datetime.h" +#include "core/fxcrt/fx_stream.h" // Output stream operator so GTEST macros work with CFX_DateTime objects. std::ostream& operator<<(std::ostream& os, const CFX_DateTime& dt); +class CFX_InvalidSeekableReadStream : public IFX_SeekableReadStream { + public: + template <typename T, typename... Args> + friend RetainPtr<T> pdfium::MakeRetain(Args&&... args); + + // IFX_SeekableReadStream overrides: + bool ReadBlock(void* buffer, FX_FILESIZE offset, size_t size) override { + return false; + } + FX_FILESIZE GetSize() override { return data_size_; } + + private: + explicit CFX_InvalidSeekableReadStream(FX_FILESIZE data_size) + : data_size_(data_size) {} + + FX_FILESIZE data_size_; +}; + +class CFX_BufferSeekableReadStream : public IFX_SeekableReadStream { + public: + template <typename T, typename... Args> + friend RetainPtr<T> pdfium::MakeRetain(Args&&... args); + + // IFX_SeekableReadStream: + bool ReadBlock(void* buffer, FX_FILESIZE offset, size_t size) override { + if (offset < 0 || static_cast<size_t>(offset) >= data_size_) + return false; + + if (static_cast<size_t>(offset) + size > data_size_) + size = data_size_ - static_cast<size_t>(offset); + if (size == 0) + return false; + + memcpy(buffer, data_ + offset, size); + return true; + } + + FX_FILESIZE GetSize() override { + return static_cast<FX_FILESIZE>(data_size_); + } + + private: + CFX_BufferSeekableReadStream(const unsigned char* src, size_t src_size) + : data_(src), data_size_(src_size) {} + + const unsigned char* data_; + size_t data_size_; +}; + #endif // TESTING_FX_STRING_TESTHELPERS_H_ diff --git a/testing/libfuzzer/xfa_codec_fuzzer.h b/testing/libfuzzer/xfa_codec_fuzzer.h index 5193a8930f..a590596dcf 100644 --- a/testing/libfuzzer/xfa_codec_fuzzer.h +++ b/testing/libfuzzer/xfa_codec_fuzzer.h @@ -13,8 +13,8 @@ #include "core/fxcodec/codec/ccodec_progressivedecoder.h" #include "core/fxcodec/codec/ccodec_tiffmodule.h" #include "core/fxcodec/fx_codec.h" -#include "core/fxcrt/fx_stream.h" #include "core/fxge/dib/cfx_dibitmap.h" +#include "testing/fx_string_testhelpers.h" #include "third_party/base/ptr_util.h" class XFACodecFuzzer { @@ -28,7 +28,7 @@ class XFACodecFuzzer { std::unique_ptr<CCodec_ProgressiveDecoder> decoder = mgr->CreateProgressiveDecoder(); - auto source = pdfium::MakeRetain<Reader>(data, size); + auto source = pdfium::MakeRetain<CFX_BufferSeekableReadStream>(data, size); FXCODEC_STATUS status = decoder->LoadImageInfo(source, type, nullptr, true); if (status != FXCODEC_STATUS_FRAME_READY) return 0; @@ -49,31 +49,6 @@ class XFACodecFuzzer { return 0; } - - private: - class Reader : public IFX_SeekableReadStream { - public: - Reader(const uint8_t* data, size_t size) : m_data(data), m_size(size) {} - ~Reader() {} - - bool ReadBlock(void* buffer, FX_FILESIZE offset, size_t size) override { - if (offset < 0 || static_cast<size_t>(offset) >= m_size) - return false; - if (offset + size > m_size) - size = m_size - offset; - if (size == 0) - return false; - - memcpy(buffer, m_data + offset, size); - return true; - } - - FX_FILESIZE GetSize() override { return static_cast<FX_FILESIZE>(m_size); } - - private: - const uint8_t* const m_data; - const size_t m_size; - }; }; #endif // TESTING_LIBFUZZER_XFA_CODEC_FUZZER_H_ |