Skip to content

Commit

Permalink
add TestDecompressorPermissions, test it with tar & zip
Browse files Browse the repository at this point in the history
  • Loading branch information
langmartin committed Aug 15, 2019
1 parent 699e076 commit 96cca11
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 0 deletions.
49 changes: 49 additions & 0 deletions decompress_tar_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package getter

import (
"io/ioutil"
"os"
"path/filepath"
"testing"
"time"
Expand Down Expand Up @@ -41,3 +43,50 @@ func TestTar(t *testing.T) {

TestDecompressor(t, new(tarDecompressor), cases)
}

// testDecompressPermissions decompresses a directory and checks the permissions of the expanded files
func testDecompressorPermissions(t *testing.T, d Decompressor, input string, expected map[string]int, umask os.FileMode) {
td, err := ioutil.TempDir("", "getter")
if err != nil {
t.Fatalf("err: %s", err)
}

// Destination is always joining result so that we have a new path
dst := filepath.Join(td, "subdir", "result")

err = d.Decompress(dst, input, true, umask)
if err != nil {
t.Fatalf("err: %s", err)
}

defer os.RemoveAll(dst)

for name, mode := range expected {
fi, err := os.Stat(filepath.Join(dst, name))
if err != nil {
t.Fatalf("err: %s", err)
}

real := fi.Mode()
if real != os.FileMode(mode) {
t.Fatalf("err: %s expected mode %o got %o", name, mode, real)
}
}
}

func TestDecompressTarPermissions(t *testing.T) {
d := new(tarDecompressor)
input := "./test-fixtures/decompress-tar/permissions.tar"
expected := map[string]int{
"directory/public": 0666,
"directory/private": 0600,
"directory/zero": 0,
"directory/exec": 0755,
"directory/setuid": 040000755,
}

testDecompressorPermissions(t, d, input, expected, os.FileMode(0))

expected["directory/setuid"] = 0755
testDecompressorPermissions(t, d, input, expected, os.FileMode(060000000))
}
19 changes: 19 additions & 0 deletions decompress_zip_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package getter

import (
"os"
"path/filepath"
"testing"
)
Expand Down Expand Up @@ -96,3 +97,21 @@ func TestZipDecompressor(t *testing.T) {

TestDecompressor(t, new(ZipDecompressor), cases)
}

func TestDecompressZipPermissions(t *testing.T) {
d := new(ZipDecompressor)
input := "./test-fixtures/decompress-zip/permissions.zip"
expected := map[string]int{
"directory/public": 0666,
"directory/private": 0600,
// zip won't add this file, since it can't be read
// "directory/zero": 0,
"directory/exec": 0755,
"directory/setuid": 040000755,
}

testDecompressorPermissions(t, d, input, expected, os.FileMode(0))

expected["directory/setuid"] = 0755
testDecompressorPermissions(t, d, input, expected, os.FileMode(060000000))
}
Binary file added test-fixtures/decompress-tar/permissions.tar
Binary file not shown.
Binary file added test-fixtures/decompress-zip/permissions.zip
Binary file not shown.

0 comments on commit 96cca11

Please sign in to comment.