Skip to content

Commit

Permalink
dropped uid/gid from upper test case level and aligned the test case
Browse files Browse the repository at this point in the history
  • Loading branch information
NodyHub committed Jan 8, 2025
1 parent f59ea35 commit 6c8d47a
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 21 deletions.
12 changes: 0 additions & 12 deletions unpack_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1611,8 +1611,6 @@ var testCases = []struct {
doesNotSupportModTime bool
doesNotSupportOwner bool
expectError bool
uid int
gid int
}{
{
name: "tar",
Expand All @@ -1622,8 +1620,6 @@ var testCases = []struct {
{Name: "sub/test", Content: []byte("hello world"), Mode: 0777, AccessTime: baseTime, ModTime: baseTime, Uid: testDataUid, Gid: testDataGid},
{Name: "link", Mode: fs.ModeSymlink | 0777, Linktarget: "sub/test", AccessTime: baseTime, ModTime: baseTime, Uid: testDataUid, Gid: testDataGid},
},
uid: testDataUid,
gid: testDataGid,
packer: packTar,
},
{
Expand All @@ -1634,8 +1630,6 @@ var testCases = []struct {
{Name: "sub/test", Content: []byte("hello world"), Mode: 0777, AccessTime: baseTime, ModTime: baseTime, Uid: testDataRootUid, Gid: testDataWheelGid},
{Name: "link", Mode: fs.ModeSymlink | 0777, Linktarget: "sub/test", AccessTime: baseTime, ModTime: baseTime, Uid: testDataRootUid, Gid: testDataWheelGid},
},
uid: testDataRootUid,
gid: testDataWheelGid,
packer: packTar,
},
{
Expand All @@ -1646,25 +1640,19 @@ var testCases = []struct {
{Name: "sub/test", Content: []byte("hello world"), Mode: 0644, AccessTime: baseTime, ModTime: baseTime, Uid: os.Getuid(), Gid: os.Getgid()},
{Name: "link", Mode: fs.ModeSymlink | 0777, Linktarget: "sub/test", AccessTime: baseTime, ModTime: baseTime, Uid: os.Getuid(), Gid: os.Getgid()},
},
uid: os.Getgid(), // bc/ of the way zip works, we can't set the uid and the created files are owned by the user running the test
gid: os.Getgid(), // bc/ of the way zip works, we can't set the gid and the created files are owned by the user running the test
doesNotSupportOwner: true,
packer: packZip,
},
{
name: "rar",
contents: contentsRar2,
uid: os.Getuid(), // bc/ of the way rar works, we can't set the uid and the created files are owned by the user running the test
gid: os.Getgid(), // bc/ of the way rar works, we can't set the gid and the created files are owned by the user running the test
doesNotSupportOwner: true,
doesNotSupportModTime: true,
packer: packRar2,
},
{
name: "7z",
contents: contents7z2,
uid: os.Getuid(), // bc/ of the way 7z works, we can't set the uid and the created files are owned by the user running the test
gid: os.Getgid(), // bc/ of the way 7z works, we can't set the gid and the created files are owned by the user running the test
doesNotSupportOwner: true,
packer: pack7z2,
},
Expand Down
11 changes: 2 additions & 9 deletions unpack_unix_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,21 +66,14 @@ func TestUnpackWithPreserveOwnershipAsNonRoot(t *testing.T) {
src = asIoReader(t, tc.packer(t, tc.contents))
cfg = extract.NewConfig(extract.WithPreserveOwner(true))
)
// compare archive uid/gid with current user
archiveEntriesEqualCurrentOwner := tc.doesNotSupportOwner

// If the archive supports owner information, check if the current user is the owner of the test data
if !tc.doesNotSupportOwner && (os.Getuid() != tc.uid || os.Getgid() != tc.gid) {
archiveEntriesEqualCurrentOwner = false
}

// Unpack should fail if the user is not root and the uid/gid
// in the archive is different from the current user (only
// if the archive supports owner information)
err := extract.Unpack(ctx, dst, src, cfg)

// chown will only fail if the user is not root and the uid/gid is different
if archiveEntriesEqualCurrentOwner && err != nil {
// chown will only fail if the user is not root
if !tc.doesNotSupportOwner && err == nil {
t.Fatalf("error unpacking archive: %v", err)
}
})
Expand Down

0 comments on commit 6c8d47a

Please sign in to comment.