Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
cpdf_security_handler_embeddertest.cpp
Go to the documentation of this file.
1// Copyright 2016 The PDFium Authors
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include <string>
6#include <type_traits>
7
8#include "build/build_config.h"
9#include "core/fpdfapi/parser/cpdf_dictionary.h"
10#include "core/fpdfapi/parser/cpdf_document.h"
11#include "core/fpdfapi/parser/cpdf_parser.h"
12#include "core/fxcrt/fx_system.h"
13#include "core/fxge/cfx_defaultrenderdevice.h"
14#include "public/cpp/fpdf_scopers.h"
15#include "public/fpdf_edit.h"
16#include "public/fpdf_save.h"
17#include "public/fpdfview.h"
18#include "testing/embedder_test.h"
19#include "testing/embedder_test_constants.h"
20#include "testing/gtest/include/gtest/gtest.h"
21
22namespace {
23
24const char kAgeUTF8[] =
25 "\xc3\xa2"
26 "ge";
27const char kAgeLatin1[] =
28 "\xe2"
29 "ge";
30
31const char kHotelUTF8[] =
32 "h"
33 "\xc3\xb4"
34 "tel";
35const char kHotelLatin1[] =
36 "h"
37 "\xf4"
38 "tel";
39
40} // namespace
41
43 protected:
45 const char* password) {
46 ASSERT_TRUE(OpenDocumentWithPassword(filename, password));
47 FPDF_PAGE page = LoadPage(0);
48 VerifyHelloWorldPage(page);
49 UnloadPage(page);
50 }
51
52 void VerifySavedHelloWorldDocumentWithPassword(const char* password) {
53 ASSERT_TRUE(OpenSavedDocumentWithPassword(password));
54 FPDF_PAGE page = LoadSavedPage(0);
55 VerifyHelloWorldPage(page);
56 CloseSavedPage(page);
58 }
59
61 ASSERT_TRUE(OpenSavedDocumentWithPassword(password));
62 FPDF_PAGE page = LoadSavedPage(0);
63 VerifyModifiedHelloWorldPage(page);
64 CloseSavedPage(page);
66 }
67
69 // This is cheating slightly to avoid a layering violation, since this file
70 // cannot include fpdfsdk/cpdfsdk_helpers.h to get access to
71 // CPDFDocumentFromFPDFDocument().
72 CPDF_Document* doc = reinterpret_cast<CPDF_Document*>((document()));
73 ASSERT_TRUE(doc);
74 CPDF_Parser* parser = doc->GetParser();
75 ASSERT_TRUE(parser);
77 ASSERT_TRUE(trailer);
78 ASSERT_TRUE(trailer->RemoveFor("ID"));
79 }
80
82 FPDF_PAGE page = LoadPage(0);
83 {
84 ScopedFPDFPageObject goodbye_object(FPDFPage_GetObject(page, 1));
85 ASSERT_TRUE(goodbye_object);
86 ASSERT_TRUE(FPDFPage_RemoveObject(page, goodbye_object.get()));
87 }
88 ASSERT_TRUE(FPDFPage_GenerateContent(page));
89 VerifyModifiedHelloWorldPage(page);
90 UnloadPage(page);
91 }
92
93 private:
94 void VerifyHelloWorldPage(FPDF_PAGE page) {
95 ASSERT_TRUE(page);
96
97 ScopedFPDFBitmap page_bitmap = RenderPage(page);
98 CompareBitmap(page_bitmap.get(), 200, 200, pdfium::HelloWorldChecksum());
99 }
100
101 void VerifyModifiedHelloWorldPage(FPDF_PAGE page) {
102 ASSERT_TRUE(page);
103
104 ScopedFPDFBitmap page_bitmap = RenderPage(page);
105 CompareBitmap(page_bitmap.get(), 200, 200,
107 }
108};
109
111 ASSERT_TRUE(OpenDocument("about_blank.pdf"));
112 // parser is missing a security handler, so always results in 0xFFFFFFFF
113 EXPECT_EQ(0xFFFFFFFF, FPDF_GetDocPermissions(document()));
114 EXPECT_EQ(0xFFFFFFFF, FPDF_GetDocUserPermissions(document()));
115}
116
118 ASSERT_TRUE(OpenDocumentWithPassword("about_blank.pdf", "foobar"));
119 // parser is missing a security handler, so always results in 0xFFFFFFFF
120 EXPECT_EQ(0xFFFFFFFF, FPDF_GetDocPermissions(document()));
121 EXPECT_EQ(0xFFFFFFFF, FPDF_GetDocUserPermissions(document()));
122}
123
125 EXPECT_FALSE(OpenDocument("encrypted.pdf"));
126}
127
129 EXPECT_FALSE(OpenDocumentWithPassword("encrypted.pdf", "tiger"));
130}
131
133 ASSERT_TRUE(OpenDocumentWithPassword("encrypted.pdf", "1234"));
134 EXPECT_EQ(0xFFFFF2C0, FPDF_GetDocPermissions(document()));
135 EXPECT_EQ(0xFFFFF2C0, FPDF_GetDocUserPermissions(document()));
136}
137
139 ASSERT_TRUE(OpenDocumentWithPassword("encrypted.pdf", "5678"));
140 EXPECT_EQ(0xFFFFFFFC, FPDF_GetDocPermissions(document()));
141 EXPECT_EQ(0xFFFFF2C0, FPDF_GetDocUserPermissions(document()));
142}
143
145 const char* checksum = []() {
146 if (CFX_DefaultRenderDevice::UseSkiaRenderer()) {
147#if BUILDFLAG(IS_WIN)
148 return "caa4bfda016a9c48a540ff7c6716468c";
149#elif BUILDFLAG(IS_APPLE)
150 return "6c1a242ce886df5cf578401eeeaa1929";
151#else
152 return "ad97491cab71c02f1f4ef5ba0a7b5593";
153#endif
154 }
155#if BUILDFLAG(IS_APPLE)
156 return "2a308e8cc20a6221112c387d122075a8";
157#else
158 return "9fe7eef8e51d15a604001854be6ed1ee";
159#endif // BUILDFLAG(IS_APPLE)
160 }();
161 {
162 ASSERT_TRUE(OpenDocumentWithOptions("encrypted.pdf", "5678",
163 LinearizeOption::kMustLinearize,
164 JavaScriptOption::kEnableJavaScript));
165 FPDF_PAGE page = LoadPage(0);
166 ASSERT_TRUE(page);
167 FPDF_PAGEOBJECT red_rect = FPDFPageObj_CreateNewRect(10, 10, 20, 20);
168 ASSERT_TRUE(red_rect);
169 EXPECT_TRUE(FPDFPageObj_SetFillColor(red_rect, 255, 0, 0, 255));
171 FPDFPage_InsertObject(page, red_rect);
172 ScopedFPDFBitmap bitmap = RenderLoadedPage(page);
173 CompareBitmap(bitmap.get(), 612, 792, checksum);
174 EXPECT_TRUE(FPDFPage_GenerateContent(page));
175 SetWholeFileAvailable();
176 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
177 UnloadPage(page);
178 }
179 std::string new_file = GetString();
180 FPDF_FILEACCESS file_access = {}; // Aggregate initialization.
181 static_assert(std::is_aggregate_v<decltype(file_access)>);
182 file_access.m_FileLen = new_file.size();
183 file_access.m_GetBlock = GetBlockFromString;
184 file_access.m_Param = &new_file;
185 EXPECT_FALSE(FPDF_LoadCustomDocument(&file_access, nullptr));
186
187 struct {
188 const char* password;
189 const unsigned long permissions;
190 } tests[] = {{"1234", 0xFFFFF2C0}, {"5678", 0xFFFFFFFC}};
191
192 for (const auto& test : tests) {
193 ASSERT_TRUE(OpenSavedDocumentWithPassword(test.password));
194 FPDF_PAGE page = LoadSavedPage(0);
195 ASSERT_TRUE(page);
196 VerifySavedRendering(page, 612, 792, checksum);
197 EXPECT_EQ(test.permissions, FPDF_GetDocPermissions(saved_document()));
198
199 CloseSavedPage(page);
200 CloseSavedDocument();
201 }
202}
203
205 ASSERT_FALSE(OpenDocument("bug_644.pdf"));
206}
207
209 ASSERT_FALSE(OpenDocumentWithPassword("bug_644.pdf", "tiger"));
210}
211
213 ASSERT_TRUE(OpenDocumentWithPassword("bug_644.pdf", "a"));
214 EXPECT_EQ(0xFFFFFFFC, FPDF_GetDocPermissions(document()));
215}
216
218 ASSERT_TRUE(OpenDocumentWithPassword("bug_644.pdf", "b"));
219 EXPECT_EQ(0xFFFFFFFC, FPDF_GetDocPermissions(document()));
220}
221
222// Should not crash. https://crbug.com/pdfium/1436
224 EXPECT_FALSE(
225 OpenDocumentWithPassword("encrypted_hello_world_r2_bad_okey.pdf", "a"));
226}
227
228// Should not crash. https://crbug.com/pdfium/1436
230 EXPECT_FALSE(
231 OpenDocumentWithPassword("encrypted_hello_world_r3_bad_okey.pdf", "a"));
232}
233
235 // The password is "age", where the 'a' has a circumflex. Encoding the
236 // password as UTF-8 works.
237 OpenAndVerifyHelloWorldDocumentWithPassword("encrypted_hello_world_r2.pdf",
238 kAgeUTF8);
239 EXPECT_EQ(2, FPDF_GetSecurityHandlerRevision(document()));
240 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
241 VerifySavedHelloWorldDocumentWithPassword(kAgeLatin1);
242 VerifySavedHelloWorldDocumentWithPassword(kAgeUTF8);
243 VerifySavedHelloWorldDocumentWithPassword(kHotelLatin1);
244 VerifySavedHelloWorldDocumentWithPassword(kHotelUTF8);
245
246 ClearString();
247 RemoveTrailerIdFromDocument();
248 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
249 // With revision 2 and 3, the owner password is not tied to the document ID in
250 // the trailer, so the owner password entry remains in the copy and is still
251 // valid, even though the document ID has changed.
252 // The user password is tied to the document ID, so without an existing ID,
253 // the user password entry has to be regenerated with the owner password.
254 // Since the user password was not used to decrypt the document, it cannot be
255 // recovered. Thus only verify the owner password, which is now also the user
256 // password.
257 VerifySavedHelloWorldDocumentWithPassword(kAgeLatin1);
258 VerifySavedHelloWorldDocumentWithPassword(kAgeUTF8);
259
260 ClearString();
261 RemoveGoodbyeObject();
262 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
263 VerifySavedModifiedHelloWorldDocumentWithPassword(kAgeLatin1);
264 VerifySavedModifiedHelloWorldDocumentWithPassword(kAgeUTF8);
265}
266
268 // The same password encoded as Latin-1 also works at revision 2.
269 OpenAndVerifyHelloWorldDocumentWithPassword("encrypted_hello_world_r2.pdf",
270 kAgeLatin1);
271 EXPECT_EQ(2, FPDF_GetSecurityHandlerRevision(document()));
272 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
273 VerifySavedHelloWorldDocumentWithPassword(kAgeLatin1);
274 VerifySavedHelloWorldDocumentWithPassword(kAgeUTF8);
275 VerifySavedHelloWorldDocumentWithPassword(kHotelLatin1);
276 VerifySavedHelloWorldDocumentWithPassword(kHotelUTF8);
277
278 ClearString();
279 RemoveTrailerIdFromDocument();
280 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
281 VerifySavedHelloWorldDocumentWithPassword(kAgeLatin1);
282 VerifySavedHelloWorldDocumentWithPassword(kAgeUTF8);
283
284 ClearString();
285 RemoveGoodbyeObject();
286 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
287 VerifySavedModifiedHelloWorldDocumentWithPassword(kAgeLatin1);
288 VerifySavedModifiedHelloWorldDocumentWithPassword(kAgeUTF8);
289}
290
292 // Same as OwnerPasswordVersion2UTF8 test above.
293 OpenAndVerifyHelloWorldDocumentWithPassword("encrypted_hello_world_r3.pdf",
294 kAgeUTF8);
295 EXPECT_EQ(3, FPDF_GetSecurityHandlerRevision(document()));
296 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
297 VerifySavedHelloWorldDocumentWithPassword(kAgeLatin1);
298 VerifySavedHelloWorldDocumentWithPassword(kAgeUTF8);
299 VerifySavedHelloWorldDocumentWithPassword(kHotelLatin1);
300 VerifySavedHelloWorldDocumentWithPassword(kHotelUTF8);
301
302 ClearString();
303 RemoveTrailerIdFromDocument();
304 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
305 VerifySavedHelloWorldDocumentWithPassword(kAgeLatin1);
306 VerifySavedHelloWorldDocumentWithPassword(kAgeUTF8);
307
308 ClearString();
309 RemoveGoodbyeObject();
310 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
311 VerifySavedModifiedHelloWorldDocumentWithPassword(kAgeLatin1);
312 VerifySavedModifiedHelloWorldDocumentWithPassword(kAgeUTF8);
313}
314
316 // Same as OwnerPasswordVersion2Latin1 test above.
317 OpenAndVerifyHelloWorldDocumentWithPassword("encrypted_hello_world_r3.pdf",
318 kAgeLatin1);
319 EXPECT_EQ(3, FPDF_GetSecurityHandlerRevision(document()));
320 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
321 VerifySavedHelloWorldDocumentWithPassword(kAgeLatin1);
322 VerifySavedHelloWorldDocumentWithPassword(kAgeUTF8);
323 VerifySavedHelloWorldDocumentWithPassword(kHotelLatin1);
324 VerifySavedHelloWorldDocumentWithPassword(kHotelUTF8);
325
326 ClearString();
327 RemoveTrailerIdFromDocument();
328 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
329 VerifySavedHelloWorldDocumentWithPassword(kAgeLatin1);
330 VerifySavedHelloWorldDocumentWithPassword(kAgeUTF8);
331
332 ClearString();
333 RemoveGoodbyeObject();
334 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
335 VerifySavedModifiedHelloWorldDocumentWithPassword(kAgeLatin1);
336 VerifySavedModifiedHelloWorldDocumentWithPassword(kAgeUTF8);
337}
338
340 // Same as OwnerPasswordVersion2UTF8 test above.
341 OpenAndVerifyHelloWorldDocumentWithPassword("encrypted_hello_world_r5.pdf",
342 kAgeUTF8);
343 EXPECT_EQ(5, FPDF_GetSecurityHandlerRevision(document()));
344 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
345 VerifySavedHelloWorldDocumentWithPassword(kAgeLatin1);
346 VerifySavedHelloWorldDocumentWithPassword(kAgeUTF8);
347 VerifySavedHelloWorldDocumentWithPassword(kHotelLatin1);
348 VerifySavedHelloWorldDocumentWithPassword(kHotelUTF8);
349
350 ClearString();
351 RemoveTrailerIdFromDocument();
352 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
353 VerifySavedHelloWorldDocumentWithPassword(kAgeLatin1);
354 VerifySavedHelloWorldDocumentWithPassword(kAgeUTF8);
355 VerifySavedHelloWorldDocumentWithPassword(kHotelLatin1);
356 VerifySavedHelloWorldDocumentWithPassword(kHotelUTF8);
357
358 ClearString();
359 RemoveGoodbyeObject();
360 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
361 VerifySavedModifiedHelloWorldDocumentWithPassword(kAgeLatin1);
362 VerifySavedModifiedHelloWorldDocumentWithPassword(kAgeUTF8);
363 VerifySavedModifiedHelloWorldDocumentWithPassword(kHotelLatin1);
364 VerifySavedModifiedHelloWorldDocumentWithPassword(kHotelUTF8);
365}
366
368 // Same as OwnerPasswordVersion2Latin1 test above.
369 OpenAndVerifyHelloWorldDocumentWithPassword("encrypted_hello_world_r5.pdf",
370 kAgeLatin1);
371 EXPECT_EQ(5, FPDF_GetSecurityHandlerRevision(document()));
372 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
373 VerifySavedHelloWorldDocumentWithPassword(kAgeLatin1);
374 VerifySavedHelloWorldDocumentWithPassword(kAgeUTF8);
375 VerifySavedHelloWorldDocumentWithPassword(kHotelLatin1);
376 VerifySavedHelloWorldDocumentWithPassword(kHotelUTF8);
377
378 ClearString();
379 RemoveTrailerIdFromDocument();
380 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
381 VerifySavedHelloWorldDocumentWithPassword(kAgeLatin1);
382 VerifySavedHelloWorldDocumentWithPassword(kAgeUTF8);
383 VerifySavedHelloWorldDocumentWithPassword(kHotelLatin1);
384 VerifySavedHelloWorldDocumentWithPassword(kHotelUTF8);
385
386 ClearString();
387 RemoveGoodbyeObject();
388 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
389 VerifySavedModifiedHelloWorldDocumentWithPassword(kAgeLatin1);
390 VerifySavedModifiedHelloWorldDocumentWithPassword(kAgeUTF8);
391 VerifySavedModifiedHelloWorldDocumentWithPassword(kHotelLatin1);
392 VerifySavedModifiedHelloWorldDocumentWithPassword(kHotelUTF8);
393}
394
396 // Same as OwnerPasswordVersion2UTF8 test above.
397 OpenAndVerifyHelloWorldDocumentWithPassword("encrypted_hello_world_r6.pdf",
398 kAgeUTF8);
399 EXPECT_EQ(6, FPDF_GetSecurityHandlerRevision(document()));
400 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
401 VerifySavedHelloWorldDocumentWithPassword(kAgeLatin1);
402 VerifySavedHelloWorldDocumentWithPassword(kAgeUTF8);
403 VerifySavedHelloWorldDocumentWithPassword(kHotelLatin1);
404 VerifySavedHelloWorldDocumentWithPassword(kHotelUTF8);
405
406 ClearString();
407 RemoveTrailerIdFromDocument();
408 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
409 VerifySavedHelloWorldDocumentWithPassword(kAgeLatin1);
410 VerifySavedHelloWorldDocumentWithPassword(kAgeUTF8);
411 VerifySavedHelloWorldDocumentWithPassword(kHotelLatin1);
412 VerifySavedHelloWorldDocumentWithPassword(kHotelUTF8);
413
414 ClearString();
415 RemoveGoodbyeObject();
416 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
417 VerifySavedModifiedHelloWorldDocumentWithPassword(kAgeLatin1);
418 VerifySavedModifiedHelloWorldDocumentWithPassword(kAgeUTF8);
419 VerifySavedModifiedHelloWorldDocumentWithPassword(kHotelLatin1);
420 VerifySavedModifiedHelloWorldDocumentWithPassword(kHotelUTF8);
421}
422
424 // Same as OwnerPasswordVersion2Latin1 test above.
425 OpenAndVerifyHelloWorldDocumentWithPassword("encrypted_hello_world_r6.pdf",
426 kAgeLatin1);
427 EXPECT_EQ(6, FPDF_GetSecurityHandlerRevision(document()));
428 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
429 VerifySavedHelloWorldDocumentWithPassword(kAgeLatin1);
430 VerifySavedHelloWorldDocumentWithPassword(kAgeUTF8);
431 VerifySavedHelloWorldDocumentWithPassword(kHotelLatin1);
432 VerifySavedHelloWorldDocumentWithPassword(kHotelUTF8);
433
434 ClearString();
435 RemoveTrailerIdFromDocument();
436 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
437 VerifySavedHelloWorldDocumentWithPassword(kAgeLatin1);
438 VerifySavedHelloWorldDocumentWithPassword(kAgeUTF8);
439 VerifySavedHelloWorldDocumentWithPassword(kHotelLatin1);
440 VerifySavedHelloWorldDocumentWithPassword(kHotelUTF8);
441
442 ClearString();
443 RemoveGoodbyeObject();
444 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
445 VerifySavedModifiedHelloWorldDocumentWithPassword(kAgeLatin1);
446 VerifySavedModifiedHelloWorldDocumentWithPassword(kAgeUTF8);
447 VerifySavedModifiedHelloWorldDocumentWithPassword(kHotelLatin1);
448 VerifySavedModifiedHelloWorldDocumentWithPassword(kHotelUTF8);
449}
450
452 // The password is "hotel", where the 'o' has a circumflex. Encoding the
453 // password as UTF-8 works.
454 OpenAndVerifyHelloWorldDocumentWithPassword("encrypted_hello_world_r2.pdf",
455 kHotelUTF8);
456 EXPECT_EQ(2, FPDF_GetSecurityHandlerRevision(document()));
457 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
458 VerifySavedHelloWorldDocumentWithPassword(kAgeLatin1);
459 VerifySavedHelloWorldDocumentWithPassword(kAgeUTF8);
460 VerifySavedHelloWorldDocumentWithPassword(kHotelLatin1);
461 VerifySavedHelloWorldDocumentWithPassword(kHotelUTF8);
462
463 ClearString();
464 RemoveTrailerIdFromDocument();
465 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
466 // Unlike the OwnerPasswordVersion2UTF8 test case, the user password was used
467 // to decrypt the document, so it is available to regenerated the user
468 // password entry. Thus it is possible to verify with both the unmodified
469 // owner password, and the updated user password.
470 VerifySavedHelloWorldDocumentWithPassword(kAgeLatin1);
471 VerifySavedHelloWorldDocumentWithPassword(kAgeUTF8);
472 VerifySavedHelloWorldDocumentWithPassword(kHotelLatin1);
473 VerifySavedHelloWorldDocumentWithPassword(kHotelUTF8);
474
475 ClearString();
476 RemoveGoodbyeObject();
477 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
478 VerifySavedModifiedHelloWorldDocumentWithPassword(kAgeLatin1);
479 VerifySavedModifiedHelloWorldDocumentWithPassword(kAgeUTF8);
480 VerifySavedModifiedHelloWorldDocumentWithPassword(kHotelLatin1);
481 VerifySavedModifiedHelloWorldDocumentWithPassword(kHotelUTF8);
482}
483
485 // The same password encoded as Latin-1 also works at revision 2.
486 OpenAndVerifyHelloWorldDocumentWithPassword("encrypted_hello_world_r2.pdf",
487 kHotelLatin1);
488 EXPECT_EQ(2, FPDF_GetSecurityHandlerRevision(document()));
489 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
490 VerifySavedHelloWorldDocumentWithPassword(kAgeLatin1);
491 VerifySavedHelloWorldDocumentWithPassword(kAgeUTF8);
492 VerifySavedHelloWorldDocumentWithPassword(kHotelLatin1);
493 VerifySavedHelloWorldDocumentWithPassword(kHotelUTF8);
494
495 ClearString();
496 RemoveTrailerIdFromDocument();
497 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
498 VerifySavedHelloWorldDocumentWithPassword(kAgeLatin1);
499 VerifySavedHelloWorldDocumentWithPassword(kAgeUTF8);
500 VerifySavedHelloWorldDocumentWithPassword(kHotelLatin1);
501 VerifySavedHelloWorldDocumentWithPassword(kHotelUTF8);
502
503 ClearString();
504 RemoveGoodbyeObject();
505 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
506 VerifySavedModifiedHelloWorldDocumentWithPassword(kAgeLatin1);
507 VerifySavedModifiedHelloWorldDocumentWithPassword(kAgeUTF8);
508 VerifySavedModifiedHelloWorldDocumentWithPassword(kHotelLatin1);
509 VerifySavedModifiedHelloWorldDocumentWithPassword(kHotelUTF8);
510}
511
513 // Same as UserPasswordVersion2UTF8 test above.
514 OpenAndVerifyHelloWorldDocumentWithPassword("encrypted_hello_world_r3.pdf",
515 kHotelUTF8);
516 EXPECT_EQ(3, FPDF_GetSecurityHandlerRevision(document()));
517 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
518 VerifySavedHelloWorldDocumentWithPassword(kAgeLatin1);
519 VerifySavedHelloWorldDocumentWithPassword(kAgeUTF8);
520 VerifySavedHelloWorldDocumentWithPassword(kHotelLatin1);
521 VerifySavedHelloWorldDocumentWithPassword(kHotelUTF8);
522
523 ClearString();
524 RemoveTrailerIdFromDocument();
525 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
526 VerifySavedHelloWorldDocumentWithPassword(kAgeLatin1);
527 VerifySavedHelloWorldDocumentWithPassword(kAgeUTF8);
528 VerifySavedHelloWorldDocumentWithPassword(kHotelLatin1);
529 VerifySavedHelloWorldDocumentWithPassword(kHotelUTF8);
530
531 ClearString();
532 RemoveGoodbyeObject();
533 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
534 VerifySavedModifiedHelloWorldDocumentWithPassword(kAgeLatin1);
535 VerifySavedModifiedHelloWorldDocumentWithPassword(kAgeUTF8);
536 VerifySavedModifiedHelloWorldDocumentWithPassword(kHotelLatin1);
537 VerifySavedModifiedHelloWorldDocumentWithPassword(kHotelUTF8);
538}
539
541 // Same as UserPasswordVersion2Latin1 test above.
542 OpenAndVerifyHelloWorldDocumentWithPassword("encrypted_hello_world_r3.pdf",
543 kHotelLatin1);
544 EXPECT_EQ(3, FPDF_GetSecurityHandlerRevision(document()));
545 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
546 VerifySavedHelloWorldDocumentWithPassword(kAgeLatin1);
547 VerifySavedHelloWorldDocumentWithPassword(kAgeUTF8);
548 VerifySavedHelloWorldDocumentWithPassword(kHotelLatin1);
549 VerifySavedHelloWorldDocumentWithPassword(kHotelUTF8);
550
551 ClearString();
552 RemoveTrailerIdFromDocument();
553 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
554 VerifySavedHelloWorldDocumentWithPassword(kAgeLatin1);
555 VerifySavedHelloWorldDocumentWithPassword(kAgeUTF8);
556 VerifySavedHelloWorldDocumentWithPassword(kHotelLatin1);
557 VerifySavedHelloWorldDocumentWithPassword(kHotelUTF8);
558
559 ClearString();
560 RemoveGoodbyeObject();
561 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
562 VerifySavedModifiedHelloWorldDocumentWithPassword(kAgeLatin1);
563 VerifySavedModifiedHelloWorldDocumentWithPassword(kAgeUTF8);
564 VerifySavedModifiedHelloWorldDocumentWithPassword(kHotelLatin1);
565 VerifySavedModifiedHelloWorldDocumentWithPassword(kHotelUTF8);
566}
567
569 // Same as UserPasswordVersion2UTF8 test above.
570 OpenAndVerifyHelloWorldDocumentWithPassword("encrypted_hello_world_r5.pdf",
571 kHotelUTF8);
572 EXPECT_EQ(5, FPDF_GetSecurityHandlerRevision(document()));
573 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
574 VerifySavedHelloWorldDocumentWithPassword(kAgeLatin1);
575 VerifySavedHelloWorldDocumentWithPassword(kAgeUTF8);
576 VerifySavedHelloWorldDocumentWithPassword(kHotelLatin1);
577 VerifySavedHelloWorldDocumentWithPassword(kHotelUTF8);
578
579 ClearString();
580 RemoveTrailerIdFromDocument();
581 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
582 VerifySavedHelloWorldDocumentWithPassword(kAgeLatin1);
583 VerifySavedHelloWorldDocumentWithPassword(kAgeUTF8);
584 VerifySavedHelloWorldDocumentWithPassword(kHotelLatin1);
585 VerifySavedHelloWorldDocumentWithPassword(kHotelUTF8);
586
587 ClearString();
588 RemoveGoodbyeObject();
589 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
590 VerifySavedModifiedHelloWorldDocumentWithPassword(kAgeLatin1);
591 VerifySavedModifiedHelloWorldDocumentWithPassword(kAgeUTF8);
592 VerifySavedModifiedHelloWorldDocumentWithPassword(kHotelLatin1);
593 VerifySavedModifiedHelloWorldDocumentWithPassword(kHotelUTF8);
594}
595
597 // Same as UserPasswordVersion2Latin1 test above.
598 OpenAndVerifyHelloWorldDocumentWithPassword("encrypted_hello_world_r5.pdf",
599 kHotelLatin1);
600 EXPECT_EQ(5, FPDF_GetSecurityHandlerRevision(document()));
601 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
602 VerifySavedHelloWorldDocumentWithPassword(kAgeLatin1);
603 VerifySavedHelloWorldDocumentWithPassword(kAgeUTF8);
604 VerifySavedHelloWorldDocumentWithPassword(kHotelLatin1);
605 VerifySavedHelloWorldDocumentWithPassword(kHotelUTF8);
606
607 ClearString();
608 RemoveTrailerIdFromDocument();
609 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
610 VerifySavedHelloWorldDocumentWithPassword(kAgeLatin1);
611 VerifySavedHelloWorldDocumentWithPassword(kAgeUTF8);
612 VerifySavedHelloWorldDocumentWithPassword(kHotelLatin1);
613 VerifySavedHelloWorldDocumentWithPassword(kHotelUTF8);
614
615 ClearString();
616 RemoveGoodbyeObject();
617 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
618 VerifySavedModifiedHelloWorldDocumentWithPassword(kAgeLatin1);
619 VerifySavedModifiedHelloWorldDocumentWithPassword(kAgeUTF8);
620 VerifySavedModifiedHelloWorldDocumentWithPassword(kHotelLatin1);
621 VerifySavedModifiedHelloWorldDocumentWithPassword(kHotelUTF8);
622}
623
625 // Same as UserPasswordVersion2UTF8 test above.
626 OpenAndVerifyHelloWorldDocumentWithPassword("encrypted_hello_world_r6.pdf",
627 kHotelUTF8);
628 EXPECT_EQ(6, FPDF_GetSecurityHandlerRevision(document()));
629 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
630 VerifySavedHelloWorldDocumentWithPassword(kAgeLatin1);
631 VerifySavedHelloWorldDocumentWithPassword(kAgeUTF8);
632 VerifySavedHelloWorldDocumentWithPassword(kHotelLatin1);
633 VerifySavedHelloWorldDocumentWithPassword(kHotelUTF8);
634
635 ClearString();
636 RemoveTrailerIdFromDocument();
637 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
638 VerifySavedHelloWorldDocumentWithPassword(kAgeLatin1);
639 VerifySavedHelloWorldDocumentWithPassword(kAgeUTF8);
640 VerifySavedHelloWorldDocumentWithPassword(kHotelLatin1);
641 VerifySavedHelloWorldDocumentWithPassword(kHotelUTF8);
642
643 ClearString();
644 RemoveGoodbyeObject();
645 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
646 VerifySavedModifiedHelloWorldDocumentWithPassword(kAgeLatin1);
647 VerifySavedModifiedHelloWorldDocumentWithPassword(kAgeUTF8);
648 VerifySavedModifiedHelloWorldDocumentWithPassword(kHotelLatin1);
649 VerifySavedModifiedHelloWorldDocumentWithPassword(kHotelUTF8);
650}
651
653 // Same as UserPasswordVersion2Latin1 test above.
654 OpenAndVerifyHelloWorldDocumentWithPassword("encrypted_hello_world_r6.pdf",
655 kHotelLatin1);
656 EXPECT_EQ(6, FPDF_GetSecurityHandlerRevision(document()));
657 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
658 VerifySavedHelloWorldDocumentWithPassword(kAgeLatin1);
659 VerifySavedHelloWorldDocumentWithPassword(kAgeUTF8);
660 VerifySavedHelloWorldDocumentWithPassword(kHotelLatin1);
661 VerifySavedHelloWorldDocumentWithPassword(kHotelUTF8);
662
663 ClearString();
664 RemoveTrailerIdFromDocument();
665 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
666 VerifySavedHelloWorldDocumentWithPassword(kAgeLatin1);
667 VerifySavedHelloWorldDocumentWithPassword(kAgeUTF8);
668 VerifySavedHelloWorldDocumentWithPassword(kHotelLatin1);
669 VerifySavedHelloWorldDocumentWithPassword(kHotelUTF8);
670
671 ClearString();
672 RemoveGoodbyeObject();
673 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
674 VerifySavedModifiedHelloWorldDocumentWithPassword(kAgeLatin1);
675 VerifySavedModifiedHelloWorldDocumentWithPassword(kAgeUTF8);
676 VerifySavedModifiedHelloWorldDocumentWithPassword(kHotelLatin1);
677 VerifySavedModifiedHelloWorldDocumentWithPassword(kHotelUTF8);
678}
679
681 OpenAndVerifyHelloWorldDocumentWithPassword("bug_1124998.pdf", "test");
682}
void VerifySavedHelloWorldDocumentWithPassword(const char *password)
void VerifySavedModifiedHelloWorldDocumentWithPassword(const char *password)
void OpenAndVerifyHelloWorldDocumentWithPassword(const char *filename, const char *password)
RetainPtr< CPDF_Object > RemoveFor(ByteStringView key)
std::map< ByteString, RetainPtr< CPDF_Object >, std::less<> > DictMap
CPDF_Parser * GetParser() const
CPDF_Dictionary * GetMutableTrailerForTesting()
FPDF_DOCUMENT OpenSavedDocumentWithPassword(const char *password)
void CloseSavedDocument()
FPDF_PAGE LoadPage(int page_index)
static void CompareBitmap(FPDF_BITMAP bitmap, int expected_width, int expected_height, const char *expected_md5sum)
void UnloadPage(FPDF_PAGE page)
FPDF_DOCUMENT document() const
void CloseSavedPage(FPDF_PAGE page)
FPDF_PAGE LoadSavedPage(int page_index)
TEST_F(CPDFSecurityHandlerEmbedderTest, Unencrypted)
#define FPDF_FILLMODE_ALTERNATE
Definition fpdf_edit.h:52
FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV FPDFPath_SetDrawMode(FPDF_PAGEOBJECT path, int fillmode, FPDF_BOOL stroke)
FPDF_EXPORT void FPDF_CALLCONV FPDFPage_InsertObject(FPDF_PAGE page, FPDF_PAGEOBJECT page_object)
FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV FPDFPageObj_SetFillColor(FPDF_PAGEOBJECT page_object, unsigned int R, unsigned int G, unsigned int B, unsigned int A)
FPDF_EXPORT FPDF_PAGEOBJECT FPDF_CALLCONV FPDFPageObj_CreateNewRect(float x, float y, float w, float h)
FPDF_EXPORT FPDF_PAGEOBJECT FPDF_CALLCONV FPDFPage_GetObject(FPDF_PAGE page, int index)
FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV FPDFPage_GenerateContent(FPDF_PAGE page)
FPDF_EXPORT FPDF_DOCUMENT FPDF_CALLCONV FPDF_LoadCustomDocument(FPDF_FILEACCESS *pFileAccess, FPDF_BYTESTRING password)
const char * HelloWorldChecksum()
const char * HelloWorldRemovedChecksum()