1   /*
2    * Copyright 2005 The Apache Software Foundation
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.apache.commons.mail;
17  
18  import java.lang.reflect.Method;
19  
20  import javax.mail.internet.InternetAddress;
21  
22  /**
23   * JUnit test case demonstrating InternetAddress validation.
24   *
25   * @since 1.0
26   * @author Niall Pemberton
27   * @version $Id: InvalidInternetAddressTest.java 279295 2005-09-07 10:56:41Z henning $
28   */
29  
30  public class InvalidInternetAddressTest extends BaseEmailTestCase
31  {
32  
33      /** */
34      private static final String VALID_QUOTED_EMAIL = "\"John O'Groats\"@domain.com";
35  
36      /** JavaMail 1.2. does not know about this */
37      private static Method validateMethod = null;
38  
39      /** */
40      private static final String[] ARR_INVALID_EMAILS =
41          {
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",
52              "local\\name@domain.com",
53              "local\"name@domain.com",
54              "local\tname@domain.com",
55              "local\nname@domain.com",
56              "local\rname@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\\com",
68              "local.name@domain\tcom",
69              "local.name@domain\ncom",
70              "local.name@domain\rcom",
71              "local.name@",
72              "@domain.com" };
73      /**
74       * @param name name
75       */
76      public InvalidInternetAddressTest(String name)
77      {
78          super(name);
79      }
80  
81      protected void setUp()
82      {
83          super.setUp();
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      /**
96       *
97       * @throws Exception Exception
98       */
99      public void testStrictConstructor() throws Exception
100     {
101         // ====================================================================
102         // Prove InternetAddress constructor is throwing exception.
103         // ====================================================================
104 
105 
106         // test Invalid Email addresses
107         for (int i = 0; i < ARR_INVALID_EMAILS.length; i++)
108         {
109 
110             try {
111 
112                 // Create Internet Address using "strict" constructor
113                 new InternetAddress(ARR_INVALID_EMAILS[i]);
114 
115                 // Expected an exception to be thrown
116                 fail("Strict " + i + " passed: " + ARR_INVALID_EMAILS[i]);
117 
118             } catch (Exception ex) {
119                 // Expected Result
120             }
121 
122         }
123 
124         // test valid 'quoted' Email addresses
125         try {
126 
127             // Create Internet Address using "strict" constructor
128             new InternetAddress(VALID_QUOTED_EMAIL);
129 
130         } catch (Exception ex) {
131             fail("Valid Quoted Email failed: " + VALID_QUOTED_EMAIL +
132                  " - " + ex.getMessage());
133         }
134     }
135 
136     /**
137      *
138      * @throws Exception Exception
139      */
140     public void testValidateMethod() throws Exception
141     {
142         if (validateMethod == null)
143         {
144             return;
145         }
146 
147         // ====================================================================
148         // Prove InternetAddress constructor isn't throwing exception and
149         // the validate() method is
150         // ====================================================================
151 
152         for (int i = 0; i < ARR_INVALID_EMAILS.length; i++)
153         {
154 
155             InternetAddress address = new InternetAddress(ARR_INVALID_EMAILS[i], "Joe");
156 
157             // N.B. validate() doesn't check addresses containing quotes or '['
158             boolean quoted = (ARR_INVALID_EMAILS[i].indexOf("\"") >= 0);
159             int atIndex    = ARR_INVALID_EMAILS[i].indexOf("@");
160             boolean domainBracket  = (atIndex >= 0) &&
161                                      (ARR_INVALID_EMAILS[i].indexOf("[", atIndex)  >= 0);
162             try {
163 
164                 validateMethod.invoke(address, null);
165 
166                 if (!(quoted || domainBracket)) {
167                     fail("Validate " + i + " passed: " + ARR_INVALID_EMAILS[i]);
168                 }
169 
170             } catch (Exception ex) {
171 
172                 if (quoted || domainBracket) {
173                     fail("Validate " + i + " failed: " + ARR_INVALID_EMAILS[i] +
174                          " - " + ex.getMessage());
175                 }
176 
177             }
178 
179         }
180 
181         // test valid 'quoted' Email addresses
182         try {
183 
184             validateMethod.invoke(new InternetAddress(VALID_QUOTED_EMAIL, "Joe"), null);
185 
186         } catch (Exception ex) {
187             fail("Valid Quoted Email failed: " + VALID_QUOTED_EMAIL +
188                  " - " + ex.getMessage());
189         }
190 
191     }
192 
193     /**
194      *
195      * @throws Exception Exception
196      */
197     public void testValidateMethodCharset() throws Exception
198     {
199         if (validateMethod == null)
200         {
201             return;
202         }
203 
204         // ====================================================================
205         // Prove InternetAddress constructor isn't throwing exception and
206         // the validate() method is
207         // ====================================================================
208 
209         for (int i = 0; i < ARR_INVALID_EMAILS.length; i++)
210         {
211 
212             InternetAddress address = new InternetAddress(ARR_INVALID_EMAILS[i], "Joe", "UTF-8");
213 
214             // N.B. validate() doesn't check addresses containing quotes or '['
215             boolean quoted = (ARR_INVALID_EMAILS[i].indexOf("\"") >= 0);
216             int atIndex    = ARR_INVALID_EMAILS[i].indexOf("@");
217             boolean domainBracket  = (atIndex >= 0) &&
218                                      (ARR_INVALID_EMAILS[i].indexOf("[", atIndex)  >= 0);
219 
220             try {
221 
222                 validateMethod.invoke(address, null);
223 
224                 if (!(quoted || domainBracket)) {
225                     fail("Validate " + i + " passed: " + ARR_INVALID_EMAILS[i]);
226                 }
227 
228             } catch (Exception ex) {
229 
230                 if (quoted || domainBracket) {
231                     fail("Validate " + i + " failed: " + ARR_INVALID_EMAILS[i] +
232                          " - " + ex.getMessage());
233                 }
234 
235             }
236 
237         }
238 
239         // test valid 'quoted' Email addresses
240         try {
241 
242             validateMethod.invoke(new InternetAddress(VALID_QUOTED_EMAIL, "Joe", "UTF-8"), null);
243 
244         } catch (Exception ex) {
245             fail("Valid Quoted Email failed: " + VALID_QUOTED_EMAIL +
246                  " - " + ex.getMessage());
247         }
248     }
249 
250 }