2
0

test_sha256.py 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. # test_sha256.py -- Tests for SHA256 support
  2. # Copyright (C) 2024 The Dulwich contributors
  3. #
  4. # SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
  5. # Dulwich is dual-licensed under the Apache License, Version 2.0 and the GNU
  6. # General Public License as public by the Free Software Foundation; version 2.0
  7. # or (at your option) any later version. You can redistribute it and/or
  8. # modify it under the terms of either of these two licenses.
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. #
  16. # You should have received a copy of the licenses; if not, see
  17. # <http://www.gnu.org/licenses/> for a copy of the GNU General Public License
  18. # and <http://www.apache.org/licenses/LICENSE-2.0> for a copy of the Apache
  19. # License, Version 2.0.
  20. #
  21. """Tests for SHA256 support in Dulwich."""
  22. import os
  23. import shutil
  24. import tempfile
  25. import unittest
  26. from dulwich.hash import SHA1, SHA256, get_hash_algorithm
  27. from dulwich.objects import Blob, Tree, valid_hexsha, zero_sha_for
  28. from dulwich.repo import MemoryRepo, Repo
  29. class HashAlgorithmTests(unittest.TestCase):
  30. """Tests for the hash algorithm abstraction."""
  31. def test_sha1_properties(self):
  32. """Test SHA1 algorithm properties."""
  33. alg = SHA1
  34. self.assertEqual(alg.name, "sha1")
  35. self.assertEqual(alg.oid_length, 20)
  36. self.assertEqual(alg.hex_length, 40)
  37. self.assertEqual(len(alg.zero_oid), 40)
  38. self.assertEqual(len(alg.zero_oid_bin), 20)
  39. def test_sha256_properties(self):
  40. """Test SHA256 algorithm properties."""
  41. alg = SHA256
  42. self.assertEqual(alg.name, "sha256")
  43. self.assertEqual(alg.oid_length, 32)
  44. self.assertEqual(alg.hex_length, 64)
  45. self.assertEqual(len(alg.zero_oid), 64)
  46. self.assertEqual(len(alg.zero_oid_bin), 32)
  47. def test_get_hash_algorithm(self):
  48. """Test getting hash algorithms by name."""
  49. self.assertEqual(get_hash_algorithm("sha1"), SHA1)
  50. self.assertEqual(get_hash_algorithm("sha256"), SHA256)
  51. self.assertEqual(get_hash_algorithm(None), SHA1) # Default
  52. with self.assertRaises(ValueError):
  53. get_hash_algorithm("invalid")
  54. class ObjectHashingTests(unittest.TestCase):
  55. """Tests for object hashing with different algorithms."""
  56. def test_blob_sha1(self):
  57. """Test blob hashing with SHA1."""
  58. blob = Blob()
  59. blob.data = b"Hello, World!"
  60. # Default should be SHA1
  61. sha1_id = blob.id
  62. self.assertEqual(len(sha1_id), 40)
  63. self.assertTrue(valid_hexsha(sha1_id))
  64. def test_blob_sha256(self):
  65. """Test blob hashing with SHA256."""
  66. blob = Blob()
  67. blob.data = b"Hello, World!"
  68. # Get SHA256 hash
  69. sha256_id = blob.get_id(SHA256)
  70. self.assertEqual(len(sha256_id), 64)
  71. self.assertTrue(valid_hexsha(sha256_id))
  72. # SHA256 ID should be different from SHA1
  73. sha1_id = blob.id
  74. self.assertNotEqual(sha1_id, sha256_id)
  75. # Verify .id property returns SHA1 for backward compatibility
  76. self.assertEqual(blob.id, sha1_id)
  77. self.assertEqual(blob.get_id(), sha1_id) # Default should be SHA1
  78. def test_tree_sha256(self):
  79. """Test tree hashing with SHA256."""
  80. tree = Tree()
  81. tree.add(b"file.txt", 0o100644, b"a" * 40) # SHA1 hex
  82. # Get SHA1 (default)
  83. sha1_id = tree.id
  84. self.assertEqual(len(sha1_id), 40)
  85. # Get SHA256
  86. sha256_id = tree.get_id(SHA256)
  87. self.assertEqual(len(sha256_id), 64)
  88. # Verify they're different
  89. self.assertNotEqual(sha1_id, sha256_id)
  90. def test_valid_hexsha(self):
  91. """Test hex SHA validation for both algorithms."""
  92. # Valid SHA1
  93. self.assertTrue(valid_hexsha(b"1234567890abcdef1234567890abcdef12345678"))
  94. # Valid SHA256
  95. self.assertTrue(
  96. valid_hexsha(
  97. b"1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef"
  98. )
  99. )
  100. # Invalid lengths
  101. self.assertFalse(valid_hexsha(b"1234"))
  102. self.assertFalse(
  103. valid_hexsha(b"1234567890abcdef1234567890abcdef123456")
  104. ) # 38 chars
  105. # Invalid characters
  106. self.assertFalse(valid_hexsha(b"123456789gabcdef1234567890abcdef12345678"))
  107. def test_zero_sha_for(self):
  108. """Test getting zero SHA for different algorithms."""
  109. # Default (SHA1)
  110. self.assertEqual(zero_sha_for(), b"0" * 40)
  111. self.assertEqual(zero_sha_for(None), b"0" * 40)
  112. # SHA1 explicit
  113. self.assertEqual(zero_sha_for(SHA1), b"0" * 40)
  114. # SHA256
  115. self.assertEqual(zero_sha_for(SHA256), b"0" * 64)
  116. class RepositorySHA256Tests(unittest.TestCase):
  117. """Tests for SHA256 repository support."""
  118. def setUp(self):
  119. """Set up test repository directory."""
  120. self.test_dir = tempfile.mkdtemp()
  121. def tearDown(self):
  122. """Clean up test repository."""
  123. shutil.rmtree(self.test_dir)
  124. def test_init_sha256_repo(self):
  125. """Test initializing a SHA256 repository."""
  126. repo_path = os.path.join(self.test_dir, "sha256_repo")
  127. repo = Repo.init(repo_path, mkdir=True, object_format="sha256")
  128. # Check repository format version
  129. config = repo.get_config()
  130. self.assertEqual(config.get(("core",), "repositoryformatversion"), b"1")
  131. # Check object format extension
  132. self.assertEqual(config.get(("extensions",), "objectformat"), b"sha256")
  133. # Check hash algorithm detection
  134. hash_alg = repo.get_hash_algorithm()
  135. self.assertEqual(hash_alg, SHA256)
  136. repo.close()
  137. def test_init_sha1_repo(self):
  138. """Test initializing a SHA1 repository (default)."""
  139. repo_path = os.path.join(self.test_dir, "sha1_repo")
  140. repo = Repo.init(repo_path, mkdir=True)
  141. # Check repository format version
  142. config = repo.get_config()
  143. self.assertEqual(config.get(("core",), "repositoryformatversion"), b"0")
  144. # Object format extension should not exist
  145. with self.assertRaises(KeyError):
  146. config.get(("extensions",), "objectformat")
  147. # Check hash algorithm detection
  148. hash_alg = repo.get_hash_algorithm()
  149. self.assertEqual(hash_alg, SHA1)
  150. repo.close()
  151. def test_format_version_validation(self):
  152. """Test format version validation for SHA256."""
  153. repo_path = os.path.join(self.test_dir, "invalid_repo")
  154. # SHA256 with format version 0 should fail
  155. with self.assertRaises(ValueError) as cm:
  156. Repo.init(repo_path, mkdir=True, format=0, object_format="sha256")
  157. self.assertIn("SHA256", str(cm.exception))
  158. def test_memory_repo_sha256(self):
  159. """Test SHA256 support in memory repository."""
  160. repo = MemoryRepo.init_bare([], {}, object_format="sha256")
  161. # Check hash algorithm
  162. hash_alg = repo.get_hash_algorithm()
  163. self.assertEqual(hash_alg, SHA256)
  164. if __name__ == "__main__":
  165. unittest.main()