Show More
@@ -1,190 +1,192 b'' | |||||
1 | import sys |
|
1 | import sys | |
2 |
|
2 | |||
3 | def check(a, b): |
|
3 | def check(a, b): | |
4 | if a != b: |
|
4 | if a != b: | |
5 | print (a, b) |
|
5 | print (a, b) | |
6 |
|
6 | |||
7 | def cert(cn): |
|
7 | def cert(cn): | |
8 | return dict(subject=((('commonName', cn),),)) |
|
8 | return dict(subject=((('commonName', cn),),)) | |
9 |
|
9 | |||
10 | from mercurial.url import _verifycert |
|
10 | from mercurial.url import _verifycert | |
11 |
|
11 | |||
12 | # Test non-wildcard certificates |
|
12 | # Test non-wildcard certificates | |
13 | check(_verifycert(cert('example.com'), 'example.com'), |
|
13 | check(_verifycert(cert('example.com'), 'example.com'), | |
14 | None) |
|
14 | None) | |
15 | check(_verifycert(cert('example.com'), 'www.example.com'), |
|
15 | check(_verifycert(cert('example.com'), 'www.example.com'), | |
16 | 'certificate is for example.com') |
|
16 | 'certificate is for example.com') | |
17 | check(_verifycert(cert('www.example.com'), 'example.com'), |
|
17 | check(_verifycert(cert('www.example.com'), 'example.com'), | |
18 | 'certificate is for www.example.com') |
|
18 | 'certificate is for www.example.com') | |
19 |
|
19 | |||
20 | # Test wildcard certificates |
|
20 | # Test wildcard certificates | |
21 | check(_verifycert(cert('*.example.com'), 'www.example.com'), |
|
21 | check(_verifycert(cert('*.example.com'), 'www.example.com'), | |
22 | None) |
|
22 | None) | |
23 | check(_verifycert(cert('*.example.com'), 'example.com'), |
|
23 | check(_verifycert(cert('*.example.com'), 'example.com'), | |
24 | 'certificate is for *.example.com') |
|
24 | 'certificate is for *.example.com') | |
25 | check(_verifycert(cert('*.example.com'), 'w.w.example.com'), |
|
25 | check(_verifycert(cert('*.example.com'), 'w.w.example.com'), | |
26 | 'certificate is for *.example.com') |
|
26 | 'certificate is for *.example.com') | |
27 |
|
27 | |||
28 | # Test subjectAltName |
|
28 | # Test subjectAltName | |
29 | san_cert = {'subject': ((('commonName', 'example.com'),),), |
|
29 | san_cert = {'subject': ((('commonName', 'example.com'),),), | |
30 | 'subjectAltName': (('DNS', '*.example.net'), |
|
30 | 'subjectAltName': (('DNS', '*.example.net'), | |
31 | ('DNS', 'example.net'))} |
|
31 | ('DNS', 'example.net'))} | |
32 | check(_verifycert(san_cert, 'example.net'), |
|
32 | check(_verifycert(san_cert, 'example.net'), | |
33 | None) |
|
33 | None) | |
34 | check(_verifycert(san_cert, 'foo.example.net'), |
|
34 | check(_verifycert(san_cert, 'foo.example.net'), | |
35 | None) |
|
35 | None) | |
36 | # subject is only checked when subjectAltName is empty |
|
36 | # subject is only checked when subjectAltName is empty | |
37 | check(_verifycert(san_cert, 'example.com'), |
|
37 | check(_verifycert(san_cert, 'example.com'), | |
38 | 'certificate is for *.example.net, example.net') |
|
38 | 'certificate is for *.example.net, example.net') | |
39 |
|
39 | |||
40 | # Avoid some pitfalls |
|
40 | # Avoid some pitfalls | |
41 | check(_verifycert(cert('*.foo'), 'foo'), |
|
41 | check(_verifycert(cert('*.foo'), 'foo'), | |
42 | 'certificate is for *.foo') |
|
42 | 'certificate is for *.foo') | |
43 | check(_verifycert(cert('*o'), 'foo'), |
|
43 | check(_verifycert(cert('*o'), 'foo'), | |
44 | 'certificate is for *o') |
|
44 | 'certificate is for *o') | |
45 |
|
45 | |||
46 | check(_verifycert({'subject': ()}, |
|
46 | check(_verifycert({'subject': ()}, | |
47 | 'example.com'), |
|
47 | 'example.com'), | |
48 | 'no commonName or subjectAltName found in certificate') |
|
48 | 'no commonName or subjectAltName found in certificate') | |
49 | check(_verifycert(None, 'example.com'), |
|
49 | check(_verifycert(None, 'example.com'), | |
50 | 'no certificate received') |
|
50 | 'no certificate received') | |
51 |
|
51 | |||
52 | import doctest |
|
52 | import doctest | |
53 |
|
53 | |||
54 | def test_url(): |
|
54 | def test_url(): | |
55 | """ |
|
55 | """ | |
56 | >>> from mercurial.url import url |
|
56 | >>> from mercurial.url import url | |
57 |
|
57 | |||
58 | This tests for edge cases in url.URL's parsing algorithm. Most of |
|
58 | This tests for edge cases in url.URL's parsing algorithm. Most of | |
59 | these aren't useful for documentation purposes, so they aren't |
|
59 | these aren't useful for documentation purposes, so they aren't | |
60 | part of the class's doc tests. |
|
60 | part of the class's doc tests. | |
61 |
|
61 | |||
62 | Query strings and fragments: |
|
62 | Query strings and fragments: | |
63 |
|
63 | |||
64 | >>> url('http://host/a?b#c') |
|
64 | >>> url('http://host/a?b#c') | |
65 | <url scheme: 'http', host: 'host', path: 'a', query: 'b', fragment: 'c'> |
|
65 | <url scheme: 'http', host: 'host', path: 'a', query: 'b', fragment: 'c'> | |
66 | >>> url('http://host/a?') |
|
66 | >>> url('http://host/a?') | |
67 | <url scheme: 'http', host: 'host', path: 'a'> |
|
67 | <url scheme: 'http', host: 'host', path: 'a'> | |
68 | >>> url('http://host/a#b#c') |
|
68 | >>> url('http://host/a#b#c') | |
69 | <url scheme: 'http', host: 'host', path: 'a', fragment: 'b#c'> |
|
69 | <url scheme: 'http', host: 'host', path: 'a', fragment: 'b#c'> | |
70 | >>> url('http://host/a#b?c') |
|
70 | >>> url('http://host/a#b?c') | |
71 | <url scheme: 'http', host: 'host', path: 'a', fragment: 'b?c'> |
|
71 | <url scheme: 'http', host: 'host', path: 'a', fragment: 'b?c'> | |
72 | >>> url('http://host/?a#b') |
|
72 | >>> url('http://host/?a#b') | |
73 | <url scheme: 'http', host: 'host', path: '', query: 'a', fragment: 'b'> |
|
73 | <url scheme: 'http', host: 'host', path: '', query: 'a', fragment: 'b'> | |
74 | >>> url('http://host/?a#b', parse_query=False) |
|
74 | >>> url('http://host/?a#b', parse_query=False) | |
75 | <url scheme: 'http', host: 'host', path: '?a', fragment: 'b'> |
|
75 | <url scheme: 'http', host: 'host', path: '?a', fragment: 'b'> | |
76 | >>> url('http://host/?a#b', parse_fragment=False) |
|
76 | >>> url('http://host/?a#b', parse_fragment=False) | |
77 | <url scheme: 'http', host: 'host', path: '', query: 'a#b'> |
|
77 | <url scheme: 'http', host: 'host', path: '', query: 'a#b'> | |
78 | >>> url('http://host/?a#b', parse_query=False, parse_fragment=False) |
|
78 | >>> url('http://host/?a#b', parse_query=False, parse_fragment=False) | |
79 | <url scheme: 'http', host: 'host', path: '?a#b'> |
|
79 | <url scheme: 'http', host: 'host', path: '?a#b'> | |
80 |
|
80 | |||
81 | IPv6 addresses: |
|
81 | IPv6 addresses: | |
82 |
|
82 | |||
83 | >>> url('ldap://[2001:db8::7]/c=GB?objectClass?one') |
|
83 | >>> url('ldap://[2001:db8::7]/c=GB?objectClass?one') | |
84 | <url scheme: 'ldap', host: '[2001:db8::7]', path: 'c=GB', |
|
84 | <url scheme: 'ldap', host: '[2001:db8::7]', path: 'c=GB', | |
85 | query: 'objectClass?one'> |
|
85 | query: 'objectClass?one'> | |
86 | >>> url('ldap://joe:xxx@[2001:db8::7]:80/c=GB?objectClass?one') |
|
86 | >>> url('ldap://joe:xxx@[2001:db8::7]:80/c=GB?objectClass?one') | |
87 | <url scheme: 'ldap', user: 'joe', passwd: 'xxx', host: '[2001:db8::7]', |
|
87 | <url scheme: 'ldap', user: 'joe', passwd: 'xxx', host: '[2001:db8::7]', | |
88 | port: '80', path: 'c=GB', query: 'objectClass?one'> |
|
88 | port: '80', path: 'c=GB', query: 'objectClass?one'> | |
89 |
|
89 | |||
90 | Missing scheme, host, etc.: |
|
90 | Missing scheme, host, etc.: | |
91 |
|
91 | |||
92 | >>> url('://192.0.2.16:80/') |
|
92 | >>> url('://192.0.2.16:80/') | |
93 | <url path: '://192.0.2.16:80/'> |
|
93 | <url path: '://192.0.2.16:80/'> | |
94 | >>> url('http://mercurial.selenic.com') |
|
94 | >>> url('http://mercurial.selenic.com') | |
95 | <url scheme: 'http', host: 'mercurial.selenic.com'> |
|
95 | <url scheme: 'http', host: 'mercurial.selenic.com'> | |
96 | >>> url('/foo') |
|
96 | >>> url('/foo') | |
97 | <url path: '/foo'> |
|
97 | <url path: '/foo'> | |
98 | >>> url('bundle:/foo') |
|
98 | >>> url('bundle:/foo') | |
99 | <url scheme: 'bundle', path: '/foo'> |
|
99 | <url scheme: 'bundle', path: '/foo'> | |
100 | >>> url('a?b#c') |
|
100 | >>> url('a?b#c') | |
101 | <url path: 'a?b', fragment: 'c'> |
|
101 | <url path: 'a?b', fragment: 'c'> | |
102 | >>> url('http://x.com?arg=/foo') |
|
102 | >>> url('http://x.com?arg=/foo') | |
103 | <url scheme: 'http', host: 'x.com', query: 'arg=/foo'> |
|
103 | <url scheme: 'http', host: 'x.com', query: 'arg=/foo'> | |
104 | >>> url('http://joe:xxx@/foo') |
|
104 | >>> url('http://joe:xxx@/foo') | |
105 | <url scheme: 'http', user: 'joe', passwd: 'xxx', path: 'foo'> |
|
105 | <url scheme: 'http', user: 'joe', passwd: 'xxx', path: 'foo'> | |
106 |
|
106 | |||
107 | Just a scheme and a path: |
|
107 | Just a scheme and a path: | |
108 |
|
108 | |||
109 | >>> url('mailto:John.Doe@example.com') |
|
109 | >>> url('mailto:John.Doe@example.com') | |
110 | <url scheme: 'mailto', path: 'John.Doe@example.com'> |
|
110 | <url scheme: 'mailto', path: 'John.Doe@example.com'> | |
111 | >>> url('a:b:c:d') |
|
111 | >>> url('a:b:c:d') | |
112 |
<url |
|
112 | <url path: 'a:b:c:d'> | |
|
113 | >>> url('aa:bb:cc:dd') | |||
|
114 | <url scheme: 'aa', path: 'bb:cc:dd'> | |||
113 |
|
115 | |||
114 | SSH examples: |
|
116 | SSH examples: | |
115 |
|
117 | |||
116 | >>> url('ssh://joe@host//home/joe') |
|
118 | >>> url('ssh://joe@host//home/joe') | |
117 | <url scheme: 'ssh', user: 'joe', host: 'host', path: '/home/joe'> |
|
119 | <url scheme: 'ssh', user: 'joe', host: 'host', path: '/home/joe'> | |
118 | >>> url('ssh://joe:xxx@host/src') |
|
120 | >>> url('ssh://joe:xxx@host/src') | |
119 | <url scheme: 'ssh', user: 'joe', passwd: 'xxx', host: 'host', path: 'src'> |
|
121 | <url scheme: 'ssh', user: 'joe', passwd: 'xxx', host: 'host', path: 'src'> | |
120 | >>> url('ssh://joe:xxx@host') |
|
122 | >>> url('ssh://joe:xxx@host') | |
121 | <url scheme: 'ssh', user: 'joe', passwd: 'xxx', host: 'host'> |
|
123 | <url scheme: 'ssh', user: 'joe', passwd: 'xxx', host: 'host'> | |
122 | >>> url('ssh://joe@host') |
|
124 | >>> url('ssh://joe@host') | |
123 | <url scheme: 'ssh', user: 'joe', host: 'host'> |
|
125 | <url scheme: 'ssh', user: 'joe', host: 'host'> | |
124 | >>> url('ssh://host') |
|
126 | >>> url('ssh://host') | |
125 | <url scheme: 'ssh', host: 'host'> |
|
127 | <url scheme: 'ssh', host: 'host'> | |
126 | >>> url('ssh://') |
|
128 | >>> url('ssh://') | |
127 | <url scheme: 'ssh'> |
|
129 | <url scheme: 'ssh'> | |
128 | >>> url('ssh:') |
|
130 | >>> url('ssh:') | |
129 | <url scheme: 'ssh'> |
|
131 | <url scheme: 'ssh'> | |
130 |
|
132 | |||
131 | Non-numeric port: |
|
133 | Non-numeric port: | |
132 |
|
134 | |||
133 | >>> url('http://example.com:dd') |
|
135 | >>> url('http://example.com:dd') | |
134 | <url scheme: 'http', host: 'example.com', port: 'dd'> |
|
136 | <url scheme: 'http', host: 'example.com', port: 'dd'> | |
135 | >>> url('ssh://joe:xxx@host:ssh/foo') |
|
137 | >>> url('ssh://joe:xxx@host:ssh/foo') | |
136 | <url scheme: 'ssh', user: 'joe', passwd: 'xxx', host: 'host', port: 'ssh', |
|
138 | <url scheme: 'ssh', user: 'joe', passwd: 'xxx', host: 'host', port: 'ssh', | |
137 | path: 'foo'> |
|
139 | path: 'foo'> | |
138 |
|
140 | |||
139 | Bad authentication credentials: |
|
141 | Bad authentication credentials: | |
140 |
|
142 | |||
141 | >>> url('http://joe@joeville:123@4:@host/a?b#c') |
|
143 | >>> url('http://joe@joeville:123@4:@host/a?b#c') | |
142 | <url scheme: 'http', user: 'joe@joeville', passwd: '123@4:', |
|
144 | <url scheme: 'http', user: 'joe@joeville', passwd: '123@4:', | |
143 | host: 'host', path: 'a', query: 'b', fragment: 'c'> |
|
145 | host: 'host', path: 'a', query: 'b', fragment: 'c'> | |
144 | >>> url('http://!*#?/@!*#?/:@host/a?b#c') |
|
146 | >>> url('http://!*#?/@!*#?/:@host/a?b#c') | |
145 | <url scheme: 'http', host: '!*', fragment: '?/@!*#?/:@host/a?b#c'> |
|
147 | <url scheme: 'http', host: '!*', fragment: '?/@!*#?/:@host/a?b#c'> | |
146 | >>> url('http://!*#?@!*#?:@host/a?b#c') |
|
148 | >>> url('http://!*#?@!*#?:@host/a?b#c') | |
147 | <url scheme: 'http', host: '!*', fragment: '?@!*#?:@host/a?b#c'> |
|
149 | <url scheme: 'http', host: '!*', fragment: '?@!*#?:@host/a?b#c'> | |
148 | >>> url('http://!*@:!*@@host/a?b#c') |
|
150 | >>> url('http://!*@:!*@@host/a?b#c') | |
149 | <url scheme: 'http', user: '!*@', passwd: '!*@', host: 'host', |
|
151 | <url scheme: 'http', user: '!*@', passwd: '!*@', host: 'host', | |
150 | path: 'a', query: 'b', fragment: 'c'> |
|
152 | path: 'a', query: 'b', fragment: 'c'> | |
151 |
|
153 | |||
152 | File paths: |
|
154 | File paths: | |
153 |
|
155 | |||
154 | >>> url('a/b/c/d.g.f') |
|
156 | >>> url('a/b/c/d.g.f') | |
155 | <url path: 'a/b/c/d.g.f'> |
|
157 | <url path: 'a/b/c/d.g.f'> | |
156 | >>> url('/x///z/y/') |
|
158 | >>> url('/x///z/y/') | |
157 | <url path: '/x///z/y/'> |
|
159 | <url path: '/x///z/y/'> | |
158 |
|
160 | |||
159 | Empty URL: |
|
161 | Empty URL: | |
160 |
|
162 | |||
161 | >>> u = url('') |
|
163 | >>> u = url('') | |
162 | >>> u |
|
164 | >>> u | |
163 | <url path: ''> |
|
165 | <url path: ''> | |
164 | >>> str(u) |
|
166 | >>> str(u) | |
165 | '' |
|
167 | '' | |
166 |
|
168 | |||
167 | Empty path with query string: |
|
169 | Empty path with query string: | |
168 |
|
170 | |||
169 | >>> str(url('http://foo/?bar')) |
|
171 | >>> str(url('http://foo/?bar')) | |
170 | 'http://foo/?bar' |
|
172 | 'http://foo/?bar' | |
171 |
|
173 | |||
172 | Invalid path: |
|
174 | Invalid path: | |
173 |
|
175 | |||
174 | >>> u = url('http://foo/bar') |
|
176 | >>> u = url('http://foo/bar') | |
175 | >>> u.path = 'bar' |
|
177 | >>> u.path = 'bar' | |
176 | >>> str(u) |
|
178 | >>> str(u) | |
177 | 'http://foo/bar' |
|
179 | 'http://foo/bar' | |
178 |
|
180 | |||
179 | >>> u = url('file:///foo/bar/baz') |
|
181 | >>> u = url('file:///foo/bar/baz') | |
180 | >>> u |
|
182 | >>> u | |
181 | <url scheme: 'file', path: '/foo/bar/baz'> |
|
183 | <url scheme: 'file', path: '/foo/bar/baz'> | |
182 | >>> str(u) |
|
184 | >>> str(u) | |
183 | 'file:/foo/bar/baz' |
|
185 | 'file:/foo/bar/baz' | |
184 | """ |
|
186 | """ | |
185 |
|
187 | |||
186 | doctest.testmod(optionflags=doctest.NORMALIZE_WHITESPACE) |
|
188 | doctest.testmod(optionflags=doctest.NORMALIZE_WHITESPACE) | |
187 |
|
189 | |||
188 | # Unicode (IDN) certname isn't supported |
|
190 | # Unicode (IDN) certname isn't supported | |
189 | check(_verifycert(cert(u'\u4f8b.jp'), 'example.jp'), |
|
191 | check(_verifycert(cert(u'\u4f8b.jp'), 'example.jp'), | |
190 | 'IDN in certificate not supported') |
|
192 | 'IDN in certificate not supported') |
General Comments 0
You need to be logged in to leave comments.
Login now