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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
|
// Copyright 2016 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.
#include <memory>
#include <vector>
#include "core/fpdfapi/parser/cpdf_dictionary.h"
#include "core/fpdfapi/parser/cpdf_name.h"
#include "core/fpdfapi/parser/cpdf_string.h"
#include "core/fpdfdoc/cpdf_filespec.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "testing/test_support.h"
#include "third_party/base/ptr_util.h"
TEST(cpdf_filespec, EncodeDecodeFileName) {
std::vector<pdfium::NullTermWstrFuncTestData> test_data = {
// Empty src string.
{L"", L""},
// only file name.
{L"test.pdf", L"test.pdf"},
#if _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_
// With drive identifier.
{L"r:\\pdfdocs\\spec.pdf", L"/r/pdfdocs/spec.pdf"},
// Relative path.
{L"My Document\\test.pdf", L"My Document/test.pdf"},
// Absolute path without drive identifier.
{L"\\pdfdocs\\spec.pdf", L"//pdfdocs/spec.pdf"},
// Absolute path with double backslashes.
{L"\\\\pdfdocs\\spec.pdf", L"/pdfdocs/spec.pdf"},
// Network resource name. It is not supported yet.
// {L"pclib/eng:\\pdfdocs\\spec.pdf", L"/pclib/eng/pdfdocs/spec.pdf"},
#elif _FXM_PLATFORM_ == _FXM_PLATFORM_APPLE_
// Absolute path with colon separator.
{L"Mac HD:PDFDocs:spec.pdf", L"/Mac HD/PDFDocs/spec.pdf"},
// Relative path with colon separator.
{L"PDFDocs:spec.pdf", L"PDFDocs/spec.pdf"},
#else
// Relative path.
{L"./docs/test.pdf", L"./docs/test.pdf"},
// Relative path with parent dir.
{L"../test_docs/test.pdf", L"../test_docs/test.pdf"},
// Absolute path.
{L"/usr/local/home/test.pdf", L"/usr/local/home/test.pdf"},
#endif
};
for (const auto& data : test_data) {
CFX_WideString encoded_str = CPDF_FileSpec::EncodeFileName(data.input);
EXPECT_TRUE(encoded_str == data.expected);
// DecodeFileName is the reverse procedure of EncodeFileName.
CFX_WideString decoded_str = CPDF_FileSpec::DecodeFileName(data.expected);
EXPECT_TRUE(decoded_str == data.input);
}
}
TEST(cpdf_filespec, GetFileName) {
{
// String object.
pdfium::NullTermWstrFuncTestData test_data = {
#if _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_
L"/C/docs/test.pdf",
L"C:\\docs\\test.pdf"
#elif _FXM_PLATFORM_ == _FXM_PLATFORM_APPLE_
L"/Mac HD/docs/test.pdf",
L"Mac HD:docs:test.pdf"
#else
L"/docs/test.pdf",
L"/docs/test.pdf"
#endif
};
auto str_obj = pdfium::MakeUnique<CPDF_String>(nullptr, test_data.input);
CPDF_FileSpec file_spec(str_obj.get());
CFX_WideString file_name;
EXPECT_TRUE(file_spec.GetFileName(&file_name));
EXPECT_TRUE(file_name == test_data.expected);
}
{
// Dictionary object.
pdfium::NullTermWstrFuncTestData test_data[5] = {
#if _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_
{L"/C/docs/test.pdf", L"C:\\docs\\test.pdf"},
{L"/D/docs/test.pdf", L"D:\\docs\\test.pdf"},
{L"/E/docs/test.pdf", L"E:\\docs\\test.pdf"},
{L"/F/docs/test.pdf", L"F:\\docs\\test.pdf"},
{L"/G/docs/test.pdf", L"G:\\docs\\test.pdf"},
#elif _FXM_PLATFORM_ == _FXM_PLATFORM_APPLE_
{L"/Mac HD/docs1/test.pdf", L"Mac HD:docs1:test.pdf"},
{L"/Mac HD/docs2/test.pdf", L"Mac HD:docs2:test.pdf"},
{L"/Mac HD/docs3/test.pdf", L"Mac HD:docs3:test.pdf"},
{L"/Mac HD/docs4/test.pdf", L"Mac HD:docs4:test.pdf"},
{L"/Mac HD/docs5/test.pdf", L"Mac HD:docs5:test.pdf"},
#else
{L"/docs/a/test.pdf", L"/docs/a/test.pdf"},
{L"/docs/b/test.pdf", L"/docs/b/test.pdf"},
{L"/docs/c/test.pdf", L"/docs/c/test.pdf"},
{L"/docs/d/test.pdf", L"/docs/d/test.pdf"},
{L"/docs/e/test.pdf", L"/docs/e/test.pdf"},
#endif
};
// Keyword fields in reverse order of precedence to retrieve the file name.
const char* const keywords[5] = {"Unix", "Mac", "DOS", "F", "UF"};
auto dict_obj = pdfium::MakeUnique<CPDF_Dictionary>();
CPDF_FileSpec file_spec(dict_obj.get());
CFX_WideString file_name;
for (int i = 0; i < 5; ++i) {
dict_obj->SetNewFor<CPDF_String>(keywords[i], test_data[i].input);
EXPECT_TRUE(file_spec.GetFileName(&file_name));
EXPECT_TRUE(file_name == test_data[i].expected);
}
// With all the former fields and 'FS' field suggests 'URL' type.
dict_obj->SetNewFor<CPDF_String>("FS", "URL", false);
EXPECT_TRUE(file_spec.GetFileName(&file_name));
// Url string is not decoded.
EXPECT_TRUE(file_name == test_data[4].input);
}
{
// Invalid object.
auto name_obj = pdfium::MakeUnique<CPDF_Name>(nullptr, "test.pdf");
CPDF_FileSpec file_spec(name_obj.get());
CFX_WideString file_name;
EXPECT_FALSE(file_spec.GetFileName(&file_name));
}
}
TEST(cpdf_filespec, SetFileName) {
pdfium::NullTermWstrFuncTestData test_data = {
#if _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_
L"C:\\docs\\test.pdf",
L"/C/docs/test.pdf"
#elif _FXM_PLATFORM_ == _FXM_PLATFORM_APPLE_
L"Mac HD:docs:test.pdf",
L"/Mac HD/docs/test.pdf"
#else
L"/docs/test.pdf",
L"/docs/test.pdf"
#endif
};
// String object.
auto str_obj = pdfium::MakeUnique<CPDF_String>(nullptr, L"babababa");
CPDF_FileSpec file_spec1(str_obj.get());
file_spec1.SetFileName(test_data.input);
// Check internal object value.
CFX_ByteString str = CFX_ByteString::FromUnicode(test_data.expected);
EXPECT_TRUE(str == str_obj->GetString());
// Check we can get the file name back.
CFX_WideString file_name;
EXPECT_TRUE(file_spec1.GetFileName(&file_name));
EXPECT_TRUE(file_name == test_data.input);
// Dictionary object.
auto dict_obj = pdfium::MakeUnique<CPDF_Dictionary>();
CPDF_FileSpec file_spec2(dict_obj.get());
file_spec2.SetFileName(test_data.input);
// Check internal object value.
file_name = dict_obj->GetUnicodeTextFor("F");
EXPECT_TRUE(file_name == test_data.expected);
file_name = dict_obj->GetUnicodeTextFor("UF");
EXPECT_TRUE(file_name == test_data.expected);
// Check we can get the file name back.
EXPECT_TRUE(file_spec2.GetFileName(&file_name));
EXPECT_TRUE(file_name == test_data.input);
}
|