You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

TestTNEFAttributes.java 8.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. /* ====================================================================
  2. Licensed to the Apache Software Foundation (ASF) under one or more
  3. contributor license agreements. See the NOTICE file distributed with
  4. this work for additional information regarding copyright ownership.
  5. The ASF licenses this file to You under the Apache License, Version 2.0
  6. (the "License"); you may not use this file except in compliance with
  7. the License. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. ==================================================================== */
  15. package org.apache.poi.hmef.attribute;
  16. import static org.junit.jupiter.api.Assertions.assertEquals;
  17. import static org.junit.jupiter.api.Assertions.assertNotNull;
  18. import static org.junit.jupiter.api.Assertions.assertTrue;
  19. import java.io.ByteArrayInputStream;
  20. import java.io.IOException;
  21. import java.io.InputStream;
  22. import java.nio.charset.StandardCharsets;
  23. import java.text.DateFormat;
  24. import java.text.SimpleDateFormat;
  25. import java.util.Locale;
  26. import org.apache.poi.POIDataSamples;
  27. import org.apache.poi.hmef.Attachment;
  28. import org.apache.poi.hmef.HMEFMessage;
  29. import org.apache.poi.hsmf.datatypes.MAPIProperty;
  30. import org.apache.poi.util.LittleEndian;
  31. import org.apache.poi.util.LocaleUtil;
  32. import org.junit.jupiter.api.BeforeEach;
  33. import org.junit.jupiter.api.Test;
  34. public final class TestTNEFAttributes {
  35. private static final POIDataSamples _samples = POIDataSamples.getHMEFInstance();
  36. private HMEFMessage quick;
  37. @BeforeEach
  38. void setUp() throws Exception {
  39. try (InputStream is = _samples.openResourceAsStream("quick-winmail.dat")) {
  40. quick = new HMEFMessage(is);
  41. }
  42. }
  43. /**
  44. * Test malformed TNEF is detected by MAPIAttribute and does not cause Out Of Memory error
  45. */
  46. @Test
  47. void testMalformedTNEF() throws Exception {
  48. try (InputStream is = _samples.openResourceAsStream("oom.tnef")) {
  49. quick = new HMEFMessage(is);
  50. } catch (Exception e) {
  51. assertTrue(e instanceof IOException);
  52. }
  53. }
  54. /**
  55. * Test counts
  56. */
  57. @Test
  58. void testCounts() throws Exception {
  59. // The message should have 4 attributes
  60. assertEquals(4, quick.getMessageAttributes().size());
  61. // Each attachment should have 6 attributes
  62. for (Attachment attach : quick.getAttachments()) {
  63. assertEquals(6, attach.getAttributes().size());
  64. }
  65. }
  66. /**
  67. * Test the basics
  68. */
  69. @Test
  70. void testBasics() throws Exception {
  71. // An int one
  72. assertEquals(
  73. 0x010000,
  74. LittleEndian.getInt(quick.getMessageAttribute(TNEFProperty.ID_TNEFVERSION).getData())
  75. );
  76. // Claims not to be text, but really is
  77. assertEquals(
  78. "IPM.Microsoft Mail.Note\0",
  79. new String(quick.getMessageAttribute(TNEFProperty.ID_MESSAGECLASS).getData(), StandardCharsets.US_ASCII)
  80. );
  81. // Try constructing two attributes
  82. byte[] data = new byte[]{
  83. // Level one, id 36870, type 8
  84. 0x01, 0x06, (byte) 0x90, 0x08, 0x00,
  85. // Length 4
  86. 0x04, 0x00, 0x00, 0x00,
  87. // Data
  88. 0x00, 0x00, 0x01, 0x00,
  89. // Checksum
  90. 0x01, 0x00,
  91. // level one, id 36871, type 6
  92. 0x01, 0x07, (byte) 0x90, 0x06, 0x00,
  93. // Length 8
  94. 0x08, 0x00, 0x00, 0x00,
  95. // Data
  96. (byte) 0xe4, 0x04, 0x00, 0x00,
  97. 0x00, 0x00, 0x00, 0x00,
  98. // Checksum
  99. (byte) 0xe8, 0x00
  100. };
  101. ByteArrayInputStream bais = new ByteArrayInputStream(data);
  102. // Create them
  103. int level = bais.read();
  104. assertEquals(1, level);
  105. TNEFAttribute attr1 = TNEFAttribute.create(bais);
  106. level = bais.read();
  107. assertEquals(1, level);
  108. TNEFAttribute attr2 = TNEFAttribute.create(bais);
  109. assertEquals(-1, bais.read());
  110. // Check them
  111. assertEquals(TNEFProperty.ID_TNEFVERSION, attr1.getProperty());
  112. assertEquals(8, attr1.getType());
  113. assertEquals(4, attr1.getData().length);
  114. assertEquals(0x010000, LittleEndian.getInt(attr1.getData()));
  115. assertEquals(TNEFProperty.ID_OEMCODEPAGE, attr2.getProperty());
  116. assertEquals(6, attr2.getType());
  117. assertEquals(8, attr2.getData().length);
  118. assertEquals(0x04e4, LittleEndian.getInt(attr2.getData()));
  119. }
  120. /**
  121. * Test string based ones
  122. */
  123. @Test
  124. void testString() throws Exception {
  125. TNEFAttribute attr = quick.getAttachments().get(0).getAttribute(
  126. TNEFProperty.ID_ATTACHTITLE
  127. );
  128. assertNotNull(attr);
  129. assertEquals(TNEFStringAttribute.class, attr.getClass());
  130. // It is a null terminated string
  131. assertEquals("quick.doc\u0000", new String(attr.getData(), StandardCharsets.US_ASCII));
  132. // But when we ask for the string, that is sorted for us
  133. TNEFStringAttribute str = (TNEFStringAttribute) attr;
  134. assertEquals("quick.doc", str.getString());
  135. }
  136. /**
  137. * Test date based ones
  138. */
  139. @Test
  140. void testDate() throws Exception {
  141. TNEFAttribute attr = quick.getAttachments().get(0).getAttribute(
  142. TNEFProperty.ID_ATTACHMODIFYDATE
  143. );
  144. assertNotNull(attr);
  145. assertEquals(TNEFDateAttribute.class, attr.getClass());
  146. // It is a series of date parts
  147. // Weds 28th April 2010 @ 12:40:56 UTC
  148. assertEquals(2010, LittleEndian.getUShort(attr.getData(), 0));
  149. assertEquals(04, LittleEndian.getUShort(attr.getData(), 2));
  150. assertEquals(28, LittleEndian.getUShort(attr.getData(), 4));
  151. assertEquals(12, LittleEndian.getUShort(attr.getData(), 6));
  152. assertEquals(40, LittleEndian.getUShort(attr.getData(), 8));
  153. assertEquals(56, LittleEndian.getUShort(attr.getData(), 10));
  154. assertEquals(3, LittleEndian.getUShort(attr.getData(), 12)); // Weds
  155. // Ask for it as a Java date, and have it converted
  156. // Pick a predictable format + location + timezone
  157. TNEFDateAttribute date = (TNEFDateAttribute) attr;
  158. DateFormat fmt = new SimpleDateFormat("dd-MMM-yyyy HH:mm:ss", Locale.UK);
  159. fmt.setTimeZone(LocaleUtil.TIMEZONE_UTC);
  160. assertEquals("28-Apr-2010 12:40:56", fmt.format(date.getDate()));
  161. }
  162. /**
  163. * Test a bit of mapi
  164. */
  165. @Test
  166. void testMAPI() throws Exception {
  167. // Message MAPI
  168. TNEFAttribute attr = quick.getMessageAttribute(
  169. TNEFProperty.ID_MAPIPROPERTIES
  170. );
  171. assertNotNull(attr);
  172. assertEquals(TNEFMAPIAttribute.class, attr.getClass());
  173. TNEFMAPIAttribute mapi = (TNEFMAPIAttribute) attr;
  174. assertEquals(54, mapi.getMAPIAttributes().size());
  175. assertEquals(
  176. MAPIProperty.ALTERNATE_RECIPIENT_ALLOWED,
  177. mapi.getMAPIAttributes().get(0).getProperty()
  178. );
  179. // Attribute MAPI
  180. attr = quick.getAttachments().get(0).getAttribute(
  181. TNEFProperty.ID_ATTACHMENT
  182. );
  183. assertNotNull(attr);
  184. assertEquals(TNEFMAPIAttribute.class, attr.getClass());
  185. mapi = (TNEFMAPIAttribute) attr;
  186. assertEquals(22, mapi.getMAPIAttributes().size());
  187. assertEquals(
  188. MAPIProperty.ATTACH_SIZE,
  189. mapi.getMAPIAttributes().get(0).getProperty()
  190. );
  191. }
  192. /**
  193. * Test common ones via helpers
  194. */
  195. @Test
  196. void testCommon() throws Exception {
  197. assertEquals("This is a test message", quick.getSubject());
  198. assertEquals("quick.doc", quick.getAttachments().get(0).getFilename());
  199. }
  200. }