test_hashutils.py 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. # 16 bytes of random data
  20. bytes = b'b\x19\xf6\x86\x0e\x1a\x1cs\x0c\xda&zv\xfc\xa2\xdd'
  21. bytes_b64encode_result = 'Yhn2hg4aHHMM2iZ6dvyi3Q=='
  22. bytes_encodestring_result = 'Yhn2hg4aHHMM2iZ6dvyi3Q==\n'
  23. bytes_md5 = '4d064241724791641dc15930c65f75c8'
  24. bytes_sha256 = '25711a31c2673a48f3d1f29b25add574697872968e546d266f441de63b17954a'
  25. bytes_sha512 = '69f1524e602c1599fc374e1e3e2941e6f6949f4f7fe7321304e4e67bb850f3204dd5cbf9c13e231814540c2f5cd370c24ea257771d9fbf311d8f6085bad12b24'
  26. bytes_hmac_challenge = b'lQibiD9r1Hpo+5JYknaudIKfTx1L5J3U58M9yQOd04c='
  27. def test_base64_b64encode(self):
  28. '''
  29. Ensure that this function converts the value passed to bytes before
  30. attempting to encode, avoiding a UnicodeEncodeError on Python 2 and a
  31. TypeError on Python 3.
  32. '''
  33. self.assertEqual(
  34. salt.utils.hashutils.base64_b64encode(self.str),
  35. self.str_b64encode_result
  36. )
  37. self.assertEqual(
  38. salt.utils.hashutils.base64_b64encode(self.bytes),
  39. self.bytes_b64encode_result
  40. )
  41. def test_base64_b64decode(self):
  42. '''
  43. Ensure that this function converts the value passed to a unicode type
  44. (if possible) on Python 2, and a str type (if possible) on Python 3.
  45. '''
  46. self.assertEqual(
  47. salt.utils.hashutils.base64_b64decode(self.str_b64encode_result),
  48. self.str
  49. )
  50. self.assertEqual(
  51. salt.utils.hashutils.base64_b64decode(self.bytes_b64encode_result),
  52. self.bytes
  53. )
  54. def test_base64_encodestring(self):
  55. '''
  56. Ensure that this function converts the value passed to bytes before
  57. attempting to encode, avoiding a UnicodeEncodeError on Python 2 and a
  58. TypeError on Python 3.
  59. '''
  60. self.assertEqual(
  61. salt.utils.hashutils.base64_encodestring(self.str),
  62. self.str_encodestring_result
  63. )
  64. self.assertEqual(
  65. salt.utils.hashutils.base64_encodestring(self.bytes),
  66. self.bytes_encodestring_result
  67. )
  68. def test_base64_decodestring(self):
  69. '''
  70. Ensure that this function converts the value passed to a unicode type
  71. (if possible) on Python 2, and a str type (if possible) on Python 3.
  72. '''
  73. self.assertEqual(
  74. salt.utils.hashutils.base64_decodestring(self.str_encodestring_result),
  75. self.str
  76. )
  77. self.assertEqual(
  78. salt.utils.hashutils.base64_decodestring(self.bytes_encodestring_result),
  79. self.bytes
  80. )
  81. def test_md5_digest(self):
  82. '''
  83. Ensure that this function converts the value passed to bytes before
  84. attempting to encode, avoiding a UnicodeEncodeError on Python 2 and a
  85. TypeError on Python 3.
  86. '''
  87. self.assertEqual(
  88. salt.utils.hashutils.md5_digest(self.str),
  89. self.str_md5
  90. )
  91. self.assertEqual(
  92. salt.utils.hashutils.md5_digest(self.bytes),
  93. self.bytes_md5
  94. )
  95. def test_sha256_digest(self):
  96. '''
  97. Ensure that this function converts the value passed to bytes before
  98. attempting to encode, avoiding a UnicodeEncodeError on Python 2 and a
  99. TypeError on Python 3.
  100. '''
  101. self.assertEqual(
  102. salt.utils.hashutils.sha256_digest(self.str),
  103. self.str_sha256
  104. )
  105. self.assertEqual(
  106. salt.utils.hashutils.sha256_digest(self.bytes),
  107. self.bytes_sha256
  108. )
  109. def test_sha512_digest(self):
  110. '''
  111. Ensure that this function converts the value passed to bytes before
  112. attempting to encode, avoiding a UnicodeEncodeError on Python 2 and a
  113. TypeError on Python 3.
  114. '''
  115. self.assertEqual(
  116. salt.utils.hashutils.sha512_digest(self.str),
  117. self.str_sha512
  118. )
  119. self.assertEqual(
  120. salt.utils.hashutils.sha512_digest(self.bytes),
  121. self.bytes_sha512
  122. )
  123. def test_hmac_signature(self):
  124. '''
  125. Ensure that this function converts the value passed to bytes before
  126. attempting to validate the hmac challenge, avoiding a
  127. UnicodeEncodeError on Python 2 and a TypeError on Python 3.
  128. '''
  129. self.assertTrue(
  130. salt.utils.hashutils.hmac_signature(
  131. self.str,
  132. self.hmac_secret,
  133. self.str_hmac_challenge
  134. )
  135. )
  136. self.assertTrue(
  137. salt.utils.hashutils.hmac_signature(
  138. self.bytes,
  139. self.hmac_secret,
  140. self.bytes_hmac_challenge
  141. )
  142. )
  143. def test_get_hash_exception(self):
  144. self.assertRaises(
  145. ValueError,
  146. salt.utils.hashutils.get_hash,
  147. '/tmp/foo/',
  148. form='INVALID')