Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 1 | # Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 | # Use of this source code is governed by a BSD-style |
| 3 | # license that can be found in the LICENSE file or at |
| 4 | # https://developers.google.com/open-source/licenses/bsd |
| 5 | |
| 6 | """This file provides unit tests for Validate functions.""" |
| 7 | from __future__ import print_function |
| 8 | from __future__ import division |
| 9 | from __future__ import absolute_import |
| 10 | |
| 11 | import unittest |
| 12 | |
| 13 | from framework import validate |
| 14 | |
| 15 | |
| 16 | class ValidateUnitTest(unittest.TestCase): |
| 17 | """Set of unit tests for validation functions.""" |
| 18 | |
| 19 | GOOD_EMAIL_ADDRESSES = [ |
| 20 | 'user@example.com', |
| 21 | 'user@e.com', |
| 22 | 'user+tag@example.com', |
| 23 | 'u.ser@example.com', |
| 24 | 'us.er@example.com', |
| 25 | 'u.s.e.r@example.com', |
| 26 | 'user@ex-ample.com', |
| 27 | 'user@ex.ample.com', |
| 28 | 'user@e.x.ample.com', |
| 29 | 'user@exampl.e.com', |
| 30 | 'user@e-x-ample.com', |
| 31 | 'user@e-x-a-m-p-l-e.com', |
| 32 | 'user@e-x.am-ple.com', |
| 33 | 'user@e--xample.com', |
| 34 | ] |
| 35 | |
| 36 | BAD_EMAIL_ADDRESSES = [ |
| 37 | ' leading.whitespace@example.com', |
| 38 | 'trailing.whitespace@example.com ', |
| 39 | '(paren.quoted@example.com)', |
| 40 | '<angle.quoted@example.com>', |
| 41 | 'trailing.@example.com', |
| 42 | 'trailing.dot.@example.com', |
| 43 | '.leading@example.com', |
| 44 | '.leading.dot@example.com', |
| 45 | 'user@example.com.', |
| 46 | 'us..er@example.com', |
| 47 | 'user@ex..ample.com', |
| 48 | 'user@example..com', |
| 49 | 'user@ex-.ample.com', |
| 50 | 'user@-example.com', |
| 51 | 'user@.example.com', |
| 52 | 'user@example-.com', |
| 53 | 'user@example', |
| 54 | 'user@example.', |
| 55 | 'user@example.c', |
| 56 | 'user@example.comcomcomc', |
| 57 | 'user@example.co-m', |
| 58 | 'user@exa_mple.com', |
| 59 | 'user@exa-_mple.com', |
| 60 | 'user@example.c0m', |
| 61 | ] |
| 62 | |
| 63 | def testIsValidEmail(self): |
| 64 | """Tests the Email validator class.""" |
| 65 | for email in self.GOOD_EMAIL_ADDRESSES: |
| 66 | self.assertTrue(validate.IsValidEmail(email), msg='Rejected:%r' % email) |
| 67 | |
| 68 | for email in self.BAD_EMAIL_ADDRESSES: |
| 69 | self.assertFalse(validate.IsValidEmail(email), msg='Accepted:%r' % email) |
| 70 | |
| 71 | def testIsValidMailTo(self): |
| 72 | for email in self.GOOD_EMAIL_ADDRESSES: |
| 73 | self.assertTrue( |
| 74 | validate.IsValidMailTo('mailto:' + email), |
| 75 | msg='Rejected:%r' % ('mailto:' + email)) |
| 76 | |
| 77 | for email in self.BAD_EMAIL_ADDRESSES: |
| 78 | self.assertFalse( |
| 79 | validate.IsValidMailTo('mailto:' + email), |
| 80 | msg='Accepted:%r' % ('mailto:' + email)) |
| 81 | |
| 82 | GOOD_URLS = [ |
| 83 | 'http://google.com', |
| 84 | 'http://maps.google.com/', |
| 85 | 'https://secure.protocol.com', |
| 86 | 'https://dash-domain.com', |
| 87 | 'http://www.google.com/search?q=foo&hl=en', |
| 88 | 'https://a.very.long.domain.name.net/with/a/long/path/inf0/too', |
| 89 | 'http://funny.ws/', |
| 90 | 'http://we.love.anchors.info/page.html#anchor', |
| 91 | 'http://redundant-slashes.com//in/path//info', |
| 92 | 'http://trailingslashe.com/in/path/info/', |
| 93 | 'http://domain.with.port.com:8080', |
| 94 | 'http://domain.with.port.com:8080/path/info', |
| 95 | 'ftp://ftp.gnu.org', |
| 96 | 'ftp://some.server.some.place.com', |
| 97 | 'http://b/123456', |
| 98 | 'http://cl/123456/', |
| 99 | ] |
| 100 | |
| 101 | BAD_URLS = [ |
| 102 | ' http://leading.whitespace.com', |
| 103 | 'http://trailing.domain.whitespace.com ', |
| 104 | 'http://trailing.whitespace.com/after/path/info ', |
| 105 | 'http://underscore_domain.com/', |
| 106 | 'http://space in domain.com', |
| 107 | 'http://user@example.com', # standard, but we purposely don't accept it. |
| 108 | 'http://user:pass@ex.com', # standard, but we purposely don't accept it. |
| 109 | 'http://:password@ex.com', # standard, but we purposely don't accept it. |
| 110 | 'missing-http.com', |
| 111 | 'http:missing-slashes.com', |
| 112 | 'http:/only-one-slash.com', |
| 113 | 'http://trailing.dot.', |
| 114 | 'mailto:bad.scheme', |
| 115 | 'javascript:attempt-to-inject', |
| 116 | 'http://short-with-no-final-slash', |
| 117 | 'http:///', |
| 118 | 'http:///no.host.name', |
| 119 | 'http://:8080/', |
| 120 | 'http://badport.com:808a0/ ', |
| 121 | ] |
| 122 | |
| 123 | def testURL(self): |
| 124 | for url in self.GOOD_URLS: |
| 125 | self.assertTrue(validate.IsValidURL(url), msg='Rejected:%r' % url) |
| 126 | |
| 127 | for url in self.BAD_URLS: |
| 128 | self.assertFalse(validate.IsValidURL(url), msg='Accepted:%r' % url) |