1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
|
#!/usr/bin/env python
"""Unit tests for M2Crypto.AuthCookie.
Copyright (c) 1999-2002 Ng Pheng Siong. All rights reserved."""
import Cookie, binascii, time, unittest, sys
from M2Crypto.AuthCookie import AuthCookie, AuthCookieJar, mix, unmix, unmix3
from M2Crypto import Rand, EVP
class AuthCookieTestCase(unittest.TestCase):
_format = 'Set-Cookie: _M2AUTH_="exp=%s&data=%s&digest=%s"'
if sys.version_info < (2,5):
_format += ';'
_token = '_M2AUTH_'
def setUp(self):
self.data = 'cogitoergosum'
self.exp = time.time() + 3600
self.jar = AuthCookieJar()
def tearDown(self):
pass
def test_mix_unmix(self):
dough = mix(self.exp, self.data)
exp, data = unmix(dough)
self.failUnlessEqual(data, self.data)
self.failUnlessEqual(exp, self.exp)
def test_make_cookie(self):
c = self.jar.makeCookie(self.exp, self.data)
self.failUnless(isinstance(c, AuthCookie))
self.failUnlessEqual(c.expiry(), self.exp)
self.failUnlessEqual(c.data(), self.data)
# Peek inside the cookie jar...
key = self.jar._key
mac = binascii.b2a_base64(EVP.hmac(key, mix(self.exp, self.data), 'sha1'))[:-1]
self.failUnlessEqual(c.mac(), mac)
# Ok, stop peeking now.
cookie_str = self._format % (repr(self.exp), self.data, mac)
self.failUnlessEqual(c.output(), cookie_str)
def test_expired(self):
t = self.exp - 7200
c = self.jar.makeCookie(t, self.data)
self.failUnless(c.isExpired())
def test_not_expired(self):
c = self.jar.makeCookie(self.exp, self.data)
self.failIf(c.isExpired())
def test_is_valid(self):
c = self.jar.makeCookie(self.exp, self.data)
self.failUnless(self.jar.isGoodCookie(c))
def test_is_invalid_expired(self):
t = self.exp - 7200
c = self.jar.makeCookie(t, self.data)
self.failIf(self.jar.isGoodCookie(c))
def test_is_invalid_changed_exp(self):
c = self.jar.makeCookie(self.exp, self.data)
c._expiry = 'this is bad'
self.failIf(self.jar.isGoodCookie(c))
def test_is_invalid_changed_data(self):
c = self.jar.makeCookie(self.exp, self.data)
c._data = 'this is bad'
self.failIf(self.jar.isGoodCookie(c))
def test_is_invalid_changed_mac(self):
c = self.jar.makeCookie(self.exp, self.data)
c._mac = 'this is bad'
self.failIf(self.jar.isGoodCookie(c))
def test_mix_unmix3(self):
c = self.jar.makeCookie(self.exp, self.data)
s = Cookie.SmartCookie()
s.load(c.output())
exp, data, digest = unmix3(s[self._token].value)
self.failUnlessEqual(data, self.data)
self.failUnlessEqual(float(exp), self.exp)
key = self.jar._key # Peeking...
mac = binascii.b2a_base64(EVP.hmac(key, mix(self.exp, self.data), 'sha1'))[:-1]
self.failUnlessEqual(digest, mac)
def test_cookie_str(self):
c = self.jar.makeCookie(self.exp, self.data)
self.failUnless(self.jar.isGoodCookieString(c.output()))
def test_cookie_str2(self):
c = self.jar.makeCookie(self.exp, self.data)
s = Cookie.SmartCookie()
s.load(c.output())
self.failUnless(self.jar.isGoodCookieString(s.output()))
def test_cookie_str_expired(self):
t = self.exp - 7200
c = self.jar.makeCookie(t, self.data)
s = Cookie.SmartCookie()
s.load(c.output())
self.failIf(self.jar.isGoodCookieString(s.output()))
def test_cookie_str_arbitrary_change(self):
c = self.jar.makeCookie(self.exp, self.data)
cout = c.output()
str = cout[:32] + 'this is bad' + cout[32:]
s = Cookie.SmartCookie()
s.load(str)
self.failIf(self.jar.isGoodCookieString(s.output()))
def test_cookie_str_changed_exp(self):
c = self.jar.makeCookie(self.exp, self.data)
cout = c.output()
str = cout[:26] + '2' + cout[27:]
s = Cookie.SmartCookie()
s.load(str)
self.failIf(self.jar.isGoodCookieString(s.output()))
def test_cookie_str_changed_data(self):
c = self.jar.makeCookie(self.exp, self.data)
cout = c.output()
str = cout[:36] + 'X' + cout[37:]
s = Cookie.SmartCookie()
s.load(str)
self.failIf(self.jar.isGoodCookieString(s.output()))
def test_cookie_str_changed_mac(self):
c = self.jar.makeCookie(self.exp, self.data)
cout = c.output()
str = cout[:76] + 'X' + cout[77:]
s = Cookie.SmartCookie()
s.load(str)
self.failIf(self.jar.isGoodCookieString(s.output()))
def suite():
return unittest.makeSuite(AuthCookieTestCase)
if __name__ == '__main__':
Rand.load_file('randpool.dat', -1)
unittest.TextTestRunner().run(suite())
Rand.save_file('randpool.dat')
|