Skip to content

Commit 9d5ac28

Browse files
author
Eduardo Gulias Davis
authored
Fix #12 - exceptions and address literals (#15)
* fix out of bound exception when moving within the tokens list * fix missing closing bracket in literal domains * fix travis issues with xenial dist * Fix infinite loop when AT token is missing
1 parent ceb4e0a commit 9d5ac28

File tree

5 files changed

+24
-5
lines changed

5 files changed

+24
-5
lines changed

.travis.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
dist: trusty
12
language: java
23
jdk:
3-
- oraclejdk8
4+
- oraclejdk8
5+
- openjdk8

src/main/java/emailvalidator4j/lexer/EmailLexer.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,10 @@ public TokenInterface getCurrent() {
6262
*/
6363
public void next() {
6464
this.position ++;
65-
if (!this.isAtEnd()) {
65+
66+
if (this.isAtEnd()) {
67+
this.position = this.tokens.size();
68+
} else {
6669
this.current = Optional.of(this.tokens.get(this.position));
6770
this.lexedText += this.tokens.get(this.position).getText();
6871
}

src/main/java/emailvalidator4j/parser/DomainPart.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@ final class DomainPart extends Parser {
2727
@Override
2828
public void parse(String domainPart) throws InvalidEmail {
2929
this.lexer.lex(domainPart);
30+
31+
if(!this.lexer.getCurrent().equals(Tokens.AT)) {
32+
throw new ExpectedAT("Missing AT token");
33+
}
34+
3035
this.lexer.next();
3136

3237
if (this.lexer.getCurrent().equals(Tokens.DOT)) {
@@ -259,6 +264,7 @@ private void parseLiteralPart() throws InvalidEmail {
259264
}
260265

261266
} while(!this.lexer.isAtEnd() && !this.lexer.isNextToken(Tokens.CLOSEBRACKET));
267+
262268
this.warnings.add(Warnings.RFC5321_ADDRESS_LITERAL);
263269
addressLiteral = this.lexer.lexedText().replace('[', '\0').replace(']', '\0');
264270
//Remove the initial @
@@ -267,7 +273,11 @@ private void parseLiteralPart() throws InvalidEmail {
267273
} else {
268274
this.warnings.add(Warnings.RFC5322_DOMAIN_LITERAL);
269275
}
276+
270277
this.lexer.next();
278+
if (!Tokens.CLOSEBRACKET.equals(this.lexer.getCurrent())) {
279+
throw new ExpectedDTEXT("CLOSEBRACKET");
280+
}
271281
}
272282

273283
private boolean isObsoleteDTEXT() {

src/main/java/emailvalidator4j/parser/LocalPart.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public void parse(String localpart) throws InvalidEmail {
2727
throw new DotAtStart("Found DOT at start");
2828
}
2929

30-
while (!this.lexer.getCurrent().equals(Tokens.AT)) {
30+
while (!this.lexer.getCurrent().equals(Tokens.AT) && !this.lexer.isAtEnd()) {
3131
closingQuote = this.checkDoubleQuote(closingQuote);
3232
if (closingQuote && parseDQuote) {
3333
this.lexer.next();

src/test/java/emailvalidator4j/EmailValidatorTest.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@ public static Object[][] invalidEmailProvider() {
5757
{"comment(example))@example.com"},
5858
{"example@example)comment.com"},
5959
{"example@example(comment)).com"},
60+
{"example@[1.2.3.4"},
61+
{"example@[IPv6:1:2:3:4:5:6:7:8"},
62+
{"exam(ple@exam).ple"},
6063
{"example@(example))comment.com"}
6164

6265
};
@@ -66,7 +69,7 @@ public static Object[][] invalidEmailProvider() {
6669
@UseDataProvider("invalidEmailProvider")
6770
public void isInvalidEmail(String email) {
6871
EmailValidator validator = new EmailValidator();
69-
Assert.assertFalse(email + " is an invalid email", validator.isValid(email));
72+
Assert.assertFalse(email + " should be an invalid email", validator.isValid(email));
7073
}
7174

7275
@DataProvider
@@ -93,6 +96,7 @@ public static Object[][] validEmailsProvider() {
9396
{"\"\\a\"@iana.org"},
9497
{"\"test\\ test\"@iana.org"},
9598
{"\"\"@iana.org"},
99+
{"\"\"@[]"}/* kind of an edge case, valid for RFC 5322 but address literal is not for 5321 */,
96100
{String.format("\"\\%s\"@iana.org", "\"")},
97101
};
98102
}
@@ -101,7 +105,7 @@ public static Object[][] validEmailsProvider() {
101105
@UseDataProvider("validEmailsProvider")
102106
public void isValidEmail(String validEmail) {
103107
EmailValidator validator = new EmailValidator();
104-
Assert.assertTrue(validEmail + " is a valid email", validator.isValid(validEmail));
108+
Assert.assertTrue(validEmail + " should be a valid email", validator.isValid(validEmail));
105109
}
106110

107111
@Test

0 commit comments

Comments
 (0)