• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
5 #ifndef TESTING_TEST_SUPPORT_H_
6 #define TESTING_TEST_SUPPORT_H_
7 
8 #include <stdlib.h>
9 
10 #include <memory>
11 #include <string>
12 #include <vector>
13 
14 #include "public/fpdfview.h"
15 
16 #if PDF_ENABLE_XFA
17 #include "xfa/fgas/font/cfgas_fontmgr.h"
18 #endif  // PDF_ENABLE_XFA
19 
20 namespace pdfium {
21 
22 #define STR_IN_TEST_CASE(input_literal, ...)               \
23   {                                                        \
24     reinterpret_cast<const unsigned char*>(input_literal), \
25         sizeof(input_literal) - 1, __VA_ARGS__             \
26   }
27 
28 #define STR_IN_OUT_CASE(input_literal, expected_literal, ...)     \
29   {                                                               \
30     reinterpret_cast<const unsigned char*>(input_literal),        \
31         sizeof(input_literal) - 1,                                \
32         reinterpret_cast<const unsigned char*>(expected_literal), \
33         sizeof(expected_literal) - 1, __VA_ARGS__                 \
34   }
35 
36 struct StrFuncTestData {
37   const unsigned char* input;
38   uint32_t input_size;
39   const unsigned char* expected;
40   uint32_t expected_size;
41 };
42 
43 struct DecodeTestData {
44   const unsigned char* input;
45   uint32_t input_size;
46   const unsigned char* expected;
47   uint32_t expected_size;
48   // The size of input string being processed.
49   uint32_t processed_size;
50 };
51 
52 struct NullTermWstrFuncTestData {
53   const wchar_t* input;
54   const wchar_t* expected;
55 };
56 
57 // Used with std::unique_ptr to free() objects that can't be deleted.
58 struct FreeDeleter {
operatorFreeDeleter59   inline void operator()(void* ptr) const { free(ptr); }
60 };
61 
62 }  // namespace pdfium
63 
64 // Reads the entire contents of a file into a newly alloc'd buffer.
65 std::unique_ptr<char, pdfium::FreeDeleter> GetFileContents(const char* filename,
66                                                            size_t* retlen);
67 
68 std::vector<std::string> StringSplit(const std::string& str, char delimiter);
69 
70 // Converts a FPDF_WIDESTRING to a std::string.
71 // Deals with differences between UTF16LE and UTF8.
72 std::string GetPlatformString(FPDF_WIDESTRING wstr);
73 
74 // Converts a FPDF_WIDESTRING to a std::wstring.
75 // Deals with differences between UTF16LE and wchar_t.
76 std::wstring GetPlatformWString(FPDF_WIDESTRING wstr);
77 
78 // Returns a newly allocated FPDF_WIDESTRING.
79 // Deals with differences between UTF16LE and wchar_t.
80 std::unique_ptr<unsigned short, pdfium::FreeDeleter> GetFPDFWideString(
81     const std::wstring& wstr);
82 
83 std::string CryptToBase16(const uint8_t* digest);
84 std::string GenerateMD5Base16(const uint8_t* data, uint32_t size);
85 
86 #ifdef PDF_ENABLE_V8
87 namespace v8 {
88 class Platform;
89 }
90 #ifdef V8_USE_EXTERNAL_STARTUP_DATA
91 namespace v8 {
92 class StartupData;
93 }
94 bool InitializeV8ForPDFium(const std::string& exe_path,
95                            const std::string& bin_dir,
96                            v8::StartupData* natives_blob,
97                            v8::StartupData* snapshot_blob,
98                            v8::Platform** platform);
99 #else   // V8_USE_EXTERNAL_STARTUP_DATA
100 bool InitializeV8ForPDFium(const std::string& exe_path,
101                            v8::Platform** platform);
102 #endif  // V8_USE_EXTERNAL_STARTUP_DATA
103 #endif  // PDF_ENABLE_V8
104 
105 class TestLoader {
106  public:
107   TestLoader(const char* pBuf, size_t len);
108   static int GetBlock(void* param,
109                       unsigned long pos,
110                       unsigned char* pBuf,
111                       unsigned long size);
112 
113  private:
114   const char* const m_pBuf;
115   const size_t m_Len;
116 };
117 
118 #if PDF_ENABLE_XFA
119 CFGAS_FontMgr* GetGlobalFontManager();
120 #endif  // PDF_ENABLE_XFA
121 
122 #endif  // TESTING_TEST_SUPPORT_H_
123