From 3e325ca36152de16e37d0f450cf1c6c39e176929 Mon Sep 17 00:00:00 2001
From: "Antoine R. Dumont (@ardumont)" <antoine.romain.dumont@gmail.com>
Date: Wed, 15 Mar 2017 15:31:54 +0100
Subject: [PATCH] Migrate from swh.core.hashutil to swh.model.hashutil

Related T700
---
 swh/model/hashutil.py               |  12 ++++
 swh/model/tests/test_hashutil.py    | 100 +++++++++++++++++++++++++++-
 swh/model/tests/test_identifiers.py |   2 +-
 3 files changed, 112 insertions(+), 2 deletions(-)

diff --git a/swh/model/hashutil.py b/swh/model/hashutil.py
index b2558a3b..ea28414f 100644
--- a/swh/model/hashutil.py
+++ b/swh/model/hashutil.py
@@ -181,9 +181,21 @@ def hash_to_hex(hash):
     return binascii.hexlify(hash).decode('ascii')
 
 
+@functools.lru_cache()
+def hash_to_bytehex(hash):
+    """Converts a hash to its hexadecimal bytes representation"""
+    return binascii.hexlify(hash)
+
+
 @functools.lru_cache()
 def hash_to_bytes(hash):
     """Converts a hash (in hex or bytes form) to its raw bytes form"""
     if isinstance(hash, bytes):
         return hash
     return bytes.fromhex(hash)
+
+
+@functools.lru_cache()
+def bytehex_to_hash(hex):
+    """Converts a hexadecimal bytes representation of a hash to that hash"""
+    return hash_to_bytes(hex.decode())
diff --git a/swh/model/tests/test_hashutil.py b/swh/model/tests/test_hashutil.py
index f795e87e..614e7ee2 100644
--- a/swh/model/tests/test_hashutil.py
+++ b/swh/model/tests/test_hashutil.py
@@ -1,4 +1,4 @@
-# Copyright (C) 2015  The Software Heritage developers
+# Copyright (C) 2015-2017  The Software Heritage developers
 # See the AUTHORS file at the top-level directory of this distribution
 # License: GNU General Public License version 3, or any later version
 # See top-level LICENSE file for more information
@@ -110,3 +110,101 @@ class Hashutil(unittest.TestCase):
             hash = self.checksums[type]
             self.assertEquals(hashutil.hash_to_bytes(hex), hash)
             self.assertEquals(hashutil.hash_to_bytes(hash), hash)
+
+    @istest
+    def hash_to_bytehex(self):
+        for algo in self.checksums:
+            self.assertEqual(self.hex_checksums[algo].encode('ascii'),
+                             hashutil.hash_to_bytehex(self.checksums[algo]))
+
+    @istest
+    def bytehex_to_hash(self):
+        for algo in self.checksums:
+            self.assertEqual(self.checksums[algo],
+                             hashutil.bytehex_to_hash(
+                                 self.hex_checksums[algo].encode()))
+
+
+class HashlibGit(unittest.TestCase):
+
+    def setUp(self):
+        self.blob_data = b'42\n'
+
+        self.tree_data = b''.join([b'40000 barfoo\0',
+                                   bytes.fromhex('c3020f6bf135a38c6df'
+                                                 '3afeb5fb38232c5e07087'),
+                                   b'100644 blah\0',
+                                   bytes.fromhex('63756ef0df5e4f10b6efa'
+                                                 '33cfe5c758749615f20'),
+                                   b'100644 hello\0',
+                                   bytes.fromhex('907b308167f0880fb2a'
+                                                 '5c0e1614bb0c7620f9dc3')])
+
+        self.commit_data = """tree 1c61f7259dcb770f46b194d941df4f08ff0a3970
+author Antoine R. Dumont (@ardumont) <antoine.romain.dumont@gmail.com> 1444054085 +0200
+committer Antoine R. Dumont (@ardumont) <antoine.romain.dumont@gmail.com> 1444054085 +0200
+
+initial
+""".encode('utf-8')  # NOQA
+        self.tag_data = """object 24d012aaec0bc5a4d2f62c56399053d6cc72a241
+type commit
+tag 0.0.1
+tagger Antoine R. Dumont (@ardumont) <antoine.romain.dumont@gmail.com> 1444225145 +0200
+
+blah
+""".encode('utf-8')  # NOQA
+
+        self.checksums = {
+            'blob_sha1_git': bytes.fromhex('d81cc0710eb6cf9efd5b920a8453e1'
+                                           'e07157b6cd'),
+            'tree_sha1_git': bytes.fromhex('ac212302c45eada382b27bfda795db'
+                                           '121dacdb1c'),
+            'commit_sha1_git': bytes.fromhex('e960570b2e6e2798fa4cfb9af2c399'
+                                             'd629189653'),
+            'tag_sha1_git': bytes.fromhex('bc2b99ba469987bcf1272c189ed534'
+                                          'e9e959f120'),
+        }
+
+    @istest
+    def unknown_header_type(self):
+        with self.assertRaises(ValueError) as cm:
+            hashutil.hash_git_data(b'any-data', 'some-unknown-type')
+
+        self.assertIn('Unexpected git object type', cm.exception.args[0])
+
+    @istest
+    def hashdata_content(self):
+        # when
+        actual_hash = hashutil.hash_git_data(self.blob_data, git_type='blob')
+
+        # then
+        self.assertEqual(actual_hash,
+                         self.checksums['blob_sha1_git'])
+
+    @istest
+    def hashdata_tree(self):
+        # when
+        actual_hash = hashutil.hash_git_data(self.tree_data, git_type='tree')
+
+        # then
+        self.assertEqual(actual_hash,
+                         self.checksums['tree_sha1_git'])
+
+    @istest
+    def hashdata_revision(self):
+        # when
+        actual_hash = hashutil.hash_git_data(self.commit_data,
+                                             git_type='commit')
+
+        # then
+        self.assertEqual(actual_hash,
+                         self.checksums['commit_sha1_git'])
+
+    @istest
+    def hashdata_tag(self):
+        # when
+        actual_hash = hashutil.hash_git_data(self.tag_data, git_type='tag')
+
+        # then
+        self.assertEqual(actual_hash,
+                         self.checksums['tag_sha1_git'])
diff --git a/swh/model/tests/test_identifiers.py b/swh/model/tests/test_identifiers.py
index 21552950..755dba60 100644
--- a/swh/model/tests/test_identifiers.py
+++ b/swh/model/tests/test_identifiers.py
@@ -1,4 +1,4 @@
-# Copyright (C) 2015  The Software Heritage developers
+# Copyright (C) 2015-2017  The Software Heritage developers
 # See the AUTHORS file at the top-level directory of this distribution
 # License: GNU General Public License version 3, or any later version
 # See top-level LICENSE file for more information
-- 
GitLab