| 1 | /** |
| 2 | * Copyright (C) 2007 by Dominik Seichter <[email protected]> |
| 3 | * Copyright (C) 2021 by Francesco Pretto <[email protected]> |
| 4 | * |
| 5 | * Licensed under GNU Library General Public 2.0 or later. |
| 6 | * Some rights reserved. See COPYING, AUTHORS. |
| 7 | */ |
| 8 | |
| 9 | #include <PdfTest.h> |
| 10 | |
| 11 | using namespace std; |
| 12 | using namespace PoDoFo; |
| 13 | |
| 14 | static void testFilter(PdfFilterType filterType, const bufferview& buffer); |
| 15 | |
| 16 | static string_view s_testBuffer1 = "Man is distinguished, not only by his reason, but by this singular passion from other animals, which is a lust of the mind, that by a perseverance of delight in the continued and indefatigable generation of knowledge, exceeds the short vehemence of any carnal pleasure."; |
| 17 | |
| 18 | const char s_testBuffer2[] = { |
| 19 | 0x01, 0x64, 0x65, static_cast<char>(0xFE), 0x6B, static_cast<char>(0x80), 0x45, 0x32, static_cast<char>(0x88), 0x12, static_cast<char>(0x71), static_cast<char>(0xEA), 0x01, |
| 20 | 0x01, 0x64, 0x65, static_cast<char>(0xFE), 0x6B, static_cast<char>(0x80), 0x45, 0x32, static_cast<char>(0x88), 0x12, static_cast<char>(0x71), static_cast<char>(0xEA), 0x03, |
| 21 | 0x01, 0x64, 0x65, static_cast<char>(0xFE), 0x6B, static_cast<char>(0x80), 0x45, 0x32, static_cast<char>(0x88), 0x12, static_cast<char>(0x71), static_cast<char>(0xEA), 0x02, |
| 22 | 0x01, 0x64, 0x65, static_cast<char>(0xFE), 0x6B, static_cast<char>(0x80), 0x45, 0x32, static_cast<char>(0x88), 0x12, static_cast<char>(0x71), static_cast<char>(0xEA), 0x00, |
| 23 | 0x01, 0x64, 0x65, static_cast<char>(0xFE), 0x6B, static_cast<char>(0x80), 0x45, 0x32, static_cast<char>(0x88), 0x12, static_cast<char>(0x71), static_cast<char>(0xEA), 0x00, |
| 24 | 0x00, 0x00, 0x00, 0x00, 0x6B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 25 | }; |
| 26 | |
| 27 | TEST_CASE("testFilters") |
| 28 | { |
| 29 | for (unsigned i = 0; i <= (unsigned)PdfFilterType::Crypt; i++) |
| 30 | { |
| 31 | testFilter(static_cast<PdfFilterType>(i), { s_testBuffer1.data(), s_testBuffer1.length() }); |
| 32 | testFilter(static_cast<PdfFilterType>(i), { s_testBuffer2, std::size(s_testBuffer2) }); |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | void testFilter(PdfFilterType filterType, const bufferview& view) |
| 37 | { |
| 38 | charbuff encoded; |
| 39 | charbuff decoded; |
| 40 | |
| 41 | unique_ptr<PdfFilter> filter; |
| 42 | if (!PdfFilterFactory::TryCreate(filterType, filter)) |
| 43 | { |
| 44 | INFO(utls::Format("!!! Filter {} not implemented.\n", (int)filterType)); |
| 45 | return; |
| 46 | } |
| 47 | |
| 48 | INFO(utls::Format("Testing Algorithm {}:", (int)filterType)); |
| 49 | INFO("\t-> Testing Encoding"); |
| 50 | try |
| 51 | { |
| 52 | filter->EncodeTo(encoded, view); |
| 53 | } |
| 54 | catch (PdfError& e) |
| 55 | { |
| 56 | if (e == PdfErrorCode::UnsupportedFilter) |
| 57 | { |
| 58 | INFO(utls::Format("\t-> Encoding not supported for filter {}.", (unsigned)filterType)); |
| 59 | return; |
| 60 | } |
| 61 | else |
| 62 | { |
| 63 | e.AddToCallStack(__FILE__, __LINE__); |
| 64 | throw e; |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | INFO("\t-> Testing Decoding"); |
| 69 | try |
| 70 | { |
| 71 | filter->DecodeTo(decoded, encoded); |
| 72 | } |
| 73 | catch (PdfError& e) |
| 74 | { |
| 75 | if (e == PdfErrorCode::UnsupportedFilter) |
| 76 | { |
| 77 | INFO(utls::Format("\t-> Decoding not supported for filter {}", (int)filterType)); |
| 78 | return; |
| 79 | } |
| 80 | else |
| 81 | { |
| 82 | e.AddToCallStack(__FILE__, __LINE__); |
| 83 | throw e; |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | INFO(utls::Format("\t-> Original Data Length: {}", view.size())); |
| 88 | INFO(utls::Format("\t-> Encoded Data Length: {}", encoded.size())); |
| 89 | INFO(utls::Format("\t-> Decoded Data Length: {}", decoded.size())); |
| 90 | |
| 91 | REQUIRE(decoded == view); |
| 92 | |
| 93 | INFO("\t-> Test succeeded!"); |
| 94 | } |