1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.mail2.javax;
18
19 import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
20 import static org.junit.jupiter.api.Assertions.assertThrows;
21 import static org.junit.jupiter.api.Assertions.fail;
22
23 import java.nio.charset.StandardCharsets;
24 import java.util.stream.IntStream;
25
26 import javax.mail.internet.InternetAddress;
27
28 import org.junit.jupiter.api.Test;
29
30
31
32
33 public class InvalidInternetAddressTest extends AbstractEmailTest {
34
35 private static final String VALID_QUOTED_EMAIL = "\"John O'Groats\"@domain.com";
36
37
38 private static final String[] ARR_INVALID_EMAILS = {
39 "local name@domain.com",
40 "local(name@domain.com",
41 "local)name@domain.com",
42 "local<name@domain.com",
43 "local>name@domain.com",
44 "local,name@domain.com",
45 "local;name@domain.com",
46 "local:name@domain.com",
47 "local[name@domain.com",
48 "local]name@domain.com",
49
50
51 "local\tname@domain.com",
52 "local\nname@domain.com",
53 "local\rname@domain.com",
54 "local.name@domain com",
55 "local.name@domain(com",
56 "local.name@domain)com",
57 "local.name@domain<com",
58 "local.name@domain>com",
59 "local.name@domain,com",
60 "local.name@domain;com",
61 "local.name@domain:com",
62
63 "local.name@domain]com",
64 "local.name@domain\\com",
65 "local.name@domain\tcom",
66 "local.name@domain\ncom",
67 "local.name@domain\rcom",
68 "local.name@",
69 "@domain.com" };
70
71
72 @Test
73 public void testStrictConstructor() throws Exception {
74
75
76
77
78 IntStream.range(0, ARR_INVALID_EMAILS.length).forEach(i -> assertThrows(Exception.class,
79 () -> new InternetAddress(ARR_INVALID_EMAILS[i]),
80 () -> "Strict " + i + " passed: " + ARR_INVALID_EMAILS[i]));
81
82
83
84
85 assertDoesNotThrow(() -> new InternetAddress(VALID_QUOTED_EMAIL), () -> "Valid Quoted Email failed: " + VALID_QUOTED_EMAIL);
86 }
87
88 @Test
89 public void testValidateMethod() throws Exception {
90
91
92
93 for (int i = 0; i < ARR_INVALID_EMAILS.length; i++) {
94
95 final InternetAddress address = new InternetAddress(ARR_INVALID_EMAILS[i], "Joe");
96
97
98 final boolean quoted = ARR_INVALID_EMAILS[i].contains("\"");
99 final int atIndex = ARR_INVALID_EMAILS[i].indexOf("@");
100 final boolean domainBracket = atIndex >= 0 && ARR_INVALID_EMAILS[i].indexOf("[", atIndex) >= 0;
101 try {
102 address.validate();
103
104 if (!(quoted || domainBracket)) {
105 fail("Validate " + i + " passed: " + ARR_INVALID_EMAILS[i]);
106 }
107 } catch (final Exception ex) {
108 if (quoted || domainBracket) {
109 fail("Validate " + i + " failed: " + ARR_INVALID_EMAILS[i] + " - " + ex.getMessage());
110 }
111 }
112 }
113
114
115 assertDoesNotThrow(() -> new InternetAddress(VALID_QUOTED_EMAIL, "Joe").validate(), () -> "Valid Quoted Email failed: " + VALID_QUOTED_EMAIL);
116 }
117
118 @Test
119 public void testValidateMethodCharset() throws Exception {
120
121
122
123 for (int i = 0; i < ARR_INVALID_EMAILS.length; i++) {
124
125 final InternetAddress address = new InternetAddress(ARR_INVALID_EMAILS[i], "Joe", StandardCharsets.UTF_8.name());
126
127
128 final boolean quoted = ARR_INVALID_EMAILS[i].contains("\"");
129 final int atIndex = ARR_INVALID_EMAILS[i].indexOf("@");
130 final boolean domainBracket = atIndex >= 0 && ARR_INVALID_EMAILS[i].indexOf("[", atIndex) >= 0;
131
132 try {
133 address.validate();
134 if (!(quoted || domainBracket)) {
135 fail("Validate " + i + " passed: " + ARR_INVALID_EMAILS[i]);
136 }
137
138 } catch (final Exception ex) {
139 if (quoted || domainBracket) {
140 fail("Validate " + i + " failed: " + ARR_INVALID_EMAILS[i] + " - " + ex.getMessage());
141 }
142 }
143
144 }
145
146
147 assertDoesNotThrow(() -> new InternetAddress(VALID_QUOTED_EMAIL, "Joe", StandardCharsets.UTF_8.name()).validate(),
148 () -> "Valid Quoted Email failed: " + VALID_QUOTED_EMAIL);
149
150 }
151
152 }