blob: 4fe7c295cdef060fbbf911142524e4eb4ae5c874 [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
62} // namespace pdfium
63
64// Reads the entire contents of a file into a newly alloc'd buffer.
65std::unique_ptr<char, pdfium::FreeDeleter> GetFileContents(const char* filename,
66 size_t* retlen);
Tom Sepezd831dc72015-10-19 16:04:22 -070067
tsepezf09bdfa2016-04-18 16:08:26 -070068std::vector<std::string> StringSplit(const std::string& str, char delimiter);
69
Lei Zhang79e893a2015-11-04 16:02:47 -080070// Converts a FPDF_WIDESTRING to a std::wstring.
71// Deals with differences between UTF16LE and wchar_t.
Tom Sepez8ab45ea2016-01-05 10:17:30 -080072std::wstring GetPlatformWString(const FPDF_WIDESTRING wstr);
73
Tom Sepez0aa35312016-01-06 10:16:32 -080074// Returns a newly allocated FPDF_WIDESTRING.
Tom Sepez8ab45ea2016-01-05 10:17:30 -080075// Deals with differences between UTF16LE and wchar_t.
Tom Sepez0aa35312016-01-06 10:16:32 -080076std::unique_ptr<unsigned short, pdfium::FreeDeleter> GetFPDFWideString(
77 const std::wstring& wstr);
Lei Zhang79e893a2015-11-04 16:02:47 -080078
Tom Sepezd831dc72015-10-19 16:04:22 -070079#ifdef PDF_ENABLE_V8
80#ifdef V8_USE_EXTERNAL_STARTUP_DATA
81bool InitializeV8ForPDFium(const std::string& exe_path,
82 const std::string& bin_dir,
83 v8::StartupData* natives_blob,
84 v8::StartupData* snapshot_blob,
85 v8::Platform** platform);
86#else // V8_USE_EXTERNAL_STARTUP_DATA
jochen9e077d22016-06-09 02:51:13 -070087bool InitializeV8ForPDFium(const std::string& exe_path,
88 v8::Platform** platform);
Tom Sepezd831dc72015-10-19 16:04:22 -070089#endif // V8_USE_EXTERNAL_STARTUP_DATA
90#endif // PDF_ENABLE_V8
91
92class TestLoader {
93 public:
94 TestLoader(const char* pBuf, size_t len);
95 static int GetBlock(void* param,
96 unsigned long pos,
97 unsigned char* pBuf,
98 unsigned long size);
99
100 private:
101 const char* const m_pBuf;
102 const size_t m_Len;
103};
104
Tom Sepez0aec19b2016-01-07 12:22:44 -0800105class TestSaver : public FPDF_FILEWRITE {
106 public:
107 TestSaver();
108
109 void ClearString();
110 const std::string& GetString() const { return m_String; }
111
Nicolas Pena742977f2017-04-13 15:28:20 -0400112 protected:
113 static int GetBlockFromString(void* param,
114 unsigned long pos,
115 unsigned char* buf,
116 unsigned long size);
117
Tom Sepez0aec19b2016-01-07 12:22:44 -0800118 private:
119 static int WriteBlockCallback(FPDF_FILEWRITE* pFileWrite,
120 const void* data,
121 unsigned long size);
122
123 std::string m_String;
124};
125
Dan Sinclairc7cd8092016-02-18 15:02:55 -0500126#endif // TESTING_TEST_SUPPORT_H_