Package lepl :: Package apps :: Package _test :: Module rfc3696
[hide private]
[frames] | no frames]

Source Code for Module lepl.apps._test.rfc3696

  1   
  2  # The contents of this file are subject to the Mozilla Public License 
  3  # (MPL) Version 1.1 (the "License"); you may not use this file except 
  4  # in compliance with the License. You may obtain a copy of the License 
  5  # at http://www.mozilla.org/MPL/ 
  6  # 
  7  # Software distributed under the License is distributed on an "AS IS" 
  8  # basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See 
  9  # the License for the specific language governing rights and 
 10  # limitations under the License. 
 11  # 
 12  # The Original Code is LEPL (http://www.acooke.org/lepl) 
 13  # The Initial Developer of the Original Code is Andrew Cooke. 
 14  # Portions created by the Initial Developer are Copyright (C) 2009-2010 
 15  # Andrew Cooke (andrew@acooke.org). All Rights Reserved. 
 16  # 
 17  # Alternatively, the contents of this file may be used under the terms 
 18  # of the LGPL license (the GNU Lesser General Public License, 
 19  # http://www.gnu.org/licenses/lgpl.html), in which case the provisions 
 20  # of the LGPL License are applicable instead of those above. 
 21  # 
 22  # If you wish to allow use of your version of this file only under the 
 23  # terms of the LGPL License and not to allow others to use your version 
 24  # of this file under the MPL, indicate your decision by deleting the 
 25  # provisions above and replace them with the notice and other provisions 
 26  # required by the LGPL License.  If you do not delete the provisions 
 27  # above, a recipient may use your version of this file under either the 
 28  # MPL or the LGPL License. 
 29   
 30  ''' 
 31  Tests for the lepl.apps.rfc3696 module. 
 32  ''' 
 33   
 34  from logging import basicConfig, DEBUG 
 35   
 36  from lepl import * 
 37  from lepl._test.base import BaseTest 
 38  from lepl.apps.rfc3696 import _PreferredFullyQualifiedDnsName, _EmailLocalPart,\ 
 39      _Email, _HttpUrl, MailToUrl, HttpUrl, Email, _IpV4Address, _Ipv6Address 
 40   
 41   
