Skip to content

Commit

Permalink
Fix #12 - exceptions and address literals (#15)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
Eduardo Gulias Davis authored Jul 18, 2019
1 parent ceb4e0a commit 9d5ac28
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 5 deletions.
4 changes: 3 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
dist: trusty
language: java
jdk:
- oraclejdk8
- oraclejdk8
- openjdk8
5 changes: 4 additions & 1 deletion src/main/java/emailvalidator4j/lexer/EmailLexer.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,10 @@ public TokenInterface getCurrent() {
*/
public void next() {
this.position ++;
if (!this.isAtEnd()) {

if (this.isAtEnd()) {
this.position = this.tokens.size();
} else {
this.current = Optional.of(this.tokens.get(this.position));
this.lexedText += this.tokens.get(this.position).getText();
}
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/emailvalidator4j/parser/DomainPart.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ final class DomainPart extends Parser {
@Override
public void parse(String domainPart) throws InvalidEmail {
this.lexer.lex(domainPart);

if(!this.lexer.getCurrent().equals(Tokens.AT)) {
throw new ExpectedAT("Missing AT token");
}

this.lexer.next();

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

} while(!this.lexer.isAtEnd() && !this.lexer.isNextToken(Tokens.CLOSEBRACKET));

this.warnings.add(Warnings.RFC5321_ADDRESS_LITERAL);
addressLiteral = this.lexer.lexedText().replace('[', '\0').replace(']', '\0');
//Remove the initial @
Expand All @@ -267,7 +273,11 @@ private void parseLiteralPart() throws InvalidEmail {
} else {
this.warnings.add(Warnings.RFC5322_DOMAIN_LITERAL);
}

this.lexer.next();
if (!Tokens.CLOSEBRACKET.equals(this.lexer.getCurrent())) {
throw new ExpectedDTEXT("CLOSEBRACKET");
}
}

private boolean isObsoleteDTEXT() {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/emailvalidator4j/parser/LocalPart.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public void parse(String localpart) throws InvalidEmail {
throw new DotAtStart("Found DOT at start");
}

while (!this.lexer.getCurrent().equals(Tokens.AT)) {
while (!this.lexer.getCurrent().equals(Tokens.AT) && !this.lexer.isAtEnd()) {
closingQuote = this.checkDoubleQuote(closingQuote);
if (closingQuote && parseDQuote) {
this.lexer.next();
Expand Down
8 changes: 6 additions & 2 deletions src/test/java/emailvalidator4j/EmailValidatorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ public static Object[][] invalidEmailProvider() {
{"comment(example))@example.com"},
{"example@example)comment.com"},
{"example@example(comment)).com"},
{"example@[1.2.3.4"},
{"example@[IPv6:1:2:3:4:5:6:7:8"},
{"exam(ple@exam).ple"},
{"example@(example))comment.com"}

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

@DataProvider
Expand All @@ -93,6 +96,7 @@ public static Object[][] validEmailsProvider() {
{"\"\\a\"@iana.org"},
{"\"test\\ test\"@iana.org"},
{"\"\"@iana.org"},
{"\"\"@[]"}/* kind of an edge case, valid for RFC 5322 but address literal is not for 5321 */,
{String.format("\"\\%s\"@iana.org", "\"")},
};
}
Expand All @@ -101,7 +105,7 @@ public static Object[][] validEmailsProvider() {
@UseDataProvider("validEmailsProvider")
public void isValidEmail(String validEmail) {
EmailValidator validator = new EmailValidator();
Assert.assertTrue(validEmail + " is a valid email", validator.isValid(validEmail));
Assert.assertTrue(validEmail + " should be a valid email", validator.isValid(validEmail));
}

@Test
Expand Down

0 comments on commit 9d5ac28

Please sign in to comment.