root / test / ganeti.http_unittest.py @ f93427cd
History | View | Annotate | Download (6.1 kB)
1 |
#!/usr/bin/python
|
---|---|
2 |
#
|
3 |
|
4 |
# Copyright (C) 2007, 2008 Google Inc.
|
5 |
#
|
6 |
# This program is free software; you can redistribute it and/or modify
|
7 |
# it under the terms of the GNU General Public License as published by
|
8 |
# the Free Software Foundation; either version 2 of the License, or
|
9 |
# (at your option) any later version.
|
10 |
#
|
11 |
# This program is distributed in the hope that it will be useful, but
|
12 |
# WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
14 |
# General Public License for more details.
|
15 |
#
|
16 |
# You should have received a copy of the GNU General Public License
|
17 |
# along with this program; if not, write to the Free Software
|
18 |
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
|
19 |
# 02110-1301, USA.
|
20 |
|
21 |
|
22 |
"""Script for unittesting the http module"""
|
23 |
|
24 |
|
25 |
import os |
26 |
import unittest |
27 |
import time |
28 |
|
29 |
from ganeti import http |
30 |
|
31 |
import ganeti.http.server |
32 |
import ganeti.http.client |
33 |
import ganeti.http.auth |
34 |
|
35 |
import testutils |
36 |
|
37 |
|
38 |
class TestStartLines(unittest.TestCase): |
39 |
"""Test cases for start line classes"""
|
40 |
|
41 |
def testClientToServerStartLine(self): |
42 |
"""Test client to server start line (HTTP request)"""
|
43 |
start_line = http.HttpClientToServerStartLine("GET", "/", "HTTP/1.1") |
44 |
self.assertEqual(str(start_line), "GET / HTTP/1.1") |
45 |
|
46 |
def testServerToClientStartLine(self): |
47 |
"""Test server to client start line (HTTP response)"""
|
48 |
start_line = http.HttpServerToClientStartLine("HTTP/1.1", 200, "OK") |
49 |
self.assertEqual(str(start_line), "HTTP/1.1 200 OK") |
50 |
|
51 |
|
52 |
class TestMisc(unittest.TestCase): |
53 |
"""Miscellaneous tests"""
|
54 |
|
55 |
def _TestDateTimeHeader(self, gmnow, expected): |
56 |
self.assertEqual(http.server._DateTimeHeader(gmnow=gmnow), expected)
|
57 |
|
58 |
def testDateTimeHeader(self): |
59 |
"""Test ganeti.http._DateTimeHeader"""
|
60 |
self._TestDateTimeHeader((2008, 1, 2, 3, 4, 5, 3, 0, 0), |
61 |
"Thu, 02 Jan 2008 03:04:05 GMT")
|
62 |
self._TestDateTimeHeader((2008, 1, 1, 0, 0, 0, 0, 0, 0), |
63 |
"Mon, 01 Jan 2008 00:00:00 GMT")
|
64 |
self._TestDateTimeHeader((2008, 12, 31, 0, 0, 0, 0, 0, 0), |
65 |
"Mon, 31 Dec 2008 00:00:00 GMT")
|
66 |
self._TestDateTimeHeader((2008, 12, 31, 23, 59, 59, 0, 0, 0), |
67 |
"Mon, 31 Dec 2008 23:59:59 GMT")
|
68 |
self._TestDateTimeHeader((2008, 12, 31, 0, 0, 0, 6, 0, 0), |
69 |
"Sun, 31 Dec 2008 00:00:00 GMT")
|
70 |
|
71 |
def testHttpServerRequest(self): |
72 |
"""Test ganeti.http.server._HttpServerRequest"""
|
73 |
fake_request = http.HttpMessage() |
74 |
fake_request.start_line = \ |
75 |
http.HttpClientToServerStartLine("GET", "/", "HTTP/1.1") |
76 |
server_request = http.server._HttpServerRequest(fake_request) |
77 |
|
78 |
# These are expected by users of the HTTP server
|
79 |
self.assert_(hasattr(server_request, "request_method")) |
80 |
self.assert_(hasattr(server_request, "request_path")) |
81 |
self.assert_(hasattr(server_request, "request_headers")) |
82 |
self.assert_(hasattr(server_request, "request_body")) |
83 |
self.assert_(isinstance(server_request.resp_headers, dict)) |
84 |
self.assert_(hasattr(server_request, "private")) |
85 |
|
86 |
def testServerSizeLimits(self): |
87 |
"""Test HTTP server size limits"""
|
88 |
message_reader_class = http.server._HttpClientToServerMessageReader |
89 |
self.assert_(message_reader_class.START_LINE_LENGTH_MAX > 0) |
90 |
self.assert_(message_reader_class.HEADER_LENGTH_MAX > 0) |
91 |
|
92 |
def testClientSizeLimits(self): |
93 |
"""Test HTTP client size limits"""
|
94 |
message_reader_class = http.client._HttpServerToClientMessageReader |
95 |
self.assert_(message_reader_class.START_LINE_LENGTH_MAX > 0) |
96 |
self.assert_(message_reader_class.HEADER_LENGTH_MAX > 0) |
97 |
|
98 |
|
99 |
class _FakeRequestAuth(http.auth.HttpServerRequestAuthentication): |
100 |
def __init__(self, realm): |
101 |
http.auth.HttpServerRequestAuthentication.__init__(self)
|
102 |
|
103 |
self.realm = realm
|
104 |
|
105 |
def GetAuthRealm(self, req): |
106 |
return self.realm |
107 |
|
108 |
|
109 |
class TestAuth(unittest.TestCase): |
110 |
"""Authentication tests"""
|
111 |
|
112 |
hsra = http.auth.HttpServerRequestAuthentication |
113 |
|
114 |
def testConstants(self): |
115 |
self.assertEqual(self.hsra._CLEARTEXT_SCHEME, |
116 |
self.hsra._CLEARTEXT_SCHEME.upper())
|
117 |
self.assertEqual(self.hsra._HA1_SCHEME, |
118 |
self.hsra._HA1_SCHEME.upper())
|
119 |
|
120 |
def _testVerifyBasicAuthPassword(self, realm, user, password, expected): |
121 |
ra = _FakeRequestAuth(realm) |
122 |
|
123 |
return ra.VerifyBasicAuthPassword(None, user, password, expected) |
124 |
|
125 |
|
126 |
def testVerifyBasicAuthPassword(self): |
127 |
tvbap = self._testVerifyBasicAuthPassword
|
128 |
|
129 |
good_pws = ["pw", "pw{", "pw}", "pw{}", "pw{x}y", "}pw", |
130 |
"0", "123", "foo...:xyz", "TeST"] |
131 |
|
132 |
for pw in good_pws: |
133 |
# Try cleartext passwords
|
134 |
self.assert_(tvbap("abc", "user", pw, pw)) |
135 |
self.assert_(tvbap("abc", "user", pw, "{cleartext}" + pw)) |
136 |
self.assert_(tvbap("abc", "user", pw, "{ClearText}" + pw)) |
137 |
self.assert_(tvbap("abc", "user", pw, "{CLEARTEXT}" + pw)) |
138 |
|
139 |
# Try with invalid password
|
140 |
self.failIf(tvbap("abc", "user", pw, "something")) |
141 |
|
142 |
# Try with invalid scheme
|
143 |
self.failIf(tvbap("abc", "user", pw, "{000}" + pw)) |
144 |
self.failIf(tvbap("abc", "user", pw, "{unk}" + pw)) |
145 |
self.failIf(tvbap("abc", "user", pw, "{Unk}" + pw)) |
146 |
self.failIf(tvbap("abc", "user", pw, "{UNK}" + pw)) |
147 |
|
148 |
# Try with invalid scheme format
|
149 |
self.failIf(tvbap("abc", "user", "pw", "{something")) |
150 |
|
151 |
# Hash is MD5("user:This is only a test:pw")
|
152 |
self.assert_(tvbap("This is only a test", "user", "pw", |
153 |
"{ha1}92ea58ae804481498c257b2f65561a17"))
|
154 |
self.assert_(tvbap("This is only a test", "user", "pw", |
155 |
"{HA1}92ea58ae804481498c257b2f65561a17"))
|
156 |
|
157 |
self.failIf(tvbap(None, "user", "pw", |
158 |
"{HA1}92ea58ae804481498c257b2f65561a17"))
|
159 |
self.failIf(tvbap("Admin area", "user", "pw", |
160 |
"{HA1}92ea58ae804481498c257b2f65561a17"))
|
161 |
self.failIf(tvbap("This is only a test", "someone", "pw", |
162 |
"{HA1}92ea58ae804481498c257b2f65561a17"))
|
163 |
self.failIf(tvbap("This is only a test", "user", "something", |
164 |
"{HA1}92ea58ae804481498c257b2f65561a17"))
|
165 |
|
166 |
|
167 |
if __name__ == '__main__': |
168 |
testutils.GanetiTestProgram() |