Debian Codegraph @

tests/test-url.py

raw
1 import doctest
2 import os
3
4
5 def check(a, b):
6 if a != b:
7 print((a, b))
8
9
10 def cert(cn):
11 return {'subject': ((('commonName', cn),),)}
12
13
14 from mercurial import sslutil
15
16 _verifycert = sslutil._verifycert
17 # Test non-wildcard certificates
18 check(_verifycert(cert('example.com'), 'example.com'), None)
19 check(
20 _verifycert(cert('example.com'), 'www.example.com'),
21 b'certificate is for example.com',
22 )
23 check(
24 _verifycert(cert('www.example.com'), 'example.com'),
25 b'certificate is for www.example.com',
26 )
27
28 # Test wildcard certificates
29 check(_verifycert(cert('*.example.com'), 'www.example.com'), None)
30 check(
31 _verifycert(cert('*.example.com'), 'example.com'),
32 b'certificate is for *.example.com',
33 )
34 check(
35 _verifycert(cert('*.example.com'), 'w.w.example.com'),
36 b'certificate is for *.example.com',
37 )
38
39 # Test subjectAltName
40 san_cert = {
41 'subject': ((('commonName', 'example.com'),),),
42 'subjectAltName': (('DNS', '*.example.net'), ('DNS', 'example.net')),
43 }
44 check(_verifycert(san_cert, 'example.net'), None)
45 check(_verifycert(san_cert, 'foo.example.net'), None)
46 # no fallback to subject commonName when subjectAltName has DNS
47 check(
48 _verifycert(san_cert, 'example.com'),
49 b'certificate is for *.example.net, example.net',
50 )
51 # fallback to subject commonName when no DNS in subjectAltName
52 san_cert = {
53 'subject': ((('commonName', 'example.com'),),),
54 'subjectAltName': (('IP Address', '8.8.8.8'),),
55 }
56 check(_verifycert(san_cert, 'example.com'), None)
57
58 # Avoid some pitfalls
59 check(_verifycert(cert('*.foo'), 'foo'), b'certificate is for *.foo')
60 check(_verifycert(cert('*o'), 'foo'), None)
61
62 check(
63 _verifycert({'subject': ()}, 'example.com'),
64 b'no commonName or subjectAltName found in certificate',
65 )
66 check(_verifycert(None, 'example.com'), b'no certificate received')
67
68 # Unicode (IDN) certname isn't supported
69 check(
70 _verifycert(cert('\u4f8b.jp'), 'example.jp'),
71 b'IDN in certificate not supported',
72 )
73
74 # The following tests are from CPython's test_ssl.py.
75 check(_verifycert(cert('example.com'), 'example.com'), None)
76 check(_verifycert(cert('example.com'), 'ExAmple.cOm'), None)
77 check(
78 _verifycert(cert('example.com'), 'www.example.com'),
79 b'certificate is for example.com',
80 )
81 check(
82 _verifycert(cert('example.com'), '.example.com'),
83 b'certificate is for example.com',
84 )
85 check(
86 _verifycert(cert('example.com'), 'example.org'),
87 b'certificate is for example.com',
88 )
89 check(
90 _verifycert(cert('example.com'), 'exampleXcom'),
91 b'certificate is for example.com',
92 )
93 check(_verifycert(cert('*.a.com'), 'foo.a.com'), None)
94 check(
95 _verifycert(cert('*.a.com'), 'bar.foo.a.com'), b'certificate is for *.a.com'
96 )
97 check(_verifycert(cert('*.a.com'), 'a.com'), b'certificate is for *.a.com')
98 check(_verifycert(cert('*.a.com'), 'Xa.com'), b'certificate is for *.a.com')
99 check(_verifycert(cert('*.a.com'), '.a.com'), b'certificate is for *.a.com')
100
101 # only match one left-most wildcard
102 check(_verifycert(cert('f*.com'), 'foo.com'), None)
103 check(_verifycert(cert('f*.com'), 'f.com'), None)
104 check(_verifycert(cert('f*.com'), 'bar.com'), b'certificate is for f*.com')
105 check(_verifycert(cert('f*.com'), 'foo.a.com'), b'certificate is for f*.com')
106 check(_verifycert(cert('f*.com'), 'bar.foo.com'), b'certificate is for f*.com')
107
108 # NULL bytes are bad, CVE-2013-4073
109 check(
110 _verifycert(
111 cert('null.python.org\x00example.org'), 'null.python.org\x00example.org'
112 ),
113 None,
114 )
115 check(
116 _verifycert(cert('null.python.org\x00example.org'), 'example.org'),
117 b'certificate is for null.python.org\x00example.org',
118 )
119 check(
120 _verifycert(cert('null.python.org\x00example.org'), 'null.python.org'),
121 b'certificate is for null.python.org\x00example.org',
122 )
123
124 # error cases with wildcards
125 check(
126 _verifycert(cert('*.*.a.com'), 'bar.foo.a.com'),
127 b'certificate is for *.*.a.com',
128 )
129 check(_verifycert(cert('*.*.a.com'), 'a.com'), b'certificate is for *.*.a.com')
130 check(_verifycert(cert('*.*.a.com'), 'Xa.com'), b'certificate is for *.*.a.com')
131 check(_verifycert(cert('*.*.a.com'), '.a.com'), b'certificate is for *.*.a.com')
132
133 check(_verifycert(cert('a.*.com'), 'a.foo.com'), b'certificate is for a.*.com')
134 check(_verifycert(cert('a.*.com'), 'a..com'), b'certificate is for a.*.com')
135 check(_verifycert(cert('a.*.com'), 'a.com'), b'certificate is for a.*.com')
136
137 # wildcard doesn't match IDNA prefix 'xn--'
138 idna = 'püthon.python.org'.encode('idna').decode('ascii')
139 check(_verifycert(cert(idna), idna), None)
140 check(
141 _verifycert(cert('x*.python.org'), idna),
142 b'certificate is for x*.python.org',
143 )
144 check(
145 _verifycert(cert('xn--p*.python.org'), idna),
146 b'certificate is for xn--p*.python.org',
147 )
148
149 # wildcard in first fragment and IDNA A-labels in sequent fragments
150 # are supported.
151 idna = 'www*.pythön.org'.encode('idna').decode('ascii')
152 check(
153 _verifycert(cert(idna), 'www.pythön.org'.encode('idna').decode('ascii')),
154 None,
155 )
156 check(
157 _verifycert(cert(idna), 'www1.pythön.org'.encode('idna').decode('ascii')),
158 None,
159 )
160 check(
161 _verifycert(cert(idna), 'ftp.pythön.org'.encode('idna').decode('ascii')),
162 b'certificate is for www*.xn--pythn-mua.org',
163 )
164 check(
165 _verifycert(cert(idna), 'pythön.org'.encode('idna').decode('ascii')),
166 b'certificate is for www*.xn--pythn-mua.org',
167 )
168
169 c = {
170 'notAfter': 'Jun 26 21:41:46 2011 GMT',
171 'subject': ((('commonName', 'linuxfrz.org'),),),
172 'subjectAltName': (
173 ('DNS', 'linuxfr.org'),
174 ('DNS', 'linuxfr.com'),
175 ('othername', '<unsupported>'),
176 ),
177 }
178 check(_verifycert(c, 'linuxfr.org'), None)
179 check(_verifycert(c, 'linuxfr.com'), None)
180 # Not a "DNS" entry
181 check(
182 _verifycert(c, '<unsupported>'),
183 b'certificate is for linuxfr.org, linuxfr.com',
184 )
185 # When there is a subjectAltName, commonName isn't used
186 check(
187 _verifycert(c, 'linuxfrz.org'),
188 b'certificate is for linuxfr.org, linuxfr.com',
189 )
190
191 # A pristine real-world example
192 c = {
193 'notAfter': 'Dec 18 23:59:59 2011 GMT',
194 'subject': (
195 (('countryName', 'US'),),
196 (('stateOrProvinceName', 'California'),),
197 (('localityName', 'Mountain View'),),
198 (('organizationName', 'Google Inc'),),
199 (('commonName', 'mail.google.com'),),
200 ),
201 }
202 check(_verifycert(c, 'mail.google.com'), None)
203 check(_verifycert(c, 'gmail.com'), b'certificate is for mail.google.com')
204
205 # Only commonName is considered
206 check(_verifycert(c, 'California'), b'certificate is for mail.google.com')
207
208 # Neither commonName nor subjectAltName
209 c = {
210 'notAfter': 'Dec 18 23:59:59 2011 GMT',
211 'subject': (
212 (('countryName', 'US'),),
213 (('stateOrProvinceName', 'California'),),
214 (('localityName', 'Mountain View'),),
215 (('organizationName', 'Google Inc'),),
216 ),
217 }
218 check(
219 _verifycert(c, 'mail.google.com'),
220 b'no commonName or subjectAltName found in certificate',
221 )
222
223 # No DNS entry in subjectAltName but a commonName
224 c = {
225 'notAfter': 'Dec 18 23:59:59 2099 GMT',
226 'subject': (
227 (('countryName', 'US'),),
228 (('stateOrProvinceName', 'California'),),
229 (('localityName', 'Mountain View'),),
230 (('commonName', 'mail.google.com'),),
231 ),
232 'subjectAltName': (('othername', 'blabla'),),
233 }
234 check(_verifycert(c, 'mail.google.com'), None)
235
236 # No DNS entry subjectAltName and no commonName
237 c = {
238 'notAfter': 'Dec 18 23:59:59 2099 GMT',
239 'subject': (
240 (('countryName', 'US'),),
241 (('stateOrProvinceName', 'California'),),
242 (('localityName', 'Mountain View'),),
243 (('organizationName', 'Google Inc'),),
244 ),
245 'subjectAltName': (('othername', 'blabla'),),
246 }
247 check(
248 _verifycert(c, 'google.com'),
249 b'no commonName or subjectAltName found in certificate',
250 )
251
252 # Empty cert / no cert
253 check(_verifycert(None, 'example.com'), b'no certificate received')
254 check(_verifycert({}, 'example.com'), b'no certificate received')
255
256 # avoid denials of service by refusing more than one
257 # wildcard per fragment.
258 check(
259 _verifycert({'subject': ((('commonName', 'a*b.com'),),)}, 'axxb.com'),
260 None,
261 )
262 check(
263 _verifycert({'subject': ((('commonName', 'a*b.co*'),),)}, 'axxb.com'),
264 b'certificate is for a*b.co*',
265 )
266 check(
267 _verifycert({'subject': ((('commonName', 'a*b*.com'),),)}, 'axxbxxc.com'),
268 b'too many wildcards in certificate DNS name: a*b*.com',
269 )
270
271
272 def test_url():
273 """
274 >>> from mercurial import error, pycompat
275 >>> from mercurial.utils.urlutil import url
276 >>> from mercurial.utils.stringutil import forcebytestr
277
278 This tests for edge cases in url.URL's parsing algorithm. Most of
279 these aren't useful for documentation purposes, so they aren't
280 part of the class's doc tests.
281
282 Query strings and fragments:
283
284 >>> url(b'http://host/a?b#c')
285 <url scheme: 'http', host: 'host', path: 'a', query: 'b', fragment: 'c'>
286 >>> url(b'http://host/a?')
287 <url scheme: 'http', host: 'host', path: 'a'>
288 >>> url(b'http://host/a#b#c')
289 <url scheme: 'http', host: 'host', path: 'a', fragment: 'b#c'>
290 >>> url(b'http://host/a#b?c')
291 <url scheme: 'http', host: 'host', path: 'a', fragment: 'b?c'>
292 >>> url(b'http://host/?a#b')
293 <url scheme: 'http', host: 'host', path: '', query: 'a', fragment: 'b'>
294 >>> url(b'http://host/?a#b', parsequery=False)
295 <url scheme: 'http', host: 'host', path: '?a', fragment: 'b'>
296 >>> url(b'http://host/?a#b', parsefragment=False)
297 <url scheme: 'http', host: 'host', path: '', query: 'a#b'>
298 >>> url(b'http://host/?a#b', parsequery=False, parsefragment=False)
299 <url scheme: 'http', host: 'host', path: '?a#b'>
300
301 IPv6 addresses:
302
303 >>> url(b'ldap://[2001:db8::7]/c=GB?objectClass?one')
304 <url scheme: 'ldap', host: '[2001:db8::7]', path: 'c=GB',
305 query: 'objectClass?one'>
306 >>> url(b'ldap://joe:xxx@[2001:db8::7]:80/c=GB?objectClass?one')
307 <url scheme: 'ldap', user: 'joe', passwd: 'xxx', host: '[2001:db8::7]',
308 port: '80', path: 'c=GB', query: 'objectClass?one'>
309
310 Missing scheme, host, etc.:
311
312 >>> url(b'://192.0.2.16:80/')
313 <url path: '://192.0.2.16:80/'>
314 >>> url(b'https://mercurial-scm.org')
315 <url scheme: 'https', host: 'mercurial-scm.org'>
316 >>> url(b'/foo')
317 <url path: '/foo'>
318 >>> url(b'bundle:/foo')
319 <url scheme: 'bundle', path: '/foo'>
320 >>> url(b'a?b#c')
321 <url path: 'a?b', fragment: 'c'>
322 >>> url(b'http://x.com?arg=/foo')
323 <url scheme: 'http', host: 'x.com', query: 'arg=/foo'>
324 >>> url(b'http://joe:xxx@/foo')
325 <url scheme: 'http', user: 'joe', passwd: 'xxx', path: 'foo'>
326
327 Just a scheme and a path:
328
329 >>> url(b'mailto:[email protected]')
330 <url scheme: 'mailto', path: '[email protected]'>
331 >>> url(b'a:b:c:d')
332 <url path: 'a:b:c:d'>
333 >>> url(b'aa:bb:cc:dd')
334 <url scheme: 'aa', path: 'bb:cc:dd'>
335
336 SSH examples:
337
338 >>> url(b'ssh://joe@host//home/joe')
339 <url scheme: 'ssh', user: 'joe', host: 'host', path: '/home/joe'>
340 >>> url(b'ssh://joe:xxx@host/src')
341 <url scheme: 'ssh', user: 'joe', passwd: 'xxx', host: 'host', path: 'src'>
342 >>> url(b'ssh://joe:xxx@host')
343 <url scheme: 'ssh', user: 'joe', passwd: 'xxx', host: 'host'>
344 >>> url(b'ssh://joe@host')
345 <url scheme: 'ssh', user: 'joe', host: 'host'>
346 >>> url(b'ssh://host')
347 <url scheme: 'ssh', host: 'host'>
348 >>> url(b'ssh://')
349 <url scheme: 'ssh'>
350 >>> url(b'ssh:')
351 <url scheme: 'ssh'>
352
353 Non-numeric port:
354
355 >>> url(b'http://example.com:dd')
356 <url scheme: 'http', host: 'example.com', port: 'dd'>
357 >>> url(b'ssh://joe:xxx@host:ssh/foo')
358 <url scheme: 'ssh', user: 'joe', passwd: 'xxx', host: 'host', port: 'ssh',
359 path: 'foo'>
360
361 Bad authentication credentials:
362
363 >>> url(b'http://joe@joeville:123@4:@host/a?b#c')
364 <url scheme: 'http', user: 'joe@joeville', passwd: '123@4:',
365 host: 'host', path: 'a', query: 'b', fragment: 'c'>
366 >>> url(b'http://!*#?/@!*#?/:@host/a?b#c')
367 <url scheme: 'http', host: '!*', fragment: '?/@!*#?/:@host/a?b#c'>
368 >>> url(b'http://!*#?@!*#?:@host/a?b#c')
369 <url scheme: 'http', host: '!*', fragment: '?@!*#?:@host/a?b#c'>
370 >>> url(b'http://!*@:!*@@host/a?b#c')
371 <url scheme: 'http', user: '!*@', passwd: '!*@', host: 'host',
372 path: 'a', query: 'b', fragment: 'c'>
373
374 File paths:
375
376 >>> url(b'a/b/c/d.g.f')
377 <url path: 'a/b/c/d.g.f'>
378 >>> url(b'/x///z/y/')
379 <url path: '/x///z/y/'>
380 >>> url(b'/foo:bar')
381 <url path: '/foo:bar'>
382 >>> url(b'\\\\foo:bar')
383 <url path: '\\\\foo:bar'>
384 >>> url(b'./foo:bar')
385 <url path: './foo:bar'>
386
387 Non-localhost file URL:
388
389 >>> try:
390 ... u = url(b'file://mercurial-scm.org/foo')
391 ... except error.Abort as e:
392 ... pycompat.bytestr(e.message)
393 'file:// URLs can only refer to localhost'
394
395 Empty URL:
396
397 >>> u = url(b'')
398 >>> u
399 <url path: ''>
400 >>> str(u)
401 ''
402
403 Empty path with query string:
404
405 >>> str(url(b'http://foo/?bar'))
406 'http://foo/?bar'
407
408 Invalid path:
409
410 >>> u = url(b'http://foo/bar')
411 >>> u.path = b'bar'
412 >>> str(u)
413 'http://foo/bar'
414
415 >>> u = url(b'file:/foo/bar/baz')
416 >>> u
417 <url scheme: 'file', path: '/foo/bar/baz'>
418 >>> str(u)
419 'file:///foo/bar/baz'
420 >>> pycompat.bytestr(u.localpath())
421 '/foo/bar/baz'
422
423 >>> u = url(b'file:///foo/bar/baz')
424 >>> u
425 <url scheme: 'file', path: '/foo/bar/baz'>
426 >>> str(u)
427 'file:///foo/bar/baz'
428 >>> pycompat.bytestr(u.localpath())
429 '/foo/bar/baz'
430
431 >>> u = url(b'file:///f:oo/bar/baz')
432 >>> u
433 <url scheme: 'file', path: 'f:oo/bar/baz'>
434 >>> str(u)
435 'file:///f:oo/bar/baz'
436 >>> pycompat.bytestr(u.localpath())
437 'f:oo/bar/baz'
438
439 >>> u = url(b'file://localhost/f:oo/bar/baz')
440 >>> u
441 <url scheme: 'file', host: 'localhost', path: 'f:oo/bar/baz'>
442 >>> str(u)
443 'file://localhost/f:oo/bar/baz'
444 >>> pycompat.bytestr(u.localpath())
445 'f:oo/bar/baz'
446
447 >>> u = url(b'file:foo/bar/baz')
448 >>> u
449 <url scheme: 'file', path: 'foo/bar/baz'>
450 >>> str(u)
451 'file:foo/bar/baz'
452 >>> pycompat.bytestr(u.localpath())
453 'foo/bar/baz'
454 """
455
456
457 if 'TERM' in os.environ:
458 del os.environ['TERM']
459
460 doctest.testmod(optionflags=doctest.NORMALIZE_WHITESPACE)