1/* 2 * Copyright (C) 2010 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17package com.android.common; 18 19import android.test.suitebuilder.annotation.SmallTest; 20 21import junit.framework.TestCase; 22 23import java.util.HashMap; 24import java.util.Map; 25 26public class Rfc822ValidatorTest extends TestCase { 27 static final String[] VALID_EMAILS = new String[] { 28 "a@example.org", "b@exemple.fr", "c@d.e-f", 29 "Very.Common@example.org", 30 "john@EXAMPLE.ORG", 31 "john@a123b.c-d.dept.example.com", 32 "xn--r8jz45g@example.com", 33 "disposable.style.email.with+symbol@example.com", 34 "other.email-with-dash@example.com", 35 "!#$%&'*+-/=?^_`{}|~@example.com", // Use of allowed special characters. 36 "a@domain-label-cannot-be-longer-than-63-chars-and-this-is-maximum.example.com", 37 // Valid de facto, even if RFC doesn't allow it. 38 "a..b@example.com", ".a@example.com", "b.@example.com", 39 // Punycode is an ASCII representation of International domain names. 40 "john.doe@xn--r8jz45g.xn--zckzah", 41 "john.doe@XN--R8JZ45G.XN--ZXKZAH", 42 "xn--r8jz45g@xn--r8jz45g.XN--ZXKZAH", 43 // Quoted address. 44 // TODO(regisd) Fix Rfc822Tokenizer which loses the quotes. 45 // "\"much.more unusual\"", 46 // "\"very.unusual.@.unusual.com\"" 47 48 // Valid only in new Internalized email address. 49 "a@\u00E9.example.com", 50 //"みんな@例え.テスト", 51 "\u307F\u3093\u306A@\u4F8B\u3048.\u30C6\u30B9\u30C8", 52 // "test@test.テスト", // Unicode in TLD only. 53 "everybody@example.\u30C6\u30B9\u30C8", 54 // "test@例え.test", // Unicode in domain only. 55 "everybody@\u4F8B\u3048.test", 56 // "みんな@example.com" // Unicode in localpart only. 57 "\u307F\u3093\u306A@example.test" 58 }; 59 60 static final String[] INVALID_EMAILS = new String[] { 61 "a", "example.com", "john.example.com", // Missing at sign. 62 "a b", "a space@example.com", // Space not allowed. 63 // Invalid domain. 64 "john@example..com", "a@b", "a@-b.com", "a@b-.com", "a@b.c", 65 "a@a123456789-123456789-123456789-123456789-123456789-123456789-bcd.example.com", 66 // Invalid characters in domain as per RFC 1034 and RFC 1035, 67 // even if these characters are in RFC5322's domain production. 68 "a@d_e.fg", "a@d!e.fg", "a@d#e.fg", "a@d$e.fg", "a@d%e.fg", "a@d&e.fg", "a@d'e.fg", 69 "a@d*e.fg", "a@d+e.fg", "a@d/e.fg", "a@d=e.fg", "a@d?e.fg", "a@d^e.fg", "a@d{}e.fg", 70 "a@d|e.fg", "a@d~e.fg", 71 // The domain is too long 72 "no@domain-label-cannot-be-longer-than-63-chars-but-this-is-64-chars.com", 73 "john@doe@example.com", // @ must be unique. 74 // Incorrect double quote. 75 // TODO(regisd): Fix Rfc822tokenizer which strips the quotes 76 // "just\"not\"right@example.com", "\"just.not\\\"@example.com", 77 "this\\ still\\\"not\\\\allowed@example.com" 78 }; 79 80 @SmallTest 81 public void testEmailValidator() { 82 Rfc822Validator validator = new Rfc822Validator("gmail.com"); 83 84 for (String email : VALID_EMAILS) { 85 assertTrue(email + " should be a valid email address", validator.isValid(email)); 86 } 87 88 for (String email : INVALID_EMAILS) { 89 assertFalse(email + " should not be a valid email address", validator.isValid(email)); 90 } 91 92 Map<String, String> fixes = new HashMap<String, String>(); 93 fixes.put("a", "<a@gmail.com>"); 94 fixes.put("a b", "<ab@gmail.com>"); 95 fixes.put("a@b", "<a@b>"); 96 fixes.put("()~><@not.work", ""); 97 98 for (Map.Entry<String, String> e : fixes.entrySet()) { 99 assertEquals(e.getValue(), validator.fixText(e.getKey()).toString()); 100 } 101 } 102 103 @SmallTest 104 public void testEmailValidatorNullDomain() { 105 Rfc822Validator validator = new Rfc822Validator(null); 106 107 Map<String, String> fixes = new HashMap<String, String>(); 108 fixes.put("a", "<a>"); 109 fixes.put("a b", "<a b>"); 110 fixes.put("a@b", "<a@b>"); 111 fixes.put("a@b.com", "<a@b.com>"); // this one is correct 112 113 for (Map.Entry<String, String> e : fixes.entrySet()) { 114 assertEquals(e.getValue(), validator.fixText(e.getKey()).toString()); 115 } 116 } 117 118 @SmallTest 119 public void testEmailValidatorRemoveInvalid() { 120 Rfc822Validator validator = new Rfc822Validator("google.com"); 121 validator.setRemoveInvalid(true); 122 123 Map<String, String> fixes = new HashMap<String, String>(); 124 fixes.put("a", ""); 125 fixes.put("a b", ""); 126 fixes.put("a@b", ""); 127 fixes.put("a@b.com", "<a@b.com>"); // this one is correct 128 129 for (Map.Entry<String, String> e : fixes.entrySet()) { 130 assertEquals(e.getValue(), validator.fixText(e.getKey()).toString()); 131 } 132 } 133} 134