root / test / ganeti.http_unittest.py @ 8572f1fe
History | View | Annotate | Download (14.4 kB)
1 | a0638838 | Oleksiy Mishchenko | #!/usr/bin/python
|
---|---|---|---|
2 | a0638838 | Oleksiy Mishchenko | #
|
3 | a0638838 | Oleksiy Mishchenko | |
4 | a0638838 | Oleksiy Mishchenko | # Copyright (C) 2007, 2008 Google Inc.
|
5 | a0638838 | Oleksiy Mishchenko | #
|
6 | a0638838 | Oleksiy Mishchenko | # This program is free software; you can redistribute it and/or modify
|
7 | a0638838 | Oleksiy Mishchenko | # it under the terms of the GNU General Public License as published by
|
8 | a0638838 | Oleksiy Mishchenko | # the Free Software Foundation; either version 2 of the License, or
|
9 | a0638838 | Oleksiy Mishchenko | # (at your option) any later version.
|
10 | a0638838 | Oleksiy Mishchenko | #
|
11 | a0638838 | Oleksiy Mishchenko | # This program is distributed in the hope that it will be useful, but
|
12 | a0638838 | Oleksiy Mishchenko | # WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 | a0638838 | Oleksiy Mishchenko | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
14 | a0638838 | Oleksiy Mishchenko | # General Public License for more details.
|
15 | a0638838 | Oleksiy Mishchenko | #
|
16 | a0638838 | Oleksiy Mishchenko | # You should have received a copy of the GNU General Public License
|
17 | a0638838 | Oleksiy Mishchenko | # along with this program; if not, write to the Free Software
|
18 | a0638838 | Oleksiy Mishchenko | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
|
19 | a0638838 | Oleksiy Mishchenko | # 02110-1301, USA.
|
20 | a0638838 | Oleksiy Mishchenko | |
21 | a0638838 | Oleksiy Mishchenko | |
22 | a0638838 | Oleksiy Mishchenko | """Script for unittesting the http module"""
|
23 | a0638838 | Oleksiy Mishchenko | |
24 | a0638838 | Oleksiy Mishchenko | |
25 | a0638838 | Oleksiy Mishchenko | import os |
26 | a0638838 | Oleksiy Mishchenko | import unittest |
27 | a0638838 | Oleksiy Mishchenko | import time |
28 | a8950eb7 | Michael Hanselmann | import tempfile |
29 | 2287b920 | Michael Hanselmann | from cStringIO import StringIO |
30 | a0638838 | Oleksiy Mishchenko | |
31 | a0638838 | Oleksiy Mishchenko | from ganeti import http |
32 | a0638838 | Oleksiy Mishchenko | |
33 | f30ca1e6 | Michael Hanselmann | import ganeti.http.server |
34 | f30ca1e6 | Michael Hanselmann | import ganeti.http.client |
35 | bf9bd8dd | Michael Hanselmann | import ganeti.http.auth |
36 | a0638838 | Oleksiy Mishchenko | |
37 | 25231ec5 | Michael Hanselmann | import testutils |
38 | 25231ec5 | Michael Hanselmann | |
39 | f30ca1e6 | Michael Hanselmann | |
40 | f30ca1e6 | Michael Hanselmann | class TestStartLines(unittest.TestCase): |
41 | f30ca1e6 | Michael Hanselmann | """Test cases for start line classes"""
|
42 | f30ca1e6 | Michael Hanselmann | |
43 | f30ca1e6 | Michael Hanselmann | def testClientToServerStartLine(self): |
44 | f30ca1e6 | Michael Hanselmann | """Test client to server start line (HTTP request)"""
|
45 | f30ca1e6 | Michael Hanselmann | start_line = http.HttpClientToServerStartLine("GET", "/", "HTTP/1.1") |
46 | f30ca1e6 | Michael Hanselmann | self.assertEqual(str(start_line), "GET / HTTP/1.1") |
47 | f30ca1e6 | Michael Hanselmann | |
48 | f30ca1e6 | Michael Hanselmann | def testServerToClientStartLine(self): |
49 | f30ca1e6 | Michael Hanselmann | """Test server to client start line (HTTP response)"""
|
50 | f30ca1e6 | Michael Hanselmann | start_line = http.HttpServerToClientStartLine("HTTP/1.1", 200, "OK") |
51 | f30ca1e6 | Michael Hanselmann | self.assertEqual(str(start_line), "HTTP/1.1 200 OK") |
52 | f30ca1e6 | Michael Hanselmann | |
53 | f30ca1e6 | Michael Hanselmann | |
54 | f30ca1e6 | Michael Hanselmann | class TestMisc(unittest.TestCase): |
55 | f30ca1e6 | Michael Hanselmann | """Miscellaneous tests"""
|
56 | f30ca1e6 | Michael Hanselmann | |
57 | f30ca1e6 | Michael Hanselmann | def _TestDateTimeHeader(self, gmnow, expected): |
58 | f30ca1e6 | Michael Hanselmann | self.assertEqual(http.server._DateTimeHeader(gmnow=gmnow), expected)
|
59 | f30ca1e6 | Michael Hanselmann | |
60 | f30ca1e6 | Michael Hanselmann | def testDateTimeHeader(self): |
61 | f30ca1e6 | Michael Hanselmann | """Test ganeti.http._DateTimeHeader"""
|
62 | f30ca1e6 | Michael Hanselmann | self._TestDateTimeHeader((2008, 1, 2, 3, 4, 5, 3, 0, 0), |
63 | f30ca1e6 | Michael Hanselmann | "Thu, 02 Jan 2008 03:04:05 GMT")
|
64 | f30ca1e6 | Michael Hanselmann | self._TestDateTimeHeader((2008, 1, 1, 0, 0, 0, 0, 0, 0), |
65 | f30ca1e6 | Michael Hanselmann | "Mon, 01 Jan 2008 00:00:00 GMT")
|
66 | f30ca1e6 | Michael Hanselmann | self._TestDateTimeHeader((2008, 12, 31, 0, 0, 0, 0, 0, 0), |
67 | f30ca1e6 | Michael Hanselmann | "Mon, 31 Dec 2008 00:00:00 GMT")
|
68 | f30ca1e6 | Michael Hanselmann | self._TestDateTimeHeader((2008, 12, 31, 23, 59, 59, 0, 0, 0), |
69 | f30ca1e6 | Michael Hanselmann | "Mon, 31 Dec 2008 23:59:59 GMT")
|
70 | f30ca1e6 | Michael Hanselmann | self._TestDateTimeHeader((2008, 12, 31, 0, 0, 0, 6, 0, 0), |
71 | f30ca1e6 | Michael Hanselmann | "Sun, 31 Dec 2008 00:00:00 GMT")
|
72 | f30ca1e6 | Michael Hanselmann | |
73 | f30ca1e6 | Michael Hanselmann | def testHttpServerRequest(self): |
74 | f30ca1e6 | Michael Hanselmann | """Test ganeti.http.server._HttpServerRequest"""
|
75 | a8950eb7 | Michael Hanselmann | server_request = http.server._HttpServerRequest("GET", "/", None, None) |
76 | f30ca1e6 | Michael Hanselmann | |
77 | f30ca1e6 | Michael Hanselmann | # These are expected by users of the HTTP server
|
78 | f30ca1e6 | Michael Hanselmann | self.assert_(hasattr(server_request, "request_method")) |
79 | f30ca1e6 | Michael Hanselmann | self.assert_(hasattr(server_request, "request_path")) |
80 | f30ca1e6 | Michael Hanselmann | self.assert_(hasattr(server_request, "request_headers")) |
81 | f30ca1e6 | Michael Hanselmann | self.assert_(hasattr(server_request, "request_body")) |
82 | f30ca1e6 | Michael Hanselmann | self.assert_(isinstance(server_request.resp_headers, dict)) |
83 | 68fa9caf | Michael Hanselmann | self.assert_(hasattr(server_request, "private")) |
84 | f30ca1e6 | Michael Hanselmann | |
85 | f30ca1e6 | Michael Hanselmann | def testServerSizeLimits(self): |
86 | f30ca1e6 | Michael Hanselmann | """Test HTTP server size limits"""
|
87 | f30ca1e6 | Michael Hanselmann | message_reader_class = http.server._HttpClientToServerMessageReader |
88 | f30ca1e6 | Michael Hanselmann | self.assert_(message_reader_class.START_LINE_LENGTH_MAX > 0) |
89 | f30ca1e6 | Michael Hanselmann | self.assert_(message_reader_class.HEADER_LENGTH_MAX > 0) |
90 | f30ca1e6 | Michael Hanselmann | |
91 | a8950eb7 | Michael Hanselmann | def testFormatAuthHeader(self): |
92 | a8950eb7 | Michael Hanselmann | self.assertEqual(http.auth._FormatAuthHeader("Basic", {}), |
93 | a8950eb7 | Michael Hanselmann | "Basic")
|
94 | a8950eb7 | Michael Hanselmann | self.assertEqual(http.auth._FormatAuthHeader("Basic", { "foo": "bar", }), |
95 | a8950eb7 | Michael Hanselmann | "Basic foo=bar")
|
96 | a8950eb7 | Michael Hanselmann | self.assertEqual(http.auth._FormatAuthHeader("Basic", { "foo": "", }), |
97 | a8950eb7 | Michael Hanselmann | "Basic foo=\"\"")
|
98 | a8950eb7 | Michael Hanselmann | self.assertEqual(http.auth._FormatAuthHeader("Basic", { "foo": "x,y", }), |
99 | a8950eb7 | Michael Hanselmann | "Basic foo=\"x,y\"")
|
100 | a8950eb7 | Michael Hanselmann | params = { |
101 | a8950eb7 | Michael Hanselmann | "foo": "x,y", |
102 | a8950eb7 | Michael Hanselmann | "realm": "secure", |
103 | a8950eb7 | Michael Hanselmann | } |
104 | a8950eb7 | Michael Hanselmann | # It's a dict whose order isn't guaranteed, hence checking a list
|
105 | a8950eb7 | Michael Hanselmann | self.assert_(http.auth._FormatAuthHeader("Digest", params) in |
106 | a8950eb7 | Michael Hanselmann | ("Digest foo=\"x,y\" realm=secure",
|
107 | a8950eb7 | Michael Hanselmann | "Digest realm=secure foo=\"x,y\""))
|
108 | a8950eb7 | Michael Hanselmann | |
109 | a0638838 | Oleksiy Mishchenko | |
110 | bf9bd8dd | Michael Hanselmann | class _FakeRequestAuth(http.auth.HttpServerRequestAuthentication): |
111 | a8950eb7 | Michael Hanselmann | def __init__(self, realm, authreq, authenticate_fn): |
112 | bf9bd8dd | Michael Hanselmann | http.auth.HttpServerRequestAuthentication.__init__(self)
|
113 | bf9bd8dd | Michael Hanselmann | |
114 | bf9bd8dd | Michael Hanselmann | self.realm = realm
|
115 | a8950eb7 | Michael Hanselmann | self.authreq = authreq
|
116 | a8950eb7 | Michael Hanselmann | self.authenticate_fn = authenticate_fn
|
117 | a8950eb7 | Michael Hanselmann | |
118 | a8950eb7 | Michael Hanselmann | def AuthenticationRequired(self, req): |
119 | a8950eb7 | Michael Hanselmann | return self.authreq |
120 | bf9bd8dd | Michael Hanselmann | |
121 | bf9bd8dd | Michael Hanselmann | def GetAuthRealm(self, req): |
122 | bf9bd8dd | Michael Hanselmann | return self.realm |
123 | bf9bd8dd | Michael Hanselmann | |
124 | a8950eb7 | Michael Hanselmann | def Authenticate(self, *args): |
125 | a8950eb7 | Michael Hanselmann | if self.authenticate_fn: |
126 | a8950eb7 | Michael Hanselmann | return self.authenticate_fn(*args) |
127 | a8950eb7 | Michael Hanselmann | raise NotImplementedError() |
128 | a8950eb7 | Michael Hanselmann | |
129 | bf9bd8dd | Michael Hanselmann | |
130 | bf9bd8dd | Michael Hanselmann | class TestAuth(unittest.TestCase): |
131 | bf9bd8dd | Michael Hanselmann | """Authentication tests"""
|
132 | bf9bd8dd | Michael Hanselmann | |
133 | bf9bd8dd | Michael Hanselmann | hsra = http.auth.HttpServerRequestAuthentication |
134 | bf9bd8dd | Michael Hanselmann | |
135 | bf9bd8dd | Michael Hanselmann | def testConstants(self): |
136 | a8950eb7 | Michael Hanselmann | for scheme in [self.hsra._CLEARTEXT_SCHEME, self.hsra._HA1_SCHEME]: |
137 | a8950eb7 | Michael Hanselmann | self.assertEqual(scheme, scheme.upper())
|
138 | a8950eb7 | Michael Hanselmann | self.assert_(scheme.startswith("{")) |
139 | a8950eb7 | Michael Hanselmann | self.assert_(scheme.endswith("}")) |
140 | bf9bd8dd | Michael Hanselmann | |
141 | bf9bd8dd | Michael Hanselmann | def _testVerifyBasicAuthPassword(self, realm, user, password, expected): |
142 | a8950eb7 | Michael Hanselmann | ra = _FakeRequestAuth(realm, False, None) |
143 | bf9bd8dd | Michael Hanselmann | |
144 | bf9bd8dd | Michael Hanselmann | return ra.VerifyBasicAuthPassword(None, user, password, expected) |
145 | bf9bd8dd | Michael Hanselmann | |
146 | bf9bd8dd | Michael Hanselmann | def testVerifyBasicAuthPassword(self): |
147 | bf9bd8dd | Michael Hanselmann | tvbap = self._testVerifyBasicAuthPassword
|
148 | bf9bd8dd | Michael Hanselmann | |
149 | bf9bd8dd | Michael Hanselmann | good_pws = ["pw", "pw{", "pw}", "pw{}", "pw{x}y", "}pw", |
150 | bf9bd8dd | Michael Hanselmann | "0", "123", "foo...:xyz", "TeST"] |
151 | bf9bd8dd | Michael Hanselmann | |
152 | bf9bd8dd | Michael Hanselmann | for pw in good_pws: |
153 | bf9bd8dd | Michael Hanselmann | # Try cleartext passwords
|
154 | bf9bd8dd | Michael Hanselmann | self.assert_(tvbap("abc", "user", pw, pw)) |
155 | bf9bd8dd | Michael Hanselmann | self.assert_(tvbap("abc", "user", pw, "{cleartext}" + pw)) |
156 | bf9bd8dd | Michael Hanselmann | self.assert_(tvbap("abc", "user", pw, "{ClearText}" + pw)) |
157 | bf9bd8dd | Michael Hanselmann | self.assert_(tvbap("abc", "user", pw, "{CLEARTEXT}" + pw)) |
158 | bf9bd8dd | Michael Hanselmann | |
159 | bf9bd8dd | Michael Hanselmann | # Try with invalid password
|
160 | bf9bd8dd | Michael Hanselmann | self.failIf(tvbap("abc", "user", pw, "something")) |
161 | bf9bd8dd | Michael Hanselmann | |
162 | bf9bd8dd | Michael Hanselmann | # Try with invalid scheme
|
163 | bf9bd8dd | Michael Hanselmann | self.failIf(tvbap("abc", "user", pw, "{000}" + pw)) |
164 | bf9bd8dd | Michael Hanselmann | self.failIf(tvbap("abc", "user", pw, "{unk}" + pw)) |
165 | bf9bd8dd | Michael Hanselmann | self.failIf(tvbap("abc", "user", pw, "{Unk}" + pw)) |
166 | bf9bd8dd | Michael Hanselmann | self.failIf(tvbap("abc", "user", pw, "{UNK}" + pw)) |
167 | bf9bd8dd | Michael Hanselmann | |
168 | bf9bd8dd | Michael Hanselmann | # Try with invalid scheme format
|
169 | bf9bd8dd | Michael Hanselmann | self.failIf(tvbap("abc", "user", "pw", "{something")) |
170 | bf9bd8dd | Michael Hanselmann | |
171 | bf9bd8dd | Michael Hanselmann | # Hash is MD5("user:This is only a test:pw")
|
172 | bf9bd8dd | Michael Hanselmann | self.assert_(tvbap("This is only a test", "user", "pw", |
173 | bf9bd8dd | Michael Hanselmann | "{ha1}92ea58ae804481498c257b2f65561a17"))
|
174 | bf9bd8dd | Michael Hanselmann | self.assert_(tvbap("This is only a test", "user", "pw", |
175 | bf9bd8dd | Michael Hanselmann | "{HA1}92ea58ae804481498c257b2f65561a17"))
|
176 | bf9bd8dd | Michael Hanselmann | |
177 | 23ccba04 | Michael Hanselmann | self.failUnlessRaises(AssertionError, tvbap, None, "user", "pw", |
178 | 23ccba04 | Michael Hanselmann | "{HA1}92ea58ae804481498c257b2f65561a17")
|
179 | bf9bd8dd | Michael Hanselmann | self.failIf(tvbap("Admin area", "user", "pw", |
180 | bf9bd8dd | Michael Hanselmann | "{HA1}92ea58ae804481498c257b2f65561a17"))
|
181 | bf9bd8dd | Michael Hanselmann | self.failIf(tvbap("This is only a test", "someone", "pw", |
182 | bf9bd8dd | Michael Hanselmann | "{HA1}92ea58ae804481498c257b2f65561a17"))
|
183 | bf9bd8dd | Michael Hanselmann | self.failIf(tvbap("This is only a test", "user", "something", |
184 | bf9bd8dd | Michael Hanselmann | "{HA1}92ea58ae804481498c257b2f65561a17"))
|
185 | bf9bd8dd | Michael Hanselmann | |
186 | bf9bd8dd | Michael Hanselmann | |
187 | a8950eb7 | Michael Hanselmann | class _SimpleAuthenticator: |
188 | a8950eb7 | Michael Hanselmann | def __init__(self, user, password): |
189 | a8950eb7 | Michael Hanselmann | self.user = user
|
190 | a8950eb7 | Michael Hanselmann | self.password = password
|
191 | a8950eb7 | Michael Hanselmann | self.called = False |
192 | a8950eb7 | Michael Hanselmann | |
193 | a8950eb7 | Michael Hanselmann | def __call__(self, req, user, password): |
194 | a8950eb7 | Michael Hanselmann | self.called = True |
195 | a8950eb7 | Michael Hanselmann | return self.user == user and self.password == password |
196 | a8950eb7 | Michael Hanselmann | |
197 | a8950eb7 | Michael Hanselmann | |
198 | a8950eb7 | Michael Hanselmann | class TestHttpServerRequestAuthentication(unittest.TestCase): |
199 | a8950eb7 | Michael Hanselmann | def testNoAuth(self): |
200 | a8950eb7 | Michael Hanselmann | req = http.server._HttpServerRequest("GET", "/", None, None) |
201 | a8950eb7 | Michael Hanselmann | _FakeRequestAuth("area1", False, None).PreHandleRequest(req) |
202 | a8950eb7 | Michael Hanselmann | |
203 | a8950eb7 | Michael Hanselmann | def testNoRealm(self): |
204 | a8950eb7 | Michael Hanselmann | headers = { http.HTTP_AUTHORIZATION: "", }
|
205 | a8950eb7 | Michael Hanselmann | req = http.server._HttpServerRequest("GET", "/", headers, None) |
206 | a8950eb7 | Michael Hanselmann | ra = _FakeRequestAuth(None, False, None) |
207 | a8950eb7 | Michael Hanselmann | self.assertRaises(AssertionError, ra.PreHandleRequest, req) |
208 | a8950eb7 | Michael Hanselmann | |
209 | a8950eb7 | Michael Hanselmann | def testNoScheme(self): |
210 | a8950eb7 | Michael Hanselmann | headers = { http.HTTP_AUTHORIZATION: "", }
|
211 | a8950eb7 | Michael Hanselmann | req = http.server._HttpServerRequest("GET", "/", headers, None) |
212 | a8950eb7 | Michael Hanselmann | ra = _FakeRequestAuth("area1", False, None) |
213 | a8950eb7 | Michael Hanselmann | self.assertRaises(http.HttpUnauthorized, ra.PreHandleRequest, req)
|
214 | a8950eb7 | Michael Hanselmann | |
215 | a8950eb7 | Michael Hanselmann | def testUnknownScheme(self): |
216 | a8950eb7 | Michael Hanselmann | headers = { http.HTTP_AUTHORIZATION: "NewStyleAuth abc", }
|
217 | a8950eb7 | Michael Hanselmann | req = http.server._HttpServerRequest("GET", "/", headers, None) |
218 | a8950eb7 | Michael Hanselmann | ra = _FakeRequestAuth("area1", False, None) |
219 | a8950eb7 | Michael Hanselmann | self.assertRaises(http.HttpUnauthorized, ra.PreHandleRequest, req)
|
220 | a8950eb7 | Michael Hanselmann | |
221 | a8950eb7 | Michael Hanselmann | def testInvalidBase64(self): |
222 | a8950eb7 | Michael Hanselmann | headers = { http.HTTP_AUTHORIZATION: "Basic x_=_", }
|
223 | a8950eb7 | Michael Hanselmann | req = http.server._HttpServerRequest("GET", "/", headers, None) |
224 | a8950eb7 | Michael Hanselmann | ra = _FakeRequestAuth("area1", False, None) |
225 | a8950eb7 | Michael Hanselmann | self.assertRaises(http.HttpUnauthorized, ra.PreHandleRequest, req)
|
226 | a8950eb7 | Michael Hanselmann | |
227 | a8950eb7 | Michael Hanselmann | def testAuthForPublicResource(self): |
228 | a8950eb7 | Michael Hanselmann | headers = { |
229 | a8950eb7 | Michael Hanselmann | http.HTTP_AUTHORIZATION: "Basic %s" % ("foo".encode("base64").strip(), ), |
230 | a8950eb7 | Michael Hanselmann | } |
231 | a8950eb7 | Michael Hanselmann | req = http.server._HttpServerRequest("GET", "/", headers, None) |
232 | a8950eb7 | Michael Hanselmann | ra = _FakeRequestAuth("area1", False, None) |
233 | a8950eb7 | Michael Hanselmann | self.assertRaises(http.HttpUnauthorized, ra.PreHandleRequest, req)
|
234 | a8950eb7 | Michael Hanselmann | |
235 | a8950eb7 | Michael Hanselmann | def testAuthForPublicResource(self): |
236 | a8950eb7 | Michael Hanselmann | headers = { |
237 | a8950eb7 | Michael Hanselmann | http.HTTP_AUTHORIZATION: |
238 | a8950eb7 | Michael Hanselmann | "Basic %s" % ("foo:bar".encode("base64").strip(), ), |
239 | a8950eb7 | Michael Hanselmann | } |
240 | a8950eb7 | Michael Hanselmann | req = http.server._HttpServerRequest("GET", "/", headers, None) |
241 | a8950eb7 | Michael Hanselmann | ac = _SimpleAuthenticator("foo", "bar") |
242 | a8950eb7 | Michael Hanselmann | ra = _FakeRequestAuth("area1", False, ac) |
243 | a8950eb7 | Michael Hanselmann | ra.PreHandleRequest(req) |
244 | a8950eb7 | Michael Hanselmann | |
245 | a8950eb7 | Michael Hanselmann | req = http.server._HttpServerRequest("GET", "/", headers, None) |
246 | a8950eb7 | Michael Hanselmann | ac = _SimpleAuthenticator("something", "else") |
247 | a8950eb7 | Michael Hanselmann | ra = _FakeRequestAuth("area1", False, ac) |
248 | a8950eb7 | Michael Hanselmann | self.assertRaises(http.HttpUnauthorized, ra.PreHandleRequest, req)
|
249 | a8950eb7 | Michael Hanselmann | |
250 | a8950eb7 | Michael Hanselmann | def testInvalidRequestHeader(self): |
251 | a8950eb7 | Michael Hanselmann | checks = { |
252 | a8950eb7 | Michael Hanselmann | http.HttpUnauthorized: ["", "\t", "-", ".", "@", "<", ">", "Digest", |
253 | a8950eb7 | Michael Hanselmann | "basic %s" % "foobar".encode("base64").strip()], |
254 | a8950eb7 | Michael Hanselmann | http.HttpBadRequest: ["Basic"],
|
255 | a8950eb7 | Michael Hanselmann | } |
256 | a8950eb7 | Michael Hanselmann | |
257 | a8950eb7 | Michael Hanselmann | for exc, headers in checks.items(): |
258 | a8950eb7 | Michael Hanselmann | for i in headers: |
259 | a8950eb7 | Michael Hanselmann | headers = { http.HTTP_AUTHORIZATION: i, } |
260 | a8950eb7 | Michael Hanselmann | req = http.server._HttpServerRequest("GET", "/", headers, None) |
261 | a8950eb7 | Michael Hanselmann | ra = _FakeRequestAuth("area1", False, None) |
262 | a8950eb7 | Michael Hanselmann | self.assertRaises(exc, ra.PreHandleRequest, req)
|
263 | a8950eb7 | Michael Hanselmann | |
264 | a8950eb7 | Michael Hanselmann | def testBasicAuth(self): |
265 | a8950eb7 | Michael Hanselmann | for user in ["", "joe", "user name with spaces"]: |
266 | a8950eb7 | Michael Hanselmann | for pw in ["", "-", ":", "foobar", "Foo Bar Baz", "@@@", "###", |
267 | a8950eb7 | Michael Hanselmann | "foo:bar:baz"]:
|
268 | a8950eb7 | Michael Hanselmann | for wrong_pw in [True, False]: |
269 | a8950eb7 | Michael Hanselmann | basic_auth = "%s:%s" % (user, pw)
|
270 | a8950eb7 | Michael Hanselmann | if wrong_pw:
|
271 | a8950eb7 | Michael Hanselmann | basic_auth += "WRONG"
|
272 | a8950eb7 | Michael Hanselmann | headers = { |
273 | a8950eb7 | Michael Hanselmann | http.HTTP_AUTHORIZATION: |
274 | a8950eb7 | Michael Hanselmann | "Basic %s" % (basic_auth.encode("base64").strip(), ), |
275 | a8950eb7 | Michael Hanselmann | } |
276 | a8950eb7 | Michael Hanselmann | req = http.server._HttpServerRequest("GET", "/", headers, None) |
277 | a8950eb7 | Michael Hanselmann | |
278 | a8950eb7 | Michael Hanselmann | ac = _SimpleAuthenticator(user, pw) |
279 | a8950eb7 | Michael Hanselmann | self.assertFalse(ac.called)
|
280 | a8950eb7 | Michael Hanselmann | ra = _FakeRequestAuth("area1", True, ac) |
281 | a8950eb7 | Michael Hanselmann | if wrong_pw:
|
282 | a8950eb7 | Michael Hanselmann | try:
|
283 | a8950eb7 | Michael Hanselmann | ra.PreHandleRequest(req) |
284 | a8950eb7 | Michael Hanselmann | except http.HttpUnauthorized, err:
|
285 | a8950eb7 | Michael Hanselmann | www_auth = err.headers[http.HTTP_WWW_AUTHENTICATE] |
286 | a8950eb7 | Michael Hanselmann | self.assert_(www_auth.startswith(http.auth.HTTP_BASIC_AUTH))
|
287 | a8950eb7 | Michael Hanselmann | else:
|
288 | a8950eb7 | Michael Hanselmann | self.fail("Didn't raise HttpUnauthorized") |
289 | a8950eb7 | Michael Hanselmann | else:
|
290 | a8950eb7 | Michael Hanselmann | ra.PreHandleRequest(req) |
291 | a8950eb7 | Michael Hanselmann | self.assert_(ac.called)
|
292 | a8950eb7 | Michael Hanselmann | |
293 | a8950eb7 | Michael Hanselmann | |
294 | 2287b920 | Michael Hanselmann | class TestReadPasswordFile(unittest.TestCase): |
295 | a8950eb7 | Michael Hanselmann | def testSimple(self): |
296 | 2287b920 | Michael Hanselmann | users = http.auth.ParsePasswordFile("user1 password")
|
297 | a8950eb7 | Michael Hanselmann | self.assertEqual(len(users), 1) |
298 | a8950eb7 | Michael Hanselmann | self.assertEqual(users["user1"].password, "password") |
299 | a8950eb7 | Michael Hanselmann | self.assertEqual(len(users["user1"].options), 0) |
300 | a8950eb7 | Michael Hanselmann | |
301 | a8950eb7 | Michael Hanselmann | def testOptions(self): |
302 | 2287b920 | Michael Hanselmann | buf = StringIO() |
303 | 2287b920 | Michael Hanselmann | buf.write("# Passwords\n")
|
304 | 2287b920 | Michael Hanselmann | buf.write("user1 password\n")
|
305 | 2287b920 | Michael Hanselmann | buf.write("\n")
|
306 | 2287b920 | Michael Hanselmann | buf.write("# Comment\n")
|
307 | 2287b920 | Michael Hanselmann | buf.write("user2 pw write,read\n")
|
308 | 2287b920 | Michael Hanselmann | buf.write(" \t# Another comment\n")
|
309 | 2287b920 | Michael Hanselmann | buf.write("invalidline\n")
|
310 | 2287b920 | Michael Hanselmann | |
311 | 2287b920 | Michael Hanselmann | users = http.auth.ParsePasswordFile(buf.getvalue()) |
312 | a8950eb7 | Michael Hanselmann | self.assertEqual(len(users), 2) |
313 | a8950eb7 | Michael Hanselmann | self.assertEqual(users["user1"].password, "password") |
314 | a8950eb7 | Michael Hanselmann | self.assertEqual(len(users["user1"].options), 0) |
315 | a8950eb7 | Michael Hanselmann | |
316 | a8950eb7 | Michael Hanselmann | self.assertEqual(users["user2"].password, "pw") |
317 | a8950eb7 | Michael Hanselmann | self.assertEqual(users["user2"].options, ["write", "read"]) |
318 | a8950eb7 | Michael Hanselmann | |
319 | a8950eb7 | Michael Hanselmann | |
320 | 33231500 | Michael Hanselmann | class TestClientRequest(unittest.TestCase): |
321 | 33231500 | Michael Hanselmann | def testRepr(self): |
322 | 33231500 | Michael Hanselmann | cr = http.client.HttpClientRequest("localhost", 1234, "GET", "/version", |
323 | 33231500 | Michael Hanselmann | headers=[], post_data="Hello World")
|
324 | 33231500 | Michael Hanselmann | self.assert_(repr(cr).startswith("<")) |
325 | 33231500 | Michael Hanselmann | |
326 | 33231500 | Michael Hanselmann | def testNoHeaders(self): |
327 | 33231500 | Michael Hanselmann | cr = http.client.HttpClientRequest("localhost", 1234, "GET", "/version", |
328 | 33231500 | Michael Hanselmann | headers=None)
|
329 | 33231500 | Michael Hanselmann | self.assert_(isinstance(cr.headers, list)) |
330 | 33231500 | Michael Hanselmann | self.assertEqual(cr.headers, [])
|
331 | 33231500 | Michael Hanselmann | self.assertEqual(cr.url, "https://localhost:1234/version") |
332 | 33231500 | Michael Hanselmann | |
333 | 33231500 | Michael Hanselmann | def testOldStyleHeaders(self): |
334 | 33231500 | Michael Hanselmann | headers = { |
335 | 33231500 | Michael Hanselmann | "Content-type": "text/plain", |
336 | 33231500 | Michael Hanselmann | "Accept": "text/html", |
337 | 33231500 | Michael Hanselmann | } |
338 | 33231500 | Michael Hanselmann | cr = http.client.HttpClientRequest("localhost", 16481, "GET", "/vg_list", |
339 | 33231500 | Michael Hanselmann | headers=headers) |
340 | 33231500 | Michael Hanselmann | self.assert_(isinstance(cr.headers, list)) |
341 | 33231500 | Michael Hanselmann | self.assertEqual(sorted(cr.headers), [ |
342 | 33231500 | Michael Hanselmann | "Accept: text/html",
|
343 | 33231500 | Michael Hanselmann | "Content-type: text/plain",
|
344 | 33231500 | Michael Hanselmann | ]) |
345 | 33231500 | Michael Hanselmann | self.assertEqual(cr.url, "https://localhost:16481/vg_list") |
346 | 33231500 | Michael Hanselmann | |
347 | 33231500 | Michael Hanselmann | def testNewStyleHeaders(self): |
348 | 33231500 | Michael Hanselmann | headers = [ |
349 | 33231500 | Michael Hanselmann | "Accept: text/html",
|
350 | 33231500 | Michael Hanselmann | "Content-type: text/plain; charset=ascii",
|
351 | 33231500 | Michael Hanselmann | "Server: httpd 1.0",
|
352 | 33231500 | Michael Hanselmann | ] |
353 | 33231500 | Michael Hanselmann | cr = http.client.HttpClientRequest("localhost", 1234, "GET", "/version", |
354 | 33231500 | Michael Hanselmann | headers=headers) |
355 | 33231500 | Michael Hanselmann | self.assert_(isinstance(cr.headers, list)) |
356 | 33231500 | Michael Hanselmann | self.assertEqual(sorted(cr.headers), sorted(headers)) |
357 | 33231500 | Michael Hanselmann | self.assertEqual(cr.url, "https://localhost:1234/version") |
358 | 33231500 | Michael Hanselmann | |
359 | 33231500 | Michael Hanselmann | def testPostData(self): |
360 | 33231500 | Michael Hanselmann | cr = http.client.HttpClientRequest("localhost", 1234, "GET", "/version", |
361 | 33231500 | Michael Hanselmann | post_data="Hello World")
|
362 | 33231500 | Michael Hanselmann | self.assertEqual(cr.post_data, "Hello World") |
363 | 33231500 | Michael Hanselmann | |
364 | 33231500 | Michael Hanselmann | def testNoPostData(self): |
365 | 33231500 | Michael Hanselmann | cr = http.client.HttpClientRequest("localhost", 1234, "GET", "/version") |
366 | 33231500 | Michael Hanselmann | self.assertEqual(cr.post_data, "") |
367 | 33231500 | Michael Hanselmann | |
368 | 33231500 | Michael Hanselmann | def testIdentity(self): |
369 | 33231500 | Michael Hanselmann | # These should all use different connections, hence also have a different
|
370 | 33231500 | Michael Hanselmann | # identity
|
371 | 33231500 | Michael Hanselmann | cr1 = http.client.HttpClientRequest("localhost", 1234, "GET", "/version") |
372 | 33231500 | Michael Hanselmann | cr2 = http.client.HttpClientRequest("localhost", 9999, "GET", "/version") |
373 | 33231500 | Michael Hanselmann | cr3 = http.client.HttpClientRequest("node1", 1234, "GET", "/version") |
374 | 33231500 | Michael Hanselmann | cr4 = http.client.HttpClientRequest("node1", 9999, "GET", "/version") |
375 | 33231500 | Michael Hanselmann | |
376 | 33231500 | Michael Hanselmann | self.assertEqual(len(set([cr1.identity, cr2.identity, |
377 | 33231500 | Michael Hanselmann | cr3.identity, cr4.identity])), 4)
|
378 | 33231500 | Michael Hanselmann | |
379 | 33231500 | Michael Hanselmann | # But this one should have the same
|
380 | 33231500 | Michael Hanselmann | cr1vglist = http.client.HttpClientRequest("localhost", 1234, |
381 | 33231500 | Michael Hanselmann | "GET", "/vg_list") |
382 | 33231500 | Michael Hanselmann | self.assertEqual(cr1.identity, cr1vglist.identity)
|
383 | 33231500 | Michael Hanselmann | |
384 | 33231500 | Michael Hanselmann | |
385 | 33231500 | Michael Hanselmann | class TestClient(unittest.TestCase): |
386 | 33231500 | Michael Hanselmann | def test(self): |
387 | 33231500 | Michael Hanselmann | pool = http.client.HttpClientPool(None)
|
388 | 33231500 | Michael Hanselmann | self.assertFalse(pool._pool)
|
389 | 33231500 | Michael Hanselmann | |
390 | 33231500 | Michael Hanselmann | |
391 | a0638838 | Oleksiy Mishchenko | if __name__ == '__main__': |
392 | 25231ec5 | Michael Hanselmann | testutils.GanetiTestProgram() |