1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
31
32
33
34
35 @Deprecated
36 public class EmailTest extends AbstractCommonTest {
37
38
39
40
41 protected static String FORM_KEY = "emailForm";
42
43
44
45
46 protected static String ACTION = "email";
47
48
49
50
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
88
89
90
91
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
103
104
105
106
107
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
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
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
181
182 @BeforeEach
183 protected void setUp() throws IOException, SAXException {
184 loadResources("EmailTest-config.xml");
185 }
186
187
188
189
190 @Test
191 public void testEmail() throws ValidatorException {
192
193 final ValueBean info = new ValueBean();
194
195 info.setValue("jsmith@apache.org");
196 valueTest(info, true);
197 }
198
199
200
201
202 @Test
203 public void testEmailAtTLD() throws ValidatorException {
204
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
218
219 @Test
220 public void testEmailExtension() throws ValidatorException {
221
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
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
263
264 @Test
265 public void testEmailWithBogusCharacter() throws ValidatorException {
266
267 final ValueBean info = new ValueBean();
268
269 info.setValue("andy.noble@\u008fdata-workshop.com");
270 valueTest(info, false);
271
272
273 info.setValue("andy.o'reilly@data-workshop.com");
274 valueTest(info, true);
275
276
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
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
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
313
314
315
316 @Test
317 public void testEmailWithDash() throws ValidatorException {
318
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
335
336 @Test
337 public void testEmailWithDotEnd() throws ValidatorException {
338
339 final ValueBean info = new ValueBean();
340
341 info.setValue("andy.noble@data-workshop.com.");
342 valueTest(info, false);
343
344 }
345
346
347
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
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
383
384
385
386
387 private void valueTest(final ValueBean info, final boolean passed) throws ValidatorException {
388
389
390 final Validator validator = new Validator(resources, FORM_KEY);
391
392
393 validator.setParameter(Validator.BEAN_PARAM, info);
394
395
396
397
398
399
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 }