42 -class DnsNameTest(BaseTest):
43
44 - def test_dns_name_matcher(self):
45 46 name = _PreferredFullyQualifiedDnsName() & Eos() 47 48 self.assert_fail('', name) 49 self.assert_fail('a', name) 50 self.assert_fail('12.34', name) 51 self.assert_fail('a.b.', name) 52 self.assert_fail(' a.b', name) 53 self.assert_fail('a.b ', name) 54 self.assert_fail('a._.', name) 55 self.assert_fail('a.-b.c', name) 56 self.assert_fail('a.b-.c', name) 57 self.assert_fail('a.b.c.123', name) 58 59 self.assert_literal('a.b.123.c', name) 60 self.assert_literal('a.b-c.d', name) 61 self.assert_literal('a.b--c.d', name) 62 self.assert_literal('acooke.org', name) 63 self.assert_literal('EXAMPLE.COM', name) 64 self.assert_literal('example.a23', name) 65 self.assert_literal('example.12c', name)
66 67
68 -class IpV4AddressTest(BaseTest):
69
70 - def test_ipv4_address(self):
71 72 address = _IpV4Address() & Eos() 73 74 self.assert_literal('1.2.3.4', address) 75 self.assert_literal('255.255.255.255', address) 76 self.assert_literal('0.0.0.0', address) 77 78 self.assert_fail('1.2.3', address) 79 self.assert_fail('1.2.3.', address) 80 self.assert_fail('1.256.3.4', address) 81 self.assert_fail('1.a.3.4', address) 82 self.assert_fail('1.-1.3.4', address)
83 84
85 -class IpV6AddressTest(BaseTest):
86
87 - def test_ipv6_address(self):
88 89 address = _Ipv6Address() & Eos() 90 91 self.assert_literal('FEDC:BA98:7654:3210:FEDC:BA98:7654:3210', address) 92 self.assert_literal('1080:0:0:0:8:800:200C:417A', address) 93 self.assert_literal('FF01:0:0:0:0:0:0:101', address) 94 self.assert_literal('0:0:0:0:0:0:0:1', address) 95 self.assert_literal('0:0:0:0:0:0:0:0', address) 96 self.assert_literal('1080::8:800:200C:417A', address) 97 self.assert_literal('FF01::101', address) 98 self.assert_literal('::1', address) 99 self.assert_literal('::', address) 100 self.assert_literal('0:0:0:0:0:0:13.1.68.3', address) 101 self.assert_literal('0:0:0:0:0:FFFF:129.144.52.38', address) 102 self.assert_literal('::13.1.68.3', address) 103 self.assert_literal('::FFFF:129.144.52.38', address) 104 105 self.assert_fail('1:2:3:4:5:6:7', address) 106 self.assert_fail('1:2:3:4:5:6:7:8:9', address) 107 self.assert_fail('::1:2:3:4:5:6:7:8', address) 108 self.assert_fail(':1::2:3:4:5:6:7:8', address) 109 self.assert_fail(':1:2:3:4:5:6:7:8::', address) 110 self.assert_fail('1:2:3:4:5:1.2.3.4', address) 111 self.assert_fail('1:2:3:4:5.6.7:1.2.3.4', address) 112 self.assert_fail('::1:2:3:4:5:6:1.2.3.4', address) 113 self.assert_fail('1::2:3:4:5:6:1.2.3.4', address) 114 self.assert_fail('1:2:3:4:5:6::1.2.3.4', address)
115 116
117 -class _EmailLocalPartTest(BaseTest):
118
120 121 local = _EmailLocalPart() & Eos() 122 123 self.assert_fail('', local) 124 self.assert_fail('""', local) 125 self.assert_fail('"unmatched', local) 126 self.assert_fail('unmatched"', local) 127 self.assert_fail(' ', local) 128 self.assert_fail('a b', local) 129 130 self.assert_literal(r'andrew', local) 131 self.assert_literal(r'Abc\@def', local) 132 self.assert_literal(r'Fred\ Bloggs', local) 133 self.assert_literal(r'Joe.\\Blow', local) 134 self.assert_literal(r'"Abc@def"', local) 135 self.assert_literal(r'"Fred Bloggs"', local) 136 self.assert_literal(r'user+mailbox', local) 137 self.assert_literal(r'customer/department=shipping', local) 138 self.assert_literal(r'$A12345', local) 139 self.assert_literal(r'!def!xyz%abc', local) 140 self.assert_literal(r'_somename', local)
141 142
143 -class _EmailTest(BaseTest):
144
145 - def test_email_matcher(self):
146 147 email = _Email() & Eos() 148 149 self.assert_literal(r'andrew@acooke.org', email) 150 self.assert_literal(r'Abc\@def@example.com', email) 151 self.assert_literal(r'Fred\ Bloggs@example.com', email) 152 self.assert_literal(r'Joe.\\Blow@example.com', email) 153 self.assert_literal(r'"Abc@def"@example.com', email) 154 self.assert_literal(r'"Fred Bloggs"@example.com', email) 155 self.assert_literal(r'user+mailbox@example.com', email) 156 self.assert_literal(r'customer/department=shipping@example.com', email) 157 self.assert_literal(r'$A12345@example.com', email) 158 self.assert_literal(r'!def!xyz%abc@example.com', email) 159 self.assert_literal(r'_somename@example.com', email)
160
161 - def test_email(self):
162 163 email = Email() 164 165 assert email(r'andrew@acooke.org',) 166 assert email(r'Abc\@def@example.com',) 167 assert email(r'Fred\ Bloggs@example.com',) 168 assert email(r'Joe.\\Blow@example.com',) 169 assert email(r'"Abc@def"@example.com',) 170 assert email(r'"Fred Bloggs"@example.com',) 171 assert email(r'user+mailbox@example.com',) 172 assert email(r'customer/department=shipping@example.com',) 173 assert email(r'$A12345@example.com',) 174 assert email(r'!def!xyz%abc@example.com',) 175 assert email(r'_somename@example.com',) 176 177 addresses = ['', 'a', '12.34', 'a.b.', ' a.b', 'a.b ', 'a._.', 178 'a.-b.c', 'a.b-.c', 'a.b.c.123'] 179 names = ['', '""', '"unmatched', 'unmatched"', ' ', 'a b'] 180 for name in names: 181 for address in addresses: 182 bad = name + '@' + address 183 assert not email(bad), bad
184 185
186 -class HttpUrlTest(BaseTest):
187
188 - def test_http_matcher(self):
189 #basicConfig(level=DEBUG) 190 191 http = _HttpUrl() & Eos() 192 http.config.compile_to_re() 193 #print(http.get_parse().matcher.tree()) 194 195 self.assert_literal(r'http://www.acooke.org', http) 196 self.assert_literal(r'http://www.acooke.org/', http) 197 self.assert_literal(r'http://www.acooke.org:80', http) 198 self.assert_literal(r'http://www.acooke.org:80/', http) 199 self.assert_literal(r'http://www.acooke.org/andrew', http) 200 self.assert_literal(r'http://www.acooke.org:80/andrew', http) 201 self.assert_literal(r'http://www.acooke.org/andrew/', http) 202 self.assert_literal(r'http://www.acooke.org:80/andrew/', http) 203 self.assert_literal(r'http://www.acooke.org/?foo', http) 204 self.assert_literal(r'http://www.acooke.org:80/?foo', http) 205 self.assert_literal(r'http://www.acooke.org/#bar', http) 206 self.assert_literal(r'http://www.acooke.org:80/#bar', http) 207 self.assert_literal(r'http://www.acooke.org/andrew?foo', http) 208 self.assert_literal(r'http://www.acooke.org:80/andrew?foo', http) 209 self.assert_literal(r'http://www.acooke.org/andrew/?foo', http) 210 self.assert_literal(r'http://www.acooke.org:80/andrew/?foo', http) 211 self.assert_literal(r'http://www.acooke.org/andrew#bar', http) 212 self.assert_literal(r'http://www.acooke.org:80/andrew#bar', http) 213 self.assert_literal(r'http://www.acooke.org/andrew/#bar', http) 214 self.assert_literal(r'http://www.acooke.org:80/andrew/#bar', http) 215 self.assert_literal(r'http://www.acooke.org/andrew?foo#bar', http) 216 self.assert_literal(r'http://www.acooke.org:80/andrew?foo#bar', http) 217 self.assert_literal(r'http://www.acooke.org/andrew/?foo#bar', http) 218 self.assert_literal(r'http://www.acooke.org:80/andrew/?foo#bar', http) 219 220 self.assert_fail(r'http://www.acooke.org:80/andrew/?foo#bar ', http) 221 self.assert_fail(r'http://www.acooke.org:80/andrew/?foo#bar baz', http)
222
223 - def test_http(self):
224 225 httpUrl = HttpUrl() 226 227 assert httpUrl(r'http://www.acooke.org') 228 assert httpUrl(r'http://www.acooke.org/') 229 assert httpUrl(r'http://www.acooke.org:80') 230 assert httpUrl(r'http://www.acooke.org:80/') 231 assert httpUrl(r'http://www.acooke.org/andrew') 232 assert httpUrl(r'http://www.acooke.org:80/andrew') 233 assert httpUrl(r'http://www.acooke.org/andrew/') 234 assert httpUrl(r'http://www.acooke.org:80/andrew/') 235 assert httpUrl(r'http://www.acooke.org/?foo') 236 assert httpUrl(r'http://www.acooke.org:80/?foo') 237 assert httpUrl(r'http://www.acooke.org/#bar') 238 assert httpUrl(r'http://www.acooke.org:80/#bar') 239 assert httpUrl(r'http://www.acooke.org/andrew?foo') 240 assert httpUrl(r'http://www.acooke.org:80/andrew?foo') 241 assert httpUrl(r'http://www.acooke.org/andrew/?foo') 242 assert httpUrl(r'http://www.acooke.org:80/andrew/?foo') 243 assert httpUrl(r'http://www.acooke.org/andrew#bar') 244 assert httpUrl(r'http://www.acooke.org:80/andrew#bar') 245 assert httpUrl(r'http://www.acooke.org/andrew/#bar') 246 assert httpUrl(r'http://www.acooke.org:80/andrew/#bar') 247 assert httpUrl(r'http://www.acooke.org/andrew?foo#bar') 248 assert httpUrl(r'http://www.acooke.org:80/andrew?foo#bar') 249 assert httpUrl(r'http://www.acooke.org/andrew/?foo#bar') 250 assert httpUrl(r'http://www.acooke.org:80/andrew/?foo#bar') 251 assert httpUrl(r'http://1.2.3.4:80/andrew/?foo#bar') 252 assert httpUrl(r'http://[1:2:3:4:5:6:7:8]:80/andrew/?foo#bar') 253 254 # http://base.google.com/support/bin/answer.py?hl=en&answer=25230 255 assert not httpUrl(r'http://www.example.com/space here.html') 256 assert not httpUrl(r'http://www.example.com\main.html') 257 assert not httpUrl(r'/main.html') 258 assert not httpUrl(r'www.example.com/main.html') 259 assert not httpUrl(r'http:www.example.com/main.html')
260 261
262 -class MailToUrlTest(BaseTest):
263
264 - def test_mail_to_url(self):
265 266 mailToUrl = MailToUrl() 267 268 assert mailToUrl('mailto:joe@example.com') 269 assert mailToUrl('mailto:user%2Bmailbox@example.com') 270 assert mailToUrl('mailto:customer%2Fdepartment=shipping@example.com') 271 assert mailToUrl('mailto:$A12345@example.com') 272 assert mailToUrl('mailto:!def!xyz%25abc@example.com') 273 assert mailToUrl('mailto:_somename@example.com')
274