github.com/mondo192/jfrog-client-go@v1.0.0/utils/io/fileutils/archive_test.go (about)

     1  package fileutils
     2  
     3  import (
     4  	"path/filepath"
     5  	"testing"
     6  
     7  	"github.com/stretchr/testify/assert"
     8  )
     9  
    10  func TestUnarchive(t *testing.T) {
    11  	tests := []string{"zip", "tar", "tar.gz"}
    12  	for _, extension := range tests {
    13  		t.Run(extension, func(t *testing.T) {
    14  			// Create temp directory
    15  			tmpDir, createTempDirCallback := createTempDirWithCallbackAndAssert(t)
    16  			defer createTempDirCallback()
    17  			// Run unarchive on archive created on Unix
    18  			err := runUnarchive("unix."+extension, "archives", filepath.Join(tmpDir, "unix"))
    19  			assert.NoError(t, err)
    20  			assert.FileExists(t, filepath.Join(tmpDir, "unix", "link"))
    21  			assert.FileExists(t, filepath.Join(tmpDir, "unix", "dir", "file"))
    22  
    23  			// Run unarchive on archive created on Windows
    24  			err = runUnarchive("win."+extension, "archives", filepath.Join(tmpDir, "win"))
    25  			assert.NoError(t, err)
    26  			assert.FileExists(t, filepath.Join(tmpDir, "win", "link.lnk"))
    27  			assert.FileExists(t, filepath.Join(tmpDir, "win", "dir", "file.txt"))
    28  		})
    29  	}
    30  }
    31  
    32  func TestUnarchiveSymlink(t *testing.T) {
    33  	tests := []string{"zip", "tar", "tar.gz"}
    34  	for _, extension := range tests {
    35  		t.Run(extension, func(t *testing.T) {
    36  			// Create temp directory
    37  			tmpDir, createTempDirCallback := createTempDirWithCallbackAndAssert(t)
    38  			defer createTempDirCallback()
    39  
    40  			// Run unarchive
    41  			err := runUnarchive("softlink-rel."+extension, "archives", tmpDir)
    42  			assert.NoError(t, err)
    43  			assert.FileExists(t, filepath.Join(tmpDir, "softlink-rel", "a", "softlink-rel"))
    44  			assert.FileExists(t, filepath.Join(tmpDir, "softlink-rel", "b", "c", "d", "file"))
    45  		})
    46  	}
    47  }
    48  
    49  func TestUnarchiveZipSlip(t *testing.T) {
    50  	tests := []struct {
    51  		testType    string
    52  		archives    []string
    53  		errorSuffix string
    54  	}{
    55  		{"rel", []string{"zip", "tar", "tar.gz"}, "illegal path in archive: '../file'"},
    56  		{"abs", []string{"tar", "tar.gz"}, "illegal path in archive: '/tmp/bla/file'"},
    57  		{"softlink-abs", []string{"zip", "tar", "tar.gz"}, "illegal link path in archive: '/tmp/bla/file'"},
    58  		{"softlink-rel", []string{"zip", "tar", "tar.gz"}, "illegal link path in archive: '../../file'"},
    59  		{"hardlink-tilde", []string{"tar", "tar.gz"}, "walking hardlink: illegal link path in archive: '~/../../../../../../../../../Users/Shared/sharedFile.txt'"},
    60  	}
    61  	for _, test := range tests {
    62  		t.Run(test.testType, func(t *testing.T) {
    63  			// Create temp directory
    64  			tmpDir, createTempDirCallback := createTempDirWithCallbackAndAssert(t)
    65  			defer createTempDirCallback()
    66  			for _, archive := range test.archives {
    67  				// Unarchive and make sure an error returns
    68  				err := runUnarchive(test.testType+"."+archive, "zipslip", tmpDir)
    69  				assert.Error(t, err)
    70  				assert.Contains(t, err.Error(), test.errorSuffix)
    71  			}
    72  		})
    73  	}
    74  }
    75  
    76  func runUnarchive(archiveFileName, sourceDir, targetDir string) error {
    77  	return Unarchive(filepath.Join("testdata", sourceDir, archiveFileName), archiveFileName, targetDir)
    78  }
    79  
    80  func createTempDirWithCallbackAndAssert(t *testing.T) (string, func()) {
    81  	tempDirPath, err := CreateTempDir()
    82  	assert.NoError(t, err, "Couldn't create temp dir")
    83  	return tempDirPath, func() {
    84  		assert.NoError(t, RemoveTempDir(tempDirPath), "Couldn't remove temp dir")
    85  	}
    86  }