You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

PDFSigningTestCase.java 4.1KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. /* $Id$ */
  18. package org.apache.fop.pdf;
  19. import java.io.ByteArrayInputStream;
  20. import java.io.ByteArrayOutputStream;
  21. import java.io.File;
  22. import java.util.StringTokenizer;
  23. import javax.xml.transform.Result;
  24. import javax.xml.transform.Source;
  25. import javax.xml.transform.Transformer;
  26. import javax.xml.transform.TransformerFactory;
  27. import javax.xml.transform.sax.SAXResult;
  28. import javax.xml.transform.stream.StreamSource;
  29. import org.junit.Assert;
  30. import org.junit.Test;
  31. import org.apache.fop.apps.FOUserAgent;
  32. import org.apache.fop.apps.Fop;
  33. import org.apache.fop.apps.FopFactory;
  34. import org.apache.fop.apps.MimeConstants;
  35. import org.apache.fop.fo.pagination.LayoutMasterSetTestCase;
  36. public class PDFSigningTestCase {
  37. @Test
  38. public void textFO() throws Exception {
  39. ByteArrayOutputStream out = new ByteArrayOutputStream();
  40. foToOutput(out, MimeConstants.MIME_PDF);
  41. StringTokenizer byteRange = new StringTokenizer(out.toString().split("/ByteRange ")[1]);
  42. byteRange.nextToken();
  43. int startOfContents = Integer.parseInt(byteRange.nextToken());
  44. int endOfContents = Integer.parseInt(byteRange.nextToken());
  45. int sizeOfEnd = Integer.parseInt(byteRange.nextToken().replace("]", ""));
  46. int sizeOfContents = 18944;
  47. Assert.assertEquals(endOfContents, startOfContents + sizeOfContents + 2);
  48. Assert.assertEquals(out.size(), startOfContents + sizeOfContents + 2 + sizeOfEnd);
  49. ByteArrayInputStream bis = new ByteArrayInputStream(out.toByteArray());
  50. bis.skip(startOfContents);
  51. Assert.assertEquals(bis.read(), '<');
  52. bis.skip(sizeOfContents);
  53. Assert.assertEquals(bis.read(), '>');
  54. byte[] end = new byte[200];
  55. bis.read(end);
  56. String endStr = new String(end);
  57. Assert.assertTrue(endStr.contains(
  58. "/ByteRange [0 " + startOfContents + " " + endOfContents + " " + sizeOfEnd + "]"));
  59. Assert.assertTrue(endStr.contains("/FT /Sig\n"
  60. + " /Type /Annot\n"
  61. + " /Subtype /Widget\n"
  62. + " /F 132\n"
  63. + " /T (Signature1)\n"
  64. + " /Rect [0 0 0 0]"));
  65. }
  66. private void foToOutput(ByteArrayOutputStream out, String mimeFopIf) throws Exception {
  67. FopFactory fopFactory = getFopFactory();
  68. FOUserAgent userAgent = fopFactory.newFOUserAgent();
  69. Fop fop = fopFactory.newFop(mimeFopIf, userAgent, out);
  70. Transformer transformer = TransformerFactory.newInstance().newTransformer();
  71. Source src = new StreamSource(LayoutMasterSetTestCase.class.getResourceAsStream("side-regions.fo"));
  72. Result res = new SAXResult(fop.getDefaultHandler());
  73. transformer.transform(src, res);
  74. }
  75. private FopFactory getFopFactory() throws Exception {
  76. String pkcs = PDFSigningTestCase.class.getResource("keystore.pkcs12").toString();
  77. String fopxconf = "<fop version=\"1.0\">\n"
  78. + " <renderers>\n"
  79. + " <renderer mime=\"application/pdf\">\n"
  80. + " <sign-params>\n"
  81. + " <keystore>" + pkcs + "</keystore>\n"
  82. + " </sign-params>\n"
  83. + " </renderer>\n"
  84. + " </renderers>\n"
  85. + "</fop>\n";
  86. return FopFactory.newInstance(new File(".").toURI(),
  87. new ByteArrayInputStream(fopxconf.getBytes()));
  88. }
  89. }