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.Ignore;
26 import org.junit.jupiter.api.BeforeEach;
27 import org.junit.jupiter.api.Test;
28 import org.xml.sax.SAXException;
29
30
31
32
33
34
35
36 @Deprecated
37 public class EmailTest extends AbstractCommonTest {
38
39
40
41
42 protected static final String FORM_KEY = "emailForm";
43
44
45
46
47 protected static final String ACTION = "email";
48
49
50
51
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
89
90 @BeforeEach
91 protected void setUp() throws IOException, SAXException {
92 loadResources("EmailTest-config.xml");
93 }
94
95
96
97
98 @Test
99 public void testEmail() throws ValidatorException {
100
101 final ValueBean info = new ValueBean();
102
103 info.setValue("jsmith@apache.org");
104 valueTest(info, true);
105 }
106
107
108
109
110 @Test
111 public void testEmailAtTLD() throws ValidatorException {
112
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
126
127 @Test
128 public void testEmailExtension() throws ValidatorException {
129
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
159
160
161
162
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
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
187
188
189
190
191
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
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
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
266
267 @Test
268 public void testEmailWithBogusCharacter() throws ValidatorException {
269
270 final ValueBean info = new ValueBean();
271
272 info.setValue("andy.noble@\u008fdata-workshop.com");
273 valueTest(info, false);
274
275
276 info.setValue("andy.o'reilly@data-workshop.com");
277 valueTest(info, true);
278
279
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
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
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
316
317
318
319 @Test
320 public void testEmailWithDash() throws ValidatorException {
321
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
338
339 @Test
340 public void testEmailWithDotEnd() throws ValidatorException {
341
342 final ValueBean info = new ValueBean();
343
344 info.setValue("andy.noble@data-workshop.com.");
345 valueTest(info, false);
346
347 }
348
349
350
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
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
386
387
388
389
390 private void valueTest(final ValueBean info, final boolean passed) throws ValidatorException {
391
392
393 final Validator validator = new Validator(resources, FORM_KEY);
394
395
396 validator.setParameter(Validator.BEAN_PARAM, info);
397
398
399
400
401
402
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 }