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