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