blob: 7795be4214c3f1c77405028db45aa2c0b8f11ba3 [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
Lei Zhangd9e0e6e2017-04-28 16:54:10 -070013#include "core/fdrm/crypto/fx_crypt.h"
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"
Chris Palmere4b035b2017-03-26 15:48:34 -070016#include "testing/gtest/include/gtest/gtest.h"
Lei Zhang79e893a2015-11-04 16:02:47 -080017
Tom Sepezd831dc72015-10-19 16:04:22 -070018#ifdef PDF_ENABLE_V8
Tom Sepezd831dc72015-10-19 16:04:22 -070019#include "v8/include/v8.h"
Lei Zhang8241df72015-11-06 14:38:48 -080020#endif // PDF_ENABLE_V8
Tom Sepezd831dc72015-10-19 16:04:22 -070021
Tom Sepez0aa35312016-01-06 10:16:32 -080022namespace pdfium {
23
Wei Li970c11e2016-02-16 14:26:22 -080024#define STR_IN_TEST_CASE(input_literal, ...) \
25 { \
26 (const unsigned char*) input_literal, sizeof(input_literal) - 1, \
27 __VA_ARGS__ \
28 }
29
Wei Li4f7f4ee2016-02-16 12:52:14 -080030#define STR_IN_OUT_CASE(input_literal, expected_literal, ...) \
Wei Li0db90092016-02-09 11:38:47 -080031 { \
32 (const unsigned char*) input_literal, sizeof(input_literal) - 1, \
33 (const unsigned char*)expected_literal, sizeof(expected_literal) - 1, \
Wei Li4f7f4ee2016-02-16 12:52:14 -080034 __VA_ARGS__ \
Wei Li0db90092016-02-09 11:38:47 -080035 }
36
37struct StrFuncTestData {
38 const unsigned char* input;
39 unsigned int input_size;
40 const unsigned char* expected;
41 unsigned int expected_size;
42};
43
44struct DecodeTestData {
45 const unsigned char* input;
46 unsigned int input_size;
47 const unsigned char* expected;
48 unsigned int expected_size;
49 // The size of input string being processed.
50 unsigned int processed_size;
51};
52
Wei Li65b36552016-02-18 14:04:57 -080053struct NullTermWstrFuncTestData {
54 const wchar_t* input;
55 const wchar_t* expected;
56};
57
Tom Sepez0aa35312016-01-06 10:16:32 -080058// Used with std::unique_ptr to free() objects that can't be deleted.
59struct FreeDeleter {
60 inline void operator()(void* ptr) const { free(ptr); }
61};
62
63} // namespace pdfium
64
65// Reads the entire contents of a file into a newly alloc'd buffer.
66std::unique_ptr<char, pdfium::FreeDeleter> GetFileContents(const char* filename,
67 size_t* retlen);
Tom Sepezd831dc72015-10-19 16:04:22 -070068
tsepezf09bdfa2016-04-18 16:08:26 -070069std::vector<std::string> StringSplit(const std::string& str, char delimiter);
70
Lei Zhang79e893a2015-11-04 16:02:47 -080071// Converts a FPDF_WIDESTRING to a std::wstring.
72// Deals with differences between UTF16LE and wchar_t.
Lei Zhang54d91ec2017-06-16 19:08:02 -070073std::wstring GetPlatformWString(FPDF_WIDESTRING wstr);
Tom Sepez8ab45ea2016-01-05 10:17:30 -080074
Tom Sepez0aa35312016-01-06 10:16:32 -080075// Returns a newly allocated FPDF_WIDESTRING.
Tom Sepez8ab45ea2016-01-05 10:17:30 -080076// Deals with differences between UTF16LE and wchar_t.
Tom Sepez0aa35312016-01-06 10:16:32 -080077std::unique_ptr<unsigned short, pdfium::FreeDeleter> GetFPDFWideString(
78 const std::wstring& wstr);
Lei Zhang79e893a2015-11-04 16:02:47 -080079
Lei Zhangd9e0e6e2017-04-28 16:54:10 -070080std::string CryptToBase16(const uint8_t* digest);
81std::string GenerateMD5Base16(const uint8_t* data, uint32_t size);
82
Tom Sepezd831dc72015-10-19 16:04:22 -070083#ifdef PDF_ENABLE_V8
84#ifdef V8_USE_EXTERNAL_STARTUP_DATA
85bool InitializeV8ForPDFium(const std::string& exe_path,
86 const std::string& bin_dir,
87 v8::StartupData* natives_blob,
88 v8::StartupData* snapshot_blob,
89 v8::Platform** platform);
90#else // V8_USE_EXTERNAL_STARTUP_DATA
jochen9e077d22016-06-09 02:51:13 -070091bool InitializeV8ForPDFium(const std::string& exe_path,
92 v8::Platform** platform);
Tom Sepezd831dc72015-10-19 16:04:22 -070093#endif // V8_USE_EXTERNAL_STARTUP_DATA
94#endif // PDF_ENABLE_V8
95
96class TestLoader {
97 public:
98 TestLoader(const char* pBuf, size_t len);
99 static int GetBlock(void* param,
100 unsigned long pos,
101 unsigned char* pBuf,
102 unsigned long size);
103
104 private:
105 const char* const m_pBuf;
106 const size_t m_Len;
107};
108
Tom Sepez0aec19b2016-01-07 12:22:44 -0800109class TestSaver : public FPDF_FILEWRITE {
110 public:
111 TestSaver();
112
113 void ClearString();
114 const std::string& GetString() const { return m_String; }
115
Nicolas Pena742977f2017-04-13 15:28:20 -0400116 protected:
117 static int GetBlockFromString(void* param,
118 unsigned long pos,
119 unsigned char* buf,
120 unsigned long size);
121
Tom Sepez0aec19b2016-01-07 12:22:44 -0800122 private:
123 static int WriteBlockCallback(FPDF_FILEWRITE* pFileWrite,
124 const void* data,
125 unsigned long size);
126
127 std::string m_String;
128};
129
Dan Sinclairc7cd8092016-02-18 15:02:55 -0500130#endif // TESTING_TEST_SUPPORT_H_