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.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   * JUnit test case demonstrating InternetAddress validation.
32   */
33  public class InvalidInternetAddressTest extends AbstractEmailTest {
34      /** */
35      private static final String VALID_QUOTED_EMAIL = "\"John O'Groats\"@domain.com";
36  
37      // @formatter:off
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              // "local\\name@domain.com", -- works for javamail-1.4.4
50              // "local\"name@domain.com", -- works for javamail-1.4.4
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              // "local.name@domain[com", -- works for javamail-1.5.5
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      // @formatter:on
71  
72      @Test
73      public void testStrictConstructor() throws Exception {
74          // Prove InternetAddress constructor is throwing exception.
75  
76          // test Invalid Email addresses
77          // @formatter:off
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          // @formatter:on
82  
83          // test valid 'quoted' Email addresses
84          // Create Internet Address using "strict" constructor
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          // Prove InternetAddress constructor isn't throwing exception and
91          // the validate() method is
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              // N.B. validate() doesn't check addresses containing quotes or '['
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         // test valid 'quoted' Email addresses
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         // Prove InternetAddress constructor isn't throwing exception and
121         // the validate() method is
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             // N.B. validate() doesn't check addresses containing quotes or '['
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         // test valid 'quoted' Email addresses
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 }