Tom Sepez | d831dc7 | 2015-10-19 16:04:22 -0700 | [diff] [blame] | 1 | // Copyright 2015 PDFium 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 | |
Dan Sinclair | c7cd809 | 2016-02-18 15:02:55 -0500 | [diff] [blame] | 5 | #ifndef TESTING_TEST_SUPPORT_H_ |
| 6 | #define TESTING_TEST_SUPPORT_H_ |
Tom Sepez | d831dc7 | 2015-10-19 16:04:22 -0700 | [diff] [blame] | 7 | |
| 8 | #include <stdlib.h> |
Tom Sepez | 0aa3531 | 2016-01-06 10:16:32 -0800 | [diff] [blame] | 9 | #include <memory> |
Tom Sepez | d831dc7 | 2015-10-19 16:04:22 -0700 | [diff] [blame] | 10 | #include <string> |
tsepez | f09bdfa | 2016-04-18 16:08:26 -0700 | [diff] [blame] | 11 | #include <vector> |
Tom Sepez | d831dc7 | 2015-10-19 16:04:22 -0700 | [diff] [blame] | 12 | |
Lei Zhang | d9e0e6e | 2017-04-28 16:54:10 -0700 | [diff] [blame^] | 13 | #include "core/fdrm/crypto/fx_crypt.h" |
Tom Sepez | 0aec19b | 2016-01-07 12:22:44 -0800 | [diff] [blame] | 14 | #include "public/fpdf_save.h" |
Dan Sinclair | 61046b9 | 2016-02-18 14:48:48 -0500 | [diff] [blame] | 15 | #include "public/fpdfview.h" |
Chris Palmer | e4b035b | 2017-03-26 15:48:34 -0700 | [diff] [blame] | 16 | #include "testing/gtest/include/gtest/gtest.h" |
Lei Zhang | 79e893a | 2015-11-04 16:02:47 -0800 | [diff] [blame] | 17 | |
Tom Sepez | d831dc7 | 2015-10-19 16:04:22 -0700 | [diff] [blame] | 18 | #ifdef PDF_ENABLE_V8 |
Tom Sepez | d831dc7 | 2015-10-19 16:04:22 -0700 | [diff] [blame] | 19 | #include "v8/include/v8.h" |
Lei Zhang | 8241df7 | 2015-11-06 14:38:48 -0800 | [diff] [blame] | 20 | #endif // PDF_ENABLE_V8 |
Tom Sepez | d831dc7 | 2015-10-19 16:04:22 -0700 | [diff] [blame] | 21 | |
Tom Sepez | 0aa3531 | 2016-01-06 10:16:32 -0800 | [diff] [blame] | 22 | namespace pdfium { |
| 23 | |
Wei Li | 970c11e | 2016-02-16 14:26:22 -0800 | [diff] [blame] | 24 | #define STR_IN_TEST_CASE(input_literal, ...) \ |
| 25 | { \ |
| 26 | (const unsigned char*) input_literal, sizeof(input_literal) - 1, \ |
| 27 | __VA_ARGS__ \ |
| 28 | } |
| 29 | |
Wei Li | 4f7f4ee | 2016-02-16 12:52:14 -0800 | [diff] [blame] | 30 | #define STR_IN_OUT_CASE(input_literal, expected_literal, ...) \ |
Wei Li | 0db9009 | 2016-02-09 11:38:47 -0800 | [diff] [blame] | 31 | { \ |
| 32 | (const unsigned char*) input_literal, sizeof(input_literal) - 1, \ |
| 33 | (const unsigned char*)expected_literal, sizeof(expected_literal) - 1, \ |
Wei Li | 4f7f4ee | 2016-02-16 12:52:14 -0800 | [diff] [blame] | 34 | __VA_ARGS__ \ |
Wei Li | 0db9009 | 2016-02-09 11:38:47 -0800 | [diff] [blame] | 35 | } |
| 36 | |
| 37 | struct StrFuncTestData { |
| 38 | const unsigned char* input; |
| 39 | unsigned int input_size; |
| 40 | const unsigned char* expected; |
| 41 | unsigned int expected_size; |
| 42 | }; |
| 43 | |
| 44 | struct DecodeTestData { |
| 45 | const unsigned char* input; |
| 46 | unsigned int input_size; |
| 47 | const unsigned char* expected; |
| 48 | unsigned int expected_size; |
| 49 | // The size of input string being processed. |
| 50 | unsigned int processed_size; |
| 51 | }; |
| 52 | |
Wei Li | 65b3655 | 2016-02-18 14:04:57 -0800 | [diff] [blame] | 53 | struct NullTermWstrFuncTestData { |
| 54 | const wchar_t* input; |
| 55 | const wchar_t* expected; |
| 56 | }; |
| 57 | |
Tom Sepez | 0aa3531 | 2016-01-06 10:16:32 -0800 | [diff] [blame] | 58 | // Used with std::unique_ptr to free() objects that can't be deleted. |
| 59 | struct FreeDeleter { |
| 60 | inline void operator()(void* ptr) const { free(ptr); } |
| 61 | }; |
| 62 | |
| 63 | } // namespace pdfium |
| 64 | |
| 65 | // Reads the entire contents of a file into a newly alloc'd buffer. |
| 66 | std::unique_ptr<char, pdfium::FreeDeleter> GetFileContents(const char* filename, |
| 67 | size_t* retlen); |
Tom Sepez | d831dc7 | 2015-10-19 16:04:22 -0700 | [diff] [blame] | 68 | |
tsepez | f09bdfa | 2016-04-18 16:08:26 -0700 | [diff] [blame] | 69 | std::vector<std::string> StringSplit(const std::string& str, char delimiter); |
| 70 | |
Lei Zhang | 79e893a | 2015-11-04 16:02:47 -0800 | [diff] [blame] | 71 | // Converts a FPDF_WIDESTRING to a std::wstring. |
| 72 | // Deals with differences between UTF16LE and wchar_t. |
Tom Sepez | 8ab45ea | 2016-01-05 10:17:30 -0800 | [diff] [blame] | 73 | std::wstring GetPlatformWString(const FPDF_WIDESTRING wstr); |
| 74 | |
Tom Sepez | 0aa3531 | 2016-01-06 10:16:32 -0800 | [diff] [blame] | 75 | // Returns a newly allocated FPDF_WIDESTRING. |
Tom Sepez | 8ab45ea | 2016-01-05 10:17:30 -0800 | [diff] [blame] | 76 | // Deals with differences between UTF16LE and wchar_t. |
Tom Sepez | 0aa3531 | 2016-01-06 10:16:32 -0800 | [diff] [blame] | 77 | std::unique_ptr<unsigned short, pdfium::FreeDeleter> GetFPDFWideString( |
| 78 | const std::wstring& wstr); |
Lei Zhang | 79e893a | 2015-11-04 16:02:47 -0800 | [diff] [blame] | 79 | |
Lei Zhang | d9e0e6e | 2017-04-28 16:54:10 -0700 | [diff] [blame^] | 80 | std::string CryptToBase16(const uint8_t* digest); |
| 81 | std::string GenerateMD5Base16(const uint8_t* data, uint32_t size); |
| 82 | |
Tom Sepez | d831dc7 | 2015-10-19 16:04:22 -0700 | [diff] [blame] | 83 | #ifdef PDF_ENABLE_V8 |
| 84 | #ifdef V8_USE_EXTERNAL_STARTUP_DATA |
| 85 | bool InitializeV8ForPDFium(const std::string& exe_path, |
| 86 | const std::string& bin_dir, |
| 87 | v8::StartupData* natives_blob, |
| 88 | v8::StartupData* snapshot_blob, |
| 89 | v8::Platform** platform); |
| 90 | #else // V8_USE_EXTERNAL_STARTUP_DATA |
jochen | 9e077d2 | 2016-06-09 02:51:13 -0700 | [diff] [blame] | 91 | bool InitializeV8ForPDFium(const std::string& exe_path, |
| 92 | v8::Platform** platform); |
Tom Sepez | d831dc7 | 2015-10-19 16:04:22 -0700 | [diff] [blame] | 93 | #endif // V8_USE_EXTERNAL_STARTUP_DATA |
| 94 | #endif // PDF_ENABLE_V8 |
| 95 | |
| 96 | class TestLoader { |
| 97 | public: |
| 98 | TestLoader(const char* pBuf, size_t len); |
| 99 | static int GetBlock(void* param, |
| 100 | unsigned long pos, |
| 101 | unsigned char* pBuf, |
| 102 | unsigned long size); |
| 103 | |
| 104 | private: |
| 105 | const char* const m_pBuf; |
| 106 | const size_t m_Len; |
| 107 | }; |
| 108 | |
Tom Sepez | 0aec19b | 2016-01-07 12:22:44 -0800 | [diff] [blame] | 109 | class TestSaver : public FPDF_FILEWRITE { |
| 110 | public: |
| 111 | TestSaver(); |
| 112 | |
| 113 | void ClearString(); |
| 114 | const std::string& GetString() const { return m_String; } |
| 115 | |
Nicolas Pena | 742977f | 2017-04-13 15:28:20 -0400 | [diff] [blame] | 116 | protected: |
| 117 | static int GetBlockFromString(void* param, |
| 118 | unsigned long pos, |
| 119 | unsigned char* buf, |
| 120 | unsigned long size); |
| 121 | |
Tom Sepez | 0aec19b | 2016-01-07 12:22:44 -0800 | [diff] [blame] | 122 | private: |
| 123 | static int WriteBlockCallback(FPDF_FILEWRITE* pFileWrite, |
| 124 | const void* data, |
| 125 | unsigned long size); |
| 126 | |
| 127 | std::string m_String; |
| 128 | }; |
| 129 | |
Dan Sinclair | c7cd809 | 2016-02-18 15:02:55 -0500 | [diff] [blame] | 130 | #endif // TESTING_TEST_SUPPORT_H_ |