Show More
@@ -1,13 +1,16 | |||||
1 | import doctest, tempfile, os, sys |
|
1 | import doctest, tempfile, os, sys | |
2 |
|
2 | |||
3 | if __name__ == "__main__": |
|
3 | if __name__ == "__main__": | |
|
4 | if 'TERM' in os.environ: | |||
|
5 | del os.environ['TERM'] | |||
|
6 | ||||
4 | fd, name = tempfile.mkstemp(suffix='hg-tst') |
|
7 | fd, name = tempfile.mkstemp(suffix='hg-tst') | |
5 |
|
8 | |||
6 | try: |
|
9 | try: | |
7 | os.write(fd, sys.stdin.read()) |
|
10 | os.write(fd, sys.stdin.read()) | |
8 | os.close(fd) |
|
11 | os.close(fd) | |
9 | failures, _ = doctest.testfile(name, module_relative=False) |
|
12 | failures, _ = doctest.testfile(name, module_relative=False) | |
10 | if failures: |
|
13 | if failures: | |
11 | sys.exit(1) |
|
14 | sys.exit(1) | |
12 | finally: |
|
15 | finally: | |
13 | os.remove(name) |
|
16 | os.remove(name) |
@@ -1,235 +1,238 | |||||
1 |
import |
|
1 | import os | |
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.sslutil import _verifycert |
|
10 | from mercurial.sslutil 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 | # no fallback to subject commonName when subjectAltName has DNS |
|
36 | # no fallback to subject commonName when subjectAltName has DNS | |
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 | # fallback to subject commonName when no DNS in subjectAltName |
|
39 | # fallback to subject commonName when no DNS in subjectAltName | |
40 | san_cert = {'subject': ((('commonName', 'example.com'),),), |
|
40 | san_cert = {'subject': ((('commonName', 'example.com'),),), | |
41 | 'subjectAltName': (('IP Address', '8.8.8.8'),)} |
|
41 | 'subjectAltName': (('IP Address', '8.8.8.8'),)} | |
42 | check(_verifycert(san_cert, 'example.com'), None) |
|
42 | check(_verifycert(san_cert, 'example.com'), None) | |
43 |
|
43 | |||
44 | # Avoid some pitfalls |
|
44 | # Avoid some pitfalls | |
45 | check(_verifycert(cert('*.foo'), 'foo'), |
|
45 | check(_verifycert(cert('*.foo'), 'foo'), | |
46 | 'certificate is for *.foo') |
|
46 | 'certificate is for *.foo') | |
47 | check(_verifycert(cert('*o'), 'foo'), |
|
47 | check(_verifycert(cert('*o'), 'foo'), | |
48 | 'certificate is for *o') |
|
48 | 'certificate is for *o') | |
49 |
|
49 | |||
50 | check(_verifycert({'subject': ()}, |
|
50 | check(_verifycert({'subject': ()}, | |
51 | 'example.com'), |
|
51 | 'example.com'), | |
52 | 'no commonName or subjectAltName found in certificate') |
|
52 | 'no commonName or subjectAltName found in certificate') | |
53 | check(_verifycert(None, 'example.com'), |
|
53 | check(_verifycert(None, 'example.com'), | |
54 | 'no certificate received') |
|
54 | 'no certificate received') | |
55 |
|
55 | |||
56 | # Unicode (IDN) certname isn't supported |
|
56 | # Unicode (IDN) certname isn't supported | |
57 | check(_verifycert(cert(u'\u4f8b.jp'), 'example.jp'), |
|
57 | check(_verifycert(cert(u'\u4f8b.jp'), 'example.jp'), | |
58 | 'IDN in certificate not supported') |
|
58 | 'IDN in certificate not supported') | |
59 |
|
59 | |||
60 | import doctest |
|
60 | import doctest | |
61 |
|
61 | |||
62 | def test_url(): |
|
62 | def test_url(): | |
63 | """ |
|
63 | """ | |
64 | >>> from mercurial.util import url |
|
64 | >>> from mercurial.util import url | |
65 |
|
65 | |||
66 | This tests for edge cases in url.URL's parsing algorithm. Most of |
|
66 | This tests for edge cases in url.URL's parsing algorithm. Most of | |
67 | these aren't useful for documentation purposes, so they aren't |
|
67 | these aren't useful for documentation purposes, so they aren't | |
68 | part of the class's doc tests. |
|
68 | part of the class's doc tests. | |
69 |
|
69 | |||
70 | Query strings and fragments: |
|
70 | Query strings and fragments: | |
71 |
|
71 | |||
72 | >>> url('http://host/a?b#c') |
|
72 | >>> url('http://host/a?b#c') | |
73 | <url scheme: 'http', host: 'host', path: 'a', query: 'b', fragment: 'c'> |
|
73 | <url scheme: 'http', host: 'host', path: 'a', query: 'b', fragment: 'c'> | |
74 | >>> url('http://host/a?') |
|
74 | >>> url('http://host/a?') | |
75 | <url scheme: 'http', host: 'host', path: 'a'> |
|
75 | <url scheme: 'http', host: 'host', path: 'a'> | |
76 | >>> url('http://host/a#b#c') |
|
76 | >>> url('http://host/a#b#c') | |
77 | <url scheme: 'http', host: 'host', path: 'a', fragment: 'b#c'> |
|
77 | <url scheme: 'http', host: 'host', path: 'a', fragment: 'b#c'> | |
78 | >>> url('http://host/a#b?c') |
|
78 | >>> url('http://host/a#b?c') | |
79 | <url scheme: 'http', host: 'host', path: 'a', fragment: 'b?c'> |
|
79 | <url scheme: 'http', host: 'host', path: 'a', fragment: 'b?c'> | |
80 | >>> url('http://host/?a#b') |
|
80 | >>> url('http://host/?a#b') | |
81 | <url scheme: 'http', host: 'host', path: '', query: 'a', fragment: 'b'> |
|
81 | <url scheme: 'http', host: 'host', path: '', query: 'a', fragment: 'b'> | |
82 | >>> url('http://host/?a#b', parsequery=False) |
|
82 | >>> url('http://host/?a#b', parsequery=False) | |
83 | <url scheme: 'http', host: 'host', path: '?a', fragment: 'b'> |
|
83 | <url scheme: 'http', host: 'host', path: '?a', fragment: 'b'> | |
84 | >>> url('http://host/?a#b', parsefragment=False) |
|
84 | >>> url('http://host/?a#b', parsefragment=False) | |
85 | <url scheme: 'http', host: 'host', path: '', query: 'a#b'> |
|
85 | <url scheme: 'http', host: 'host', path: '', query: 'a#b'> | |
86 | >>> url('http://host/?a#b', parsequery=False, parsefragment=False) |
|
86 | >>> url('http://host/?a#b', parsequery=False, parsefragment=False) | |
87 | <url scheme: 'http', host: 'host', path: '?a#b'> |
|
87 | <url scheme: 'http', host: 'host', path: '?a#b'> | |
88 |
|
88 | |||
89 | IPv6 addresses: |
|
89 | IPv6 addresses: | |
90 |
|
90 | |||
91 | >>> url('ldap://[2001:db8::7]/c=GB?objectClass?one') |
|
91 | >>> url('ldap://[2001:db8::7]/c=GB?objectClass?one') | |
92 | <url scheme: 'ldap', host: '[2001:db8::7]', path: 'c=GB', |
|
92 | <url scheme: 'ldap', host: '[2001:db8::7]', path: 'c=GB', | |
93 | query: 'objectClass?one'> |
|
93 | query: 'objectClass?one'> | |
94 | >>> url('ldap://joe:xxx@[2001:db8::7]:80/c=GB?objectClass?one') |
|
94 | >>> url('ldap://joe:xxx@[2001:db8::7]:80/c=GB?objectClass?one') | |
95 | <url scheme: 'ldap', user: 'joe', passwd: 'xxx', host: '[2001:db8::7]', |
|
95 | <url scheme: 'ldap', user: 'joe', passwd: 'xxx', host: '[2001:db8::7]', | |
96 | port: '80', path: 'c=GB', query: 'objectClass?one'> |
|
96 | port: '80', path: 'c=GB', query: 'objectClass?one'> | |
97 |
|
97 | |||
98 | Missing scheme, host, etc.: |
|
98 | Missing scheme, host, etc.: | |
99 |
|
99 | |||
100 | >>> url('://192.0.2.16:80/') |
|
100 | >>> url('://192.0.2.16:80/') | |
101 | <url path: '://192.0.2.16:80/'> |
|
101 | <url path: '://192.0.2.16:80/'> | |
102 | >>> url('http://mercurial.selenic.com') |
|
102 | >>> url('http://mercurial.selenic.com') | |
103 | <url scheme: 'http', host: 'mercurial.selenic.com'> |
|
103 | <url scheme: 'http', host: 'mercurial.selenic.com'> | |
104 | >>> url('/foo') |
|
104 | >>> url('/foo') | |
105 | <url path: '/foo'> |
|
105 | <url path: '/foo'> | |
106 | >>> url('bundle:/foo') |
|
106 | >>> url('bundle:/foo') | |
107 | <url scheme: 'bundle', path: '/foo'> |
|
107 | <url scheme: 'bundle', path: '/foo'> | |
108 | >>> url('a?b#c') |
|
108 | >>> url('a?b#c') | |
109 | <url path: 'a?b', fragment: 'c'> |
|
109 | <url path: 'a?b', fragment: 'c'> | |
110 | >>> url('http://x.com?arg=/foo') |
|
110 | >>> url('http://x.com?arg=/foo') | |
111 | <url scheme: 'http', host: 'x.com', query: 'arg=/foo'> |
|
111 | <url scheme: 'http', host: 'x.com', query: 'arg=/foo'> | |
112 | >>> url('http://joe:xxx@/foo') |
|
112 | >>> url('http://joe:xxx@/foo') | |
113 | <url scheme: 'http', user: 'joe', passwd: 'xxx', path: 'foo'> |
|
113 | <url scheme: 'http', user: 'joe', passwd: 'xxx', path: 'foo'> | |
114 |
|
114 | |||
115 | Just a scheme and a path: |
|
115 | Just a scheme and a path: | |
116 |
|
116 | |||
117 | >>> url('mailto:John.Doe@example.com') |
|
117 | >>> url('mailto:John.Doe@example.com') | |
118 | <url scheme: 'mailto', path: 'John.Doe@example.com'> |
|
118 | <url scheme: 'mailto', path: 'John.Doe@example.com'> | |
119 | >>> url('a:b:c:d') |
|
119 | >>> url('a:b:c:d') | |
120 | <url path: 'a:b:c:d'> |
|
120 | <url path: 'a:b:c:d'> | |
121 | >>> url('aa:bb:cc:dd') |
|
121 | >>> url('aa:bb:cc:dd') | |
122 | <url scheme: 'aa', path: 'bb:cc:dd'> |
|
122 | <url scheme: 'aa', path: 'bb:cc:dd'> | |
123 |
|
123 | |||
124 | SSH examples: |
|
124 | SSH examples: | |
125 |
|
125 | |||
126 | >>> url('ssh://joe@host//home/joe') |
|
126 | >>> url('ssh://joe@host//home/joe') | |
127 | <url scheme: 'ssh', user: 'joe', host: 'host', path: '/home/joe'> |
|
127 | <url scheme: 'ssh', user: 'joe', host: 'host', path: '/home/joe'> | |
128 | >>> url('ssh://joe:xxx@host/src') |
|
128 | >>> url('ssh://joe:xxx@host/src') | |
129 | <url scheme: 'ssh', user: 'joe', passwd: 'xxx', host: 'host', path: 'src'> |
|
129 | <url scheme: 'ssh', user: 'joe', passwd: 'xxx', host: 'host', path: 'src'> | |
130 | >>> url('ssh://joe:xxx@host') |
|
130 | >>> url('ssh://joe:xxx@host') | |
131 | <url scheme: 'ssh', user: 'joe', passwd: 'xxx', host: 'host'> |
|
131 | <url scheme: 'ssh', user: 'joe', passwd: 'xxx', host: 'host'> | |
132 | >>> url('ssh://joe@host') |
|
132 | >>> url('ssh://joe@host') | |
133 | <url scheme: 'ssh', user: 'joe', host: 'host'> |
|
133 | <url scheme: 'ssh', user: 'joe', host: 'host'> | |
134 | >>> url('ssh://host') |
|
134 | >>> url('ssh://host') | |
135 | <url scheme: 'ssh', host: 'host'> |
|
135 | <url scheme: 'ssh', host: 'host'> | |
136 | >>> url('ssh://') |
|
136 | >>> url('ssh://') | |
137 | <url scheme: 'ssh'> |
|
137 | <url scheme: 'ssh'> | |
138 | >>> url('ssh:') |
|
138 | >>> url('ssh:') | |
139 | <url scheme: 'ssh'> |
|
139 | <url scheme: 'ssh'> | |
140 |
|
140 | |||
141 | Non-numeric port: |
|
141 | Non-numeric port: | |
142 |
|
142 | |||
143 | >>> url('http://example.com:dd') |
|
143 | >>> url('http://example.com:dd') | |
144 | <url scheme: 'http', host: 'example.com', port: 'dd'> |
|
144 | <url scheme: 'http', host: 'example.com', port: 'dd'> | |
145 | >>> url('ssh://joe:xxx@host:ssh/foo') |
|
145 | >>> url('ssh://joe:xxx@host:ssh/foo') | |
146 | <url scheme: 'ssh', user: 'joe', passwd: 'xxx', host: 'host', port: 'ssh', |
|
146 | <url scheme: 'ssh', user: 'joe', passwd: 'xxx', host: 'host', port: 'ssh', | |
147 | path: 'foo'> |
|
147 | path: 'foo'> | |
148 |
|
148 | |||
149 | Bad authentication credentials: |
|
149 | Bad authentication credentials: | |
150 |
|
150 | |||
151 | >>> url('http://joe@joeville:123@4:@host/a?b#c') |
|
151 | >>> url('http://joe@joeville:123@4:@host/a?b#c') | |
152 | <url scheme: 'http', user: 'joe@joeville', passwd: '123@4:', |
|
152 | <url scheme: 'http', user: 'joe@joeville', passwd: '123@4:', | |
153 | host: 'host', path: 'a', query: 'b', fragment: 'c'> |
|
153 | host: 'host', path: 'a', query: 'b', fragment: 'c'> | |
154 | >>> url('http://!*#?/@!*#?/:@host/a?b#c') |
|
154 | >>> url('http://!*#?/@!*#?/:@host/a?b#c') | |
155 | <url scheme: 'http', host: '!*', fragment: '?/@!*#?/:@host/a?b#c'> |
|
155 | <url scheme: 'http', host: '!*', fragment: '?/@!*#?/:@host/a?b#c'> | |
156 | >>> url('http://!*#?@!*#?:@host/a?b#c') |
|
156 | >>> url('http://!*#?@!*#?:@host/a?b#c') | |
157 | <url scheme: 'http', host: '!*', fragment: '?@!*#?:@host/a?b#c'> |
|
157 | <url scheme: 'http', host: '!*', fragment: '?@!*#?:@host/a?b#c'> | |
158 | >>> url('http://!*@:!*@@host/a?b#c') |
|
158 | >>> url('http://!*@:!*@@host/a?b#c') | |
159 | <url scheme: 'http', user: '!*@', passwd: '!*@', host: 'host', |
|
159 | <url scheme: 'http', user: '!*@', passwd: '!*@', host: 'host', | |
160 | path: 'a', query: 'b', fragment: 'c'> |
|
160 | path: 'a', query: 'b', fragment: 'c'> | |
161 |
|
161 | |||
162 | File paths: |
|
162 | File paths: | |
163 |
|
163 | |||
164 | >>> url('a/b/c/d.g.f') |
|
164 | >>> url('a/b/c/d.g.f') | |
165 | <url path: 'a/b/c/d.g.f'> |
|
165 | <url path: 'a/b/c/d.g.f'> | |
166 | >>> url('/x///z/y/') |
|
166 | >>> url('/x///z/y/') | |
167 | <url path: '/x///z/y/'> |
|
167 | <url path: '/x///z/y/'> | |
168 | >>> url('/foo:bar') |
|
168 | >>> url('/foo:bar') | |
169 | <url path: '/foo:bar'> |
|
169 | <url path: '/foo:bar'> | |
170 | >>> url('\\\\foo:bar') |
|
170 | >>> url('\\\\foo:bar') | |
171 | <url path: '\\\\foo:bar'> |
|
171 | <url path: '\\\\foo:bar'> | |
172 | >>> url('./foo:bar') |
|
172 | >>> url('./foo:bar') | |
173 | <url path: './foo:bar'> |
|
173 | <url path: './foo:bar'> | |
174 |
|
174 | |||
175 | Non-localhost file URL: |
|
175 | Non-localhost file URL: | |
176 |
|
176 | |||
177 | >>> u = url('file://mercurial.selenic.com/foo') |
|
177 | >>> u = url('file://mercurial.selenic.com/foo') | |
178 | Traceback (most recent call last): |
|
178 | Traceback (most recent call last): | |
179 | File "<stdin>", line 1, in ? |
|
179 | File "<stdin>", line 1, in ? | |
180 | Abort: file:// URLs can only refer to localhost |
|
180 | Abort: file:// URLs can only refer to localhost | |
181 |
|
181 | |||
182 | Empty URL: |
|
182 | Empty URL: | |
183 |
|
183 | |||
184 | >>> u = url('') |
|
184 | >>> u = url('') | |
185 | >>> u |
|
185 | >>> u | |
186 | <url path: ''> |
|
186 | <url path: ''> | |
187 | >>> str(u) |
|
187 | >>> str(u) | |
188 | '' |
|
188 | '' | |
189 |
|
189 | |||
190 | Empty path with query string: |
|
190 | Empty path with query string: | |
191 |
|
191 | |||
192 | >>> str(url('http://foo/?bar')) |
|
192 | >>> str(url('http://foo/?bar')) | |
193 | 'http://foo/?bar' |
|
193 | 'http://foo/?bar' | |
194 |
|
194 | |||
195 | Invalid path: |
|
195 | Invalid path: | |
196 |
|
196 | |||
197 | >>> u = url('http://foo/bar') |
|
197 | >>> u = url('http://foo/bar') | |
198 | >>> u.path = 'bar' |
|
198 | >>> u.path = 'bar' | |
199 | >>> str(u) |
|
199 | >>> str(u) | |
200 | 'http://foo/bar' |
|
200 | 'http://foo/bar' | |
201 |
|
201 | |||
202 | >>> u = url('file:/foo/bar/baz') |
|
202 | >>> u = url('file:/foo/bar/baz') | |
203 | >>> u |
|
203 | >>> u | |
204 | <url scheme: 'file', path: '/foo/bar/baz'> |
|
204 | <url scheme: 'file', path: '/foo/bar/baz'> | |
205 | >>> str(u) |
|
205 | >>> str(u) | |
206 | 'file:///foo/bar/baz' |
|
206 | 'file:///foo/bar/baz' | |
207 | >>> u.localpath() |
|
207 | >>> u.localpath() | |
208 | '/foo/bar/baz' |
|
208 | '/foo/bar/baz' | |
209 |
|
209 | |||
210 | >>> u = url('file:///foo/bar/baz') |
|
210 | >>> u = url('file:///foo/bar/baz') | |
211 | >>> u |
|
211 | >>> u | |
212 | <url scheme: 'file', path: '/foo/bar/baz'> |
|
212 | <url scheme: 'file', path: '/foo/bar/baz'> | |
213 | >>> str(u) |
|
213 | >>> str(u) | |
214 | 'file:///foo/bar/baz' |
|
214 | 'file:///foo/bar/baz' | |
215 | >>> u.localpath() |
|
215 | >>> u.localpath() | |
216 | '/foo/bar/baz' |
|
216 | '/foo/bar/baz' | |
217 |
|
217 | |||
218 | >>> u = url('file:///f:oo/bar/baz') |
|
218 | >>> u = url('file:///f:oo/bar/baz') | |
219 | >>> u |
|
219 | >>> u | |
220 | <url scheme: 'file', path: 'f:oo/bar/baz'> |
|
220 | <url scheme: 'file', path: 'f:oo/bar/baz'> | |
221 | >>> str(u) |
|
221 | >>> str(u) | |
222 | 'file:f%3Aoo/bar/baz' |
|
222 | 'file:f%3Aoo/bar/baz' | |
223 | >>> u.localpath() |
|
223 | >>> u.localpath() | |
224 | 'f:oo/bar/baz' |
|
224 | 'f:oo/bar/baz' | |
225 |
|
225 | |||
226 | >>> u = url('file:foo/bar/baz') |
|
226 | >>> u = url('file:foo/bar/baz') | |
227 | >>> u |
|
227 | >>> u | |
228 | <url scheme: 'file', path: 'foo/bar/baz'> |
|
228 | <url scheme: 'file', path: 'foo/bar/baz'> | |
229 | >>> str(u) |
|
229 | >>> str(u) | |
230 | 'file:foo/bar/baz' |
|
230 | 'file:foo/bar/baz' | |
231 | >>> u.localpath() |
|
231 | >>> u.localpath() | |
232 | 'foo/bar/baz' |
|
232 | 'foo/bar/baz' | |
233 | """ |
|
233 | """ | |
234 |
|
234 | |||
|
235 | if 'TERM' in os.environ: | |||
|
236 | del os.environ['TERM'] | |||
|
237 | ||||
235 | doctest.testmod(optionflags=doctest.NORMALIZE_WHITESPACE) |
|
238 | doctest.testmod(optionflags=doctest.NORMALIZE_WHITESPACE) |
General Comments 0
You need to be logged in to leave comments.
Login now