| 1 |
/* |
| 2 |
* Created on 13/set/2011 |
| 3 |
* Copyright 2011 by Andrea Vacondio ([email protected]). |
| 4 |
* |
| 5 |
* This file is part of the Sejda source code |
| 6 |
* |
| 7 |
* This program is free software: you can redistribute it and/or modify |
| 8 |
* it under the terms of the GNU Affero General Public License as |
| 9 |
* published by the Free Software Foundation, either version 3 of the |
| 10 |
* License, or (at your option) any later version. |
| 11 |
* |
| 12 |
* This program is distributed in the hope that it will be useful, |
| 13 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 |
* GNU Affero General Public License for more details. |
| 16 |
* |
| 17 |
* You should have received a copy of the GNU Affero General Public License |
| 18 |
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 19 |
*/ |
| 20 |
package org.sejda.impl.sambox.component; |
| 21 |
|
| 22 |
import static org.mockito.Mockito.mock; |
| 23 |
import static org.mockito.Mockito.when; |
| 24 |
|
| 25 |
import org.junit.Before; |
| 26 |
import org.junit.Test; |
| 27 |
import org.sejda.core.Sejda; |
| 28 |
import org.sejda.model.exception.TaskPermissionsException; |
| 29 |
import org.sejda.model.pdf.encryption.PdfAccessPermission; |
| 30 |
import org.sejda.sambox.pdmodel.PDDocument; |
| 31 |
import org.sejda.sambox.pdmodel.encryption.AccessPermission; |
| 32 |
|
| 33 |
/** |
| 34 |
* @author Andrea Vacondio |
| 35 |
* |
| 36 |
*/ |
| 37 |
public class PDDocumentAccessPermissionTest { |
| 38 |
|
| 39 |
private PDDocumentAccessPermission victim; |
| 40 |
private AccessPermission permission; |
| 41 |
|
| 42 |
@Before |
| 43 |
public void setUp() { |
| 44 |
PDDocument document = mock(PDDocument.class); |
| 45 |
permission = mock(AccessPermission.class); |
| 46 |
when(document.getCurrentAccessPermission()).thenReturn(permission); |
| 47 |
victim = new PDDocumentAccessPermission(document); |
| 48 |
} |
| 49 |
|
| 50 |
@Test(expected = IllegalArgumentException.class) |
| 51 |
public void testNull() { |
| 52 |
new PDDocumentAccessPermission(null); |
| 53 |
} |
| 54 |
|
| 55 |
@Test(expected = TaskPermissionsException.class) |
| 56 |
public void testNotOwner() throws TaskPermissionsException { |
| 57 |
when(permission.isOwnerPermission()).thenReturn(Boolean.FALSE); |
| 58 |
victim.ensureOwnerPermissions(); |
| 59 |
} |
| 60 |
|
| 61 |
@Test |
| 62 |
public void testOwner() throws TaskPermissionsException { |
| 63 |
when(permission.isOwnerPermission()).thenReturn(Boolean.TRUE); |
| 64 |
victim.ensureOwnerPermissions(); |
| 65 |
} |
| 66 |
|
| 67 |
@Test |
| 68 |
public void testOwnerUnethical() throws TaskPermissionsException { |
| 69 |
when(permission.isOwnerPermission()).thenReturn(Boolean.FALSE); |
| 70 |
System.setProperty(Sejda.UNETHICAL_READ_PROPERTY_NAME, "true"); |
| 71 |
victim.ensureOwnerPermissions(); |
| 72 |
System.setProperty(Sejda.UNETHICAL_READ_PROPERTY_NAME, "false"); |
| 73 |
} |
| 74 |
|
| 75 |
@Test(expected = TaskPermissionsException.class) |
| 76 |
public void testNotPrint() throws TaskPermissionsException { |
| 77 |
when(permission.canPrint()).thenReturn(Boolean.FALSE); |
| 78 |
victim.ensurePermission(PdfAccessPermission.PRINT); |
| 79 |
} |
| 80 |
|
| 81 |
@Test |
| 82 |
public void testPrint() throws TaskPermissionsException { |
| 83 |
when(permission.canPrint()).thenReturn(Boolean.TRUE); |
| 84 |
victim.ensurePermission(PdfAccessPermission.PRINT); |
| 85 |
} |
| 86 |
|
| 87 |
@Test |
| 88 |
public void testPrintUnethical() throws TaskPermissionsException { |
| 89 |
System.setProperty(Sejda.UNETHICAL_READ_PROPERTY_NAME, "true"); |
| 90 |
when(permission.canPrint()).thenReturn(Boolean.FALSE); |
| 91 |
victim.ensurePermission(PdfAccessPermission.PRINT); |
| 92 |
System.setProperty(Sejda.UNETHICAL_READ_PROPERTY_NAME, "false"); |
| 93 |
} |
| 94 |
|
| 95 |
@Test(expected = TaskPermissionsException.class) |
| 96 |
public void testNotDegradedPrint() throws TaskPermissionsException { |
| 97 |
when(permission.canPrintDegraded()).thenReturn(Boolean.FALSE); |
| 98 |
victim.ensurePermission(PdfAccessPermission.DEGRADATED_PRINT); |
| 99 |
} |
| 100 |
|
| 101 |
@Test |
| 102 |
public void testDegradedPrint() throws TaskPermissionsException { |
| 103 |
when(permission.canPrintDegraded()).thenReturn(Boolean.TRUE); |
| 104 |
victim.ensurePermission(PdfAccessPermission.DEGRADATED_PRINT); |
| 105 |
} |
| 106 |
|
| 107 |
@Test(expected = TaskPermissionsException.class) |
| 108 |
public void testNotAssemble() throws TaskPermissionsException { |
| 109 |
when(permission.canAssembleDocument()).thenReturn(Boolean.FALSE); |
| 110 |
victim.ensurePermission(PdfAccessPermission.ASSEMBLE); |
| 111 |
} |
| 112 |
|
| 113 |
@Test |
| 114 |
public void testAssemble() throws TaskPermissionsException { |
| 115 |
when(permission.canAssembleDocument()).thenReturn(Boolean.TRUE); |
| 116 |
victim.ensurePermission(PdfAccessPermission.ASSEMBLE); |
| 117 |
} |
| 118 |
|
| 119 |
@Test(expected = TaskPermissionsException.class) |
| 120 |
public void testNotAannotation() throws TaskPermissionsException { |
| 121 |
when(permission.canModifyAnnotations()).thenReturn(Boolean.FALSE); |
| 122 |
victim.ensurePermission(PdfAccessPermission.ANNOTATION); |
| 123 |
} |
| 124 |
|
| 125 |
@Test |
| 126 |
public void testAnnotation() throws TaskPermissionsException { |
| 127 |
when(permission.canModifyAnnotations()).thenReturn(Boolean.TRUE); |
| 128 |
victim.ensurePermission(PdfAccessPermission.ANNOTATION); |
| 129 |
} |
| 130 |
|
| 131 |
@Test(expected = TaskPermissionsException.class) |
| 132 |
public void testNotCopy() throws TaskPermissionsException { |
| 133 |
when(permission.canExtractContent()).thenReturn(Boolean.FALSE); |
| 134 |
victim.ensurePermission(PdfAccessPermission.COPY_AND_EXTRACT); |
| 135 |
} |
| 136 |
|
| 137 |
@Test |
| 138 |
public void testCopy() throws TaskPermissionsException { |
| 139 |
when(permission.canExtractContent()).thenReturn(Boolean.TRUE); |
| 140 |
victim.ensurePermission(PdfAccessPermission.COPY_AND_EXTRACT); |
| 141 |
} |
| 142 |
|
| 143 |
@Test(expected = TaskPermissionsException.class) |
| 144 |
public void testNotExtract() throws TaskPermissionsException { |
| 145 |
when(permission.canExtractForAccessibility()).thenReturn(Boolean.FALSE); |
| 146 |
victim.ensurePermission(PdfAccessPermission.EXTRACTION_FOR_DISABLES); |
| 147 |
} |
| 148 |
|
| 149 |
@Test |
| 150 |
public void testExtract() throws TaskPermissionsException { |
| 151 |
when(permission.canExtractForAccessibility()).thenReturn(Boolean.TRUE); |
| 152 |
victim.ensurePermission(PdfAccessPermission.EXTRACTION_FOR_DISABLES); |
| 153 |
} |
| 154 |
|
| 155 |
@Test(expected = TaskPermissionsException.class) |
| 156 |
public void testNotFillForm() throws TaskPermissionsException { |
| 157 |
when(permission.canFillInForm()).thenReturn(Boolean.FALSE); |
| 158 |
victim.ensurePermission(PdfAccessPermission.FILL_FORMS); |
| 159 |
} |
| 160 |
|
| 161 |
@Test |
| 162 |
public void testFillForm() throws TaskPermissionsException { |
| 163 |
when(permission.canFillInForm()).thenReturn(Boolean.TRUE); |
| 164 |
victim.ensurePermission(PdfAccessPermission.FILL_FORMS); |
| 165 |
} |
| 166 |
|
| 167 |
@Test(expected = TaskPermissionsException.class) |
| 168 |
public void testNotModify() throws TaskPermissionsException { |
| 169 |
when(permission.canModify()).thenReturn(Boolean.FALSE); |
| 170 |
victim.ensurePermission(PdfAccessPermission.MODIFY); |
| 171 |
} |
| 172 |
|
| 173 |
@Test |
| 174 |
public void testModify() throws TaskPermissionsException { |
| 175 |
when(permission.canModify()).thenReturn(Boolean.TRUE); |
| 176 |
victim.ensurePermission(PdfAccessPermission.MODIFY); |
| 177 |
} |
| 178 |
|
| 179 |
@Test(expected = IllegalArgumentException.class) |
| 180 |
public void testNullPermission() throws TaskPermissionsException { |
| 181 |
victim.ensurePermission(null); |
| 182 |
} |
| 183 |
} |