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.jsieve;
21  
22  import junit.framework.TestCase;
23  
24  import org.apache.jsieve.exception.SieveException;
25  import org.apache.jsieve.mail.ActionReject;
26  import org.apache.jsieve.mail.MailAdapter;
27  import org.apache.jsieve.parser.generated.ParseException;
28  import org.apache.jsieve.utils.JUnitUtils;
29  
30  /**
31   * Class RejectTest
32   */
33  public class MultilineTextTest extends TestCase {
34      
35      /**
36       * Tests that a multiline message is correctly passed
37       */
38      public void testRejectMultilineMessage() throws Exception {
39          String message = "This is not a love song";
40          String script = "reject text:\n" + message + "\n.\n;";
41          ActionReject rejection = runRejectScript(script);        
42          assertEquals(message, rejection.getMessage());
43      }
44      
45      /**
46       * Tests that a multiline message is correctly passed when whitespace is inserted
47       * between the command and the content.
48       */
49      public void testRejectMultilineMessageWithWhitespace() throws Exception {
50          String message = "This is not a love song";
51          String script = "reject text: \t \t \n" + message + "\n.\n;";
52          ActionReject rejection = runRejectScript(script);        
53          assertEquals(message, rejection.getMessage());
54      }    
55      
56      /**
57       * Tests that a multiline message is correctly passed when dots within a line
58       * between the command and the content.
59       */
60      public void testRejectMultilineMessageWithDotsMidline() throws Exception {
61          String message = "This is not.....a love song";
62          String script = "reject text:\n" + message + "\n.\n;";
63          ActionReject rejection = runRejectScript(script);        
64          assertEquals(message, rejection.getMessage());
65      }    
66      
67      /**
68       * Tests that a multiline message with dot stuffing is correctly decoded.
69       */
70      public void testRejectMultilineMessageWithDotStuffing() throws Exception {
71          String lineOne = "This is not\n";
72          String lineTwo = ".A Love Story";
73          String script = "reject text:\n" + lineOne + '.' + lineTwo + "\n.\n;";
74          ActionReject rejection = runRejectScript(script);        
75          assertEquals(lineOne + lineTwo, rejection.getMessage());
76      }
77      
78      /**
79       * Tests that a multiline message with missed dot stuffing is correctly decoded.
80       */
81      public void testRejectMultilineMessageWithMissedDotStuffing() throws Exception {
82          String lineOne = "This is not\n";
83          String lineTwo = ".A Love Story";
84          String script = "reject text:\n" + lineOne + lineTwo + "\n.\n;";
85          ActionReject rejection = runRejectScript(script);        
86          assertEquals(lineOne + lineTwo, rejection.getMessage());
87      }
88      
89      /**
90       * Tests that a multiline message with many dots stuffed is correctly decoded.
91       */
92      public void testNumberOfStuffedDotsInMultilineMessage() throws Exception {
93          String lineOne = "This is line 1.\n";
94          String lineTwo = "This is line 2.\n";
95          String lineThree = "........ This is line 3.\n";
96          String script = "reject text:\n" + lineOne + lineTwo + '.' + lineThree + "\n.\n;";
97          ActionReject rejection = runRejectScript(script);        
98          assertEquals(lineOne + lineTwo + lineThree, rejection.getMessage());
99      }
100     
101     /**
102      * Tests that a multiline message with many dots stuffed is correctly decoded.
103      */
104     public void testConsecutiveDotStuffedLineInMultilineMessage() throws Exception {
105         String lineOne = "This is line 1.\n";
106         String lineTwo = "This is line 2.\n";
107         String lineThree = "........ This is line 3.\n";
108         String lineFour = ".\n";
109         String lineFive = ".\n";
110         String script = "reject text:\n" + lineOne + lineTwo + '.' + lineThree + '.' + lineFour + '.' + lineFive + "\n.\n;";
111         ActionReject rejection = runRejectScript(script);        
112         assertEquals(lineOne + lineTwo + lineThree + lineFour + lineFive, rejection.getMessage());
113     }
114     
115     private ActionReject runRejectScript(String script) throws SieveException, ParseException {
116         MailAdapter mail = JUnitUtils.createMail();
117         JUnitUtils.interpret(mail, script);
118         assertTrue(mail.getActions().size() == 1);
119         Object action = mail.getActions().get(0);
120         assertTrue(action instanceof ActionReject);
121         ActionReject rejection = (ActionReject) action;
122         return rejection;
123     }
124 }