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