OLD | NEW |
(Empty) | |
| 1 # Copyright (c) 2010 Robert Mela |
| 2 # |
| 3 # Permission is hereby granted, free of charge, to any person obtaining a |
| 4 # copy of this software and associated documentation files (the |
| 5 # "Software"), to deal in the Software without restriction, including |
| 6 # without limitation the rights to use, copy, modify, merge, publish, dis- |
| 7 # tribute, sublicense, and/or sell copies of the Software, and to permit |
| 8 # persons to whom the Software is furnished to do so, subject to the fol- |
| 9 # lowing conditions: |
| 10 # |
| 11 # The above copyright notice and this permission notice shall be included |
| 12 # in all copies or substantial portions of the Software. |
| 13 # |
| 14 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
| 15 # OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABIL- |
| 16 # ITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT |
| 17 # SHALL THE AUTHOR BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, |
| 18 # WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 19 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS |
| 20 # IN THE SOFTWARE. |
| 21 # |
| 22 import unittest |
| 23 |
| 24 |
| 25 import logging |
| 26 log = logging.getLogger(__file__) |
| 27 |
| 28 class TestPassword(unittest.TestCase): |
| 29 """Test basic password functionality""" |
| 30 |
| 31 def clstest(self,cls): |
| 32 |
| 33 """Insure that password.__eq__ hashes test value before compare""" |
| 34 |
| 35 password=cls('foo') |
| 36 log.debug( "Password %s" % password ) |
| 37 self.assertNotEquals(password , 'foo') |
| 38 |
| 39 password.set('foo') |
| 40 hashed = str(password) |
| 41 self.assertEquals(password , 'foo') |
| 42 self.assertEquals(password.str, hashed) |
| 43 |
| 44 password = cls(hashed) |
| 45 self.assertNotEquals(password.str , 'foo') |
| 46 self.assertEquals(password , 'foo') |
| 47 self.assertEquals(password.str , hashed) |
| 48 |
| 49 |
| 50 def test_aaa_version_1_9_default_behavior(self): |
| 51 from boto.utils import Password |
| 52 self.clstest(Password) |
| 53 |
| 54 def test_custom_hashclass(self): |
| 55 |
| 56 from boto.utils import Password |
| 57 import hashlib |
| 58 |
| 59 class SHA224Password(Password): |
| 60 hashfunc=hashlib.sha224 |
| 61 |
| 62 password=SHA224Password() |
| 63 password.set('foo') |
| 64 self.assertEquals( hashlib.sha224('foo').hexdigest(), str(password)) |
| 65 |
| 66 def test_hmac(self): |
| 67 from boto.utils import Password |
| 68 import hmac |
| 69 |
| 70 def hmac_hashfunc(cls,msg): |
| 71 log.debug("\n%s %s" % (cls.__class__, cls) ) |
| 72 return hmac.new('mysecretkey', msg) |
| 73 |
| 74 class HMACPassword(Password): |
| 75 hashfunc=hmac_hashfunc |
| 76 |
| 77 self.clstest(HMACPassword) |
| 78 password=HMACPassword() |
| 79 password.set('foo') |
| 80 |
| 81 self.assertEquals(str(password), hmac.new('mysecretkey','foo').hexdigest
()) |
| 82 |
| 83 def test_constructor(self): |
| 84 from boto.utils import Password |
| 85 import hmac |
| 86 |
| 87 hmac_hashfunc = lambda msg: hmac.new('mysecretkey', msg ) |
| 88 |
| 89 password = Password(hashfunc=hmac_hashfunc) |
| 90 password.set('foo') |
| 91 self.assertEquals(password.str, hmac.new('mysecretkey','foo').hexdigest(
)) |
| 92 |
| 93 |
| 94 |
| 95 if __name__ == '__main__': |
| 96 import sys |
| 97 sys.path = [ '../../' ] + sys.path |
| 98 #logging.basicConfig() |
| 99 #log.setLevel(logging.DEBUG) |
| 100 suite = unittest.TestLoader().loadTestsFromTestCase(TestPassword) |
| 101 unittest.TextTestRunner(verbosity=2).run(suite) |
OLD | NEW |