#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
*/
public final class Types {
private static Map<Integer, MAPIType> builtInTypes = new HashMap<>();
private static Map<Integer, MAPIType> customTypes = new HashMap<>();
private static final Map<Integer, MAPIType> builtInTypes = new HashMap<>();
private static final Map<Integer, MAPIType> customTypes = new HashMap<>();
/** Unspecified */
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?
*/
public boolean isFixedLength() {
return (length != -1) && (length <= 8);
return ((length != -1) && (length <= 8)) || (id == Types.CLS_ID.id);
}
public int getId() {
@ -132,8 +132,7 @@ public final class Types {
@Override
public String toString() {
return id + " / 0x" + asFileEnding() + " - " + name + " @ "
+ length;
return id + " / 0x" + asFileEnding() + " - " + name + " @ " + 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;
import static org.junit.Assert.assertEquals;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Locale;
import org.apache.poi.POIDataSamples;
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;
@Override
protected void setUp() throws Exception {
super.setUp();
@Before
public void setUp() throws Exception {
quick = new HMEFMessage(
_samples.openResourceAsStream("quick-winmail.dat")
);
@ -39,6 +46,7 @@ public final class TestAttachments extends HMEFTest {
/**
* Check the file is as we expect
*/
@Test
public void testCounts() throws Exception {
// Should have 5 attachments
assertEquals(5, quick.getAttachments().size());
@ -47,6 +55,7 @@ public final class TestAttachments extends HMEFTest {
/**
* Check some basic bits about the attachments
*/
@Test
public void testBasicAttachments() throws Exception {
List<Attachment> attachments = quick.getAttachments();
@ -80,6 +89,7 @@ public final class TestAttachments extends HMEFTest {
* Query the attachments in detail, and check we see
* the right values for key things
*/
@Test
public void testAttachmentDetails() throws Exception {
List<Attachment> attachments = quick.getAttachments();
@ -88,11 +98,11 @@ public final class TestAttachments extends HMEFTest {
fmt.setTimeZone(LocaleUtil.TIMEZONE_UTC);
// 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(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(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(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(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(4).getModifiedDate()));
// They should all have a 3512 byte metafile rendered version
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
*/
@Test
public void testAttachmentContents() throws Exception {
List<Attachment> attachments = quick.getAttachments();
@ -114,4 +125,9 @@ public final class TestAttachments extends HMEFTest {
assertContents("quick.txt", attachments.get(3));
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;
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.InputStream;
import junit.framework.TestCase;
import org.apache.poi.POIDataSamples;
import org.apache.poi.hmef.attribute.MAPIAttribute;
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.LittleEndian;
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 String block1 = "{\\rtf1\\adeflang102";
@ -40,22 +43,24 @@ public final class TestCompressedRTF extends TestCase {
* Check that things are as we expected. If this fails,
* then decoding has no hope...
*/
@Test
public void testQuickBasics() throws Exception {
HMEFMessage msg = new HMEFMessage(
_samples.openResourceAsStream("quick-winmail.dat")
);
HMEFMessage msg;
try (InputStream is = _samples.openResourceAsStream("quick-winmail.dat")) {
msg = new HMEFMessage(is);
}
MAPIAttribute rtfAttr = msg.getMessageMAPIAttribute(MAPIProperty.RTF_COMPRESSED);
assertNotNull(rtfAttr);
assertTrue(rtfAttr instanceof MAPIRtfAttribute);
// Check the start of the compressed version
byte[] data = ((MAPIRtfAttribute)rtfAttr).getRawData();
byte[] data = ((MAPIRtfAttribute) rtfAttr).getRawData();
assertEquals(5907, data.length);
// First 16 bytes is header stuff
// Check it has the length + compressed marker
assertEquals(5907-4, LittleEndian.getShort(data));
assertEquals(5907 - 4, LittleEndian.getShort(data));
assertEquals(
"LZFu",
StringUtil.getFromCompressedUnicode(data, 8, 4)
@ -63,49 +68,51 @@ public final class TestCompressedRTF extends TestCase {
// Now Look at the code
assertEquals((byte)0x07, data[16+0]); // Flag: cccUUUUU
assertEquals((byte)0x00, data[16+1]); // c1a: offset 0 / 0x000
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+4]); // c2b: length 1+2 -> def
assertEquals((byte)0x0b, data[16+5]); // c3a: offset 182 / 0xb6
assertEquals((byte)0x60, data[16+6]); // c3b: length 0+2 -> la
assertEquals((byte)0x6e, data[16+7]); // n
assertEquals((byte)0x67, data[16+8]); // g
assertEquals((byte)0x31, data[16+9]); // 1
assertEquals((byte)0x30, data[16+10]); // 0
assertEquals((byte)0x32, data[16+11]); // 2
assertEquals((byte) 0x07, data[16 + 0]); // Flag: cccUUUUU
assertEquals((byte) 0x00, data[16 + 1]); // c1a: offset 0 / 0x000
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 + 4]); // c2b: length 1+2 -> def
assertEquals((byte) 0x0b, data[16 + 5]); // c3a: offset 182 / 0xb6
assertEquals((byte) 0x60, data[16 + 6]); // c3b: length 0+2 -> la
assertEquals((byte) 0x6e, data[16 + 7]); // n
assertEquals((byte) 0x67, data[16 + 8]); // g
assertEquals((byte) 0x31, data[16 + 9]); // 1
assertEquals((byte) 0x30, data[16 + 10]); // 0
assertEquals((byte) 0x32, data[16 + 11]); // 2
assertEquals((byte)0x66, data[16+12]); // Flag: UccUUccU
assertEquals((byte)0x35, data[16+13]); // 5
assertEquals((byte)0x00, data[16+14]); // c2a: offset 6 / 0x006
assertEquals((byte)0x64, data[16+15]); // c2b: length 4+2 -> \ansi\a
assertEquals((byte)0x00, data[16+16]); // c3a: offset 7 / 0x007
assertEquals((byte)0x72, data[16+17]); // c3b: length 2+2 -> nsi
assertEquals((byte)0x63, data[16+18]); // c
assertEquals((byte)0x70, data[16+19]); // p
assertEquals((byte)0x0d, data[16+20]); // c6a: offset 221 / 0x0dd
assertEquals((byte)0xd0, data[16+21]); // c6b: length 0+2 -> g1
assertEquals((byte)0x0e, data[16+22]); // c7a: offset 224 / 0x0e0
assertEquals((byte)0x00, data[16+23]); // c7b: length 0+2 -> 25
assertEquals((byte)0x32, data[16+24]); // 2
assertEquals((byte) 0x66, data[16 + 12]); // Flag: UccUUccU
assertEquals((byte) 0x35, data[16 + 13]); // 5
assertEquals((byte) 0x00, data[16 + 14]); // c2a: offset 6 / 0x006
assertEquals((byte) 0x64, data[16 + 15]); // c2b: length 4+2 -> \ansi\a
assertEquals((byte) 0x00, data[16 + 16]); // c3a: offset 7 / 0x007
assertEquals((byte) 0x72, data[16 + 17]); // c3b: length 2+2 -> nsi
assertEquals((byte) 0x63, data[16 + 18]); // c
assertEquals((byte) 0x70, data[16 + 19]); // p
assertEquals((byte) 0x0d, data[16 + 20]); // c6a: offset 221 / 0x0dd
assertEquals((byte) 0xd0, data[16 + 21]); // c6b: length 0+2 -> g1
assertEquals((byte) 0x0e, data[16 + 22]); // c7a: offset 224 / 0x0e0
assertEquals((byte) 0x00, data[16 + 23]); // c7b: length 0+2 -> 25
assertEquals((byte) 0x32, data[16 + 24]); // 2
}
/**
* Check that we can decode the first 8 codes
* (1 flag byte + 8 codes)
*/
@Test
public void testFirstBlock() throws Exception {
HMEFMessage msg = new HMEFMessage(
_samples.openResourceAsStream("quick-winmail.dat")
);
HMEFMessage msg;
try (InputStream is = _samples.openResourceAsStream("quick-winmail.dat")) {
msg = new HMEFMessage(is);
}
MAPIAttribute attr = msg.getMessageMAPIAttribute(MAPIProperty.RTF_COMPRESSED);
assertNotNull(attr);
MAPIRtfAttribute rtfAttr = (MAPIRtfAttribute)attr;
MAPIRtfAttribute rtfAttr = (MAPIRtfAttribute) attr;
// 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);
// Decompress it
@ -122,17 +129,19 @@ public final class TestCompressedRTF extends TestCase {
* Check that we can decode the first 16 codes
* (flag + 8 codes, flag + 8 codes)
*/
@Test
public void testFirstTwoBlocks() throws Exception {
HMEFMessage msg = new HMEFMessage(
_samples.openResourceAsStream("quick-winmail.dat")
);
HMEFMessage msg;
try (InputStream is = _samples.openResourceAsStream("quick-winmail.dat")) {
msg = new HMEFMessage(is);
}
MAPIAttribute attr = msg.getMessageMAPIAttribute(MAPIProperty.RTF_COMPRESSED);
assertNotNull(attr);
MAPIRtfAttribute rtfAttr = (MAPIRtfAttribute)attr;
MAPIRtfAttribute rtfAttr = (MAPIRtfAttribute) attr;
// 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);
// Decompress it
@ -149,18 +158,21 @@ public final class TestCompressedRTF extends TestCase {
* Check that we can correctly decode the whole file
* TODO Fix what looks like a padding issue
*/
@Test
public void testFull() throws Exception {
HMEFMessage msg = new HMEFMessage(
_samples.openResourceAsStream("quick-winmail.dat")
);
final HMEFMessage msg;
try (InputStream is = _samples.openResourceAsStream("quick-winmail.dat")) {
msg = new HMEFMessage(is);
}
MAPIAttribute attr = msg.getMessageMAPIAttribute(MAPIProperty.RTF_COMPRESSED);
assertNotNull(attr);
MAPIRtfAttribute rtfAttr = (MAPIRtfAttribute)attr;
MAPIRtfAttribute rtfAttr = (MAPIRtfAttribute) attr;
InputStream stream = _samples.openResourceAsStream("quick-contents/message.rtf");
try {
byte[] expected = IOUtils.toByteArray(stream);
final byte[] expected;
try (InputStream stream = _samples.openResourceAsStream("quick-contents/message.rtf")) {
expected = IOUtils.toByteArray(stream);
}
CompressedRTF comp = new CompressedRTF();
byte[] data = rtfAttr.getRawData();
@ -171,14 +183,14 @@ public final class TestCompressedRTF extends TestCase {
assertEquals(expected.length, comp.getDeCompressedSize());
// Will have been padded though
assertEquals(expected.length+2, decomp.length);
assertEquals(expected.length + 2, decomp.length);
byte[] tmp = new byte[expected.length];
System.arraycopy(decomp, 0, tmp, 0, tmp.length);
decomp = tmp;
// By byte
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]);
}
@ -187,8 +199,5 @@ public final class TestCompressedRTF extends TestCase {
String decompStr = rtfAttr.getDataString();
assertEquals(expString.length(), decompStr.length());
assertEquals(expString, decompStr);
} finally {
stream.close();
}
}
}

View File

@ -17,19 +17,32 @@
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.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import org.apache.poi.POIDataSamples;
import org.apache.poi.hmef.attribute.MAPIAttribute;
import org.apache.poi.hmef.attribute.MAPIRtfAttribute;
import org.apache.poi.hmef.attribute.MAPIStringAttribute;
import org.apache.poi.hmef.attribute.TNEFProperty;
import org.apache.poi.hsmf.datatypes.MAPIProperty;
import org.apache.poi.hsmf.datatypes.Types;
import org.apache.poi.util.IOUtils;
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 {
HMEFMessage msg = new HMEFMessage(
_samples.openResourceAsStream("quick-winmail.dat")
@ -38,6 +51,7 @@ public final class TestHMEFMessage extends HMEFTest {
assertNotNull(msg);
}
@Test
public void testCounts() throws Exception {
HMEFMessage msg = new HMEFMessage(
_samples.openResourceAsStream("quick-winmail.dat")
@ -55,7 +69,7 @@ public final class TestHMEFMessage extends HMEFTest {
// Each attachment should have 6 normal attributes, and
// 20 or so MAPI ones
for(Attachment attach : msg.getAttachments()) {
for (Attachment attach : msg.getAttachments()) {
int attrCount = attach.getAttributes().size();
int mapiAttrCount = attach.getMAPIAttributes().size();
@ -65,6 +79,7 @@ public final class TestHMEFMessage extends HMEFTest {
}
}
@Test
public void testBasicMessageAttributes() throws Exception {
HMEFMessage msg = new HMEFMessage(
_samples.openResourceAsStream("quick-winmail.dat")
@ -90,7 +105,7 @@ public final class TestHMEFMessage extends HMEFTest {
// Now check the details of one or two
assertEquals(
0x010000,
LittleEndian.getInt( msg.getMessageAttribute(TNEFProperty.ID_TNEFVERSION).getData() )
LittleEndian.getInt(msg.getMessageAttribute(TNEFProperty.ID_TNEFVERSION).getData())
);
assertEquals(
"IPM.Microsoft Mail.Note\0",
@ -98,6 +113,7 @@ public final class TestHMEFMessage extends HMEFTest {
);
}
@Test
public void testBasicMessageMAPIAttributes() throws Exception {
HMEFMessage msg = new HMEFMessage(
_samples.openResourceAsStream("quick-winmail.dat")
@ -111,6 +127,7 @@ public final class TestHMEFMessage extends HMEFTest {
* Checks that the compressed RTF message contents
* can be correctly extracted
*/
@Test
public void testMessageContents() throws Exception {
HMEFMessage msg = new HMEFMessage(
_samples.openResourceAsStream("quick-winmail.dat")
@ -131,6 +148,7 @@ public final class TestHMEFMessage extends HMEFTest {
assertNull(msg.getMessageMAPIAttribute(MAPIProperty.AB_DEFAULT_DIR));
}
@Test
public void testMessageSample1() throws Exception {
HMEFMessage msg = new HMEFMessage(
_samples.openResourceAsStream("winmail-sample1.dat"));
@ -153,8 +171,9 @@ public final class TestHMEFMessage extends HMEFTest {
assertNotNull(msg.getBody());
}
@Test
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 {
assertNotNull(new HMEFMessage(str));
fail("Should catch an exception here");
@ -163,7 +182,7 @@ public final class TestHMEFMessage extends HMEFTest {
}
}
@Test
public void testNoData() throws Exception {
ByteArrayOutputStream out = new ByteArrayOutputStream();
@ -180,6 +199,7 @@ public final class TestHMEFMessage extends HMEFTest {
assertNull(msg.getBody());
}
@Test
public void testInvalidLevel() throws Exception {
ByteArrayOutputStream out = new ByteArrayOutputStream();
@ -202,6 +222,7 @@ public final class TestHMEFMessage extends HMEFTest {
}
}
@Test
public void testCustomProperty() throws Exception {
HMEFMessage msg = new HMEFMessage(
_samples.openResourceAsStream("quick-winmail.dat")
@ -226,8 +247,21 @@ public final class TestHMEFMessage extends HMEFTest {
assertEquals(MAPIStringAttribute.class, msg.getMessageMAPIAttribute(propE28).getClass());
assertEquals(
"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;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
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.util.LittleEndian;
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 extends TestCase {
public final class TestMAPIAttributes {
private static final POIDataSamples _samples = POIDataSamples.getHMEFInstance();
private HMEFMessage quick;
private InputStream stream;
@Override
protected void setUp() throws Exception {
super.setUp();
@Before
public void setUp() throws Exception {
stream = _samples.openResourceAsStream("quick-winmail.dat");
quick = new HMEFMessage(stream);
}
@Override
protected void tearDown() throws Exception {
@After
public void tearDown() throws Exception {
stream.close();
super.tearDown();
}
/**
/**
* Test counts
*/
@Test
public void testCounts() throws Exception {
// Message should have 54
assertEquals(54, quick.getMessageMAPIAttributes().size());
@ -67,11 +67,12 @@ protected void tearDown() throws Exception {
/**
* Test various general ones
*/
@Test
public void testBasics() throws Exception {
// Try constructing two attributes
byte[] data = new byte[] {
byte[] data = new byte[]{
// Level one, id 36867, type 6
0x01, 0x03, (byte)0x90, 0x06, 0x00,
0x01, 0x03, (byte) 0x90, 0x06, 0x00,
// Length 24
0x24, 0x00, 0x00, 0x00,
@ -87,8 +88,8 @@ protected void tearDown() throws Exception {
0x1E, 0x00, 0x70, 0x00,
0x01, 0x00, 0x00, 0x00,
0x04, 0x00, 0x00, 0x00,
(byte)'T', (byte)'e',
(byte)'s', (byte)'t',
(byte) 'T', (byte) 'e',
(byte) 's', (byte) 't',
// Checksum (may be wrong...)
0x01, 0x00
};
@ -103,7 +104,7 @@ protected void tearDown() throws Exception {
assertNotNull(attr);
assertEquals(TNEFMAPIAttribute.class, attr.getClass());
TNEFMAPIAttribute mapi = (TNEFMAPIAttribute)attr;
TNEFMAPIAttribute mapi = (TNEFMAPIAttribute) attr;
assertEquals(3, mapi.getMAPIAttributes().size());
assertEquals(
@ -128,13 +129,14 @@ protected void tearDown() throws Exception {
);
assertEquals(
"Test",
((MAPIStringAttribute)mapi.getMAPIAttributes().get(2)).getDataString()
((MAPIStringAttribute) mapi.getMAPIAttributes().get(2)).getDataString()
);
}
/**
* Test String, Date and RTF ones
*/
@Test
public void testTyped() throws Exception {
MAPIAttribute attr;
@ -144,14 +146,14 @@ protected void tearDown() throws Exception {
assertNotNull(attr);
assertEquals(MAPIStringAttribute.class, attr.getClass());
MAPIStringAttribute str = (MAPIStringAttribute)attr;
MAPIStringAttribute str = (MAPIStringAttribute) attr;
assertEquals("This is a test message", str.getDataString());
// Date
// (Unknown/Custom) 32955 -> Wed Dec 15 2010 @ 14:46:31 UTC
attr = null;
for(MAPIAttribute a : quick.getMessageMAPIAttributes()) {
if(a.getProperty().id == 32955) {
for (MAPIAttribute a : quick.getMessageMAPIAttributes()) {
if (a.getProperty().id == 32955) {
attr = a;
break;
}
@ -159,7 +161,7 @@ protected void tearDown() throws Exception {
assertNotNull(attr);
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);
fmt.setTimeZone(LocaleUtil.TIMEZONE_UTC);
assertEquals("15-Dec-2010 14:46:31", fmt.format(date.getDate()));
@ -170,13 +172,14 @@ protected void tearDown() throws Exception {
assertNotNull(attr);
assertEquals(MAPIRtfAttribute.class, attr.getClass());
MAPIRtfAttribute rtf = (MAPIRtfAttribute)attr;
MAPIRtfAttribute rtf = (MAPIRtfAttribute) attr;
assertEquals("{\\rtf1", rtf.getDataString().substring(0, 6));
}
/**
* Check common ones via helper accessors
*/
@Test
public void testCommon() throws Exception {
assertEquals("This is a test message", quick.getSubject());

View File

@ -17,7 +17,11 @@
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.InputStream;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
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.util.LittleEndian;
import org.apache.poi.util.LocaleUtil;
import org.junit.Before;
import org.junit.Test;
import junit.framework.TestCase;
public final class TestTNEFAttributes extends TestCase {
public final class TestTNEFAttributes {
private static final POIDataSamples _samples = POIDataSamples.getHMEFInstance();
private HMEFMessage quick;
@Override
protected void setUp() throws Exception {
super.setUp();
quick = new HMEFMessage(
_samples.openResourceAsStream("quick-winmail.dat")
);
@Before
public void setUp() throws Exception {
try (InputStream is = _samples.openResourceAsStream("quick-winmail.dat")) {
quick = new HMEFMessage(is);
}
}
/**
* Test counts
*/
@Test
public void testCounts() throws Exception {
// The message should have 4 attributes
assertEquals(4, quick.getMessageAttributes().size());
// Each attachment should have 6 attributes
for(Attachment attach : quick.getAttachments()) {
for (Attachment attach : quick.getAttachments()) {
assertEquals(6, attach.getAttributes().size());
}
}
@ -60,11 +63,12 @@ public final class TestTNEFAttributes extends TestCase {
/**
* Test the basics
*/
@Test
public void testBasics() throws Exception {
// An int one
assertEquals(
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
@ -74,9 +78,9 @@ public final class TestTNEFAttributes extends TestCase {
);
// Try constructing two attributes
byte[] data = new byte[] {
byte[] data = new byte[]{
// Level one, id 36870, type 8
0x01, 0x06, (byte)0x90, 0x08, 0x00,
0x01, 0x06, (byte) 0x90, 0x08, 0x00,
// Length 4
0x04, 0x00, 0x00, 0x00,
// Data
@ -85,14 +89,14 @@ public final class TestTNEFAttributes extends TestCase {
0x01, 0x00,
// level one, id 36871, type 6
0x01, 0x07, (byte)0x90, 0x06, 0x00,
0x01, 0x07, (byte) 0x90, 0x06, 0x00,
// Length 8
0x08, 0x00, 0x00, 0x00,
// Data
(byte)0xe4, 0x04, 0x00, 0x00,
(byte) 0xe4, 0x04, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
// Checksum
(byte)0xe8, 0x00
(byte) 0xe8, 0x00
};
ByteArrayInputStream bais = new ByteArrayInputStream(data);
@ -111,17 +115,18 @@ public final class TestTNEFAttributes extends TestCase {
assertEquals(TNEFProperty.ID_TNEFVERSION, attr1.getProperty());
assertEquals(8, attr1.getType());
assertEquals(4, attr1.getData().length);
assertEquals(0x010000, LittleEndian.getInt( attr1.getData() ));
assertEquals(0x010000, LittleEndian.getInt(attr1.getData()));
assertEquals(TNEFProperty.ID_OEMCODEPAGE, attr2.getProperty());
assertEquals(6, attr2.getType());
assertEquals(8, attr2.getData().length);
assertEquals(0x04e4, LittleEndian.getInt( attr2.getData() ));
assertEquals(0x04e4, LittleEndian.getInt(attr2.getData()));
}
/**
* Test string based ones
*/
@Test
public void testString() throws Exception {
TNEFAttribute attr = quick.getAttachments().get(0).getAttribute(
TNEFProperty.ID_ATTACHTITLE
@ -133,13 +138,14 @@ public final class TestTNEFAttributes extends TestCase {
assertEquals("quick.doc\u0000", new String(attr.getData(), "ASCII"));
// 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());
}
/**
* Test date based ones
*/
@Test
public void testDate() throws Exception {
TNEFAttribute attr = quick.getAttachments().get(0).getAttribute(
TNEFProperty.ID_ATTACHMODIFYDATE
@ -159,7 +165,7 @@ public final class TestTNEFAttributes extends TestCase {
// Ask for it as a Java date, and have it converted
// 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);
fmt.setTimeZone(LocaleUtil.TIMEZONE_UTC);
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
public void testMAPI() throws Exception {
// Message MAPI
TNEFAttribute attr = quick.getMessageAttribute(
@ -176,7 +183,7 @@ public final class TestTNEFAttributes extends TestCase {
assertNotNull(attr);
assertEquals(TNEFMAPIAttribute.class, attr.getClass());
TNEFMAPIAttribute mapi = (TNEFMAPIAttribute)attr;
TNEFMAPIAttribute mapi = (TNEFMAPIAttribute) attr;
assertEquals(54, mapi.getMAPIAttributes().size());
assertEquals(
MAPIProperty.ALTERNATE_RECIPIENT_ALLOWED,
@ -191,7 +198,7 @@ public final class TestTNEFAttributes extends TestCase {
assertNotNull(attr);
assertEquals(TNEFMAPIAttribute.class, attr.getClass());
mapi = (TNEFMAPIAttribute)attr;
mapi = (TNEFMAPIAttribute) attr;
assertEquals(22, mapi.getMAPIAttributes().size());
assertEquals(
MAPIProperty.ATTACH_SIZE,
@ -202,6 +209,7 @@ public final class TestTNEFAttributes extends TestCase {
/**
* Test common ones via helpers
*/
@Test
public void testCommon() throws Exception {
assertEquals("This is a test message", quick.getSubject());
assertEquals("quick.doc", quick.getAttachments().get(0).getFilename());

View File

@ -20,31 +20,38 @@
==================================================================== */
package org.apache.poi.hmef.dev;
import org.apache.poi.POIDataSamples;
import org.junit.Test;
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 {
@Test(expected = IllegalArgumentException.class)
public void noArguments() throws Exception {
HMEFDumper.main(new String[] {});
doMain();
}
@Test
public void main() throws Exception {
File file = POIDataSamples.getHMEFInstance().getFile("quick-winmail.dat");
HMEFDumper.main(new String[] {
file.getAbsolutePath()
});
doMain(file.getAbsolutePath());
}
@Test
public void mainFull() throws Exception {
File file = POIDataSamples.getHMEFInstance().getFile("quick-winmail.dat");
HMEFDumper.main(new String[] {
"--full",
file.getAbsolutePath()
});
doMain("--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);
}
}
}