Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

TestPOIFSChunkParser.java 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  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.hsmf.parsers;
  16. import java.io.FileInputStream;
  17. import java.io.FileNotFoundException;
  18. import java.io.IOException;
  19. import java.text.SimpleDateFormat;
  20. import java.util.Arrays;
  21. import java.util.Calendar;
  22. import org.apache.poi.hsmf.MAPIMessage;
  23. import org.apache.poi.hsmf.datatypes.AttachmentChunks;
  24. import org.apache.poi.hsmf.datatypes.ChunkGroup;
  25. import org.apache.poi.hsmf.datatypes.Chunks;
  26. import org.apache.poi.hsmf.datatypes.MAPIProperty;
  27. import org.apache.poi.hsmf.datatypes.NameIdChunks;
  28. import org.apache.poi.hsmf.datatypes.RecipientChunks;
  29. import org.apache.poi.hsmf.datatypes.RecipientChunks.RecipientChunksSorter;
  30. import org.apache.poi.hsmf.datatypes.StringChunk;
  31. import org.apache.poi.hsmf.datatypes.Types;
  32. import org.apache.poi.hsmf.exceptions.ChunkNotFoundException;
  33. import org.apache.poi.poifs.filesystem.POIFSFileSystem;
  34. import org.apache.poi.POIDataSamples;
  35. import junit.framework.TestCase;
  36. /**
  37. * Tests to verify that the chunk parser works properly
  38. */
  39. public final class TestPOIFSChunkParser extends TestCase {
  40. private POIDataSamples samples;
  41. public TestPOIFSChunkParser() throws IOException {
  42. samples = POIDataSamples.getHSMFInstance();
  43. }
  44. public void testFindsCore() throws IOException {
  45. POIFSFileSystem simple = new POIFSFileSystem(
  46. new FileInputStream(samples.getFile("quick.msg"))
  47. );
  48. // Check a few core things are present
  49. simple.getRoot().getEntry(
  50. (new StringChunk(MAPIProperty.SUBJECT.id, Types.ASCII_STRING)).getEntryName()
  51. );
  52. simple.getRoot().getEntry(
  53. (new StringChunk(MAPIProperty.SENDER_NAME.id, Types.ASCII_STRING)).getEntryName()
  54. );
  55. // Now load the file
  56. MAPIMessage msg = new MAPIMessage(simple);
  57. try {
  58. assertEquals("Kevin Roast", msg.getDisplayTo());
  59. assertEquals("Kevin Roast", msg.getDisplayFrom());
  60. assertEquals("Test the content transformer", msg.getSubject());
  61. } catch(ChunkNotFoundException e) {
  62. fail();
  63. }
  64. // Check date too
  65. try {
  66. SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  67. Calendar c = msg.getMessageDate();
  68. assertEquals( "2007-06-14 09:42:55", f.format(c.getTime()) );
  69. } catch(ChunkNotFoundException e) {
  70. fail();
  71. }
  72. }
  73. public void testFindsRecips() throws IOException, ChunkNotFoundException {
  74. POIFSFileSystem simple = new POIFSFileSystem(
  75. new FileInputStream(samples.getFile("quick.msg"))
  76. );
  77. simple.getRoot().getEntry("__recip_version1.0_#00000000");
  78. ChunkGroup[] groups = POIFSChunkParser.parse(simple.getRoot());
  79. assertEquals(3, groups.length);
  80. assertTrue(groups[0] instanceof Chunks);
  81. assertTrue(groups[1] instanceof RecipientChunks);
  82. assertTrue(groups[2] instanceof NameIdChunks);
  83. RecipientChunks recips = (RecipientChunks)groups[1];
  84. assertEquals("kevin.roast@alfresco.org", recips.recipientSMTPChunk.getValue());
  85. assertEquals("/O=HOSTEDSERVICE2/OU=FIRST ADMINISTRATIVE GROUP/CN=RECIPIENTS/CN=Kevin.roast@ben",
  86. recips.recipientEmailChunk.getValue());
  87. String search = new String(recips.recipientSearchChunk.getValue(), "ASCII");
  88. assertEquals("CN=KEVIN.ROAST@BEN\0", search.substring(search.length()-19));
  89. // Now via MAPIMessage
  90. MAPIMessage msg = new MAPIMessage(simple);
  91. assertNotNull(msg.getRecipientDetailsChunks());
  92. assertEquals(1, msg.getRecipientDetailsChunks().length);
  93. assertEquals("kevin.roast@alfresco.org", msg.getRecipientDetailsChunks()[0].recipientSMTPChunk.getValue());
  94. assertEquals("kevin.roast@alfresco.org", msg.getRecipientDetailsChunks()[0].getRecipientEmailAddress());
  95. assertEquals("Kevin Roast", msg.getRecipientDetailsChunks()[0].getRecipientName());
  96. assertEquals("kevin.roast@alfresco.org", msg.getRecipientEmailAddress());
  97. // Try both SMTP and EX files for recipient
  98. assertEquals("EX", msg.getRecipientDetailsChunks()[0].deliveryTypeChunk.getValue());
  99. assertEquals("kevin.roast@alfresco.org", msg.getRecipientDetailsChunks()[0].recipientSMTPChunk.getValue());
  100. assertEquals("/O=HOSTEDSERVICE2/OU=FIRST ADMINISTRATIVE GROUP/CN=RECIPIENTS/CN=Kevin.roast@ben",
  101. msg.getRecipientDetailsChunks()[0].recipientEmailChunk.getValue());
  102. // Now look at another message
  103. msg = new MAPIMessage(new POIFSFileSystem(
  104. new FileInputStream(samples.getFile("simple_test_msg.msg"))
  105. ));
  106. assertNotNull(msg.getRecipientDetailsChunks());
  107. assertEquals(1, msg.getRecipientDetailsChunks().length);
  108. assertEquals("SMTP", msg.getRecipientDetailsChunks()[0].deliveryTypeChunk.getValue());
  109. assertEquals(null, msg.getRecipientDetailsChunks()[0].recipientSMTPChunk);
  110. assertEquals(null, msg.getRecipientDetailsChunks()[0].recipientNameChunk);
  111. assertEquals("travis@overwrittenstack.com", msg.getRecipientDetailsChunks()[0].recipientEmailChunk.getValue());
  112. assertEquals("travis@overwrittenstack.com", msg.getRecipientEmailAddress());
  113. }
  114. public void testFindsMultipleRecipients() throws IOException, ChunkNotFoundException {
  115. POIFSFileSystem multiple = new POIFSFileSystem(
  116. new FileInputStream(samples.getFile("example_received_unicode.msg"))
  117. );
  118. multiple.getRoot().getEntry("__recip_version1.0_#00000000");
  119. multiple.getRoot().getEntry("__recip_version1.0_#00000001");
  120. multiple.getRoot().getEntry("__recip_version1.0_#00000002");
  121. multiple.getRoot().getEntry("__recip_version1.0_#00000003");
  122. multiple.getRoot().getEntry("__recip_version1.0_#00000004");
  123. multiple.getRoot().getEntry("__recip_version1.0_#00000005");
  124. ChunkGroup[] groups = POIFSChunkParser.parse(multiple.getRoot());
  125. assertEquals(9, groups.length);
  126. assertTrue(groups[0] instanceof Chunks);
  127. assertTrue(groups[1] instanceof RecipientChunks);
  128. assertTrue(groups[2] instanceof RecipientChunks);
  129. assertTrue(groups[3] instanceof RecipientChunks);
  130. assertTrue(groups[4] instanceof RecipientChunks);
  131. assertTrue(groups[5] instanceof AttachmentChunks);
  132. assertTrue(groups[6] instanceof RecipientChunks);
  133. assertTrue(groups[7] instanceof RecipientChunks);
  134. assertTrue(groups[8] instanceof NameIdChunks);
  135. // In FS order initially
  136. RecipientChunks[] chunks = new RecipientChunks[] {
  137. (RecipientChunks)groups[1],
  138. (RecipientChunks)groups[2],
  139. (RecipientChunks)groups[3],
  140. (RecipientChunks)groups[4],
  141. (RecipientChunks)groups[6],
  142. (RecipientChunks)groups[7],
  143. };
  144. assertEquals(6, chunks.length);
  145. assertEquals(0, chunks[0].recipientNumber);
  146. assertEquals(2, chunks[1].recipientNumber);
  147. assertEquals(4, chunks[2].recipientNumber);
  148. assertEquals(5, chunks[3].recipientNumber);
  149. assertEquals(3, chunks[4].recipientNumber);
  150. assertEquals(1, chunks[5].recipientNumber);
  151. // Check
  152. assertEquals("'Ashutosh Dandavate'", chunks[0].getRecipientName());
  153. assertEquals("ashutosh.dandavate@alfresco.com", chunks[0].getRecipientEmailAddress());
  154. assertEquals("'Mike Farman'", chunks[1].getRecipientName());
  155. assertEquals("mikef@alfresco.com", chunks[1].getRecipientEmailAddress());
  156. assertEquals("nick.burch@alfresco.com", chunks[2].getRecipientName());
  157. assertEquals("nick.burch@alfresco.com", chunks[2].getRecipientEmailAddress());
  158. assertEquals("'Roy Wetherall'", chunks[3].getRecipientName());
  159. assertEquals("roy.wetherall@alfresco.com", chunks[3].getRecipientEmailAddress());
  160. assertEquals("nickb@alfresco.com", chunks[4].getRecipientName());
  161. assertEquals("nickb@alfresco.com", chunks[4].getRecipientEmailAddress());
  162. assertEquals("'Paul Holmes-Higgin'", chunks[5].getRecipientName());
  163. assertEquals("paul.hh@alfresco.com", chunks[5].getRecipientEmailAddress());
  164. // Now sort, and re-check
  165. Arrays.sort(chunks, new RecipientChunksSorter());
  166. assertEquals("'Ashutosh Dandavate'", chunks[0].getRecipientName());
  167. assertEquals("ashutosh.dandavate@alfresco.com", chunks[0].getRecipientEmailAddress());
  168. assertEquals("'Paul Holmes-Higgin'", chunks[1].getRecipientName());
  169. assertEquals("paul.hh@alfresco.com", chunks[1].getRecipientEmailAddress());
  170. assertEquals("'Mike Farman'", chunks[2].getRecipientName());
  171. assertEquals("mikef@alfresco.com", chunks[2].getRecipientEmailAddress());
  172. assertEquals("nickb@alfresco.com", chunks[3].getRecipientName());
  173. assertEquals("nickb@alfresco.com", chunks[3].getRecipientEmailAddress());
  174. assertEquals("nick.burch@alfresco.com", chunks[4].getRecipientName());
  175. assertEquals("nick.burch@alfresco.com", chunks[4].getRecipientEmailAddress());
  176. assertEquals("'Roy Wetherall'", chunks[5].getRecipientName());
  177. assertEquals("roy.wetherall@alfresco.com", chunks[5].getRecipientEmailAddress());
  178. // Finally check on message
  179. MAPIMessage msg = new MAPIMessage(multiple);
  180. assertEquals(6, msg.getRecipientEmailAddressList().length);
  181. assertEquals(6, msg.getRecipientNamesList().length);
  182. assertEquals("'Ashutosh Dandavate'", msg.getRecipientNamesList()[0]);
  183. assertEquals("'Paul Holmes-Higgin'", msg.getRecipientNamesList()[1]);
  184. assertEquals("'Mike Farman'", msg.getRecipientNamesList()[2]);
  185. assertEquals("nickb@alfresco.com", msg.getRecipientNamesList()[3]);
  186. assertEquals("nick.burch@alfresco.com", msg.getRecipientNamesList()[4]);
  187. assertEquals("'Roy Wetherall'", msg.getRecipientNamesList()[5]);
  188. assertEquals("ashutosh.dandavate@alfresco.com", msg.getRecipientEmailAddressList()[0]);
  189. assertEquals("paul.hh@alfresco.com", msg.getRecipientEmailAddressList()[1]);
  190. assertEquals("mikef@alfresco.com", msg.getRecipientEmailAddressList()[2]);
  191. assertEquals("nickb@alfresco.com", msg.getRecipientEmailAddressList()[3]);
  192. assertEquals("nick.burch@alfresco.com", msg.getRecipientEmailAddressList()[4]);
  193. assertEquals("roy.wetherall@alfresco.com", msg.getRecipientEmailAddressList()[5]);
  194. }
  195. public void testFindsNameId() throws IOException {
  196. POIFSFileSystem simple = new POIFSFileSystem(
  197. new FileInputStream(samples.getFile("quick.msg"))
  198. );
  199. simple.getRoot().getEntry("__nameid_version1.0");
  200. ChunkGroup[] groups = POIFSChunkParser.parse(simple.getRoot());
  201. assertEquals(3, groups.length);
  202. assertTrue(groups[0] instanceof Chunks);
  203. assertTrue(groups[1] instanceof RecipientChunks);
  204. assertTrue(groups[2] instanceof NameIdChunks);
  205. NameIdChunks nameId = (NameIdChunks)groups[2];
  206. assertEquals(10, nameId.getAll().length);
  207. // Now via MAPIMessage
  208. MAPIMessage msg = new MAPIMessage(simple);
  209. assertNotNull(msg.getNameIdChunks());
  210. assertEquals(10, msg.getNameIdChunks().getAll().length);
  211. }
  212. public void testFindsAttachments() throws IOException {
  213. POIFSFileSystem with = new POIFSFileSystem(
  214. new FileInputStream(samples.getFile("attachment_test_msg.msg"))
  215. );
  216. POIFSFileSystem without = new POIFSFileSystem(
  217. new FileInputStream(samples.getFile("quick.msg"))
  218. );
  219. AttachmentChunks attachment;
  220. // Check raw details on the one with
  221. with.getRoot().getEntry("__attach_version1.0_#00000000");
  222. with.getRoot().getEntry("__attach_version1.0_#00000001");
  223. POIFSChunkParser.parse(with.getRoot());
  224. ChunkGroup[] groups = POIFSChunkParser.parse(with.getRoot());
  225. assertEquals(5, groups.length);
  226. assertTrue(groups[0] instanceof Chunks);
  227. assertTrue(groups[1] instanceof RecipientChunks);
  228. assertTrue(groups[2] instanceof AttachmentChunks);
  229. assertTrue(groups[3] instanceof AttachmentChunks);
  230. assertTrue(groups[4] instanceof NameIdChunks);
  231. attachment = (AttachmentChunks)groups[2];
  232. assertEquals("TEST-U~1.DOC", attachment.attachFileName.toString());
  233. assertEquals("test-unicode.doc", attachment.attachLongFileName.toString());
  234. assertEquals(24064, attachment.attachData.getValue().length);
  235. attachment = (AttachmentChunks)groups[3];
  236. assertEquals("pj1.txt", attachment.attachFileName.toString());
  237. assertEquals("pj1.txt", attachment.attachLongFileName.toString());
  238. assertEquals(89, attachment.attachData.getValue().length);
  239. // Check raw details on one without
  240. try {
  241. without.getRoot().getEntry("__attach_version1.0_#00000000");
  242. fail();
  243. } catch(FileNotFoundException e) {}
  244. try {
  245. without.getRoot().getEntry("__attach_version1.0_#00000001");
  246. fail();
  247. } catch(FileNotFoundException e) {}
  248. // One with, from the top
  249. MAPIMessage msgWith = new MAPIMessage(with);
  250. assertEquals(2, msgWith.getAttachmentFiles().length);
  251. attachment = msgWith.getAttachmentFiles()[0];
  252. assertEquals("TEST-U~1.DOC", attachment.attachFileName.toString());
  253. assertEquals("test-unicode.doc", attachment.attachLongFileName.toString());
  254. assertEquals(24064, attachment.attachData.getValue().length);
  255. attachment = msgWith.getAttachmentFiles()[1];
  256. assertEquals("pj1.txt", attachment.attachFileName.toString());
  257. assertEquals("pj1.txt", attachment.attachLongFileName.toString());
  258. assertEquals(89, attachment.attachData.getValue().length);
  259. // Plus check core details are there
  260. try {
  261. assertEquals("'nicolas1.23456@free.fr'", msgWith.getDisplayTo());
  262. assertEquals("Nicolas1 23456", msgWith.getDisplayFrom());
  263. assertEquals("test pi\u00e8ce jointe 1", msgWith.getSubject());
  264. } catch(ChunkNotFoundException e) {
  265. fail();
  266. }
  267. // One without, from the top
  268. MAPIMessage msgWithout = new MAPIMessage(without);
  269. // No attachments
  270. assertEquals(0, msgWithout.getAttachmentFiles().length);
  271. // But has core details
  272. try {
  273. assertEquals("Kevin Roast", msgWithout.getDisplayTo());
  274. assertEquals("Kevin Roast", msgWithout.getDisplayFrom());
  275. assertEquals("Test the content transformer", msgWithout.getSubject());
  276. } catch(ChunkNotFoundException e) {
  277. fail();
  278. }
  279. }
  280. /**
  281. * Bugzilla #51873 - Outlook 2002 files created with dragging and
  282. * dropping files to the disk include a non-standard named streams
  283. * such as "Olk10SideProps_0001"
  284. */
  285. public void testOlk10SideProps() throws Exception {
  286. POIFSFileSystem poifs = new POIFSFileSystem(
  287. new FileInputStream(samples.getFile("51873.msg"))
  288. );
  289. MAPIMessage msg = new MAPIMessage(poifs);
  290. // Check core details came through
  291. assertEquals("bubba@bubbasmith.com", msg.getDisplayTo());
  292. assertEquals("Test with Olk10SideProps_ Chunk", msg.getSubject());
  293. }
  294. }