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 1606710 2014-06-30 12:29:13Z ggregory $
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       */
81      @Before
82      public void setUpInvalidInternetAddressTest()
83      {
84          try
85          {
86              validateMethod = InternetAddress.class.getMethod("validate", new Class [0]);
87          }
88          catch (final Exception e)
89          {
90              assertEquals("Got wrong Exception when looking for validate()", NoSuchMethodException.class, e.getClass());
91          }
92      }
93  
94      @Test
95      public void testStrictConstructor() throws Exception
96      {
97          // ====================================================================
98          // Prove InternetAddress constructor is throwing exception.
99          // ====================================================================
100 
101 
102         // test Invalid Email addresses
103         for (int i = 0; i < ARR_INVALID_EMAILS.length; i++)
104         {
105 
106             try
107             {
108                 // Create Internet Address using "strict" constructor
109                 new InternetAddress(ARR_INVALID_EMAILS[i]);
110 
111                 // Expected an exception to be thrown
112                 fail("Strict " + i + " passed: " + ARR_INVALID_EMAILS[i]);
113             }
114             catch (final Exception ex)
115             {
116                 // Expected Result
117             }
118 
119         }
120 
121         // test valid 'quoted' Email addresses
122         try
123         {
124 
125             // Create Internet Address using "strict" constructor
126             new InternetAddress(VALID_QUOTED_EMAIL);
127 
128         }
129         catch (final Exception ex)
130         {
131             fail("Valid Quoted Email failed: " + VALID_QUOTED_EMAIL
132                 + " - " + ex.getMessage());
133         }
134     }
135 
136     @Test
137     public void testValidateMethod() throws Exception
138     {
139         if (validateMethod == null)
140         {
141             return;
142         }
143 
144         // ====================================================================
145         // Prove InternetAddress constructor isn't throwing exception and
146         // the validate() method is
147         // ====================================================================
148 
149         for (int i = 0; i < ARR_INVALID_EMAILS.length; i++)
150         {
151 
152             final InternetAddress address = new InternetAddress(ARR_INVALID_EMAILS[i], "Joe");
153 
154             // N.B. validate() doesn't check addresses containing quotes or '['
155             final boolean quoted = ARR_INVALID_EMAILS[i].contains("\"");
156             final int atIndex    = ARR_INVALID_EMAILS[i].indexOf("@");
157             final boolean domainBracket  = atIndex >= 0
158                 && ARR_INVALID_EMAILS[i].indexOf("[", atIndex)  >= 0;
159             try
160             {
161                 validateMethod.invoke(address, (Object[]) null);
162 
163                 if (!(quoted || domainBracket))
164                 {
165                     fail("Validate " + i + " passed: " + ARR_INVALID_EMAILS[i]);
166                 }
167             }
168             catch (final Exception ex)
169             {
170                 if (quoted || domainBracket)
171                 {
172                     fail("Validate " + i + " failed: " + ARR_INVALID_EMAILS[i]
173                         + " - " + ex.getMessage());
174                 }
175             }
176         }
177 
178         // test valid 'quoted' Email addresses
179         try
180         {
181             validateMethod.invoke(new InternetAddress(VALID_QUOTED_EMAIL, "Joe"), (Object[]) null);
182         }
183         catch (final Exception ex)
184         {
185             fail("Valid Quoted Email failed: " + VALID_QUOTED_EMAIL
186                 + " - " + ex.getMessage());
187         }
188     }
189 
190     @Test
191     public void testValidateMethodCharset() throws Exception
192     {
193         if (validateMethod == null)
194         {
195             return;
196         }
197 
198         // ====================================================================
199         // Prove InternetAddress constructor isn't throwing exception and
200         // the validate() method is
201         // ====================================================================
202 
203         for (int i = 0; i < ARR_INVALID_EMAILS.length; i++)
204         {
205 
206             final InternetAddress address = new InternetAddress(ARR_INVALID_EMAILS[i], "Joe", "UTF-8");
207 
208             // N.B. validate() doesn't check addresses containing quotes or '['
209             final boolean quoted = ARR_INVALID_EMAILS[i].contains("\"");
210             final int atIndex    = ARR_INVALID_EMAILS[i].indexOf("@");
211             final boolean domainBracket  = atIndex >= 0
212                 && ARR_INVALID_EMAILS[i].indexOf("[", atIndex)  >= 0;
213 
214             try
215             {
216                 validateMethod.invoke(address, (Object[]) null);
217                 if (!(quoted || domainBracket))
218                 {
219                     fail("Validate " + i + " passed: " + ARR_INVALID_EMAILS[i]);
220                 }
221 
222             }
223             catch (final Exception ex)
224             {
225 
226                 if (quoted || domainBracket)
227                 {
228                     fail("Validate " + i + " failed: " + ARR_INVALID_EMAILS[i]
229                         + " - " + ex.getMessage());
230                 }
231 
232             }
233 
234         }
235 
236         // test valid 'quoted' Email addresses
237         try
238         {
239             validateMethod.invoke(new InternetAddress(VALID_QUOTED_EMAIL, "Joe", "UTF-8"), (Object[]) null);
240         }
241         catch (final Exception ex)
242         {
243             fail("Valid Quoted Email failed: " + VALID_QUOTED_EMAIL
244                 + " - " + ex.getMessage());
245         }
246     }
247 
248 }