test_sha256.py 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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. 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_object_format("sha1"), SHA1)
  50. self.assertEqual(get_object_format("sha256"), SHA256)
  51. self.assertEqual(get_object_format(None), SHA1) # Default
  52. with self.assertRaises(ValueError):
  53. get_object_format("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. class RepositorySHA256Tests(unittest.TestCase):
  108. """Tests for SHA256 repository support."""
  109. def setUp(self):
  110. """Set up test repository directory."""
  111. self.test_dir = tempfile.mkdtemp()
  112. def tearDown(self):
  113. """Clean up test repository."""
  114. shutil.rmtree(self.test_dir)
  115. def test_init_sha256_repo(self):
  116. """Test initializing a SHA256 repository."""
  117. repo_path = os.path.join(self.test_dir, "sha256_repo")
  118. repo = Repo.init(repo_path, mkdir=True, object_format="sha256")
  119. # Check repository format version
  120. config = repo.get_config()
  121. self.assertEqual(config.get(("core",), "repositoryformatversion"), b"1")
  122. # Check object format extension
  123. self.assertEqual(config.get(("extensions",), "objectformat"), b"sha256")
  124. # Check hash algorithm detection
  125. self.assertEqual(repo.object_format, SHA256)
  126. repo.close()
  127. def test_init_sha1_repo(self):
  128. """Test initializing a SHA1 repository (default)."""
  129. repo_path = os.path.join(self.test_dir, "sha1_repo")
  130. repo = Repo.init(repo_path, mkdir=True)
  131. # Check repository format version
  132. config = repo.get_config()
  133. self.assertEqual(config.get(("core",), "repositoryformatversion"), b"0")
  134. # Object format extension should not exist
  135. with self.assertRaises(KeyError):
  136. config.get(("extensions",), "objectformat")
  137. # Check hash algorithm detection
  138. self.assertEqual(repo.object_format, SHA1)
  139. repo.close()
  140. def test_format_version_validation(self):
  141. """Test format version validation for SHA256."""
  142. repo_path = os.path.join(self.test_dir, "invalid_repo")
  143. # SHA256 with format version 0 should fail
  144. with self.assertRaises(ValueError) as cm:
  145. Repo.init(repo_path, mkdir=True, format=0, object_format="sha256")
  146. self.assertIn("SHA256", str(cm.exception))
  147. def test_memory_repo_sha256(self):
  148. """Test SHA256 support in memory repository."""
  149. repo = MemoryRepo.init_bare([], {}, object_format="sha256")
  150. # Check hash algorithm
  151. self.assertEqual(repo.object_format, SHA256)
  152. if __name__ == "__main__":
  153. unittest.main()