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