blob: 4111ca61ed625d37a061a698a0e7b2e34474fcff [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"
Lei Zhang79e893a2015-11-04 16:02:47 -080015
Tom Sepezd831dc72015-10-19 16:04:22 -070016#ifdef PDF_ENABLE_V8
Tom Sepezd831dc72015-10-19 16:04:22 -070017#include "v8/include/v8.h"
Lei Zhang8241df72015-11-06 14:38:48 -080018#endif // PDF_ENABLE_V8
Tom Sepezd831dc72015-10-19 16:04:22 -070019
Tom Sepez0aa35312016-01-06 10:16:32 -080020namespace pdfium {
21
Wei Li970c11e2016-02-16 14:26:22 -080022#define STR_IN_TEST_CASE(input_literal, ...) \
23 { \
24 (const unsigned char*) input_literal, sizeof(input_literal) - 1, \
25 __VA_ARGS__ \
26 }
27
Wei Li4f7f4ee2016-02-16 12:52:14 -080028#define STR_IN_OUT_CASE(input_literal, expected_literal, ...) \
Wei Li0db90092016-02-09 11:38:47 -080029 { \
30 (const unsigned char*) input_literal, sizeof(input_literal) - 1, \
31 (const unsigned char*)expected_literal, sizeof(expected_literal) - 1, \
Wei Li4f7f4ee2016-02-16 12:52:14 -080032 __VA_ARGS__ \
Wei Li0db90092016-02-09 11:38:47 -080033 }
34
35struct StrFuncTestData {
36 const unsigned char* input;
37 unsigned int input_size;
38 const unsigned char* expected;
39 unsigned int expected_size;
40};
41
42struct DecodeTestData {
43 const unsigned char* input;
44 unsigned int input_size;
45 const unsigned char* expected;
46 unsigned int expected_size;
47 // The size of input string being processed.
48 unsigned int processed_size;
49};
50
Wei Li65b36552016-02-18 14:04:57 -080051struct NullTermWstrFuncTestData {
52 const wchar_t* input;
53 const wchar_t* expected;
54};
55
Tom Sepez0aa35312016-01-06 10:16:32 -080056// Used with std::unique_ptr to free() objects that can't be deleted.
57struct FreeDeleter {
58 inline void operator()(void* ptr) const { free(ptr); }
59};
60
61} // namespace pdfium
62
63// Reads the entire contents of a file into a newly alloc'd buffer.
64std::unique_ptr<char, pdfium::FreeDeleter> GetFileContents(const char* filename,
65 size_t* retlen);
Tom Sepezd831dc72015-10-19 16:04:22 -070066
tsepezf09bdfa2016-04-18 16:08:26 -070067std::vector<std::string> StringSplit(const std::string& str, char delimiter);
68
Lei Zhang79e893a2015-11-04 16:02:47 -080069// Converts a FPDF_WIDESTRING to a std::wstring.
70// Deals with differences between UTF16LE and wchar_t.
Tom Sepez8ab45ea2016-01-05 10:17:30 -080071std::wstring GetPlatformWString(const FPDF_WIDESTRING wstr);
72
Tom Sepez0aa35312016-01-06 10:16:32 -080073// Returns a newly allocated FPDF_WIDESTRING.
Tom Sepez8ab45ea2016-01-05 10:17:30 -080074// Deals with differences between UTF16LE and wchar_t.
Tom Sepez0aa35312016-01-06 10:16:32 -080075std::unique_ptr<unsigned short, pdfium::FreeDeleter> GetFPDFWideString(
76 const std::wstring& wstr);
Lei Zhang79e893a2015-11-04 16:02:47 -080077
Tom Sepezd831dc72015-10-19 16:04:22 -070078#ifdef PDF_ENABLE_V8
79#ifdef V8_USE_EXTERNAL_STARTUP_DATA
80bool InitializeV8ForPDFium(const std::string& exe_path,
81 const std::string& bin_dir,
82 v8::StartupData* natives_blob,
83 v8::StartupData* snapshot_blob,
84 v8::Platform** platform);
85#else // V8_USE_EXTERNAL_STARTUP_DATA
jochen9e077d22016-06-09 02:51:13 -070086bool InitializeV8ForPDFium(const std::string& exe_path,
87 v8::Platform** platform);
Tom Sepezd831dc72015-10-19 16:04:22 -070088#endif // V8_USE_EXTERNAL_STARTUP_DATA
89#endif // PDF_ENABLE_V8
90
91class TestLoader {
92 public:
93 TestLoader(const char* pBuf, size_t len);
94 static int GetBlock(void* param,
95 unsigned long pos,
96 unsigned char* pBuf,
97 unsigned long size);
98
99 private:
100 const char* const m_pBuf;
101 const size_t m_Len;
102};
103
Tom Sepez0aec19b2016-01-07 12:22:44 -0800104class TestSaver : public FPDF_FILEWRITE {
105 public:
106 TestSaver();
107
108 void ClearString();
109 const std::string& GetString() const { return m_String; }
110
111 private:
112 static int WriteBlockCallback(FPDF_FILEWRITE* pFileWrite,
113 const void* data,
114 unsigned long size);
115
116 std::string m_String;
117};
118
Dan Sinclairc7cd8092016-02-18 15:02:55 -0500119#endif // TESTING_TEST_SUPPORT_H_