1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
|
/* ====================================================================
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 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 java.nio.charset.StandardCharsets;
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 {
protected static final POIDataSamples _samples = POIDataSamples.getHMEFInstance();
@Test
public void testOpen() throws Exception {
HMEFMessage msg = new HMEFMessage(
_samples.openResourceAsStream("quick-winmail.dat")
);
assertNotNull(msg);
}
@Test
public void testCounts() throws Exception {
HMEFMessage msg = new HMEFMessage(
_samples.openResourceAsStream("quick-winmail.dat")
);
// Should have 4 attributes on the message
assertEquals(4, msg.getMessageAttributes().size());
// And should have 54 MAPI attributes on it
assertEquals(54, msg.getMessageMAPIAttributes().size());
// Should have 5 attachments
assertEquals(5, msg.getAttachments().size());
// Each attachment should have 6 normal attributes, and
// 20 or so MAPI ones
for (Attachment attach : msg.getAttachments()) {
int attrCount = attach.getAttributes().size();
int mapiAttrCount = attach.getMAPIAttributes().size();
assertEquals(6, attrCount);
assertTrue("Should be 20-25 mapi attributes, found " + mapiAttrCount, mapiAttrCount >= 20);
assertTrue("Should be 20-25 mapi attributes, found " + mapiAttrCount, mapiAttrCount <= 25);
}
}
@Test
public void testBasicMessageAttributes() throws Exception {
HMEFMessage msg = new HMEFMessage(
_samples.openResourceAsStream("quick-winmail.dat")
);
// Should have version, codepage, class and MAPI
assertEquals(4, msg.getMessageAttributes().size());
assertNotNull(msg.getMessageAttribute(TNEFProperty.ID_TNEFVERSION));
assertNotNull(msg.getMessageAttribute(TNEFProperty.ID_OEMCODEPAGE));
assertNotNull(msg.getMessageAttribute(TNEFProperty.ID_MESSAGECLASS));
assertNotNull(msg.getMessageAttribute(TNEFProperty.ID_MAPIPROPERTIES));
// Check the order
assertEquals(TNEFProperty.ID_TNEFVERSION, msg.getMessageAttributes().get(0).getProperty());
assertEquals(TNEFProperty.ID_OEMCODEPAGE, msg.getMessageAttributes().get(1).getProperty());
assertEquals(TNEFProperty.ID_MESSAGECLASS, msg.getMessageAttributes().get(2).getProperty());
assertEquals(TNEFProperty.ID_MAPIPROPERTIES, msg.getMessageAttributes().get(3).getProperty());
// Check some that aren't there
assertNull(msg.getMessageAttribute(TNEFProperty.ID_AIDOWNER));
assertNull(msg.getMessageAttribute(TNEFProperty.ID_ATTACHDATA));
// Now check the details of one or two
assertEquals(
0x010000,
LittleEndian.getInt(msg.getMessageAttribute(TNEFProperty.ID_TNEFVERSION).getData())
);
assertEquals(
"IPM.Microsoft Mail.Note\0",
new String(msg.getMessageAttribute(TNEFProperty.ID_MESSAGECLASS).getData(), StandardCharsets.US_ASCII)
);
}
@Test
public void testBasicMessageMAPIAttributes() throws Exception {
HMEFMessage msg = new HMEFMessage(
_samples.openResourceAsStream("quick-winmail.dat")
);
assertEquals("This is a test message", msg.getSubject());
assertEquals("{\\rtf1", msg.getBody().substring(0, 6));
}
/**
* 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")
);
// Firstly by byte
MAPIRtfAttribute rtf = (MAPIRtfAttribute)
msg.getMessageMAPIAttribute(MAPIProperty.RTF_COMPRESSED);
assertContents("message.rtf", rtf.getData());
// Then by String
String contents = msg.getBody();
// It's all low bytes
byte[] contentsBytes = contents.getBytes(StandardCharsets.US_ASCII);
assertContents("message.rtf", contentsBytes);
// try to get a message id that does not exist
assertNull(msg.getMessageMAPIAttribute(MAPIProperty.AB_DEFAULT_DIR));
}
@Test
public void testMessageSample1() throws Exception {
HMEFMessage msg = new HMEFMessage(
_samples.openResourceAsStream("winmail-sample1.dat"));
// Firstly by byte
MAPIRtfAttribute rtf = (MAPIRtfAttribute) msg
.getMessageMAPIAttribute(MAPIProperty.RTF_COMPRESSED);
// assertContents("message.rtf", rtf.getData());
assertNotNull(rtf);
// Then by String
String contents = msg.getBody();
//System.out.println(contents);
// It's all low bytes
byte[] contentsBytes = contents.getBytes(StandardCharsets.US_ASCII);
// assertContents("message.rtf", contentsBytes);
assertNotNull(contentsBytes);
assertNotNull(msg.getSubject());
assertNotNull(msg.getBody());
}
@Test
public void testInvalidMessage() throws Exception {
InputStream str = new ByteArrayInputStream(new byte[]{0, 0, 0, 0});
try {
assertNotNull(new HMEFMessage(str));
fail("Should catch an exception here");
} catch (IllegalArgumentException e) {
assertTrue(e.getMessage().contains("TNEF signature not detected in file, expected 574529400 but got 0"));
}
}
@Test
public void testNoData() throws Exception {
ByteArrayOutputStream out = new ByteArrayOutputStream();
// Header
LittleEndian.putInt(HMEFMessage.HEADER_SIGNATURE, out);
// field
LittleEndian.putUShort(0, out);
byte[] bytes = out.toByteArray();
InputStream str = new ByteArrayInputStream(bytes);
HMEFMessage msg = new HMEFMessage(str);
assertNull(msg.getSubject());
assertNull(msg.getBody());
}
@Test
public void testInvalidLevel() throws Exception {
ByteArrayOutputStream out = new ByteArrayOutputStream();
// Header
LittleEndian.putInt(HMEFMessage.HEADER_SIGNATURE, out);
// field
LittleEndian.putUShort(0, out);
// invalid level
LittleEndian.putUShort(90, out);
byte[] bytes = out.toByteArray();
InputStream str = new ByteArrayInputStream(bytes);
try {
assertNotNull(new HMEFMessage(str));
fail("Should catch an exception here");
} catch (IllegalStateException e) {
assertTrue(e.getMessage().contains("Unhandled level 90"));
}
}
@Test
public void testCustomProperty() throws Exception {
HMEFMessage msg = new HMEFMessage(
_samples.openResourceAsStream("quick-winmail.dat")
);
// Should have non-standard properties with IDs 0xE28 and 0xE29
boolean hasE28 = false;
boolean hasE29 = false;
for (MAPIAttribute attr : msg.getMessageMAPIAttributes()) {
if (attr.getProperty().id == 0xe28) hasE28 = true;
if (attr.getProperty().id == 0xe29) hasE29 = true;
}
assertTrue(hasE28);
assertTrue(hasE29);
// Ensure we can fetch those as custom ones
MAPIProperty propE28 = MAPIProperty.createCustom(0xe28, Types.ASCII_STRING, "Custom E28");
MAPIProperty propE29 = MAPIProperty.createCustom(0xe29, Types.ASCII_STRING, "Custom E29");
assertNotNull(msg.getMessageMAPIAttribute(propE28));
assertNotNull(msg.getMessageMAPIAttribute(propE29));
assertEquals(MAPIStringAttribute.class, msg.getMessageMAPIAttribute(propE28).getClass());
assertEquals(
"Zimbra - Mark Rogers",
((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]);
}
}
}
}
|