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.validator;
18  
19  import static org.junit.jupiter.api.Assertions.assertFalse;
20  import static org.junit.jupiter.api.Assertions.assertNotNull;
21  import static org.junit.jupiter.api.Assertions.assertTrue;
22  
23  import java.io.IOException;
24  
25  import org.junit.jupiter.api.BeforeEach;
26  import org.junit.jupiter.api.Test;
27  import org.xml.sax.SAXException;
28  
29  /**
30   * Performs Validation Test for e-mail validations.
31   *
32   *
33   * @deprecated to be removed when target class is removed
34   */
35  @Deprecated
36  public class EmailTest extends AbstractCommonTest {
37  
38      /**
39       * The key used to retrieve the set of validation rules from the xml file.
40       */
41      protected static String FORM_KEY = "emailForm";
42  
43      /**
44       * The key used to retrieve the validator action.
45       */
46      protected static String ACTION = "email";
47  
48      /**
49       * These test values derive directly from RFC 822 & Mail::RFC822::Address & RFC::RFC822::Address perl test.pl For traceability don't combine these test
50       * values with other tests.
51       */
52      ResultPair[] testEmailFromPerl = { new ResultPair("abigail@example.com", true), new ResultPair("abigail@example.com ", true),
53              new ResultPair(" abigail@example.com", true), new ResultPair("abigail @example.com ", true), new ResultPair("*@example.net", true),
54              new ResultPair("\"\\\"\"@foo.bar", true), new ResultPair("fred&barny@example.com", true), new ResultPair("---@example.com", true),
55              new ResultPair("foo-bar@example.net", true), new ResultPair("\"127.0.0.1\"@[127.0.0.1]", true),
56              new ResultPair("Abigail <abigail@example.com>", true), new ResultPair("Abigail<abigail@example.com>", true),
57              new ResultPair("Abigail<@a,@b,@c:abigail@example.com>", true), new ResultPair("\"This is a phrase\"<abigail@example.com>", true),
58              new ResultPair("\"Abigail \"<abigail@example.com>", true), new ResultPair("\"Joe & J. Harvey\" <example @Org>", true),
59              new ResultPair("Abigail <abigail @ example.com>", true), new ResultPair("Abigail made this <  abigail   @   example  .    com    >", true),
60              new ResultPair("Abigail(the bitch)@example.com", true), new ResultPair("Abigail <abigail @ example . (bar) com >", true),
61              new ResultPair("Abigail < (one)  abigail (two) @(three)example . (bar) com (quz) >", true),
62              new ResultPair("Abigail (foo) (((baz)(nested) (comment)) ! ) < (one)  abigail (two) @(three)example . (bar) com (quz) >", true),
63              new ResultPair("Abigail <abigail(fo\\(o)@example.com>", true), new ResultPair("Abigail <abigail(fo\\)o)@example.com> ", true),
64              new ResultPair("(foo) abigail@example.com", true), new ResultPair("abigail@example.com (foo)", true),
65              new ResultPair("\"Abi\\\"gail\" <abigail@example.com>", true), new ResultPair("abigail@[example.com]", true),
66              new ResultPair("abigail@[exa\\[ple.com]", true), new ResultPair("abigail@[exa\\]ple.com]", true),
67              new ResultPair("\":sysmail\"@  Some-Group. Some-Org", true), new ResultPair("Muhammed.(I am  the greatest) Ali @(the)Vegas.WBA", true),
68              new ResultPair("mailbox.sub1.sub2@this-domain", true), new ResultPair("sub-net.mailbox@sub-domain.domain", true), new ResultPair("name:;", true),
69              new ResultPair("':;", true), new ResultPair("name:   ;", true), new ResultPair("Alfred Neuman <Neuman@BBN-TENEXA>", true),
70              new ResultPair("Neuman@BBN-TENEXA", true), new ResultPair("\"George, Ted\" <Shared@Group.Arpanet>", true),
71              new ResultPair("Wilt . (the  Stilt) Chamberlain@NBA.US", true), new ResultPair("Cruisers:  Port@Portugal, Jones@SEA;", true),
72              new ResultPair("$@[]", true), new ResultPair("*()@[]", true), new ResultPair("\"quoted ( brackets\" ( a comment )@example.com", true),
73              new ResultPair("\"Joe & J. Harvey\"\\x0D\\x0A     <ddd\\@ Org>", true), new ResultPair("\"Joe &\\x0D\\x0A J. Harvey\" <ddd \\@ Org>", true),
74              new ResultPair(
75                      "Gourmets:  Pompous Person <WhoZiWhatZit\\@Cordon-Bleu>,\\x0D\\x0A" + "        Childs\\@WGBH.Boston, \"Galloping Gourmet\"\\@\\x0D\\x0A"
76                              + "        ANT.Down-Under (Australian National Television),\\x0D\\x0A" + "        Cheapie\\@Discount-Liquors;",
77                      true),
78              new ResultPair("   Just a string", false), new ResultPair("string", false), new ResultPair("(comment)", false),
79              new ResultPair("()@example.com", false), new ResultPair("fred(&)barny@example.com", false), new ResultPair("fred\\ barny@example.com", false),
80              new ResultPair("Abigail <abi gail @ example.com>", false), new ResultPair("Abigail <abigail(fo(o)@example.com>", false),
81              new ResultPair("Abigail <abigail(fo)o)@example.com>", false), new ResultPair("\"Abi\"gail\" <abigail@example.com>", false),
82              new ResultPair("abigail@[exa]ple.com]", false), new ResultPair("abigail@[exa[ple.com]", false), new ResultPair("abigail@[exaple].com]", false),
83              new ResultPair("abigail@", false), new ResultPair("@example.com", false),
84              new ResultPair("phrase: abigail@example.com abigail@example.com ;", false), new ResultPair("invalid�char@example.com", false) };
85  
86      /**
87       * Write this test based on perl Mail::RFC822::Address which takes its example email address directly from RFC822
88       *
89       * @throws ValidatorException
90       *
91       *                            FIXME This test fails so disable it with a leading _ for 1.1.4 release. The real solution is to fix the email parsing.
92       */
93      public void _testEmailFromPerl() throws ValidatorException {
94          final ValueBean info = new ValueBean();
95          for (final ResultPair element : testEmailFromPerl) {
96              info.setValue(element.item);
97              valueTest(info, element.valid);
98          }
99      }
100 
101     /**
102      * Write this test according to parts of RFC, as opposed to the type of character that is being tested.
103      *
104      * <p>
105      * <b>FIXME</b>: This test fails so disable it with a leading _ for 1.1.4 release. The real solution is to fix the email parsing.
106      *
107      * @throws ValidatorException
108      */
109     public void _testEmailUserName() throws ValidatorException {
110         final ValueBean info = new ValueBean();
111         info.setValue("joe1blow@apache.org");
112         valueTest(info, true);
113         info.setValue("joe$blow@apache.org");
114         valueTest(info, true);
115         info.setValue("joe-@apache.org");
116         valueTest(info, true);
117         info.setValue("joe_@apache.org");
118         valueTest(info, true);
119 
120         // UnQuoted Special characters are invalid
121 
122         info.setValue("joe.@apache.org");
123         valueTest(info, false);
124         info.setValue("joe+@apache.org");
125         valueTest(info, false);
126         info.setValue("joe!@apache.org");
127         valueTest(info, false);
128         info.setValue("joe*@apache.org");
129         valueTest(info, false);
130         info.setValue("joe'@apache.org");
131         valueTest(info, false);
132         info.setValue("joe(@apache.org");
133         valueTest(info, false);
134         info.setValue("joe)@apache.org");
135         valueTest(info, false);
136         info.setValue("joe,@apache.org");
137         valueTest(info, false);
138         info.setValue("joe%45@apache.org");
139         valueTest(info, false);
140         info.setValue("joe;@apache.org");
141         valueTest(info, false);
142         info.setValue("joe?@apache.org");
143         valueTest(info, false);
144         info.setValue("joe&@apache.org");
145         valueTest(info, false);
146         info.setValue("joe=@apache.org");
147         valueTest(info, false);
148 
149         // Quoted Special characters are valid
150         info.setValue("\"joe.\"@apache.org");
151         valueTest(info, true);
152         info.setValue("\"joe+\"@apache.org");
153         valueTest(info, true);
154         info.setValue("\"joe!\"@apache.org");
155         valueTest(info, true);
156         info.setValue("\"joe*\"@apache.org");
157         valueTest(info, true);
158         info.setValue("\"joe'\"@apache.org");
159         valueTest(info, true);
160         info.setValue("\"joe(\"@apache.org");
161         valueTest(info, true);
162         info.setValue("\"joe)\"@apache.org");
163         valueTest(info, true);
164         info.setValue("\"joe,\"@apache.org");
165         valueTest(info, true);
166         info.setValue("\"joe%45\"@apache.org");
167         valueTest(info, true);
168         info.setValue("\"joe;\"@apache.org");
169         valueTest(info, true);
170         info.setValue("\"joe?\"@apache.org");
171         valueTest(info, true);
172         info.setValue("\"joe&\"@apache.org");
173         valueTest(info, true);
174         info.setValue("\"joe=\"@apache.org");
175         valueTest(info, true);
176 
177     }
178 
179     /**
180      * Load <code>ValidatorResources</code> from validator-regexp.xml.
181      */
182     @BeforeEach
183     protected void setUp() throws IOException, SAXException {
184         loadResources("EmailTest-config.xml");
185     }
186 
187     /**
188      * Tests the e-mail validation.
189      */
190     @Test
191     public void testEmail() throws ValidatorException {
192         // Create bean to run test on.
193         final ValueBean info = new ValueBean();
194 
195         info.setValue("jsmith@apache.org");
196         valueTest(info, true);
197     }
198 
199     /**
200      * Tests the e-mail validation with a user at a TLD
201      */
202     @Test
203     public void testEmailAtTLD() throws ValidatorException {
204         // Create bean to run test on.
205         final ValueBean info = new ValueBean();
206 
207         info.setValue("m@de");
208         valueTest(info, false);
209 
210         final org.apache.commons.validator.routines.EmailValidator validator = org.apache.commons.validator.routines.EmailValidator.getInstance(true, true);
211         final boolean result = validator.isValid("m@de");
212         assertTrue(result, "Result should have been true");
213 
214     }
215 
216     /**
217      * Tests the e-mail validation.
218      */
219     @Test
220     public void testEmailExtension() throws ValidatorException {
221         // Create bean to run test on.
222         final ValueBean info = new ValueBean();
223 
224         info.setValue("jsmith@apache.org");
225         valueTest(info, true);
226 
227         info.setValue("jsmith@apache.com");
228         valueTest(info, true);
229 
230         info.setValue("jsmith@apache.net");
231         valueTest(info, true);
232 
233         info.setValue("jsmith@apache.info");
234         valueTest(info, true);
235 
236         info.setValue("jsmith@apache.");
237         valueTest(info, false);
238 
239         info.setValue("jsmith@apache.c");
240         valueTest(info, false);
241 
242         info.setValue("someone@yahoo.museum");
243         valueTest(info, true);
244 
245         info.setValue("someone@yahoo.mu-seum");
246         valueTest(info, false);
247     }
248 
249     /**
250      * Test that @localhost and @localhost.localdomain addresses aren't declared valid by default
251      */
252     @Test
253     public void testEmailLocalhost() throws ValidatorException {
254         final ValueBean info = new ValueBean();
255         info.setValue("joe@localhost");
256         valueTest(info, false);
257         info.setValue("joe@localhost.localdomain");
258         valueTest(info, false);
259     }
260 
261     /**
262      * Tests the e-mail validation with an RCS-noncompliant character in the address.
263      */
264     @Test
265     public void testEmailWithBogusCharacter() throws ValidatorException {
266         // Create bean to run test on.
267         final ValueBean info = new ValueBean();
268 
269         info.setValue("andy.noble@\u008fdata-workshop.com");
270         valueTest(info, false);
271 
272         // The ' character is valid in an email username.
273         info.setValue("andy.o'reilly@data-workshop.com");
274         valueTest(info, true);
275 
276         // But not in the domain name.
277         info.setValue("andy@o'reilly.data-workshop.com");
278         valueTest(info, false);
279 
280         info.setValue("foo+bar@i.am.not.in.us.example.com");
281         valueTest(info, true);
282     }
283 
284     /**
285      * Tests the email validation with commas.
286      */
287     @Test
288     public void testEmailWithCommas() throws ValidatorException {
289         final ValueBean info = new ValueBean();
290         info.setValue("joeblow@apa,che.org");
291         valueTest(info, false);
292         info.setValue("joeblow@apache.o,rg");
293         valueTest(info, false);
294         info.setValue("joeblow@apache,org");
295         valueTest(info, false);
296 
297     }
298 
299     /**
300      * Tests the email validation with ASCII control characters. (i.e. ASCII chars 0 - 31 and 127)
301      */
302     @Test
303     public void testEmailWithControlChars() {
304         final EmailValidator validator = new EmailValidator();
305         for (char c = 0; c < 32; c++) {
306             assertFalse(validator.isValid("foo" + c + "bar@domain.com"), "Test control char " + (int) c);
307         }
308         assertFalse(validator.isValid("foo" + (char) 127 + "bar@domain.com"), "Test control char 127");
309     }
310 
311     /**
312      * <p>
313      * Tests the e-mail validation with a dash in the address.
314      * </p>
315      */
316     @Test
317     public void testEmailWithDash() throws ValidatorException {
318         // Create bean to run test on.
319         final ValueBean info = new ValueBean();
320 
321         info.setValue("andy.noble@data-workshop.com");
322         valueTest(info, true);
323 
324         info.setValue("andy-noble@data-workshop.-com");
325         valueTest(info, false);
326         info.setValue("andy-noble@data-workshop.c-om");
327         valueTest(info, false);
328         info.setValue("andy-noble@data-workshop.co-m");
329         valueTest(info, false);
330 
331     }
332 
333     /**
334      * Tests the e-mail validation with a dot at the end of the address.
335      */
336     @Test
337     public void testEmailWithDotEnd() throws ValidatorException {
338         // Create bean to run test on.
339         final ValueBean info = new ValueBean();
340 
341         info.setValue("andy.noble@data-workshop.com.");
342         valueTest(info, false);
343 
344     }
345 
346     /**
347      * Tests the email validation with numeric domains.
348      */
349     @Test
350     public void testEmailWithNumericAddress() throws ValidatorException {
351         final ValueBean info = new ValueBean();
352         info.setValue("someone@[216.109.118.76]");
353         valueTest(info, true);
354         info.setValue("someone@yahoo.com");
355         valueTest(info, true);
356     }
357 
358     /**
359      * Tests the email validation with spaces.
360      */
361     @Test
362     public void testEmailWithSpaces() throws ValidatorException {
363         final ValueBean info = new ValueBean();
364         info.setValue("joeblow @apache.org");
365         valueTest(info, false);
366         info.setValue("joeblow@ apache.org");
367         valueTest(info, false);
368         info.setValue(" joeblow@apache.org");
369         valueTest(info, false);
370         info.setValue("joeblow@apache.org ");
371         valueTest(info, false);
372         info.setValue("joe blow@apache.org ");
373         valueTest(info, false);
374         info.setValue("joeblow@apa che.org ");
375         valueTest(info, false);
376         info.setValue("\"joe blow\"@apache.org");
377         valueTest(info, true);
378 
379     }
380 
381     /**
382      * Utlity class to run a test on a value.
383      *
384      * @param info   Value to run test on.
385      * @param passed Whether or not the test is expected to pass.
386      */
387     private void valueTest(final ValueBean info, final boolean passed) throws ValidatorException {
388         // Construct validator based on the loaded resources
389         // and the form key
390         final Validator validator = new Validator(resources, FORM_KEY);
391         // add the name bean to the validator as a resource
392         // for the validations to be performed on.
393         validator.setParameter(Validator.BEAN_PARAM, info);
394 
395         // Get results of the validation.
396         // throws ValidatorException,
397         // but we aren't catching for testing
398         // since no validation methods we use
399         // throw this
400         final ValidatorResults results = validator.validate();
401 
402         assertNotNull(results, "Results are null.");
403 
404         final ValidatorResult result = results.getValidatorResult("value");
405 
406         assertNotNull(result, () -> ACTION + " value ValidatorResult should not be null.");
407         assertTrue(result.containsAction(ACTION), () -> "Value " + info.getValue() + " ValidatorResult should contain the '" + ACTION + "' action.");
408         assertTrue(passed ? result.isValid(ACTION) : !result.isValid(ACTION),
409                 () -> "Value " + info.getValue() + "ValidatorResult for the '" + ACTION + "' action should have " + (passed ? "passed" : "failed") + ".");
410     }
411 }