#63955 - HMEFContentsExtractor fails to extract content from winmail.dat

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1870692 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Andreas Beeker 2019-12-01 21:06:54 +00:00
parent 713a69afdb
commit 2eee474063
8 changed files with 827 additions and 799 deletions

View File

@ -26,8 +26,8 @@ import java.util.Map;
* http://msdn.microsoft.com/en-us/library/microsoft.exchange.data.contenttypes.tnef.tnefpropertytype%28v=EXCHG.140%29.aspx * http://msdn.microsoft.com/en-us/library/microsoft.exchange.data.contenttypes.tnef.tnefpropertytype%28v=EXCHG.140%29.aspx
*/ */
public final class Types { public final class Types {
private static Map<Integer, MAPIType> builtInTypes = new HashMap<>(); private static final Map<Integer, MAPIType> builtInTypes = new HashMap<>();
private static Map<Integer, MAPIType> customTypes = new HashMap<>(); private static final Map<Integer, MAPIType> customTypes = new HashMap<>();
/** Unspecified */ /** Unspecified */
public static final MAPIType UNSPECIFIED = new MAPIType(0x0000, public static final MAPIType UNSPECIFIED = new MAPIType(0x0000,
@ -119,7 +119,7 @@ public final class Types {
* Is this type a fixed-length type, or a variable-length one? * Is this type a fixed-length type, or a variable-length one?
*/ */
public boolean isFixedLength() { public boolean isFixedLength() {
return (length != -1) && (length <= 8); return ((length != -1) && (length <= 8)) || (id == Types.CLS_ID.id);
} }
public int getId() { public int getId() {
@ -132,8 +132,7 @@ public final class Types {
@Override @Override
public String toString() { public String toString() {
return id + " / 0x" + asFileEnding() + " - " + name + " @ " return id + " / 0x" + asFileEnding() + " - " + name + " @ " + length;
+ length;
} }
/** /**

View File

@ -1,48 +0,0 @@
/* ====================================================================
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==================================================================== */
package org.apache.poi.hmef;
import java.io.IOException;
import java.io.InputStream;
import junit.framework.TestCase;
import org.apache.poi.POIDataSamples;
import org.apache.poi.util.IOUtils;
public abstract class HMEFTest extends TestCase {
protected static final POIDataSamples _samples = POIDataSamples.getHMEFInstance();
protected void assertContents(String filename, Attachment attachment)
throws IOException {
assertEquals(filename, attachment.getLongFilename());
assertContents(filename, attachment.getContents());
}
protected void assertContents(String filename, byte[] actual)
throws IOException {
try (InputStream stream = _samples.openResourceAsStream("quick-contents/" + filename)) {
byte[] expected = IOUtils.toByteArray(stream);
assertEquals(expected.length, actual.length);
for (int i = 0; i < expected.length; i++) {
assertEquals("Byte " + i + " wrong", expected[i], actual[i]);
}
}
}
}

View File

@ -17,20 +17,27 @@
package org.apache.poi.hmef; package org.apache.poi.hmef;
import static org.junit.Assert.assertEquals;
import java.io.IOException;
import java.text.DateFormat; import java.text.DateFormat;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.List; import java.util.List;
import java.util.Locale; import java.util.Locale;
import org.apache.poi.POIDataSamples;
import org.apache.poi.util.LocaleUtil; import org.apache.poi.util.LocaleUtil;
import org.junit.Before;
import org.junit.Test;
public final class TestAttachments {
protected static final POIDataSamples _samples = POIDataSamples.getHMEFInstance();
public final class TestAttachments extends HMEFTest {
private HMEFMessage quick; private HMEFMessage quick;
@Override @Before
protected void setUp() throws Exception { public void setUp() throws Exception {
super.setUp();
quick = new HMEFMessage( quick = new HMEFMessage(
_samples.openResourceAsStream("quick-winmail.dat") _samples.openResourceAsStream("quick-winmail.dat")
); );
@ -39,6 +46,7 @@ public final class TestAttachments extends HMEFTest {
/** /**
* Check the file is as we expect * Check the file is as we expect
*/ */
@Test
public void testCounts() throws Exception { public void testCounts() throws Exception {
// Should have 5 attachments // Should have 5 attachments
assertEquals(5, quick.getAttachments().size()); assertEquals(5, quick.getAttachments().size());
@ -47,6 +55,7 @@ public final class TestAttachments extends HMEFTest {
/** /**
* Check some basic bits about the attachments * Check some basic bits about the attachments
*/ */
@Test
public void testBasicAttachments() throws Exception { public void testBasicAttachments() throws Exception {
List<Attachment> attachments = quick.getAttachments(); List<Attachment> attachments = quick.getAttachments();
@ -80,6 +89,7 @@ public final class TestAttachments extends HMEFTest {
* Query the attachments in detail, and check we see * Query the attachments in detail, and check we see
* the right values for key things * the right values for key things
*/ */
@Test
public void testAttachmentDetails() throws Exception { public void testAttachmentDetails() throws Exception {
List<Attachment> attachments = quick.getAttachments(); List<Attachment> attachments = quick.getAttachments();
@ -88,11 +98,11 @@ public final class TestAttachments extends HMEFTest {
fmt.setTimeZone(LocaleUtil.TIMEZONE_UTC); fmt.setTimeZone(LocaleUtil.TIMEZONE_UTC);
// They should all have the same date on them // They should all have the same date on them
assertEquals("28-Apr-2010 12:40:56", fmt.format( attachments.get(0).getModifiedDate())); assertEquals("28-Apr-2010 12:40:56", fmt.format(attachments.get(0).getModifiedDate()));
assertEquals("28-Apr-2010 12:40:56", fmt.format( attachments.get(1).getModifiedDate())); assertEquals("28-Apr-2010 12:40:56", fmt.format(attachments.get(1).getModifiedDate()));
assertEquals("28-Apr-2010 12:40:56", fmt.format( attachments.get(2).getModifiedDate())); assertEquals("28-Apr-2010 12:40:56", fmt.format(attachments.get(2).getModifiedDate()));
assertEquals("28-Apr-2010 12:40:56", fmt.format( attachments.get(3).getModifiedDate())); assertEquals("28-Apr-2010 12:40:56", fmt.format(attachments.get(3).getModifiedDate()));
assertEquals("28-Apr-2010 12:40:56", fmt.format( attachments.get(4).getModifiedDate())); assertEquals("28-Apr-2010 12:40:56", fmt.format(attachments.get(4).getModifiedDate()));
// They should all have a 3512 byte metafile rendered version // They should all have a 3512 byte metafile rendered version
assertEquals(3512, attachments.get(0).getRenderedMetaFile().length); assertEquals(3512, attachments.get(0).getRenderedMetaFile().length);
@ -105,6 +115,7 @@ public final class TestAttachments extends HMEFTest {
/** /**
* Ensure the attachment contents come back as they should do * Ensure the attachment contents come back as they should do
*/ */
@Test
public void testAttachmentContents() throws Exception { public void testAttachmentContents() throws Exception {
List<Attachment> attachments = quick.getAttachments(); List<Attachment> attachments = quick.getAttachments();
@ -114,4 +125,9 @@ public final class TestAttachments extends HMEFTest {
assertContents("quick.txt", attachments.get(3)); assertContents("quick.txt", attachments.get(3));
assertContents("quick.xml", attachments.get(4)); assertContents("quick.xml", attachments.get(4));
} }
static void assertContents(String filename, Attachment attachment) throws IOException {
assertEquals(filename, attachment.getLongFilename());
TestHMEFMessage.assertContents(filename, attachment.getContents());
}
} }

View File

@ -17,11 +17,13 @@
package org.apache.poi.hmef; package org.apache.poi.hmef;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.InputStream; import java.io.InputStream;
import junit.framework.TestCase;
import org.apache.poi.POIDataSamples; import org.apache.poi.POIDataSamples;
import org.apache.poi.hmef.attribute.MAPIAttribute; import org.apache.poi.hmef.attribute.MAPIAttribute;
import org.apache.poi.hmef.attribute.MAPIRtfAttribute; import org.apache.poi.hmef.attribute.MAPIRtfAttribute;
@ -29,8 +31,9 @@ import org.apache.poi.hsmf.datatypes.MAPIProperty;
import org.apache.poi.util.IOUtils; import org.apache.poi.util.IOUtils;
import org.apache.poi.util.LittleEndian; import org.apache.poi.util.LittleEndian;
import org.apache.poi.util.StringUtil; import org.apache.poi.util.StringUtil;
import org.junit.Test;
public final class TestCompressedRTF extends TestCase { public final class TestCompressedRTF {
private static final POIDataSamples _samples = POIDataSamples.getHMEFInstance(); private static final POIDataSamples _samples = POIDataSamples.getHMEFInstance();
private static final String block1 = "{\\rtf1\\adeflang102"; private static final String block1 = "{\\rtf1\\adeflang102";
@ -40,22 +43,24 @@ public final class TestCompressedRTF extends TestCase {
* Check that things are as we expected. If this fails, * Check that things are as we expected. If this fails,
* then decoding has no hope... * then decoding has no hope...
*/ */
@Test
public void testQuickBasics() throws Exception { public void testQuickBasics() throws Exception {
HMEFMessage msg = new HMEFMessage( HMEFMessage msg;
_samples.openResourceAsStream("quick-winmail.dat") try (InputStream is = _samples.openResourceAsStream("quick-winmail.dat")) {
); msg = new HMEFMessage(is);
}
MAPIAttribute rtfAttr = msg.getMessageMAPIAttribute(MAPIProperty.RTF_COMPRESSED); MAPIAttribute rtfAttr = msg.getMessageMAPIAttribute(MAPIProperty.RTF_COMPRESSED);
assertNotNull(rtfAttr); assertNotNull(rtfAttr);
assertTrue(rtfAttr instanceof MAPIRtfAttribute); assertTrue(rtfAttr instanceof MAPIRtfAttribute);
// Check the start of the compressed version // Check the start of the compressed version
byte[] data = ((MAPIRtfAttribute)rtfAttr).getRawData(); byte[] data = ((MAPIRtfAttribute) rtfAttr).getRawData();
assertEquals(5907, data.length); assertEquals(5907, data.length);
// First 16 bytes is header stuff // First 16 bytes is header stuff
// Check it has the length + compressed marker // Check it has the length + compressed marker
assertEquals(5907-4, LittleEndian.getShort(data)); assertEquals(5907 - 4, LittleEndian.getShort(data));
assertEquals( assertEquals(
"LZFu", "LZFu",
StringUtil.getFromCompressedUnicode(data, 8, 4) StringUtil.getFromCompressedUnicode(data, 8, 4)
@ -63,49 +68,51 @@ public final class TestCompressedRTF extends TestCase {
// Now Look at the code // Now Look at the code
assertEquals((byte)0x07, data[16+0]); // Flag: cccUUUUU assertEquals((byte) 0x07, data[16 + 0]); // Flag: cccUUUUU
assertEquals((byte)0x00, data[16+1]); // c1a: offset 0 / 0x000 assertEquals((byte) 0x00, data[16 + 1]); // c1a: offset 0 / 0x000
assertEquals((byte)0x06, data[16+2]); // c1b: length 6+2 -> {\rtf1\a assertEquals((byte) 0x06, data[16 + 2]); // c1b: length 6+2 -> {\rtf1\a
assertEquals((byte)0x01, data[16+3]); // c2a: offset 16 / 0x010 assertEquals((byte) 0x01, data[16 + 3]); // c2a: offset 16 / 0x010
assertEquals((byte)0x01, data[16+4]); // c2b: length 1+2 -> def assertEquals((byte) 0x01, data[16 + 4]); // c2b: length 1+2 -> def
assertEquals((byte)0x0b, data[16+5]); // c3a: offset 182 / 0xb6 assertEquals((byte) 0x0b, data[16 + 5]); // c3a: offset 182 / 0xb6
assertEquals((byte)0x60, data[16+6]); // c3b: length 0+2 -> la assertEquals((byte) 0x60, data[16 + 6]); // c3b: length 0+2 -> la
assertEquals((byte)0x6e, data[16+7]); // n assertEquals((byte) 0x6e, data[16 + 7]); // n
assertEquals((byte)0x67, data[16+8]); // g assertEquals((byte) 0x67, data[16 + 8]); // g
assertEquals((byte)0x31, data[16+9]); // 1 assertEquals((byte) 0x31, data[16 + 9]); // 1
assertEquals((byte)0x30, data[16+10]); // 0 assertEquals((byte) 0x30, data[16 + 10]); // 0
assertEquals((byte)0x32, data[16+11]); // 2 assertEquals((byte) 0x32, data[16 + 11]); // 2
assertEquals((byte)0x66, data[16+12]); // Flag: UccUUccU assertEquals((byte) 0x66, data[16 + 12]); // Flag: UccUUccU
assertEquals((byte)0x35, data[16+13]); // 5 assertEquals((byte) 0x35, data[16 + 13]); // 5
assertEquals((byte)0x00, data[16+14]); // c2a: offset 6 / 0x006 assertEquals((byte) 0x00, data[16 + 14]); // c2a: offset 6 / 0x006
assertEquals((byte)0x64, data[16+15]); // c2b: length 4+2 -> \ansi\a assertEquals((byte) 0x64, data[16 + 15]); // c2b: length 4+2 -> \ansi\a
assertEquals((byte)0x00, data[16+16]); // c3a: offset 7 / 0x007 assertEquals((byte) 0x00, data[16 + 16]); // c3a: offset 7 / 0x007
assertEquals((byte)0x72, data[16+17]); // c3b: length 2+2 -> nsi assertEquals((byte) 0x72, data[16 + 17]); // c3b: length 2+2 -> nsi
assertEquals((byte)0x63, data[16+18]); // c assertEquals((byte) 0x63, data[16 + 18]); // c
assertEquals((byte)0x70, data[16+19]); // p assertEquals((byte) 0x70, data[16 + 19]); // p
assertEquals((byte)0x0d, data[16+20]); // c6a: offset 221 / 0x0dd assertEquals((byte) 0x0d, data[16 + 20]); // c6a: offset 221 / 0x0dd
assertEquals((byte)0xd0, data[16+21]); // c6b: length 0+2 -> g1 assertEquals((byte) 0xd0, data[16 + 21]); // c6b: length 0+2 -> g1
assertEquals((byte)0x0e, data[16+22]); // c7a: offset 224 / 0x0e0 assertEquals((byte) 0x0e, data[16 + 22]); // c7a: offset 224 / 0x0e0
assertEquals((byte)0x00, data[16+23]); // c7b: length 0+2 -> 25 assertEquals((byte) 0x00, data[16 + 23]); // c7b: length 0+2 -> 25
assertEquals((byte)0x32, data[16+24]); // 2 assertEquals((byte) 0x32, data[16 + 24]); // 2
} }
/** /**
* Check that we can decode the first 8 codes * Check that we can decode the first 8 codes
* (1 flag byte + 8 codes) * (1 flag byte + 8 codes)
*/ */
@Test
public void testFirstBlock() throws Exception { public void testFirstBlock() throws Exception {
HMEFMessage msg = new HMEFMessage( HMEFMessage msg;
_samples.openResourceAsStream("quick-winmail.dat") try (InputStream is = _samples.openResourceAsStream("quick-winmail.dat")) {
); msg = new HMEFMessage(is);
}
MAPIAttribute attr = msg.getMessageMAPIAttribute(MAPIProperty.RTF_COMPRESSED); MAPIAttribute attr = msg.getMessageMAPIAttribute(MAPIProperty.RTF_COMPRESSED);
assertNotNull(attr); assertNotNull(attr);
MAPIRtfAttribute rtfAttr = (MAPIRtfAttribute)attr; MAPIRtfAttribute rtfAttr = (MAPIRtfAttribute) attr;
// Truncate to header + flag + data for flag // Truncate to header + flag + data for flag
byte[] data = new byte[16+12]; byte[] data = new byte[16 + 12];
System.arraycopy(rtfAttr.getRawData(), 0, data, 0, data.length); System.arraycopy(rtfAttr.getRawData(), 0, data, 0, data.length);
// Decompress it // Decompress it
@ -122,17 +129,19 @@ public final class TestCompressedRTF extends TestCase {
* Check that we can decode the first 16 codes * Check that we can decode the first 16 codes
* (flag + 8 codes, flag + 8 codes) * (flag + 8 codes, flag + 8 codes)
*/ */
@Test
public void testFirstTwoBlocks() throws Exception { public void testFirstTwoBlocks() throws Exception {
HMEFMessage msg = new HMEFMessage( HMEFMessage msg;
_samples.openResourceAsStream("quick-winmail.dat") try (InputStream is = _samples.openResourceAsStream("quick-winmail.dat")) {
); msg = new HMEFMessage(is);
}
MAPIAttribute attr = msg.getMessageMAPIAttribute(MAPIProperty.RTF_COMPRESSED); MAPIAttribute attr = msg.getMessageMAPIAttribute(MAPIProperty.RTF_COMPRESSED);
assertNotNull(attr); assertNotNull(attr);
MAPIRtfAttribute rtfAttr = (MAPIRtfAttribute)attr; MAPIRtfAttribute rtfAttr = (MAPIRtfAttribute) attr;
// Truncate to header + flag + data for flag + flag + data // Truncate to header + flag + data for flag + flag + data
byte[] data = new byte[16+12+13]; byte[] data = new byte[16 + 12 + 13];
System.arraycopy(rtfAttr.getRawData(), 0, data, 0, data.length); System.arraycopy(rtfAttr.getRawData(), 0, data, 0, data.length);
// Decompress it // Decompress it
@ -149,18 +158,21 @@ public final class TestCompressedRTF extends TestCase {
* Check that we can correctly decode the whole file * Check that we can correctly decode the whole file
* TODO Fix what looks like a padding issue * TODO Fix what looks like a padding issue
*/ */
@Test
public void testFull() throws Exception { public void testFull() throws Exception {
HMEFMessage msg = new HMEFMessage( final HMEFMessage msg;
_samples.openResourceAsStream("quick-winmail.dat") try (InputStream is = _samples.openResourceAsStream("quick-winmail.dat")) {
); msg = new HMEFMessage(is);
}
MAPIAttribute attr = msg.getMessageMAPIAttribute(MAPIProperty.RTF_COMPRESSED); MAPIAttribute attr = msg.getMessageMAPIAttribute(MAPIProperty.RTF_COMPRESSED);
assertNotNull(attr); assertNotNull(attr);
MAPIRtfAttribute rtfAttr = (MAPIRtfAttribute)attr; MAPIRtfAttribute rtfAttr = (MAPIRtfAttribute) attr;
InputStream stream = _samples.openResourceAsStream("quick-contents/message.rtf"); final byte[] expected;
try { try (InputStream stream = _samples.openResourceAsStream("quick-contents/message.rtf")) {
byte[] expected = IOUtils.toByteArray(stream); expected = IOUtils.toByteArray(stream);
}
CompressedRTF comp = new CompressedRTF(); CompressedRTF comp = new CompressedRTF();
byte[] data = rtfAttr.getRawData(); byte[] data = rtfAttr.getRawData();
@ -171,14 +183,14 @@ public final class TestCompressedRTF extends TestCase {
assertEquals(expected.length, comp.getDeCompressedSize()); assertEquals(expected.length, comp.getDeCompressedSize());
// Will have been padded though // Will have been padded though
assertEquals(expected.length+2, decomp.length); assertEquals(expected.length + 2, decomp.length);
byte[] tmp = new byte[expected.length]; byte[] tmp = new byte[expected.length];
System.arraycopy(decomp, 0, tmp, 0, tmp.length); System.arraycopy(decomp, 0, tmp, 0, tmp.length);
decomp = tmp; decomp = tmp;
// By byte // By byte
assertEquals(expected.length, decomp.length); assertEquals(expected.length, decomp.length);
for(int i=0; i<expected.length; i++) { for (int i = 0; i < expected.length; i++) {
assertEquals(expected[i], decomp[i]); assertEquals(expected[i], decomp[i]);
} }
@ -187,8 +199,5 @@ public final class TestCompressedRTF extends TestCase {
String decompStr = rtfAttr.getDataString(); String decompStr = rtfAttr.getDataString();
assertEquals(expString.length(), decompStr.length()); assertEquals(expString.length(), decompStr.length());
assertEquals(expString, decompStr); assertEquals(expString, decompStr);
} finally {
stream.close();
}
} }
} }

View File

@ -17,19 +17,32 @@
package org.apache.poi.hmef; package org.apache.poi.hmef;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import org.apache.poi.POIDataSamples;
import org.apache.poi.hmef.attribute.MAPIAttribute; import org.apache.poi.hmef.attribute.MAPIAttribute;
import org.apache.poi.hmef.attribute.MAPIRtfAttribute; import org.apache.poi.hmef.attribute.MAPIRtfAttribute;
import org.apache.poi.hmef.attribute.MAPIStringAttribute; import org.apache.poi.hmef.attribute.MAPIStringAttribute;
import org.apache.poi.hmef.attribute.TNEFProperty; import org.apache.poi.hmef.attribute.TNEFProperty;
import org.apache.poi.hsmf.datatypes.MAPIProperty; import org.apache.poi.hsmf.datatypes.MAPIProperty;
import org.apache.poi.hsmf.datatypes.Types; import org.apache.poi.hsmf.datatypes.Types;
import org.apache.poi.util.IOUtils;
import org.apache.poi.util.LittleEndian; import org.apache.poi.util.LittleEndian;
import org.junit.Test;
public final class TestHMEFMessage extends HMEFTest { public final class TestHMEFMessage {
protected static final POIDataSamples _samples = POIDataSamples.getHMEFInstance();
@Test
public void testOpen() throws Exception { public void testOpen() throws Exception {
HMEFMessage msg = new HMEFMessage( HMEFMessage msg = new HMEFMessage(
_samples.openResourceAsStream("quick-winmail.dat") _samples.openResourceAsStream("quick-winmail.dat")
@ -38,6 +51,7 @@ public final class TestHMEFMessage extends HMEFTest {
assertNotNull(msg); assertNotNull(msg);
} }
@Test
public void testCounts() throws Exception { public void testCounts() throws Exception {
HMEFMessage msg = new HMEFMessage( HMEFMessage msg = new HMEFMessage(
_samples.openResourceAsStream("quick-winmail.dat") _samples.openResourceAsStream("quick-winmail.dat")
@ -55,7 +69,7 @@ public final class TestHMEFMessage extends HMEFTest {
// Each attachment should have 6 normal attributes, and // Each attachment should have 6 normal attributes, and
// 20 or so MAPI ones // 20 or so MAPI ones
for(Attachment attach : msg.getAttachments()) { for (Attachment attach : msg.getAttachments()) {
int attrCount = attach.getAttributes().size(); int attrCount = attach.getAttributes().size();
int mapiAttrCount = attach.getMAPIAttributes().size(); int mapiAttrCount = attach.getMAPIAttributes().size();
@ -65,6 +79,7 @@ public final class TestHMEFMessage extends HMEFTest {
} }
} }
@Test
public void testBasicMessageAttributes() throws Exception { public void testBasicMessageAttributes() throws Exception {
HMEFMessage msg = new HMEFMessage( HMEFMessage msg = new HMEFMessage(
_samples.openResourceAsStream("quick-winmail.dat") _samples.openResourceAsStream("quick-winmail.dat")
@ -90,7 +105,7 @@ public final class TestHMEFMessage extends HMEFTest {
// Now check the details of one or two // Now check the details of one or two
assertEquals( assertEquals(
0x010000, 0x010000,
LittleEndian.getInt( msg.getMessageAttribute(TNEFProperty.ID_TNEFVERSION).getData() ) LittleEndian.getInt(msg.getMessageAttribute(TNEFProperty.ID_TNEFVERSION).getData())
); );
assertEquals( assertEquals(
"IPM.Microsoft Mail.Note\0", "IPM.Microsoft Mail.Note\0",
@ -98,6 +113,7 @@ public final class TestHMEFMessage extends HMEFTest {
); );
} }
@Test
public void testBasicMessageMAPIAttributes() throws Exception { public void testBasicMessageMAPIAttributes() throws Exception {
HMEFMessage msg = new HMEFMessage( HMEFMessage msg = new HMEFMessage(
_samples.openResourceAsStream("quick-winmail.dat") _samples.openResourceAsStream("quick-winmail.dat")
@ -111,6 +127,7 @@ public final class TestHMEFMessage extends HMEFTest {
* Checks that the compressed RTF message contents * Checks that the compressed RTF message contents
* can be correctly extracted * can be correctly extracted
*/ */
@Test
public void testMessageContents() throws Exception { public void testMessageContents() throws Exception {
HMEFMessage msg = new HMEFMessage( HMEFMessage msg = new HMEFMessage(
_samples.openResourceAsStream("quick-winmail.dat") _samples.openResourceAsStream("quick-winmail.dat")
@ -131,6 +148,7 @@ public final class TestHMEFMessage extends HMEFTest {
assertNull(msg.getMessageMAPIAttribute(MAPIProperty.AB_DEFAULT_DIR)); assertNull(msg.getMessageMAPIAttribute(MAPIProperty.AB_DEFAULT_DIR));
} }
@Test
public void testMessageSample1() throws Exception { public void testMessageSample1() throws Exception {
HMEFMessage msg = new HMEFMessage( HMEFMessage msg = new HMEFMessage(
_samples.openResourceAsStream("winmail-sample1.dat")); _samples.openResourceAsStream("winmail-sample1.dat"));
@ -153,8 +171,9 @@ public final class TestHMEFMessage extends HMEFTest {
assertNotNull(msg.getBody()); assertNotNull(msg.getBody());
} }
@Test
public void testInvalidMessage() throws Exception { public void testInvalidMessage() throws Exception {
InputStream str = new ByteArrayInputStream(new byte[] {0, 0, 0, 0}); InputStream str = new ByteArrayInputStream(new byte[]{0, 0, 0, 0});
try { try {
assertNotNull(new HMEFMessage(str)); assertNotNull(new HMEFMessage(str));
fail("Should catch an exception here"); fail("Should catch an exception here");
@ -163,7 +182,7 @@ public final class TestHMEFMessage extends HMEFTest {
} }
} }
@Test
public void testNoData() throws Exception { public void testNoData() throws Exception {
ByteArrayOutputStream out = new ByteArrayOutputStream(); ByteArrayOutputStream out = new ByteArrayOutputStream();
@ -180,6 +199,7 @@ public final class TestHMEFMessage extends HMEFTest {
assertNull(msg.getBody()); assertNull(msg.getBody());
} }
@Test
public void testInvalidLevel() throws Exception { public void testInvalidLevel() throws Exception {
ByteArrayOutputStream out = new ByteArrayOutputStream(); ByteArrayOutputStream out = new ByteArrayOutputStream();
@ -202,6 +222,7 @@ public final class TestHMEFMessage extends HMEFTest {
} }
} }
@Test
public void testCustomProperty() throws Exception { public void testCustomProperty() throws Exception {
HMEFMessage msg = new HMEFMessage( HMEFMessage msg = new HMEFMessage(
_samples.openResourceAsStream("quick-winmail.dat") _samples.openResourceAsStream("quick-winmail.dat")
@ -226,8 +247,21 @@ public final class TestHMEFMessage extends HMEFTest {
assertEquals(MAPIStringAttribute.class, msg.getMessageMAPIAttribute(propE28).getClass()); assertEquals(MAPIStringAttribute.class, msg.getMessageMAPIAttribute(propE28).getClass());
assertEquals( assertEquals(
"Zimbra - Mark Rogers", "Zimbra - Mark Rogers",
((MAPIStringAttribute)msg.getMessageMAPIAttribute(propE28)).getDataString().substring(10) ((MAPIStringAttribute) msg.getMessageMAPIAttribute(propE28)).getDataString().substring(10)
); );
} }
static void assertContents(String filename, byte[] actual)
throws IOException {
try (InputStream stream = _samples.openResourceAsStream("quick-contents/" + filename)) {
byte[] expected = IOUtils.toByteArray(stream);
assertEquals(expected.length, actual.length);
for (int i = 0; i < expected.length; i++) {
assertEquals("Byte " + i + " wrong", expected[i], actual[i]);
}
}
}
} }

View File

@ -17,6 +17,9 @@
package org.apache.poi.hmef.attribute; package org.apache.poi.hmef.attribute;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.InputStream; import java.io.InputStream;
import java.text.DateFormat; import java.text.DateFormat;
@ -28,34 +31,31 @@ import org.apache.poi.hmef.HMEFMessage;
import org.apache.poi.hsmf.datatypes.MAPIProperty; import org.apache.poi.hsmf.datatypes.MAPIProperty;
import org.apache.poi.util.LittleEndian; import org.apache.poi.util.LittleEndian;
import org.apache.poi.util.LocaleUtil; import org.apache.poi.util.LocaleUtil;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import junit.framework.TestCase; public final class TestMAPIAttributes {
public final class TestMAPIAttributes extends TestCase {
private static final POIDataSamples _samples = POIDataSamples.getHMEFInstance(); private static final POIDataSamples _samples = POIDataSamples.getHMEFInstance();
private HMEFMessage quick; private HMEFMessage quick;
private InputStream stream; private InputStream stream;
@Override @Before
protected void setUp() throws Exception { public void setUp() throws Exception {
super.setUp();
stream = _samples.openResourceAsStream("quick-winmail.dat"); stream = _samples.openResourceAsStream("quick-winmail.dat");
quick = new HMEFMessage(stream); quick = new HMEFMessage(stream);
} }
@After
@Override public void tearDown() throws Exception {
protected void tearDown() throws Exception {
stream.close(); stream.close();
super.tearDown();
} }
/** /**
* Test counts * Test counts
*/ */
@Test
public void testCounts() throws Exception { public void testCounts() throws Exception {
// Message should have 54 // Message should have 54
assertEquals(54, quick.getMessageMAPIAttributes().size()); assertEquals(54, quick.getMessageMAPIAttributes().size());
@ -67,11 +67,12 @@ protected void tearDown() throws Exception {
/** /**
* Test various general ones * Test various general ones
*/ */
@Test
public void testBasics() throws Exception { public void testBasics() throws Exception {
// Try constructing two attributes // Try constructing two attributes
byte[] data = new byte[] { byte[] data = new byte[]{
// Level one, id 36867, type 6 // Level one, id 36867, type 6
0x01, 0x03, (byte)0x90, 0x06, 0x00, 0x01, 0x03, (byte) 0x90, 0x06, 0x00,
// Length 24 // Length 24
0x24, 0x00, 0x00, 0x00, 0x24, 0x00, 0x00, 0x00,
@ -87,8 +88,8 @@ protected void tearDown() throws Exception {
0x1E, 0x00, 0x70, 0x00, 0x1E, 0x00, 0x70, 0x00,
0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
0x04, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00,
(byte)'T', (byte)'e', (byte) 'T', (byte) 'e',
(byte)'s', (byte)'t', (byte) 's', (byte) 't',
// Checksum (may be wrong...) // Checksum (may be wrong...)
0x01, 0x00 0x01, 0x00
}; };
@ -103,7 +104,7 @@ protected void tearDown() throws Exception {
assertNotNull(attr); assertNotNull(attr);
assertEquals(TNEFMAPIAttribute.class, attr.getClass()); assertEquals(TNEFMAPIAttribute.class, attr.getClass());
TNEFMAPIAttribute mapi = (TNEFMAPIAttribute)attr; TNEFMAPIAttribute mapi = (TNEFMAPIAttribute) attr;
assertEquals(3, mapi.getMAPIAttributes().size()); assertEquals(3, mapi.getMAPIAttributes().size());
assertEquals( assertEquals(
@ -128,13 +129,14 @@ protected void tearDown() throws Exception {
); );
assertEquals( assertEquals(
"Test", "Test",
((MAPIStringAttribute)mapi.getMAPIAttributes().get(2)).getDataString() ((MAPIStringAttribute) mapi.getMAPIAttributes().get(2)).getDataString()
); );
} }
/** /**
* Test String, Date and RTF ones * Test String, Date and RTF ones
*/ */
@Test
public void testTyped() throws Exception { public void testTyped() throws Exception {
MAPIAttribute attr; MAPIAttribute attr;
@ -144,14 +146,14 @@ protected void tearDown() throws Exception {
assertNotNull(attr); assertNotNull(attr);
assertEquals(MAPIStringAttribute.class, attr.getClass()); assertEquals(MAPIStringAttribute.class, attr.getClass());
MAPIStringAttribute str = (MAPIStringAttribute)attr; MAPIStringAttribute str = (MAPIStringAttribute) attr;
assertEquals("This is a test message", str.getDataString()); assertEquals("This is a test message", str.getDataString());
// Date // Date
// (Unknown/Custom) 32955 -> Wed Dec 15 2010 @ 14:46:31 UTC // (Unknown/Custom) 32955 -> Wed Dec 15 2010 @ 14:46:31 UTC
attr = null; attr = null;
for(MAPIAttribute a : quick.getMessageMAPIAttributes()) { for (MAPIAttribute a : quick.getMessageMAPIAttributes()) {
if(a.getProperty().id == 32955) { if (a.getProperty().id == 32955) {
attr = a; attr = a;
break; break;
} }
@ -159,7 +161,7 @@ protected void tearDown() throws Exception {
assertNotNull(attr); assertNotNull(attr);
assertEquals(MAPIDateAttribute.class, attr.getClass()); assertEquals(MAPIDateAttribute.class, attr.getClass());
MAPIDateAttribute date = (MAPIDateAttribute)attr; MAPIDateAttribute date = (MAPIDateAttribute) attr;
DateFormat fmt = new SimpleDateFormat("dd-MMM-yyyy HH:mm:ss", Locale.UK); DateFormat fmt = new SimpleDateFormat("dd-MMM-yyyy HH:mm:ss", Locale.UK);
fmt.setTimeZone(LocaleUtil.TIMEZONE_UTC); fmt.setTimeZone(LocaleUtil.TIMEZONE_UTC);
assertEquals("15-Dec-2010 14:46:31", fmt.format(date.getDate())); assertEquals("15-Dec-2010 14:46:31", fmt.format(date.getDate()));
@ -170,13 +172,14 @@ protected void tearDown() throws Exception {
assertNotNull(attr); assertNotNull(attr);
assertEquals(MAPIRtfAttribute.class, attr.getClass()); assertEquals(MAPIRtfAttribute.class, attr.getClass());
MAPIRtfAttribute rtf = (MAPIRtfAttribute)attr; MAPIRtfAttribute rtf = (MAPIRtfAttribute) attr;
assertEquals("{\\rtf1", rtf.getDataString().substring(0, 6)); assertEquals("{\\rtf1", rtf.getDataString().substring(0, 6));
} }
/** /**
* Check common ones via helper accessors * Check common ones via helper accessors
*/ */
@Test
public void testCommon() throws Exception { public void testCommon() throws Exception {
assertEquals("This is a test message", quick.getSubject()); assertEquals("This is a test message", quick.getSubject());

View File

@ -17,7 +17,11 @@
package org.apache.poi.hmef.attribute; package org.apache.poi.hmef.attribute;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.text.DateFormat; import java.text.DateFormat;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.Locale; import java.util.Locale;
@ -28,31 +32,30 @@ import org.apache.poi.hmef.HMEFMessage;
import org.apache.poi.hsmf.datatypes.MAPIProperty; import org.apache.poi.hsmf.datatypes.MAPIProperty;
import org.apache.poi.util.LittleEndian; import org.apache.poi.util.LittleEndian;
import org.apache.poi.util.LocaleUtil; import org.apache.poi.util.LocaleUtil;
import org.junit.Before;
import org.junit.Test;
import junit.framework.TestCase; public final class TestTNEFAttributes {
public final class TestTNEFAttributes extends TestCase {
private static final POIDataSamples _samples = POIDataSamples.getHMEFInstance(); private static final POIDataSamples _samples = POIDataSamples.getHMEFInstance();
private HMEFMessage quick; private HMEFMessage quick;
@Override @Before
protected void setUp() throws Exception { public void setUp() throws Exception {
super.setUp(); try (InputStream is = _samples.openResourceAsStream("quick-winmail.dat")) {
quick = new HMEFMessage(is);
quick = new HMEFMessage( }
_samples.openResourceAsStream("quick-winmail.dat")
);
} }
/** /**
* Test counts * Test counts
*/ */
@Test
public void testCounts() throws Exception { public void testCounts() throws Exception {
// The message should have 4 attributes // The message should have 4 attributes
assertEquals(4, quick.getMessageAttributes().size()); assertEquals(4, quick.getMessageAttributes().size());
// Each attachment should have 6 attributes // Each attachment should have 6 attributes
for(Attachment attach : quick.getAttachments()) { for (Attachment attach : quick.getAttachments()) {
assertEquals(6, attach.getAttributes().size()); assertEquals(6, attach.getAttributes().size());
} }
} }
@ -60,11 +63,12 @@ public final class TestTNEFAttributes extends TestCase {
/** /**
* Test the basics * Test the basics
*/ */
@Test
public void testBasics() throws Exception { public void testBasics() throws Exception {
// An int one // An int one
assertEquals( assertEquals(
0x010000, 0x010000,
LittleEndian.getInt( quick.getMessageAttribute(TNEFProperty.ID_TNEFVERSION).getData() ) LittleEndian.getInt(quick.getMessageAttribute(TNEFProperty.ID_TNEFVERSION).getData())
); );
// Claims not to be text, but really is // Claims not to be text, but really is
@ -74,9 +78,9 @@ public final class TestTNEFAttributes extends TestCase {
); );
// Try constructing two attributes // Try constructing two attributes
byte[] data = new byte[] { byte[] data = new byte[]{
// Level one, id 36870, type 8 // Level one, id 36870, type 8
0x01, 0x06, (byte)0x90, 0x08, 0x00, 0x01, 0x06, (byte) 0x90, 0x08, 0x00,
// Length 4 // Length 4
0x04, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00,
// Data // Data
@ -85,14 +89,14 @@ public final class TestTNEFAttributes extends TestCase {
0x01, 0x00, 0x01, 0x00,
// level one, id 36871, type 6 // level one, id 36871, type 6
0x01, 0x07, (byte)0x90, 0x06, 0x00, 0x01, 0x07, (byte) 0x90, 0x06, 0x00,
// Length 8 // Length 8
0x08, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00,
// Data // Data
(byte)0xe4, 0x04, 0x00, 0x00, (byte) 0xe4, 0x04, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
// Checksum // Checksum
(byte)0xe8, 0x00 (byte) 0xe8, 0x00
}; };
ByteArrayInputStream bais = new ByteArrayInputStream(data); ByteArrayInputStream bais = new ByteArrayInputStream(data);
@ -111,17 +115,18 @@ public final class TestTNEFAttributes extends TestCase {
assertEquals(TNEFProperty.ID_TNEFVERSION, attr1.getProperty()); assertEquals(TNEFProperty.ID_TNEFVERSION, attr1.getProperty());
assertEquals(8, attr1.getType()); assertEquals(8, attr1.getType());
assertEquals(4, attr1.getData().length); assertEquals(4, attr1.getData().length);
assertEquals(0x010000, LittleEndian.getInt( attr1.getData() )); assertEquals(0x010000, LittleEndian.getInt(attr1.getData()));
assertEquals(TNEFProperty.ID_OEMCODEPAGE, attr2.getProperty()); assertEquals(TNEFProperty.ID_OEMCODEPAGE, attr2.getProperty());
assertEquals(6, attr2.getType()); assertEquals(6, attr2.getType());
assertEquals(8, attr2.getData().length); assertEquals(8, attr2.getData().length);
assertEquals(0x04e4, LittleEndian.getInt( attr2.getData() )); assertEquals(0x04e4, LittleEndian.getInt(attr2.getData()));
} }
/** /**
* Test string based ones * Test string based ones
*/ */
@Test
public void testString() throws Exception { public void testString() throws Exception {
TNEFAttribute attr = quick.getAttachments().get(0).getAttribute( TNEFAttribute attr = quick.getAttachments().get(0).getAttribute(
TNEFProperty.ID_ATTACHTITLE TNEFProperty.ID_ATTACHTITLE
@ -133,13 +138,14 @@ public final class TestTNEFAttributes extends TestCase {
assertEquals("quick.doc\u0000", new String(attr.getData(), "ASCII")); assertEquals("quick.doc\u0000", new String(attr.getData(), "ASCII"));
// But when we ask for the string, that is sorted for us // But when we ask for the string, that is sorted for us
TNEFStringAttribute str = (TNEFStringAttribute)attr; TNEFStringAttribute str = (TNEFStringAttribute) attr;
assertEquals("quick.doc", str.getString()); assertEquals("quick.doc", str.getString());
} }
/** /**
* Test date based ones * Test date based ones
*/ */
@Test
public void testDate() throws Exception { public void testDate() throws Exception {
TNEFAttribute attr = quick.getAttachments().get(0).getAttribute( TNEFAttribute attr = quick.getAttachments().get(0).getAttribute(
TNEFProperty.ID_ATTACHMODIFYDATE TNEFProperty.ID_ATTACHMODIFYDATE
@ -159,7 +165,7 @@ public final class TestTNEFAttributes extends TestCase {
// Ask for it as a Java date, and have it converted // Ask for it as a Java date, and have it converted
// Pick a predictable format + location + timezone // Pick a predictable format + location + timezone
TNEFDateAttribute date = (TNEFDateAttribute)attr; TNEFDateAttribute date = (TNEFDateAttribute) attr;
DateFormat fmt = new SimpleDateFormat("dd-MMM-yyyy HH:mm:ss", Locale.UK); DateFormat fmt = new SimpleDateFormat("dd-MMM-yyyy HH:mm:ss", Locale.UK);
fmt.setTimeZone(LocaleUtil.TIMEZONE_UTC); fmt.setTimeZone(LocaleUtil.TIMEZONE_UTC);
assertEquals("28-Apr-2010 12:40:56", fmt.format(date.getDate())); assertEquals("28-Apr-2010 12:40:56", fmt.format(date.getDate()));
@ -168,6 +174,7 @@ public final class TestTNEFAttributes extends TestCase {
/** /**
* Test a bit of mapi * Test a bit of mapi
*/ */
@Test
public void testMAPI() throws Exception { public void testMAPI() throws Exception {
// Message MAPI // Message MAPI
TNEFAttribute attr = quick.getMessageAttribute( TNEFAttribute attr = quick.getMessageAttribute(
@ -176,7 +183,7 @@ public final class TestTNEFAttributes extends TestCase {
assertNotNull(attr); assertNotNull(attr);
assertEquals(TNEFMAPIAttribute.class, attr.getClass()); assertEquals(TNEFMAPIAttribute.class, attr.getClass());
TNEFMAPIAttribute mapi = (TNEFMAPIAttribute)attr; TNEFMAPIAttribute mapi = (TNEFMAPIAttribute) attr;
assertEquals(54, mapi.getMAPIAttributes().size()); assertEquals(54, mapi.getMAPIAttributes().size());
assertEquals( assertEquals(
MAPIProperty.ALTERNATE_RECIPIENT_ALLOWED, MAPIProperty.ALTERNATE_RECIPIENT_ALLOWED,
@ -191,7 +198,7 @@ public final class TestTNEFAttributes extends TestCase {
assertNotNull(attr); assertNotNull(attr);
assertEquals(TNEFMAPIAttribute.class, attr.getClass()); assertEquals(TNEFMAPIAttribute.class, attr.getClass());
mapi = (TNEFMAPIAttribute)attr; mapi = (TNEFMAPIAttribute) attr;
assertEquals(22, mapi.getMAPIAttributes().size()); assertEquals(22, mapi.getMAPIAttributes().size());
assertEquals( assertEquals(
MAPIProperty.ATTACH_SIZE, MAPIProperty.ATTACH_SIZE,
@ -202,6 +209,7 @@ public final class TestTNEFAttributes extends TestCase {
/** /**
* Test common ones via helpers * Test common ones via helpers
*/ */
@Test
public void testCommon() throws Exception { public void testCommon() throws Exception {
assertEquals("This is a test message", quick.getSubject()); assertEquals("This is a test message", quick.getSubject());
assertEquals("quick.doc", quick.getAttachments().get(0).getFilename()); assertEquals("quick.doc", quick.getAttachments().get(0).getFilename());

View File

@ -20,31 +20,38 @@
==================================================================== */ ==================================================================== */
package org.apache.poi.hmef.dev; package org.apache.poi.hmef.dev;
import org.apache.poi.POIDataSamples;
import org.junit.Test;
import java.io.File; import java.io.File;
import java.io.PrintStream;
import org.apache.poi.POIDataSamples;
import org.apache.poi.util.NullOutputStream;
import org.junit.Test;
public class TestHMEFDumper { public class TestHMEFDumper {
@Test(expected = IllegalArgumentException.class) @Test(expected = IllegalArgumentException.class)
public void noArguments() throws Exception { public void noArguments() throws Exception {
HMEFDumper.main(new String[] {}); doMain();
} }
@Test @Test
public void main() throws Exception { public void main() throws Exception {
File file = POIDataSamples.getHMEFInstance().getFile("quick-winmail.dat"); File file = POIDataSamples.getHMEFInstance().getFile("quick-winmail.dat");
HMEFDumper.main(new String[] { doMain(file.getAbsolutePath());
file.getAbsolutePath()
});
} }
@Test @Test
public void mainFull() throws Exception { public void mainFull() throws Exception {
File file = POIDataSamples.getHMEFInstance().getFile("quick-winmail.dat"); File file = POIDataSamples.getHMEFInstance().getFile("quick-winmail.dat");
HMEFDumper.main(new String[] { doMain("--full", file.getAbsolutePath());
"--full", }
file.getAbsolutePath()
}); private static void doMain(String... args) throws Exception {
PrintStream ps = System.out;
try {
System.setOut(new PrintStream(new NullOutputStream(), true, "UTF-8"));
HMEFDumper.main(args);
} finally {
System.setOut(ps);
}
} }
} }