Skip to contentSkip to conversation

tests/functional/qemu_test: Use Python hashlib instead of external programs

PATCH In review Thomas Huth 3 participants 5 messages Sep 10, 2024 – Sep 16, 2024

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 tests/functional/qemu_test: Use Python hashlib instead of external programs

Some systems (like OpenBSD) do not have the sha256sum or sha512sum programs installed by default. Use the Python hashlib instead so we don't have to rely on the external programs.

---
 tests/functional/qemu_test/asset.py | 16 +++++++++++-----
 1 file changed, 11 insertions(+), 5 deletions(-)
tests/functional/qemu_test/asset.py +11 -5
index d3be2aff82..3ec429217e 100644
@@ -43,15 +43,21 @@ def _check(self, cache_file):
         if self.hash is None:
             return True
         if len(self.hash) == 64:
-            sum_prog = 'sha256sum'
+            hl = hashlib.sha256()
         elif len(self.hash) == 128:
-            sum_prog = 'sha512sum'
+            hl = hashlib.sha512()
         else:
             raise Exception("unknown hash type")
 
-        checksum = subprocess.check_output(
-            [sum_prog, str(cache_file)]).split()[0]
-        return self.hash == checksum.decode("utf-8")
+        # Calculate the hash of the file:
+        with open(cache_file, 'rb') as file:
+            while True:
+                chunk = file.read(1 << 20)
+                if not chunk:
+                    break
+                hl.update(chunk)
+
+        return  hl.hexdigest()
 
     def valid(self):
         return self.cache_file.exists() and self._check(self.cache_file)
Signature · 1 line
2.46.0
#1 lore raw
Quoted 3 lines · Thomas Huth

Some systems (like OpenBSD) do not have the sha256sum or sha512sum programs installed by default. Use the Python hashlib instead so we don't have to rely on the external programs.

On OpenBSD they're named sha256 and sha512.

Quoted 37 lines
---
  tests/functional/qemu_test/asset.py | 16 +++++++++++-----
  1 file changed, 11 insertions(+), 5 deletions(-)
tests/functional/qemu_test/asset.py +11 -5
index d3be2aff82..3ec429217e 100644
@@ -43,15 +43,21 @@ def _check(self, cache_file):
          if self.hash is None:
              return True
          if len(self.hash) == 64:
-            sum_prog = 'sha256sum'
+            hl = hashlib.sha256()
          elif len(self.hash) == 128:
-            sum_prog = 'sha512sum'
+            hl = hashlib.sha512()
          else:
              raise Exception("unknown hash type")
 
-        checksum = subprocess.check_output(
-            [sum_prog, str(cache_file)]).split()[0]
-        return self.hash == checksum.decode("utf-8")
+        # Calculate the hash of the file:
+        with open(cache_file, 'rb') as file:
+            while True:
+                chunk = file.read(1 << 20)
+                if not chunk:
+                    break
+                hl.update(chunk)
+
+        return  hl.hexdigest()
 
      def valid(self):
          return self.cache_file.exists() and self._check(self.cache_file)
Reviewed-by
#2 lore raw
Quoted 7 lines · Brad Smith
Quoted 3 lines · Thomas Huth

Some systems (like OpenBSD) do not have the sha256sum or sha512sum programs installed by default. Use the Python hashlib instead so we don't have to rely on the external programs.

On OpenBSD they're named sha256 and sha512.

Rather than port the test to each OS's particular program names, we should use the portable solution that's included w/Python.

Quoted 3 lines
Quoted 3 lines

---

Quoted 33 lines
Quoted 32 lines

  tests/functional/qemu_test/asset.py | 16 +++++++++++-----

  1 file changed, 11 insertions(+), 5 deletions(-)
tests/functional/qemu_test/asset.py +0 -0
index d3be2aff82..3ec429217e 100644
@@ -43,15 +43,21 @@ def _check(self, cache_file):

          if self.hash is None:
              return True
          if len(self.hash) == 64:
-            sum_prog = 'sha256sum'
+            hl = hashlib.sha256()
          elif len(self.hash) == 128:
-            sum_prog = 'sha512sum'
+            hl = hashlib.sha512()
          else:
              raise Exception("unknown hash type")
  -        checksum = subprocess.check_output(
-            [sum_prog, str(cache_file)]).split()[0]
-        return self.hash == checksum.decode("utf-8")
+        # Calculate the hash of the file:
+        with open(cache_file, 'rb') as file:
+            while True:
+                chunk = file.read(1 << 20)
+                if not chunk:
+                    break
+                hl.update(chunk)
+
+        return  hl.hexdigest()
        def valid(self):
          return self.cache_file.exists() and self._check(self.cache_file)

#3 lore raw
Quoted 10 lines · Brian Cain
Quoted 7 lines · Brad Smith
Quoted 3 lines · Thomas Huth

Some systems (like OpenBSD) do not have the sha256sum or sha512sum programs installed by default. Use the Python hashlib instead so we don't have to rely on the external programs.

On OpenBSD they're named sha256 and sha512.

Rather than port the test to each OS's particular program names, we should use the portable solution that's included w/Python.

I wasn't trying to imply that the patch wasn't the right direction, it is when it comes portability. Just commenting on what it says in the commit message. This isn't the first time such differences have come up.

#4 lore raw
Quoted 8 lines · Brad Smith
Quoted 7 lines · Brian Cain
Quoted 5 lines · Brad Smith
Quoted 3 lines · Thomas Huth

Some systems (like OpenBSD) do not have the sha256sum or sha512sum programs installed by default. Use the Python hashlib instead so we don't have to rely on the external programs.

Quoted 7 lines
Quoted 3 lines
Quoted 2 lines

On OpenBSD they're named sha256 and sha512.

Rather than port the test to each OS's particular program names, we should use the portable solution that's included w/Python.

I wasn't trying to imply that the patch wasn't the right direction, it is when it comes portability. Just commenting on what it says in the commit message. This isn't the first time such differences have come up.

I see.  Good point.