|
| 1 | +package eu.pretix.libpretixsync.utils |
| 2 | + |
| 3 | +import org.junit.Assert.assertEquals |
| 4 | +import org.junit.Test |
| 5 | +import org.junit.runner.RunWith |
| 6 | +import org.junit.runners.Parameterized |
| 7 | + |
| 8 | +@RunWith(Parameterized::class) |
| 9 | +class EmailValidatorTests(private val input: String, private val result: Boolean) { |
| 10 | + |
| 11 | + @Test |
| 12 | + fun test() { |
| 13 | + assertEquals(input, result, EmailValidator().isValidEmail(input)) |
| 14 | + } |
| 15 | + |
| 16 | + companion object { |
| 17 | + |
| 18 | + @JvmStatic |
| 19 | + @Parameterized.Parameters |
| 20 | + fun data() = listOf( |
| 21 | + // These are from https://github.com/django/django/blob/1b5338d03ecc962af8ab4678426bc60b0672b8dd/tests/validators/tests.py#L279 |
| 22 | + arrayOf("email@here.com", true), |
| 23 | + arrayOf("weirder-email@here.and.there.com", true), |
| 24 | + arrayOf("example@valid-----hyphens.com", true), |
| 25 | + arrayOf("example@valid-with-hyphens.com", true), |
| 26 | + arrayOf("test@domain.with.idn.tld.उदाहरण.परीक्षा", true), |
| 27 | + arrayOf( |
| 28 | + "email@localhost", |
| 29 | + false |
| 30 | + ), // difference to django since we did not implement the whitelist |
| 31 | + arrayOf("\"test@test\"@example.com", true), |
| 32 | + arrayOf("example@atm.${"a".repeat(63)}", true), |
| 33 | + arrayOf("example@${"a".repeat(63)}.atm", true), |
| 34 | + arrayOf("example@${"a".repeat(63)}.${"b".repeat(10)}.atm", true), |
| 35 | + arrayOf("example@atm.${"a".repeat(64)}", false), |
| 36 | + arrayOf("example@${"b".repeat(64)}.atm.${"a".repeat(63)}", false), |
| 37 | + arrayOf("example@${("a".repeat(63) + ".").repeat(100)}com", false), |
| 38 | + arrayOf("", false), |
| 39 | + arrayOf("abc", false), |
| 40 | + arrayOf("abc@", false), |
| 41 | + arrayOf("abc@bar", false), |
| 42 | + arrayOf("a @x.cz", false), |
| 43 | + arrayOf("abc@.com", false), |
| 44 | + arrayOf("something@@somewhere.com", false), |
| 45 | + arrayOf("example@invalid-.com", false), |
| 46 | + arrayOf("example@-invalid.com", false), |
| 47 | + arrayOf("example@invalid.com-", false), |
| 48 | + arrayOf("example@inv-.alid-.com", false), |
| 49 | + arrayOf("example@inv-.-alid.com", false), |
| 50 | + arrayOf("test@example.com\n\n<script src=\"x.js\">", false), |
| 51 | + // Quoted-string format (CR not allowed) |
| 52 | + arrayOf("\"\\\t\"@here.com", true), |
| 53 | + arrayOf("\"\\\r\"@here.com", false), |
| 54 | + arrayOf("trailingdot@shouldfail.com.", false), |
| 55 | + // Max length of domain name labels is 63 characters per RFC 1034. |
| 56 | + arrayOf("a@${"a".repeat(63)}.us", true), |
| 57 | + arrayOf("a@${"a".repeat(64)}.us", false), |
| 58 | + // Trailing newlines in username or domain not allowed |
| 59 | + arrayOf("a@b.com\n", false), |
| 60 | + arrayOf("a\n@b.com", false), |
| 61 | + arrayOf("\"test@test\"\n@example.com", false), |
| 62 | + |
| 63 | + // We are even stricter than Django and do not allow any IP addresses |
| 64 | + arrayOf("email@[127.0.0.1]", false), |
| 65 | + arrayOf("email@[2001:dB8::1]", false), |
| 66 | + arrayOf("email@[2001:dB8:0:0:0:0:0:1]", false), |
| 67 | + arrayOf("email@[::fffF:127.0.0.1]", false), |
| 68 | + arrayOf("email@127.0.0.1", false), |
| 69 | + arrayOf("email@[127.0.0.256]", false), |
| 70 | + arrayOf("email@[2001:db8::12345]", false), |
| 71 | + arrayOf("email@[2001:db8:0:0:0:0:1]", false), |
| 72 | + arrayOf("email@[::ffff:127.0.0.256]", false), |
| 73 | + arrayOf("email@[2001:dg8::1]", false), |
| 74 | + arrayOf("email@[2001:dG8:0:0:0:0:0:1]", false), |
| 75 | + arrayOf("email@[::fTzF:127.0.0.1]", false), |
| 76 | + arrayOf("a@[127.0.0.1]\n", false), |
| 77 | + |
| 78 | + // Real-world find |
| 79 | + arrayOf("foobar@example.com.k", false), |
| 80 | + ) |
| 81 | + } |
| 82 | +} |
0 commit comments