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 import hashlib |
| 24 import hmac |
| 25 |
| 26 from boto.utils import Password |
| 27 from boto.utils import pythonize_name |
| 28 |
| 29 |
| 30 class TestPassword(unittest.TestCase): |
| 31 """Test basic password functionality""" |
| 32 |
| 33 def clstest(self, cls): |
| 34 """Insure that password.__eq__ hashes test value before compare.""" |
| 35 password = cls('foo') |
| 36 self.assertNotEquals(password, 'foo') |
| 37 |
| 38 password.set('foo') |
| 39 hashed = str(password) |
| 40 self.assertEquals(password, 'foo') |
| 41 self.assertEquals(password.str, hashed) |
| 42 |
| 43 password = cls(hashed) |
| 44 self.assertNotEquals(password.str, 'foo') |
| 45 self.assertEquals(password, 'foo') |
| 46 self.assertEquals(password.str, hashed) |
| 47 |
| 48 def test_aaa_version_1_9_default_behavior(self): |
| 49 self.clstest(Password) |
| 50 |
| 51 def test_custom_hashclass(self): |
| 52 class SHA224Password(Password): |
| 53 hashfunc = hashlib.sha224 |
| 54 |
| 55 password = SHA224Password() |
| 56 password.set('foo') |
| 57 self.assertEquals(hashlib.sha224('foo').hexdigest(), str(password)) |
| 58 |
| 59 def test_hmac(self): |
| 60 def hmac_hashfunc(cls, msg): |
| 61 return hmac.new('mysecretkey', msg) |
| 62 |
| 63 class HMACPassword(Password): |
| 64 hashfunc = hmac_hashfunc |
| 65 |
| 66 self.clstest(HMACPassword) |
| 67 password = HMACPassword() |
| 68 password.set('foo') |
| 69 |
| 70 self.assertEquals(str(password), |
| 71 hmac.new('mysecretkey', 'foo').hexdigest()) |
| 72 |
| 73 def test_constructor(self): |
| 74 hmac_hashfunc = lambda msg: hmac.new('mysecretkey', msg) |
| 75 |
| 76 password = Password(hashfunc=hmac_hashfunc) |
| 77 password.set('foo') |
| 78 self.assertEquals(password.str, |
| 79 hmac.new('mysecretkey', 'foo').hexdigest()) |
| 80 |
| 81 |
| 82 class TestPythonizeName(unittest.TestCase): |
| 83 def test_empty_string(self): |
| 84 self.assertEqual(pythonize_name(''), '') |
| 85 |
| 86 def test_all_lower_case(self): |
| 87 self.assertEqual(pythonize_name('lowercase'), 'lowercase') |
| 88 |
| 89 def test_all_upper_case(self): |
| 90 self.assertEqual(pythonize_name('UPPERCASE'), 'uppercase') |
| 91 |
| 92 def test_camel_case(self): |
| 93 self.assertEqual(pythonize_name('OriginallyCamelCased'), |
| 94 'originally_camel_cased') |
| 95 |
| 96 def test_already_pythonized(self): |
| 97 self.assertEqual(pythonize_name('already_pythonized'), |
| 98 'already_pythonized') |
| 99 |
| 100 def test_multiple_upper_cased_letters(self): |
| 101 self.assertEqual(pythonize_name('HTTPRequest'), 'http_request') |
| 102 self.assertEqual(pythonize_name('RequestForHTTP'), 'request_for_http') |
| 103 |
| 104 def test_string_with_numbers(self): |
| 105 self.assertEqual(pythonize_name('HTTPStatus200Ok'), 'http_status_200_ok'
) |
| 106 |
| 107 |
| 108 if __name__ == '__main__': |
| 109 unittest.main() |
OLD | NEW |