root / test / ganeti.http_unittest.py @ fabee4b2
History | View | Annotate | Download (6.1 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 | a0638838 | Oleksiy Mishchenko | |
29 | a0638838 | Oleksiy Mishchenko | from ganeti import http |
30 | a0638838 | Oleksiy Mishchenko | |
31 | f30ca1e6 | Michael Hanselmann | import ganeti.http.server |
32 | f30ca1e6 | Michael Hanselmann | import ganeti.http.client |
33 | bf9bd8dd | Michael Hanselmann | import ganeti.http.auth |
34 | a0638838 | Oleksiy Mishchenko | |
35 | 25231ec5 | Michael Hanselmann | import testutils |
36 | 25231ec5 | Michael Hanselmann | |
37 | f30ca1e6 | Michael Hanselmann | |
38 | f30ca1e6 | Michael Hanselmann | class TestStartLines(unittest.TestCase): |
39 | f30ca1e6 | Michael Hanselmann | """Test cases for start line classes"""
|
40 | f30ca1e6 | Michael Hanselmann | |
41 | f30ca1e6 | Michael Hanselmann | def testClientToServerStartLine(self): |
42 | f30ca1e6 | Michael Hanselmann | """Test client to server start line (HTTP request)"""
|
43 | f30ca1e6 | Michael Hanselmann | start_line = http.HttpClientToServerStartLine("GET", "/", "HTTP/1.1") |
44 | f30ca1e6 | Michael Hanselmann | self.assertEqual(str(start_line), "GET / HTTP/1.1") |
45 | f30ca1e6 | Michael Hanselmann | |
46 | f30ca1e6 | Michael Hanselmann | def testServerToClientStartLine(self): |
47 | f30ca1e6 | Michael Hanselmann | """Test server to client start line (HTTP response)"""
|
48 | f30ca1e6 | Michael Hanselmann | start_line = http.HttpServerToClientStartLine("HTTP/1.1", 200, "OK") |
49 | f30ca1e6 | Michael Hanselmann | self.assertEqual(str(start_line), "HTTP/1.1 200 OK") |
50 | f30ca1e6 | Michael Hanselmann | |
51 | f30ca1e6 | Michael Hanselmann | |
52 | f30ca1e6 | Michael Hanselmann | class TestMisc(unittest.TestCase): |
53 | f30ca1e6 | Michael Hanselmann | """Miscellaneous tests"""
|
54 | f30ca1e6 | Michael Hanselmann | |
55 | f30ca1e6 | Michael Hanselmann | def _TestDateTimeHeader(self, gmnow, expected): |
56 | f30ca1e6 | Michael Hanselmann | self.assertEqual(http.server._DateTimeHeader(gmnow=gmnow), expected)
|
57 | f30ca1e6 | Michael Hanselmann | |
58 | f30ca1e6 | Michael Hanselmann | def testDateTimeHeader(self): |
59 | f30ca1e6 | Michael Hanselmann | """Test ganeti.http._DateTimeHeader"""
|
60 | f30ca1e6 | Michael Hanselmann | self._TestDateTimeHeader((2008, 1, 2, 3, 4, 5, 3, 0, 0), |
61 | f30ca1e6 | Michael Hanselmann | "Thu, 02 Jan 2008 03:04:05 GMT")
|
62 | f30ca1e6 | Michael Hanselmann | self._TestDateTimeHeader((2008, 1, 1, 0, 0, 0, 0, 0, 0), |
63 | f30ca1e6 | Michael Hanselmann | "Mon, 01 Jan 2008 00:00:00 GMT")
|
64 | f30ca1e6 | Michael Hanselmann | self._TestDateTimeHeader((2008, 12, 31, 0, 0, 0, 0, 0, 0), |
65 | f30ca1e6 | Michael Hanselmann | "Mon, 31 Dec 2008 00:00:00 GMT")
|
66 | f30ca1e6 | Michael Hanselmann | self._TestDateTimeHeader((2008, 12, 31, 23, 59, 59, 0, 0, 0), |
67 | f30ca1e6 | Michael Hanselmann | "Mon, 31 Dec 2008 23:59:59 GMT")
|
68 | f30ca1e6 | Michael Hanselmann | self._TestDateTimeHeader((2008, 12, 31, 0, 0, 0, 6, 0, 0), |
69 | f30ca1e6 | Michael Hanselmann | "Sun, 31 Dec 2008 00:00:00 GMT")
|
70 | f30ca1e6 | Michael Hanselmann | |
71 | f30ca1e6 | Michael Hanselmann | def testHttpServerRequest(self): |
72 | f30ca1e6 | Michael Hanselmann | """Test ganeti.http.server._HttpServerRequest"""
|
73 | f30ca1e6 | Michael Hanselmann | fake_request = http.HttpMessage() |
74 | f30ca1e6 | Michael Hanselmann | fake_request.start_line = \ |
75 | f30ca1e6 | Michael Hanselmann | http.HttpClientToServerStartLine("GET", "/", "HTTP/1.1") |
76 | f30ca1e6 | Michael Hanselmann | server_request = http.server._HttpServerRequest(fake_request) |
77 | f30ca1e6 | Michael Hanselmann | |
78 | f30ca1e6 | Michael Hanselmann | # These are expected by users of the HTTP server
|
79 | f30ca1e6 | Michael Hanselmann | self.assert_(hasattr(server_request, "request_method")) |
80 | f30ca1e6 | Michael Hanselmann | self.assert_(hasattr(server_request, "request_path")) |
81 | f30ca1e6 | Michael Hanselmann | self.assert_(hasattr(server_request, "request_headers")) |
82 | f30ca1e6 | Michael Hanselmann | self.assert_(hasattr(server_request, "request_body")) |
83 | f30ca1e6 | Michael Hanselmann | self.assert_(isinstance(server_request.resp_headers, dict)) |
84 | 68fa9caf | Michael Hanselmann | self.assert_(hasattr(server_request, "private")) |
85 | f30ca1e6 | Michael Hanselmann | |
86 | f30ca1e6 | Michael Hanselmann | def testServerSizeLimits(self): |
87 | f30ca1e6 | Michael Hanselmann | """Test HTTP server size limits"""
|
88 | f30ca1e6 | Michael Hanselmann | message_reader_class = http.server._HttpClientToServerMessageReader |
89 | f30ca1e6 | Michael Hanselmann | self.assert_(message_reader_class.START_LINE_LENGTH_MAX > 0) |
90 | f30ca1e6 | Michael Hanselmann | self.assert_(message_reader_class.HEADER_LENGTH_MAX > 0) |
91 | f30ca1e6 | Michael Hanselmann | |
92 | f30ca1e6 | Michael Hanselmann | def testClientSizeLimits(self): |
93 | f30ca1e6 | Michael Hanselmann | """Test HTTP client size limits"""
|
94 | f30ca1e6 | Michael Hanselmann | message_reader_class = http.client._HttpServerToClientMessageReader |
95 | f30ca1e6 | Michael Hanselmann | self.assert_(message_reader_class.START_LINE_LENGTH_MAX > 0) |
96 | f30ca1e6 | Michael Hanselmann | self.assert_(message_reader_class.HEADER_LENGTH_MAX > 0) |
97 | a0638838 | Oleksiy Mishchenko | |
98 | a0638838 | Oleksiy Mishchenko | |
99 | bf9bd8dd | Michael Hanselmann | class _FakeRequestAuth(http.auth.HttpServerRequestAuthentication): |
100 | bf9bd8dd | Michael Hanselmann | def __init__(self, realm): |
101 | bf9bd8dd | Michael Hanselmann | http.auth.HttpServerRequestAuthentication.__init__(self)
|
102 | bf9bd8dd | Michael Hanselmann | |
103 | bf9bd8dd | Michael Hanselmann | self.realm = realm
|
104 | bf9bd8dd | Michael Hanselmann | |
105 | bf9bd8dd | Michael Hanselmann | def GetAuthRealm(self, req): |
106 | bf9bd8dd | Michael Hanselmann | return self.realm |
107 | bf9bd8dd | Michael Hanselmann | |
108 | bf9bd8dd | Michael Hanselmann | |
109 | bf9bd8dd | Michael Hanselmann | class TestAuth(unittest.TestCase): |
110 | bf9bd8dd | Michael Hanselmann | """Authentication tests"""
|
111 | bf9bd8dd | Michael Hanselmann | |
112 | bf9bd8dd | Michael Hanselmann | hsra = http.auth.HttpServerRequestAuthentication |
113 | bf9bd8dd | Michael Hanselmann | |
114 | bf9bd8dd | Michael Hanselmann | def testConstants(self): |
115 | bf9bd8dd | Michael Hanselmann | self.assertEqual(self.hsra._CLEARTEXT_SCHEME, |
116 | bf9bd8dd | Michael Hanselmann | self.hsra._CLEARTEXT_SCHEME.upper())
|
117 | bf9bd8dd | Michael Hanselmann | self.assertEqual(self.hsra._HA1_SCHEME, |
118 | bf9bd8dd | Michael Hanselmann | self.hsra._HA1_SCHEME.upper())
|
119 | bf9bd8dd | Michael Hanselmann | |
120 | bf9bd8dd | Michael Hanselmann | def _testVerifyBasicAuthPassword(self, realm, user, password, expected): |
121 | bf9bd8dd | Michael Hanselmann | ra = _FakeRequestAuth(realm) |
122 | bf9bd8dd | Michael Hanselmann | |
123 | bf9bd8dd | Michael Hanselmann | return ra.VerifyBasicAuthPassword(None, user, password, expected) |
124 | bf9bd8dd | Michael Hanselmann | |
125 | bf9bd8dd | Michael Hanselmann | |
126 | bf9bd8dd | Michael Hanselmann | def testVerifyBasicAuthPassword(self): |
127 | bf9bd8dd | Michael Hanselmann | tvbap = self._testVerifyBasicAuthPassword
|
128 | bf9bd8dd | Michael Hanselmann | |
129 | bf9bd8dd | Michael Hanselmann | good_pws = ["pw", "pw{", "pw}", "pw{}", "pw{x}y", "}pw", |
130 | bf9bd8dd | Michael Hanselmann | "0", "123", "foo...:xyz", "TeST"] |
131 | bf9bd8dd | Michael Hanselmann | |
132 | bf9bd8dd | Michael Hanselmann | for pw in good_pws: |
133 | bf9bd8dd | Michael Hanselmann | # Try cleartext passwords
|
134 | bf9bd8dd | Michael Hanselmann | self.assert_(tvbap("abc", "user", pw, pw)) |
135 | bf9bd8dd | Michael Hanselmann | self.assert_(tvbap("abc", "user", pw, "{cleartext}" + pw)) |
136 | bf9bd8dd | Michael Hanselmann | self.assert_(tvbap("abc", "user", pw, "{ClearText}" + pw)) |
137 | bf9bd8dd | Michael Hanselmann | self.assert_(tvbap("abc", "user", pw, "{CLEARTEXT}" + pw)) |
138 | bf9bd8dd | Michael Hanselmann | |
139 | bf9bd8dd | Michael Hanselmann | # Try with invalid password
|
140 | bf9bd8dd | Michael Hanselmann | self.failIf(tvbap("abc", "user", pw, "something")) |
141 | bf9bd8dd | Michael Hanselmann | |
142 | bf9bd8dd | Michael Hanselmann | # Try with invalid scheme
|
143 | bf9bd8dd | Michael Hanselmann | self.failIf(tvbap("abc", "user", pw, "{000}" + pw)) |
144 | bf9bd8dd | Michael Hanselmann | self.failIf(tvbap("abc", "user", pw, "{unk}" + pw)) |
145 | bf9bd8dd | Michael Hanselmann | self.failIf(tvbap("abc", "user", pw, "{Unk}" + pw)) |
146 | bf9bd8dd | Michael Hanselmann | self.failIf(tvbap("abc", "user", pw, "{UNK}" + pw)) |
147 | bf9bd8dd | Michael Hanselmann | |
148 | bf9bd8dd | Michael Hanselmann | # Try with invalid scheme format
|
149 | bf9bd8dd | Michael Hanselmann | self.failIf(tvbap("abc", "user", "pw", "{something")) |
150 | bf9bd8dd | Michael Hanselmann | |
151 | bf9bd8dd | Michael Hanselmann | # Hash is MD5("user:This is only a test:pw")
|
152 | bf9bd8dd | Michael Hanselmann | self.assert_(tvbap("This is only a test", "user", "pw", |
153 | bf9bd8dd | Michael Hanselmann | "{ha1}92ea58ae804481498c257b2f65561a17"))
|
154 | bf9bd8dd | Michael Hanselmann | self.assert_(tvbap("This is only a test", "user", "pw", |
155 | bf9bd8dd | Michael Hanselmann | "{HA1}92ea58ae804481498c257b2f65561a17"))
|
156 | bf9bd8dd | Michael Hanselmann | |
157 | bf9bd8dd | Michael Hanselmann | self.failIf(tvbap(None, "user", "pw", |
158 | bf9bd8dd | Michael Hanselmann | "{HA1}92ea58ae804481498c257b2f65561a17"))
|
159 | bf9bd8dd | Michael Hanselmann | self.failIf(tvbap("Admin area", "user", "pw", |
160 | bf9bd8dd | Michael Hanselmann | "{HA1}92ea58ae804481498c257b2f65561a17"))
|
161 | bf9bd8dd | Michael Hanselmann | self.failIf(tvbap("This is only a test", "someone", "pw", |
162 | bf9bd8dd | Michael Hanselmann | "{HA1}92ea58ae804481498c257b2f65561a17"))
|
163 | bf9bd8dd | Michael Hanselmann | self.failIf(tvbap("This is only a test", "user", "something", |
164 | bf9bd8dd | Michael Hanselmann | "{HA1}92ea58ae804481498c257b2f65561a17"))
|
165 | bf9bd8dd | Michael Hanselmann | |
166 | bf9bd8dd | Michael Hanselmann | |
167 | a0638838 | Oleksiy Mishchenko | if __name__ == '__main__': |
168 | 25231ec5 | Michael Hanselmann | testutils.GanetiTestProgram() |