blob: 146582251fc2d8f0405035aaf145b88a83e8f262 [file] [log] [blame]
Tom Sepezd831dc72015-10-19 16:04:22 -07001// 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 Sinclairc7cd8092016-02-18 15:02:55 -05005#ifndef TESTING_TEST_SUPPORT_H_
6#define TESTING_TEST_SUPPORT_H_
Tom Sepezd831dc72015-10-19 16:04:22 -07007
8#include <stdlib.h>
Lei Zhang9a25ede2017-06-22 00:05:12 -07009
Tom Sepez0aa35312016-01-06 10:16:32 -080010#include <memory>
Tom Sepezd831dc72015-10-19 16:04:22 -070011#include <string>
tsepezf09bdfa2016-04-18 16:08:26 -070012#include <vector>
Tom Sepezd831dc72015-10-19 16:04:22 -070013
Tom Sepez0aec19b2016-01-07 12:22:44 -080014#include "public/fpdf_save.h"
Dan Sinclair61046b92016-02-18 14:48:48 -050015#include "public/fpdfview.h"
Tom Sepezd831dc72015-10-19 16:04:22 -070016
Tom Sepez0aa35312016-01-06 10:16:32 -080017namespace pdfium {
18
Wei Li970c11e2016-02-16 14:26:22 -080019#define STR_IN_TEST_CASE(input_literal, ...) \
20 { \
21 (const unsigned char*) input_literal, sizeof(input_literal) - 1, \
22 __VA_ARGS__ \
23 }
24
Wei Li4f7f4ee2016-02-16 12:52:14 -080025#define STR_IN_OUT_CASE(input_literal, expected_literal, ...) \
Wei Li0db90092016-02-09 11:38:47 -080026 { \
27 (const unsigned char*) input_literal, sizeof(input_literal) - 1, \
28 (const unsigned char*)expected_literal, sizeof(expected_literal) - 1, \
Wei Li4f7f4ee2016-02-16 12:52:14 -080029 __VA_ARGS__ \
Wei Li0db90092016-02-09 11:38:47 -080030 }
31
32struct StrFuncTestData {
33 const unsigned char* input;
34 unsigned int input_size;
35 const unsigned char* expected;
36 unsigned int expected_size;
37};
38
39struct 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 Li65b36552016-02-18 14:04:57 -080048struct NullTermWstrFuncTestData {
49 const wchar_t* input;
50 const wchar_t* expected;
51};
52
Tom Sepez0aa35312016-01-06 10:16:32 -080053// Used with std::unique_ptr to free() objects that can't be deleted.
54struct 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.
61std::unique_ptr<char, pdfium::FreeDeleter> GetFileContents(const char* filename,
62 size_t* retlen);
Tom Sepezd831dc72015-10-19 16:04:22 -070063
tsepezf09bdfa2016-04-18 16:08:26 -070064std::vector<std::string> StringSplit(const std::string& str, char delimiter);
65
Lei Zhang79e893a2015-11-04 16:02:47 -080066// Converts a FPDF_WIDESTRING to a std::wstring.
67// Deals with differences between UTF16LE and wchar_t.
Lei Zhang54d91ec2017-06-16 19:08:02 -070068std::wstring GetPlatformWString(FPDF_WIDESTRING wstr);
Tom Sepez8ab45ea2016-01-05 10:17:30 -080069
Tom Sepez0aa35312016-01-06 10:16:32 -080070// Returns a newly allocated FPDF_WIDESTRING.
Tom Sepez8ab45ea2016-01-05 10:17:30 -080071// Deals with differences between UTF16LE and wchar_t.
Tom Sepez0aa35312016-01-06 10:16:32 -080072std::unique_ptr<unsigned short, pdfium::FreeDeleter> GetFPDFWideString(
73 const std::wstring& wstr);
Lei Zhang79e893a2015-11-04 16:02:47 -080074
Lei Zhangd9e0e6e2017-04-28 16:54:10 -070075std::string CryptToBase16(const uint8_t* digest);
76std::string GenerateMD5Base16(const uint8_t* data, uint32_t size);
77
Tom Sepezd831dc72015-10-19 16:04:22 -070078#ifdef PDF_ENABLE_V8
Lei Zhang9a25ede2017-06-22 00:05:12 -070079namespace v8 {
80class Platform;
81}
Tom Sepezd831dc72015-10-19 16:04:22 -070082#ifdef V8_USE_EXTERNAL_STARTUP_DATA
Lei Zhang9a25ede2017-06-22 00:05:12 -070083namespace v8 {
84class StartupData;
85}
Tom Sepezd831dc72015-10-19 16:04:22 -070086bool 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
jochen9e077d22016-06-09 02:51:13 -070092bool InitializeV8ForPDFium(const std::string& exe_path,
93 v8::Platform** platform);
Tom Sepezd831dc72015-10-19 16:04:22 -070094#endif // V8_USE_EXTERNAL_STARTUP_DATA
95#endif // PDF_ENABLE_V8
96
97class 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 Sepez0aec19b2016-01-07 12:22:44 -0800110class TestSaver : public FPDF_FILEWRITE {
111 public:
112 TestSaver();
113
114 void ClearString();
115 const std::string& GetString() const { return m_String; }
116
Nicolas Pena742977f2017-04-13 15:28:20 -0400117 protected:
118 static int GetBlockFromString(void* param,
119 unsigned long pos,
120 unsigned char* buf,
121 unsigned long size);
122
Tom Sepez0aec19b2016-01-07 12:22:44 -0800123 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 Sinclairc7cd8092016-02-18 15:02:55 -0500131#endif // TESTING_TEST_SUPPORT_H_