View Javadoc
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    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.net.smtp;
18  
19  import static org.junit.Assert.assertEquals;
20  import static org.junit.Assert.assertNotNull;
21  import static org.junit.Assert.fail;
22  import static org.junit.jupiter.api.Assertions.assertThrows;
23  
24  import java.text.ParseException;
25  import java.text.SimpleDateFormat;
26  import java.util.Date;
27  import java.util.Locale;
28  import java.util.regex.Matcher;
29  import java.util.regex.Pattern;
30  
31  import org.junit.Before;
32  import org.junit.Test;
33  
34  public class SimpleSMTPHeaderTestCase {
35  
36      private SimpleSMTPHeader header;
37      private Date beforeDate;
38  
39      // Returns the msg without a date
40      private String checkDate(final String msg) {
41          final Pattern pat = Pattern.compile("^(Date: (.+))$", Pattern.MULTILINE);
42          final Matcher m = pat.matcher(msg);
43          if (m.find()) {
44              final String date = m.group(2);
45              final String pattern = "EEE, dd MMM yyyy HH:mm:ss Z"; // Fri, 21 Nov 1997 09:55:06 -0600
46              final SimpleDateFormat format = new SimpleDateFormat(pattern, Locale.ENGLISH);
47              try {
48                  final Date sentDate = format.parse(date);
49                  // Round to nearest second because the text format does not include ms
50                  final long sentSecs = sentDate.getTime() / 1000;
51                  final long beforeDateSecs = beforeDate.getTime() / 1000;
52                  final Date afterDate = new Date();
53                  final long afterDateSecs = afterDate.getTime() / 1000;
54                  if (sentSecs < beforeDateSecs) {
55                      fail(sentDate + " should be after " + beforeDate);
56                  }
57                  if (sentSecs > afterDateSecs) {
58                      fail(sentDate + " should be before " + afterDate);
59                  }
60              } catch (final ParseException e) {
61                  fail("" + e);
62              }
63  
64              final int start = m.start(1);
65              final int end = m.end(1);
66              if (start == 0) {
67                  return msg.substring(end + 1);
68              }
69              return msg.substring(0, start) + msg.substring(end + 1);
70          }
71          fail("Expecting Date header in " + msg);
72          return null;
73      }
74  
75      @Before
76      public void setUp() {
77          beforeDate = new Date();
78          header = new SimpleSMTPHeader("from@here.invalid", "to@there.invalid", "Test email");
79      }
80  
81      @Test
82      public void testToString() {
83          assertNotNull(header);
84          // Note that the DotTerminatedMessageWriter converts LF to CRLF
85          assertEquals("From: from@here.invalid\nTo: to@there.invalid\nSubject: Test email\n\n", checkDate(header.toString()));
86      }
87  
88      @Test
89      public void testToStringAddHeader() {
90          final SimpleSMTPHeader hdr = new SimpleSMTPHeader("from@here.invalid", null, null);
91          assertNotNull(hdr);
92          hdr.addHeaderField("X-Header1", "value 1");
93          hdr.addHeaderField("X-Header2", "value 2");
94          // Note that the DotTerminatedMessageWriter converts LF to CRLF
95          assertEquals("X-Header1: value 1\nX-Header2: value 2\nFrom: from@here.invalid\n\n", checkDate(hdr.toString()));
96      }
97  
98      @Test
99      public void testToStringAddHeaderDate() {
100         final SimpleSMTPHeader hdr = new SimpleSMTPHeader("from@here.invalid", null, null);
101         assertNotNull(hdr);
102         hdr.addHeaderField("Date", "dummy date");
103         // does not replace the Date field
104         assertEquals("Date: dummy date\nFrom: from@here.invalid\n\n", hdr.toString());
105     }
106 
107     @Test
108     public void testToStringNoFrom() {
109         assertThrows(IllegalArgumentException.class, () -> new SimpleSMTPHeader(null, null, null));
110     }
111 
112     @Test
113     public void testToStringNoSubject() {
114         final SimpleSMTPHeader hdr = new SimpleSMTPHeader("from@here.invalid", "to@there.invalid", null);
115         assertNotNull(hdr);
116         // Note that the DotTerminatedMessageWriter converts LF to CRLF
117         assertEquals("From: from@here.invalid\nTo: to@there.invalid\n\n", checkDate(hdr.toString()));
118     }
119 
120     @Test
121     public void testToStringNoTo() {
122         final SimpleSMTPHeader hdr = new SimpleSMTPHeader("from@here.invalid", null, null);
123         assertNotNull(hdr);
124         // Note that the DotTerminatedMessageWriter converts LF to CRLF
125         assertEquals("From: from@here.invalid\n\n", checkDate(hdr.toString()));
126     }
127 }