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.mail;
18  
19  import static org.junit.Assert.assertEquals;
20  import static org.junit.Assert.fail;
21  
22  import java.lang.reflect.Method;
23  
24  import javax.mail.internet.InternetAddress;
25  
26  import org.junit.Before;
27  import org.junit.Test;
28  
29  /**
30   * JUnit test case demonstrating InternetAddress validation.
31   *
32   * @since 1.0
33   * @version $Id: InvalidInternetAddressTest.java 1510676 2013-08-05 19:29:09Z britter $
34   */
35  public class InvalidInternetAddressTest extends AbstractEmailTest
36  {
37      /** */
38      private static final String VALID_QUOTED_EMAIL = "\"John O'Groats\"@domain.com";
39  
40      /** JavaMail 1.2. does not know about this */
41      private static Method validateMethod;
42  
43      /** */
44      private static final String[] ARR_INVALID_EMAILS =
45          {
46              "local name@domain.com",
47              "local(name@domain.com",
48              "local)name@domain.com",
49              "local<name@domain.com",
50              "local>name@domain.com",
51              "local,name@domain.com",
52              "local;name@domain.com",
53              "local:name@domain.com",
54              "local[name@domain.com",
55              "local]name@domain.com",
56              // "local\\name@domain.com", -- works for javamail-1.4.4
57              // "local\"name@domain.com", -- works for javamail-1.4.4
58              "local\tname@domain.com",
59              "local\nname@domain.com",
60              "local\rname@domain.com",
61              "local.name@domain com",
62              "local.name@domain(com",
63              "local.name@domain)com",
64              "local.name@domain<com",
65              "local.name@domain>com",
66              "local.name@domain,com",
67              "local.name@domain;com",
68              "local.name@domain:com",
69              "local.name@domain[com",
70              "local.name@domain]com",
71              "local.name@domain\\com",
72              "local.name@domain\tcom",
73              "local.name@domain\ncom",
74              "local.name@domain\rcom",
75              "local.name@",
76              "@domain.com" };
77  
78      /**
79       * Setup for a test
80       * @throws Exception on any error
81       */
82      @Before
83      public void setUpInvalidInternetAddressTest()
84      {
85          try
86          {
87              validateMethod = InternetAddress.class.getMethod("validate", new Class [0]);
88          }
89          catch (Exception e)
90          {
91              assertEquals("Got wrong Exception when looking for validate()", NoSuchMethodException.class, e.getClass());
92          }
93      }
94  
95      @Test
96      public void testStrictConstructor() throws Exception
97      {
98          // ====================================================================
99          // Prove InternetAddress constructor is throwing exception.
100         // ====================================================================
101 
102 
103         // test Invalid Email addresses
104         for (int i = 0; i < ARR_INVALID_EMAILS.length; i++)
105         {
106 
107             try
108             {
109                 // Create Internet Address using "strict" constructor
110                 new InternetAddress(ARR_INVALID_EMAILS[i]);
111 
112                 // Expected an exception to be thrown
113                 fail("Strict " + i + " passed: " + ARR_INVALID_EMAILS[i]);
114             }
115             catch (Exception ex)
116             {
117                 // Expected Result
118             }
119 
120         }
121 
122         // test valid 'quoted' Email addresses
123         try
124         {
125 
126             // Create Internet Address using "strict" constructor
127             new InternetAddress(VALID_QUOTED_EMAIL);
128 
129         }
130         catch (Exception ex)
131         {
132             fail("Valid Quoted Email failed: " + VALID_QUOTED_EMAIL
133                 + " - " + ex.getMessage());
134         }
135     }
136 
137     @Test
138     public void testValidateMethod() throws Exception
139     {
140         if (validateMethod == null)
141         {
142             return;
143         }
144 
145         // ====================================================================
146         // Prove InternetAddress constructor isn't throwing exception and
147         // the validate() method is
148         // ====================================================================
149 
150         for (int i = 0; i < ARR_INVALID_EMAILS.length; i++)
151         {
152 
153             InternetAddress address = new InternetAddress(ARR_INVALID_EMAILS[i], "Joe");
154 
155             // N.B. validate() doesn't check addresses containing quotes or '['
156             boolean quoted = ARR_INVALID_EMAILS[i].contains("\"");
157             int atIndex    = ARR_INVALID_EMAILS[i].indexOf("@");
158             boolean domainBracket  = (atIndex >= 0)
159                 && (ARR_INVALID_EMAILS[i].indexOf("[", atIndex)  >= 0);
160             try
161             {
162                 validateMethod.invoke(address, (Object[]) null);
163 
164                 if (!(quoted || domainBracket))
165                 {
166                     fail("Validate " + i + " passed: " + ARR_INVALID_EMAILS[i]);
167                 }
168             }
169             catch (Exception ex)
170             {
171                 if (quoted || domainBracket)
172                 {
173                     fail("Validate " + i + " failed: " + ARR_INVALID_EMAILS[i]
174                         + " - " + ex.getMessage());
175                 }
176             }
177         }
178 
179         // test valid 'quoted' Email addresses
180         try
181         {
182             validateMethod.invoke(new InternetAddress(VALID_QUOTED_EMAIL, "Joe"), (Object[]) null);
183         }
184         catch (Exception ex)
185         {
186             fail("Valid Quoted Email failed: " + VALID_QUOTED_EMAIL
187                 + " - " + ex.getMessage());
188         }
189     }
190 
191     @Test
192     public void testValidateMethodCharset() throws Exception
193     {
194         if (validateMethod == null)
195         {
196             return;
197         }
198 
199         // ====================================================================
200         // Prove InternetAddress constructor isn't throwing exception and
201         // the validate() method is
202         // ====================================================================
203 
204         for (int i = 0; i < ARR_INVALID_EMAILS.length; i++)
205         {
206 
207             InternetAddress address = new InternetAddress(ARR_INVALID_EMAILS[i], "Joe", "UTF-8");
208 
209             // N.B. validate() doesn't check addresses containing quotes or '['
210             boolean quoted = ARR_INVALID_EMAILS[i].contains("\"");
211             int atIndex    = ARR_INVALID_EMAILS[i].indexOf("@");
212             boolean domainBracket  = (atIndex >= 0)
213                 && (ARR_INVALID_EMAILS[i].indexOf("[", atIndex)  >= 0);
214 
215             try
216             {
217                 validateMethod.invoke(address, (Object[]) null);
218                 if (!(quoted || domainBracket))
219                 {
220                     fail("Validate " + i + " passed: " + ARR_INVALID_EMAILS[i]);
221                 }
222 
223             }
224             catch (Exception ex)
225             {
226 
227                 if (quoted || domainBracket)
228                 {
229                     fail("Validate " + i + " failed: " + ARR_INVALID_EMAILS[i]
230                         + " - " + ex.getMessage());
231                 }
232 
233             }
234 
235         }
236 
237         // test valid 'quoted' Email addresses
238         try
239         {
240             validateMethod.invoke(new InternetAddress(VALID_QUOTED_EMAIL, "Joe", "UTF-8"), (Object[]) null);
241         }
242         catch (Exception ex)
243         {
244             fail("Valid Quoted Email failed: " + VALID_QUOTED_EMAIL
245                 + " - " + ex.getMessage());
246         }
247     }
248 
249 }