2012-12-30 21:34:25 +00:00
|
|
|
import binascii
|
2012-12-30 20:15:56 +00:00
|
|
|
from libmproxy import authentication
|
|
|
|
from netlib import odict
|
2012-12-30 21:34:25 +00:00
|
|
|
import tutils
|
2012-12-30 20:15:56 +00:00
|
|
|
|
|
|
|
|
|
|
|
class TestNullProxyAuth:
|
|
|
|
def test_simple(self):
|
|
|
|
na = authentication.NullProxyAuth(authentication.PermissivePasswordManager())
|
|
|
|
assert not na.auth_challenge_headers()
|
|
|
|
assert na.authenticate("foo")
|
2012-12-30 21:56:44 +00:00
|
|
|
na.clean({})
|
2012-12-30 20:15:56 +00:00
|
|
|
|
|
|
|
|
|
|
|
class TestBasicProxyAuth:
|
|
|
|
def test_simple(self):
|
2012-12-30 21:56:44 +00:00
|
|
|
ba = authentication.BasicProxyAuth(authentication.PermissivePasswordManager(), "test")
|
2012-12-30 20:15:56 +00:00
|
|
|
h = odict.ODictCaseless()
|
|
|
|
assert ba.auth_challenge_headers()
|
|
|
|
assert not ba.authenticate(h)
|
|
|
|
|
2012-12-30 21:34:25 +00:00
|
|
|
def test_parse_auth_value(self):
|
2012-12-30 21:56:44 +00:00
|
|
|
ba = authentication.BasicProxyAuth(authentication.PermissivePasswordManager(), "test")
|
2012-12-30 21:34:25 +00:00
|
|
|
vals = ("basic", "foo", "bar")
|
|
|
|
assert ba.parse_auth_value(ba.unparse_auth_value(*vals)) == vals
|
|
|
|
tutils.raises(ValueError, ba.parse_auth_value, "")
|
|
|
|
tutils.raises(ValueError, ba.parse_auth_value, "foo bar")
|
|
|
|
|
|
|
|
v = "basic " + binascii.b2a_base64("foo")
|
|
|
|
tutils.raises(ValueError, ba.parse_auth_value, v)
|
|
|
|
|
2012-12-30 21:56:44 +00:00
|
|
|
def test_authenticate_clean(self):
|
|
|
|
ba = authentication.BasicProxyAuth(authentication.PermissivePasswordManager(), "test")
|
|
|
|
|
|
|
|
hdrs = odict.ODictCaseless()
|
|
|
|
vals = ("basic", "foo", "bar")
|
|
|
|
hdrs[ba.AUTH_HEADER] = [ba.unparse_auth_value(*vals)]
|
|
|
|
assert ba.authenticate(hdrs)
|
|
|
|
|
|
|
|
ba.clean(hdrs)
|
|
|
|
assert not ba.AUTH_HEADER in hdrs
|
|
|
|
|
|
|
|
|
|
|
|
hdrs[ba.AUTH_HEADER] = [""]
|
|
|
|
assert not ba.authenticate(hdrs)
|
|
|
|
|
|
|
|
hdrs[ba.AUTH_HEADER] = ["foo"]
|
|
|
|
assert not ba.authenticate(hdrs)
|
|
|
|
|
|
|
|
vals = ("foo", "foo", "bar")
|
|
|
|
hdrs[ba.AUTH_HEADER] = [ba.unparse_auth_value(*vals)]
|
|
|
|
assert not ba.authenticate(hdrs)
|
|
|
|
|
|
|
|
ba = authentication.BasicProxyAuth(authentication.PasswordManager(), "test")
|
|
|
|
vals = ("basic", "foo", "bar")
|
|
|
|
hdrs[ba.AUTH_HEADER] = [ba.unparse_auth_value(*vals)]
|
|
|
|
assert not ba.authenticate(hdrs)
|
|
|
|
|