| 1 |
/* |
| 2 |
* Created on 17 set 2015 |
| 3 |
* Copyright 2015 by Andrea Vacondio ([email protected]). |
| 4 |
* This file is part of Sejda. |
| 5 |
* |
| 6 |
* Sejda is free software: you can redistribute it and/or modify |
| 7 |
* it under the terms of the GNU Affero General Public License as published by |
| 8 |
* the Free Software Foundation, either version 3 of the License, or |
| 9 |
* (at your option) any later version. |
| 10 |
* |
| 11 |
* Sejda is distributed in the hope that it will be useful, |
| 12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 |
* GNU Affero General Public License for more details. |
| 15 |
* |
| 16 |
* You should have received a copy of the GNU Affero General Public License |
| 17 |
* along with Sejda. If not, see <http://www.gnu.org/licenses/>. |
| 18 |
*/ |
| 19 |
package org.sejda.impl.sambox.component; |
| 20 |
|
| 21 |
import static org.junit.Assert.assertEquals; |
| 22 |
import static org.junit.Assert.assertNotNull; |
| 23 |
import static org.junit.Assert.fail; |
| 24 |
|
| 25 |
import java.io.File; |
| 26 |
import java.io.FileOutputStream; |
| 27 |
import java.io.IOException; |
| 28 |
import java.io.InputStream; |
| 29 |
import java.io.OutputStream; |
| 30 |
|
| 31 |
import org.junit.Test; |
| 32 |
import org.sejda.core.service.BaseTaskTest; |
| 33 |
import org.sejda.core.support.io.IOUtils; |
| 34 |
import org.sejda.model.exception.TaskIOException; |
| 35 |
import org.sejda.model.exception.TaskWrongPasswordException; |
| 36 |
import org.sejda.model.input.PdfFileSource; |
| 37 |
import org.sejda.model.input.PdfStreamSource; |
| 38 |
|
| 39 |
/** |
| 40 |
* @author Andrea Vacondio |
| 41 |
* |
| 42 |
*/ |
| 43 |
public class DefaultPdfSourceOpenerTest { |
| 44 |
|
| 45 |
@Test |
| 46 |
public void openDocument() throws TaskIOException { |
| 47 |
assertNotNull(new DefaultPdfSourceOpener().open(PdfStreamSource.newInstanceNoPassword( |
| 48 |
getClass().getClassLoader().getResourceAsStream("pdf/test_file.pdf"), "my source"))); |
| 49 |
} |
| 50 |
|
| 51 |
@Test |
| 52 |
public void openDocumentWithPasswordAES128() throws TaskIOException { |
| 53 |
assertNotNull(new DefaultPdfSourceOpener().open(PdfStreamSource.newInstanceWithPassword( |
| 54 |
getClass().getClassLoader().getResourceAsStream("pdf/encrypted_AES128_user_pwd.pdf"), "my source", |
| 55 |
"test"))); |
| 56 |
} |
| 57 |
|
| 58 |
@Test |
| 59 |
public void openDocumentWithPasswordAES256() throws TaskIOException { |
| 60 |
assertNotNull(new DefaultPdfSourceOpener().open(PdfStreamSource.newInstanceWithPassword( |
| 61 |
getClass().getClassLoader().getResourceAsStream("pdf/encrypted_AES256_user_pwd.pdf"), "my source", |
| 62 |
"test"))); |
| 63 |
} |
| 64 |
|
| 65 |
@Test |
| 66 |
public void openDocumentWrongPassword() throws TaskIOException, IOException { |
| 67 |
try { |
| 68 |
File tmp = IOUtils.createTemporaryBufferWithName("dumbo.pdf"); |
| 69 |
OutputStream out = new FileOutputStream(tmp); |
| 70 |
InputStream in = getClass().getClassLoader().getResourceAsStream("pdf/encrypted_AES128_user_pwd.pdf"); |
| 71 |
|
| 72 |
try { |
| 73 |
org.apache.commons.io.IOUtils.copy(in, out); |
| 74 |
} finally { |
| 75 |
org.sejda.commons.util.IOUtils.closeQuietly(out); |
| 76 |
org.sejda.commons.util.IOUtils.closeQuietly(in); |
| 77 |
} |
| 78 |
|
| 79 |
new DefaultPdfSourceOpener().open(PdfFileSource.newInstanceWithPassword(tmp, "my source")); |
| 80 |
fail("Exception expected"); |
| 81 |
} catch (TaskWrongPasswordException e) { |
| 82 |
assertEquals("Unable to open 'dumbo.pdf' due to a wrong password.", e.getMessage()); |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
@Test(expected = TaskIOException.class) |
| 87 |
public void openDocumentError() throws TaskIOException { |
| 88 |
new DefaultPdfSourceOpener().open(PdfStreamSource.newInstanceNoPassword( |
| 89 |
getClass().getClassLoader().getResourceAsStream("pdf/not_a_pdf.pdf"), "my source")); |
| 90 |
|
| 91 |
} |
| 92 |
|
| 93 |
@Test(expected = TaskIOException.class) |
| 94 |
public void openDocumentTwice_streamSource_not_possible() throws TaskIOException, IOException { |
| 95 |
PdfStreamSource source = PdfStreamSource.newInstanceNoPassword( |
| 96 |
getClass().getClassLoader().getResourceAsStream("pdf/test_file.pdf"), "my source"); |
| 97 |
try(PDDocumentHandler handler = new DefaultPdfSourceOpener().open(source)) { |
| 98 |
assertNotNull(handler.getUnderlyingPDDocument()); |
| 99 |
} |
| 100 |
|
| 101 |
try(PDDocumentHandler handler = new DefaultPdfSourceOpener().open(source)) { |
| 102 |
// exception expected |
| 103 |
fail("Exception was expected"); |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
@Test |
| 108 |
public void openDocumentTwice_fileSource() throws TaskIOException, IOException { |
| 109 |
PdfFileSource source = BaseTaskTest.customInputAsFileSource("pdf/test_file.pdf"); |
| 110 |
try(PDDocumentHandler handler = new DefaultPdfSourceOpener().open(source)) { |
| 111 |
assertNotNull(handler.getUnderlyingPDDocument()); |
| 112 |
} |
| 113 |
|
| 114 |
try(PDDocumentHandler handler = new DefaultPdfSourceOpener().open(source)) { |
| 115 |
assertNotNull(handler.getUnderlyingPDDocument()); |
| 116 |
} |
| 117 |
} |
| 118 |
} |