001/* 002 * Copyright (C) 2016 Hobrasoft s.r.o. 003 * 004 * This program is free software: you can redistribute it and/or modify 005 * it under the terms of the GNU Affero General Public License as published by 006 * the Free Software Foundation, either version 3 of the License, or 007 * (at your option) any later version. 008 * 009 * This program is distributed in the hope that it will be useful, 010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 012 * GNU Affero General Public License for more details. 013 * 014 * You should have received a copy of the GNU Affero General Public License 015 * along with this program. If not, see <http://www.gnu.org/licenses/>. 016 */ 017package cz.hobrasoft.pdfmu; 018 019import com.tngtech.java.junit.dataprovider.DataProvider; 020import com.tngtech.java.junit.dataprovider.DataProviderRunner; 021import com.tngtech.java.junit.dataprovider.UseDataProvider; 022import java.io.File; 023import java.io.IOException; 024import java.util.ArrayList; 025import java.util.List; 026import org.junit.Assert; 027import org.junit.Test; 028import org.junit.contrib.java.lang.system.Assertion; 029import org.junit.experimental.categories.Category; 030import org.junit.runner.RunWith; 031 032/** 033 * @author Filip Bártek 034 */ 035@RunWith(DataProviderRunner.class) 036public class MainSignTsaIT extends MainTest { 037 038 public interface Online { 039 /* category marker */ } 040 041 /** 042 * The SSL configuration must be shared among the tests, because it cannot 043 * be re-configured once it is used for the first time. The first test that 044 * sets and uses SSL determines the SSL configuration for the remainder of 045 * the tests. The truststore is stored internally in the JVM so the 046 * truststore file need only be present during the first use. 047 */ 048 private List<String> sslArgs() throws IOException { 049 List<String> argsList = new ArrayList<>(); 050 051 File sslTruststoreFile = new FileResource("cacerts.jks").getFile(folder); 052 argsList.add("--ssl-truststore"); 053 argsList.add(sslTruststoreFile.getAbsolutePath()); 054 //argsList.add("--ssl-truststore-type"); 055 //argsList.add("jks"); 056 argsList.add("--ssl-truststore-password"); 057 argsList.add("changeit"); 058 059 File sslKeystoreFile = new FileResource("auth-changeit.pfx").getFile(folder); 060 argsList.add("--ssl-keystore"); 061 argsList.add(sslKeystoreFile.getAbsolutePath()); 062 //argsList.add("--ssl-keystore-type"); 063 //argsList.add("pkcs12"); 064 argsList.add("--ssl-keystore-password"); 065 argsList.add("changeit"); 066 067 return argsList; 068 } 069 070 @Test 071 public void testNoUrl() throws IOException { 072 final PdfFileResource inFileResource = BLANK_12_PDF; 073 File inFile = inFileResource.getFile(folder); 074 File keystoreFile = new FileResource("1.p12").getFile(folder); 075 final File outFile = newFile("out.pdf", false); 076 077 List<String> argsList = new ArrayList<>(); 078 argsList.add("sign"); 079 argsList.add(inFile.getAbsolutePath()); 080 argsList.add("--out"); 081 argsList.add(outFile.getAbsolutePath()); 082 argsList.add("--keystore"); 083 argsList.add(keystoreFile.getAbsolutePath()); 084 argsList.add("--tsa-url"); 085 086 exit.expectSystemExitWithStatus(15); 087 exit.checkAssertionAfterwards(new Assertion() { 088 @Override 089 public void checkAssertion() { 090 Assert.assertFalse(outFile.exists()); 091 } 092 }); 093 Main.main(argsList.toArray(new String[]{})); 094 assert false; 095 } 096 097 @Test 098 public void testInvalidUrl() throws IOException { 099 final PdfFileResource inFileResource = BLANK_12_PDF; 100 File inFile = inFileResource.getFile(folder); 101 File keystoreFile = new FileResource("1.p12").getFile(folder); 102 final File outFile = newFile("out.pdf", false); 103 104 List<String> argsList = new ArrayList<>(); 105 argsList.add("sign"); 106 argsList.add(inFile.getAbsolutePath()); 107 argsList.add("--out"); 108 argsList.add(outFile.getAbsolutePath()); 109 argsList.add("--keystore"); 110 argsList.add(keystoreFile.getAbsolutePath()); 111 argsList.add("--tsa-url"); 112 argsList.add(""); 113 114 exit.expectSystemExitWithStatus(91); 115 exit.checkAssertionAfterwards(new Assertion() { 116 @Override 117 public void checkAssertion() { 118 Assert.assertFalse(outFile.exists()); 119 } 120 }); 121 Main.main(argsList.toArray(new String[]{})); 122 assert false; 123 } 124 125 @Test 126 @Category(Online.class) 127 public void testIncorrectUrl() throws IOException { 128 final PdfFileResource inFileResource = BLANK_12_PDF; 129 File inFile = inFileResource.getFile(folder); 130 File keystoreFile = new FileResource("1.p12").getFile(folder); 131 final File outFile = newFile("out.pdf", false); 132 133 List<String> argsList = new ArrayList<>(); 134 argsList.add("--output-format"); 135 argsList.add("json"); 136 argsList.add("sign"); 137 argsList.add(inFile.getAbsolutePath()); 138 argsList.add("--out"); 139 argsList.add(outFile.getAbsolutePath()); 140 argsList.add("--keystore"); 141 argsList.add(keystoreFile.getAbsolutePath()); 142 argsList.add("--tsa-url"); 143 argsList.add("http://example.com/"); 144 145 exit.expectSystemExitWithStatus(61); 146 exit.checkAssertionAfterwards(new Assertion() { 147 @Override 148 public void checkAssertion() { 149 Assert.assertFalse(outFile.exists()); 150 // TODO?: Check the JSON output 151 } 152 }); 153 Main.main(argsList.toArray(new String[]{})); 154 assert false; 155 } 156 157 @Test 158 @Category(Online.class) 159 public void testNoUsername() throws IOException { 160 final PdfFileResource inFileResource = BLANK_12_PDF; 161 File inFile = inFileResource.getFile(folder); 162 File keystoreFile = new FileResource("1.p12").getFile(folder); 163 final File outFile = newFile("out.pdf", false); 164 165 List<String> argsList = new ArrayList<>(); 166 argsList.add("sign"); 167 argsList.add(inFile.getAbsolutePath()); 168 argsList.add("--out"); 169 argsList.add(outFile.getAbsolutePath()); 170 argsList.add("--keystore"); 171 argsList.add(keystoreFile.getAbsolutePath()); 172 argsList.add("--tsa-url"); 173 argsList.add("https://www3.postsignum.cz/DEMOTSA/TSS_user/"); 174 argsList.addAll(sslArgs()); 175 176 exit.expectSystemExitWithStatus(63); 177 exit.checkAssertionAfterwards(new Assertion() { 178 @Override 179 public void checkAssertion() { 180 Assert.assertFalse(outFile.exists()); 181 } 182 }); 183 Main.main(argsList.toArray(new String[]{})); 184 assert false; 185 } 186 187 @Test 188 @Category(Online.class) 189 public void testNoPassword() throws IOException { 190 final PdfFileResource inFileResource = BLANK_12_PDF; 191 File inFile = inFileResource.getFile(folder); 192 File keystoreFile = new FileResource("1.p12").getFile(folder); 193 final File outFile = newFile("out.pdf", false); 194 195 List<String> argsList = new ArrayList<>(); 196 argsList.add("sign"); 197 argsList.add(inFile.getAbsolutePath()); 198 argsList.add("--out"); 199 argsList.add(outFile.getAbsolutePath()); 200 argsList.add("--keystore"); 201 argsList.add(keystoreFile.getAbsolutePath()); 202 argsList.add("--tsa-url"); 203 argsList.add("https://www3.postsignum.cz/DEMOTSA/TSS_user/"); 204 argsList.add("--tsa-username"); 205 argsList.add("demoTSA"); 206 argsList.addAll(sslArgs()); 207 208 exit.expectSystemExitWithStatus(64); 209 exit.checkAssertionAfterwards(new Assertion() { 210 @Override 211 public void checkAssertion() { 212 Assert.assertFalse(outFile.exists()); 213 } 214 }); 215 Main.main(argsList.toArray(new String[]{})); 216 assert false; 217 } 218 219 @Test 220 @Category(Online.class) 221 public void testIncorrectPassword() throws IOException { 222 final PdfFileResource inFileResource = BLANK_12_PDF; 223 File inFile = inFileResource.getFile(folder); 224 File keystoreFile = new FileResource("1.p12").getFile(folder); 225 final File outFile = newFile("out.pdf", false); 226 227 List<String> argsList = new ArrayList<>(); 228 argsList.add("sign"); 229 argsList.add(inFile.getAbsolutePath()); 230 argsList.add("--out"); 231 argsList.add(outFile.getAbsolutePath()); 232 argsList.add("--keystore"); 233 argsList.add(keystoreFile.getAbsolutePath()); 234 argsList.add("--tsa-url"); 235 argsList.add("https://www3.postsignum.cz/DEMOTSA/TSS_user/"); 236 argsList.add("--tsa-username"); 237 argsList.add("demoTSA"); 238 argsList.add("--tsa-password"); 239 argsList.add("incorrect-password"); 240 argsList.addAll(sslArgs()); 241 242 exit.expectSystemExitWithStatus(64); 243 exit.checkAssertionAfterwards(new Assertion() { 244 @Override 245 public void checkAssertion() { 246 Assert.assertFalse(outFile.exists()); 247 } 248 }); 249 Main.main(argsList.toArray(new String[]{})); 250 assert false; 251 } 252 253 @DataProvider 254 public static Object[][] dataProviderUsernamePasswordSuccess() { 255 return new Object[][]{ 256 new Object[]{"https://www3.postsignum.cz/DEMOTSA/TSS_user/", "demoTSA", "demoTSA2010"}, 257 new Object[]{"https://bteszt.e-szigno.hu/tsa", "teszt", "teszt"} 258 }; 259 } 260 261 @Test 262 @Category(Online.class) 263 @UseDataProvider 264 public void testUsernamePasswordSuccess(String url, String username, String password) throws IOException { 265 assert url != null; 266 assert username != null; 267 assert password != null; 268 269 final PdfFileResource inFileResource = BLANK_12_PDF; 270 File inFile = inFileResource.getFile(folder); 271 File keystoreFile = new FileResource("1.p12").getFile(folder); 272 final File outFile = newFile("out.pdf", false); 273 274 List<String> argsList = new ArrayList<>(); 275 argsList.add("sign"); 276 argsList.add(inFile.getAbsolutePath()); 277 argsList.add("--out"); 278 argsList.add(outFile.getAbsolutePath()); 279 argsList.add("--keystore"); 280 argsList.add(keystoreFile.getAbsolutePath()); 281 argsList.add("--tsa-url"); 282 argsList.add(url); 283 argsList.add("--tsa-username"); 284 argsList.add(username); 285 argsList.add("--tsa-password"); 286 argsList.add(password); 287 argsList.addAll(sslArgs()); 288 289 exit.expectSystemExitWithStatus(0); 290 exit.checkAssertionAfterwards(new Assertion() { 291 @Override 292 public void checkAssertion() { 293 Assert.assertTrue(outFile.exists()); 294 // TODO?: Inspect output PDF file 295 } 296 }); 297 Main.main(argsList.toArray(new String[]{})); 298 assert false; 299 } 300 301 @Test 302 @Category(Online.class) 303 public void testCertificateSuccess() throws IOException { 304 final PdfFileResource inFileResource = BLANK_12_PDF; 305 File inFile = inFileResource.getFile(folder); 306 File keystoreFile = new FileResource("1.p12").getFile(folder); 307 final File outFile = newFile("out.pdf", false); 308 309 List<String> argsList = new ArrayList<>(); 310 argsList.add("sign"); 311 argsList.add(inFile.getAbsolutePath()); 312 argsList.add("--out"); 313 argsList.add(outFile.getAbsolutePath()); 314 argsList.add("--keystore"); 315 argsList.add(keystoreFile.getAbsolutePath()); 316 argsList.add("--tsa-url"); 317 argsList.add("https://teszt.e-szigno.hu/tsa"); 318 argsList.addAll(sslArgs()); 319 320 exit.expectSystemExitWithStatus(0); 321 exit.checkAssertionAfterwards(new Assertion() { 322 @Override 323 public void checkAssertion() { 324 Assert.assertTrue(outFile.exists()); 325 } 326 }); 327 Main.main(argsList.toArray(new String[]{})); 328 assert false; 329 } 330}