1
0

test_hashutils.py 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. # -*- coding: utf-8 -*-
  2. # Import python libs
  3. from __future__ import absolute_import, unicode_literals, print_function
  4. # Import Salt Testing libs
  5. from tests.support.unit import TestCase
  6. # Import Salt libs
  7. import salt.utils.hashutils
  8. class HashutilsTestCase(TestCase):
  9. hmac_secret = 's00p3r s3kr1t'
  10. # Use a non-ascii unicode type to confirm that no UnicodeEncodeError is
  11. # raised on Python 2.
  12. str = 'спам'
  13. str_b64encode_result = '0YHQv9Cw0Lw='
  14. str_encodestring_result = '0YHQv9Cw0Lw=\n'
  15. str_md5 = 'a035ac08ab2f03556f9b3ee640052e5c'
  16. str_sha256 = '095291ffa3d361436d4617879e22c1da06c6ab61a3fb081321ec854a27a091ac'
  17. str_sha512 = '12efd90e507289f1f21e5dcfe2e92cf0bb4904abccb55c3ce9177670c711981501054b32b807c37058675590d1c484bd2b72a4215a2fa397aa4f2b12f298b1f0'
  18. str_hmac_challenge = b'qz2k0t1aevKEme3JGsNQJX/xpmf+/w3q6qmWDk1ZqbY='
  19. str_hmac_compute = 'ab3da4d2dd5a7af28499edc91ac350257ff1a667feff0deaeaa9960e4d59a9b6'
  20. # 16 bytes of random data
  21. bytes = b'b\x19\xf6\x86\x0e\x1a\x1cs\x0c\xda&zv\xfc\xa2\xdd'
  22. bytes_b64encode_result = 'Yhn2hg4aHHMM2iZ6dvyi3Q=='
  23. bytes_encodestring_result = 'Yhn2hg4aHHMM2iZ6dvyi3Q==\n'
  24. bytes_md5 = '4d064241724791641dc15930c65f75c8'
  25. bytes_sha256 = '25711a31c2673a48f3d1f29b25add574697872968e546d266f441de63b17954a'
  26. bytes_sha512 = '69f1524e602c1599fc374e1e3e2941e6f6949f4f7fe7321304e4e67bb850f3204dd5cbf9c13e231814540c2f5cd370c24ea257771d9fbf311d8f6085bad12b24'
  27. bytes_hmac_challenge = b'lQibiD9r1Hpo+5JYknaudIKfTx1L5J3U58M9yQOd04c='
  28. bytes_hmac_compute = '95089b883f6bd47a68fb92589276ae74829f4f1d4be49dd4e7c33dc9039dd387'
  29. def test_base64_b64encode(self):
  30. '''
  31. Ensure that this function converts the value passed to bytes before
  32. attempting to encode, avoiding a UnicodeEncodeError on Python 2 and a
  33. TypeError on Python 3.
  34. '''
  35. self.assertEqual(
  36. salt.utils.hashutils.base64_b64encode(self.str),
  37. self.str_b64encode_result
  38. )
  39. self.assertEqual(
  40. salt.utils.hashutils.base64_b64encode(self.bytes),
  41. self.bytes_b64encode_result
  42. )
  43. def test_base64_b64decode(self):
  44. '''
  45. Ensure that this function converts the value passed to a unicode type
  46. (if possible) on Python 2, and a str type (if possible) on Python 3.
  47. '''
  48. self.assertEqual(
  49. salt.utils.hashutils.base64_b64decode(self.str_b64encode_result),
  50. self.str
  51. )
  52. self.assertEqual(
  53. salt.utils.hashutils.base64_b64decode(self.bytes_b64encode_result),
  54. self.bytes
  55. )
  56. def test_base64_encodestring(self):
  57. '''
  58. Ensure that this function converts the value passed to bytes before
  59. attempting to encode, avoiding a UnicodeEncodeError on Python 2 and a
  60. TypeError on Python 3.
  61. '''
  62. self.assertEqual(
  63. salt.utils.hashutils.base64_encodestring(self.str),
  64. self.str_encodestring_result
  65. )
  66. self.assertEqual(
  67. salt.utils.hashutils.base64_encodestring(self.bytes),
  68. self.bytes_encodestring_result
  69. )
  70. def test_base64_decodestring(self):
  71. '''
  72. Ensure that this function converts the value passed to a unicode type
  73. (if possible) on Python 2, and a str type (if possible) on Python 3.
  74. '''
  75. self.assertEqual(
  76. salt.utils.hashutils.base64_decodestring(self.str_encodestring_result),
  77. self.str
  78. )
  79. self.assertEqual(
  80. salt.utils.hashutils.base64_decodestring(self.bytes_encodestring_result),
  81. self.bytes
  82. )
  83. def test_md5_digest(self):
  84. '''
  85. Ensure that this function converts the value passed to bytes before
  86. attempting to encode, avoiding a UnicodeEncodeError on Python 2 and a
  87. TypeError on Python 3.
  88. '''
  89. self.assertEqual(
  90. salt.utils.hashutils.md5_digest(self.str),
  91. self.str_md5
  92. )
  93. self.assertEqual(
  94. salt.utils.hashutils.md5_digest(self.bytes),
  95. self.bytes_md5
  96. )
  97. def test_sha256_digest(self):
  98. '''
  99. Ensure that this function converts the value passed to bytes before
  100. attempting to encode, avoiding a UnicodeEncodeError on Python 2 and a
  101. TypeError on Python 3.
  102. '''
  103. self.assertEqual(
  104. salt.utils.hashutils.sha256_digest(self.str),
  105. self.str_sha256
  106. )
  107. self.assertEqual(
  108. salt.utils.hashutils.sha256_digest(self.bytes),
  109. self.bytes_sha256
  110. )
  111. def test_sha512_digest(self):
  112. '''
  113. Ensure that this function converts the value passed to bytes before
  114. attempting to encode, avoiding a UnicodeEncodeError on Python 2 and a
  115. TypeError on Python 3.
  116. '''
  117. self.assertEqual(
  118. salt.utils.hashutils.sha512_digest(self.str),
  119. self.str_sha512
  120. )
  121. self.assertEqual(
  122. salt.utils.hashutils.sha512_digest(self.bytes),
  123. self.bytes_sha512
  124. )
  125. def test_hmac_signature(self):
  126. '''
  127. Ensure that this function converts the value passed to bytes before
  128. attempting to validate the hmac challenge, avoiding a
  129. UnicodeEncodeError on Python 2 and a TypeError on Python 3.
  130. '''
  131. self.assertTrue(
  132. salt.utils.hashutils.hmac_signature(
  133. self.str,
  134. self.hmac_secret,
  135. self.str_hmac_challenge
  136. )
  137. )
  138. self.assertTrue(
  139. salt.utils.hashutils.hmac_signature(
  140. self.bytes,
  141. self.hmac_secret,
  142. self.bytes_hmac_challenge
  143. )
  144. )
  145. def test_hmac_compute(self):
  146. '''
  147. Ensure that this function converts the value passed to bytes before
  148. attempting to encode, avoiding a UnicodeEncodeError on Python 2 and a
  149. TypeError on Python 3.
  150. '''
  151. self.assertEqual(
  152. salt.utils.hashutils.hmac_compute(self.str, self.hmac_secret),
  153. self.str_hmac_compute
  154. )
  155. self.assertEqual(
  156. salt.utils.hashutils.hmac_compute(self.bytes, self.hmac_secret),
  157. self.bytes_hmac_compute
  158. )
  159. def test_get_hash_exception(self):
  160. self.assertRaises(
  161. ValueError,
  162. salt.utils.hashutils.get_hash,
  163. '/tmp/foo/',
  164. form='INVALID')