001: /* ====================================================================
002: Licensed to the Apache Software Foundation (ASF) under one or more
003: contributor license agreements. See the NOTICE file distributed with
004: this work for additional information regarding copyright ownership.
005: The ASF licenses this file to You under the Apache License, Version 2.0
006: (the "License"); you may not use this file except in compliance with
007: the License. You may obtain a copy of the License at
008:
009: http://www.apache.org/licenses/LICENSE-2.0
010:
011: Unless required by applicable law or agreed to in writing, software
012: distributed under the License is distributed on an "AS IS" BASIS,
013: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: See the License for the specific language governing permissions and
015: limitations under the License.
016: ==================================================================== */
017: package org.apache.poi.hdgf.chunks;
018:
019: import junit.framework.TestCase;
020:
021: public class TestChunks extends TestCase {
022: public static final byte[] data_a = new byte[] { 70, 0, 0, 0, -1,
023: -1, -1, -1, 2, 0, 0, 0, 68, 0, 0, 0, 0, 0, 0, 68, 0, 0, 0,
024: 0, 0, 0, 0, 2, 0, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
025: -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0,
026: 0, 0, 0, -1, -1, -1, -1, 0, 0, 0, 0, -1, -1, -1, -1, 0, 0,
027: 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0,
028: 0, 0, 0, 0, 0, 0, 0, 0, 104, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0,
029: 0, 36, 0, 0, 0, 1, 0, 84, 24, 0, 0, 0, 12, 0, 0, 0, 0, 0,
030: 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 3, 0, 0, 0, 0, 0,
031: 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, -110,
032: 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -124, 0, 0, 0, 2, 0, 85,
033: 73, 0, 0, 0, 0, 0, 0, -56, 63, 73, 0, 0, 0, 0, 0, 0, -64,
034: 63, 63, 0, 0, 0, 0, 0, 0, -64, 63, 63, 0, 0, 0, 0, 0, 0,
035: -64, -65, 73, 0, 0, 0, 0, 0, 0, -16, 63, 73, 0, 0, 0, 0, 0,
036: 0, -16, 63, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
037: 0, 0, 0, 0, 0, 0, 0, 80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
038: 0, 0, 0, -16, 63, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
039: 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, -1,
040: 3, 0, 0, 32, 0, 0, 0, 0, 0, -73, 0, 0, 0, 1, 0, 0, 0, 0, 0,
041: 0, 0, 79, 0, 0, 0, 2, 0, 85, 32, 32, 64, 0, 0, 0, 0, 0, 0,
042: 0, 0, 64, 0, 0, 0, 0, 0, 0, 0, 0, 64, 0, 0, 0, 0, 0, 0, 0,
043: 0, 64, 0, 0, 0, 0, 0, 0, 0, 0, 8, 8, 65, 0, 0, 0, 0, 0, 0,
044: 0, 0, 65, 0, 0, 0, 0, 0, 0, 0, 0, 64, 0, 0, 0, 0, 0, 0, 0,
045: 0, 64, 0, 0, 0, 0, 0, 0, 0, 0, 1, -13, 15, 0, 0, 0, 0, -56,
046: 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 72, 0, 0, 0, 2, 0, 85, 63,
047: 0, 0, 0, 0, 0, 0, -48, 63, 63, 0, 0, 0, 0, 0, 0, -48, 63,
048: 63, 0, 0, 0, 0, 0, 0, -48, 63, 63, 0, 0, 0, 0, 0, 0, -48,
049: 63, 0, 0, 0, 0, 0, 0, -16, 63, 0, 0, 0, 0, 0, 0, -16, 63,
050: 1, 0, 1, 0, 0, 0, 0, 0, 7, 0, 0, 0, 0, 0, 0, 0, 1, -1, 15,
051: 7, 0, 0, 0, 0, 101, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 28, 0,
052: 0, 0, 1, 0, 84, 24, 0, 0, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0,
053: 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0,
054: 0, 0, 0, 0, 0, 0, -125, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
055: 10, 0, 0, 0, 2, 0, 85, 5, 0, 0, 0, 72, 0, 0, 0, 0, 0, 0, 0,
056: 0, 0, 0 };
057: public static final byte[] data_b = new byte[] { 70, 0, 0, 0, -1,
058: -1, -1, -1, 3, 0, 0, 0, 68, 0, 0, 0, 0, 0, 0, 68, 0, 0, 0,
059: 0, 0, 0, 0, 2, 0, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
060: -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0,
061: 0, 0, 0, -1, -1, -1, -1, 0, 0, 0, 0, -1, -1, -1, -1, 0, 0,
062: 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0,
063: 0, 0, 0, 0, 0, 0, 0, 0, 104, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0,
064: 0, 32, 0, 0, 0, 1, 0, 84, 24, 0, 0, 0, 8, 0, 0, 0, 0, 0, 0,
065: 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0,
066: 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, -110, 0, 0, 0, 0, 0,
067: 0, 0, 0, 0, 0, 0, -124, 0, 0, 0, 2, 0, 85, 63, 0, 0, 0, 0,
068: 0, 0, 33, 64, 63, 0, 0, 0, 0, 0, 0, 38, 64, 63, 0, 0, 0, 0,
069: 0, 0, -64, 63, 63, 0, 0, 0, 0, 0, 0, -64, -65, 73, 0, 0, 0,
070: 0, 0, 0, -16, 63, 73, 0, 0, 0, 0, 0,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0,
088: 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
089: 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
090: 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
091: 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
092: 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
093:
094: public void testChunkHeaderA() throws Exception {
095: ChunkFactory cf = new ChunkFactory(11);
096: ChunkHeader h = ChunkHeader.createChunkHeader(11, data_a, 0);
097:
098: assertTrue(h instanceof ChunkHeaderV11);
099: ChunkHeaderV11 header = (ChunkHeaderV11) h;
100:
101: assertEquals(70, header.getType());
102: assertEquals(-1, header.getId());
103: assertEquals(2, header.getUnknown1());
104: assertEquals(68, header.getLength());
105: assertEquals(0, header.getUnknown2());
106: assertEquals(0, header.getUnknown3());
107:
108: assertTrue(header.hasTrailer());
109: assertTrue(header.hasSeparator());
110: }
111:
112: public void testChunkHeaderB() throws Exception {
113: ChunkFactory cf = new ChunkFactory(11);
114: ChunkHeader h = ChunkHeader.createChunkHeader(11, data_b, 0);
115:
116: assertTrue(h instanceof ChunkHeaderV11);
117: ChunkHeaderV11 header = (ChunkHeaderV11) h;
118:
119: assertEquals(70, header.getType());
120: assertEquals(-1, header.getId());
121: assertEquals(3, header.getUnknown1());
122: assertEquals(68, header.getLength());
123: assertEquals(0, header.getUnknown2());
124: assertEquals(0, header.getUnknown3());
125:
126: assertTrue(header.hasTrailer());
127: assertTrue(header.hasSeparator());
128: }
129:
130: public void testOneChunk() throws Exception {
131: ChunkFactory cf = new ChunkFactory(11);
132: cf.createChunk(data_a, 0);
133: cf.createChunk(data_b, 0);
134:
135: Chunk chunk = cf.createChunk(data_a, 0);
136: assertNotNull(chunk.getHeader());
137: assertNotNull(chunk.getTrailer());
138: assertNotNull(chunk.getSeparator());
139:
140: // Should be 19 + length + 8 + 4 big
141: assertEquals(68, chunk.getHeader().getLength());
142: assertEquals(68 + 19 + 8 + 4, chunk.getOnDiskSize());
143:
144: // Type is 70, or 0x46
145: assertEquals(70, chunk.getHeader().getType());
146: assertEquals(0x46, chunk.getHeader().getType());
147:
148: // Should have two virtual chunk commands, a
149: // 10 (page sheet) and an 18
150: assertEquals(2, chunk.commandDefinitions.length);
151: assertEquals(0, chunk.getCommands().length);
152:
153: assertEquals(10, chunk.commandDefinitions[0].getType());
154: assertEquals(0, chunk.commandDefinitions[0].getOffset());
155: assertEquals("PageSheet", chunk.commandDefinitions[0].getName());
156:
157: assertEquals(18, chunk.commandDefinitions[1].getType());
158: assertEquals(0, chunk.commandDefinitions[1].getOffset());
159: assertEquals("0", chunk.commandDefinitions[1].getName());
160: }
161:
162: public void testAnotherChunk() throws Exception {
163: ChunkFactory cf = new ChunkFactory(11);
164:
165: // Go for the 2nd chunk in the stream
166: int offset = 0;
167: Chunk chunk = cf.createChunk(data_b, offset);
168: offset += chunk.getOnDiskSize();
169: chunk = cf.createChunk(data_b, offset);
170:
171: assertNotNull(chunk.getHeader());
172: assertNotNull(chunk.getTrailer());
173: assertNotNull(chunk.getSeparator());
174:
175: // Should be 19 + length + 8 + 4 big
176: assertEquals(32, chunk.getHeader().getLength());
177: assertEquals(32 + 19 + 8 + 4, chunk.getOnDiskSize());
178:
179: // Type is 104, or 0x68
180: assertEquals(104, chunk.getHeader().getType());
181: assertEquals(0x68, chunk.getHeader().getType());
182:
183: // Should have two virtual chunk commands, a
184: // 10 (Unknown) and an 18
185: assertEquals(2, chunk.commandDefinitions.length);
186: assertEquals(0, chunk.getCommands().length);
187:
188: assertEquals(10, chunk.commandDefinitions[0].getType());
189: assertEquals(0, chunk.commandDefinitions[0].getOffset());
190: assertEquals("Unknown 0x68", chunk.commandDefinitions[0]
191: .getName());
192:
193: assertEquals(18, chunk.commandDefinitions[1].getType());
194: assertEquals(0, chunk.commandDefinitions[1].getOffset());
195: assertEquals("0", chunk.commandDefinitions[1].getName());
196: }
197:
198: public void testManyChunks() throws Exception {
199: ChunkFactory cf = new ChunkFactory(11);
200: Chunk chunk;
201: int offset = 0;
202:
203: chunk = cf.createChunk(data_a, offset);
204: assertNotNull(chunk.getHeader());
205: assertNotNull(chunk.getTrailer());
206: assertNotNull(chunk.getSeparator());
207: offset += chunk.getOnDiskSize();
208:
209: chunk = cf.createChunk(data_a, offset);
210: assertNotNull(chunk.getHeader());
211: assertNotNull(chunk.getTrailer());
212: assertNotNull(chunk.getSeparator());
213: offset += chunk.getOnDiskSize();
214:
215: // Has a separator but no trailer
216: chunk = cf.createChunk(data_a, offset);
217: assertNotNull(chunk.getHeader());
218: assertNull(chunk.getTrailer());
219: assertNotNull(chunk.getSeparator());
220: offset += chunk.getOnDiskSize();
221:
222: chunk = cf.createChunk(data_a, offset);
223: assertNotNull(chunk.getHeader());
224: assertNull(chunk.getTrailer());
225: assertNotNull(chunk.getSeparator());
226: offset += chunk.getOnDiskSize();
227:
228: chunk = cf.createChunk(data_a, offset);
229: assertNotNull(chunk.getHeader());
230: assertNull(chunk.getTrailer());
231: assertNotNull(chunk.getSeparator());
232: offset += chunk.getOnDiskSize();
233: }
234: }
|