001 /* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017 package org.apache.commons.mail; 018 019 import java.lang.reflect.Method; 020 021 import javax.mail.internet.InternetAddress; 022 023 /** 024 * JUnit test case demonstrating InternetAddress validation. 025 * 026 * @since 1.0 027 * @author Niall Pemberton 028 * @version $Id: InvalidInternetAddressTest.java 512208 2007-02-27 10:22:28Z dion $ 029 */ 030 031 public class InvalidInternetAddressTest extends BaseEmailTestCase 032 { 033 034 /** */ 035 private static final String VALID_QUOTED_EMAIL = "\"John O'Groats\"@domain.com"; 036 037 /** JavaMail 1.2. does not know about this */ 038 private static Method validateMethod; 039 040 /** */ 041 private static final String[] ARR_INVALID_EMAILS = 042 { 043 "local name@domain.com", 044 "local(name@domain.com", 045 "local)name@domain.com", 046 "local<name@domain.com", 047 "local>name@domain.com", 048 "local,name@domain.com", 049 "local;name@domain.com", 050 "local:name@domain.com", 051 "local[name@domain.com", 052 "local]name@domain.com", 053 "local\\name@domain.com", 054 "local\"name@domain.com", 055 "local\tname@domain.com", 056 "local\nname@domain.com", 057 "local\rname@domain.com", 058 "local.name@domain com", 059 "local.name@domain(com", 060 "local.name@domain)com", 061 "local.name@domain<com", 062 "local.name@domain>com", 063 "local.name@domain,com", 064 "local.name@domain;com", 065 "local.name@domain:com", 066 "local.name@domain[com", 067 "local.name@domain]com", 068 "local.name@domain\\com", 069 "local.name@domain\tcom", 070 "local.name@domain\ncom", 071 "local.name@domain\rcom", 072 "local.name@", 073 "@domain.com" }; 074 /** 075 * @param name name 076 */ 077 public InvalidInternetAddressTest(String name) 078 { 079 super(name); 080 } 081 082 /** 083 * Setup for a test 084 * @throws Exception on any error 085 */ 086 protected void setUp() throws Exception 087 { 088 super.setUp(); 089 090 try 091 { 092 validateMethod = InternetAddress.class.getMethod("validate", new Class [0]); 093 } 094 catch (Exception e) 095 { 096 assertEquals("Got wrong Exception when looking for validate()", NoSuchMethodException.class, e.getClass()); 097 } 098 } 099 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 }