View Javadoc
1   /* 
2    * Copyright (C) 2016 Hobrasoft s.r.o.
3    *
4    * This program is free software: you can redistribute it and/or modify
5    * it under the terms of the GNU Affero General Public License as published by
6    * the Free Software Foundation, either version 3 of the License, or
7    * (at your option) any later version.
8    *
9    * This program is distributed in the hope that it will be useful,
10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12   * GNU Affero General Public License for more details.
13   *
14   * You should have received a copy of the GNU Affero General Public License
15   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16   */
17  package cz.hobrasoft.pdfmu;
18  
19  import com.tngtech.java.junit.dataprovider.DataProvider;
20  import com.tngtech.java.junit.dataprovider.DataProviderRunner;
21  import com.tngtech.java.junit.dataprovider.UseDataProvider;
22  import java.io.File;
23  import java.io.IOException;
24  import java.util.ArrayList;
25  import java.util.List;
26  import org.junit.Assert;
27  import org.junit.Test;
28  import org.junit.contrib.java.lang.system.Assertion;
29  import org.junit.experimental.categories.Category;
30  import org.junit.runner.RunWith;
31  
32  /**
33   * @author Filip Bártek
34   */
35  @RunWith(DataProviderRunner.class)
36  public class MainSignTsaIT extends MainTest {
37  
38      public interface Online {
39          /* category marker */ }
40  
41      /**
42       * The SSL configuration must be shared among the tests, because it cannot
43       * be re-configured once it is used for the first time. The first test that
44       * sets and uses SSL determines the SSL configuration for the remainder of
45       * the tests. The truststore is stored internally in the JVM so the
46       * truststore file need only be present during the first use.
47       */
48      private List<String> sslArgs() throws IOException {
49          List<String> argsList = new ArrayList<>();
50  
51          File sslTruststoreFile = new FileResource("cacerts.jks").getFile(folder);
52          argsList.add("--ssl-truststore");
53          argsList.add(sslTruststoreFile.getAbsolutePath());
54          //argsList.add("--ssl-truststore-type");
55          //argsList.add("jks");
56          argsList.add("--ssl-truststore-password");
57          argsList.add("changeit");
58  
59          File sslKeystoreFile = new FileResource("auth-changeit.pfx").getFile(folder);
60          argsList.add("--ssl-keystore");
61          argsList.add(sslKeystoreFile.getAbsolutePath());
62          //argsList.add("--ssl-keystore-type");
63          //argsList.add("pkcs12");
64          argsList.add("--ssl-keystore-password");
65          argsList.add("changeit");
66  
67          return argsList;
68      }
69  
70      @Test
71      public void testNoUrl() throws IOException {
72          final PdfFileResource inFileResource = BLANK_12_PDF;
73          File inFile = inFileResource.getFile(folder);
74          File keystoreFile = new FileResource("1.p12").getFile(folder);
75          final File outFile = newFile("out.pdf", false);
76  
77          List<String> argsList = new ArrayList<>();
78          argsList.add("sign");
79          argsList.add(inFile.getAbsolutePath());
80          argsList.add("--out");
81          argsList.add(outFile.getAbsolutePath());
82          argsList.add("--keystore");
83          argsList.add(keystoreFile.getAbsolutePath());
84          argsList.add("--tsa-url");
85  
86          exit.expectSystemExitWithStatus(15);
87          exit.checkAssertionAfterwards(new Assertion() {
88              @Override
89              public void checkAssertion() {
90                  Assert.assertFalse(outFile.exists());
91              }
92          });
93          Main.main(argsList.toArray(new String[]{}));
94          assert false;
95      }
96  
97      @Test
98      public void testInvalidUrl() throws IOException {
99          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 }