From 9a903dc0a52722dcfe818c1657a47e4c4e95f6aa Mon Sep 17 00:00:00 2001
From: Valentin Lorentz <vlorentz@softwareheritage.org>
Date: Wed, 5 Aug 2020 19:49:13 +0200
Subject: [PATCH] tarball: add test for permissions.

---
 swh/core/tests/test_tarball.py | 52 ++++++++++++++++++++++++++++++++++
 1 file changed, 52 insertions(+)

diff --git a/swh/core/tests/test_tarball.py b/swh/core/tests/test_tarball.py
index ab432e67..3c0449f1 100644
--- a/swh/core/tests/test_tarball.py
+++ b/swh/core/tests/test_tarball.py
@@ -44,6 +44,35 @@ def test_compress_uncompress_zip(tmp_path):
     assert ["file%s.txt" % i for i in range(10)] == lsdir
 
 
+@pytest.mark.xfail(
+    reason=(
+        "Python's zipfile library doesn't support Info-ZIP's "
+        "extension for file permissions."
+    )
+)
+def test_compress_uncompress_zip_modes(tmp_path):
+    tocompress = tmp_path / "compressme"
+    tocompress.mkdir()
+
+    fpath = tocompress / "text.txt"
+    fpath.write_text("echo foo")
+    fpath.chmod(0o644)
+
+    fpath = tocompress / "executable.sh"
+    fpath.write_text("echo foo")
+    fpath.chmod(0o755)
+
+    zipfile = tmp_path / "archive.zip"
+    tarball.compress(str(zipfile), "zip", str(tocompress))
+
+    destdir = tmp_path / "destdir"
+    tarball.uncompress(str(zipfile), str(destdir))
+
+    (executable_path, text_path) = sorted(destdir.iterdir())
+    assert text_path.stat().st_mode == 0o100644  # succeeds, it's the default
+    assert executable_path.stat().st_mode == 0o100755  # fails
+
+
 def test_compress_uncompress_tar(tmp_path):
     tocompress = tmp_path / "compressme"
     tocompress.mkdir()
@@ -62,6 +91,29 @@ def test_compress_uncompress_tar(tmp_path):
     assert ["file%s.txt" % i for i in range(10)] == lsdir
 
 
+def test_compress_uncompress_tar_modes(tmp_path):
+    tocompress = tmp_path / "compressme"
+    tocompress.mkdir()
+
+    fpath = tocompress / "text.txt"
+    fpath.write_text("echo foo")
+    fpath.chmod(0o644)
+
+    fpath = tocompress / "executable.sh"
+    fpath.write_text("echo foo")
+    fpath.chmod(0o755)
+
+    tarfile = tmp_path / "archive.tar"
+    tarball.compress(str(tarfile), "tar", str(tocompress))
+
+    destdir = tmp_path / "destdir"
+    tarball.uncompress(str(tarfile), str(destdir))
+
+    (executable_path, text_path) = sorted(destdir.iterdir())
+    assert text_path.stat().st_mode == 0o100644
+    assert executable_path.stat().st_mode == 0o100755
+
+
 def test__unpack_tar_failure(tmp_path, datadir):
     """Unpack inexistent tarball should fail
 
-- 
GitLab