github.com/keybase/client/go@v0.0.0-20240309051027-028f7c731f8b/updater/util/unzip_test.go (about)

     1  // Copyright 2015 Keybase, Inc. All rights reserved. Use of
     2  // this source code is governed by the included BSD license.
     3  
     4  package util
     5  
     6  import (
     7  	"fmt"
     8  	"path/filepath"
     9  	"runtime"
    10  	"testing"
    11  
    12  	"github.com/stretchr/testify/assert"
    13  	"github.com/stretchr/testify/require"
    14  )
    15  
    16  var testZipPath, testSymZipPath, testCorruptedZipPath, testInvalidZipPath string
    17  
    18  func init() {
    19  	_, filename, _, _ := runtime.Caller(0)
    20  	// testZipPath is a valid zip file
    21  	testZipPath = filepath.Join(filepath.Dir(filename), "../test/test.zip")
    22  	// testSymZipPath is a valid zip file with a symbolic link
    23  	testSymZipPath = filepath.Join(filepath.Dir(filename), "../test/test-with-sym.zip")
    24  	// testCorruptedZipPath is a corrupted zip file (flipped a bit)
    25  	testCorruptedZipPath = filepath.Join(filepath.Dir(filename), "../test/test-corrupted2.zip")
    26  	// testInvalidZipPath is not a valid zip file
    27  	testInvalidZipPath = filepath.Join(filepath.Dir(filename), "../test/test-invalid.zip")
    28  }
    29  
    30  func assertFileExists(t *testing.T, path string) {
    31  	t.Logf("Checking %s", path)
    32  	fileExists, err := FileExists(path)
    33  	assert.NoError(t, err)
    34  	assert.True(t, fileExists)
    35  }
    36  
    37  func testUnzipOverValid(t *testing.T, path string) string {
    38  	destinationPath := TempPath("", "TestUnzipOver.")
    39  
    40  	noCheck := func(sourcePath, destinationPath string) error { return nil }
    41  
    42  	err := UnzipOver(path, "test", destinationPath, noCheck, "", testLog)
    43  	require.NoError(t, err)
    44  
    45  	dirExists, err := FileExists(destinationPath)
    46  	assert.NoError(t, err)
    47  	assert.True(t, dirExists)
    48  
    49  	assertFileExists(t, filepath.Join(destinationPath, "testfile"))
    50  	assertFileExists(t, filepath.Join(destinationPath, "testfolder"))
    51  	assertFileExists(t, filepath.Join(destinationPath, "testfolder", "testsubfolder"))
    52  	assertFileExists(t, filepath.Join(destinationPath, "testfolder", "testsubfolder", "testfile2"))
    53  
    54  	// Unzip again over existing path
    55  	err = UnzipOver(path, "test", destinationPath, noCheck, "", testLog)
    56  	require.NoError(t, err)
    57  
    58  	dirExists2, err := FileExists(destinationPath)
    59  	require.NoError(t, err)
    60  	require.True(t, dirExists2)
    61  
    62  	fileExists2, err := FileExists(filepath.Join(destinationPath, "testfile"))
    63  	require.NoError(t, err)
    64  	require.True(t, fileExists2)
    65  
    66  	// Unzip again over existing path, fail check
    67  	failCheck := func(sourcePath, destinationPath string) error { return fmt.Errorf("Failed check") }
    68  	err = UnzipOver(testZipPath, "test", destinationPath, failCheck, "", testLog)
    69  	assert.Error(t, err)
    70  
    71  	return destinationPath
    72  }
    73  
    74  func TestUnzipOverValid(t *testing.T) {
    75  	destinationPath := testUnzipOverValid(t, testZipPath)
    76  	defer RemoveFileAtPath(destinationPath)
    77  }
    78  
    79  func TestUnzipOverSymlink(t *testing.T) {
    80  	if runtime.GOOS == "windows" {
    81  		t.Skip("Symlink in zip unsupported on Windows")
    82  	}
    83  	destinationPath := testUnzipOverValid(t, testSymZipPath)
    84  	defer RemoveFileAtPath(destinationPath)
    85  	assertFileExists(t, filepath.Join(destinationPath, "testfolder", "testlink"))
    86  }
    87  
    88  func TestUnzipOverInvalidPath(t *testing.T) {
    89  	noCheck := func(sourcePath, destinationPath string) error { return nil }
    90  	err := UnzipOver(testZipPath, "test", "", noCheck, "", testLog)
    91  	assert.Error(t, err)
    92  
    93  	destinationPath := TempPath("", "TestUnzipOverInvalidPath.")
    94  	defer RemoveFileAtPath(destinationPath)
    95  	err = UnzipOver("/badfile.zip", "test", destinationPath, noCheck, "", testLog)
    96  	assert.Error(t, err)
    97  
    98  	err = UnzipOver("", "test", destinationPath, noCheck, "", testLog)
    99  	assert.Error(t, err)
   100  
   101  	err = unzipOver("", "", testLog)
   102  	assert.Error(t, err)
   103  }
   104  
   105  func TestUnzipOverInvalidZip(t *testing.T) {
   106  	noCheck := func(sourcePath, destinationPath string) error { return nil }
   107  	destinationPath := TempPath("", "TestUnzipOverInvalidZip.")
   108  	defer RemoveFileAtPath(destinationPath)
   109  	err := UnzipOver(testInvalidZipPath, "test", destinationPath, noCheck, "", testLog)
   110  	t.Logf("Error: %s", err)
   111  	assert.Error(t, err)
   112  }
   113  
   114  func TestUnzipOverInvalidContents(t *testing.T) {
   115  	noCheck := func(sourcePath, destinationPath string) error { return nil }
   116  	destinationPath := TempPath("", "TestUnzipOverInvalidContents.")
   117  	defer RemoveFileAtPath(destinationPath)
   118  	err := UnzipOver(testInvalidZipPath, "invalid", destinationPath, noCheck, "", testLog)
   119  	t.Logf("Error: %s", err)
   120  	assert.Error(t, err)
   121  }
   122  
   123  func TestUnzipOverCorrupted(t *testing.T) {
   124  	noCheck := func(sourcePath, destinationPath string) error { return nil }
   125  	destinationPath := TempPath("", "TestUnzipOverCorrupted.")
   126  	defer RemoveFileAtPath(destinationPath)
   127  	err := UnzipOver(testCorruptedZipPath, "test", destinationPath, noCheck, "", testLog)
   128  	t.Logf("Error: %s", err)
   129  	assert.Error(t, err)
   130  }
   131  
   132  func tempDir(t *testing.T) string {
   133  	tmpDir := TempPath("", "TestUnzipOver")
   134  	err := MakeDirs(tmpDir, 0700, testLog)
   135  	require.NoError(t, err)
   136  	return tmpDir
   137  }
   138  
   139  func TestUnzipOverMoveExisting(t *testing.T) {
   140  	noCheck := func(sourcePath, destinationPath string) error { return nil }
   141  	destinationPath := TempPath("", "TestUnzipOverMoveExisting.")
   142  	defer RemoveFileAtPath(destinationPath)
   143  	tmpDir := tempDir(t)
   144  	defer RemoveFileAtPath(tmpDir)
   145  	err := UnzipOver(testZipPath, "test", destinationPath, noCheck, tmpDir, testLog)
   146  	assert.NoError(t, err)
   147  	err = UnzipOver(testZipPath, "test", destinationPath, noCheck, tmpDir, testLog)
   148  	assert.NoError(t, err)
   149  
   150  	assertFileExists(t, filepath.Join(tmpDir, filepath.Base(destinationPath)))
   151  }