blob: 9716122ed0e1ee354268f440f6ed25e72d56a71e [file] [log] [blame]
Adrià Vilanova Martínezf19ea432024-01-23 20:20:52 +01001# Copyright 2016 The Chromium Authors
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
Copybara854996b2021-09-07 19:36:02 +00004
5"""This file provides unit tests for Validate functions."""
6from __future__ import print_function
7from __future__ import division
8from __future__ import absolute_import
9
10import unittest
11
12from framework import validate
13
14
15class ValidateUnitTest(unittest.TestCase):
16 """Set of unit tests for validation functions."""
17
18 GOOD_EMAIL_ADDRESSES = [
19 'user@example.com',
20 'user@e.com',
21 'user+tag@example.com',
22 'u.ser@example.com',
23 'us.er@example.com',
24 'u.s.e.r@example.com',
25 'user@ex-ample.com',
26 'user@ex.ample.com',
27 'user@e.x.ample.com',
28 'user@exampl.e.com',
29 'user@e-x-ample.com',
30 'user@e-x-a-m-p-l-e.com',
31 'user@e-x.am-ple.com',
32 'user@e--xample.com',
Adrià Vilanova Martínezf19ea432024-01-23 20:20:52 +010033 'user@example.c',
34 'user@example.comcomcomc',
35 'user@example.co-m',
36 'user@example.c0m',
37 'very-long-email-address@very-long-domain.iam.abcdefghijklmnopqrstuvwxyz',
Copybara854996b2021-09-07 19:36:02 +000038 ]
39
40 BAD_EMAIL_ADDRESSES = [
41 ' leading.whitespace@example.com',
42 'trailing.whitespace@example.com ',
43 '(paren.quoted@example.com)',
44 '<angle.quoted@example.com>',
45 'trailing.@example.com',
46 'trailing.dot.@example.com',
47 '.leading@example.com',
48 '.leading.dot@example.com',
49 'user@example.com.',
50 'us..er@example.com',
51 'user@ex..ample.com',
52 'user@example..com',
53 'user@ex-.ample.com',
54 'user@-example.com',
55 'user@.example.com',
56 'user@example-.com',
57 'user@example',
58 'user@example.',
Copybara854996b2021-09-07 19:36:02 +000059 'user@exa_mple.com',
60 'user@exa-_mple.com',
Copybara854996b2021-09-07 19:36:02 +000061 ]
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)