test_sha256.py 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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.object_format import SHA1, SHA256, get_object_format
  27. from dulwich.objects import Blob, Tree, valid_hexsha
  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. def test_sha256_properties(self):
  38. """Test SHA256 algorithm properties."""
  39. alg = SHA256
  40. self.assertEqual(alg.name, "sha256")
  41. self.assertEqual(alg.oid_length, 32)
  42. self.assertEqual(alg.hex_length, 64)
  43. def test_get_hash_algorithm(self):
  44. """Test getting hash algorithms by name."""
  45. self.assertEqual(get_object_format("sha1"), SHA1)
  46. self.assertEqual(get_object_format("sha256"), SHA256)
  47. self.assertEqual(get_object_format(None), SHA1) # Default
  48. with self.assertRaises(ValueError):
  49. get_object_format("invalid")
  50. class ObjectHashingTests(unittest.TestCase):
  51. """Tests for object hashing with different algorithms."""
  52. def test_blob_sha1(self):
  53. """Test blob hashing with SHA1."""
  54. blob = Blob()
  55. blob.data = b"Hello, World!"
  56. # Default should be SHA1
  57. sha1_id = blob.id
  58. self.assertEqual(len(sha1_id), 40)
  59. self.assertTrue(valid_hexsha(sha1_id))
  60. def test_blob_sha256(self):
  61. """Test blob hashing with SHA256."""
  62. blob = Blob()
  63. blob.data = b"Hello, World!"
  64. # Get SHA256 hash
  65. sha256_id = blob.get_id(SHA256)
  66. self.assertEqual(len(sha256_id), 64)
  67. self.assertTrue(valid_hexsha(sha256_id))
  68. # SHA256 ID should be different from SHA1
  69. sha1_id = blob.id
  70. self.assertNotEqual(sha1_id, sha256_id)
  71. # Verify .id property returns SHA1 for backward compatibility
  72. self.assertEqual(blob.id, sha1_id)
  73. self.assertEqual(blob.get_id(), sha1_id) # Default should be SHA1
  74. def test_tree_sha256(self):
  75. """Test tree hashing with SHA256."""
  76. tree = Tree()
  77. tree.add(b"file.txt", 0o100644, b"a" * 40) # SHA1 hex
  78. # Get SHA1 (default)
  79. sha1_id = tree.id
  80. self.assertEqual(len(sha1_id), 40)
  81. # Get SHA256
  82. sha256_id = tree.get_id(SHA256)
  83. self.assertEqual(len(sha256_id), 64)
  84. # Verify they're different
  85. self.assertNotEqual(sha1_id, sha256_id)
  86. def test_valid_hexsha(self):
  87. """Test hex SHA validation for both algorithms."""
  88. # Valid SHA1
  89. self.assertTrue(valid_hexsha(b"1234567890abcdef1234567890abcdef12345678"))
  90. # Valid SHA256
  91. self.assertTrue(
  92. valid_hexsha(
  93. b"1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef"
  94. )
  95. )
  96. # Invalid lengths
  97. self.assertFalse(valid_hexsha(b"1234"))
  98. self.assertFalse(
  99. valid_hexsha(b"1234567890abcdef1234567890abcdef123456")
  100. ) # 38 chars
  101. # Invalid characters
  102. self.assertFalse(valid_hexsha(b"123456789gabcdef1234567890abcdef12345678"))
  103. class RepositorySHA256Tests(unittest.TestCase):
  104. """Tests for SHA256 repository support."""
  105. def setUp(self):
  106. """Set up test repository directory."""
  107. self.test_dir = tempfile.mkdtemp()
  108. def tearDown(self):
  109. """Clean up test repository."""
  110. shutil.rmtree(self.test_dir)
  111. def test_init_sha256_repo(self):
  112. """Test initializing a SHA256 repository."""
  113. repo_path = os.path.join(self.test_dir, "sha256_repo")
  114. repo = Repo.init(repo_path, mkdir=True, object_format="sha256")
  115. # Check repository format version
  116. config = repo.get_config()
  117. self.assertEqual(config.get(("core",), "repositoryformatversion"), b"1")
  118. # Check object format extension
  119. self.assertEqual(config.get(("extensions",), "objectformat"), b"sha256")
  120. # Check hash algorithm detection
  121. self.assertEqual(repo.object_format, SHA256)
  122. repo.close()
  123. def test_init_sha1_repo(self):
  124. """Test initializing a SHA1 repository (default)."""
  125. repo_path = os.path.join(self.test_dir, "sha1_repo")
  126. repo = Repo.init(repo_path, mkdir=True)
  127. # Check repository format version
  128. config = repo.get_config()
  129. self.assertEqual(config.get(("core",), "repositoryformatversion"), b"0")
  130. # Object format extension should not exist
  131. with self.assertRaises(KeyError):
  132. config.get(("extensions",), "objectformat")
  133. # Check hash algorithm detection
  134. self.assertEqual(repo.object_format, SHA1)
  135. repo.close()
  136. def test_format_version_validation(self):
  137. """Test format version validation for SHA256."""
  138. repo_path = os.path.join(self.test_dir, "invalid_repo")
  139. # SHA256 with format version 0 should fail
  140. with self.assertRaises(ValueError) as cm:
  141. Repo.init(repo_path, mkdir=True, format=0, object_format="sha256")
  142. self.assertIn("SHA256", str(cm.exception))
  143. def test_memory_repo_sha256(self):
  144. """Test SHA256 support in memory repository."""
  145. repo = MemoryRepo.init_bare([], {}, object_format="sha256")
  146. # Check hash algorithm
  147. self.assertEqual(repo.object_format, SHA256)
  148. if __name__ == "__main__":
  149. unittest.main()