| 1 |
/* |
| 2 |
* Created on 18 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.assertFalse; |
| 22 |
import static org.junit.Assert.assertTrue; |
| 23 |
|
| 24 |
import java.util.Arrays; |
| 25 |
import java.util.Collection; |
| 26 |
|
| 27 |
import org.junit.Before; |
| 28 |
import org.junit.Test; |
| 29 |
import org.sejda.sambox.cos.COSDictionary; |
| 30 |
import org.sejda.sambox.cos.COSName; |
| 31 |
import org.sejda.sambox.pdmodel.PDDocument; |
| 32 |
import org.sejda.sambox.pdmodel.interactive.annotation.PDAnnotation; |
| 33 |
import org.sejda.sambox.pdmodel.interactive.annotation.PDAnnotationLink; |
| 34 |
import org.sejda.sambox.pdmodel.interactive.annotation.PDAnnotationWidget; |
| 35 |
import org.sejda.sambox.pdmodel.interactive.form.PDAcroForm; |
| 36 |
import org.sejda.sambox.pdmodel.interactive.form.PDField; |
| 37 |
import org.sejda.sambox.pdmodel.interactive.form.PDFieldFactory; |
| 38 |
|
| 39 |
/** |
| 40 |
* @author Andrea Vacondio |
| 41 |
* |
| 42 |
*/ |
| 43 |
public class SignatureClipperTest { |
| 44 |
|
| 45 |
private COSDictionary dictionary; |
| 46 |
|
| 47 |
@Before |
| 48 |
public void setUp() { |
| 49 |
dictionary = new COSDictionary(); |
| 50 |
dictionary.setItem(COSName.FT, COSName.SIG); |
| 51 |
dictionary.setItem(COSName.V, COSName.getPDFName("value")); |
| 52 |
dictionary.setItem(COSName.SV, COSName.getPDFName("seed")); |
| 53 |
dictionary.setItem(COSName.LOCK, COSName.getPDFName("lock")); |
| 54 |
} |
| 55 |
|
| 56 |
@Test |
| 57 |
public void nullCollectionDoesntFail() { |
| 58 |
SignatureClipper.clipSignatures((Collection<PDAnnotation>) null); |
| 59 |
} |
| 60 |
|
| 61 |
@Test |
| 62 |
public void nullFieldDoesntFail() { |
| 63 |
assertFalse(SignatureClipper.clipSignature(null)); |
| 64 |
} |
| 65 |
|
| 66 |
@Test |
| 67 |
public void clipWidget() { |
| 68 |
PDAnnotationWidget widget = new PDAnnotationWidget(dictionary); |
| 69 |
SignatureClipper.clipSignatures(Arrays.asList(widget)); |
| 70 |
assertFalse(widget.getCOSObject().containsKey(COSName.V)); |
| 71 |
assertFalse(widget.getCOSObject().containsKey(COSName.SV)); |
| 72 |
assertFalse(widget.getCOSObject().containsKey(COSName.LOCK)); |
| 73 |
} |
| 74 |
|
| 75 |
@Test |
| 76 |
public void clipNonWidget() { |
| 77 |
PDAnnotationLink widget = new PDAnnotationLink(dictionary); |
| 78 |
SignatureClipper.clipSignatures(Arrays.asList(widget)); |
| 79 |
assertTrue(widget.getCOSObject().containsKey(COSName.V)); |
| 80 |
assertTrue(widget.getCOSObject().containsKey(COSName.SV)); |
| 81 |
assertTrue(widget.getCOSObject().containsKey(COSName.LOCK)); |
| 82 |
} |
| 83 |
|
| 84 |
@Test |
| 85 |
public void clipWidgetNonSign() { |
| 86 |
dictionary.setItem(COSName.FT, COSName.BTN); |
| 87 |
PDAnnotationWidget widget = new PDAnnotationWidget(dictionary); |
| 88 |
SignatureClipper.clipSignatures(Arrays.asList(widget)); |
| 89 |
assertTrue(widget.getCOSObject().containsKey(COSName.V)); |
| 90 |
assertTrue(widget.getCOSObject().containsKey(COSName.SV)); |
| 91 |
assertTrue(widget.getCOSObject().containsKey(COSName.LOCK)); |
| 92 |
} |
| 93 |
|
| 94 |
@Test |
| 95 |
public void clipField() { |
| 96 |
PDField field = PDFieldFactory.createFieldAddingChildToParent(new PDAcroForm(new PDDocument()), dictionary, |
| 97 |
null); |
| 98 |
assertTrue(SignatureClipper.clipSignature(field)); |
| 99 |
assertFalse(field.getCOSObject().containsKey(COSName.V)); |
| 100 |
assertFalse(field.getCOSObject().containsKey(COSName.SV)); |
| 101 |
assertFalse(field.getCOSObject().containsKey(COSName.LOCK)); |
| 102 |
} |
| 103 |
|
| 104 |
@Test |
| 105 |
public void clipFieldNotSignature() { |
| 106 |
dictionary.setItem(COSName.FT, COSName.BTN); |
| 107 |
PDField field = PDFieldFactory.createFieldAddingChildToParent(new PDAcroForm(new PDDocument()), dictionary, |
| 108 |
null); |
| 109 |
SignatureClipper.clipSignature(field); |
| 110 |
assertTrue(field.getCOSObject().containsKey(COSName.V)); |
| 111 |
assertTrue(field.getCOSObject().containsKey(COSName.SV)); |
| 112 |
assertTrue(field.getCOSObject().containsKey(COSName.LOCK)); |
| 113 |
} |
| 114 |
} |