[6785] | 1 | /**************************************************************** |
---|
| 2 | * Licensed to the Apache Software Foundation (ASF) under one * |
---|
| 3 | * or more contributor license agreements. See the NOTICE file * |
---|
| 4 | * distributed with this work for additional information * |
---|
| 5 | * regarding copyright ownership. The ASF licenses this file * |
---|
| 6 | * to you under the Apache License, Version 2.0 (the * |
---|
| 7 | * "License"); you may not use this file except in compliance * |
---|
| 8 | * with the License. You may obtain a copy of the License at * |
---|
| 9 | * * |
---|
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 * |
---|
| 11 | * * |
---|
| 12 | * Unless required by applicable law or agreed to in writing, * |
---|
| 13 | * software distributed under the License is distributed on an * |
---|
| 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * |
---|
| 15 | * KIND, either express or implied. See the License for the * |
---|
| 16 | * specific language governing permissions and limitations * |
---|
| 17 | * under the License. * |
---|
| 18 | ****************************************************************/ |
---|
| 19 | |
---|
| 20 | package org.apache.james.mime4j.stream; |
---|
| 21 | |
---|
| 22 | import java.io.ByteArrayInputStream; |
---|
| 23 | import java.io.InputStream; |
---|
| 24 | |
---|
| 25 | import org.apache.commons.io.IOUtils; |
---|
| 26 | |
---|
| 27 | import junit.framework.TestCase; |
---|
| 28 | |
---|
| 29 | public class StrictMimeTokenStreamTest extends TestCase { |
---|
| 30 | |
---|
| 31 | private static final String HEADER_ONLY = "From: foo@abr.com\r\nSubject: A subject\r\n"; |
---|
| 32 | private static final String CORRECT_HEADERS = HEADER_ONLY + "\r\n"; |
---|
| 33 | |
---|
| 34 | MimeTokenStream parser; |
---|
| 35 | |
---|
| 36 | @Override |
---|
| 37 | protected void setUp() throws Exception { |
---|
| 38 | super.setUp(); |
---|
| 39 | MimeEntityConfig config = new MimeEntityConfig(); |
---|
| 40 | config.setStrictParsing(true); |
---|
| 41 | parser = new MimeTokenStream(config); |
---|
| 42 | } |
---|
| 43 | |
---|
| 44 | @Override |
---|
| 45 | protected void tearDown() throws Exception { |
---|
| 46 | super.tearDown(); |
---|
| 47 | } |
---|
| 48 | |
---|
| 49 | public void testUnexpectedEndOfHeaders() throws Exception { |
---|
| 50 | parser.parse(new ByteArrayInputStream(HEADER_ONLY.getBytes("US-ASCII"))); |
---|
| 51 | |
---|
| 52 | checkNextIs(EntityState.T_START_HEADER); |
---|
| 53 | checkNextIs(EntityState.T_FIELD); |
---|
| 54 | try { |
---|
| 55 | parser.next(); |
---|
| 56 | fail("Expected exception to be thrown"); |
---|
| 57 | } catch (MimeParseEventException e) { |
---|
| 58 | assertEquals("Premature end of headers", Event.HEADERS_PREMATURE_END, e.getEvent()); |
---|
| 59 | } |
---|
| 60 | } |
---|
| 61 | |
---|
| 62 | public void testCorrectEndOfHeaders() throws Exception { |
---|
| 63 | parser.parse(new ByteArrayInputStream(CORRECT_HEADERS.getBytes("US-ASCII"))); |
---|
| 64 | |
---|
| 65 | checkNextIs(EntityState.T_START_HEADER); |
---|
| 66 | checkNextIs(EntityState.T_FIELD); |
---|
| 67 | checkNextIs(EntityState.T_FIELD); |
---|
| 68 | checkNextIs(EntityState.T_END_HEADER); |
---|
| 69 | } |
---|
| 70 | |
---|
| 71 | public void testMissingBoundary() throws Exception { |
---|
| 72 | String message = |
---|
| 73 | "Content-Type: multipart/mixed;boundary=aaaa\r\n\r\n" + |
---|
| 74 | "--aaaa\r\n" + |
---|
| 75 | "Content-Type:text/plain; charset=US-ASCII\r\n\r\n" + |
---|
| 76 | "Oh my god! Boundary is missing!\r\n"; |
---|
| 77 | parser.parse(new ByteArrayInputStream(message.getBytes("US-ASCII"))); |
---|
| 78 | checkNextIs(EntityState.T_START_HEADER); |
---|
| 79 | checkNextIs(EntityState.T_FIELD); |
---|
| 80 | checkNextIs(EntityState.T_END_HEADER); |
---|
| 81 | checkNextIs(EntityState.T_START_MULTIPART); |
---|
| 82 | checkNextIs(EntityState.T_START_BODYPART); |
---|
| 83 | checkNextIs(EntityState.T_START_HEADER); |
---|
| 84 | checkNextIs(EntityState.T_FIELD); |
---|
| 85 | checkNextIs(EntityState.T_END_HEADER); |
---|
| 86 | checkNextIs(EntityState.T_BODY); |
---|
| 87 | InputStream out = parser.getInputStream(); |
---|
| 88 | assertEquals("Oh my god! Boundary is missing!\r\n", IOUtils.toString(out, "US-ASCII")); |
---|
| 89 | checkNextIs(EntityState.T_END_BODYPART); |
---|
| 90 | try { |
---|
| 91 | parser.next(); |
---|
| 92 | fail("MimeParseEventException should have been thrown"); |
---|
| 93 | } catch (MimeParseEventException expected) { |
---|
| 94 | } |
---|
| 95 | } |
---|
| 96 | |
---|
| 97 | private void checkNextIs(EntityState expected) throws Exception { |
---|
| 98 | assertEquals(MimeTokenStream.stateToString(expected), MimeTokenStream.stateToString(parser.next())); |
---|
| 99 | } |
---|
| 100 | } |
---|