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