blob: f175811078cc65f66e3a8d7115705a199efa0334 [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>
Tom Sepez0aa35312016-01-06 10:16:32 -08009#include <memory>
Tom Sepezd831dc72015-10-19 16:04:22 -070010#include <string>
tsepezf09bdfa2016-04-18 16:08:26 -070011#include <vector>
Tom Sepezd831dc72015-10-19 16:04:22 -070012
Tom Sepez0aec19b2016-01-07 12:22:44 -080013#include "public/fpdf_save.h"
Dan Sinclair61046b92016-02-18 14:48:48 -050014#include "public/fpdfview.h"
Chris Palmere4b035b2017-03-26 15:48:34 -070015#include "testing/gtest/include/gtest/gtest.h"
Lei Zhang79e893a2015-11-04 16:02:47 -080016
Tom Sepezd831dc72015-10-19 16:04:22 -070017#ifdef PDF_ENABLE_V8
Tom Sepezd831dc72015-10-19 16:04:22 -070018#include "v8/include/v8.h"
Lei Zhang8241df72015-11-06 14:38:48 -080019#endif // PDF_ENABLE_V8
Tom Sepezd831dc72015-10-19 16:04:22 -070020
Tom Sepez0aa35312016-01-06 10:16:32 -080021namespace pdfium {
22
Wei Li970c11e2016-02-16 14:26:22 -080023#define STR_IN_TEST_CASE(input_literal, ...) \
24 { \
25 (const unsigned char*) input_literal, sizeof(input_literal) - 1, \
26 __VA_ARGS__ \
27 }
28
Wei Li4f7f4ee2016-02-16 12:52:14 -080029#define STR_IN_OUT_CASE(input_literal, expected_literal, ...) \
Wei Li0db90092016-02-09 11:38:47 -080030 { \
31 (const unsigned char*) input_literal, sizeof(input_literal) - 1, \
32 (const unsigned char*)expected_literal, sizeof(expected_literal) - 1, \
Wei Li4f7f4ee2016-02-16 12:52:14 -080033 __VA_ARGS__ \
Wei Li0db90092016-02-09 11:38:47 -080034 }
35
36struct StrFuncTestData {
37 const unsigned char* input;
38 unsigned int input_size;
39 const unsigned char* expected;
40 unsigned int expected_size;
41};
42
43struct 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 Li65b36552016-02-18 14:04:57 -080052struct NullTermWstrFuncTestData {
53 const wchar_t* input;
54 const wchar_t* expected;
55};
56
Tom Sepez0aa35312016-01-06 10:16:32 -080057// Used with std::unique_ptr to free() objects that can't be deleted.
58struct FreeDeleter {
59 inline void operator()(void* ptr) const { free(ptr); }
60};
61
Chris Palmere4b035b2017-03-26 15:48:34 -070062class FPDF_Test : public ::testing::Test {
63 public:
64 void SetUp() override;
65 void TearDown() override;
66};
67
Tom Sepez0aa35312016-01-06 10:16:32 -080068} // namespace pdfium
69
70// Reads the entire contents of a file into a newly alloc'd buffer.
71std::unique_ptr<char, pdfium::FreeDeleter> GetFileContents(const char* filename,
72 size_t* retlen);
Tom Sepezd831dc72015-10-19 16:04:22 -070073
tsepezf09bdfa2016-04-18 16:08:26 -070074std::vector<std::string> StringSplit(const std::string& str, char delimiter);
75
Lei Zhang79e893a2015-11-04 16:02:47 -080076// Converts a FPDF_WIDESTRING to a std::wstring.
77// Deals with differences between UTF16LE and wchar_t.
Tom Sepez8ab45ea2016-01-05 10:17:30 -080078std::wstring GetPlatformWString(const FPDF_WIDESTRING wstr);
79
Tom Sepez0aa35312016-01-06 10:16:32 -080080// Returns a newly allocated FPDF_WIDESTRING.
Tom Sepez8ab45ea2016-01-05 10:17:30 -080081// Deals with differences between UTF16LE and wchar_t.
Tom Sepez0aa35312016-01-06 10:16:32 -080082std::unique_ptr<unsigned short, pdfium::FreeDeleter> GetFPDFWideString(
83 const std::wstring& wstr);
Lei Zhang79e893a2015-11-04 16:02:47 -080084
Tom Sepezd831dc72015-10-19 16:04:22 -070085#ifdef PDF_ENABLE_V8
86#ifdef V8_USE_EXTERNAL_STARTUP_DATA
87bool 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
jochen9e077d22016-06-09 02:51:13 -070093bool InitializeV8ForPDFium(const std::string& exe_path,
94 v8::Platform** platform);
Tom Sepezd831dc72015-10-19 16:04:22 -070095#endif // V8_USE_EXTERNAL_STARTUP_DATA
96#endif // PDF_ENABLE_V8
97
98class 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 Sepez0aec19b2016-01-07 12:22:44 -0800111class TestSaver : public FPDF_FILEWRITE {
112 public:
113 TestSaver();
114
115 void ClearString();
116 const std::string& GetString() const { return m_String; }
117
Nicolas Pena742977f2017-04-13 15:28:20 -0400118 protected:
119 static int GetBlockFromString(void* param,
120 unsigned long pos,
121 unsigned char* buf,
122 unsigned long size);
123
Tom Sepez0aec19b2016-01-07 12:22:44 -0800124 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 Sinclairc7cd8092016-02-18 15:02:55 -0500132#endif // TESTING_TEST_SUPPORT_H_