summaryrefslogtreecommitdiff
path: root/testing/test_support.h
blob: fdb24fe61baf52112ee349461299d2888cb2e070 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
// Copyright 2015 PDFium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef TESTING_TEST_SUPPORT_H_
#define TESTING_TEST_SUPPORT_H_

#include <stdlib.h>
#include <memory>
#include <string>
#include <vector>

#include "public/fpdf_save.h"
#include "public/fpdfview.h"

#ifdef PDF_ENABLE_V8
#include "v8/include/v8.h"
#endif  // PDF_ENABLE_V8

namespace pdfium {

#define STR_IN_TEST_CASE(input_literal, ...)                         \
  {                                                                  \
    (const unsigned char*) input_literal, sizeof(input_literal) - 1, \
        __VA_ARGS__                                                  \
  }

#define STR_IN_OUT_CASE(input_literal, expected_literal, ...)                 \
  {                                                                           \
    (const unsigned char*) input_literal, sizeof(input_literal) - 1,          \
        (const unsigned char*)expected_literal, sizeof(expected_literal) - 1, \
        __VA_ARGS__                                                           \
  }

struct StrFuncTestData {
  const unsigned char* input;
  unsigned int input_size;
  const unsigned char* expected;
  unsigned int expected_size;
};

struct DecodeTestData {
  const unsigned char* input;
  unsigned int input_size;
  const unsigned char* expected;
  unsigned int expected_size;
  // The size of input string being processed.
  unsigned int processed_size;
};

struct NullTermWstrFuncTestData {
  const wchar_t* input;
  const wchar_t* expected;
};

// Used with std::unique_ptr to free() objects that can't be deleted.
struct FreeDeleter {
  inline void operator()(void* ptr) const { free(ptr); }
};

}  // namespace pdfium

// Reads the entire contents of a file into a newly alloc'd buffer.
std::unique_ptr<char, pdfium::FreeDeleter> GetFileContents(const char* filename,
                                                           size_t* retlen);

std::vector<std::string> StringSplit(const std::string& str, char delimiter);

// Converts a FPDF_WIDESTRING to a std::wstring.
// Deals with differences between UTF16LE and wchar_t.
std::wstring GetPlatformWString(const FPDF_WIDESTRING wstr);

// Returns a newly allocated FPDF_WIDESTRING.
// Deals with differences between UTF16LE and wchar_t.
std::unique_ptr<unsigned short, pdfium::FreeDeleter> GetFPDFWideString(
    const std::wstring& wstr);

#ifdef PDF_ENABLE_V8
#ifdef V8_USE_EXTERNAL_STARTUP_DATA
bool InitializeV8ForPDFium(const std::string& exe_path,
                           const std::string& bin_dir,
                           v8::StartupData* natives_blob,
                           v8::StartupData* snapshot_blob,
                           v8::Platform** platform);
#else   // V8_USE_EXTERNAL_STARTUP_DATA
bool InitializeV8ForPDFium(v8::Platform** platform);
#endif  // V8_USE_EXTERNAL_STARTUP_DATA
#endif  // PDF_ENABLE_V8

class TestLoader {
 public:
  TestLoader(const char* pBuf, size_t len);
  static int GetBlock(void* param,
                      unsigned long pos,
                      unsigned char* pBuf,
                      unsigned long size);

 private:
  const char* const m_pBuf;
  const size_t m_Len;
};

class TestSaver : public FPDF_FILEWRITE {
 public:
  TestSaver();

  void ClearString();
  const std::string& GetString() const { return m_String; }

 private:
  static int WriteBlockCallback(FPDF_FILEWRITE* pFileWrite,
                                const void* data,
                                unsigned long size);

  std::string m_String;
};

#endif  // TESTING_TEST_SUPPORT_H_