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