github.com/keybase/client/go@v0.0.0-20241007131713-f10651d043c8/updater/util/unzip_nix_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  //go:build !windows
     5  // +build !windows
     6  
     7  package util
     8  
     9  import (
    10  	"os"
    11  	"os/user"
    12  	"path/filepath"
    13  	"runtime"
    14  	"strconv"
    15  	"syscall"
    16  	"testing"
    17  	"time"
    18  
    19  	"github.com/stretchr/testify/assert"
    20  	"github.com/stretchr/testify/require"
    21  )
    22  
    23  // TestUnzipOtherUser checks to make sure that a zip file created from a
    24  // different uid has the current uid after unpacking.
    25  func TestUnzipOtherUser(t *testing.T) {
    26  	if runtime.GOOS == "windows" {
    27  		t.Skip("Unsupported on windows")
    28  	}
    29  	_, filename, _, _ := runtime.Caller(0)
    30  	testZipOtherUserPath := filepath.Join(filepath.Dir(filename), "../test/test-uid-503.zip")
    31  	destinationPath := TempPath("", "TestUnzipOtherUser.")
    32  	err := Unzip(testZipOtherUserPath, destinationPath, testLog)
    33  	require.NoError(t, err)
    34  
    35  	// Get uid, gid of current user
    36  	currentUser, err := user.Current()
    37  	require.NoError(t, err)
    38  	uid, err := strconv.Atoi(currentUser.Uid)
    39  	require.NoError(t, err)
    40  
    41  	fileInfo, err := os.Stat(filepath.Join(destinationPath, "test"))
    42  	require.NoError(t, err)
    43  	fileUID := fileInfo.Sys().(*syscall.Stat_t).Uid
    44  	assert.Equal(t, uid, int(fileUID))
    45  }
    46  
    47  // TestUnzipFileModTime checks to make sure after unpacking zip file the file
    48  // modification time is "now" and not the original file time.
    49  func TestUnzipFileModTime(t *testing.T) {
    50  	// Fudge now a bit, since the timestamps below on Linux seem
    51  	// to happen a bit *before* now.
    52  	now := time.Now().Add(-time.Second)
    53  	t.Logf("Now: %s", now)
    54  	destinationPath := TempPath("", "TestUnzipFileModTime.")
    55  	err := Unzip(testZipPath, destinationPath, testLog)
    56  	require.NoError(t, err)
    57  
    58  	fileInfo, err := os.Stat(filepath.Join(destinationPath, "test"))
    59  	require.NoError(t, err)
    60  	dirMod := fileInfo.ModTime()
    61  	diffDir := dirMod.Sub(now)
    62  	t.Logf("Diff (dir): %s", diffDir)
    63  	assert.True(t, diffDir >= 0, "now=%s, dirtime=%s", now, dirMod)
    64  
    65  	fileInfo, err = os.Stat(filepath.Join(destinationPath, "test", "testfile"))
    66  	require.NoError(t, err)
    67  	fileMod := fileInfo.ModTime()
    68  	diffFile := fileMod.Sub(now)
    69  	t.Logf("Diff (file): %s", diffFile)
    70  	assert.True(t, diffFile >= 0, "now=%s, filetime=%s", now, fileMod)
    71  }