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