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