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.

MODCAParserTestCase.java 8.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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.afp.parser;
  19. import java.io.ByteArrayInputStream;
  20. import java.io.ByteArrayOutputStream;
  21. import java.io.EOFException;
  22. import java.io.InputStream;
  23. import java.util.Arrays;
  24. import org.junit.Test;
  25. import static org.junit.Assert.assertEquals;
  26. import static org.junit.Assert.assertNull;
  27. import static org.junit.Assert.assertTrue;
  28. import static org.junit.Assert.fail;
  29. /**
  30. * MODCAParser and MODCAParser.UnparsedStructuredField Unit tests
  31. */
  32. public class MODCAParserTestCase {
  33. /** The carriage control character (0x5A) used to indicate the start of a structured field. */
  34. public static final byte CARRIAGE_CONTROL_CHAR = (byte)0x5A;
  35. /**ASCII carriage return control character*/
  36. public static final byte CARRIAGE_RETURN = (byte)0x0A;
  37. /**ASCII line feed control character */
  38. public static final byte LINE_FEED = (byte)0x0D;
  39. /** 8 byte introducer describe the SF */
  40. private static final int INTRODUCER_LENGTH = 8;
  41. /**
  42. * Test that the MODCA parser recognises carriage control (0x5A) as the Structured Field
  43. * delimeter
  44. *
  45. * @throws Exception *
  46. */
  47. @Test
  48. public void testReadNextStructuredField1() throws Exception {
  49. // carriage control (0x5A) delimits structured fields,
  50. // and control is handed to readStructuredField(DataInputStream)
  51. byte[][] goodInputStream = new byte[][]{
  52. new byte[]{CARRIAGE_CONTROL_CHAR}
  53. };
  54. for (byte[] b : goodInputStream) {
  55. try {
  56. new MODCAParser(new ByteArrayInputStream(b))
  57. .readNextStructuredField();
  58. fail("BAD SF should throw EOF: " + byteArrayToString(b));
  59. } catch (EOFException eof) {
  60. //passed
  61. }
  62. }
  63. // EOFException thrown when reading the input stream are caught and
  64. // a null value is returned
  65. byte[][] badInputStream = new byte[][]{
  66. new byte[]{},
  67. new byte[]{CARRIAGE_RETURN},
  68. new byte[]{LINE_FEED}
  69. };
  70. for (byte[] b : badInputStream) {
  71. UnparsedStructuredField usf = new MODCAParser(new ByteArrayInputStream(b))
  72. .readNextStructuredField();
  73. assertNull(usf);
  74. }
  75. }
  76. /**
  77. * Test that the MODCA parser correctly constructs an UnparsedStructuredField
  78. * from a well formed structured field
  79. *
  80. * @throws Exception *
  81. */
  82. @Test
  83. public void testReadNextStructuredField2() throws Exception {
  84. // no extension data
  85. testSF((byte)0xd3, (byte)0xa8, (byte)0x89, //SFTypeID
  86. (byte)0, //flags excluding the bits for
  87. //extension present, segmented data and padding present
  88. false, false,
  89. new byte[]{0, 0},
  90. new byte[]{1}, null);
  91. // with extension data
  92. testSF((byte)0xd3, (byte)0xa8, (byte)0x89, //SFTypeID
  93. (byte)0, //flags excluding the bits for
  94. //extension present, segmented data and padding present
  95. false, false,
  96. new byte[]{0, 0},
  97. new byte[]{1}, new byte[]{10});
  98. // with ignored reserved bits
  99. testSF((byte)0xd3, (byte)0xa8, (byte)0x89, //SFTypeID
  100. (byte)0, //flags excluding the bits for
  101. //extension present, segmented data and padding present
  102. false, false,
  103. new byte[]{1, 2},
  104. new byte[]{1}, null);
  105. // with padding present and segmented data
  106. testSF((byte)0xd3, (byte)0xa8, (byte)0x89, //SFTypeID
  107. (byte)(1 << 3), //flags excluding the bits for
  108. //extension present, segmented data and padding present
  109. true, true,
  110. new byte[]{0, 0},
  111. new byte[]{1}, null);
  112. // with flags non zero
  113. testSF((byte)0xd3, (byte)0xa8, (byte)0x89, //SFTypeID
  114. (byte)(1 << 3), //flags excluding the bits for
  115. //extension present, segmented data and padding present
  116. false, false,
  117. new byte[]{0, 0},
  118. new byte[]{1}, null);
  119. }
  120. private void testSF(byte classCode, byte typeCode, byte categoryCode,
  121. byte flags, boolean segmentedData, boolean paddingPresent, byte[] reserved,
  122. byte[] data, byte[] extData) throws Exception {
  123. byte extDataLength = 0;
  124. boolean extensionPresent = (extData != null);
  125. if (extensionPresent) {
  126. flags = (byte)(flags | 0x01);
  127. extDataLength = (byte)(extData.length + 1); //length includes length byte
  128. }
  129. if (segmentedData) {
  130. flags = (byte)(flags | 0x04);
  131. }
  132. if (paddingPresent) {
  133. flags = (byte)(flags | 0x10);
  134. }
  135. short length = (short)(INTRODUCER_LENGTH + data.length + extDataLength);
  136. byte[] lengthBytes = new byte[]{(byte)(length >> 8), (byte)(length & 0xFF)};
  137. byte[] sfBytes = new byte[length];
  138. //introducer bytes
  139. sfBytes[0] = lengthBytes[0];
  140. sfBytes[1] = lengthBytes[1];
  141. sfBytes[2] = classCode;
  142. sfBytes[3] = typeCode;
  143. sfBytes[4] = categoryCode;
  144. sfBytes[5] = flags;
  145. sfBytes[6] = reserved[0];
  146. sfBytes[7] = reserved[1];
  147. if (extDataLength > 0) {
  148. sfBytes[8] = (byte)(extData.length + 1);
  149. System.arraycopy(extData, 0, sfBytes, 9, extData.length);
  150. }
  151. System.arraycopy(data, 0, sfBytes, length - data.length, data.length);
  152. byte[] delimiteredSF = new byte[length + 1];
  153. delimiteredSF[0] = (byte)0x5A;
  154. System.arraycopy(sfBytes, 0, delimiteredSF, 1, length);
  155. InputStream bis = new ByteArrayInputStream(delimiteredSF);
  156. UnparsedStructuredField actual = new MODCAParser(bis)
  157. .readNextStructuredField();
  158. //check introducer
  159. assertEquals(length, actual.getSfLength());
  160. assertEquals(classCode, actual.getSfClassCode());
  161. assertEquals(typeCode, actual.getSfTypeCode());
  162. assertEquals(categoryCode, actual.getSfCategoryCode());
  163. assertEquals(extensionPresent, actual.isSfiExtensionPresent());
  164. assertEquals(segmentedData, actual.isSfiSegmentedData());
  165. assertEquals(paddingPresent, actual.isSfiPaddingPresent());
  166. byte[] introducerData = new byte[]{(byte)(length >> 8), (byte)(length & 0xFF),
  167. classCode, typeCode, categoryCode, flags, reserved[0], reserved[1]};
  168. assertTrue(Arrays.equals(introducerData, actual.getIntroducerData()));
  169. //check data
  170. assertTrue(Arrays.equals(data, actual.getData()));
  171. //check extension data
  172. if (extData != null) {
  173. assertTrue(Arrays.equals(extData, actual.getExtData()));
  174. }
  175. assertEquals(
  176. (extData == null) ? 0 : extData.length + 1, // 1 byte for length byte
  177. actual.getExtLength());
  178. assertTrue(Arrays.equals(data, actual.getData()));
  179. int expectedSfTypeID = ((classCode & 0xFF) << 16)
  180. | ((typeCode & 0xFF) << 8)
  181. | (categoryCode & 0xFF);
  182. assertEquals(expectedSfTypeID, actual.getSfTypeID());
  183. assertTrue(Arrays.equals(sfBytes, actual.getCompleteFieldAsBytes()));
  184. ByteArrayOutputStream baos = new ByteArrayOutputStream();
  185. actual.writeTo(baos);
  186. assertTrue(Arrays.equals(sfBytes, baos.toByteArray()));
  187. }
  188. private static String byteArrayToString(byte[] byteArray) {
  189. StringBuilder sb = new StringBuilder();
  190. for (byte b : byteArray) {
  191. sb.append(Integer.toHexString(b & 0xFF)).append(" ");
  192. }
  193. return sb.toString();
  194. }
  195. }